AsmPrinter: Stop storing MDLocalVariable in DebugLocEntry
[oota-llvm.git] / lib / CodeGen / AsmPrinter / DebugLocEntry.h
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
10 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCENTRY_H
11 #define LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCENTRY_H
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/IR/Constants.h"
14 #include "llvm/IR/DebugInfo.h"
15 #include "llvm/MC/MCSymbol.h"
16 #include "llvm/MC/MachineLocation.h"
17
18 namespace llvm {
19 class AsmPrinter;
20 class MDNode;
21 /// \brief This struct describes location entries emitted in the .debug_loc
22 /// section.
23 class DebugLocEntry {
24   /// Begin and end symbols for the address range that this location is valid.
25   const MCSymbol *Begin;
26   const MCSymbol *End;
27
28 public:
29   /// \brief A single location or constant.
30   struct Value {
31     Value(const MDNode *Expr, int64_t i)
32         : Expression(Expr), EntryKind(E_Integer) {
33       Constant.Int = i;
34     }
35     Value(const MDNode *Expr, const ConstantFP *CFP)
36         : Expression(Expr), EntryKind(E_ConstantFP) {
37       Constant.CFP = CFP;
38     }
39     Value(const MDNode *Expr, const ConstantInt *CIP)
40         : Expression(Expr), EntryKind(E_ConstantInt) {
41       Constant.CIP = CIP;
42     }
43     Value(const MDNode *Expr, MachineLocation Loc)
44         : Expression(Expr), EntryKind(E_Location), Loc(Loc) {
45       assert(cast<MDExpression>(Expr)->isValid());
46     }
47
48     /// Any complex address location expression for this Value.
49     const MDNode *Expression;
50
51     /// Type of entry that this represents.
52     enum EntryType { E_Location, E_Integer, E_ConstantFP, E_ConstantInt };
53     enum EntryType EntryKind;
54
55     /// Either a constant,
56     union {
57       int64_t Int;
58       const ConstantFP *CFP;
59       const ConstantInt *CIP;
60     } Constant;
61
62     // Or a location in the machine frame.
63     MachineLocation Loc;
64
65     bool isLocation() const { return EntryKind == E_Location; }
66     bool isInt() const { return EntryKind == E_Integer; }
67     bool isConstantFP() const { return EntryKind == E_ConstantFP; }
68     bool isConstantInt() const { return EntryKind == E_ConstantInt; }
69     int64_t getInt() const { return Constant.Int; }
70     const ConstantFP *getConstantFP() const { return Constant.CFP; }
71     const ConstantInt *getConstantInt() const { return Constant.CIP; }
72     MachineLocation getLoc() const { return Loc; }
73     bool isBitPiece() const { return getExpression()->isBitPiece(); }
74     DIExpression getExpression() const {
75       return cast_or_null<MDExpression>(Expression);
76     }
77     friend bool operator==(const Value &, const Value &);
78     friend bool operator<(const Value &, const Value &);
79   };
80
81 private:
82   /// A nonempty list of locations/constants belonging to this entry,
83   /// sorted by offset.
84   SmallVector<Value, 1> Values;
85   SmallString<8> DWARFBytes;
86   SmallVector<std::string, 1> Comments;
87
88 public:
89   DebugLocEntry(const MCSymbol *B, const MCSymbol *E, Value Val)
90       : Begin(B), End(E) {
91     Values.push_back(std::move(Val));
92   }
93
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) {
99     if (Begin == Next.Begin) {
100       DIExpression Expr = cast_or_null<MDExpression>(Values[0].Expression);
101       DIExpression NextExpr =
102           cast_or_null<MDExpression>(Next.Values[0].Expression);
103       if (Expr->isBitPiece() && NextExpr->isBitPiece()) {
104         addValues(Next.Values);
105         End = Next.End;
106         return true;
107       }
108     }
109     return false;
110   }
111
112   /// \brief Attempt to merge this DebugLocEntry with Next and return
113   /// true if the merge was successful. Entries can be merged if they
114   /// share the same Loc/Constant and if Next immediately follows this
115   /// Entry.
116   bool MergeRanges(const DebugLocEntry &Next) {
117     // If this and Next are describing the same variable, merge them.
118     if ((End == Next.Begin && Values == Next.Values)) {
119       End = Next.End;
120       return true;
121     }
122     return false;
123   }
124
125   const MCSymbol *getBeginSym() const { return Begin; }
126   const MCSymbol *getEndSym() const { return End; }
127   ArrayRef<Value> getValues() const { return Values; }
128   void addValues(ArrayRef<DebugLocEntry::Value> Vals) {
129     Values.append(Vals.begin(), Vals.end());
130     sortUniqueValues();
131     assert(std::all_of(Values.begin(), Values.end(), [](DebugLocEntry::Value V){
132           return V.isBitPiece();
133         }) && "value must be a piece");
134   }
135
136   // \brief Sort the pieces by offset.
137   // Remove any duplicate entries by dropping all but the first.
138   void sortUniqueValues() {
139     std::sort(Values.begin(), Values.end());
140     Values.erase(
141         std::unique(
142             Values.begin(), Values.end(), [](const Value &A, const Value &B) {
143               return A.getExpression() == B.getExpression();
144             }),
145         Values.end());
146   }
147
148   /// \brief Lower this entry into a DWARF expression.
149   void finalize(const AsmPrinter &AP, const MDBasicType *TypeIdentifierMap);
150
151   /// \brief Return the lowered DWARF expression.
152   StringRef getDWARFBytes() const { return DWARFBytes; }
153   /// \brief Return the assembler comments for the lowered DWARF expression.
154   const SmallVectorImpl<std::string> &getComments() const { return Comments; }
155 };
156
157 /// \brief Compare two Values for equality.
158 inline bool operator==(const DebugLocEntry::Value &A,
159                        const DebugLocEntry::Value &B) {
160   if (A.EntryKind != B.EntryKind)
161     return false;
162
163   if (A.Expression != B.Expression)
164     return false;
165
166   switch (A.EntryKind) {
167   case DebugLocEntry::Value::E_Location:
168     return A.Loc == B.Loc;
169   case DebugLocEntry::Value::E_Integer:
170     return A.Constant.Int == B.Constant.Int;
171   case DebugLocEntry::Value::E_ConstantFP:
172     return A.Constant.CFP == B.Constant.CFP;
173   case DebugLocEntry::Value::E_ConstantInt:
174     return A.Constant.CIP == B.Constant.CIP;
175   }
176   llvm_unreachable("unhandled EntryKind");
177 }
178
179 /// \brief Compare two pieces based on their offset.
180 inline bool operator<(const DebugLocEntry::Value &A,
181                       const DebugLocEntry::Value &B) {
182   return A.getExpression()->getBitPieceOffset() <
183          B.getExpression()->getBitPieceOffset();
184 }
185
186 }
187
188 #endif