Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / lib / CodeGen / AsmPrinter / DebugLocEntry.h
blob40360ba4d34abb65e580c1055c9be07cc018bf23
1 //===-- llvm/CodeGen/DebugLocEntry.h - Entry in debug_loc list -*- C++ -*--===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCENTRY_H
10 #define LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCENTRY_H
12 #include "DebugLocStream.h"
13 #include "llvm/Config/llvm-config.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"
20 namespace llvm {
21 class AsmPrinter;
23 /// 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;
30 public:
31 /// 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;
37 Value(const DIExpression *Expr, const ConstantFP *CFP)
38 : Expression(Expr), EntryKind(E_ConstantFP) {
39 Constant.CFP = CFP;
41 Value(const DIExpression *Expr, const ConstantInt *CIP)
42 : Expression(Expr), EntryKind(E_ConstantInt) {
43 Constant.CIP = CIP;
45 Value(const DIExpression *Expr, MachineLocation Loc)
46 : Expression(Expr), EntryKind(E_Location), Loc(Loc) {
47 assert(cast<DIExpression>(Expr)->isValid());
50 /// Any complex address location expression for this Value.
51 const DIExpression *Expression;
53 /// Type of entry that this represents.
54 enum EntryType { E_Location, E_Integer, E_ConstantFP, E_ConstantInt };
55 enum EntryType EntryKind;
57 /// Either a constant,
58 union {
59 int64_t Int;
60 const ConstantFP *CFP;
61 const ConstantInt *CIP;
62 } Constant;
64 // Or a location in the machine frame.
65 MachineLocation Loc;
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 isFragment() const { return getExpression()->isFragment(); }
76 const DIExpression *getExpression() const { return Expression; }
77 friend bool operator==(const Value &, const Value &);
78 friend bool operator<(const Value &, const Value &);
79 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
80 LLVM_DUMP_METHOD void dump() const {
81 if (isLocation()) {
82 llvm::dbgs() << "Loc = { reg=" << Loc.getReg() << " ";
83 if (Loc.isIndirect())
84 llvm::dbgs() << "+0";
85 llvm::dbgs() << "} ";
87 else if (isConstantInt())
88 Constant.CIP->dump();
89 else if (isConstantFP())
90 Constant.CFP->dump();
91 if (Expression)
92 Expression->dump();
94 #endif
97 private:
98 /// A nonempty list of locations/constants belonging to this entry,
99 /// sorted by offset.
100 SmallVector<Value, 1> Values;
102 public:
103 DebugLocEntry(const MCSymbol *B, const MCSymbol *E, Value Val)
104 : Begin(B), End(E) {
105 Values.push_back(std::move(Val));
108 /// If this and Next are describing different pieces of the same
109 /// variable, merge them by appending Next's values to the current
110 /// list of values.
111 /// Return true if the merge was successful.
112 bool MergeValues(const DebugLocEntry &Next);
114 /// Attempt to merge this DebugLocEntry with Next and return
115 /// true if the merge was successful. Entries can be merged if they
116 /// share the same Loc/Constant and if Next immediately follows this
117 /// Entry.
118 bool MergeRanges(const DebugLocEntry &Next) {
119 // If this and Next are describing the same variable, merge them.
120 if ((End == Next.Begin && Values == Next.Values)) {
121 End = Next.End;
122 return true;
124 return false;
127 const MCSymbol *getBeginSym() const { return Begin; }
128 const MCSymbol *getEndSym() const { return End; }
129 ArrayRef<Value> getValues() const { return Values; }
130 void addValues(ArrayRef<DebugLocEntry::Value> Vals) {
131 Values.append(Vals.begin(), Vals.end());
132 sortUniqueValues();
133 assert(all_of(Values, [](DebugLocEntry::Value V) {
134 return V.isFragment();
135 }) && "value must be a piece");
138 // Sort the pieces by offset.
139 // Remove any duplicate entries by dropping all but the first.
140 void sortUniqueValues() {
141 llvm::sort(Values);
142 Values.erase(
143 std::unique(
144 Values.begin(), Values.end(), [](const Value &A, const Value &B) {
145 return A.getExpression() == B.getExpression();
147 Values.end());
150 /// Lower this entry into a DWARF expression.
151 void finalize(const AsmPrinter &AP, DebugLocStream::ListBuilder &List,
152 const DIBasicType *BT);
155 /// Compare two Values for equality.
156 inline bool operator==(const DebugLocEntry::Value &A,
157 const DebugLocEntry::Value &B) {
158 if (A.EntryKind != B.EntryKind)
159 return false;
161 if (A.Expression != B.Expression)
162 return false;
164 switch (A.EntryKind) {
165 case DebugLocEntry::Value::E_Location:
166 return A.Loc == B.Loc;
167 case DebugLocEntry::Value::E_Integer:
168 return A.Constant.Int == B.Constant.Int;
169 case DebugLocEntry::Value::E_ConstantFP:
170 return A.Constant.CFP == B.Constant.CFP;
171 case DebugLocEntry::Value::E_ConstantInt:
172 return A.Constant.CIP == B.Constant.CIP;
174 llvm_unreachable("unhandled EntryKind");
177 /// Compare two fragments based on their offset.
178 inline bool operator<(const DebugLocEntry::Value &A,
179 const DebugLocEntry::Value &B) {
180 return A.getExpression()->getFragmentInfo()->OffsetInBits <
181 B.getExpression()->getFragmentInfo()->OffsetInBits;
186 #endif