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
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/IR/Constants.h"
14 #include "llvm/IR/DebugInfo.h"
15 #include "llvm/MC/MCSymbol.h"
16 #include "llvm/MC/MachineLocation.h"
21 /// \brief This struct describes location entries emitted in the .debug_loc
24 // Begin and end symbols for the address range that this location is valid.
25 const MCSymbol *Begin;
29 /// A single location or constant.
31 Value(const MDNode *Var, const MDNode *Expr, int64_t i)
32 : Variable(Var), Expression(Expr), EntryKind(E_Integer) {
35 Value(const MDNode *Var, const MDNode *Expr, const ConstantFP *CFP)
36 : Variable(Var), Expression(Expr), EntryKind(E_ConstantFP) {
39 Value(const MDNode *Var, const MDNode *Expr, const ConstantInt *CIP)
40 : Variable(Var), Expression(Expr), EntryKind(E_ConstantInt) {
43 Value(const MDNode *Var, const MDNode *Expr, MachineLocation Loc)
44 : Variable(Var), Expression(Expr), EntryKind(E_Location), Loc(Loc) {
45 assert(DIVariable(Var).Verify());
46 assert(DIExpression(Expr).Verify());
49 // The variable to which this location entry corresponds.
50 const MDNode *Variable;
52 // Any complex address location expression for this Value.
53 const MDNode *Expression;
55 // Type of entry that this represents.
56 enum EntryType { E_Location, E_Integer, E_ConstantFP, E_ConstantInt };
57 enum EntryType EntryKind;
62 const ConstantFP *CFP;
63 const ConstantInt *CIP;
66 // Or a location in the machine frame.
69 bool isLocation() const { return EntryKind == E_Location; }
70 bool isInt() const { return EntryKind == E_Integer; }
71 bool isConstantFP() const { return EntryKind == E_ConstantFP; }
72 bool isConstantInt() const { return EntryKind == E_ConstantInt; }
73 int64_t getInt() const { return Constant.Int; }
74 const ConstantFP *getConstantFP() const { return Constant.CFP; }
75 const ConstantInt *getConstantInt() const { return Constant.CIP; }
76 MachineLocation getLoc() const { return Loc; }
77 const MDNode *getVariableNode() const { return Variable; }
78 DIVariable getVariable() const { return DIVariable(Variable); }
79 bool isBitPiece() const { return getExpression().isBitPiece(); }
80 DIExpression getExpression() const { return DIExpression(Expression); }
81 friend bool operator==(const Value &, const Value &);
82 friend bool operator<(const Value &, const Value &);
86 /// A nonempty list of locations/constants belonging to this entry,
88 SmallVector<Value, 1> Values;
89 SmallString<8> DWARFBytes;
90 SmallVector<std::string, 1> Comments;
93 DebugLocEntry(const MCSymbol *B, const MCSymbol *E, Value Val)
95 Values.push_back(std::move(Val));
98 /// \brief If this and Next are describing different pieces of the same
99 // variable, merge them by appending Next's values to the current
101 // Return true if the merge was successful.
102 bool MergeValues(const DebugLocEntry &Next) {
103 if (Begin == Next.Begin) {
104 DIExpression Expr(Values[0].Expression);
105 DIVariable Var(Values[0].Variable);
106 DIExpression NextExpr(Next.Values[0].Expression);
107 DIVariable NextVar(Next.Values[0].Variable);
108 if (Var == NextVar && Expr.isBitPiece() &&
109 NextExpr.isBitPiece()) {
110 addValues(Next.Values);
118 /// \brief Attempt to merge this DebugLocEntry with Next and return
119 /// true if the merge was successful. Entries can be merged if they
120 /// share the same Loc/Constant and if Next immediately follows this
122 bool MergeRanges(const DebugLocEntry &Next) {
123 // If this and Next are describing the same variable, merge them.
124 if ((End == Next.Begin && Values == Next.Values)) {
131 const MCSymbol *getBeginSym() const { return Begin; }
132 const MCSymbol *getEndSym() const { return End; }
133 ArrayRef<Value> getValues() const { return Values; }
134 void addValues(ArrayRef<DebugLocEntry::Value> Vals) {
135 Values.append(Vals.begin(), Vals.end());
137 assert(std::all_of(Values.begin(), Values.end(), [](DebugLocEntry::Value V){
138 return V.isBitPiece();
139 }) && "value must be a piece");
142 // Sort the pieces by offset.
143 // Remove any duplicate entries by dropping all but the first.
144 void sortUniqueValues() {
145 std::sort(Values.begin(), Values.end());
146 Values.erase(std::unique(Values.begin(), Values.end(),
147 [](const Value &A, const Value &B) {
148 return A.getVariable() == B.getVariable() &&
149 A.getExpression() == B.getExpression();
154 void finalize(const AsmPrinter &AP,
155 const DITypeIdentifierMap &TypeIdentifierMap);
156 StringRef getDWARFBytes() const {
157 assert((!DWARFBytes.empty() || Values[0].isConstantFP())
158 && "DebugLocEntry not finalized?");
161 const SmallVectorImpl<std::string> &getComments() const {
166 /// Compare two Values for equality.
167 inline bool operator==(const DebugLocEntry::Value &A,
168 const DebugLocEntry::Value &B) {
169 if (A.EntryKind != B.EntryKind)
172 if (A.Expression != B.Expression)
175 if (A.Variable != B.Variable)
178 switch (A.EntryKind) {
179 case DebugLocEntry::Value::E_Location:
180 return A.Loc == B.Loc;
181 case DebugLocEntry::Value::E_Integer:
182 return A.Constant.Int == B.Constant.Int;
183 case DebugLocEntry::Value::E_ConstantFP:
184 return A.Constant.CFP == B.Constant.CFP;
185 case DebugLocEntry::Value::E_ConstantInt:
186 return A.Constant.CIP == B.Constant.CIP;
188 llvm_unreachable("unhandled EntryKind");
191 /// Compare two pieces based on their offset.
192 inline bool operator<(const DebugLocEntry::Value &A,
193 const DebugLocEntry::Value &B) {
194 return A.getExpression().getBitPieceOffset() <
195 B.getExpression().getBitPieceOffset();