Bug Summary

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

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name LiveDebugValues.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-eagerly-assume -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn336939/build-llvm/lib/CodeGen -I /build/llvm-toolchain-snapshot-7~svn336939/lib/CodeGen -I /build/llvm-toolchain-snapshot-7~svn336939/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn336939/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn336939/build-llvm/lib/CodeGen -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-07-13-043813-3945-1 -x c++ /build/llvm-toolchain-snapshot-7~svn336939/lib/CodeGen/LiveDebugValues.cpp
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/DenseMap.h"
22#include "llvm/ADT/PostOrderIterator.h"
23#include "llvm/ADT/SmallPtrSet.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/ADT/SparseBitVector.h"
26#include "llvm/ADT/Statistic.h"
27#include "llvm/ADT/UniqueVector.h"
28#include "llvm/CodeGen/LexicalScopes.h"
29#include "llvm/CodeGen/MachineBasicBlock.h"
30#include "llvm/CodeGen/MachineFrameInfo.h"
31#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineFunctionPass.h"
33#include "llvm/CodeGen/MachineInstr.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
35#include "llvm/CodeGen/MachineMemOperand.h"
36#include "llvm/CodeGen/MachineOperand.h"
37#include "llvm/CodeGen/PseudoSourceValue.h"
38#include "llvm/CodeGen/TargetFrameLowering.h"
39#include "llvm/CodeGen/TargetInstrInfo.h"
40#include "llvm/CodeGen/TargetLowering.h"
41#include "llvm/CodeGen/TargetRegisterInfo.h"
42#include "llvm/CodeGen/TargetSubtargetInfo.h"
43#include "llvm/Config/llvm-config.h"
44#include "llvm/IR/DebugInfoMetadata.h"
45#include "llvm/IR/DebugLoc.h"
46#include "llvm/IR/Function.h"
47#include "llvm/IR/Module.h"
48#include "llvm/MC/MCRegisterInfo.h"
49#include "llvm/Pass.h"
50#include "llvm/Support/Casting.h"
51#include "llvm/Support/Compiler.h"
52#include "llvm/Support/Debug.h"
53#include "llvm/Support/raw_ostream.h"
54#include <algorithm>
55#include <cassert>
56#include <cstdint>
57#include <functional>
58#include <queue>
59#include <utility>
60#include <vector>
61
62using namespace llvm;
63
64#define DEBUG_TYPE"livedebugvalues" "livedebugvalues"
65
66STATISTIC(NumInserted, "Number of DBG_VALUE instructions inserted")static llvm::Statistic NumInserted = {"livedebugvalues", "NumInserted"
, "Number of DBG_VALUE instructions inserted", {0}, {false}}
;
67
68// If @MI is a DBG_VALUE with debug value described by a defined
69// register, returns the number of this register. In the other case, returns 0.
70static unsigned isDbgValueDescribedByReg(const MachineInstr &MI) {
71 assert(MI.isDebugValue() && "expected a DBG_VALUE")(static_cast <bool> (MI.isDebugValue() && "expected a DBG_VALUE"
) ? void (0) : __assert_fail ("MI.isDebugValue() && \"expected a DBG_VALUE\""
, "/build/llvm-toolchain-snapshot-7~svn336939/lib/CodeGen/LiveDebugValues.cpp"
, 71, __extension__ __PRETTY_FUNCTION__))
;
72 assert(MI.getNumOperands() == 4 && "malformed DBG_VALUE")(static_cast <bool> (MI.getNumOperands() == 4 &&
"malformed DBG_VALUE") ? void (0) : __assert_fail ("MI.getNumOperands() == 4 && \"malformed DBG_VALUE\""
, "/build/llvm-toolchain-snapshot-7~svn336939/lib/CodeGen/LiveDebugValues.cpp"
, 72, __extension__ __PRETTY_FUNCTION__))
;
73 // If location of variable is described using a register (directly
74 // or indirectly), this register is always a first operand.
75 return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0;
76}
77
78namespace {
79
80class LiveDebugValues : public MachineFunctionPass {
81private:
82 const TargetRegisterInfo *TRI;
83 const TargetInstrInfo *TII;
84 const TargetFrameLowering *TFI;
85 LexicalScopes LS;
86
87 /// Keeps track of lexical scopes associated with a user value's source
88 /// location.
89 class UserValueScopes {
90 DebugLoc DL;
91 LexicalScopes &LS;
92 SmallPtrSet<const MachineBasicBlock *, 4> LBlocks;
93
94 public:
95 UserValueScopes(DebugLoc D, LexicalScopes &L) : DL(std::move(D)), LS(L) {}
96
97 /// Return true if current scope dominates at least one machine
98 /// instruction in a given machine basic block.
99 bool dominates(MachineBasicBlock *MBB) {
100 if (LBlocks.empty())
101 LS.getMachineBasicBlocks(DL, LBlocks);
102 return LBlocks.count(MBB) != 0 || LS.dominates(DL, MBB);
103 }
104 };
105
106 /// Based on std::pair so it can be used as an index into a DenseMap.
107 using DebugVariableBase =
108 std::pair<const DILocalVariable *, const DILocation *>;
109 /// A potentially inlined instance of a variable.
110 struct DebugVariable : public DebugVariableBase {
111 DebugVariable(const DILocalVariable *Var, const DILocation *InlinedAt)
112 : DebugVariableBase(Var, InlinedAt) {}
113
114 const DILocalVariable *getVar() const { return this->first; }
115 const DILocation *getInlinedAt() const { return this->second; }
116
117 bool operator<(const DebugVariable &DV) const {
118 if (getVar() == DV.getVar())
119 return getInlinedAt() < DV.getInlinedAt();
120 return getVar() < DV.getVar();
121 }
122 };
123
124 /// A pair of debug variable and value location.
125 struct VarLoc {
126 const DebugVariable Var;
127 const MachineInstr &MI; ///< Only used for cloning a new DBG_VALUE.
128 mutable UserValueScopes UVS;
129 enum { InvalidKind = 0, RegisterKind } Kind = InvalidKind;
130
131 /// The value location. Stored separately to avoid repeatedly
132 /// extracting it from MI.
133 union {
134 uint64_t RegNo;
135 uint64_t Hash;
136 } Loc;
137
138 VarLoc(const MachineInstr &MI, LexicalScopes &LS)
139 : Var(MI.getDebugVariable(), MI.getDebugLoc()->getInlinedAt()), MI(MI),
140 UVS(MI.getDebugLoc(), LS) {
141 static_assert((sizeof(Loc) == sizeof(uint64_t)),
142 "hash does not cover all members of Loc");
143 assert(MI.isDebugValue() && "not a DBG_VALUE")(static_cast <bool> (MI.isDebugValue() && "not a DBG_VALUE"
) ? void (0) : __assert_fail ("MI.isDebugValue() && \"not a DBG_VALUE\""
, "/build/llvm-toolchain-snapshot-7~svn336939/lib/CodeGen/LiveDebugValues.cpp"
, 143, __extension__ __PRETTY_FUNCTION__))
;
144 assert(MI.getNumOperands() == 4 && "malformed DBG_VALUE")(static_cast <bool> (MI.getNumOperands() == 4 &&
"malformed DBG_VALUE") ? void (0) : __assert_fail ("MI.getNumOperands() == 4 && \"malformed DBG_VALUE\""
, "/build/llvm-toolchain-snapshot-7~svn336939/lib/CodeGen/LiveDebugValues.cpp"
, 144, __extension__ __PRETTY_FUNCTION__))
;
145 if (int RegNo = isDbgValueDescribedByReg(MI)) {
146 Kind = RegisterKind;
147 Loc.RegNo = RegNo;
148 }
149 }
150
151 /// If this variable is described by a register, return it,
152 /// otherwise return 0.
153 unsigned isDescribedByReg() const {
154 if (Kind == RegisterKind)
155 return Loc.RegNo;
156 return 0;
157 }
158
159 /// Determine whether the lexical scope of this value's debug location
160 /// dominates MBB.
161 bool dominates(MachineBasicBlock &MBB) const { return UVS.dominates(&MBB); }
162
163#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
164 LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void dump() const { MI.dump(); }
165#endif
166
167 bool operator==(const VarLoc &Other) const {
168 return Var == Other.Var && Loc.Hash == Other.Loc.Hash;
169 }
170
171 /// This operator guarantees that VarLocs are sorted by Variable first.
172 bool operator<(const VarLoc &Other) const {
173 if (Var == Other.Var)
174 return Loc.Hash < Other.Loc.Hash;
175 return Var < Other.Var;
176 }
177 };
178
179 using VarLocMap = UniqueVector<VarLoc>;
180 using VarLocSet = SparseBitVector<>;
181 using VarLocInMBB = SmallDenseMap<const MachineBasicBlock *, VarLocSet>;
182 struct SpillDebugPair {
183 MachineInstr *SpillInst;
184 MachineInstr *DebugInst;
185 };
186 using SpillMap = SmallVector<SpillDebugPair, 4>;
187
188 /// This holds the working set of currently open ranges. For fast
189 /// access, this is done both as a set of VarLocIDs, and a map of
190 /// DebugVariable to recent VarLocID. Note that a DBG_VALUE ends all
191 /// previous open ranges for the same variable.
192 class OpenRangesSet {
193 VarLocSet VarLocs;
194 SmallDenseMap<DebugVariableBase, unsigned, 8> Vars;
195
196 public:
197 const VarLocSet &getVarLocs() const { return VarLocs; }
198
199 /// Terminate all open ranges for Var by removing it from the set.
200 void erase(DebugVariable Var) {
201 auto It = Vars.find(Var);
202 if (It != Vars.end()) {
203 unsigned ID = It->second;
204 VarLocs.reset(ID);
205 Vars.erase(It);
206 }
207 }
208
209 /// Terminate all open ranges listed in \c KillSet by removing
210 /// them from the set.
211 void erase(const VarLocSet &KillSet, const VarLocMap &VarLocIDs) {
212 VarLocs.intersectWithComplement(KillSet);
213 for (unsigned ID : KillSet)
214 Vars.erase(VarLocIDs[ID].Var);
215 }
216
217 /// Insert a new range into the set.
218 void insert(unsigned VarLocID, DebugVariableBase Var) {
219 VarLocs.set(VarLocID);
220 Vars.insert({Var, VarLocID});
221 }
222
223 /// Empty the set.
224 void clear() {
225 VarLocs.clear();
226 Vars.clear();
227 }
228
229 /// Return whether the set is empty or not.
230 bool empty() const {
231 assert(Vars.empty() == VarLocs.empty() && "open ranges are inconsistent")(static_cast <bool> (Vars.empty() == VarLocs.empty() &&
"open ranges are inconsistent") ? void (0) : __assert_fail (
"Vars.empty() == VarLocs.empty() && \"open ranges are inconsistent\""
, "/build/llvm-toolchain-snapshot-7~svn336939/lib/CodeGen/LiveDebugValues.cpp"
, 231, __extension__ __PRETTY_FUNCTION__))
;
232 return VarLocs.empty();
233 }
234 };
235
236 bool isSpillInstruction(const MachineInstr &MI, MachineFunction *MF,
237 unsigned &Reg);
238 int extractSpillBaseRegAndOffset(const MachineInstr &MI, unsigned &Reg);
239
240 void transferDebugValue(const MachineInstr &MI, OpenRangesSet &OpenRanges,
241 VarLocMap &VarLocIDs);
242 void transferSpillInst(MachineInstr &MI, OpenRangesSet &OpenRanges,
243 VarLocMap &VarLocIDs, SpillMap &Spills);
244 void transferRegisterDef(MachineInstr &MI, OpenRangesSet &OpenRanges,
245 const VarLocMap &VarLocIDs);
246 bool transferTerminatorInst(MachineInstr &MI, OpenRangesSet &OpenRanges,
247 VarLocInMBB &OutLocs, const VarLocMap &VarLocIDs);
248 bool transfer(MachineInstr &MI, OpenRangesSet &OpenRanges,
249 VarLocInMBB &OutLocs, VarLocMap &VarLocIDs, SpillMap &Spills,
250 bool transferSpills);
251
252 bool join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs, VarLocInMBB &InLocs,
253 const VarLocMap &VarLocIDs,
254 SmallPtrSet<const MachineBasicBlock *, 16> &Visited);
255
256 bool ExtendRanges(MachineFunction &MF);
257
258public:
259 static char ID;
260
261 /// Default construct and initialize the pass.
262 LiveDebugValues();
263
264 /// Tell the pass manager which passes we depend on and what
265 /// information we preserve.
266 void getAnalysisUsage(AnalysisUsage &AU) const override;
267
268 MachineFunctionProperties getRequiredProperties() const override {
269 return MachineFunctionProperties().set(
270 MachineFunctionProperties::Property::NoVRegs);
271 }
272
273 /// Print to ostream with a message.
274 void printVarLocInMBB(const MachineFunction &MF, const VarLocInMBB &V,
275 const VarLocMap &VarLocIDs, const char *msg,
276 raw_ostream &Out) const;
277
278 /// Calculate the liveness information for the given machine function.
279 bool runOnMachineFunction(MachineFunction &MF) override;
280};
281
282} // end anonymous namespace
283
284//===----------------------------------------------------------------------===//
285// Implementation
286//===----------------------------------------------------------------------===//
287
288char LiveDebugValues::ID = 0;
289
290char &llvm::LiveDebugValuesID = LiveDebugValues::ID;
291
292INITIALIZE_PASS(LiveDebugValues, DEBUG_TYPE, "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)); }
293 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)); }
294
295/// Default construct and initialize the pass.
296LiveDebugValues::LiveDebugValues() : MachineFunctionPass(ID) {
297 initializeLiveDebugValuesPass(*PassRegistry::getPassRegistry());
298}
299
300/// Tell the pass manager which passes we depend on and what information we
301/// preserve.
302void LiveDebugValues::getAnalysisUsage(AnalysisUsage &AU) const {
303 AU.setPreservesCFG();
304 MachineFunctionPass::getAnalysisUsage(AU);
305}
306
307//===----------------------------------------------------------------------===//
308// Debug Range Extension Implementation
309//===----------------------------------------------------------------------===//
310
311#ifndef NDEBUG
312void LiveDebugValues::printVarLocInMBB(const MachineFunction &MF,
313 const VarLocInMBB &V,
314 const VarLocMap &VarLocIDs,
315 const char *msg,
316 raw_ostream &Out) const {
317 Out << '\n' << msg << '\n';
318 for (const MachineBasicBlock &BB : MF) {
319 const auto &L = V.lookup(&BB);
320 Out << "MBB: " << BB.getName() << ":\n";
321 for (unsigned VLL : L) {
322 const VarLoc &VL = VarLocIDs[VLL];
323 Out << " Var: " << VL.Var.getVar()->getName();
324 Out << " MI: ";
325 VL.dump();
326 }
327 }
328 Out << "\n";
329}
330#endif
331
332/// Given a spill instruction, extract the register and offset used to
333/// address the spill location in a target independent way.
334int LiveDebugValues::extractSpillBaseRegAndOffset(const MachineInstr &MI,
335 unsigned &Reg) {
336 assert(MI.hasOneMemOperand() &&(static_cast <bool> (MI.hasOneMemOperand() && "Spill instruction does not have exactly one memory operand?"
) ? void (0) : __assert_fail ("MI.hasOneMemOperand() && \"Spill instruction does not have exactly one memory operand?\""
, "/build/llvm-toolchain-snapshot-7~svn336939/lib/CodeGen/LiveDebugValues.cpp"
, 337, __extension__ __PRETTY_FUNCTION__))
337 "Spill instruction does not have exactly one memory operand?")(static_cast <bool> (MI.hasOneMemOperand() && "Spill instruction does not have exactly one memory operand?"
) ? void (0) : __assert_fail ("MI.hasOneMemOperand() && \"Spill instruction does not have exactly one memory operand?\""
, "/build/llvm-toolchain-snapshot-7~svn336939/lib/CodeGen/LiveDebugValues.cpp"
, 337, __extension__ __PRETTY_FUNCTION__))
;
338 auto MMOI = MI.memoperands_begin();
339 const PseudoSourceValue *PVal = (*MMOI)->getPseudoValue();
340 assert(PVal->kind() == PseudoSourceValue::FixedStack &&(static_cast <bool> (PVal->kind() == PseudoSourceValue
::FixedStack && "Inconsistent memory operand in spill instruction"
) ? void (0) : __assert_fail ("PVal->kind() == PseudoSourceValue::FixedStack && \"Inconsistent memory operand in spill instruction\""
, "/build/llvm-toolchain-snapshot-7~svn336939/lib/CodeGen/LiveDebugValues.cpp"
, 341, __extension__ __PRETTY_FUNCTION__))
341 "Inconsistent memory operand in spill instruction")(static_cast <bool> (PVal->kind() == PseudoSourceValue
::FixedStack && "Inconsistent memory operand in spill instruction"
) ? void (0) : __assert_fail ("PVal->kind() == PseudoSourceValue::FixedStack && \"Inconsistent memory operand in spill instruction\""
, "/build/llvm-toolchain-snapshot-7~svn336939/lib/CodeGen/LiveDebugValues.cpp"
, 341, __extension__ __PRETTY_FUNCTION__))
;
342 int FI = cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex();
343 const MachineBasicBlock *MBB = MI.getParent();
344 return TFI->getFrameIndexReference(*MBB->getParent(), FI, Reg);
345}
346
347/// End all previous ranges related to @MI and start a new range from @MI
348/// if it is a DBG_VALUE instr.
349void LiveDebugValues::transferDebugValue(const MachineInstr &MI,
350 OpenRangesSet &OpenRanges,
351 VarLocMap &VarLocIDs) {
352 if (!MI.isDebugValue())
353 return;
354 const DILocalVariable *Var = MI.getDebugVariable();
355 const DILocation *DebugLoc = MI.getDebugLoc();
356 const DILocation *InlinedAt = DebugLoc->getInlinedAt();
357 assert(Var->isValidLocationForIntrinsic(DebugLoc) &&(static_cast <bool> (Var->isValidLocationForIntrinsic
(DebugLoc) && "Expected inlined-at fields to agree") ?
void (0) : __assert_fail ("Var->isValidLocationForIntrinsic(DebugLoc) && \"Expected inlined-at fields to agree\""
, "/build/llvm-toolchain-snapshot-7~svn336939/lib/CodeGen/LiveDebugValues.cpp"
, 358, __extension__ __PRETTY_FUNCTION__))
358 "Expected inlined-at fields to agree")(static_cast <bool> (Var->isValidLocationForIntrinsic
(DebugLoc) && "Expected inlined-at fields to agree") ?
void (0) : __assert_fail ("Var->isValidLocationForIntrinsic(DebugLoc) && \"Expected inlined-at fields to agree\""
, "/build/llvm-toolchain-snapshot-7~svn336939/lib/CodeGen/LiveDebugValues.cpp"
, 358, __extension__ __PRETTY_FUNCTION__))
;
359
360 // End all previous ranges of Var.
361 DebugVariable V(Var, InlinedAt);
362 OpenRanges.erase(V);
363
364 // Add the VarLoc to OpenRanges from this DBG_VALUE.
365 // TODO: Currently handles DBG_VALUE which has only reg as location.
366 if (isDbgValueDescribedByReg(MI)) {
367 VarLoc VL(MI, LS);
368 unsigned ID = VarLocIDs.insert(VL);
369 OpenRanges.insert(ID, VL.Var);
370 }
371}
372
373/// A definition of a register may mark the end of a range.
374void LiveDebugValues::transferRegisterDef(MachineInstr &MI,
375 OpenRangesSet &OpenRanges,
376 const VarLocMap &VarLocIDs) {
377 MachineFunction *MF = MI.getMF();
378 const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
379 unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
380 SparseBitVector<> KillSet;
381 for (const MachineOperand &MO : MI.operands()) {
382 // Determine whether the operand is a register def. Assume that call
383 // instructions never clobber SP, because some backends (e.g., AArch64)
384 // never list SP in the regmask.
385 if (MO.isReg() && MO.isDef() && MO.getReg() &&
386 TRI->isPhysicalRegister(MO.getReg()) &&
387 !(MI.isCall() && MO.getReg() == SP)) {
388 // Remove ranges of all aliased registers.
389 for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI)
390 for (unsigned ID : OpenRanges.getVarLocs())
391 if (VarLocIDs[ID].isDescribedByReg() == *RAI)
392 KillSet.set(ID);
393 } else if (MO.isRegMask()) {
394 // Remove ranges of all clobbered registers. Register masks don't usually
395 // list SP as preserved. While the debug info may be off for an
396 // instruction or two around callee-cleanup calls, transferring the
397 // DEBUG_VALUE across the call is still a better user experience.
398 for (unsigned ID : OpenRanges.getVarLocs()) {
399 unsigned Reg = VarLocIDs[ID].isDescribedByReg();
400 if (Reg && Reg != SP && MO.clobbersPhysReg(Reg))
401 KillSet.set(ID);
402 }
403 }
404 }
405 OpenRanges.erase(KillSet, VarLocIDs);
406}
407
408/// Decide if @MI is a spill instruction and return true if it is. We use 2
409/// criteria to make this decision:
410/// - Is this instruction a store to a spill slot?
411/// - Is there a register operand that is both used and killed?
412/// TODO: Store optimization can fold spills into other stores (including
413/// other spills). We do not handle this yet (more than one memory operand).
414bool LiveDebugValues::isSpillInstruction(const MachineInstr &MI,
415 MachineFunction *MF, unsigned &Reg) {
416 const MachineFrameInfo &FrameInfo = MF->getFrameInfo();
417 int FI;
418 const MachineMemOperand *MMO;
419
420 // TODO: Handle multiple stores folded into one.
421 if (!MI.hasOneMemOperand())
422 return false;
423
424 // To identify a spill instruction, use the same criteria as in AsmPrinter.
425 if (!((TII->isStoreToStackSlotPostFE(MI, FI) ||
426 TII->hasStoreToStackSlot(MI, MMO, FI)) &&
427 FrameInfo.isSpillSlotObjectIndex(FI)))
428 return false;
429
430 auto isKilledReg = [&](const MachineOperand MO, unsigned &Reg) {
431 if (!MO.isReg() || !MO.isUse()) {
432 Reg = 0;
433 return false;
434 }
435 Reg = MO.getReg();
436 return MO.isKill();
437 };
438
439 for (const MachineOperand &MO : MI.operands()) {
440 // In a spill instruction generated by the InlineSpiller the spilled
441 // register has its kill flag set.
442 if (isKilledReg(MO, Reg))
443 return true;
444 if (Reg != 0) {
445 // Check whether next instruction kills the spilled register.
446 // FIXME: Current solution does not cover search for killed register in
447 // bundles and instructions further down the chain.
448 auto NextI = std::next(MI.getIterator());
449 // Skip next instruction that points to basic block end iterator.
450 if (MI.getParent()->end() == NextI)
451 continue;
452 unsigned RegNext;
453 for (const MachineOperand &MONext : NextI->operands()) {
454 // Return true if we came across the register from the
455 // previous spill instruction that is killed in NextI.
456 if (isKilledReg(MONext, RegNext) && RegNext == Reg)
457 return true;
458 }
459 }
460 }
461 // Return false if we didn't find spilled register.
462 return false;
463}
464
465/// A spilled register may indicate that we have to end the current range of
466/// a variable and create a new one for the spill location.
467/// We don't want to insert any instructions in transfer(), so we just create
468/// the DBG_VALUE witout inserting it and keep track of it in @Spills.
469/// It will be inserted into the BB when we're done iterating over the
470/// instructions.
471void LiveDebugValues::transferSpillInst(MachineInstr &MI,
472 OpenRangesSet &OpenRanges,
473 VarLocMap &VarLocIDs,
474 SpillMap &Spills) {
475 unsigned Reg;
476 MachineFunction *MF = MI.getMF();
477 if (!isSpillInstruction(MI, MF, Reg))
478 return;
479
480 // Check if the register is the location of a debug value.
481 for (unsigned ID : OpenRanges.getVarLocs()) {
482 if (VarLocIDs[ID].isDescribedByReg() == Reg) {
483 LLVM_DEBUG(dbgs() << "Spilling Register " << printReg(Reg, TRI) << '('do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("livedebugvalues")) { dbgs() << "Spilling Register " <<
printReg(Reg, TRI) << '(' << VarLocIDs[ID].Var.getVar
()->getName() << ")\n"; } } while (false)
484 << VarLocIDs[ID].Var.getVar()->getName() << ")\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("livedebugvalues")) { dbgs() << "Spilling Register " <<
printReg(Reg, TRI) << '(' << VarLocIDs[ID].Var.getVar
()->getName() << ")\n"; } } while (false)
;
485
486 // Create a DBG_VALUE instruction to describe the Var in its spilled
487 // location, but don't insert it yet to avoid invalidating the
488 // iterator in our caller.
489 unsigned SpillBase;
490 int SpillOffset = extractSpillBaseRegAndOffset(MI, SpillBase);
491 const MachineInstr *DMI = &VarLocIDs[ID].MI;
492 auto *SpillExpr = DIExpression::prepend(
493 DMI->getDebugExpression(), DIExpression::NoDeref, SpillOffset);
494 MachineInstr *SpDMI =
495 BuildMI(*MF, DMI->getDebugLoc(), DMI->getDesc(), true, SpillBase,
496 DMI->getDebugVariable(), SpillExpr);
497 LLVM_DEBUG(dbgs() << "Creating DBG_VALUE inst for spill: ";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("livedebugvalues")) { dbgs() << "Creating DBG_VALUE inst for spill: "
; SpDMI->print(dbgs(), false, TII); } } while (false)
498 SpDMI->print(dbgs(), false, TII))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("livedebugvalues")) { dbgs() << "Creating DBG_VALUE inst for spill: "
; SpDMI->print(dbgs(), false, TII); } } while (false)
;
499
500 // The newly created DBG_VALUE instruction SpDMI must be inserted after
501 // MI. Keep track of the pairing.
502 SpillDebugPair MIP = {&MI, SpDMI};
503 Spills.push_back(MIP);
504
505 // End all previous ranges of Var.
506 OpenRanges.erase(VarLocIDs[ID].Var);
507
508 // Add the VarLoc to OpenRanges.
509 VarLoc VL(*SpDMI, LS);
510 unsigned SpillLocID = VarLocIDs.insert(VL);
511 OpenRanges.insert(SpillLocID, VL.Var);
512 return;
513 }
514 }
515}
516
517/// Terminate all open ranges at the end of the current basic block.
518bool LiveDebugValues::transferTerminatorInst(MachineInstr &MI,
519 OpenRangesSet &OpenRanges,
520 VarLocInMBB &OutLocs,
521 const VarLocMap &VarLocIDs) {
522 bool Changed = false;
523 const MachineBasicBlock *CurMBB = MI.getParent();
524 if (!(MI.isTerminator() || (&MI == &CurMBB->back())))
525 return false;
526
527 if (OpenRanges.empty())
528 return false;
529
530 LLVM_DEBUG(for (unsigned IDdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("livedebugvalues")) { for (unsigned ID : OpenRanges.getVarLocs
()) { dbgs() << "Add to OutLocs: "; VarLocIDs[ID].dump(
); }; } } while (false)
531 : OpenRanges.getVarLocs()) {do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("livedebugvalues")) { for (unsigned ID : OpenRanges.getVarLocs
()) { dbgs() << "Add to OutLocs: "; VarLocIDs[ID].dump(
); }; } } while (false)
532 // Copy OpenRanges to OutLocs, if not already present.do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("livedebugvalues")) { for (unsigned ID : OpenRanges.getVarLocs
()) { dbgs() << "Add to OutLocs: "; VarLocIDs[ID].dump(
); }; } } while (false)
533 dbgs() << "Add to OutLocs: ";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("livedebugvalues")) { for (unsigned ID : OpenRanges.getVarLocs
()) { dbgs() << "Add to OutLocs: "; VarLocIDs[ID].dump(
); }; } } while (false)
534 VarLocIDs[ID].dump();do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("livedebugvalues")) { for (unsigned ID : OpenRanges.getVarLocs
()) { dbgs() << "Add to OutLocs: "; VarLocIDs[ID].dump(
); }; } } while (false)
535 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("livedebugvalues")) { for (unsigned ID : OpenRanges.getVarLocs
()) { dbgs() << "Add to OutLocs: "; VarLocIDs[ID].dump(
); }; } } while (false)
;
536 VarLocSet &VLS = OutLocs[CurMBB];
537 Changed = VLS |= OpenRanges.getVarLocs();
538 OpenRanges.clear();
539 return Changed;
540}
541
542/// This routine creates OpenRanges and OutLocs.
543bool LiveDebugValues::transfer(MachineInstr &MI, OpenRangesSet &OpenRanges,
544 VarLocInMBB &OutLocs, VarLocMap &VarLocIDs,
545 SpillMap &Spills, bool transferSpills) {
546 bool Changed = false;
547 transferDebugValue(MI, OpenRanges, VarLocIDs);
548 transferRegisterDef(MI, OpenRanges, VarLocIDs);
549 if (transferSpills)
550 transferSpillInst(MI, OpenRanges, VarLocIDs, Spills);
551 Changed = transferTerminatorInst(MI, OpenRanges, OutLocs, VarLocIDs);
552 return Changed;
553}
554
555/// This routine joins the analysis results of all incoming edges in @MBB by
556/// inserting a new DBG_VALUE instruction at the start of the @MBB - if the same
557/// source variable in all the predecessors of @MBB reside in the same location.
558bool LiveDebugValues::join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs,
559 VarLocInMBB &InLocs, const VarLocMap &VarLocIDs,
560 SmallPtrSet<const MachineBasicBlock *, 16> &Visited) {
561 LLVM_DEBUG(dbgs() << "join MBB: " << MBB.getName() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("livedebugvalues")) { dbgs() << "join MBB: " << MBB
.getName() << "\n"; } } while (false)
;
562 bool Changed = false;
563
564 VarLocSet InLocsT; // Temporary incoming locations.
565
566 // For all predecessors of this MBB, find the set of VarLocs that
567 // can be joined.
568 int NumVisited = 0;
569 for (auto p : MBB.predecessors()) {
570 // Ignore unvisited predecessor blocks. As we are processing
571 // the blocks in reverse post-order any unvisited block can
572 // be considered to not remove any incoming values.
573 if (!Visited.count(p))
574 continue;
575 auto OL = OutLocs.find(p);
576 // Join is null in case of empty OutLocs from any of the pred.
577 if (OL == OutLocs.end())
578 return false;
579
580 // Just copy over the Out locs to incoming locs for the first visited
581 // predecessor, and for all other predecessors join the Out locs.
582 if (!NumVisited)
583 InLocsT = OL->second;
584 else
585 InLocsT &= OL->second;
586 NumVisited++;
587 }
588
589 // Filter out DBG_VALUES that are out of scope.
590 VarLocSet KillSet;
591 for (auto ID : InLocsT)
592 if (!VarLocIDs[ID].dominates(MBB))
593 KillSet.set(ID);
594 InLocsT.intersectWithComplement(KillSet);
595
596 // As we are processing blocks in reverse post-order we
597 // should have processed at least one predecessor, unless it
598 // is the entry block which has no predecessor.
599 assert((NumVisited || MBB.pred_empty()) &&(static_cast <bool> ((NumVisited || MBB.pred_empty()) &&
"Should have processed at least one predecessor") ? void (0)
: __assert_fail ("(NumVisited || MBB.pred_empty()) && \"Should have processed at least one predecessor\""
, "/build/llvm-toolchain-snapshot-7~svn336939/lib/CodeGen/LiveDebugValues.cpp"
, 600, __extension__ __PRETTY_FUNCTION__))
600 "Should have processed at least one predecessor")(static_cast <bool> ((NumVisited || MBB.pred_empty()) &&
"Should have processed at least one predecessor") ? void (0)
: __assert_fail ("(NumVisited || MBB.pred_empty()) && \"Should have processed at least one predecessor\""
, "/build/llvm-toolchain-snapshot-7~svn336939/lib/CodeGen/LiveDebugValues.cpp"
, 600, __extension__ __PRETTY_FUNCTION__))
;
601 if (InLocsT.empty())
602 return false;
603
604 VarLocSet &ILS = InLocs[&MBB];
605
606 // Insert DBG_VALUE instructions, if not already inserted.
607 VarLocSet Diff = InLocsT;
608 Diff.intersectWithComplement(ILS);
609 for (auto ID : Diff) {
610 // This VarLoc is not found in InLocs i.e. it is not yet inserted. So, a
611 // new range is started for the var from the mbb's beginning by inserting
612 // a new DBG_VALUE. transfer() will end this range however appropriate.
613 const VarLoc &DiffIt = VarLocIDs[ID];
614 const MachineInstr *DMI = &DiffIt.MI;
615 MachineInstr *MI =
616 BuildMI(MBB, MBB.instr_begin(), DMI->getDebugLoc(), DMI->getDesc(),
617 DMI->isIndirectDebugValue(), DMI->getOperand(0).getReg(),
618 DMI->getDebugVariable(), DMI->getDebugExpression());
619 if (DMI->isIndirectDebugValue())
620 MI->getOperand(1).setImm(DMI->getOperand(1).getImm());
621 LLVM_DEBUG(dbgs() << "Inserted: "; MI->dump();)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("livedebugvalues")) { dbgs() << "Inserted: "; MI->dump
();; } } while (false)
;
622 ILS.set(ID);
623 ++NumInserted;
624 Changed = true;
625 }
626 return Changed;
627}
628
629/// Calculate the liveness information for the given machine function and
630/// extend ranges across basic blocks.
631bool LiveDebugValues::ExtendRanges(MachineFunction &MF) {
632 LLVM_DEBUG(dbgs() << "\nDebug Range Extension\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("livedebugvalues")) { dbgs() << "\nDebug Range Extension\n"
; } } while (false)
;
633
634 bool Changed = false;
635 bool OLChanged = false;
636 bool MBBJoined = false;
637
638 VarLocMap VarLocIDs; // Map VarLoc<>unique ID for use in bitvectors.
639 OpenRangesSet OpenRanges; // Ranges that are open until end of bb.
640 VarLocInMBB OutLocs; // Ranges that exist beyond bb.
641 VarLocInMBB InLocs; // Ranges that are incoming after joining.
642 SpillMap Spills; // DBG_VALUEs associated with spills.
643
644 DenseMap<unsigned int, MachineBasicBlock *> OrderToBB;
645 DenseMap<MachineBasicBlock *, unsigned int> BBToOrder;
646 std::priority_queue<unsigned int, std::vector<unsigned int>,
647 std::greater<unsigned int>>
648 Worklist;
649 std::priority_queue<unsigned int, std::vector<unsigned int>,
650 std::greater<unsigned int>>
651 Pending;
652
653 // Initialize every mbb with OutLocs.
654 // We are not looking at any spill instructions during the initial pass
655 // over the BBs. The LiveDebugVariables pass has already created DBG_VALUE
656 // instructions for spills of registers that are known to be user variables
657 // within the BB in which the spill occurs.
658 for (auto &MBB : MF)
659 for (auto &MI : MBB)
660 transfer(MI, OpenRanges, OutLocs, VarLocIDs, Spills,
661 /*transferSpills=*/false);
662
663 LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs,do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("livedebugvalues")) { printVarLocInMBB(MF, OutLocs, VarLocIDs
, "OutLocs after initialization", dbgs()); } } while (false)
664 "OutLocs after initialization", dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("livedebugvalues")) { printVarLocInMBB(MF, OutLocs, VarLocIDs
, "OutLocs after initialization", dbgs()); } } while (false)
;
665
666 ReversePostOrderTraversal<MachineFunction *> RPOT(&MF);
667 unsigned int RPONumber = 0;
668 for (auto RI = RPOT.begin(), RE = RPOT.end(); RI != RE; ++RI) {
669 OrderToBB[RPONumber] = *RI;
670 BBToOrder[*RI] = RPONumber;
671 Worklist.push(RPONumber);
672 ++RPONumber;
673 }
674 // This is a standard "union of predecessor outs" dataflow problem.
675 // To solve it, we perform join() and transfer() using the two worklist method
676 // until the ranges converge.
677 // Ranges have converged when both worklists are empty.
678 SmallPtrSet<const MachineBasicBlock *, 16> Visited;
679 while (!Worklist.empty() || !Pending.empty()) {
680 // We track what is on the pending worklist to avoid inserting the same
681 // thing twice. We could avoid this with a custom priority queue, but this
682 // is probably not worth it.
683 SmallPtrSet<MachineBasicBlock *, 16> OnPending;
684 LLVM_DEBUG(dbgs() << "Processing Worklist\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("livedebugvalues")) { dbgs() << "Processing Worklist\n"
; } } while (false)
;
685 while (!Worklist.empty()) {
686 MachineBasicBlock *MBB = OrderToBB[Worklist.top()];
687 Worklist.pop();
688 MBBJoined = join(*MBB, OutLocs, InLocs, VarLocIDs, Visited);
689 Visited.insert(MBB);
690 if (MBBJoined) {
691 MBBJoined = false;
Value stored to 'MBBJoined' is never read
692 Changed = true;
693 // Now that we have started to extend ranges across BBs we need to
694 // examine spill instructions to see whether they spill registers that
695 // correspond to user variables.
696 for (auto &MI : *MBB)
697 OLChanged |= transfer(MI, OpenRanges, OutLocs, VarLocIDs, Spills,
698 /*transferSpills=*/true);
699
700 // Add any DBG_VALUE instructions necessitated by spills.
701 for (auto &SP : Spills)
702 MBB->insertAfter(MachineBasicBlock::iterator(*SP.SpillInst),
703 SP.DebugInst);
704 Spills.clear();
705
706 LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs,do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("livedebugvalues")) { printVarLocInMBB(MF, OutLocs, VarLocIDs
, "OutLocs after propagating", dbgs()); } } while (false)
707 "OutLocs after propagating", dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("livedebugvalues")) { printVarLocInMBB(MF, OutLocs, VarLocIDs
, "OutLocs after propagating", dbgs()); } } while (false)
;
708 LLVM_DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs,do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("livedebugvalues")) { printVarLocInMBB(MF, InLocs, VarLocIDs
, "InLocs after propagating", dbgs()); } } while (false)
709 "InLocs after propagating", dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("livedebugvalues")) { printVarLocInMBB(MF, InLocs, VarLocIDs
, "InLocs after propagating", dbgs()); } } while (false)
;
710
711 if (OLChanged) {
712 OLChanged = false;
713 for (auto s : MBB->successors())
714 if (OnPending.insert(s).second) {
715 Pending.push(BBToOrder[s]);
716 }
717 }
718 }
719 }
720 Worklist.swap(Pending);
721 // At this point, pending must be empty, since it was just the empty
722 // worklist
723 assert(Pending.empty() && "Pending should be empty")(static_cast <bool> (Pending.empty() && "Pending should be empty"
) ? void (0) : __assert_fail ("Pending.empty() && \"Pending should be empty\""
, "/build/llvm-toolchain-snapshot-7~svn336939/lib/CodeGen/LiveDebugValues.cpp"
, 723, __extension__ __PRETTY_FUNCTION__))
;
724 }
725
726 LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, "Final OutLocs", dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("livedebugvalues")) { printVarLocInMBB(MF, OutLocs, VarLocIDs
, "Final OutLocs", dbgs()); } } while (false)
;
727 LLVM_DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs, "Final InLocs", dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("livedebugvalues")) { printVarLocInMBB(MF, InLocs, VarLocIDs
, "Final InLocs", dbgs()); } } while (false)
;
728 return Changed;
729}
730
731bool LiveDebugValues::runOnMachineFunction(MachineFunction &MF) {
732 if (!MF.getFunction().getSubprogram())
733 // LiveDebugValues will already have removed all DBG_VALUEs.
734 return false;
735
736 // Skip functions from NoDebug compilation units.
737 if (MF.getFunction().getSubprogram()->getUnit()->getEmissionKind() ==
738 DICompileUnit::NoDebug)
739 return false;
740
741 TRI = MF.getSubtarget().getRegisterInfo();
742 TII = MF.getSubtarget().getInstrInfo();
743 TFI = MF.getSubtarget().getFrameLowering();
744 LS.initialize(MF);
745
746 bool Changed = ExtendRanges(MF);
747 return Changed;
748}