1 //===-- llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp -------*- C++ -*--===//
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 // Common functionality for different debug information format backends.
11 // LLVM currently supports DWARF and CodeView.
13 //===----------------------------------------------------------------------===//
15 #include "DebugHandlerBase.h"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineInstr.h"
19 #include "llvm/CodeGen/MachineModuleInfo.h"
20 #include "llvm/IR/DebugInfo.h"
21 #include "llvm/Target/TargetSubtargetInfo.h"
25 DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {}
27 // Each LexicalScope has first instruction and last instruction to mark
28 // beginning and end of a scope respectively. Create an inverse map that list
29 // scopes starts (and ends) with an instruction. One instruction may start (or
30 // end) multiple scopes. Ignore scopes that are not reachable.
31 void DebugHandlerBase::identifyScopeMarkers() {
32 SmallVector<LexicalScope *, 4> WorkList;
33 WorkList.push_back(LScopes.getCurrentFunctionScope());
34 while (!WorkList.empty()) {
35 LexicalScope *S = WorkList.pop_back_val();
37 const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
38 if (!Children.empty())
39 WorkList.append(Children.begin(), Children.end());
41 if (S->isAbstractScope())
44 for (const InsnRange &R : S->getRanges()) {
45 assert(R.first && "InsnRange does not have first instruction!");
46 assert(R.second && "InsnRange does not have second instruction!");
47 requestLabelBeforeInsn(R.first);
48 requestLabelAfterInsn(R.second);
53 // Return Label preceding the instruction.
54 MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) {
55 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
56 assert(Label && "Didn't insert label before instruction");
60 // Return Label immediately following the instruction.
61 MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) {
62 return LabelsAfterInsn.lookup(MI);
65 // Determine the relative position of the pieces described by P1 and P2.
66 // Returns -1 if P1 is entirely before P2, 0 if P1 and P2 overlap,
67 // 1 if P1 is entirely after P2.
68 int DebugHandlerBase::pieceCmp(const DIExpression *P1, const DIExpression *P2) {
69 unsigned l1 = P1->getBitPieceOffset();
70 unsigned l2 = P2->getBitPieceOffset();
71 unsigned r1 = l1 + P1->getBitPieceSize();
72 unsigned r2 = l2 + P2->getBitPieceSize();
81 /// Determine whether two variable pieces overlap.
82 bool DebugHandlerBase::piecesOverlap(const DIExpression *P1, const DIExpression *P2) {
83 if (!P1->isBitPiece() || !P2->isBitPiece())
85 return pieceCmp(P1, P2) == 0;
88 /// If this type is derived from a base type then return base type size.
89 uint64_t DebugHandlerBase::getBaseTypeSize(const DITypeRef TyRef) {
90 DIType *Ty = TyRef.resolve();
92 DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty);
94 return Ty->getSizeInBits();
96 unsigned Tag = DDTy->getTag();
98 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
99 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
100 Tag != dwarf::DW_TAG_restrict_type)
101 return DDTy->getSizeInBits();
103 DIType *BaseType = DDTy->getBaseType().resolve();
105 assert(BaseType && "Unexpected invalid base type");
107 // If this is a derived type, go ahead and get the base type, unless it's a
108 // reference then it's just the size of the field. Pointer types have no need
109 // of this since they're a different type of qualification on the type.
110 if (BaseType->getTag() == dwarf::DW_TAG_reference_type ||
111 BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type)
112 return Ty->getSizeInBits();
114 return getBaseTypeSize(BaseType);
117 void DebugHandlerBase::beginFunction(const MachineFunction *MF) {
118 // Grab the lexical scopes for the function, if we don't have any of those
119 // then we're not going to be able to do anything.
120 LScopes.initialize(*MF);
124 // Make sure that each lexical scope will have a begin/end label.
125 identifyScopeMarkers();
127 // Calculate history for local variables.
128 assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
129 calculateDbgValueHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
132 // Request labels for the full history.
133 for (const auto &I : DbgValues) {
134 const auto &Ranges = I.second;
138 // The first mention of a function argument gets the CurrentFnBegin
139 // label, so arguments are visible when breaking at function entry.
140 const DILocalVariable *DIVar = Ranges.front().first->getDebugVariable();
141 if (DIVar->isParameter() &&
142 getDISubprogram(DIVar->getScope())->describes(MF->getFunction())) {
143 LabelsBeforeInsn[Ranges.front().first] = Asm->getFunctionBegin();
144 if (Ranges.front().first->getDebugExpression()->isBitPiece()) {
145 // Mark all non-overlapping initial pieces.
146 for (auto I = Ranges.begin(); I != Ranges.end(); ++I) {
147 const DIExpression *Piece = I->first->getDebugExpression();
148 if (std::all_of(Ranges.begin(), I,
149 [&](DbgValueHistoryMap::InstrRange Pred) {
150 return !piecesOverlap(Piece, Pred.first->getDebugExpression());
152 LabelsBeforeInsn[I->first] = Asm->getFunctionBegin();
159 for (const auto &Range : Ranges) {
160 requestLabelBeforeInsn(Range.first);
162 requestLabelAfterInsn(Range.second);
166 PrevInstLoc = DebugLoc();
167 PrevLabel = Asm->getFunctionBegin();
170 void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {
171 if (!MMI->hasDebugInfo())
174 assert(CurMI == nullptr);
177 // Insert labels where requested.
178 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
179 LabelsBeforeInsn.find(MI);
182 if (I == LabelsBeforeInsn.end())
185 // Label already assigned.
190 PrevLabel = MMI->getContext().createTempSymbol();
191 Asm->OutStreamer->EmitLabel(PrevLabel);
193 I->second = PrevLabel;
196 void DebugHandlerBase::endInstruction() {
197 if (!MMI->hasDebugInfo())
200 assert(CurMI != nullptr);
201 // Don't create a new label after DBG_VALUE instructions.
202 // They don't generate code.
203 if (!CurMI->isDebugValue())
206 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
207 LabelsAfterInsn.find(CurMI);
211 if (I == LabelsAfterInsn.end())
214 // Label already assigned.
218 // We need a label after this instruction.
220 PrevLabel = MMI->getContext().createTempSymbol();
221 Asm->OutStreamer->EmitLabel(PrevLabel);
223 I->second = PrevLabel;
226 void DebugHandlerBase::endFunction(const MachineFunction *MF) {
228 LabelsBeforeInsn.clear();
229 LabelsAfterInsn.clear();