blob: 131167630d655daaab0560de2e4d6bcbfaa63563 [file] [log] [blame]
Vikram TV859ad292015-12-16 11:09:48 +00001//===------ LiveDebugValues.cpp - Tracking Debug Value MIs ----------------===//
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 pass implements a data flow analysis that propagates debug location
11/// information by inserting additional DBG_VALUE instructions into the machine
12/// instruction stream. The pass internally builds debug location liveness
13/// ranges to determine the points where additional DBG_VALUEs need to be
14/// inserted.
15///
16/// This is a separate pass from DbgValueHistoryCalculator to facilitate
17/// testing and improve modularity.
18///
19//===----------------------------------------------------------------------===//
20
Daniel Berlin72560592016-01-10 18:08:32 +000021#include "llvm/ADT/PostOrderIterator.h"
22#include "llvm/ADT/SmallPtrSet.h"
Adrian Prantl6ee02c72016-05-25 22:21:12 +000023#include "llvm/ADT/SparseBitVector.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000024#include "llvm/ADT/Statistic.h"
Adrian Prantl6ee02c72016-05-25 22:21:12 +000025#include "llvm/ADT/UniqueVector.h"
Adrian Prantl7f5866c2016-09-28 17:51:14 +000026#include "llvm/CodeGen/LexicalScopes.h"
Vikram TV859ad292015-12-16 11:09:48 +000027#include "llvm/CodeGen/MachineFunction.h"
28#include "llvm/CodeGen/MachineFunctionPass.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
30#include "llvm/CodeGen/Passes.h"
Reid Kleckner28865802016-04-14 18:29:59 +000031#include "llvm/IR/DebugInfo.h"
Vikram TV859ad292015-12-16 11:09:48 +000032#include "llvm/Support/Debug.h"
33#include "llvm/Support/raw_ostream.h"
34#include "llvm/Target/TargetInstrInfo.h"
Reid Klecknerf6f04f82016-03-25 17:54:46 +000035#include "llvm/Target/TargetLowering.h"
Vikram TV859ad292015-12-16 11:09:48 +000036#include "llvm/Target/TargetRegisterInfo.h"
37#include "llvm/Target/TargetSubtargetInfo.h"
Vikram TV859ad292015-12-16 11:09:48 +000038#include <list>
Mehdi Aminib550cb12016-04-18 09:17:29 +000039#include <queue>
Vikram TV859ad292015-12-16 11:09:48 +000040
41using namespace llvm;
42
43#define DEBUG_TYPE "live-debug-values"
44
45STATISTIC(NumInserted, "Number of DBG_VALUE instructions inserted");
46
47namespace {
48
Adrian Prantl6ee02c72016-05-25 22:21:12 +000049// \brief If @MI is a DBG_VALUE with debug value described by a defined
50// register, returns the number of this register. In the other case, returns 0.
Adrian Prantl00698732016-05-25 22:37:29 +000051static unsigned isDbgValueDescribedByReg(const MachineInstr &MI) {
Adrian Prantl6ee02c72016-05-25 22:21:12 +000052 assert(MI.isDebugValue() && "expected a DBG_VALUE");
53 assert(MI.getNumOperands() == 4 && "malformed DBG_VALUE");
54 // If location of variable is described using a register (directly
55 // or indirectly), this register is always a first operand.
56 return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0;
57}
58
Vikram TV859ad292015-12-16 11:09:48 +000059class LiveDebugValues : public MachineFunctionPass {
60
61private:
62 const TargetRegisterInfo *TRI;
63 const TargetInstrInfo *TII;
Adrian Prantl7f5866c2016-09-28 17:51:14 +000064 LexicalScopes LS;
65
66 /// Keeps track of lexical scopes associated with a user value's source
67 /// location.
68 class UserValueScopes {
69 DebugLoc DL;
70 LexicalScopes &LS;
71 SmallPtrSet<const MachineBasicBlock *, 4> LBlocks;
72
73 public:
74 UserValueScopes(DebugLoc D, LexicalScopes &L) : DL(std::move(D)), LS(L) {}
75
76 /// Return true if current scope dominates at least one machine
77 /// instruction in a given machine basic block.
78 bool dominates(MachineBasicBlock *MBB) {
79 if (LBlocks.empty())
80 LS.getMachineBasicBlocks(DL, LBlocks);
81 return LBlocks.count(MBB) != 0 || LS.dominates(DL, MBB);
82 }
83 };
Vikram TV859ad292015-12-16 11:09:48 +000084
Adrian Prantl7509d542016-05-26 21:42:47 +000085 /// Based on std::pair so it can be used as an index into a DenseMap.
Vikram TV859ad292015-12-16 11:09:48 +000086 typedef std::pair<const DILocalVariable *, const DILocation *>
Adrian Prantl7509d542016-05-26 21:42:47 +000087 DebugVariableBase;
Vikram TV859ad292015-12-16 11:09:48 +000088 /// A potentially inlined instance of a variable.
Adrian Prantl7509d542016-05-26 21:42:47 +000089 struct DebugVariable : public DebugVariableBase {
90 DebugVariable(const DILocalVariable *Var, const DILocation *InlinedAt)
91 : DebugVariableBase(Var, InlinedAt) {}
Vikram TV859ad292015-12-16 11:09:48 +000092
Adrian Prantl7509d542016-05-26 21:42:47 +000093 const DILocalVariable *getVar() const { return this->first; };
94 const DILocation *getInlinedAt() const { return this->second; };
Vikram TV859ad292015-12-16 11:09:48 +000095
Adrian Prantl6ee02c72016-05-25 22:21:12 +000096 bool operator<(const DebugVariable &DV) const {
Adrian Prantl7509d542016-05-26 21:42:47 +000097 if (getVar() == DV.getVar())
98 return getInlinedAt() < DV.getInlinedAt();
99 return getVar() < DV.getVar();
Vikram TV859ad292015-12-16 11:09:48 +0000100 }
101 };
102
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000103 /// A pair of debug variable and value location.
Vikram TV859ad292015-12-16 11:09:48 +0000104 struct VarLoc {
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000105 const DebugVariable Var;
106 const MachineInstr &MI; ///< Only used for cloning a new DBG_VALUE.
Adrian Prantl7f5866c2016-09-28 17:51:14 +0000107 mutable UserValueScopes UVS;
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000108 enum { InvalidKind = 0, RegisterKind } Kind;
Vikram TV859ad292015-12-16 11:09:48 +0000109
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000110 /// The value location. Stored separately to avoid repeatedly
111 /// extracting it from MI.
112 union {
113 struct {
114 uint32_t RegNo;
115 uint32_t Offset;
116 } RegisterLoc;
117 uint64_t Hash;
118 } Loc;
119
Adrian Prantl7f5866c2016-09-28 17:51:14 +0000120 VarLoc(const MachineInstr &MI, LexicalScopes &LS)
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000121 : Var(MI.getDebugVariable(), MI.getDebugLoc()->getInlinedAt()), MI(MI),
Adrian Prantl7f5866c2016-09-28 17:51:14 +0000122 UVS(MI.getDebugLoc(), LS), Kind(InvalidKind) {
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000123 static_assert((sizeof(Loc) == sizeof(uint64_t)),
124 "hash does not cover all members of Loc");
125 assert(MI.isDebugValue() && "not a DBG_VALUE");
126 assert(MI.getNumOperands() == 4 && "malformed DBG_VALUE");
Adrian Prantl00698732016-05-25 22:37:29 +0000127 if (int RegNo = isDbgValueDescribedByReg(MI)) {
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000128 Kind = RegisterKind;
129 Loc.RegisterLoc.RegNo = RegNo;
Nico Weberee0b0ec2017-02-10 21:57:30 +0000130 uint64_t Offset =
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000131 MI.isIndirectDebugValue() ? MI.getOperand(1).getImm() : 0;
132 // We don't support offsets larger than 4GiB here. They are
133 // slated to be replaced with DIExpressions anyway.
Nico Weberee0b0ec2017-02-10 21:57:30 +0000134 if (Offset >= (1ULL << 32))
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000135 Kind = InvalidKind;
136 else
137 Loc.RegisterLoc.Offset = Offset;
138 }
139 }
140
141 /// If this variable is described by a register, return it,
142 /// otherwise return 0.
143 unsigned isDescribedByReg() const {
144 if (Kind == RegisterKind)
145 return Loc.RegisterLoc.RegNo;
146 return 0;
147 }
148
Adrian Prantl7f5866c2016-09-28 17:51:14 +0000149 /// Determine whether the lexical scope of this value's debug location
150 /// dominates MBB.
151 bool dominates(MachineBasicBlock &MBB) const { return UVS.dominates(&MBB); }
152
Matthias Braun194ded52017-01-28 06:53:55 +0000153#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
154 LLVM_DUMP_METHOD void dump() const { MI.dump(); }
155#endif
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000156
157 bool operator==(const VarLoc &Other) const {
158 return Var == Other.Var && Loc.Hash == Other.Loc.Hash;
159 }
160
Adrian Prantl7509d542016-05-26 21:42:47 +0000161 /// This operator guarantees that VarLocs are sorted by Variable first.
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000162 bool operator<(const VarLoc &Other) const {
163 if (Var == Other.Var)
164 return Loc.Hash < Other.Loc.Hash;
165 return Var < Other.Var;
166 }
Vikram TV859ad292015-12-16 11:09:48 +0000167 };
168
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000169 typedef UniqueVector<VarLoc> VarLocMap;
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000170 typedef SparseBitVector<> VarLocSet;
171 typedef SmallDenseMap<const MachineBasicBlock *, VarLocSet> VarLocInMBB;
Vikram TV859ad292015-12-16 11:09:48 +0000172
Adrian Prantl7509d542016-05-26 21:42:47 +0000173 /// This holds the working set of currently open ranges. For fast
174 /// access, this is done both as a set of VarLocIDs, and a map of
175 /// DebugVariable to recent VarLocID. Note that a DBG_VALUE ends all
176 /// previous open ranges for the same variable.
177 class OpenRangesSet {
178 VarLocSet VarLocs;
179 SmallDenseMap<DebugVariableBase, unsigned, 8> Vars;
180
181 public:
182 const VarLocSet &getVarLocs() const { return VarLocs; }
183
184 /// Terminate all open ranges for Var by removing it from the set.
185 void erase(DebugVariable Var) {
186 auto It = Vars.find(Var);
187 if (It != Vars.end()) {
188 unsigned ID = It->second;
189 VarLocs.reset(ID);
190 Vars.erase(It);
191 }
192 }
193
194 /// Terminate all open ranges listed in \c KillSet by removing
195 /// them from the set.
196 void erase(const VarLocSet &KillSet, const VarLocMap &VarLocIDs) {
197 VarLocs.intersectWithComplement(KillSet);
198 for (unsigned ID : KillSet)
199 Vars.erase(VarLocIDs[ID].Var);
200 }
201
202 /// Insert a new range into the set.
203 void insert(unsigned VarLocID, DebugVariableBase Var) {
204 VarLocs.set(VarLocID);
205 Vars.insert({Var, VarLocID});
206 }
207
208 /// Empty the set.
209 void clear() {
210 VarLocs.clear();
211 Vars.clear();
212 }
213
214 /// Return whether the set is empty or not.
215 bool empty() const {
216 assert(Vars.empty() == VarLocs.empty() && "open ranges are inconsistent");
217 return VarLocs.empty();
218 }
219 };
220
221 void transferDebugValue(const MachineInstr &MI, OpenRangesSet &OpenRanges,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000222 VarLocMap &VarLocIDs);
Adrian Prantl7509d542016-05-26 21:42:47 +0000223 void transferRegisterDef(MachineInstr &MI, OpenRangesSet &OpenRanges,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000224 const VarLocMap &VarLocIDs);
Adrian Prantl7509d542016-05-26 21:42:47 +0000225 bool transferTerminatorInst(MachineInstr &MI, OpenRangesSet &OpenRanges,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000226 VarLocInMBB &OutLocs, const VarLocMap &VarLocIDs);
Adrian Prantl7509d542016-05-26 21:42:47 +0000227 bool transfer(MachineInstr &MI, OpenRangesSet &OpenRanges,
Nico Weberee0b0ec2017-02-10 21:57:30 +0000228 VarLocInMBB &OutLocs, VarLocMap &VarLocIDs);
Vikram TV859ad292015-12-16 11:09:48 +0000229
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000230 bool join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs, VarLocInMBB &InLocs,
Keith Walker83ebef52016-09-27 16:46:07 +0000231 const VarLocMap &VarLocIDs,
232 SmallPtrSet<const MachineBasicBlock *, 16> &Visited);
Vikram TV859ad292015-12-16 11:09:48 +0000233
234 bool ExtendRanges(MachineFunction &MF);
235
236public:
237 static char ID;
238
239 /// Default construct and initialize the pass.
240 LiveDebugValues();
241
242 /// Tell the pass manager which passes we depend on and what
243 /// information we preserve.
244 void getAnalysisUsage(AnalysisUsage &AU) const override;
245
Derek Schuffad154c82016-03-28 17:05:30 +0000246 MachineFunctionProperties getRequiredProperties() const override {
247 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000248 MachineFunctionProperties::Property::NoVRegs);
Derek Schuffad154c82016-03-28 17:05:30 +0000249 }
250
Vikram TV859ad292015-12-16 11:09:48 +0000251 /// Print to ostream with a message.
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000252 void printVarLocInMBB(const MachineFunction &MF, const VarLocInMBB &V,
253 const VarLocMap &VarLocIDs, const char *msg,
Vikram TV859ad292015-12-16 11:09:48 +0000254 raw_ostream &Out) const;
255
256 /// Calculate the liveness information for the given machine function.
257 bool runOnMachineFunction(MachineFunction &MF) override;
258};
Adrian Prantl7f5866c2016-09-28 17:51:14 +0000259
Vikram TV859ad292015-12-16 11:09:48 +0000260} // namespace
261
262//===----------------------------------------------------------------------===//
263// Implementation
264//===----------------------------------------------------------------------===//
265
266char LiveDebugValues::ID = 0;
267char &llvm::LiveDebugValuesID = LiveDebugValues::ID;
268INITIALIZE_PASS(LiveDebugValues, "livedebugvalues", "Live DEBUG_VALUE analysis",
269 false, false)
270
271/// Default construct and initialize the pass.
272LiveDebugValues::LiveDebugValues() : MachineFunctionPass(ID) {
273 initializeLiveDebugValuesPass(*PassRegistry::getPassRegistry());
274}
275
276/// Tell the pass manager which passes we depend on and what information we
277/// preserve.
278void LiveDebugValues::getAnalysisUsage(AnalysisUsage &AU) const {
Matt Arsenaultb1630a12016-06-08 05:18:01 +0000279 AU.setPreservesCFG();
Vikram TV859ad292015-12-16 11:09:48 +0000280 MachineFunctionPass::getAnalysisUsage(AU);
281}
282
Vikram TV859ad292015-12-16 11:09:48 +0000283//===----------------------------------------------------------------------===//
284// Debug Range Extension Implementation
285//===----------------------------------------------------------------------===//
286
Matthias Braun194ded52017-01-28 06:53:55 +0000287#ifndef NDEBUG
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000288void LiveDebugValues::printVarLocInMBB(const MachineFunction &MF,
289 const VarLocInMBB &V,
290 const VarLocMap &VarLocIDs,
291 const char *msg,
Vikram TV859ad292015-12-16 11:09:48 +0000292 raw_ostream &Out) const {
Keith Walkerf83a19f2016-09-20 16:04:31 +0000293 Out << '\n' << msg << '\n';
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000294 for (const MachineBasicBlock &BB : MF) {
295 const auto &L = V.lookup(&BB);
296 Out << "MBB: " << BB.getName() << ":\n";
297 for (unsigned VLL : L) {
298 const VarLoc &VL = VarLocIDs[VLL];
Adrian Prantl7509d542016-05-26 21:42:47 +0000299 Out << " Var: " << VL.Var.getVar()->getName();
Vikram TV859ad292015-12-16 11:09:48 +0000300 Out << " MI: ";
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000301 VL.dump();
Vikram TV859ad292015-12-16 11:09:48 +0000302 }
303 }
304 Out << "\n";
305}
Matthias Braun194ded52017-01-28 06:53:55 +0000306#endif
Vikram TV859ad292015-12-16 11:09:48 +0000307
Vikram TV859ad292015-12-16 11:09:48 +0000308/// End all previous ranges related to @MI and start a new range from @MI
309/// if it is a DBG_VALUE instr.
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000310void LiveDebugValues::transferDebugValue(const MachineInstr &MI,
Adrian Prantl7509d542016-05-26 21:42:47 +0000311 OpenRangesSet &OpenRanges,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000312 VarLocMap &VarLocIDs) {
Vikram TV859ad292015-12-16 11:09:48 +0000313 if (!MI.isDebugValue())
314 return;
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000315 const DILocalVariable *Var = MI.getDebugVariable();
316 const DILocation *DebugLoc = MI.getDebugLoc();
317 const DILocation *InlinedAt = DebugLoc->getInlinedAt();
318 assert(Var->isValidLocationForIntrinsic(DebugLoc) &&
Vikram TV859ad292015-12-16 11:09:48 +0000319 "Expected inlined-at fields to agree");
Vikram TV859ad292015-12-16 11:09:48 +0000320
321 // End all previous ranges of Var.
Adrian Prantl7509d542016-05-26 21:42:47 +0000322 DebugVariable V(Var, InlinedAt);
323 OpenRanges.erase(V);
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000324
325 // Add the VarLoc to OpenRanges from this DBG_VALUE.
326 // TODO: Currently handles DBG_VALUE which has only reg as location.
Adrian Prantl7509d542016-05-26 21:42:47 +0000327 if (isDbgValueDescribedByReg(MI)) {
Adrian Prantl7f5866c2016-09-28 17:51:14 +0000328 VarLoc VL(MI, LS);
Adrian Prantl7509d542016-05-26 21:42:47 +0000329 unsigned ID = VarLocIDs.insert(VL);
330 OpenRanges.insert(ID, VL.Var);
331 }
Vikram TV859ad292015-12-16 11:09:48 +0000332}
333
334/// A definition of a register may mark the end of a range.
335void LiveDebugValues::transferRegisterDef(MachineInstr &MI,
Adrian Prantl7509d542016-05-26 21:42:47 +0000336 OpenRangesSet &OpenRanges,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000337 const VarLocMap &VarLocIDs) {
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000338 MachineFunction *MF = MI.getParent()->getParent();
339 const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
340 unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000341 SparseBitVector<> KillSet;
Vikram TV859ad292015-12-16 11:09:48 +0000342 for (const MachineOperand &MO : MI.operands()) {
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000343 if (MO.isReg() && MO.isDef() && MO.getReg() &&
344 TRI->isPhysicalRegister(MO.getReg())) {
345 // Remove ranges of all aliased registers.
346 for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI)
Adrian Prantl7509d542016-05-26 21:42:47 +0000347 for (unsigned ID : OpenRanges.getVarLocs())
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000348 if (VarLocIDs[ID].isDescribedByReg() == *RAI)
349 KillSet.set(ID);
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000350 } else if (MO.isRegMask()) {
351 // Remove ranges of all clobbered registers. Register masks don't usually
352 // list SP as preserved. While the debug info may be off for an
353 // instruction or two around callee-cleanup calls, transferring the
354 // DEBUG_VALUE across the call is still a better user experience.
Adrian Prantl7509d542016-05-26 21:42:47 +0000355 for (unsigned ID : OpenRanges.getVarLocs()) {
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000356 unsigned Reg = VarLocIDs[ID].isDescribedByReg();
357 if (Reg && Reg != SP && MO.clobbersPhysReg(Reg))
358 KillSet.set(ID);
359 }
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000360 }
Vikram TV859ad292015-12-16 11:09:48 +0000361 }
Adrian Prantl7509d542016-05-26 21:42:47 +0000362 OpenRanges.erase(KillSet, VarLocIDs);
Vikram TV859ad292015-12-16 11:09:48 +0000363}
364
365/// Terminate all open ranges at the end of the current basic block.
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000366bool LiveDebugValues::transferTerminatorInst(MachineInstr &MI,
Adrian Prantl7509d542016-05-26 21:42:47 +0000367 OpenRangesSet &OpenRanges,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000368 VarLocInMBB &OutLocs,
369 const VarLocMap &VarLocIDs) {
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000370 bool Changed = false;
Vikram TV859ad292015-12-16 11:09:48 +0000371 const MachineBasicBlock *CurMBB = MI.getParent();
372 if (!(MI.isTerminator() || (&MI == &CurMBB->instr_back())))
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000373 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000374
375 if (OpenRanges.empty())
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000376 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000377
Adrian Prantl7509d542016-05-26 21:42:47 +0000378 DEBUG(for (unsigned ID : OpenRanges.getVarLocs()) {
379 // Copy OpenRanges to OutLocs, if not already present.
380 dbgs() << "Add to OutLocs: "; VarLocIDs[ID].dump();
381 });
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000382 VarLocSet &VLS = OutLocs[CurMBB];
Adrian Prantl7509d542016-05-26 21:42:47 +0000383 Changed = VLS |= OpenRanges.getVarLocs();
Vikram TV859ad292015-12-16 11:09:48 +0000384 OpenRanges.clear();
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000385 return Changed;
Vikram TV859ad292015-12-16 11:09:48 +0000386}
387
388/// This routine creates OpenRanges and OutLocs.
Adrian Prantl7509d542016-05-26 21:42:47 +0000389bool LiveDebugValues::transfer(MachineInstr &MI, OpenRangesSet &OpenRanges,
Nico Weberee0b0ec2017-02-10 21:57:30 +0000390 VarLocInMBB &OutLocs, VarLocMap &VarLocIDs) {
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000391 bool Changed = false;
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000392 transferDebugValue(MI, OpenRanges, VarLocIDs);
393 transferRegisterDef(MI, OpenRanges, VarLocIDs);
394 Changed = transferTerminatorInst(MI, OpenRanges, OutLocs, VarLocIDs);
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000395 return Changed;
Vikram TV859ad292015-12-16 11:09:48 +0000396}
397
398/// This routine joins the analysis results of all incoming edges in @MBB by
399/// inserting a new DBG_VALUE instruction at the start of the @MBB - if the same
400/// source variable in all the predecessors of @MBB reside in the same location.
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000401bool LiveDebugValues::join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs,
Keith Walker83ebef52016-09-27 16:46:07 +0000402 VarLocInMBB &InLocs, const VarLocMap &VarLocIDs,
403 SmallPtrSet<const MachineBasicBlock *, 16> &Visited) {
Vikram TV859ad292015-12-16 11:09:48 +0000404 DEBUG(dbgs() << "join MBB: " << MBB.getName() << "\n");
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000405 bool Changed = false;
Vikram TV859ad292015-12-16 11:09:48 +0000406
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000407 VarLocSet InLocsT; // Temporary incoming locations.
Vikram TV859ad292015-12-16 11:09:48 +0000408
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000409 // For all predecessors of this MBB, find the set of VarLocs that
410 // can be joined.
Keith Walker83ebef52016-09-27 16:46:07 +0000411 int NumVisited = 0;
Vikram TV859ad292015-12-16 11:09:48 +0000412 for (auto p : MBB.predecessors()) {
Keith Walker83ebef52016-09-27 16:46:07 +0000413 // Ignore unvisited predecessor blocks. As we are processing
414 // the blocks in reverse post-order any unvisited block can
415 // be considered to not remove any incoming values.
416 if (!Visited.count(p))
417 continue;
Vikram TV859ad292015-12-16 11:09:48 +0000418 auto OL = OutLocs.find(p);
419 // Join is null in case of empty OutLocs from any of the pred.
420 if (OL == OutLocs.end())
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000421 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000422
Keith Walker83ebef52016-09-27 16:46:07 +0000423 // Just copy over the Out locs to incoming locs for the first visited
424 // predecessor, and for all other predecessors join the Out locs.
425 if (!NumVisited)
Vikram TV859ad292015-12-16 11:09:48 +0000426 InLocsT = OL->second;
Keith Walker83ebef52016-09-27 16:46:07 +0000427 else
428 InLocsT &= OL->second;
429 NumVisited++;
Vikram TV859ad292015-12-16 11:09:48 +0000430 }
431
Adrian Prantl7f5866c2016-09-28 17:51:14 +0000432 // Filter out DBG_VALUES that are out of scope.
433 VarLocSet KillSet;
434 for (auto ID : InLocsT)
435 if (!VarLocIDs[ID].dominates(MBB))
436 KillSet.set(ID);
437 InLocsT.intersectWithComplement(KillSet);
438
Keith Walker83ebef52016-09-27 16:46:07 +0000439 // As we are processing blocks in reverse post-order we
440 // should have processed at least one predecessor, unless it
441 // is the entry block which has no predecessor.
442 assert((NumVisited || MBB.pred_empty()) &&
443 "Should have processed at least one predecessor");
Vikram TV859ad292015-12-16 11:09:48 +0000444 if (InLocsT.empty())
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000445 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000446
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000447 VarLocSet &ILS = InLocs[&MBB];
Vikram TV859ad292015-12-16 11:09:48 +0000448
449 // Insert DBG_VALUE instructions, if not already inserted.
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000450 VarLocSet Diff = InLocsT;
451 Diff.intersectWithComplement(ILS);
452 for (auto ID : Diff) {
453 // This VarLoc is not found in InLocs i.e. it is not yet inserted. So, a
454 // new range is started for the var from the mbb's beginning by inserting
455 // a new DBG_VALUE. transfer() will end this range however appropriate.
456 const VarLoc &DiffIt = VarLocIDs[ID];
457 const MachineInstr *DMI = &DiffIt.MI;
458 MachineInstr *MI =
459 BuildMI(MBB, MBB.instr_begin(), DMI->getDebugLoc(), DMI->getDesc(),
460 DMI->isIndirectDebugValue(), DMI->getOperand(0).getReg(), 0,
461 DMI->getDebugVariable(), DMI->getDebugExpression());
462 if (DMI->isIndirectDebugValue())
463 MI->getOperand(1).setImm(DMI->getOperand(1).getImm());
464 DEBUG(dbgs() << "Inserted: "; MI->dump(););
465 ILS.set(ID);
466 ++NumInserted;
467 Changed = true;
Vikram TV859ad292015-12-16 11:09:48 +0000468 }
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000469 return Changed;
Vikram TV859ad292015-12-16 11:09:48 +0000470}
471
472/// Calculate the liveness information for the given machine function and
473/// extend ranges across basic blocks.
474bool LiveDebugValues::ExtendRanges(MachineFunction &MF) {
475
476 DEBUG(dbgs() << "\nDebug Range Extension\n");
477
478 bool Changed = false;
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000479 bool OLChanged = false;
480 bool MBBJoined = false;
Vikram TV859ad292015-12-16 11:09:48 +0000481
Nico Weberee0b0ec2017-02-10 21:57:30 +0000482 VarLocMap VarLocIDs; // Map VarLoc<>unique ID for use in bitvectors.
Adrian Prantl7509d542016-05-26 21:42:47 +0000483 OpenRangesSet OpenRanges; // Ranges that are open until end of bb.
Nico Weberee0b0ec2017-02-10 21:57:30 +0000484 VarLocInMBB OutLocs; // Ranges that exist beyond bb.
485 VarLocInMBB InLocs; // Ranges that are incoming after joining.
Vikram TV859ad292015-12-16 11:09:48 +0000486
Daniel Berlin72560592016-01-10 18:08:32 +0000487 DenseMap<unsigned int, MachineBasicBlock *> OrderToBB;
488 DenseMap<MachineBasicBlock *, unsigned int> BBToOrder;
489 std::priority_queue<unsigned int, std::vector<unsigned int>,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000490 std::greater<unsigned int>>
491 Worklist;
Daniel Berlin72560592016-01-10 18:08:32 +0000492 std::priority_queue<unsigned int, std::vector<unsigned int>,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000493 std::greater<unsigned int>>
494 Pending;
495
Vikram TV859ad292015-12-16 11:09:48 +0000496 // Initialize every mbb with OutLocs.
497 for (auto &MBB : MF)
498 for (auto &MI : MBB)
Nico Weberee0b0ec2017-02-10 21:57:30 +0000499 transfer(MI, OpenRanges, OutLocs, VarLocIDs);
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000500
501 DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, "OutLocs after initialization",
502 dbgs()));
Vikram TV859ad292015-12-16 11:09:48 +0000503
Daniel Berlin72560592016-01-10 18:08:32 +0000504 ReversePostOrderTraversal<MachineFunction *> RPOT(&MF);
505 unsigned int RPONumber = 0;
506 for (auto RI = RPOT.begin(), RE = RPOT.end(); RI != RE; ++RI) {
507 OrderToBB[RPONumber] = *RI;
508 BBToOrder[*RI] = RPONumber;
509 Worklist.push(RPONumber);
510 ++RPONumber;
511 }
Daniel Berlin72560592016-01-10 18:08:32 +0000512 // This is a standard "union of predecessor outs" dataflow problem.
513 // To solve it, we perform join() and transfer() using the two worklist method
514 // until the ranges converge.
515 // Ranges have converged when both worklists are empty.
Keith Walker83ebef52016-09-27 16:46:07 +0000516 SmallPtrSet<const MachineBasicBlock *, 16> Visited;
Daniel Berlin72560592016-01-10 18:08:32 +0000517 while (!Worklist.empty() || !Pending.empty()) {
518 // We track what is on the pending worklist to avoid inserting the same
519 // thing twice. We could avoid this with a custom priority queue, but this
520 // is probably not worth it.
521 SmallPtrSet<MachineBasicBlock *, 16> OnPending;
Keith Walkerf83a19f2016-09-20 16:04:31 +0000522 DEBUG(dbgs() << "Processing Worklist\n");
Daniel Berlin72560592016-01-10 18:08:32 +0000523 while (!Worklist.empty()) {
524 MachineBasicBlock *MBB = OrderToBB[Worklist.top()];
525 Worklist.pop();
Keith Walker83ebef52016-09-27 16:46:07 +0000526 MBBJoined = join(*MBB, OutLocs, InLocs, VarLocIDs, Visited);
527 Visited.insert(MBB);
Daniel Berlin72560592016-01-10 18:08:32 +0000528 if (MBBJoined) {
529 MBBJoined = false;
530 Changed = true;
531 for (auto &MI : *MBB)
Nico Weberee0b0ec2017-02-10 21:57:30 +0000532 OLChanged |= transfer(MI, OpenRanges, OutLocs, VarLocIDs);
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000533
534 DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs,
535 "OutLocs after propagating", dbgs()));
536 DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs,
537 "InLocs after propagating", dbgs()));
Vikram TV859ad292015-12-16 11:09:48 +0000538
Daniel Berlin72560592016-01-10 18:08:32 +0000539 if (OLChanged) {
540 OLChanged = false;
541 for (auto s : MBB->successors())
Benjamin Kramer4dea8f52016-06-17 18:59:41 +0000542 if (OnPending.insert(s).second) {
Daniel Berlin72560592016-01-10 18:08:32 +0000543 Pending.push(BBToOrder[s]);
544 }
545 }
Vikram TV859ad292015-12-16 11:09:48 +0000546 }
547 }
Daniel Berlin72560592016-01-10 18:08:32 +0000548 Worklist.swap(Pending);
549 // At this point, pending must be empty, since it was just the empty
550 // worklist
551 assert(Pending.empty() && "Pending should be empty");
Vikram TV859ad292015-12-16 11:09:48 +0000552 }
Daniel Berlin72560592016-01-10 18:08:32 +0000553
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000554 DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, "Final OutLocs", dbgs()));
555 DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs, "Final InLocs", dbgs()));
Vikram TV859ad292015-12-16 11:09:48 +0000556 return Changed;
557}
558
559bool LiveDebugValues::runOnMachineFunction(MachineFunction &MF) {
Adrian Prantl7f5866c2016-09-28 17:51:14 +0000560 if (!MF.getFunction()->getSubprogram())
561 // LiveDebugValues will already have removed all DBG_VALUEs.
562 return false;
563
Vikram TV859ad292015-12-16 11:09:48 +0000564 TRI = MF.getSubtarget().getRegisterInfo();
565 TII = MF.getSubtarget().getInstrInfo();
Adrian Prantl7f5866c2016-09-28 17:51:14 +0000566 LS.initialize(MF);
Vikram TV859ad292015-12-16 11:09:48 +0000567
Adrian Prantl7f5866c2016-09-28 17:51:14 +0000568 bool Changed = ExtendRanges(MF);
Vikram TV859ad292015-12-16 11:09:48 +0000569 return Changed;
570}