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/Config/llvm-config.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"
19 #include "llvm/Support/Debug.h"
24 /// This struct describes location entries emitted in the .debug_loc
27 /// Begin and end symbols for the address range that this location is valid.
28 const MCSymbol *Begin;
32 /// A single location or constant.
34 Value(const DIExpression *Expr, int64_t i)
35 : Expression(Expr), EntryKind(E_Integer) {
38 Value(const DIExpression *Expr, const ConstantFP *CFP)
39 : Expression(Expr), EntryKind(E_ConstantFP) {
42 Value(const DIExpression *Expr, const ConstantInt *CIP)
43 : Expression(Expr), EntryKind(E_ConstantInt) {
46 Value(const DIExpression *Expr, MachineLocation Loc)
47 : Expression(Expr), EntryKind(E_Location), Loc(Loc) {
48 assert(cast<DIExpression>(Expr)->isValid());
51 /// Any complex address location expression for this Value.
52 const DIExpression *Expression;
54 /// Type of entry that this represents.
55 enum EntryType { E_Location, E_Integer, E_ConstantFP, E_ConstantInt };
56 enum EntryType EntryKind;
58 /// Either a constant,
61 const ConstantFP *CFP;
62 const ConstantInt *CIP;
65 // Or a location in the machine frame.
68 bool isLocation() const { return EntryKind == E_Location; }
69 bool isInt() const { return EntryKind == E_Integer; }
70 bool isConstantFP() const { return EntryKind == E_ConstantFP; }
71 bool isConstantInt() const { return EntryKind == E_ConstantInt; }
72 int64_t getInt() const { return Constant.Int; }
73 const ConstantFP *getConstantFP() const { return Constant.CFP; }
74 const ConstantInt *getConstantInt() const { return Constant.CIP; }
75 MachineLocation getLoc() const { return Loc; }
76 bool isFragment() const { return getExpression()->isFragment(); }
77 const DIExpression *getExpression() const { return Expression; }
78 friend bool operator==(const Value &, const Value &);
79 friend bool operator<(const Value &, const Value &);
80 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
81 LLVM_DUMP_METHOD void dump() const {
83 llvm::dbgs() << "Loc = { reg=" << Loc.getReg() << " ";
88 else if (isConstantInt())
90 else if (isConstantFP())
99 /// A nonempty list of locations/constants belonging to this entry,
100 /// sorted by offset.
101 SmallVector<Value, 1> Values;
104 DebugLocEntry(const MCSymbol *B, const MCSymbol *E, Value Val)
106 Values.push_back(std::move(Val));
109 /// If this and Next are describing different pieces of the same
110 /// variable, merge them by appending Next's values to the current
112 /// Return true if the merge was successful.
113 bool MergeValues(const DebugLocEntry &Next);
115 /// Attempt to merge this DebugLocEntry with Next and return
116 /// true if the merge was successful. Entries can be merged if they
117 /// share the same Loc/Constant and if Next immediately follows this
119 bool MergeRanges(const DebugLocEntry &Next) {
120 // If this and Next are describing the same variable, merge them.
121 if ((End == Next.Begin && Values == Next.Values)) {
128 const MCSymbol *getBeginSym() const { return Begin; }
129 const MCSymbol *getEndSym() const { return End; }
130 ArrayRef<Value> getValues() const { return Values; }
131 void addValues(ArrayRef<DebugLocEntry::Value> Vals) {
132 Values.append(Vals.begin(), Vals.end());
134 assert(all_of(Values, [](DebugLocEntry::Value V) {
135 return V.isFragment();
136 }) && "value must be a piece");
139 // Sort the pieces by offset.
140 // Remove any duplicate entries by dropping all but the first.
141 void sortUniqueValues() {
145 Values.begin(), Values.end(), [](const Value &A, const Value &B) {
146 return A.getExpression() == B.getExpression();
151 /// Lower this entry into a DWARF expression.
152 void finalize(const AsmPrinter &AP, DebugLocStream::ListBuilder &List,
153 const DIBasicType *BT);
156 /// Compare two Values for equality.
157 inline bool operator==(const DebugLocEntry::Value &A,
158 const DebugLocEntry::Value &B) {
159 if (A.EntryKind != B.EntryKind)
162 if (A.Expression != B.Expression)
165 switch (A.EntryKind) {
166 case DebugLocEntry::Value::E_Location:
167 return A.Loc == B.Loc;
168 case DebugLocEntry::Value::E_Integer:
169 return A.Constant.Int == B.Constant.Int;
170 case DebugLocEntry::Value::E_ConstantFP:
171 return A.Constant.CFP == B.Constant.CFP;
172 case DebugLocEntry::Value::E_ConstantInt:
173 return A.Constant.CIP == B.Constant.CIP;
175 llvm_unreachable("unhandled EntryKind");
178 /// Compare two fragments based on their offset.
179 inline bool operator<(const DebugLocEntry::Value &A,
180 const DebugLocEntry::Value &B) {
181 return A.getExpression()->getFragmentInfo()->OffsetInBits <
182 B.getExpression()->getFragmentInfo()->OffsetInBits;