blob: 25518a339c61c5a7d588be50ff498d73b075efa8 [file] [log] [blame]
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +00001//===- llvm/CodeGen/AsmPrinter/DbgValueHistoryCalculator.cpp --------------===//
Alexey Samsonov414b6fb2014-04-30 21:34:11 +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#include "DbgValueHistoryCalculator.h"
Benjamin Kramer6bf8af52014-10-06 15:31:04 +000011#include "llvm/ADT/BitVector.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000012#include "llvm/ADT/STLExtras.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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000034// If @MI is a DBG_VALUE with debug value described by a
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000035// defined register, returns the number of this register.
36// In the other case, returns 0.
37static unsigned isDescribedByReg(const MachineInstr &MI) {
38 assert(MI.isDebugValue());
Adrian Prantl87b7eb92014-10-01 18:55:02 +000039 assert(MI.getNumOperands() == 4);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000040 // If location of variable is described using a register (directly or
Dominic Chen6ba19652016-08-11 17:52:40 +000041 // indirectly), this register is always a first operand.
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000042 return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0;
43}
44
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +000045void DbgValueHistoryMap::startInstrRange(InlinedVariable Var,
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000046 const MachineInstr &MI) {
47 // Instruction range should start with a DBG_VALUE instruction for the
48 // variable.
Adrian Prantl87b7eb92014-10-01 18:55:02 +000049 assert(MI.isDebugValue() && "not a DBG_VALUE");
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000050 auto &Ranges = VarInstrRanges[Var];
51 if (!Ranges.empty() && Ranges.back().second == nullptr &&
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +000052 Ranges.back().first->isIdenticalTo(MI)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000053 LLVM_DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
54 << "\t" << Ranges.back().first << "\t" << MI << "\n");
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000055 return;
56 }
57 Ranges.push_back(std::make_pair(&MI, nullptr));
58}
59
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +000060void DbgValueHistoryMap::endInstrRange(InlinedVariable Var,
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000061 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
66 // boundaries.
67 assert(Ranges.back().first->getParent() == MI.getParent());
68 Ranges.back().second = &MI;
69}
70
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +000071unsigned DbgValueHistoryMap::getRegisterForVar(InlinedVariable Var) const {
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000072 const auto &I = VarInstrRanges.find(Var);
73 if (I == VarInstrRanges.end())
74 return 0;
75 const auto &Ranges = I->second;
76 if (Ranges.empty() || Ranges.back().second != nullptr)
77 return 0;
78 return isDescribedByReg(*Ranges.back().first);
79}
80
81namespace {
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000082
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000083// Maps physreg numbers to the variables they describe.
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000084using InlinedVariable = DbgValueHistoryMap::InlinedVariable;
85using RegDescribedVarsMap = std::map<unsigned, SmallVector<InlinedVariable, 1>>;
86
87} // end anonymous namespace
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000088
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000089// Claim that @Var is not described by @RegNo anymore.
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +000090static void dropRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo,
91 InlinedVariable Var) {
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000092 const auto &I = RegVars.find(RegNo);
93 assert(RegNo != 0U && I != RegVars.end());
94 auto &VarSet = I->second;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000095 const auto &VarPos = llvm::find(VarSet, Var);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000096 assert(VarPos != VarSet.end());
97 VarSet.erase(VarPos);
98 // Don't keep empty sets in a map to keep it as small as possible.
99 if (VarSet.empty())
100 RegVars.erase(I);
101}
102
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000103// Claim that @Var is now described by @RegNo.
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000104static void addRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo,
105 InlinedVariable Var) {
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000106 assert(RegNo != 0U);
Alexey Samsonovbb2990d2014-05-27 23:09:50 +0000107 auto &VarSet = RegVars[RegNo];
David Majnemer0d955d02016-08-11 22:21:41 +0000108 assert(!is_contained(VarSet, Var));
Alexey Samsonovbb2990d2014-05-27 23:09:50 +0000109 VarSet.push_back(Var);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000110}
111
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000112// Terminate the location range for variables described by register at
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000113// @I by inserting @ClobberingInstr to their history.
114static void clobberRegisterUses(RegDescribedVarsMap &RegVars,
115 RegDescribedVarsMap::iterator I,
116 DbgValueHistoryMap &HistMap,
117 const MachineInstr &ClobberingInstr) {
118 // Iterate over all variables described by this register and add this
119 // instruction to their history, clobbering it.
120 for (const auto &Var : I->second)
121 HistMap.endInstrRange(Var, ClobberingInstr);
122 RegVars.erase(I);
123}
124
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000125// Terminate the location range for variables described by register
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000126// @RegNo by inserting @ClobberingInstr to their history.
127static void clobberRegisterUses(RegDescribedVarsMap &RegVars, unsigned RegNo,
128 DbgValueHistoryMap &HistMap,
129 const MachineInstr &ClobberingInstr) {
130 const auto &I = RegVars.find(RegNo);
131 if (I == RegVars.end())
132 return;
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000133 clobberRegisterUses(RegVars, I, HistMap, ClobberingInstr);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000134}
135
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000136// Returns the first instruction in @MBB which corresponds to
Alexey Samsonov8000e272014-06-09 21:53:47 +0000137// the function epilogue, or nullptr if @MBB doesn't contain an epilogue.
138static const MachineInstr *getFirstEpilogueInst(const MachineBasicBlock &MBB) {
139 auto LastMI = MBB.getLastNonDebugInstr();
140 if (LastMI == MBB.end() || !LastMI->isReturn())
141 return nullptr;
142 // Assume that epilogue starts with instruction having the same debug location
143 // as the return instruction.
144 DebugLoc LastLoc = LastMI->getDebugLoc();
145 auto Res = LastMI;
Duncan P. N. Exon Smith18720962016-09-11 18:51:28 +0000146 for (MachineBasicBlock::const_reverse_iterator I = LastMI.getReverse(),
147 E = MBB.rend();
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000148 I != E; ++I) {
Alexey Samsonov8000e272014-06-09 21:53:47 +0000149 if (I->getDebugLoc() != LastLoc)
Duncan P. N. Exon Smith5bff5112016-07-08 19:31:47 +0000150 return &*Res;
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000151 Res = &*I;
Alexey Samsonov8000e272014-06-09 21:53:47 +0000152 }
153 // If all instructions have the same debug location, assume whole MBB is
154 // an epilogue.
Duncan P. N. Exon Smith5bff5112016-07-08 19:31:47 +0000155 return &*MBB.begin();
Alexey Samsonov8000e272014-06-09 21:53:47 +0000156}
157
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000158// Collect registers that are modified in the function body (their
Adrian Prantl364d1312014-08-06 18:41:24 +0000159// contents is changed outside of the prologue and epilogue).
Alexey Samsonov8000e272014-06-09 21:53:47 +0000160static void collectChangingRegs(const MachineFunction *MF,
161 const TargetRegisterInfo *TRI,
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000162 BitVector &Regs) {
Alexey Samsonov8000e272014-06-09 21:53:47 +0000163 for (const auto &MBB : *MF) {
164 auto FirstEpilogueInst = getFirstEpilogueInst(MBB);
Adrian Prantle2d63752014-08-06 18:41:19 +0000165
Alexey Samsonov8000e272014-06-09 21:53:47 +0000166 for (const auto &MI : MBB) {
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000167 // Avoid looking at prologue or epilogue instructions.
Adrian Prantle2d63752014-08-06 18:41:19 +0000168 if (&MI == FirstEpilogueInst)
169 break;
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000170 if (MI.getFlag(MachineInstr::FrameSetup))
171 continue;
172
173 // Look for register defs and register masks. Register masks are
174 // typically on calls and they clobber everything not in the mask.
175 for (const MachineOperand &MO : MI.operands()) {
Dominic Chen6ba19652016-08-11 17:52:40 +0000176 // Skip virtual registers since they are handled by the parent.
177 if (MO.isReg() && MO.isDef() && MO.getReg() &&
178 !TRI->isVirtualRegister(MO.getReg())) {
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000179 for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid();
180 ++AI)
181 Regs.set(*AI);
182 } else if (MO.isRegMask()) {
183 Regs.setBitsNotInMask(MO.getRegMask());
184 }
185 }
Alexey Samsonov8000e272014-06-09 21:53:47 +0000186 }
187 }
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000188}
189
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000190void llvm::calculateDbgValueHistory(const MachineFunction *MF,
191 const TargetRegisterInfo *TRI,
192 DbgValueHistoryMap &Result) {
193 BitVector ChangingRegs(TRI->getNumRegs());
Alexey Samsonov8000e272014-06-09 21:53:47 +0000194 collectChangingRegs(MF, TRI, ChangingRegs);
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000195
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000196 const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
197 unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
Alexey Samsonov8000e272014-06-09 21:53:47 +0000198 RegDescribedVarsMap RegVars;
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000199 for (const auto &MBB : *MF) {
200 for (const auto &MI : MBB) {
Shiva Chen801bf7e2018-05-09 02:42:00 +0000201 if (!MI.isDebugInstr()) {
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000202 // Not a DBG_VALUE instruction. It may clobber registers which describe
203 // some variables.
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000204 for (const MachineOperand &MO : MI.operands()) {
205 if (MO.isReg() && MO.isDef() && MO.getReg()) {
Adrian Prantld9cd4d52017-06-01 21:14:58 +0000206 // Ignore call instructions that claim to clobber SP. The AArch64
207 // backend does this for aggregate function arguments.
208 if (MI.isCall() && MO.getReg() == SP)
209 continue;
Dominic Chen6ba19652016-08-11 17:52:40 +0000210 // If this is a virtual register, only clobber it since it doesn't
211 // have aliases.
212 if (TRI->isVirtualRegister(MO.getReg()))
213 clobberRegisterUses(RegVars, MO.getReg(), Result, MI);
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000214 // If this is a register def operand, it may end a debug value
215 // range.
Dominic Chen6ba19652016-08-11 17:52:40 +0000216 else {
217 for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid();
218 ++AI)
219 if (ChangingRegs.test(*AI))
220 clobberRegisterUses(RegVars, *AI, Result, MI);
221 }
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000222 } else if (MO.isRegMask()) {
223 // If this is a register mask operand, clobber all debug values in
224 // non-CSRs.
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +0000225 for (unsigned I : ChangingRegs.set_bits()) {
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000226 // Don't consider SP to be clobbered by register masks.
227 if (unsigned(I) != SP && TRI->isPhysicalRegister(I) &&
228 MO.clobbersPhysReg(I)) {
229 clobberRegisterUses(RegVars, I, Result, MI);
230 }
231 }
232 }
233 }
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000234 continue;
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000235 }
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000236
Shiva Chen801bf7e2018-05-09 02:42:00 +0000237 // Skip DBG_LABEL instructions.
238 if (MI.isDebugLabel())
239 continue;
240
Alexey Samsonovf0e0cca2014-05-27 22:35:00 +0000241 assert(MI.getNumOperands() > 1 && "Invalid DBG_VALUE instruction!");
Adrian Prantlb1416832014-08-01 22:11:58 +0000242 // Use the base variable (without any DW_OP_piece expressions)
243 // as index into History. The full variables including the
244 // piece expressions are attached to the MI.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000245 const DILocalVariable *RawVar = MI.getDebugVariable();
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000246 assert(RawVar->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +0000247 "Expected inlined-at fields to agree");
Duncan P. N. Exon Smith78a95272015-04-16 22:12:59 +0000248 InlinedVariable Var(RawVar, MI.getDebugLoc()->getInlinedAt());
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000249
Alexey Samsonovbb2990d2014-05-27 23:09:50 +0000250 if (unsigned PrevReg = Result.getRegisterForVar(Var))
251 dropRegDescribedVar(RegVars, PrevReg, Var);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000252
Alexey Samsonovbb2990d2014-05-27 23:09:50 +0000253 Result.startInstrRange(Var, MI);
254
255 if (unsigned NewReg = isDescribedByReg(MI))
256 addRegDescribedVar(RegVars, NewReg, Var);
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000257 }
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000258
259 // Make sure locations for register-described variables are valid only
260 // until the end of the basic block (unless it's the last basic block, in
261 // which case let their liveness run off to the end of the function).
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000262 if (!MBB.empty() && &MBB != &MF->back()) {
263 for (auto I = RegVars.begin(), E = RegVars.end(); I != E;) {
264 auto CurElem = I++; // CurElem can be erased below.
Dominic Chen6ba19652016-08-11 17:52:40 +0000265 if (TRI->isVirtualRegister(CurElem->first) ||
266 ChangingRegs.test(CurElem->first))
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000267 clobberRegisterUses(RegVars, CurElem, Result, MBB.back());
268 }
Alexey Samsonov8000e272014-06-09 21:53:47 +0000269 }
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000270 }
271}
Vedant Kumar7224c082018-06-01 22:33:15 +0000272
273#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
274LLVM_DUMP_METHOD void DbgValueHistoryMap::dump() const {
275 dbgs() << "DbgValueHistoryMap:\n";
276 for (const auto &VarRangePair : *this) {
277 const InlinedVariable &Var = VarRangePair.first;
278 const InstrRanges &Ranges = VarRangePair.second;
279
280 const DILocalVariable *LocalVar = Var.first;
281 const DILocation *Location = Var.second;
282
283 dbgs() << " - " << LocalVar->getName() << " at ";
284
285 if (Location)
286 dbgs() << Location->getFilename() << ":" << Location->getLine() << ":"
287 << Location->getColumn();
288 else
289 dbgs() << "<unknown location>";
290
291 dbgs() << " --\n";
292
293 for (const InstrRange &Range : Ranges) {
294 dbgs() << " Begin: " << *Range.first;
295 if (Range.second)
296 dbgs() << " End : " << *Range.second;
297 dbgs() << "\n";
298 }
299 }
300}
301#endif