• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/IR/Constants.h"
15 #include "llvm/IR/DebugInfo.h"
16 #include "llvm/MC/MCSymbol.h"
17 #include "llvm/MC/MachineLocation.h"
18 #include "llvm/Support/Debug.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 {
ValueValue33     Value(const DIExpression *Expr, int64_t i)
34         : Expression(Expr), EntryKind(E_Integer) {
35       Constant.Int = i;
36     }
ValueValue37     Value(const DIExpression *Expr, const ConstantFP *CFP)
38         : Expression(Expr), EntryKind(E_ConstantFP) {
39       Constant.CFP = CFP;
40     }
ValueValue41     Value(const DIExpression *Expr, const ConstantInt *CIP)
42         : Expression(Expr), EntryKind(E_ConstantInt) {
43       Constant.CIP = CIP;
44     }
ValueValue45     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 
isLocationValue67     bool isLocation() const { return EntryKind == E_Location; }
isIntValue68     bool isInt() const { return EntryKind == E_Integer; }
isConstantFPValue69     bool isConstantFP() const { return EntryKind == E_ConstantFP; }
isConstantIntValue70     bool isConstantInt() const { return EntryKind == E_ConstantInt; }
getIntValue71     int64_t getInt() const { return Constant.Int; }
getConstantFPValue72     const ConstantFP *getConstantFP() const { return Constant.CFP; }
getConstantIntValue73     const ConstantInt *getConstantInt() const { return Constant.CIP; }
getLocValue74     MachineLocation getLoc() const { return Loc; }
isBitPieceValue75     bool isBitPiece() const { return getExpression()->isBitPiece(); }
getExpressionValue76     const DIExpression *getExpression() const { return Expression; }
77     friend bool operator==(const Value &, const Value &);
78     friend bool operator<(const Value &, const Value &);
dumpValue79     void dump() const {
80       if (isLocation()) {
81         llvm::dbgs() << "Loc = { reg=" << Loc.getReg() << " ";
82         if (Loc.isIndirect())
83           llvm::dbgs() << '+' << Loc.getOffset();
84         llvm::dbgs() << "} ";
85       }
86       else if (isConstantInt())
87         Constant.CIP->dump();
88       else if (isConstantFP())
89         Constant.CFP->dump();
90       if (Expression)
91         Expression->dump();
92     }
93   };
94 
95 private:
96   /// A nonempty list of locations/constants belonging to this entry,
97   /// sorted by offset.
98   SmallVector<Value, 1> Values;
99 
100 public:
DebugLocEntry(const MCSymbol * B,const MCSymbol * E,Value Val)101   DebugLocEntry(const MCSymbol *B, const MCSymbol *E, Value Val)
102       : Begin(B), End(E) {
103     Values.push_back(std::move(Val));
104   }
105 
106   /// \brief If this and Next are describing different pieces of the same
107   /// variable, merge them by appending Next's values to the current
108   /// list of values.
109   /// Return true if the merge was successful.
110   bool MergeValues(const DebugLocEntry &Next);
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.
MergeRanges(const DebugLocEntry & Next)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 
getBeginSym()125   const MCSymbol *getBeginSym() const { return Begin; }
getEndSym()126   const MCSymbol *getEndSym() const { return End; }
getValues()127   ArrayRef<Value> getValues() const { return Values; }
addValues(ArrayRef<DebugLocEntry::Value> Vals)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.
sortUniqueValues()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, DebugLocStream::ListBuilder &List,
150                 const DIBasicType *BT);
151 };
152 
153 /// \brief Compare two Values for equality.
154 inline bool operator==(const DebugLocEntry::Value &A,
155                        const DebugLocEntry::Value &B) {
156   if (A.EntryKind != B.EntryKind)
157     return false;
158 
159   if (A.Expression != B.Expression)
160     return false;
161 
162   switch (A.EntryKind) {
163   case DebugLocEntry::Value::E_Location:
164     return A.Loc == B.Loc;
165   case DebugLocEntry::Value::E_Integer:
166     return A.Constant.Int == B.Constant.Int;
167   case DebugLocEntry::Value::E_ConstantFP:
168     return A.Constant.CFP == B.Constant.CFP;
169   case DebugLocEntry::Value::E_ConstantInt:
170     return A.Constant.CIP == B.Constant.CIP;
171   }
172   llvm_unreachable("unhandled EntryKind");
173 }
174 
175 /// \brief Compare two pieces based on their offset.
176 inline bool operator<(const DebugLocEntry::Value &A,
177                       const DebugLocEntry::Value &B) {
178   return A.getExpression()->getBitPieceOffset() <
179          B.getExpression()->getBitPieceOffset();
180 }
181 
182 }
183 
184 #endif
185