blob: c60a958c07374fe75c7d0233ae21c19322918e2a [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
10#ifndef CODEGEN_ASMPRINTER_DEBUGLOCENTRY_H__
11#define CODEGEN_ASMPRINTER_DEBUGLOCENTRY_H__
12#include "llvm/IR/Constants.h"
Adrian Prantlb1416832014-08-01 22:11:58 +000013#include "llvm/IR/DebugInfo.h"
David Blaikie1275e4f2014-04-01 21:49:04 +000014#include "llvm/MC/MachineLocation.h"
15#include "llvm/MC/MCSymbol.h"
16
17namespace llvm {
David Blaikie1275e4f2014-04-01 21:49:04 +000018class MDNode;
19/// \brief This struct describes location entries emitted in the .debug_loc
20/// section.
21class DebugLocEntry {
22 // Begin and end symbols for the address range that this location is valid.
23 const MCSymbol *Begin;
24 const MCSymbol *End;
25
Adrian Prantle19e5ef2014-04-27 18:25:40 +000026public:
27 /// A single location or constant.
28 struct Value {
29 Value(const MDNode *Var, int64_t i)
30 : Variable(Var), EntryKind(E_Integer) {
31 Constant.Int = i;
32 }
33 Value(const MDNode *Var, const ConstantFP *CFP)
34 : Variable(Var), EntryKind(E_ConstantFP) {
35 Constant.CFP = CFP;
36 }
37 Value(const MDNode *Var, const ConstantInt *CIP)
38 : Variable(Var), EntryKind(E_ConstantInt) {
39 Constant.CIP = CIP;
40 }
41 Value(const MDNode *Var, MachineLocation Loc)
42 : Variable(Var), EntryKind(E_Location), Loc(Loc) {
David Blaikied306baf2014-04-01 22:04:07 +000043 }
44
Adrian Prantle19e5ef2014-04-27 18:25:40 +000045 // The variable to which this location entry corresponds.
46 const MDNode *Variable;
47
48 // Type of entry that this represents.
49 enum EntryType { E_Location, E_Integer, E_ConstantFP, E_ConstantInt };
50 enum EntryType EntryKind;
51
52 // Either a constant,
53 union {
54 int64_t Int;
55 const ConstantFP *CFP;
56 const ConstantInt *CIP;
57 } Constant;
58
59 // Or a location in the machine frame.
60 MachineLocation Loc;
61
Adrian Prantle19e5ef2014-04-27 18:25:40 +000062 bool isLocation() const { return EntryKind == E_Location; }
63 bool isInt() const { return EntryKind == E_Integer; }
64 bool isConstantFP() const { return EntryKind == E_ConstantFP; }
65 bool isConstantInt() const { return EntryKind == E_ConstantInt; }
66 int64_t getInt() const { return Constant.Int; }
67 const ConstantFP *getConstantFP() const { return Constant.CFP; }
68 const ConstantInt *getConstantInt() const { return Constant.CIP; }
69 MachineLocation getLoc() const { return Loc; }
70 const MDNode *getVariable() const { return Variable; }
Adrian Prantle8bde9f2014-08-11 22:52:56 +000071 friend bool operator==(const Value &, const Value &);
72 friend bool operator<(const Value &, const Value &);
Adrian Prantle19e5ef2014-04-27 18:25:40 +000073 };
Adrian Prantle8bde9f2014-08-11 22:52:56 +000074
Adrian Prantle19e5ef2014-04-27 18:25:40 +000075private:
Adrian Prantlbe4b5172014-08-11 21:06:03 +000076 /// A nonempty list of locations/constants belonging to this entry,
77 /// sorted by offset.
Adrian Prantle19e5ef2014-04-27 18:25:40 +000078 SmallVector<Value, 1> Values;
79
David Blaikie1275e4f2014-04-01 21:49:04 +000080public:
David Blaikiee1a26a62014-08-05 23:14:16 +000081 DebugLocEntry(const MCSymbol *B, const MCSymbol *E, Value Val)
82 : Begin(B), End(E) {
Adrian Prantle19e5ef2014-04-27 18:25:40 +000083 Values.push_back(std::move(Val));
David Blaikie1275e4f2014-04-01 21:49:04 +000084 }
85
Adrian Prantle09ee3f2014-08-11 20:59:28 +000086 /// \brief If this and Next are describing different pieces of the same
87 // variable, merge them by appending Next's values to the current
88 // list of values.
89 // Return true if the merge was successful.
90 bool MergeValues(const DebugLocEntry &Next) {
Adrian Prantlb1416832014-08-01 22:11:58 +000091 if (Begin == Next.Begin && Values.size() > 0 && Next.Values.size() > 0) {
92 DIVariable Var(Values[0].Variable);
93 DIVariable NextVar(Next.Values[0].Variable);
94 if (Var.getName() == NextVar.getName() &&
95 Var.isVariablePiece() && NextVar.isVariablePiece()) {
Adrian Prantl1c6f2ec2014-08-11 21:06:00 +000096 addValues(Next.Values);
Adrian Prantlb1416832014-08-01 22:11:58 +000097 End = Next.End;
98 return true;
99 }
100 }
Adrian Prantle09ee3f2014-08-11 20:59:28 +0000101 return false;
102 }
103
104 /// \brief Attempt to merge this DebugLocEntry with Next and return
105 /// true if the merge was successful. Entries can be merged if they
106 /// share the same Loc/Constant and if Next immediately follows this
107 /// Entry.
108 bool MergeRanges(const DebugLocEntry &Next) {
Adrian Prantlb1416832014-08-01 22:11:58 +0000109 // If this and Next are describing the same variable, merge them.
Adrian Prantle19e5ef2014-04-27 18:25:40 +0000110 if ((End == Next.Begin && Values == Next.Values)) {
David Blaikie6fa99662014-04-01 23:19:23 +0000111 End = Next.End;
112 return true;
113 }
114 return false;
David Blaikie1275e4f2014-04-01 21:49:04 +0000115 }
Adrian Prantlb1416832014-08-01 22:11:58 +0000116
David Blaikie1275e4f2014-04-01 21:49:04 +0000117 const MCSymbol *getBeginSym() const { return Begin; }
118 const MCSymbol *getEndSym() const { return End; }
Adrian Prantle19e5ef2014-04-27 18:25:40 +0000119 const ArrayRef<Value> getValues() const { return Values; }
Adrian Prantl1c6f2ec2014-08-11 21:06:00 +0000120 void addValues(ArrayRef<DebugLocEntry::Value> Vals) {
121 Values.append(Vals.begin(), Vals.end());
Adrian Prantl293dd932014-08-11 21:05:55 +0000122 sortUniqueValues();
Adrian Prantl1c6f2ec2014-08-11 21:06:00 +0000123 assert(std::all_of(Values.begin(), Values.end(), [](DebugLocEntry::Value V){
124 return DIVariable(V.Variable).isVariablePiece();
125 }) && "value must be a piece");
Adrian Prantl293dd932014-08-11 21:05:55 +0000126 }
127
128 // Sort the pieces by offset.
129 // Remove any duplicate entries by dropping all but the first.
130 void sortUniqueValues() {
131 std::sort(Values.begin(), Values.end());
132 Values.erase(std::unique(Values.begin(), Values.end()), Values.end());
Adrian Prantlb1416832014-08-01 22:11:58 +0000133 }
David Blaikie1275e4f2014-04-01 21:49:04 +0000134};
135
Adrian Prantle8bde9f2014-08-11 22:52:56 +0000136/// Compare two Values for equality.
137inline bool operator==(const DebugLocEntry::Value &A,
138 const DebugLocEntry::Value &B) {
139 if (A.EntryKind != B.EntryKind)
140 return false;
141
142 if (A.getVariable() != B.getVariable())
143 return false;
144
145 switch (A.EntryKind) {
146 case DebugLocEntry::Value::E_Location:
147 return A.Loc == B.Loc;
148 case DebugLocEntry::Value::E_Integer:
149 return A.Constant.Int == B.Constant.Int;
150 case DebugLocEntry::Value::E_ConstantFP:
151 return A.Constant.CFP == B.Constant.CFP;
152 case DebugLocEntry::Value::E_ConstantInt:
153 return A.Constant.CIP == B.Constant.CIP;
154 }
155 llvm_unreachable("unhandled EntryKind");
David Blaikie1275e4f2014-04-01 21:49:04 +0000156}
Adrian Prantle8bde9f2014-08-11 22:52:56 +0000157
158/// Compare two pieces based on their offset.
159inline bool operator<(const DebugLocEntry::Value &A,
160 const DebugLocEntry::Value &B) {
161 DIVariable Var(A.getVariable());
162 DIVariable OtherVar(B.getVariable());
163 return Var.getPieceOffset() < OtherVar.getPieceOffset();
164}
165
166}
167
David Blaikie1275e4f2014-04-01 21:49:04 +0000168#endif