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