LLVM  14.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 
20 namespace llvm {
21 class AsmPrinter;
22 
23 /// This struct describes target specific location.
25  int Index;
26  int Offset;
27 
28  TargetIndexLocation() = default;
29  TargetIndexLocation(unsigned Idx, int64_t Offset)
30  : Index(Idx), Offset(Offset) {}
31 
32  bool operator==(const TargetIndexLocation &Other) const {
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;
55  const ConstantFP *CFP;
56  const ConstantInt *CIP;
57  } Constant;
58 
59  union {
60  /// Or a location in the machine frame.
62  /// Or a location from target specific location.
64  };
65 
66 public:
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 isTargetIndexLocation() const {
80  return EntryKind == E_TargetIndexLocation;
81  }
82  bool isInt() const { return EntryKind == E_Integer; }
83  bool isConstantFP() const { return EntryKind == E_ConstantFP; }
84  bool isConstantInt() const { return EntryKind == E_ConstantInt; }
85  int64_t getInt() const { return Constant.Int; }
86  const ConstantFP *getConstantFP() const { return Constant.CFP; }
87  const ConstantInt *getConstantInt() const { return Constant.CIP; }
88  MachineLocation getLoc() const { return Loc; }
90  friend bool operator==(const DbgValueLocEntry &, const DbgValueLocEntry &);
91 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
92  LLVM_DUMP_METHOD void dump() const {
93  if (isLocation()) {
94  llvm::dbgs() << "Loc = { reg=" << Loc.getReg() << " ";
95  if (Loc.isIndirect())
96  llvm::dbgs() << "+0";
97  llvm::dbgs() << "} ";
98  } else if (isConstantInt())
99  Constant.CIP->dump();
100  else if (isConstantFP())
101  Constant.CFP->dump();
102  }
103 #endif
104 };
105 
106 /// The location of a single variable, composed of an expression and 0 or more
107 /// DbgValueLocEntries.
108 class DbgValueLoc {
109  /// Any complex address location expression for this DbgValueLoc.
110  const DIExpression *Expression;
111 
112  SmallVector<DbgValueLocEntry, 2> ValueLocEntries;
113 
114  bool IsVariadic;
115 
116 public:
118  : Expression(Expr), ValueLocEntries(Locs.begin(), Locs.end()),
119  IsVariadic(true) {
120 #ifndef NDEBUG
121  // Currently, DBG_VALUE_VAR expressions must use stack_value.
122  assert(Expr && Expr->isValid() &&
123  is_contained(Locs, dwarf::DW_OP_stack_value));
124 #endif
125  }
126 
128  bool IsVariadic)
129  : Expression(Expr), ValueLocEntries(Locs.begin(), Locs.end()),
130  IsVariadic(IsVariadic) {
131 #ifndef NDEBUG
132  assert(cast<DIExpression>(Expr)->isValid() ||
133  !any_of(Locs, [](auto LE) { return LE.isLocation(); }));
134  if (!IsVariadic) {
135  assert(ValueLocEntries.size() == 1);
136  } else {
137  // Currently, DBG_VALUE_VAR expressions must use stack_value.
138  assert(Expr && Expr->isValid() &&
139  is_contained(Expr->getElements(), dwarf::DW_OP_stack_value));
140  }
141 #endif
142  }
143 
145  : Expression(Expr), ValueLocEntries(1, Loc), IsVariadic(false) {
146  assert(((Expr && Expr->isValid()) || !Loc.isLocation()) &&
147  "DBG_VALUE with a machine location must have a valid expression.");
148  }
149 
150  bool isFragment() const { return getExpression()->isFragment(); }
151  bool isEntryVal() const { return getExpression()->isEntryValue(); }
152  bool isVariadic() const { return IsVariadic; }
153  const DIExpression *getExpression() const { return Expression; }
155  return ValueLocEntries;
156  }
157  friend bool operator==(const DbgValueLoc &, const DbgValueLoc &);
158  friend bool operator<(const DbgValueLoc &, const DbgValueLoc &);
159 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
160  LLVM_DUMP_METHOD void dump() const {
161  for (const DbgValueLocEntry &DV : ValueLocEntries)
162  DV.dump();
163  if (Expression)
164  Expression->dump();
165  }
166 #endif
167 };
168 
169 /// This struct describes location entries emitted in the .debug_loc
170 /// section.
172  /// Begin and end symbols for the address range that this location is valid.
173  const MCSymbol *Begin;
174  const MCSymbol *End;
175 
176  /// A nonempty list of locations/constants belonging to this entry,
177  /// sorted by offset.
179 
180 public:
181  /// Create a location list entry for the range [\p Begin, \p End).
182  ///
183  /// \param Vals One or more values describing (parts of) the variable.
184  DebugLocEntry(const MCSymbol *Begin, const MCSymbol *End,
186  : Begin(Begin), End(End) {
187  addValues(Vals);
188  }
189 
190  /// Attempt to merge this DebugLocEntry with Next and return
191  /// true if the merge was successful. Entries can be merged if they
192  /// share the same Loc/Constant and if Next immediately follows this
193  /// Entry.
194  bool MergeRanges(const DebugLocEntry &Next) {
195  // If this and Next are describing the same variable, merge them.
196  if ((End == Next.Begin && Values == Next.Values)) {
197  End = Next.End;
198  return true;
199  }
200  return false;
201  }
202 
203  const MCSymbol *getBeginSym() const { return Begin; }
204  const MCSymbol *getEndSym() const { return End; }
205  ArrayRef<DbgValueLoc> getValues() const { return Values; }
207  Values.append(Vals.begin(), Vals.end());
209  assert((Values.size() == 1 || all_of(Values, [](DbgValueLoc V) {
210  return V.isFragment();
211  })) && "must either have a single value or multiple pieces");
212  }
213 
214  // Sort the pieces by offset.
215  // Remove any duplicate entries by dropping all but the first.
217  llvm::sort(Values);
218  Values.erase(std::unique(Values.begin(), Values.end(),
219  [](const DbgValueLoc &A, const DbgValueLoc &B) {
220  return A.getExpression() == B.getExpression();
221  }),
222  Values.end());
223  }
224 
225  /// Lower this entry into a DWARF expression.
226  void finalize(const AsmPrinter &AP,
228  const DIBasicType *BT,
229  DwarfCompileUnit &TheCU);
230 };
231 
232 /// Compare two DbgValueLocEntries for equality.
233 inline bool operator==(const DbgValueLocEntry &A, const DbgValueLocEntry &B) {
234  if (A.EntryKind != B.EntryKind)
235  return false;
236 
237  switch (A.EntryKind) {
238  case DbgValueLocEntry::E_Location:
239  return A.Loc == B.Loc;
240  case DbgValueLocEntry::E_TargetIndexLocation:
241  return A.TIL == B.TIL;
242  case DbgValueLocEntry::E_Integer:
243  return A.Constant.Int == B.Constant.Int;
244  case DbgValueLocEntry::E_ConstantFP:
245  return A.Constant.CFP == B.Constant.CFP;
246  case DbgValueLocEntry::E_ConstantInt:
247  return A.Constant.CIP == B.Constant.CIP;
248  }
249  llvm_unreachable("unhandled EntryKind");
250 }
251 
252 /// Compare two DbgValueLocs for equality.
253 inline bool operator==(const DbgValueLoc &A, const DbgValueLoc &B) {
254  return A.ValueLocEntries == B.ValueLocEntries &&
255  A.Expression == B.Expression && A.IsVariadic == B.IsVariadic;
256 }
257 
258 /// Compare two fragments based on their offset.
259 inline bool operator<(const DbgValueLoc &A,
260  const DbgValueLoc &B) {
261  return A.getExpression()->getFragmentInfo()->OffsetInBits <
262  B.getExpression()->getFragmentInfo()->OffsetInBits;
263 }
264 
265 }
266 
267 #endif
i
i
Definition: README.txt:29
llvm::DbgValueLocEntry::TIL
TargetIndexLocation TIL
Or a location from target specific location.
Definition: DebugLocEntry.h:63
LLVM_DUMP_METHOD
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:491
llvm::MachineLocation
Definition: MachineLocation.h:22
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::SmallVectorImpl::erase
iterator erase(const_iterator CI)
Definition: SmallVector.h:705
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
llvm::DbgValueLoc::dump
LLVM_DUMP_METHOD void dump() const
Definition: DebugLocEntry.h:160
llvm::DbgValueLocEntry::DbgValueLocEntry
DbgValueLocEntry(int64_t i)
Definition: DebugLocEntry.h:67
llvm::DbgValueLocEntry::dump
LLVM_DUMP_METHOD void dump() const
Definition: DebugLocEntry.h:92
DebugLocStream.h
llvm::Value::dump
void dump() const
Support for debugging, callable in GDB: V->dump()
Definition: AsmWriter.cpp:4759
llvm::DwarfCompileUnit
Definition: DwarfCompileUnit.h:47
llvm::TargetIndexLocation
This struct describes target specific location.
Definition: DebugLocEntry.h:24
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
llvm::DebugLocEntry::getBeginSym
const MCSymbol * getBeginSym() const
Definition: DebugLocEntry.h:203
llvm::DbgValueLocEntry::getTargetIndexLocation
TargetIndexLocation getTargetIndexLocation() const
Definition: DebugLocEntry.h:89
llvm::DbgValueLoc::getLocEntries
const ArrayRef< DbgValueLocEntry > getLocEntries() const
Definition: DebugLocEntry.h:154
llvm::DbgValueLocEntry::getInt
int64_t getInt() const
Definition: DebugLocEntry.h:85
llvm::DebugLocEntry::addValues
void addValues(ArrayRef< DbgValueLoc > Vals)
Definition: DebugLocEntry.h:206
llvm::sys::path::end
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:233
llvm::sys::path::begin
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:224
llvm::DbgValueLocEntry::operator==
friend bool operator==(const DbgValueLocEntry &, const DbgValueLocEntry &)
Compare two DbgValueLocEntries for equality.
Definition: DebugLocEntry.h:233
llvm::DIExpression
DWARF expression.
Definition: DebugInfoMetadata.h:2586
llvm::DbgValueLoc::isEntryVal
bool isEntryVal() const
Definition: DebugLocEntry.h:151
llvm::DbgValueLocEntry::DbgValueLocEntry
DbgValueLocEntry(const ConstantInt *CIP)
Definition: DebugLocEntry.h:71
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::DbgValueLoc::operator==
friend bool operator==(const DbgValueLoc &, const DbgValueLoc &)
Compare two DbgValueLocs for equality.
Definition: DebugLocEntry.h:253
llvm::ConstantInt
This is the shared class of boolean and integer constants.
Definition: Constants.h:79
llvm::all_of
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:1551
Constants.h
llvm::DbgValueLocEntry::isLocation
bool isLocation() const
Definition: DebugLocEntry.h:78
llvm::SmallVectorImpl::append
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:648
llvm::DbgValueLocEntry::Int
int64_t Int
Definition: DebugLocEntry.h:54
MCSymbol.h
llvm::DbgValueLocEntry::isConstantInt
bool isConstantInt() const
Definition: DebugLocEntry.h:84
llvm::BitTracker
Definition: BitTracker.h:35
false
Definition: StackSlotColoring.cpp:142
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::DebugLocEntry::DebugLocEntry
DebugLocEntry(const MCSymbol *Begin, const MCSymbol *End, ArrayRef< DbgValueLoc > Vals)
Create a location list entry for the range [Begin, End).
Definition: DebugLocEntry.h:184
llvm::DbgValueLoc::DbgValueLoc
DbgValueLoc(const DIExpression *Expr, ArrayRef< DbgValueLocEntry > Locs, bool IsVariadic)
Definition: DebugLocEntry.h:127
llvm::ConstantFP
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:257
llvm::AArch64CC::LE
@ LE
Definition: AArch64BaseInfo.h:268
llvm::TargetIndexLocation::TargetIndexLocation
TargetIndexLocation(unsigned Idx, int64_t Offset)
Definition: DebugLocEntry.h:29
llvm::DbgValueLoc::DbgValueLoc
DbgValueLoc(const DIExpression *Expr, ArrayRef< DbgValueLocEntry > Locs)
Definition: DebugLocEntry.h:117
llvm::DbgValueLocEntry::isConstantFP
bool isConstantFP() const
Definition: DebugLocEntry.h:83
llvm::DebugLocStream::ListBuilder
Builder for DebugLocStream lists.
Definition: DebugLocStream.h:156
llvm::DebugLocEntry
This struct describes location entries emitted in the .debug_loc section.
Definition: DebugLocEntry.h:171
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::DbgValueLocEntry::getLoc
MachineLocation getLoc() const
Definition: DebugLocEntry.h:88
llvm::MachineLocation::getReg
unsigned getReg() const
Definition: MachineLocation.h:47
llvm::DbgValueLocEntry::isTargetIndexLocation
bool isTargetIndexLocation() const
Definition: DebugLocEntry.h:79
llvm::TargetIndexLocation::Index
int Index
Definition: DebugLocEntry.h:25
llvm::operator<
bool operator<(int64_t V1, const APSInt &V2)
Definition: APSInt.h:338
DebugInfo.h
llvm::DbgValueLocEntry::isInt
bool isInt() const
Definition: DebugLocEntry.h:82
llvm::is_contained
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
Definition: STLExtras.h:1616
llvm::DbgValueLocEntry::CIP
const ConstantInt * CIP
Definition: DebugLocEntry.h:56
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::DbgValueLoc::operator<
friend bool operator<(const DbgValueLoc &, const DbgValueLoc &)
Compare two fragments based on their offset.
Definition: DebugLocEntry.h:259
llvm::DIExpression::isEntryValue
bool isEntryValue() const
Check if the expression consists of exactly one entry value operand.
Definition: DebugInfoMetadata.h:2904
llvm::operator==
bool operator==(uint64_t V1, const APInt &V2)
Definition: APInt.h:1974
llvm::DbgValueLoc::getExpression
const DIExpression * getExpression() const
Definition: DebugLocEntry.h:153
llvm::DIBasicType
Basic type, like 'int' or 'float'.
Definition: DebugInfoMetadata.h:769
llvm::Expression
Class representing an expression and its matching format.
Definition: FileCheckImpl.h:237
llvm::DbgValueLoc::isVariadic
bool isVariadic() const
Definition: DebugLocEntry.h:152
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::any_of
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:1558
MachineLocation.h
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
llvm::DebugLocEntry::MergeRanges
bool MergeRanges(const DebugLocEntry &Next)
Attempt to merge this DebugLocEntry with Next and return true if the merge was successful.
Definition: DebugLocEntry.h:194
llvm::DbgValueLoc::isFragment
bool isFragment() const
Definition: DebugLocEntry.h:150
llvm::DebugLocEntry::getEndSym
const MCSymbol * getEndSym() const
Definition: DebugLocEntry.h:204
llvm::MachineLocation::isIndirect
bool isIndirect() const
Definition: MachineLocation.h:45
llvm::DebugLocEntry::sortUniqueValues
void sortUniqueValues()
Definition: DebugLocEntry.h:216
llvm::DIExpression::isFragment
bool isFragment() const
Return whether this is a piece of an aggregate variable.
Definition: DebugInfoMetadata.h:2765
llvm::ArrayRef::begin
iterator begin() const
Definition: ArrayRef.h:153
llvm::DebugLocEntry::finalize
void finalize(const AsmPrinter &AP, DebugLocStream::ListBuilder &List, const DIBasicType *BT, DwarfCompileUnit &TheCU)
Lower this entry into a DWARF expression.
Definition: DwarfDebug.cpp:2636
llvm::DbgValueLocEntry
A single location or constant within a variable location description, with either a single entry (wit...
Definition: DebugLocEntry.h:40
llvm::sort
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1492
llvm::DbgValueLocEntry::DbgValueLocEntry
DbgValueLocEntry(TargetIndexLocation Loc)
Definition: DebugLocEntry.h:75
llvm::DIExpression::getElements
ArrayRef< uint64_t > getElements() const
Definition: DebugInfoMetadata.h:2610
llvm::DbgValueLoc::DbgValueLoc
DbgValueLoc(const DIExpression *Expr, DbgValueLocEntry Loc)
Definition: DebugLocEntry.h:144
llvm::AsmPrinter
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:82
llvm::unique
auto unique(Range &&R, Predicate P)
Definition: STLExtras.h:1704
llvm::DbgValueLocEntry::DbgValueLocEntry
DbgValueLocEntry(const ConstantFP *CFP)
Definition: DebugLocEntry.h:68
llvm::TargetIndexLocation::operator==
bool operator==(const TargetIndexLocation &Other) const
Definition: DebugLocEntry.h:32
llvm::DbgValueLocEntry::getConstantFP
const ConstantFP * getConstantFP() const
Definition: DebugLocEntry.h:86
llvm::DbgValueLoc
The location of a single variable, composed of an expression and 0 or more DbgValueLocEntries.
Definition: DebugLocEntry.h:108
isValid
static bool isValid(const char C)
Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
Definition: RustDemangle.cpp:216
llvm::DbgValueLocEntry::CFP
const ConstantFP * CFP
Definition: DebugLocEntry.h:55
llvm::DbgValueLocEntry::getConstantInt
const ConstantInt * getConstantInt() const
Definition: DebugLocEntry.h:87
llvm::TargetIndexLocation::TargetIndexLocation
TargetIndexLocation()=default
llvm::DbgValueLocEntry::DbgValueLocEntry
DbgValueLocEntry(MachineLocation Loc)
Definition: DebugLocEntry.h:74
llvm::TargetIndexLocation::Offset
int Offset
Definition: DebugLocEntry.h:26
true
basic Basic Alias true
Definition: BasicAliasAnalysis.cpp:1815
llvm::DbgValueLocEntry::Loc
MachineLocation Loc
Or a location in the machine frame.
Definition: DebugLocEntry.h:61
Debug.h
llvm::ArrayRef::end
iterator end() const
Definition: ArrayRef.h:154
llvm::DebugLocEntry::getValues
ArrayRef< DbgValueLoc > getValues() const
Definition: DebugLocEntry.h:205
Other
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1172
llvm::DIExpression::isValid
bool isValid() const
Definition: DebugInfoMetadata.cpp:1096