blob: c006f3cc2efc346e595cca0503169177cf050516 [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"
David Stenberg5ffec6d2019-04-10 11:28:20 +000012#include "llvm/ADT/SmallSet.h"
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000013#include "llvm/ADT/SmallVector.h"
Alexey Samsonov414b6fb2014-04-30 21:34:11 +000014#include "llvm/CodeGen/MachineBasicBlock.h"
15#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000016#include "llvm/CodeGen/MachineInstr.h"
17#include "llvm/CodeGen/MachineOperand.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000018#include "llvm/CodeGen/TargetLowering.h"
19#include "llvm/CodeGen/TargetRegisterInfo.h"
20#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000021#include "llvm/IR/DebugInfoMetadata.h"
22#include "llvm/IR/DebugLoc.h"
23#include "llvm/MC/MCRegisterInfo.h"
Alexey Samsonov414b6fb2014-04-30 21:34:11 +000024#include "llvm/Support/Debug.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000025#include "llvm/Support/raw_ostream.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000026#include <cassert>
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000027#include <map>
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000028#include <utility>
29
Benjamin Kramer6bf8af52014-10-06 15:31:04 +000030using namespace llvm;
Alexey Samsonov414b6fb2014-04-30 21:34:11 +000031
32#define DEBUG_TYPE "dwarfdebug"
33
David Stenberg5ffec6d2019-04-10 11:28:20 +000034namespace {
35using EntryIndex = DbgValueHistoryMap::EntryIndex;
36}
37
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000038// If @MI is a DBG_VALUE with debug value described by a
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000039// defined register, returns the number of this register.
40// In the other case, returns 0.
41static unsigned isDescribedByReg(const MachineInstr &MI) {
42 assert(MI.isDebugValue());
Adrian Prantl87b7eb92014-10-01 18:55:02 +000043 assert(MI.getNumOperands() == 4);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000044 // If location of variable is described using a register (directly or
Dominic Chen6ba19652016-08-11 17:52:40 +000045 // indirectly), this register is always a first operand.
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000046 return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0;
47}
48
David Stenberg5ffec6d2019-04-10 11:28:20 +000049bool DbgValueHistoryMap::startDbgValue(InlinedEntity Var,
50 const MachineInstr &MI,
51 EntryIndex &NewIndex) {
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000052 // Instruction range should start with a DBG_VALUE instruction for the
53 // variable.
Adrian Prantl87b7eb92014-10-01 18:55:02 +000054 assert(MI.isDebugValue() && "not a DBG_VALUE");
David Stenberg6feef562019-04-10 09:07:43 +000055 auto &Entries = VarEntries[Var];
David Stenberg5ffec6d2019-04-10 11:28:20 +000056 if (!Entries.empty() && Entries.back().isDbgValue() &&
57 !Entries.back().isClosed() &&
58 Entries.back().getInstr()->isIdenticalTo(MI)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000059 LLVM_DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
David Stenberg5ffec6d2019-04-10 11:28:20 +000060 << "\t" << Entries.back().getInstr() << "\t" << MI
David Stenberg37399792019-04-10 09:07:32 +000061 << "\n");
David Stenberg5ffec6d2019-04-10 11:28:20 +000062 return false;
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000063 }
David Stenberg5ffec6d2019-04-10 11:28:20 +000064 Entries.emplace_back(&MI, Entry::DbgValue);
65 NewIndex = Entries.size() - 1;
66 return true;
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000067}
68
David Stenberg5ffec6d2019-04-10 11:28:20 +000069EntryIndex DbgValueHistoryMap::startClobber(InlinedEntity Var,
70 const MachineInstr &MI) {
David Stenberg6feef562019-04-10 09:07:43 +000071 auto &Entries = VarEntries[Var];
David Stenbergb96943b2019-04-10 11:28:28 +000072 // If an instruction clobbers multiple registers that the variable is
73 // described by, then we may have already created a clobbering instruction.
74 if (Entries.back().isClobber() && Entries.back().getInstr() == &MI)
75 return Entries.size() - 1;
David Stenberg5ffec6d2019-04-10 11:28:20 +000076 Entries.emplace_back(&MI, Entry::Clobber);
77 return Entries.size() - 1;
David Stenberg37399792019-04-10 09:07:32 +000078}
79
David Stenberg5ffec6d2019-04-10 11:28:20 +000080void DbgValueHistoryMap::Entry::endEntry(EntryIndex Index) {
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000081 // For now, instruction ranges are not allowed to cross basic block
82 // boundaries.
David Stenberg5ffec6d2019-04-10 11:28:20 +000083 assert(isDbgValue() && "Setting end index for non-debug value");
84 assert(!isClosed() && "End index has already been set");
85 EndIndex = Index;
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000086}
87
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +000088void DbgLabelInstrMap::addInstr(InlinedEntity Label, const MachineInstr &MI) {
Hsiangkai Wang2532ac82018-08-17 15:22:04 +000089 assert(MI.isDebugLabel() && "not a DBG_LABEL");
90 LabelInstr[Label] = &MI;
91}
92
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000093namespace {
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000094
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000095// Maps physreg numbers to the variables they describe.
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +000096using InlinedEntity = DbgValueHistoryMap::InlinedEntity;
97using RegDescribedVarsMap = std::map<unsigned, SmallVector<InlinedEntity, 1>>;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000098
David Stenberg5ffec6d2019-04-10 11:28:20 +000099// Keeps track of the debug value entries that are currently live for each
100// inlined entity. As the history map entries are stored in a SmallVector, they
101// may be moved at insertion of new entries, so store indices rather than
102// pointers.
103using DbgValueEntriesMap = std::map<InlinedEntity, SmallSet<EntryIndex, 1>>;
104
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000105} // end anonymous namespace
Alexey Samsonovbb2990d2014-05-27 23:09:50 +0000106
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000107// Claim that @Var is not described by @RegNo anymore.
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000108static void dropRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo,
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +0000109 InlinedEntity Var) {
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000110 const auto &I = RegVars.find(RegNo);
111 assert(RegNo != 0U && I != RegVars.end());
112 auto &VarSet = I->second;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000113 const auto &VarPos = llvm::find(VarSet, Var);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000114 assert(VarPos != VarSet.end());
115 VarSet.erase(VarPos);
116 // Don't keep empty sets in a map to keep it as small as possible.
117 if (VarSet.empty())
118 RegVars.erase(I);
119}
120
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000121// Claim that @Var is now described by @RegNo.
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000122static void addRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo,
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +0000123 InlinedEntity Var) {
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000124 assert(RegNo != 0U);
Alexey Samsonovbb2990d2014-05-27 23:09:50 +0000125 auto &VarSet = RegVars[RegNo];
David Majnemer0d955d02016-08-11 22:21:41 +0000126 assert(!is_contained(VarSet, Var));
Alexey Samsonovbb2990d2014-05-27 23:09:50 +0000127 VarSet.push_back(Var);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000128}
129
David Stenbergb96943b2019-04-10 11:28:28 +0000130/// Create a clobbering entry and end all open debug value entries
131/// for \p Var that are described by \p RegNo using that entry.
David Stenberg5ffec6d2019-04-10 11:28:20 +0000132static void clobberRegEntries(InlinedEntity Var, unsigned RegNo,
133 const MachineInstr &ClobberingInstr,
134 DbgValueEntriesMap &LiveEntries,
135 DbgValueHistoryMap &HistMap) {
136 EntryIndex ClobberIndex = HistMap.startClobber(Var, ClobberingInstr);
137
David Stenbergb96943b2019-04-10 11:28:28 +0000138 // Close all entries whose values are described by the register.
139 SmallVector<EntryIndex, 4> IndicesToErase;
140 for (auto Index : LiveEntries[Var]) {
141 auto &Entry = HistMap.getEntry(Var, Index);
142 assert(Entry.isDbgValue() && "Not a DBG_VALUE in LiveEntries");
143 if (isDescribedByReg(*Entry.getInstr()) == RegNo) {
144 IndicesToErase.push_back(Index);
145 Entry.endEntry(ClobberIndex);
146 }
147 }
148
149 // Drop all entries that have ended.
150 for (auto Index : IndicesToErase)
151 LiveEntries[Var].erase(Index);
David Stenberg5ffec6d2019-04-10 11:28:20 +0000152}
153
154/// Add a new debug value for \p Var. Closes all overlapping debug values.
155static void handleNewDebugValue(InlinedEntity Var, const MachineInstr &DV,
156 RegDescribedVarsMap &RegVars,
157 DbgValueEntriesMap &LiveEntries,
158 DbgValueHistoryMap &HistMap) {
David Stenberg5ffec6d2019-04-10 11:28:20 +0000159 EntryIndex NewIndex;
160 if (HistMap.startDbgValue(Var, DV, NewIndex)) {
David Stenbergb96943b2019-04-10 11:28:28 +0000161 SmallDenseMap<unsigned, bool, 4> TrackedRegs;
162
David Stenberg5ffec6d2019-04-10 11:28:20 +0000163 // If we have created a new debug value entry, close all preceding
164 // live entries that overlap.
165 SmallVector<EntryIndex, 4> IndicesToErase;
166 const DIExpression *DIExpr = DV.getDebugExpression();
167 for (auto Index : LiveEntries[Var]) {
168 auto &Entry = HistMap.getEntry(Var, Index);
169 assert(Entry.isDbgValue() && "Not a DBG_VALUE in LiveEntries");
170 const MachineInstr &DV = *Entry.getInstr();
David Stenbergb96943b2019-04-10 11:28:28 +0000171 bool Overlaps = DIExpr->fragmentsOverlap(DV.getDebugExpression());
172 if (Overlaps) {
David Stenberg5ffec6d2019-04-10 11:28:20 +0000173 IndicesToErase.push_back(Index);
174 Entry.endEntry(NewIndex);
175 }
David Stenbergb96943b2019-04-10 11:28:28 +0000176 if (unsigned Reg = isDescribedByReg(DV))
177 TrackedRegs[Reg] |= !Overlaps;
David Stenberg5ffec6d2019-04-10 11:28:20 +0000178 }
David Stenbergb96943b2019-04-10 11:28:28 +0000179
180 // If the new debug value is described by a register, add tracking of
181 // that register if it is not already tracked.
182 if (unsigned NewReg = isDescribedByReg(DV)) {
183 if (!TrackedRegs.count(NewReg))
184 addRegDescribedVar(RegVars, NewReg, Var);
185 LiveEntries[Var].insert(NewIndex);
186 TrackedRegs[NewReg] = true;
187 }
188
189 // Drop tracking of registers that are no longer used.
190 for (auto I : TrackedRegs)
191 if (!I.second)
192 dropRegDescribedVar(RegVars, I.first, Var);
193
David Stenberg5ffec6d2019-04-10 11:28:20 +0000194 // Drop all entries that have ended, and mark the new entry as live.
195 for (auto Index : IndicesToErase)
196 LiveEntries[Var].erase(Index);
197 LiveEntries[Var].insert(NewIndex);
198 }
David Stenberg5ffec6d2019-04-10 11:28:20 +0000199}
200
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000201// Terminate the location range for variables described by register at
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000202// @I by inserting @ClobberingInstr to their history.
203static void clobberRegisterUses(RegDescribedVarsMap &RegVars,
204 RegDescribedVarsMap::iterator I,
205 DbgValueHistoryMap &HistMap,
David Stenberg5ffec6d2019-04-10 11:28:20 +0000206 DbgValueEntriesMap &LiveEntries,
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000207 const MachineInstr &ClobberingInstr) {
208 // Iterate over all variables described by this register and add this
209 // instruction to their history, clobbering it.
210 for (const auto &Var : I->second)
David Stenberg5ffec6d2019-04-10 11:28:20 +0000211 clobberRegEntries(Var, I->first, ClobberingInstr, LiveEntries, HistMap);
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000212 RegVars.erase(I);
213}
214
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000215// Terminate the location range for variables described by register
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000216// @RegNo by inserting @ClobberingInstr to their history.
217static void clobberRegisterUses(RegDescribedVarsMap &RegVars, unsigned RegNo,
218 DbgValueHistoryMap &HistMap,
David Stenberg5ffec6d2019-04-10 11:28:20 +0000219 DbgValueEntriesMap &LiveEntries,
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000220 const MachineInstr &ClobberingInstr) {
221 const auto &I = RegVars.find(RegNo);
222 if (I == RegVars.end())
223 return;
David Stenberg5ffec6d2019-04-10 11:28:20 +0000224 clobberRegisterUses(RegVars, I, HistMap, LiveEntries, ClobberingInstr);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000225}
226
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000227// Returns the first instruction in @MBB which corresponds to
Alexey Samsonov8000e272014-06-09 21:53:47 +0000228// the function epilogue, or nullptr if @MBB doesn't contain an epilogue.
229static const MachineInstr *getFirstEpilogueInst(const MachineBasicBlock &MBB) {
230 auto LastMI = MBB.getLastNonDebugInstr();
231 if (LastMI == MBB.end() || !LastMI->isReturn())
232 return nullptr;
233 // Assume that epilogue starts with instruction having the same debug location
234 // as the return instruction.
235 DebugLoc LastLoc = LastMI->getDebugLoc();
236 auto Res = LastMI;
Duncan P. N. Exon Smith18720962016-09-11 18:51:28 +0000237 for (MachineBasicBlock::const_reverse_iterator I = LastMI.getReverse(),
238 E = MBB.rend();
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000239 I != E; ++I) {
Alexey Samsonov8000e272014-06-09 21:53:47 +0000240 if (I->getDebugLoc() != LastLoc)
Duncan P. N. Exon Smith5bff5112016-07-08 19:31:47 +0000241 return &*Res;
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000242 Res = &*I;
Alexey Samsonov8000e272014-06-09 21:53:47 +0000243 }
244 // If all instructions have the same debug location, assume whole MBB is
245 // an epilogue.
Duncan P. N. Exon Smith5bff5112016-07-08 19:31:47 +0000246 return &*MBB.begin();
Alexey Samsonov8000e272014-06-09 21:53:47 +0000247}
248
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000249// Collect registers that are modified in the function body (their
Adrian Prantl364d1312014-08-06 18:41:24 +0000250// contents is changed outside of the prologue and epilogue).
Alexey Samsonov8000e272014-06-09 21:53:47 +0000251static void collectChangingRegs(const MachineFunction *MF,
252 const TargetRegisterInfo *TRI,
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000253 BitVector &Regs) {
Alexey Samsonov8000e272014-06-09 21:53:47 +0000254 for (const auto &MBB : *MF) {
255 auto FirstEpilogueInst = getFirstEpilogueInst(MBB);
Adrian Prantle2d63752014-08-06 18:41:19 +0000256
Alexey Samsonov8000e272014-06-09 21:53:47 +0000257 for (const auto &MI : MBB) {
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000258 // Avoid looking at prologue or epilogue instructions.
Adrian Prantle2d63752014-08-06 18:41:19 +0000259 if (&MI == FirstEpilogueInst)
260 break;
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000261 if (MI.getFlag(MachineInstr::FrameSetup))
262 continue;
263
264 // Look for register defs and register masks. Register masks are
265 // typically on calls and they clobber everything not in the mask.
266 for (const MachineOperand &MO : MI.operands()) {
Dominic Chen6ba19652016-08-11 17:52:40 +0000267 // Skip virtual registers since they are handled by the parent.
268 if (MO.isReg() && MO.isDef() && MO.getReg() &&
269 !TRI->isVirtualRegister(MO.getReg())) {
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000270 for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid();
271 ++AI)
272 Regs.set(*AI);
273 } else if (MO.isRegMask()) {
274 Regs.setBitsNotInMask(MO.getRegMask());
275 }
276 }
Alexey Samsonov8000e272014-06-09 21:53:47 +0000277 }
278 }
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000279}
280
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000281void llvm::calculateDbgEntityHistory(const MachineFunction *MF,
282 const TargetRegisterInfo *TRI,
283 DbgValueHistoryMap &DbgValues,
284 DbgLabelInstrMap &DbgLabels) {
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000285 BitVector ChangingRegs(TRI->getNumRegs());
Alexey Samsonov8000e272014-06-09 21:53:47 +0000286 collectChangingRegs(MF, TRI, ChangingRegs);
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000287
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000288 const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
289 unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
Alexey Samsonov8000e272014-06-09 21:53:47 +0000290 RegDescribedVarsMap RegVars;
David Stenberg5ffec6d2019-04-10 11:28:20 +0000291 DbgValueEntriesMap LiveEntries;
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000292 for (const auto &MBB : *MF) {
293 for (const auto &MI : MBB) {
Shiva Chen801bf7e2018-05-09 02:42:00 +0000294 if (!MI.isDebugInstr()) {
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000295 // Not a DBG_VALUE instruction. It may clobber registers which describe
296 // some variables.
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000297 for (const MachineOperand &MO : MI.operands()) {
298 if (MO.isReg() && MO.isDef() && MO.getReg()) {
Adrian Prantld9cd4d52017-06-01 21:14:58 +0000299 // Ignore call instructions that claim to clobber SP. The AArch64
300 // backend does this for aggregate function arguments.
301 if (MI.isCall() && MO.getReg() == SP)
302 continue;
Dominic Chen6ba19652016-08-11 17:52:40 +0000303 // If this is a virtual register, only clobber it since it doesn't
304 // have aliases.
305 if (TRI->isVirtualRegister(MO.getReg()))
David Stenberg5ffec6d2019-04-10 11:28:20 +0000306 clobberRegisterUses(RegVars, MO.getReg(), DbgValues, LiveEntries,
307 MI);
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000308 // If this is a register def operand, it may end a debug value
309 // range.
Dominic Chen6ba19652016-08-11 17:52:40 +0000310 else {
311 for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid();
312 ++AI)
313 if (ChangingRegs.test(*AI))
David Stenberg5ffec6d2019-04-10 11:28:20 +0000314 clobberRegisterUses(RegVars, *AI, DbgValues, LiveEntries, MI);
Dominic Chen6ba19652016-08-11 17:52:40 +0000315 }
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000316 } else if (MO.isRegMask()) {
317 // If this is a register mask operand, clobber all debug values in
318 // non-CSRs.
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +0000319 for (unsigned I : ChangingRegs.set_bits()) {
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000320 // Don't consider SP to be clobbered by register masks.
321 if (unsigned(I) != SP && TRI->isPhysicalRegister(I) &&
322 MO.clobbersPhysReg(I)) {
David Stenberg5ffec6d2019-04-10 11:28:20 +0000323 clobberRegisterUses(RegVars, I, DbgValues, LiveEntries, MI);
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000324 }
325 }
326 }
327 }
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000328 continue;
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000329 }
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000330
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000331 if (MI.isDebugValue()) {
332 assert(MI.getNumOperands() > 1 && "Invalid DBG_VALUE instruction!");
333 // Use the base variable (without any DW_OP_piece expressions)
334 // as index into History. The full variables including the
335 // piece expressions are attached to the MI.
336 const DILocalVariable *RawVar = MI.getDebugVariable();
337 assert(RawVar->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
338 "Expected inlined-at fields to agree");
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +0000339 InlinedEntity Var(RawVar, MI.getDebugLoc()->getInlinedAt());
Shiva Chen801bf7e2018-05-09 02:42:00 +0000340
David Stenberg5ffec6d2019-04-10 11:28:20 +0000341 handleNewDebugValue(Var, MI, RegVars, LiveEntries, DbgValues);
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000342 } else if (MI.isDebugLabel()) {
343 assert(MI.getNumOperands() == 1 && "Invalid DBG_LABEL instruction!");
344 const DILabel *RawLabel = MI.getDebugLabel();
345 assert(RawLabel->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
346 "Expected inlined-at fields to agree");
347 // When collecting debug information for labels, there is no MCSymbol
348 // generated for it. So, we keep MachineInstr in DbgLabels in order
349 // to query MCSymbol afterward.
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +0000350 InlinedEntity L(RawLabel, MI.getDebugLoc()->getInlinedAt());
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000351 DbgLabels.addInstr(L, MI);
352 }
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000353 }
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000354
Eric Christopherc93f56d2019-05-08 23:54:03 +0000355 // Make sure locations for register-described variables are valid only
356 // until the end of the basic block (unless it's the last basic block, in
357 // which case let their liveness run off to the end of the function).
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000358 if (!MBB.empty() && &MBB != &MF->back()) {
Eric Christopherc93f56d2019-05-08 23:54:03 +0000359 for (auto I = RegVars.begin(), E = RegVars.end(); I != E;) {
360 auto CurElem = I++; // CurElem can be erased below.
361 if (TRI->isVirtualRegister(CurElem->first) ||
362 ChangingRegs.test(CurElem->first))
363 clobberRegisterUses(RegVars, CurElem, DbgValues, LiveEntries,
364 MBB.back());
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000365 }
Alexey Samsonov8000e272014-06-09 21:53:47 +0000366 }
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000367 }
368}
Vedant Kumar7224c082018-06-01 22:33:15 +0000369
370#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
371LLVM_DUMP_METHOD void DbgValueHistoryMap::dump() const {
372 dbgs() << "DbgValueHistoryMap:\n";
373 for (const auto &VarRangePair : *this) {
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +0000374 const InlinedEntity &Var = VarRangePair.first;
David Stenberg6feef562019-04-10 09:07:43 +0000375 const Entries &Entries = VarRangePair.second;
Vedant Kumar7224c082018-06-01 22:33:15 +0000376
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +0000377 const DILocalVariable *LocalVar = cast<DILocalVariable>(Var.first);
Vedant Kumar7224c082018-06-01 22:33:15 +0000378 const DILocation *Location = Var.second;
379
380 dbgs() << " - " << LocalVar->getName() << " at ";
381
382 if (Location)
383 dbgs() << Location->getFilename() << ":" << Location->getLine() << ":"
384 << Location->getColumn();
385 else
386 dbgs() << "<unknown location>";
387
388 dbgs() << " --\n";
389
David Stenberg5ffec6d2019-04-10 11:28:20 +0000390 for (const auto &E : enumerate(Entries)) {
391 const auto &Entry = E.value();
392 dbgs() << " Entry[" << E.index() << "]: ";
393 if (Entry.isDbgValue())
394 dbgs() << "Debug value\n";
395 else
396 dbgs() << "Clobber\n";
397 dbgs() << " Instr: " << *Entry.getInstr();
398 if (Entry.isDbgValue()) {
399 if (Entry.getEndIndex() == NoEntry)
400 dbgs() << " - Valid until end of function\n";
401 else
402 dbgs() << " - Closed by Entry[" << Entry.getEndIndex() << "]\n";
403 }
Vedant Kumar7224c082018-06-01 22:33:15 +0000404 dbgs() << "\n";
405 }
406 }
407}
408#endif