blob: a920a0ba6f98f328186436aac1b0dc858cb87bed [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"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineFunctionPass.h"
25#include "llvm/CodeGen/MachineInstrBuilder.h"
26#include "llvm/CodeGen/Passes.h"
27#include "llvm/Support/CommandLine.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/raw_ostream.h"
30#include "llvm/Target/TargetInstrInfo.h"
31#include "llvm/Target/TargetRegisterInfo.h"
32#include "llvm/Target/TargetSubtargetInfo.h"
33#include <deque>
34#include <list>
35
36using namespace llvm;
37
38#define DEBUG_TYPE "live-debug-values"
39
40STATISTIC(NumInserted, "Number of DBG_VALUE instructions inserted");
41
42namespace {
43
44class LiveDebugValues : public MachineFunctionPass {
45
46private:
47 const TargetRegisterInfo *TRI;
48 const TargetInstrInfo *TII;
49
50 typedef std::pair<const DILocalVariable *, const DILocation *>
51 InlinedVariable;
52
53 /// A potentially inlined instance of a variable.
54 struct DebugVariable {
55 const DILocalVariable *Var;
56 const DILocation *InlinedAt;
57
58 DebugVariable(const DILocalVariable *_var, const DILocation *_inlinedAt)
59 : Var(_var), InlinedAt(_inlinedAt) {}
60
61 bool operator==(const DebugVariable &DV) const {
62 return (Var == DV.Var) && (InlinedAt == DV.InlinedAt);
63 }
64 };
65
66 /// Member variables and functions for Range Extension across basic blocks.
67 struct VarLoc {
68 DebugVariable Var;
69 const MachineInstr *MI; // MachineInstr should be a DBG_VALUE instr.
70
71 VarLoc(DebugVariable _var, const MachineInstr *_mi) : Var(_var), MI(_mi) {}
72
73 bool operator==(const VarLoc &V) const;
74 };
75
76 typedef std::list<VarLoc> VarLocList;
77 typedef SmallDenseMap<const MachineBasicBlock *, VarLocList> VarLocInMBB;
78
Vikram TV859ad292015-12-16 11:09:48 +000079 void transferDebugValue(MachineInstr &MI, VarLocList &OpenRanges);
80 void transferRegisterDef(MachineInstr &MI, VarLocList &OpenRanges);
Daniel Berlinca4d93a2016-01-10 03:25:42 +000081 bool transferTerminatorInst(MachineInstr &MI, VarLocList &OpenRanges,
Vikram TV859ad292015-12-16 11:09:48 +000082 VarLocInMBB &OutLocs);
Daniel Berlinca4d93a2016-01-10 03:25:42 +000083 bool transfer(MachineInstr &MI, VarLocList &OpenRanges, VarLocInMBB &OutLocs);
Vikram TV859ad292015-12-16 11:09:48 +000084
Daniel Berlinca4d93a2016-01-10 03:25:42 +000085 bool join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs, VarLocInMBB &InLocs);
Vikram TV859ad292015-12-16 11:09:48 +000086
87 bool ExtendRanges(MachineFunction &MF);
88
89public:
90 static char ID;
91
92 /// Default construct and initialize the pass.
93 LiveDebugValues();
94
95 /// Tell the pass manager which passes we depend on and what
96 /// information we preserve.
97 void getAnalysisUsage(AnalysisUsage &AU) const override;
98
99 /// Print to ostream with a message.
100 void printVarLocInMBB(const VarLocInMBB &V, const char *msg,
101 raw_ostream &Out) const;
102
103 /// Calculate the liveness information for the given machine function.
104 bool runOnMachineFunction(MachineFunction &MF) override;
105};
106} // namespace
107
108//===----------------------------------------------------------------------===//
109// Implementation
110//===----------------------------------------------------------------------===//
111
112char LiveDebugValues::ID = 0;
113char &llvm::LiveDebugValuesID = LiveDebugValues::ID;
114INITIALIZE_PASS(LiveDebugValues, "livedebugvalues", "Live DEBUG_VALUE analysis",
115 false, false)
116
117/// Default construct and initialize the pass.
118LiveDebugValues::LiveDebugValues() : MachineFunctionPass(ID) {
119 initializeLiveDebugValuesPass(*PassRegistry::getPassRegistry());
120}
121
122/// Tell the pass manager which passes we depend on and what information we
123/// preserve.
124void LiveDebugValues::getAnalysisUsage(AnalysisUsage &AU) const {
125 MachineFunctionPass::getAnalysisUsage(AU);
126}
127
128// \brief If @MI is a DBG_VALUE with debug value described by a defined
129// register, returns the number of this register. In the other case, returns 0.
130static unsigned isDescribedByReg(const MachineInstr &MI) {
131 assert(MI.isDebugValue());
132 assert(MI.getNumOperands() == 4);
133 // If location of variable is described using a register (directly or
134 // indirecltly), this register is always a first operand.
135 return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0;
136}
137
138// \brief This function takes two DBG_VALUE instructions and returns true
139// if their offsets are equal; otherwise returns false.
140static bool areOffsetsEqual(const MachineInstr &MI1, const MachineInstr &MI2) {
141 assert(MI1.isDebugValue());
142 assert(MI1.getNumOperands() == 4);
143
144 assert(MI2.isDebugValue());
145 assert(MI2.getNumOperands() == 4);
146
147 if (!MI1.isIndirectDebugValue() && !MI2.isIndirectDebugValue())
148 return true;
149
150 // Check if both MIs are indirect and they are equal.
151 if (MI1.isIndirectDebugValue() && MI2.isIndirectDebugValue())
152 return MI1.getOperand(1).getImm() == MI2.getOperand(1).getImm();
153
154 return false;
155}
156
157//===----------------------------------------------------------------------===//
158// Debug Range Extension Implementation
159//===----------------------------------------------------------------------===//
160
161void LiveDebugValues::printVarLocInMBB(const VarLocInMBB &V, const char *msg,
162 raw_ostream &Out) const {
163 Out << "Printing " << msg << ":\n";
164 for (const auto &L : V) {
165 Out << "MBB: " << L.first->getName() << ":\n";
166 for (const auto &VLL : L.second) {
167 Out << " Var: " << VLL.Var.Var->getName();
168 Out << " MI: ";
169 (*VLL.MI).dump();
170 Out << "\n";
171 }
172 }
173 Out << "\n";
174}
175
176bool LiveDebugValues::VarLoc::operator==(const VarLoc &V) const {
177 return (Var == V.Var) && (isDescribedByReg(*MI) == isDescribedByReg(*V.MI)) &&
178 (areOffsetsEqual(*MI, *V.MI));
179}
180
181/// End all previous ranges related to @MI and start a new range from @MI
182/// if it is a DBG_VALUE instr.
183void LiveDebugValues::transferDebugValue(MachineInstr &MI,
184 VarLocList &OpenRanges) {
185 if (!MI.isDebugValue())
186 return;
187 const DILocalVariable *RawVar = MI.getDebugVariable();
188 assert(RawVar->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
189 "Expected inlined-at fields to agree");
190 DebugVariable Var(RawVar, MI.getDebugLoc()->getInlinedAt());
191
192 // End all previous ranges of Var.
193 OpenRanges.erase(
194 std::remove_if(OpenRanges.begin(), OpenRanges.end(),
195 [&](const VarLoc &V) { return (Var == V.Var); }),
196 OpenRanges.end());
197
198 // Add Var to OpenRanges from this DBG_VALUE.
199 // TODO: Currently handles DBG_VALUE which has only reg as location.
200 if (isDescribedByReg(MI)) {
201 VarLoc V(Var, &MI);
202 OpenRanges.push_back(std::move(V));
203 }
204}
205
206/// A definition of a register may mark the end of a range.
207void LiveDebugValues::transferRegisterDef(MachineInstr &MI,
208 VarLocList &OpenRanges) {
209 for (const MachineOperand &MO : MI.operands()) {
210 if (!(MO.isReg() && MO.isDef() && MO.getReg() &&
211 TRI->isPhysicalRegister(MO.getReg())))
212 continue;
213 // Remove ranges of all aliased registers.
214 for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI)
215 OpenRanges.erase(std::remove_if(OpenRanges.begin(), OpenRanges.end(),
216 [&](const VarLoc &V) {
217 return (*RAI ==
218 isDescribedByReg(*V.MI));
219 }),
220 OpenRanges.end());
221 }
222}
223
224/// Terminate all open ranges at the end of the current basic block.
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000225bool LiveDebugValues::transferTerminatorInst(MachineInstr &MI,
Vikram TV859ad292015-12-16 11:09:48 +0000226 VarLocList &OpenRanges,
227 VarLocInMBB &OutLocs) {
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000228 bool Changed = false;
Vikram TV859ad292015-12-16 11:09:48 +0000229 const MachineBasicBlock *CurMBB = MI.getParent();
230 if (!(MI.isTerminator() || (&MI == &CurMBB->instr_back())))
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000231 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000232
233 if (OpenRanges.empty())
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000234 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000235
Alexey Samsonov117b1042016-01-07 23:38:45 +0000236 VarLocList &VLL = OutLocs[CurMBB];
Vikram TV859ad292015-12-16 11:09:48 +0000237
238 for (auto OR : OpenRanges) {
239 // Copy OpenRanges to OutLocs, if not already present.
240 assert(OR.MI->isDebugValue());
241 DEBUG(dbgs() << "Add to OutLocs: "; OR.MI->dump(););
242 if (std::find_if(VLL.begin(), VLL.end(),
243 [&](const VarLoc &V) { return (OR == V); }) == VLL.end()) {
244 VLL.push_back(std::move(OR));
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000245 Changed = true;
Vikram TV859ad292015-12-16 11:09:48 +0000246 }
247 }
248 OpenRanges.clear();
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000249 return Changed;
Vikram TV859ad292015-12-16 11:09:48 +0000250}
251
252/// This routine creates OpenRanges and OutLocs.
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000253bool LiveDebugValues::transfer(MachineInstr &MI, VarLocList &OpenRanges,
Vikram TV859ad292015-12-16 11:09:48 +0000254 VarLocInMBB &OutLocs) {
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000255 bool Changed = false;
Vikram TV859ad292015-12-16 11:09:48 +0000256 transferDebugValue(MI, OpenRanges);
257 transferRegisterDef(MI, OpenRanges);
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000258 Changed = transferTerminatorInst(MI, OpenRanges, OutLocs);
259 return Changed;
Vikram TV859ad292015-12-16 11:09:48 +0000260}
261
262/// This routine joins the analysis results of all incoming edges in @MBB by
263/// inserting a new DBG_VALUE instruction at the start of the @MBB - if the same
264/// source variable in all the predecessors of @MBB reside in the same location.
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000265bool LiveDebugValues::join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs,
Vikram TV859ad292015-12-16 11:09:48 +0000266 VarLocInMBB &InLocs) {
267 DEBUG(dbgs() << "join MBB: " << MBB.getName() << "\n");
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000268 bool Changed = false;
Vikram TV859ad292015-12-16 11:09:48 +0000269
270 VarLocList InLocsT; // Temporary incoming locations.
271
272 // For all predecessors of this MBB, find the set of VarLocs that can be
273 // joined.
274 for (auto p : MBB.predecessors()) {
275 auto OL = OutLocs.find(p);
276 // Join is null in case of empty OutLocs from any of the pred.
277 if (OL == OutLocs.end())
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000278 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000279
280 // Just copy over the Out locs to incoming locs for the first predecessor.
281 if (p == *MBB.pred_begin()) {
282 InLocsT = OL->second;
283 continue;
284 }
285
286 // Join with this predecessor.
287 VarLocList &VLL = OL->second;
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000288 InLocsT.erase(
289 std::remove_if(InLocsT.begin(), InLocsT.end(), [&](VarLoc &ILT) {
290 return (std::find_if(VLL.begin(), VLL.end(), [&](const VarLoc &V) {
291 return (ILT == V);
292 }) == VLL.end());
293 }), InLocsT.end());
Vikram TV859ad292015-12-16 11:09:48 +0000294 }
295
296 if (InLocsT.empty())
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000297 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000298
Alexey Samsonov117b1042016-01-07 23:38:45 +0000299 VarLocList &ILL = InLocs[&MBB];
Vikram TV859ad292015-12-16 11:09:48 +0000300
301 // Insert DBG_VALUE instructions, if not already inserted.
302 for (auto ILT : InLocsT) {
303 if (std::find_if(ILL.begin(), ILL.end(), [&](const VarLoc &I) {
304 return (ILT == I);
305 }) == ILL.end()) {
306 // This VarLoc is not found in InLocs i.e. it is not yet inserted. So, a
307 // new range is started for the var from the mbb's beginning by inserting
308 // a new DBG_VALUE. transfer() will end this range however appropriate.
309 const MachineInstr *DMI = ILT.MI;
310 MachineInstr *MI =
311 BuildMI(MBB, MBB.instr_begin(), DMI->getDebugLoc(), DMI->getDesc(),
312 DMI->isIndirectDebugValue(), DMI->getOperand(0).getReg(), 0,
313 DMI->getDebugVariable(), DMI->getDebugExpression());
314 if (DMI->isIndirectDebugValue())
315 MI->getOperand(1).setImm(DMI->getOperand(1).getImm());
316 DEBUG(dbgs() << "Inserted: "; MI->dump(););
317 ++NumInserted;
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000318 Changed = true;
Vikram TV859ad292015-12-16 11:09:48 +0000319
320 VarLoc V(ILT.Var, MI);
321 ILL.push_back(std::move(V));
322 }
323 }
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000324 return Changed;
Vikram TV859ad292015-12-16 11:09:48 +0000325}
326
327/// Calculate the liveness information for the given machine function and
328/// extend ranges across basic blocks.
329bool LiveDebugValues::ExtendRanges(MachineFunction &MF) {
330
331 DEBUG(dbgs() << "\nDebug Range Extension\n");
332
333 bool Changed = false;
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000334 bool OLChanged = false;
335 bool MBBJoined = false;
Vikram TV859ad292015-12-16 11:09:48 +0000336
337 VarLocList OpenRanges; // Ranges that are open until end of bb.
338 VarLocInMBB OutLocs; // Ranges that exist beyond bb.
339 VarLocInMBB InLocs; // Ranges that are incoming after joining.
340
341 std::deque<MachineBasicBlock *> BBWorklist;
342
343 // Initialize every mbb with OutLocs.
344 for (auto &MBB : MF)
345 for (auto &MI : MBB)
346 transfer(MI, OpenRanges, OutLocs);
347 DEBUG(printVarLocInMBB(OutLocs, "OutLocs after initialization", dbgs()));
348
349 // Construct a worklist of MBBs.
350 for (auto &MBB : MF)
351 BBWorklist.push_back(&MBB);
352
353 // Perform join() and transfer() using the worklist until the ranges converge
354 // Ranges have converged when the worklist is empty.
355 while (!BBWorklist.empty()) {
356 MachineBasicBlock *MBB = BBWorklist.front();
357 BBWorklist.pop_front();
358
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000359 MBBJoined = join(*MBB, OutLocs, InLocs);
Vikram TV859ad292015-12-16 11:09:48 +0000360
361 if (MBBJoined) {
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000362 MBBJoined = false;
Vikram TV859ad292015-12-16 11:09:48 +0000363 Changed = true;
364 for (auto &MI : *MBB)
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000365 OLChanged |= transfer(MI, OpenRanges, OutLocs);
Vikram TV859ad292015-12-16 11:09:48 +0000366 DEBUG(printVarLocInMBB(OutLocs, "OutLocs after propagating", dbgs()));
367 DEBUG(printVarLocInMBB(InLocs, "InLocs after propagating", dbgs()));
368
369 if (OLChanged) {
370 OLChanged = false;
371 for (auto s : MBB->successors())
372 if (std::find(BBWorklist.begin(), BBWorklist.end(), s) ==
373 BBWorklist.end()) // add if not already present.
374 BBWorklist.push_back(s);
375 }
376 }
377 }
378 DEBUG(printVarLocInMBB(OutLocs, "Final OutLocs", dbgs()));
379 DEBUG(printVarLocInMBB(InLocs, "Final InLocs", dbgs()));
380 return Changed;
381}
382
383bool LiveDebugValues::runOnMachineFunction(MachineFunction &MF) {
384 TRI = MF.getSubtarget().getRegisterInfo();
385 TII = MF.getSubtarget().getInstrInfo();
386
387 bool Changed = false;
388
389 Changed |= ExtendRanges(MF);
390
391 return Changed;
392}