David Blaikie | 1275e4f | 2014-04-01 21:49:04 +0000 | [diff] [blame] | 1 | //===-- 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 Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 10 | #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCENTRY_H |
| 11 | #define LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCENTRY_H |
David Blaikie | 1275e4f | 2014-04-01 21:49:04 +0000 | [diff] [blame] | 12 | #include "llvm/IR/Constants.h" |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 13 | #include "llvm/IR/DebugInfo.h" |
David Blaikie | 1275e4f | 2014-04-01 21:49:04 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCSymbol.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MachineLocation.h" |
David Blaikie | 1275e4f | 2014-04-01 21:49:04 +0000 | [diff] [blame] | 16 | |
| 17 | namespace llvm { |
David Blaikie | 1275e4f | 2014-04-01 21:49:04 +0000 | [diff] [blame] | 18 | class MDNode; |
| 19 | /// \brief This struct describes location entries emitted in the .debug_loc |
| 20 | /// section. |
| 21 | class 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 Prantl | e19e5ef | 2014-04-27 18:25:40 +0000 | [diff] [blame] | 26 | public: |
| 27 | /// A single location or constant. |
| 28 | struct Value { |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 29 | Value(const MDNode *Var, const MDNode *Expr, int64_t i) |
| 30 | : Variable(Var), Expression(Expr), EntryKind(E_Integer) { |
Adrian Prantl | e19e5ef | 2014-04-27 18:25:40 +0000 | [diff] [blame] | 31 | Constant.Int = i; |
| 32 | } |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 33 | Value(const MDNode *Var, const MDNode *Expr, const ConstantFP *CFP) |
| 34 | : Variable(Var), Expression(Expr), EntryKind(E_ConstantFP) { |
Adrian Prantl | e19e5ef | 2014-04-27 18:25:40 +0000 | [diff] [blame] | 35 | Constant.CFP = CFP; |
| 36 | } |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 37 | Value(const MDNode *Var, const MDNode *Expr, const ConstantInt *CIP) |
| 38 | : Variable(Var), Expression(Expr), EntryKind(E_ConstantInt) { |
Adrian Prantl | e19e5ef | 2014-04-27 18:25:40 +0000 | [diff] [blame] | 39 | Constant.CIP = CIP; |
| 40 | } |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 41 | Value(const MDNode *Var, const MDNode *Expr, MachineLocation Loc) |
| 42 | : Variable(Var), Expression(Expr), EntryKind(E_Location), Loc(Loc) { |
| 43 | assert(DIVariable(Var).Verify()); |
| 44 | assert(DIExpression(Expr).Verify()); |
David Blaikie | d306baf | 2014-04-01 22:04:07 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Adrian Prantl | e19e5ef | 2014-04-27 18:25:40 +0000 | [diff] [blame] | 47 | // The variable to which this location entry corresponds. |
| 48 | const MDNode *Variable; |
| 49 | |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 50 | // Any complex address location expression for this Value. |
| 51 | const MDNode *Expression; |
| 52 | |
Adrian Prantl | e19e5ef | 2014-04-27 18:25:40 +0000 | [diff] [blame] | 53 | // Type of entry that this represents. |
| 54 | enum EntryType { E_Location, E_Integer, E_ConstantFP, E_ConstantInt }; |
| 55 | enum EntryType EntryKind; |
| 56 | |
| 57 | // Either a constant, |
| 58 | 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 Prantl | e19e5ef | 2014-04-27 18:25:40 +0000 | [diff] [blame] | 67 | 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 Prantl | 76502d8 | 2014-08-11 23:22:59 +0000 | [diff] [blame] | 75 | const MDNode *getVariableNode() const { return Variable; } |
| 76 | DIVariable getVariable() const { return DIVariable(Variable); } |
Adrian Prantl | 27bd01f | 2015-02-09 23:57:15 +0000 | [diff] [blame] | 77 | bool isBitPiece() const { return getExpression().isBitPiece(); } |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 78 | DIExpression getExpression() const { return DIExpression(Expression); } |
Adrian Prantl | e8bde9f | 2014-08-11 22:52:56 +0000 | [diff] [blame] | 79 | friend bool operator==(const Value &, const Value &); |
| 80 | friend bool operator<(const Value &, const Value &); |
Adrian Prantl | e19e5ef | 2014-04-27 18:25:40 +0000 | [diff] [blame] | 81 | }; |
Adrian Prantl | e8bde9f | 2014-08-11 22:52:56 +0000 | [diff] [blame] | 82 | |
Adrian Prantl | e19e5ef | 2014-04-27 18:25:40 +0000 | [diff] [blame] | 83 | private: |
Adrian Prantl | be4b517 | 2014-08-11 21:06:03 +0000 | [diff] [blame] | 84 | /// A nonempty list of locations/constants belonging to this entry, |
| 85 | /// sorted by offset. |
Adrian Prantl | e19e5ef | 2014-04-27 18:25:40 +0000 | [diff] [blame] | 86 | SmallVector<Value, 1> Values; |
| 87 | |
David Blaikie | 1275e4f | 2014-04-01 21:49:04 +0000 | [diff] [blame] | 88 | public: |
David Blaikie | e1a26a6 | 2014-08-05 23:14:16 +0000 | [diff] [blame] | 89 | DebugLocEntry(const MCSymbol *B, const MCSymbol *E, Value Val) |
| 90 | : Begin(B), End(E) { |
Adrian Prantl | e19e5ef | 2014-04-27 18:25:40 +0000 | [diff] [blame] | 91 | Values.push_back(std::move(Val)); |
David Blaikie | 1275e4f | 2014-04-01 21:49:04 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Adrian Prantl | e09ee3f | 2014-08-11 20:59:28 +0000 | [diff] [blame] | 94 | /// \brief If this and Next are describing different pieces of the same |
| 95 | // variable, merge them by appending Next's values to the current |
| 96 | // list of values. |
| 97 | // Return true if the merge was successful. |
| 98 | bool MergeValues(const DebugLocEntry &Next) { |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 99 | if (Begin == Next.Begin) { |
| 100 | DIExpression Expr(Values[0].Expression); |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 101 | DIVariable Var(Values[0].Variable); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 102 | DIExpression NextExpr(Next.Values[0].Expression); |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 103 | DIVariable NextVar(Next.Values[0].Variable); |
Adrian Prantl | 27bd01f | 2015-02-09 23:57:15 +0000 | [diff] [blame] | 104 | if (Var == NextVar && Expr.isBitPiece() && |
| 105 | NextExpr.isBitPiece()) { |
Adrian Prantl | 1c6f2ec | 2014-08-11 21:06:00 +0000 | [diff] [blame] | 106 | addValues(Next.Values); |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 107 | End = Next.End; |
| 108 | return true; |
| 109 | } |
| 110 | } |
Adrian Prantl | e09ee3f | 2014-08-11 20:59:28 +0000 | [diff] [blame] | 111 | return false; |
| 112 | } |
| 113 | |
| 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 Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 119 | // If this and Next are describing the same variable, merge them. |
Adrian Prantl | e19e5ef | 2014-04-27 18:25:40 +0000 | [diff] [blame] | 120 | if ((End == Next.Begin && Values == Next.Values)) { |
David Blaikie | 6fa9966 | 2014-04-01 23:19:23 +0000 | [diff] [blame] | 121 | End = Next.End; |
| 122 | return true; |
| 123 | } |
| 124 | return false; |
David Blaikie | 1275e4f | 2014-04-01 21:49:04 +0000 | [diff] [blame] | 125 | } |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 126 | |
David Blaikie | 1275e4f | 2014-04-01 21:49:04 +0000 | [diff] [blame] | 127 | const MCSymbol *getBeginSym() const { return Begin; } |
| 128 | const MCSymbol *getEndSym() const { return End; } |
Craig Topper | 3af9722 | 2014-08-27 05:25:00 +0000 | [diff] [blame] | 129 | ArrayRef<Value> getValues() const { return Values; } |
Adrian Prantl | 1c6f2ec | 2014-08-11 21:06:00 +0000 | [diff] [blame] | 130 | void addValues(ArrayRef<DebugLocEntry::Value> Vals) { |
| 131 | Values.append(Vals.begin(), Vals.end()); |
Adrian Prantl | 293dd93 | 2014-08-11 21:05:55 +0000 | [diff] [blame] | 132 | sortUniqueValues(); |
Adrian Prantl | 1c6f2ec | 2014-08-11 21:06:00 +0000 | [diff] [blame] | 133 | assert(std::all_of(Values.begin(), Values.end(), [](DebugLocEntry::Value V){ |
Adrian Prantl | 27bd01f | 2015-02-09 23:57:15 +0000 | [diff] [blame] | 134 | return V.isBitPiece(); |
Adrian Prantl | 1c6f2ec | 2014-08-11 21:06:00 +0000 | [diff] [blame] | 135 | }) && "value must be a piece"); |
Adrian Prantl | 293dd93 | 2014-08-11 21:05:55 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // Sort the pieces by offset. |
| 139 | // Remove any duplicate entries by dropping all but the first. |
| 140 | void sortUniqueValues() { |
| 141 | std::sort(Values.begin(), Values.end()); |
Adrian Prantl | 9724b5c | 2014-08-12 01:07:53 +0000 | [diff] [blame] | 142 | Values.erase(std::unique(Values.begin(), Values.end(), |
| 143 | [](const Value &A, const Value &B) { |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 144 | return A.getVariable() == B.getVariable() && |
| 145 | A.getExpression() == B.getExpression(); |
| 146 | }), |
| 147 | Values.end()); |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 148 | } |
David Blaikie | 1275e4f | 2014-04-01 21:49:04 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
Adrian Prantl | e8bde9f | 2014-08-11 22:52:56 +0000 | [diff] [blame] | 151 | /// Compare two Values for equality. |
| 152 | inline bool operator==(const DebugLocEntry::Value &A, |
| 153 | const DebugLocEntry::Value &B) { |
| 154 | if (A.EntryKind != B.EntryKind) |
| 155 | return false; |
| 156 | |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 157 | if (A.Expression != B.Expression) |
| 158 | return false; |
| 159 | |
| 160 | if (A.Variable != B.Variable) |
Adrian Prantl | e8bde9f | 2014-08-11 22:52:56 +0000 | [diff] [blame] | 161 | return false; |
| 162 | |
| 163 | switch (A.EntryKind) { |
| 164 | case DebugLocEntry::Value::E_Location: |
| 165 | return A.Loc == B.Loc; |
| 166 | case DebugLocEntry::Value::E_Integer: |
| 167 | return A.Constant.Int == B.Constant.Int; |
| 168 | case DebugLocEntry::Value::E_ConstantFP: |
| 169 | return A.Constant.CFP == B.Constant.CFP; |
| 170 | case DebugLocEntry::Value::E_ConstantInt: |
| 171 | return A.Constant.CIP == B.Constant.CIP; |
| 172 | } |
| 173 | llvm_unreachable("unhandled EntryKind"); |
David Blaikie | 1275e4f | 2014-04-01 21:49:04 +0000 | [diff] [blame] | 174 | } |
Adrian Prantl | e8bde9f | 2014-08-11 22:52:56 +0000 | [diff] [blame] | 175 | |
| 176 | /// Compare two pieces based on their offset. |
| 177 | inline bool operator<(const DebugLocEntry::Value &A, |
| 178 | const DebugLocEntry::Value &B) { |
Adrian Prantl | 27bd01f | 2015-02-09 23:57:15 +0000 | [diff] [blame] | 179 | return A.getExpression().getBitPieceOffset() < |
| 180 | B.getExpression().getBitPieceOffset(); |
Adrian Prantl | e8bde9f | 2014-08-11 22:52:56 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | } |
| 184 | |
David Blaikie | 1275e4f | 2014-04-01 21:49:04 +0000 | [diff] [blame] | 185 | #endif |