LLVM  3.7.0
DebugLocEntry.h
Go to the documentation of this file.
1 //===-- llvm/CodeGen/DebugLocEntry.h - Entry in debug_loc list -*- C++ -*--===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCENTRY_H
11 #define LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCENTRY_H
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/IR/Constants.h"
14 #include "llvm/IR/DebugInfo.h"
15 #include "llvm/MC/MCSymbol.h"
17 
18 namespace llvm {
19 class AsmPrinter;
20 class DebugLocStream;
21 
22 /// \brief This struct describes location entries emitted in the .debug_loc
23 /// section.
25  /// Begin and end symbols for the address range that this location is valid.
26  const MCSymbol *Begin;
27  const MCSymbol *End;
28 
29 public:
30  /// \brief A single location or constant.
31  struct Value {
32  Value(const DIExpression *Expr, int64_t i)
33  : Expression(Expr), EntryKind(E_Integer) {
34  Constant.Int = i;
35  }
36  Value(const DIExpression *Expr, const ConstantFP *CFP)
38  Constant.CFP = CFP;
39  }
40  Value(const DIExpression *Expr, const ConstantInt *CIP)
42  Constant.CIP = CIP;
43  }
45  : Expression(Expr), EntryKind(E_Location), Loc(Loc) {
46  assert(cast<DIExpression>(Expr)->isValid());
47  }
48 
49  /// Any complex address location expression for this Value.
51 
52  /// Type of entry that this represents.
55 
56  /// Either a constant,
57  union {
58  int64_t Int;
59  const ConstantFP *CFP;
60  const ConstantInt *CIP;
61  } Constant;
62 
63  // Or a location in the machine frame.
65 
66  bool isLocation() const { return EntryKind == E_Location; }
67  bool isInt() const { return EntryKind == E_Integer; }
68  bool isConstantFP() const { return EntryKind == E_ConstantFP; }
69  bool isConstantInt() const { return EntryKind == E_ConstantInt; }
70  int64_t getInt() const { return Constant.Int; }
71  const ConstantFP *getConstantFP() const { return Constant.CFP; }
72  const ConstantInt *getConstantInt() const { return Constant.CIP; }
73  MachineLocation getLoc() const { return Loc; }
74  bool isBitPiece() const { return getExpression()->isBitPiece(); }
75  const DIExpression *getExpression() const { return Expression; }
76  friend bool operator==(const Value &, const Value &);
77  friend bool operator<(const Value &, const Value &);
78  };
79 
80 private:
81  /// A nonempty list of locations/constants belonging to this entry,
82  /// sorted by offset.
83  SmallVector<Value, 1> Values;
84 
85 public:
86  DebugLocEntry(const MCSymbol *B, const MCSymbol *E, Value Val)
87  : Begin(B), End(E) {
88  Values.push_back(std::move(Val));
89  }
90 
91  /// \brief If this and Next are describing different pieces of the same
92  /// variable, merge them by appending Next's values to the current
93  /// list of values.
94  /// Return true if the merge was successful.
95  bool MergeValues(const DebugLocEntry &Next) {
96  if (Begin == Next.Begin) {
97  auto *Expr = cast_or_null<DIExpression>(Values[0].Expression);
98  auto *NextExpr = cast_or_null<DIExpression>(Next.Values[0].Expression);
99  if (Expr->isBitPiece() && NextExpr->isBitPiece()) {
100  addValues(Next.Values);
101  End = Next.End;
102  return true;
103  }
104  }
105  return false;
106  }
107 
108  /// \brief Attempt to merge this DebugLocEntry with Next and return
109  /// true if the merge was successful. Entries can be merged if they
110  /// share the same Loc/Constant and if Next immediately follows this
111  /// Entry.
112  bool MergeRanges(const DebugLocEntry &Next) {
113  // If this and Next are describing the same variable, merge them.
114  if ((End == Next.Begin && Values == Next.Values)) {
115  End = Next.End;
116  return true;
117  }
118  return false;
119  }
120 
121  const MCSymbol *getBeginSym() const { return Begin; }
122  const MCSymbol *getEndSym() const { return End; }
123  ArrayRef<Value> getValues() const { return Values; }
125  Values.append(Vals.begin(), Vals.end());
127  assert(std::all_of(Values.begin(), Values.end(), [](DebugLocEntry::Value V){
128  return V.isBitPiece();
129  }) && "value must be a piece");
130  }
131 
132  // \brief Sort the pieces by offset.
133  // Remove any duplicate entries by dropping all but the first.
135  std::sort(Values.begin(), Values.end());
136  Values.erase(
137  std::unique(
138  Values.begin(), Values.end(), [](const Value &A, const Value &B) {
139  return A.getExpression() == B.getExpression();
140  }),
141  Values.end());
142  }
143 
144  /// \brief Lower this entry into a DWARF expression.
146  const DIBasicType *BT);
147 };
148 
149 /// \brief Compare two Values for equality.
150 inline bool operator==(const DebugLocEntry::Value &A,
151  const DebugLocEntry::Value &B) {
152  if (A.EntryKind != B.EntryKind)
153  return false;
154 
155  if (A.Expression != B.Expression)
156  return false;
157 
158  switch (A.EntryKind) {
160  return A.Loc == B.Loc;
162  return A.Constant.Int == B.Constant.Int;
164  return A.Constant.CFP == B.Constant.CFP;
166  return A.Constant.CIP == B.Constant.CIP;
167  }
168  llvm_unreachable("unhandled EntryKind");
169 }
170 
171 /// \brief Compare two pieces based on their offset.
172 inline bool operator<(const DebugLocEntry::Value &A,
173  const DebugLocEntry::Value &B) {
174  return A.getExpression()->getBitPieceOffset() <
176 }
177 
178 }
179 
180 #endif
MachineLocation getLoc() const
Definition: DebugLocEntry.h:73
bool MergeRanges(const DebugLocEntry &Next)
Attempt to merge this DebugLocEntry with Next and return true if the merge was successful.
const ConstantInt * getConstantInt() const
Definition: DebugLocEntry.h:72
Builder for DebugLocStream lists.
friend bool operator==(const Value &, const Value &)
Compare two Values for equality.
This struct describes location entries emitted in the .debug_loc section.
Definition: DebugLocEntry.h:24
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
const ConstantFP * getConstantFP() const
Definition: DebugLocEntry.h:71
iterator end() const
Definition: ArrayRef.h:123
Value(const DIExpression *Expr, int64_t i)
Definition: DebugLocEntry.h:32
const DIExpression * Expression
Any complex address location expression for this Value.
Definition: DebugLocEntry.h:50
Value(const DIExpression *Expr, const ConstantFP *CFP)
Definition: DebugLocEntry.h:36
EntryType
Type of entry that this represents.
Definition: DebugLocEntry.h:53
ArrayRef< Value > getValues() const
const ConstantFP * CFP
Definition: DebugLocEntry.h:59
const DIExpression * getExpression() const
Definition: DebugLocEntry.h:75
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
DebugLocEntry(const MCSymbol *B, const MCSymbol *E, Value Val)
Definition: DebugLocEntry.h:86
Value(const DIExpression *Expr, const ConstantInt *CIP)
Definition: DebugLocEntry.h:40
bool MergeValues(const DebugLocEntry &Next)
If this and Next are describing different pieces of the same variable, merge them by appending Next's...
Definition: DebugLocEntry.h:95
const MCSymbol * getBeginSym() const
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:31
Value(const DIExpression *Expr, MachineLocation Loc)
Definition: DebugLocEntry.h:44
const MCSymbol * getEndSym() const
friend bool operator<(const Value &, const Value &)
Compare two pieces based on their offset.
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:233
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:66
iterator begin() const
Definition: ArrayRef.h:122
const ConstantInt * CIP
Definition: DebugLocEntry.h:60
This is the shared class of boolean and integer constants.
Definition: Constants.h:47
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
DWARF expression.
A single location or constant.
Definition: DebugLocEntry.h:31
void finalize(const AsmPrinter &AP, DebugLocStream::ListBuilder &List, const DIBasicType *BT)
Lower this entry into a DWARF expression.
bool isBitPiece() const
Return whether this is a piece of an aggregate variable.
bool all_of(R &&Range, UnaryPredicate &&P)
Provide wrappers to std::all_of which take ranges instead of having to pass being/end explicitly...
Definition: STLExtras.h:334
bool operator<(int64_t V1, const APSInt &V2)
Definition: APSInt.h:332
uint64_t getBitPieceOffset() const
Return the offset of this piece in bits.
bool operator==(uint64_t V1, const APInt &V2)
Definition: APInt.h:1734
void addValues(ArrayRef< DebugLocEntry::Value > Vals)
int64_t getInt() const
Definition: DebugLocEntry.h:70
union llvm::DebugLocEntry::Value::@199 Constant
Either a constant,.
Basic type, like 'int' or 'float'.