blob: 4ff88d528108cf33952bd4ba7a4d9b32ce13a710 [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"
Vikram TV859ad292015-12-16 11:09:48 +000026#include "llvm/CodeGen/MachineFunction.h"
27#include "llvm/CodeGen/MachineFunctionPass.h"
28#include "llvm/CodeGen/MachineInstrBuilder.h"
29#include "llvm/CodeGen/Passes.h"
Reid Kleckner28865802016-04-14 18:29:59 +000030#include "llvm/IR/DebugInfo.h"
Vikram TV859ad292015-12-16 11:09:48 +000031#include "llvm/Support/Debug.h"
32#include "llvm/Support/raw_ostream.h"
33#include "llvm/Target/TargetInstrInfo.h"
Reid Klecknerf6f04f82016-03-25 17:54:46 +000034#include "llvm/Target/TargetLowering.h"
Vikram TV859ad292015-12-16 11:09:48 +000035#include "llvm/Target/TargetRegisterInfo.h"
36#include "llvm/Target/TargetSubtargetInfo.h"
Vikram TV859ad292015-12-16 11:09:48 +000037#include <list>
Mehdi Aminib550cb12016-04-18 09:17:29 +000038#include <queue>
Vikram TV859ad292015-12-16 11:09:48 +000039
40using namespace llvm;
41
42#define DEBUG_TYPE "live-debug-values"
43
44STATISTIC(NumInserted, "Number of DBG_VALUE instructions inserted");
45
46namespace {
47
Adrian Prantl6ee02c72016-05-25 22:21:12 +000048// \brief If @MI is a DBG_VALUE with debug value described by a defined
49// register, returns the number of this register. In the other case, returns 0.
Adrian Prantl00698732016-05-25 22:37:29 +000050static unsigned isDbgValueDescribedByReg(const MachineInstr &MI) {
Adrian Prantl6ee02c72016-05-25 22:21:12 +000051 assert(MI.isDebugValue() && "expected a DBG_VALUE");
52 assert(MI.getNumOperands() == 4 && "malformed DBG_VALUE");
53 // If location of variable is described using a register (directly
54 // or indirectly), this register is always a first operand.
55 return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0;
56}
57
Vikram TV859ad292015-12-16 11:09:48 +000058class LiveDebugValues : public MachineFunctionPass {
59
60private:
61 const TargetRegisterInfo *TRI;
62 const TargetInstrInfo *TII;
63
Adrian Prantl7509d542016-05-26 21:42:47 +000064 /// Based on std::pair so it can be used as an index into a DenseMap.
Vikram TV859ad292015-12-16 11:09:48 +000065 typedef std::pair<const DILocalVariable *, const DILocation *>
Adrian Prantl7509d542016-05-26 21:42:47 +000066 DebugVariableBase;
Vikram TV859ad292015-12-16 11:09:48 +000067 /// A potentially inlined instance of a variable.
Adrian Prantl7509d542016-05-26 21:42:47 +000068 struct DebugVariable : public DebugVariableBase {
69 DebugVariable(const DILocalVariable *Var, const DILocation *InlinedAt)
70 : DebugVariableBase(Var, InlinedAt) {}
Vikram TV859ad292015-12-16 11:09:48 +000071
Adrian Prantl7509d542016-05-26 21:42:47 +000072 const DILocalVariable *getVar() const { return this->first; };
73 const DILocation *getInlinedAt() const { return this->second; };
Vikram TV859ad292015-12-16 11:09:48 +000074
Adrian Prantl6ee02c72016-05-25 22:21:12 +000075 bool operator<(const DebugVariable &DV) const {
Adrian Prantl7509d542016-05-26 21:42:47 +000076 if (getVar() == DV.getVar())
77 return getInlinedAt() < DV.getInlinedAt();
78 return getVar() < DV.getVar();
Vikram TV859ad292015-12-16 11:09:48 +000079 }
80 };
81
Adrian Prantl6ee02c72016-05-25 22:21:12 +000082 /// A pair of debug variable and value location.
Vikram TV859ad292015-12-16 11:09:48 +000083 struct VarLoc {
Adrian Prantl6ee02c72016-05-25 22:21:12 +000084 const DebugVariable Var;
85 const MachineInstr &MI; ///< Only used for cloning a new DBG_VALUE.
Vikram TV859ad292015-12-16 11:09:48 +000086
Adrian Prantl6ee02c72016-05-25 22:21:12 +000087 enum { InvalidKind = 0, RegisterKind } Kind;
Vikram TV859ad292015-12-16 11:09:48 +000088
Adrian Prantl6ee02c72016-05-25 22:21:12 +000089 /// The value location. Stored separately to avoid repeatedly
90 /// extracting it from MI.
91 union {
92 struct {
93 uint32_t RegNo;
94 uint32_t Offset;
95 } RegisterLoc;
96 uint64_t Hash;
97 } Loc;
98
99 VarLoc(const MachineInstr &MI)
100 : Var(MI.getDebugVariable(), MI.getDebugLoc()->getInlinedAt()), MI(MI),
101 Kind(InvalidKind) {
102 static_assert((sizeof(Loc) == sizeof(uint64_t)),
103 "hash does not cover all members of Loc");
104 assert(MI.isDebugValue() && "not a DBG_VALUE");
105 assert(MI.getNumOperands() == 4 && "malformed DBG_VALUE");
Adrian Prantl00698732016-05-25 22:37:29 +0000106 if (int RegNo = isDbgValueDescribedByReg(MI)) {
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000107 Kind = RegisterKind;
108 Loc.RegisterLoc.RegNo = RegNo;
109 uint64_t Offset =
110 MI.isIndirectDebugValue() ? MI.getOperand(1).getImm() : 0;
111 // We don't support offsets larger than 4GiB here. They are
112 // slated to be replaced with DIExpressions anyway.
113 if (Offset >= (1ULL << 32))
114 Kind = InvalidKind;
115 else
116 Loc.RegisterLoc.Offset = Offset;
117 }
118 }
119
120 /// If this variable is described by a register, return it,
121 /// otherwise return 0.
122 unsigned isDescribedByReg() const {
123 if (Kind == RegisterKind)
124 return Loc.RegisterLoc.RegNo;
125 return 0;
126 }
127
128 void dump() const { MI.dump(); }
129
130 bool operator==(const VarLoc &Other) const {
131 return Var == Other.Var && Loc.Hash == Other.Loc.Hash;
132 }
133
Adrian Prantl7509d542016-05-26 21:42:47 +0000134 /// This operator guarantees that VarLocs are sorted by Variable first.
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000135 bool operator<(const VarLoc &Other) const {
136 if (Var == Other.Var)
137 return Loc.Hash < Other.Loc.Hash;
138 return Var < Other.Var;
139 }
Vikram TV859ad292015-12-16 11:09:48 +0000140 };
141
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000142 typedef UniqueVector<VarLoc> VarLocMap;
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000143 typedef SparseBitVector<> VarLocSet;
144 typedef SmallDenseMap<const MachineBasicBlock *, VarLocSet> VarLocInMBB;
Vikram TV859ad292015-12-16 11:09:48 +0000145
Adrian Prantl7509d542016-05-26 21:42:47 +0000146 /// This holds the working set of currently open ranges. For fast
147 /// access, this is done both as a set of VarLocIDs, and a map of
148 /// DebugVariable to recent VarLocID. Note that a DBG_VALUE ends all
149 /// previous open ranges for the same variable.
150 class OpenRangesSet {
151 VarLocSet VarLocs;
152 SmallDenseMap<DebugVariableBase, unsigned, 8> Vars;
153
154 public:
155 const VarLocSet &getVarLocs() const { return VarLocs; }
156
157 /// Terminate all open ranges for Var by removing it from the set.
158 void erase(DebugVariable Var) {
159 auto It = Vars.find(Var);
160 if (It != Vars.end()) {
161 unsigned ID = It->second;
162 VarLocs.reset(ID);
163 Vars.erase(It);
164 }
165 }
166
167 /// Terminate all open ranges listed in \c KillSet by removing
168 /// them from the set.
169 void erase(const VarLocSet &KillSet, const VarLocMap &VarLocIDs) {
170 VarLocs.intersectWithComplement(KillSet);
171 for (unsigned ID : KillSet)
172 Vars.erase(VarLocIDs[ID].Var);
173 }
174
175 /// Insert a new range into the set.
176 void insert(unsigned VarLocID, DebugVariableBase Var) {
177 VarLocs.set(VarLocID);
178 Vars.insert({Var, VarLocID});
179 }
180
181 /// Empty the set.
182 void clear() {
183 VarLocs.clear();
184 Vars.clear();
185 }
186
187 /// Return whether the set is empty or not.
188 bool empty() const {
189 assert(Vars.empty() == VarLocs.empty() && "open ranges are inconsistent");
190 return VarLocs.empty();
191 }
192 };
193
194 void transferDebugValue(const MachineInstr &MI, OpenRangesSet &OpenRanges,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000195 VarLocMap &VarLocIDs);
Adrian Prantl7509d542016-05-26 21:42:47 +0000196 void transferRegisterDef(MachineInstr &MI, OpenRangesSet &OpenRanges,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000197 const VarLocMap &VarLocIDs);
Adrian Prantl7509d542016-05-26 21:42:47 +0000198 bool transferTerminatorInst(MachineInstr &MI, OpenRangesSet &OpenRanges,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000199 VarLocInMBB &OutLocs, const VarLocMap &VarLocIDs);
Adrian Prantl7509d542016-05-26 21:42:47 +0000200 bool transfer(MachineInstr &MI, OpenRangesSet &OpenRanges,
201 VarLocInMBB &OutLocs, VarLocMap &VarLocIDs);
Vikram TV859ad292015-12-16 11:09:48 +0000202
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000203 bool join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs, VarLocInMBB &InLocs,
204 const VarLocMap &VarLocIDs);
Vikram TV859ad292015-12-16 11:09:48 +0000205
206 bool ExtendRanges(MachineFunction &MF);
207
208public:
209 static char ID;
210
211 /// Default construct and initialize the pass.
212 LiveDebugValues();
213
214 /// Tell the pass manager which passes we depend on and what
215 /// information we preserve.
216 void getAnalysisUsage(AnalysisUsage &AU) const override;
217
Derek Schuffad154c82016-03-28 17:05:30 +0000218 MachineFunctionProperties getRequiredProperties() const override {
219 return MachineFunctionProperties().set(
220 MachineFunctionProperties::Property::AllVRegsAllocated);
221 }
222
Vikram TV859ad292015-12-16 11:09:48 +0000223 /// Print to ostream with a message.
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000224 void printVarLocInMBB(const MachineFunction &MF, const VarLocInMBB &V,
225 const VarLocMap &VarLocIDs, const char *msg,
Vikram TV859ad292015-12-16 11:09:48 +0000226 raw_ostream &Out) const;
227
228 /// Calculate the liveness information for the given machine function.
229 bool runOnMachineFunction(MachineFunction &MF) override;
230};
231} // namespace
232
233//===----------------------------------------------------------------------===//
234// Implementation
235//===----------------------------------------------------------------------===//
236
237char LiveDebugValues::ID = 0;
238char &llvm::LiveDebugValuesID = LiveDebugValues::ID;
239INITIALIZE_PASS(LiveDebugValues, "livedebugvalues", "Live DEBUG_VALUE analysis",
240 false, false)
241
242/// Default construct and initialize the pass.
243LiveDebugValues::LiveDebugValues() : MachineFunctionPass(ID) {
244 initializeLiveDebugValuesPass(*PassRegistry::getPassRegistry());
245}
246
247/// Tell the pass manager which passes we depend on and what information we
248/// preserve.
249void LiveDebugValues::getAnalysisUsage(AnalysisUsage &AU) const {
Matt Arsenaultb1630a12016-06-08 05:18:01 +0000250 AU.setPreservesCFG();
Vikram TV859ad292015-12-16 11:09:48 +0000251 MachineFunctionPass::getAnalysisUsage(AU);
252}
253
Vikram TV859ad292015-12-16 11:09:48 +0000254//===----------------------------------------------------------------------===//
255// Debug Range Extension Implementation
256//===----------------------------------------------------------------------===//
257
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000258void LiveDebugValues::printVarLocInMBB(const MachineFunction &MF,
259 const VarLocInMBB &V,
260 const VarLocMap &VarLocIDs,
261 const char *msg,
Vikram TV859ad292015-12-16 11:09:48 +0000262 raw_ostream &Out) const {
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000263 for (const MachineBasicBlock &BB : MF) {
264 const auto &L = V.lookup(&BB);
265 Out << "MBB: " << BB.getName() << ":\n";
266 for (unsigned VLL : L) {
267 const VarLoc &VL = VarLocIDs[VLL];
Adrian Prantl7509d542016-05-26 21:42:47 +0000268 Out << " Var: " << VL.Var.getVar()->getName();
Vikram TV859ad292015-12-16 11:09:48 +0000269 Out << " MI: ";
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000270 VL.dump();
Vikram TV859ad292015-12-16 11:09:48 +0000271 Out << "\n";
272 }
273 }
274 Out << "\n";
275}
276
Vikram TV859ad292015-12-16 11:09:48 +0000277/// End all previous ranges related to @MI and start a new range from @MI
278/// if it is a DBG_VALUE instr.
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000279void LiveDebugValues::transferDebugValue(const MachineInstr &MI,
Adrian Prantl7509d542016-05-26 21:42:47 +0000280 OpenRangesSet &OpenRanges,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000281 VarLocMap &VarLocIDs) {
Vikram TV859ad292015-12-16 11:09:48 +0000282 if (!MI.isDebugValue())
283 return;
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000284 const DILocalVariable *Var = MI.getDebugVariable();
285 const DILocation *DebugLoc = MI.getDebugLoc();
286 const DILocation *InlinedAt = DebugLoc->getInlinedAt();
287 assert(Var->isValidLocationForIntrinsic(DebugLoc) &&
Vikram TV859ad292015-12-16 11:09:48 +0000288 "Expected inlined-at fields to agree");
Vikram TV859ad292015-12-16 11:09:48 +0000289
290 // End all previous ranges of Var.
Adrian Prantl7509d542016-05-26 21:42:47 +0000291 DebugVariable V(Var, InlinedAt);
292 OpenRanges.erase(V);
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000293
294 // Add the VarLoc to OpenRanges from this DBG_VALUE.
295 // TODO: Currently handles DBG_VALUE which has only reg as location.
Adrian Prantl7509d542016-05-26 21:42:47 +0000296 if (isDbgValueDescribedByReg(MI)) {
297 VarLoc VL(MI);
298 unsigned ID = VarLocIDs.insert(VL);
299 OpenRanges.insert(ID, VL.Var);
300 }
Vikram TV859ad292015-12-16 11:09:48 +0000301}
302
303/// A definition of a register may mark the end of a range.
304void LiveDebugValues::transferRegisterDef(MachineInstr &MI,
Adrian Prantl7509d542016-05-26 21:42:47 +0000305 OpenRangesSet &OpenRanges,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000306 const VarLocMap &VarLocIDs) {
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000307 MachineFunction *MF = MI.getParent()->getParent();
308 const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
309 unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000310 SparseBitVector<> KillSet;
Vikram TV859ad292015-12-16 11:09:48 +0000311 for (const MachineOperand &MO : MI.operands()) {
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000312 if (MO.isReg() && MO.isDef() && MO.getReg() &&
313 TRI->isPhysicalRegister(MO.getReg())) {
314 // Remove ranges of all aliased registers.
315 for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI)
Adrian Prantl7509d542016-05-26 21:42:47 +0000316 for (unsigned ID : OpenRanges.getVarLocs())
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000317 if (VarLocIDs[ID].isDescribedByReg() == *RAI)
318 KillSet.set(ID);
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000319 } else if (MO.isRegMask()) {
320 // Remove ranges of all clobbered registers. Register masks don't usually
321 // list SP as preserved. While the debug info may be off for an
322 // instruction or two around callee-cleanup calls, transferring the
323 // DEBUG_VALUE across the call is still a better user experience.
Adrian Prantl7509d542016-05-26 21:42:47 +0000324 for (unsigned ID : OpenRanges.getVarLocs()) {
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000325 unsigned Reg = VarLocIDs[ID].isDescribedByReg();
326 if (Reg && Reg != SP && MO.clobbersPhysReg(Reg))
327 KillSet.set(ID);
328 }
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000329 }
Vikram TV859ad292015-12-16 11:09:48 +0000330 }
Adrian Prantl7509d542016-05-26 21:42:47 +0000331 OpenRanges.erase(KillSet, VarLocIDs);
Vikram TV859ad292015-12-16 11:09:48 +0000332}
333
334/// Terminate all open ranges at the end of the current basic block.
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000335bool LiveDebugValues::transferTerminatorInst(MachineInstr &MI,
Adrian Prantl7509d542016-05-26 21:42:47 +0000336 OpenRangesSet &OpenRanges,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000337 VarLocInMBB &OutLocs,
338 const VarLocMap &VarLocIDs) {
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000339 bool Changed = false;
Vikram TV859ad292015-12-16 11:09:48 +0000340 const MachineBasicBlock *CurMBB = MI.getParent();
341 if (!(MI.isTerminator() || (&MI == &CurMBB->instr_back())))
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000342 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000343
344 if (OpenRanges.empty())
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000345 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000346
Adrian Prantl7509d542016-05-26 21:42:47 +0000347 DEBUG(for (unsigned ID : OpenRanges.getVarLocs()) {
348 // Copy OpenRanges to OutLocs, if not already present.
349 dbgs() << "Add to OutLocs: "; VarLocIDs[ID].dump();
350 });
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000351 VarLocSet &VLS = OutLocs[CurMBB];
Adrian Prantl7509d542016-05-26 21:42:47 +0000352 Changed = VLS |= OpenRanges.getVarLocs();
Vikram TV859ad292015-12-16 11:09:48 +0000353 OpenRanges.clear();
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000354 return Changed;
Vikram TV859ad292015-12-16 11:09:48 +0000355}
356
357/// This routine creates OpenRanges and OutLocs.
Adrian Prantl7509d542016-05-26 21:42:47 +0000358bool LiveDebugValues::transfer(MachineInstr &MI, OpenRangesSet &OpenRanges,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000359 VarLocInMBB &OutLocs, VarLocMap &VarLocIDs) {
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000360 bool Changed = false;
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000361 transferDebugValue(MI, OpenRanges, VarLocIDs);
362 transferRegisterDef(MI, OpenRanges, VarLocIDs);
363 Changed = transferTerminatorInst(MI, OpenRanges, OutLocs, VarLocIDs);
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000364 return Changed;
Vikram TV859ad292015-12-16 11:09:48 +0000365}
366
367/// This routine joins the analysis results of all incoming edges in @MBB by
368/// inserting a new DBG_VALUE instruction at the start of the @MBB - if the same
369/// source variable in all the predecessors of @MBB reside in the same location.
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000370bool LiveDebugValues::join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000371 VarLocInMBB &InLocs, const VarLocMap &VarLocIDs) {
Vikram TV859ad292015-12-16 11:09:48 +0000372 DEBUG(dbgs() << "join MBB: " << MBB.getName() << "\n");
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000373 bool Changed = false;
Vikram TV859ad292015-12-16 11:09:48 +0000374
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000375 VarLocSet InLocsT; // Temporary incoming locations.
Vikram TV859ad292015-12-16 11:09:48 +0000376
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000377 // For all predecessors of this MBB, find the set of VarLocs that
378 // can be joined.
Vikram TV859ad292015-12-16 11:09:48 +0000379 for (auto p : MBB.predecessors()) {
380 auto OL = OutLocs.find(p);
381 // Join is null in case of empty OutLocs from any of the pred.
382 if (OL == OutLocs.end())
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000383 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000384
385 // Just copy over the Out locs to incoming locs for the first predecessor.
386 if (p == *MBB.pred_begin()) {
387 InLocsT = OL->second;
388 continue;
389 }
Vikram TV859ad292015-12-16 11:09:48 +0000390 // Join with this predecessor.
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000391 InLocsT &= OL->second;
Vikram TV859ad292015-12-16 11:09:48 +0000392 }
393
394 if (InLocsT.empty())
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000395 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000396
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000397 VarLocSet &ILS = InLocs[&MBB];
Vikram TV859ad292015-12-16 11:09:48 +0000398
399 // Insert DBG_VALUE instructions, if not already inserted.
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000400 VarLocSet Diff = InLocsT;
401 Diff.intersectWithComplement(ILS);
402 for (auto ID : Diff) {
403 // This VarLoc is not found in InLocs i.e. it is not yet inserted. So, a
404 // new range is started for the var from the mbb's beginning by inserting
405 // a new DBG_VALUE. transfer() will end this range however appropriate.
406 const VarLoc &DiffIt = VarLocIDs[ID];
407 const MachineInstr *DMI = &DiffIt.MI;
408 MachineInstr *MI =
409 BuildMI(MBB, MBB.instr_begin(), DMI->getDebugLoc(), DMI->getDesc(),
410 DMI->isIndirectDebugValue(), DMI->getOperand(0).getReg(), 0,
411 DMI->getDebugVariable(), DMI->getDebugExpression());
412 if (DMI->isIndirectDebugValue())
413 MI->getOperand(1).setImm(DMI->getOperand(1).getImm());
414 DEBUG(dbgs() << "Inserted: "; MI->dump(););
415 ILS.set(ID);
416 ++NumInserted;
417 Changed = true;
Vikram TV859ad292015-12-16 11:09:48 +0000418 }
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000419 return Changed;
Vikram TV859ad292015-12-16 11:09:48 +0000420}
421
422/// Calculate the liveness information for the given machine function and
423/// extend ranges across basic blocks.
424bool LiveDebugValues::ExtendRanges(MachineFunction &MF) {
425
426 DEBUG(dbgs() << "\nDebug Range Extension\n");
427
428 bool Changed = false;
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000429 bool OLChanged = false;
430 bool MBBJoined = false;
Vikram TV859ad292015-12-16 11:09:48 +0000431
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000432 VarLocMap VarLocIDs; // Map VarLoc<>unique ID for use in bitvectors.
Adrian Prantl7509d542016-05-26 21:42:47 +0000433 OpenRangesSet OpenRanges; // Ranges that are open until end of bb.
Vikram TV859ad292015-12-16 11:09:48 +0000434 VarLocInMBB OutLocs; // Ranges that exist beyond bb.
435 VarLocInMBB InLocs; // Ranges that are incoming after joining.
436
Daniel Berlin72560592016-01-10 18:08:32 +0000437 DenseMap<unsigned int, MachineBasicBlock *> OrderToBB;
438 DenseMap<MachineBasicBlock *, unsigned int> BBToOrder;
439 std::priority_queue<unsigned int, std::vector<unsigned int>,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000440 std::greater<unsigned int>>
441 Worklist;
Daniel Berlin72560592016-01-10 18:08:32 +0000442 std::priority_queue<unsigned int, std::vector<unsigned int>,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000443 std::greater<unsigned int>>
444 Pending;
445
Vikram TV859ad292015-12-16 11:09:48 +0000446 // Initialize every mbb with OutLocs.
447 for (auto &MBB : MF)
448 for (auto &MI : MBB)
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000449 transfer(MI, OpenRanges, OutLocs, VarLocIDs);
450
451 DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, "OutLocs after initialization",
452 dbgs()));
Vikram TV859ad292015-12-16 11:09:48 +0000453
Daniel Berlin72560592016-01-10 18:08:32 +0000454 ReversePostOrderTraversal<MachineFunction *> RPOT(&MF);
455 unsigned int RPONumber = 0;
456 for (auto RI = RPOT.begin(), RE = RPOT.end(); RI != RE; ++RI) {
457 OrderToBB[RPONumber] = *RI;
458 BBToOrder[*RI] = RPONumber;
459 Worklist.push(RPONumber);
460 ++RPONumber;
461 }
Daniel Berlin72560592016-01-10 18:08:32 +0000462 // This is a standard "union of predecessor outs" dataflow problem.
463 // To solve it, we perform join() and transfer() using the two worklist method
464 // until the ranges converge.
465 // Ranges have converged when both worklists are empty.
466 while (!Worklist.empty() || !Pending.empty()) {
467 // We track what is on the pending worklist to avoid inserting the same
468 // thing twice. We could avoid this with a custom priority queue, but this
469 // is probably not worth it.
470 SmallPtrSet<MachineBasicBlock *, 16> OnPending;
471 while (!Worklist.empty()) {
472 MachineBasicBlock *MBB = OrderToBB[Worklist.top()];
473 Worklist.pop();
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000474 MBBJoined = join(*MBB, OutLocs, InLocs, VarLocIDs);
Vikram TV859ad292015-12-16 11:09:48 +0000475
Daniel Berlin72560592016-01-10 18:08:32 +0000476 if (MBBJoined) {
477 MBBJoined = false;
478 Changed = true;
479 for (auto &MI : *MBB)
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000480 OLChanged |= transfer(MI, OpenRanges, OutLocs, VarLocIDs);
481
482 DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs,
483 "OutLocs after propagating", dbgs()));
484 DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs,
485 "InLocs after propagating", dbgs()));
Vikram TV859ad292015-12-16 11:09:48 +0000486
Daniel Berlin72560592016-01-10 18:08:32 +0000487 if (OLChanged) {
488 OLChanged = false;
489 for (auto s : MBB->successors())
Benjamin Kramer4dea8f52016-06-17 18:59:41 +0000490 if (OnPending.insert(s).second) {
Daniel Berlin72560592016-01-10 18:08:32 +0000491 Pending.push(BBToOrder[s]);
492 }
493 }
Vikram TV859ad292015-12-16 11:09:48 +0000494 }
495 }
Daniel Berlin72560592016-01-10 18:08:32 +0000496 Worklist.swap(Pending);
497 // At this point, pending must be empty, since it was just the empty
498 // worklist
499 assert(Pending.empty() && "Pending should be empty");
Vikram TV859ad292015-12-16 11:09:48 +0000500 }
Daniel Berlin72560592016-01-10 18:08:32 +0000501
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000502 DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, "Final OutLocs", dbgs()));
503 DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs, "Final InLocs", dbgs()));
Vikram TV859ad292015-12-16 11:09:48 +0000504 return Changed;
505}
506
507bool LiveDebugValues::runOnMachineFunction(MachineFunction &MF) {
508 TRI = MF.getSubtarget().getRegisterInfo();
509 TII = MF.getSubtarget().getInstrInfo();
510
511 bool Changed = false;
512
513 Changed |= ExtendRanges(MF);
514
515 return Changed;
516}