1 //===-- llvm/CodeGen/DebugLocEntry.h - Entry in debug_loc list -*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCENTRY_H
11 #define LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCENTRY_H
13 #include "DebugLocStream.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/DebugInfo.h"
17 #include "llvm/MC/MCSymbol.h"
18 #include "llvm/MC/MachineLocation.h"
23 /// \brief This struct describes location entries emitted in the .debug_loc
26 /// Begin and end symbols for the address range that this location is valid.
27 const MCSymbol *Begin;
31 /// \brief A single location or constant.
33 Value(const DIExpression *Expr, int64_t i)
34 : Expression(Expr), EntryKind(E_Integer) {
37 Value(const DIExpression *Expr, const ConstantFP *CFP)
38 : Expression(Expr), EntryKind(E_ConstantFP) {
41 Value(const DIExpression *Expr, const ConstantInt *CIP)
42 : Expression(Expr), EntryKind(E_ConstantInt) {
45 Value(const DIExpression *Expr, MachineLocation Loc)
46 : Expression(Expr), EntryKind(E_Location), Loc(Loc) {
47 assert(cast<DIExpression>(Expr)->isValid());
50 /// Any complex address location expression for this Value.
51 const DIExpression *Expression;
53 /// Type of entry that this represents.
54 enum EntryType { E_Location, E_Integer, E_ConstantFP, E_ConstantInt };
55 enum EntryType EntryKind;
57 /// Either a constant,
60 const ConstantFP *CFP;
61 const ConstantInt *CIP;
64 // Or a location in the machine frame.
67 bool isLocation() const { return EntryKind == E_Location; }
68 bool isInt() const { return EntryKind == E_Integer; }
69 bool isConstantFP() const { return EntryKind == E_ConstantFP; }
70 bool isConstantInt() const { return EntryKind == E_ConstantInt; }
71 int64_t getInt() const { return Constant.Int; }
72 const ConstantFP *getConstantFP() const { return Constant.CFP; }
73 const ConstantInt *getConstantInt() const { return Constant.CIP; }
74 MachineLocation getLoc() const { return Loc; }
75 bool isBitPiece() const { return getExpression()->isBitPiece(); }
76 const DIExpression *getExpression() const { return Expression; }
77 friend bool operator==(const Value &, const Value &);
78 friend bool operator<(const Value &, const Value &);
82 /// A nonempty list of locations/constants belonging to this entry,
84 SmallVector<Value, 1> Values;
87 DebugLocEntry(const MCSymbol *B, const MCSymbol *E, Value Val)
89 Values.push_back(std::move(Val));
92 /// \brief If this and Next are describing different pieces of the same
93 /// variable, merge them by appending Next's values to the current
95 /// Return true if the merge was successful.
96 bool MergeValues(const DebugLocEntry &Next) {
97 if (Begin == Next.Begin) {
98 auto *Expr = cast_or_null<DIExpression>(Values[0].Expression);
99 auto *NextExpr = cast_or_null<DIExpression>(Next.Values[0].Expression);
100 if (Expr->isBitPiece() && NextExpr->isBitPiece()) {
101 addValues(Next.Values);
109 /// \brief Attempt to merge this DebugLocEntry with Next and return
110 /// true if the merge was successful. Entries can be merged if they
111 /// share the same Loc/Constant and if Next immediately follows this
113 bool MergeRanges(const DebugLocEntry &Next) {
114 // If this and Next are describing the same variable, merge them.
115 if ((End == Next.Begin && Values == Next.Values)) {
122 const MCSymbol *getBeginSym() const { return Begin; }
123 const MCSymbol *getEndSym() const { return End; }
124 ArrayRef<Value> getValues() const { return Values; }
125 void addValues(ArrayRef<DebugLocEntry::Value> Vals) {
126 Values.append(Vals.begin(), Vals.end());
128 assert(std::all_of(Values.begin(), Values.end(), [](DebugLocEntry::Value V){
129 return V.isBitPiece();
130 }) && "value must be a piece");
133 // \brief Sort the pieces by offset.
134 // Remove any duplicate entries by dropping all but the first.
135 void sortUniqueValues() {
136 std::sort(Values.begin(), Values.end());
139 Values.begin(), Values.end(), [](const Value &A, const Value &B) {
140 return A.getExpression() == B.getExpression();
145 /// \brief Lower this entry into a DWARF expression.
146 void finalize(const AsmPrinter &AP, DebugLocStream::ListBuilder &List,
147 const DIBasicType *BT);
150 /// \brief Compare two Values for equality.
151 inline bool operator==(const DebugLocEntry::Value &A,
152 const DebugLocEntry::Value &B) {
153 if (A.EntryKind != B.EntryKind)
156 if (A.Expression != B.Expression)
159 switch (A.EntryKind) {
160 case DebugLocEntry::Value::E_Location:
161 return A.Loc == B.Loc;
162 case DebugLocEntry::Value::E_Integer:
163 return A.Constant.Int == B.Constant.Int;
164 case DebugLocEntry::Value::E_ConstantFP:
165 return A.Constant.CFP == B.Constant.CFP;
166 case DebugLocEntry::Value::E_ConstantInt:
167 return A.Constant.CIP == B.Constant.CIP;
169 llvm_unreachable("unhandled EntryKind");
172 /// \brief Compare two pieces based on their offset.
173 inline bool operator<(const DebugLocEntry::Value &A,
174 const DebugLocEntry::Value &B) {
175 return A.getExpression()->getBitPieceOffset() <
176 B.getExpression()->getBitPieceOffset();