blob: 202e92a896247082121cccb3a17a426dada44fc2 [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
21#include "llvm/ADT/Statistic.h"
Daniel Berlin72560592016-01-10 18:08:32 +000022#include "llvm/ADT/PostOrderIterator.h"
23#include "llvm/ADT/SmallPtrSet.h"
Vikram TV859ad292015-12-16 11:09:48 +000024#include "llvm/ADT/SmallVector.h"
25#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineFunctionPass.h"
27#include "llvm/CodeGen/MachineInstrBuilder.h"
28#include "llvm/CodeGen/Passes.h"
Reid Kleckner28865802016-04-14 18:29:59 +000029#include "llvm/IR/DebugInfo.h"
Vikram TV859ad292015-12-16 11:09:48 +000030#include "llvm/Support/CommandLine.h"
31#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"
Daniel Berlin72560592016-01-10 18:08:32 +000037#include <queue>
Vikram TV859ad292015-12-16 11:09:48 +000038#include <list>
39
40using namespace llvm;
41
42#define DEBUG_TYPE "live-debug-values"
43
44STATISTIC(NumInserted, "Number of DBG_VALUE instructions inserted");
45
46namespace {
47
48class LiveDebugValues : public MachineFunctionPass {
49
50private:
51 const TargetRegisterInfo *TRI;
52 const TargetInstrInfo *TII;
53
54 typedef std::pair<const DILocalVariable *, const DILocation *>
55 InlinedVariable;
56
57 /// A potentially inlined instance of a variable.
58 struct DebugVariable {
59 const DILocalVariable *Var;
60 const DILocation *InlinedAt;
61
62 DebugVariable(const DILocalVariable *_var, const DILocation *_inlinedAt)
63 : Var(_var), InlinedAt(_inlinedAt) {}
64
65 bool operator==(const DebugVariable &DV) const {
66 return (Var == DV.Var) && (InlinedAt == DV.InlinedAt);
67 }
68 };
69
70 /// Member variables and functions for Range Extension across basic blocks.
71 struct VarLoc {
72 DebugVariable Var;
73 const MachineInstr *MI; // MachineInstr should be a DBG_VALUE instr.
74
75 VarLoc(DebugVariable _var, const MachineInstr *_mi) : Var(_var), MI(_mi) {}
76
77 bool operator==(const VarLoc &V) const;
78 };
79
80 typedef std::list<VarLoc> VarLocList;
81 typedef SmallDenseMap<const MachineBasicBlock *, VarLocList> VarLocInMBB;
82
Vikram TV859ad292015-12-16 11:09:48 +000083 void transferDebugValue(MachineInstr &MI, VarLocList &OpenRanges);
84 void transferRegisterDef(MachineInstr &MI, VarLocList &OpenRanges);
Daniel Berlinca4d93a2016-01-10 03:25:42 +000085 bool transferTerminatorInst(MachineInstr &MI, VarLocList &OpenRanges,
Vikram TV859ad292015-12-16 11:09:48 +000086 VarLocInMBB &OutLocs);
Daniel Berlinca4d93a2016-01-10 03:25:42 +000087 bool transfer(MachineInstr &MI, VarLocList &OpenRanges, VarLocInMBB &OutLocs);
Vikram TV859ad292015-12-16 11:09:48 +000088
Daniel Berlinca4d93a2016-01-10 03:25:42 +000089 bool join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs, VarLocInMBB &InLocs);
Vikram TV859ad292015-12-16 11:09:48 +000090
91 bool ExtendRanges(MachineFunction &MF);
92
93public:
94 static char ID;
95
96 /// Default construct and initialize the pass.
97 LiveDebugValues();
98
99 /// Tell the pass manager which passes we depend on and what
100 /// information we preserve.
101 void getAnalysisUsage(AnalysisUsage &AU) const override;
102
Derek Schuffad154c82016-03-28 17:05:30 +0000103 MachineFunctionProperties getRequiredProperties() const override {
104 return MachineFunctionProperties().set(
105 MachineFunctionProperties::Property::AllVRegsAllocated);
106 }
107
Vikram TV859ad292015-12-16 11:09:48 +0000108 /// Print to ostream with a message.
109 void printVarLocInMBB(const VarLocInMBB &V, const char *msg,
110 raw_ostream &Out) const;
111
112 /// Calculate the liveness information for the given machine function.
113 bool runOnMachineFunction(MachineFunction &MF) override;
114};
115} // namespace
116
117//===----------------------------------------------------------------------===//
118// Implementation
119//===----------------------------------------------------------------------===//
120
121char LiveDebugValues::ID = 0;
122char &llvm::LiveDebugValuesID = LiveDebugValues::ID;
123INITIALIZE_PASS(LiveDebugValues, "livedebugvalues", "Live DEBUG_VALUE analysis",
124 false, false)
125
126/// Default construct and initialize the pass.
127LiveDebugValues::LiveDebugValues() : MachineFunctionPass(ID) {
128 initializeLiveDebugValuesPass(*PassRegistry::getPassRegistry());
129}
130
131/// Tell the pass manager which passes we depend on and what information we
132/// preserve.
133void LiveDebugValues::getAnalysisUsage(AnalysisUsage &AU) const {
134 MachineFunctionPass::getAnalysisUsage(AU);
135}
136
137// \brief If @MI is a DBG_VALUE with debug value described by a defined
138// register, returns the number of this register. In the other case, returns 0.
139static unsigned isDescribedByReg(const MachineInstr &MI) {
140 assert(MI.isDebugValue());
141 assert(MI.getNumOperands() == 4);
142 // If location of variable is described using a register (directly or
143 // indirecltly), this register is always a first operand.
144 return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0;
145}
146
147// \brief This function takes two DBG_VALUE instructions and returns true
148// if their offsets are equal; otherwise returns false.
149static bool areOffsetsEqual(const MachineInstr &MI1, const MachineInstr &MI2) {
150 assert(MI1.isDebugValue());
151 assert(MI1.getNumOperands() == 4);
152
153 assert(MI2.isDebugValue());
154 assert(MI2.getNumOperands() == 4);
155
156 if (!MI1.isIndirectDebugValue() && !MI2.isIndirectDebugValue())
157 return true;
158
159 // Check if both MIs are indirect and they are equal.
160 if (MI1.isIndirectDebugValue() && MI2.isIndirectDebugValue())
161 return MI1.getOperand(1).getImm() == MI2.getOperand(1).getImm();
162
163 return false;
164}
165
166//===----------------------------------------------------------------------===//
167// Debug Range Extension Implementation
168//===----------------------------------------------------------------------===//
169
170void LiveDebugValues::printVarLocInMBB(const VarLocInMBB &V, const char *msg,
171 raw_ostream &Out) const {
172 Out << "Printing " << msg << ":\n";
173 for (const auto &L : V) {
174 Out << "MBB: " << L.first->getName() << ":\n";
175 for (const auto &VLL : L.second) {
176 Out << " Var: " << VLL.Var.Var->getName();
177 Out << " MI: ";
178 (*VLL.MI).dump();
179 Out << "\n";
180 }
181 }
182 Out << "\n";
183}
184
185bool LiveDebugValues::VarLoc::operator==(const VarLoc &V) const {
186 return (Var == V.Var) && (isDescribedByReg(*MI) == isDescribedByReg(*V.MI)) &&
187 (areOffsetsEqual(*MI, *V.MI));
188}
189
190/// End all previous ranges related to @MI and start a new range from @MI
191/// if it is a DBG_VALUE instr.
192void LiveDebugValues::transferDebugValue(MachineInstr &MI,
193 VarLocList &OpenRanges) {
194 if (!MI.isDebugValue())
195 return;
196 const DILocalVariable *RawVar = MI.getDebugVariable();
197 assert(RawVar->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
198 "Expected inlined-at fields to agree");
199 DebugVariable Var(RawVar, MI.getDebugLoc()->getInlinedAt());
200
201 // End all previous ranges of Var.
202 OpenRanges.erase(
203 std::remove_if(OpenRanges.begin(), OpenRanges.end(),
204 [&](const VarLoc &V) { return (Var == V.Var); }),
205 OpenRanges.end());
206
207 // Add Var to OpenRanges from this DBG_VALUE.
208 // TODO: Currently handles DBG_VALUE which has only reg as location.
209 if (isDescribedByReg(MI)) {
210 VarLoc V(Var, &MI);
211 OpenRanges.push_back(std::move(V));
212 }
213}
214
215/// A definition of a register may mark the end of a range.
216void LiveDebugValues::transferRegisterDef(MachineInstr &MI,
217 VarLocList &OpenRanges) {
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000218 MachineFunction *MF = MI.getParent()->getParent();
219 const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
220 unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
Vikram TV859ad292015-12-16 11:09:48 +0000221 for (const MachineOperand &MO : MI.operands()) {
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000222 if (MO.isReg() && MO.isDef() && MO.getReg() &&
223 TRI->isPhysicalRegister(MO.getReg())) {
224 // Remove ranges of all aliased registers.
225 for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI)
226 OpenRanges.erase(std::remove_if(OpenRanges.begin(), OpenRanges.end(),
227 [&](const VarLoc &V) {
228 return (*RAI ==
229 isDescribedByReg(*V.MI));
230 }),
231 OpenRanges.end());
232 } else if (MO.isRegMask()) {
233 // Remove ranges of all clobbered registers. Register masks don't usually
234 // list SP as preserved. While the debug info may be off for an
235 // instruction or two around callee-cleanup calls, transferring the
236 // DEBUG_VALUE across the call is still a better user experience.
Vikram TV859ad292015-12-16 11:09:48 +0000237 OpenRanges.erase(std::remove_if(OpenRanges.begin(), OpenRanges.end(),
238 [&](const VarLoc &V) {
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000239 unsigned Reg = isDescribedByReg(*V.MI);
240 return Reg && Reg != SP &&
241 MO.clobbersPhysReg(Reg);
Vikram TV859ad292015-12-16 11:09:48 +0000242 }),
243 OpenRanges.end());
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000244 }
Vikram TV859ad292015-12-16 11:09:48 +0000245 }
246}
247
248/// Terminate all open ranges at the end of the current basic block.
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000249bool LiveDebugValues::transferTerminatorInst(MachineInstr &MI,
Vikram TV859ad292015-12-16 11:09:48 +0000250 VarLocList &OpenRanges,
251 VarLocInMBB &OutLocs) {
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000252 bool Changed = false;
Vikram TV859ad292015-12-16 11:09:48 +0000253 const MachineBasicBlock *CurMBB = MI.getParent();
254 if (!(MI.isTerminator() || (&MI == &CurMBB->instr_back())))
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000255 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000256
257 if (OpenRanges.empty())
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000258 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000259
Alexey Samsonov117b1042016-01-07 23:38:45 +0000260 VarLocList &VLL = OutLocs[CurMBB];
Vikram TV859ad292015-12-16 11:09:48 +0000261
262 for (auto OR : OpenRanges) {
263 // Copy OpenRanges to OutLocs, if not already present.
264 assert(OR.MI->isDebugValue());
265 DEBUG(dbgs() << "Add to OutLocs: "; OR.MI->dump(););
266 if (std::find_if(VLL.begin(), VLL.end(),
267 [&](const VarLoc &V) { return (OR == V); }) == VLL.end()) {
268 VLL.push_back(std::move(OR));
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000269 Changed = true;
Vikram TV859ad292015-12-16 11:09:48 +0000270 }
271 }
272 OpenRanges.clear();
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000273 return Changed;
Vikram TV859ad292015-12-16 11:09:48 +0000274}
275
276/// This routine creates OpenRanges and OutLocs.
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000277bool LiveDebugValues::transfer(MachineInstr &MI, VarLocList &OpenRanges,
Vikram TV859ad292015-12-16 11:09:48 +0000278 VarLocInMBB &OutLocs) {
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000279 bool Changed = false;
Vikram TV859ad292015-12-16 11:09:48 +0000280 transferDebugValue(MI, OpenRanges);
281 transferRegisterDef(MI, OpenRanges);
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000282 Changed = transferTerminatorInst(MI, OpenRanges, OutLocs);
283 return Changed;
Vikram TV859ad292015-12-16 11:09:48 +0000284}
285
286/// This routine joins the analysis results of all incoming edges in @MBB by
287/// inserting a new DBG_VALUE instruction at the start of the @MBB - if the same
288/// source variable in all the predecessors of @MBB reside in the same location.
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000289bool LiveDebugValues::join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs,
Vikram TV859ad292015-12-16 11:09:48 +0000290 VarLocInMBB &InLocs) {
291 DEBUG(dbgs() << "join MBB: " << MBB.getName() << "\n");
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000292 bool Changed = false;
Vikram TV859ad292015-12-16 11:09:48 +0000293
294 VarLocList InLocsT; // Temporary incoming locations.
295
296 // For all predecessors of this MBB, find the set of VarLocs that can be
297 // joined.
298 for (auto p : MBB.predecessors()) {
299 auto OL = OutLocs.find(p);
300 // Join is null in case of empty OutLocs from any of the pred.
301 if (OL == OutLocs.end())
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000302 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000303
304 // Just copy over the Out locs to incoming locs for the first predecessor.
305 if (p == *MBB.pred_begin()) {
306 InLocsT = OL->second;
307 continue;
308 }
309
310 // Join with this predecessor.
311 VarLocList &VLL = OL->second;
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000312 InLocsT.erase(
313 std::remove_if(InLocsT.begin(), InLocsT.end(), [&](VarLoc &ILT) {
314 return (std::find_if(VLL.begin(), VLL.end(), [&](const VarLoc &V) {
315 return (ILT == V);
316 }) == VLL.end());
317 }), InLocsT.end());
Vikram TV859ad292015-12-16 11:09:48 +0000318 }
319
320 if (InLocsT.empty())
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000321 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000322
Alexey Samsonov117b1042016-01-07 23:38:45 +0000323 VarLocList &ILL = InLocs[&MBB];
Vikram TV859ad292015-12-16 11:09:48 +0000324
325 // Insert DBG_VALUE instructions, if not already inserted.
326 for (auto ILT : InLocsT) {
327 if (std::find_if(ILL.begin(), ILL.end(), [&](const VarLoc &I) {
328 return (ILT == I);
329 }) == ILL.end()) {
330 // This VarLoc is not found in InLocs i.e. it is not yet inserted. So, a
331 // new range is started for the var from the mbb's beginning by inserting
332 // a new DBG_VALUE. transfer() will end this range however appropriate.
333 const MachineInstr *DMI = ILT.MI;
334 MachineInstr *MI =
335 BuildMI(MBB, MBB.instr_begin(), DMI->getDebugLoc(), DMI->getDesc(),
336 DMI->isIndirectDebugValue(), DMI->getOperand(0).getReg(), 0,
337 DMI->getDebugVariable(), DMI->getDebugExpression());
338 if (DMI->isIndirectDebugValue())
339 MI->getOperand(1).setImm(DMI->getOperand(1).getImm());
340 DEBUG(dbgs() << "Inserted: "; MI->dump(););
341 ++NumInserted;
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000342 Changed = true;
Vikram TV859ad292015-12-16 11:09:48 +0000343
344 VarLoc V(ILT.Var, MI);
345 ILL.push_back(std::move(V));
346 }
347 }
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000348 return Changed;
Vikram TV859ad292015-12-16 11:09:48 +0000349}
350
351/// Calculate the liveness information for the given machine function and
352/// extend ranges across basic blocks.
353bool LiveDebugValues::ExtendRanges(MachineFunction &MF) {
354
355 DEBUG(dbgs() << "\nDebug Range Extension\n");
356
357 bool Changed = false;
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000358 bool OLChanged = false;
359 bool MBBJoined = false;
Vikram TV859ad292015-12-16 11:09:48 +0000360
361 VarLocList OpenRanges; // Ranges that are open until end of bb.
362 VarLocInMBB OutLocs; // Ranges that exist beyond bb.
363 VarLocInMBB InLocs; // Ranges that are incoming after joining.
364
Daniel Berlin72560592016-01-10 18:08:32 +0000365 DenseMap<unsigned int, MachineBasicBlock *> OrderToBB;
366 DenseMap<MachineBasicBlock *, unsigned int> BBToOrder;
367 std::priority_queue<unsigned int, std::vector<unsigned int>,
368 std::greater<unsigned int>> Worklist;
369 std::priority_queue<unsigned int, std::vector<unsigned int>,
370 std::greater<unsigned int>> Pending;
Vikram TV859ad292015-12-16 11:09:48 +0000371 // Initialize every mbb with OutLocs.
372 for (auto &MBB : MF)
373 for (auto &MI : MBB)
374 transfer(MI, OpenRanges, OutLocs);
375 DEBUG(printVarLocInMBB(OutLocs, "OutLocs after initialization", dbgs()));
376
Daniel Berlin72560592016-01-10 18:08:32 +0000377 ReversePostOrderTraversal<MachineFunction *> RPOT(&MF);
378 unsigned int RPONumber = 0;
379 for (auto RI = RPOT.begin(), RE = RPOT.end(); RI != RE; ++RI) {
380 OrderToBB[RPONumber] = *RI;
381 BBToOrder[*RI] = RPONumber;
382 Worklist.push(RPONumber);
383 ++RPONumber;
384 }
Vikram TV859ad292015-12-16 11:09:48 +0000385
Daniel Berlin72560592016-01-10 18:08:32 +0000386 // This is a standard "union of predecessor outs" dataflow problem.
387 // To solve it, we perform join() and transfer() using the two worklist method
388 // until the ranges converge.
389 // Ranges have converged when both worklists are empty.
390 while (!Worklist.empty() || !Pending.empty()) {
391 // We track what is on the pending worklist to avoid inserting the same
392 // thing twice. We could avoid this with a custom priority queue, but this
393 // is probably not worth it.
394 SmallPtrSet<MachineBasicBlock *, 16> OnPending;
395 while (!Worklist.empty()) {
396 MachineBasicBlock *MBB = OrderToBB[Worklist.top()];
397 Worklist.pop();
398 MBBJoined = join(*MBB, OutLocs, InLocs);
Vikram TV859ad292015-12-16 11:09:48 +0000399
Daniel Berlin72560592016-01-10 18:08:32 +0000400 if (MBBJoined) {
401 MBBJoined = false;
402 Changed = true;
403 for (auto &MI : *MBB)
404 OLChanged |= transfer(MI, OpenRanges, OutLocs);
405 DEBUG(printVarLocInMBB(OutLocs, "OutLocs after propagating", dbgs()));
406 DEBUG(printVarLocInMBB(InLocs, "InLocs after propagating", dbgs()));
Vikram TV859ad292015-12-16 11:09:48 +0000407
Daniel Berlin72560592016-01-10 18:08:32 +0000408 if (OLChanged) {
409 OLChanged = false;
410 for (auto s : MBB->successors())
411 if (!OnPending.count(s)) {
412 OnPending.insert(s);
413 Pending.push(BBToOrder[s]);
414 }
415 }
Vikram TV859ad292015-12-16 11:09:48 +0000416 }
417 }
Daniel Berlin72560592016-01-10 18:08:32 +0000418 Worklist.swap(Pending);
419 // At this point, pending must be empty, since it was just the empty
420 // worklist
421 assert(Pending.empty() && "Pending should be empty");
Vikram TV859ad292015-12-16 11:09:48 +0000422 }
Daniel Berlin72560592016-01-10 18:08:32 +0000423
Vikram TV859ad292015-12-16 11:09:48 +0000424 DEBUG(printVarLocInMBB(OutLocs, "Final OutLocs", dbgs()));
425 DEBUG(printVarLocInMBB(InLocs, "Final InLocs", dbgs()));
426 return Changed;
427}
428
429bool LiveDebugValues::runOnMachineFunction(MachineFunction &MF) {
430 TRI = MF.getSubtarget().getRegisterInfo();
431 TII = MF.getSubtarget().getInstrInfo();
432
433 bool Changed = false;
434
435 Changed |= ExtendRanges(MF);
436
437 return Changed;
438}