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/IR/Constants.h"
13 #include "llvm/IR/DebugInfo.h"
14 #include "llvm/MC/MCSymbol.h"
15 #include "llvm/MC/MachineLocation.h"
19 /// \brief This struct describes location entries emitted in the .debug_loc
22 // Begin and end symbols for the address range that this location is valid.
23 const MCSymbol *Begin;
27 /// A single location or constant.
29 Value(const MDNode *Var, const MDNode *Expr, int64_t i)
30 : Variable(Var), Expression(Expr), EntryKind(E_Integer) {
33 Value(const MDNode *Var, const MDNode *Expr, const ConstantFP *CFP)
34 : Variable(Var), Expression(Expr), EntryKind(E_ConstantFP) {
37 Value(const MDNode *Var, const MDNode *Expr, const ConstantInt *CIP)
38 : Variable(Var), Expression(Expr), EntryKind(E_ConstantInt) {
41 Value(const MDNode *Var, const MDNode *Expr, MachineLocation Loc)
42 : Variable(Var), Expression(Expr), EntryKind(E_Location), Loc(Loc) {
43 assert(DIVariable(Var).Verify());
44 assert(DIExpression(Expr).Verify());
47 // The variable to which this location entry corresponds.
48 const MDNode *Variable;
50 // Any complex address location expression for this Value.
51 const MDNode *Expression;
53 // Type of entry that this represents.
54 enum EntryType { E_Location, E_Integer, E_ConstantFP, E_ConstantInt };
55 enum EntryType EntryKind;
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 const MDNode *getVariableNode() const { return Variable; }
76 DIVariable getVariable() const { return DIVariable(Variable); }
77 bool isVariablePiece() const { return getExpression().isVariablePiece(); }
78 DIExpression getExpression() const { return DIExpression(Expression); }
79 friend bool operator==(const Value &, const Value &);
80 friend bool operator<(const Value &, const Value &);
84 /// A nonempty list of locations/constants belonging to this entry,
86 SmallVector<Value, 1> Values;
89 DebugLocEntry(const MCSymbol *B, const MCSymbol *E, Value Val)
91 Values.push_back(std::move(Val));
94 /// \brief If this and Next are describing different pieces of the same
95 // variable, merge them by appending Next's values to the current
97 // Return true if the merge was successful.
98 bool MergeValues(const DebugLocEntry &Next) {
99 if (Begin == Next.Begin) {
100 DIExpression Expr(Values[0].Expression);
101 DIVariable Var(Values[0].Variable);
102 DIExpression NextExpr(Next.Values[0].Expression);
103 DIVariable NextVar(Next.Values[0].Variable);
104 if (Var == NextVar && Expr.isVariablePiece() &&
105 NextExpr.isVariablePiece()) {
106 addValues(Next.Values);
114 /// \brief Attempt to merge this DebugLocEntry with Next and return
115 /// true if the merge was successful. Entries can be merged if they
116 /// share the same Loc/Constant and if Next immediately follows this
118 bool MergeRanges(const DebugLocEntry &Next) {
119 // If this and Next are describing the same variable, merge them.
120 if ((End == Next.Begin && Values == Next.Values)) {
127 const MCSymbol *getBeginSym() const { return Begin; }
128 const MCSymbol *getEndSym() const { return End; }
129 ArrayRef<Value> getValues() const { return Values; }
130 void addValues(ArrayRef<DebugLocEntry::Value> Vals) {
131 Values.append(Vals.begin(), Vals.end());
133 assert(std::all_of(Values.begin(), Values.end(), [](DebugLocEntry::Value V){
134 return V.isVariablePiece();
135 }) && "value must be a piece");
138 // Sort the pieces by offset.
139 // Remove any duplicate entries by dropping all but the first.
140 void sortUniqueValues() {
141 std::sort(Values.begin(), Values.end());
142 Values.erase(std::unique(Values.begin(), Values.end(),
143 [](const Value &A, const Value &B) {
144 return A.getVariable() == B.getVariable() &&
145 A.getExpression() == B.getExpression();
151 /// Compare two Values for equality.
152 inline bool operator==(const DebugLocEntry::Value &A,
153 const DebugLocEntry::Value &B) {
154 if (A.EntryKind != B.EntryKind)
157 if (A.Expression != B.Expression)
160 if (A.Variable != B.Variable)
163 switch (A.EntryKind) {
164 case DebugLocEntry::Value::E_Location:
165 return A.Loc == B.Loc;
166 case DebugLocEntry::Value::E_Integer:
167 return A.Constant.Int == B.Constant.Int;
168 case DebugLocEntry::Value::E_ConstantFP:
169 return A.Constant.CFP == B.Constant.CFP;
170 case DebugLocEntry::Value::E_ConstantInt:
171 return A.Constant.CIP == B.Constant.CIP;
173 llvm_unreachable("unhandled EntryKind");
176 /// Compare two pieces based on their offset.
177 inline bool operator<(const DebugLocEntry::Value &A,
178 const DebugLocEntry::Value &B) {
179 return A.getExpression().getPieceOffset() <
180 B.getExpression().getPieceOffset();