1 //===-- llvm/CodeGen/AsmPrinter/DbgValueHistoryCalculator.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 "DbgValueHistoryCalculator.h"
11 #include "llvm/ADT/BitVector.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/CodeGen/MachineBasicBlock.h"
14 #include "llvm/CodeGen/MachineFunction.h"
15 #include "llvm/IR/DebugInfo.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include "llvm/Target/TargetLowering.h"
19 #include "llvm/Target/TargetRegisterInfo.h"
20 #include "llvm/Target/TargetSubtargetInfo.h"
25 #define DEBUG_TYPE "dwarfdebug"
27 // \brief If @MI is a DBG_VALUE with debug value described by a
28 // defined register, returns the number of this register.
29 // In the other case, returns 0.
30 static unsigned isDescribedByReg(const MachineInstr &MI) {
31 assert(MI.isDebugValue());
32 assert(MI.getNumOperands() == 4);
33 // If location of variable is described using a register (directly or
34 // indirectly), this register is always a first operand.
35 return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0;
38 void DbgValueHistoryMap::startInstrRange(InlinedVariable Var,
39 const MachineInstr &MI) {
40 // Instruction range should start with a DBG_VALUE instruction for the
42 assert(MI.isDebugValue() && "not a DBG_VALUE");
43 auto &Ranges = VarInstrRanges[Var];
44 if (!Ranges.empty() && Ranges.back().second == nullptr &&
45 Ranges.back().first->isIdenticalTo(MI)) {
46 DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
47 << "\t" << Ranges.back().first << "\t" << MI << "\n");
50 Ranges.push_back(std::make_pair(&MI, nullptr));
53 void DbgValueHistoryMap::endInstrRange(InlinedVariable Var,
54 const MachineInstr &MI) {
55 auto &Ranges = VarInstrRanges[Var];
56 // Verify that the current instruction range is not yet closed.
57 assert(!Ranges.empty() && Ranges.back().second == nullptr);
58 // For now, instruction ranges are not allowed to cross basic block
60 assert(Ranges.back().first->getParent() == MI.getParent());
61 Ranges.back().second = &MI;
64 unsigned DbgValueHistoryMap::getRegisterForVar(InlinedVariable Var) const {
65 const auto &I = VarInstrRanges.find(Var);
66 if (I == VarInstrRanges.end())
68 const auto &Ranges = I->second;
69 if (Ranges.empty() || Ranges.back().second != nullptr)
71 return isDescribedByReg(*Ranges.back().first);
75 // Maps physreg numbers to the variables they describe.
76 typedef DbgValueHistoryMap::InlinedVariable InlinedVariable;
77 typedef std::map<unsigned, SmallVector<InlinedVariable, 1>> RegDescribedVarsMap;
80 // \brief Claim that @Var is not described by @RegNo anymore.
81 static void dropRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo,
82 InlinedVariable Var) {
83 const auto &I = RegVars.find(RegNo);
84 assert(RegNo != 0U && I != RegVars.end());
85 auto &VarSet = I->second;
86 const auto &VarPos = find(VarSet, Var);
87 assert(VarPos != VarSet.end());
89 // Don't keep empty sets in a map to keep it as small as possible.
94 // \brief Claim that @Var is now described by @RegNo.
95 static void addRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo,
96 InlinedVariable Var) {
98 auto &VarSet = RegVars[RegNo];
99 assert(!is_contained(VarSet, Var));
100 VarSet.push_back(Var);
103 // \brief Terminate the location range for variables described by register at
104 // @I by inserting @ClobberingInstr to their history.
105 static void clobberRegisterUses(RegDescribedVarsMap &RegVars,
106 RegDescribedVarsMap::iterator I,
107 DbgValueHistoryMap &HistMap,
108 const MachineInstr &ClobberingInstr) {
109 // Iterate over all variables described by this register and add this
110 // instruction to their history, clobbering it.
111 for (const auto &Var : I->second)
112 HistMap.endInstrRange(Var, ClobberingInstr);
116 // \brief Terminate the location range for variables described by register
117 // @RegNo by inserting @ClobberingInstr to their history.
118 static void clobberRegisterUses(RegDescribedVarsMap &RegVars, unsigned RegNo,
119 DbgValueHistoryMap &HistMap,
120 const MachineInstr &ClobberingInstr) {
121 const auto &I = RegVars.find(RegNo);
122 if (I == RegVars.end())
124 clobberRegisterUses(RegVars, I, HistMap, ClobberingInstr);
127 // \brief Returns the first instruction in @MBB which corresponds to
128 // the function epilogue, or nullptr if @MBB doesn't contain an epilogue.
129 static const MachineInstr *getFirstEpilogueInst(const MachineBasicBlock &MBB) {
130 auto LastMI = MBB.getLastNonDebugInstr();
131 if (LastMI == MBB.end() || !LastMI->isReturn())
133 // Assume that epilogue starts with instruction having the same debug location
134 // as the return instruction.
135 DebugLoc LastLoc = LastMI->getDebugLoc();
137 for (MachineBasicBlock::const_reverse_iterator I = LastMI.getReverse(),
140 if (I->getDebugLoc() != LastLoc)
144 // If all instructions have the same debug location, assume whole MBB is
146 return &*MBB.begin();
149 // \brief Collect registers that are modified in the function body (their
150 // contents is changed outside of the prologue and epilogue).
151 static void collectChangingRegs(const MachineFunction *MF,
152 const TargetRegisterInfo *TRI,
154 for (const auto &MBB : *MF) {
155 auto FirstEpilogueInst = getFirstEpilogueInst(MBB);
157 for (const auto &MI : MBB) {
158 // Avoid looking at prologue or epilogue instructions.
159 if (&MI == FirstEpilogueInst)
161 if (MI.getFlag(MachineInstr::FrameSetup))
164 // Look for register defs and register masks. Register masks are
165 // typically on calls and they clobber everything not in the mask.
166 for (const MachineOperand &MO : MI.operands()) {
167 // Skip virtual registers since they are handled by the parent.
168 if (MO.isReg() && MO.isDef() && MO.getReg() &&
169 !TRI->isVirtualRegister(MO.getReg())) {
170 for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid();
173 } else if (MO.isRegMask()) {
174 Regs.setBitsNotInMask(MO.getRegMask());
181 void llvm::calculateDbgValueHistory(const MachineFunction *MF,
182 const TargetRegisterInfo *TRI,
183 DbgValueHistoryMap &Result) {
184 BitVector ChangingRegs(TRI->getNumRegs());
185 collectChangingRegs(MF, TRI, ChangingRegs);
187 const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
188 unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
189 RegDescribedVarsMap RegVars;
190 for (const auto &MBB : *MF) {
191 for (const auto &MI : MBB) {
192 if (!MI.isDebugValue()) {
193 // Not a DBG_VALUE instruction. It may clobber registers which describe
195 for (const MachineOperand &MO : MI.operands()) {
196 if (MO.isReg() && MO.isDef() && MO.getReg()) {
197 // Ignore call instructions that claim to clobber SP. The AArch64
198 // backend does this for aggregate function arguments.
199 if (MI.isCall() && MO.getReg() == SP)
201 // If this is a virtual register, only clobber it since it doesn't
203 if (TRI->isVirtualRegister(MO.getReg()))
204 clobberRegisterUses(RegVars, MO.getReg(), Result, MI);
205 // If this is a register def operand, it may end a debug value
208 for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid();
210 if (ChangingRegs.test(*AI))
211 clobberRegisterUses(RegVars, *AI, Result, MI);
213 } else if (MO.isRegMask()) {
214 // If this is a register mask operand, clobber all debug values in
216 for (unsigned I : ChangingRegs.set_bits()) {
217 // Don't consider SP to be clobbered by register masks.
218 if (unsigned(I) != SP && TRI->isPhysicalRegister(I) &&
219 MO.clobbersPhysReg(I)) {
220 clobberRegisterUses(RegVars, I, Result, MI);
228 assert(MI.getNumOperands() > 1 && "Invalid DBG_VALUE instruction!");
229 // Use the base variable (without any DW_OP_piece expressions)
230 // as index into History. The full variables including the
231 // piece expressions are attached to the MI.
232 const DILocalVariable *RawVar = MI.getDebugVariable();
233 assert(RawVar->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
234 "Expected inlined-at fields to agree");
235 InlinedVariable Var(RawVar, MI.getDebugLoc()->getInlinedAt());
237 if (unsigned PrevReg = Result.getRegisterForVar(Var))
238 dropRegDescribedVar(RegVars, PrevReg, Var);
240 Result.startInstrRange(Var, MI);
242 if (unsigned NewReg = isDescribedByReg(MI))
243 addRegDescribedVar(RegVars, NewReg, Var);
246 // Make sure locations for register-described variables are valid only
247 // until the end of the basic block (unless it's the last basic block, in
248 // which case let their liveness run off to the end of the function).
249 if (!MBB.empty() && &MBB != &MF->back()) {
250 for (auto I = RegVars.begin(), E = RegVars.end(); I != E;) {
251 auto CurElem = I++; // CurElem can be erased below.
252 if (TRI->isVirtualRegister(CurElem->first) ||
253 ChangingRegs.test(CurElem->first))
254 clobberRegisterUses(RegVars, CurElem, Result, MBB.back());