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"
15 #include "llvm/IR/DebugInfoMetadata.h"
20 class DILocalVariable
;
21 class MachineFunction
;
23 class TargetRegisterInfo
;
25 /// For each user variable, keep a list of instruction ranges where this
26 /// variable is accessible. The variables are listed in order of appearance.
27 class DbgValueHistoryMap
{
29 /// Index in the entry vector.
30 typedef size_t EntryIndex
;
32 /// Special value to indicate that an entry is valid until the end of the
34 static const EntryIndex NoEntry
= std::numeric_limits
<EntryIndex
>::max();
36 /// Specifies a change in a variable's debug value history.
38 /// There exist two types of entries:
40 /// * Debug value entry:
42 /// A new debug value becomes live. If the entry's \p EndIndex is \p NoEntry,
43 /// the value is valid until the end of the function. For other values, the
44 /// index points to the entry in the entry vector that ends this debug
45 /// value. The ending entry can either be an overlapping debug value, or
46 /// an instruction that clobbers the value.
48 /// * Clobbering entry:
50 /// This entry's instruction clobbers one or more preceding
51 /// register-described debug values that have their end index
52 /// set to this entry's position in the entry vector.
55 enum EntryKind
{ DbgValue
, Clobber
};
57 Entry(const MachineInstr
*Instr
, EntryKind Kind
)
58 : Instr(Instr
, Kind
), EndIndex(NoEntry
) {}
60 const MachineInstr
*getInstr() const { return Instr
.getPointer(); }
61 EntryIndex
getEndIndex() const { return EndIndex
; }
62 EntryKind
getEntryKind() const { return Instr
.getInt(); }
64 bool isClobber() const { return getEntryKind() == Clobber
; }
65 bool isDbgValue() const { return getEntryKind() == DbgValue
; }
66 bool isClosed() const { return EndIndex
!= NoEntry
; }
68 void endEntry(EntryIndex EndIndex
);
71 PointerIntPair
<const MachineInstr
*, 1, EntryKind
> Instr
;
74 using Entries
= SmallVector
<Entry
, 4>;
75 using InlinedEntity
= std::pair
<const DINode
*, const DILocation
*>;
76 using EntriesMap
= MapVector
<InlinedEntity
, Entries
>;
79 EntriesMap VarEntries
;
82 bool startDbgValue(InlinedEntity Var
, const MachineInstr
&MI
,
83 EntryIndex
&NewIndex
);
84 EntryIndex
startClobber(InlinedEntity Var
, const MachineInstr
&MI
);
86 Entry
&getEntry(InlinedEntity Var
, EntryIndex Index
) {
87 auto &Entries
= VarEntries
[Var
];
88 return Entries
[Index
];
91 bool empty() const { return VarEntries
.empty(); }
92 void clear() { VarEntries
.clear(); }
93 EntriesMap::const_iterator
begin() const { return VarEntries
.begin(); }
94 EntriesMap::const_iterator
end() const { return VarEntries
.end(); }
96 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
97 LLVM_DUMP_METHOD
void dump() const;
101 /// For each inlined instance of a source-level label, keep the corresponding
102 /// DBG_LABEL instruction. The DBG_LABEL instruction could be used to generate
103 /// a temporary (assembler) label before it.
104 class DbgLabelInstrMap
{
106 using InlinedEntity
= std::pair
<const DINode
*, const DILocation
*>;
107 using InstrMap
= MapVector
<InlinedEntity
, const MachineInstr
*>;
113 void addInstr(InlinedEntity Label
, const MachineInstr
&MI
);
115 bool empty() const { return LabelInstr
.empty(); }
116 void clear() { LabelInstr
.clear(); }
117 InstrMap::const_iterator
begin() const { return LabelInstr
.begin(); }
118 InstrMap::const_iterator
end() const { return LabelInstr
.end(); }
121 void calculateDbgEntityHistory(const MachineFunction
*MF
,
122 const TargetRegisterInfo
*TRI
,
123 DbgValueHistoryMap
&DbgValues
,
124 DbgLabelInstrMap
&DbgLabels
);
126 } // end namespace llvm
128 #endif // LLVM_CODEGEN_DBGVALUEHISTORYCALCULATOR_H