1*9880d681SAndroid Build Coastguard Worker //===-- llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp -------*- C++ -*--===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // Common functionality for different debug information format backends.
11*9880d681SAndroid Build Coastguard Worker // LLVM currently supports DWARF and CodeView.
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
14*9880d681SAndroid Build Coastguard Worker
15*9880d681SAndroid Build Coastguard Worker #include "DebugHandlerBase.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/AsmPrinter.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunction.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstr.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineModuleInfo.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DebugInfo.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetSubtargetInfo.h"
22*9880d681SAndroid Build Coastguard Worker
23*9880d681SAndroid Build Coastguard Worker using namespace llvm;
24*9880d681SAndroid Build Coastguard Worker
DebugHandlerBase(AsmPrinter * A)25*9880d681SAndroid Build Coastguard Worker DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {}
26*9880d681SAndroid Build Coastguard Worker
27*9880d681SAndroid Build Coastguard Worker // Each LexicalScope has first instruction and last instruction to mark
28*9880d681SAndroid Build Coastguard Worker // beginning and end of a scope respectively. Create an inverse map that list
29*9880d681SAndroid Build Coastguard Worker // scopes starts (and ends) with an instruction. One instruction may start (or
30*9880d681SAndroid Build Coastguard Worker // end) multiple scopes. Ignore scopes that are not reachable.
identifyScopeMarkers()31*9880d681SAndroid Build Coastguard Worker void DebugHandlerBase::identifyScopeMarkers() {
32*9880d681SAndroid Build Coastguard Worker SmallVector<LexicalScope *, 4> WorkList;
33*9880d681SAndroid Build Coastguard Worker WorkList.push_back(LScopes.getCurrentFunctionScope());
34*9880d681SAndroid Build Coastguard Worker while (!WorkList.empty()) {
35*9880d681SAndroid Build Coastguard Worker LexicalScope *S = WorkList.pop_back_val();
36*9880d681SAndroid Build Coastguard Worker
37*9880d681SAndroid Build Coastguard Worker const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
38*9880d681SAndroid Build Coastguard Worker if (!Children.empty())
39*9880d681SAndroid Build Coastguard Worker WorkList.append(Children.begin(), Children.end());
40*9880d681SAndroid Build Coastguard Worker
41*9880d681SAndroid Build Coastguard Worker if (S->isAbstractScope())
42*9880d681SAndroid Build Coastguard Worker continue;
43*9880d681SAndroid Build Coastguard Worker
44*9880d681SAndroid Build Coastguard Worker for (const InsnRange &R : S->getRanges()) {
45*9880d681SAndroid Build Coastguard Worker assert(R.first && "InsnRange does not have first instruction!");
46*9880d681SAndroid Build Coastguard Worker assert(R.second && "InsnRange does not have second instruction!");
47*9880d681SAndroid Build Coastguard Worker requestLabelBeforeInsn(R.first);
48*9880d681SAndroid Build Coastguard Worker requestLabelAfterInsn(R.second);
49*9880d681SAndroid Build Coastguard Worker }
50*9880d681SAndroid Build Coastguard Worker }
51*9880d681SAndroid Build Coastguard Worker }
52*9880d681SAndroid Build Coastguard Worker
53*9880d681SAndroid Build Coastguard Worker // Return Label preceding the instruction.
getLabelBeforeInsn(const MachineInstr * MI)54*9880d681SAndroid Build Coastguard Worker MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) {
55*9880d681SAndroid Build Coastguard Worker MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
56*9880d681SAndroid Build Coastguard Worker assert(Label && "Didn't insert label before instruction");
57*9880d681SAndroid Build Coastguard Worker return Label;
58*9880d681SAndroid Build Coastguard Worker }
59*9880d681SAndroid Build Coastguard Worker
60*9880d681SAndroid Build Coastguard Worker // Return Label immediately following the instruction.
getLabelAfterInsn(const MachineInstr * MI)61*9880d681SAndroid Build Coastguard Worker MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) {
62*9880d681SAndroid Build Coastguard Worker return LabelsAfterInsn.lookup(MI);
63*9880d681SAndroid Build Coastguard Worker }
64*9880d681SAndroid Build Coastguard Worker
65*9880d681SAndroid Build Coastguard Worker // Determine the relative position of the pieces described by P1 and P2.
66*9880d681SAndroid Build Coastguard Worker // Returns -1 if P1 is entirely before P2, 0 if P1 and P2 overlap,
67*9880d681SAndroid Build Coastguard Worker // 1 if P1 is entirely after P2.
pieceCmp(const DIExpression * P1,const DIExpression * P2)68*9880d681SAndroid Build Coastguard Worker int DebugHandlerBase::pieceCmp(const DIExpression *P1, const DIExpression *P2) {
69*9880d681SAndroid Build Coastguard Worker unsigned l1 = P1->getBitPieceOffset();
70*9880d681SAndroid Build Coastguard Worker unsigned l2 = P2->getBitPieceOffset();
71*9880d681SAndroid Build Coastguard Worker unsigned r1 = l1 + P1->getBitPieceSize();
72*9880d681SAndroid Build Coastguard Worker unsigned r2 = l2 + P2->getBitPieceSize();
73*9880d681SAndroid Build Coastguard Worker if (r1 <= l2)
74*9880d681SAndroid Build Coastguard Worker return -1;
75*9880d681SAndroid Build Coastguard Worker else if (r2 <= l1)
76*9880d681SAndroid Build Coastguard Worker return 1;
77*9880d681SAndroid Build Coastguard Worker else
78*9880d681SAndroid Build Coastguard Worker return 0;
79*9880d681SAndroid Build Coastguard Worker }
80*9880d681SAndroid Build Coastguard Worker
81*9880d681SAndroid Build Coastguard Worker /// Determine whether two variable pieces overlap.
piecesOverlap(const DIExpression * P1,const DIExpression * P2)82*9880d681SAndroid Build Coastguard Worker bool DebugHandlerBase::piecesOverlap(const DIExpression *P1, const DIExpression *P2) {
83*9880d681SAndroid Build Coastguard Worker if (!P1->isBitPiece() || !P2->isBitPiece())
84*9880d681SAndroid Build Coastguard Worker return true;
85*9880d681SAndroid Build Coastguard Worker return pieceCmp(P1, P2) == 0;
86*9880d681SAndroid Build Coastguard Worker }
87*9880d681SAndroid Build Coastguard Worker
88*9880d681SAndroid Build Coastguard Worker /// If this type is derived from a base type then return base type size.
getBaseTypeSize(const DITypeRef TyRef)89*9880d681SAndroid Build Coastguard Worker uint64_t DebugHandlerBase::getBaseTypeSize(const DITypeRef TyRef) {
90*9880d681SAndroid Build Coastguard Worker DIType *Ty = TyRef.resolve();
91*9880d681SAndroid Build Coastguard Worker assert(Ty);
92*9880d681SAndroid Build Coastguard Worker DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty);
93*9880d681SAndroid Build Coastguard Worker if (!DDTy)
94*9880d681SAndroid Build Coastguard Worker return Ty->getSizeInBits();
95*9880d681SAndroid Build Coastguard Worker
96*9880d681SAndroid Build Coastguard Worker unsigned Tag = DDTy->getTag();
97*9880d681SAndroid Build Coastguard Worker
98*9880d681SAndroid Build Coastguard Worker if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
99*9880d681SAndroid Build Coastguard Worker Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
100*9880d681SAndroid Build Coastguard Worker Tag != dwarf::DW_TAG_restrict_type)
101*9880d681SAndroid Build Coastguard Worker return DDTy->getSizeInBits();
102*9880d681SAndroid Build Coastguard Worker
103*9880d681SAndroid Build Coastguard Worker DIType *BaseType = DDTy->getBaseType().resolve();
104*9880d681SAndroid Build Coastguard Worker
105*9880d681SAndroid Build Coastguard Worker assert(BaseType && "Unexpected invalid base type");
106*9880d681SAndroid Build Coastguard Worker
107*9880d681SAndroid Build Coastguard Worker // If this is a derived type, go ahead and get the base type, unless it's a
108*9880d681SAndroid Build Coastguard Worker // reference then it's just the size of the field. Pointer types have no need
109*9880d681SAndroid Build Coastguard Worker // of this since they're a different type of qualification on the type.
110*9880d681SAndroid Build Coastguard Worker if (BaseType->getTag() == dwarf::DW_TAG_reference_type ||
111*9880d681SAndroid Build Coastguard Worker BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type)
112*9880d681SAndroid Build Coastguard Worker return Ty->getSizeInBits();
113*9880d681SAndroid Build Coastguard Worker
114*9880d681SAndroid Build Coastguard Worker return getBaseTypeSize(BaseType);
115*9880d681SAndroid Build Coastguard Worker }
116*9880d681SAndroid Build Coastguard Worker
beginFunction(const MachineFunction * MF)117*9880d681SAndroid Build Coastguard Worker void DebugHandlerBase::beginFunction(const MachineFunction *MF) {
118*9880d681SAndroid Build Coastguard Worker // Grab the lexical scopes for the function, if we don't have any of those
119*9880d681SAndroid Build Coastguard Worker // then we're not going to be able to do anything.
120*9880d681SAndroid Build Coastguard Worker LScopes.initialize(*MF);
121*9880d681SAndroid Build Coastguard Worker if (LScopes.empty())
122*9880d681SAndroid Build Coastguard Worker return;
123*9880d681SAndroid Build Coastguard Worker
124*9880d681SAndroid Build Coastguard Worker // Make sure that each lexical scope will have a begin/end label.
125*9880d681SAndroid Build Coastguard Worker identifyScopeMarkers();
126*9880d681SAndroid Build Coastguard Worker
127*9880d681SAndroid Build Coastguard Worker // Calculate history for local variables.
128*9880d681SAndroid Build Coastguard Worker assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
129*9880d681SAndroid Build Coastguard Worker calculateDbgValueHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
130*9880d681SAndroid Build Coastguard Worker DbgValues);
131*9880d681SAndroid Build Coastguard Worker
132*9880d681SAndroid Build Coastguard Worker // Request labels for the full history.
133*9880d681SAndroid Build Coastguard Worker for (const auto &I : DbgValues) {
134*9880d681SAndroid Build Coastguard Worker const auto &Ranges = I.second;
135*9880d681SAndroid Build Coastguard Worker if (Ranges.empty())
136*9880d681SAndroid Build Coastguard Worker continue;
137*9880d681SAndroid Build Coastguard Worker
138*9880d681SAndroid Build Coastguard Worker // The first mention of a function argument gets the CurrentFnBegin
139*9880d681SAndroid Build Coastguard Worker // label, so arguments are visible when breaking at function entry.
140*9880d681SAndroid Build Coastguard Worker const DILocalVariable *DIVar = Ranges.front().first->getDebugVariable();
141*9880d681SAndroid Build Coastguard Worker if (DIVar->isParameter() &&
142*9880d681SAndroid Build Coastguard Worker getDISubprogram(DIVar->getScope())->describes(MF->getFunction())) {
143*9880d681SAndroid Build Coastguard Worker LabelsBeforeInsn[Ranges.front().first] = Asm->getFunctionBegin();
144*9880d681SAndroid Build Coastguard Worker if (Ranges.front().first->getDebugExpression()->isBitPiece()) {
145*9880d681SAndroid Build Coastguard Worker // Mark all non-overlapping initial pieces.
146*9880d681SAndroid Build Coastguard Worker for (auto I = Ranges.begin(); I != Ranges.end(); ++I) {
147*9880d681SAndroid Build Coastguard Worker const DIExpression *Piece = I->first->getDebugExpression();
148*9880d681SAndroid Build Coastguard Worker if (std::all_of(Ranges.begin(), I,
149*9880d681SAndroid Build Coastguard Worker [&](DbgValueHistoryMap::InstrRange Pred) {
150*9880d681SAndroid Build Coastguard Worker return !piecesOverlap(Piece, Pred.first->getDebugExpression());
151*9880d681SAndroid Build Coastguard Worker }))
152*9880d681SAndroid Build Coastguard Worker LabelsBeforeInsn[I->first] = Asm->getFunctionBegin();
153*9880d681SAndroid Build Coastguard Worker else
154*9880d681SAndroid Build Coastguard Worker break;
155*9880d681SAndroid Build Coastguard Worker }
156*9880d681SAndroid Build Coastguard Worker }
157*9880d681SAndroid Build Coastguard Worker }
158*9880d681SAndroid Build Coastguard Worker
159*9880d681SAndroid Build Coastguard Worker for (const auto &Range : Ranges) {
160*9880d681SAndroid Build Coastguard Worker requestLabelBeforeInsn(Range.first);
161*9880d681SAndroid Build Coastguard Worker if (Range.second)
162*9880d681SAndroid Build Coastguard Worker requestLabelAfterInsn(Range.second);
163*9880d681SAndroid Build Coastguard Worker }
164*9880d681SAndroid Build Coastguard Worker }
165*9880d681SAndroid Build Coastguard Worker
166*9880d681SAndroid Build Coastguard Worker PrevInstLoc = DebugLoc();
167*9880d681SAndroid Build Coastguard Worker PrevLabel = Asm->getFunctionBegin();
168*9880d681SAndroid Build Coastguard Worker }
169*9880d681SAndroid Build Coastguard Worker
beginInstruction(const MachineInstr * MI)170*9880d681SAndroid Build Coastguard Worker void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {
171*9880d681SAndroid Build Coastguard Worker if (!MMI->hasDebugInfo())
172*9880d681SAndroid Build Coastguard Worker return;
173*9880d681SAndroid Build Coastguard Worker
174*9880d681SAndroid Build Coastguard Worker assert(CurMI == nullptr);
175*9880d681SAndroid Build Coastguard Worker CurMI = MI;
176*9880d681SAndroid Build Coastguard Worker
177*9880d681SAndroid Build Coastguard Worker // Insert labels where requested.
178*9880d681SAndroid Build Coastguard Worker DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
179*9880d681SAndroid Build Coastguard Worker LabelsBeforeInsn.find(MI);
180*9880d681SAndroid Build Coastguard Worker
181*9880d681SAndroid Build Coastguard Worker // No label needed.
182*9880d681SAndroid Build Coastguard Worker if (I == LabelsBeforeInsn.end())
183*9880d681SAndroid Build Coastguard Worker return;
184*9880d681SAndroid Build Coastguard Worker
185*9880d681SAndroid Build Coastguard Worker // Label already assigned.
186*9880d681SAndroid Build Coastguard Worker if (I->second)
187*9880d681SAndroid Build Coastguard Worker return;
188*9880d681SAndroid Build Coastguard Worker
189*9880d681SAndroid Build Coastguard Worker if (!PrevLabel) {
190*9880d681SAndroid Build Coastguard Worker PrevLabel = MMI->getContext().createTempSymbol();
191*9880d681SAndroid Build Coastguard Worker Asm->OutStreamer->EmitLabel(PrevLabel);
192*9880d681SAndroid Build Coastguard Worker }
193*9880d681SAndroid Build Coastguard Worker I->second = PrevLabel;
194*9880d681SAndroid Build Coastguard Worker }
195*9880d681SAndroid Build Coastguard Worker
endInstruction()196*9880d681SAndroid Build Coastguard Worker void DebugHandlerBase::endInstruction() {
197*9880d681SAndroid Build Coastguard Worker if (!MMI->hasDebugInfo())
198*9880d681SAndroid Build Coastguard Worker return;
199*9880d681SAndroid Build Coastguard Worker
200*9880d681SAndroid Build Coastguard Worker assert(CurMI != nullptr);
201*9880d681SAndroid Build Coastguard Worker // Don't create a new label after DBG_VALUE instructions.
202*9880d681SAndroid Build Coastguard Worker // They don't generate code.
203*9880d681SAndroid Build Coastguard Worker if (!CurMI->isDebugValue())
204*9880d681SAndroid Build Coastguard Worker PrevLabel = nullptr;
205*9880d681SAndroid Build Coastguard Worker
206*9880d681SAndroid Build Coastguard Worker DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
207*9880d681SAndroid Build Coastguard Worker LabelsAfterInsn.find(CurMI);
208*9880d681SAndroid Build Coastguard Worker CurMI = nullptr;
209*9880d681SAndroid Build Coastguard Worker
210*9880d681SAndroid Build Coastguard Worker // No label needed.
211*9880d681SAndroid Build Coastguard Worker if (I == LabelsAfterInsn.end())
212*9880d681SAndroid Build Coastguard Worker return;
213*9880d681SAndroid Build Coastguard Worker
214*9880d681SAndroid Build Coastguard Worker // Label already assigned.
215*9880d681SAndroid Build Coastguard Worker if (I->second)
216*9880d681SAndroid Build Coastguard Worker return;
217*9880d681SAndroid Build Coastguard Worker
218*9880d681SAndroid Build Coastguard Worker // We need a label after this instruction.
219*9880d681SAndroid Build Coastguard Worker if (!PrevLabel) {
220*9880d681SAndroid Build Coastguard Worker PrevLabel = MMI->getContext().createTempSymbol();
221*9880d681SAndroid Build Coastguard Worker Asm->OutStreamer->EmitLabel(PrevLabel);
222*9880d681SAndroid Build Coastguard Worker }
223*9880d681SAndroid Build Coastguard Worker I->second = PrevLabel;
224*9880d681SAndroid Build Coastguard Worker }
225*9880d681SAndroid Build Coastguard Worker
endFunction(const MachineFunction * MF)226*9880d681SAndroid Build Coastguard Worker void DebugHandlerBase::endFunction(const MachineFunction *MF) {
227*9880d681SAndroid Build Coastguard Worker DbgValues.clear();
228*9880d681SAndroid Build Coastguard Worker LabelsBeforeInsn.clear();
229*9880d681SAndroid Build Coastguard Worker LabelsAfterInsn.clear();
230*9880d681SAndroid Build Coastguard Worker }
231