blob: d3cbac8cc176d161fe8895346f1fe1e36649dc28 [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
102 /// Print to ostream with a message.
103 void printVarLocInMBB(const VarLocInMBB &V, const char *msg,
104 raw_ostream &Out) const;
105
106 /// Calculate the liveness information for the given machine function.
107 bool runOnMachineFunction(MachineFunction &MF) override;
108};
109} // namespace
110
111//===----------------------------------------------------------------------===//
112// Implementation
113//===----------------------------------------------------------------------===//
114
115char LiveDebugValues::ID = 0;
116char &llvm::LiveDebugValuesID = LiveDebugValues::ID;
117INITIALIZE_PASS(LiveDebugValues, "livedebugvalues", "Live DEBUG_VALUE analysis",
118 false, false)
119
120/// Default construct and initialize the pass.
121LiveDebugValues::LiveDebugValues() : MachineFunctionPass(ID) {
122 initializeLiveDebugValuesPass(*PassRegistry::getPassRegistry());
123}
124
125/// Tell the pass manager which passes we depend on and what information we
126/// preserve.
127void LiveDebugValues::getAnalysisUsage(AnalysisUsage &AU) const {
128 MachineFunctionPass::getAnalysisUsage(AU);
129}
130
131// \brief If @MI is a DBG_VALUE with debug value described by a defined
132// register, returns the number of this register. In the other case, returns 0.
133static unsigned isDescribedByReg(const MachineInstr &MI) {
134 assert(MI.isDebugValue());
135 assert(MI.getNumOperands() == 4);
136 // If location of variable is described using a register (directly or
137 // indirecltly), this register is always a first operand.
138 return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0;
139}
140
141// \brief This function takes two DBG_VALUE instructions and returns true
142// if their offsets are equal; otherwise returns false.
143static bool areOffsetsEqual(const MachineInstr &MI1, const MachineInstr &MI2) {
144 assert(MI1.isDebugValue());
145 assert(MI1.getNumOperands() == 4);
146
147 assert(MI2.isDebugValue());
148 assert(MI2.getNumOperands() == 4);
149
150 if (!MI1.isIndirectDebugValue() && !MI2.isIndirectDebugValue())
151 return true;
152
153 // Check if both MIs are indirect and they are equal.
154 if (MI1.isIndirectDebugValue() && MI2.isIndirectDebugValue())
155 return MI1.getOperand(1).getImm() == MI2.getOperand(1).getImm();
156
157 return false;
158}
159
160//===----------------------------------------------------------------------===//
161// Debug Range Extension Implementation
162//===----------------------------------------------------------------------===//
163
164void LiveDebugValues::printVarLocInMBB(const VarLocInMBB &V, const char *msg,
165 raw_ostream &Out) const {
166 Out << "Printing " << msg << ":\n";
167 for (const auto &L : V) {
168 Out << "MBB: " << L.first->getName() << ":\n";
169 for (const auto &VLL : L.second) {
170 Out << " Var: " << VLL.Var.Var->getName();
171 Out << " MI: ";
172 (*VLL.MI).dump();
173 Out << "\n";
174 }
175 }
176 Out << "\n";
177}
178
179bool LiveDebugValues::VarLoc::operator==(const VarLoc &V) const {
180 return (Var == V.Var) && (isDescribedByReg(*MI) == isDescribedByReg(*V.MI)) &&
181 (areOffsetsEqual(*MI, *V.MI));
182}
183
184/// End all previous ranges related to @MI and start a new range from @MI
185/// if it is a DBG_VALUE instr.
186void LiveDebugValues::transferDebugValue(MachineInstr &MI,
187 VarLocList &OpenRanges) {
188 if (!MI.isDebugValue())
189 return;
190 const DILocalVariable *RawVar = MI.getDebugVariable();
191 assert(RawVar->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
192 "Expected inlined-at fields to agree");
193 DebugVariable Var(RawVar, MI.getDebugLoc()->getInlinedAt());
194
195 // End all previous ranges of Var.
196 OpenRanges.erase(
197 std::remove_if(OpenRanges.begin(), OpenRanges.end(),
198 [&](const VarLoc &V) { return (Var == V.Var); }),
199 OpenRanges.end());
200
201 // Add Var to OpenRanges from this DBG_VALUE.
202 // TODO: Currently handles DBG_VALUE which has only reg as location.
203 if (isDescribedByReg(MI)) {
204 VarLoc V(Var, &MI);
205 OpenRanges.push_back(std::move(V));
206 }
207}
208
209/// A definition of a register may mark the end of a range.
210void LiveDebugValues::transferRegisterDef(MachineInstr &MI,
211 VarLocList &OpenRanges) {
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000212 MachineFunction *MF = MI.getParent()->getParent();
213 const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
214 unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
Vikram TV859ad292015-12-16 11:09:48 +0000215 for (const MachineOperand &MO : MI.operands()) {
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000216 if (MO.isReg() && MO.isDef() && MO.getReg() &&
217 TRI->isPhysicalRegister(MO.getReg())) {
218 // Remove ranges of all aliased registers.
219 for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI)
220 OpenRanges.erase(std::remove_if(OpenRanges.begin(), OpenRanges.end(),
221 [&](const VarLoc &V) {
222 return (*RAI ==
223 isDescribedByReg(*V.MI));
224 }),
225 OpenRanges.end());
226 } else if (MO.isRegMask()) {
227 // Remove ranges of all clobbered registers. Register masks don't usually
228 // list SP as preserved. While the debug info may be off for an
229 // instruction or two around callee-cleanup calls, transferring the
230 // DEBUG_VALUE across the call is still a better user experience.
Vikram TV859ad292015-12-16 11:09:48 +0000231 OpenRanges.erase(std::remove_if(OpenRanges.begin(), OpenRanges.end(),
232 [&](const VarLoc &V) {
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000233 unsigned Reg = isDescribedByReg(*V.MI);
234 return Reg && Reg != SP &&
235 MO.clobbersPhysReg(Reg);
Vikram TV859ad292015-12-16 11:09:48 +0000236 }),
237 OpenRanges.end());
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000238 }
Vikram TV859ad292015-12-16 11:09:48 +0000239 }
240}
241
242/// Terminate all open ranges at the end of the current basic block.
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000243bool LiveDebugValues::transferTerminatorInst(MachineInstr &MI,
Vikram TV859ad292015-12-16 11:09:48 +0000244 VarLocList &OpenRanges,
245 VarLocInMBB &OutLocs) {
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000246 bool Changed = false;
Vikram TV859ad292015-12-16 11:09:48 +0000247 const MachineBasicBlock *CurMBB = MI.getParent();
248 if (!(MI.isTerminator() || (&MI == &CurMBB->instr_back())))
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000249 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000250
251 if (OpenRanges.empty())
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000252 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000253
Alexey Samsonov117b1042016-01-07 23:38:45 +0000254 VarLocList &VLL = OutLocs[CurMBB];
Vikram TV859ad292015-12-16 11:09:48 +0000255
256 for (auto OR : OpenRanges) {
257 // Copy OpenRanges to OutLocs, if not already present.
258 assert(OR.MI->isDebugValue());
259 DEBUG(dbgs() << "Add to OutLocs: "; OR.MI->dump(););
260 if (std::find_if(VLL.begin(), VLL.end(),
261 [&](const VarLoc &V) { return (OR == V); }) == VLL.end()) {
262 VLL.push_back(std::move(OR));
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000263 Changed = true;
Vikram TV859ad292015-12-16 11:09:48 +0000264 }
265 }
266 OpenRanges.clear();
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000267 return Changed;
Vikram TV859ad292015-12-16 11:09:48 +0000268}
269
270/// This routine creates OpenRanges and OutLocs.
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000271bool LiveDebugValues::transfer(MachineInstr &MI, VarLocList &OpenRanges,
Vikram TV859ad292015-12-16 11:09:48 +0000272 VarLocInMBB &OutLocs) {
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000273 bool Changed = false;
Vikram TV859ad292015-12-16 11:09:48 +0000274 transferDebugValue(MI, OpenRanges);
275 transferRegisterDef(MI, OpenRanges);
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000276 Changed = transferTerminatorInst(MI, OpenRanges, OutLocs);
277 return Changed;
Vikram TV859ad292015-12-16 11:09:48 +0000278}
279
280/// This routine joins the analysis results of all incoming edges in @MBB by
281/// inserting a new DBG_VALUE instruction at the start of the @MBB - if the same
282/// source variable in all the predecessors of @MBB reside in the same location.
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000283bool LiveDebugValues::join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs,
Vikram TV859ad292015-12-16 11:09:48 +0000284 VarLocInMBB &InLocs) {
285 DEBUG(dbgs() << "join MBB: " << MBB.getName() << "\n");
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000286 bool Changed = false;
Vikram TV859ad292015-12-16 11:09:48 +0000287
288 VarLocList InLocsT; // Temporary incoming locations.
289
290 // For all predecessors of this MBB, find the set of VarLocs that can be
291 // joined.
292 for (auto p : MBB.predecessors()) {
293 auto OL = OutLocs.find(p);
294 // Join is null in case of empty OutLocs from any of the pred.
295 if (OL == OutLocs.end())
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000296 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000297
298 // Just copy over the Out locs to incoming locs for the first predecessor.
299 if (p == *MBB.pred_begin()) {
300 InLocsT = OL->second;
301 continue;
302 }
303
304 // Join with this predecessor.
305 VarLocList &VLL = OL->second;
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000306 InLocsT.erase(
307 std::remove_if(InLocsT.begin(), InLocsT.end(), [&](VarLoc &ILT) {
308 return (std::find_if(VLL.begin(), VLL.end(), [&](const VarLoc &V) {
309 return (ILT == V);
310 }) == VLL.end());
311 }), InLocsT.end());
Vikram TV859ad292015-12-16 11:09:48 +0000312 }
313
314 if (InLocsT.empty())
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000315 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000316
Alexey Samsonov117b1042016-01-07 23:38:45 +0000317 VarLocList &ILL = InLocs[&MBB];
Vikram TV859ad292015-12-16 11:09:48 +0000318
319 // Insert DBG_VALUE instructions, if not already inserted.
320 for (auto ILT : InLocsT) {
321 if (std::find_if(ILL.begin(), ILL.end(), [&](const VarLoc &I) {
322 return (ILT == I);
323 }) == ILL.end()) {
324 // This VarLoc is not found in InLocs i.e. it is not yet inserted. So, a
325 // new range is started for the var from the mbb's beginning by inserting
326 // a new DBG_VALUE. transfer() will end this range however appropriate.
327 const MachineInstr *DMI = ILT.MI;
328 MachineInstr *MI =
329 BuildMI(MBB, MBB.instr_begin(), DMI->getDebugLoc(), DMI->getDesc(),
330 DMI->isIndirectDebugValue(), DMI->getOperand(0).getReg(), 0,
331 DMI->getDebugVariable(), DMI->getDebugExpression());
332 if (DMI->isIndirectDebugValue())
333 MI->getOperand(1).setImm(DMI->getOperand(1).getImm());
334 DEBUG(dbgs() << "Inserted: "; MI->dump(););
335 ++NumInserted;
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000336 Changed = true;
Vikram TV859ad292015-12-16 11:09:48 +0000337
338 VarLoc V(ILT.Var, MI);
339 ILL.push_back(std::move(V));
340 }
341 }
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000342 return Changed;
Vikram TV859ad292015-12-16 11:09:48 +0000343}
344
345/// Calculate the liveness information for the given machine function and
346/// extend ranges across basic blocks.
347bool LiveDebugValues::ExtendRanges(MachineFunction &MF) {
348
349 DEBUG(dbgs() << "\nDebug Range Extension\n");
350
351 bool Changed = false;
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000352 bool OLChanged = false;
353 bool MBBJoined = false;
Vikram TV859ad292015-12-16 11:09:48 +0000354
355 VarLocList OpenRanges; // Ranges that are open until end of bb.
356 VarLocInMBB OutLocs; // Ranges that exist beyond bb.
357 VarLocInMBB InLocs; // Ranges that are incoming after joining.
358
Daniel Berlin72560592016-01-10 18:08:32 +0000359 DenseMap<unsigned int, MachineBasicBlock *> OrderToBB;
360 DenseMap<MachineBasicBlock *, unsigned int> BBToOrder;
361 std::priority_queue<unsigned int, std::vector<unsigned int>,
362 std::greater<unsigned int>> Worklist;
363 std::priority_queue<unsigned int, std::vector<unsigned int>,
364 std::greater<unsigned int>> Pending;
Vikram TV859ad292015-12-16 11:09:48 +0000365 // Initialize every mbb with OutLocs.
366 for (auto &MBB : MF)
367 for (auto &MI : MBB)
368 transfer(MI, OpenRanges, OutLocs);
369 DEBUG(printVarLocInMBB(OutLocs, "OutLocs after initialization", dbgs()));
370
Daniel Berlin72560592016-01-10 18:08:32 +0000371 ReversePostOrderTraversal<MachineFunction *> RPOT(&MF);
372 unsigned int RPONumber = 0;
373 for (auto RI = RPOT.begin(), RE = RPOT.end(); RI != RE; ++RI) {
374 OrderToBB[RPONumber] = *RI;
375 BBToOrder[*RI] = RPONumber;
376 Worklist.push(RPONumber);
377 ++RPONumber;
378 }
Vikram TV859ad292015-12-16 11:09:48 +0000379
Daniel Berlin72560592016-01-10 18:08:32 +0000380 // This is a standard "union of predecessor outs" dataflow problem.
381 // To solve it, we perform join() and transfer() using the two worklist method
382 // until the ranges converge.
383 // Ranges have converged when both worklists are empty.
384 while (!Worklist.empty() || !Pending.empty()) {
385 // We track what is on the pending worklist to avoid inserting the same
386 // thing twice. We could avoid this with a custom priority queue, but this
387 // is probably not worth it.
388 SmallPtrSet<MachineBasicBlock *, 16> OnPending;
389 while (!Worklist.empty()) {
390 MachineBasicBlock *MBB = OrderToBB[Worklist.top()];
391 Worklist.pop();
392 MBBJoined = join(*MBB, OutLocs, InLocs);
Vikram TV859ad292015-12-16 11:09:48 +0000393
Daniel Berlin72560592016-01-10 18:08:32 +0000394 if (MBBJoined) {
395 MBBJoined = false;
396 Changed = true;
397 for (auto &MI : *MBB)
398 OLChanged |= transfer(MI, OpenRanges, OutLocs);
399 DEBUG(printVarLocInMBB(OutLocs, "OutLocs after propagating", dbgs()));
400 DEBUG(printVarLocInMBB(InLocs, "InLocs after propagating", dbgs()));
Vikram TV859ad292015-12-16 11:09:48 +0000401
Daniel Berlin72560592016-01-10 18:08:32 +0000402 if (OLChanged) {
403 OLChanged = false;
404 for (auto s : MBB->successors())
405 if (!OnPending.count(s)) {
406 OnPending.insert(s);
407 Pending.push(BBToOrder[s]);
408 }
409 }
Vikram TV859ad292015-12-16 11:09:48 +0000410 }
411 }
Daniel Berlin72560592016-01-10 18:08:32 +0000412 Worklist.swap(Pending);
413 // At this point, pending must be empty, since it was just the empty
414 // worklist
415 assert(Pending.empty() && "Pending should be empty");
Vikram TV859ad292015-12-16 11:09:48 +0000416 }
Daniel Berlin72560592016-01-10 18:08:32 +0000417
Vikram TV859ad292015-12-16 11:09:48 +0000418 DEBUG(printVarLocInMBB(OutLocs, "Final OutLocs", dbgs()));
419 DEBUG(printVarLocInMBB(InLocs, "Final InLocs", dbgs()));
420 return Changed;
421}
422
423bool LiveDebugValues::runOnMachineFunction(MachineFunction &MF) {
424 TRI = MF.getSubtarget().getRegisterInfo();
425 TII = MF.getSubtarget().getInstrInfo();
426
427 bool Changed = false;
428
429 Changed |= ExtendRanges(MF);
430
431 return Changed;
432}