Bug Summary

File:lib/CodeGen/LiveDebugValues.cpp
Warning:line 529, column 9
Value stored to 'MBBJoined' is never read

Annotated Source Code

1//===------ 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/PostOrderIterator.h"
22#include "llvm/ADT/SmallPtrSet.h"
23#include "llvm/ADT/SparseBitVector.h"
24#include "llvm/ADT/Statistic.h"
25#include "llvm/ADT/UniqueVector.h"
26#include "llvm/CodeGen/LexicalScopes.h"
27#include "llvm/CodeGen/MachineFunction.h"
28#include "llvm/CodeGen/MachineFunctionPass.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
30#include "llvm/CodeGen/Passes.h"
31#include "llvm/IR/DebugInfo.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/Support/raw_ostream.h"
34#include "llvm/Target/TargetInstrInfo.h"
35#include "llvm/Target/TargetLowering.h"
36#include "llvm/Target/TargetRegisterInfo.h"
37#include "llvm/Target/TargetSubtargetInfo.h"
38#include <list>
39#include <queue>
40
41using namespace llvm;
42
43#define DEBUG_TYPE"live-debug-values" "live-debug-values"
44
45STATISTIC(NumInserted, "Number of DBG_VALUE instructions inserted")static llvm::Statistic NumInserted = {"live-debug-values", "NumInserted"
, "Number of DBG_VALUE instructions inserted", {0}, false}
;
46
47namespace {
48
49// \brief If @MI is a DBG_VALUE with debug value described by a defined
50// register, returns the number of this register. In the other case, returns 0.
51static unsigned isDbgValueDescribedByReg(const MachineInstr &MI) {
52 assert(MI.isDebugValue() && "expected a DBG_VALUE")((MI.isDebugValue() && "expected a DBG_VALUE") ? static_cast
<void> (0) : __assert_fail ("MI.isDebugValue() && \"expected a DBG_VALUE\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn295052/lib/CodeGen/LiveDebugValues.cpp"
, 52, __PRETTY_FUNCTION__))
;
53 assert(MI.getNumOperands() == 4 && "malformed DBG_VALUE")((MI.getNumOperands() == 4 && "malformed DBG_VALUE") ?
static_cast<void> (0) : __assert_fail ("MI.getNumOperands() == 4 && \"malformed DBG_VALUE\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn295052/lib/CodeGen/LiveDebugValues.cpp"
, 53, __PRETTY_FUNCTION__))
;
54 // If location of variable is described using a register (directly
55 // or indirectly), this register is always a first operand.
56 return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0;
57}
58
59class LiveDebugValues : public MachineFunctionPass {
60
61private:
62 const TargetRegisterInfo *TRI;
63 const TargetInstrInfo *TII;
64 LexicalScopes LS;
65
66 /// Keeps track of lexical scopes associated with a user value's source
67 /// location.
68 class UserValueScopes {
69 DebugLoc DL;
70 LexicalScopes &LS;
71 SmallPtrSet<const MachineBasicBlock *, 4> LBlocks;
72
73 public:
74 UserValueScopes(DebugLoc D, LexicalScopes &L) : DL(std::move(D)), LS(L) {}
75
76 /// Return true if current scope dominates at least one machine
77 /// instruction in a given machine basic block.
78 bool dominates(MachineBasicBlock *MBB) {
79 if (LBlocks.empty())
80 LS.getMachineBasicBlocks(DL, LBlocks);
81 return LBlocks.count(MBB) != 0 || LS.dominates(DL, MBB);
82 }
83 };
84
85 /// Based on std::pair so it can be used as an index into a DenseMap.
86 typedef std::pair<const DILocalVariable *, const DILocation *>
87 DebugVariableBase;
88 /// A potentially inlined instance of a variable.
89 struct DebugVariable : public DebugVariableBase {
90 DebugVariable(const DILocalVariable *Var, const DILocation *InlinedAt)
91 : DebugVariableBase(Var, InlinedAt) {}
92
93 const DILocalVariable *getVar() const { return this->first; };
94 const DILocation *getInlinedAt() const { return this->second; };
95
96 bool operator<(const DebugVariable &DV) const {
97 if (getVar() == DV.getVar())
98 return getInlinedAt() < DV.getInlinedAt();
99 return getVar() < DV.getVar();
100 }
101 };
102
103 /// A pair of debug variable and value location.
104 struct VarLoc {
105 const DebugVariable Var;
106 const MachineInstr &MI; ///< Only used for cloning a new DBG_VALUE.
107 mutable UserValueScopes UVS;
108 enum { InvalidKind = 0, RegisterKind } Kind;
109
110 /// The value location. Stored separately to avoid repeatedly
111 /// extracting it from MI.
112 union {
113 struct {
114 uint32_t RegNo;
115 uint32_t Offset;
116 } RegisterLoc;
117 uint64_t Hash;
118 } Loc;
119
120 VarLoc(const MachineInstr &MI, LexicalScopes &LS)
121 : Var(MI.getDebugVariable(), MI.getDebugLoc()->getInlinedAt()), MI(MI),
122 UVS(MI.getDebugLoc(), LS), Kind(InvalidKind) {
123 static_assert((sizeof(Loc) == sizeof(uint64_t)),
124 "hash does not cover all members of Loc");
125 assert(MI.isDebugValue() && "not a DBG_VALUE")((MI.isDebugValue() && "not a DBG_VALUE") ? static_cast
<void> (0) : __assert_fail ("MI.isDebugValue() && \"not a DBG_VALUE\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn295052/lib/CodeGen/LiveDebugValues.cpp"
, 125, __PRETTY_FUNCTION__))
;
126 assert(MI.getNumOperands() == 4 && "malformed DBG_VALUE")((MI.getNumOperands() == 4 && "malformed DBG_VALUE") ?
static_cast<void> (0) : __assert_fail ("MI.getNumOperands() == 4 && \"malformed DBG_VALUE\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn295052/lib/CodeGen/LiveDebugValues.cpp"
, 126, __PRETTY_FUNCTION__))
;
127 if (int RegNo = isDbgValueDescribedByReg(MI)) {
128 Kind = RegisterKind;
129 Loc.RegisterLoc.RegNo = RegNo;
130 uint64_t Offset =
131 MI.isIndirectDebugValue() ? MI.getOperand(1).getImm() : 0;
132 // We don't support offsets larger than 4GiB here. They are
133 // slated to be replaced with DIExpressions anyway.
134 if (Offset >= (1ULL << 32))
135 Kind = InvalidKind;
136 else
137 Loc.RegisterLoc.Offset = Offset;
138 }
139 }
140
141 /// If this variable is described by a register, return it,
142 /// otherwise return 0.
143 unsigned isDescribedByReg() const {
144 if (Kind == RegisterKind)
145 return Loc.RegisterLoc.RegNo;
146 return 0;
147 }
148
149 /// Determine whether the lexical scope of this value's debug location
150 /// dominates MBB.
151 bool dominates(MachineBasicBlock &MBB) const { return UVS.dominates(&MBB); }
152
153#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
154 LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void dump() const { MI.dump(); }
155#endif
156
157 bool operator==(const VarLoc &Other) const {
158 return Var == Other.Var && Loc.Hash == Other.Loc.Hash;
159 }
160
161 /// This operator guarantees that VarLocs are sorted by Variable first.
162 bool operator<(const VarLoc &Other) const {
163 if (Var == Other.Var)
164 return Loc.Hash < Other.Loc.Hash;
165 return Var < Other.Var;
166 }
167 };
168
169 typedef UniqueVector<VarLoc> VarLocMap;
170 typedef SparseBitVector<> VarLocSet;
171 typedef SmallDenseMap<const MachineBasicBlock *, VarLocSet> VarLocInMBB;
172
173 /// This holds the working set of currently open ranges. For fast
174 /// access, this is done both as a set of VarLocIDs, and a map of
175 /// DebugVariable to recent VarLocID. Note that a DBG_VALUE ends all
176 /// previous open ranges for the same variable.
177 class OpenRangesSet {
178 VarLocSet VarLocs;
179 SmallDenseMap<DebugVariableBase, unsigned, 8> Vars;
180
181 public:
182 const VarLocSet &getVarLocs() const { return VarLocs; }
183
184 /// Terminate all open ranges for Var by removing it from the set.
185 void erase(DebugVariable Var) {
186 auto It = Vars.find(Var);
187 if (It != Vars.end()) {
188 unsigned ID = It->second;
189 VarLocs.reset(ID);
190 Vars.erase(It);
191 }
192 }
193
194 /// Terminate all open ranges listed in \c KillSet by removing
195 /// them from the set.
196 void erase(const VarLocSet &KillSet, const VarLocMap &VarLocIDs) {
197 VarLocs.intersectWithComplement(KillSet);
198 for (unsigned ID : KillSet)
199 Vars.erase(VarLocIDs[ID].Var);
200 }
201
202 /// Insert a new range into the set.
203 void insert(unsigned VarLocID, DebugVariableBase Var) {
204 VarLocs.set(VarLocID);
205 Vars.insert({Var, VarLocID});
206 }
207
208 /// Empty the set.
209 void clear() {
210 VarLocs.clear();
211 Vars.clear();
212 }
213
214 /// Return whether the set is empty or not.
215 bool empty() const {
216 assert(Vars.empty() == VarLocs.empty() && "open ranges are inconsistent")((Vars.empty() == VarLocs.empty() && "open ranges are inconsistent"
) ? static_cast<void> (0) : __assert_fail ("Vars.empty() == VarLocs.empty() && \"open ranges are inconsistent\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn295052/lib/CodeGen/LiveDebugValues.cpp"
, 216, __PRETTY_FUNCTION__))
;
217 return VarLocs.empty();
218 }
219 };
220
221 void transferDebugValue(const MachineInstr &MI, OpenRangesSet &OpenRanges,
222 VarLocMap &VarLocIDs);
223 void transferRegisterDef(MachineInstr &MI, OpenRangesSet &OpenRanges,
224 const VarLocMap &VarLocIDs);
225 bool transferTerminatorInst(MachineInstr &MI, OpenRangesSet &OpenRanges,
226 VarLocInMBB &OutLocs, const VarLocMap &VarLocIDs);
227 bool transfer(MachineInstr &MI, OpenRangesSet &OpenRanges,
228 VarLocInMBB &OutLocs, VarLocMap &VarLocIDs);
229
230 bool join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs, VarLocInMBB &InLocs,
231 const VarLocMap &VarLocIDs,
232 SmallPtrSet<const MachineBasicBlock *, 16> &Visited);
233
234 bool ExtendRanges(MachineFunction &MF);
235
236public:
237 static char ID;
238
239 /// Default construct and initialize the pass.
240 LiveDebugValues();
241
242 /// Tell the pass manager which passes we depend on and what
243 /// information we preserve.
244 void getAnalysisUsage(AnalysisUsage &AU) const override;
245
246 MachineFunctionProperties getRequiredProperties() const override {
247 return MachineFunctionProperties().set(
248 MachineFunctionProperties::Property::NoVRegs);
249 }
250
251 /// Print to ostream with a message.
252 void printVarLocInMBB(const MachineFunction &MF, const VarLocInMBB &V,
253 const VarLocMap &VarLocIDs, const char *msg,
254 raw_ostream &Out) const;
255
256 /// Calculate the liveness information for the given machine function.
257 bool runOnMachineFunction(MachineFunction &MF) override;
258};
259
260} // namespace
261
262//===----------------------------------------------------------------------===//
263// Implementation
264//===----------------------------------------------------------------------===//
265
266char LiveDebugValues::ID = 0;
267char &llvm::LiveDebugValuesID = LiveDebugValues::ID;
268INITIALIZE_PASS(LiveDebugValues, "livedebugvalues", "Live DEBUG_VALUE analysis",static void *initializeLiveDebugValuesPassOnce(PassRegistry &
Registry) { PassInfo *PI = new PassInfo( "Live DEBUG_VALUE analysis"
, "livedebugvalues", &LiveDebugValues::ID, PassInfo::NormalCtor_t
(callDefaultCtor<LiveDebugValues>), false, false); Registry
.registerPass(*PI, true); return PI; } static llvm::once_flag
InitializeLiveDebugValuesPassFlag; void llvm::initializeLiveDebugValuesPass
(PassRegistry &Registry) { llvm::call_once(InitializeLiveDebugValuesPassFlag
, initializeLiveDebugValuesPassOnce, std::ref(Registry)); }
269 false, false)static void *initializeLiveDebugValuesPassOnce(PassRegistry &
Registry) { PassInfo *PI = new PassInfo( "Live DEBUG_VALUE analysis"
, "livedebugvalues", &LiveDebugValues::ID, PassInfo::NormalCtor_t
(callDefaultCtor<LiveDebugValues>), false, false); Registry
.registerPass(*PI, true); return PI; } static llvm::once_flag
InitializeLiveDebugValuesPassFlag; void llvm::initializeLiveDebugValuesPass
(PassRegistry &Registry) { llvm::call_once(InitializeLiveDebugValuesPassFlag
, initializeLiveDebugValuesPassOnce, std::ref(Registry)); }
270
271/// Default construct and initialize the pass.
272LiveDebugValues::LiveDebugValues() : MachineFunctionPass(ID) {
273 initializeLiveDebugValuesPass(*PassRegistry::getPassRegistry());
274}
275
276/// Tell the pass manager which passes we depend on and what information we
277/// preserve.
278void LiveDebugValues::getAnalysisUsage(AnalysisUsage &AU) const {
279 AU.setPreservesCFG();
280 MachineFunctionPass::getAnalysisUsage(AU);
281}
282
283//===----------------------------------------------------------------------===//
284// Debug Range Extension Implementation
285//===----------------------------------------------------------------------===//
286
287#ifndef NDEBUG
288void LiveDebugValues::printVarLocInMBB(const MachineFunction &MF,
289 const VarLocInMBB &V,
290 const VarLocMap &VarLocIDs,
291 const char *msg,
292 raw_ostream &Out) const {
293 Out << '\n' << msg << '\n';
294 for (const MachineBasicBlock &BB : MF) {
295 const auto &L = V.lookup(&BB);
296 Out << "MBB: " << BB.getName() << ":\n";
297 for (unsigned VLL : L) {
298 const VarLoc &VL = VarLocIDs[VLL];
299 Out << " Var: " << VL.Var.getVar()->getName();
300 Out << " MI: ";
301 VL.dump();
302 }
303 }
304 Out << "\n";
305}
306#endif
307
308/// End all previous ranges related to @MI and start a new range from @MI
309/// if it is a DBG_VALUE instr.
310void LiveDebugValues::transferDebugValue(const MachineInstr &MI,
311 OpenRangesSet &OpenRanges,
312 VarLocMap &VarLocIDs) {
313 if (!MI.isDebugValue())
314 return;
315 const DILocalVariable *Var = MI.getDebugVariable();
316 const DILocation *DebugLoc = MI.getDebugLoc();
317 const DILocation *InlinedAt = DebugLoc->getInlinedAt();
318 assert(Var->isValidLocationForIntrinsic(DebugLoc) &&((Var->isValidLocationForIntrinsic(DebugLoc) && "Expected inlined-at fields to agree"
) ? static_cast<void> (0) : __assert_fail ("Var->isValidLocationForIntrinsic(DebugLoc) && \"Expected inlined-at fields to agree\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn295052/lib/CodeGen/LiveDebugValues.cpp"
, 319, __PRETTY_FUNCTION__))
319 "Expected inlined-at fields to agree")((Var->isValidLocationForIntrinsic(DebugLoc) && "Expected inlined-at fields to agree"
) ? static_cast<void> (0) : __assert_fail ("Var->isValidLocationForIntrinsic(DebugLoc) && \"Expected inlined-at fields to agree\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn295052/lib/CodeGen/LiveDebugValues.cpp"
, 319, __PRETTY_FUNCTION__))
;
320
321 // End all previous ranges of Var.
322 DebugVariable V(Var, InlinedAt);
323 OpenRanges.erase(V);
324
325 // Add the VarLoc to OpenRanges from this DBG_VALUE.
326 // TODO: Currently handles DBG_VALUE which has only reg as location.
327 if (isDbgValueDescribedByReg(MI)) {
328 VarLoc VL(MI, LS);
329 unsigned ID = VarLocIDs.insert(VL);
330 OpenRanges.insert(ID, VL.Var);
331 }
332}
333
334/// A definition of a register may mark the end of a range.
335void LiveDebugValues::transferRegisterDef(MachineInstr &MI,
336 OpenRangesSet &OpenRanges,
337 const VarLocMap &VarLocIDs) {
338 MachineFunction *MF = MI.getParent()->getParent();
339 const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
340 unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
341 SparseBitVector<> KillSet;
342 for (const MachineOperand &MO : MI.operands()) {
343 if (MO.isReg() && MO.isDef() && MO.getReg() &&
344 TRI->isPhysicalRegister(MO.getReg())) {
345 // Remove ranges of all aliased registers.
346 for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI)
347 for (unsigned ID : OpenRanges.getVarLocs())
348 if (VarLocIDs[ID].isDescribedByReg() == *RAI)
349 KillSet.set(ID);
350 } else if (MO.isRegMask()) {
351 // Remove ranges of all clobbered registers. Register masks don't usually
352 // list SP as preserved. While the debug info may be off for an
353 // instruction or two around callee-cleanup calls, transferring the
354 // DEBUG_VALUE across the call is still a better user experience.
355 for (unsigned ID : OpenRanges.getVarLocs()) {
356 unsigned Reg = VarLocIDs[ID].isDescribedByReg();
357 if (Reg && Reg != SP && MO.clobbersPhysReg(Reg))
358 KillSet.set(ID);
359 }
360 }
361 }
362 OpenRanges.erase(KillSet, VarLocIDs);
363}
364
365/// Terminate all open ranges at the end of the current basic block.
366bool LiveDebugValues::transferTerminatorInst(MachineInstr &MI,
367 OpenRangesSet &OpenRanges,
368 VarLocInMBB &OutLocs,
369 const VarLocMap &VarLocIDs) {
370 bool Changed = false;
371 const MachineBasicBlock *CurMBB = MI.getParent();
372 if (!(MI.isTerminator() || (&MI == &CurMBB->instr_back())))
373 return false;
374
375 if (OpenRanges.empty())
376 return false;
377
378 DEBUG(for (unsigned ID : OpenRanges.getVarLocs()) {do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("live-debug-values")) { for (unsigned ID : OpenRanges.getVarLocs
()) { dbgs() << "Add to OutLocs: "; VarLocIDs[ID].dump(
); }; } } while (false)
379 // Copy OpenRanges to OutLocs, if not already present.do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("live-debug-values")) { for (unsigned ID : OpenRanges.getVarLocs
()) { dbgs() << "Add to OutLocs: "; VarLocIDs[ID].dump(
); }; } } while (false)
380 dbgs() << "Add to OutLocs: "; VarLocIDs[ID].dump();do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("live-debug-values")) { for (unsigned ID : OpenRanges.getVarLocs
()) { dbgs() << "Add to OutLocs: "; VarLocIDs[ID].dump(
); }; } } while (false)
381 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("live-debug-values")) { for (unsigned ID : OpenRanges.getVarLocs
()) { dbgs() << "Add to OutLocs: "; VarLocIDs[ID].dump(
); }; } } while (false)
;
382 VarLocSet &VLS = OutLocs[CurMBB];
383 Changed = VLS |= OpenRanges.getVarLocs();
384 OpenRanges.clear();
385 return Changed;
386}
387
388/// This routine creates OpenRanges and OutLocs.
389bool LiveDebugValues::transfer(MachineInstr &MI, OpenRangesSet &OpenRanges,
390 VarLocInMBB &OutLocs, VarLocMap &VarLocIDs) {
391 bool Changed = false;
392 transferDebugValue(MI, OpenRanges, VarLocIDs);
393 transferRegisterDef(MI, OpenRanges, VarLocIDs);
394 Changed = transferTerminatorInst(MI, OpenRanges, OutLocs, VarLocIDs);
395 return Changed;
396}
397
398/// This routine joins the analysis results of all incoming edges in @MBB by
399/// inserting a new DBG_VALUE instruction at the start of the @MBB - if the same
400/// source variable in all the predecessors of @MBB reside in the same location.
401bool LiveDebugValues::join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs,
402 VarLocInMBB &InLocs, const VarLocMap &VarLocIDs,
403 SmallPtrSet<const MachineBasicBlock *, 16> &Visited) {
404 DEBUG(dbgs() << "join MBB: " << MBB.getName() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("live-debug-values")) { dbgs() << "join MBB: " <<
MBB.getName() << "\n"; } } while (false)
;
405 bool Changed = false;
406
407 VarLocSet InLocsT; // Temporary incoming locations.
408
409 // For all predecessors of this MBB, find the set of VarLocs that
410 // can be joined.
411 int NumVisited = 0;
412 for (auto p : MBB.predecessors()) {
413 // Ignore unvisited predecessor blocks. As we are processing
414 // the blocks in reverse post-order any unvisited block can
415 // be considered to not remove any incoming values.
416 if (!Visited.count(p))
417 continue;
418 auto OL = OutLocs.find(p);
419 // Join is null in case of empty OutLocs from any of the pred.
420 if (OL == OutLocs.end())
421 return false;
422
423 // Just copy over the Out locs to incoming locs for the first visited
424 // predecessor, and for all other predecessors join the Out locs.
425 if (!NumVisited)
426 InLocsT = OL->second;
427 else
428 InLocsT &= OL->second;
429 NumVisited++;
430 }
431
432 // Filter out DBG_VALUES that are out of scope.
433 VarLocSet KillSet;
434 for (auto ID : InLocsT)
435 if (!VarLocIDs[ID].dominates(MBB))
436 KillSet.set(ID);
437 InLocsT.intersectWithComplement(KillSet);
438
439 // As we are processing blocks in reverse post-order we
440 // should have processed at least one predecessor, unless it
441 // is the entry block which has no predecessor.
442 assert((NumVisited || MBB.pred_empty()) &&(((NumVisited || MBB.pred_empty()) && "Should have processed at least one predecessor"
) ? static_cast<void> (0) : __assert_fail ("(NumVisited || MBB.pred_empty()) && \"Should have processed at least one predecessor\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn295052/lib/CodeGen/LiveDebugValues.cpp"
, 443, __PRETTY_FUNCTION__))
443 "Should have processed at least one predecessor")(((NumVisited || MBB.pred_empty()) && "Should have processed at least one predecessor"
) ? static_cast<void> (0) : __assert_fail ("(NumVisited || MBB.pred_empty()) && \"Should have processed at least one predecessor\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn295052/lib/CodeGen/LiveDebugValues.cpp"
, 443, __PRETTY_FUNCTION__))
;
444 if (InLocsT.empty())
445 return false;
446
447 VarLocSet &ILS = InLocs[&MBB];
448
449 // Insert DBG_VALUE instructions, if not already inserted.
450 VarLocSet Diff = InLocsT;
451 Diff.intersectWithComplement(ILS);
452 for (auto ID : Diff) {
453 // This VarLoc is not found in InLocs i.e. it is not yet inserted. So, a
454 // new range is started for the var from the mbb's beginning by inserting
455 // a new DBG_VALUE. transfer() will end this range however appropriate.
456 const VarLoc &DiffIt = VarLocIDs[ID];
457 const MachineInstr *DMI = &DiffIt.MI;
458 MachineInstr *MI =
459 BuildMI(MBB, MBB.instr_begin(), DMI->getDebugLoc(), DMI->getDesc(),
460 DMI->isIndirectDebugValue(), DMI->getOperand(0).getReg(), 0,
461 DMI->getDebugVariable(), DMI->getDebugExpression());
462 if (DMI->isIndirectDebugValue())
463 MI->getOperand(1).setImm(DMI->getOperand(1).getImm());
464 DEBUG(dbgs() << "Inserted: "; MI->dump();)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("live-debug-values")) { dbgs() << "Inserted: "; MI->
dump();; } } while (false)
;
465 ILS.set(ID);
466 ++NumInserted;
467 Changed = true;
468 }
469 return Changed;
470}
471
472/// Calculate the liveness information for the given machine function and
473/// extend ranges across basic blocks.
474bool LiveDebugValues::ExtendRanges(MachineFunction &MF) {
475
476 DEBUG(dbgs() << "\nDebug Range Extension\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("live-debug-values")) { dbgs() << "\nDebug Range Extension\n"
; } } while (false)
;
477
478 bool Changed = false;
479 bool OLChanged = false;
480 bool MBBJoined = false;
481
482 VarLocMap VarLocIDs; // Map VarLoc<>unique ID for use in bitvectors.
483 OpenRangesSet OpenRanges; // Ranges that are open until end of bb.
484 VarLocInMBB OutLocs; // Ranges that exist beyond bb.
485 VarLocInMBB InLocs; // Ranges that are incoming after joining.
486
487 DenseMap<unsigned int, MachineBasicBlock *> OrderToBB;
488 DenseMap<MachineBasicBlock *, unsigned int> BBToOrder;
489 std::priority_queue<unsigned int, std::vector<unsigned int>,
490 std::greater<unsigned int>>
491 Worklist;
492 std::priority_queue<unsigned int, std::vector<unsigned int>,
493 std::greater<unsigned int>>
494 Pending;
495
496 // Initialize every mbb with OutLocs.
497 for (auto &MBB : MF)
498 for (auto &MI : MBB)
499 transfer(MI, OpenRanges, OutLocs, VarLocIDs);
500
501 DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, "OutLocs after initialization",do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("live-debug-values")) { printVarLocInMBB(MF, OutLocs, VarLocIDs
, "OutLocs after initialization", dbgs()); } } while (false)
502 dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("live-debug-values")) { printVarLocInMBB(MF, OutLocs, VarLocIDs
, "OutLocs after initialization", dbgs()); } } while (false)
;
503
504 ReversePostOrderTraversal<MachineFunction *> RPOT(&MF);
505 unsigned int RPONumber = 0;
506 for (auto RI = RPOT.begin(), RE = RPOT.end(); RI != RE; ++RI) {
507 OrderToBB[RPONumber] = *RI;
508 BBToOrder[*RI] = RPONumber;
509 Worklist.push(RPONumber);
510 ++RPONumber;
511 }
512 // This is a standard "union of predecessor outs" dataflow problem.
513 // To solve it, we perform join() and transfer() using the two worklist method
514 // until the ranges converge.
515 // Ranges have converged when both worklists are empty.
516 SmallPtrSet<const MachineBasicBlock *, 16> Visited;
517 while (!Worklist.empty() || !Pending.empty()) {
518 // We track what is on the pending worklist to avoid inserting the same
519 // thing twice. We could avoid this with a custom priority queue, but this
520 // is probably not worth it.
521 SmallPtrSet<MachineBasicBlock *, 16> OnPending;
522 DEBUG(dbgs() << "Processing Worklist\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("live-debug-values")) { dbgs() << "Processing Worklist\n"
; } } while (false)
;
523 while (!Worklist.empty()) {
524 MachineBasicBlock *MBB = OrderToBB[Worklist.top()];
525 Worklist.pop();
526 MBBJoined = join(*MBB, OutLocs, InLocs, VarLocIDs, Visited);
527 Visited.insert(MBB);
528 if (MBBJoined) {
529 MBBJoined = false;
Value stored to 'MBBJoined' is never read
530 Changed = true;
531 for (auto &MI : *MBB)
532 OLChanged |= transfer(MI, OpenRanges, OutLocs, VarLocIDs);
533
534 DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs,do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("live-debug-values")) { printVarLocInMBB(MF, OutLocs, VarLocIDs
, "OutLocs after propagating", dbgs()); } } while (false)
535 "OutLocs after propagating", dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("live-debug-values")) { printVarLocInMBB(MF, OutLocs, VarLocIDs
, "OutLocs after propagating", dbgs()); } } while (false)
;
536 DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs,do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("live-debug-values")) { printVarLocInMBB(MF, InLocs, VarLocIDs
, "InLocs after propagating", dbgs()); } } while (false)
537 "InLocs after propagating", dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("live-debug-values")) { printVarLocInMBB(MF, InLocs, VarLocIDs
, "InLocs after propagating", dbgs()); } } while (false)
;
538
539 if (OLChanged) {
540 OLChanged = false;
541 for (auto s : MBB->successors())
542 if (OnPending.insert(s).second) {
543 Pending.push(BBToOrder[s]);
544 }
545 }
546 }
547 }
548 Worklist.swap(Pending);
549 // At this point, pending must be empty, since it was just the empty
550 // worklist
551 assert(Pending.empty() && "Pending should be empty")((Pending.empty() && "Pending should be empty") ? static_cast
<void> (0) : __assert_fail ("Pending.empty() && \"Pending should be empty\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn295052/lib/CodeGen/LiveDebugValues.cpp"
, 551, __PRETTY_FUNCTION__))
;
552 }
553
554 DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, "Final OutLocs", dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("live-debug-values")) { printVarLocInMBB(MF, OutLocs, VarLocIDs
, "Final OutLocs", dbgs()); } } while (false)
;
555 DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs, "Final InLocs", dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("live-debug-values")) { printVarLocInMBB(MF, InLocs, VarLocIDs
, "Final InLocs", dbgs()); } } while (false)
;
556 return Changed;
557}
558
559bool LiveDebugValues::runOnMachineFunction(MachineFunction &MF) {
560 if (!MF.getFunction()->getSubprogram())
561 // LiveDebugValues will already have removed all DBG_VALUEs.
562 return false;
563
564 TRI = MF.getSubtarget().getRegisterInfo();
565 TII = MF.getSubtarget().getInstrInfo();
566 LS.initialize(MF);
567
568 bool Changed = ExtendRanges(MF);
569 return Changed;
570}