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 CODEGEN_ASMPRINTER_DEBUGLOCENTRY_H__
11 #define CODEGEN_ASMPRINTER_DEBUGLOCENTRY_H__
12 #include "llvm/IR/Constants.h"
13 #include "llvm/IR/DebugInfo.h"
14 #include "llvm/MC/MachineLocation.h"
15 #include "llvm/MC/MCSymbol.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, int64_t i)
30 : Variable(Var), EntryKind(E_Integer) {
33 Value(const MDNode *Var, const ConstantFP *CFP)
34 : Variable(Var), EntryKind(E_ConstantFP) {
37 Value(const MDNode *Var, const ConstantInt *CIP)
38 : Variable(Var), EntryKind(E_ConstantInt) {
41 Value(const MDNode *Var, MachineLocation Loc)
42 : Variable(Var), EntryKind(E_Location), Loc(Loc) {
45 // The variable to which this location entry corresponds.
46 const MDNode *Variable;
48 // Type of entry that this represents.
49 enum EntryType { E_Location, E_Integer, E_ConstantFP, E_ConstantInt };
50 enum EntryType EntryKind;
55 const ConstantFP *CFP;
56 const ConstantInt *CIP;
59 // Or a location in the machine frame.
62 bool operator==(const Value &other) const {
63 if (EntryKind != other.EntryKind)
68 return Loc == other.Loc;
70 return Constant.Int == other.Constant.Int;
72 return Constant.CFP == other.Constant.CFP;
74 return Constant.CIP == other.Constant.CIP;
76 llvm_unreachable("unhandled EntryKind");
79 // Compare two pieces based on their offset.
80 bool operator<(const Value &other) const {
81 DIVariable Var(Variable);
82 DIVariable OtherVar(other.Variable);
83 return Var.getPieceOffset() < OtherVar.getPieceOffset();
86 bool isLocation() const { return EntryKind == E_Location; }
87 bool isInt() const { return EntryKind == E_Integer; }
88 bool isConstantFP() const { return EntryKind == E_ConstantFP; }
89 bool isConstantInt() const { return EntryKind == E_ConstantInt; }
90 int64_t getInt() const { return Constant.Int; }
91 const ConstantFP *getConstantFP() const { return Constant.CFP; }
92 const ConstantInt *getConstantInt() const { return Constant.CIP; }
93 MachineLocation getLoc() const { return Loc; }
94 const MDNode *getVariable() const { return Variable; }
97 /// A list of locations/constants belonging to this entry, sorted by offset.
98 SmallVector<Value, 1> Values;
101 DebugLocEntry() : Begin(nullptr), End(nullptr) {}
102 DebugLocEntry(const MCSymbol *B, const MCSymbol *E, Value Val)
104 Values.push_back(std::move(Val));
107 /// \brief If this and Next are describing different pieces of the same
108 // variable, merge them by appending Next's values to the current
110 // Return true if the merge was successful.
111 bool MergeValues(const DebugLocEntry &Next) {
112 if (Begin == Next.Begin && Values.size() > 0 && Next.Values.size() > 0) {
113 DIVariable Var(Values[0].Variable);
114 DIVariable NextVar(Next.Values[0].Variable);
115 if (Var.getName() == NextVar.getName() &&
116 Var.isVariablePiece() && NextVar.isVariablePiece()) {
117 Values.append(Next.Values.begin(), Next.Values.end());
126 /// \brief Attempt to merge this DebugLocEntry with Next and return
127 /// true if the merge was successful. Entries can be merged if they
128 /// share the same Loc/Constant and if Next immediately follows this
130 bool MergeRanges(const DebugLocEntry &Next) {
131 // If this and Next are describing the same variable, merge them.
132 if ((End == Next.Begin && Values == Next.Values)) {
139 const MCSymbol *getBeginSym() const { return Begin; }
140 const MCSymbol *getEndSym() const { return End; }
141 const ArrayRef<Value> getValues() const { return Values; }
142 void addValue(Value Val) {
143 assert(DIVariable(Val.Variable).isVariablePiece() &&
144 "multi-value DebugLocEntries must be pieces");
145 Values.push_back(Val);
149 // Sort the pieces by offset.
150 // Remove any duplicate entries by dropping all but the first.
151 void sortUniqueValues() {
152 std::sort(Values.begin(), Values.end());
153 Values.erase(std::unique(Values.begin(), Values.end()), Values.end());