8cfe39d2ca3fe089c5647423b2aad3c83f002b3b
[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 CODEGEN_ASMPRINTER_DEBUGLOCENTRY_H__
11 #define CODEGEN_ASMPRINTER_DEBUGLOCENTRY_H__
12 #include "llvm/IR/Constants.h"
13 #include "llvm/IR/DebugInfo.h"
14 #include "llvm/MC/MachineLocation.h"
15 #include "llvm/MC/MCSymbol.h"
16
17 namespace llvm {
18 class DwarfCompileUnit;
19 class MDNode;
20 /// \brief This struct describes location entries emitted in the .debug_loc
21 /// section.
22 class DebugLocEntry {
23   // Begin and end symbols for the address range that this location is valid.
24   const MCSymbol *Begin;
25   const MCSymbol *End;
26
27 public:
28   /// A single location or constant.
29   struct Value {
30     Value(const MDNode *Var, int64_t i)
31       : Variable(Var), EntryKind(E_Integer) {
32       Constant.Int = i;
33     }
34     Value(const MDNode *Var, const ConstantFP *CFP)
35       : Variable(Var), EntryKind(E_ConstantFP) {
36       Constant.CFP = CFP;
37     }
38     Value(const MDNode *Var, const ConstantInt *CIP)
39       : Variable(Var), EntryKind(E_ConstantInt) {
40       Constant.CIP = CIP;
41     }
42     Value(const MDNode *Var, MachineLocation Loc)
43       : Variable(Var), EntryKind(E_Location), Loc(Loc) {
44     }
45
46     // The variable to which this location entry corresponds.
47     const MDNode *Variable;
48
49     // Type of entry that this represents.
50     enum EntryType { E_Location, E_Integer, E_ConstantFP, E_ConstantInt };
51     enum EntryType EntryKind;
52
53     // Either a constant,
54     union {
55       int64_t Int;
56       const ConstantFP *CFP;
57       const ConstantInt *CIP;
58     } Constant;
59
60     // Or a location in the machine frame.
61     MachineLocation Loc;
62
63     bool operator==(const Value &other) const {
64       if (EntryKind != other.EntryKind)
65         return false;
66
67       switch (EntryKind) {
68       case E_Location:
69         return Loc == other.Loc;
70       case E_Integer:
71         return Constant.Int == other.Constant.Int;
72       case E_ConstantFP:
73         return Constant.CFP == other.Constant.CFP;
74       case E_ConstantInt:
75         return Constant.CIP == other.Constant.CIP;
76       }
77       llvm_unreachable("unhandled EntryKind");
78     }
79
80     bool isLocation() const { return EntryKind == E_Location; }
81     bool isInt() const { return EntryKind == E_Integer; }
82     bool isConstantFP() const { return EntryKind == E_ConstantFP; }
83     bool isConstantInt() const { return EntryKind == E_ConstantInt; }
84     int64_t getInt() const { return Constant.Int; }
85     const ConstantFP *getConstantFP() const { return Constant.CFP; }
86     const ConstantInt *getConstantInt() const { return Constant.CIP; }
87     MachineLocation getLoc() const { return Loc; }
88     const MDNode *getVariable() const { return Variable; }
89   };
90 private:
91   /// A list of locations/constants belonging to this entry.
92   SmallVector<Value, 1> Values;
93
94   /// The compile unit that this location entry is referenced by.
95   const DwarfCompileUnit *Unit;
96
97 public:
98   DebugLocEntry() : Begin(nullptr), End(nullptr), Unit(nullptr) {}
99   DebugLocEntry(const MCSymbol *B, const MCSymbol *E,
100                 Value Val, const DwarfCompileUnit *U)
101       : Begin(B), End(E), Unit(U) {
102     Values.push_back(std::move(Val));
103   }
104
105   /// \brief Attempt to merge this DebugLocEntry with Next and return
106   /// true if the merge was successful. Entries can be merged if they
107   /// share the same Loc/Constant and if Next immediately follows this
108   /// Entry.
109   bool Merge(const DebugLocEntry &Next) {
110     // If this and Next are describing different pieces of the same
111     // variable, merge them by appending next's values to the current
112     // list of values.
113     if (Begin == Next.Begin && Values.size() > 0 && Next.Values.size() > 0) {
114       DIVariable Var(Values[0].Variable);
115       DIVariable NextVar(Next.Values[0].Variable);
116       if (Var.getName() == NextVar.getName() &&
117           Var.isVariablePiece() && NextVar.isVariablePiece()) {
118         Values.append(Next.Values.begin(), Next.Values.end());
119         End = Next.End;
120         return true;
121       }
122     }
123     // If this and Next are describing the same variable, merge them.
124     if ((End == Next.Begin && Values == Next.Values)) {
125       End = Next.End;
126       return true;
127     }
128     return false;
129   }
130
131   const MCSymbol *getBeginSym() const { return Begin; }
132   const MCSymbol *getEndSym() const { return End; }
133   const DwarfCompileUnit *getCU() const { return Unit; }
134   const ArrayRef<Value> getValues() const { return Values; }
135   void addValue(Value Val) {
136     assert(DIVariable(Val.Variable).isVariablePiece() &&
137            "multi-value DebugLocEntries must be pieces");
138     Values.push_back(Val);
139   }
140 };
141
142 }
143 #endif