blob: 4f828da55e271c3419ccd09d63f0f621c179f6c2 [file] [log] [blame]
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001//===- llvm/CodeGen/AsmPrinter/DbgEntityHistoryCalculator.cpp -------------===//
Alexey Samsonov414b6fb2014-04-30 21:34:11 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Alexey Samsonov414b6fb2014-04-30 21:34:11 +00006//
7//===----------------------------------------------------------------------===//
8
Yonghong Song61b189e2018-12-18 23:10:17 +00009#include "llvm/CodeGen/DbgEntityHistoryCalculator.h"
Benjamin Kramer6bf8af52014-10-06 15:31:04 +000010#include "llvm/ADT/BitVector.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000011#include "llvm/ADT/STLExtras.h"
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000012#include "llvm/ADT/SmallVector.h"
Alexey Samsonov414b6fb2014-04-30 21:34:11 +000013#include "llvm/CodeGen/MachineBasicBlock.h"
14#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000015#include "llvm/CodeGen/MachineInstr.h"
16#include "llvm/CodeGen/MachineOperand.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000017#include "llvm/CodeGen/TargetLowering.h"
18#include "llvm/CodeGen/TargetRegisterInfo.h"
19#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000020#include "llvm/IR/DebugInfoMetadata.h"
21#include "llvm/IR/DebugLoc.h"
22#include "llvm/MC/MCRegisterInfo.h"
Alexey Samsonov414b6fb2014-04-30 21:34:11 +000023#include "llvm/Support/Debug.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000024#include "llvm/Support/raw_ostream.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000025#include <cassert>
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000026#include <map>
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000027#include <utility>
28
Benjamin Kramer6bf8af52014-10-06 15:31:04 +000029using namespace llvm;
Alexey Samsonov414b6fb2014-04-30 21:34:11 +000030
31#define DEBUG_TYPE "dwarfdebug"
32
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000033// If @MI is a DBG_VALUE with debug value described by a
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000034// defined register, returns the number of this register.
35// In the other case, returns 0.
36static unsigned isDescribedByReg(const MachineInstr &MI) {
37 assert(MI.isDebugValue());
Adrian Prantl87b7eb92014-10-01 18:55:02 +000038 assert(MI.getNumOperands() == 4);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000039 // If location of variable is described using a register (directly or
Dominic Chen6ba19652016-08-11 17:52:40 +000040 // indirectly), this register is always a first operand.
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000041 return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0;
42}
43
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +000044void DbgValueHistoryMap::startInstrRange(InlinedEntity Var,
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000045 const MachineInstr &MI) {
46 // Instruction range should start with a DBG_VALUE instruction for the
47 // variable.
Adrian Prantl87b7eb92014-10-01 18:55:02 +000048 assert(MI.isDebugValue() && "not a DBG_VALUE");
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000049 auto &Ranges = VarInstrRanges[Var];
50 if (!Ranges.empty() && Ranges.back().second == nullptr &&
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +000051 Ranges.back().first->isIdenticalTo(MI)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000052 LLVM_DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
53 << "\t" << Ranges.back().first << "\t" << MI << "\n");
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000054 return;
55 }
56 Ranges.push_back(std::make_pair(&MI, nullptr));
57}
58
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +000059void DbgValueHistoryMap::endInstrRange(InlinedEntity Var,
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000060 const MachineInstr &MI) {
61 auto &Ranges = VarInstrRanges[Var];
62 // Verify that the current instruction range is not yet closed.
63 assert(!Ranges.empty() && Ranges.back().second == nullptr);
64 // For now, instruction ranges are not allowed to cross basic block
65 // boundaries.
66 assert(Ranges.back().first->getParent() == MI.getParent());
67 Ranges.back().second = &MI;
68}
69
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +000070unsigned DbgValueHistoryMap::getRegisterForVar(InlinedEntity Var) const {
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000071 const auto &I = VarInstrRanges.find(Var);
72 if (I == VarInstrRanges.end())
73 return 0;
74 const auto &Ranges = I->second;
75 if (Ranges.empty() || Ranges.back().second != nullptr)
76 return 0;
77 return isDescribedByReg(*Ranges.back().first);
78}
79
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +000080void DbgLabelInstrMap::addInstr(InlinedEntity Label, const MachineInstr &MI) {
Hsiangkai Wang2532ac82018-08-17 15:22:04 +000081 assert(MI.isDebugLabel() && "not a DBG_LABEL");
82 LabelInstr[Label] = &MI;
83}
84
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000085namespace {
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000086
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000087// Maps physreg numbers to the variables they describe.
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +000088using InlinedEntity = DbgValueHistoryMap::InlinedEntity;
89using RegDescribedVarsMap = std::map<unsigned, SmallVector<InlinedEntity, 1>>;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000090
91} // end anonymous namespace
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000092
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000093// Claim that @Var is not described by @RegNo anymore.
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +000094static void dropRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo,
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +000095 InlinedEntity Var) {
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000096 const auto &I = RegVars.find(RegNo);
97 assert(RegNo != 0U && I != RegVars.end());
98 auto &VarSet = I->second;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000099 const auto &VarPos = llvm::find(VarSet, Var);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000100 assert(VarPos != VarSet.end());
101 VarSet.erase(VarPos);
102 // Don't keep empty sets in a map to keep it as small as possible.
103 if (VarSet.empty())
104 RegVars.erase(I);
105}
106
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000107// Claim that @Var is now described by @RegNo.
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000108static void addRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo,
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +0000109 InlinedEntity Var) {
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000110 assert(RegNo != 0U);
Alexey Samsonovbb2990d2014-05-27 23:09:50 +0000111 auto &VarSet = RegVars[RegNo];
David Majnemer0d955d02016-08-11 22:21:41 +0000112 assert(!is_contained(VarSet, Var));
Alexey Samsonovbb2990d2014-05-27 23:09:50 +0000113 VarSet.push_back(Var);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000114}
115
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000116// Terminate the location range for variables described by register at
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000117// @I by inserting @ClobberingInstr to their history.
118static void clobberRegisterUses(RegDescribedVarsMap &RegVars,
119 RegDescribedVarsMap::iterator I,
120 DbgValueHistoryMap &HistMap,
121 const MachineInstr &ClobberingInstr) {
122 // Iterate over all variables described by this register and add this
123 // instruction to their history, clobbering it.
124 for (const auto &Var : I->second)
125 HistMap.endInstrRange(Var, ClobberingInstr);
126 RegVars.erase(I);
127}
128
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000129// Terminate the location range for variables described by register
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000130// @RegNo by inserting @ClobberingInstr to their history.
131static void clobberRegisterUses(RegDescribedVarsMap &RegVars, unsigned RegNo,
132 DbgValueHistoryMap &HistMap,
133 const MachineInstr &ClobberingInstr) {
134 const auto &I = RegVars.find(RegNo);
135 if (I == RegVars.end())
136 return;
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000137 clobberRegisterUses(RegVars, I, HistMap, ClobberingInstr);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000138}
139
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000140// Returns the first instruction in @MBB which corresponds to
Alexey Samsonov8000e272014-06-09 21:53:47 +0000141// the function epilogue, or nullptr if @MBB doesn't contain an epilogue.
142static const MachineInstr *getFirstEpilogueInst(const MachineBasicBlock &MBB) {
143 auto LastMI = MBB.getLastNonDebugInstr();
144 if (LastMI == MBB.end() || !LastMI->isReturn())
145 return nullptr;
146 // Assume that epilogue starts with instruction having the same debug location
147 // as the return instruction.
148 DebugLoc LastLoc = LastMI->getDebugLoc();
149 auto Res = LastMI;
Duncan P. N. Exon Smith18720962016-09-11 18:51:28 +0000150 for (MachineBasicBlock::const_reverse_iterator I = LastMI.getReverse(),
151 E = MBB.rend();
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000152 I != E; ++I) {
Alexey Samsonov8000e272014-06-09 21:53:47 +0000153 if (I->getDebugLoc() != LastLoc)
Duncan P. N. Exon Smith5bff5112016-07-08 19:31:47 +0000154 return &*Res;
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000155 Res = &*I;
Alexey Samsonov8000e272014-06-09 21:53:47 +0000156 }
157 // If all instructions have the same debug location, assume whole MBB is
158 // an epilogue.
Duncan P. N. Exon Smith5bff5112016-07-08 19:31:47 +0000159 return &*MBB.begin();
Alexey Samsonov8000e272014-06-09 21:53:47 +0000160}
161
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000162// Collect registers that are modified in the function body (their
Adrian Prantl364d1312014-08-06 18:41:24 +0000163// contents is changed outside of the prologue and epilogue).
Alexey Samsonov8000e272014-06-09 21:53:47 +0000164static void collectChangingRegs(const MachineFunction *MF,
165 const TargetRegisterInfo *TRI,
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000166 BitVector &Regs) {
Alexey Samsonov8000e272014-06-09 21:53:47 +0000167 for (const auto &MBB : *MF) {
168 auto FirstEpilogueInst = getFirstEpilogueInst(MBB);
Adrian Prantle2d63752014-08-06 18:41:19 +0000169
Alexey Samsonov8000e272014-06-09 21:53:47 +0000170 for (const auto &MI : MBB) {
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000171 // Avoid looking at prologue or epilogue instructions.
Adrian Prantle2d63752014-08-06 18:41:19 +0000172 if (&MI == FirstEpilogueInst)
173 break;
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000174 if (MI.getFlag(MachineInstr::FrameSetup))
175 continue;
176
177 // Look for register defs and register masks. Register masks are
178 // typically on calls and they clobber everything not in the mask.
179 for (const MachineOperand &MO : MI.operands()) {
Dominic Chen6ba19652016-08-11 17:52:40 +0000180 // Skip virtual registers since they are handled by the parent.
181 if (MO.isReg() && MO.isDef() && MO.getReg() &&
182 !TRI->isVirtualRegister(MO.getReg())) {
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000183 for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid();
184 ++AI)
185 Regs.set(*AI);
186 } else if (MO.isRegMask()) {
187 Regs.setBitsNotInMask(MO.getRegMask());
188 }
189 }
Alexey Samsonov8000e272014-06-09 21:53:47 +0000190 }
191 }
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000192}
193
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000194void llvm::calculateDbgEntityHistory(const MachineFunction *MF,
195 const TargetRegisterInfo *TRI,
196 DbgValueHistoryMap &DbgValues,
197 DbgLabelInstrMap &DbgLabels) {
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000198 BitVector ChangingRegs(TRI->getNumRegs());
Alexey Samsonov8000e272014-06-09 21:53:47 +0000199 collectChangingRegs(MF, TRI, ChangingRegs);
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000200
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000201 const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
202 unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
Alexey Samsonov8000e272014-06-09 21:53:47 +0000203 RegDescribedVarsMap RegVars;
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000204 for (const auto &MBB : *MF) {
205 for (const auto &MI : MBB) {
Shiva Chen801bf7e2018-05-09 02:42:00 +0000206 if (!MI.isDebugInstr()) {
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000207 // Not a DBG_VALUE instruction. It may clobber registers which describe
208 // some variables.
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000209 for (const MachineOperand &MO : MI.operands()) {
210 if (MO.isReg() && MO.isDef() && MO.getReg()) {
Adrian Prantld9cd4d52017-06-01 21:14:58 +0000211 // Ignore call instructions that claim to clobber SP. The AArch64
212 // backend does this for aggregate function arguments.
213 if (MI.isCall() && MO.getReg() == SP)
214 continue;
Dominic Chen6ba19652016-08-11 17:52:40 +0000215 // If this is a virtual register, only clobber it since it doesn't
216 // have aliases.
217 if (TRI->isVirtualRegister(MO.getReg()))
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000218 clobberRegisterUses(RegVars, MO.getReg(), DbgValues, MI);
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000219 // If this is a register def operand, it may end a debug value
220 // range.
Dominic Chen6ba19652016-08-11 17:52:40 +0000221 else {
222 for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid();
223 ++AI)
224 if (ChangingRegs.test(*AI))
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000225 clobberRegisterUses(RegVars, *AI, DbgValues, MI);
Dominic Chen6ba19652016-08-11 17:52:40 +0000226 }
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000227 } else if (MO.isRegMask()) {
228 // If this is a register mask operand, clobber all debug values in
229 // non-CSRs.
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +0000230 for (unsigned I : ChangingRegs.set_bits()) {
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000231 // Don't consider SP to be clobbered by register masks.
232 if (unsigned(I) != SP && TRI->isPhysicalRegister(I) &&
233 MO.clobbersPhysReg(I)) {
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000234 clobberRegisterUses(RegVars, I, DbgValues, MI);
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000235 }
236 }
237 }
238 }
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000239 continue;
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000240 }
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000241
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000242 if (MI.isDebugValue()) {
243 assert(MI.getNumOperands() > 1 && "Invalid DBG_VALUE instruction!");
244 // Use the base variable (without any DW_OP_piece expressions)
245 // as index into History. The full variables including the
246 // piece expressions are attached to the MI.
247 const DILocalVariable *RawVar = MI.getDebugVariable();
248 assert(RawVar->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
249 "Expected inlined-at fields to agree");
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +0000250 InlinedEntity Var(RawVar, MI.getDebugLoc()->getInlinedAt());
Shiva Chen801bf7e2018-05-09 02:42:00 +0000251
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000252 if (unsigned PrevReg = DbgValues.getRegisterForVar(Var))
253 dropRegDescribedVar(RegVars, PrevReg, Var);
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000254
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000255 DbgValues.startInstrRange(Var, MI);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000256
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000257 if (unsigned NewReg = isDescribedByReg(MI))
258 addRegDescribedVar(RegVars, NewReg, Var);
259 } else if (MI.isDebugLabel()) {
260 assert(MI.getNumOperands() == 1 && "Invalid DBG_LABEL instruction!");
261 const DILabel *RawLabel = MI.getDebugLabel();
262 assert(RawLabel->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
263 "Expected inlined-at fields to agree");
264 // When collecting debug information for labels, there is no MCSymbol
265 // generated for it. So, we keep MachineInstr in DbgLabels in order
266 // to query MCSymbol afterward.
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +0000267 InlinedEntity L(RawLabel, MI.getDebugLoc()->getInlinedAt());
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000268 DbgLabels.addInstr(L, MI);
269 }
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000270 }
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000271
272 // Make sure locations for register-described variables are valid only
273 // until the end of the basic block (unless it's the last basic block, in
274 // which case let their liveness run off to the end of the function).
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000275 if (!MBB.empty() && &MBB != &MF->back()) {
276 for (auto I = RegVars.begin(), E = RegVars.end(); I != E;) {
277 auto CurElem = I++; // CurElem can be erased below.
Dominic Chen6ba19652016-08-11 17:52:40 +0000278 if (TRI->isVirtualRegister(CurElem->first) ||
279 ChangingRegs.test(CurElem->first))
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000280 clobberRegisterUses(RegVars, CurElem, DbgValues, MBB.back());
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000281 }
Alexey Samsonov8000e272014-06-09 21:53:47 +0000282 }
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000283 }
284}
Vedant Kumar7224c082018-06-01 22:33:15 +0000285
286#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
287LLVM_DUMP_METHOD void DbgValueHistoryMap::dump() const {
288 dbgs() << "DbgValueHistoryMap:\n";
289 for (const auto &VarRangePair : *this) {
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +0000290 const InlinedEntity &Var = VarRangePair.first;
Vedant Kumar7224c082018-06-01 22:33:15 +0000291 const InstrRanges &Ranges = VarRangePair.second;
292
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +0000293 const DILocalVariable *LocalVar = cast<DILocalVariable>(Var.first);
Vedant Kumar7224c082018-06-01 22:33:15 +0000294 const DILocation *Location = Var.second;
295
296 dbgs() << " - " << LocalVar->getName() << " at ";
297
298 if (Location)
299 dbgs() << Location->getFilename() << ":" << Location->getLine() << ":"
300 << Location->getColumn();
301 else
302 dbgs() << "<unknown location>";
303
304 dbgs() << " --\n";
305
306 for (const InstrRange &Range : Ranges) {
307 dbgs() << " Begin: " << *Range.first;
308 if (Range.second)
309 dbgs() << " End : " << *Range.second;
310 dbgs() << "\n";
311 }
312 }
313}
314#endif