File: | lib/CodeGen/LiveDebugValues.cpp |
Warning: | line 922, column 9 Value stored to 'MBBJoined' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- LiveDebugValues.cpp - Tracking Debug Value MIs ---------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | /// |
9 | /// This pass implements a data flow analysis that propagates debug location |
10 | /// information by inserting additional DBG_VALUE instructions into the machine |
11 | /// instruction stream. The pass internally builds debug location liveness |
12 | /// ranges to determine the points where additional DBG_VALUEs need to be |
13 | /// inserted. |
14 | /// |
15 | /// This is a separate pass from DbgValueHistoryCalculator to facilitate |
16 | /// testing and improve modularity. |
17 | /// |
18 | //===----------------------------------------------------------------------===// |
19 | |
20 | #include "llvm/ADT/DenseMap.h" |
21 | #include "llvm/ADT/PostOrderIterator.h" |
22 | #include "llvm/ADT/SmallPtrSet.h" |
23 | #include "llvm/ADT/SmallVector.h" |
24 | #include "llvm/ADT/SparseBitVector.h" |
25 | #include "llvm/ADT/Statistic.h" |
26 | #include "llvm/ADT/UniqueVector.h" |
27 | #include "llvm/CodeGen/LexicalScopes.h" |
28 | #include "llvm/CodeGen/MachineBasicBlock.h" |
29 | #include "llvm/CodeGen/MachineFrameInfo.h" |
30 | #include "llvm/CodeGen/MachineFunction.h" |
31 | #include "llvm/CodeGen/MachineFunctionPass.h" |
32 | #include "llvm/CodeGen/MachineInstr.h" |
33 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
34 | #include "llvm/CodeGen/MachineMemOperand.h" |
35 | #include "llvm/CodeGen/MachineOperand.h" |
36 | #include "llvm/CodeGen/PseudoSourceValue.h" |
37 | #include "llvm/CodeGen/RegisterScavenging.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/DIBuilder.h" |
45 | #include "llvm/IR/DebugInfoMetadata.h" |
46 | #include "llvm/IR/DebugLoc.h" |
47 | #include "llvm/IR/Function.h" |
48 | #include "llvm/IR/Module.h" |
49 | #include "llvm/MC/MCRegisterInfo.h" |
50 | #include "llvm/Pass.h" |
51 | #include "llvm/Support/Casting.h" |
52 | #include "llvm/Support/Compiler.h" |
53 | #include "llvm/Support/Debug.h" |
54 | #include "llvm/Support/raw_ostream.h" |
55 | #include <algorithm> |
56 | #include <cassert> |
57 | #include <cstdint> |
58 | #include <functional> |
59 | #include <queue> |
60 | #include <utility> |
61 | #include <vector> |
62 | |
63 | using namespace llvm; |
64 | |
65 | #define DEBUG_TYPE"livedebugvalues" "livedebugvalues" |
66 | |
67 | STATISTIC(NumInserted, "Number of DBG_VALUE instructions inserted")static llvm::Statistic NumInserted = {"livedebugvalues", "NumInserted" , "Number of DBG_VALUE instructions inserted", {0}, {false}}; |
68 | |
69 | // If @MI is a DBG_VALUE with debug value described by a defined |
70 | // register, returns the number of this register. In the other case, returns 0. |
71 | static unsigned isDbgValueDescribedByReg(const MachineInstr &MI) { |
72 | 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\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/CodeGen/LiveDebugValues.cpp" , 72, __PRETTY_FUNCTION__)); |
73 | 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\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/CodeGen/LiveDebugValues.cpp" , 73, __PRETTY_FUNCTION__)); |
74 | // If location of variable is described using a register (directly |
75 | // or indirectly), this register is always a first operand. |
76 | return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0; |
77 | } |
78 | |
79 | namespace { |
80 | |
81 | class LiveDebugValues : public MachineFunctionPass { |
82 | private: |
83 | const TargetRegisterInfo *TRI; |
84 | const TargetInstrInfo *TII; |
85 | const TargetFrameLowering *TFI; |
86 | BitVector CalleeSavedRegs; |
87 | LexicalScopes LS; |
88 | |
89 | enum struct TransferKind { TransferCopy, TransferSpill, TransferRestore }; |
90 | |
91 | /// Keeps track of lexical scopes associated with a user value's source |
92 | /// location. |
93 | class UserValueScopes { |
94 | DebugLoc DL; |
95 | LexicalScopes &LS; |
96 | SmallPtrSet<const MachineBasicBlock *, 4> LBlocks; |
97 | |
98 | public: |
99 | UserValueScopes(DebugLoc D, LexicalScopes &L) : DL(std::move(D)), LS(L) {} |
100 | |
101 | /// Return true if current scope dominates at least one machine |
102 | /// instruction in a given machine basic block. |
103 | bool dominates(MachineBasicBlock *MBB) { |
104 | if (LBlocks.empty()) |
105 | LS.getMachineBasicBlocks(DL, LBlocks); |
106 | return LBlocks.count(MBB) != 0 || LS.dominates(DL, MBB); |
107 | } |
108 | }; |
109 | |
110 | /// Based on std::pair so it can be used as an index into a DenseMap. |
111 | using DebugVariableBase = |
112 | std::pair<const DILocalVariable *, const DILocation *>; |
113 | /// A potentially inlined instance of a variable. |
114 | struct DebugVariable : public DebugVariableBase { |
115 | DebugVariable(const DILocalVariable *Var, const DILocation *InlinedAt) |
116 | : DebugVariableBase(Var, InlinedAt) {} |
117 | |
118 | const DILocalVariable *getVar() const { return this->first; } |
119 | const DILocation *getInlinedAt() const { return this->second; } |
120 | |
121 | bool operator<(const DebugVariable &DV) const { |
122 | if (getVar() == DV.getVar()) |
123 | return getInlinedAt() < DV.getInlinedAt(); |
124 | return getVar() < DV.getVar(); |
125 | } |
126 | }; |
127 | |
128 | /// A pair of debug variable and value location. |
129 | struct VarLoc { |
130 | // The location at which a spilled variable resides. It consists of a |
131 | // register and an offset. |
132 | struct SpillLoc { |
133 | unsigned SpillBase; |
134 | int SpillOffset; |
135 | bool operator==(const SpillLoc &Other) const { |
136 | return SpillBase == Other.SpillBase && SpillOffset == Other.SpillOffset; |
137 | } |
138 | }; |
139 | |
140 | const DebugVariable Var; |
141 | const MachineInstr &MI; ///< Only used for cloning a new DBG_VALUE. |
142 | mutable UserValueScopes UVS; |
143 | enum VarLocKind { |
144 | InvalidKind = 0, |
145 | RegisterKind, |
146 | SpillLocKind |
147 | } Kind = InvalidKind; |
148 | |
149 | /// The value location. Stored separately to avoid repeatedly |
150 | /// extracting it from MI. |
151 | union { |
152 | uint64_t RegNo; |
153 | SpillLoc SpillLocation; |
154 | uint64_t Hash; |
155 | } Loc; |
156 | |
157 | VarLoc(const MachineInstr &MI, LexicalScopes &LS) |
158 | : Var(MI.getDebugVariable(), MI.getDebugLoc()->getInlinedAt()), MI(MI), |
159 | UVS(MI.getDebugLoc(), LS) { |
160 | static_assert((sizeof(Loc) == sizeof(uint64_t)), |
161 | "hash does not cover all members of Loc"); |
162 | 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\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/CodeGen/LiveDebugValues.cpp" , 162, __PRETTY_FUNCTION__)); |
163 | 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\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/CodeGen/LiveDebugValues.cpp" , 163, __PRETTY_FUNCTION__)); |
164 | if (int RegNo = isDbgValueDescribedByReg(MI)) { |
165 | Kind = RegisterKind; |
166 | Loc.RegNo = RegNo; |
167 | } |
168 | } |
169 | |
170 | /// The constructor for spill locations. |
171 | VarLoc(const MachineInstr &MI, unsigned SpillBase, int SpillOffset, |
172 | LexicalScopes &LS) |
173 | : Var(MI.getDebugVariable(), MI.getDebugLoc()->getInlinedAt()), MI(MI), |
174 | UVS(MI.getDebugLoc(), LS) { |
175 | 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\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/CodeGen/LiveDebugValues.cpp" , 175, __PRETTY_FUNCTION__)); |
176 | 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\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/CodeGen/LiveDebugValues.cpp" , 176, __PRETTY_FUNCTION__)); |
177 | Kind = SpillLocKind; |
178 | Loc.SpillLocation = {SpillBase, SpillOffset}; |
179 | } |
180 | |
181 | /// If this variable is described by a register, return it, |
182 | /// otherwise return 0. |
183 | unsigned isDescribedByReg() const { |
184 | if (Kind == RegisterKind) |
185 | return Loc.RegNo; |
186 | return 0; |
187 | } |
188 | |
189 | /// Determine whether the lexical scope of this value's debug location |
190 | /// dominates MBB. |
191 | bool dominates(MachineBasicBlock &MBB) const { return UVS.dominates(&MBB); } |
192 | |
193 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
194 | LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void dump() const { MI.dump(); } |
195 | #endif |
196 | |
197 | bool operator==(const VarLoc &Other) const { |
198 | return Var == Other.Var && Loc.Hash == Other.Loc.Hash; |
199 | } |
200 | |
201 | /// This operator guarantees that VarLocs are sorted by Variable first. |
202 | bool operator<(const VarLoc &Other) const { |
203 | if (Var == Other.Var) |
204 | return Loc.Hash < Other.Loc.Hash; |
205 | return Var < Other.Var; |
206 | } |
207 | }; |
208 | |
209 | using VarLocMap = UniqueVector<VarLoc>; |
210 | using VarLocSet = SparseBitVector<>; |
211 | using VarLocInMBB = SmallDenseMap<const MachineBasicBlock *, VarLocSet>; |
212 | struct TransferDebugPair { |
213 | MachineInstr *TransferInst; |
214 | MachineInstr *DebugInst; |
215 | }; |
216 | using TransferMap = SmallVector<TransferDebugPair, 4>; |
217 | |
218 | /// This holds the working set of currently open ranges. For fast |
219 | /// access, this is done both as a set of VarLocIDs, and a map of |
220 | /// DebugVariable to recent VarLocID. Note that a DBG_VALUE ends all |
221 | /// previous open ranges for the same variable. |
222 | class OpenRangesSet { |
223 | VarLocSet VarLocs; |
224 | SmallDenseMap<DebugVariableBase, unsigned, 8> Vars; |
225 | |
226 | public: |
227 | const VarLocSet &getVarLocs() const { return VarLocs; } |
228 | |
229 | /// Terminate all open ranges for Var by removing it from the set. |
230 | void erase(DebugVariable Var) { |
231 | auto It = Vars.find(Var); |
232 | if (It != Vars.end()) { |
233 | unsigned ID = It->second; |
234 | VarLocs.reset(ID); |
235 | Vars.erase(It); |
236 | } |
237 | } |
238 | |
239 | /// Terminate all open ranges listed in \c KillSet by removing |
240 | /// them from the set. |
241 | void erase(const VarLocSet &KillSet, const VarLocMap &VarLocIDs) { |
242 | VarLocs.intersectWithComplement(KillSet); |
243 | for (unsigned ID : KillSet) |
244 | Vars.erase(VarLocIDs[ID].Var); |
245 | } |
246 | |
247 | /// Insert a new range into the set. |
248 | void insert(unsigned VarLocID, DebugVariableBase Var) { |
249 | VarLocs.set(VarLocID); |
250 | Vars.insert({Var, VarLocID}); |
251 | } |
252 | |
253 | /// Empty the set. |
254 | void clear() { |
255 | VarLocs.clear(); |
256 | Vars.clear(); |
257 | } |
258 | |
259 | /// Return whether the set is empty or not. |
260 | bool empty() const { |
261 | 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\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/CodeGen/LiveDebugValues.cpp" , 261, __PRETTY_FUNCTION__)); |
262 | return VarLocs.empty(); |
263 | } |
264 | }; |
265 | |
266 | bool isSpillInstruction(const MachineInstr &MI, MachineFunction *MF, |
267 | unsigned &Reg); |
268 | /// If a given instruction is identified as a spill, return the spill location |
269 | /// and set \p Reg to the spilled register. |
270 | Optional<VarLoc::SpillLoc> isRestoreInstruction(const MachineInstr &MI, |
271 | MachineFunction *MF, |
272 | unsigned &Reg); |
273 | /// Given a spill instruction, extract the register and offset used to |
274 | /// address the spill location in a target independent way. |
275 | VarLoc::SpillLoc extractSpillBaseRegAndOffset(const MachineInstr &MI); |
276 | void insertTransferDebugPair(MachineInstr &MI, OpenRangesSet &OpenRanges, |
277 | TransferMap &Transfers, VarLocMap &VarLocIDs, |
278 | unsigned OldVarID, TransferKind Kind, |
279 | unsigned NewReg = 0); |
280 | |
281 | void transferDebugValue(const MachineInstr &MI, OpenRangesSet &OpenRanges, |
282 | VarLocMap &VarLocIDs); |
283 | void transferSpillOrRestoreInst(MachineInstr &MI, OpenRangesSet &OpenRanges, |
284 | VarLocMap &VarLocIDs, TransferMap &Transfers); |
285 | void transferRegisterCopy(MachineInstr &MI, OpenRangesSet &OpenRanges, |
286 | VarLocMap &VarLocIDs, TransferMap &Transfers); |
287 | void transferRegisterDef(MachineInstr &MI, OpenRangesSet &OpenRanges, |
288 | const VarLocMap &VarLocIDs); |
289 | bool transferTerminatorInst(MachineInstr &MI, OpenRangesSet &OpenRanges, |
290 | VarLocInMBB &OutLocs, const VarLocMap &VarLocIDs); |
291 | |
292 | bool process(MachineInstr &MI, OpenRangesSet &OpenRanges, |
293 | VarLocInMBB &OutLocs, VarLocMap &VarLocIDs, |
294 | TransferMap &Transfers, bool transferChanges); |
295 | |
296 | bool join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs, VarLocInMBB &InLocs, |
297 | const VarLocMap &VarLocIDs, |
298 | SmallPtrSet<const MachineBasicBlock *, 16> &Visited, |
299 | SmallPtrSetImpl<const MachineBasicBlock *> &ArtificialBlocks); |
300 | |
301 | bool ExtendRanges(MachineFunction &MF); |
302 | |
303 | public: |
304 | static char ID; |
305 | |
306 | /// Default construct and initialize the pass. |
307 | LiveDebugValues(); |
308 | |
309 | /// Tell the pass manager which passes we depend on and what |
310 | /// information we preserve. |
311 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
312 | |
313 | MachineFunctionProperties getRequiredProperties() const override { |
314 | return MachineFunctionProperties().set( |
315 | MachineFunctionProperties::Property::NoVRegs); |
316 | } |
317 | |
318 | /// Print to ostream with a message. |
319 | void printVarLocInMBB(const MachineFunction &MF, const VarLocInMBB &V, |
320 | const VarLocMap &VarLocIDs, const char *msg, |
321 | raw_ostream &Out) const; |
322 | |
323 | /// Calculate the liveness information for the given machine function. |
324 | bool runOnMachineFunction(MachineFunction &MF) override; |
325 | }; |
326 | |
327 | } // end anonymous namespace |
328 | |
329 | //===----------------------------------------------------------------------===// |
330 | // Implementation |
331 | //===----------------------------------------------------------------------===// |
332 | |
333 | char LiveDebugValues::ID = 0; |
334 | |
335 | char &llvm::LiveDebugValuesID = LiveDebugValues::ID; |
336 | |
337 | INITIALIZE_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)); } |
338 | 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)); } |
339 | |
340 | /// Default construct and initialize the pass. |
341 | LiveDebugValues::LiveDebugValues() : MachineFunctionPass(ID) { |
342 | initializeLiveDebugValuesPass(*PassRegistry::getPassRegistry()); |
343 | } |
344 | |
345 | /// Tell the pass manager which passes we depend on and what information we |
346 | /// preserve. |
347 | void LiveDebugValues::getAnalysisUsage(AnalysisUsage &AU) const { |
348 | AU.setPreservesCFG(); |
349 | MachineFunctionPass::getAnalysisUsage(AU); |
350 | } |
351 | |
352 | //===----------------------------------------------------------------------===// |
353 | // Debug Range Extension Implementation |
354 | //===----------------------------------------------------------------------===// |
355 | |
356 | #ifndef NDEBUG |
357 | void LiveDebugValues::printVarLocInMBB(const MachineFunction &MF, |
358 | const VarLocInMBB &V, |
359 | const VarLocMap &VarLocIDs, |
360 | const char *msg, |
361 | raw_ostream &Out) const { |
362 | Out << '\n' << msg << '\n'; |
363 | for (const MachineBasicBlock &BB : MF) { |
364 | const VarLocSet &L = V.lookup(&BB); |
365 | if (L.empty()) |
366 | continue; |
367 | Out << "MBB: " << BB.getNumber() << ":\n"; |
368 | for (unsigned VLL : L) { |
369 | const VarLoc &VL = VarLocIDs[VLL]; |
370 | Out << " Var: " << VL.Var.getVar()->getName(); |
371 | Out << " MI: "; |
372 | VL.dump(); |
373 | } |
374 | } |
375 | Out << "\n"; |
376 | } |
377 | #endif |
378 | |
379 | LiveDebugValues::VarLoc::SpillLoc |
380 | LiveDebugValues::extractSpillBaseRegAndOffset(const MachineInstr &MI) { |
381 | assert(MI.hasOneMemOperand() &&((MI.hasOneMemOperand() && "Spill instruction does not have exactly one memory operand?" ) ? static_cast<void> (0) : __assert_fail ("MI.hasOneMemOperand() && \"Spill instruction does not have exactly one memory operand?\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/CodeGen/LiveDebugValues.cpp" , 382, __PRETTY_FUNCTION__)) |
382 | "Spill instruction does not have exactly one memory operand?")((MI.hasOneMemOperand() && "Spill instruction does not have exactly one memory operand?" ) ? static_cast<void> (0) : __assert_fail ("MI.hasOneMemOperand() && \"Spill instruction does not have exactly one memory operand?\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/CodeGen/LiveDebugValues.cpp" , 382, __PRETTY_FUNCTION__)); |
383 | auto MMOI = MI.memoperands_begin(); |
384 | const PseudoSourceValue *PVal = (*MMOI)->getPseudoValue(); |
385 | assert(PVal->kind() == PseudoSourceValue::FixedStack &&((PVal->kind() == PseudoSourceValue::FixedStack && "Inconsistent memory operand in spill instruction") ? static_cast <void> (0) : __assert_fail ("PVal->kind() == PseudoSourceValue::FixedStack && \"Inconsistent memory operand in spill instruction\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/CodeGen/LiveDebugValues.cpp" , 386, __PRETTY_FUNCTION__)) |
386 | "Inconsistent memory operand in spill instruction")((PVal->kind() == PseudoSourceValue::FixedStack && "Inconsistent memory operand in spill instruction") ? static_cast <void> (0) : __assert_fail ("PVal->kind() == PseudoSourceValue::FixedStack && \"Inconsistent memory operand in spill instruction\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/CodeGen/LiveDebugValues.cpp" , 386, __PRETTY_FUNCTION__)); |
387 | int FI = cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex(); |
388 | const MachineBasicBlock *MBB = MI.getParent(); |
389 | unsigned Reg; |
390 | int Offset = TFI->getFrameIndexReference(*MBB->getParent(), FI, Reg); |
391 | return {Reg, Offset}; |
392 | } |
393 | |
394 | /// End all previous ranges related to @MI and start a new range from @MI |
395 | /// if it is a DBG_VALUE instr. |
396 | void LiveDebugValues::transferDebugValue(const MachineInstr &MI, |
397 | OpenRangesSet &OpenRanges, |
398 | VarLocMap &VarLocIDs) { |
399 | if (!MI.isDebugValue()) |
400 | return; |
401 | const DILocalVariable *Var = MI.getDebugVariable(); |
402 | const DILocation *DebugLoc = MI.getDebugLoc(); |
403 | const DILocation *InlinedAt = DebugLoc->getInlinedAt(); |
404 | 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\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/CodeGen/LiveDebugValues.cpp" , 405, __PRETTY_FUNCTION__)) |
405 | "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\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/CodeGen/LiveDebugValues.cpp" , 405, __PRETTY_FUNCTION__)); |
406 | |
407 | // End all previous ranges of Var. |
408 | DebugVariable V(Var, InlinedAt); |
409 | OpenRanges.erase(V); |
410 | |
411 | // Add the VarLoc to OpenRanges from this DBG_VALUE. |
412 | // TODO: Currently handles DBG_VALUE which has only reg as location. |
413 | if (isDbgValueDescribedByReg(MI)) { |
414 | VarLoc VL(MI, LS); |
415 | unsigned ID = VarLocIDs.insert(VL); |
416 | OpenRanges.insert(ID, VL.Var); |
417 | } |
418 | } |
419 | |
420 | /// Create new TransferDebugPair and insert it in \p Transfers. The VarLoc |
421 | /// with \p OldVarID should be deleted form \p OpenRanges and replaced with |
422 | /// new VarLoc. If \p NewReg is different than default zero value then the |
423 | /// new location will be register location created by the copy like instruction, |
424 | /// otherwise it is variable's location on the stack. |
425 | void LiveDebugValues::insertTransferDebugPair( |
426 | MachineInstr &MI, OpenRangesSet &OpenRanges, TransferMap &Transfers, |
427 | VarLocMap &VarLocIDs, unsigned OldVarID, TransferKind Kind, |
428 | unsigned NewReg) { |
429 | const MachineInstr *DebugInstr = &VarLocIDs[OldVarID].MI; |
430 | MachineFunction *MF = MI.getParent()->getParent(); |
431 | MachineInstr *NewDebugInstr; |
432 | |
433 | auto ProcessVarLoc = [&MI, &OpenRanges, &Transfers, &DebugInstr, |
434 | &VarLocIDs](VarLoc &VL, MachineInstr *NewDebugInstr) { |
435 | unsigned LocId = VarLocIDs.insert(VL); |
436 | |
437 | // Close this variable's previous location range. |
438 | DebugVariable V(DebugInstr->getDebugVariable(), |
439 | DebugInstr->getDebugLoc()->getInlinedAt()); |
440 | OpenRanges.erase(V); |
441 | |
442 | OpenRanges.insert(LocId, VL.Var); |
443 | // The newly created DBG_VALUE instruction NewDebugInstr must be inserted |
444 | // after MI. Keep track of the pairing. |
445 | TransferDebugPair MIP = {&MI, NewDebugInstr}; |
446 | Transfers.push_back(MIP); |
447 | }; |
448 | |
449 | // End all previous ranges of Var. |
450 | OpenRanges.erase(VarLocIDs[OldVarID].Var); |
451 | switch (Kind) { |
452 | case TransferKind::TransferCopy: { |
453 | assert(NewReg &&((NewReg && "No register supplied when handling a copy of a debug value" ) ? static_cast<void> (0) : __assert_fail ("NewReg && \"No register supplied when handling a copy of a debug value\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/CodeGen/LiveDebugValues.cpp" , 454, __PRETTY_FUNCTION__)) |
454 | "No register supplied when handling a copy of a debug value")((NewReg && "No register supplied when handling a copy of a debug value" ) ? static_cast<void> (0) : __assert_fail ("NewReg && \"No register supplied when handling a copy of a debug value\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/CodeGen/LiveDebugValues.cpp" , 454, __PRETTY_FUNCTION__)); |
455 | // Create a DBG_VALUE instruction to describe the Var in its new |
456 | // register location. |
457 | NewDebugInstr = BuildMI( |
458 | *MF, DebugInstr->getDebugLoc(), DebugInstr->getDesc(), |
459 | DebugInstr->isIndirectDebugValue(), NewReg, |
460 | DebugInstr->getDebugVariable(), DebugInstr->getDebugExpression()); |
461 | if (DebugInstr->isIndirectDebugValue()) |
462 | NewDebugInstr->getOperand(1).setImm(DebugInstr->getOperand(1).getImm()); |
463 | VarLoc VL(*NewDebugInstr, LS); |
464 | ProcessVarLoc(VL, NewDebugInstr); |
465 | LLVM_DEBUG(dbgs() << "Creating DBG_VALUE inst for register copy: ";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { dbgs() << "Creating DBG_VALUE inst for register copy: " ; NewDebugInstr->print(dbgs(), false, false, false, true, TII ); } } while (false) |
466 | NewDebugInstr->print(dbgs(), /*IsStandalone*/false,do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { dbgs() << "Creating DBG_VALUE inst for register copy: " ; NewDebugInstr->print(dbgs(), false, false, false, true, TII ); } } while (false) |
467 | /*SkipOpers*/false, /*SkipDebugLoc*/false,do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { dbgs() << "Creating DBG_VALUE inst for register copy: " ; NewDebugInstr->print(dbgs(), false, false, false, true, TII ); } } while (false) |
468 | /*AddNewLine*/true, TII))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { dbgs() << "Creating DBG_VALUE inst for register copy: " ; NewDebugInstr->print(dbgs(), false, false, false, true, TII ); } } while (false); |
469 | return; |
470 | } |
471 | case TransferKind::TransferSpill: { |
472 | // Create a DBG_VALUE instruction to describe the Var in its spilled |
473 | // location. |
474 | VarLoc::SpillLoc SpillLocation = extractSpillBaseRegAndOffset(MI); |
475 | auto *SpillExpr = DIExpression::prepend(DebugInstr->getDebugExpression(), |
476 | DIExpression::ApplyOffset, |
477 | SpillLocation.SpillOffset); |
478 | NewDebugInstr = BuildMI( |
479 | *MF, DebugInstr->getDebugLoc(), DebugInstr->getDesc(), true, |
480 | SpillLocation.SpillBase, DebugInstr->getDebugVariable(), SpillExpr); |
481 | VarLoc VL(*NewDebugInstr, SpillLocation.SpillBase, |
482 | SpillLocation.SpillOffset, LS); |
483 | ProcessVarLoc(VL, NewDebugInstr); |
484 | LLVM_DEBUG(dbgs() << "Creating DBG_VALUE inst for spill: ";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { dbgs() << "Creating DBG_VALUE inst for spill: " ; NewDebugInstr->print(dbgs(), false, false, false, true, TII ); } } while (false) |
485 | NewDebugInstr->print(dbgs(), /*IsStandalone*/false,do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { dbgs() << "Creating DBG_VALUE inst for spill: " ; NewDebugInstr->print(dbgs(), false, false, false, true, TII ); } } while (false) |
486 | /*SkipOpers*/false, /*SkipDebugLoc*/false,do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { dbgs() << "Creating DBG_VALUE inst for spill: " ; NewDebugInstr->print(dbgs(), false, false, false, true, TII ); } } while (false) |
487 | /*AddNewLine*/true, TII))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { dbgs() << "Creating DBG_VALUE inst for spill: " ; NewDebugInstr->print(dbgs(), false, false, false, true, TII ); } } while (false); |
488 | return; |
489 | } |
490 | case TransferKind::TransferRestore: { |
491 | assert(NewReg &&((NewReg && "No register supplied when handling a restore of a debug value" ) ? static_cast<void> (0) : __assert_fail ("NewReg && \"No register supplied when handling a restore of a debug value\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/CodeGen/LiveDebugValues.cpp" , 492, __PRETTY_FUNCTION__)) |
492 | "No register supplied when handling a restore of a debug value")((NewReg && "No register supplied when handling a restore of a debug value" ) ? static_cast<void> (0) : __assert_fail ("NewReg && \"No register supplied when handling a restore of a debug value\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/CodeGen/LiveDebugValues.cpp" , 492, __PRETTY_FUNCTION__)); |
493 | MachineFunction *MF = MI.getMF(); |
494 | DIBuilder DIB(*const_cast<Function &>(MF->getFunction()).getParent()); |
495 | NewDebugInstr = |
496 | BuildMI(*MF, DebugInstr->getDebugLoc(), DebugInstr->getDesc(), false, |
497 | NewReg, DebugInstr->getDebugVariable(), DIB.createExpression()); |
498 | VarLoc VL(*NewDebugInstr, LS); |
499 | ProcessVarLoc(VL, NewDebugInstr); |
500 | LLVM_DEBUG(dbgs() << "Creating DBG_VALUE inst for register restore: ";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { dbgs() << "Creating DBG_VALUE inst for register restore: " ; NewDebugInstr->print(dbgs(), false, false, false, true, TII ); } } while (false) |
501 | NewDebugInstr->print(dbgs(), /*IsStandalone*/false,do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { dbgs() << "Creating DBG_VALUE inst for register restore: " ; NewDebugInstr->print(dbgs(), false, false, false, true, TII ); } } while (false) |
502 | /*SkipOpers*/false, /*SkipDebugLoc*/false,do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { dbgs() << "Creating DBG_VALUE inst for register restore: " ; NewDebugInstr->print(dbgs(), false, false, false, true, TII ); } } while (false) |
503 | /*AddNewLine*/true, TII))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { dbgs() << "Creating DBG_VALUE inst for register restore: " ; NewDebugInstr->print(dbgs(), false, false, false, true, TII ); } } while (false); |
504 | return; |
505 | } |
506 | } |
507 | llvm_unreachable("Invalid transfer kind")::llvm::llvm_unreachable_internal("Invalid transfer kind", "/build/llvm-toolchain-snapshot-9~svn362543/lib/CodeGen/LiveDebugValues.cpp" , 507); |
508 | } |
509 | |
510 | /// A definition of a register may mark the end of a range. |
511 | void LiveDebugValues::transferRegisterDef(MachineInstr &MI, |
512 | OpenRangesSet &OpenRanges, |
513 | const VarLocMap &VarLocIDs) { |
514 | MachineFunction *MF = MI.getMF(); |
515 | const TargetLowering *TLI = MF->getSubtarget().getTargetLowering(); |
516 | unsigned SP = TLI->getStackPointerRegisterToSaveRestore(); |
517 | SparseBitVector<> KillSet; |
518 | for (const MachineOperand &MO : MI.operands()) { |
519 | // Determine whether the operand is a register def. Assume that call |
520 | // instructions never clobber SP, because some backends (e.g., AArch64) |
521 | // never list SP in the regmask. |
522 | if (MO.isReg() && MO.isDef() && MO.getReg() && |
523 | TRI->isPhysicalRegister(MO.getReg()) && |
524 | !(MI.isCall() && MO.getReg() == SP)) { |
525 | // Remove ranges of all aliased registers. |
526 | for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI) |
527 | for (unsigned ID : OpenRanges.getVarLocs()) |
528 | if (VarLocIDs[ID].isDescribedByReg() == *RAI) |
529 | KillSet.set(ID); |
530 | } else if (MO.isRegMask()) { |
531 | // Remove ranges of all clobbered registers. Register masks don't usually |
532 | // list SP as preserved. While the debug info may be off for an |
533 | // instruction or two around callee-cleanup calls, transferring the |
534 | // DEBUG_VALUE across the call is still a better user experience. |
535 | for (unsigned ID : OpenRanges.getVarLocs()) { |
536 | unsigned Reg = VarLocIDs[ID].isDescribedByReg(); |
537 | if (Reg && Reg != SP && MO.clobbersPhysReg(Reg)) |
538 | KillSet.set(ID); |
539 | } |
540 | } |
541 | } |
542 | OpenRanges.erase(KillSet, VarLocIDs); |
543 | } |
544 | |
545 | /// Decide if @MI is a spill instruction and return true if it is. We use 2 |
546 | /// criteria to make this decision: |
547 | /// - Is this instruction a store to a spill slot? |
548 | /// - Is there a register operand that is both used and killed? |
549 | /// TODO: Store optimization can fold spills into other stores (including |
550 | /// other spills). We do not handle this yet (more than one memory operand). |
551 | bool LiveDebugValues::isSpillInstruction(const MachineInstr &MI, |
552 | MachineFunction *MF, unsigned &Reg) { |
553 | SmallVector<const MachineMemOperand*, 1> Accesses; |
554 | |
555 | // TODO: Handle multiple stores folded into one. |
556 | if (!MI.hasOneMemOperand()) |
557 | return false; |
558 | |
559 | if (!MI.getSpillSize(TII) && !MI.getFoldedSpillSize(TII)) |
560 | return false; // This is not a spill instruction, since no valid size was |
561 | // returned from either function. |
562 | |
563 | auto isKilledReg = [&](const MachineOperand MO, unsigned &Reg) { |
564 | if (!MO.isReg() || !MO.isUse()) { |
565 | Reg = 0; |
566 | return false; |
567 | } |
568 | Reg = MO.getReg(); |
569 | return MO.isKill(); |
570 | }; |
571 | |
572 | for (const MachineOperand &MO : MI.operands()) { |
573 | // In a spill instruction generated by the InlineSpiller the spilled |
574 | // register has its kill flag set. |
575 | if (isKilledReg(MO, Reg)) |
576 | return true; |
577 | if (Reg != 0) { |
578 | // Check whether next instruction kills the spilled register. |
579 | // FIXME: Current solution does not cover search for killed register in |
580 | // bundles and instructions further down the chain. |
581 | auto NextI = std::next(MI.getIterator()); |
582 | // Skip next instruction that points to basic block end iterator. |
583 | if (MI.getParent()->end() == NextI) |
584 | continue; |
585 | unsigned RegNext; |
586 | for (const MachineOperand &MONext : NextI->operands()) { |
587 | // Return true if we came across the register from the |
588 | // previous spill instruction that is killed in NextI. |
589 | if (isKilledReg(MONext, RegNext) && RegNext == Reg) |
590 | return true; |
591 | } |
592 | } |
593 | } |
594 | // Return false if we didn't find spilled register. |
595 | return false; |
596 | } |
597 | |
598 | Optional<LiveDebugValues::VarLoc::SpillLoc> |
599 | LiveDebugValues::isRestoreInstruction(const MachineInstr &MI, |
600 | MachineFunction *MF, unsigned &Reg) { |
601 | if (!MI.hasOneMemOperand()) |
602 | return None; |
603 | |
604 | // FIXME: Handle folded restore instructions with more than one memory |
605 | // operand. |
606 | if (MI.getRestoreSize(TII)) { |
607 | Reg = MI.getOperand(0).getReg(); |
608 | return extractSpillBaseRegAndOffset(MI); |
609 | } |
610 | return None; |
611 | } |
612 | |
613 | /// A spilled register may indicate that we have to end the current range of |
614 | /// a variable and create a new one for the spill location. |
615 | /// A restored register may indicate the reverse situation. |
616 | /// We don't want to insert any instructions in process(), so we just create |
617 | /// the DBG_VALUE without inserting it and keep track of it in \p Transfers. |
618 | /// It will be inserted into the BB when we're done iterating over the |
619 | /// instructions. |
620 | void LiveDebugValues::transferSpillOrRestoreInst(MachineInstr &MI, |
621 | OpenRangesSet &OpenRanges, |
622 | VarLocMap &VarLocIDs, |
623 | TransferMap &Transfers) { |
624 | MachineFunction *MF = MI.getMF(); |
625 | TransferKind TKind; |
626 | unsigned Reg; |
627 | Optional<VarLoc::SpillLoc> Loc; |
628 | |
629 | LLVM_DEBUG(dbgs() << "Examining instruction: "; MI.dump();)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { dbgs() << "Examining instruction: " ; MI.dump();; } } while (false); |
630 | |
631 | if (isSpillInstruction(MI, MF, Reg)) { |
632 | TKind = TransferKind::TransferSpill; |
633 | LLVM_DEBUG(dbgs() << "Recognized as spill: "; MI.dump();)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { dbgs() << "Recognized as spill: " ; MI.dump();; } } while (false); |
634 | LLVM_DEBUG(dbgs() << "Register: " << Reg << " " << printReg(Reg, TRI)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { dbgs() << "Register: " << Reg << " " << printReg(Reg, TRI) << "\n"; } } while (false) |
635 | << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { dbgs() << "Register: " << Reg << " " << printReg(Reg, TRI) << "\n"; } } while (false); |
636 | } else { |
637 | if (!(Loc = isRestoreInstruction(MI, MF, Reg))) |
638 | return; |
639 | TKind = TransferKind::TransferRestore; |
640 | LLVM_DEBUG(dbgs() << "Recognized as restore: "; MI.dump();)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { dbgs() << "Recognized as restore: " ; MI.dump();; } } while (false); |
641 | LLVM_DEBUG(dbgs() << "Register: " << Reg << " " << printReg(Reg, TRI)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { dbgs() << "Register: " << Reg << " " << printReg(Reg, TRI) << "\n"; } } while (false) |
642 | << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { dbgs() << "Register: " << Reg << " " << printReg(Reg, TRI) << "\n"; } } while (false); |
643 | } |
644 | // Check if the register or spill location is the location of a debug value. |
645 | for (unsigned ID : OpenRanges.getVarLocs()) { |
646 | if (TKind == TransferKind::TransferSpill && |
647 | VarLocIDs[ID].isDescribedByReg() == Reg) { |
648 | 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) |
649 | << 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); |
650 | } else if (TKind == TransferKind::TransferRestore && |
651 | VarLocIDs[ID].Loc.SpillLocation == *Loc) { |
652 | LLVM_DEBUG(dbgs() << "Restoring Register " << printReg(Reg, TRI) << '('do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { dbgs() << "Restoring Register " << printReg(Reg, TRI) << '(' << VarLocIDs[ID].Var.getVar ()->getName() << ")\n"; } } while (false) |
653 | << VarLocIDs[ID].Var.getVar()->getName() << ")\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { dbgs() << "Restoring Register " << printReg(Reg, TRI) << '(' << VarLocIDs[ID].Var.getVar ()->getName() << ")\n"; } } while (false); |
654 | } else |
655 | continue; |
656 | insertTransferDebugPair(MI, OpenRanges, Transfers, VarLocIDs, ID, TKind, |
657 | Reg); |
658 | return; |
659 | } |
660 | } |
661 | |
662 | /// If \p MI is a register copy instruction, that copies a previously tracked |
663 | /// value from one register to another register that is callee saved, we |
664 | /// create new DBG_VALUE instruction described with copy destination register. |
665 | void LiveDebugValues::transferRegisterCopy(MachineInstr &MI, |
666 | OpenRangesSet &OpenRanges, |
667 | VarLocMap &VarLocIDs, |
668 | TransferMap &Transfers) { |
669 | const MachineOperand *SrcRegOp, *DestRegOp; |
670 | |
671 | if (!TII->isCopyInstr(MI, SrcRegOp, DestRegOp) || !SrcRegOp->isKill() || |
672 | !DestRegOp->isDef()) |
673 | return; |
674 | |
675 | auto isCalleSavedReg = [&](unsigned Reg) { |
676 | for (MCRegAliasIterator RAI(Reg, TRI, true); RAI.isValid(); ++RAI) |
677 | if (CalleeSavedRegs.test(*RAI)) |
678 | return true; |
679 | return false; |
680 | }; |
681 | |
682 | unsigned SrcReg = SrcRegOp->getReg(); |
683 | unsigned DestReg = DestRegOp->getReg(); |
684 | |
685 | // We want to recognize instructions where destination register is callee |
686 | // saved register. If register that could be clobbered by the call is |
687 | // included, there would be a great chance that it is going to be clobbered |
688 | // soon. It is more likely that previous register location, which is callee |
689 | // saved, is going to stay unclobbered longer, even if it is killed. |
690 | if (!isCalleSavedReg(DestReg)) |
691 | return; |
692 | |
693 | for (unsigned ID : OpenRanges.getVarLocs()) { |
694 | if (VarLocIDs[ID].isDescribedByReg() == SrcReg) { |
695 | insertTransferDebugPair(MI, OpenRanges, Transfers, VarLocIDs, ID, |
696 | TransferKind::TransferCopy, DestReg); |
697 | return; |
698 | } |
699 | } |
700 | } |
701 | |
702 | /// Terminate all open ranges at the end of the current basic block. |
703 | bool LiveDebugValues::transferTerminatorInst(MachineInstr &MI, |
704 | OpenRangesSet &OpenRanges, |
705 | VarLocInMBB &OutLocs, |
706 | const VarLocMap &VarLocIDs) { |
707 | bool Changed = false; |
708 | const MachineBasicBlock *CurMBB = MI.getParent(); |
709 | if (!(MI.isTerminator() || (&MI == &CurMBB->back()))) |
710 | return false; |
711 | |
712 | if (OpenRanges.empty()) |
713 | return false; |
714 | |
715 | LLVM_DEBUG(for (unsigned IDdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { for (unsigned ID : OpenRanges.getVarLocs ()) { dbgs() << "Add to OutLocs in MBB #" << CurMBB ->getNumber() << ": "; VarLocIDs[ID].dump(); }; } } while (false) |
716 | : OpenRanges.getVarLocs()) {do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { for (unsigned ID : OpenRanges.getVarLocs ()) { dbgs() << "Add to OutLocs in MBB #" << CurMBB ->getNumber() << ": "; VarLocIDs[ID].dump(); }; } } while (false) |
717 | // Copy OpenRanges to OutLocs, if not already present.do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { for (unsigned ID : OpenRanges.getVarLocs ()) { dbgs() << "Add to OutLocs in MBB #" << CurMBB ->getNumber() << ": "; VarLocIDs[ID].dump(); }; } } while (false) |
718 | dbgs() << "Add to OutLocs in MBB #" << CurMBB->getNumber() << ": ";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { for (unsigned ID : OpenRanges.getVarLocs ()) { dbgs() << "Add to OutLocs in MBB #" << CurMBB ->getNumber() << ": "; VarLocIDs[ID].dump(); }; } } while (false) |
719 | VarLocIDs[ID].dump();do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { for (unsigned ID : OpenRanges.getVarLocs ()) { dbgs() << "Add to OutLocs in MBB #" << CurMBB ->getNumber() << ": "; VarLocIDs[ID].dump(); }; } } while (false) |
720 | })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { for (unsigned ID : OpenRanges.getVarLocs ()) { dbgs() << "Add to OutLocs in MBB #" << CurMBB ->getNumber() << ": "; VarLocIDs[ID].dump(); }; } } while (false); |
721 | VarLocSet &VLS = OutLocs[CurMBB]; |
722 | Changed = VLS |= OpenRanges.getVarLocs(); |
723 | // New OutLocs set may be different due to spill, restore or register |
724 | // copy instruction processing. |
725 | if (Changed) |
726 | VLS = OpenRanges.getVarLocs(); |
727 | OpenRanges.clear(); |
728 | return Changed; |
729 | } |
730 | |
731 | /// This routine creates OpenRanges and OutLocs. |
732 | bool LiveDebugValues::process(MachineInstr &MI, OpenRangesSet &OpenRanges, |
733 | VarLocInMBB &OutLocs, VarLocMap &VarLocIDs, |
734 | TransferMap &Transfers, bool transferChanges) { |
735 | bool Changed = false; |
736 | transferDebugValue(MI, OpenRanges, VarLocIDs); |
737 | transferRegisterDef(MI, OpenRanges, VarLocIDs); |
738 | if (transferChanges) { |
739 | transferRegisterCopy(MI, OpenRanges, VarLocIDs, Transfers); |
740 | transferSpillOrRestoreInst(MI, OpenRanges, VarLocIDs, Transfers); |
741 | } |
742 | Changed = transferTerminatorInst(MI, OpenRanges, OutLocs, VarLocIDs); |
743 | return Changed; |
744 | } |
745 | |
746 | /// This routine joins the analysis results of all incoming edges in @MBB by |
747 | /// inserting a new DBG_VALUE instruction at the start of the @MBB - if the same |
748 | /// source variable in all the predecessors of @MBB reside in the same location. |
749 | bool LiveDebugValues::join( |
750 | MachineBasicBlock &MBB, VarLocInMBB &OutLocs, VarLocInMBB &InLocs, |
751 | const VarLocMap &VarLocIDs, |
752 | SmallPtrSet<const MachineBasicBlock *, 16> &Visited, |
753 | SmallPtrSetImpl<const MachineBasicBlock *> &ArtificialBlocks) { |
754 | LLVM_DEBUG(dbgs() << "join MBB: " << MBB.getNumber() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { dbgs() << "join MBB: " << MBB .getNumber() << "\n"; } } while (false); |
755 | bool Changed = false; |
756 | |
757 | VarLocSet InLocsT; // Temporary incoming locations. |
758 | |
759 | // For all predecessors of this MBB, find the set of VarLocs that |
760 | // can be joined. |
761 | int NumVisited = 0; |
762 | for (auto p : MBB.predecessors()) { |
763 | // Ignore unvisited predecessor blocks. As we are processing |
764 | // the blocks in reverse post-order any unvisited block can |
765 | // be considered to not remove any incoming values. |
766 | if (!Visited.count(p)) { |
767 | LLVM_DEBUG(dbgs() << " ignoring unvisited pred MBB: " << p->getNumber()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { dbgs() << " ignoring unvisited pred MBB: " << p->getNumber() << "\n"; } } while (false) |
768 | << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { dbgs() << " ignoring unvisited pred MBB: " << p->getNumber() << "\n"; } } while (false); |
769 | continue; |
770 | } |
771 | auto OL = OutLocs.find(p); |
772 | // Join is null in case of empty OutLocs from any of the pred. |
773 | if (OL == OutLocs.end()) |
774 | return false; |
775 | |
776 | // Just copy over the Out locs to incoming locs for the first visited |
777 | // predecessor, and for all other predecessors join the Out locs. |
778 | if (!NumVisited) |
779 | InLocsT = OL->second; |
780 | else |
781 | InLocsT &= OL->second; |
782 | |
783 | LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { { if (!InLocsT.empty()) { for (auto ID : InLocsT) dbgs() << " gathered candidate incoming var: " << VarLocIDs[ID].Var.getVar()->getName() << "\n" ; } }; } } while (false) |
784 | if (!InLocsT.empty()) {do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { { if (!InLocsT.empty()) { for (auto ID : InLocsT) dbgs() << " gathered candidate incoming var: " << VarLocIDs[ID].Var.getVar()->getName() << "\n" ; } }; } } while (false) |
785 | for (auto ID : InLocsT)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { { if (!InLocsT.empty()) { for (auto ID : InLocsT) dbgs() << " gathered candidate incoming var: " << VarLocIDs[ID].Var.getVar()->getName() << "\n" ; } }; } } while (false) |
786 | dbgs() << " gathered candidate incoming var: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { { if (!InLocsT.empty()) { for (auto ID : InLocsT) dbgs() << " gathered candidate incoming var: " << VarLocIDs[ID].Var.getVar()->getName() << "\n" ; } }; } } while (false) |
787 | << VarLocIDs[ID].Var.getVar()->getName() << "\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { { if (!InLocsT.empty()) { for (auto ID : InLocsT) dbgs() << " gathered candidate incoming var: " << VarLocIDs[ID].Var.getVar()->getName() << "\n" ; } }; } } while (false) |
788 | }do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { { if (!InLocsT.empty()) { for (auto ID : InLocsT) dbgs() << " gathered candidate incoming var: " << VarLocIDs[ID].Var.getVar()->getName() << "\n" ; } }; } } while (false) |
789 | })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { { if (!InLocsT.empty()) { for (auto ID : InLocsT) dbgs() << " gathered candidate incoming var: " << VarLocIDs[ID].Var.getVar()->getName() << "\n" ; } }; } } while (false); |
790 | |
791 | NumVisited++; |
792 | } |
793 | |
794 | // Filter out DBG_VALUES that are out of scope. |
795 | VarLocSet KillSet; |
796 | bool IsArtificial = ArtificialBlocks.count(&MBB); |
797 | if (!IsArtificial) { |
798 | for (auto ID : InLocsT) { |
799 | if (!VarLocIDs[ID].dominates(MBB)) { |
800 | KillSet.set(ID); |
801 | LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { { auto Name = VarLocIDs[ID].Var.getVar ()->getName(); dbgs() << " killing " << Name << ", it doesn't dominate MBB\n"; }; } } while (false) |
802 | auto Name = VarLocIDs[ID].Var.getVar()->getName();do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { { auto Name = VarLocIDs[ID].Var.getVar ()->getName(); dbgs() << " killing " << Name << ", it doesn't dominate MBB\n"; }; } } while (false) |
803 | dbgs() << " killing " << Name << ", it doesn't dominate MBB\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { { auto Name = VarLocIDs[ID].Var.getVar ()->getName(); dbgs() << " killing " << Name << ", it doesn't dominate MBB\n"; }; } } while (false) |
804 | })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { { auto Name = VarLocIDs[ID].Var.getVar ()->getName(); dbgs() << " killing " << Name << ", it doesn't dominate MBB\n"; }; } } while (false); |
805 | } |
806 | } |
807 | } |
808 | InLocsT.intersectWithComplement(KillSet); |
809 | |
810 | // As we are processing blocks in reverse post-order we |
811 | // should have processed at least one predecessor, unless it |
812 | // is the entry block which has no predecessor. |
813 | 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\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/CodeGen/LiveDebugValues.cpp" , 814, __PRETTY_FUNCTION__)) |
814 | "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\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/CodeGen/LiveDebugValues.cpp" , 814, __PRETTY_FUNCTION__)); |
815 | if (InLocsT.empty()) |
816 | return false; |
817 | |
818 | VarLocSet &ILS = InLocs[&MBB]; |
819 | |
820 | // Insert DBG_VALUE instructions, if not already inserted. |
821 | VarLocSet Diff = InLocsT; |
822 | Diff.intersectWithComplement(ILS); |
823 | for (auto ID : Diff) { |
824 | // This VarLoc is not found in InLocs i.e. it is not yet inserted. So, a |
825 | // new range is started for the var from the mbb's beginning by inserting |
826 | // a new DBG_VALUE. process() will end this range however appropriate. |
827 | const VarLoc &DiffIt = VarLocIDs[ID]; |
828 | const MachineInstr *DebugInstr = &DiffIt.MI; |
829 | MachineInstr *MI = BuildMI( |
830 | MBB, MBB.instr_begin(), DebugInstr->getDebugLoc(), |
831 | DebugInstr->getDesc(), DebugInstr->isIndirectDebugValue(), |
832 | DebugInstr->getOperand(0).getReg(), DebugInstr->getDebugVariable(), |
833 | DebugInstr->getDebugExpression()); |
834 | if (DebugInstr->isIndirectDebugValue()) |
835 | MI->getOperand(1).setImm(DebugInstr->getOperand(1).getImm()); |
836 | LLVM_DEBUG(dbgs() << "Inserted: "; MI->dump();)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { dbgs() << "Inserted: "; MI->dump ();; } } while (false); |
837 | ILS.set(ID); |
838 | ++NumInserted; |
839 | Changed = true; |
840 | } |
841 | return Changed; |
842 | } |
843 | |
844 | /// Calculate the liveness information for the given machine function and |
845 | /// extend ranges across basic blocks. |
846 | bool LiveDebugValues::ExtendRanges(MachineFunction &MF) { |
847 | LLVM_DEBUG(dbgs() << "\nDebug Range Extension\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { dbgs() << "\nDebug Range Extension\n" ; } } while (false); |
848 | |
849 | bool Changed = false; |
850 | bool OLChanged = false; |
851 | bool MBBJoined = false; |
852 | |
853 | VarLocMap VarLocIDs; // Map VarLoc<>unique ID for use in bitvectors. |
854 | OpenRangesSet OpenRanges; // Ranges that are open until end of bb. |
855 | VarLocInMBB OutLocs; // Ranges that exist beyond bb. |
856 | VarLocInMBB InLocs; // Ranges that are incoming after joining. |
857 | TransferMap Transfers; // DBG_VALUEs associated with spills. |
858 | |
859 | // Blocks which are artificial, i.e. blocks which exclusively contain |
860 | // instructions without locations, or with line 0 locations. |
861 | SmallPtrSet<const MachineBasicBlock *, 16> ArtificialBlocks; |
862 | |
863 | DenseMap<unsigned int, MachineBasicBlock *> OrderToBB; |
864 | DenseMap<MachineBasicBlock *, unsigned int> BBToOrder; |
865 | std::priority_queue<unsigned int, std::vector<unsigned int>, |
866 | std::greater<unsigned int>> |
867 | Worklist; |
868 | std::priority_queue<unsigned int, std::vector<unsigned int>, |
869 | std::greater<unsigned int>> |
870 | Pending; |
871 | |
872 | enum : bool { dontTransferChanges = false, transferChanges = true }; |
873 | |
874 | // Initialize every mbb with OutLocs. |
875 | // We are not looking at any spill instructions during the initial pass |
876 | // over the BBs. The LiveDebugVariables pass has already created DBG_VALUE |
877 | // instructions for spills of registers that are known to be user variables |
878 | // within the BB in which the spill occurs. |
879 | for (auto &MBB : MF) |
880 | for (auto &MI : MBB) |
881 | process(MI, OpenRanges, OutLocs, VarLocIDs, Transfers, |
882 | dontTransferChanges); |
883 | |
884 | auto hasNonArtificialLocation = [](const MachineInstr &MI) -> bool { |
885 | if (const DebugLoc &DL = MI.getDebugLoc()) |
886 | return DL.getLine() != 0; |
887 | return false; |
888 | }; |
889 | for (auto &MBB : MF) |
890 | if (none_of(MBB.instrs(), hasNonArtificialLocation)) |
891 | ArtificialBlocks.insert(&MBB); |
892 | |
893 | LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs,do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { printVarLocInMBB(MF, OutLocs, VarLocIDs , "OutLocs after initialization", dbgs()); } } while (false) |
894 | "OutLocs after initialization", dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { printVarLocInMBB(MF, OutLocs, VarLocIDs , "OutLocs after initialization", dbgs()); } } while (false); |
895 | |
896 | ReversePostOrderTraversal<MachineFunction *> RPOT(&MF); |
897 | unsigned int RPONumber = 0; |
898 | for (auto RI = RPOT.begin(), RE = RPOT.end(); RI != RE; ++RI) { |
899 | OrderToBB[RPONumber] = *RI; |
900 | BBToOrder[*RI] = RPONumber; |
901 | Worklist.push(RPONumber); |
902 | ++RPONumber; |
903 | } |
904 | // This is a standard "union of predecessor outs" dataflow problem. |
905 | // To solve it, we perform join() and process() using the two worklist method |
906 | // until the ranges converge. |
907 | // Ranges have converged when both worklists are empty. |
908 | SmallPtrSet<const MachineBasicBlock *, 16> Visited; |
909 | while (!Worklist.empty() || !Pending.empty()) { |
910 | // We track what is on the pending worklist to avoid inserting the same |
911 | // thing twice. We could avoid this with a custom priority queue, but this |
912 | // is probably not worth it. |
913 | SmallPtrSet<MachineBasicBlock *, 16> OnPending; |
914 | LLVM_DEBUG(dbgs() << "Processing Worklist\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { dbgs() << "Processing Worklist\n" ; } } while (false); |
915 | while (!Worklist.empty()) { |
916 | MachineBasicBlock *MBB = OrderToBB[Worklist.top()]; |
917 | Worklist.pop(); |
918 | MBBJoined = |
919 | join(*MBB, OutLocs, InLocs, VarLocIDs, Visited, ArtificialBlocks); |
920 | Visited.insert(MBB); |
921 | if (MBBJoined) { |
922 | MBBJoined = false; |
Value stored to 'MBBJoined' is never read | |
923 | Changed = true; |
924 | // Now that we have started to extend ranges across BBs we need to |
925 | // examine spill instructions to see whether they spill registers that |
926 | // correspond to user variables. |
927 | for (auto &MI : *MBB) |
928 | OLChanged |= process(MI, OpenRanges, OutLocs, VarLocIDs, Transfers, |
929 | transferChanges); |
930 | |
931 | // Add any DBG_VALUE instructions necessitated by spills. |
932 | for (auto &TR : Transfers) |
933 | MBB->insertAfter(MachineBasicBlock::iterator(*TR.TransferInst), |
934 | TR.DebugInst); |
935 | Transfers.clear(); |
936 | |
937 | LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs,do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { printVarLocInMBB(MF, OutLocs, VarLocIDs , "OutLocs after propagating", dbgs()); } } while (false) |
938 | "OutLocs after propagating", dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { printVarLocInMBB(MF, OutLocs, VarLocIDs , "OutLocs after propagating", dbgs()); } } while (false); |
939 | LLVM_DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs,do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { printVarLocInMBB(MF, InLocs, VarLocIDs , "InLocs after propagating", dbgs()); } } while (false) |
940 | "InLocs after propagating", dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("livedebugvalues")) { printVarLocInMBB(MF, InLocs, VarLocIDs , "InLocs after propagating", dbgs()); } } while (false); |
941 | |
942 | if (OLChanged) { |
943 | OLChanged = false; |
944 | for (auto s : MBB->successors()) |
945 | if (OnPending.insert(s).second) { |
946 | Pending.push(BBToOrder[s]); |
947 | } |
948 | } |
949 | } |
950 | } |
951 | Worklist.swap(Pending); |
952 | // At this point, pending must be empty, since it was just the empty |
953 | // worklist |
954 | 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\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/CodeGen/LiveDebugValues.cpp" , 954, __PRETTY_FUNCTION__)); |
955 | } |
956 | |
957 | 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); |
958 | 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); |
959 | return Changed; |
960 | } |
961 | |
962 | bool LiveDebugValues::runOnMachineFunction(MachineFunction &MF) { |
963 | if (!MF.getFunction().getSubprogram()) |
964 | // LiveDebugValues will already have removed all DBG_VALUEs. |
965 | return false; |
966 | |
967 | // Skip functions from NoDebug compilation units. |
968 | if (MF.getFunction().getSubprogram()->getUnit()->getEmissionKind() == |
969 | DICompileUnit::NoDebug) |
970 | return false; |
971 | |
972 | TRI = MF.getSubtarget().getRegisterInfo(); |
973 | TII = MF.getSubtarget().getInstrInfo(); |
974 | TFI = MF.getSubtarget().getFrameLowering(); |
975 | TFI->determineCalleeSaves(MF, CalleeSavedRegs, |
976 | make_unique<RegScavenger>().get()); |
977 | LS.initialize(MF); |
978 | |
979 | bool Changed = ExtendRanges(MF); |
980 | return Changed; |
981 | } |