LLVM  13.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.
38 class DbgValueLoc {
39  /// Any complex address location expression for this DbgValueLoc.
40  const DIExpression *Expression;
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  DbgValueLoc(const DIExpression *Expr, int64_t i)
68  : Expression(Expr), EntryKind(E_Integer) {
69  Constant.Int = i;
70  }
71  DbgValueLoc(const DIExpression *Expr, const ConstantFP *CFP)
72  : Expression(Expr), EntryKind(E_ConstantFP) {
73  Constant.CFP = CFP;
74  }
75  DbgValueLoc(const DIExpression *Expr, const ConstantInt *CIP)
76  : Expression(Expr), EntryKind(E_ConstantInt) {
77  Constant.CIP = CIP;
78  }
80  : Expression(Expr), EntryKind(E_Location), Loc(Loc) {
81  assert(cast<DIExpression>(Expr)->isValid());
82  }
84  : Expression(Expr), EntryKind(E_TargetIndexLocation), TIL(Loc) {}
85 
86  bool isLocation() const { return EntryKind == E_Location; }
87  bool isTargetIndexLocation() const {
88  return EntryKind == E_TargetIndexLocation;
89  }
90  bool isInt() const { return EntryKind == E_Integer; }
91  bool isConstantFP() const { return EntryKind == E_ConstantFP; }
92  bool isConstantInt() const { return EntryKind == E_ConstantInt; }
93  int64_t getInt() const { return Constant.Int; }
94  const ConstantFP *getConstantFP() const { return Constant.CFP; }
95  const ConstantInt *getConstantInt() const { return Constant.CIP; }
96  MachineLocation getLoc() const { return Loc; }
98  bool isFragment() const { return getExpression()->isFragment(); }
99  bool isEntryVal() const { return getExpression()->isEntryValue(); }
100  const DIExpression *getExpression() const { return Expression; }
101  friend bool operator==(const DbgValueLoc &, const DbgValueLoc &);
102  friend bool operator<(const DbgValueLoc &, const DbgValueLoc &);
103 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
104  LLVM_DUMP_METHOD void dump() const {
105  if (isLocation()) {
106  llvm::dbgs() << "Loc = { reg=" << Loc.getReg() << " ";
107  if (Loc.isIndirect())
108  llvm::dbgs() << "+0";
109  llvm::dbgs() << "} ";
110  } else if (isConstantInt())
111  Constant.CIP->dump();
112  else if (isConstantFP())
113  Constant.CFP->dump();
114  if (Expression)
115  Expression->dump();
116  }
117 #endif
118 };
119 
120 /// This struct describes location entries emitted in the .debug_loc
121 /// section.
123  /// Begin and end symbols for the address range that this location is valid.
124  const MCSymbol *Begin;
125  const MCSymbol *End;
126 
127  /// A nonempty list of locations/constants belonging to this entry,
128  /// sorted by offset.
130 
131 public:
132  /// Create a location list entry for the range [\p Begin, \p End).
133  ///
134  /// \param Vals One or more values describing (parts of) the variable.
135  DebugLocEntry(const MCSymbol *Begin, const MCSymbol *End,
137  : Begin(Begin), End(End) {
138  addValues(Vals);
139  }
140 
141  /// Attempt to merge this DebugLocEntry with Next and return
142  /// true if the merge was successful. Entries can be merged if they
143  /// share the same Loc/Constant and if Next immediately follows this
144  /// Entry.
145  bool MergeRanges(const DebugLocEntry &Next) {
146  // If this and Next are describing the same variable, merge them.
147  if ((End == Next.Begin && Values == Next.Values)) {
148  End = Next.End;
149  return true;
150  }
151  return false;
152  }
153 
154  const MCSymbol *getBeginSym() const { return Begin; }
155  const MCSymbol *getEndSym() const { return End; }
156  ArrayRef<DbgValueLoc> getValues() const { return Values; }
158  Values.append(Vals.begin(), Vals.end());
160  assert((Values.size() == 1 || all_of(Values, [](DbgValueLoc V) {
161  return V.isFragment();
162  })) && "must either have a single value or multiple pieces");
163  }
164 
165  // Sort the pieces by offset.
166  // Remove any duplicate entries by dropping all but the first.
168  llvm::sort(Values);
169  Values.erase(std::unique(Values.begin(), Values.end(),
170  [](const DbgValueLoc &A, const DbgValueLoc &B) {
171  return A.getExpression() == B.getExpression();
172  }),
173  Values.end());
174  }
175 
176  /// Lower this entry into a DWARF expression.
177  void finalize(const AsmPrinter &AP,
179  const DIBasicType *BT,
180  DwarfCompileUnit &TheCU);
181 };
182 
183 /// Compare two DbgValueLocs for equality.
184 inline bool operator==(const DbgValueLoc &A,
185  const DbgValueLoc &B) {
186  if (A.EntryKind != B.EntryKind)
187  return false;
188 
189  if (A.Expression != B.Expression)
190  return false;
191 
192  switch (A.EntryKind) {
193  case DbgValueLoc::E_Location:
194  return A.Loc == B.Loc;
195  case DbgValueLoc::E_TargetIndexLocation:
196  return A.TIL == B.TIL;
197  case DbgValueLoc::E_Integer:
198  return A.Constant.Int == B.Constant.Int;
199  case DbgValueLoc::E_ConstantFP:
200  return A.Constant.CFP == B.Constant.CFP;
201  case DbgValueLoc::E_ConstantInt:
202  return A.Constant.CIP == B.Constant.CIP;
203  }
204  llvm_unreachable("unhandled EntryKind");
205 }
206 
207 /// Compare two fragments based on their offset.
208 inline bool operator<(const DbgValueLoc &A,
209  const DbgValueLoc &B) {
210  return A.getExpression()->getFragmentInfo()->OffsetInBits <
211  B.getExpression()->getFragmentInfo()->OffsetInBits;
212 }
213 
214 }
215 
216 #endif
i
i
Definition: README.txt:29
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:498
llvm::MachineLocation
Definition: MachineLocation.h:22
llvm
This class represents lattice values for constants.
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:104
DebugLocStream.h
llvm::DbgValueLoc::DbgValueLoc
DbgValueLoc(const DIExpression *Expr, int64_t i)
Definition: DebugLocEntry.h:67
llvm::Value::dump
void dump() const
Support for debugging, callable in GDB: V->dump()
Definition: AsmWriter.cpp:4760
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:154
llvm::DbgValueLoc::getLoc
MachineLocation getLoc() const
Definition: DebugLocEntry.h:96
llvm::DbgValueLoc::getTargetIndexLocation
TargetIndexLocation getTargetIndexLocation() const
Definition: DebugLocEntry.h:97
llvm::DebugLocEntry::addValues
void addValues(ArrayRef< DbgValueLoc > Vals)
Definition: DebugLocEntry.h:157
llvm::DIExpression
DWARF expression.
Definition: DebugInfoMetadata.h:2553
llvm::DbgValueLoc::isEntryVal
bool isEntryVal() const
Definition: DebugLocEntry.h:99
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
llvm::DbgValueLoc::operator==
friend bool operator==(const DbgValueLoc &, const DbgValueLoc &)
Compare two DbgValueLocs for equality.
Definition: DebugLocEntry.h:184
llvm::DbgValueLoc::getConstantFP
const ConstantFP * getConstantFP() const
Definition: DebugLocEntry.h:94
llvm::ConstantInt
This is the shared class of boolean and integer constants.
Definition: Constants.h:77
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:1498
Constants.h
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
MCSymbol.h
llvm::BitTracker
Definition: BitTracker.h:35
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::DbgValueLoc::isTargetIndexLocation
bool isTargetIndexLocation() const
Definition: DebugLocEntry.h:87
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:135
llvm::ConstantFP
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:273
llvm::DbgValueLoc::DbgValueLoc
DbgValueLoc(const DIExpression *Expr, const ConstantInt *CIP)
Definition: DebugLocEntry.h:75
llvm::DbgValueLoc::TIL
TargetIndexLocation TIL
Or a location from target specific location.
Definition: DebugLocEntry.h:63
llvm::TargetIndexLocation::TargetIndexLocation
TargetIndexLocation(unsigned Idx, int64_t Offset)
Definition: DebugLocEntry.h:29
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:122
llvm::DbgValueLoc::Loc
MachineLocation Loc
Or a location in the machine frame.
Definition: DebugLocEntry.h:61
llvm::DbgValueLoc::CFP
const ConstantFP * CFP
Definition: DebugLocEntry.h:55
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::DbgValueLoc::isConstantInt
bool isConstantInt() const
Definition: DebugLocEntry.h:92
llvm::MachineLocation::getReg
unsigned getReg() const
Definition: MachineLocation.h:47
llvm::DbgValueLoc::DbgValueLoc
DbgValueLoc(const DIExpression *Expr, TargetIndexLocation Loc)
Definition: DebugLocEntry.h:83
llvm::DbgValueLoc::isConstantFP
bool isConstantFP() const
Definition: DebugLocEntry.h:91
llvm::TargetIndexLocation::Index
int Index
Definition: DebugLocEntry.h:25
llvm::operator<
bool operator<(int64_t V1, const APSInt &V2)
Definition: APSInt.h:343
DebugInfo.h
llvm::DbgValueLoc::DbgValueLoc
DbgValueLoc(const DIExpression *Expr, const ConstantFP *CFP)
Definition: DebugLocEntry.h:71
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::DbgValueLoc::getConstantInt
const ConstantInt * getConstantInt() const
Definition: DebugLocEntry.h:95
llvm::DbgValueLoc::operator<
friend bool operator<(const DbgValueLoc &, const DbgValueLoc &)
Compare two fragments based on their offset.
Definition: DebugLocEntry.h:208
llvm::DIExpression::isEntryValue
bool isEntryValue() const
Check if the expression consists of exactly one entry value operand.
Definition: DebugInfoMetadata.h:2844
llvm::operator==
bool operator==(uint64_t V1, const APInt &V2)
Definition: APInt.h:2035
llvm::DbgValueLoc::Int
int64_t Int
Definition: DebugLocEntry.h:54
llvm::DbgValueLoc::getInt
int64_t getInt() const
Definition: DebugLocEntry.h:93
llvm::DbgValueLoc::getExpression
const DIExpression * getExpression() const
Definition: DebugLocEntry.h:100
llvm::DIBasicType
Basic type, like 'int' or 'float'.
Definition: DebugInfoMetadata.h:761
llvm::DbgValueLoc::isLocation
bool isLocation() const
Definition: DebugLocEntry.h:86
llvm::Expression
Class representing an expression and its matching format.
Definition: FileCheckImpl.h:234
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
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:145
llvm::DbgValueLoc::isFragment
bool isFragment() const
Definition: DebugLocEntry.h:98
llvm::DebugLocEntry::getEndSym
const MCSymbol * getEndSym() const
Definition: DebugLocEntry.h:155
llvm::MachineLocation::isIndirect
bool isIndirect() const
Definition: MachineLocation.h:45
llvm::DebugLocEntry::sortUniqueValues
void sortUniqueValues()
Definition: DebugLocEntry.h:167
llvm::DIExpression::isFragment
bool isFragment() const
Return whether this is a piece of an aggregate variable.
Definition: DebugInfoMetadata.h:2718
llvm::ArrayRef::begin
iterator begin() const
Definition: ArrayRef.h:151
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:2536
llvm::sort
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1439
llvm::AsmPrinter
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:82
llvm::TargetIndexLocation::operator==
bool operator==(const TargetIndexLocation &Other) const
Definition: DebugLocEntry.h:32
llvm::DbgValueLoc
A single location or constant.
Definition: DebugLocEntry.h:38
llvm::DbgValueLoc::isInt
bool isInt() const
Definition: DebugLocEntry.h:90
llvm::TargetIndexLocation::TargetIndexLocation
TargetIndexLocation()=default
llvm::TargetIndexLocation::Offset
int Offset
Definition: DebugLocEntry.h:26
llvm::DbgValueLoc::CIP
const ConstantInt * CIP
Definition: DebugLocEntry.h:56
llvm::DbgValueLoc::DbgValueLoc
DbgValueLoc(const DIExpression *Expr, MachineLocation Loc)
Definition: DebugLocEntry.h:79
Debug.h
llvm::ArrayRef::end
iterator end() const
Definition: ArrayRef.h:152
llvm::DebugLocEntry::getValues
ArrayRef< DbgValueLoc > getValues() const
Definition: DebugLocEntry.h:156
Other
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1131