blob: cace04430adca96163b5f037aa523d7ef1a84b8a [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:
Jeremy Morseed29dba2019-10-15 10:46:24 +0000102 DbgValueLocation(unsigned LocNo)
103 : LocNo(LocNo) {
Reid Kleckner04e25e02017-10-03 17:59:02 +0000104 static_assert(sizeof(*this) == sizeof(unsigned), "bad bitfield packing");
105 assert(locNo() == LocNo && "location truncation");
106 }
107
Jeremy Morseed29dba2019-10-15 10:46:24 +0000108 DbgValueLocation() : LocNo(0) {}
Reid Kleckner04e25e02017-10-03 17:59:02 +0000109
110 unsigned locNo() const {
111 // Fix up the undef location number, which gets truncated.
112 return LocNo == INT_MAX ? UndefLocNo : LocNo;
113 }
Reid Kleckner04e25e02017-10-03 17:59:02 +0000114 bool isUndef() const { return locNo() == UndefLocNo; }
115
116 DbgValueLocation changeLocNo(unsigned NewLocNo) const {
Jeremy Morseed29dba2019-10-15 10:46:24 +0000117 return DbgValueLocation(NewLocNo);
Reid Kleckner04e25e02017-10-03 17:59:02 +0000118 }
119
Reid Klecknerb4569de72017-10-03 18:30:11 +0000120 friend inline bool operator==(const DbgValueLocation &LHS,
121 const DbgValueLocation &RHS) {
Jeremy Morseed29dba2019-10-15 10:46:24 +0000122 return LHS.LocNo == RHS.LocNo;
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:
Jeremy Morseed29dba2019-10-15 10:46:24 +0000131 unsigned LocNo;
Reid Kleckner04e25e02017-10-03 17:59:02 +0000132};
133
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000134/// Map of where a user value is live, and its location.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000135using LocMap = IntervalMap<SlotIndex, DbgValueLocation, 4>;
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000136
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000137/// Map of stack slot offsets for spilled locations.
David Stenberg45acc962018-09-07 13:54:07 +0000138/// Non-spilled locations are not added to the map.
139using SpillOffsetMap = DenseMap<unsigned, unsigned>;
140
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000141namespace {
142
143class LDVImpl;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000144
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000145/// A user value is a part of a debug info user variable.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000146///
147/// A DBG_VALUE instruction notes that (a sub-register of) a virtual register
148/// holds part of a user variable. The part is identified by a byte offset.
Andrea Di Biagio67720e72019-10-29 12:04:32 +0000149///
150/// UserValues are grouped into equivalence classes for easier searching. Two
151/// user values are related if they refer to the same variable, or if they are
152/// held by the same virtual register. The equivalence class is the transitive
153/// closure of that relation.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000154class UserValue {
Reid Kleckner4e040282017-09-20 18:19:08 +0000155 const DILocalVariable *Variable; ///< The debug info variable we are part of.
156 const DIExpression *Expression; ///< Any complex address expression.
Devang Patel26ffa012011-02-04 01:43:25 +0000157 DebugLoc dl; ///< The debug location for the variable. This is
158 ///< used by dwarf writer to find lexical scope.
Andrea Di Biagio67720e72019-10-29 12:04:32 +0000159 UserValue *leader; ///< Equivalence class leader.
160 UserValue *next = nullptr; ///< Next value in equivalence class, or null.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000161
162 /// Numbered locations referenced by locmap.
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000163 SmallVector<MachineOperand, 4> locations;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000164
165 /// Map of slot indices where this value is live.
166 LocMap locInts;
167
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000168 /// Insert a DBG_VALUE into MBB at Idx for LocNo.
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +0000169 void insertDebugValue(MachineBasicBlock *MBB, SlotIndex StartIdx,
David Stenberg45acc962018-09-07 13:54:07 +0000170 SlotIndex StopIdx, DbgValueLocation Loc, bool Spilled,
171 unsigned SpillOffset, LiveIntervals &LIS,
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +0000172 const TargetInstrInfo &TII,
173 const TargetRegisterInfo &TRI);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000174
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000175 /// Replace OldLocNo ranges with NewRegs ranges where NewRegs
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000176 /// is live. Returns true if any changes were made.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000177 bool splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
178 LiveIntervals &LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000179
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000180public:
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000181 /// Create a new UserValue.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000182 UserValue(const DILocalVariable *var, const DIExpression *expr, DebugLoc L,
183 LocMap::Allocator &alloc)
Andrea Di Biagio67720e72019-10-29 12:04:32 +0000184 : Variable(var), Expression(expr), dl(std::move(L)), leader(this),
185 locInts(alloc) {}
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000186
Andrea Di Biagio67720e72019-10-29 12:04:32 +0000187 /// Get the leader of this value's equivalence class.
188 UserValue *getLeader() {
189 UserValue *l = leader;
190 while (l != l->leader)
191 l = l->leader;
192 return leader = l;
193 }
194
195 /// Return the next UserValue in the equivalence class.
196 UserValue *getNext() const { return next; }
197
198 /// Does this UserValue match the parameters?
199 bool match(const DILocalVariable *Var, const DIExpression *Expr,
200 const DILocation *IA) const {
201 // FIXME: The fragment should be part of the equivalence class, but not
202 // other things in the expression like stack values.
203 return Var == Variable && Expr == Expression && dl->getInlinedAt() == IA;
204 }
205
206 /// Merge equivalence classes.
207 static UserValue *merge(UserValue *L1, UserValue *L2) {
208 L2 = L2->getLeader();
209 if (!L1)
210 return L2;
211 L1 = L1->getLeader();
212 if (L1 == L2)
213 return L1;
214 // Splice L2 before L1's members.
215 UserValue *End = L2;
216 while (End->next) {
217 End->leader = L1;
218 End = End->next;
219 }
220 End->leader = L1;
221 End->next = L1->next;
222 L1->next = L2;
223 return L1;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000224 }
225
Mikael Holmen57b33f62018-06-21 07:02:46 +0000226 /// Return the location number that matches Loc.
227 ///
228 /// For undef values we always return location number UndefLocNo without
229 /// inserting anything in locations. Since locations is a vector and the
230 /// location number is the position in the vector and UndefLocNo is ~0,
231 /// we would need a very big vector to put the value at the right position.
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000232 unsigned getLocationNo(const MachineOperand &LocMO) {
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000233 if (LocMO.isReg()) {
234 if (LocMO.getReg() == 0)
Reid Klecknereed09732017-09-15 22:08:50 +0000235 return UndefLocNo;
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000236 // For register locations we dont care about use/def and other flags.
237 for (unsigned i = 0, e = locations.size(); i != e; ++i)
238 if (locations[i].isReg() &&
239 locations[i].getReg() == LocMO.getReg() &&
240 locations[i].getSubReg() == LocMO.getSubReg())
241 return i;
242 } else
243 for (unsigned i = 0, e = locations.size(); i != e; ++i)
244 if (LocMO.isIdenticalTo(locations[i]))
245 return i;
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000246 locations.push_back(LocMO);
247 // We are storing a MachineOperand outside a MachineInstr.
248 locations.back().clearParent();
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000249 // Don't store def operands.
Geoff Berryb3d126d2017-12-29 21:01:09 +0000250 if (locations.back().isReg()) {
251 if (locations.back().isDef())
252 locations.back().setIsDead(false);
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000253 locations.back().setIsUse();
Geoff Berryb3d126d2017-12-29 21:01:09 +0000254 }
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000255 return locations.size() - 1;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000256 }
257
Bjorn Pettersson56c22932019-10-25 19:03:18 +0200258 /// Remove (recycle) a location number. If \p LocNo still is used by the
259 /// locInts nothing is done.
260 void removeLocationIfUnused(unsigned LocNo) {
261 // Bail out if LocNo still is used.
262 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
263 DbgValueLocation Loc = I.value();
264 if (Loc.locNo() == LocNo)
265 return;
266 }
267 // Remove the entry in the locations vector, and adjust all references to
268 // location numbers above the removed entry.
269 locations.erase(locations.begin() + LocNo);
270 for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) {
271 DbgValueLocation Loc = I.value();
272 if (!Loc.isUndef() && Loc.locNo() > LocNo)
273 I.setValueUnchecked(Loc.changeLocNo(Loc.locNo() - 1));
274 }
275 }
276
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000277 /// Ensure that all virtual register locations are mapped.
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000278 void mapVirtRegs(LDVImpl *LDV);
279
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000280 /// Add a definition point to this value.
Jeremy Morseed29dba2019-10-15 10:46:24 +0000281 void addDef(SlotIndex Idx, const MachineOperand &LocMO) {
282 DbgValueLocation Loc(getLocationNo(LocMO));
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000283 // Add a singular (Idx,Idx) -> Loc mapping.
284 LocMap::iterator I = locInts.find(Idx);
285 if (!I.valid() || I.start() != Idx)
Reid Kleckner04e25e02017-10-03 17:59:02 +0000286 I.insert(Idx, Idx.getNextSlot(), Loc);
Jakob Stoklund Olesen2539af62011-08-03 23:44:31 +0000287 else
288 // A later DBG_VALUE at the same SlotIndex overrides the old location.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000289 I.setValue(Loc);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000290 }
291
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000292 /// Extend the current definition as far as possible down.
293 ///
Adrian Prantlf8d10ce2016-09-28 21:34:23 +0000294 /// Stop when meeting an existing def or when leaving the live
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000295 /// range of VNI. End points where VNI is no longer live are added to Kills.
296 ///
297 /// We only propagate DBG_VALUES locally here. LiveDebugValues performs a
298 /// data-flow analysis to propagate them beyond basic block boundaries.
299 ///
300 /// \param Idx Starting point for the definition.
301 /// \param Loc Location number to propagate.
302 /// \param LR Restrict liveness to where LR has the value VNI. May be null.
303 /// \param VNI When LR is not null, this is the value to restrict to.
304 /// \param [out] Kills Append end points of VNI's live range to Kills.
305 /// \param LIS Live intervals analysis.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000306 void extendDef(SlotIndex Idx, DbgValueLocation Loc,
Matthias Braun34e1be92013-10-10 21:29:02 +0000307 LiveRange *LR, const VNInfo *VNI,
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000308 SmallVectorImpl<SlotIndex> *Kills,
Adrian Prantlf8d10ce2016-09-28 21:34:23 +0000309 LiveIntervals &LIS);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000310
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000311 /// The value in LI/LocNo may be copies to other registers. Determine if
312 /// any of the copies are available at the kill points, and add defs if
313 /// possible.
314 ///
315 /// \param LI Scan for copies of the value in LI->reg.
316 /// \param LocNo Location number of LI->reg.
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000317 /// \param Kills Points where the range of LocNo could be extended.
318 /// \param [in,out] NewDefs Append (Idx, LocNo) of inserted defs here.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000319 void addDefsFromCopies(
Jeremy Morseed29dba2019-10-15 10:46:24 +0000320 LiveInterval *LI, unsigned LocNo,
Reid Kleckner04e25e02017-10-03 17:59:02 +0000321 const SmallVectorImpl<SlotIndex> &Kills,
322 SmallVectorImpl<std::pair<SlotIndex, DbgValueLocation>> &NewDefs,
323 MachineRegisterInfo &MRI, LiveIntervals &LIS);
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000324
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000325 /// Compute the live intervals of all locations after collecting all their
326 /// def points.
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000327 void computeIntervals(MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
Robert Lougher10f740d2017-08-03 11:54:02 +0000328 LiveIntervals &LIS, LexicalScopes &LS);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000329
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000330 /// Replace OldReg ranges with NewRegs ranges where NewRegs is
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000331 /// live. Returns true if any changes were made.
Fangrui Songcb0bab82018-07-16 18:51:40 +0000332 bool splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs,
Mark Laceyf9ea8852013-08-14 23:50:04 +0000333 LiveIntervals &LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000334
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000335 /// Rewrite virtual register locations according to the provided virtual
336 /// register map. Record the stack slot offsets for the locations that
337 /// were spilled.
David Stenberg45acc962018-09-07 13:54:07 +0000338 void rewriteLocations(VirtRegMap &VRM, const MachineFunction &MF,
339 const TargetInstrInfo &TII,
340 const TargetRegisterInfo &TRI,
341 SpillOffsetMap &SpillOffsets);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000342
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000343 /// Recreate DBG_VALUE instruction from data structures.
Reid Kleckner4e040282017-09-20 18:19:08 +0000344 void emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +0000345 const TargetInstrInfo &TII,
346 const TargetRegisterInfo &TRI,
David Stenberg45acc962018-09-07 13:54:07 +0000347 const SpillOffsetMap &SpillOffsets);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000348
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000349 /// Return DebugLoc of this UserValue.
Devang Patelf9e2ae92011-09-13 18:40:53 +0000350 DebugLoc getDebugLoc() { return dl;}
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000351
Eric Christopher1cdefae2015-02-27 00:11:34 +0000352 void print(raw_ostream &, const TargetRegisterInfo *);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000353};
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000354
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000355/// A user label is a part of a debug info user label.
356class UserLabel {
357 const DILabel *Label; ///< The debug info label we are part of.
358 DebugLoc dl; ///< The debug location for the label. This is
359 ///< used by dwarf writer to find lexical scope.
360 SlotIndex loc; ///< Slot used by the debug label.
361
362 /// Insert a DBG_LABEL into MBB at Idx.
363 void insertDebugLabel(MachineBasicBlock *MBB, SlotIndex Idx,
364 LiveIntervals &LIS, const TargetInstrInfo &TII);
365
366public:
367 /// Create a new UserLabel.
368 UserLabel(const DILabel *label, DebugLoc L, SlotIndex Idx)
369 : Label(label), dl(std::move(L)), loc(Idx) {}
370
371 /// Does this UserLabel match the parameters?
372 bool match(const DILabel *L, const DILocation *IA,
373 const SlotIndex Index) const {
374 return Label == L && dl->getInlinedAt() == IA && loc == Index;
375 }
376
377 /// Recreate DBG_LABEL instruction from data structures.
378 void emitDebugLabel(LiveIntervals &LIS, const TargetInstrInfo &TII);
379
380 /// Return DebugLoc of this UserLabel.
381 DebugLoc getDebugLoc() { return dl; }
382
383 void print(raw_ostream &, const TargetRegisterInfo *);
384};
385
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000386/// Implementation of the LiveDebugVariables pass.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000387class LDVImpl {
388 LiveDebugVariables &pass;
389 LocMap::Allocator allocator;
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000390 MachineFunction *MF = nullptr;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000391 LiveIntervals *LIS;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000392 const TargetRegisterInfo *TRI;
393
Manman Ren7a4c8a72013-02-13 20:23:48 +0000394 /// Whether emitDebugValues is called.
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000395 bool EmitDone = false;
396
Manman Ren7a4c8a72013-02-13 20:23:48 +0000397 /// Whether the machine function is modified during the pass.
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000398 bool ModifiedMF = false;
Manman Ren7a4c8a72013-02-13 20:23:48 +0000399
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000400 /// All allocated UserValue instances.
David Blaikie2b1dfa72014-04-21 20:37:07 +0000401 SmallVector<std::unique_ptr<UserValue>, 8> userValues;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000402
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000403 /// All allocated UserLabel instances.
404 SmallVector<std::unique_ptr<UserLabel>, 2> userLabels;
405
Andrea Di Biagio67720e72019-10-29 12:04:32 +0000406 /// Map virtual register to eq class leader.
407 using VRMap = DenseMap<unsigned, UserValue *>;
408 VRMap virtRegToEqClass;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000409
Andrea Di Biagio67720e72019-10-29 12:04:32 +0000410 /// Map user variable to eq class leader.
411 using UVMap = DenseMap<const DILocalVariable *, UserValue *>;
412 UVMap userVarMap;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000413
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000414 /// Find or create a UserValue.
Reid Kleckner4e040282017-09-20 18:19:08 +0000415 UserValue *getUserValue(const DILocalVariable *Var, const DIExpression *Expr,
Reid Kleckner04e25e02017-10-03 17:59:02 +0000416 const DebugLoc &DL);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000417
Andrea Di Biagio67720e72019-10-29 12:04:32 +0000418 /// Find the EC leader for VirtReg or null.
419 UserValue *lookupVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000420
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000421 /// Add DBG_VALUE instruction to our maps.
422 ///
423 /// \param MI DBG_VALUE instruction
424 /// \param Idx Last valid SLotIndex before instruction.
425 ///
426 /// \returns True if the DBG_VALUE instruction should be deleted.
Duncan P. N. Exon Smithfb612ac2016-06-30 23:13:38 +0000427 bool handleDebugValue(MachineInstr &MI, SlotIndex Idx);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000428
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000429 /// Add DBG_LABEL instruction to UserLabel.
430 ///
431 /// \param MI DBG_LABEL instruction
432 /// \param Idx Last valid SlotIndex before instruction.
433 ///
434 /// \returns True if the DBG_LABEL instruction should be deleted.
435 bool handleDebugLabel(MachineInstr &MI, SlotIndex Idx);
436
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000437 /// Collect and erase all DBG_VALUE instructions, adding a UserValue def
438 /// for each instruction.
439 ///
440 /// \param mf MachineFunction to be scanned.
441 ///
442 /// \returns True if any debug values were found.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000443 bool collectDebugValues(MachineFunction &mf);
444
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000445 /// Compute the live intervals of all user values after collecting all
446 /// their def points.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000447 void computeIntervals();
448
449public:
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000450 LDVImpl(LiveDebugVariables *ps) : pass(*ps) {}
451
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000452 bool runOnMachineFunction(MachineFunction &mf);
453
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000454 /// Release all memory.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000455 void clear() {
David Blaikie2f040112014-07-25 16:10:16 +0000456 MF = nullptr;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000457 userValues.clear();
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000458 userLabels.clear();
Andrea Di Biagio67720e72019-10-29 12:04:32 +0000459 virtRegToEqClass.clear();
460 userVarMap.clear();
Manman Ren7a4c8a72013-02-13 20:23:48 +0000461 // Make sure we call emitDebugValues if the machine function was modified.
462 assert((!ModifiedMF || EmitDone) &&
463 "Dbg values are not emitted in LDV");
464 EmitDone = false;
465 ModifiedMF = false;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000466 }
467
Andrea Di Biagio67720e72019-10-29 12:04:32 +0000468 /// Map virtual register to an equivalence class.
469 void mapVirtReg(unsigned VirtReg, UserValue *EC);
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000470
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000471 /// Replace all references to OldReg with NewRegs.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000472 void splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000473
Hsiangkai Wangd72f6f1332018-11-30 08:07:24 +0000474 /// Recreate DBG_VALUE instruction from data structures.
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000475 void emitDebugValues(VirtRegMap *VRM);
476
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000477 void print(raw_ostream&);
478};
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000479
480} // end anonymous namespace
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000481
Aaron Ballman615eb472017-10-15 14:32:27 +0000482#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000483static void printDebugLoc(const DebugLoc &DL, raw_ostream &CommentOS,
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000484 const LLVMContext &Ctx) {
485 if (!DL)
486 return;
487
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000488 auto *Scope = cast<DIScope>(DL.getScope());
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000489 // Omit the directory, because it's likely to be long and uninteresting.
Duncan P. N. Exon Smithb273d062015-04-16 01:37:00 +0000490 CommentOS << Scope->getFilename();
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000491 CommentOS << ':' << DL.getLine();
492 if (DL.getCol() != 0)
493 CommentOS << ':' << DL.getCol();
494
495 DebugLoc InlinedAtDL = DL.getInlinedAt();
496 if (!InlinedAtDL)
497 return;
498
499 CommentOS << " @[ ";
500 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
501 CommentOS << " ]";
502}
503
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000504static void printExtendedName(raw_ostream &OS, const DINode *Node,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000505 const DILocation *DL) {
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000506 const LLVMContext &Ctx = Node->getContext();
507 StringRef Res;
508 unsigned Line;
509 if (const auto *V = dyn_cast<const DILocalVariable>(Node)) {
510 Res = V->getName();
511 Line = V->getLine();
512 } else if (const auto *L = dyn_cast<const DILabel>(Node)) {
513 Res = L->getName();
514 Line = L->getLine();
515 }
516
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000517 if (!Res.empty())
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000518 OS << Res << "," << Line;
Pengfei Wangc05aad02019-05-09 08:09:21 +0000519 auto *InlinedAt = DL ? DL->getInlinedAt() : nullptr;
520 if (InlinedAt) {
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000521 if (DebugLoc InlinedAtDL = InlinedAt) {
522 OS << " @[";
523 printDebugLoc(InlinedAtDL, OS, Ctx);
524 OS << "]";
525 }
526 }
527}
528
Eric Christopher1cdefae2015-02-27 00:11:34 +0000529void UserValue::print(raw_ostream &OS, const TargetRegisterInfo *TRI) {
Frederic Risse6bb1872014-08-07 20:04:00 +0000530 OS << "!\"";
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000531 printExtendedName(OS, Variable, dl);
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000532
Devang Patel6c1ed312011-08-09 01:03:35 +0000533 OS << "\"\t";
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000534 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
535 OS << " [" << I.start() << ';' << I.stop() << "):";
Reid Kleckner04e25e02017-10-03 17:59:02 +0000536 if (I.value().isUndef())
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000537 OS << "undef";
Reid Kleckner04e25e02017-10-03 17:59:02 +0000538 else {
539 OS << I.value().locNo();
Reid Kleckner04e25e02017-10-03 17:59:02 +0000540 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000541 }
Jakob Stoklund Olesenc86fe052011-05-06 17:59:59 +0000542 for (unsigned i = 0, e = locations.size(); i != e; ++i) {
543 OS << " Loc" << i << '=';
Eric Christopher1cdefae2015-02-27 00:11:34 +0000544 locations[i].print(OS, TRI);
Jakob Stoklund Olesenc86fe052011-05-06 17:59:59 +0000545 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000546 OS << '\n';
547}
548
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000549void UserLabel::print(raw_ostream &OS, const TargetRegisterInfo *TRI) {
550 OS << "!\"";
551 printExtendedName(OS, Label, dl);
552
553 OS << "\"\t";
554 OS << loc;
555 OS << '\n';
556}
557
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000558void LDVImpl::print(raw_ostream &OS) {
559 OS << "********** DEBUG VARIABLES **********\n";
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000560 for (auto &userValue : userValues)
561 userValue->print(OS, TRI);
562 OS << "********** DEBUG LABELS **********\n";
563 for (auto &userLabel : userLabels)
564 userLabel->print(OS, TRI);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000565}
Florian Hahn6b3216a2017-07-31 10:07:49 +0000566#endif
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000567
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000568void UserValue::mapVirtRegs(LDVImpl *LDV) {
569 for (unsigned i = 0, e = locations.size(); i != e; ++i)
570 if (locations[i].isReg() &&
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000571 Register::isVirtualRegister(locations[i].getReg()))
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000572 LDV->mapVirtReg(locations[i].getReg(), this);
573}
574
Reid Kleckner4e040282017-09-20 18:19:08 +0000575UserValue *LDVImpl::getUserValue(const DILocalVariable *Var,
Reid Kleckner04e25e02017-10-03 17:59:02 +0000576 const DIExpression *Expr, const DebugLoc &DL) {
Andrea Di Biagio67720e72019-10-29 12:04:32 +0000577 UserValue *&Leader = userVarMap[Var];
578 if (Leader) {
579 UserValue *UV = Leader->getLeader();
580 Leader = UV;
581 for (; UV; UV = UV->getNext())
582 if (UV->match(Var, Expr, DL->getInlinedAt()))
583 return UV;
584 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000585
Andrea Di Biagio67720e72019-10-29 12:04:32 +0000586 userValues.push_back(
587 std::make_unique<UserValue>(Var, Expr, DL, allocator));
588 UserValue *UV = userValues.back().get();
589 Leader = UserValue::merge(Leader, UV);
590 return UV;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000591}
592
Andrea Di Biagio67720e72019-10-29 12:04:32 +0000593void LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *EC) {
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000594 assert(Register::isVirtualRegister(VirtReg) && "Only map VirtRegs");
Andrea Di Biagio67720e72019-10-29 12:04:32 +0000595 UserValue *&Leader = virtRegToEqClass[VirtReg];
596 Leader = UserValue::merge(Leader, EC);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000597}
598
Andrea Di Biagio67720e72019-10-29 12:04:32 +0000599UserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) {
600 if (UserValue *UV = virtRegToEqClass.lookup(VirtReg))
601 return UV->getLeader();
Craig Topperc0196b12014-04-14 00:51:57 +0000602 return nullptr;
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000603}
604
Duncan P. N. Exon Smithfb612ac2016-06-30 23:13:38 +0000605bool LDVImpl::handleDebugValue(MachineInstr &MI, SlotIndex Idx) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000606 // DBG_VALUE loc, offset, variable
Duncan P. N. Exon Smithfb612ac2016-06-30 23:13:38 +0000607 if (MI.getNumOperands() != 4 ||
608 !(MI.getOperand(1).isReg() || MI.getOperand(1).isImm()) ||
609 !MI.getOperand(2).isMetadata()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000610 LLVM_DEBUG(dbgs() << "Can't handle " << MI);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000611 return false;
612 }
613
Bjorn Petterssonbdf0c002018-03-06 08:47:07 +0000614 // Detect invalid DBG_VALUE instructions, with a debug-use of a virtual
615 // register that hasn't been defined yet. If we do not remove those here, then
616 // the re-insertion of the DBG_VALUE instruction after register allocation
617 // will be incorrect.
618 // TODO: If earlier passes are corrected to generate sane debug information
619 // (and if the machine verifier is improved to catch this), then these checks
620 // could be removed or replaced by asserts.
621 bool Discard = false;
622 if (MI.getOperand(0).isReg() &&
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000623 Register::isVirtualRegister(MI.getOperand(0).getReg())) {
Daniel Sanders0c476112019-08-15 19:22:08 +0000624 const Register Reg = MI.getOperand(0).getReg();
Bjorn Petterssonbdf0c002018-03-06 08:47:07 +0000625 if (!LIS->hasInterval(Reg)) {
626 // The DBG_VALUE is described by a virtual register that does not have a
627 // live interval. Discard the DBG_VALUE.
628 Discard = true;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000629 LLVM_DEBUG(dbgs() << "Discarding debug info (no LIS interval): " << Idx
630 << " " << MI);
Bjorn Petterssonbdf0c002018-03-06 08:47:07 +0000631 } else {
632 // The DBG_VALUE is only valid if either Reg is live out from Idx, or Reg
633 // is defined dead at Idx (where Idx is the slot index for the instruction
Jordan Rupprecht6387fa22019-02-23 01:28:32 +0000634 // preceding the DBG_VALUE).
Bjorn Petterssonbdf0c002018-03-06 08:47:07 +0000635 const LiveInterval &LI = LIS->getInterval(Reg);
636 LiveQueryResult LRQ = LI.Query(Idx);
637 if (!LRQ.valueOutOrDead()) {
638 // We have found a DBG_VALUE with the value in a virtual register that
639 // is not live. Discard the DBG_VALUE.
640 Discard = true;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000641 LLVM_DEBUG(dbgs() << "Discarding debug info (reg not live): " << Idx
642 << " " << MI);
Bjorn Petterssonbdf0c002018-03-06 08:47:07 +0000643 }
644 }
645 }
646
Reid Kleckner04e25e02017-10-03 17:59:02 +0000647 // Get or create the UserValue for (variable,offset) here.
Jeremy Morseed29dba2019-10-15 10:46:24 +0000648 assert(!MI.getOperand(1).isImm() && "DBG_VALUE with indirect flag before "
649 "LiveDebugVariables");
Reid Kleckner4e040282017-09-20 18:19:08 +0000650 const DILocalVariable *Var = MI.getDebugVariable();
651 const DIExpression *Expr = MI.getDebugExpression();
Reid Kleckner04e25e02017-10-03 17:59:02 +0000652 UserValue *UV =
653 getUserValue(Var, Expr, MI.getDebugLoc());
Bjorn Petterssonbdf0c002018-03-06 08:47:07 +0000654 if (!Discard)
Jeremy Morseed29dba2019-10-15 10:46:24 +0000655 UV->addDef(Idx, MI.getOperand(0));
Bjorn Petterssone0050d72018-03-06 13:23:28 +0000656 else {
657 MachineOperand MO = MachineOperand::CreateReg(0U, false);
658 MO.setIsDebug();
Jeremy Morseed29dba2019-10-15 10:46:24 +0000659 UV->addDef(Idx, MO);
Bjorn Petterssone0050d72018-03-06 13:23:28 +0000660 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000661 return true;
662}
663
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000664bool LDVImpl::handleDebugLabel(MachineInstr &MI, SlotIndex Idx) {
665 // DBG_LABEL label
666 if (MI.getNumOperands() != 1 || !MI.getOperand(0).isMetadata()) {
667 LLVM_DEBUG(dbgs() << "Can't handle " << MI);
668 return false;
669 }
670
671 // Get or create the UserLabel for label here.
672 const DILabel *Label = MI.getDebugLabel();
673 const DebugLoc &DL = MI.getDebugLoc();
674 bool Found = false;
675 for (auto const &L : userLabels) {
676 if (L->match(Label, DL->getInlinedAt(), Idx)) {
677 Found = true;
678 break;
679 }
680 }
681 if (!Found)
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000682 userLabels.push_back(std::make_unique<UserLabel>(Label, DL, Idx));
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000683
684 return true;
685}
686
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000687bool LDVImpl::collectDebugValues(MachineFunction &mf) {
688 bool Changed = false;
689 for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE;
690 ++MFI) {
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +0000691 MachineBasicBlock *MBB = &*MFI;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000692 for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
693 MBBI != MBBE;) {
Hsiangkai Wangb2b7f5f2018-09-05 05:58:53 +0000694 // Use the first debug instruction in the sequence to get a SlotIndex
695 // for following consecutive debug instructions.
696 if (!MBBI->isDebugInstr()) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000697 ++MBBI;
698 continue;
699 }
Hsiangkai Wangb2b7f5f2018-09-05 05:58:53 +0000700 // Debug instructions has no slot index. Use the previous
701 // non-debug instruction's SlotIndex as its SlotIndex.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000702 SlotIndex Idx =
703 MBBI == MBB->begin()
704 ? LIS->getMBBStartIdx(MBB)
705 : LIS->getInstructionIndex(*std::prev(MBBI)).getRegSlot();
Hsiangkai Wangb2b7f5f2018-09-05 05:58:53 +0000706 // Handle consecutive debug instructions with the same slot index.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000707 do {
Hsiangkai Wangb2b7f5f2018-09-05 05:58:53 +0000708 // Only handle DBG_VALUE in handleDebugValue(). Skip all other
709 // kinds of debug instructions.
Hsiangkai Wang66609a82019-01-18 07:17:09 +0000710 if ((MBBI->isDebugValue() && handleDebugValue(*MBBI, Idx)) ||
711 (MBBI->isDebugLabel() && handleDebugLabel(*MBBI, Idx))) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000712 MBBI = MBB->erase(MBBI);
713 Changed = true;
714 } else
715 ++MBBI;
Hsiangkai Wangb2b7f5f2018-09-05 05:58:53 +0000716 } while (MBBI != MBBE && MBBI->isDebugInstr());
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000717 }
718 }
719 return Changed;
720}
721
Reid Kleckner04e25e02017-10-03 17:59:02 +0000722void UserValue::extendDef(SlotIndex Idx, DbgValueLocation Loc, LiveRange *LR,
Adrian Prantlce858132015-12-21 20:03:00 +0000723 const VNInfo *VNI, SmallVectorImpl<SlotIndex> *Kills,
Adrian Prantlf8d10ce2016-09-28 21:34:23 +0000724 LiveIntervals &LIS) {
Adrian Prantlce858132015-12-21 20:03:00 +0000725 SlotIndex Start = Idx;
726 MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start);
727 SlotIndex Stop = LIS.getMBBEndIdx(MBB);
728 LocMap::iterator I = locInts.find(Start);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000729
Adrian Prantlce858132015-12-21 20:03:00 +0000730 // Limit to VNI's live range.
731 bool ToEnd = true;
732 if (LR && VNI) {
733 LiveInterval::Segment *Segment = LR->getSegmentContaining(Start);
734 if (!Segment || Segment->valno != VNI) {
735 if (Kills)
736 Kills->push_back(Start);
737 return;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000738 }
Richard Trieu7a083812016-02-18 22:09:30 +0000739 if (Segment->end < Stop) {
740 Stop = Segment->end;
741 ToEnd = false;
742 }
Adrian Prantlce858132015-12-21 20:03:00 +0000743 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000744
Adrian Prantlce858132015-12-21 20:03:00 +0000745 // There could already be a short def at Start.
746 if (I.valid() && I.start() <= Start) {
747 // Stop when meeting a different location or an already extended interval.
748 Start = Start.getNextSlot();
Reid Kleckner04e25e02017-10-03 17:59:02 +0000749 if (I.value() != Loc || I.stop() != Start)
Adrian Prantlce858132015-12-21 20:03:00 +0000750 return;
751 // This is a one-slot placeholder. Just skip it.
752 ++I;
753 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000754
Adrian Prantlce858132015-12-21 20:03:00 +0000755 // Limited by the next def.
Fangrui Songb251cc02019-07-12 14:58:15 +0000756 if (I.valid() && I.start() < Stop)
Richard Trieu7a083812016-02-18 22:09:30 +0000757 Stop = I.start();
Adrian Prantlce858132015-12-21 20:03:00 +0000758 // Limited by VNI's live range.
759 else if (!ToEnd && Kills)
760 Kills->push_back(Stop);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000761
Adrian Prantlce858132015-12-21 20:03:00 +0000762 if (Start < Stop)
Reid Kleckner04e25e02017-10-03 17:59:02 +0000763 I.insert(Start, Stop, Loc);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000764}
765
Reid Kleckner04e25e02017-10-03 17:59:02 +0000766void UserValue::addDefsFromCopies(
Jeremy Morseed29dba2019-10-15 10:46:24 +0000767 LiveInterval *LI, unsigned LocNo,
Reid Kleckner04e25e02017-10-03 17:59:02 +0000768 const SmallVectorImpl<SlotIndex> &Kills,
769 SmallVectorImpl<std::pair<SlotIndex, DbgValueLocation>> &NewDefs,
770 MachineRegisterInfo &MRI, LiveIntervals &LIS) {
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000771 if (Kills.empty())
772 return;
773 // Don't track copies from physregs, there are too many uses.
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000774 if (!Register::isVirtualRegister(LI->reg))
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000775 return;
776
777 // Collect all the (vreg, valno) pairs that are copies of LI.
778 SmallVector<std::pair<LiveInterval*, const VNInfo*>, 8> CopyValues;
Owen Andersonb36376e2014-03-17 19:36:09 +0000779 for (MachineOperand &MO : MRI.use_nodbg_operands(LI->reg)) {
780 MachineInstr *MI = MO.getParent();
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000781 // Copies of the full value.
Owen Andersonb36376e2014-03-17 19:36:09 +0000782 if (MO.getSubReg() || !MI->isCopy())
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000783 continue;
Daniel Sanders0c476112019-08-15 19:22:08 +0000784 Register DstReg = MI->getOperand(0).getReg();
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000785
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +0000786 // Don't follow copies to physregs. These are usually setting up call
787 // arguments, and the argument registers are always call clobbered. We are
788 // better off in the source register which could be a callee-saved register,
789 // or it could be spilled.
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000790 if (!Register::isVirtualRegister(DstReg))
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +0000791 continue;
792
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000793 // Is LocNo extended to reach this copy? If not, another def may be blocking
794 // it, or we are looking at a wrong value of LI.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000795 SlotIndex Idx = LIS.getInstructionIndex(*MI);
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000796 LocMap::iterator I = locInts.find(Idx.getRegSlot(true));
Reid Kleckner04e25e02017-10-03 17:59:02 +0000797 if (!I.valid() || I.value().locNo() != LocNo)
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000798 continue;
799
800 if (!LIS.hasInterval(DstReg))
801 continue;
802 LiveInterval *DstLI = &LIS.getInterval(DstReg);
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000803 const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx.getRegSlot());
804 assert(DstVNI && DstVNI->def == Idx.getRegSlot() && "Bad copy value");
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000805 CopyValues.push_back(std::make_pair(DstLI, DstVNI));
806 }
807
808 if (CopyValues.empty())
809 return;
810
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000811 LLVM_DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI
812 << '\n');
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000813
814 // Try to add defs of the copied values for each kill point.
815 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
816 SlotIndex Idx = Kills[i];
817 for (unsigned j = 0, e = CopyValues.size(); j != e; ++j) {
818 LiveInterval *DstLI = CopyValues[j].first;
819 const VNInfo *DstVNI = CopyValues[j].second;
820 if (DstLI->getVNInfoAt(Idx) != DstVNI)
821 continue;
822 // Check that there isn't already a def at Idx
823 LocMap::iterator I = locInts.find(Idx);
824 if (I.valid() && I.start() <= Idx)
825 continue;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000826 LLVM_DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #"
827 << DstVNI->id << " in " << *DstLI << '\n');
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000828 MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def);
829 assert(CopyMI && CopyMI->isCopy() && "Bad copy value");
830 unsigned LocNo = getLocationNo(CopyMI->getOperand(0));
Jeremy Morseed29dba2019-10-15 10:46:24 +0000831 DbgValueLocation NewLoc(LocNo);
Reid Kleckner04e25e02017-10-03 17:59:02 +0000832 I.insert(Idx, Idx.getNextSlot(), NewLoc);
833 NewDefs.push_back(std::make_pair(Idx, NewLoc));
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000834 break;
835 }
836 }
837}
838
Robert Lougher10f740d2017-08-03 11:54:02 +0000839void UserValue::computeIntervals(MachineRegisterInfo &MRI,
840 const TargetRegisterInfo &TRI,
841 LiveIntervals &LIS, LexicalScopes &LS) {
Reid Kleckner04e25e02017-10-03 17:59:02 +0000842 SmallVector<std::pair<SlotIndex, DbgValueLocation>, 16> Defs;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000843
844 // Collect all defs to be extended (Skipping undefs).
845 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I)
Reid Kleckner04e25e02017-10-03 17:59:02 +0000846 if (!I.value().isUndef())
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000847 Defs.push_back(std::make_pair(I.start(), I.value()));
848
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000849 // Extend all defs, and possibly add new ones along the way.
850 for (unsigned i = 0; i != Defs.size(); ++i) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000851 SlotIndex Idx = Defs[i].first;
Reid Kleckner04e25e02017-10-03 17:59:02 +0000852 DbgValueLocation Loc = Defs[i].second;
853 const MachineOperand &LocMO = locations[Loc.locNo()];
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000854
Reid Kleckner04e25e02017-10-03 17:59:02 +0000855 if (!LocMO.isReg()) {
856 extendDef(Idx, Loc, nullptr, nullptr, nullptr, LIS);
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000857 continue;
858 }
859
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000860 // Register locations are constrained to where the register value is live.
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000861 if (Register::isVirtualRegister(LocMO.getReg())) {
Craig Topperc0196b12014-04-14 00:51:57 +0000862 LiveInterval *LI = nullptr;
863 const VNInfo *VNI = nullptr;
Reid Kleckner04e25e02017-10-03 17:59:02 +0000864 if (LIS.hasInterval(LocMO.getReg())) {
865 LI = &LIS.getInterval(LocMO.getReg());
Jakob Stoklund Olesen48a16472012-06-22 18:51:35 +0000866 VNI = LI->getVNInfoAt(Idx);
867 }
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000868 SmallVector<SlotIndex, 16> Kills;
Reid Kleckner04e25e02017-10-03 17:59:02 +0000869 extendDef(Idx, Loc, LI, VNI, &Kills, LIS);
Bjorn Pettersson84830042018-08-25 10:02:03 +0000870 // FIXME: Handle sub-registers in addDefsFromCopies. The problem is that
871 // if the original location for example is %vreg0:sub_hi, and we find a
872 // full register copy in addDefsFromCopies (at the moment it only handles
873 // full register copies), then we must add the sub1 sub-register index to
874 // the new location. However, that is only possible if the new virtual
875 // register is of the same regclass (or if there is an equivalent
876 // sub-register in that regclass). For now, simply skip handling copies if
877 // a sub-register is involved.
878 if (LI && !LocMO.getSubReg())
Jeremy Morseed29dba2019-10-15 10:46:24 +0000879 addDefsFromCopies(LI, Loc.locNo(), Kills, Defs, MRI, LIS);
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000880 continue;
881 }
882
Bjorn Pettersson715a5ef2017-09-28 13:10:06 +0000883 // For physregs, we only mark the start slot idx. DwarfDebug will see it
884 // as if the DBG_VALUE is valid up until the end of the basic block, or
885 // the next def of the physical register. So we do not need to extend the
886 // range. It might actually happen that the DBG_VALUE is the last use of
887 // the physical register (e.g. if this is an unused input argument to a
888 // function).
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000889 }
890
Robert Lougher10f740d2017-08-03 11:54:02 +0000891 // The computed intervals may extend beyond the range of the debug
892 // location's lexical scope. In this case, splitting of an interval
893 // can result in an interval outside of the scope being created,
894 // causing extra unnecessary DBG_VALUEs to be emitted. To prevent
895 // this, trim the intervals to the lexical scope.
896
897 LexicalScope *Scope = LS.findLexicalScope(dl);
898 if (!Scope)
899 return;
900
901 SlotIndex PrevEnd;
902 LocMap::iterator I = locInts.begin();
903
904 // Iterate over the lexical scope ranges. Each time round the loop
905 // we check the intervals for overlap with the end of the previous
906 // range and the start of the next. The first range is handled as
907 // a special case where there is no PrevEnd.
908 for (const InsnRange &Range : Scope->getRanges()) {
909 SlotIndex RStart = LIS.getInstructionIndex(*Range.first);
910 SlotIndex REnd = LIS.getInstructionIndex(*Range.second);
911
912 // At the start of each iteration I has been advanced so that
913 // I.stop() >= PrevEnd. Check for overlap.
914 if (PrevEnd && I.start() < PrevEnd) {
915 SlotIndex IStop = I.stop();
Reid Kleckner04e25e02017-10-03 17:59:02 +0000916 DbgValueLocation Loc = I.value();
Robert Lougher10f740d2017-08-03 11:54:02 +0000917
918 // Stop overlaps previous end - trim the end of the interval to
919 // the scope range.
920 I.setStopUnchecked(PrevEnd);
921 ++I;
922
923 // If the interval also overlaps the start of the "next" (i.e.
Alexey Lapshinb9f1e7b2019-06-06 21:19:39 +0000924 // current) range create a new interval for the remainder
Robert Lougher10f740d2017-08-03 11:54:02 +0000925 if (RStart < IStop)
Reid Kleckner04e25e02017-10-03 17:59:02 +0000926 I.insert(RStart, IStop, Loc);
Robert Lougher10f740d2017-08-03 11:54:02 +0000927 }
928
929 // Advance I so that I.stop() >= RStart, and check for overlap.
930 I.advanceTo(RStart);
931 if (!I.valid())
932 return;
933
Robert Lougher10f740d2017-08-03 11:54:02 +0000934 // 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
Bjorn Pettersson56c22932019-10-25 19:03:18 +02001095 // Finally, remove OldLocNo unless it is still used by some interval in the
1096 // locInts map. One case when OldLocNo still is in use is when the register
1097 // has been spilled. In such situations the spilled register is kept as a
1098 // location until rewriteLocations is called (VirtRegMap is mapping the old
1099 // register to the spill slot). So for a while we can have locations that map
1100 // to virtual registers that have been removed from both the MachineFunction
1101 // and from LiveIntervals.
1102 removeLocationIfUnused(OldLocNo);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001103
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001104 LLVM_DEBUG({
1105 dbgs() << "Split result: \t";
1106 print(dbgs(), nullptr);
1107 });
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001108 return DidChange;
1109}
1110
1111bool
Mark Laceyf9ea8852013-08-14 23:50:04 +00001112UserValue::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs,
1113 LiveIntervals &LIS) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001114 bool DidChange = false;
Jakob Stoklund Olesen57c8f582011-05-06 19:31:19 +00001115 // Split locations referring to OldReg. Iterate backwards so splitLocation can
Eric Christopherbe153e62012-03-15 21:33:35 +00001116 // safely erase unused locations.
Jakob Stoklund Olesen57c8f582011-05-06 19:31:19 +00001117 for (unsigned i = locations.size(); i ; --i) {
1118 unsigned LocNo = i-1;
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001119 const MachineOperand *Loc = &locations[LocNo];
1120 if (!Loc->isReg() || Loc->getReg() != OldReg)
1121 continue;
Mark Laceyf9ea8852013-08-14 23:50:04 +00001122 DidChange |= splitLocation(LocNo, NewRegs, LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001123 }
1124 return DidChange;
1125}
1126
Mark Laceyf9ea8852013-08-14 23:50:04 +00001127void LDVImpl::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001128 bool DidChange = false;
Andrea Di Biagio67720e72019-10-29 12:04:32 +00001129 for (UserValue *UV = lookupVirtReg(OldReg); UV; UV = UV->getNext())
1130 DidChange |= UV->splitRegister(OldReg, NewRegs, *LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001131
1132 if (!DidChange)
1133 return;
1134
1135 // Map all of the new virtual registers.
Andrea Di Biagio67720e72019-10-29 12:04:32 +00001136 UserValue *UV = lookupVirtReg(OldReg);
1137 for (unsigned i = 0; i != NewRegs.size(); ++i)
1138 mapVirtReg(NewRegs[i], UV);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001139}
1140
1141void LiveDebugVariables::
Mark Laceyf9ea8852013-08-14 23:50:04 +00001142splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs, LiveIntervals &LIS) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001143 if (pImpl)
1144 static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs);
1145}
1146
David Stenberg45acc962018-09-07 13:54:07 +00001147void UserValue::rewriteLocations(VirtRegMap &VRM, const MachineFunction &MF,
1148 const TargetInstrInfo &TII,
1149 const TargetRegisterInfo &TRI,
1150 SpillOffsetMap &SpillOffsets) {
Reid Kleckner92687d42017-09-20 17:32:54 +00001151 // Build a set of new locations with new numbers so we can coalesce our
1152 // IntervalMap if two vreg intervals collapse to the same physical location.
1153 // Use MapVector instead of SetVector because MapVector::insert returns the
Reid Kleckner4e040282017-09-20 18:19:08 +00001154 // position of the previously or newly inserted element. The boolean value
1155 // tracks if the location was produced by a spill.
1156 // FIXME: This will be problematic if we ever support direct and indirect
1157 // frame index locations, i.e. expressing both variables in memory and
1158 // 'int x, *px = &x'. The "spilled" bit must become part of the location.
David Stenberg45acc962018-09-07 13:54:07 +00001159 MapVector<MachineOperand, std::pair<bool, unsigned>> NewLocations;
Reid Kleckner92687d42017-09-20 17:32:54 +00001160 SmallVector<unsigned, 4> LocNoMap(locations.size());
1161 for (unsigned I = 0, E = locations.size(); I != E; ++I) {
Reid Kleckner4e040282017-09-20 18:19:08 +00001162 bool Spilled = false;
David Stenberg45acc962018-09-07 13:54:07 +00001163 unsigned SpillOffset = 0;
Reid Kleckner92687d42017-09-20 17:32:54 +00001164 MachineOperand Loc = locations[I];
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001165 // Only virtual registers are rewritten.
Reid Kleckner92687d42017-09-20 17:32:54 +00001166 if (Loc.isReg() && Loc.getReg() &&
Daniel Sanders2bea69b2019-08-01 23:27:28 +00001167 Register::isVirtualRegister(Loc.getReg())) {
Daniel Sanders0c476112019-08-15 19:22:08 +00001168 Register VirtReg = Loc.getReg();
Reid Kleckner92687d42017-09-20 17:32:54 +00001169 if (VRM.isAssignedReg(VirtReg) &&
Daniel Sanders2bea69b2019-08-01 23:27:28 +00001170 Register::isPhysicalRegister(VRM.getPhys(VirtReg))) {
Reid Kleckner92687d42017-09-20 17:32:54 +00001171 // This can create a %noreg operand in rare cases when the sub-register
1172 // index is no longer available. That means the user value is in a
1173 // non-existent sub-register, and %noreg is exactly what we want.
1174 Loc.substPhysReg(VRM.getPhys(VirtReg), TRI);
1175 } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT) {
David Stenberg45acc962018-09-07 13:54:07 +00001176 // Retrieve the stack slot offset.
1177 unsigned SpillSize;
1178 const MachineRegisterInfo &MRI = MF.getRegInfo();
1179 const TargetRegisterClass *TRC = MRI.getRegClass(VirtReg);
1180 bool Success = TII.getStackSlotRange(TRC, Loc.getSubReg(), SpillSize,
1181 SpillOffset, MF);
1182
1183 // FIXME: Invalidate the location if the offset couldn't be calculated.
1184 (void)Success;
1185
Reid Kleckner92687d42017-09-20 17:32:54 +00001186 Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg));
Reid Kleckner4e040282017-09-20 18:19:08 +00001187 Spilled = true;
Reid Kleckner92687d42017-09-20 17:32:54 +00001188 } else {
1189 Loc.setReg(0);
1190 Loc.setSubReg(0);
1191 }
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001192 }
Reid Kleckner92687d42017-09-20 17:32:54 +00001193
1194 // Insert this location if it doesn't already exist and record a mapping
1195 // from the old number to the new number.
David Stenberg45acc962018-09-07 13:54:07 +00001196 auto InsertResult = NewLocations.insert({Loc, {Spilled, SpillOffset}});
Reid Kleckner4e040282017-09-20 18:19:08 +00001197 unsigned NewLocNo = std::distance(NewLocations.begin(), InsertResult.first);
1198 LocNoMap[I] = NewLocNo;
Reid Kleckner92687d42017-09-20 17:32:54 +00001199 }
1200
David Stenberg45acc962018-09-07 13:54:07 +00001201 // Rewrite the locations and record the stack slot offsets for spills.
Reid Kleckner92687d42017-09-20 17:32:54 +00001202 locations.clear();
David Stenberg45acc962018-09-07 13:54:07 +00001203 SpillOffsets.clear();
Reid Kleckner4e040282017-09-20 18:19:08 +00001204 for (auto &Pair : NewLocations) {
David Stenberg45acc962018-09-07 13:54:07 +00001205 bool Spilled;
1206 unsigned SpillOffset;
1207 std::tie(Spilled, SpillOffset) = Pair.second;
Reid Kleckner92687d42017-09-20 17:32:54 +00001208 locations.push_back(Pair.first);
David Stenberg45acc962018-09-07 13:54:07 +00001209 if (Spilled) {
Reid Kleckner4e040282017-09-20 18:19:08 +00001210 unsigned NewLocNo = std::distance(&*NewLocations.begin(), &Pair);
David Stenberg45acc962018-09-07 13:54:07 +00001211 SpillOffsets[NewLocNo] = SpillOffset;
Reid Kleckner4e040282017-09-20 18:19:08 +00001212 }
1213 }
Reid Kleckner92687d42017-09-20 17:32:54 +00001214
1215 // Update the interval map, but only coalesce left, since intervals to the
1216 // right use the old location numbers. This should merge two contiguous
1217 // DBG_VALUE intervals with different vregs that were allocated to the same
1218 // physical register.
1219 for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) {
Reid Kleckner04e25e02017-10-03 17:59:02 +00001220 DbgValueLocation Loc = I.value();
Mikael Holmen57b33f62018-06-21 07:02:46 +00001221 // Undef values don't exist in locations (and thus not in LocNoMap either)
1222 // so skip over them. See getLocationNo().
1223 if (Loc.isUndef())
1224 continue;
Reid Kleckner04e25e02017-10-03 17:59:02 +00001225 unsigned NewLocNo = LocNoMap[Loc.locNo()];
1226 I.setValueUnchecked(Loc.changeLocNo(NewLocNo));
Reid Kleckner92687d42017-09-20 17:32:54 +00001227 I.setStart(I.start());
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001228 }
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001229}
1230
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001231/// Find an iterator for inserting a DBG_VALUE instruction.
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001232static MachineBasicBlock::iterator
Devang Patel26ffa012011-02-04 01:43:25 +00001233findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx,
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001234 LiveIntervals &LIS) {
1235 SlotIndex Start = LIS.getMBBStartIdx(MBB);
1236 Idx = Idx.getBaseIndex();
1237
1238 // Try to find an insert location by going backwards from Idx.
1239 MachineInstr *MI;
1240 while (!(MI = LIS.getInstructionFromIndex(Idx))) {
1241 // We've reached the beginning of MBB.
1242 if (Idx == Start) {
Keith Walker830a8c12016-09-16 14:07:29 +00001243 MachineBasicBlock::iterator I = MBB->SkipPHIsLabelsAndDebug(MBB->begin());
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001244 return I;
1245 }
1246 Idx = Idx.getPrevIndex();
1247 }
Devang Patel26ffa012011-02-04 01:43:25 +00001248
Jakob Stoklund Olesen088b30a2011-01-13 23:35:53 +00001249 // Don't insert anything after the first terminator, though.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001250 return MI->isTerminator() ? MBB->getFirstTerminator() :
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001251 std::next(MachineBasicBlock::iterator(MI));
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001252}
1253
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001254/// Find an iterator for inserting the next DBG_VALUE instruction
1255/// (or end if no more insert locations found).
1256static MachineBasicBlock::iterator
1257findNextInsertLocation(MachineBasicBlock *MBB,
1258 MachineBasicBlock::iterator I,
1259 SlotIndex StopIdx, MachineOperand &LocMO,
1260 LiveIntervals &LIS,
1261 const TargetRegisterInfo &TRI) {
1262 if (!LocMO.isReg())
1263 return MBB->instr_end();
Daniel Sanders0c476112019-08-15 19:22:08 +00001264 Register Reg = LocMO.getReg();
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001265
1266 // Find the next instruction in the MBB that define the register Reg.
Stefan Maksimovic991af7a2018-02-09 14:03:26 +00001267 while (I != MBB->end() && !I->isTerminator()) {
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001268 if (!LIS.isNotInMIMap(*I) &&
1269 SlotIndex::isEarlierEqualInstr(StopIdx, LIS.getInstructionIndex(*I)))
1270 break;
1271 if (I->definesRegister(Reg, &TRI))
1272 // The insert location is directly after the instruction/bundle.
1273 return std::next(I);
1274 ++I;
1275 }
1276 return MBB->end();
1277}
1278
1279void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex StartIdx,
David Stenberg45acc962018-09-07 13:54:07 +00001280 SlotIndex StopIdx, DbgValueLocation Loc,
1281 bool Spilled, unsigned SpillOffset,
1282 LiveIntervals &LIS, const TargetInstrInfo &TII,
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001283 const TargetRegisterInfo &TRI) {
1284 SlotIndex MBBEndIdx = LIS.getMBBEndIdx(&*MBB);
1285 // Only search within the current MBB.
1286 StopIdx = (MBBEndIdx < StopIdx) ? MBBEndIdx : StopIdx;
1287 MachineBasicBlock::iterator I = findInsertLocation(MBB, StartIdx, LIS);
Mikael Holmen57b33f62018-06-21 07:02:46 +00001288 // Undef values don't exist in locations so create new "noreg" register MOs
1289 // for them. See getLocationNo().
1290 MachineOperand MO = !Loc.isUndef() ?
1291 locations[Loc.locNo()] :
1292 MachineOperand::CreateReg(/* Reg */ 0, /* isDef */ false, /* isImp */ false,
1293 /* isKill */ false, /* isDead */ false,
1294 /* isUndef */ false, /* isEarlyClobber */ false,
1295 /* SubReg */ 0, /* isDebug */ true);
1296
Devang Pateleabc3cea2011-08-04 20:42:11 +00001297 ++NumInsertedDebugValues;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001298
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001299 assert(cast<DILocalVariable>(Variable)
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +00001300 ->isValidLocationForIntrinsic(getDebugLoc()) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +00001301 "Expected inlined-at fields to agree");
Reid Kleckner4e040282017-09-20 18:19:08 +00001302
1303 // If the location was spilled, the new DBG_VALUE will be indirect. If the
1304 // original DBG_VALUE was indirect, we need to add DW_OP_deref to indicate
David Stenberg45acc962018-09-07 13:54:07 +00001305 // that the original virtual register was a pointer. Also, add the stack slot
1306 // offset for the spilled register to the expression.
Reid Kleckner4e040282017-09-20 18:19:08 +00001307 const DIExpression *Expr = Expression;
Jeremy Morseed29dba2019-10-15 10:46:24 +00001308 if (Spilled)
1309 Expr = DIExpression::prepend(Expr, DIExpression::ApplyOffset, SpillOffset);
Reid Kleckner4e040282017-09-20 18:19:08 +00001310
Reid Kleckner04e25e02017-10-03 17:59:02 +00001311 assert((!Spilled || MO.isFI()) && "a spilled location must be a frame index");
Reid Kleckner4e040282017-09-20 18:19:08 +00001312
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001313 do {
Mikael Holmen42f7bc92018-06-21 10:03:34 +00001314 BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_VALUE),
Jeremy Morseed29dba2019-10-15 10:46:24 +00001315 Spilled, MO, Variable, Expr);
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001316
1317 // Continue and insert DBG_VALUES after every redefinition of register
1318 // associated with the debug value within the range
1319 I = findNextInsertLocation(MBB, I, StopIdx, MO, LIS, TRI);
1320 } while (I != MBB->end());
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001321}
1322
Hsiangkai Wang66609a82019-01-18 07:17:09 +00001323void UserLabel::insertDebugLabel(MachineBasicBlock *MBB, SlotIndex Idx,
1324 LiveIntervals &LIS,
1325 const TargetInstrInfo &TII) {
1326 MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS);
1327 ++NumInsertedDebugLabels;
1328 BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_LABEL))
1329 .addMetadata(Label);
1330}
1331
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001332void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
Reid Kleckner4e040282017-09-20 18:19:08 +00001333 const TargetInstrInfo &TII,
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001334 const TargetRegisterInfo &TRI,
David Stenberg45acc962018-09-07 13:54:07 +00001335 const SpillOffsetMap &SpillOffsets) {
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001336 MachineFunction::iterator MFEnd = VRM->getMachineFunction().end();
1337
1338 for (LocMap::const_iterator I = locInts.begin(); I.valid();) {
1339 SlotIndex Start = I.start();
1340 SlotIndex Stop = I.stop();
Reid Kleckner04e25e02017-10-03 17:59:02 +00001341 DbgValueLocation Loc = I.value();
David Stenberg45acc962018-09-07 13:54:07 +00001342 auto SpillIt =
1343 !Loc.isUndef() ? SpillOffsets.find(Loc.locNo()) : SpillOffsets.end();
1344 bool Spilled = SpillIt != SpillOffsets.end();
1345 unsigned SpillOffset = Spilled ? SpillIt->second : 0;
Robert Lougher10f740d2017-08-03 11:54:02 +00001346
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001347 LLVM_DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << Loc.locNo());
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +00001348 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
1349 SlotIndex MBBEnd = LIS.getMBBEndIdx(&*MBB);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001350
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001351 LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd);
David Stenberg45acc962018-09-07 13:54:07 +00001352 insertDebugValue(&*MBB, Start, Stop, Loc, Spilled, SpillOffset, LIS, TII,
1353 TRI);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001354 // This interval may span multiple basic blocks.
1355 // Insert a DBG_VALUE into each one.
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001356 while (Stop > MBBEnd) {
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001357 // Move to the next block.
1358 Start = MBBEnd;
1359 if (++MBB == MFEnd)
1360 break;
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +00001361 MBBEnd = LIS.getMBBEndIdx(&*MBB);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001362 LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd);
David Stenberg45acc962018-09-07 13:54:07 +00001363 insertDebugValue(&*MBB, Start, Stop, Loc, Spilled, SpillOffset, LIS, TII,
1364 TRI);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001365 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001366 LLVM_DEBUG(dbgs() << '\n');
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001367 if (MBB == MFEnd)
1368 break;
1369
1370 ++I;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001371 }
1372}
1373
Hsiangkai Wang66609a82019-01-18 07:17:09 +00001374void UserLabel::emitDebugLabel(LiveIntervals &LIS, const TargetInstrInfo &TII) {
1375 LLVM_DEBUG(dbgs() << "\t" << loc);
1376 MachineFunction::iterator MBB = LIS.getMBBFromIndex(loc)->getIterator();
1377
1378 LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB));
1379 insertDebugLabel(&*MBB, loc, LIS, TII);
1380
1381 LLVM_DEBUG(dbgs() << '\n');
1382}
1383
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001384void LDVImpl::emitDebugValues(VirtRegMap *VRM) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001385 LLVM_DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n");
David Blaikie2f040112014-07-25 16:10:16 +00001386 if (!MF)
1387 return;
Eric Christopherfc6de422014-08-05 02:39:49 +00001388 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
David Stenberg45acc962018-09-07 13:54:07 +00001389 SpillOffsetMap SpillOffsets;
Hsiangkai Wang66609a82019-01-18 07:17:09 +00001390 for (auto &userValue : userValues) {
1391 LLVM_DEBUG(userValue->print(dbgs(), TRI));
1392 userValue->rewriteLocations(*VRM, *MF, *TII, *TRI, SpillOffsets);
1393 userValue->emitDebugValues(VRM, *LIS, *TII, *TRI, SpillOffsets);
1394 }
1395 LLVM_DEBUG(dbgs() << "********** EMITTING LIVE DEBUG LABELS **********\n");
1396 for (auto &userLabel : userLabels) {
1397 LLVM_DEBUG(userLabel->print(dbgs(), TRI));
1398 userLabel->emitDebugLabel(*LIS, *TII);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001399 }
Manman Ren7a4c8a72013-02-13 20:23:48 +00001400 EmitDone = true;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001401}
1402
1403void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) {
Manman Ren7a4c8a72013-02-13 20:23:48 +00001404 if (pImpl)
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001405 static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM);
1406}
1407
David Blaikie2f040112014-07-25 16:10:16 +00001408bool LiveDebugVariables::doInitialization(Module &M) {
David Blaikie2f040112014-07-25 16:10:16 +00001409 return Pass::doInitialization(M);
1410}
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001411
Aaron Ballman615eb472017-10-15 14:32:27 +00001412#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Sam Clegg705f7982017-06-21 22:19:17 +00001413LLVM_DUMP_METHOD void LiveDebugVariables::dump() const {
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +00001414 if (pImpl)
1415 static_cast<LDVImpl*>(pImpl)->print(dbgs());
1416}
1417#endif