blob: afe48bfffe8508330275f131c42e0734a049d736 [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"
Eric Christopherb6c190d2019-04-12 06:16:33 +000025#include "llvm/ADT/MapVector.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"
Matthias Braunf8422972017-12-13 02:51:04 +000033#include "llvm/CodeGen/LiveIntervals.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"
David Blaikie3f833ed2017-11-08 01:01:31 +000042#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000043#include "llvm/CodeGen/TargetOpcodes.h"
44#include "llvm/CodeGen/TargetRegisterInfo.h"
45#include "llvm/CodeGen/TargetSubtargetInfo.h"
Jakob Stoklund Olesen26c9d702012-11-28 19:13:06 +000046#include "llvm/CodeGen/VirtRegMap.h"
Nico Weber432a3882018-04-30 14:59:11 +000047#include "llvm/Config/llvm-config.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000048#include "llvm/IR/DebugInfoMetadata.h"
49#include "llvm/IR/DebugLoc.h"
50#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000051#include "llvm/IR/Metadata.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000052#include "llvm/MC/MCRegisterInfo.h"
53#include "llvm/Pass.h"
54#include "llvm/Support/Casting.h"
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000055#include "llvm/Support/CommandLine.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000056#include "llvm/Support/Compiler.h"
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000057#include "llvm/Support/Debug.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000058#include "llvm/Support/raw_ostream.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000059#include <algorithm>
60#include <cassert>
61#include <iterator>
David Blaikie2b1dfa72014-04-21 20:37:07 +000062#include <memory>
Benjamin Kramer82de7d32016-05-27 14:27:24 +000063#include <utility>
David Blaikie2b1dfa72014-04-21 20:37:07 +000064
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000065using namespace llvm;
66
Matthias Braun1527baa2017-05-25 21:26:32 +000067#define DEBUG_TYPE "livedebugvars"
Chandler Carruth1b9dde02014-04-22 02:02:50 +000068
Devang Patelacbee0b2011-01-07 22:33:41 +000069static cl::opt<bool>
Jakob Stoklund Olesen74ded572011-01-12 23:36:21 +000070EnableLDV("live-debug-variables", cl::init(true),
Devang Patelacbee0b2011-01-07 22:33:41 +000071 cl::desc("Enable the live debug variables pass"), cl::Hidden);
72
Devang Patelb4568662011-08-04 18:45:38 +000073STATISTIC(NumInsertedDebugValues, "Number of DBG_VALUEs inserted");
Hsiangkai Wang66609a82019-01-18 07:17:09 +000074STATISTIC(NumInsertedDebugLabels, "Number of DBG_LABELs inserted");
Eugene Zelenko5df3d892017-08-24 21:21:39 +000075
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000076char LiveDebugVariables::ID = 0;
77
Matthias Braun1527baa2017-05-25 21:26:32 +000078INITIALIZE_PASS_BEGIN(LiveDebugVariables, DEBUG_TYPE,
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000079 "Debug Variable Analysis", false, false)
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000080INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000081INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
Matthias Braun1527baa2017-05-25 21:26:32 +000082INITIALIZE_PASS_END(LiveDebugVariables, DEBUG_TYPE,
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000083 "Debug Variable Analysis", false, false)
84
85void LiveDebugVariables::getAnalysisUsage(AnalysisUsage &AU) const {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000086 AU.addRequired<MachineDominatorTree>();
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000087 AU.addRequiredTransitive<LiveIntervals>();
88 AU.setPreservesAll();
89 MachineFunctionPass::getAnalysisUsage(AU);
90}
91
Eugene Zelenko5df3d892017-08-24 21:21:39 +000092LiveDebugVariables::LiveDebugVariables() : MachineFunctionPass(ID) {
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000093 initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
94}
95
Reid Kleckner04e25e02017-10-03 17:59:02 +000096enum : unsigned { UndefLocNo = ~0U };
97
98/// Describes a location by number along with some flags about the original
99/// usage of the location.
100class DbgValueLocation {
101public:
102 DbgValueLocation(unsigned LocNo, bool WasIndirect)
103 : LocNo(LocNo), WasIndirect(WasIndirect) {
104 static_assert(sizeof(*this) == sizeof(unsigned), "bad bitfield packing");
105 assert(locNo() == LocNo && "location truncation");
106 }
107
108 DbgValueLocation() : LocNo(0), WasIndirect(0) {}
109
110 unsigned locNo() const {
111 // Fix up the undef location number, which gets truncated.
112 return LocNo == INT_MAX ? UndefLocNo : LocNo;
113 }
114 bool wasIndirect() const { return WasIndirect; }
115 bool isUndef() const { return locNo() == UndefLocNo; }
116
117 DbgValueLocation changeLocNo(unsigned NewLocNo) const {
118 return DbgValueLocation(NewLocNo, WasIndirect);
119 }
120
Reid Klecknerb4569de72017-10-03 18:30:11 +0000121 friend inline bool operator==(const DbgValueLocation &LHS,
122 const DbgValueLocation &RHS) {
123 return LHS.LocNo == RHS.LocNo && LHS.WasIndirect == RHS.WasIndirect;
Reid Kleckner04e25e02017-10-03 17:59:02 +0000124 }
Reid Klecknerb4569de72017-10-03 18:30:11 +0000125
126 friend inline bool operator!=(const DbgValueLocation &LHS,
127 const DbgValueLocation &RHS) {
128 return !(LHS == RHS);
129 }
Reid Kleckner04e25e02017-10-03 17:59:02 +0000130
131private:
132 unsigned LocNo : 31;
133 unsigned WasIndirect : 1;
134};
135
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000136/// Map of where a user value is live, and its location.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000137using LocMap = IntervalMap<SlotIndex, DbgValueLocation, 4>;
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000138
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000139/// Map of stack slot offsets for spilled locations.
David Stenberg45acc962018-09-07 13:54:07 +0000140/// Non-spilled locations are not added to the map.
141using SpillOffsetMap = DenseMap<unsigned, unsigned>;
142
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000143namespace {
144
145class LDVImpl;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000146
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000147/// A user value is a part of a debug info user variable.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000148///
149/// A DBG_VALUE instruction notes that (a sub-register of) a virtual register
150/// holds part of a user variable. The part is identified by a byte offset.
151///
152/// UserValues are grouped into equivalence classes for easier searching. Two
153/// user values are related if they refer to the same variable, or if they are
154/// held by the same virtual register. The equivalence class is the transitive
155/// closure of that relation.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000156class UserValue {
Reid Kleckner4e040282017-09-20 18:19:08 +0000157 const DILocalVariable *Variable; ///< The debug info variable we are part of.
158 const DIExpression *Expression; ///< Any complex address expression.
Devang Patel26ffa012011-02-04 01:43:25 +0000159 DebugLoc dl; ///< The debug location for the variable. This is
160 ///< used by dwarf writer to find lexical scope.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000161 UserValue *leader; ///< Equivalence class leader.
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000162 UserValue *next = nullptr; ///< Next value in equivalence class, or null.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000163
164 /// Numbered locations referenced by locmap.
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000165 SmallVector<MachineOperand, 4> locations;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000166
167 /// Map of slot indices where this value is live.
168 LocMap locInts;
169
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000170 /// Insert a DBG_VALUE into MBB at Idx for LocNo.
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +0000171 void insertDebugValue(MachineBasicBlock *MBB, SlotIndex StartIdx,
David Stenberg45acc962018-09-07 13:54:07 +0000172 SlotIndex StopIdx, DbgValueLocation Loc, bool Spilled,
173 unsigned SpillOffset, LiveIntervals &LIS,
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +0000174 const TargetInstrInfo &TII,
175 const TargetRegisterInfo &TRI);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000176
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000177 /// Replace OldLocNo ranges with NewRegs ranges where NewRegs
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000178 /// is live. Returns true if any changes were made.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000179 bool splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
180 LiveIntervals &LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000181
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000182public:
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000183 /// Create a new UserValue.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000184 UserValue(const DILocalVariable *var, const DIExpression *expr, DebugLoc L,
185 LocMap::Allocator &alloc)
186 : Variable(var), Expression(expr), dl(std::move(L)), leader(this),
187 locInts(alloc) {}
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000188
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000189 /// Get the leader of this value's equivalence class.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000190 UserValue *getLeader() {
191 UserValue *l = leader;
192 while (l != l->leader)
193 l = l->leader;
194 return leader = l;
195 }
196
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000197 /// Return the next UserValue in the equivalence class.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000198 UserValue *getNext() const { return next; }
199
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000200 /// Does this UserValue match the parameters?
Reid Kleckner4e040282017-09-20 18:19:08 +0000201 bool match(const DILocalVariable *Var, const DIExpression *Expr,
Reid Kleckner04e25e02017-10-03 17:59:02 +0000202 const DILocation *IA) const {
203 // FIXME: The fragment should be part of the equivalence class, but not
204 // other things in the expression like stack values.
205 return Var == Variable && Expr == Expression && dl->getInlinedAt() == IA;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000206 }
207
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000208 /// Merge equivalence classes.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000209 static UserValue *merge(UserValue *L1, UserValue *L2) {
210 L2 = L2->getLeader();
211 if (!L1)
212 return L2;
213 L1 = L1->getLeader();
214 if (L1 == L2)
215 return L1;
216 // Splice L2 before L1's members.
217 UserValue *End = L2;
Richard Trieu7a083812016-02-18 22:09:30 +0000218 while (End->next) {
219 End->leader = L1;
220 End = End->next;
221 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000222 End->leader = L1;
223 End->next = L1->next;
224 L1->next = L2;
225 return L1;
226 }
227
Mikael Holmen57b33f62018-06-21 07:02:46 +0000228 /// Return the location number that matches Loc.
229 ///
230 /// For undef values we always return location number UndefLocNo without
231 /// inserting anything in locations. Since locations is a vector and the
232 /// location number is the position in the vector and UndefLocNo is ~0,
233 /// we would need a very big vector to put the value at the right position.
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000234 unsigned getLocationNo(const MachineOperand &LocMO) {
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000235 if (LocMO.isReg()) {
236 if (LocMO.getReg() == 0)
Reid Klecknereed09732017-09-15 22:08:50 +0000237 return UndefLocNo;
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000238 // For register locations we dont care about use/def and other flags.
239 for (unsigned i = 0, e = locations.size(); i != e; ++i)
240 if (locations[i].isReg() &&
241 locations[i].getReg() == LocMO.getReg() &&
242 locations[i].getSubReg() == LocMO.getSubReg())
243 return i;
244 } else
245 for (unsigned i = 0, e = locations.size(); i != e; ++i)
246 if (LocMO.isIdenticalTo(locations[i]))
247 return i;
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000248 locations.push_back(LocMO);
249 // We are storing a MachineOperand outside a MachineInstr.
250 locations.back().clearParent();
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000251 // Don't store def operands.
Geoff Berryb3d126d2017-12-29 21:01:09 +0000252 if (locations.back().isReg()) {
253 if (locations.back().isDef())
254 locations.back().setIsDead(false);
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000255 locations.back().setIsUse();
Geoff Berryb3d126d2017-12-29 21:01:09 +0000256 }
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000257 return locations.size() - 1;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000258 }
259
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000260 /// Ensure that all virtual register locations are mapped.
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000261 void mapVirtRegs(LDVImpl *LDV);
262
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000263 /// Add a definition point to this value.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000264 void addDef(SlotIndex Idx, const MachineOperand &LocMO, bool IsIndirect) {
265 DbgValueLocation Loc(getLocationNo(LocMO), IsIndirect);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000266 // Add a singular (Idx,Idx) -> Loc mapping.
267 LocMap::iterator I = locInts.find(Idx);
268 if (!I.valid() || I.start() != Idx)
Reid Kleckner04e25e02017-10-03 17:59:02 +0000269 I.insert(Idx, Idx.getNextSlot(), Loc);
Jakob Stoklund Olesen2539af62011-08-03 23:44:31 +0000270 else
271 // A later DBG_VALUE at the same SlotIndex overrides the old location.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000272 I.setValue(Loc);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000273 }
274
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000275 /// Extend the current definition as far as possible down.
276 ///
Adrian Prantlf8d10ce2016-09-28 21:34:23 +0000277 /// Stop when meeting an existing def or when leaving the live
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000278 /// range of VNI. End points where VNI is no longer live are added to Kills.
279 ///
280 /// We only propagate DBG_VALUES locally here. LiveDebugValues performs a
281 /// data-flow analysis to propagate them beyond basic block boundaries.
282 ///
283 /// \param Idx Starting point for the definition.
284 /// \param Loc Location number to propagate.
285 /// \param LR Restrict liveness to where LR has the value VNI. May be null.
286 /// \param VNI When LR is not null, this is the value to restrict to.
287 /// \param [out] Kills Append end points of VNI's live range to Kills.
288 /// \param LIS Live intervals analysis.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000289 void extendDef(SlotIndex Idx, DbgValueLocation Loc,
Matthias Braun34e1be92013-10-10 21:29:02 +0000290 LiveRange *LR, const VNInfo *VNI,
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000291 SmallVectorImpl<SlotIndex> *Kills,
Adrian Prantlf8d10ce2016-09-28 21:34:23 +0000292 LiveIntervals &LIS);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000293
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000294 /// The value in LI/LocNo may be copies to other registers. Determine if
295 /// any of the copies are available at the kill points, and add defs if
296 /// possible.
297 ///
298 /// \param LI Scan for copies of the value in LI->reg.
299 /// \param LocNo Location number of LI->reg.
300 /// \param WasIndirect Indicates if the original use of LI->reg was indirect
301 /// \param Kills Points where the range of LocNo could be extended.
302 /// \param [in,out] NewDefs Append (Idx, LocNo) of inserted defs here.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000303 void addDefsFromCopies(
304 LiveInterval *LI, unsigned LocNo, bool WasIndirect,
305 const SmallVectorImpl<SlotIndex> &Kills,
306 SmallVectorImpl<std::pair<SlotIndex, DbgValueLocation>> &NewDefs,
307 MachineRegisterInfo &MRI, LiveIntervals &LIS);
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000308
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000309 /// Compute the live intervals of all locations after collecting all their
310 /// def points.
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000311 void computeIntervals(MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
Robert Lougher10f740d2017-08-03 11:54:02 +0000312 LiveIntervals &LIS, LexicalScopes &LS);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000313
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000314 /// Replace OldReg ranges with NewRegs ranges where NewRegs is
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000315 /// live. Returns true if any changes were made.
Fangrui Songcb0bab82018-07-16 18:51:40 +0000316 bool splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs,
Mark Laceyf9ea8852013-08-14 23:50:04 +0000317 LiveIntervals &LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000318
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000319 /// Rewrite virtual register locations according to the provided virtual
320 /// register map. Record the stack slot offsets for the locations that
321 /// were spilled.
David Stenberg45acc962018-09-07 13:54:07 +0000322 void rewriteLocations(VirtRegMap &VRM, const MachineFunction &MF,
323 const TargetInstrInfo &TII,
324 const TargetRegisterInfo &TRI,
325 SpillOffsetMap &SpillOffsets);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000326
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000327 /// Recreate DBG_VALUE instruction from data structures.
Reid Kleckner4e040282017-09-20 18:19:08 +0000328 void emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +0000329 const TargetInstrInfo &TII,
330 const TargetRegisterInfo &TRI,
David Stenberg45acc962018-09-07 13:54:07 +0000331 const SpillOffsetMap &SpillOffsets);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000332
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000333 /// Return DebugLoc of this UserValue.
Devang Patelf9e2ae92011-09-13 18:40:53 +0000334 DebugLoc getDebugLoc() { return dl;}
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000335
Eric Christopher1cdefae2015-02-27 00:11:34 +0000336 void print(raw_ostream &, const TargetRegisterInfo *);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000337};
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000338
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000339/// A user label is a part of a debug info user label.
340class UserLabel {
341 const DILabel *Label; ///< The debug info label we are part of.
342 DebugLoc dl; ///< The debug location for the label. This is
343 ///< used by dwarf writer to find lexical scope.
344 SlotIndex loc; ///< Slot used by the debug label.
345
346 /// Insert a DBG_LABEL into MBB at Idx.
347 void insertDebugLabel(MachineBasicBlock *MBB, SlotIndex Idx,
348 LiveIntervals &LIS, const TargetInstrInfo &TII);
349
350public:
351 /// Create a new UserLabel.
352 UserLabel(const DILabel *label, DebugLoc L, SlotIndex Idx)
353 : Label(label), dl(std::move(L)), loc(Idx) {}
354
355 /// Does this UserLabel match the parameters?
356 bool match(const DILabel *L, const DILocation *IA,
357 const SlotIndex Index) const {
358 return Label == L && dl->getInlinedAt() == IA && loc == Index;
359 }
360
361 /// Recreate DBG_LABEL instruction from data structures.
362 void emitDebugLabel(LiveIntervals &LIS, const TargetInstrInfo &TII);
363
364 /// Return DebugLoc of this UserLabel.
365 DebugLoc getDebugLoc() { return dl; }
366
367 void print(raw_ostream &, const TargetRegisterInfo *);
368};
369
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000370/// Implementation of the LiveDebugVariables pass.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000371class LDVImpl {
372 LiveDebugVariables &pass;
373 LocMap::Allocator allocator;
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000374 MachineFunction *MF = nullptr;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000375 LiveIntervals *LIS;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000376 const TargetRegisterInfo *TRI;
377
Manman Ren7a4c8a72013-02-13 20:23:48 +0000378 /// Whether emitDebugValues is called.
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000379 bool EmitDone = false;
380
Manman Ren7a4c8a72013-02-13 20:23:48 +0000381 /// Whether the machine function is modified during the pass.
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000382 bool ModifiedMF = false;
Manman Ren7a4c8a72013-02-13 20:23:48 +0000383
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000384 /// All allocated UserValue instances.
David Blaikie2b1dfa72014-04-21 20:37:07 +0000385 SmallVector<std::unique_ptr<UserValue>, 8> userValues;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000386
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000387 /// All allocated UserLabel instances.
388 SmallVector<std::unique_ptr<UserLabel>, 2> userLabels;
389
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000390 /// Map virtual register to eq class leader.
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000391 using VRMap = DenseMap<unsigned, UserValue *>;
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000392 VRMap virtRegToEqClass;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000393
394 /// Map user variable to eq class leader.
Reid Kleckner4e040282017-09-20 18:19:08 +0000395 using UVMap = DenseMap<const DILocalVariable *, UserValue *>;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000396 UVMap userVarMap;
397
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000398 /// Find or create a UserValue.
Reid Kleckner4e040282017-09-20 18:19:08 +0000399 UserValue *getUserValue(const DILocalVariable *Var, const DIExpression *Expr,
Reid Kleckner04e25e02017-10-03 17:59:02 +0000400 const DebugLoc &DL);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000401
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000402 /// Find the EC leader for VirtReg or null.
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000403 UserValue *lookupVirtReg(unsigned VirtReg);
404
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000405 /// Add DBG_VALUE instruction to our maps.
406 ///
407 /// \param MI DBG_VALUE instruction
408 /// \param Idx Last valid SLotIndex before instruction.
409 ///
410 /// \returns True if the DBG_VALUE instruction should be deleted.
Duncan P. N. Exon Smithfb612ac2016-06-30 23:13:38 +0000411 bool handleDebugValue(MachineInstr &MI, SlotIndex Idx);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000412
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000413 /// Add DBG_LABEL instruction to UserLabel.
414 ///
415 /// \param MI DBG_LABEL instruction
416 /// \param Idx Last valid SlotIndex before instruction.
417 ///
418 /// \returns True if the DBG_LABEL instruction should be deleted.
419 bool handleDebugLabel(MachineInstr &MI, SlotIndex Idx);
420
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000421 /// Collect and erase all DBG_VALUE instructions, adding a UserValue def
422 /// for each instruction.
423 ///
424 /// \param mf MachineFunction to be scanned.
425 ///
426 /// \returns True if any debug values were found.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000427 bool collectDebugValues(MachineFunction &mf);
428
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000429 /// Compute the live intervals of all user values after collecting all
430 /// their def points.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000431 void computeIntervals();
432
433public:
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000434 LDVImpl(LiveDebugVariables *ps) : pass(*ps) {}
435
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000436 bool runOnMachineFunction(MachineFunction &mf);
437
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000438 /// Release all memory.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000439 void clear() {
David Blaikie2f040112014-07-25 16:10:16 +0000440 MF = nullptr;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000441 userValues.clear();
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000442 userLabels.clear();
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000443 virtRegToEqClass.clear();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000444 userVarMap.clear();
Manman Ren7a4c8a72013-02-13 20:23:48 +0000445 // Make sure we call emitDebugValues if the machine function was modified.
446 assert((!ModifiedMF || EmitDone) &&
447 "Dbg values are not emitted in LDV");
448 EmitDone = false;
449 ModifiedMF = false;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000450 }
451
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000452 /// Map virtual register to an equivalence class.
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000453 void mapVirtReg(unsigned VirtReg, UserValue *EC);
454
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000455 /// Replace all references to OldReg with NewRegs.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000456 void splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000457
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000458 /// Recreate DBG_VALUE instruction from data structures.
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000459 void emitDebugValues(VirtRegMap *VRM);
460
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000461 void print(raw_ostream&);
462};
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000463
464} // end anonymous namespace
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000465
Aaron Ballman615eb472017-10-15 14:32:27 +0000466#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000467static void printDebugLoc(const DebugLoc &DL, raw_ostream &CommentOS,
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000468 const LLVMContext &Ctx) {
469 if (!DL)
470 return;
471
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000472 auto *Scope = cast<DIScope>(DL.getScope());
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000473 // Omit the directory, because it's likely to be long and uninteresting.
Duncan P. N. Exon Smithb273d062015-04-16 01:37:00 +0000474 CommentOS << Scope->getFilename();
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000475 CommentOS << ':' << DL.getLine();
476 if (DL.getCol() != 0)
477 CommentOS << ':' << DL.getCol();
478
479 DebugLoc InlinedAtDL = DL.getInlinedAt();
480 if (!InlinedAtDL)
481 return;
482
483 CommentOS << " @[ ";
484 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
485 CommentOS << " ]";
486}
487
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000488static void printExtendedName(raw_ostream &OS, const DINode *Node,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000489 const DILocation *DL) {
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000490 const LLVMContext &Ctx = Node->getContext();
491 StringRef Res;
492 unsigned Line;
493 if (const auto *V = dyn_cast<const DILocalVariable>(Node)) {
494 Res = V->getName();
495 Line = V->getLine();
496 } else if (const auto *L = dyn_cast<const DILabel>(Node)) {
497 Res = L->getName();
498 Line = L->getLine();
499 }
500
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000501 if (!Res.empty())
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000502 OS << Res << "," << Line;
Pengfei Wangc05aad02019-05-09 08:09:21 +0000503 auto *InlinedAt = DL ? DL->getInlinedAt() : nullptr;
504 if (InlinedAt) {
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000505 if (DebugLoc InlinedAtDL = InlinedAt) {
506 OS << " @[";
507 printDebugLoc(InlinedAtDL, OS, Ctx);
508 OS << "]";
509 }
510 }
511}
512
Eric Christopher1cdefae2015-02-27 00:11:34 +0000513void UserValue::print(raw_ostream &OS, const TargetRegisterInfo *TRI) {
Frederic Risse6bb1872014-08-07 20:04:00 +0000514 OS << "!\"";
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000515 printExtendedName(OS, Variable, dl);
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000516
Devang Patel6c1ed312011-08-09 01:03:35 +0000517 OS << "\"\t";
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000518 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
519 OS << " [" << I.start() << ';' << I.stop() << "):";
Reid Kleckner04e25e02017-10-03 17:59:02 +0000520 if (I.value().isUndef())
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000521 OS << "undef";
Reid Kleckner04e25e02017-10-03 17:59:02 +0000522 else {
523 OS << I.value().locNo();
524 if (I.value().wasIndirect())
525 OS << " ind";
526 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000527 }
Jakob Stoklund Olesenc86fe052011-05-06 17:59:59 +0000528 for (unsigned i = 0, e = locations.size(); i != e; ++i) {
529 OS << " Loc" << i << '=';
Eric Christopher1cdefae2015-02-27 00:11:34 +0000530 locations[i].print(OS, TRI);
Jakob Stoklund Olesenc86fe052011-05-06 17:59:59 +0000531 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000532 OS << '\n';
533}
534
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000535void UserLabel::print(raw_ostream &OS, const TargetRegisterInfo *TRI) {
536 OS << "!\"";
537 printExtendedName(OS, Label, dl);
538
539 OS << "\"\t";
540 OS << loc;
541 OS << '\n';
542}
543
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000544void LDVImpl::print(raw_ostream &OS) {
545 OS << "********** DEBUG VARIABLES **********\n";
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000546 for (auto &userValue : userValues)
547 userValue->print(OS, TRI);
548 OS << "********** DEBUG LABELS **********\n";
549 for (auto &userLabel : userLabels)
550 userLabel->print(OS, TRI);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000551}
Florian Hahn6b3216a2017-07-31 10:07:49 +0000552#endif
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000553
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000554void UserValue::mapVirtRegs(LDVImpl *LDV) {
555 for (unsigned i = 0, e = locations.size(); i != e; ++i)
556 if (locations[i].isReg() &&
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000557 Register::isVirtualRegister(locations[i].getReg()))
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000558 LDV->mapVirtReg(locations[i].getReg(), this);
559}
560
Reid Kleckner4e040282017-09-20 18:19:08 +0000561UserValue *LDVImpl::getUserValue(const DILocalVariable *Var,
Reid Kleckner04e25e02017-10-03 17:59:02 +0000562 const DIExpression *Expr, const DebugLoc &DL) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000563 UserValue *&Leader = userVarMap[Var];
564 if (Leader) {
565 UserValue *UV = Leader->getLeader();
566 Leader = UV;
567 for (; UV; UV = UV->getNext())
Reid Kleckner04e25e02017-10-03 17:59:02 +0000568 if (UV->match(Var, Expr, DL->getInlinedAt()))
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000569 return UV;
570 }
571
David Blaikie2b1dfa72014-04-21 20:37:07 +0000572 userValues.push_back(
Reid Kleckner04e25e02017-10-03 17:59:02 +0000573 llvm::make_unique<UserValue>(Var, Expr, DL, allocator));
David Blaikie2b1dfa72014-04-21 20:37:07 +0000574 UserValue *UV = userValues.back().get();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000575 Leader = UserValue::merge(Leader, UV);
576 return UV;
577}
578
579void LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *EC) {
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000580 assert(Register::isVirtualRegister(VirtReg) && "Only map VirtRegs");
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000581 UserValue *&Leader = virtRegToEqClass[VirtReg];
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000582 Leader = UserValue::merge(Leader, EC);
583}
584
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000585UserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) {
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000586 if (UserValue *UV = virtRegToEqClass.lookup(VirtReg))
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000587 return UV->getLeader();
Craig Topperc0196b12014-04-14 00:51:57 +0000588 return nullptr;
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000589}
590
Duncan P. N. Exon Smithfb612ac2016-06-30 23:13:38 +0000591bool LDVImpl::handleDebugValue(MachineInstr &MI, SlotIndex Idx) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000592 // DBG_VALUE loc, offset, variable
Duncan P. N. Exon Smithfb612ac2016-06-30 23:13:38 +0000593 if (MI.getNumOperands() != 4 ||
594 !(MI.getOperand(1).isReg() || MI.getOperand(1).isImm()) ||
595 !MI.getOperand(2).isMetadata()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000596 LLVM_DEBUG(dbgs() << "Can't handle " << MI);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000597 return false;
598 }
599
Bjorn Petterssonbdf0c002018-03-06 08:47:07 +0000600 // Detect invalid DBG_VALUE instructions, with a debug-use of a virtual
601 // register that hasn't been defined yet. If we do not remove those here, then
602 // the re-insertion of the DBG_VALUE instruction after register allocation
603 // will be incorrect.
604 // TODO: If earlier passes are corrected to generate sane debug information
605 // (and if the machine verifier is improved to catch this), then these checks
606 // could be removed or replaced by asserts.
607 bool Discard = false;
608 if (MI.getOperand(0).isReg() &&
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000609 Register::isVirtualRegister(MI.getOperand(0).getReg())) {
Bjorn Petterssonbdf0c002018-03-06 08:47:07 +0000610 const unsigned Reg = MI.getOperand(0).getReg();
611 if (!LIS->hasInterval(Reg)) {
612 // The DBG_VALUE is described by a virtual register that does not have a
613 // live interval. Discard the DBG_VALUE.
614 Discard = true;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000615 LLVM_DEBUG(dbgs() << "Discarding debug info (no LIS interval): " << Idx
616 << " " << MI);
Bjorn Petterssonbdf0c002018-03-06 08:47:07 +0000617 } else {
618 // The DBG_VALUE is only valid if either Reg is live out from Idx, or Reg
619 // is defined dead at Idx (where Idx is the slot index for the instruction
Jordan Rupprecht6387fa22019-02-23 01:28:32 +0000620 // preceding the DBG_VALUE).
Bjorn Petterssonbdf0c002018-03-06 08:47:07 +0000621 const LiveInterval &LI = LIS->getInterval(Reg);
622 LiveQueryResult LRQ = LI.Query(Idx);
623 if (!LRQ.valueOutOrDead()) {
624 // We have found a DBG_VALUE with the value in a virtual register that
625 // is not live. Discard the DBG_VALUE.
626 Discard = true;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000627 LLVM_DEBUG(dbgs() << "Discarding debug info (reg not live): " << Idx
628 << " " << MI);
Bjorn Petterssonbdf0c002018-03-06 08:47:07 +0000629 }
630 }
631 }
632
Reid Kleckner04e25e02017-10-03 17:59:02 +0000633 // Get or create the UserValue for (variable,offset) here.
Reid Kleckner4e040282017-09-20 18:19:08 +0000634 bool IsIndirect = MI.getOperand(1).isImm();
Adrian Prantlb2811e52017-07-28 23:06:50 +0000635 if (IsIndirect)
636 assert(MI.getOperand(1).getImm() == 0 && "DBG_VALUE with nonzero offset");
Reid Kleckner4e040282017-09-20 18:19:08 +0000637 const DILocalVariable *Var = MI.getDebugVariable();
638 const DIExpression *Expr = MI.getDebugExpression();
Reid Kleckner04e25e02017-10-03 17:59:02 +0000639 UserValue *UV =
640 getUserValue(Var, Expr, MI.getDebugLoc());
Bjorn Petterssonbdf0c002018-03-06 08:47:07 +0000641 if (!Discard)
642 UV->addDef(Idx, MI.getOperand(0), IsIndirect);
Bjorn Petterssone0050d72018-03-06 13:23:28 +0000643 else {
644 MachineOperand MO = MachineOperand::CreateReg(0U, false);
645 MO.setIsDebug();
646 UV->addDef(Idx, MO, false);
647 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000648 return true;
649}
650
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000651bool LDVImpl::handleDebugLabel(MachineInstr &MI, SlotIndex Idx) {
652 // DBG_LABEL label
653 if (MI.getNumOperands() != 1 || !MI.getOperand(0).isMetadata()) {
654 LLVM_DEBUG(dbgs() << "Can't handle " << MI);
655 return false;
656 }
657
658 // Get or create the UserLabel for label here.
659 const DILabel *Label = MI.getDebugLabel();
660 const DebugLoc &DL = MI.getDebugLoc();
661 bool Found = false;
662 for (auto const &L : userLabels) {
663 if (L->match(Label, DL->getInlinedAt(), Idx)) {
664 Found = true;
665 break;
666 }
667 }
668 if (!Found)
669 userLabels.push_back(llvm::make_unique<UserLabel>(Label, DL, Idx));
670
671 return true;
672}
673
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000674bool LDVImpl::collectDebugValues(MachineFunction &mf) {
675 bool Changed = false;
676 for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE;
677 ++MFI) {
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +0000678 MachineBasicBlock *MBB = &*MFI;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000679 for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
680 MBBI != MBBE;) {
Hsiangkai Wangb2b7f5f2018-09-05 05:58:53 +0000681 // Use the first debug instruction in the sequence to get a SlotIndex
682 // for following consecutive debug instructions.
683 if (!MBBI->isDebugInstr()) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000684 ++MBBI;
685 continue;
686 }
Hsiangkai Wangb2b7f5f2018-09-05 05:58:53 +0000687 // Debug instructions has no slot index. Use the previous
688 // non-debug instruction's SlotIndex as its SlotIndex.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000689 SlotIndex Idx =
690 MBBI == MBB->begin()
691 ? LIS->getMBBStartIdx(MBB)
692 : LIS->getInstructionIndex(*std::prev(MBBI)).getRegSlot();
Hsiangkai Wangb2b7f5f2018-09-05 05:58:53 +0000693 // Handle consecutive debug instructions with the same slot index.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000694 do {
Hsiangkai Wangb2b7f5f2018-09-05 05:58:53 +0000695 // Only handle DBG_VALUE in handleDebugValue(). Skip all other
696 // kinds of debug instructions.
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000697 if ((MBBI->isDebugValue() && handleDebugValue(*MBBI, Idx)) ||
698 (MBBI->isDebugLabel() && handleDebugLabel(*MBBI, Idx))) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000699 MBBI = MBB->erase(MBBI);
700 Changed = true;
701 } else
702 ++MBBI;
Hsiangkai Wangb2b7f5f2018-09-05 05:58:53 +0000703 } while (MBBI != MBBE && MBBI->isDebugInstr());
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000704 }
705 }
706 return Changed;
707}
708
Reid Kleckner04e25e02017-10-03 17:59:02 +0000709void UserValue::extendDef(SlotIndex Idx, DbgValueLocation Loc, LiveRange *LR,
Adrian Prantlce858132015-12-21 20:03:00 +0000710 const VNInfo *VNI, SmallVectorImpl<SlotIndex> *Kills,
Adrian Prantlf8d10ce2016-09-28 21:34:23 +0000711 LiveIntervals &LIS) {
Adrian Prantlce858132015-12-21 20:03:00 +0000712 SlotIndex Start = Idx;
713 MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start);
714 SlotIndex Stop = LIS.getMBBEndIdx(MBB);
715 LocMap::iterator I = locInts.find(Start);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000716
Adrian Prantlce858132015-12-21 20:03:00 +0000717 // Limit to VNI's live range.
718 bool ToEnd = true;
719 if (LR && VNI) {
720 LiveInterval::Segment *Segment = LR->getSegmentContaining(Start);
721 if (!Segment || Segment->valno != VNI) {
722 if (Kills)
723 Kills->push_back(Start);
724 return;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000725 }
Richard Trieu7a083812016-02-18 22:09:30 +0000726 if (Segment->end < Stop) {
727 Stop = Segment->end;
728 ToEnd = false;
729 }
Adrian Prantlce858132015-12-21 20:03:00 +0000730 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000731
Adrian Prantlce858132015-12-21 20:03:00 +0000732 // There could already be a short def at Start.
733 if (I.valid() && I.start() <= Start) {
734 // Stop when meeting a different location or an already extended interval.
735 Start = Start.getNextSlot();
Reid Kleckner04e25e02017-10-03 17:59:02 +0000736 if (I.value() != Loc || I.stop() != Start)
Adrian Prantlce858132015-12-21 20:03:00 +0000737 return;
738 // This is a one-slot placeholder. Just skip it.
739 ++I;
740 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000741
Adrian Prantlce858132015-12-21 20:03:00 +0000742 // Limited by the next def.
Fangrui Songb251cc02019-07-12 14:58:15 +0000743 if (I.valid() && I.start() < Stop)
Richard Trieu7a083812016-02-18 22:09:30 +0000744 Stop = I.start();
Adrian Prantlce858132015-12-21 20:03:00 +0000745 // Limited by VNI's live range.
746 else if (!ToEnd && Kills)
747 Kills->push_back(Stop);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000748
Adrian Prantlce858132015-12-21 20:03:00 +0000749 if (Start < Stop)
Reid Kleckner04e25e02017-10-03 17:59:02 +0000750 I.insert(Start, Stop, Loc);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000751}
752
Reid Kleckner04e25e02017-10-03 17:59:02 +0000753void UserValue::addDefsFromCopies(
754 LiveInterval *LI, unsigned LocNo, bool WasIndirect,
755 const SmallVectorImpl<SlotIndex> &Kills,
756 SmallVectorImpl<std::pair<SlotIndex, DbgValueLocation>> &NewDefs,
757 MachineRegisterInfo &MRI, LiveIntervals &LIS) {
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000758 if (Kills.empty())
759 return;
760 // Don't track copies from physregs, there are too many uses.
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000761 if (!Register::isVirtualRegister(LI->reg))
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000762 return;
763
764 // Collect all the (vreg, valno) pairs that are copies of LI.
765 SmallVector<std::pair<LiveInterval*, const VNInfo*>, 8> CopyValues;
Owen Andersonb36376e2014-03-17 19:36:09 +0000766 for (MachineOperand &MO : MRI.use_nodbg_operands(LI->reg)) {
767 MachineInstr *MI = MO.getParent();
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000768 // Copies of the full value.
Owen Andersonb36376e2014-03-17 19:36:09 +0000769 if (MO.getSubReg() || !MI->isCopy())
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000770 continue;
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000771 unsigned DstReg = MI->getOperand(0).getReg();
772
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +0000773 // Don't follow copies to physregs. These are usually setting up call
774 // arguments, and the argument registers are always call clobbered. We are
775 // better off in the source register which could be a callee-saved register,
776 // or it could be spilled.
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000777 if (!Register::isVirtualRegister(DstReg))
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +0000778 continue;
779
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000780 // Is LocNo extended to reach this copy? If not, another def may be blocking
781 // it, or we are looking at a wrong value of LI.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000782 SlotIndex Idx = LIS.getInstructionIndex(*MI);
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000783 LocMap::iterator I = locInts.find(Idx.getRegSlot(true));
Reid Kleckner04e25e02017-10-03 17:59:02 +0000784 if (!I.valid() || I.value().locNo() != LocNo)
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000785 continue;
786
787 if (!LIS.hasInterval(DstReg))
788 continue;
789 LiveInterval *DstLI = &LIS.getInterval(DstReg);
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000790 const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx.getRegSlot());
791 assert(DstVNI && DstVNI->def == Idx.getRegSlot() && "Bad copy value");
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000792 CopyValues.push_back(std::make_pair(DstLI, DstVNI));
793 }
794
795 if (CopyValues.empty())
796 return;
797
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000798 LLVM_DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI
799 << '\n');
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000800
801 // Try to add defs of the copied values for each kill point.
802 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
803 SlotIndex Idx = Kills[i];
804 for (unsigned j = 0, e = CopyValues.size(); j != e; ++j) {
805 LiveInterval *DstLI = CopyValues[j].first;
806 const VNInfo *DstVNI = CopyValues[j].second;
807 if (DstLI->getVNInfoAt(Idx) != DstVNI)
808 continue;
809 // Check that there isn't already a def at Idx
810 LocMap::iterator I = locInts.find(Idx);
811 if (I.valid() && I.start() <= Idx)
812 continue;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000813 LLVM_DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #"
814 << DstVNI->id << " in " << *DstLI << '\n');
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000815 MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def);
816 assert(CopyMI && CopyMI->isCopy() && "Bad copy value");
817 unsigned LocNo = getLocationNo(CopyMI->getOperand(0));
Reid Kleckner04e25e02017-10-03 17:59:02 +0000818 DbgValueLocation NewLoc(LocNo, WasIndirect);
819 I.insert(Idx, Idx.getNextSlot(), NewLoc);
820 NewDefs.push_back(std::make_pair(Idx, NewLoc));
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000821 break;
822 }
823 }
824}
825
Robert Lougher10f740d2017-08-03 11:54:02 +0000826void UserValue::computeIntervals(MachineRegisterInfo &MRI,
827 const TargetRegisterInfo &TRI,
828 LiveIntervals &LIS, LexicalScopes &LS) {
Reid Kleckner04e25e02017-10-03 17:59:02 +0000829 SmallVector<std::pair<SlotIndex, DbgValueLocation>, 16> Defs;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000830
831 // Collect all defs to be extended (Skipping undefs).
832 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I)
Reid Kleckner04e25e02017-10-03 17:59:02 +0000833 if (!I.value().isUndef())
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000834 Defs.push_back(std::make_pair(I.start(), I.value()));
835
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000836 // Extend all defs, and possibly add new ones along the way.
837 for (unsigned i = 0; i != Defs.size(); ++i) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000838 SlotIndex Idx = Defs[i].first;
Reid Kleckner04e25e02017-10-03 17:59:02 +0000839 DbgValueLocation Loc = Defs[i].second;
840 const MachineOperand &LocMO = locations[Loc.locNo()];
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000841
Reid Kleckner04e25e02017-10-03 17:59:02 +0000842 if (!LocMO.isReg()) {
843 extendDef(Idx, Loc, nullptr, nullptr, nullptr, LIS);
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000844 continue;
845 }
846
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000847 // Register locations are constrained to where the register value is live.
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000848 if (Register::isVirtualRegister(LocMO.getReg())) {
Craig Topperc0196b12014-04-14 00:51:57 +0000849 LiveInterval *LI = nullptr;
850 const VNInfo *VNI = nullptr;
Reid Kleckner04e25e02017-10-03 17:59:02 +0000851 if (LIS.hasInterval(LocMO.getReg())) {
852 LI = &LIS.getInterval(LocMO.getReg());
Jakob Stoklund Olesen48a16472012-06-22 18:51:35 +0000853 VNI = LI->getVNInfoAt(Idx);
854 }
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000855 SmallVector<SlotIndex, 16> Kills;
Reid Kleckner04e25e02017-10-03 17:59:02 +0000856 extendDef(Idx, Loc, LI, VNI, &Kills, LIS);
Bjorn Pettersson84830042018-08-25 10:02:03 +0000857 // FIXME: Handle sub-registers in addDefsFromCopies. The problem is that
858 // if the original location for example is %vreg0:sub_hi, and we find a
859 // full register copy in addDefsFromCopies (at the moment it only handles
860 // full register copies), then we must add the sub1 sub-register index to
861 // the new location. However, that is only possible if the new virtual
862 // register is of the same regclass (or if there is an equivalent
863 // sub-register in that regclass). For now, simply skip handling copies if
864 // a sub-register is involved.
865 if (LI && !LocMO.getSubReg())
Reid Kleckner04e25e02017-10-03 17:59:02 +0000866 addDefsFromCopies(LI, Loc.locNo(), Loc.wasIndirect(), Kills, Defs, MRI,
867 LIS);
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000868 continue;
869 }
870
Bjorn Pettersson715a5ef2017-09-28 13:10:06 +0000871 // For physregs, we only mark the start slot idx. DwarfDebug will see it
872 // as if the DBG_VALUE is valid up until the end of the basic block, or
873 // the next def of the physical register. So we do not need to extend the
874 // range. It might actually happen that the DBG_VALUE is the last use of
875 // the physical register (e.g. if this is an unused input argument to a
876 // function).
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000877 }
878
Robert Lougher10f740d2017-08-03 11:54:02 +0000879 // The computed intervals may extend beyond the range of the debug
880 // location's lexical scope. In this case, splitting of an interval
881 // can result in an interval outside of the scope being created,
882 // causing extra unnecessary DBG_VALUEs to be emitted. To prevent
883 // this, trim the intervals to the lexical scope.
884
885 LexicalScope *Scope = LS.findLexicalScope(dl);
886 if (!Scope)
887 return;
888
889 SlotIndex PrevEnd;
890 LocMap::iterator I = locInts.begin();
891
892 // Iterate over the lexical scope ranges. Each time round the loop
893 // we check the intervals for overlap with the end of the previous
894 // range and the start of the next. The first range is handled as
895 // a special case where there is no PrevEnd.
896 for (const InsnRange &Range : Scope->getRanges()) {
897 SlotIndex RStart = LIS.getInstructionIndex(*Range.first);
898 SlotIndex REnd = LIS.getInstructionIndex(*Range.second);
899
900 // At the start of each iteration I has been advanced so that
901 // I.stop() >= PrevEnd. Check for overlap.
902 if (PrevEnd && I.start() < PrevEnd) {
903 SlotIndex IStop = I.stop();
Reid Kleckner04e25e02017-10-03 17:59:02 +0000904 DbgValueLocation Loc = I.value();
Robert Lougher10f740d2017-08-03 11:54:02 +0000905
906 // Stop overlaps previous end - trim the end of the interval to
907 // the scope range.
908 I.setStopUnchecked(PrevEnd);
909 ++I;
910
911 // If the interval also overlaps the start of the "next" (i.e.
Alexey Lapshinb9f1e7b2019-06-06 21:19:39 +0000912 // current) range create a new interval for the remainder
Robert Lougher10f740d2017-08-03 11:54:02 +0000913 if (RStart < IStop)
Reid Kleckner04e25e02017-10-03 17:59:02 +0000914 I.insert(RStart, IStop, Loc);
Robert Lougher10f740d2017-08-03 11:54:02 +0000915 }
916
917 // Advance I so that I.stop() >= RStart, and check for overlap.
918 I.advanceTo(RStart);
919 if (!I.valid())
920 return;
921
Robert Lougher10f740d2017-08-03 11:54:02 +0000922 // The end of a lexical scope range is the last instruction in the
923 // range. To convert to an interval we need the index of the
924 // instruction after it.
925 REnd = REnd.getNextIndex();
926
927 // Advance I to first interval outside current range.
928 I.advanceTo(REnd);
929 if (!I.valid())
930 return;
931
932 PrevEnd = REnd;
933 }
934
935 // Check for overlap with end of final range.
936 if (PrevEnd && I.start() < PrevEnd)
937 I.setStopUnchecked(PrevEnd);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000938}
939
940void LDVImpl::computeIntervals() {
Robert Lougher10f740d2017-08-03 11:54:02 +0000941 LexicalScopes LS;
942 LS.initialize(*MF);
943
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000944 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
Robert Lougher10f740d2017-08-03 11:54:02 +0000945 userValues[i]->computeIntervals(MF->getRegInfo(), *TRI, *LIS, LS);
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000946 userValues[i]->mapVirtRegs(this);
947 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000948}
949
950bool LDVImpl::runOnMachineFunction(MachineFunction &mf) {
David Blaikie2f040112014-07-25 16:10:16 +0000951 clear();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000952 MF = &mf;
953 LIS = &pass.getAnalysis<LiveIntervals>();
Eric Christopherfc6de422014-08-05 02:39:49 +0000954 TRI = mf.getSubtarget().getRegisterInfo();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000955 LLVM_DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: "
956 << mf.getName() << " **********\n");
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000957
958 bool Changed = collectDebugValues(mf);
959 computeIntervals();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000960 LLVM_DEBUG(print(dbgs()));
Manman Ren7a4c8a72013-02-13 20:23:48 +0000961 ModifiedMF = Changed;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000962 return Changed;
963}
964
David Blaikie2f040112014-07-25 16:10:16 +0000965static void removeDebugValues(MachineFunction &mf) {
966 for (MachineBasicBlock &MBB : mf) {
967 for (auto MBBI = MBB.begin(), MBBE = MBB.end(); MBBI != MBBE; ) {
968 if (!MBBI->isDebugValue()) {
969 ++MBBI;
970 continue;
971 }
972 MBBI = MBB.erase(MBBI);
973 }
974 }
975}
976
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +0000977bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) {
Devang Patelacbee0b2011-01-07 22:33:41 +0000978 if (!EnableLDV)
979 return false;
Matthias Braunf1caa282017-12-15 22:22:58 +0000980 if (!mf.getFunction().getSubprogram()) {
David Blaikie2f040112014-07-25 16:10:16 +0000981 removeDebugValues(mf);
982 return false;
983 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000984 if (!pImpl)
985 pImpl = new LDVImpl(this);
Manman Ren7a4c8a72013-02-13 20:23:48 +0000986 return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000987}
988
989void LiveDebugVariables::releaseMemory() {
Manman Ren7a4c8a72013-02-13 20:23:48 +0000990 if (pImpl)
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000991 static_cast<LDVImpl*>(pImpl)->clear();
992}
993
994LiveDebugVariables::~LiveDebugVariables() {
995 if (pImpl)
996 delete static_cast<LDVImpl*>(pImpl);
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +0000997}
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000998
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000999//===----------------------------------------------------------------------===//
1000// Live Range Splitting
1001//===----------------------------------------------------------------------===//
1002
1003bool
Mark Laceyf9ea8852013-08-14 23:50:04 +00001004UserValue::splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
1005 LiveIntervals& LIS) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001006 LLVM_DEBUG({
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001007 dbgs() << "Splitting Loc" << OldLocNo << '\t';
Craig Topperc0196b12014-04-14 00:51:57 +00001008 print(dbgs(), nullptr);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001009 });
1010 bool DidChange = false;
1011 LocMap::iterator LocMapI;
1012 LocMapI.setMap(locInts);
1013 for (unsigned i = 0; i != NewRegs.size(); ++i) {
Mark Laceyf9ea8852013-08-14 23:50:04 +00001014 LiveInterval *LI = &LIS.getInterval(NewRegs[i]);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001015 if (LI->empty())
1016 continue;
1017
1018 // Don't allocate the new LocNo until it is needed.
Reid Klecknereed09732017-09-15 22:08:50 +00001019 unsigned NewLocNo = UndefLocNo;
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001020
1021 // Iterate over the overlaps between locInts and LI.
1022 LocMapI.find(LI->beginIndex());
1023 if (!LocMapI.valid())
1024 continue;
1025 LiveInterval::iterator LII = LI->advanceTo(LI->begin(), LocMapI.start());
1026 LiveInterval::iterator LIE = LI->end();
1027 while (LocMapI.valid() && LII != LIE) {
1028 // At this point, we know that LocMapI.stop() > LII->start.
1029 LII = LI->advanceTo(LII, LocMapI.start());
1030 if (LII == LIE)
1031 break;
1032
1033 // Now LII->end > LocMapI.start(). Do we have an overlap?
Reid Kleckner04e25e02017-10-03 17:59:02 +00001034 if (LocMapI.value().locNo() == OldLocNo && LII->start < LocMapI.stop()) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001035 // Overlapping correct location. Allocate NewLocNo now.
Reid Klecknereed09732017-09-15 22:08:50 +00001036 if (NewLocNo == UndefLocNo) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001037 MachineOperand MO = MachineOperand::CreateReg(LI->reg, false);
1038 MO.setSubReg(locations[OldLocNo].getSubReg());
1039 NewLocNo = getLocationNo(MO);
1040 DidChange = true;
1041 }
1042
1043 SlotIndex LStart = LocMapI.start();
1044 SlotIndex LStop = LocMapI.stop();
Reid Kleckner04e25e02017-10-03 17:59:02 +00001045 DbgValueLocation OldLoc = LocMapI.value();
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001046
1047 // Trim LocMapI down to the LII overlap.
1048 if (LStart < LII->start)
1049 LocMapI.setStartUnchecked(LII->start);
1050 if (LStop > LII->end)
1051 LocMapI.setStopUnchecked(LII->end);
1052
1053 // Change the value in the overlap. This may trigger coalescing.
Reid Kleckner04e25e02017-10-03 17:59:02 +00001054 LocMapI.setValue(OldLoc.changeLocNo(NewLocNo));
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001055
1056 // Re-insert any removed OldLocNo ranges.
1057 if (LStart < LocMapI.start()) {
Reid Kleckner04e25e02017-10-03 17:59:02 +00001058 LocMapI.insert(LStart, LocMapI.start(), OldLoc);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001059 ++LocMapI;
1060 assert(LocMapI.valid() && "Unexpected coalescing");
1061 }
1062 if (LStop > LocMapI.stop()) {
1063 ++LocMapI;
Reid Kleckner04e25e02017-10-03 17:59:02 +00001064 LocMapI.insert(LII->end, LStop, OldLoc);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001065 --LocMapI;
1066 }
1067 }
1068
1069 // Advance to the next overlap.
1070 if (LII->end < LocMapI.stop()) {
1071 if (++LII == LIE)
1072 break;
1073 LocMapI.advanceTo(LII->start);
1074 } else {
1075 ++LocMapI;
1076 if (!LocMapI.valid())
1077 break;
1078 LII = LI->advanceTo(LII, LocMapI.start());
1079 }
1080 }
1081 }
1082
1083 // Finally, remove any remaining OldLocNo intervals and OldLocNo itself.
1084 locations.erase(locations.begin() + OldLocNo);
1085 LocMapI.goToBegin();
1086 while (LocMapI.valid()) {
Reid Kleckner04e25e02017-10-03 17:59:02 +00001087 DbgValueLocation v = LocMapI.value();
1088 if (v.locNo() == OldLocNo) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001089 LLVM_DEBUG(dbgs() << "Erasing [" << LocMapI.start() << ';'
1090 << LocMapI.stop() << ")\n");
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001091 LocMapI.erase();
1092 } else {
Mikael Holmen57b33f62018-06-21 07:02:46 +00001093 // Undef values always have location number UndefLocNo, so don't change
1094 // locNo in that case. See getLocationNo().
1095 if (!v.isUndef() && v.locNo() > OldLocNo)
Reid Kleckner04e25e02017-10-03 17:59:02 +00001096 LocMapI.setValueUnchecked(v.changeLocNo(v.locNo() - 1));
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001097 ++LocMapI;
1098 }
1099 }
1100
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001101 LLVM_DEBUG({
1102 dbgs() << "Split result: \t";
1103 print(dbgs(), nullptr);
1104 });
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001105 return DidChange;
1106}
1107
1108bool
Mark Laceyf9ea8852013-08-14 23:50:04 +00001109UserValue::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs,
1110 LiveIntervals &LIS) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001111 bool DidChange = false;
Jakob Stoklund Olesen57c8f582011-05-06 19:31:19 +00001112 // Split locations referring to OldReg. Iterate backwards so splitLocation can
Eric Christopherbe153e62012-03-15 21:33:35 +00001113 // safely erase unused locations.
Jakob Stoklund Olesen57c8f582011-05-06 19:31:19 +00001114 for (unsigned i = locations.size(); i ; --i) {
1115 unsigned LocNo = i-1;
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001116 const MachineOperand *Loc = &locations[LocNo];
1117 if (!Loc->isReg() || Loc->getReg() != OldReg)
1118 continue;
Mark Laceyf9ea8852013-08-14 23:50:04 +00001119 DidChange |= splitLocation(LocNo, NewRegs, LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001120 }
1121 return DidChange;
1122}
1123
Mark Laceyf9ea8852013-08-14 23:50:04 +00001124void LDVImpl::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001125 bool DidChange = false;
1126 for (UserValue *UV = lookupVirtReg(OldReg); UV; UV = UV->getNext())
Mark Laceyf9ea8852013-08-14 23:50:04 +00001127 DidChange |= UV->splitRegister(OldReg, NewRegs, *LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001128
1129 if (!DidChange)
1130 return;
1131
1132 // Map all of the new virtual registers.
1133 UserValue *UV = lookupVirtReg(OldReg);
1134 for (unsigned i = 0; i != NewRegs.size(); ++i)
Mark Laceyf9ea8852013-08-14 23:50:04 +00001135 mapVirtReg(NewRegs[i], UV);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001136}
1137
1138void LiveDebugVariables::
Mark Laceyf9ea8852013-08-14 23:50:04 +00001139splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs, LiveIntervals &LIS) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001140 if (pImpl)
1141 static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs);
1142}
1143
David Stenberg45acc962018-09-07 13:54:07 +00001144void UserValue::rewriteLocations(VirtRegMap &VRM, const MachineFunction &MF,
1145 const TargetInstrInfo &TII,
1146 const TargetRegisterInfo &TRI,
1147 SpillOffsetMap &SpillOffsets) {
Reid Kleckner92687d42017-09-20 17:32:54 +00001148 // Build a set of new locations with new numbers so we can coalesce our
1149 // IntervalMap if two vreg intervals collapse to the same physical location.
1150 // Use MapVector instead of SetVector because MapVector::insert returns the
Reid Kleckner4e040282017-09-20 18:19:08 +00001151 // position of the previously or newly inserted element. The boolean value
1152 // tracks if the location was produced by a spill.
1153 // FIXME: This will be problematic if we ever support direct and indirect
1154 // frame index locations, i.e. expressing both variables in memory and
1155 // 'int x, *px = &x'. The "spilled" bit must become part of the location.
David Stenberg45acc962018-09-07 13:54:07 +00001156 MapVector<MachineOperand, std::pair<bool, unsigned>> NewLocations;
Reid Kleckner92687d42017-09-20 17:32:54 +00001157 SmallVector<unsigned, 4> LocNoMap(locations.size());
1158 for (unsigned I = 0, E = locations.size(); I != E; ++I) {
Reid Kleckner4e040282017-09-20 18:19:08 +00001159 bool Spilled = false;
David Stenberg45acc962018-09-07 13:54:07 +00001160 unsigned SpillOffset = 0;
Reid Kleckner92687d42017-09-20 17:32:54 +00001161 MachineOperand Loc = locations[I];
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001162 // Only virtual registers are rewritten.
Reid Kleckner92687d42017-09-20 17:32:54 +00001163 if (Loc.isReg() && Loc.getReg() &&
Daniel Sanders2bea69b2019-08-01 23:27:28 +00001164 Register::isVirtualRegister(Loc.getReg())) {
Reid Kleckner92687d42017-09-20 17:32:54 +00001165 unsigned VirtReg = Loc.getReg();
1166 if (VRM.isAssignedReg(VirtReg) &&
Daniel Sanders2bea69b2019-08-01 23:27:28 +00001167 Register::isPhysicalRegister(VRM.getPhys(VirtReg))) {
Reid Kleckner92687d42017-09-20 17:32:54 +00001168 // This can create a %noreg operand in rare cases when the sub-register
1169 // index is no longer available. That means the user value is in a
1170 // non-existent sub-register, and %noreg is exactly what we want.
1171 Loc.substPhysReg(VRM.getPhys(VirtReg), TRI);
1172 } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT) {
David Stenberg45acc962018-09-07 13:54:07 +00001173 // Retrieve the stack slot offset.
1174 unsigned SpillSize;
1175 const MachineRegisterInfo &MRI = MF.getRegInfo();
1176 const TargetRegisterClass *TRC = MRI.getRegClass(VirtReg);
1177 bool Success = TII.getStackSlotRange(TRC, Loc.getSubReg(), SpillSize,
1178 SpillOffset, MF);
1179
1180 // FIXME: Invalidate the location if the offset couldn't be calculated.
1181 (void)Success;
1182
Reid Kleckner92687d42017-09-20 17:32:54 +00001183 Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg));
Reid Kleckner4e040282017-09-20 18:19:08 +00001184 Spilled = true;
Reid Kleckner92687d42017-09-20 17:32:54 +00001185 } else {
1186 Loc.setReg(0);
1187 Loc.setSubReg(0);
1188 }
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001189 }
Reid Kleckner92687d42017-09-20 17:32:54 +00001190
1191 // Insert this location if it doesn't already exist and record a mapping
1192 // from the old number to the new number.
David Stenberg45acc962018-09-07 13:54:07 +00001193 auto InsertResult = NewLocations.insert({Loc, {Spilled, SpillOffset}});
Reid Kleckner4e040282017-09-20 18:19:08 +00001194 unsigned NewLocNo = std::distance(NewLocations.begin(), InsertResult.first);
1195 LocNoMap[I] = NewLocNo;
Reid Kleckner92687d42017-09-20 17:32:54 +00001196 }
1197
David Stenberg45acc962018-09-07 13:54:07 +00001198 // Rewrite the locations and record the stack slot offsets for spills.
Reid Kleckner92687d42017-09-20 17:32:54 +00001199 locations.clear();
David Stenberg45acc962018-09-07 13:54:07 +00001200 SpillOffsets.clear();
Reid Kleckner4e040282017-09-20 18:19:08 +00001201 for (auto &Pair : NewLocations) {
David Stenberg45acc962018-09-07 13:54:07 +00001202 bool Spilled;
1203 unsigned SpillOffset;
1204 std::tie(Spilled, SpillOffset) = Pair.second;
Reid Kleckner92687d42017-09-20 17:32:54 +00001205 locations.push_back(Pair.first);
David Stenberg45acc962018-09-07 13:54:07 +00001206 if (Spilled) {
Reid Kleckner4e040282017-09-20 18:19:08 +00001207 unsigned NewLocNo = std::distance(&*NewLocations.begin(), &Pair);
David Stenberg45acc962018-09-07 13:54:07 +00001208 SpillOffsets[NewLocNo] = SpillOffset;
Reid Kleckner4e040282017-09-20 18:19:08 +00001209 }
1210 }
Reid Kleckner92687d42017-09-20 17:32:54 +00001211
1212 // Update the interval map, but only coalesce left, since intervals to the
1213 // right use the old location numbers. This should merge two contiguous
1214 // DBG_VALUE intervals with different vregs that were allocated to the same
1215 // physical register.
1216 for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) {
Reid Kleckner04e25e02017-10-03 17:59:02 +00001217 DbgValueLocation Loc = I.value();
Mikael Holmen57b33f62018-06-21 07:02:46 +00001218 // Undef values don't exist in locations (and thus not in LocNoMap either)
1219 // so skip over them. See getLocationNo().
1220 if (Loc.isUndef())
1221 continue;
Reid Kleckner04e25e02017-10-03 17:59:02 +00001222 unsigned NewLocNo = LocNoMap[Loc.locNo()];
1223 I.setValueUnchecked(Loc.changeLocNo(NewLocNo));
Reid Kleckner92687d42017-09-20 17:32:54 +00001224 I.setStart(I.start());
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001225 }
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001226}
1227
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001228/// Find an iterator for inserting a DBG_VALUE instruction.
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001229static MachineBasicBlock::iterator
Devang Patel26ffa012011-02-04 01:43:25 +00001230findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx,
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001231 LiveIntervals &LIS) {
1232 SlotIndex Start = LIS.getMBBStartIdx(MBB);
1233 Idx = Idx.getBaseIndex();
1234
1235 // Try to find an insert location by going backwards from Idx.
1236 MachineInstr *MI;
1237 while (!(MI = LIS.getInstructionFromIndex(Idx))) {
1238 // We've reached the beginning of MBB.
1239 if (Idx == Start) {
Keith Walker830a8c12016-09-16 14:07:29 +00001240 MachineBasicBlock::iterator I = MBB->SkipPHIsLabelsAndDebug(MBB->begin());
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001241 return I;
1242 }
1243 Idx = Idx.getPrevIndex();
1244 }
Devang Patel26ffa012011-02-04 01:43:25 +00001245
Jakob Stoklund Olesen088b30a2011-01-13 23:35:53 +00001246 // Don't insert anything after the first terminator, though.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001247 return MI->isTerminator() ? MBB->getFirstTerminator() :
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001248 std::next(MachineBasicBlock::iterator(MI));
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001249}
1250
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001251/// Find an iterator for inserting the next DBG_VALUE instruction
1252/// (or end if no more insert locations found).
1253static MachineBasicBlock::iterator
1254findNextInsertLocation(MachineBasicBlock *MBB,
1255 MachineBasicBlock::iterator I,
1256 SlotIndex StopIdx, MachineOperand &LocMO,
1257 LiveIntervals &LIS,
1258 const TargetRegisterInfo &TRI) {
1259 if (!LocMO.isReg())
1260 return MBB->instr_end();
1261 unsigned Reg = LocMO.getReg();
1262
1263 // Find the next instruction in the MBB that define the register Reg.
Stefan Maksimovic991af7a2018-02-09 14:03:26 +00001264 while (I != MBB->end() && !I->isTerminator()) {
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001265 if (!LIS.isNotInMIMap(*I) &&
1266 SlotIndex::isEarlierEqualInstr(StopIdx, LIS.getInstructionIndex(*I)))
1267 break;
1268 if (I->definesRegister(Reg, &TRI))
1269 // The insert location is directly after the instruction/bundle.
1270 return std::next(I);
1271 ++I;
1272 }
1273 return MBB->end();
1274}
1275
1276void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex StartIdx,
David Stenberg45acc962018-09-07 13:54:07 +00001277 SlotIndex StopIdx, DbgValueLocation Loc,
1278 bool Spilled, unsigned SpillOffset,
1279 LiveIntervals &LIS, const TargetInstrInfo &TII,
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001280 const TargetRegisterInfo &TRI) {
1281 SlotIndex MBBEndIdx = LIS.getMBBEndIdx(&*MBB);
1282 // Only search within the current MBB.
1283 StopIdx = (MBBEndIdx < StopIdx) ? MBBEndIdx : StopIdx;
1284 MachineBasicBlock::iterator I = findInsertLocation(MBB, StartIdx, LIS);
Mikael Holmen57b33f62018-06-21 07:02:46 +00001285 // Undef values don't exist in locations so create new "noreg" register MOs
1286 // for them. See getLocationNo().
1287 MachineOperand MO = !Loc.isUndef() ?
1288 locations[Loc.locNo()] :
1289 MachineOperand::CreateReg(/* Reg */ 0, /* isDef */ false, /* isImp */ false,
1290 /* isKill */ false, /* isDead */ false,
1291 /* isUndef */ false, /* isEarlyClobber */ false,
1292 /* SubReg */ 0, /* isDebug */ true);
1293
Devang Pateleabc3cea2011-08-04 20:42:11 +00001294 ++NumInsertedDebugValues;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001295
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001296 assert(cast<DILocalVariable>(Variable)
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +00001297 ->isValidLocationForIntrinsic(getDebugLoc()) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +00001298 "Expected inlined-at fields to agree");
Reid Kleckner4e040282017-09-20 18:19:08 +00001299
1300 // If the location was spilled, the new DBG_VALUE will be indirect. If the
1301 // original DBG_VALUE was indirect, we need to add DW_OP_deref to indicate
David Stenberg45acc962018-09-07 13:54:07 +00001302 // that the original virtual register was a pointer. Also, add the stack slot
1303 // offset for the spilled register to the expression.
Reid Kleckner4e040282017-09-20 18:19:08 +00001304 const DIExpression *Expr = Expression;
Petar Jovanovice85bbf52019-05-20 10:35:57 +00001305 uint8_t DIExprFlags = DIExpression::ApplyOffset;
Reid Kleckner04e25e02017-10-03 17:59:02 +00001306 bool IsIndirect = Loc.wasIndirect();
1307 if (Spilled) {
Petar Jovanovice85bbf52019-05-20 10:35:57 +00001308 if (IsIndirect)
1309 DIExprFlags |= DIExpression::DerefAfter;
David Stenberg45acc962018-09-07 13:54:07 +00001310 Expr =
Petar Jovanovice85bbf52019-05-20 10:35:57 +00001311 DIExpression::prepend(Expr, DIExprFlags, SpillOffset);
Reid Kleckner04e25e02017-10-03 17:59:02 +00001312 IsIndirect = true;
1313 }
Reid Kleckner4e040282017-09-20 18:19:08 +00001314
Reid Kleckner04e25e02017-10-03 17:59:02 +00001315 assert((!Spilled || MO.isFI()) && "a spilled location must be a frame index");
Reid Kleckner4e040282017-09-20 18:19:08 +00001316
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001317 do {
Mikael Holmen42f7bc92018-06-21 10:03:34 +00001318 BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_VALUE),
1319 IsIndirect, MO, Variable, Expr);
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001320
1321 // Continue and insert DBG_VALUES after every redefinition of register
1322 // associated with the debug value within the range
1323 I = findNextInsertLocation(MBB, I, StopIdx, MO, LIS, TRI);
1324 } while (I != MBB->end());
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001325}
1326
Hsiangkai Wang66609a82019-01-18 07:17:09 +00001327void UserLabel::insertDebugLabel(MachineBasicBlock *MBB, SlotIndex Idx,
1328 LiveIntervals &LIS,
1329 const TargetInstrInfo &TII) {
1330 MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS);
1331 ++NumInsertedDebugLabels;
1332 BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_LABEL))
1333 .addMetadata(Label);
1334}
1335
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001336void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
Reid Kleckner4e040282017-09-20 18:19:08 +00001337 const TargetInstrInfo &TII,
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001338 const TargetRegisterInfo &TRI,
David Stenberg45acc962018-09-07 13:54:07 +00001339 const SpillOffsetMap &SpillOffsets) {
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001340 MachineFunction::iterator MFEnd = VRM->getMachineFunction().end();
1341
1342 for (LocMap::const_iterator I = locInts.begin(); I.valid();) {
1343 SlotIndex Start = I.start();
1344 SlotIndex Stop = I.stop();
Reid Kleckner04e25e02017-10-03 17:59:02 +00001345 DbgValueLocation Loc = I.value();
David Stenberg45acc962018-09-07 13:54:07 +00001346 auto SpillIt =
1347 !Loc.isUndef() ? SpillOffsets.find(Loc.locNo()) : SpillOffsets.end();
1348 bool Spilled = SpillIt != SpillOffsets.end();
1349 unsigned SpillOffset = Spilled ? SpillIt->second : 0;
Robert Lougher10f740d2017-08-03 11:54:02 +00001350
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001351 LLVM_DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << Loc.locNo());
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +00001352 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
1353 SlotIndex MBBEnd = LIS.getMBBEndIdx(&*MBB);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001354
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001355 LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd);
David Stenberg45acc962018-09-07 13:54:07 +00001356 insertDebugValue(&*MBB, Start, Stop, Loc, Spilled, SpillOffset, LIS, TII,
1357 TRI);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001358 // This interval may span multiple basic blocks.
1359 // Insert a DBG_VALUE into each one.
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001360 while (Stop > MBBEnd) {
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001361 // Move to the next block.
1362 Start = MBBEnd;
1363 if (++MBB == MFEnd)
1364 break;
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +00001365 MBBEnd = LIS.getMBBEndIdx(&*MBB);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001366 LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd);
David Stenberg45acc962018-09-07 13:54:07 +00001367 insertDebugValue(&*MBB, Start, Stop, Loc, Spilled, SpillOffset, LIS, TII,
1368 TRI);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001369 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001370 LLVM_DEBUG(dbgs() << '\n');
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001371 if (MBB == MFEnd)
1372 break;
1373
1374 ++I;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001375 }
1376}
1377
Hsiangkai Wang66609a82019-01-18 07:17:09 +00001378void UserLabel::emitDebugLabel(LiveIntervals &LIS, const TargetInstrInfo &TII) {
1379 LLVM_DEBUG(dbgs() << "\t" << loc);
1380 MachineFunction::iterator MBB = LIS.getMBBFromIndex(loc)->getIterator();
1381
1382 LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB));
1383 insertDebugLabel(&*MBB, loc, LIS, TII);
1384
1385 LLVM_DEBUG(dbgs() << '\n');
1386}
1387
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001388void LDVImpl::emitDebugValues(VirtRegMap *VRM) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001389 LLVM_DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n");
David Blaikie2f040112014-07-25 16:10:16 +00001390 if (!MF)
1391 return;
Eric Christopherfc6de422014-08-05 02:39:49 +00001392 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
David Stenberg45acc962018-09-07 13:54:07 +00001393 SpillOffsetMap SpillOffsets;
Hsiangkai Wang66609a82019-01-18 07:17:09 +00001394 for (auto &userValue : userValues) {
1395 LLVM_DEBUG(userValue->print(dbgs(), TRI));
1396 userValue->rewriteLocations(*VRM, *MF, *TII, *TRI, SpillOffsets);
1397 userValue->emitDebugValues(VRM, *LIS, *TII, *TRI, SpillOffsets);
1398 }
1399 LLVM_DEBUG(dbgs() << "********** EMITTING LIVE DEBUG LABELS **********\n");
1400 for (auto &userLabel : userLabels) {
1401 LLVM_DEBUG(userLabel->print(dbgs(), TRI));
1402 userLabel->emitDebugLabel(*LIS, *TII);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001403 }
Manman Ren7a4c8a72013-02-13 20:23:48 +00001404 EmitDone = true;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001405}
1406
1407void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) {
Manman Ren7a4c8a72013-02-13 20:23:48 +00001408 if (pImpl)
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001409 static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM);
1410}
1411
David Blaikie2f040112014-07-25 16:10:16 +00001412bool LiveDebugVariables::doInitialization(Module &M) {
David Blaikie2f040112014-07-25 16:10:16 +00001413 return Pass::doInitialization(M);
1414}
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001415
Aaron Ballman615eb472017-10-15 14:32:27 +00001416#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Sam Clegg705f7982017-06-21 22:19:17 +00001417LLVM_DUMP_METHOD void LiveDebugVariables::dump() const {
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +00001418 if (pImpl)
1419 static_cast<LDVImpl*>(pImpl)->print(dbgs());
1420}
1421#endif