blob: 6a943c64ea22f7be27622d77d7ba825d5f5e724b [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;
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000020class DebugLocStream;
Duncan P. N. Exon Smithc0f7dd72015-04-17 16:36:10 +000021
David Blaikie1275e4f2014-04-01 21:49:04 +000022/// \brief This struct describes location entries emitted in the .debug_loc
23/// section.
24class DebugLocEntry {
Adrian Prantl92da14b2015-03-02 22:02:33 +000025 /// Begin and end symbols for the address range that this location is valid.
David Blaikie1275e4f2014-04-01 21:49:04 +000026 const MCSymbol *Begin;
27 const MCSymbol *End;
28
Adrian Prantle19e5ef2014-04-27 18:25:40 +000029public:
Adrian Prantl92da14b2015-03-02 22:02:33 +000030 /// \brief A single location or constant.
Adrian Prantle19e5ef2014-04-27 18:25:40 +000031 struct Value {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000032 Value(const DIExpression *Expr, int64_t i)
Duncan P. N. Exon Smith546c8be2015-04-17 16:33:37 +000033 : Expression(Expr), EntryKind(E_Integer) {
Adrian Prantle19e5ef2014-04-27 18:25:40 +000034 Constant.Int = i;
35 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000036 Value(const DIExpression *Expr, const ConstantFP *CFP)
Duncan P. N. Exon Smith546c8be2015-04-17 16:33:37 +000037 : Expression(Expr), EntryKind(E_ConstantFP) {
Adrian Prantle19e5ef2014-04-27 18:25:40 +000038 Constant.CFP = CFP;
39 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000040 Value(const DIExpression *Expr, const ConstantInt *CIP)
Duncan P. N. Exon Smith546c8be2015-04-17 16:33:37 +000041 : Expression(Expr), EntryKind(E_ConstantInt) {
Adrian Prantle19e5ef2014-04-27 18:25:40 +000042 Constant.CIP = CIP;
43 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000044 Value(const DIExpression *Expr, MachineLocation Loc)
Duncan P. N. Exon Smith546c8be2015-04-17 16:33:37 +000045 : Expression(Expr), EntryKind(E_Location), Loc(Loc) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000046 assert(cast<DIExpression>(Expr)->isValid());
David Blaikied306baf2014-04-01 22:04:07 +000047 }
48
Adrian Prantl92da14b2015-03-02 22:02:33 +000049 /// Any complex address location expression for this Value.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000050 const DIExpression *Expression;
Adrian Prantl87b7eb92014-10-01 18:55:02 +000051
Adrian Prantl92da14b2015-03-02 22:02:33 +000052 /// Type of entry that this represents.
Adrian Prantle19e5ef2014-04-27 18:25:40 +000053 enum EntryType { E_Location, E_Integer, E_ConstantFP, E_ConstantInt };
54 enum EntryType EntryKind;
55
Adrian Prantl92da14b2015-03-02 22:02:33 +000056 /// Either a constant,
Adrian Prantle19e5ef2014-04-27 18:25:40 +000057 union {
58 int64_t Int;
59 const ConstantFP *CFP;
60 const ConstantInt *CIP;
61 } Constant;
62
63 // Or a location in the machine frame.
64 MachineLocation Loc;
65
Adrian Prantle19e5ef2014-04-27 18:25:40 +000066 bool isLocation() const { return EntryKind == E_Location; }
67 bool isInt() const { return EntryKind == E_Integer; }
68 bool isConstantFP() const { return EntryKind == E_ConstantFP; }
69 bool isConstantInt() const { return EntryKind == E_ConstantInt; }
70 int64_t getInt() const { return Constant.Int; }
71 const ConstantFP *getConstantFP() const { return Constant.CFP; }
72 const ConstantInt *getConstantInt() const { return Constant.CIP; }
73 MachineLocation getLoc() const { return Loc; }
Duncan P. N. Exon Smith6a0320a2015-04-14 01:12:42 +000074 bool isBitPiece() const { return getExpression()->isBitPiece(); }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000075 const DIExpression *getExpression() const { return Expression; }
Adrian Prantle8bde9f2014-08-11 22:52:56 +000076 friend bool operator==(const Value &, const Value &);
77 friend bool operator<(const Value &, const Value &);
Adrian Prantle19e5ef2014-04-27 18:25:40 +000078 };
Adrian Prantle8bde9f2014-08-11 22:52:56 +000079
Adrian Prantle19e5ef2014-04-27 18:25:40 +000080private:
Adrian Prantlbe4b5172014-08-11 21:06:03 +000081 /// A nonempty list of locations/constants belonging to this entry,
82 /// sorted by offset.
Adrian Prantle19e5ef2014-04-27 18:25:40 +000083 SmallVector<Value, 1> Values;
84
David Blaikie1275e4f2014-04-01 21:49:04 +000085public:
David Blaikiee1a26a62014-08-05 23:14:16 +000086 DebugLocEntry(const MCSymbol *B, const MCSymbol *E, Value Val)
87 : Begin(B), End(E) {
Adrian Prantle19e5ef2014-04-27 18:25:40 +000088 Values.push_back(std::move(Val));
David Blaikie1275e4f2014-04-01 21:49:04 +000089 }
90
Adrian Prantle09ee3f2014-08-11 20:59:28 +000091 /// \brief If this and Next are describing different pieces of the same
Adrian Prantl92da14b2015-03-02 22:02:33 +000092 /// variable, merge them by appending Next's values to the current
93 /// list of values.
94 /// Return true if the merge was successful.
Adrian Prantle09ee3f2014-08-11 20:59:28 +000095 bool MergeValues(const DebugLocEntry &Next) {
Adrian Prantl87b7eb92014-10-01 18:55:02 +000096 if (Begin == Next.Begin) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000097 auto *Expr = cast_or_null<DIExpression>(Values[0].Expression);
98 auto *NextExpr = cast_or_null<DIExpression>(Next.Values[0].Expression);
Duncan P. N. Exon Smith546c8be2015-04-17 16:33:37 +000099 if (Expr->isBitPiece() && NextExpr->isBitPiece()) {
Adrian Prantl1c6f2ec2014-08-11 21:06:00 +0000100 addValues(Next.Values);
Adrian Prantlb1416832014-08-01 22:11:58 +0000101 End = Next.End;
102 return true;
103 }
104 }
Adrian Prantle09ee3f2014-08-11 20:59:28 +0000105 return false;
106 }
107
108 /// \brief Attempt to merge this DebugLocEntry with Next and return
109 /// true if the merge was successful. Entries can be merged if they
110 /// share the same Loc/Constant and if Next immediately follows this
111 /// Entry.
112 bool MergeRanges(const DebugLocEntry &Next) {
Adrian Prantlb1416832014-08-01 22:11:58 +0000113 // If this and Next are describing the same variable, merge them.
Adrian Prantle19e5ef2014-04-27 18:25:40 +0000114 if ((End == Next.Begin && Values == Next.Values)) {
David Blaikie6fa99662014-04-01 23:19:23 +0000115 End = Next.End;
116 return true;
117 }
118 return false;
David Blaikie1275e4f2014-04-01 21:49:04 +0000119 }
Adrian Prantlb1416832014-08-01 22:11:58 +0000120
David Blaikie1275e4f2014-04-01 21:49:04 +0000121 const MCSymbol *getBeginSym() const { return Begin; }
122 const MCSymbol *getEndSym() const { return End; }
Craig Topper3af97222014-08-27 05:25:00 +0000123 ArrayRef<Value> getValues() const { return Values; }
Adrian Prantl1c6f2ec2014-08-11 21:06:00 +0000124 void addValues(ArrayRef<DebugLocEntry::Value> Vals) {
125 Values.append(Vals.begin(), Vals.end());
Adrian Prantl293dd932014-08-11 21:05:55 +0000126 sortUniqueValues();
Adrian Prantl1c6f2ec2014-08-11 21:06:00 +0000127 assert(std::all_of(Values.begin(), Values.end(), [](DebugLocEntry::Value V){
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000128 return V.isBitPiece();
Adrian Prantl1c6f2ec2014-08-11 21:06:00 +0000129 }) && "value must be a piece");
Adrian Prantl293dd932014-08-11 21:05:55 +0000130 }
131
Adrian Prantl92da14b2015-03-02 22:02:33 +0000132 // \brief Sort the pieces by offset.
Adrian Prantl293dd932014-08-11 21:05:55 +0000133 // Remove any duplicate entries by dropping all but the first.
134 void sortUniqueValues() {
135 std::sort(Values.begin(), Values.end());
Duncan P. N. Exon Smith546c8be2015-04-17 16:33:37 +0000136 Values.erase(
137 std::unique(
138 Values.begin(), Values.end(), [](const Value &A, const Value &B) {
139 return A.getExpression() == B.getExpression();
140 }),
141 Values.end());
Adrian Prantlb1416832014-08-01 22:11:58 +0000142 }
Adrian Prantl92da14b2015-03-02 22:02:33 +0000143
144 /// \brief Lower this entry into a DWARF expression.
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +0000145 void finalize(const AsmPrinter &AP, DebugLocStream &Locs,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000146 const DIBasicType *BT);
David Blaikie1275e4f2014-04-01 21:49:04 +0000147};
148
Adrian Prantl92da14b2015-03-02 22:02:33 +0000149/// \brief Compare two Values for equality.
Adrian Prantle8bde9f2014-08-11 22:52:56 +0000150inline bool operator==(const DebugLocEntry::Value &A,
151 const DebugLocEntry::Value &B) {
152 if (A.EntryKind != B.EntryKind)
153 return false;
154
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000155 if (A.Expression != B.Expression)
156 return false;
157
Adrian Prantle8bde9f2014-08-11 22:52:56 +0000158 switch (A.EntryKind) {
159 case DebugLocEntry::Value::E_Location:
160 return A.Loc == B.Loc;
161 case DebugLocEntry::Value::E_Integer:
162 return A.Constant.Int == B.Constant.Int;
163 case DebugLocEntry::Value::E_ConstantFP:
164 return A.Constant.CFP == B.Constant.CFP;
165 case DebugLocEntry::Value::E_ConstantInt:
166 return A.Constant.CIP == B.Constant.CIP;
167 }
168 llvm_unreachable("unhandled EntryKind");
David Blaikie1275e4f2014-04-01 21:49:04 +0000169}
Adrian Prantle8bde9f2014-08-11 22:52:56 +0000170
Adrian Prantl92da14b2015-03-02 22:02:33 +0000171/// \brief Compare two pieces based on their offset.
Adrian Prantle8bde9f2014-08-11 22:52:56 +0000172inline bool operator<(const DebugLocEntry::Value &A,
173 const DebugLocEntry::Value &B) {
Duncan P. N. Exon Smith6a0320a2015-04-14 01:12:42 +0000174 return A.getExpression()->getBitPieceOffset() <
175 B.getExpression()->getBitPieceOffset();
Adrian Prantle8bde9f2014-08-11 22:52:56 +0000176}
177
178}
179
David Blaikie1275e4f2014-04-01 21:49:04 +0000180#endif