LLVM 19.0.0git
DebugLocEntry.h
Go to the documentation of this file.
1//===-- llvm/CodeGen/DebugLocEntry.h - Entry in debug_loc list -*- 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#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCENTRY_H
10#define LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCENTRY_H
11
12#include "DebugLocStream.h"
13#include "llvm/Config/llvm-config.h"
14#include "llvm/IR/Constants.h"
15#include "llvm/IR/DebugInfo.h"
16#include "llvm/MC/MCSymbol.h"
18#include "llvm/Support/Debug.h"
19
20namespace llvm {
21class AsmPrinter;
22
23/// This struct describes target specific location.
25 int Index;
26 int Offset;
27
29 TargetIndexLocation(unsigned Idx, int64_t Offset)
30 : Index(Idx), Offset(Offset) {}
31
33 return Index == Other.Index && Offset == Other.Offset;
34 }
35};
36
37/// A single location or constant within a variable location description, with
38/// either a single entry (with an optional DIExpression) used for a DBG_VALUE,
39/// or a list of entries used for a DBG_VALUE_LIST.
41
42 /// Type of entry that this represents.
43 enum EntryType {
44 E_Location,
45 E_Integer,
46 E_ConstantFP,
47 E_ConstantInt,
48 E_TargetIndexLocation
49 };
50 enum EntryType EntryKind;
51
52 /// Either a constant,
53 union {
54 int64_t Int;
57 } Constant;
58
59 union {
60 /// Or a location in the machine frame.
62 /// Or a location from target specific location.
64 };
65
66public:
67 DbgValueLocEntry(int64_t i) : EntryKind(E_Integer) { Constant.Int = i; }
68 DbgValueLocEntry(const ConstantFP *CFP) : EntryKind(E_ConstantFP) {
69 Constant.CFP = CFP;
70 }
71 DbgValueLocEntry(const ConstantInt *CIP) : EntryKind(E_ConstantInt) {
72 Constant.CIP = CIP;
73 }
74 DbgValueLocEntry(MachineLocation Loc) : EntryKind(E_Location), Loc(Loc) {}
76 : EntryKind(E_TargetIndexLocation), TIL(Loc) {}
77
78 bool isLocation() const { return EntryKind == E_Location; }
79 bool isIndirectLocation() const {
80 return EntryKind == E_Location && Loc.isIndirect();
81 }
82 bool isTargetIndexLocation() const {
83 return EntryKind == E_TargetIndexLocation;
84 }
85 bool isInt() const { return EntryKind == E_Integer; }
86 bool isConstantFP() const { return EntryKind == E_ConstantFP; }
87 bool isConstantInt() const { return EntryKind == E_ConstantInt; }
88 int64_t getInt() const { return Constant.Int; }
89 const ConstantFP *getConstantFP() const { return Constant.CFP; }
90 const ConstantInt *getConstantInt() const { return Constant.CIP; }
91 MachineLocation getLoc() const { return Loc; }
93 friend bool operator==(const DbgValueLocEntry &, const DbgValueLocEntry &);
94#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
95 LLVM_DUMP_METHOD void dump() const {
96 if (isLocation()) {
97 llvm::dbgs() << "Loc = { reg=" << Loc.getReg() << " ";
98 if (Loc.isIndirect())
99 llvm::dbgs() << "+0";
100 llvm::dbgs() << "} ";
101 } else if (isConstantInt())
102 Constant.CIP->dump();
103 else if (isConstantFP())
104 Constant.CFP->dump();
105 }
106#endif
107};
108
109/// The location of a single variable, composed of an expression and 0 or more
110/// DbgValueLocEntries.
112 /// Any complex address location expression for this DbgValueLoc.
114
115 SmallVector<DbgValueLocEntry, 2> ValueLocEntries;
116
117 bool IsVariadic;
118
119public:
121 : Expression(Expr), ValueLocEntries(Locs.begin(), Locs.end()),
122 IsVariadic(true) {}
123
125 bool IsVariadic)
126 : Expression(Expr), ValueLocEntries(Locs.begin(), Locs.end()),
127 IsVariadic(IsVariadic) {
128#ifndef NDEBUG
129 assert(Expr->isValid() ||
130 !any_of(Locs, [](auto LE) { return LE.isLocation(); }));
131 if (!IsVariadic) {
132 assert(ValueLocEntries.size() == 1);
133 }
134#endif
135 }
136
138 : Expression(Expr), ValueLocEntries(1, Loc), IsVariadic(false) {
139 assert(((Expr && Expr->isValid()) || !Loc.isLocation()) &&
140 "DBG_VALUE with a machine location must have a valid expression.");
141 }
142
143 bool isFragment() const { return getExpression()->isFragment(); }
144 bool isEntryVal() const { return getExpression()->isEntryValue(); }
145 bool isVariadic() const { return IsVariadic; }
146 bool isEquivalent(const DbgValueLoc &Other) const {
147 // Cannot be equivalent with different numbers of entries.
148 if (ValueLocEntries.size() != Other.ValueLocEntries.size())
149 return false;
150 bool ThisIsIndirect =
151 !IsVariadic && ValueLocEntries[0].isIndirectLocation();
152 bool OtherIsIndirect =
153 !Other.IsVariadic && Other.ValueLocEntries[0].isIndirectLocation();
154 // Check equivalence of DIExpressions + Directness together.
155 if (!DIExpression::isEqualExpression(Expression, ThisIsIndirect,
156 Other.Expression, OtherIsIndirect))
157 return false;
158 // Indirectness should have been accounted for in the above check, so just
159 // compare register values directly here.
160 if (ThisIsIndirect || OtherIsIndirect) {
161 DbgValueLocEntry ThisOp = ValueLocEntries[0];
162 DbgValueLocEntry OtherOp = Other.ValueLocEntries[0];
163 return ThisOp.isLocation() && OtherOp.isLocation() &&
164 ThisOp.getLoc().getReg() == OtherOp.getLoc().getReg();
165 }
166 // If neither are indirect, then just compare the loc entries directly.
167 return ValueLocEntries == Other.ValueLocEntries;
168 }
169 const DIExpression *getExpression() const { return Expression; }
170 ArrayRef<DbgValueLocEntry> getLocEntries() const { return ValueLocEntries; }
171 friend bool operator==(const DbgValueLoc &, const DbgValueLoc &);
172 friend bool operator<(const DbgValueLoc &, const DbgValueLoc &);
173#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
174 LLVM_DUMP_METHOD void dump() const {
175 for (const DbgValueLocEntry &DV : ValueLocEntries)
176 DV.dump();
177 if (Expression)
178 Expression->dump();
179 }
180#endif
181};
182
183/// This struct describes location entries emitted in the .debug_loc
184/// section.
186 /// Begin and end symbols for the address range that this location is valid.
187 const MCSymbol *Begin;
188 const MCSymbol *End;
189
190 /// A nonempty list of locations/constants belonging to this entry,
191 /// sorted by offset.
193
194public:
195 /// Create a location list entry for the range [\p Begin, \p End).
196 ///
197 /// \param Vals One or more values describing (parts of) the variable.
198 DebugLocEntry(const MCSymbol *Begin, const MCSymbol *End,
200 : Begin(Begin), End(End) {
201 addValues(Vals);
202 }
203
204 /// Attempt to merge this DebugLocEntry with Next and return
205 /// true if the merge was successful. Entries can be merged if they
206 /// share the same Loc/Constant and if Next immediately follows this
207 /// Entry.
208 bool MergeRanges(const DebugLocEntry &Next) {
209 // If this and Next are describing the same variable, merge them.
210 if (End != Next.Begin)
211 return false;
212 if (Values.size() != Next.Values.size())
213 return false;
214 for (unsigned EntryIdx = 0; EntryIdx < Values.size(); ++EntryIdx)
215 if (!Values[EntryIdx].isEquivalent(Next.Values[EntryIdx]))
216 return false;
217 End = Next.End;
218 return true;
219 }
220
221 const MCSymbol *getBeginSym() const { return Begin; }
222 const MCSymbol *getEndSym() const { return End; }
223 ArrayRef<DbgValueLoc> getValues() const { return Values; }
225 Values.append(Vals.begin(), Vals.end());
227 assert((Values.size() == 1 || all_of(Values, [](DbgValueLoc V) {
228 return V.isFragment();
229 })) && "must either have a single value or multiple pieces");
230 }
231
232 // Sort the pieces by offset.
233 // Remove any duplicate entries by dropping all but the first.
235 // Values is either 1 item that does not have a fragment, or many items
236 // that all do. No need to sort if the former and also prevents operator<
237 // being called on a non fragment item when _GLIBCXX_DEBUG is defined.
238 if (Values.size() == 1)
239 return;
240 llvm::sort(Values);
241 Values.erase(std::unique(Values.begin(), Values.end(),
242 [](const DbgValueLoc &A, const DbgValueLoc &B) {
243 return A.getExpression() == B.getExpression();
244 }),
245 Values.end());
246 }
247
248 /// Lower this entry into a DWARF expression.
249 void finalize(const AsmPrinter &AP,
251 const DIBasicType *BT,
252 DwarfCompileUnit &TheCU);
253};
254
255/// Compare two DbgValueLocEntries for equality.
256inline bool operator==(const DbgValueLocEntry &A, const DbgValueLocEntry &B) {
257 if (A.EntryKind != B.EntryKind)
258 return false;
259
260 switch (A.EntryKind) {
261 case DbgValueLocEntry::E_Location:
262 return A.Loc == B.Loc;
263 case DbgValueLocEntry::E_TargetIndexLocation:
264 return A.TIL == B.TIL;
265 case DbgValueLocEntry::E_Integer:
266 return A.Constant.Int == B.Constant.Int;
267 case DbgValueLocEntry::E_ConstantFP:
268 return A.Constant.CFP == B.Constant.CFP;
269 case DbgValueLocEntry::E_ConstantInt:
270 return A.Constant.CIP == B.Constant.CIP;
271 }
272 llvm_unreachable("unhandled EntryKind");
273}
274
275/// Compare two DbgValueLocs for equality.
276inline bool operator==(const DbgValueLoc &A, const DbgValueLoc &B) {
277 return A.ValueLocEntries == B.ValueLocEntries &&
278 A.Expression == B.Expression && A.IsVariadic == B.IsVariadic;
279}
280
281/// Compare two fragments based on their offset.
282inline bool operator<(const DbgValueLoc &A,
283 const DbgValueLoc &B) {
284 return A.getExpression()->getFragmentInfo()->OffsetInBits <
285 B.getExpression()->getFragmentInfo()->OffsetInBits;
286}
287
288}
289
290#endif
arc branch finalize
basic Basic Alias true
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:529
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
bool End
Definition: ELF_riscv.cpp:480
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
iterator end() const
Definition: ArrayRef.h:154
iterator begin() const
Definition: ArrayRef.h:153
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:84
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:268
This is the shared class of boolean and integer constants.
Definition: Constants.h:80
This is an important base class in LLVM.
Definition: Constant.h:41
Basic type, like 'int' or 'float'.
DWARF expression.
bool isEntryValue() const
Check if the expression consists of exactly one entry value operand.
bool isFragment() const
Return whether this is a piece of an aggregate variable.
static bool isEqualExpression(const DIExpression *FirstExpr, bool FirstIndirect, const DIExpression *SecondExpr, bool SecondIndirect)
Determines whether two debug values should produce equivalent DWARF expressions, using their DIExpres...
A single location or constant within a variable location description, with either a single entry (wit...
Definition: DebugLocEntry.h:40
DbgValueLocEntry(const ConstantFP *CFP)
Definition: DebugLocEntry.h:68
TargetIndexLocation getTargetIndexLocation() const
Definition: DebugLocEntry.h:92
MachineLocation Loc
Or a location in the machine frame.
Definition: DebugLocEntry.h:61
friend bool operator==(const DbgValueLocEntry &, const DbgValueLocEntry &)
Compare two DbgValueLocEntries for equality.
const ConstantInt * CIP
Definition: DebugLocEntry.h:56
const ConstantFP * getConstantFP() const
Definition: DebugLocEntry.h:89
int64_t getInt() const
Definition: DebugLocEntry.h:88
bool isConstantFP() const
Definition: DebugLocEntry.h:86
DbgValueLocEntry(MachineLocation Loc)
Definition: DebugLocEntry.h:74
DbgValueLocEntry(int64_t i)
Definition: DebugLocEntry.h:67
bool isConstantInt() const
Definition: DebugLocEntry.h:87
TargetIndexLocation TIL
Or a location from target specific location.
Definition: DebugLocEntry.h:63
bool isLocation() const
Definition: DebugLocEntry.h:78
bool isIndirectLocation() const
Definition: DebugLocEntry.h:79
const ConstantInt * getConstantInt() const
Definition: DebugLocEntry.h:90
LLVM_DUMP_METHOD void dump() const
Definition: DebugLocEntry.h:95
bool isTargetIndexLocation() const
Definition: DebugLocEntry.h:82
MachineLocation getLoc() const
Definition: DebugLocEntry.h:91
DbgValueLocEntry(const ConstantInt *CIP)
Definition: DebugLocEntry.h:71
DbgValueLocEntry(TargetIndexLocation Loc)
Definition: DebugLocEntry.h:75
const ConstantFP * CFP
Definition: DebugLocEntry.h:55
The location of a single variable, composed of an expression and 0 or more DbgValueLocEntries.
DbgValueLoc(const DIExpression *Expr, DbgValueLocEntry Loc)
DbgValueLoc(const DIExpression *Expr, ArrayRef< DbgValueLocEntry > Locs, bool IsVariadic)
ArrayRef< DbgValueLocEntry > getLocEntries() const
DbgValueLoc(const DIExpression *Expr, ArrayRef< DbgValueLocEntry > Locs)
const DIExpression * getExpression() const
friend bool operator<(const DbgValueLoc &, const DbgValueLoc &)
Compare two fragments based on their offset.
friend bool operator==(const DbgValueLoc &, const DbgValueLoc &)
Compare two DbgValueLocs for equality.
bool isFragment() const
LLVM_DUMP_METHOD void dump() const
bool isVariadic() const
bool isEquivalent(const DbgValueLoc &Other) const
bool isEntryVal() const
This struct describes location entries emitted in the .debug_loc section.
DebugLocEntry(const MCSymbol *Begin, const MCSymbol *End, ArrayRef< DbgValueLoc > Vals)
Create a location list entry for the range [Begin, End).
void addValues(ArrayRef< DbgValueLoc > Vals)
ArrayRef< DbgValueLoc > getValues() const
const MCSymbol * getBeginSym() const
const MCSymbol * getEndSym() const
bool MergeRanges(const DebugLocEntry &Next)
Attempt to merge this DebugLocEntry with Next and return true if the merge was successful.
Builder for DebugLocStream lists.
Class representing an expression and its matching format.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:40
unsigned getReg() const
size_t size() const
Definition: SmallVector.h:91
iterator erase(const_iterator CI)
Definition: SmallVector.h:750
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:696
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
void dump() const
Support for debugging, callable in GDB: V->dump()
Definition: AsmWriter.cpp:5219
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool operator<(int64_t V1, const APSInt &V2)
Definition: APSInt.h:361
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1731
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1738
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1656
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
@ Other
Any other memory.
This struct describes target specific location.
Definition: DebugLocEntry.h:24
bool operator==(const TargetIndexLocation &Other) const
Definition: DebugLocEntry.h:32
TargetIndexLocation(unsigned Idx, int64_t Offset)
Definition: DebugLocEntry.h:29