[DAGCombiner] Eliminate dead stores to stack.
[llvm-complete.git] / lib / CodeGen / AsmPrinter / DebugHandlerBase.cpp
blob0680d2040fb4d0faaf6f05760fe821de188bcbca
1 //===-- llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp -------*- C++ -*--===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Common functionality for different debug information format backends.
10 // LLVM currently supports DWARF and CodeView.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/DebugHandlerBase.h"
15 #include "llvm/ADT/Optional.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/CodeGen/AsmPrinter.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineInstr.h"
20 #include "llvm/CodeGen/MachineModuleInfo.h"
21 #include "llvm/CodeGen/TargetSubtargetInfo.h"
22 #include "llvm/IR/DebugInfo.h"
23 #include "llvm/MC/MCStreamer.h"
25 using namespace llvm;
27 #define DEBUG_TYPE "dwarfdebug"
29 Optional<DbgVariableLocation>
30 DbgVariableLocation::extractFromMachineInstruction(
31 const MachineInstr &Instruction) {
32 DbgVariableLocation Location;
33 if (!Instruction.isDebugValue())
34 return None;
35 if (!Instruction.getOperand(0).isReg())
36 return None;
37 Location.Register = Instruction.getOperand(0).getReg();
38 Location.FragmentInfo.reset();
39 // We only handle expressions generated by DIExpression::appendOffset,
40 // which doesn't require a full stack machine.
41 int64_t Offset = 0;
42 const DIExpression *DIExpr = Instruction.getDebugExpression();
43 auto Op = DIExpr->expr_op_begin();
44 while (Op != DIExpr->expr_op_end()) {
45 switch (Op->getOp()) {
46 case dwarf::DW_OP_constu: {
47 int Value = Op->getArg(0);
48 ++Op;
49 if (Op != DIExpr->expr_op_end()) {
50 switch (Op->getOp()) {
51 case dwarf::DW_OP_minus:
52 Offset -= Value;
53 break;
54 case dwarf::DW_OP_plus:
55 Offset += Value;
56 break;
57 default:
58 continue;
61 } break;
62 case dwarf::DW_OP_plus_uconst:
63 Offset += Op->getArg(0);
64 break;
65 case dwarf::DW_OP_LLVM_fragment:
66 Location.FragmentInfo = {Op->getArg(1), Op->getArg(0)};
67 break;
68 case dwarf::DW_OP_deref:
69 Location.LoadChain.push_back(Offset);
70 Offset = 0;
71 break;
72 default:
73 return None;
75 ++Op;
78 // Do one final implicit DW_OP_deref if this was an indirect DBG_VALUE
79 // instruction.
80 // FIXME: Replace these with DIExpression.
81 if (Instruction.isIndirectDebugValue())
82 Location.LoadChain.push_back(Offset);
84 return Location;
87 DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {}
89 // Each LexicalScope has first instruction and last instruction to mark
90 // beginning and end of a scope respectively. Create an inverse map that list
91 // scopes starts (and ends) with an instruction. One instruction may start (or
92 // end) multiple scopes. Ignore scopes that are not reachable.
93 void DebugHandlerBase::identifyScopeMarkers() {
94 SmallVector<LexicalScope *, 4> WorkList;
95 WorkList.push_back(LScopes.getCurrentFunctionScope());
96 while (!WorkList.empty()) {
97 LexicalScope *S = WorkList.pop_back_val();
99 const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
100 if (!Children.empty())
101 WorkList.append(Children.begin(), Children.end());
103 if (S->isAbstractScope())
104 continue;
106 for (const InsnRange &R : S->getRanges()) {
107 assert(R.first && "InsnRange does not have first instruction!");
108 assert(R.second && "InsnRange does not have second instruction!");
109 requestLabelBeforeInsn(R.first);
110 requestLabelAfterInsn(R.second);
115 // Return Label preceding the instruction.
116 MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) {
117 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
118 assert(Label && "Didn't insert label before instruction");
119 return Label;
122 // Return Label immediately following the instruction.
123 MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) {
124 return LabelsAfterInsn.lookup(MI);
127 // Return the function-local offset of an instruction.
128 const MCExpr *
129 DebugHandlerBase::getFunctionLocalOffsetAfterInsn(const MachineInstr *MI) {
130 MCContext &MC = Asm->OutContext;
132 MCSymbol *Start = Asm->getFunctionBegin();
133 const auto *StartRef = MCSymbolRefExpr::create(Start, MC);
135 MCSymbol *AfterInsn = getLabelAfterInsn(MI);
136 assert(AfterInsn && "Expected label after instruction");
137 const auto *AfterRef = MCSymbolRefExpr::create(AfterInsn, MC);
139 return MCBinaryExpr::createSub(AfterRef, StartRef, MC);
142 /// If this type is derived from a base type then return base type size.
143 uint64_t DebugHandlerBase::getBaseTypeSize(const DITypeRef TyRef) {
144 DIType *Ty = TyRef.resolve();
145 assert(Ty);
146 DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty);
147 if (!DDTy)
148 return Ty->getSizeInBits();
150 unsigned Tag = DDTy->getTag();
152 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
153 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
154 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_atomic_type)
155 return DDTy->getSizeInBits();
157 DIType *BaseType = DDTy->getBaseType().resolve();
159 if (!BaseType)
160 return 0;
162 // If this is a derived type, go ahead and get the base type, unless it's a
163 // reference then it's just the size of the field. Pointer types have no need
164 // of this since they're a different type of qualification on the type.
165 if (BaseType->getTag() == dwarf::DW_TAG_reference_type ||
166 BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type)
167 return Ty->getSizeInBits();
169 return getBaseTypeSize(BaseType);
172 static bool hasDebugInfo(const MachineModuleInfo *MMI,
173 const MachineFunction *MF) {
174 if (!MMI->hasDebugInfo())
175 return false;
176 auto *SP = MF->getFunction().getSubprogram();
177 if (!SP)
178 return false;
179 assert(SP->getUnit());
180 auto EK = SP->getUnit()->getEmissionKind();
181 if (EK == DICompileUnit::NoDebug)
182 return false;
183 return true;
186 void DebugHandlerBase::beginFunction(const MachineFunction *MF) {
187 PrevInstBB = nullptr;
189 if (!Asm || !hasDebugInfo(MMI, MF)) {
190 skippedNonDebugFunction();
191 return;
194 // Grab the lexical scopes for the function, if we don't have any of those
195 // then we're not going to be able to do anything.
196 LScopes.initialize(*MF);
197 if (LScopes.empty()) {
198 beginFunctionImpl(MF);
199 return;
202 // Make sure that each lexical scope will have a begin/end label.
203 identifyScopeMarkers();
205 // Calculate history for local variables.
206 assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
207 assert(DbgLabels.empty() && "DbgLabels map wasn't cleaned!");
208 calculateDbgEntityHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
209 DbgValues, DbgLabels);
210 LLVM_DEBUG(DbgValues.dump());
212 // Request labels for the full history.
213 for (const auto &I : DbgValues) {
214 const auto &Ranges = I.second;
215 if (Ranges.empty())
216 continue;
218 auto IsDescribedByReg = [](const MachineInstr *MI) {
219 return MI->getOperand(0).isReg() && MI->getOperand(0).getReg();
222 // The first mention of a function argument gets the CurrentFnBegin label,
223 // so arguments are visible when breaking at function entry.
225 // We do not change the label for values that are described by registers,
226 // as that could place them above their defining instructions. We should
227 // ideally not change the labels for constant debug values either, since
228 // doing that violates the ranges that are calculated in the history map.
229 // However, we currently do not emit debug values for constant arguments
230 // directly at the start of the function, so this code is still useful.
231 const DILocalVariable *DIVar = Ranges.front().first->getDebugVariable();
232 if (DIVar->isParameter() &&
233 getDISubprogram(DIVar->getScope())->describes(&MF->getFunction())) {
234 if (!IsDescribedByReg(Ranges.front().first))
235 LabelsBeforeInsn[Ranges.front().first] = Asm->getFunctionBegin();
236 if (Ranges.front().first->getDebugExpression()->isFragment()) {
237 // Mark all non-overlapping initial fragments.
238 for (auto I = Ranges.begin(); I != Ranges.end(); ++I) {
239 const DIExpression *Fragment = I->first->getDebugExpression();
240 if (std::any_of(Ranges.begin(), I,
241 [&](DbgValueHistoryMap::InstrRange Pred) {
242 return Fragment->fragmentsOverlap(
243 Pred.first->getDebugExpression());
245 break;
246 if (!IsDescribedByReg(I->first))
247 LabelsBeforeInsn[I->first] = Asm->getFunctionBegin();
252 for (const auto &Range : Ranges) {
253 requestLabelBeforeInsn(Range.first);
254 if (Range.second)
255 requestLabelAfterInsn(Range.second);
259 // Ensure there is a symbol before DBG_LABEL.
260 for (const auto &I : DbgLabels) {
261 const MachineInstr *MI = I.second;
262 requestLabelBeforeInsn(MI);
265 PrevInstLoc = DebugLoc();
266 PrevLabel = Asm->getFunctionBegin();
267 beginFunctionImpl(MF);
270 void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {
271 if (!MMI->hasDebugInfo())
272 return;
274 assert(CurMI == nullptr);
275 CurMI = MI;
277 // Insert labels where requested.
278 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
279 LabelsBeforeInsn.find(MI);
281 // No label needed.
282 if (I == LabelsBeforeInsn.end())
283 return;
285 // Label already assigned.
286 if (I->second)
287 return;
289 if (!PrevLabel) {
290 PrevLabel = MMI->getContext().createTempSymbol();
291 Asm->OutStreamer->EmitLabel(PrevLabel);
293 I->second = PrevLabel;
296 void DebugHandlerBase::endInstruction() {
297 if (!MMI->hasDebugInfo())
298 return;
300 assert(CurMI != nullptr);
301 // Don't create a new label after DBG_VALUE and other instructions that don't
302 // generate code.
303 if (!CurMI->isMetaInstruction()) {
304 PrevLabel = nullptr;
305 PrevInstBB = CurMI->getParent();
308 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
309 LabelsAfterInsn.find(CurMI);
310 CurMI = nullptr;
312 // No label needed.
313 if (I == LabelsAfterInsn.end())
314 return;
316 // Label already assigned.
317 if (I->second)
318 return;
320 // We need a label after this instruction.
321 if (!PrevLabel) {
322 PrevLabel = MMI->getContext().createTempSymbol();
323 Asm->OutStreamer->EmitLabel(PrevLabel);
325 I->second = PrevLabel;
328 void DebugHandlerBase::endFunction(const MachineFunction *MF) {
329 if (hasDebugInfo(MMI, MF))
330 endFunctionImpl(MF);
331 DbgValues.clear();
332 DbgLabels.clear();
333 LabelsBeforeInsn.clear();
334 LabelsAfterInsn.clear();