blob: ac49657b68fafc4910ff2723a5164c4150dbf9d9 [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"
Nico Weber432a3882018-04-30 14:59:11 +000014#include "llvm/Config/llvm-config.h"
David Blaikie1275e4f2014-04-01 21:49:04 +000015#include "llvm/IR/Constants.h"
Adrian Prantlb1416832014-08-01 22:11:58 +000016#include "llvm/IR/DebugInfo.h"
David Blaikie1275e4f2014-04-01 21:49:04 +000017#include "llvm/MC/MCSymbol.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000018#include "llvm/MC/MachineLocation.h"
Benjamin Kramer4dbf3372016-03-04 10:49:30 +000019#include "llvm/Support/Debug.h"
David Blaikie1275e4f2014-04-01 21:49:04 +000020
21namespace llvm {
Adrian Prantl92da14b2015-03-02 22:02:33 +000022class AsmPrinter;
Duncan P. N. Exon Smithc0f7dd72015-04-17 16:36:10 +000023
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000024/// This struct describes location entries emitted in the .debug_loc
David Blaikie1275e4f2014-04-01 21:49:04 +000025/// section.
26class DebugLocEntry {
Adrian Prantl92da14b2015-03-02 22:02:33 +000027 /// Begin and end symbols for the address range that this location is valid.
David Blaikie1275e4f2014-04-01 21:49:04 +000028 const MCSymbol *Begin;
29 const MCSymbol *End;
30
Adrian Prantle19e5ef2014-04-27 18:25:40 +000031public:
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000032 /// A single location or constant.
Adrian Prantle19e5ef2014-04-27 18:25:40 +000033 struct Value {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000034 Value(const DIExpression *Expr, int64_t i)
Duncan P. N. Exon Smith546c8be2015-04-17 16:33:37 +000035 : Expression(Expr), EntryKind(E_Integer) {
Adrian Prantle19e5ef2014-04-27 18:25:40 +000036 Constant.Int = i;
37 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000038 Value(const DIExpression *Expr, const ConstantFP *CFP)
Duncan P. N. Exon Smith546c8be2015-04-17 16:33:37 +000039 : Expression(Expr), EntryKind(E_ConstantFP) {
Adrian Prantle19e5ef2014-04-27 18:25:40 +000040 Constant.CFP = CFP;
41 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000042 Value(const DIExpression *Expr, const ConstantInt *CIP)
Duncan P. N. Exon Smith546c8be2015-04-17 16:33:37 +000043 : Expression(Expr), EntryKind(E_ConstantInt) {
Adrian Prantle19e5ef2014-04-27 18:25:40 +000044 Constant.CIP = CIP;
45 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000046 Value(const DIExpression *Expr, MachineLocation Loc)
Duncan P. N. Exon Smith546c8be2015-04-17 16:33:37 +000047 : Expression(Expr), EntryKind(E_Location), Loc(Loc) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000048 assert(cast<DIExpression>(Expr)->isValid());
David Blaikied306baf2014-04-01 22:04:07 +000049 }
50
Adrian Prantl92da14b2015-03-02 22:02:33 +000051 /// Any complex address location expression for this Value.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000052 const DIExpression *Expression;
Adrian Prantl87b7eb92014-10-01 18:55:02 +000053
Adrian Prantl92da14b2015-03-02 22:02:33 +000054 /// Type of entry that this represents.
Adrian Prantle19e5ef2014-04-27 18:25:40 +000055 enum EntryType { E_Location, E_Integer, E_ConstantFP, E_ConstantInt };
56 enum EntryType EntryKind;
57
Adrian Prantl92da14b2015-03-02 22:02:33 +000058 /// Either a constant,
Adrian Prantle19e5ef2014-04-27 18:25:40 +000059 union {
60 int64_t Int;
61 const ConstantFP *CFP;
62 const ConstantInt *CIP;
63 } Constant;
64
65 // Or a location in the machine frame.
66 MachineLocation Loc;
67
Adrian Prantle19e5ef2014-04-27 18:25:40 +000068 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; }
Adrian Prantl941fa752016-12-05 18:04:47 +000076 bool isFragment() const { return getExpression()->isFragment(); }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000077 const DIExpression *getExpression() const { return Expression; }
Adrian Prantle8bde9f2014-08-11 22:52:56 +000078 friend bool operator==(const Value &, const Value &);
79 friend bool operator<(const Value &, const Value &);
Aaron Ballman615eb472017-10-15 14:32:27 +000080#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Braun194ded52017-01-28 06:53:55 +000081 LLVM_DUMP_METHOD void dump() const {
Adrian Prantldba58fb2016-02-29 22:28:22 +000082 if (isLocation()) {
83 llvm::dbgs() << "Loc = { reg=" << Loc.getReg() << " ";
84 if (Loc.isIndirect())
Adrian Prantl2049c0d2017-08-02 15:22:17 +000085 llvm::dbgs() << "+0";
Adrian Prantldba58fb2016-02-29 22:28:22 +000086 llvm::dbgs() << "} ";
87 }
88 else if (isConstantInt())
89 Constant.CIP->dump();
90 else if (isConstantFP())
91 Constant.CFP->dump();
92 if (Expression)
93 Expression->dump();
94 }
Matthias Braun194ded52017-01-28 06:53:55 +000095#endif
Adrian Prantle19e5ef2014-04-27 18:25:40 +000096 };
Adrian Prantle8bde9f2014-08-11 22:52:56 +000097
Adrian Prantle19e5ef2014-04-27 18:25:40 +000098private:
Adrian Prantlbe4b5172014-08-11 21:06:03 +000099 /// A nonempty list of locations/constants belonging to this entry,
100 /// sorted by offset.
Adrian Prantle19e5ef2014-04-27 18:25:40 +0000101 SmallVector<Value, 1> Values;
102
David Blaikie1275e4f2014-04-01 21:49:04 +0000103public:
David Blaikiee1a26a62014-08-05 23:14:16 +0000104 DebugLocEntry(const MCSymbol *B, const MCSymbol *E, Value Val)
105 : Begin(B), End(E) {
Adrian Prantle19e5ef2014-04-27 18:25:40 +0000106 Values.push_back(std::move(Val));
David Blaikie1275e4f2014-04-01 21:49:04 +0000107 }
108
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000109 /// If this and Next are describing different pieces of the same
Adrian Prantl92da14b2015-03-02 22:02:33 +0000110 /// variable, merge them by appending Next's values to the current
111 /// list of values.
112 /// Return true if the merge was successful.
Keno Fischerf8eb6a12016-01-16 01:11:33 +0000113 bool MergeValues(const DebugLocEntry &Next);
Adrian Prantle09ee3f2014-08-11 20:59:28 +0000114
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000115 /// Attempt to merge this DebugLocEntry with Next and return
Adrian Prantle09ee3f2014-08-11 20:59:28 +0000116 /// true if the merge was successful. Entries can be merged if they
117 /// share the same Loc/Constant and if Next immediately follows this
118 /// Entry.
119 bool MergeRanges(const DebugLocEntry &Next) {
Adrian Prantlb1416832014-08-01 22:11:58 +0000120 // If this and Next are describing the same variable, merge them.
Adrian Prantle19e5ef2014-04-27 18:25:40 +0000121 if ((End == Next.Begin && Values == Next.Values)) {
David Blaikie6fa99662014-04-01 23:19:23 +0000122 End = Next.End;
123 return true;
124 }
125 return false;
David Blaikie1275e4f2014-04-01 21:49:04 +0000126 }
Adrian Prantlb1416832014-08-01 22:11:58 +0000127
David Blaikie1275e4f2014-04-01 21:49:04 +0000128 const MCSymbol *getBeginSym() const { return Begin; }
129 const MCSymbol *getEndSym() const { return End; }
Craig Topper3af97222014-08-27 05:25:00 +0000130 ArrayRef<Value> getValues() const { return Values; }
Adrian Prantl1c6f2ec2014-08-11 21:06:00 +0000131 void addValues(ArrayRef<DebugLocEntry::Value> Vals) {
132 Values.append(Vals.begin(), Vals.end());
Adrian Prantl293dd932014-08-11 21:05:55 +0000133 sortUniqueValues();
David Majnemer0a16c222016-08-11 21:15:00 +0000134 assert(all_of(Values, [](DebugLocEntry::Value V) {
Adrian Prantl941fa752016-12-05 18:04:47 +0000135 return V.isFragment();
Adrian Prantl1c6f2ec2014-08-11 21:06:00 +0000136 }) && "value must be a piece");
Adrian Prantl293dd932014-08-11 21:05:55 +0000137 }
138
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000139 // Sort the pieces by offset.
Adrian Prantl293dd932014-08-11 21:05:55 +0000140 // Remove any duplicate entries by dropping all but the first.
141 void sortUniqueValues() {
Mandeep Singh Grange92f0cf2018-04-06 18:08:42 +0000142 llvm::sort(Values.begin(), Values.end());
Duncan P. N. Exon Smith546c8be2015-04-17 16:33:37 +0000143 Values.erase(
144 std::unique(
145 Values.begin(), Values.end(), [](const Value &A, const Value &B) {
146 return A.getExpression() == B.getExpression();
147 }),
148 Values.end());
Adrian Prantlb1416832014-08-01 22:11:58 +0000149 }
Adrian Prantl92da14b2015-03-02 22:02:33 +0000150
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000151 /// Lower this entry into a DWARF expression.
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +0000152 void finalize(const AsmPrinter &AP, DebugLocStream::ListBuilder &List,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000153 const DIBasicType *BT);
David Blaikie1275e4f2014-04-01 21:49:04 +0000154};
155
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000156/// Compare two Values for equality.
Adrian Prantle8bde9f2014-08-11 22:52:56 +0000157inline bool operator==(const DebugLocEntry::Value &A,
158 const DebugLocEntry::Value &B) {
159 if (A.EntryKind != B.EntryKind)
160 return false;
161
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000162 if (A.Expression != B.Expression)
163 return false;
164
Adrian Prantle8bde9f2014-08-11 22:52:56 +0000165 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;
174 }
175 llvm_unreachable("unhandled EntryKind");
David Blaikie1275e4f2014-04-01 21:49:04 +0000176}
Adrian Prantle8bde9f2014-08-11 22:52:56 +0000177
Adrian Prantl941fa752016-12-05 18:04:47 +0000178/// Compare two fragments based on their offset.
Adrian Prantle8bde9f2014-08-11 22:52:56 +0000179inline bool operator<(const DebugLocEntry::Value &A,
180 const DebugLocEntry::Value &B) {
Adrian Prantl49797ca2016-12-22 05:27:12 +0000181 return A.getExpression()->getFragmentInfo()->OffsetInBits <
182 B.getExpression()->getFragmentInfo()->OffsetInBits;
Adrian Prantle8bde9f2014-08-11 22:52:56 +0000183}
184
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000185}
Adrian Prantle8bde9f2014-08-11 22:52:56 +0000186
David Blaikie1275e4f2014-04-01 21:49:04 +0000187#endif