AsmPrinter: Create a unified .debug_loc stream
[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 DebugLocStream;
21
22 /// \brief This struct describes location entries emitted in the .debug_loc
23 /// section.
24 class DebugLocEntry {
25   /// Begin and end symbols for the address range that this location is valid.
26   const MCSymbol *Begin;
27   const MCSymbol *End;
28
29 public:
30   /// \brief A single location or constant.
31   struct Value {
32     Value(const MDExpression *Expr, int64_t i)
33         : Expression(Expr), EntryKind(E_Integer) {
34       Constant.Int = i;
35     }
36     Value(const MDExpression *Expr, const ConstantFP *CFP)
37         : Expression(Expr), EntryKind(E_ConstantFP) {
38       Constant.CFP = CFP;
39     }
40     Value(const MDExpression *Expr, const ConstantInt *CIP)
41         : Expression(Expr), EntryKind(E_ConstantInt) {
42       Constant.CIP = CIP;
43     }
44     Value(const MDExpression *Expr, MachineLocation Loc)
45         : Expression(Expr), EntryKind(E_Location), Loc(Loc) {
46       assert(cast<MDExpression>(Expr)->isValid());
47     }
48
49     /// Any complex address location expression for this Value.
50     const MDExpression *Expression;
51
52     /// Type of entry that this represents.
53     enum EntryType { E_Location, E_Integer, E_ConstantFP, E_ConstantInt };
54     enum EntryType EntryKind;
55
56     /// Either a constant,
57     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
66     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; }
74     bool isBitPiece() const { return getExpression()->isBitPiece(); }
75     DIExpression getExpression() const { return Expression; }
76     friend bool operator==(const Value &, const Value &);
77     friend bool operator<(const Value &, const Value &);
78   };
79
80 private:
81   /// A nonempty list of locations/constants belonging to this entry,
82   /// sorted by offset.
83   SmallVector<Value, 1> Values;
84
85 public:
86   DebugLocEntry(const MCSymbol *B, const MCSymbol *E, Value Val)
87       : Begin(B), End(E) {
88     Values.push_back(std::move(Val));
89   }
90
91   /// \brief If this and Next are describing different pieces of the same
92   /// variable, merge them by appending Next's values to the current
93   /// list of values.
94   /// Return true if the merge was successful.
95   bool MergeValues(const DebugLocEntry &Next) {
96     if (Begin == Next.Begin) {
97       DIExpression Expr = cast_or_null<MDExpression>(Values[0].Expression);
98       DIExpression NextExpr =
99           cast_or_null<MDExpression>(Next.Values[0].Expression);
100       if (Expr->isBitPiece() && NextExpr->isBitPiece()) {
101         addValues(Next.Values);
102         End = Next.End;
103         return true;
104       }
105     }
106     return false;
107   }
108
109   /// \brief Attempt to merge this DebugLocEntry with Next and return
110   /// true if the merge was successful. Entries can be merged if they
111   /// share the same Loc/Constant and if Next immediately follows this
112   /// Entry.
113   bool MergeRanges(const DebugLocEntry &Next) {
114     // If this and Next are describing the same variable, merge them.
115     if ((End == Next.Begin && Values == Next.Values)) {
116       End = Next.End;
117       return true;
118     }
119     return false;
120   }
121
122   const MCSymbol *getBeginSym() const { return Begin; }
123   const MCSymbol *getEndSym() const { return End; }
124   ArrayRef<Value> getValues() const { return Values; }
125   void addValues(ArrayRef<DebugLocEntry::Value> Vals) {
126     Values.append(Vals.begin(), Vals.end());
127     sortUniqueValues();
128     assert(std::all_of(Values.begin(), Values.end(), [](DebugLocEntry::Value V){
129           return V.isBitPiece();
130         }) && "value must be a piece");
131   }
132
133   // \brief Sort the pieces by offset.
134   // Remove any duplicate entries by dropping all but the first.
135   void sortUniqueValues() {
136     std::sort(Values.begin(), Values.end());
137     Values.erase(
138         std::unique(
139             Values.begin(), Values.end(), [](const Value &A, const Value &B) {
140               return A.getExpression() == B.getExpression();
141             }),
142         Values.end());
143   }
144
145   /// \brief Lower this entry into a DWARF expression.
146   void finalize(const AsmPrinter &AP, DebugLocStream &Locs,
147                 const MDBasicType *BT);
148 };
149
150 /// \brief Compare two Values for equality.
151 inline bool operator==(const DebugLocEntry::Value &A,
152                        const DebugLocEntry::Value &B) {
153   if (A.EntryKind != B.EntryKind)
154     return false;
155
156   if (A.Expression != B.Expression)
157     return false;
158
159   switch (A.EntryKind) {
160   case DebugLocEntry::Value::E_Location:
161     return A.Loc == B.Loc;
162   case DebugLocEntry::Value::E_Integer:
163     return A.Constant.Int == B.Constant.Int;
164   case DebugLocEntry::Value::E_ConstantFP:
165     return A.Constant.CFP == B.Constant.CFP;
166   case DebugLocEntry::Value::E_ConstantInt:
167     return A.Constant.CIP == B.Constant.CIP;
168   }
169   llvm_unreachable("unhandled EntryKind");
170 }
171
172 /// \brief Compare two pieces based on their offset.
173 inline bool operator<(const DebugLocEntry::Value &A,
174                       const DebugLocEntry::Value &B) {
175   return A.getExpression()->getBitPieceOffset() <
176          B.getExpression()->getBitPieceOffset();
177 }
178
179 }
180
181 #endif