1 //===- llvm/CodeGen/AsmPrinter/DbgEntityHistoryCalculator.cpp -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "DbgEntityHistoryCalculator.h"
11 #include "llvm/ADT/BitVector.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/CodeGen/MachineBasicBlock.h"
15 #include "llvm/CodeGen/MachineFunction.h"
16 #include "llvm/CodeGen/MachineInstr.h"
17 #include "llvm/CodeGen/MachineOperand.h"
18 #include "llvm/CodeGen/TargetLowering.h"
19 #include "llvm/CodeGen/TargetRegisterInfo.h"
20 #include "llvm/CodeGen/TargetSubtargetInfo.h"
21 #include "llvm/IR/DebugInfoMetadata.h"
22 #include "llvm/IR/DebugLoc.h"
23 #include "llvm/MC/MCRegisterInfo.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/raw_ostream.h"
32 #define DEBUG_TYPE "dwarfdebug"
34 // If @MI is a DBG_VALUE with debug value described by a
35 // defined register, returns the number of this register.
36 // In the other case, returns 0.
37 static unsigned isDescribedByReg(const MachineInstr &MI) {
38 assert(MI.isDebugValue());
39 assert(MI.getNumOperands() == 4);
40 // If location of variable is described using a register (directly or
41 // indirectly), this register is always a first operand.
42 return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0;
45 void DbgValueHistoryMap::startInstrRange(InlinedVariable Var,
46 const MachineInstr &MI) {
47 // Instruction range should start with a DBG_VALUE instruction for the
49 assert(MI.isDebugValue() && "not a DBG_VALUE");
50 auto &Ranges = VarInstrRanges[Var];
51 if (!Ranges.empty() && Ranges.back().second == nullptr &&
52 Ranges.back().first->isIdenticalTo(MI)) {
53 LLVM_DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
54 << "\t" << Ranges.back().first << "\t" << MI << "\n");
57 Ranges.push_back(std::make_pair(&MI, nullptr));
60 void DbgValueHistoryMap::endInstrRange(InlinedVariable Var,
61 const MachineInstr &MI) {
62 auto &Ranges = VarInstrRanges[Var];
63 // Verify that the current instruction range is not yet closed.
64 assert(!Ranges.empty() && Ranges.back().second == nullptr);
65 // For now, instruction ranges are not allowed to cross basic block
67 assert(Ranges.back().first->getParent() == MI.getParent());
68 Ranges.back().second = &MI;
71 unsigned DbgValueHistoryMap::getRegisterForVar(InlinedVariable Var) const {
72 const auto &I = VarInstrRanges.find(Var);
73 if (I == VarInstrRanges.end())
75 const auto &Ranges = I->second;
76 if (Ranges.empty() || Ranges.back().second != nullptr)
78 return isDescribedByReg(*Ranges.back().first);
81 void DbgLabelInstrMap::addInstr(InlinedLabel Label, const MachineInstr &MI) {
82 assert(MI.isDebugLabel() && "not a DBG_LABEL");
83 LabelInstr[Label] = &MI;
88 // Maps physreg numbers to the variables they describe.
89 using InlinedVariable = DbgValueHistoryMap::InlinedVariable;
90 using RegDescribedVarsMap = std::map<unsigned, SmallVector<InlinedVariable, 1>>;
91 using InlinedLabel = DbgLabelInstrMap::InlinedLabel;
93 } // end anonymous namespace
95 // Claim that @Var is not described by @RegNo anymore.
96 static void dropRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo,
97 InlinedVariable Var) {
98 const auto &I = RegVars.find(RegNo);
99 assert(RegNo != 0U && I != RegVars.end());
100 auto &VarSet = I->second;
101 const auto &VarPos = llvm::find(VarSet, Var);
102 assert(VarPos != VarSet.end());
103 VarSet.erase(VarPos);
104 // Don't keep empty sets in a map to keep it as small as possible.
109 // Claim that @Var is now described by @RegNo.
110 static void addRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo,
111 InlinedVariable Var) {
113 auto &VarSet = RegVars[RegNo];
114 assert(!is_contained(VarSet, Var));
115 VarSet.push_back(Var);
118 // Terminate the location range for variables described by register at
119 // @I by inserting @ClobberingInstr to their history.
120 static void clobberRegisterUses(RegDescribedVarsMap &RegVars,
121 RegDescribedVarsMap::iterator I,
122 DbgValueHistoryMap &HistMap,
123 const MachineInstr &ClobberingInstr) {
124 // Iterate over all variables described by this register and add this
125 // instruction to their history, clobbering it.
126 for (const auto &Var : I->second)
127 HistMap.endInstrRange(Var, ClobberingInstr);
131 // Terminate the location range for variables described by register
132 // @RegNo by inserting @ClobberingInstr to their history.
133 static void clobberRegisterUses(RegDescribedVarsMap &RegVars, unsigned RegNo,
134 DbgValueHistoryMap &HistMap,
135 const MachineInstr &ClobberingInstr) {
136 const auto &I = RegVars.find(RegNo);
137 if (I == RegVars.end())
139 clobberRegisterUses(RegVars, I, HistMap, ClobberingInstr);
142 // Returns the first instruction in @MBB which corresponds to
143 // the function epilogue, or nullptr if @MBB doesn't contain an epilogue.
144 static const MachineInstr *getFirstEpilogueInst(const MachineBasicBlock &MBB) {
145 auto LastMI = MBB.getLastNonDebugInstr();
146 if (LastMI == MBB.end() || !LastMI->isReturn())
148 // Assume that epilogue starts with instruction having the same debug location
149 // as the return instruction.
150 DebugLoc LastLoc = LastMI->getDebugLoc();
152 for (MachineBasicBlock::const_reverse_iterator I = LastMI.getReverse(),
155 if (I->getDebugLoc() != LastLoc)
159 // If all instructions have the same debug location, assume whole MBB is
161 return &*MBB.begin();
164 // Collect registers that are modified in the function body (their
165 // contents is changed outside of the prologue and epilogue).
166 static void collectChangingRegs(const MachineFunction *MF,
167 const TargetRegisterInfo *TRI,
169 for (const auto &MBB : *MF) {
170 auto FirstEpilogueInst = getFirstEpilogueInst(MBB);
172 for (const auto &MI : MBB) {
173 // Avoid looking at prologue or epilogue instructions.
174 if (&MI == FirstEpilogueInst)
176 if (MI.getFlag(MachineInstr::FrameSetup))
179 // Look for register defs and register masks. Register masks are
180 // typically on calls and they clobber everything not in the mask.
181 for (const MachineOperand &MO : MI.operands()) {
182 // Skip virtual registers since they are handled by the parent.
183 if (MO.isReg() && MO.isDef() && MO.getReg() &&
184 !TRI->isVirtualRegister(MO.getReg())) {
185 for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid();
188 } else if (MO.isRegMask()) {
189 Regs.setBitsNotInMask(MO.getRegMask());
196 void llvm::calculateDbgEntityHistory(const MachineFunction *MF,
197 const TargetRegisterInfo *TRI,
198 DbgValueHistoryMap &DbgValues,
199 DbgLabelInstrMap &DbgLabels) {
200 BitVector ChangingRegs(TRI->getNumRegs());
201 collectChangingRegs(MF, TRI, ChangingRegs);
203 const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
204 unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
205 RegDescribedVarsMap RegVars;
206 for (const auto &MBB : *MF) {
207 for (const auto &MI : MBB) {
208 if (!MI.isDebugInstr()) {
209 // Not a DBG_VALUE instruction. It may clobber registers which describe
211 for (const MachineOperand &MO : MI.operands()) {
212 if (MO.isReg() && MO.isDef() && MO.getReg()) {
213 // Ignore call instructions that claim to clobber SP. The AArch64
214 // backend does this for aggregate function arguments.
215 if (MI.isCall() && MO.getReg() == SP)
217 // If this is a virtual register, only clobber it since it doesn't
219 if (TRI->isVirtualRegister(MO.getReg()))
220 clobberRegisterUses(RegVars, MO.getReg(), DbgValues, MI);
221 // If this is a register def operand, it may end a debug value
224 for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid();
226 if (ChangingRegs.test(*AI))
227 clobberRegisterUses(RegVars, *AI, DbgValues, MI);
229 } else if (MO.isRegMask()) {
230 // If this is a register mask operand, clobber all debug values in
232 for (unsigned I : ChangingRegs.set_bits()) {
233 // Don't consider SP to be clobbered by register masks.
234 if (unsigned(I) != SP && TRI->isPhysicalRegister(I) &&
235 MO.clobbersPhysReg(I)) {
236 clobberRegisterUses(RegVars, I, DbgValues, MI);
244 if (MI.isDebugValue()) {
245 assert(MI.getNumOperands() > 1 && "Invalid DBG_VALUE instruction!");
246 // Use the base variable (without any DW_OP_piece expressions)
247 // as index into History. The full variables including the
248 // piece expressions are attached to the MI.
249 const DILocalVariable *RawVar = MI.getDebugVariable();
250 assert(RawVar->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
251 "Expected inlined-at fields to agree");
252 InlinedVariable Var(RawVar, MI.getDebugLoc()->getInlinedAt());
254 if (unsigned PrevReg = DbgValues.getRegisterForVar(Var))
255 dropRegDescribedVar(RegVars, PrevReg, Var);
257 DbgValues.startInstrRange(Var, MI);
259 if (unsigned NewReg = isDescribedByReg(MI))
260 addRegDescribedVar(RegVars, NewReg, Var);
261 } else if (MI.isDebugLabel()) {
262 assert(MI.getNumOperands() == 1 && "Invalid DBG_LABEL instruction!");
263 const DILabel *RawLabel = MI.getDebugLabel();
264 assert(RawLabel->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
265 "Expected inlined-at fields to agree");
266 // When collecting debug information for labels, there is no MCSymbol
267 // generated for it. So, we keep MachineInstr in DbgLabels in order
268 // to query MCSymbol afterward.
269 InlinedLabel L(RawLabel, MI.getDebugLoc()->getInlinedAt());
270 DbgLabels.addInstr(L, MI);
274 // Make sure locations for register-described variables are valid only
275 // until the end of the basic block (unless it's the last basic block, in
276 // which case let their liveness run off to the end of the function).
277 if (!MBB.empty() && &MBB != &MF->back()) {
278 for (auto I = RegVars.begin(), E = RegVars.end(); I != E;) {
279 auto CurElem = I++; // CurElem can be erased below.
280 if (TRI->isVirtualRegister(CurElem->first) ||
281 ChangingRegs.test(CurElem->first))
282 clobberRegisterUses(RegVars, CurElem, DbgValues, MBB.back());
288 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
289 LLVM_DUMP_METHOD void DbgValueHistoryMap::dump() const {
290 dbgs() << "DbgValueHistoryMap:\n";
291 for (const auto &VarRangePair : *this) {
292 const InlinedVariable &Var = VarRangePair.first;
293 const InstrRanges &Ranges = VarRangePair.second;
295 const DILocalVariable *LocalVar = Var.first;
296 const DILocation *Location = Var.second;
298 dbgs() << " - " << LocalVar->getName() << " at ";
301 dbgs() << Location->getFilename() << ":" << Location->getLine() << ":"
302 << Location->getColumn();
304 dbgs() << "<unknown location>";
308 for (const InstrRange &Range : Ranges) {
309 dbgs() << " Begin: " << *Range.first;
311 dbgs() << " End : " << *Range.second;