LLVM  10.0.0svn
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 /// A single location or constant.
24 class DbgValueLoc {
25  /// Any complex address location expression for this DbgValueLoc.
26  const DIExpression *Expression;
27 
28  /// Type of entry that this represents.
29  enum EntryType { E_Location, E_Integer, E_ConstantFP, E_ConstantInt };
30  enum EntryType EntryKind;
31 
32  /// Either a constant,
33  union {
34  int64_t Int;
35  const ConstantFP *CFP;
36  const ConstantInt *CIP;
37  } Constant;
38 
39  /// Or a location in the machine frame.
40  MachineLocation Loc;
41 
42 public:
43  DbgValueLoc(const DIExpression *Expr, int64_t i)
44  : Expression(Expr), EntryKind(E_Integer) {
45  Constant.Int = i;
46  }
47  DbgValueLoc(const DIExpression *Expr, const ConstantFP *CFP)
48  : Expression(Expr), EntryKind(E_ConstantFP) {
49  Constant.CFP = CFP;
50  }
51  DbgValueLoc(const DIExpression *Expr, const ConstantInt *CIP)
52  : Expression(Expr), EntryKind(E_ConstantInt) {
53  Constant.CIP = CIP;
54  }
56  : Expression(Expr), EntryKind(E_Location), Loc(Loc) {
57  assert(cast<DIExpression>(Expr)->isValid());
58  }
59 
60  bool isLocation() const { return EntryKind == E_Location; }
61  bool isInt() const { return EntryKind == E_Integer; }
62  bool isConstantFP() const { return EntryKind == E_ConstantFP; }
63  bool isConstantInt() const { return EntryKind == E_ConstantInt; }
64  int64_t getInt() const { return Constant.Int; }
65  const ConstantFP *getConstantFP() const { return Constant.CFP; }
66  const ConstantInt *getConstantInt() const { return Constant.CIP; }
67  MachineLocation getLoc() const { return Loc; }
68  bool isFragment() const { return getExpression()->isFragment(); }
69  bool isEntryVal() const { return getExpression()->isEntryValue(); }
70  const DIExpression *getExpression() const { return Expression; }
71  friend bool operator==(const DbgValueLoc &, const DbgValueLoc &);
72  friend bool operator<(const DbgValueLoc &, const DbgValueLoc &);
73 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
74  LLVM_DUMP_METHOD void dump() const {
75  if (isLocation()) {
76  llvm::dbgs() << "Loc = { reg=" << Loc.getReg() << " ";
77  if (Loc.isIndirect())
78  llvm::dbgs() << "+0";
79  llvm::dbgs() << "} ";
80  } else if (isConstantInt())
81  Constant.CIP->dump();
82  else if (isConstantFP())
83  Constant.CFP->dump();
84  if (Expression)
85  Expression->dump();
86  }
87 #endif
88 };
89 
90 /// This struct describes location entries emitted in the .debug_loc
91 /// section.
93  /// Begin and end symbols for the address range that this location is valid.
94  const MCSymbol *Begin;
95  const MCSymbol *End;
96 
97  /// A nonempty list of locations/constants belonging to this entry,
98  /// sorted by offset.
100 
101 public:
102  /// Create a location list entry for the range [\p Begin, \p End).
103  ///
104  /// \param Vals One or more values describing (parts of) the variable.
105  DebugLocEntry(const MCSymbol *Begin, const MCSymbol *End,
107  : Begin(Begin), End(End) {
108  addValues(Vals);
109  }
110 
111  /// Attempt to merge this DebugLocEntry with Next and return
112  /// true if the merge was successful. Entries can be merged if they
113  /// share the same Loc/Constant and if Next immediately follows this
114  /// Entry.
115  bool MergeRanges(const DebugLocEntry &Next) {
116  // If this and Next are describing the same variable, merge them.
117  if ((End == Next.Begin && Values == Next.Values)) {
118  End = Next.End;
119  return true;
120  }
121  return false;
122  }
123 
124  const MCSymbol *getBeginSym() const { return Begin; }
125  const MCSymbol *getEndSym() const { return End; }
126  ArrayRef<DbgValueLoc> getValues() const { return Values; }
128  Values.append(Vals.begin(), Vals.end());
129  sortUniqueValues();
130  assert((Values.size() == 1 || all_of(Values, [](DbgValueLoc V) {
131  return V.isFragment();
132  })) && "must either have a single value or multiple pieces");
133  }
134 
135  // Sort the pieces by offset.
136  // Remove any duplicate entries by dropping all but the first.
138  llvm::sort(Values);
139  Values.erase(std::unique(Values.begin(), Values.end(),
140  [](const DbgValueLoc &A, const DbgValueLoc &B) {
141  return A.getExpression() == B.getExpression();
142  }),
143  Values.end());
144  }
145 
146  /// Lower this entry into a DWARF expression.
147  void finalize(const AsmPrinter &AP,
149  const DIBasicType *BT,
150  DwarfCompileUnit &TheCU);
151 };
152 
153 /// Compare two DbgValueLocs for equality.
154 inline bool operator==(const DbgValueLoc &A,
155  const DbgValueLoc &B) {
156  if (A.EntryKind != B.EntryKind)
157  return false;
158 
159  if (A.Expression != B.Expression)
160  return false;
161 
162  switch (A.EntryKind) {
163  case DbgValueLoc::E_Location:
164  return A.Loc == B.Loc;
165  case DbgValueLoc::E_Integer:
166  return A.Constant.Int == B.Constant.Int;
167  case DbgValueLoc::E_ConstantFP:
168  return A.Constant.CFP == B.Constant.CFP;
169  case DbgValueLoc::E_ConstantInt:
170  return A.Constant.CIP == B.Constant.CIP;
171  }
172  llvm_unreachable("unhandled EntryKind");
173 }
174 
175 /// Compare two fragments based on their offset.
176 inline bool operator<(const DbgValueLoc &A,
177  const DbgValueLoc &B) {
178  return A.getExpression()->getFragmentInfo()->OffsetInBits <
179  B.getExpression()->getFragmentInfo()->OffsetInBits;
180 }
181 
182 }
183 
184 #endif
const ConstantInt * CIP
Definition: DebugLocEntry.h:36
bool MergeRanges(const DebugLocEntry &Next)
Attempt to merge this DebugLocEntry with Next and return true if the merge was successful.
Builder for DebugLocStream lists.
bool isEntryValue() const
Check if the expression consists of exactly one entry value operand.
int64_t getInt() const
Definition: DebugLocEntry.h:64
void addValues(ArrayRef< DbgValueLoc > Vals)
This struct describes location entries emitted in the .debug_loc section.
Definition: DebugLocEntry.h:92
This class represents lattice values for constants.
Definition: AllocatorList.h:23
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:484
const ConstantFP * getConstantFP() const
Definition: DebugLocEntry.h:65
friend bool operator==(const DbgValueLoc &, const DbgValueLoc &)
Compare two DbgValueLocs for equality.
arc branch finalize
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
iterator begin() const
Definition: ArrayRef.h:136
bool isInt() const
Definition: DebugLocEntry.h:61
bool isConstantFP() const
Definition: DebugLocEntry.h:62
unsigned getReg() const
DbgValueLoc(const DIExpression *Expr, int64_t i)
Definition: DebugLocEntry.h:43
DebugLocEntry(const MCSymbol *Begin, const MCSymbol *End, ArrayRef< DbgValueLoc > Vals)
Create a location list entry for the range [Begin, End).
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:1165
const ConstantInt * getConstantInt() const
Definition: DebugLocEntry.h:66
bool isFragment() const
Definition: DebugLocEntry.h:68
void dump() const
Support for debugging, callable in GDB: V->dump()
Definition: AsmWriter.cpp:4429
const MCSymbol * getBeginSym() const
DbgValueLoc(const DIExpression *Expr, const ConstantFP *CFP)
Definition: DebugLocEntry.h:47
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
static Optional< FragmentInfo > getFragmentInfo(expr_op_iterator Start, expr_op_iterator End)
Retrieve the details of this fragment expression.
DbgValueLoc(const DIExpression *Expr, const ConstantInt *CIP)
Definition: DebugLocEntry.h:51
const DIExpression * getExpression() const
Definition: DebugLocEntry.h:70
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
bool isEntryVal() const
Definition: DebugLocEntry.h:69
This is an important base class in LLVM.
Definition: Constant.h:41
This file contains the declarations for the subclasses of Constant, which represent the different fla...
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:263
const ConstantFP * CFP
Definition: DebugLocEntry.h:35
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:78
LLVM_DUMP_METHOD void dump() const
Definition: DebugLocEntry.h:74
bool isLocation() const
Definition: DebugLocEntry.h:60
iterator erase(const_iterator CI)
Definition: SmallVector.h:434
size_t size() const
Definition: SmallVector.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1095
ArrayRef< DbgValueLoc > getValues() const
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
friend bool operator<(const DbgValueLoc &, const DbgValueLoc &)
Compare two fragments based on their offset.
iterator end() const
Definition: ArrayRef.h:137
const MCSymbol * getEndSym() const
DWARF expression.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:387
const NodeList & List
Definition: RDFGraph.cpp:201
void dump() const
User-friendly dump.
Definition: AsmWriter.cpp:4451
MachineLocation getLoc() const
Definition: DebugLocEntry.h:67
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool isConstantInt() const
Definition: DebugLocEntry.h:63
bool isFragment() const
Return whether this is a piece of an aggregate variable.
DbgValueLoc(const DIExpression *Expr, MachineLocation Loc)
Definition: DebugLocEntry.h:55
A single location or constant.
Definition: DebugLocEntry.h:24
Basic type, like &#39;int&#39; or &#39;float&#39;.