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"
22 /// \brief This struct describes location entries emitted in the .debug_loc
25 /// Begin and end symbols for the address range that this location is valid.
26 const MCSymbol *Begin;
30 /// \brief A single location or constant.
32 Value(const DIExpression *Expr, int64_t i)
33 : Expression(Expr), EntryKind(E_Integer) {
36 Value(const DIExpression *Expr, const ConstantFP *CFP)
37 : Expression(Expr), EntryKind(E_ConstantFP) {
40 Value(const DIExpression *Expr, const ConstantInt *CIP)
41 : Expression(Expr), EntryKind(E_ConstantInt) {
44 Value(const DIExpression *Expr, MachineLocation Loc)
45 : Expression(Expr), EntryKind(E_Location), Loc(Loc) {
46 assert(cast<DIExpression>(Expr)->isValid());
49 /// Any complex address location expression for this Value.
50 const DIExpression *Expression;
52 /// Type of entry that this represents.
53 enum EntryType { E_Location, E_Integer, E_ConstantFP, E_ConstantInt };
54 enum EntryType EntryKind;
56 /// Either a constant,
59 const ConstantFP *CFP;
60 const ConstantInt *CIP;
63 // Or a location in the machine frame.
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 &);
81 /// A nonempty list of locations/constants belonging to this entry,
83 SmallVector<Value, 1> Values;
86 DebugLocEntry(const MCSymbol *B, const MCSymbol *E, Value Val)
88 Values.push_back(std::move(Val));
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
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);
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
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)) {
121 const MCSymbol *getBeginSym() const { return Begin; }
122 const MCSymbol *getEndSym() const { return End; }
123 ArrayRef<Value> getValues() const { return Values; }
124 void addValues(ArrayRef<DebugLocEntry::Value> Vals) {
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");
132 // \brief Sort the pieces by offset.
133 // Remove any duplicate entries by dropping all but the first.
134 void sortUniqueValues() {
135 std::sort(Values.begin(), Values.end());
138 Values.begin(), Values.end(), [](const Value &A, const Value &B) {
139 return A.getExpression() == B.getExpression();
144 /// \brief Lower this entry into a DWARF expression.
145 void finalize(const AsmPrinter &AP, DebugLocStream &Locs,
146 const DIBasicType *BT);
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)
155 if (A.Expression != B.Expression)
158 switch (A.EntryKind) {
159 case DebugLocEntry::Value::E_Location:
160 return A.Loc == B.Loc;
161 case DebugLocEntry::Value::E_Integer:
162 return A.Constant.Int == B.Constant.Int;
163 case DebugLocEntry::Value::E_ConstantFP:
164 return A.Constant.CFP == B.Constant.CFP;
165 case DebugLocEntry::Value::E_ConstantInt:
166 return A.Constant.CIP == B.Constant.CIP;
168 llvm_unreachable("unhandled EntryKind");
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() <
175 B.getExpression()->getBitPieceOffset();