blob: 22851e110cfc8b0b49c60e3574d49537fef6c9ae [file] [log] [blame]
Wolfgang Piebe018bbd2017-07-19 19:36:40 +00001//===------ LiveDebugValues.cpp - Tracking Debug Value MIs ----------------===//G
Vikram TV859ad292015-12-16 11:09:48 +00002//
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
Daniel Berlin72560592016-01-10 18:08:32 +000021#include "llvm/ADT/PostOrderIterator.h"
22#include "llvm/ADT/SmallPtrSet.h"
Adrian Prantl6ee02c72016-05-25 22:21:12 +000023#include "llvm/ADT/SparseBitVector.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000024#include "llvm/ADT/Statistic.h"
Adrian Prantl6ee02c72016-05-25 22:21:12 +000025#include "llvm/ADT/UniqueVector.h"
Adrian Prantl7f5866c2016-09-28 17:51:14 +000026#include "llvm/CodeGen/LexicalScopes.h"
Wolfgang Pieb399dcfa2017-02-14 19:08:45 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
Vikram TV859ad292015-12-16 11:09:48 +000028#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineFunctionPass.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
Wolfgang Pieb399dcfa2017-02-14 19:08:45 +000031#include "llvm/CodeGen/MachineMemOperand.h"
Vikram TV859ad292015-12-16 11:09:48 +000032#include "llvm/CodeGen/Passes.h"
Reid Kleckner28865802016-04-14 18:29:59 +000033#include "llvm/IR/DebugInfo.h"
Vikram TV859ad292015-12-16 11:09:48 +000034#include "llvm/Support/Debug.h"
35#include "llvm/Support/raw_ostream.h"
Wolfgang Pieb399dcfa2017-02-14 19:08:45 +000036#include "llvm/Target/TargetFrameLowering.h"
Vikram TV859ad292015-12-16 11:09:48 +000037#include "llvm/Target/TargetInstrInfo.h"
Reid Klecknerf6f04f82016-03-25 17:54:46 +000038#include "llvm/Target/TargetLowering.h"
Vikram TV859ad292015-12-16 11:09:48 +000039#include "llvm/Target/TargetRegisterInfo.h"
40#include "llvm/Target/TargetSubtargetInfo.h"
Vikram TV859ad292015-12-16 11:09:48 +000041#include <list>
Mehdi Aminib550cb12016-04-18 09:17:29 +000042#include <queue>
Vikram TV859ad292015-12-16 11:09:48 +000043
44using namespace llvm;
45
Matthias Braun1527baa2017-05-25 21:26:32 +000046#define DEBUG_TYPE "livedebugvalues"
Vikram TV859ad292015-12-16 11:09:48 +000047
48STATISTIC(NumInserted, "Number of DBG_VALUE instructions inserted");
49
50namespace {
51
Adrian Prantl6ee02c72016-05-25 22:21:12 +000052// \brief If @MI is a DBG_VALUE with debug value described by a defined
53// register, returns the number of this register. In the other case, returns 0.
Adrian Prantl00698732016-05-25 22:37:29 +000054static unsigned isDbgValueDescribedByReg(const MachineInstr &MI) {
Adrian Prantl6ee02c72016-05-25 22:21:12 +000055 assert(MI.isDebugValue() && "expected a DBG_VALUE");
56 assert(MI.getNumOperands() == 4 && "malformed DBG_VALUE");
57 // If location of variable is described using a register (directly
58 // or indirectly), this register is always a first operand.
59 return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0;
60}
61
Vikram TV859ad292015-12-16 11:09:48 +000062class LiveDebugValues : public MachineFunctionPass {
63
64private:
65 const TargetRegisterInfo *TRI;
66 const TargetInstrInfo *TII;
Wolfgang Pieb399dcfa2017-02-14 19:08:45 +000067 const TargetFrameLowering *TFI;
Adrian Prantl7f5866c2016-09-28 17:51:14 +000068 LexicalScopes LS;
69
70 /// Keeps track of lexical scopes associated with a user value's source
71 /// location.
72 class UserValueScopes {
73 DebugLoc DL;
74 LexicalScopes &LS;
75 SmallPtrSet<const MachineBasicBlock *, 4> LBlocks;
76
77 public:
78 UserValueScopes(DebugLoc D, LexicalScopes &L) : DL(std::move(D)), LS(L) {}
79
80 /// Return true if current scope dominates at least one machine
81 /// instruction in a given machine basic block.
82 bool dominates(MachineBasicBlock *MBB) {
83 if (LBlocks.empty())
84 LS.getMachineBasicBlocks(DL, LBlocks);
85 return LBlocks.count(MBB) != 0 || LS.dominates(DL, MBB);
86 }
87 };
Vikram TV859ad292015-12-16 11:09:48 +000088
Adrian Prantl7509d542016-05-26 21:42:47 +000089 /// Based on std::pair so it can be used as an index into a DenseMap.
Vikram TV859ad292015-12-16 11:09:48 +000090 typedef std::pair<const DILocalVariable *, const DILocation *>
Adrian Prantl7509d542016-05-26 21:42:47 +000091 DebugVariableBase;
Vikram TV859ad292015-12-16 11:09:48 +000092 /// A potentially inlined instance of a variable.
Adrian Prantl7509d542016-05-26 21:42:47 +000093 struct DebugVariable : public DebugVariableBase {
94 DebugVariable(const DILocalVariable *Var, const DILocation *InlinedAt)
95 : DebugVariableBase(Var, InlinedAt) {}
Vikram TV859ad292015-12-16 11:09:48 +000096
Adrian Prantl7509d542016-05-26 21:42:47 +000097 const DILocalVariable *getVar() const { return this->first; };
98 const DILocation *getInlinedAt() const { return this->second; };
Vikram TV859ad292015-12-16 11:09:48 +000099
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000100 bool operator<(const DebugVariable &DV) const {
Adrian Prantl7509d542016-05-26 21:42:47 +0000101 if (getVar() == DV.getVar())
102 return getInlinedAt() < DV.getInlinedAt();
103 return getVar() < DV.getVar();
Vikram TV859ad292015-12-16 11:09:48 +0000104 }
105 };
106
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000107 /// A pair of debug variable and value location.
Vikram TV859ad292015-12-16 11:09:48 +0000108 struct VarLoc {
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000109 const DebugVariable Var;
110 const MachineInstr &MI; ///< Only used for cloning a new DBG_VALUE.
Adrian Prantl7f5866c2016-09-28 17:51:14 +0000111 mutable UserValueScopes UVS;
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000112 enum { InvalidKind = 0, RegisterKind } Kind;
Vikram TV859ad292015-12-16 11:09:48 +0000113
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000114 /// The value location. Stored separately to avoid repeatedly
115 /// extracting it from MI.
116 union {
Adrian Prantl359846f2017-07-28 23:25:51 +0000117 uint64_t RegNo;
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000118 uint64_t Hash;
119 } Loc;
120
Adrian Prantl7f5866c2016-09-28 17:51:14 +0000121 VarLoc(const MachineInstr &MI, LexicalScopes &LS)
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000122 : Var(MI.getDebugVariable(), MI.getDebugLoc()->getInlinedAt()), MI(MI),
Adrian Prantl7f5866c2016-09-28 17:51:14 +0000123 UVS(MI.getDebugLoc(), LS), Kind(InvalidKind) {
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000124 static_assert((sizeof(Loc) == sizeof(uint64_t)),
125 "hash does not cover all members of Loc");
126 assert(MI.isDebugValue() && "not a DBG_VALUE");
127 assert(MI.getNumOperands() == 4 && "malformed DBG_VALUE");
Adrian Prantl00698732016-05-25 22:37:29 +0000128 if (int RegNo = isDbgValueDescribedByReg(MI)) {
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000129 Kind = RegisterKind;
Adrian Prantl359846f2017-07-28 23:25:51 +0000130 Loc.RegNo = RegNo;
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000131 }
132 }
133
134 /// If this variable is described by a register, return it,
135 /// otherwise return 0.
136 unsigned isDescribedByReg() const {
137 if (Kind == RegisterKind)
Adrian Prantl359846f2017-07-28 23:25:51 +0000138 return Loc.RegNo;
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000139 return 0;
140 }
141
Adrian Prantl7f5866c2016-09-28 17:51:14 +0000142 /// Determine whether the lexical scope of this value's debug location
143 /// dominates MBB.
144 bool dominates(MachineBasicBlock &MBB) const { return UVS.dominates(&MBB); }
145
Matthias Braun194ded52017-01-28 06:53:55 +0000146#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
147 LLVM_DUMP_METHOD void dump() const { MI.dump(); }
148#endif
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000149
150 bool operator==(const VarLoc &Other) const {
151 return Var == Other.Var && Loc.Hash == Other.Loc.Hash;
152 }
153
Adrian Prantl7509d542016-05-26 21:42:47 +0000154 /// This operator guarantees that VarLocs are sorted by Variable first.
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000155 bool operator<(const VarLoc &Other) const {
156 if (Var == Other.Var)
157 return Loc.Hash < Other.Loc.Hash;
158 return Var < Other.Var;
159 }
Vikram TV859ad292015-12-16 11:09:48 +0000160 };
161
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000162 typedef UniqueVector<VarLoc> VarLocMap;
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000163 typedef SparseBitVector<> VarLocSet;
164 typedef SmallDenseMap<const MachineBasicBlock *, VarLocSet> VarLocInMBB;
Wolfgang Pieb399dcfa2017-02-14 19:08:45 +0000165 struct SpillDebugPair {
166 MachineInstr *SpillInst;
167 MachineInstr *DebugInst;
168 };
169 typedef SmallVector<SpillDebugPair, 4> SpillMap;
Vikram TV859ad292015-12-16 11:09:48 +0000170
Adrian Prantl7509d542016-05-26 21:42:47 +0000171 /// This holds the working set of currently open ranges. For fast
172 /// access, this is done both as a set of VarLocIDs, and a map of
173 /// DebugVariable to recent VarLocID. Note that a DBG_VALUE ends all
174 /// previous open ranges for the same variable.
175 class OpenRangesSet {
176 VarLocSet VarLocs;
177 SmallDenseMap<DebugVariableBase, unsigned, 8> Vars;
178
179 public:
180 const VarLocSet &getVarLocs() const { return VarLocs; }
181
182 /// Terminate all open ranges for Var by removing it from the set.
183 void erase(DebugVariable Var) {
184 auto It = Vars.find(Var);
185 if (It != Vars.end()) {
186 unsigned ID = It->second;
187 VarLocs.reset(ID);
188 Vars.erase(It);
189 }
190 }
191
192 /// Terminate all open ranges listed in \c KillSet by removing
193 /// them from the set.
194 void erase(const VarLocSet &KillSet, const VarLocMap &VarLocIDs) {
195 VarLocs.intersectWithComplement(KillSet);
196 for (unsigned ID : KillSet)
197 Vars.erase(VarLocIDs[ID].Var);
198 }
199
200 /// Insert a new range into the set.
201 void insert(unsigned VarLocID, DebugVariableBase Var) {
202 VarLocs.set(VarLocID);
203 Vars.insert({Var, VarLocID});
204 }
205
206 /// Empty the set.
207 void clear() {
208 VarLocs.clear();
209 Vars.clear();
210 }
211
212 /// Return whether the set is empty or not.
213 bool empty() const {
214 assert(Vars.empty() == VarLocs.empty() && "open ranges are inconsistent");
215 return VarLocs.empty();
216 }
217 };
218
Wolfgang Pieb399dcfa2017-02-14 19:08:45 +0000219 bool isSpillInstruction(const MachineInstr &MI, MachineFunction *MF,
220 unsigned &Reg);
221 int extractSpillBaseRegAndOffset(const MachineInstr &MI, unsigned &Reg);
222
Adrian Prantl7509d542016-05-26 21:42:47 +0000223 void transferDebugValue(const MachineInstr &MI, OpenRangesSet &OpenRanges,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000224 VarLocMap &VarLocIDs);
Wolfgang Pieb399dcfa2017-02-14 19:08:45 +0000225 void transferSpillInst(MachineInstr &MI, OpenRangesSet &OpenRanges,
226 VarLocMap &VarLocIDs, SpillMap &Spills);
Adrian Prantl7509d542016-05-26 21:42:47 +0000227 void transferRegisterDef(MachineInstr &MI, OpenRangesSet &OpenRanges,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000228 const VarLocMap &VarLocIDs);
Adrian Prantl7509d542016-05-26 21:42:47 +0000229 bool transferTerminatorInst(MachineInstr &MI, OpenRangesSet &OpenRanges,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000230 VarLocInMBB &OutLocs, const VarLocMap &VarLocIDs);
Adrian Prantl7509d542016-05-26 21:42:47 +0000231 bool transfer(MachineInstr &MI, OpenRangesSet &OpenRanges,
Wolfgang Pieb399dcfa2017-02-14 19:08:45 +0000232 VarLocInMBB &OutLocs, VarLocMap &VarLocIDs, SpillMap &Spills,
233 bool transferSpills);
Vikram TV859ad292015-12-16 11:09:48 +0000234
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000235 bool join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs, VarLocInMBB &InLocs,
Keith Walker83ebef52016-09-27 16:46:07 +0000236 const VarLocMap &VarLocIDs,
237 SmallPtrSet<const MachineBasicBlock *, 16> &Visited);
Vikram TV859ad292015-12-16 11:09:48 +0000238
239 bool ExtendRanges(MachineFunction &MF);
240
241public:
242 static char ID;
243
244 /// Default construct and initialize the pass.
245 LiveDebugValues();
246
247 /// Tell the pass manager which passes we depend on and what
248 /// information we preserve.
249 void getAnalysisUsage(AnalysisUsage &AU) const override;
250
Derek Schuffad154c82016-03-28 17:05:30 +0000251 MachineFunctionProperties getRequiredProperties() const override {
252 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000253 MachineFunctionProperties::Property::NoVRegs);
Derek Schuffad154c82016-03-28 17:05:30 +0000254 }
255
Vikram TV859ad292015-12-16 11:09:48 +0000256 /// Print to ostream with a message.
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000257 void printVarLocInMBB(const MachineFunction &MF, const VarLocInMBB &V,
258 const VarLocMap &VarLocIDs, const char *msg,
Vikram TV859ad292015-12-16 11:09:48 +0000259 raw_ostream &Out) const;
260
261 /// Calculate the liveness information for the given machine function.
262 bool runOnMachineFunction(MachineFunction &MF) override;
263};
Adrian Prantl7f5866c2016-09-28 17:51:14 +0000264
Vikram TV859ad292015-12-16 11:09:48 +0000265} // namespace
266
267//===----------------------------------------------------------------------===//
268// Implementation
269//===----------------------------------------------------------------------===//
270
271char LiveDebugValues::ID = 0;
272char &llvm::LiveDebugValuesID = LiveDebugValues::ID;
Matthias Braun1527baa2017-05-25 21:26:32 +0000273INITIALIZE_PASS(LiveDebugValues, DEBUG_TYPE, "Live DEBUG_VALUE analysis",
Vikram TV859ad292015-12-16 11:09:48 +0000274 false, false)
275
276/// Default construct and initialize the pass.
277LiveDebugValues::LiveDebugValues() : MachineFunctionPass(ID) {
278 initializeLiveDebugValuesPass(*PassRegistry::getPassRegistry());
279}
280
281/// Tell the pass manager which passes we depend on and what information we
282/// preserve.
283void LiveDebugValues::getAnalysisUsage(AnalysisUsage &AU) const {
Matt Arsenaultb1630a12016-06-08 05:18:01 +0000284 AU.setPreservesCFG();
Vikram TV859ad292015-12-16 11:09:48 +0000285 MachineFunctionPass::getAnalysisUsage(AU);
286}
287
Vikram TV859ad292015-12-16 11:09:48 +0000288//===----------------------------------------------------------------------===//
289// Debug Range Extension Implementation
290//===----------------------------------------------------------------------===//
291
Matthias Braun194ded52017-01-28 06:53:55 +0000292#ifndef NDEBUG
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000293void LiveDebugValues::printVarLocInMBB(const MachineFunction &MF,
294 const VarLocInMBB &V,
295 const VarLocMap &VarLocIDs,
296 const char *msg,
Vikram TV859ad292015-12-16 11:09:48 +0000297 raw_ostream &Out) const {
Keith Walkerf83a19f2016-09-20 16:04:31 +0000298 Out << '\n' << msg << '\n';
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000299 for (const MachineBasicBlock &BB : MF) {
300 const auto &L = V.lookup(&BB);
301 Out << "MBB: " << BB.getName() << ":\n";
302 for (unsigned VLL : L) {
303 const VarLoc &VL = VarLocIDs[VLL];
Adrian Prantl7509d542016-05-26 21:42:47 +0000304 Out << " Var: " << VL.Var.getVar()->getName();
Vikram TV859ad292015-12-16 11:09:48 +0000305 Out << " MI: ";
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000306 VL.dump();
Vikram TV859ad292015-12-16 11:09:48 +0000307 }
308 }
309 Out << "\n";
310}
Matthias Braun194ded52017-01-28 06:53:55 +0000311#endif
Vikram TV859ad292015-12-16 11:09:48 +0000312
Wolfgang Pieb399dcfa2017-02-14 19:08:45 +0000313/// Given a spill instruction, extract the register and offset used to
314/// address the spill location in a target independent way.
315int LiveDebugValues::extractSpillBaseRegAndOffset(const MachineInstr &MI,
316 unsigned &Reg) {
317 assert(MI.hasOneMemOperand() &&
318 "Spill instruction does not have exactly one memory operand?");
319 auto MMOI = MI.memoperands_begin();
320 const PseudoSourceValue *PVal = (*MMOI)->getPseudoValue();
321 assert(PVal->kind() == PseudoSourceValue::FixedStack &&
322 "Inconsistent memory operand in spill instruction");
323 int FI = cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex();
324 const MachineBasicBlock *MBB = MI.getParent();
325 return TFI->getFrameIndexReference(*MBB->getParent(), FI, Reg);
326}
327
Vikram TV859ad292015-12-16 11:09:48 +0000328/// End all previous ranges related to @MI and start a new range from @MI
329/// if it is a DBG_VALUE instr.
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000330void LiveDebugValues::transferDebugValue(const MachineInstr &MI,
Adrian Prantl7509d542016-05-26 21:42:47 +0000331 OpenRangesSet &OpenRanges,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000332 VarLocMap &VarLocIDs) {
Vikram TV859ad292015-12-16 11:09:48 +0000333 if (!MI.isDebugValue())
334 return;
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000335 const DILocalVariable *Var = MI.getDebugVariable();
336 const DILocation *DebugLoc = MI.getDebugLoc();
337 const DILocation *InlinedAt = DebugLoc->getInlinedAt();
338 assert(Var->isValidLocationForIntrinsic(DebugLoc) &&
Vikram TV859ad292015-12-16 11:09:48 +0000339 "Expected inlined-at fields to agree");
Vikram TV859ad292015-12-16 11:09:48 +0000340
341 // End all previous ranges of Var.
Adrian Prantl7509d542016-05-26 21:42:47 +0000342 DebugVariable V(Var, InlinedAt);
343 OpenRanges.erase(V);
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000344
345 // Add the VarLoc to OpenRanges from this DBG_VALUE.
346 // TODO: Currently handles DBG_VALUE which has only reg as location.
Adrian Prantl7509d542016-05-26 21:42:47 +0000347 if (isDbgValueDescribedByReg(MI)) {
Adrian Prantl7f5866c2016-09-28 17:51:14 +0000348 VarLoc VL(MI, LS);
Adrian Prantl7509d542016-05-26 21:42:47 +0000349 unsigned ID = VarLocIDs.insert(VL);
350 OpenRanges.insert(ID, VL.Var);
351 }
Vikram TV859ad292015-12-16 11:09:48 +0000352}
353
354/// A definition of a register may mark the end of a range.
355void LiveDebugValues::transferRegisterDef(MachineInstr &MI,
Adrian Prantl7509d542016-05-26 21:42:47 +0000356 OpenRangesSet &OpenRanges,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000357 const VarLocMap &VarLocIDs) {
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000358 MachineFunction *MF = MI.getParent()->getParent();
359 const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
360 unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000361 SparseBitVector<> KillSet;
Vikram TV859ad292015-12-16 11:09:48 +0000362 for (const MachineOperand &MO : MI.operands()) {
Adrian Prantlea8880b2017-03-03 01:08:25 +0000363 // Determine whether the operand is a register def. Assume that call
364 // instructions never clobber SP, because some backends (e.g., AArch64)
365 // never list SP in the regmask.
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000366 if (MO.isReg() && MO.isDef() && MO.getReg() &&
Adrian Prantlea8880b2017-03-03 01:08:25 +0000367 TRI->isPhysicalRegister(MO.getReg()) &&
368 !(MI.isCall() && MO.getReg() == SP)) {
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000369 // Remove ranges of all aliased registers.
370 for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI)
Adrian Prantl7509d542016-05-26 21:42:47 +0000371 for (unsigned ID : OpenRanges.getVarLocs())
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000372 if (VarLocIDs[ID].isDescribedByReg() == *RAI)
373 KillSet.set(ID);
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000374 } else if (MO.isRegMask()) {
375 // Remove ranges of all clobbered registers. Register masks don't usually
376 // list SP as preserved. While the debug info may be off for an
377 // instruction or two around callee-cleanup calls, transferring the
378 // DEBUG_VALUE across the call is still a better user experience.
Adrian Prantl7509d542016-05-26 21:42:47 +0000379 for (unsigned ID : OpenRanges.getVarLocs()) {
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000380 unsigned Reg = VarLocIDs[ID].isDescribedByReg();
381 if (Reg && Reg != SP && MO.clobbersPhysReg(Reg))
382 KillSet.set(ID);
383 }
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000384 }
Vikram TV859ad292015-12-16 11:09:48 +0000385 }
Adrian Prantl7509d542016-05-26 21:42:47 +0000386 OpenRanges.erase(KillSet, VarLocIDs);
Vikram TV859ad292015-12-16 11:09:48 +0000387}
388
Wolfgang Pieb399dcfa2017-02-14 19:08:45 +0000389/// Decide if @MI is a spill instruction and return true if it is. We use 2
390/// criteria to make this decision:
391/// - Is this instruction a store to a spill slot?
392/// - Is there a register operand that is both used and killed?
393/// TODO: Store optimization can fold spills into other stores (including
394/// other spills). We do not handle this yet (more than one memory operand).
395bool LiveDebugValues::isSpillInstruction(const MachineInstr &MI,
396 MachineFunction *MF, unsigned &Reg) {
397 const MachineFrameInfo &FrameInfo = MF->getFrameInfo();
398 int FI;
399 const MachineMemOperand *MMO;
400
401 // TODO: Handle multiple stores folded into one.
402 if (!MI.hasOneMemOperand())
403 return false;
404
405 // To identify a spill instruction, use the same criteria as in AsmPrinter.
406 if (!((TII->isStoreToStackSlotPostFE(MI, FI) ||
407 TII->hasStoreToStackSlot(MI, MMO, FI)) &&
408 FrameInfo.isSpillSlotObjectIndex(FI)))
409 return false;
410
411 // In a spill instruction generated by the InlineSpiller the spilled register
412 // has its kill flag set. Return false if we don't find such a register.
413 Reg = 0;
414 for (const MachineOperand &MO : MI.operands()) {
415 if (MO.isReg() && MO.isUse() && MO.isKill()) {
416 Reg = MO.getReg();
417 break;
418 }
419 }
420 return Reg != 0;
421}
422
423/// A spilled register may indicate that we have to end the current range of
424/// a variable and create a new one for the spill location.
425/// We don't want to insert any instructions in transfer(), so we just create
426/// the DBG_VALUE witout inserting it and keep track of it in @Spills.
427/// It will be inserted into the BB when we're done iterating over the
428/// instructions.
429void LiveDebugValues::transferSpillInst(MachineInstr &MI,
430 OpenRangesSet &OpenRanges,
431 VarLocMap &VarLocIDs,
432 SpillMap &Spills) {
433 unsigned Reg;
434 MachineFunction *MF = MI.getParent()->getParent();
435 if (!isSpillInstruction(MI, MF, Reg))
436 return;
437
438 // Check if the register is the location of a debug value.
439 for (unsigned ID : OpenRanges.getVarLocs()) {
440 if (VarLocIDs[ID].isDescribedByReg() == Reg) {
441 DEBUG(dbgs() << "Spilling Register " << PrintReg(Reg, TRI) << '('
442 << VarLocIDs[ID].Var.getVar()->getName() << ")\n");
443
444 // Create a DBG_VALUE instruction to describe the Var in its spilled
445 // location, but don't insert it yet to avoid invalidating the
446 // iterator in our caller.
447 unsigned SpillBase;
448 int SpillOffset = extractSpillBaseRegAndOffset(MI, SpillBase);
449 const MachineInstr *DMI = &VarLocIDs[ID].MI;
450 MachineInstr *SpDMI =
Adrian Prantl8b9bb532017-07-28 23:00:45 +0000451 BuildMI(*MF, DMI->getDebugLoc(), DMI->getDesc(), true, SpillBase,
Wolfgang Pieb399dcfa2017-02-14 19:08:45 +0000452 DMI->getDebugVariable(), DMI->getDebugExpression());
453 SpDMI->getOperand(1).setImm(SpillOffset);
454 DEBUG(dbgs() << "Creating DBG_VALUE inst for spill: ";
455 SpDMI->print(dbgs(), false, TII));
456
457 // The newly created DBG_VALUE instruction SpDMI must be inserted after
458 // MI. Keep track of the pairing.
459 SpillDebugPair MIP = {&MI, SpDMI};
460 Spills.push_back(MIP);
461
462 // End all previous ranges of Var.
463 OpenRanges.erase(VarLocIDs[ID].Var);
464
465 // Add the VarLoc to OpenRanges.
466 VarLoc VL(*SpDMI, LS);
467 unsigned SpillLocID = VarLocIDs.insert(VL);
468 OpenRanges.insert(SpillLocID, VL.Var);
469 return;
470 }
471 }
472}
473
Vikram TV859ad292015-12-16 11:09:48 +0000474/// Terminate all open ranges at the end of the current basic block.
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000475bool LiveDebugValues::transferTerminatorInst(MachineInstr &MI,
Adrian Prantl7509d542016-05-26 21:42:47 +0000476 OpenRangesSet &OpenRanges,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000477 VarLocInMBB &OutLocs,
478 const VarLocMap &VarLocIDs) {
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000479 bool Changed = false;
Vikram TV859ad292015-12-16 11:09:48 +0000480 const MachineBasicBlock *CurMBB = MI.getParent();
481 if (!(MI.isTerminator() || (&MI == &CurMBB->instr_back())))
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000482 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000483
484 if (OpenRanges.empty())
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000485 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000486
Adrian Prantl7509d542016-05-26 21:42:47 +0000487 DEBUG(for (unsigned ID : OpenRanges.getVarLocs()) {
488 // Copy OpenRanges to OutLocs, if not already present.
489 dbgs() << "Add to OutLocs: "; VarLocIDs[ID].dump();
490 });
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000491 VarLocSet &VLS = OutLocs[CurMBB];
Adrian Prantl7509d542016-05-26 21:42:47 +0000492 Changed = VLS |= OpenRanges.getVarLocs();
Vikram TV859ad292015-12-16 11:09:48 +0000493 OpenRanges.clear();
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000494 return Changed;
Vikram TV859ad292015-12-16 11:09:48 +0000495}
496
497/// This routine creates OpenRanges and OutLocs.
Adrian Prantl7509d542016-05-26 21:42:47 +0000498bool LiveDebugValues::transfer(MachineInstr &MI, OpenRangesSet &OpenRanges,
Wolfgang Pieb399dcfa2017-02-14 19:08:45 +0000499 VarLocInMBB &OutLocs, VarLocMap &VarLocIDs,
500 SpillMap &Spills, bool transferSpills) {
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000501 bool Changed = false;
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000502 transferDebugValue(MI, OpenRanges, VarLocIDs);
503 transferRegisterDef(MI, OpenRanges, VarLocIDs);
Wolfgang Pieb399dcfa2017-02-14 19:08:45 +0000504 if (transferSpills)
505 transferSpillInst(MI, OpenRanges, VarLocIDs, Spills);
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000506 Changed = transferTerminatorInst(MI, OpenRanges, OutLocs, VarLocIDs);
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000507 return Changed;
Vikram TV859ad292015-12-16 11:09:48 +0000508}
509
510/// This routine joins the analysis results of all incoming edges in @MBB by
511/// inserting a new DBG_VALUE instruction at the start of the @MBB - if the same
512/// source variable in all the predecessors of @MBB reside in the same location.
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000513bool LiveDebugValues::join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs,
Keith Walker83ebef52016-09-27 16:46:07 +0000514 VarLocInMBB &InLocs, const VarLocMap &VarLocIDs,
515 SmallPtrSet<const MachineBasicBlock *, 16> &Visited) {
Vikram TV859ad292015-12-16 11:09:48 +0000516 DEBUG(dbgs() << "join MBB: " << MBB.getName() << "\n");
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000517 bool Changed = false;
Vikram TV859ad292015-12-16 11:09:48 +0000518
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000519 VarLocSet InLocsT; // Temporary incoming locations.
Vikram TV859ad292015-12-16 11:09:48 +0000520
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000521 // For all predecessors of this MBB, find the set of VarLocs that
522 // can be joined.
Keith Walker83ebef52016-09-27 16:46:07 +0000523 int NumVisited = 0;
Vikram TV859ad292015-12-16 11:09:48 +0000524 for (auto p : MBB.predecessors()) {
Keith Walker83ebef52016-09-27 16:46:07 +0000525 // Ignore unvisited predecessor blocks. As we are processing
526 // the blocks in reverse post-order any unvisited block can
527 // be considered to not remove any incoming values.
528 if (!Visited.count(p))
529 continue;
Vikram TV859ad292015-12-16 11:09:48 +0000530 auto OL = OutLocs.find(p);
531 // Join is null in case of empty OutLocs from any of the pred.
532 if (OL == OutLocs.end())
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000533 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000534
Keith Walker83ebef52016-09-27 16:46:07 +0000535 // Just copy over the Out locs to incoming locs for the first visited
536 // predecessor, and for all other predecessors join the Out locs.
537 if (!NumVisited)
Vikram TV859ad292015-12-16 11:09:48 +0000538 InLocsT = OL->second;
Keith Walker83ebef52016-09-27 16:46:07 +0000539 else
540 InLocsT &= OL->second;
541 NumVisited++;
Vikram TV859ad292015-12-16 11:09:48 +0000542 }
543
Adrian Prantl7f5866c2016-09-28 17:51:14 +0000544 // Filter out DBG_VALUES that are out of scope.
545 VarLocSet KillSet;
546 for (auto ID : InLocsT)
547 if (!VarLocIDs[ID].dominates(MBB))
548 KillSet.set(ID);
549 InLocsT.intersectWithComplement(KillSet);
550
Keith Walker83ebef52016-09-27 16:46:07 +0000551 // As we are processing blocks in reverse post-order we
552 // should have processed at least one predecessor, unless it
553 // is the entry block which has no predecessor.
554 assert((NumVisited || MBB.pred_empty()) &&
555 "Should have processed at least one predecessor");
Vikram TV859ad292015-12-16 11:09:48 +0000556 if (InLocsT.empty())
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000557 return false;
Vikram TV859ad292015-12-16 11:09:48 +0000558
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000559 VarLocSet &ILS = InLocs[&MBB];
Vikram TV859ad292015-12-16 11:09:48 +0000560
561 // Insert DBG_VALUE instructions, if not already inserted.
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000562 VarLocSet Diff = InLocsT;
563 Diff.intersectWithComplement(ILS);
564 for (auto ID : Diff) {
565 // This VarLoc is not found in InLocs i.e. it is not yet inserted. So, a
566 // new range is started for the var from the mbb's beginning by inserting
567 // a new DBG_VALUE. transfer() will end this range however appropriate.
568 const VarLoc &DiffIt = VarLocIDs[ID];
569 const MachineInstr *DMI = &DiffIt.MI;
570 MachineInstr *MI =
571 BuildMI(MBB, MBB.instr_begin(), DMI->getDebugLoc(), DMI->getDesc(),
Adrian Prantl8b9bb532017-07-28 23:00:45 +0000572 DMI->isIndirectDebugValue(), DMI->getOperand(0).getReg(),
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000573 DMI->getDebugVariable(), DMI->getDebugExpression());
574 if (DMI->isIndirectDebugValue())
575 MI->getOperand(1).setImm(DMI->getOperand(1).getImm());
576 DEBUG(dbgs() << "Inserted: "; MI->dump(););
577 ILS.set(ID);
578 ++NumInserted;
579 Changed = true;
Vikram TV859ad292015-12-16 11:09:48 +0000580 }
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000581 return Changed;
Vikram TV859ad292015-12-16 11:09:48 +0000582}
583
584/// Calculate the liveness information for the given machine function and
585/// extend ranges across basic blocks.
586bool LiveDebugValues::ExtendRanges(MachineFunction &MF) {
587
588 DEBUG(dbgs() << "\nDebug Range Extension\n");
589
590 bool Changed = false;
Daniel Berlinca4d93a2016-01-10 03:25:42 +0000591 bool OLChanged = false;
592 bool MBBJoined = false;
Vikram TV859ad292015-12-16 11:09:48 +0000593
Wolfgang Pieb399dcfa2017-02-14 19:08:45 +0000594 VarLocMap VarLocIDs; // Map VarLoc<>unique ID for use in bitvectors.
Adrian Prantl7509d542016-05-26 21:42:47 +0000595 OpenRangesSet OpenRanges; // Ranges that are open until end of bb.
Wolfgang Pieb399dcfa2017-02-14 19:08:45 +0000596 VarLocInMBB OutLocs; // Ranges that exist beyond bb.
597 VarLocInMBB InLocs; // Ranges that are incoming after joining.
598 SpillMap Spills; // DBG_VALUEs associated with spills.
Vikram TV859ad292015-12-16 11:09:48 +0000599
Daniel Berlin72560592016-01-10 18:08:32 +0000600 DenseMap<unsigned int, MachineBasicBlock *> OrderToBB;
601 DenseMap<MachineBasicBlock *, unsigned int> BBToOrder;
602 std::priority_queue<unsigned int, std::vector<unsigned int>,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000603 std::greater<unsigned int>>
604 Worklist;
Daniel Berlin72560592016-01-10 18:08:32 +0000605 std::priority_queue<unsigned int, std::vector<unsigned int>,
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000606 std::greater<unsigned int>>
607 Pending;
608
Vikram TV859ad292015-12-16 11:09:48 +0000609 // Initialize every mbb with OutLocs.
Wolfgang Pieb399dcfa2017-02-14 19:08:45 +0000610 // We are not looking at any spill instructions during the initial pass
611 // over the BBs. The LiveDebugVariables pass has already created DBG_VALUE
612 // instructions for spills of registers that are known to be user variables
613 // within the BB in which the spill occurs.
Vikram TV859ad292015-12-16 11:09:48 +0000614 for (auto &MBB : MF)
615 for (auto &MI : MBB)
Wolfgang Pieb399dcfa2017-02-14 19:08:45 +0000616 transfer(MI, OpenRanges, OutLocs, VarLocIDs, Spills,
617 /*transferSpills=*/false);
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000618
619 DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, "OutLocs after initialization",
620 dbgs()));
Vikram TV859ad292015-12-16 11:09:48 +0000621
Daniel Berlin72560592016-01-10 18:08:32 +0000622 ReversePostOrderTraversal<MachineFunction *> RPOT(&MF);
623 unsigned int RPONumber = 0;
624 for (auto RI = RPOT.begin(), RE = RPOT.end(); RI != RE; ++RI) {
625 OrderToBB[RPONumber] = *RI;
626 BBToOrder[*RI] = RPONumber;
627 Worklist.push(RPONumber);
628 ++RPONumber;
629 }
Daniel Berlin72560592016-01-10 18:08:32 +0000630 // This is a standard "union of predecessor outs" dataflow problem.
631 // To solve it, we perform join() and transfer() using the two worklist method
632 // until the ranges converge.
633 // Ranges have converged when both worklists are empty.
Keith Walker83ebef52016-09-27 16:46:07 +0000634 SmallPtrSet<const MachineBasicBlock *, 16> Visited;
Daniel Berlin72560592016-01-10 18:08:32 +0000635 while (!Worklist.empty() || !Pending.empty()) {
636 // We track what is on the pending worklist to avoid inserting the same
637 // thing twice. We could avoid this with a custom priority queue, but this
638 // is probably not worth it.
639 SmallPtrSet<MachineBasicBlock *, 16> OnPending;
Keith Walkerf83a19f2016-09-20 16:04:31 +0000640 DEBUG(dbgs() << "Processing Worklist\n");
Daniel Berlin72560592016-01-10 18:08:32 +0000641 while (!Worklist.empty()) {
642 MachineBasicBlock *MBB = OrderToBB[Worklist.top()];
643 Worklist.pop();
Keith Walker83ebef52016-09-27 16:46:07 +0000644 MBBJoined = join(*MBB, OutLocs, InLocs, VarLocIDs, Visited);
645 Visited.insert(MBB);
Daniel Berlin72560592016-01-10 18:08:32 +0000646 if (MBBJoined) {
647 MBBJoined = false;
648 Changed = true;
Wolfgang Pieb399dcfa2017-02-14 19:08:45 +0000649 // Now that we have started to extend ranges across BBs we need to
650 // examine spill instructions to see whether they spill registers that
651 // correspond to user variables.
Daniel Berlin72560592016-01-10 18:08:32 +0000652 for (auto &MI : *MBB)
Wolfgang Pieb399dcfa2017-02-14 19:08:45 +0000653 OLChanged |= transfer(MI, OpenRanges, OutLocs, VarLocIDs, Spills,
654 /*transferSpills=*/true);
655
656 // Add any DBG_VALUE instructions necessitated by spills.
657 for (auto &SP : Spills)
658 MBB->insertAfter(MachineBasicBlock::iterator(*SP.SpillInst),
659 SP.DebugInst);
660 Spills.clear();
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000661
662 DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs,
663 "OutLocs after propagating", dbgs()));
664 DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs,
665 "InLocs after propagating", dbgs()));
Vikram TV859ad292015-12-16 11:09:48 +0000666
Daniel Berlin72560592016-01-10 18:08:32 +0000667 if (OLChanged) {
668 OLChanged = false;
669 for (auto s : MBB->successors())
Benjamin Kramer4dea8f52016-06-17 18:59:41 +0000670 if (OnPending.insert(s).second) {
Daniel Berlin72560592016-01-10 18:08:32 +0000671 Pending.push(BBToOrder[s]);
672 }
673 }
Vikram TV859ad292015-12-16 11:09:48 +0000674 }
675 }
Daniel Berlin72560592016-01-10 18:08:32 +0000676 Worklist.swap(Pending);
677 // At this point, pending must be empty, since it was just the empty
678 // worklist
679 assert(Pending.empty() && "Pending should be empty");
Vikram TV859ad292015-12-16 11:09:48 +0000680 }
Daniel Berlin72560592016-01-10 18:08:32 +0000681
Adrian Prantl6ee02c72016-05-25 22:21:12 +0000682 DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, "Final OutLocs", dbgs()));
683 DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs, "Final InLocs", dbgs()));
Vikram TV859ad292015-12-16 11:09:48 +0000684 return Changed;
685}
686
687bool LiveDebugValues::runOnMachineFunction(MachineFunction &MF) {
Adrian Prantl7f5866c2016-09-28 17:51:14 +0000688 if (!MF.getFunction()->getSubprogram())
689 // LiveDebugValues will already have removed all DBG_VALUEs.
690 return false;
691
Wolfgang Piebe018bbd2017-07-19 19:36:40 +0000692 // Skip functions from NoDebug compilation units.
693 if (MF.getFunction()->getSubprogram()->getUnit()->getEmissionKind() ==
694 DICompileUnit::NoDebug)
695 return false;
696
Vikram TV859ad292015-12-16 11:09:48 +0000697 TRI = MF.getSubtarget().getRegisterInfo();
698 TII = MF.getSubtarget().getInstrInfo();
Wolfgang Pieb399dcfa2017-02-14 19:08:45 +0000699 TFI = MF.getSubtarget().getFrameLowering();
Adrian Prantl7f5866c2016-09-28 17:51:14 +0000700 LS.initialize(MF);
Vikram TV859ad292015-12-16 11:09:48 +0000701
Adrian Prantl7f5866c2016-09-28 17:51:14 +0000702 bool Changed = ExtendRanges(MF);
Vikram TV859ad292015-12-16 11:09:48 +0000703 return Changed;
704}