blob: a68e8cc6b4b31ca4f06c8f8b376c615ac4937925 [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 Prantl96b15512015-10-15 20:58:55 +000012
13#include "DebugLocStream.h"
David Blaikie1275e4f2014-04-01 21:49:04 +000014#include "llvm/IR/Constants.h"
Adrian Prantlb1416832014-08-01 22:11:58 +000015#include "llvm/IR/DebugInfo.h"
David Blaikie1275e4f2014-04-01 21:49:04 +000016#include "llvm/MC/MCSymbol.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000017#include "llvm/MC/MachineLocation.h"
Benjamin Kramer4dbf3372016-03-04 10:49:30 +000018#include "llvm/Support/Debug.h"
David Blaikie1275e4f2014-04-01 21:49:04 +000019
20namespace llvm {
Adrian Prantl92da14b2015-03-02 22:02:33 +000021class AsmPrinter;
Duncan P. N. Exon Smithc0f7dd72015-04-17 16:36:10 +000022
David Blaikie1275e4f2014-04-01 21:49:04 +000023/// \brief This struct describes location entries emitted in the .debug_loc
24/// section.
25class DebugLocEntry {
Adrian Prantl92da14b2015-03-02 22:02:33 +000026 /// Begin and end symbols for the address range that this location is valid.
David Blaikie1275e4f2014-04-01 21:49:04 +000027 const MCSymbol *Begin;
28 const MCSymbol *End;
29
Adrian Prantle19e5ef2014-04-27 18:25:40 +000030public:
Adrian Prantl92da14b2015-03-02 22:02:33 +000031 /// \brief A single location or constant.
Adrian Prantle19e5ef2014-04-27 18:25:40 +000032 struct Value {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000033 Value(const DIExpression *Expr, int64_t i)
Duncan P. N. Exon Smith546c8be2015-04-17 16:33:37 +000034 : Expression(Expr), EntryKind(E_Integer) {
Adrian Prantle19e5ef2014-04-27 18:25:40 +000035 Constant.Int = i;
36 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000037 Value(const DIExpression *Expr, const ConstantFP *CFP)
Duncan P. N. Exon Smith546c8be2015-04-17 16:33:37 +000038 : Expression(Expr), EntryKind(E_ConstantFP) {
Adrian Prantle19e5ef2014-04-27 18:25:40 +000039 Constant.CFP = CFP;
40 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000041 Value(const DIExpression *Expr, const ConstantInt *CIP)
Duncan P. N. Exon Smith546c8be2015-04-17 16:33:37 +000042 : Expression(Expr), EntryKind(E_ConstantInt) {
Adrian Prantle19e5ef2014-04-27 18:25:40 +000043 Constant.CIP = CIP;
44 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000045 Value(const DIExpression *Expr, MachineLocation Loc)
Duncan P. N. Exon Smith546c8be2015-04-17 16:33:37 +000046 : Expression(Expr), EntryKind(E_Location), Loc(Loc) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000047 assert(cast<DIExpression>(Expr)->isValid());
David Blaikied306baf2014-04-01 22:04:07 +000048 }
49
Adrian Prantl92da14b2015-03-02 22:02:33 +000050 /// Any complex address location expression for this Value.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000051 const DIExpression *Expression;
Adrian Prantl87b7eb92014-10-01 18:55:02 +000052
Adrian Prantl92da14b2015-03-02 22:02:33 +000053 /// Type of entry that this represents.
Adrian Prantle19e5ef2014-04-27 18:25:40 +000054 enum EntryType { E_Location, E_Integer, E_ConstantFP, E_ConstantInt };
55 enum EntryType EntryKind;
56
Adrian Prantl92da14b2015-03-02 22:02:33 +000057 /// Either a constant,
Adrian Prantle19e5ef2014-04-27 18:25:40 +000058 union {
59 int64_t Int;
60 const ConstantFP *CFP;
61 const ConstantInt *CIP;
62 } Constant;
63
64 // Or a location in the machine frame.
65 MachineLocation Loc;
66
Adrian Prantle19e5ef2014-04-27 18:25:40 +000067 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; }
Adrian Prantl941fa752016-12-05 18:04:47 +000075 bool isFragment() const { return getExpression()->isFragment(); }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000076 const DIExpression *getExpression() const { return Expression; }
Adrian Prantle8bde9f2014-08-11 22:52:56 +000077 friend bool operator==(const Value &, const Value &);
78 friend bool operator<(const Value &, const Value &);
Matthias Braun194ded52017-01-28 06:53:55 +000079#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
80 LLVM_DUMP_METHOD void dump() const {
Adrian Prantldba58fb2016-02-29 22:28:22 +000081 if (isLocation()) {
82 llvm::dbgs() << "Loc = { reg=" << Loc.getReg() << " ";
83 if (Loc.isIndirect())
84 llvm::dbgs() << '+' << Loc.getOffset();
85 llvm::dbgs() << "} ";
86 }
87 else if (isConstantInt())
88 Constant.CIP->dump();
89 else if (isConstantFP())
90 Constant.CFP->dump();
91 if (Expression)
92 Expression->dump();
93 }
Matthias Braun194ded52017-01-28 06:53:55 +000094#endif
Adrian Prantle19e5ef2014-04-27 18:25:40 +000095 };
Adrian Prantle8bde9f2014-08-11 22:52:56 +000096
Adrian Prantle19e5ef2014-04-27 18:25:40 +000097private:
Adrian Prantlbe4b5172014-08-11 21:06:03 +000098 /// A nonempty list of locations/constants belonging to this entry,
99 /// sorted by offset.
Adrian Prantle19e5ef2014-04-27 18:25:40 +0000100 SmallVector<Value, 1> Values;
101
David Blaikie1275e4f2014-04-01 21:49:04 +0000102public:
David Blaikiee1a26a62014-08-05 23:14:16 +0000103 DebugLocEntry(const MCSymbol *B, const MCSymbol *E, Value Val)
104 : Begin(B), End(E) {
Adrian Prantle19e5ef2014-04-27 18:25:40 +0000105 Values.push_back(std::move(Val));
David Blaikie1275e4f2014-04-01 21:49:04 +0000106 }
107
Adrian Prantle09ee3f2014-08-11 20:59:28 +0000108 /// \brief If this and Next are describing different pieces of the same
Adrian Prantl92da14b2015-03-02 22:02:33 +0000109 /// variable, merge them by appending Next's values to the current
110 /// list of values.
111 /// Return true if the merge was successful.
Keno Fischerf8eb6a12016-01-16 01:11:33 +0000112 bool MergeValues(const DebugLocEntry &Next);
Adrian Prantle09ee3f2014-08-11 20:59:28 +0000113
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
117 /// Entry.
118 bool MergeRanges(const DebugLocEntry &Next) {
Adrian Prantlb1416832014-08-01 22:11:58 +0000119 // If this and Next are describing the same variable, merge them.
Adrian Prantle19e5ef2014-04-27 18:25:40 +0000120 if ((End == Next.Begin && Values == Next.Values)) {
David Blaikie6fa99662014-04-01 23:19:23 +0000121 End = Next.End;
122 return true;
123 }
124 return false;
David Blaikie1275e4f2014-04-01 21:49:04 +0000125 }
Adrian Prantlb1416832014-08-01 22:11:58 +0000126
David Blaikie1275e4f2014-04-01 21:49:04 +0000127 const MCSymbol *getBeginSym() const { return Begin; }
128 const MCSymbol *getEndSym() const { return End; }
Craig Topper3af97222014-08-27 05:25:00 +0000129 ArrayRef<Value> getValues() const { return Values; }
Adrian Prantl1c6f2ec2014-08-11 21:06:00 +0000130 void addValues(ArrayRef<DebugLocEntry::Value> Vals) {
131 Values.append(Vals.begin(), Vals.end());
Adrian Prantl293dd932014-08-11 21:05:55 +0000132 sortUniqueValues();
David Majnemer0a16c222016-08-11 21:15:00 +0000133 assert(all_of(Values, [](DebugLocEntry::Value V) {
Adrian Prantl941fa752016-12-05 18:04:47 +0000134 return V.isFragment();
Adrian Prantl1c6f2ec2014-08-11 21:06:00 +0000135 }) && "value must be a piece");
Adrian Prantl293dd932014-08-11 21:05:55 +0000136 }
137
Adrian Prantl92da14b2015-03-02 22:02:33 +0000138 // \brief Sort the pieces by offset.
Adrian Prantl293dd932014-08-11 21:05:55 +0000139 // Remove any duplicate entries by dropping all but the first.
140 void sortUniqueValues() {
141 std::sort(Values.begin(), Values.end());
Duncan P. N. Exon Smith546c8be2015-04-17 16:33:37 +0000142 Values.erase(
143 std::unique(
144 Values.begin(), Values.end(), [](const Value &A, const Value &B) {
145 return A.getExpression() == B.getExpression();
146 }),
147 Values.end());
Adrian Prantlb1416832014-08-01 22:11:58 +0000148 }
Adrian Prantl92da14b2015-03-02 22:02:33 +0000149
150 /// \brief Lower this entry into a DWARF expression.
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +0000151 void finalize(const AsmPrinter &AP, DebugLocStream::ListBuilder &List,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000152 const DIBasicType *BT);
David Blaikie1275e4f2014-04-01 21:49:04 +0000153};
154
Adrian Prantl92da14b2015-03-02 22:02:33 +0000155/// \brief Compare two Values for equality.
Adrian Prantle8bde9f2014-08-11 22:52:56 +0000156inline bool operator==(const DebugLocEntry::Value &A,
157 const DebugLocEntry::Value &B) {
158 if (A.EntryKind != B.EntryKind)
159 return false;
160
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000161 if (A.Expression != B.Expression)
162 return false;
163
Adrian Prantle8bde9f2014-08-11 22:52:56 +0000164 switch (A.EntryKind) {
165 case DebugLocEntry::Value::E_Location:
166 return A.Loc == B.Loc;
167 case DebugLocEntry::Value::E_Integer:
168 return A.Constant.Int == B.Constant.Int;
169 case DebugLocEntry::Value::E_ConstantFP:
170 return A.Constant.CFP == B.Constant.CFP;
171 case DebugLocEntry::Value::E_ConstantInt:
172 return A.Constant.CIP == B.Constant.CIP;
173 }
174 llvm_unreachable("unhandled EntryKind");
David Blaikie1275e4f2014-04-01 21:49:04 +0000175}
Adrian Prantle8bde9f2014-08-11 22:52:56 +0000176
Adrian Prantl941fa752016-12-05 18:04:47 +0000177/// Compare two fragments based on their offset.
Adrian Prantle8bde9f2014-08-11 22:52:56 +0000178inline bool operator<(const DebugLocEntry::Value &A,
179 const DebugLocEntry::Value &B) {
Adrian Prantl49797ca2016-12-22 05:27:12 +0000180 return A.getExpression()->getFragmentInfo()->OffsetInBits <
181 B.getExpression()->getFragmentInfo()->OffsetInBits;
Adrian Prantle8bde9f2014-08-11 22:52:56 +0000182}
183
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000184}
Adrian Prantle8bde9f2014-08-11 22:52:56 +0000185
David Blaikie1275e4f2014-04-01 21:49:04 +0000186#endif