blob: 7d9461214ef50dce43b3c043de144dc4ac4adb59 [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,
Keith Walker83ebef52016-09-27 16:46:07 +0000204 const VarLocMap &VarLocIDs,
205 SmallPtrSet<const MachineBasicBlock *, 16> &Visited);
Vikram TV859ad292015-12-16 11:09:48 +0000206
207 bool ExtendRanges(MachineFunction &MF);
208
209public:
210 static char ID;
211
212 /// Default construct and initialize the pass.
213 LiveDebugValues();
214
215 /// Tell the pass manager which passes we depend on and what
216 /// information we preserve.
217 void getAnalysisUsage(AnalysisUsage &AU) const override;
218
Derek Schuffad154c82016-03-28 17:05:30 +0000219 MachineFunctionProperties getRequiredProperties() const override {
220 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000221 MachineFunctionProperties::Property::NoVRegs);
Derek Schuffad154c82016-03-28 17:05:30 +0000222 }
223
Vikram TV859ad292015-12-16 11:09:48 +0000224 /// Print to ostream with a message.
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000225 void printVarLocInMBB(const MachineFunction &MF, const VarLocInMBB &V,
226 const VarLocMap &VarLocIDs, const char *msg,
Vikram TV859ad292015-12-16 11:09:48 +0000227 raw_ostream &Out) const;
228
229 /// Calculate the liveness information for the given machine function.
230 bool runOnMachineFunction(MachineFunction &MF) override;
231};
232} // namespace
233
234//===----------------------------------------------------------------------===//
235// Implementation
236//===----------------------------------------------------------------------===//
237
238char LiveDebugValues::ID = 0;
239char &llvm::LiveDebugValuesID = LiveDebugValues::ID;
240INITIALIZE_PASS(LiveDebugValues, "livedebugvalues", "Live DEBUG_VALUE analysis",
241 false, false)
242
243/// Default construct and initialize the pass.
244LiveDebugValues::LiveDebugValues() : MachineFunctionPass(ID) {
245 initializeLiveDebugValuesPass(*PassRegistry::getPassRegistry());
246}
247
248/// Tell the pass manager which passes we depend on and what information we
249/// preserve.
250void LiveDebugValues::getAnalysisUsage(AnalysisUsage &AU) const {
Matt Arsenaultb1630a12016-06-08 05:18:01 +0000251 AU.setPreservesCFG();
Vikram TV859ad292015-12-16 11:09:48 +0000252 MachineFunctionPass::getAnalysisUsage(AU);
253}
254
Vikram TV859ad292015-12-16 11:09:48 +0000255//===----------------------------------------------------------------------===//
256// Debug Range Extension Implementation
257//===----------------------------------------------------------------------===//
258
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000259void LiveDebugValues::printVarLocInMBB(const MachineFunction &MF,
260 const VarLocInMBB &V,
261 const VarLocMap &VarLocIDs,
262 const char *msg,
Vikram TV859ad292015-12-16 11:09:48 +0000263 raw_ostream &Out) const {
Keith Walkerf83a19f2016-09-20 16:04:31 +0000264 Out << '\n' << msg << '\n';
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000265 for (const MachineBasicBlock &BB : MF) {
266 const auto &L = V.lookup(&BB);
267 Out << "MBB: " << BB.getName() << ":\n";
268 for (unsigned VLL : L) {
269 const VarLoc &VL = VarLocIDs[VLL];
Adrian Prantl7509d542016-05-26 21:42:47 +0000270 Out << " Var: " << VL.Var.getVar()->getName();
Vikram TV859ad292015-12-16 11:09:48 +0000271 Out << " MI: ";
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000272 VL.dump();
Vikram TV859ad292015-12-16 11:09:48 +0000273 }
274 }
275 Out << "\n";
276}
277
Vikram TV859ad292015-12-16 11:09:48 +0000278/// End all previous ranges related to @MI and start a new range from @MI
279/// if it is a DBG_VALUE instr.
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000280void LiveDebugValues::transferDebugValue(const MachineInstr &MI,
Adrian Prantl7509d542016-05-26 21:42:47 +0000281 OpenRangesSet &OpenRanges,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000282 VarLocMap &VarLocIDs) {
Vikram TV859ad292015-12-16 11:09:48 +0000283 if (!MI.isDebugValue())
284 return;
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000285 const DILocalVariable *Var = MI.getDebugVariable();
286 const DILocation *DebugLoc = MI.getDebugLoc();
287 const DILocation *InlinedAt = DebugLoc->getInlinedAt();
288 assert(Var->isValidLocationForIntrinsic(DebugLoc) &&
Vikram TV859ad292015-12-16 11:09:48 +0000289 "Expected inlined-at fields to agree");
Vikram TV859ad292015-12-16 11:09:48 +0000290
291 // End all previous ranges of Var.
Adrian Prantl7509d542016-05-26 21:42:47 +0000292 DebugVariable V(Var, InlinedAt);
293 OpenRanges.erase(V);
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000294
295 // Add the VarLoc to OpenRanges from this DBG_VALUE.
296 // TODO: Currently handles DBG_VALUE which has only reg as location.
Adrian Prantl7509d542016-05-26 21:42:47 +0000297 if (isDbgValueDescribedByReg(MI)) {
298 VarLoc VL(MI);
299 unsigned ID = VarLocIDs.insert(VL);
300 OpenRanges.insert(ID, VL.Var);
301 }
Vikram TV859ad292015-12-16 11:09:48 +0000302}
303
304/// A definition of a register may mark the end of a range.
305void LiveDebugValues::transferRegisterDef(MachineInstr &MI,
Adrian Prantl7509d542016-05-26 21:42:47 +0000306 OpenRangesSet &OpenRanges,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000307 const VarLocMap &VarLocIDs) {
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000308 MachineFunction *MF = MI.getParent()->getParent();
309 const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
310 unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000311 SparseBitVector<> KillSet;
Vikram TV859ad292015-12-16 11:09:48 +0000312 for (const MachineOperand &MO : MI.operands()) {
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000313 if (MO.isReg() && MO.isDef() && MO.getReg() &&
314 TRI->isPhysicalRegister(MO.getReg())) {
315 // Remove ranges of all aliased registers.
316 for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI)
Adrian Prantl7509d542016-05-26 21:42:47 +0000317 for (unsigned ID : OpenRanges.getVarLocs())
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000318 if (VarLocIDs[ID].isDescribedByReg() == *RAI)
319 KillSet.set(ID);
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000320 } else if (MO.isRegMask()) {
321 // Remove ranges of all clobbered registers. Register masks don't usually
322 // list SP as preserved. While the debug info may be off for an
323 // instruction or two around callee-cleanup calls, transferring the
324 // DEBUG_VALUE across the call is still a better user experience.
Adrian Prantl7509d542016-05-26 21:42:47 +0000325 for (unsigned ID : OpenRanges.getVarLocs()) {
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000326 unsigned Reg = VarLocIDs[ID].isDescribedByReg();
327 if (Reg && Reg != SP && MO.clobbersPhysReg(Reg))
328 KillSet.set(ID);
329 }
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000330 }
Vikram TV859ad292015-12-16 11:09:48 +0000331 }
Adrian Prantl7509d542016-05-26 21:42:47 +0000332 OpenRanges.erase(KillSet, VarLocIDs);
Vikram TV859ad292015-12-16 11:09:48 +0000333}
334
335/// Terminate all open ranges at the end of the current basic block.
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000336bool LiveDebugValues::transferTerminatorInst(MachineInstr &MI,
Adrian Prantl7509d542016-05-26 21:42:47 +0000337 OpenRangesSet &OpenRanges,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000338 VarLocInMBB &OutLocs,
339 const VarLocMap &VarLocIDs) {
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000340 bool Changed = false;
Vikram TV859ad292015-12-16 11:09:48 +0000341 const MachineBasicBlock *CurMBB = MI.getParent();
342 if (!(MI.isTerminator() || (&MI == &CurMBB->instr_back())))
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000343 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000344
345 if (OpenRanges.empty())
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000346 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000347
Adrian Prantl7509d542016-05-26 21:42:47 +0000348 DEBUG(for (unsigned ID : OpenRanges.getVarLocs()) {
349 // Copy OpenRanges to OutLocs, if not already present.
350 dbgs() << "Add to OutLocs: "; VarLocIDs[ID].dump();
351 });
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000352 VarLocSet &VLS = OutLocs[CurMBB];
Adrian Prantl7509d542016-05-26 21:42:47 +0000353 Changed = VLS |= OpenRanges.getVarLocs();
Vikram TV859ad292015-12-16 11:09:48 +0000354 OpenRanges.clear();
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000355 return Changed;
Vikram TV859ad292015-12-16 11:09:48 +0000356}
357
358/// This routine creates OpenRanges and OutLocs.
Adrian Prantl7509d542016-05-26 21:42:47 +0000359bool LiveDebugValues::transfer(MachineInstr &MI, OpenRangesSet &OpenRanges,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000360 VarLocInMBB &OutLocs, VarLocMap &VarLocIDs) {
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000361 bool Changed = false;
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000362 transferDebugValue(MI, OpenRanges, VarLocIDs);
363 transferRegisterDef(MI, OpenRanges, VarLocIDs);
364 Changed = transferTerminatorInst(MI, OpenRanges, OutLocs, VarLocIDs);
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000365 return Changed;
Vikram TV859ad292015-12-16 11:09:48 +0000366}
367
368/// This routine joins the analysis results of all incoming edges in @MBB by
369/// inserting a new DBG_VALUE instruction at the start of the @MBB - if the same
370/// source variable in all the predecessors of @MBB reside in the same location.
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000371bool LiveDebugValues::join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs,
Keith Walker83ebef52016-09-27 16:46:07 +0000372 VarLocInMBB &InLocs, const VarLocMap &VarLocIDs,
373 SmallPtrSet<const MachineBasicBlock *, 16> &Visited) {
Vikram TV859ad292015-12-16 11:09:48 +0000374 DEBUG(dbgs() << "join MBB: " << MBB.getName() << "\n");
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000375 bool Changed = false;
Vikram TV859ad292015-12-16 11:09:48 +0000376
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000377 VarLocSet InLocsT; // Temporary incoming locations.
Vikram TV859ad292015-12-16 11:09:48 +0000378
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000379 // For all predecessors of this MBB, find the set of VarLocs that
380 // can be joined.
Keith Walker83ebef52016-09-27 16:46:07 +0000381 int NumVisited = 0;
Vikram TV859ad292015-12-16 11:09:48 +0000382 for (auto p : MBB.predecessors()) {
Keith Walker83ebef52016-09-27 16:46:07 +0000383 // Ignore unvisited predecessor blocks. As we are processing
384 // the blocks in reverse post-order any unvisited block can
385 // be considered to not remove any incoming values.
386 if (!Visited.count(p))
387 continue;
Vikram TV859ad292015-12-16 11:09:48 +0000388 auto OL = OutLocs.find(p);
389 // Join is null in case of empty OutLocs from any of the pred.
390 if (OL == OutLocs.end())
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000391 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000392
Keith Walker83ebef52016-09-27 16:46:07 +0000393 // Just copy over the Out locs to incoming locs for the first visited
394 // predecessor, and for all other predecessors join the Out locs.
395 if (!NumVisited)
Vikram TV859ad292015-12-16 11:09:48 +0000396 InLocsT = OL->second;
Keith Walker83ebef52016-09-27 16:46:07 +0000397 else
398 InLocsT &= OL->second;
399 NumVisited++;
Vikram TV859ad292015-12-16 11:09:48 +0000400 }
401
Keith Walker83ebef52016-09-27 16:46:07 +0000402 // As we are processing blocks in reverse post-order we
403 // should have processed at least one predecessor, unless it
404 // is the entry block which has no predecessor.
405 assert((NumVisited || MBB.pred_empty()) &&
406 "Should have processed at least one predecessor");
Vikram TV859ad292015-12-16 11:09:48 +0000407 if (InLocsT.empty())
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000408 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000409
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000410 VarLocSet &ILS = InLocs[&MBB];
Vikram TV859ad292015-12-16 11:09:48 +0000411
412 // Insert DBG_VALUE instructions, if not already inserted.
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000413 VarLocSet Diff = InLocsT;
414 Diff.intersectWithComplement(ILS);
415 for (auto ID : Diff) {
416 // This VarLoc is not found in InLocs i.e. it is not yet inserted. So, a
417 // new range is started for the var from the mbb's beginning by inserting
418 // a new DBG_VALUE. transfer() will end this range however appropriate.
419 const VarLoc &DiffIt = VarLocIDs[ID];
420 const MachineInstr *DMI = &DiffIt.MI;
421 MachineInstr *MI =
422 BuildMI(MBB, MBB.instr_begin(), DMI->getDebugLoc(), DMI->getDesc(),
423 DMI->isIndirectDebugValue(), DMI->getOperand(0).getReg(), 0,
424 DMI->getDebugVariable(), DMI->getDebugExpression());
425 if (DMI->isIndirectDebugValue())
426 MI->getOperand(1).setImm(DMI->getOperand(1).getImm());
427 DEBUG(dbgs() << "Inserted: "; MI->dump(););
428 ILS.set(ID);
429 ++NumInserted;
430 Changed = true;
Vikram TV859ad292015-12-16 11:09:48 +0000431 }
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000432 return Changed;
Vikram TV859ad292015-12-16 11:09:48 +0000433}
434
435/// Calculate the liveness information for the given machine function and
436/// extend ranges across basic blocks.
437bool LiveDebugValues::ExtendRanges(MachineFunction &MF) {
438
439 DEBUG(dbgs() << "\nDebug Range Extension\n");
440
441 bool Changed = false;
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000442 bool OLChanged = false;
443 bool MBBJoined = false;
Vikram TV859ad292015-12-16 11:09:48 +0000444
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000445 VarLocMap VarLocIDs; // Map VarLoc<>unique ID for use in bitvectors.
Adrian Prantl7509d542016-05-26 21:42:47 +0000446 OpenRangesSet OpenRanges; // Ranges that are open until end of bb.
Vikram TV859ad292015-12-16 11:09:48 +0000447 VarLocInMBB OutLocs; // Ranges that exist beyond bb.
448 VarLocInMBB InLocs; // Ranges that are incoming after joining.
449
Daniel Berlin72560592016-01-10 18:08:32 +0000450 DenseMap<unsigned int, MachineBasicBlock *> OrderToBB;
451 DenseMap<MachineBasicBlock *, unsigned int> BBToOrder;
452 std::priority_queue<unsigned int, std::vector<unsigned int>,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000453 std::greater<unsigned int>>
454 Worklist;
Daniel Berlin72560592016-01-10 18:08:32 +0000455 std::priority_queue<unsigned int, std::vector<unsigned int>,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000456 std::greater<unsigned int>>
457 Pending;
458
Vikram TV859ad292015-12-16 11:09:48 +0000459 // Initialize every mbb with OutLocs.
460 for (auto &MBB : MF)
461 for (auto &MI : MBB)
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000462 transfer(MI, OpenRanges, OutLocs, VarLocIDs);
463
464 DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, "OutLocs after initialization",
465 dbgs()));
Vikram TV859ad292015-12-16 11:09:48 +0000466
Daniel Berlin72560592016-01-10 18:08:32 +0000467 ReversePostOrderTraversal<MachineFunction *> RPOT(&MF);
468 unsigned int RPONumber = 0;
469 for (auto RI = RPOT.begin(), RE = RPOT.end(); RI != RE; ++RI) {
470 OrderToBB[RPONumber] = *RI;
471 BBToOrder[*RI] = RPONumber;
472 Worklist.push(RPONumber);
473 ++RPONumber;
474 }
Daniel Berlin72560592016-01-10 18:08:32 +0000475 // This is a standard "union of predecessor outs" dataflow problem.
476 // To solve it, we perform join() and transfer() using the two worklist method
477 // until the ranges converge.
478 // Ranges have converged when both worklists are empty.
Keith Walker83ebef52016-09-27 16:46:07 +0000479 SmallPtrSet<const MachineBasicBlock *, 16> Visited;
Daniel Berlin72560592016-01-10 18:08:32 +0000480 while (!Worklist.empty() || !Pending.empty()) {
481 // We track what is on the pending worklist to avoid inserting the same
482 // thing twice. We could avoid this with a custom priority queue, but this
483 // is probably not worth it.
484 SmallPtrSet<MachineBasicBlock *, 16> OnPending;
Keith Walkerf83a19f2016-09-20 16:04:31 +0000485 DEBUG(dbgs() << "Processing Worklist\n");
Daniel Berlin72560592016-01-10 18:08:32 +0000486 while (!Worklist.empty()) {
487 MachineBasicBlock *MBB = OrderToBB[Worklist.top()];
488 Worklist.pop();
Keith Walker83ebef52016-09-27 16:46:07 +0000489 MBBJoined = join(*MBB, OutLocs, InLocs, VarLocIDs, Visited);
490 Visited.insert(MBB);
Daniel Berlin72560592016-01-10 18:08:32 +0000491 if (MBBJoined) {
492 MBBJoined = false;
493 Changed = true;
494 for (auto &MI : *MBB)
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000495 OLChanged |= transfer(MI, OpenRanges, OutLocs, VarLocIDs);
496
497 DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs,
498 "OutLocs after propagating", dbgs()));
499 DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs,
500 "InLocs after propagating", dbgs()));
Vikram TV859ad292015-12-16 11:09:48 +0000501
Daniel Berlin72560592016-01-10 18:08:32 +0000502 if (OLChanged) {
503 OLChanged = false;
504 for (auto s : MBB->successors())
Benjamin Kramer4dea8f52016-06-17 18:59:41 +0000505 if (OnPending.insert(s).second) {
Daniel Berlin72560592016-01-10 18:08:32 +0000506 Pending.push(BBToOrder[s]);
507 }
508 }
Vikram TV859ad292015-12-16 11:09:48 +0000509 }
510 }
Daniel Berlin72560592016-01-10 18:08:32 +0000511 Worklist.swap(Pending);
512 // At this point, pending must be empty, since it was just the empty
513 // worklist
514 assert(Pending.empty() && "Pending should be empty");
Vikram TV859ad292015-12-16 11:09:48 +0000515 }
Daniel Berlin72560592016-01-10 18:08:32 +0000516
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000517 DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, "Final OutLocs", dbgs()));
518 DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs, "Final InLocs", dbgs()));
Vikram TV859ad292015-12-16 11:09:48 +0000519 return Changed;
520}
521
522bool LiveDebugValues::runOnMachineFunction(MachineFunction &MF) {
523 TRI = MF.getSubtarget().getRegisterInfo();
524 TII = MF.getSubtarget().getInstrInfo();
525
526 bool Changed = false;
527
528 Changed |= ExtendRanges(MF);
529
530 return Changed;
531}