blob: f3fcd004defdbffd081d482c6c2566499375bbce [file] [log] [blame]
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +00001//===- LiveDebugVariables.cpp - Tracking debug info variables -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LiveDebugVariables analysis.
11//
12// Remove all DBG_VALUE instructions referencing virtual registers and replace
13// them with a data structure tracking where live user variables are kept - in a
14// virtual register or in a stack slot.
15//
16// Allow the data structure to be updated during register allocation when values
17// are moved between registers and stack slots. Finally emit new DBG_VALUE
18// instructions after register allocation is complete.
19//
20//===----------------------------------------------------------------------===//
21
22#include "LiveDebugVariables.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000023#include "llvm/ADT/ArrayRef.h"
24#include "llvm/ADT/DenseMap.h"
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000025#include "llvm/ADT/IntervalMap.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000026#include "llvm/ADT/STLExtras.h"
Robert Lougher10f740d2017-08-03 11:54:02 +000027#include "llvm/ADT/SmallSet.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000028#include "llvm/ADT/SmallVector.h"
Devang Patelb4568662011-08-04 18:45:38 +000029#include "llvm/ADT/Statistic.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000030#include "llvm/ADT/StringRef.h"
Robert Lougher10f740d2017-08-03 11:54:02 +000031#include "llvm/CodeGen/LexicalScopes.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000032#include "llvm/CodeGen/LiveInterval.h"
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"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000047#include "llvm/IR/DebugInfoMetadata.h"
48#include "llvm/IR/DebugLoc.h"
49#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000050#include "llvm/IR/Metadata.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000051#include "llvm/MC/MCRegisterInfo.h"
52#include "llvm/Pass.h"
53#include "llvm/Support/Casting.h"
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000054#include "llvm/Support/CommandLine.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000055#include "llvm/Support/Compiler.h"
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000056#include "llvm/Support/Debug.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000057#include "llvm/Support/raw_ostream.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000058#include <algorithm>
59#include <cassert>
60#include <iterator>
David Blaikie2b1dfa72014-04-21 20:37:07 +000061#include <memory>
Benjamin Kramer82de7d32016-05-27 14:27:24 +000062#include <utility>
David Blaikie2b1dfa72014-04-21 20:37:07 +000063
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000064using namespace llvm;
65
Matthias Braun1527baa2017-05-25 21:26:32 +000066#define DEBUG_TYPE "livedebugvars"
Chandler Carruth1b9dde02014-04-22 02:02:50 +000067
Devang Patelacbee0b2011-01-07 22:33:41 +000068static cl::opt<bool>
Jakob Stoklund Olesen74ded572011-01-12 23:36:21 +000069EnableLDV("live-debug-variables", cl::init(true),
Devang Patelacbee0b2011-01-07 22:33:41 +000070 cl::desc("Enable the live debug variables pass"), cl::Hidden);
71
Devang Patelb4568662011-08-04 18:45:38 +000072STATISTIC(NumInsertedDebugValues, "Number of DBG_VALUEs inserted");
Eugene Zelenko5df3d892017-08-24 21:21:39 +000073
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000074char LiveDebugVariables::ID = 0;
75
Matthias Braun1527baa2017-05-25 21:26:32 +000076INITIALIZE_PASS_BEGIN(LiveDebugVariables, DEBUG_TYPE,
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000077 "Debug Variable Analysis", false, false)
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000078INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000079INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
Matthias Braun1527baa2017-05-25 21:26:32 +000080INITIALIZE_PASS_END(LiveDebugVariables, DEBUG_TYPE,
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000081 "Debug Variable Analysis", false, false)
82
83void LiveDebugVariables::getAnalysisUsage(AnalysisUsage &AU) const {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +000084 AU.addRequired<MachineDominatorTree>();
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000085 AU.addRequiredTransitive<LiveIntervals>();
86 AU.setPreservesAll();
87 MachineFunctionPass::getAnalysisUsage(AU);
88}
89
Eugene Zelenko5df3d892017-08-24 21:21:39 +000090LiveDebugVariables::LiveDebugVariables() : MachineFunctionPass(ID) {
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +000091 initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
92}
93
Reid Kleckner04e25e02017-10-03 17:59:02 +000094enum : unsigned { UndefLocNo = ~0U };
95
96/// Describes a location by number along with some flags about the original
97/// usage of the location.
98class DbgValueLocation {
99public:
100 DbgValueLocation(unsigned LocNo, bool WasIndirect)
101 : LocNo(LocNo), WasIndirect(WasIndirect) {
102 static_assert(sizeof(*this) == sizeof(unsigned), "bad bitfield packing");
103 assert(locNo() == LocNo && "location truncation");
104 }
105
106 DbgValueLocation() : LocNo(0), WasIndirect(0) {}
107
108 unsigned locNo() const {
109 // Fix up the undef location number, which gets truncated.
110 return LocNo == INT_MAX ? UndefLocNo : LocNo;
111 }
112 bool wasIndirect() const { return WasIndirect; }
113 bool isUndef() const { return locNo() == UndefLocNo; }
114
115 DbgValueLocation changeLocNo(unsigned NewLocNo) const {
116 return DbgValueLocation(NewLocNo, WasIndirect);
117 }
118
Reid Klecknerb4569de72017-10-03 18:30:11 +0000119 friend inline bool operator==(const DbgValueLocation &LHS,
120 const DbgValueLocation &RHS) {
121 return LHS.LocNo == RHS.LocNo && LHS.WasIndirect == RHS.WasIndirect;
Reid Kleckner04e25e02017-10-03 17:59:02 +0000122 }
Reid Klecknerb4569de72017-10-03 18:30:11 +0000123
124 friend inline bool operator!=(const DbgValueLocation &LHS,
125 const DbgValueLocation &RHS) {
126 return !(LHS == RHS);
127 }
Reid Kleckner04e25e02017-10-03 17:59:02 +0000128
129private:
130 unsigned LocNo : 31;
131 unsigned WasIndirect : 1;
132};
133
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000134/// LocMap - 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
137namespace {
138
139class LDVImpl;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000140
141/// UserValue - A user value is a part of a debug info user variable.
142///
143/// A DBG_VALUE instruction notes that (a sub-register of) a virtual register
144/// holds part of a user variable. The part is identified by a byte offset.
145///
146/// UserValues are grouped into equivalence classes for easier searching. Two
147/// user values are related if they refer to the same variable, or if they are
148/// held by the same virtual register. The equivalence class is the transitive
149/// closure of that relation.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000150class UserValue {
Reid Kleckner4e040282017-09-20 18:19:08 +0000151 const DILocalVariable *Variable; ///< The debug info variable we are part of.
152 const DIExpression *Expression; ///< Any complex address expression.
Devang Patel26ffa012011-02-04 01:43:25 +0000153 DebugLoc dl; ///< The debug location for the variable. This is
154 ///< used by dwarf writer to find lexical scope.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000155 UserValue *leader; ///< Equivalence class leader.
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000156 UserValue *next = nullptr; ///< Next value in equivalence class, or null.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000157
158 /// Numbered locations referenced by locmap.
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000159 SmallVector<MachineOperand, 4> locations;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000160
161 /// Map of slot indices where this value is live.
162 LocMap locInts;
163
Robert Lougher10f740d2017-08-03 11:54:02 +0000164 /// Set of interval start indexes that have been trimmed to the
165 /// lexical scope.
166 SmallSet<SlotIndex, 2> trimmedDefs;
167
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000168 /// insertDebugValue - 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,
170 SlotIndex StopIdx,
Reid Kleckner04e25e02017-10-03 17:59:02 +0000171 DbgValueLocation Loc, bool Spilled, 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
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000175 /// splitLocation - Replace OldLocNo ranges with NewRegs ranges where NewRegs
176 /// 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:
181 /// UserValue - 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)
184 : Variable(var), Expression(expr), dl(std::move(L)), leader(this),
185 locInts(alloc) {}
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000186
187 /// getLeader - 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 /// getNext - Return the next UserValue in the equivalence class.
196 UserValue *getNext() const { return next; }
197
Devang Patel338e4322011-07-06 23:09:51 +0000198 /// match - Does this UserValue match the parameters?
Reid Kleckner4e040282017-09-20 18:19:08 +0000199 bool match(const DILocalVariable *Var, const DIExpression *Expr,
Reid Kleckner04e25e02017-10-03 17:59:02 +0000200 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;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000204 }
205
206 /// merge - 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;
Richard Trieu7a083812016-02-18 22:09:30 +0000216 while (End->next) {
217 End->leader = L1;
218 End = End->next;
219 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000220 End->leader = L1;
221 End->next = L1->next;
222 L1->next = L2;
223 return L1;
224 }
225
226 /// getLocationNo - Return the location number that matches Loc.
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000227 unsigned getLocationNo(const MachineOperand &LocMO) {
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000228 if (LocMO.isReg()) {
229 if (LocMO.getReg() == 0)
Reid Klecknereed09732017-09-15 22:08:50 +0000230 return UndefLocNo;
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000231 // For register locations we dont care about use/def and other flags.
232 for (unsigned i = 0, e = locations.size(); i != e; ++i)
233 if (locations[i].isReg() &&
234 locations[i].getReg() == LocMO.getReg() &&
235 locations[i].getSubReg() == LocMO.getSubReg())
236 return i;
237 } else
238 for (unsigned i = 0, e = locations.size(); i != e; ++i)
239 if (LocMO.isIdenticalTo(locations[i]))
240 return i;
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000241 locations.push_back(LocMO);
242 // We are storing a MachineOperand outside a MachineInstr.
243 locations.back().clearParent();
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000244 // Don't store def operands.
Geoff Berryb3d126d2017-12-29 21:01:09 +0000245 if (locations.back().isReg()) {
246 if (locations.back().isDef())
247 locations.back().setIsDead(false);
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000248 locations.back().setIsUse();
Geoff Berryb3d126d2017-12-29 21:01:09 +0000249 }
Jakob Stoklund Olesen9adf5e02011-01-09 05:33:21 +0000250 return locations.size() - 1;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000251 }
252
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000253 /// mapVirtRegs - Ensure that all virtual register locations are mapped.
254 void mapVirtRegs(LDVImpl *LDV);
255
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000256 /// addDef - Add a definition point to this value.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000257 void addDef(SlotIndex Idx, const MachineOperand &LocMO, bool IsIndirect) {
258 DbgValueLocation Loc(getLocationNo(LocMO), IsIndirect);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000259 // Add a singular (Idx,Idx) -> Loc mapping.
260 LocMap::iterator I = locInts.find(Idx);
261 if (!I.valid() || I.start() != Idx)
Reid Kleckner04e25e02017-10-03 17:59:02 +0000262 I.insert(Idx, Idx.getNextSlot(), Loc);
Jakob Stoklund Olesen2539af62011-08-03 23:44:31 +0000263 else
264 // A later DBG_VALUE at the same SlotIndex overrides the old location.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000265 I.setValue(Loc);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000266 }
267
Adrian Prantlf8d10ce2016-09-28 21:34:23 +0000268 /// extendDef - Extend the current definition as far as possible down.
269 /// Stop when meeting an existing def or when leaving the live
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000270 /// range of VNI.
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000271 /// End points where VNI is no longer live are added to Kills.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000272 /// @param Idx Starting point for the definition.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000273 /// @param Loc Location number to propagate.
Matthias Braun34e1be92013-10-10 21:29:02 +0000274 /// @param LR Restrict liveness to where LR has the value VNI. May be null.
275 /// @param VNI When LR is not null, this is the value to restrict to.
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000276 /// @param Kills Append end points of VNI's live range to Kills.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000277 /// @param LIS Live intervals analysis.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000278 void extendDef(SlotIndex Idx, DbgValueLocation Loc,
Matthias Braun34e1be92013-10-10 21:29:02 +0000279 LiveRange *LR, const VNInfo *VNI,
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000280 SmallVectorImpl<SlotIndex> *Kills,
Adrian Prantlf8d10ce2016-09-28 21:34:23 +0000281 LiveIntervals &LIS);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000282
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000283 /// addDefsFromCopies - The value in LI/LocNo may be copies to other
284 /// registers. Determine if any of the copies are available at the kill
285 /// points, and add defs if possible.
286 /// @param LI Scan for copies of the value in LI->reg.
287 /// @param LocNo Location number of LI->reg.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000288 /// @param WasIndirect Indicates if the original use of LI->reg was indirect
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000289 /// @param Kills Points where the range of LocNo could be extended.
290 /// @param NewDefs Append (Idx, LocNo) of inserted defs here.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000291 void addDefsFromCopies(
292 LiveInterval *LI, unsigned LocNo, bool WasIndirect,
293 const SmallVectorImpl<SlotIndex> &Kills,
294 SmallVectorImpl<std::pair<SlotIndex, DbgValueLocation>> &NewDefs,
295 MachineRegisterInfo &MRI, LiveIntervals &LIS);
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000296
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000297 /// computeIntervals - Compute the live intervals of all locations after
298 /// collecting all their def points.
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000299 void computeIntervals(MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
Robert Lougher10f740d2017-08-03 11:54:02 +0000300 LiveIntervals &LIS, LexicalScopes &LS);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000301
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000302 /// splitRegister - Replace OldReg ranges with NewRegs ranges where NewRegs is
303 /// live. Returns true if any changes were made.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000304 bool splitRegister(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
305 LiveIntervals &LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000306
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000307 /// rewriteLocations - Rewrite virtual register locations according to the
Reid Kleckner4e040282017-09-20 18:19:08 +0000308 /// provided virtual register map. Record which locations were spilled.
309 void rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI,
310 BitVector &SpilledLocations);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000311
Eric Christopherbc671702013-02-13 02:29:18 +0000312 /// emitDebugValues - Recreate DBG_VALUE instruction from data structures.
Reid Kleckner4e040282017-09-20 18:19:08 +0000313 void emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +0000314 const TargetInstrInfo &TII,
315 const TargetRegisterInfo &TRI,
Reid Kleckner4e040282017-09-20 18:19:08 +0000316 const BitVector &SpilledLocations);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000317
Devang Patelf9e2ae92011-09-13 18:40:53 +0000318 /// getDebugLoc - Return DebugLoc of this UserValue.
319 DebugLoc getDebugLoc() { return dl;}
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000320
Eric Christopher1cdefae2015-02-27 00:11:34 +0000321 void print(raw_ostream &, const TargetRegisterInfo *);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000322};
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000323
324/// LDVImpl - Implementation of the LiveDebugVariables pass.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000325class LDVImpl {
326 LiveDebugVariables &pass;
327 LocMap::Allocator allocator;
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000328 MachineFunction *MF = nullptr;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000329 LiveIntervals *LIS;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000330 const TargetRegisterInfo *TRI;
331
Manman Ren7a4c8a72013-02-13 20:23:48 +0000332 /// Whether emitDebugValues is called.
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000333 bool EmitDone = false;
334
Manman Ren7a4c8a72013-02-13 20:23:48 +0000335 /// Whether the machine function is modified during the pass.
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000336 bool ModifiedMF = false;
Manman Ren7a4c8a72013-02-13 20:23:48 +0000337
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000338 /// userValues - All allocated UserValue instances.
David Blaikie2b1dfa72014-04-21 20:37:07 +0000339 SmallVector<std::unique_ptr<UserValue>, 8> userValues;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000340
341 /// Map virtual register to eq class leader.
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000342 using VRMap = DenseMap<unsigned, UserValue *>;
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000343 VRMap virtRegToEqClass;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000344
345 /// Map user variable to eq class leader.
Reid Kleckner4e040282017-09-20 18:19:08 +0000346 using UVMap = DenseMap<const DILocalVariable *, UserValue *>;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000347 UVMap userVarMap;
348
349 /// getUserValue - Find or create a UserValue.
Reid Kleckner4e040282017-09-20 18:19:08 +0000350 UserValue *getUserValue(const DILocalVariable *Var, const DIExpression *Expr,
Reid Kleckner04e25e02017-10-03 17:59:02 +0000351 const DebugLoc &DL);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000352
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000353 /// lookupVirtReg - Find the EC leader for VirtReg or null.
354 UserValue *lookupVirtReg(unsigned VirtReg);
355
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000356 /// handleDebugValue - Add DBG_VALUE instruction to our maps.
357 /// @param MI DBG_VALUE instruction
358 /// @param Idx Last valid SLotIndex before instruction.
359 /// @return True if the DBG_VALUE instruction should be deleted.
Duncan P. N. Exon Smithfb612ac2016-06-30 23:13:38 +0000360 bool handleDebugValue(MachineInstr &MI, SlotIndex Idx);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000361
362 /// collectDebugValues - Collect and erase all DBG_VALUE instructions, adding
363 /// a UserValue def for each instruction.
364 /// @param mf MachineFunction to be scanned.
365 /// @return True if any debug values were found.
366 bool collectDebugValues(MachineFunction &mf);
367
368 /// computeIntervals - Compute the live intervals of all user values after
369 /// collecting all their def points.
370 void computeIntervals();
371
372public:
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000373 LDVImpl(LiveDebugVariables *ps) : pass(*ps) {}
374
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000375 bool runOnMachineFunction(MachineFunction &mf);
376
Manman Ren7a4c8a72013-02-13 20:23:48 +0000377 /// clear - Release all memory.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000378 void clear() {
David Blaikie2f040112014-07-25 16:10:16 +0000379 MF = nullptr;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000380 userValues.clear();
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000381 virtRegToEqClass.clear();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000382 userVarMap.clear();
Manman Ren7a4c8a72013-02-13 20:23:48 +0000383 // Make sure we call emitDebugValues if the machine function was modified.
384 assert((!ModifiedMF || EmitDone) &&
385 "Dbg values are not emitted in LDV");
386 EmitDone = false;
387 ModifiedMF = false;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000388 }
389
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000390 /// mapVirtReg - Map virtual register to an equivalence class.
391 void mapVirtReg(unsigned VirtReg, UserValue *EC);
392
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000393 /// splitRegister - Replace all references to OldReg with NewRegs.
Mark Laceyf9ea8852013-08-14 23:50:04 +0000394 void splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000395
Eric Christopherbc671702013-02-13 02:29:18 +0000396 /// emitDebugValues - Recreate DBG_VALUE instruction from data structures.
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +0000397 void emitDebugValues(VirtRegMap *VRM);
398
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000399 void print(raw_ostream&);
400};
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000401
402} // end anonymous namespace
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000403
Aaron Ballman615eb472017-10-15 14:32:27 +0000404#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000405static void printDebugLoc(const DebugLoc &DL, raw_ostream &CommentOS,
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000406 const LLVMContext &Ctx) {
407 if (!DL)
408 return;
409
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000410 auto *Scope = cast<DIScope>(DL.getScope());
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000411 // Omit the directory, because it's likely to be long and uninteresting.
Duncan P. N. Exon Smithb273d062015-04-16 01:37:00 +0000412 CommentOS << Scope->getFilename();
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000413 CommentOS << ':' << DL.getLine();
414 if (DL.getCol() != 0)
415 CommentOS << ':' << DL.getCol();
416
417 DebugLoc InlinedAtDL = DL.getInlinedAt();
418 if (!InlinedAtDL)
419 return;
420
421 CommentOS << " @[ ";
422 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
423 CommentOS << " ]";
424}
425
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000426static void printExtendedName(raw_ostream &OS, const DILocalVariable *V,
427 const DILocation *DL) {
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000428 const LLVMContext &Ctx = V->getContext();
429 StringRef Res = V->getName();
430 if (!Res.empty())
431 OS << Res << "," << V->getLine();
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000432 if (auto *InlinedAt = DL->getInlinedAt()) {
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000433 if (DebugLoc InlinedAtDL = InlinedAt) {
434 OS << " @[";
435 printDebugLoc(InlinedAtDL, OS, Ctx);
436 OS << "]";
437 }
438 }
439}
440
Eric Christopher1cdefae2015-02-27 00:11:34 +0000441void UserValue::print(raw_ostream &OS, const TargetRegisterInfo *TRI) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000442 auto *DV = cast<DILocalVariable>(Variable);
Frederic Risse6bb1872014-08-07 20:04:00 +0000443 OS << "!\"";
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000444 printExtendedName(OS, DV, dl);
Duncan P. N. Exon Smith32e7f282015-04-14 02:09:32 +0000445
Devang Patel6c1ed312011-08-09 01:03:35 +0000446 OS << "\"\t";
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000447 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
448 OS << " [" << I.start() << ';' << I.stop() << "):";
Reid Kleckner04e25e02017-10-03 17:59:02 +0000449 if (I.value().isUndef())
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000450 OS << "undef";
Reid Kleckner04e25e02017-10-03 17:59:02 +0000451 else {
452 OS << I.value().locNo();
453 if (I.value().wasIndirect())
454 OS << " ind";
455 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000456 }
Jakob Stoklund Olesenc86fe052011-05-06 17:59:59 +0000457 for (unsigned i = 0, e = locations.size(); i != e; ++i) {
458 OS << " Loc" << i << '=';
Eric Christopher1cdefae2015-02-27 00:11:34 +0000459 locations[i].print(OS, TRI);
Jakob Stoklund Olesenc86fe052011-05-06 17:59:59 +0000460 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000461 OS << '\n';
462}
463
464void LDVImpl::print(raw_ostream &OS) {
465 OS << "********** DEBUG VARIABLES **********\n";
466 for (unsigned i = 0, e = userValues.size(); i != e; ++i)
Eric Christopher1cdefae2015-02-27 00:11:34 +0000467 userValues[i]->print(OS, TRI);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000468}
Florian Hahn6b3216a2017-07-31 10:07:49 +0000469#endif
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000470
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000471void UserValue::mapVirtRegs(LDVImpl *LDV) {
472 for (unsigned i = 0, e = locations.size(); i != e; ++i)
473 if (locations[i].isReg() &&
474 TargetRegisterInfo::isVirtualRegister(locations[i].getReg()))
475 LDV->mapVirtReg(locations[i].getReg(), this);
476}
477
Reid Kleckner4e040282017-09-20 18:19:08 +0000478UserValue *LDVImpl::getUserValue(const DILocalVariable *Var,
Reid Kleckner04e25e02017-10-03 17:59:02 +0000479 const DIExpression *Expr, const DebugLoc &DL) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000480 UserValue *&Leader = userVarMap[Var];
481 if (Leader) {
482 UserValue *UV = Leader->getLeader();
483 Leader = UV;
484 for (; UV; UV = UV->getNext())
Reid Kleckner04e25e02017-10-03 17:59:02 +0000485 if (UV->match(Var, Expr, DL->getInlinedAt()))
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000486 return UV;
487 }
488
David Blaikie2b1dfa72014-04-21 20:37:07 +0000489 userValues.push_back(
Reid Kleckner04e25e02017-10-03 17:59:02 +0000490 llvm::make_unique<UserValue>(Var, Expr, DL, allocator));
David Blaikie2b1dfa72014-04-21 20:37:07 +0000491 UserValue *UV = userValues.back().get();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000492 Leader = UserValue::merge(Leader, UV);
493 return UV;
494}
495
496void LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *EC) {
497 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && "Only map VirtRegs");
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000498 UserValue *&Leader = virtRegToEqClass[VirtReg];
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000499 Leader = UserValue::merge(Leader, EC);
500}
501
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000502UserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) {
Jakob Stoklund Olesen922e1fa2010-12-03 22:25:09 +0000503 if (UserValue *UV = virtRegToEqClass.lookup(VirtReg))
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000504 return UV->getLeader();
Craig Topperc0196b12014-04-14 00:51:57 +0000505 return nullptr;
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000506}
507
Duncan P. N. Exon Smithfb612ac2016-06-30 23:13:38 +0000508bool LDVImpl::handleDebugValue(MachineInstr &MI, SlotIndex Idx) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000509 // DBG_VALUE loc, offset, variable
Duncan P. N. Exon Smithfb612ac2016-06-30 23:13:38 +0000510 if (MI.getNumOperands() != 4 ||
511 !(MI.getOperand(1).isReg() || MI.getOperand(1).isImm()) ||
512 !MI.getOperand(2).isMetadata()) {
513 DEBUG(dbgs() << "Can't handle " << MI);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000514 return false;
515 }
516
Bjorn Petterssonbdf0c002018-03-06 08:47:07 +0000517 // Detect invalid DBG_VALUE instructions, with a debug-use of a virtual
518 // register that hasn't been defined yet. If we do not remove those here, then
519 // the re-insertion of the DBG_VALUE instruction after register allocation
520 // will be incorrect.
521 // TODO: If earlier passes are corrected to generate sane debug information
522 // (and if the machine verifier is improved to catch this), then these checks
523 // could be removed or replaced by asserts.
524 bool Discard = false;
525 if (MI.getOperand(0).isReg() &&
526 TargetRegisterInfo::isVirtualRegister(MI.getOperand(0).getReg())) {
527 const unsigned Reg = MI.getOperand(0).getReg();
528 if (!LIS->hasInterval(Reg)) {
529 // The DBG_VALUE is described by a virtual register that does not have a
530 // live interval. Discard the DBG_VALUE.
531 Discard = true;
532 DEBUG(dbgs() << "Discarding debug info (no LIS interval): "
533 << Idx << " " << MI);
534 } else {
535 // The DBG_VALUE is only valid if either Reg is live out from Idx, or Reg
536 // is defined dead at Idx (where Idx is the slot index for the instruction
537 // preceeding the DBG_VALUE).
538 const LiveInterval &LI = LIS->getInterval(Reg);
539 LiveQueryResult LRQ = LI.Query(Idx);
540 if (!LRQ.valueOutOrDead()) {
541 // We have found a DBG_VALUE with the value in a virtual register that
542 // is not live. Discard the DBG_VALUE.
543 Discard = true;
544 DEBUG(dbgs() << "Discarding debug info (reg not live): "
545 << Idx << " " << MI);
546 }
547 }
548 }
549
Reid Kleckner04e25e02017-10-03 17:59:02 +0000550 // Get or create the UserValue for (variable,offset) here.
Reid Kleckner4e040282017-09-20 18:19:08 +0000551 bool IsIndirect = MI.getOperand(1).isImm();
Adrian Prantlb2811e52017-07-28 23:06:50 +0000552 if (IsIndirect)
553 assert(MI.getOperand(1).getImm() == 0 && "DBG_VALUE with nonzero offset");
Reid Kleckner4e040282017-09-20 18:19:08 +0000554 const DILocalVariable *Var = MI.getDebugVariable();
555 const DIExpression *Expr = MI.getDebugExpression();
Reid Kleckner04e25e02017-10-03 17:59:02 +0000556 UserValue *UV =
557 getUserValue(Var, Expr, MI.getDebugLoc());
Bjorn Petterssonbdf0c002018-03-06 08:47:07 +0000558 if (!Discard)
559 UV->addDef(Idx, MI.getOperand(0), IsIndirect);
560 else
561 UV->addDef(Idx, MachineOperand::CreateReg(0U, RegState::Debug), false);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000562 return true;
563}
564
565bool LDVImpl::collectDebugValues(MachineFunction &mf) {
566 bool Changed = false;
567 for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE;
568 ++MFI) {
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +0000569 MachineBasicBlock *MBB = &*MFI;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000570 for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
571 MBBI != MBBE;) {
572 if (!MBBI->isDebugValue()) {
573 ++MBBI;
574 continue;
575 }
576 // DBG_VALUE has no slot index, use the previous instruction instead.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000577 SlotIndex Idx =
578 MBBI == MBB->begin()
579 ? LIS->getMBBStartIdx(MBB)
580 : LIS->getInstructionIndex(*std::prev(MBBI)).getRegSlot();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000581 // Handle consecutive DBG_VALUE instructions with the same slot index.
582 do {
Duncan P. N. Exon Smithfb612ac2016-06-30 23:13:38 +0000583 if (handleDebugValue(*MBBI, Idx)) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000584 MBBI = MBB->erase(MBBI);
585 Changed = true;
586 } else
587 ++MBBI;
588 } while (MBBI != MBBE && MBBI->isDebugValue());
589 }
590 }
591 return Changed;
592}
593
Adrian Prantlce858132015-12-21 20:03:00 +0000594/// We only propagate DBG_VALUES locally here. LiveDebugValues performs a
595/// data-flow analysis to propagate them beyond basic block boundaries.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000596void UserValue::extendDef(SlotIndex Idx, DbgValueLocation Loc, LiveRange *LR,
Adrian Prantlce858132015-12-21 20:03:00 +0000597 const VNInfo *VNI, SmallVectorImpl<SlotIndex> *Kills,
Adrian Prantlf8d10ce2016-09-28 21:34:23 +0000598 LiveIntervals &LIS) {
Adrian Prantlce858132015-12-21 20:03:00 +0000599 SlotIndex Start = Idx;
600 MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start);
601 SlotIndex Stop = LIS.getMBBEndIdx(MBB);
602 LocMap::iterator I = locInts.find(Start);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000603
Adrian Prantlce858132015-12-21 20:03:00 +0000604 // Limit to VNI's live range.
605 bool ToEnd = true;
606 if (LR && VNI) {
607 LiveInterval::Segment *Segment = LR->getSegmentContaining(Start);
608 if (!Segment || Segment->valno != VNI) {
609 if (Kills)
610 Kills->push_back(Start);
611 return;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000612 }
Richard Trieu7a083812016-02-18 22:09:30 +0000613 if (Segment->end < Stop) {
614 Stop = Segment->end;
615 ToEnd = false;
616 }
Adrian Prantlce858132015-12-21 20:03:00 +0000617 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000618
Adrian Prantlce858132015-12-21 20:03:00 +0000619 // There could already be a short def at Start.
620 if (I.valid() && I.start() <= Start) {
621 // Stop when meeting a different location or an already extended interval.
622 Start = Start.getNextSlot();
Reid Kleckner04e25e02017-10-03 17:59:02 +0000623 if (I.value() != Loc || I.stop() != Start)
Adrian Prantlce858132015-12-21 20:03:00 +0000624 return;
625 // This is a one-slot placeholder. Just skip it.
626 ++I;
627 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000628
Adrian Prantlce858132015-12-21 20:03:00 +0000629 // Limited by the next def.
Richard Trieu7a083812016-02-18 22:09:30 +0000630 if (I.valid() && I.start() < Stop) {
631 Stop = I.start();
632 ToEnd = false;
633 }
Adrian Prantlce858132015-12-21 20:03:00 +0000634 // Limited by VNI's live range.
635 else if (!ToEnd && Kills)
636 Kills->push_back(Stop);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000637
Adrian Prantlce858132015-12-21 20:03:00 +0000638 if (Start < Stop)
Reid Kleckner04e25e02017-10-03 17:59:02 +0000639 I.insert(Start, Stop, Loc);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000640}
641
Reid Kleckner04e25e02017-10-03 17:59:02 +0000642void UserValue::addDefsFromCopies(
643 LiveInterval *LI, unsigned LocNo, bool WasIndirect,
644 const SmallVectorImpl<SlotIndex> &Kills,
645 SmallVectorImpl<std::pair<SlotIndex, DbgValueLocation>> &NewDefs,
646 MachineRegisterInfo &MRI, LiveIntervals &LIS) {
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000647 if (Kills.empty())
648 return;
649 // Don't track copies from physregs, there are too many uses.
650 if (!TargetRegisterInfo::isVirtualRegister(LI->reg))
651 return;
652
653 // Collect all the (vreg, valno) pairs that are copies of LI.
654 SmallVector<std::pair<LiveInterval*, const VNInfo*>, 8> CopyValues;
Owen Andersonb36376e2014-03-17 19:36:09 +0000655 for (MachineOperand &MO : MRI.use_nodbg_operands(LI->reg)) {
656 MachineInstr *MI = MO.getParent();
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000657 // Copies of the full value.
Owen Andersonb36376e2014-03-17 19:36:09 +0000658 if (MO.getSubReg() || !MI->isCopy())
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000659 continue;
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000660 unsigned DstReg = MI->getOperand(0).getReg();
661
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +0000662 // Don't follow copies to physregs. These are usually setting up call
663 // arguments, and the argument registers are always call clobbered. We are
664 // better off in the source register which could be a callee-saved register,
665 // or it could be spilled.
666 if (!TargetRegisterInfo::isVirtualRegister(DstReg))
667 continue;
668
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000669 // Is LocNo extended to reach this copy? If not, another def may be blocking
670 // it, or we are looking at a wrong value of LI.
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000671 SlotIndex Idx = LIS.getInstructionIndex(*MI);
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000672 LocMap::iterator I = locInts.find(Idx.getRegSlot(true));
Reid Kleckner04e25e02017-10-03 17:59:02 +0000673 if (!I.valid() || I.value().locNo() != LocNo)
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000674 continue;
675
676 if (!LIS.hasInterval(DstReg))
677 continue;
678 LiveInterval *DstLI = &LIS.getInterval(DstReg);
Jakob Stoklund Olesen90b5e562011-11-13 20:45:27 +0000679 const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx.getRegSlot());
680 assert(DstVNI && DstVNI->def == Idx.getRegSlot() && "Bad copy value");
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000681 CopyValues.push_back(std::make_pair(DstLI, DstVNI));
682 }
683
684 if (CopyValues.empty())
685 return;
686
687 DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI << '\n');
688
689 // Try to add defs of the copied values for each kill point.
690 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
691 SlotIndex Idx = Kills[i];
692 for (unsigned j = 0, e = CopyValues.size(); j != e; ++j) {
693 LiveInterval *DstLI = CopyValues[j].first;
694 const VNInfo *DstVNI = CopyValues[j].second;
695 if (DstLI->getVNInfoAt(Idx) != DstVNI)
696 continue;
697 // Check that there isn't already a def at Idx
698 LocMap::iterator I = locInts.find(Idx);
699 if (I.valid() && I.start() <= Idx)
700 continue;
701 DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #"
702 << DstVNI->id << " in " << *DstLI << '\n');
703 MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def);
704 assert(CopyMI && CopyMI->isCopy() && "Bad copy value");
705 unsigned LocNo = getLocationNo(CopyMI->getOperand(0));
Reid Kleckner04e25e02017-10-03 17:59:02 +0000706 DbgValueLocation NewLoc(LocNo, WasIndirect);
707 I.insert(Idx, Idx.getNextSlot(), NewLoc);
708 NewDefs.push_back(std::make_pair(Idx, NewLoc));
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000709 break;
710 }
711 }
712}
713
Robert Lougher10f740d2017-08-03 11:54:02 +0000714void UserValue::computeIntervals(MachineRegisterInfo &MRI,
715 const TargetRegisterInfo &TRI,
716 LiveIntervals &LIS, LexicalScopes &LS) {
Reid Kleckner04e25e02017-10-03 17:59:02 +0000717 SmallVector<std::pair<SlotIndex, DbgValueLocation>, 16> Defs;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000718
719 // Collect all defs to be extended (Skipping undefs).
720 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I)
Reid Kleckner04e25e02017-10-03 17:59:02 +0000721 if (!I.value().isUndef())
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000722 Defs.push_back(std::make_pair(I.start(), I.value()));
723
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000724 // Extend all defs, and possibly add new ones along the way.
725 for (unsigned i = 0; i != Defs.size(); ++i) {
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000726 SlotIndex Idx = Defs[i].first;
Reid Kleckner04e25e02017-10-03 17:59:02 +0000727 DbgValueLocation Loc = Defs[i].second;
728 const MachineOperand &LocMO = locations[Loc.locNo()];
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000729
Reid Kleckner04e25e02017-10-03 17:59:02 +0000730 if (!LocMO.isReg()) {
731 extendDef(Idx, Loc, nullptr, nullptr, nullptr, LIS);
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000732 continue;
733 }
734
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000735 // Register locations are constrained to where the register value is live.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000736 if (TargetRegisterInfo::isVirtualRegister(LocMO.getReg())) {
Craig Topperc0196b12014-04-14 00:51:57 +0000737 LiveInterval *LI = nullptr;
738 const VNInfo *VNI = nullptr;
Reid Kleckner04e25e02017-10-03 17:59:02 +0000739 if (LIS.hasInterval(LocMO.getReg())) {
740 LI = &LIS.getInterval(LocMO.getReg());
Jakob Stoklund Olesen48a16472012-06-22 18:51:35 +0000741 VNI = LI->getVNInfoAt(Idx);
742 }
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000743 SmallVector<SlotIndex, 16> Kills;
Reid Kleckner04e25e02017-10-03 17:59:02 +0000744 extendDef(Idx, Loc, LI, VNI, &Kills, LIS);
Jakob Stoklund Olesen48a16472012-06-22 18:51:35 +0000745 if (LI)
Reid Kleckner04e25e02017-10-03 17:59:02 +0000746 addDefsFromCopies(LI, Loc.locNo(), Loc.wasIndirect(), Kills, Defs, MRI,
747 LIS);
Jakob Stoklund Olesen32449632012-06-22 17:15:32 +0000748 continue;
749 }
750
Bjorn Pettersson715a5ef2017-09-28 13:10:06 +0000751 // For physregs, we only mark the start slot idx. DwarfDebug will see it
752 // as if the DBG_VALUE is valid up until the end of the basic block, or
753 // the next def of the physical register. So we do not need to extend the
754 // range. It might actually happen that the DBG_VALUE is the last use of
755 // the physical register (e.g. if this is an unused input argument to a
756 // function).
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000757 }
758
Robert Lougher10f740d2017-08-03 11:54:02 +0000759 // Erase all the undefs.
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000760 for (LocMap::iterator I = locInts.begin(); I.valid();)
Reid Kleckner04e25e02017-10-03 17:59:02 +0000761 if (I.value().isUndef())
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000762 I.erase();
763 else
764 ++I;
Robert Lougher10f740d2017-08-03 11:54:02 +0000765
766 // The computed intervals may extend beyond the range of the debug
767 // location's lexical scope. In this case, splitting of an interval
768 // can result in an interval outside of the scope being created,
769 // causing extra unnecessary DBG_VALUEs to be emitted. To prevent
770 // this, trim the intervals to the lexical scope.
771
772 LexicalScope *Scope = LS.findLexicalScope(dl);
773 if (!Scope)
774 return;
775
776 SlotIndex PrevEnd;
777 LocMap::iterator I = locInts.begin();
778
779 // Iterate over the lexical scope ranges. Each time round the loop
780 // we check the intervals for overlap with the end of the previous
781 // range and the start of the next. The first range is handled as
782 // a special case where there is no PrevEnd.
783 for (const InsnRange &Range : Scope->getRanges()) {
784 SlotIndex RStart = LIS.getInstructionIndex(*Range.first);
785 SlotIndex REnd = LIS.getInstructionIndex(*Range.second);
786
787 // At the start of each iteration I has been advanced so that
788 // I.stop() >= PrevEnd. Check for overlap.
789 if (PrevEnd && I.start() < PrevEnd) {
790 SlotIndex IStop = I.stop();
Reid Kleckner04e25e02017-10-03 17:59:02 +0000791 DbgValueLocation Loc = I.value();
Robert Lougher10f740d2017-08-03 11:54:02 +0000792
793 // Stop overlaps previous end - trim the end of the interval to
794 // the scope range.
795 I.setStopUnchecked(PrevEnd);
796 ++I;
797
798 // If the interval also overlaps the start of the "next" (i.e.
799 // current) range create a new interval for the remainder (which
800 // may be further trimmed).
801 if (RStart < IStop)
Reid Kleckner04e25e02017-10-03 17:59:02 +0000802 I.insert(RStart, IStop, Loc);
Robert Lougher10f740d2017-08-03 11:54:02 +0000803 }
804
805 // Advance I so that I.stop() >= RStart, and check for overlap.
806 I.advanceTo(RStart);
807 if (!I.valid())
808 return;
809
810 if (I.start() < RStart) {
811 // Interval start overlaps range - trim to the scope range.
812 I.setStartUnchecked(RStart);
813 // Remember that this interval was trimmed.
814 trimmedDefs.insert(RStart);
815 }
816
817 // The end of a lexical scope range is the last instruction in the
818 // range. To convert to an interval we need the index of the
819 // instruction after it.
820 REnd = REnd.getNextIndex();
821
822 // Advance I to first interval outside current range.
823 I.advanceTo(REnd);
824 if (!I.valid())
825 return;
826
827 PrevEnd = REnd;
828 }
829
830 // Check for overlap with end of final range.
831 if (PrevEnd && I.start() < PrevEnd)
832 I.setStopUnchecked(PrevEnd);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000833}
834
835void LDVImpl::computeIntervals() {
Robert Lougher10f740d2017-08-03 11:54:02 +0000836 LexicalScopes LS;
837 LS.initialize(*MF);
838
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000839 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
Robert Lougher10f740d2017-08-03 11:54:02 +0000840 userValues[i]->computeIntervals(MF->getRegInfo(), *TRI, *LIS, LS);
Jakob Stoklund Olesen816f5f42011-03-18 21:42:19 +0000841 userValues[i]->mapVirtRegs(this);
842 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000843}
844
845bool LDVImpl::runOnMachineFunction(MachineFunction &mf) {
David Blaikie2f040112014-07-25 16:10:16 +0000846 clear();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000847 MF = &mf;
848 LIS = &pass.getAnalysis<LiveIntervals>();
Eric Christopherfc6de422014-08-05 02:39:49 +0000849 TRI = mf.getSubtarget().getRegisterInfo();
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000850 DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: "
David Blaikiec8c29202012-08-22 17:18:53 +0000851 << mf.getName() << " **********\n");
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000852
853 bool Changed = collectDebugValues(mf);
854 computeIntervals();
855 DEBUG(print(dbgs()));
Manman Ren7a4c8a72013-02-13 20:23:48 +0000856 ModifiedMF = Changed;
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000857 return Changed;
858}
859
David Blaikie2f040112014-07-25 16:10:16 +0000860static void removeDebugValues(MachineFunction &mf) {
861 for (MachineBasicBlock &MBB : mf) {
862 for (auto MBBI = MBB.begin(), MBBE = MBB.end(); MBBI != MBBE; ) {
863 if (!MBBI->isDebugValue()) {
864 ++MBBI;
865 continue;
866 }
867 MBBI = MBB.erase(MBBI);
868 }
869 }
870}
871
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +0000872bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) {
Devang Patelacbee0b2011-01-07 22:33:41 +0000873 if (!EnableLDV)
874 return false;
Matthias Braunf1caa282017-12-15 22:22:58 +0000875 if (!mf.getFunction().getSubprogram()) {
David Blaikie2f040112014-07-25 16:10:16 +0000876 removeDebugValues(mf);
877 return false;
878 }
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000879 if (!pImpl)
880 pImpl = new LDVImpl(this);
Manman Ren7a4c8a72013-02-13 20:23:48 +0000881 return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf);
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000882}
883
884void LiveDebugVariables::releaseMemory() {
Manman Ren7a4c8a72013-02-13 20:23:48 +0000885 if (pImpl)
Jakob Stoklund Olesen4be0bd72010-12-02 00:37:37 +0000886 static_cast<LDVImpl*>(pImpl)->clear();
887}
888
889LiveDebugVariables::~LiveDebugVariables() {
890 if (pImpl)
891 delete static_cast<LDVImpl*>(pImpl);
Jakob Stoklund Olesend4900a62010-11-30 02:17:10 +0000892}
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +0000893
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000894//===----------------------------------------------------------------------===//
895// Live Range Splitting
896//===----------------------------------------------------------------------===//
897
898bool
Mark Laceyf9ea8852013-08-14 23:50:04 +0000899UserValue::splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs,
900 LiveIntervals& LIS) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000901 DEBUG({
902 dbgs() << "Splitting Loc" << OldLocNo << '\t';
Craig Topperc0196b12014-04-14 00:51:57 +0000903 print(dbgs(), nullptr);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000904 });
905 bool DidChange = false;
906 LocMap::iterator LocMapI;
907 LocMapI.setMap(locInts);
908 for (unsigned i = 0; i != NewRegs.size(); ++i) {
Mark Laceyf9ea8852013-08-14 23:50:04 +0000909 LiveInterval *LI = &LIS.getInterval(NewRegs[i]);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000910 if (LI->empty())
911 continue;
912
913 // Don't allocate the new LocNo until it is needed.
Reid Klecknereed09732017-09-15 22:08:50 +0000914 unsigned NewLocNo = UndefLocNo;
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000915
916 // Iterate over the overlaps between locInts and LI.
917 LocMapI.find(LI->beginIndex());
918 if (!LocMapI.valid())
919 continue;
920 LiveInterval::iterator LII = LI->advanceTo(LI->begin(), LocMapI.start());
921 LiveInterval::iterator LIE = LI->end();
922 while (LocMapI.valid() && LII != LIE) {
923 // At this point, we know that LocMapI.stop() > LII->start.
924 LII = LI->advanceTo(LII, LocMapI.start());
925 if (LII == LIE)
926 break;
927
928 // Now LII->end > LocMapI.start(). Do we have an overlap?
Reid Kleckner04e25e02017-10-03 17:59:02 +0000929 if (LocMapI.value().locNo() == OldLocNo && LII->start < LocMapI.stop()) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000930 // Overlapping correct location. Allocate NewLocNo now.
Reid Klecknereed09732017-09-15 22:08:50 +0000931 if (NewLocNo == UndefLocNo) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000932 MachineOperand MO = MachineOperand::CreateReg(LI->reg, false);
933 MO.setSubReg(locations[OldLocNo].getSubReg());
934 NewLocNo = getLocationNo(MO);
935 DidChange = true;
936 }
937
938 SlotIndex LStart = LocMapI.start();
939 SlotIndex LStop = LocMapI.stop();
Reid Kleckner04e25e02017-10-03 17:59:02 +0000940 DbgValueLocation OldLoc = LocMapI.value();
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000941
942 // Trim LocMapI down to the LII overlap.
943 if (LStart < LII->start)
944 LocMapI.setStartUnchecked(LII->start);
945 if (LStop > LII->end)
946 LocMapI.setStopUnchecked(LII->end);
947
948 // Change the value in the overlap. This may trigger coalescing.
Reid Kleckner04e25e02017-10-03 17:59:02 +0000949 LocMapI.setValue(OldLoc.changeLocNo(NewLocNo));
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000950
951 // Re-insert any removed OldLocNo ranges.
952 if (LStart < LocMapI.start()) {
Reid Kleckner04e25e02017-10-03 17:59:02 +0000953 LocMapI.insert(LStart, LocMapI.start(), OldLoc);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000954 ++LocMapI;
955 assert(LocMapI.valid() && "Unexpected coalescing");
956 }
957 if (LStop > LocMapI.stop()) {
958 ++LocMapI;
Reid Kleckner04e25e02017-10-03 17:59:02 +0000959 LocMapI.insert(LII->end, LStop, OldLoc);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000960 --LocMapI;
961 }
962 }
963
964 // Advance to the next overlap.
965 if (LII->end < LocMapI.stop()) {
966 if (++LII == LIE)
967 break;
968 LocMapI.advanceTo(LII->start);
969 } else {
970 ++LocMapI;
971 if (!LocMapI.valid())
972 break;
973 LII = LI->advanceTo(LII, LocMapI.start());
974 }
975 }
976 }
977
978 // Finally, remove any remaining OldLocNo intervals and OldLocNo itself.
979 locations.erase(locations.begin() + OldLocNo);
980 LocMapI.goToBegin();
981 while (LocMapI.valid()) {
Reid Kleckner04e25e02017-10-03 17:59:02 +0000982 DbgValueLocation v = LocMapI.value();
983 if (v.locNo() == OldLocNo) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000984 DEBUG(dbgs() << "Erasing [" << LocMapI.start() << ';'
985 << LocMapI.stop() << ")\n");
986 LocMapI.erase();
987 } else {
Reid Kleckner04e25e02017-10-03 17:59:02 +0000988 if (v.locNo() > OldLocNo)
989 LocMapI.setValueUnchecked(v.changeLocNo(v.locNo() - 1));
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000990 ++LocMapI;
991 }
992 }
993
Craig Topperc0196b12014-04-14 00:51:57 +0000994 DEBUG({dbgs() << "Split result: \t"; print(dbgs(), nullptr);});
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000995 return DidChange;
996}
997
998bool
Mark Laceyf9ea8852013-08-14 23:50:04 +0000999UserValue::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs,
1000 LiveIntervals &LIS) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001001 bool DidChange = false;
Jakob Stoklund Olesen57c8f582011-05-06 19:31:19 +00001002 // Split locations referring to OldReg. Iterate backwards so splitLocation can
Eric Christopherbe153e62012-03-15 21:33:35 +00001003 // safely erase unused locations.
Jakob Stoklund Olesen57c8f582011-05-06 19:31:19 +00001004 for (unsigned i = locations.size(); i ; --i) {
1005 unsigned LocNo = i-1;
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001006 const MachineOperand *Loc = &locations[LocNo];
1007 if (!Loc->isReg() || Loc->getReg() != OldReg)
1008 continue;
Mark Laceyf9ea8852013-08-14 23:50:04 +00001009 DidChange |= splitLocation(LocNo, NewRegs, LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001010 }
1011 return DidChange;
1012}
1013
Mark Laceyf9ea8852013-08-14 23:50:04 +00001014void LDVImpl::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001015 bool DidChange = false;
1016 for (UserValue *UV = lookupVirtReg(OldReg); UV; UV = UV->getNext())
Mark Laceyf9ea8852013-08-14 23:50:04 +00001017 DidChange |= UV->splitRegister(OldReg, NewRegs, *LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001018
1019 if (!DidChange)
1020 return;
1021
1022 // Map all of the new virtual registers.
1023 UserValue *UV = lookupVirtReg(OldReg);
1024 for (unsigned i = 0; i != NewRegs.size(); ++i)
Mark Laceyf9ea8852013-08-14 23:50:04 +00001025 mapVirtReg(NewRegs[i], UV);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001026}
1027
1028void LiveDebugVariables::
Mark Laceyf9ea8852013-08-14 23:50:04 +00001029splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs, LiveIntervals &LIS) {
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001030 if (pImpl)
1031 static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs);
1032}
1033
Reid Kleckner4e040282017-09-20 18:19:08 +00001034void UserValue::rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI,
1035 BitVector &SpilledLocations) {
Reid Kleckner92687d42017-09-20 17:32:54 +00001036 // Build a set of new locations with new numbers so we can coalesce our
1037 // IntervalMap if two vreg intervals collapse to the same physical location.
1038 // Use MapVector instead of SetVector because MapVector::insert returns the
Reid Kleckner4e040282017-09-20 18:19:08 +00001039 // position of the previously or newly inserted element. The boolean value
1040 // tracks if the location was produced by a spill.
1041 // FIXME: This will be problematic if we ever support direct and indirect
1042 // frame index locations, i.e. expressing both variables in memory and
1043 // 'int x, *px = &x'. The "spilled" bit must become part of the location.
Reid Kleckner92687d42017-09-20 17:32:54 +00001044 MapVector<MachineOperand, bool> NewLocations;
1045 SmallVector<unsigned, 4> LocNoMap(locations.size());
1046 for (unsigned I = 0, E = locations.size(); I != E; ++I) {
Reid Kleckner4e040282017-09-20 18:19:08 +00001047 bool Spilled = false;
Reid Kleckner92687d42017-09-20 17:32:54 +00001048 MachineOperand Loc = locations[I];
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001049 // Only virtual registers are rewritten.
Reid Kleckner92687d42017-09-20 17:32:54 +00001050 if (Loc.isReg() && Loc.getReg() &&
1051 TargetRegisterInfo::isVirtualRegister(Loc.getReg())) {
1052 unsigned VirtReg = Loc.getReg();
1053 if (VRM.isAssignedReg(VirtReg) &&
1054 TargetRegisterInfo::isPhysicalRegister(VRM.getPhys(VirtReg))) {
1055 // This can create a %noreg operand in rare cases when the sub-register
1056 // index is no longer available. That means the user value is in a
1057 // non-existent sub-register, and %noreg is exactly what we want.
1058 Loc.substPhysReg(VRM.getPhys(VirtReg), TRI);
1059 } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT) {
1060 // FIXME: Translate SubIdx to a stackslot offset.
1061 Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg));
Reid Kleckner4e040282017-09-20 18:19:08 +00001062 Spilled = true;
Reid Kleckner92687d42017-09-20 17:32:54 +00001063 } else {
1064 Loc.setReg(0);
1065 Loc.setSubReg(0);
1066 }
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001067 }
Reid Kleckner92687d42017-09-20 17:32:54 +00001068
1069 // Insert this location if it doesn't already exist and record a mapping
1070 // from the old number to the new number.
Reid Kleckner4e040282017-09-20 18:19:08 +00001071 auto InsertResult = NewLocations.insert({Loc, Spilled});
1072 unsigned NewLocNo = std::distance(NewLocations.begin(), InsertResult.first);
1073 LocNoMap[I] = NewLocNo;
Reid Kleckner92687d42017-09-20 17:32:54 +00001074 }
1075
Reid Kleckner4e040282017-09-20 18:19:08 +00001076 // Rewrite the locations and record which ones were spill slots.
Reid Kleckner92687d42017-09-20 17:32:54 +00001077 locations.clear();
Reid Kleckner4e040282017-09-20 18:19:08 +00001078 SpilledLocations.clear();
1079 SpilledLocations.resize(NewLocations.size());
1080 for (auto &Pair : NewLocations) {
Reid Kleckner92687d42017-09-20 17:32:54 +00001081 locations.push_back(Pair.first);
Reid Kleckner4e040282017-09-20 18:19:08 +00001082 if (Pair.second) {
1083 unsigned NewLocNo = std::distance(&*NewLocations.begin(), &Pair);
1084 SpilledLocations.set(NewLocNo);
1085 }
1086 }
Reid Kleckner92687d42017-09-20 17:32:54 +00001087
1088 // Update the interval map, but only coalesce left, since intervals to the
1089 // right use the old location numbers. This should merge two contiguous
1090 // DBG_VALUE intervals with different vregs that were allocated to the same
1091 // physical register.
1092 for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) {
Reid Kleckner04e25e02017-10-03 17:59:02 +00001093 DbgValueLocation Loc = I.value();
1094 unsigned NewLocNo = LocNoMap[Loc.locNo()];
1095 I.setValueUnchecked(Loc.changeLocNo(NewLocNo));
Reid Kleckner92687d42017-09-20 17:32:54 +00001096 I.setStart(I.start());
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001097 }
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001098}
1099
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001100/// Find an iterator for inserting a DBG_VALUE instruction.
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001101static MachineBasicBlock::iterator
Devang Patel26ffa012011-02-04 01:43:25 +00001102findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx,
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001103 LiveIntervals &LIS) {
1104 SlotIndex Start = LIS.getMBBStartIdx(MBB);
1105 Idx = Idx.getBaseIndex();
1106
1107 // Try to find an insert location by going backwards from Idx.
1108 MachineInstr *MI;
1109 while (!(MI = LIS.getInstructionFromIndex(Idx))) {
1110 // We've reached the beginning of MBB.
1111 if (Idx == Start) {
Keith Walker830a8c12016-09-16 14:07:29 +00001112 MachineBasicBlock::iterator I = MBB->SkipPHIsLabelsAndDebug(MBB->begin());
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001113 return I;
1114 }
1115 Idx = Idx.getPrevIndex();
1116 }
Devang Patel26ffa012011-02-04 01:43:25 +00001117
Jakob Stoklund Olesen088b30a2011-01-13 23:35:53 +00001118 // Don't insert anything after the first terminator, though.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001119 return MI->isTerminator() ? MBB->getFirstTerminator() :
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001120 std::next(MachineBasicBlock::iterator(MI));
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001121}
1122
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001123/// Find an iterator for inserting the next DBG_VALUE instruction
1124/// (or end if no more insert locations found).
1125static MachineBasicBlock::iterator
1126findNextInsertLocation(MachineBasicBlock *MBB,
1127 MachineBasicBlock::iterator I,
1128 SlotIndex StopIdx, MachineOperand &LocMO,
1129 LiveIntervals &LIS,
1130 const TargetRegisterInfo &TRI) {
1131 if (!LocMO.isReg())
1132 return MBB->instr_end();
1133 unsigned Reg = LocMO.getReg();
1134
1135 // Find the next instruction in the MBB that define the register Reg.
Stefan Maksimovic991af7a2018-02-09 14:03:26 +00001136 while (I != MBB->end() && !I->isTerminator()) {
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001137 if (!LIS.isNotInMIMap(*I) &&
1138 SlotIndex::isEarlierEqualInstr(StopIdx, LIS.getInstructionIndex(*I)))
1139 break;
1140 if (I->definesRegister(Reg, &TRI))
1141 // The insert location is directly after the instruction/bundle.
1142 return std::next(I);
1143 ++I;
1144 }
1145 return MBB->end();
1146}
1147
1148void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex StartIdx,
1149 SlotIndex StopIdx,
Reid Kleckner04e25e02017-10-03 17:59:02 +00001150 DbgValueLocation Loc, bool Spilled,
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001151 LiveIntervals &LIS,
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001152 const TargetInstrInfo &TII,
1153 const TargetRegisterInfo &TRI) {
1154 SlotIndex MBBEndIdx = LIS.getMBBEndIdx(&*MBB);
1155 // Only search within the current MBB.
1156 StopIdx = (MBBEndIdx < StopIdx) ? MBBEndIdx : StopIdx;
1157 MachineBasicBlock::iterator I = findInsertLocation(MBB, StartIdx, LIS);
Reid Kleckner04e25e02017-10-03 17:59:02 +00001158 MachineOperand &MO = locations[Loc.locNo()];
Devang Pateleabc3cea2011-08-04 20:42:11 +00001159 ++NumInsertedDebugValues;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001160
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001161 assert(cast<DILocalVariable>(Variable)
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +00001162 ->isValidLocationForIntrinsic(getDebugLoc()) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +00001163 "Expected inlined-at fields to agree");
Reid Kleckner4e040282017-09-20 18:19:08 +00001164
1165 // If the location was spilled, the new DBG_VALUE will be indirect. If the
1166 // original DBG_VALUE was indirect, we need to add DW_OP_deref to indicate
1167 // that the original virtual register was a pointer.
Reid Kleckner4e040282017-09-20 18:19:08 +00001168 const DIExpression *Expr = Expression;
Reid Kleckner04e25e02017-10-03 17:59:02 +00001169 bool IsIndirect = Loc.wasIndirect();
1170 if (Spilled) {
1171 if (IsIndirect)
1172 Expr = DIExpression::prepend(Expr, DIExpression::WithDeref);
1173 IsIndirect = true;
1174 }
Reid Kleckner4e040282017-09-20 18:19:08 +00001175
Reid Kleckner04e25e02017-10-03 17:59:02 +00001176 assert((!Spilled || MO.isFI()) && "a spilled location must be a frame index");
Reid Kleckner4e040282017-09-20 18:19:08 +00001177
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001178 do {
1179 MachineInstrBuilder MIB =
Reid Kleckner4e040282017-09-20 18:19:08 +00001180 BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_VALUE))
Reid Kleckner04e25e02017-10-03 17:59:02 +00001181 .add(MO);
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001182 if (IsIndirect)
1183 MIB.addImm(0U);
1184 else
1185 MIB.addReg(0U, RegState::Debug);
1186 MIB.addMetadata(Variable).addMetadata(Expr);
1187
1188 // Continue and insert DBG_VALUES after every redefinition of register
1189 // associated with the debug value within the range
1190 I = findNextInsertLocation(MBB, I, StopIdx, MO, LIS, TRI);
1191 } while (I != MBB->end());
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001192}
1193
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001194void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
Reid Kleckner4e040282017-09-20 18:19:08 +00001195 const TargetInstrInfo &TII,
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001196 const TargetRegisterInfo &TRI,
Reid Kleckner4e040282017-09-20 18:19:08 +00001197 const BitVector &SpilledLocations) {
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001198 MachineFunction::iterator MFEnd = VRM->getMachineFunction().end();
1199
1200 for (LocMap::const_iterator I = locInts.begin(); I.valid();) {
1201 SlotIndex Start = I.start();
1202 SlotIndex Stop = I.stop();
Reid Kleckner04e25e02017-10-03 17:59:02 +00001203 DbgValueLocation Loc = I.value();
1204 bool Spilled = !Loc.isUndef() ? SpilledLocations.test(Loc.locNo()) : false;
Robert Lougher10f740d2017-08-03 11:54:02 +00001205
1206 // If the interval start was trimmed to the lexical scope insert the
1207 // DBG_VALUE at the previous index (otherwise it appears after the
1208 // first instruction in the range).
1209 if (trimmedDefs.count(Start))
1210 Start = Start.getPrevIndex();
1211
Reid Kleckner04e25e02017-10-03 17:59:02 +00001212 DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << Loc.locNo());
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +00001213 MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator();
1214 SlotIndex MBBEnd = LIS.getMBBEndIdx(&*MBB);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001215
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +00001216 DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd);
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001217 insertDebugValue(&*MBB, Start, Stop, Loc, Spilled, LIS, TII, TRI);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001218 // This interval may span multiple basic blocks.
1219 // Insert a DBG_VALUE into each one.
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001220 while (Stop > MBBEnd) {
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001221 // Move to the next block.
1222 Start = MBBEnd;
1223 if (++MBB == MFEnd)
1224 break;
Duncan P. N. Exon Smith5ae59392015-10-09 19:13:58 +00001225 MBBEnd = LIS.getMBBEndIdx(&*MBB);
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +00001226 DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd);
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001227 insertDebugValue(&*MBB, Start, Stop, Loc, Spilled, LIS, TII, TRI);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001228 }
1229 DEBUG(dbgs() << '\n');
1230 if (MBB == MFEnd)
1231 break;
1232
1233 ++I;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001234 }
1235}
1236
1237void LDVImpl::emitDebugValues(VirtRegMap *VRM) {
1238 DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n");
David Blaikie2f040112014-07-25 16:10:16 +00001239 if (!MF)
1240 return;
Eric Christopherfc6de422014-08-05 02:39:49 +00001241 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
Reid Kleckner4e040282017-09-20 18:19:08 +00001242 BitVector SpilledLocations;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001243 for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
Eric Christopher1cdefae2015-02-27 00:11:34 +00001244 DEBUG(userValues[i]->print(dbgs(), TRI));
Reid Kleckner4e040282017-09-20 18:19:08 +00001245 userValues[i]->rewriteLocations(*VRM, *TRI, SpilledLocations);
Karl-Johan Karlsson8d8d2012017-10-05 08:37:31 +00001246 userValues[i]->emitDebugValues(VRM, *LIS, *TII, *TRI, SpilledLocations);
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001247 }
Manman Ren7a4c8a72013-02-13 20:23:48 +00001248 EmitDone = true;
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001249}
1250
1251void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) {
Manman Ren7a4c8a72013-02-13 20:23:48 +00001252 if (pImpl)
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001253 static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM);
1254}
1255
David Blaikie2f040112014-07-25 16:10:16 +00001256bool LiveDebugVariables::doInitialization(Module &M) {
David Blaikie2f040112014-07-25 16:10:16 +00001257 return Pass::doInitialization(M);
1258}
Jakob Stoklund Olesenafc2bc22010-12-03 21:47:10 +00001259
Aaron Ballman615eb472017-10-15 14:32:27 +00001260#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Sam Clegg705f7982017-06-21 22:19:17 +00001261LLVM_DUMP_METHOD void LiveDebugVariables::dump() const {
Jakob Stoklund Olesen9ec20112010-12-02 18:15:44 +00001262 if (pImpl)
1263 static_cast<LDVImpl*>(pImpl)->print(dbgs());
1264}
1265#endif