1 //===- llvm/CodeGen/DbgEntityHistoryCalculator.h ----------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CODEGEN_DBGVALUEHISTORYCALCULATOR_H
10 #define LLVM_CODEGEN_DBGVALUEHISTORYCALCULATOR_H
12 #include "llvm/ADT/MapVector.h"
13 #include "llvm/ADT/PointerIntPair.h"
14 #include "llvm/ADT/SmallVector.h"
19 class DILocalVariable;
22 class MachineFunction;
24 class TargetRegisterInfo;
26 /// For each user variable, keep a list of instruction ranges where this
27 /// variable is accessible. The variables are listed in order of appearance.
28 class DbgValueHistoryMap {
30 /// Index in the entry vector.
31 typedef size_t EntryIndex;
33 /// Special value to indicate that an entry is valid until the end of the
35 static const EntryIndex NoEntry = std::numeric_limits<EntryIndex>::max();
37 /// Specifies a change in a variable's debug value history.
39 /// There exist two types of entries:
41 /// * Debug value entry:
43 /// A new debug value becomes live. If the entry's \p EndIndex is \p NoEntry,
44 /// the value is valid until the end of the function. For other values, the
45 /// index points to the entry in the entry vector that ends this debug
46 /// value. The ending entry can either be an overlapping debug value, or
47 /// an instruction that clobbers the value.
49 /// * Clobbering entry:
51 /// This entry's instruction clobbers one or more preceding
52 /// register-described debug values that have their end index
53 /// set to this entry's position in the entry vector.
56 enum EntryKind { DbgValue, Clobber };
58 Entry(const MachineInstr *Instr, EntryKind Kind)
59 : Instr(Instr, Kind), EndIndex(NoEntry) {}
61 const MachineInstr *getInstr() const { return Instr.getPointer(); }
62 EntryIndex getEndIndex() const { return EndIndex; }
63 EntryKind getEntryKind() const { return Instr.getInt(); }
65 bool isClobber() const { return getEntryKind() == Clobber; }
66 bool isDbgValue() const { return getEntryKind() == DbgValue; }
67 bool isClosed() const { return EndIndex != NoEntry; }
69 void endEntry(EntryIndex EndIndex);
72 PointerIntPair<const MachineInstr *, 1, EntryKind> Instr;
75 using Entries = SmallVector<Entry, 4>;
76 using InlinedEntity = std::pair<const DINode *, const DILocation *>;
77 using EntriesMap = MapVector<InlinedEntity, Entries>;
80 EntriesMap VarEntries;
83 bool startDbgValue(InlinedEntity Var, const MachineInstr &MI,
84 EntryIndex &NewIndex);
85 EntryIndex startClobber(InlinedEntity Var, const MachineInstr &MI);
87 Entry &getEntry(InlinedEntity Var, EntryIndex Index) {
88 auto &Entries = VarEntries[Var];
89 return Entries[Index];
92 bool empty() const { return VarEntries.empty(); }
93 void clear() { VarEntries.clear(); }
94 EntriesMap::const_iterator begin() const { return VarEntries.begin(); }
95 EntriesMap::const_iterator end() const { return VarEntries.end(); }
97 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
98 LLVM_DUMP_METHOD void dump() const;
102 /// For each inlined instance of a source-level label, keep the corresponding
103 /// DBG_LABEL instruction. The DBG_LABEL instruction could be used to generate
104 /// a temporary (assembler) label before it.
105 class DbgLabelInstrMap {
107 using InlinedEntity = std::pair<const DINode *, const DILocation *>;
108 using InstrMap = MapVector<InlinedEntity, const MachineInstr *>;
114 void addInstr(InlinedEntity Label, const MachineInstr &MI);
116 bool empty() const { return LabelInstr.empty(); }
117 void clear() { LabelInstr.clear(); }
118 InstrMap::const_iterator begin() const { return LabelInstr.begin(); }
119 InstrMap::const_iterator end() const { return LabelInstr.end(); }
122 void calculateDbgEntityHistory(const MachineFunction *MF,
123 const TargetRegisterInfo *TRI,
124 DbgValueHistoryMap &DbgValues,
125 DbgLabelInstrMap &DbgLabels);
127 } // end namespace llvm
129 #endif // LLVM_CODEGEN_DBGVALUEHISTORYCALCULATOR_H