blob: dd7f806e0297f46e2d8f7fff7d16465f177270a9 [file] [log] [blame]
David Blaikie1275e4f2014-04-01 21:49:04 +00001//===-- llvm/CodeGen/DebugLocEntry.h - Entry in debug_loc list -*- C++ -*--===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000010#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCENTRY_H
11#define LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCENTRY_H
Adrian Prantl92da14b2015-03-02 22:02:33 +000012#include "llvm/ADT/SmallString.h"
David Blaikie1275e4f2014-04-01 21:49:04 +000013#include "llvm/IR/Constants.h"
Adrian Prantlb1416832014-08-01 22:11:58 +000014#include "llvm/IR/DebugInfo.h"
David Blaikie1275e4f2014-04-01 21:49:04 +000015#include "llvm/MC/MCSymbol.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000016#include "llvm/MC/MachineLocation.h"
David Blaikie1275e4f2014-04-01 21:49:04 +000017
18namespace llvm {
Adrian Prantl92da14b2015-03-02 22:02:33 +000019class AsmPrinter;
David Blaikie1275e4f2014-04-01 21:49:04 +000020class MDNode;
21/// \brief This struct describes location entries emitted in the .debug_loc
22/// section.
23class DebugLocEntry {
Adrian Prantl92da14b2015-03-02 22:02:33 +000024 /// Begin and end symbols for the address range that this location is valid.
David Blaikie1275e4f2014-04-01 21:49:04 +000025 const MCSymbol *Begin;
26 const MCSymbol *End;
27
Adrian Prantle19e5ef2014-04-27 18:25:40 +000028public:
Adrian Prantl92da14b2015-03-02 22:02:33 +000029 /// \brief A single location or constant.
Adrian Prantle19e5ef2014-04-27 18:25:40 +000030 struct Value {
Adrian Prantl87b7eb92014-10-01 18:55:02 +000031 Value(const MDNode *Var, const MDNode *Expr, int64_t i)
32 : Variable(Var), Expression(Expr), EntryKind(E_Integer) {
Adrian Prantle19e5ef2014-04-27 18:25:40 +000033 Constant.Int = i;
34 }
Adrian Prantl87b7eb92014-10-01 18:55:02 +000035 Value(const MDNode *Var, const MDNode *Expr, const ConstantFP *CFP)
36 : Variable(Var), Expression(Expr), EntryKind(E_ConstantFP) {
Adrian Prantle19e5ef2014-04-27 18:25:40 +000037 Constant.CFP = CFP;
38 }
Adrian Prantl87b7eb92014-10-01 18:55:02 +000039 Value(const MDNode *Var, const MDNode *Expr, const ConstantInt *CIP)
40 : Variable(Var), Expression(Expr), EntryKind(E_ConstantInt) {
Adrian Prantle19e5ef2014-04-27 18:25:40 +000041 Constant.CIP = CIP;
42 }
Adrian Prantl87b7eb92014-10-01 18:55:02 +000043 Value(const MDNode *Var, const MDNode *Expr, MachineLocation Loc)
44 : Variable(Var), Expression(Expr), EntryKind(E_Location), Loc(Loc) {
Duncan P. N. Exon Smitha5099dc2015-04-06 19:49:39 +000045 assert(isa<MDLocalVariable>(Var));
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +000046 assert(cast<MDExpression>(Expr)->isValid());
David Blaikied306baf2014-04-01 22:04:07 +000047 }
48
Adrian Prantl92da14b2015-03-02 22:02:33 +000049 /// The variable to which this location entry corresponds.
Adrian Prantle19e5ef2014-04-27 18:25:40 +000050 const MDNode *Variable;
51
Adrian Prantl92da14b2015-03-02 22:02:33 +000052 /// Any complex address location expression for this Value.
Adrian Prantl87b7eb92014-10-01 18:55:02 +000053 const MDNode *Expression;
54
Adrian Prantl92da14b2015-03-02 22:02:33 +000055 /// Type of entry that this represents.
Adrian Prantle19e5ef2014-04-27 18:25:40 +000056 enum EntryType { E_Location, E_Integer, E_ConstantFP, E_ConstantInt };
57 enum EntryType EntryKind;
58
Adrian Prantl92da14b2015-03-02 22:02:33 +000059 /// Either a constant,
Adrian Prantle19e5ef2014-04-27 18:25:40 +000060 union {
61 int64_t Int;
62 const ConstantFP *CFP;
63 const ConstantInt *CIP;
64 } Constant;
65
66 // Or a location in the machine frame.
67 MachineLocation Loc;
68
Adrian Prantle19e5ef2014-04-27 18:25:40 +000069 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; }
Adrian Prantl76502d82014-08-11 23:22:59 +000077 const MDNode *getVariableNode() const { return Variable; }
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +000078 DIVariable getVariable() const { return cast<MDLocalVariable>(Variable); }
Duncan P. N. Exon Smith6a0320a2015-04-14 01:12:42 +000079 bool isBitPiece() const { return getExpression()->isBitPiece(); }
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +000080 DIExpression getExpression() const {
81 return cast_or_null<MDExpression>(Expression);
82 }
Adrian Prantle8bde9f2014-08-11 22:52:56 +000083 friend bool operator==(const Value &, const Value &);
84 friend bool operator<(const Value &, const Value &);
Adrian Prantle19e5ef2014-04-27 18:25:40 +000085 };
Adrian Prantle8bde9f2014-08-11 22:52:56 +000086
Adrian Prantle19e5ef2014-04-27 18:25:40 +000087private:
Adrian Prantlbe4b5172014-08-11 21:06:03 +000088 /// A nonempty list of locations/constants belonging to this entry,
89 /// sorted by offset.
Adrian Prantle19e5ef2014-04-27 18:25:40 +000090 SmallVector<Value, 1> Values;
Adrian Prantl92da14b2015-03-02 22:02:33 +000091 SmallString<8> DWARFBytes;
92 SmallVector<std::string, 1> Comments;
Adrian Prantle19e5ef2014-04-27 18:25:40 +000093
David Blaikie1275e4f2014-04-01 21:49:04 +000094public:
David Blaikiee1a26a62014-08-05 23:14:16 +000095 DebugLocEntry(const MCSymbol *B, const MCSymbol *E, Value Val)
96 : Begin(B), End(E) {
Adrian Prantle19e5ef2014-04-27 18:25:40 +000097 Values.push_back(std::move(Val));
David Blaikie1275e4f2014-04-01 21:49:04 +000098 }
99
Adrian Prantle09ee3f2014-08-11 20:59:28 +0000100 /// \brief If this and Next are describing different pieces of the same
Adrian Prantl92da14b2015-03-02 22:02:33 +0000101 /// variable, merge them by appending Next's values to the current
102 /// list of values.
103 /// Return true if the merge was successful.
Adrian Prantle09ee3f2014-08-11 20:59:28 +0000104 bool MergeValues(const DebugLocEntry &Next) {
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000105 if (Begin == Next.Begin) {
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000106 DIExpression Expr = cast_or_null<MDExpression>(Values[0].Expression);
107 DIVariable Var = cast_or_null<MDLocalVariable>(Values[0].Variable);
108 DIExpression NextExpr =
109 cast_or_null<MDExpression>(Next.Values[0].Expression);
110 DIVariable NextVar = cast_or_null<MDLocalVariable>(Next.Values[0].Variable);
Duncan P. N. Exon Smith6a0320a2015-04-14 01:12:42 +0000111 if (Var == NextVar && Expr->isBitPiece() && NextExpr->isBitPiece()) {
Adrian Prantl1c6f2ec2014-08-11 21:06:00 +0000112 addValues(Next.Values);
Adrian Prantlb1416832014-08-01 22:11:58 +0000113 End = Next.End;
114 return true;
115 }
116 }
Adrian Prantle09ee3f2014-08-11 20:59:28 +0000117 return false;
118 }
119
120 /// \brief Attempt to merge this DebugLocEntry with Next and return
121 /// true if the merge was successful. Entries can be merged if they
122 /// share the same Loc/Constant and if Next immediately follows this
123 /// Entry.
124 bool MergeRanges(const DebugLocEntry &Next) {
Adrian Prantlb1416832014-08-01 22:11:58 +0000125 // If this and Next are describing the same variable, merge them.
Adrian Prantle19e5ef2014-04-27 18:25:40 +0000126 if ((End == Next.Begin && Values == Next.Values)) {
David Blaikie6fa99662014-04-01 23:19:23 +0000127 End = Next.End;
128 return true;
129 }
130 return false;
David Blaikie1275e4f2014-04-01 21:49:04 +0000131 }
Adrian Prantlb1416832014-08-01 22:11:58 +0000132
David Blaikie1275e4f2014-04-01 21:49:04 +0000133 const MCSymbol *getBeginSym() const { return Begin; }
134 const MCSymbol *getEndSym() const { return End; }
Craig Topper3af97222014-08-27 05:25:00 +0000135 ArrayRef<Value> getValues() const { return Values; }
Adrian Prantl1c6f2ec2014-08-11 21:06:00 +0000136 void addValues(ArrayRef<DebugLocEntry::Value> Vals) {
137 Values.append(Vals.begin(), Vals.end());
Adrian Prantl293dd932014-08-11 21:05:55 +0000138 sortUniqueValues();
Adrian Prantl1c6f2ec2014-08-11 21:06:00 +0000139 assert(std::all_of(Values.begin(), Values.end(), [](DebugLocEntry::Value V){
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000140 return V.isBitPiece();
Adrian Prantl1c6f2ec2014-08-11 21:06:00 +0000141 }) && "value must be a piece");
Adrian Prantl293dd932014-08-11 21:05:55 +0000142 }
143
Adrian Prantl92da14b2015-03-02 22:02:33 +0000144 // \brief Sort the pieces by offset.
Adrian Prantl293dd932014-08-11 21:05:55 +0000145 // Remove any duplicate entries by dropping all but the first.
146 void sortUniqueValues() {
147 std::sort(Values.begin(), Values.end());
Adrian Prantl9724b5c2014-08-12 01:07:53 +0000148 Values.erase(std::unique(Values.begin(), Values.end(),
149 [](const Value &A, const Value &B) {
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000150 return A.getVariable() == B.getVariable() &&
151 A.getExpression() == B.getExpression();
152 }),
153 Values.end());
Adrian Prantlb1416832014-08-01 22:11:58 +0000154 }
Adrian Prantl92da14b2015-03-02 22:02:33 +0000155
156 /// \brief Lower this entry into a DWARF expression.
157 void finalize(const AsmPrinter &AP,
158 const DITypeIdentifierMap &TypeIdentifierMap);
159
160 /// \brief Return the lowered DWARF expression.
161 StringRef getDWARFBytes() const { return DWARFBytes; }
162 /// \brief Return the assembler comments for the lowered DWARF expression.
163 const SmallVectorImpl<std::string> &getComments() const { return Comments; }
David Blaikie1275e4f2014-04-01 21:49:04 +0000164};
165
Adrian Prantl92da14b2015-03-02 22:02:33 +0000166/// \brief Compare two Values for equality.
Adrian Prantle8bde9f2014-08-11 22:52:56 +0000167inline bool operator==(const DebugLocEntry::Value &A,
168 const DebugLocEntry::Value &B) {
169 if (A.EntryKind != B.EntryKind)
170 return false;
171
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000172 if (A.Expression != B.Expression)
173 return false;
174
175 if (A.Variable != B.Variable)
Adrian Prantle8bde9f2014-08-11 22:52:56 +0000176 return false;
177
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;
187 }
188 llvm_unreachable("unhandled EntryKind");
David Blaikie1275e4f2014-04-01 21:49:04 +0000189}
Adrian Prantle8bde9f2014-08-11 22:52:56 +0000190
Adrian Prantl92da14b2015-03-02 22:02:33 +0000191/// \brief Compare two pieces based on their offset.
Adrian Prantle8bde9f2014-08-11 22:52:56 +0000192inline bool operator<(const DebugLocEntry::Value &A,
193 const DebugLocEntry::Value &B) {
Duncan P. N. Exon Smith6a0320a2015-04-14 01:12:42 +0000194 return A.getExpression()->getBitPieceOffset() <
195 B.getExpression()->getBitPieceOffset();
Adrian Prantle8bde9f2014-08-11 22:52:56 +0000196}
197
198}
199
David Blaikie1275e4f2014-04-01 21:49:04 +0000200#endif