blob: 9afff06685c53b82934b5ff4c4e096d2a6fc7aa9 [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
64 typedef std::pair<const DILocalVariable *, const DILocation *>
65 InlinedVariable;
66
67 /// A potentially inlined instance of a variable.
68 struct DebugVariable {
69 const DILocalVariable *Var;
70 const DILocation *InlinedAt;
71
72 DebugVariable(const DILocalVariable *_var, const DILocation *_inlinedAt)
73 : Var(_var), InlinedAt(_inlinedAt) {}
74
Adrian Prantl6ee02c72016-05-25 22:21:12 +000075 bool operator<(const DebugVariable &DV) const {
76 if (Var == DV.Var)
77 return InlinedAt < DV.InlinedAt;
78 return Var < DV.Var;
79 }
80
Vikram TV859ad292015-12-16 11:09:48 +000081 bool operator==(const DebugVariable &DV) const {
82 return (Var == DV.Var) && (InlinedAt == DV.InlinedAt);
83 }
84 };
85
Adrian Prantl6ee02c72016-05-25 22:21:12 +000086 /// A pair of debug variable and value location.
Vikram TV859ad292015-12-16 11:09:48 +000087 struct VarLoc {
Adrian Prantl6ee02c72016-05-25 22:21:12 +000088 const DebugVariable Var;
89 const MachineInstr &MI; ///< Only used for cloning a new DBG_VALUE.
Vikram TV859ad292015-12-16 11:09:48 +000090
Adrian Prantl6ee02c72016-05-25 22:21:12 +000091 enum { InvalidKind = 0, RegisterKind } Kind;
Vikram TV859ad292015-12-16 11:09:48 +000092
Adrian Prantl6ee02c72016-05-25 22:21:12 +000093 /// The value location. Stored separately to avoid repeatedly
94 /// extracting it from MI.
95 union {
96 struct {
97 uint32_t RegNo;
98 uint32_t Offset;
99 } RegisterLoc;
100 uint64_t Hash;
101 } Loc;
102
103 VarLoc(const MachineInstr &MI)
104 : Var(MI.getDebugVariable(), MI.getDebugLoc()->getInlinedAt()), MI(MI),
105 Kind(InvalidKind) {
106 static_assert((sizeof(Loc) == sizeof(uint64_t)),
107 "hash does not cover all members of Loc");
108 assert(MI.isDebugValue() && "not a DBG_VALUE");
109 assert(MI.getNumOperands() == 4 && "malformed DBG_VALUE");
Adrian Prantl00698732016-05-25 22:37:29 +0000110 if (int RegNo = isDbgValueDescribedByReg(MI)) {
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000111 Kind = RegisterKind;
112 Loc.RegisterLoc.RegNo = RegNo;
113 uint64_t Offset =
114 MI.isIndirectDebugValue() ? MI.getOperand(1).getImm() : 0;
115 // We don't support offsets larger than 4GiB here. They are
116 // slated to be replaced with DIExpressions anyway.
117 if (Offset >= (1ULL << 32))
118 Kind = InvalidKind;
119 else
120 Loc.RegisterLoc.Offset = Offset;
121 }
122 }
123
124 /// If this variable is described by a register, return it,
125 /// otherwise return 0.
126 unsigned isDescribedByReg() const {
127 if (Kind == RegisterKind)
128 return Loc.RegisterLoc.RegNo;
129 return 0;
130 }
131
132 void dump() const { MI.dump(); }
133
134 bool operator==(const VarLoc &Other) const {
135 return Var == Other.Var && Loc.Hash == Other.Loc.Hash;
136 }
137
138 bool operator<(const VarLoc &Other) const {
139 if (Var == Other.Var)
140 return Loc.Hash < Other.Loc.Hash;
141 return Var < Other.Var;
142 }
Vikram TV859ad292015-12-16 11:09:48 +0000143 };
144
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000145 typedef UniqueVector<VarLoc> VarLocMap;
146 typedef SparseBitVector<> VarLocList;
147 typedef SparseBitVector<> VarLocSet;
148 typedef SmallDenseMap<const MachineBasicBlock *, VarLocSet> VarLocInMBB;
Vikram TV859ad292015-12-16 11:09:48 +0000149
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000150 void transferDebugValue(const MachineInstr &MI, VarLocList &OpenRanges,
151 VarLocMap &VarLocIDs);
152 void transferRegisterDef(MachineInstr &MI, VarLocList &OpenRanges,
153 const VarLocMap &VarLocIDs);
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000154 bool transferTerminatorInst(MachineInstr &MI, VarLocList &OpenRanges,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000155 VarLocInMBB &OutLocs, const VarLocMap &VarLocIDs);
156 bool transfer(MachineInstr &MI, VarLocList &OpenRanges, VarLocInMBB &OutLocs,
157 VarLocMap &VarLocIDs);
Vikram TV859ad292015-12-16 11:09:48 +0000158
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000159 bool join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs, VarLocInMBB &InLocs,
160 const VarLocMap &VarLocIDs);
Vikram TV859ad292015-12-16 11:09:48 +0000161
162 bool ExtendRanges(MachineFunction &MF);
163
164public:
165 static char ID;
166
167 /// Default construct and initialize the pass.
168 LiveDebugValues();
169
170 /// Tell the pass manager which passes we depend on and what
171 /// information we preserve.
172 void getAnalysisUsage(AnalysisUsage &AU) const override;
173
Derek Schuffad154c82016-03-28 17:05:30 +0000174 MachineFunctionProperties getRequiredProperties() const override {
175 return MachineFunctionProperties().set(
176 MachineFunctionProperties::Property::AllVRegsAllocated);
177 }
178
Vikram TV859ad292015-12-16 11:09:48 +0000179 /// Print to ostream with a message.
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000180 void printVarLocInMBB(const MachineFunction &MF, const VarLocInMBB &V,
181 const VarLocMap &VarLocIDs, const char *msg,
Vikram TV859ad292015-12-16 11:09:48 +0000182 raw_ostream &Out) const;
183
184 /// Calculate the liveness information for the given machine function.
185 bool runOnMachineFunction(MachineFunction &MF) override;
186};
187} // namespace
188
189//===----------------------------------------------------------------------===//
190// Implementation
191//===----------------------------------------------------------------------===//
192
193char LiveDebugValues::ID = 0;
194char &llvm::LiveDebugValuesID = LiveDebugValues::ID;
195INITIALIZE_PASS(LiveDebugValues, "livedebugvalues", "Live DEBUG_VALUE analysis",
196 false, false)
197
198/// Default construct and initialize the pass.
199LiveDebugValues::LiveDebugValues() : MachineFunctionPass(ID) {
200 initializeLiveDebugValuesPass(*PassRegistry::getPassRegistry());
201}
202
203/// Tell the pass manager which passes we depend on and what information we
204/// preserve.
205void LiveDebugValues::getAnalysisUsage(AnalysisUsage &AU) const {
206 MachineFunctionPass::getAnalysisUsage(AU);
207}
208
Vikram TV859ad292015-12-16 11:09:48 +0000209//===----------------------------------------------------------------------===//
210// Debug Range Extension Implementation
211//===----------------------------------------------------------------------===//
212
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000213void LiveDebugValues::printVarLocInMBB(const MachineFunction &MF,
214 const VarLocInMBB &V,
215 const VarLocMap &VarLocIDs,
216 const char *msg,
Vikram TV859ad292015-12-16 11:09:48 +0000217 raw_ostream &Out) const {
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000218 for (const MachineBasicBlock &BB : MF) {
219 const auto &L = V.lookup(&BB);
220 Out << "MBB: " << BB.getName() << ":\n";
221 for (unsigned VLL : L) {
222 const VarLoc &VL = VarLocIDs[VLL];
223 Out << " Var: " << VL.Var.Var->getName();
Vikram TV859ad292015-12-16 11:09:48 +0000224 Out << " MI: ";
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000225 VL.dump();
Vikram TV859ad292015-12-16 11:09:48 +0000226 Out << "\n";
227 }
228 }
229 Out << "\n";
230}
231
Vikram TV859ad292015-12-16 11:09:48 +0000232/// End all previous ranges related to @MI and start a new range from @MI
233/// if it is a DBG_VALUE instr.
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000234void LiveDebugValues::transferDebugValue(const MachineInstr &MI,
235 VarLocList &OpenRanges,
236 VarLocMap &VarLocIDs) {
Vikram TV859ad292015-12-16 11:09:48 +0000237 if (!MI.isDebugValue())
238 return;
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000239 const DILocalVariable *Var = MI.getDebugVariable();
240 const DILocation *DebugLoc = MI.getDebugLoc();
241 const DILocation *InlinedAt = DebugLoc->getInlinedAt();
242 assert(Var->isValidLocationForIntrinsic(DebugLoc) &&
Vikram TV859ad292015-12-16 11:09:48 +0000243 "Expected inlined-at fields to agree");
Vikram TV859ad292015-12-16 11:09:48 +0000244
245 // End all previous ranges of Var.
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000246 SparseBitVector<> KillSet;
247 for (unsigned ID : OpenRanges) {
248 auto &ORVar = VarLocIDs[ID].Var;
249 if (ORVar.Var == Var && ORVar.InlinedAt == InlinedAt)
250 KillSet.set(ID);
Vikram TV859ad292015-12-16 11:09:48 +0000251 }
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000252 OpenRanges.intersectWithComplement(KillSet);
253
254 // Add the VarLoc to OpenRanges from this DBG_VALUE.
255 // TODO: Currently handles DBG_VALUE which has only reg as location.
Adrian Prantl00698732016-05-25 22:37:29 +0000256 if (isDbgValueDescribedByReg(MI))
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000257 OpenRanges.set(VarLocIDs.insert(MI));
Vikram TV859ad292015-12-16 11:09:48 +0000258}
259
260/// A definition of a register may mark the end of a range.
261void LiveDebugValues::transferRegisterDef(MachineInstr &MI,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000262 VarLocList &OpenRanges,
263 const VarLocMap &VarLocIDs) {
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000264 MachineFunction *MF = MI.getParent()->getParent();
265 const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
266 unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000267 SparseBitVector<> KillSet;
Vikram TV859ad292015-12-16 11:09:48 +0000268 for (const MachineOperand &MO : MI.operands()) {
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000269 if (MO.isReg() && MO.isDef() && MO.getReg() &&
270 TRI->isPhysicalRegister(MO.getReg())) {
271 // Remove ranges of all aliased registers.
272 for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI)
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000273 for (unsigned ID : OpenRanges)
274 if (VarLocIDs[ID].isDescribedByReg() == *RAI)
275 KillSet.set(ID);
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000276 } else if (MO.isRegMask()) {
277 // Remove ranges of all clobbered registers. Register masks don't usually
278 // list SP as preserved. While the debug info may be off for an
279 // instruction or two around callee-cleanup calls, transferring the
280 // DEBUG_VALUE across the call is still a better user experience.
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000281 for (unsigned ID : OpenRanges) {
282 unsigned Reg = VarLocIDs[ID].isDescribedByReg();
283 if (Reg && Reg != SP && MO.clobbersPhysReg(Reg))
284 KillSet.set(ID);
285 }
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000286 }
Vikram TV859ad292015-12-16 11:09:48 +0000287 }
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000288 OpenRanges.intersectWithComplement(KillSet);
Vikram TV859ad292015-12-16 11:09:48 +0000289}
290
291/// Terminate all open ranges at the end of the current basic block.
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000292bool LiveDebugValues::transferTerminatorInst(MachineInstr &MI,
Vikram TV859ad292015-12-16 11:09:48 +0000293 VarLocList &OpenRanges,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000294 VarLocInMBB &OutLocs,
295 const VarLocMap &VarLocIDs) {
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000296 bool Changed = false;
Vikram TV859ad292015-12-16 11:09:48 +0000297 const MachineBasicBlock *CurMBB = MI.getParent();
298 if (!(MI.isTerminator() || (&MI == &CurMBB->instr_back())))
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000299 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000300
301 if (OpenRanges.empty())
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000302 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000303
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000304 DEBUG(for (unsigned ID
305 : OpenRanges) {
Vikram TV859ad292015-12-16 11:09:48 +0000306 // Copy OpenRanges to OutLocs, if not already present.
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000307 dbgs() << "Add to OutLocs: ";
308 VarLocIDs[ID].dump();
309 });
310 VarLocSet &VLS = OutLocs[CurMBB];
311 Changed = VLS |= OpenRanges;
Vikram TV859ad292015-12-16 11:09:48 +0000312 OpenRanges.clear();
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000313 return Changed;
Vikram TV859ad292015-12-16 11:09:48 +0000314}
315
316/// This routine creates OpenRanges and OutLocs.
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000317bool LiveDebugValues::transfer(MachineInstr &MI, VarLocList &OpenRanges,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000318 VarLocInMBB &OutLocs, VarLocMap &VarLocIDs) {
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000319 bool Changed = false;
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000320 transferDebugValue(MI, OpenRanges, VarLocIDs);
321 transferRegisterDef(MI, OpenRanges, VarLocIDs);
322 Changed = transferTerminatorInst(MI, OpenRanges, OutLocs, VarLocIDs);
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000323 return Changed;
Vikram TV859ad292015-12-16 11:09:48 +0000324}
325
326/// This routine joins the analysis results of all incoming edges in @MBB by
327/// inserting a new DBG_VALUE instruction at the start of the @MBB - if the same
328/// source variable in all the predecessors of @MBB reside in the same location.
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000329bool LiveDebugValues::join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000330 VarLocInMBB &InLocs, const VarLocMap &VarLocIDs) {
Vikram TV859ad292015-12-16 11:09:48 +0000331 DEBUG(dbgs() << "join MBB: " << MBB.getName() << "\n");
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000332 bool Changed = false;
Vikram TV859ad292015-12-16 11:09:48 +0000333
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000334 VarLocSet InLocsT; // Temporary incoming locations.
Vikram TV859ad292015-12-16 11:09:48 +0000335
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000336 // For all predecessors of this MBB, find the set of VarLocs that
337 // can be joined.
Vikram TV859ad292015-12-16 11:09:48 +0000338 for (auto p : MBB.predecessors()) {
339 auto OL = OutLocs.find(p);
340 // Join is null in case of empty OutLocs from any of the pred.
341 if (OL == OutLocs.end())
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000342 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000343
344 // Just copy over the Out locs to incoming locs for the first predecessor.
345 if (p == *MBB.pred_begin()) {
346 InLocsT = OL->second;
347 continue;
348 }
Vikram TV859ad292015-12-16 11:09:48 +0000349 // Join with this predecessor.
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000350 InLocsT &= OL->second;
Vikram TV859ad292015-12-16 11:09:48 +0000351 }
352
353 if (InLocsT.empty())
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000354 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000355
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000356 VarLocSet &ILS = InLocs[&MBB];
Vikram TV859ad292015-12-16 11:09:48 +0000357
358 // Insert DBG_VALUE instructions, if not already inserted.
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000359 VarLocSet Diff = InLocsT;
360 Diff.intersectWithComplement(ILS);
361 for (auto ID : Diff) {
362 // This VarLoc is not found in InLocs i.e. it is not yet inserted. So, a
363 // new range is started for the var from the mbb's beginning by inserting
364 // a new DBG_VALUE. transfer() will end this range however appropriate.
365 const VarLoc &DiffIt = VarLocIDs[ID];
366 const MachineInstr *DMI = &DiffIt.MI;
367 MachineInstr *MI =
368 BuildMI(MBB, MBB.instr_begin(), DMI->getDebugLoc(), DMI->getDesc(),
369 DMI->isIndirectDebugValue(), DMI->getOperand(0).getReg(), 0,
370 DMI->getDebugVariable(), DMI->getDebugExpression());
371 if (DMI->isIndirectDebugValue())
372 MI->getOperand(1).setImm(DMI->getOperand(1).getImm());
373 DEBUG(dbgs() << "Inserted: "; MI->dump(););
374 ILS.set(ID);
375 ++NumInserted;
376 Changed = true;
Vikram TV859ad292015-12-16 11:09:48 +0000377 }
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000378 return Changed;
Vikram TV859ad292015-12-16 11:09:48 +0000379}
380
381/// Calculate the liveness information for the given machine function and
382/// extend ranges across basic blocks.
383bool LiveDebugValues::ExtendRanges(MachineFunction &MF) {
384
385 DEBUG(dbgs() << "\nDebug Range Extension\n");
386
387 bool Changed = false;
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000388 bool OLChanged = false;
389 bool MBBJoined = false;
Vikram TV859ad292015-12-16 11:09:48 +0000390
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000391 VarLocMap VarLocIDs; // Map VarLoc<>unique ID for use in bitvectors.
Vikram TV859ad292015-12-16 11:09:48 +0000392 VarLocList OpenRanges; // Ranges that are open until end of bb.
393 VarLocInMBB OutLocs; // Ranges that exist beyond bb.
394 VarLocInMBB InLocs; // Ranges that are incoming after joining.
395
Daniel Berlin72560592016-01-10 18:08:32 +0000396 DenseMap<unsigned int, MachineBasicBlock *> OrderToBB;
397 DenseMap<MachineBasicBlock *, unsigned int> BBToOrder;
398 std::priority_queue<unsigned int, std::vector<unsigned int>,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000399 std::greater<unsigned int>>
400 Worklist;
Daniel Berlin72560592016-01-10 18:08:32 +0000401 std::priority_queue<unsigned int, std::vector<unsigned int>,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000402 std::greater<unsigned int>>
403 Pending;
404
Vikram TV859ad292015-12-16 11:09:48 +0000405 // Initialize every mbb with OutLocs.
406 for (auto &MBB : MF)
407 for (auto &MI : MBB)
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000408 transfer(MI, OpenRanges, OutLocs, VarLocIDs);
409
410 DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, "OutLocs after initialization",
411 dbgs()));
Vikram TV859ad292015-12-16 11:09:48 +0000412
Daniel Berlin72560592016-01-10 18:08:32 +0000413 ReversePostOrderTraversal<MachineFunction *> RPOT(&MF);
414 unsigned int RPONumber = 0;
415 for (auto RI = RPOT.begin(), RE = RPOT.end(); RI != RE; ++RI) {
416 OrderToBB[RPONumber] = *RI;
417 BBToOrder[*RI] = RPONumber;
418 Worklist.push(RPONumber);
419 ++RPONumber;
420 }
Daniel Berlin72560592016-01-10 18:08:32 +0000421 // This is a standard "union of predecessor outs" dataflow problem.
422 // To solve it, we perform join() and transfer() using the two worklist method
423 // until the ranges converge.
424 // Ranges have converged when both worklists are empty.
425 while (!Worklist.empty() || !Pending.empty()) {
426 // We track what is on the pending worklist to avoid inserting the same
427 // thing twice. We could avoid this with a custom priority queue, but this
428 // is probably not worth it.
429 SmallPtrSet<MachineBasicBlock *, 16> OnPending;
430 while (!Worklist.empty()) {
431 MachineBasicBlock *MBB = OrderToBB[Worklist.top()];
432 Worklist.pop();
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000433 MBBJoined = join(*MBB, OutLocs, InLocs, VarLocIDs);
Vikram TV859ad292015-12-16 11:09:48 +0000434
Daniel Berlin72560592016-01-10 18:08:32 +0000435 if (MBBJoined) {
436 MBBJoined = false;
437 Changed = true;
438 for (auto &MI : *MBB)
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000439 OLChanged |= transfer(MI, OpenRanges, OutLocs, VarLocIDs);
440
441 DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs,
442 "OutLocs after propagating", dbgs()));
443 DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs,
444 "InLocs after propagating", dbgs()));
Vikram TV859ad292015-12-16 11:09:48 +0000445
Daniel Berlin72560592016-01-10 18:08:32 +0000446 if (OLChanged) {
447 OLChanged = false;
448 for (auto s : MBB->successors())
449 if (!OnPending.count(s)) {
450 OnPending.insert(s);
451 Pending.push(BBToOrder[s]);
452 }
453 }
Vikram TV859ad292015-12-16 11:09:48 +0000454 }
455 }
Daniel Berlin72560592016-01-10 18:08:32 +0000456 Worklist.swap(Pending);
457 // At this point, pending must be empty, since it was just the empty
458 // worklist
459 assert(Pending.empty() && "Pending should be empty");
Vikram TV859ad292015-12-16 11:09:48 +0000460 }
Daniel Berlin72560592016-01-10 18:08:32 +0000461
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000462 DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, "Final OutLocs", dbgs()));
463 DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs, "Final InLocs", dbgs()));
Vikram TV859ad292015-12-16 11:09:48 +0000464 return Changed;
465}
466
467bool LiveDebugValues::runOnMachineFunction(MachineFunction &MF) {
468 TRI = MF.getSubtarget().getRegisterInfo();
469 TII = MF.getSubtarget().getInstrInfo();
470
471 bool Changed = false;
472
473 Changed |= ExtendRanges(MF);
474
475 return Changed;
476}