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