120bb9fe5SJeremy Morse //===- VarLocBasedImpl.cpp - Tracking Debug Value MIs with VarLoc class----===//
2fba06e3cSJeremy Morse //
3fba06e3cSJeremy Morse // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4fba06e3cSJeremy Morse // See https://llvm.org/LICENSE.txt for license information.
5fba06e3cSJeremy Morse // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fba06e3cSJeremy Morse //
7fba06e3cSJeremy Morse //===----------------------------------------------------------------------===//
8fba06e3cSJeremy Morse ///
920bb9fe5SJeremy Morse /// \file VarLocBasedImpl.cpp
10fba06e3cSJeremy Morse ///
11fba06e3cSJeremy Morse /// LiveDebugValues is an optimistic "available expressions" dataflow
12fba06e3cSJeremy Morse /// algorithm. The set of expressions is the set of machine locations
13fba06e3cSJeremy Morse /// (registers, spill slots, constants) that a variable fragment might be
14fba06e3cSJeremy Morse /// located, qualified by a DIExpression and indirect-ness flag, while each
15fba06e3cSJeremy Morse /// variable is identified by a DebugVariable object. The availability of an
16fba06e3cSJeremy Morse /// expression begins when a DBG_VALUE instruction specifies the location of a
17fba06e3cSJeremy Morse /// DebugVariable, and continues until that location is clobbered or
18fba06e3cSJeremy Morse /// re-specified by a different DBG_VALUE for the same DebugVariable.
19fba06e3cSJeremy Morse ///
2020bb9fe5SJeremy Morse /// The output of LiveDebugValues is additional DBG_VALUE instructions,
2120bb9fe5SJeremy Morse /// placed to extend variable locations as far they're available. This file
2220bb9fe5SJeremy Morse /// and the VarLocBasedLDV class is an implementation that explicitly tracks
2320bb9fe5SJeremy Morse /// locations, using the VarLoc class.
2420bb9fe5SJeremy Morse ///
25d9345999SMatt Arsenault /// The canonical "available expressions" problem doesn't have expression
26fba06e3cSJeremy Morse /// clobbering, instead when a variable is re-assigned, any expressions using
27fba06e3cSJeremy Morse /// that variable get invalidated. LiveDebugValues can map onto "available
28fba06e3cSJeremy Morse /// expressions" by having every register represented by a variable, which is
29fba06e3cSJeremy Morse /// used in an expression that becomes available at a DBG_VALUE instruction.
30fba06e3cSJeremy Morse /// When the register is clobbered, its variable is effectively reassigned, and
31fba06e3cSJeremy Morse /// expressions computed from it become unavailable. A similar construct is
32fba06e3cSJeremy Morse /// needed when a DebugVariable has its location re-specified, to invalidate
33fba06e3cSJeremy Morse /// all other locations for that DebugVariable.
34fba06e3cSJeremy Morse ///
35fba06e3cSJeremy Morse /// Using the dataflow analysis to compute the available expressions, we create
36fba06e3cSJeremy Morse /// a DBG_VALUE at the beginning of each block where the expression is
37fba06e3cSJeremy Morse /// live-in. This propagates variable locations into every basic block where
38fba06e3cSJeremy Morse /// the location can be determined, rather than only having DBG_VALUEs in blocks
39fba06e3cSJeremy Morse /// where locations are specified due to an assignment or some optimization.
40fba06e3cSJeremy Morse /// Movements of values between registers and spill slots are annotated with
41fba06e3cSJeremy Morse /// DBG_VALUEs too to track variable values bewteen locations. All this allows
42fba06e3cSJeremy Morse /// DbgEntityHistoryCalculator to focus on only the locations within individual
43fba06e3cSJeremy Morse /// blocks, facilitating testing and improving modularity.
44fba06e3cSJeremy Morse ///
45fba06e3cSJeremy Morse /// We follow an optimisic dataflow approach, with this lattice:
46fba06e3cSJeremy Morse ///
47fba06e3cSJeremy Morse /// \verbatim
48fba06e3cSJeremy Morse /// ┬ "Unknown"
49fba06e3cSJeremy Morse /// |
50fba06e3cSJeremy Morse /// v
51fba06e3cSJeremy Morse /// True
52fba06e3cSJeremy Morse /// |
53fba06e3cSJeremy Morse /// v
54fba06e3cSJeremy Morse /// ⊥ False
55fba06e3cSJeremy Morse /// \endverbatim With "True" signifying that the expression is available (and
56fba06e3cSJeremy Morse /// thus a DebugVariable's location is the corresponding register), while
57fba06e3cSJeremy Morse /// "False" signifies that the expression is unavailable. "Unknown"s never
58fba06e3cSJeremy Morse /// survive to the end of the analysis (see below).
59fba06e3cSJeremy Morse ///
60fba06e3cSJeremy Morse /// Formally, all DebugVariable locations that are live-out of a block are
61fba06e3cSJeremy Morse /// initialized to \top. A blocks live-in values take the meet of the lattice
62fba06e3cSJeremy Morse /// value for every predecessors live-outs, except for the entry block, where
63fba06e3cSJeremy Morse /// all live-ins are \bot. The usual dataflow propagation occurs: the transfer
64fba06e3cSJeremy Morse /// function for a block assigns an expression for a DebugVariable to be "True"
65fba06e3cSJeremy Morse /// if a DBG_VALUE in the block specifies it; "False" if the location is
66fba06e3cSJeremy Morse /// clobbered; or the live-in value if it is unaffected by the block. We
67fba06e3cSJeremy Morse /// visit each block in reverse post order until a fixedpoint is reached. The
68fba06e3cSJeremy Morse /// solution produced is maximal.
69fba06e3cSJeremy Morse ///
70fba06e3cSJeremy Morse /// Intuitively, we start by assuming that every expression / variable location
71fba06e3cSJeremy Morse /// is at least "True", and then propagate "False" from the entry block and any
72fba06e3cSJeremy Morse /// clobbers until there are no more changes to make. This gives us an accurate
73fba06e3cSJeremy Morse /// solution because all incorrect locations will have a "False" propagated into
74fba06e3cSJeremy Morse /// them. It also gives us a solution that copes well with loops by assuming
75fba06e3cSJeremy Morse /// that variable locations are live-through every loop, and then removing those
76fba06e3cSJeremy Morse /// that are not through dataflow.
77fba06e3cSJeremy Morse ///
78fba06e3cSJeremy Morse /// Within LiveDebugValues: each variable location is represented by a
79e2196ddcSgbtozers /// VarLoc object that identifies the source variable, the set of
80e2196ddcSgbtozers /// machine-locations that currently describe it (a single location for
81e2196ddcSgbtozers /// DBG_VALUE or multiple for DBG_VALUE_LIST), and the DBG_VALUE inst that
82e2196ddcSgbtozers /// specifies the location. Each VarLoc is indexed in the (function-scope) \p
83e2196ddcSgbtozers /// VarLocMap, giving each VarLoc a set of unique indexes, each of which
84e2196ddcSgbtozers /// corresponds to one of the VarLoc's machine-locations and can be used to
85e2196ddcSgbtozers /// lookup the VarLoc in the VarLocMap. Rather than operate directly on machine
86e2196ddcSgbtozers /// locations, the dataflow analysis in this pass identifies locations by their
87e2196ddcSgbtozers /// indices in the VarLocMap, meaning all the variable locations in a block can
88e2196ddcSgbtozers /// be described by a sparse vector of VarLocMap indicies.
89fba06e3cSJeremy Morse ///
90fba06e3cSJeremy Morse /// All the storage for the dataflow analysis is local to the ExtendRanges
91fba06e3cSJeremy Morse /// method and passed down to helper methods. "OutLocs" and "InLocs" record the
92fba06e3cSJeremy Morse /// in and out lattice values for each block. "OpenRanges" maintains a list of
93fba06e3cSJeremy Morse /// variable locations and, with the "process" method, evaluates the transfer
94e2196ddcSgbtozers /// function of each block. "flushPendingLocs" installs debug value instructions
95e2196ddcSgbtozers /// for each live-in location at the start of blocks, while "Transfers" records
96fba06e3cSJeremy Morse /// transfers of values between machine-locations.
97fba06e3cSJeremy Morse ///
98fba06e3cSJeremy Morse /// We avoid explicitly representing the "Unknown" (\top) lattice value in the
99fba06e3cSJeremy Morse /// implementation. Instead, unvisited blocks implicitly have all lattice
100fba06e3cSJeremy Morse /// values set as "Unknown". After being visited, there will be path back to
101fba06e3cSJeremy Morse /// the entry block where the lattice value is "False", and as the transfer
102fba06e3cSJeremy Morse /// function cannot make new "Unknown" locations, there are no scenarios where
103fba06e3cSJeremy Morse /// a block can have an "Unknown" location after being visited. Similarly, we
104fba06e3cSJeremy Morse /// don't enumerate all possible variable locations before exploring the
105fba06e3cSJeremy Morse /// function: when a new location is discovered, all blocks previously explored
106fba06e3cSJeremy Morse /// were implicitly "False" but unrecorded, and become explicitly "False" when
107fba06e3cSJeremy Morse /// a new VarLoc is created with its bit not set in predecessor InLocs or
108fba06e3cSJeremy Morse /// OutLocs.
109fba06e3cSJeremy Morse ///
110fba06e3cSJeremy Morse //===----------------------------------------------------------------------===//
111fba06e3cSJeremy Morse
11220bb9fe5SJeremy Morse #include "LiveDebugValues.h"
11320bb9fe5SJeremy Morse
114fba06e3cSJeremy Morse #include "llvm/ADT/CoalescingBitVector.h"
115fba06e3cSJeremy Morse #include "llvm/ADT/DenseMap.h"
116fba06e3cSJeremy Morse #include "llvm/ADT/PostOrderIterator.h"
117fba06e3cSJeremy Morse #include "llvm/ADT/SmallPtrSet.h"
118fba06e3cSJeremy Morse #include "llvm/ADT/SmallSet.h"
119fba06e3cSJeremy Morse #include "llvm/ADT/SmallVector.h"
120fba06e3cSJeremy Morse #include "llvm/ADT/Statistic.h"
121989f1c72Sserge-sans-paille #include "llvm/BinaryFormat/Dwarf.h"
122fba06e3cSJeremy Morse #include "llvm/CodeGen/LexicalScopes.h"
123fba06e3cSJeremy Morse #include "llvm/CodeGen/MachineBasicBlock.h"
124fba06e3cSJeremy Morse #include "llvm/CodeGen/MachineFunction.h"
125fba06e3cSJeremy Morse #include "llvm/CodeGen/MachineInstr.h"
126fba06e3cSJeremy Morse #include "llvm/CodeGen/MachineInstrBuilder.h"
127fba06e3cSJeremy Morse #include "llvm/CodeGen/MachineMemOperand.h"
128fba06e3cSJeremy Morse #include "llvm/CodeGen/MachineOperand.h"
129fba06e3cSJeremy Morse #include "llvm/CodeGen/PseudoSourceValue.h"
130fba06e3cSJeremy Morse #include "llvm/CodeGen/TargetFrameLowering.h"
131fba06e3cSJeremy Morse #include "llvm/CodeGen/TargetInstrInfo.h"
132fba06e3cSJeremy Morse #include "llvm/CodeGen/TargetLowering.h"
133fba06e3cSJeremy Morse #include "llvm/CodeGen/TargetPassConfig.h"
134fba06e3cSJeremy Morse #include "llvm/CodeGen/TargetRegisterInfo.h"
135fba06e3cSJeremy Morse #include "llvm/CodeGen/TargetSubtargetInfo.h"
136fba06e3cSJeremy Morse #include "llvm/Config/llvm-config.h"
137fba06e3cSJeremy Morse #include "llvm/IR/DebugInfoMetadata.h"
138fba06e3cSJeremy Morse #include "llvm/IR/DebugLoc.h"
139fba06e3cSJeremy Morse #include "llvm/IR/Function.h"
140fba06e3cSJeremy Morse #include "llvm/MC/MCRegisterInfo.h"
141fba06e3cSJeremy Morse #include "llvm/Support/Casting.h"
142fba06e3cSJeremy Morse #include "llvm/Support/Debug.h"
14384a11209SSander de Smalen #include "llvm/Support/TypeSize.h"
144fba06e3cSJeremy Morse #include "llvm/Support/raw_ostream.h"
145fba06e3cSJeremy Morse #include "llvm/Target/TargetMachine.h"
146fba06e3cSJeremy Morse #include <algorithm>
147fba06e3cSJeremy Morse #include <cassert>
148fba06e3cSJeremy Morse #include <cstdint>
149fba06e3cSJeremy Morse #include <functional>
15086f5288eSDjordje Todorovic #include <map>
151fba06e3cSJeremy Morse #include <queue>
152fba06e3cSJeremy Morse #include <tuple>
153fba06e3cSJeremy Morse #include <utility>
154fba06e3cSJeremy Morse #include <vector>
155fba06e3cSJeremy Morse
156fba06e3cSJeremy Morse using namespace llvm;
157fba06e3cSJeremy Morse
158fba06e3cSJeremy Morse #define DEBUG_TYPE "livedebugvalues"
159fba06e3cSJeremy Morse
160fba06e3cSJeremy Morse STATISTIC(NumInserted, "Number of DBG_VALUE instructions inserted");
161fba06e3cSJeremy Morse
162fba06e3cSJeremy Morse /// If \p Op is a stack or frame register return true, otherwise return false.
163fba06e3cSJeremy Morse /// This is used to avoid basing the debug entry values on the registers, since
164fba06e3cSJeremy Morse /// we do not support it at the moment.
isRegOtherThanSPAndFP(const MachineOperand & Op,const MachineInstr & MI,const TargetRegisterInfo * TRI)165fba06e3cSJeremy Morse static bool isRegOtherThanSPAndFP(const MachineOperand &Op,
166fba06e3cSJeremy Morse const MachineInstr &MI,
167fba06e3cSJeremy Morse const TargetRegisterInfo *TRI) {
168fba06e3cSJeremy Morse if (!Op.isReg())
169fba06e3cSJeremy Morse return false;
170fba06e3cSJeremy Morse
171fba06e3cSJeremy Morse const MachineFunction *MF = MI.getParent()->getParent();
172fba06e3cSJeremy Morse const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
173fba06e3cSJeremy Morse Register SP = TLI->getStackPointerRegisterToSaveRestore();
174fba06e3cSJeremy Morse Register FP = TRI->getFrameRegister(*MF);
175fba06e3cSJeremy Morse Register Reg = Op.getReg();
176fba06e3cSJeremy Morse
177fba06e3cSJeremy Morse return Reg && Reg != SP && Reg != FP;
178fba06e3cSJeremy Morse }
179fba06e3cSJeremy Morse
180fba06e3cSJeremy Morse namespace {
181fba06e3cSJeremy Morse
182fba06e3cSJeremy Morse // Max out the number of statically allocated elements in DefinedRegsSet, as
183fba06e3cSJeremy Morse // this prevents fallback to std::set::count() operations.
184fba06e3cSJeremy Morse using DefinedRegsSet = SmallSet<Register, 32>;
185fba06e3cSJeremy Morse
186e2196ddcSgbtozers // The IDs in this set correspond to MachineLocs in VarLocs, as well as VarLocs
187e2196ddcSgbtozers // that represent Entry Values; every VarLoc in the set will also appear
188e2196ddcSgbtozers // exactly once at Location=0.
189e2196ddcSgbtozers // As a result, each VarLoc may appear more than once in this "set", but each
190e2196ddcSgbtozers // range corresponding to a Reg, SpillLoc, or EntryValue type will still be a
191e2196ddcSgbtozers // "true" set (i.e. each VarLoc may appear only once), and the range Location=0
192e2196ddcSgbtozers // is the set of all VarLocs.
193fba06e3cSJeremy Morse using VarLocSet = CoalescingBitVector<uint64_t>;
194fba06e3cSJeremy Morse
195fba06e3cSJeremy Morse /// A type-checked pair of {Register Location (or 0), Index}, used to index
196fba06e3cSJeremy Morse /// into a \ref VarLocMap. This can be efficiently converted to a 64-bit int
197fba06e3cSJeremy Morse /// for insertion into a \ref VarLocSet, and efficiently converted back. The
198fba06e3cSJeremy Morse /// type-checker helps ensure that the conversions aren't lossy.
199fba06e3cSJeremy Morse ///
200fba06e3cSJeremy Morse /// Why encode a location /into/ the VarLocMap index? This makes it possible
201fba06e3cSJeremy Morse /// to find the open VarLocs killed by a register def very quickly. This is a
202fba06e3cSJeremy Morse /// performance-critical operation for LiveDebugValues.
203fba06e3cSJeremy Morse struct LocIndex {
204fba06e3cSJeremy Morse using u32_location_t = uint32_t;
205fba06e3cSJeremy Morse using u32_index_t = uint32_t;
206fba06e3cSJeremy Morse
207fba06e3cSJeremy Morse u32_location_t Location; // Physical registers live in the range [1;2^30) (see
208fba06e3cSJeremy Morse // \ref MCRegister), so we have plenty of range left
209fba06e3cSJeremy Morse // here to encode non-register locations.
210fba06e3cSJeremy Morse u32_index_t Index;
211fba06e3cSJeremy Morse
212e2196ddcSgbtozers /// The location that has an entry for every VarLoc in the map.
213e2196ddcSgbtozers static constexpr u32_location_t kUniversalLocation = 0;
214e2196ddcSgbtozers
215e2196ddcSgbtozers /// The first location that is reserved for VarLocs with locations of kind
216e2196ddcSgbtozers /// RegisterKind.
217e2196ddcSgbtozers static constexpr u32_location_t kFirstRegLocation = 1;
218e2196ddcSgbtozers
219e2196ddcSgbtozers /// The first location greater than 0 that is not reserved for VarLocs with
220e2196ddcSgbtozers /// locations of kind RegisterKind.
221fba06e3cSJeremy Morse static constexpr u32_location_t kFirstInvalidRegLocation = 1 << 30;
222fba06e3cSJeremy Morse
223e2196ddcSgbtozers /// A special location reserved for VarLocs with locations of kind
224e2196ddcSgbtozers /// SpillLocKind.
225fba06e3cSJeremy Morse static constexpr u32_location_t kSpillLocation = kFirstInvalidRegLocation;
226fba06e3cSJeremy Morse
227fba06e3cSJeremy Morse /// A special location reserved for VarLocs of kind EntryValueBackupKind and
228fba06e3cSJeremy Morse /// EntryValueCopyBackupKind.
229fba06e3cSJeremy Morse static constexpr u32_location_t kEntryValueBackupLocation =
230fba06e3cSJeremy Morse kFirstInvalidRegLocation + 1;
231fba06e3cSJeremy Morse
LocIndex__anon32558ef20111::LocIndex232fba06e3cSJeremy Morse LocIndex(u32_location_t Location, u32_index_t Index)
233fba06e3cSJeremy Morse : Location(Location), Index(Index) {}
234fba06e3cSJeremy Morse
getAsRawInteger__anon32558ef20111::LocIndex235fba06e3cSJeremy Morse uint64_t getAsRawInteger() const {
236fba06e3cSJeremy Morse return (static_cast<uint64_t>(Location) << 32) | Index;
237fba06e3cSJeremy Morse }
238fba06e3cSJeremy Morse
fromRawInteger__anon32558ef20111::LocIndex239fba06e3cSJeremy Morse template<typename IntT> static LocIndex fromRawInteger(IntT ID) {
240fba06e3cSJeremy Morse static_assert(std::is_unsigned<IntT>::value &&
241fba06e3cSJeremy Morse sizeof(ID) == sizeof(uint64_t),
242fba06e3cSJeremy Morse "Cannot convert raw integer to LocIndex");
243fba06e3cSJeremy Morse return {static_cast<u32_location_t>(ID >> 32),
244fba06e3cSJeremy Morse static_cast<u32_index_t>(ID)};
245fba06e3cSJeremy Morse }
246fba06e3cSJeremy Morse
247fba06e3cSJeremy Morse /// Get the start of the interval reserved for VarLocs of kind RegisterKind
248fba06e3cSJeremy Morse /// which reside in \p Reg. The end is at rawIndexForReg(Reg+1)-1.
rawIndexForReg__anon32558ef20111::LocIndex249e2196ddcSgbtozers static uint64_t rawIndexForReg(Register Reg) {
250fba06e3cSJeremy Morse return LocIndex(Reg, 0).getAsRawInteger();
251fba06e3cSJeremy Morse }
252fba06e3cSJeremy Morse
253fba06e3cSJeremy Morse /// Return a range covering all set indices in the interval reserved for
254fba06e3cSJeremy Morse /// \p Location in \p Set.
indexRangeForLocation__anon32558ef20111::LocIndex255fba06e3cSJeremy Morse static auto indexRangeForLocation(const VarLocSet &Set,
256fba06e3cSJeremy Morse u32_location_t Location) {
257fba06e3cSJeremy Morse uint64_t Start = LocIndex(Location, 0).getAsRawInteger();
258fba06e3cSJeremy Morse uint64_t End = LocIndex(Location + 1, 0).getAsRawInteger();
259fba06e3cSJeremy Morse return Set.half_open_range(Start, End);
260fba06e3cSJeremy Morse }
261fba06e3cSJeremy Morse };
262fba06e3cSJeremy Morse
263e2196ddcSgbtozers // Simple Set for storing all the VarLoc Indices at a Location bucket.
264e2196ddcSgbtozers using VarLocsInRange = SmallSet<LocIndex::u32_index_t, 32>;
265e2196ddcSgbtozers // Vector of all `LocIndex`s for a given VarLoc; the same Location should not
266e2196ddcSgbtozers // appear in any two of these, as each VarLoc appears at most once in any
267e2196ddcSgbtozers // Location bucket.
268e2196ddcSgbtozers using LocIndices = SmallVector<LocIndex, 2>;
269e2196ddcSgbtozers
27020bb9fe5SJeremy Morse class VarLocBasedLDV : public LDVImpl {
271fba06e3cSJeremy Morse private:
272fba06e3cSJeremy Morse const TargetRegisterInfo *TRI;
273fba06e3cSJeremy Morse const TargetInstrInfo *TII;
274fba06e3cSJeremy Morse const TargetFrameLowering *TFI;
27520bb9fe5SJeremy Morse TargetPassConfig *TPC;
276fba06e3cSJeremy Morse BitVector CalleeSavedRegs;
277fba06e3cSJeremy Morse LexicalScopes LS;
278fba06e3cSJeremy Morse VarLocSet::Allocator Alloc;
279fba06e3cSJeremy Morse
28086f5288eSDjordje Todorovic const MachineInstr *LastNonDbgMI;
28186f5288eSDjordje Todorovic
282fba06e3cSJeremy Morse enum struct TransferKind { TransferCopy, TransferSpill, TransferRestore };
283fba06e3cSJeremy Morse
284fba06e3cSJeremy Morse using FragmentInfo = DIExpression::FragmentInfo;
285fba06e3cSJeremy Morse using OptFragmentInfo = Optional<DIExpression::FragmentInfo>;
286fba06e3cSJeremy Morse
287fba06e3cSJeremy Morse /// A pair of debug variable and value location.
288fba06e3cSJeremy Morse struct VarLoc {
289fba06e3cSJeremy Morse // The location at which a spilled variable resides. It consists of a
290fba06e3cSJeremy Morse // register and an offset.
291fba06e3cSJeremy Morse struct SpillLoc {
292fba06e3cSJeremy Morse unsigned SpillBase;
29384a11209SSander de Smalen StackOffset SpillOffset;
operator ==__anon32558ef20111::VarLocBasedLDV::VarLoc::SpillLoc294fba06e3cSJeremy Morse bool operator==(const SpillLoc &Other) const {
295fba06e3cSJeremy Morse return SpillBase == Other.SpillBase && SpillOffset == Other.SpillOffset;
296fba06e3cSJeremy Morse }
operator !=__anon32558ef20111::VarLocBasedLDV::VarLoc::SpillLoc297fba06e3cSJeremy Morse bool operator!=(const SpillLoc &Other) const {
298fba06e3cSJeremy Morse return !(*this == Other);
299fba06e3cSJeremy Morse }
300fba06e3cSJeremy Morse };
301fba06e3cSJeremy Morse
302fba06e3cSJeremy Morse /// Identity of the variable at this location.
303fba06e3cSJeremy Morse const DebugVariable Var;
304fba06e3cSJeremy Morse
305fba06e3cSJeremy Morse /// The expression applied to this location.
306fba06e3cSJeremy Morse const DIExpression *Expr;
307fba06e3cSJeremy Morse
308fba06e3cSJeremy Morse /// DBG_VALUE to clone var/expr information from if this location
309fba06e3cSJeremy Morse /// is moved.
310fba06e3cSJeremy Morse const MachineInstr &MI;
311fba06e3cSJeremy Morse
312e2196ddcSgbtozers enum class MachineLocKind {
313fba06e3cSJeremy Morse InvalidKind = 0,
314fba06e3cSJeremy Morse RegisterKind,
315fba06e3cSJeremy Morse SpillLocKind,
316e2196ddcSgbtozers ImmediateKind
317e2196ddcSgbtozers };
318e2196ddcSgbtozers
319e2196ddcSgbtozers enum class EntryValueLocKind {
320e2196ddcSgbtozers NonEntryValueKind = 0,
321fba06e3cSJeremy Morse EntryValueKind,
322fba06e3cSJeremy Morse EntryValueBackupKind,
323fba06e3cSJeremy Morse EntryValueCopyBackupKind
3242bea207dSKazu Hirata } EVKind = EntryValueLocKind::NonEntryValueKind;
325fba06e3cSJeremy Morse
326fba06e3cSJeremy Morse /// The value location. Stored separately to avoid repeatedly
327fba06e3cSJeremy Morse /// extracting it from MI.
328e2196ddcSgbtozers union MachineLocValue {
329fba06e3cSJeremy Morse uint64_t RegNo;
330fba06e3cSJeremy Morse SpillLoc SpillLocation;
331fba06e3cSJeremy Morse uint64_t Hash;
332fba06e3cSJeremy Morse int64_t Immediate;
333fba06e3cSJeremy Morse const ConstantFP *FPImm;
334fba06e3cSJeremy Morse const ConstantInt *CImm;
MachineLocValue()335e2196ddcSgbtozers MachineLocValue() : Hash(0) {}
336e2196ddcSgbtozers };
337e2196ddcSgbtozers
338e2196ddcSgbtozers /// A single machine location; its Kind is either a register, spill
339e2196ddcSgbtozers /// location, or immediate value.
340e2196ddcSgbtozers /// If the VarLoc is not a NonEntryValueKind, then it will use only a
341e2196ddcSgbtozers /// single MachineLoc of RegisterKind.
342e2196ddcSgbtozers struct MachineLoc {
343e2196ddcSgbtozers MachineLocKind Kind;
344e2196ddcSgbtozers MachineLocValue Value;
operator ==__anon32558ef20111::VarLocBasedLDV::VarLoc::MachineLoc345e2196ddcSgbtozers bool operator==(const MachineLoc &Other) const {
346e2196ddcSgbtozers if (Kind != Other.Kind)
347e2196ddcSgbtozers return false;
348e2196ddcSgbtozers switch (Kind) {
349e2196ddcSgbtozers case MachineLocKind::SpillLocKind:
350e2196ddcSgbtozers return Value.SpillLocation == Other.Value.SpillLocation;
351e2196ddcSgbtozers case MachineLocKind::RegisterKind:
352e2196ddcSgbtozers case MachineLocKind::ImmediateKind:
353e2196ddcSgbtozers return Value.Hash == Other.Value.Hash;
354e2196ddcSgbtozers default:
355e2196ddcSgbtozers llvm_unreachable("Invalid kind");
356e2196ddcSgbtozers }
357e2196ddcSgbtozers }
operator <__anon32558ef20111::VarLocBasedLDV::VarLoc::MachineLoc358e2196ddcSgbtozers bool operator<(const MachineLoc &Other) const {
359e2196ddcSgbtozers switch (Kind) {
360e2196ddcSgbtozers case MachineLocKind::SpillLocKind:
361e2196ddcSgbtozers return std::make_tuple(
362e2196ddcSgbtozers Kind, Value.SpillLocation.SpillBase,
363e2196ddcSgbtozers Value.SpillLocation.SpillOffset.getFixed(),
364e2196ddcSgbtozers Value.SpillLocation.SpillOffset.getScalable()) <
365e2196ddcSgbtozers std::make_tuple(
366e2196ddcSgbtozers Other.Kind, Other.Value.SpillLocation.SpillBase,
367e2196ddcSgbtozers Other.Value.SpillLocation.SpillOffset.getFixed(),
368e2196ddcSgbtozers Other.Value.SpillLocation.SpillOffset.getScalable());
369e2196ddcSgbtozers case MachineLocKind::RegisterKind:
370e2196ddcSgbtozers case MachineLocKind::ImmediateKind:
371e2196ddcSgbtozers return std::tie(Kind, Value.Hash) <
372e2196ddcSgbtozers std::tie(Other.Kind, Other.Value.Hash);
373e2196ddcSgbtozers default:
374e2196ddcSgbtozers llvm_unreachable("Invalid kind");
375e2196ddcSgbtozers }
376e2196ddcSgbtozers }
377e2196ddcSgbtozers };
378e2196ddcSgbtozers
379e2196ddcSgbtozers /// The set of machine locations used to determine the variable's value, in
380e2196ddcSgbtozers /// conjunction with Expr. Initially populated with MI's debug operands,
381e2196ddcSgbtozers /// but may be transformed independently afterwards.
382e2196ddcSgbtozers SmallVector<MachineLoc, 8> Locs;
383e2196ddcSgbtozers /// Used to map the index of each location in Locs back to the index of its
384e2196ddcSgbtozers /// original debug operand in MI. Used when multiple location operands are
385e2196ddcSgbtozers /// coalesced and the original MI's operands need to be accessed while
386e2196ddcSgbtozers /// emitting a debug value.
387e2196ddcSgbtozers SmallVector<unsigned, 8> OrigLocMap;
388fba06e3cSJeremy Morse
VarLoc__anon32558ef20111::VarLocBasedLDV::VarLoc389fba06e3cSJeremy Morse VarLoc(const MachineInstr &MI, LexicalScopes &LS)
390fba06e3cSJeremy Morse : Var(MI.getDebugVariable(), MI.getDebugExpression(),
391fba06e3cSJeremy Morse MI.getDebugLoc()->getInlinedAt()),
3922bea207dSKazu Hirata Expr(MI.getDebugExpression()), MI(MI) {
393fba06e3cSJeremy Morse assert(MI.isDebugValue() && "not a DBG_VALUE");
394e2196ddcSgbtozers assert((MI.isDebugValueList() || MI.getNumOperands() == 4) &&
395e2196ddcSgbtozers "malformed DBG_VALUE");
396e2196ddcSgbtozers for (const MachineOperand &Op : MI.debug_operands()) {
397e2196ddcSgbtozers MachineLoc ML = GetLocForOp(Op);
398e2196ddcSgbtozers auto It = find(Locs, ML);
399e2196ddcSgbtozers if (It == Locs.end()) {
400e2196ddcSgbtozers Locs.push_back(ML);
401e2196ddcSgbtozers OrigLocMap.push_back(MI.getDebugOperandIndex(&Op));
402e2196ddcSgbtozers } else {
403e2196ddcSgbtozers // ML duplicates an element in Locs; replace references to Op
404e2196ddcSgbtozers // with references to the duplicating element.
405e2196ddcSgbtozers unsigned OpIdx = Locs.size();
406e2196ddcSgbtozers unsigned DuplicatingIdx = std::distance(Locs.begin(), It);
407e2196ddcSgbtozers Expr = DIExpression::replaceArg(Expr, OpIdx, DuplicatingIdx);
408e2196ddcSgbtozers }
409fba06e3cSJeremy Morse }
410fba06e3cSJeremy Morse
411e2196ddcSgbtozers // We create the debug entry values from the factory functions rather
412e2196ddcSgbtozers // than from this ctor.
413e2196ddcSgbtozers assert(EVKind != EntryValueLocKind::EntryValueKind &&
414e2196ddcSgbtozers !isEntryBackupLoc());
415e2196ddcSgbtozers }
416e2196ddcSgbtozers
GetLocForOp__anon32558ef20111::VarLocBasedLDV::VarLoc417e2196ddcSgbtozers static MachineLoc GetLocForOp(const MachineOperand &Op) {
418e2196ddcSgbtozers MachineLocKind Kind;
419e2196ddcSgbtozers MachineLocValue Loc;
420e2196ddcSgbtozers if (Op.isReg()) {
421e2196ddcSgbtozers Kind = MachineLocKind::RegisterKind;
422e2196ddcSgbtozers Loc.RegNo = Op.getReg();
423e2196ddcSgbtozers } else if (Op.isImm()) {
424e2196ddcSgbtozers Kind = MachineLocKind::ImmediateKind;
425e2196ddcSgbtozers Loc.Immediate = Op.getImm();
426e2196ddcSgbtozers } else if (Op.isFPImm()) {
427e2196ddcSgbtozers Kind = MachineLocKind::ImmediateKind;
428e2196ddcSgbtozers Loc.FPImm = Op.getFPImm();
429e2196ddcSgbtozers } else if (Op.isCImm()) {
430e2196ddcSgbtozers Kind = MachineLocKind::ImmediateKind;
431e2196ddcSgbtozers Loc.CImm = Op.getCImm();
432e2196ddcSgbtozers } else
433e2196ddcSgbtozers llvm_unreachable("Invalid Op kind for MachineLoc.");
434e2196ddcSgbtozers return {Kind, Loc};
435fba06e3cSJeremy Morse }
436fba06e3cSJeremy Morse
437fba06e3cSJeremy Morse /// Take the variable and machine-location in DBG_VALUE MI, and build an
438fba06e3cSJeremy Morse /// entry location using the given expression.
CreateEntryLoc__anon32558ef20111::VarLocBasedLDV::VarLoc439fba06e3cSJeremy Morse static VarLoc CreateEntryLoc(const MachineInstr &MI, LexicalScopes &LS,
440fba06e3cSJeremy Morse const DIExpression *EntryExpr, Register Reg) {
441fba06e3cSJeremy Morse VarLoc VL(MI, LS);
442e2196ddcSgbtozers assert(VL.Locs.size() == 1 &&
443e2196ddcSgbtozers VL.Locs[0].Kind == MachineLocKind::RegisterKind);
444e2196ddcSgbtozers VL.EVKind = EntryValueLocKind::EntryValueKind;
445fba06e3cSJeremy Morse VL.Expr = EntryExpr;
446e2196ddcSgbtozers VL.Locs[0].Value.RegNo = Reg;
447fba06e3cSJeremy Morse return VL;
448fba06e3cSJeremy Morse }
449fba06e3cSJeremy Morse
450fba06e3cSJeremy Morse /// Take the variable and machine-location from the DBG_VALUE (from the
451fba06e3cSJeremy Morse /// function entry), and build an entry value backup location. The backup
452fba06e3cSJeremy Morse /// location will turn into the normal location if the backup is valid at
453fba06e3cSJeremy Morse /// the time of the primary location clobbering.
CreateEntryBackupLoc__anon32558ef20111::VarLocBasedLDV::VarLoc454fba06e3cSJeremy Morse static VarLoc CreateEntryBackupLoc(const MachineInstr &MI,
455fba06e3cSJeremy Morse LexicalScopes &LS,
456fba06e3cSJeremy Morse const DIExpression *EntryExpr) {
457fba06e3cSJeremy Morse VarLoc VL(MI, LS);
458e2196ddcSgbtozers assert(VL.Locs.size() == 1 &&
459e2196ddcSgbtozers VL.Locs[0].Kind == MachineLocKind::RegisterKind);
460e2196ddcSgbtozers VL.EVKind = EntryValueLocKind::EntryValueBackupKind;
461fba06e3cSJeremy Morse VL.Expr = EntryExpr;
462fba06e3cSJeremy Morse return VL;
463fba06e3cSJeremy Morse }
464fba06e3cSJeremy Morse
465fba06e3cSJeremy Morse /// Take the variable and machine-location from the DBG_VALUE (from the
466fba06e3cSJeremy Morse /// function entry), and build a copy of an entry value backup location by
467fba06e3cSJeremy Morse /// setting the register location to NewReg.
CreateEntryCopyBackupLoc__anon32558ef20111::VarLocBasedLDV::VarLoc468fba06e3cSJeremy Morse static VarLoc CreateEntryCopyBackupLoc(const MachineInstr &MI,
469fba06e3cSJeremy Morse LexicalScopes &LS,
470fba06e3cSJeremy Morse const DIExpression *EntryExpr,
471fba06e3cSJeremy Morse Register NewReg) {
472fba06e3cSJeremy Morse VarLoc VL(MI, LS);
473e2196ddcSgbtozers assert(VL.Locs.size() == 1 &&
474e2196ddcSgbtozers VL.Locs[0].Kind == MachineLocKind::RegisterKind);
475e2196ddcSgbtozers VL.EVKind = EntryValueLocKind::EntryValueCopyBackupKind;
476fba06e3cSJeremy Morse VL.Expr = EntryExpr;
477e2196ddcSgbtozers VL.Locs[0].Value.RegNo = NewReg;
478fba06e3cSJeremy Morse return VL;
479fba06e3cSJeremy Morse }
480fba06e3cSJeremy Morse
481fba06e3cSJeremy Morse /// Copy the register location in DBG_VALUE MI, updating the register to
482fba06e3cSJeremy Morse /// be NewReg.
CreateCopyLoc__anon32558ef20111::VarLocBasedLDV::VarLoc483e2196ddcSgbtozers static VarLoc CreateCopyLoc(const VarLoc &OldVL, const MachineLoc &OldML,
484fba06e3cSJeremy Morse Register NewReg) {
485e2196ddcSgbtozers VarLoc VL = OldVL;
4863aed2822SKazu Hirata for (MachineLoc &ML : VL.Locs)
4873aed2822SKazu Hirata if (ML == OldML) {
4883aed2822SKazu Hirata ML.Kind = MachineLocKind::RegisterKind;
4893aed2822SKazu Hirata ML.Value.RegNo = NewReg;
490fba06e3cSJeremy Morse return VL;
491fba06e3cSJeremy Morse }
492e2196ddcSgbtozers llvm_unreachable("Should have found OldML in new VarLoc.");
493e2196ddcSgbtozers }
494fba06e3cSJeremy Morse
495e2196ddcSgbtozers /// Take the variable described by DBG_VALUE* MI, and create a VarLoc
496fba06e3cSJeremy Morse /// locating it in the specified spill location.
CreateSpillLoc__anon32558ef20111::VarLocBasedLDV::VarLoc497e2196ddcSgbtozers static VarLoc CreateSpillLoc(const VarLoc &OldVL, const MachineLoc &OldML,
498e2196ddcSgbtozers unsigned SpillBase, StackOffset SpillOffset) {
499e2196ddcSgbtozers VarLoc VL = OldVL;
5003aed2822SKazu Hirata for (MachineLoc &ML : VL.Locs)
5013aed2822SKazu Hirata if (ML == OldML) {
5023aed2822SKazu Hirata ML.Kind = MachineLocKind::SpillLocKind;
5033aed2822SKazu Hirata ML.Value.SpillLocation = {SpillBase, SpillOffset};
504fba06e3cSJeremy Morse return VL;
505fba06e3cSJeremy Morse }
506e2196ddcSgbtozers llvm_unreachable("Should have found OldML in new VarLoc.");
507e2196ddcSgbtozers }
508fba06e3cSJeremy Morse
509fba06e3cSJeremy Morse /// Create a DBG_VALUE representing this VarLoc in the given function.
510fba06e3cSJeremy Morse /// Copies variable-specific information such as DILocalVariable and
511fba06e3cSJeremy Morse /// inlining information from the original DBG_VALUE instruction, which may
512fba06e3cSJeremy Morse /// have been several transfers ago.
BuildDbgValue__anon32558ef20111::VarLocBasedLDV::VarLoc513fba06e3cSJeremy Morse MachineInstr *BuildDbgValue(MachineFunction &MF) const {
514e2196ddcSgbtozers assert(!isEntryBackupLoc() &&
515e2196ddcSgbtozers "Tried to produce DBG_VALUE for backup VarLoc");
516fba06e3cSJeremy Morse const DebugLoc &DbgLoc = MI.getDebugLoc();
517fba06e3cSJeremy Morse bool Indirect = MI.isIndirectDebugValue();
518fba06e3cSJeremy Morse const auto &IID = MI.getDesc();
519fba06e3cSJeremy Morse const DILocalVariable *Var = MI.getDebugVariable();
520fba06e3cSJeremy Morse NumInserted++;
521fba06e3cSJeremy Morse
522e2196ddcSgbtozers const DIExpression *DIExpr = Expr;
523e2196ddcSgbtozers SmallVector<MachineOperand, 8> MOs;
524e2196ddcSgbtozers for (unsigned I = 0, E = Locs.size(); I < E; ++I) {
525e2196ddcSgbtozers MachineLocKind LocKind = Locs[I].Kind;
526e2196ddcSgbtozers MachineLocValue Loc = Locs[I].Value;
527e2196ddcSgbtozers const MachineOperand &Orig = MI.getDebugOperand(OrigLocMap[I]);
528e2196ddcSgbtozers switch (LocKind) {
529e2196ddcSgbtozers case MachineLocKind::RegisterKind:
530fba06e3cSJeremy Morse // An entry value is a register location -- but with an updated
531e2196ddcSgbtozers // expression. The register location of such DBG_VALUE is always the
532e2196ddcSgbtozers // one from the entry DBG_VALUE, it does not matter if the entry value
533e2196ddcSgbtozers // was copied in to another register due to some optimizations.
534e2196ddcSgbtozers // Non-entry value register locations are like the source
535e2196ddcSgbtozers // DBG_VALUE, but with the register number from this VarLoc.
536e2196ddcSgbtozers MOs.push_back(MachineOperand::CreateReg(
537e2196ddcSgbtozers EVKind == EntryValueLocKind::EntryValueKind ? Orig.getReg()
538e2196ddcSgbtozers : Register(Loc.RegNo),
539e2196ddcSgbtozers false));
540e2196ddcSgbtozers break;
541e2196ddcSgbtozers case MachineLocKind::SpillLocKind: {
542fba06e3cSJeremy Morse // Spills are indirect DBG_VALUEs, with a base register and offset.
543fba06e3cSJeremy Morse // Use the original DBG_VALUEs expression to build the spilt location
544fba06e3cSJeremy Morse // on top of. FIXME: spill locations created before this pass runs
545fba06e3cSJeremy Morse // are not recognized, and not handled here.
546fba06e3cSJeremy Morse unsigned Base = Loc.SpillLocation.SpillBase;
547e2196ddcSgbtozers auto *TRI = MF.getSubtarget().getRegisterInfo();
548e2196ddcSgbtozers if (MI.isNonListDebugValue()) {
54993b09a2aSEvgeny Leviant auto Deref = Indirect ? DIExpression::DerefAfter : 0;
55093b09a2aSEvgeny Leviant DIExpr = TRI->prependOffsetExpression(
55193b09a2aSEvgeny Leviant DIExpr, DIExpression::ApplyOffset | Deref,
552e2196ddcSgbtozers Loc.SpillLocation.SpillOffset);
553e2196ddcSgbtozers Indirect = true;
554e2196ddcSgbtozers } else {
555e2196ddcSgbtozers SmallVector<uint64_t, 4> Ops;
556e2196ddcSgbtozers TRI->getOffsetOpcodes(Loc.SpillLocation.SpillOffset, Ops);
557e2196ddcSgbtozers Ops.push_back(dwarf::DW_OP_deref);
558e2196ddcSgbtozers DIExpr = DIExpression::appendOpsToArg(DIExpr, Ops, I);
559fba06e3cSJeremy Morse }
560e2196ddcSgbtozers MOs.push_back(MachineOperand::CreateReg(Base, false));
561e2196ddcSgbtozers break;
562fba06e3cSJeremy Morse }
563e2196ddcSgbtozers case MachineLocKind::ImmediateKind: {
564e2196ddcSgbtozers MOs.push_back(Orig);
565e2196ddcSgbtozers break;
566fba06e3cSJeremy Morse }
567e2196ddcSgbtozers case MachineLocKind::InvalidKind:
568e2196ddcSgbtozers llvm_unreachable("Tried to produce DBG_VALUE for invalid VarLoc");
569e2196ddcSgbtozers }
570e2196ddcSgbtozers }
571e2196ddcSgbtozers return BuildMI(MF, DbgLoc, IID, Indirect, MOs, Var, DIExpr);
572fba06e3cSJeremy Morse }
573fba06e3cSJeremy Morse
574fba06e3cSJeremy Morse /// Is the Loc field a constant or constant object?
isConstant__anon32558ef20111::VarLocBasedLDV::VarLoc575e2196ddcSgbtozers bool isConstant(MachineLocKind Kind) const {
576e2196ddcSgbtozers return Kind == MachineLocKind::ImmediateKind;
577e2196ddcSgbtozers }
578fba06e3cSJeremy Morse
579fba06e3cSJeremy Morse /// Check if the Loc field is an entry backup location.
isEntryBackupLoc__anon32558ef20111::VarLocBasedLDV::VarLoc580fba06e3cSJeremy Morse bool isEntryBackupLoc() const {
581e2196ddcSgbtozers return EVKind == EntryValueLocKind::EntryValueBackupKind ||
582e2196ddcSgbtozers EVKind == EntryValueLocKind::EntryValueCopyBackupKind;
583fba06e3cSJeremy Morse }
584fba06e3cSJeremy Morse
585e2196ddcSgbtozers /// If this variable is described by register \p Reg holding the entry
586e2196ddcSgbtozers /// value, return true.
isEntryValueBackupReg__anon32558ef20111::VarLocBasedLDV::VarLoc587e2196ddcSgbtozers bool isEntryValueBackupReg(Register Reg) const {
588e2196ddcSgbtozers return EVKind == EntryValueLocKind::EntryValueBackupKind && usesReg(Reg);
589fba06e3cSJeremy Morse }
590fba06e3cSJeremy Morse
591e2196ddcSgbtozers /// If this variable is described by register \p Reg holding a copy of the
592e2196ddcSgbtozers /// entry value, return true.
isEntryValueCopyBackupReg__anon32558ef20111::VarLocBasedLDV::VarLoc593e2196ddcSgbtozers bool isEntryValueCopyBackupReg(Register Reg) const {
594e2196ddcSgbtozers return EVKind == EntryValueLocKind::EntryValueCopyBackupKind &&
595e2196ddcSgbtozers usesReg(Reg);
596fba06e3cSJeremy Morse }
597fba06e3cSJeremy Morse
598e2196ddcSgbtozers /// If this variable is described in whole or part by \p Reg, return true.
usesReg__anon32558ef20111::VarLocBasedLDV::VarLoc599e2196ddcSgbtozers bool usesReg(Register Reg) const {
600e2196ddcSgbtozers MachineLoc RegML;
601e2196ddcSgbtozers RegML.Kind = MachineLocKind::RegisterKind;
602e2196ddcSgbtozers RegML.Value.RegNo = Reg;
603e2196ddcSgbtozers return is_contained(Locs, RegML);
604e2196ddcSgbtozers }
605e2196ddcSgbtozers
606e2196ddcSgbtozers /// If this variable is described in whole or part by \p Reg, return true.
getRegIdx__anon32558ef20111::VarLocBasedLDV::VarLoc607e2196ddcSgbtozers unsigned getRegIdx(Register Reg) const {
608e2196ddcSgbtozers for (unsigned Idx = 0; Idx < Locs.size(); ++Idx)
609e2196ddcSgbtozers if (Locs[Idx].Kind == MachineLocKind::RegisterKind &&
610dfb213c2SMarcelo Juchem Register{static_cast<unsigned>(Locs[Idx].Value.RegNo)} == Reg)
611e2196ddcSgbtozers return Idx;
612e2196ddcSgbtozers llvm_unreachable("Could not find given Reg in Locs");
613e2196ddcSgbtozers }
614e2196ddcSgbtozers
615e2196ddcSgbtozers /// If this variable is described in whole or part by 1 or more registers,
616e2196ddcSgbtozers /// add each of them to \p Regs and return true.
getDescribingRegs__anon32558ef20111::VarLocBasedLDV::VarLoc617e2196ddcSgbtozers bool getDescribingRegs(SmallVectorImpl<uint32_t> &Regs) const {
618e2196ddcSgbtozers bool AnyRegs = false;
6194af76434SSimon Pilgrim for (const auto &Loc : Locs)
620e2196ddcSgbtozers if (Loc.Kind == MachineLocKind::RegisterKind) {
621e2196ddcSgbtozers Regs.push_back(Loc.Value.RegNo);
622e2196ddcSgbtozers AnyRegs = true;
623e2196ddcSgbtozers }
624e2196ddcSgbtozers return AnyRegs;
625e2196ddcSgbtozers }
626e2196ddcSgbtozers
containsSpillLocs__anon32558ef20111::VarLocBasedLDV::VarLoc627e2196ddcSgbtozers bool containsSpillLocs() const {
628e2196ddcSgbtozers return any_of(Locs, [](VarLoc::MachineLoc ML) {
629e2196ddcSgbtozers return ML.Kind == VarLoc::MachineLocKind::SpillLocKind;
630e2196ddcSgbtozers });
631e2196ddcSgbtozers }
632e2196ddcSgbtozers
633e2196ddcSgbtozers /// If this variable is described in whole or part by \p SpillLocation,
634e2196ddcSgbtozers /// return true.
usesSpillLoc__anon32558ef20111::VarLocBasedLDV::VarLoc635e2196ddcSgbtozers bool usesSpillLoc(SpillLoc SpillLocation) const {
636e2196ddcSgbtozers MachineLoc SpillML;
637e2196ddcSgbtozers SpillML.Kind = MachineLocKind::SpillLocKind;
638e2196ddcSgbtozers SpillML.Value.SpillLocation = SpillLocation;
639e2196ddcSgbtozers return is_contained(Locs, SpillML);
640e2196ddcSgbtozers }
641e2196ddcSgbtozers
642e2196ddcSgbtozers /// If this variable is described in whole or part by \p SpillLocation,
643e2196ddcSgbtozers /// return the index .
getSpillLocIdx__anon32558ef20111::VarLocBasedLDV::VarLoc644e2196ddcSgbtozers unsigned getSpillLocIdx(SpillLoc SpillLocation) const {
645e2196ddcSgbtozers for (unsigned Idx = 0; Idx < Locs.size(); ++Idx)
646e2196ddcSgbtozers if (Locs[Idx].Kind == MachineLocKind::SpillLocKind &&
647e2196ddcSgbtozers Locs[Idx].Value.SpillLocation == SpillLocation)
648e2196ddcSgbtozers return Idx;
649e2196ddcSgbtozers llvm_unreachable("Could not find given SpillLoc in Locs");
650fba06e3cSJeremy Morse }
651fba06e3cSJeremy Morse
652fba06e3cSJeremy Morse /// Determine whether the lexical scope of this value's debug location
653fba06e3cSJeremy Morse /// dominates MBB.
dominates__anon32558ef20111::VarLocBasedLDV::VarLoc654fba06e3cSJeremy Morse bool dominates(LexicalScopes &LS, MachineBasicBlock &MBB) const {
655fba06e3cSJeremy Morse return LS.dominates(MI.getDebugLoc().get(), &MBB);
656fba06e3cSJeremy Morse }
657fba06e3cSJeremy Morse
658fba06e3cSJeremy Morse #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
659fba06e3cSJeremy Morse // TRI can be null.
dump__anon32558ef20111::VarLocBasedLDV::VarLoc660fba06e3cSJeremy Morse void dump(const TargetRegisterInfo *TRI, raw_ostream &Out = dbgs()) const {
661fba06e3cSJeremy Morse Out << "VarLoc(";
662e2196ddcSgbtozers for (const MachineLoc &MLoc : Locs) {
663e2196ddcSgbtozers if (Locs.begin() != &MLoc)
664e2196ddcSgbtozers Out << ", ";
665e2196ddcSgbtozers switch (MLoc.Kind) {
666e2196ddcSgbtozers case MachineLocKind::RegisterKind:
667e2196ddcSgbtozers Out << printReg(MLoc.Value.RegNo, TRI);
668fba06e3cSJeremy Morse break;
669e2196ddcSgbtozers case MachineLocKind::SpillLocKind:
670e2196ddcSgbtozers Out << printReg(MLoc.Value.SpillLocation.SpillBase, TRI);
671e2196ddcSgbtozers Out << "[" << MLoc.Value.SpillLocation.SpillOffset.getFixed() << " + "
672e2196ddcSgbtozers << MLoc.Value.SpillLocation.SpillOffset.getScalable()
673e2196ddcSgbtozers << "x vscale"
67484a11209SSander de Smalen << "]";
675fba06e3cSJeremy Morse break;
676e2196ddcSgbtozers case MachineLocKind::ImmediateKind:
677e2196ddcSgbtozers Out << MLoc.Value.Immediate;
678fba06e3cSJeremy Morse break;
679e2196ddcSgbtozers case MachineLocKind::InvalidKind:
680fba06e3cSJeremy Morse llvm_unreachable("Invalid VarLoc in dump method");
681fba06e3cSJeremy Morse }
682e2196ddcSgbtozers }
683fba06e3cSJeremy Morse
684fba06e3cSJeremy Morse Out << ", \"" << Var.getVariable()->getName() << "\", " << *Expr << ", ";
685fba06e3cSJeremy Morse if (Var.getInlinedAt())
686fba06e3cSJeremy Morse Out << "!" << Var.getInlinedAt()->getMetadataID() << ")\n";
687fba06e3cSJeremy Morse else
688fba06e3cSJeremy Morse Out << "(null))";
689fba06e3cSJeremy Morse
690fba06e3cSJeremy Morse if (isEntryBackupLoc())
691fba06e3cSJeremy Morse Out << " (backup loc)\n";
692fba06e3cSJeremy Morse else
693fba06e3cSJeremy Morse Out << "\n";
694fba06e3cSJeremy Morse }
695fba06e3cSJeremy Morse #endif
696fba06e3cSJeremy Morse
operator ==__anon32558ef20111::VarLocBasedLDV::VarLoc697fba06e3cSJeremy Morse bool operator==(const VarLoc &Other) const {
698e2196ddcSgbtozers return std::tie(EVKind, Var, Expr, Locs) ==
699e2196ddcSgbtozers std::tie(Other.EVKind, Other.Var, Other.Expr, Other.Locs);
700fba06e3cSJeremy Morse }
701fba06e3cSJeremy Morse
702fba06e3cSJeremy Morse /// This operator guarantees that VarLocs are sorted by Variable first.
operator <__anon32558ef20111::VarLocBasedLDV::VarLoc703fba06e3cSJeremy Morse bool operator<(const VarLoc &Other) const {
704e2196ddcSgbtozers return std::tie(Var, EVKind, Locs, Expr) <
705e2196ddcSgbtozers std::tie(Other.Var, Other.EVKind, Other.Locs, Other.Expr);
706fba06e3cSJeremy Morse }
707fba06e3cSJeremy Morse };
708fba06e3cSJeremy Morse
709e2196ddcSgbtozers #ifndef NDEBUG
710e2196ddcSgbtozers using VarVec = SmallVector<VarLoc, 32>;
711e2196ddcSgbtozers #endif
712e2196ddcSgbtozers
713fba06e3cSJeremy Morse /// VarLocMap is used for two things:
714e2196ddcSgbtozers /// 1) Assigning LocIndices to a VarLoc. The LocIndices can be used to
715fba06e3cSJeremy Morse /// virtually insert a VarLoc into a VarLocSet.
716fba06e3cSJeremy Morse /// 2) Given a LocIndex, look up the unique associated VarLoc.
717fba06e3cSJeremy Morse class VarLocMap {
718fba06e3cSJeremy Morse /// Map a VarLoc to an index within the vector reserved for its location
719fba06e3cSJeremy Morse /// within Loc2Vars.
720e2196ddcSgbtozers std::map<VarLoc, LocIndices> Var2Indices;
721fba06e3cSJeremy Morse
722fba06e3cSJeremy Morse /// Map a location to a vector which holds VarLocs which live in that
723fba06e3cSJeremy Morse /// location.
724fba06e3cSJeremy Morse SmallDenseMap<LocIndex::u32_location_t, std::vector<VarLoc>> Loc2Vars;
725fba06e3cSJeremy Morse
726e2196ddcSgbtozers public:
727e2196ddcSgbtozers /// Retrieve LocIndices for \p VL.
insert(const VarLoc & VL)728e2196ddcSgbtozers LocIndices insert(const VarLoc &VL) {
729e2196ddcSgbtozers LocIndices &Indices = Var2Indices[VL];
730e2196ddcSgbtozers // If Indices is not empty, VL is already in the map.
731e2196ddcSgbtozers if (!Indices.empty())
732e2196ddcSgbtozers return Indices;
733e2196ddcSgbtozers SmallVector<LocIndex::u32_location_t, 4> Locations;
734e2196ddcSgbtozers // LocIndices are determined by EVKind and MLs; each Register has a
735e2196ddcSgbtozers // unique location, while all SpillLocs use a single bucket, and any EV
736e2196ddcSgbtozers // VarLocs use only the Backup bucket or none at all (except the
737e2196ddcSgbtozers // compulsory entry at the universal location index). LocIndices will
738e2196ddcSgbtozers // always have an index at the universal location index as the last index.
739e2196ddcSgbtozers if (VL.EVKind == VarLoc::EntryValueLocKind::NonEntryValueKind) {
740e2196ddcSgbtozers VL.getDescribingRegs(Locations);
741e2196ddcSgbtozers assert(all_of(Locations,
742e2196ddcSgbtozers [](auto RegNo) {
743e2196ddcSgbtozers return RegNo < LocIndex::kFirstInvalidRegLocation;
744e2196ddcSgbtozers }) &&
745fba06e3cSJeremy Morse "Physreg out of range?");
746e2196ddcSgbtozers if (VL.containsSpillLocs()) {
747e2196ddcSgbtozers LocIndex::u32_location_t Loc = LocIndex::kSpillLocation;
748e2196ddcSgbtozers Locations.push_back(Loc);
749fba06e3cSJeremy Morse }
750e2196ddcSgbtozers } else if (VL.EVKind != VarLoc::EntryValueLocKind::EntryValueKind) {
751e2196ddcSgbtozers LocIndex::u32_location_t Loc = LocIndex::kEntryValueBackupLocation;
752e2196ddcSgbtozers Locations.push_back(Loc);
753e2196ddcSgbtozers }
754e2196ddcSgbtozers Locations.push_back(LocIndex::kUniversalLocation);
755e2196ddcSgbtozers for (LocIndex::u32_location_t Location : Locations) {
756e2196ddcSgbtozers auto &Vars = Loc2Vars[Location];
757e2196ddcSgbtozers Indices.push_back(
758e2196ddcSgbtozers {Location, static_cast<LocIndex::u32_index_t>(Vars.size())});
759e2196ddcSgbtozers Vars.push_back(VL);
760e2196ddcSgbtozers }
761e2196ddcSgbtozers return Indices;
762fba06e3cSJeremy Morse }
763fba06e3cSJeremy Morse
getAllIndices(const VarLoc & VL) const764e2196ddcSgbtozers LocIndices getAllIndices(const VarLoc &VL) const {
765e2196ddcSgbtozers auto IndIt = Var2Indices.find(VL);
766e2196ddcSgbtozers assert(IndIt != Var2Indices.end() && "VarLoc not tracked");
767e2196ddcSgbtozers return IndIt->second;
768fba06e3cSJeremy Morse }
769fba06e3cSJeremy Morse
770fba06e3cSJeremy Morse /// Retrieve the unique VarLoc associated with \p ID.
operator [](LocIndex ID) const771fba06e3cSJeremy Morse const VarLoc &operator[](LocIndex ID) const {
772fba06e3cSJeremy Morse auto LocIt = Loc2Vars.find(ID.Location);
773fba06e3cSJeremy Morse assert(LocIt != Loc2Vars.end() && "Location not tracked");
774fba06e3cSJeremy Morse return LocIt->second[ID.Index];
775fba06e3cSJeremy Morse }
776fba06e3cSJeremy Morse };
777fba06e3cSJeremy Morse
778fba06e3cSJeremy Morse using VarLocInMBB =
779fba06e3cSJeremy Morse SmallDenseMap<const MachineBasicBlock *, std::unique_ptr<VarLocSet>>;
780fba06e3cSJeremy Morse struct TransferDebugPair {
781fba06e3cSJeremy Morse MachineInstr *TransferInst; ///< Instruction where this transfer occurs.
782fba06e3cSJeremy Morse LocIndex LocationID; ///< Location number for the transfer dest.
783fba06e3cSJeremy Morse };
784fba06e3cSJeremy Morse using TransferMap = SmallVector<TransferDebugPair, 4>;
78586f5288eSDjordje Todorovic // Types for recording Entry Var Locations emitted by a single MachineInstr,
78686f5288eSDjordje Todorovic // as well as recording MachineInstr which last defined a register.
78786f5288eSDjordje Todorovic using InstToEntryLocMap = std::multimap<const MachineInstr *, LocIndex>;
78886f5288eSDjordje Todorovic using RegDefToInstMap = DenseMap<Register, MachineInstr *>;
789fba06e3cSJeremy Morse
790fba06e3cSJeremy Morse // Types for recording sets of variable fragments that overlap. For a given
791fba06e3cSJeremy Morse // local variable, we record all other fragments of that variable that could
792fba06e3cSJeremy Morse // overlap it, to reduce search time.
793fba06e3cSJeremy Morse using FragmentOfVar =
794fba06e3cSJeremy Morse std::pair<const DILocalVariable *, DIExpression::FragmentInfo>;
795fba06e3cSJeremy Morse using OverlapMap =
796fba06e3cSJeremy Morse DenseMap<FragmentOfVar, SmallVector<DIExpression::FragmentInfo, 1>>;
797fba06e3cSJeremy Morse
798fba06e3cSJeremy Morse // Helper while building OverlapMap, a map of all fragments seen for a given
799fba06e3cSJeremy Morse // DILocalVariable.
800fba06e3cSJeremy Morse using VarToFragments =
801fba06e3cSJeremy Morse DenseMap<const DILocalVariable *, SmallSet<FragmentInfo, 4>>;
802fba06e3cSJeremy Morse
803e2196ddcSgbtozers /// Collects all VarLocs from \p CollectFrom. Each unique VarLoc is added
804e2196ddcSgbtozers /// to \p Collected once, in order of insertion into \p VarLocIDs.
805e2196ddcSgbtozers static void collectAllVarLocs(SmallVectorImpl<VarLoc> &Collected,
806e2196ddcSgbtozers const VarLocSet &CollectFrom,
807e2196ddcSgbtozers const VarLocMap &VarLocIDs);
808e2196ddcSgbtozers
809e2196ddcSgbtozers /// Get the registers which are used by VarLocs of kind RegisterKind tracked
810e2196ddcSgbtozers /// by \p CollectFrom.
811e2196ddcSgbtozers void getUsedRegs(const VarLocSet &CollectFrom,
812e2196ddcSgbtozers SmallVectorImpl<Register> &UsedRegs) const;
813e2196ddcSgbtozers
814fba06e3cSJeremy Morse /// This holds the working set of currently open ranges. For fast
815fba06e3cSJeremy Morse /// access, this is done both as a set of VarLocIDs, and a map of
816fba06e3cSJeremy Morse /// DebugVariable to recent VarLocID. Note that a DBG_VALUE ends all
817fba06e3cSJeremy Morse /// previous open ranges for the same variable. In addition, we keep
818fba06e3cSJeremy Morse /// two different maps (Vars/EntryValuesBackupVars), so erase/insert
819fba06e3cSJeremy Morse /// methods act differently depending on whether a VarLoc is primary
820fba06e3cSJeremy Morse /// location or backup one. In the case the VarLoc is backup location
821fba06e3cSJeremy Morse /// we will erase/insert from the EntryValuesBackupVars map, otherwise
822fba06e3cSJeremy Morse /// we perform the operation on the Vars.
823fba06e3cSJeremy Morse class OpenRangesSet {
824e2196ddcSgbtozers VarLocSet::Allocator &Alloc;
825fba06e3cSJeremy Morse VarLocSet VarLocs;
826fba06e3cSJeremy Morse // Map the DebugVariable to recent primary location ID.
827e2196ddcSgbtozers SmallDenseMap<DebugVariable, LocIndices, 8> Vars;
828fba06e3cSJeremy Morse // Map the DebugVariable to recent backup location ID.
829e2196ddcSgbtozers SmallDenseMap<DebugVariable, LocIndices, 8> EntryValuesBackupVars;
830fba06e3cSJeremy Morse OverlapMap &OverlappingFragments;
831fba06e3cSJeremy Morse
832fba06e3cSJeremy Morse public:
OpenRangesSet(VarLocSet::Allocator & Alloc,OverlapMap & _OLapMap)833fba06e3cSJeremy Morse OpenRangesSet(VarLocSet::Allocator &Alloc, OverlapMap &_OLapMap)
834e2196ddcSgbtozers : Alloc(Alloc), VarLocs(Alloc), OverlappingFragments(_OLapMap) {}
835fba06e3cSJeremy Morse
getVarLocs() const836fba06e3cSJeremy Morse const VarLocSet &getVarLocs() const { return VarLocs; }
837fba06e3cSJeremy Morse
838e2196ddcSgbtozers // Fetches all VarLocs in \p VarLocIDs and inserts them into \p Collected.
839e2196ddcSgbtozers // This method is needed to get every VarLoc once, as each VarLoc may have
840e2196ddcSgbtozers // multiple indices in a VarLocMap (corresponding to each applicable
841e2196ddcSgbtozers // location), but all VarLocs appear exactly once at the universal location
842e2196ddcSgbtozers // index.
getUniqueVarLocs(SmallVectorImpl<VarLoc> & Collected,const VarLocMap & VarLocIDs) const843e2196ddcSgbtozers void getUniqueVarLocs(SmallVectorImpl<VarLoc> &Collected,
844e2196ddcSgbtozers const VarLocMap &VarLocIDs) const {
845e2196ddcSgbtozers collectAllVarLocs(Collected, VarLocs, VarLocIDs);
846e2196ddcSgbtozers }
847e2196ddcSgbtozers
848fba06e3cSJeremy Morse /// Terminate all open ranges for VL.Var by removing it from the set.
849fba06e3cSJeremy Morse void erase(const VarLoc &VL);
850fba06e3cSJeremy Morse
851e2196ddcSgbtozers /// Terminate all open ranges listed as indices in \c KillSet with
852e2196ddcSgbtozers /// \c Location by removing them from the set.
853e2196ddcSgbtozers void erase(const VarLocsInRange &KillSet, const VarLocMap &VarLocIDs,
854e2196ddcSgbtozers LocIndex::u32_location_t Location);
855fba06e3cSJeremy Morse
856fba06e3cSJeremy Morse /// Insert a new range into the set.
857e2196ddcSgbtozers void insert(LocIndices VarLocIDs, const VarLoc &VL);
858fba06e3cSJeremy Morse
859fba06e3cSJeremy Morse /// Insert a set of ranges.
860e2196ddcSgbtozers void insertFromLocSet(const VarLocSet &ToLoad, const VarLocMap &Map);
861fba06e3cSJeremy Morse
862e2196ddcSgbtozers llvm::Optional<LocIndices> getEntryValueBackup(DebugVariable Var);
863fba06e3cSJeremy Morse
864fba06e3cSJeremy Morse /// Empty the set.
clear()865fba06e3cSJeremy Morse void clear() {
866fba06e3cSJeremy Morse VarLocs.clear();
867fba06e3cSJeremy Morse Vars.clear();
868fba06e3cSJeremy Morse EntryValuesBackupVars.clear();
869fba06e3cSJeremy Morse }
870fba06e3cSJeremy Morse
871fba06e3cSJeremy Morse /// Return whether the set is empty or not.
empty() const872fba06e3cSJeremy Morse bool empty() const {
873fba06e3cSJeremy Morse assert(Vars.empty() == EntryValuesBackupVars.empty() &&
874fba06e3cSJeremy Morse Vars.empty() == VarLocs.empty() &&
875fba06e3cSJeremy Morse "open ranges are inconsistent");
876fba06e3cSJeremy Morse return VarLocs.empty();
877fba06e3cSJeremy Morse }
878fba06e3cSJeremy Morse
879fba06e3cSJeremy Morse /// Get an empty range of VarLoc IDs.
getEmptyVarLocRange() const880fba06e3cSJeremy Morse auto getEmptyVarLocRange() const {
881fba06e3cSJeremy Morse return iterator_range<VarLocSet::const_iterator>(getVarLocs().end(),
882fba06e3cSJeremy Morse getVarLocs().end());
883fba06e3cSJeremy Morse }
884fba06e3cSJeremy Morse
885e2196ddcSgbtozers /// Get all set IDs for VarLocs with MLs of kind RegisterKind in \p Reg.
getRegisterVarLocs(Register Reg) const886fba06e3cSJeremy Morse auto getRegisterVarLocs(Register Reg) const {
887fba06e3cSJeremy Morse return LocIndex::indexRangeForLocation(getVarLocs(), Reg);
888fba06e3cSJeremy Morse }
889fba06e3cSJeremy Morse
890e2196ddcSgbtozers /// Get all set IDs for VarLocs with MLs of kind SpillLocKind.
getSpillVarLocs() const891fba06e3cSJeremy Morse auto getSpillVarLocs() const {
892fba06e3cSJeremy Morse return LocIndex::indexRangeForLocation(getVarLocs(),
893fba06e3cSJeremy Morse LocIndex::kSpillLocation);
894fba06e3cSJeremy Morse }
895fba06e3cSJeremy Morse
896e2196ddcSgbtozers /// Get all set IDs for VarLocs of EVKind EntryValueBackupKind or
897fba06e3cSJeremy Morse /// EntryValueCopyBackupKind.
getEntryValueBackupVarLocs() const898fba06e3cSJeremy Morse auto getEntryValueBackupVarLocs() const {
899fba06e3cSJeremy Morse return LocIndex::indexRangeForLocation(
900fba06e3cSJeremy Morse getVarLocs(), LocIndex::kEntryValueBackupLocation);
901fba06e3cSJeremy Morse }
902fba06e3cSJeremy Morse };
903fba06e3cSJeremy Morse
904e2196ddcSgbtozers /// Collect all VarLoc IDs from \p CollectFrom for VarLocs with MLs of kind
905e2196ddcSgbtozers /// RegisterKind which are located in any reg in \p Regs. The IDs for each
906e2196ddcSgbtozers /// VarLoc correspond to entries in the universal location bucket, which every
907e2196ddcSgbtozers /// VarLoc has exactly 1 entry for. Insert collected IDs into \p Collected.
908e2196ddcSgbtozers static void collectIDsForRegs(VarLocsInRange &Collected,
909e2196ddcSgbtozers const DefinedRegsSet &Regs,
910e2196ddcSgbtozers const VarLocSet &CollectFrom,
911e2196ddcSgbtozers const VarLocMap &VarLocIDs);
912fba06e3cSJeremy Morse
getVarLocsInMBB(const MachineBasicBlock * MBB,VarLocInMBB & Locs)913fba06e3cSJeremy Morse VarLocSet &getVarLocsInMBB(const MachineBasicBlock *MBB, VarLocInMBB &Locs) {
914fba06e3cSJeremy Morse std::unique_ptr<VarLocSet> &VLS = Locs[MBB];
915fba06e3cSJeremy Morse if (!VLS)
916fba06e3cSJeremy Morse VLS = std::make_unique<VarLocSet>(Alloc);
9171eada2adSKazu Hirata return *VLS;
918fba06e3cSJeremy Morse }
919fba06e3cSJeremy Morse
getVarLocsInMBB(const MachineBasicBlock * MBB,const VarLocInMBB & Locs) const920fba06e3cSJeremy Morse const VarLocSet &getVarLocsInMBB(const MachineBasicBlock *MBB,
921fba06e3cSJeremy Morse const VarLocInMBB &Locs) const {
922fba06e3cSJeremy Morse auto It = Locs.find(MBB);
923fba06e3cSJeremy Morse assert(It != Locs.end() && "MBB not in map");
9241eada2adSKazu Hirata return *It->second;
925fba06e3cSJeremy Morse }
926fba06e3cSJeremy Morse
927fba06e3cSJeremy Morse /// Tests whether this instruction is a spill to a stack location.
928fba06e3cSJeremy Morse bool isSpillInstruction(const MachineInstr &MI, MachineFunction *MF);
929fba06e3cSJeremy Morse
930fba06e3cSJeremy Morse /// Decide if @MI is a spill instruction and return true if it is. We use 2
931fba06e3cSJeremy Morse /// criteria to make this decision:
932fba06e3cSJeremy Morse /// - Is this instruction a store to a spill slot?
933fba06e3cSJeremy Morse /// - Is there a register operand that is both used and killed?
934fba06e3cSJeremy Morse /// TODO: Store optimization can fold spills into other stores (including
935fba06e3cSJeremy Morse /// other spills). We do not handle this yet (more than one memory operand).
936fba06e3cSJeremy Morse bool isLocationSpill(const MachineInstr &MI, MachineFunction *MF,
937fba06e3cSJeremy Morse Register &Reg);
938fba06e3cSJeremy Morse
939fba06e3cSJeremy Morse /// Returns true if the given machine instruction is a debug value which we
940fba06e3cSJeremy Morse /// can emit entry values for.
941fba06e3cSJeremy Morse ///
942fba06e3cSJeremy Morse /// Currently, we generate debug entry values only for parameters that are
943fba06e3cSJeremy Morse /// unmodified throughout the function and located in a register.
944fba06e3cSJeremy Morse bool isEntryValueCandidate(const MachineInstr &MI,
945fba06e3cSJeremy Morse const DefinedRegsSet &Regs) const;
946fba06e3cSJeremy Morse
947fba06e3cSJeremy Morse /// If a given instruction is identified as a spill, return the spill location
948fba06e3cSJeremy Morse /// and set \p Reg to the spilled register.
949fba06e3cSJeremy Morse Optional<VarLoc::SpillLoc> isRestoreInstruction(const MachineInstr &MI,
950fba06e3cSJeremy Morse MachineFunction *MF,
951fba06e3cSJeremy Morse Register &Reg);
952fba06e3cSJeremy Morse /// Given a spill instruction, extract the register and offset used to
953fba06e3cSJeremy Morse /// address the spill location in a target independent way.
954fba06e3cSJeremy Morse VarLoc::SpillLoc extractSpillBaseRegAndOffset(const MachineInstr &MI);
955fba06e3cSJeremy Morse void insertTransferDebugPair(MachineInstr &MI, OpenRangesSet &OpenRanges,
956fba06e3cSJeremy Morse TransferMap &Transfers, VarLocMap &VarLocIDs,
957fba06e3cSJeremy Morse LocIndex OldVarID, TransferKind Kind,
958e2196ddcSgbtozers const VarLoc::MachineLoc &OldLoc,
959fba06e3cSJeremy Morse Register NewReg = Register());
960fba06e3cSJeremy Morse
961fba06e3cSJeremy Morse void transferDebugValue(const MachineInstr &MI, OpenRangesSet &OpenRanges,
96286f5288eSDjordje Todorovic VarLocMap &VarLocIDs,
96386f5288eSDjordje Todorovic InstToEntryLocMap &EntryValTransfers,
96486f5288eSDjordje Todorovic RegDefToInstMap &RegSetInstrs);
965fba06e3cSJeremy Morse void transferSpillOrRestoreInst(MachineInstr &MI, OpenRangesSet &OpenRanges,
966fba06e3cSJeremy Morse VarLocMap &VarLocIDs, TransferMap &Transfers);
96786f5288eSDjordje Todorovic void cleanupEntryValueTransfers(const MachineInstr *MI,
96886f5288eSDjordje Todorovic OpenRangesSet &OpenRanges,
96986f5288eSDjordje Todorovic VarLocMap &VarLocIDs, const VarLoc &EntryVL,
97086f5288eSDjordje Todorovic InstToEntryLocMap &EntryValTransfers);
97186f5288eSDjordje Todorovic void removeEntryValue(const MachineInstr &MI, OpenRangesSet &OpenRanges,
97286f5288eSDjordje Todorovic VarLocMap &VarLocIDs, const VarLoc &EntryVL,
97386f5288eSDjordje Todorovic InstToEntryLocMap &EntryValTransfers,
97486f5288eSDjordje Todorovic RegDefToInstMap &RegSetInstrs);
975fba06e3cSJeremy Morse void emitEntryValues(MachineInstr &MI, OpenRangesSet &OpenRanges,
97686f5288eSDjordje Todorovic VarLocMap &VarLocIDs,
97786f5288eSDjordje Todorovic InstToEntryLocMap &EntryValTransfers,
978e2196ddcSgbtozers VarLocsInRange &KillSet);
979fba06e3cSJeremy Morse void recordEntryValue(const MachineInstr &MI,
980fba06e3cSJeremy Morse const DefinedRegsSet &DefinedRegs,
981fba06e3cSJeremy Morse OpenRangesSet &OpenRanges, VarLocMap &VarLocIDs);
982fba06e3cSJeremy Morse void transferRegisterCopy(MachineInstr &MI, OpenRangesSet &OpenRanges,
983fba06e3cSJeremy Morse VarLocMap &VarLocIDs, TransferMap &Transfers);
984fba06e3cSJeremy Morse void transferRegisterDef(MachineInstr &MI, OpenRangesSet &OpenRanges,
98586f5288eSDjordje Todorovic VarLocMap &VarLocIDs,
98686f5288eSDjordje Todorovic InstToEntryLocMap &EntryValTransfers,
98786f5288eSDjordje Todorovic RegDefToInstMap &RegSetInstrs);
988fba06e3cSJeremy Morse bool transferTerminator(MachineBasicBlock *MBB, OpenRangesSet &OpenRanges,
989fba06e3cSJeremy Morse VarLocInMBB &OutLocs, const VarLocMap &VarLocIDs);
990fba06e3cSJeremy Morse
991fba06e3cSJeremy Morse void process(MachineInstr &MI, OpenRangesSet &OpenRanges,
99286f5288eSDjordje Todorovic VarLocMap &VarLocIDs, TransferMap &Transfers,
99386f5288eSDjordje Todorovic InstToEntryLocMap &EntryValTransfers,
99486f5288eSDjordje Todorovic RegDefToInstMap &RegSetInstrs);
995fba06e3cSJeremy Morse
996fba06e3cSJeremy Morse void accumulateFragmentMap(MachineInstr &MI, VarToFragments &SeenFragments,
997fba06e3cSJeremy Morse OverlapMap &OLapMap);
998fba06e3cSJeremy Morse
999fba06e3cSJeremy Morse bool join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs, VarLocInMBB &InLocs,
1000fba06e3cSJeremy Morse const VarLocMap &VarLocIDs,
1001fba06e3cSJeremy Morse SmallPtrSet<const MachineBasicBlock *, 16> &Visited,
1002fba06e3cSJeremy Morse SmallPtrSetImpl<const MachineBasicBlock *> &ArtificialBlocks);
1003fba06e3cSJeremy Morse
1004fba06e3cSJeremy Morse /// Create DBG_VALUE insts for inlocs that have been propagated but
1005fba06e3cSJeremy Morse /// had their instruction creation deferred.
1006fba06e3cSJeremy Morse void flushPendingLocs(VarLocInMBB &PendingInLocs, VarLocMap &VarLocIDs);
1007fba06e3cSJeremy Morse
1008a3936a6cSJeremy Morse bool ExtendRanges(MachineFunction &MF, MachineDominatorTree *DomTree,
1009a3936a6cSJeremy Morse TargetPassConfig *TPC, unsigned InputBBLimit,
1010a3936a6cSJeremy Morse unsigned InputDbgValLimit) override;
1011fba06e3cSJeremy Morse
1012fba06e3cSJeremy Morse public:
1013fba06e3cSJeremy Morse /// Default construct and initialize the pass.
101420bb9fe5SJeremy Morse VarLocBasedLDV();
1015fba06e3cSJeremy Morse
101620bb9fe5SJeremy Morse ~VarLocBasedLDV();
1017fba06e3cSJeremy Morse
1018fba06e3cSJeremy Morse /// Print to ostream with a message.
1019fba06e3cSJeremy Morse void printVarLocInMBB(const MachineFunction &MF, const VarLocInMBB &V,
1020fba06e3cSJeremy Morse const VarLocMap &VarLocIDs, const char *msg,
1021fba06e3cSJeremy Morse raw_ostream &Out) const;
1022fba06e3cSJeremy Morse };
1023fba06e3cSJeremy Morse
1024fba06e3cSJeremy Morse } // end anonymous namespace
1025fba06e3cSJeremy Morse
1026fba06e3cSJeremy Morse //===----------------------------------------------------------------------===//
1027fba06e3cSJeremy Morse // Implementation
1028fba06e3cSJeremy Morse //===----------------------------------------------------------------------===//
1029fba06e3cSJeremy Morse
10303a8c5148SKazu Hirata VarLocBasedLDV::VarLocBasedLDV() = default;
1031fba06e3cSJeremy Morse
10323a8c5148SKazu Hirata VarLocBasedLDV::~VarLocBasedLDV() = default;
1033fba06e3cSJeremy Morse
1034fba06e3cSJeremy Morse /// Erase a variable from the set of open ranges, and additionally erase any
1035d9345999SMatt Arsenault /// fragments that may overlap it. If the VarLoc is a backup location, erase
1036fba06e3cSJeremy Morse /// the variable from the EntryValuesBackupVars set, indicating we should stop
1037fba06e3cSJeremy Morse /// tracking its backup entry location. Otherwise, if the VarLoc is primary
1038fba06e3cSJeremy Morse /// location, erase the variable from the Vars set.
erase(const VarLoc & VL)103920bb9fe5SJeremy Morse void VarLocBasedLDV::OpenRangesSet::erase(const VarLoc &VL) {
1040fba06e3cSJeremy Morse // Erasure helper.
1041fba06e3cSJeremy Morse auto DoErase = [VL, this](DebugVariable VarToErase) {
1042fba06e3cSJeremy Morse auto *EraseFrom = VL.isEntryBackupLoc() ? &EntryValuesBackupVars : &Vars;
1043fba06e3cSJeremy Morse auto It = EraseFrom->find(VarToErase);
1044fba06e3cSJeremy Morse if (It != EraseFrom->end()) {
1045e2196ddcSgbtozers LocIndices IDs = It->second;
1046e2196ddcSgbtozers for (LocIndex ID : IDs)
1047fba06e3cSJeremy Morse VarLocs.reset(ID.getAsRawInteger());
1048fba06e3cSJeremy Morse EraseFrom->erase(It);
1049fba06e3cSJeremy Morse }
1050fba06e3cSJeremy Morse };
1051fba06e3cSJeremy Morse
1052fba06e3cSJeremy Morse DebugVariable Var = VL.Var;
1053fba06e3cSJeremy Morse
1054fba06e3cSJeremy Morse // Erase the variable/fragment that ends here.
1055fba06e3cSJeremy Morse DoErase(Var);
1056fba06e3cSJeremy Morse
1057fba06e3cSJeremy Morse // Extract the fragment. Interpret an empty fragment as one that covers all
1058fba06e3cSJeremy Morse // possible bits.
1059fba06e3cSJeremy Morse FragmentInfo ThisFragment = Var.getFragmentOrDefault();
1060fba06e3cSJeremy Morse
1061fba06e3cSJeremy Morse // There may be fragments that overlap the designated fragment. Look them up
1062fba06e3cSJeremy Morse // in the pre-computed overlap map, and erase them too.
1063fba06e3cSJeremy Morse auto MapIt = OverlappingFragments.find({Var.getVariable(), ThisFragment});
1064fba06e3cSJeremy Morse if (MapIt != OverlappingFragments.end()) {
1065fba06e3cSJeremy Morse for (auto Fragment : MapIt->second) {
106620bb9fe5SJeremy Morse VarLocBasedLDV::OptFragmentInfo FragmentHolder;
1067fba06e3cSJeremy Morse if (!DebugVariable::isDefaultFragment(Fragment))
106820bb9fe5SJeremy Morse FragmentHolder = VarLocBasedLDV::OptFragmentInfo(Fragment);
1069fba06e3cSJeremy Morse DoErase({Var.getVariable(), FragmentHolder, Var.getInlinedAt()});
1070fba06e3cSJeremy Morse }
1071fba06e3cSJeremy Morse }
1072fba06e3cSJeremy Morse }
1073fba06e3cSJeremy Morse
erase(const VarLocsInRange & KillSet,const VarLocMap & VarLocIDs,LocIndex::u32_location_t Location)1074e2196ddcSgbtozers void VarLocBasedLDV::OpenRangesSet::erase(const VarLocsInRange &KillSet,
1075e2196ddcSgbtozers const VarLocMap &VarLocIDs,
1076e2196ddcSgbtozers LocIndex::u32_location_t Location) {
1077e2196ddcSgbtozers VarLocSet RemoveSet(Alloc);
1078e2196ddcSgbtozers for (LocIndex::u32_index_t ID : KillSet) {
1079e2196ddcSgbtozers const VarLoc &VL = VarLocIDs[LocIndex(Location, ID)];
1080e2196ddcSgbtozers auto *EraseFrom = VL.isEntryBackupLoc() ? &EntryValuesBackupVars : &Vars;
1081e2196ddcSgbtozers EraseFrom->erase(VL.Var);
1082e2196ddcSgbtozers LocIndices VLI = VarLocIDs.getAllIndices(VL);
1083e2196ddcSgbtozers for (LocIndex ID : VLI)
1084e2196ddcSgbtozers RemoveSet.set(ID.getAsRawInteger());
1085e2196ddcSgbtozers }
1086e2196ddcSgbtozers VarLocs.intersectWithComplement(RemoveSet);
1087e2196ddcSgbtozers }
1088e2196ddcSgbtozers
insertFromLocSet(const VarLocSet & ToLoad,const VarLocMap & Map)1089e2196ddcSgbtozers void VarLocBasedLDV::OpenRangesSet::insertFromLocSet(const VarLocSet &ToLoad,
1090e2196ddcSgbtozers const VarLocMap &Map) {
1091e2196ddcSgbtozers VarLocsInRange UniqueVarLocIDs;
1092e2196ddcSgbtozers DefinedRegsSet Regs;
1093e2196ddcSgbtozers Regs.insert(LocIndex::kUniversalLocation);
1094e2196ddcSgbtozers collectIDsForRegs(UniqueVarLocIDs, Regs, ToLoad, Map);
1095e2196ddcSgbtozers for (uint64_t ID : UniqueVarLocIDs) {
1096e2196ddcSgbtozers LocIndex Idx = LocIndex::fromRawInteger(ID);
1097e2196ddcSgbtozers const VarLoc &VarL = Map[Idx];
1098e2196ddcSgbtozers const LocIndices Indices = Map.getAllIndices(VarL);
1099e2196ddcSgbtozers insert(Indices, VarL);
1100fba06e3cSJeremy Morse }
1101fba06e3cSJeremy Morse }
1102fba06e3cSJeremy Morse
insert(LocIndices VarLocIDs,const VarLoc & VL)1103e2196ddcSgbtozers void VarLocBasedLDV::OpenRangesSet::insert(LocIndices VarLocIDs,
1104fba06e3cSJeremy Morse const VarLoc &VL) {
1105fba06e3cSJeremy Morse auto *InsertInto = VL.isEntryBackupLoc() ? &EntryValuesBackupVars : &Vars;
1106e2196ddcSgbtozers for (LocIndex ID : VarLocIDs)
1107e2196ddcSgbtozers VarLocs.set(ID.getAsRawInteger());
1108e2196ddcSgbtozers InsertInto->insert({VL.Var, VarLocIDs});
1109fba06e3cSJeremy Morse }
1110fba06e3cSJeremy Morse
1111fba06e3cSJeremy Morse /// Return the Loc ID of an entry value backup location, if it exists for the
1112fba06e3cSJeremy Morse /// variable.
1113e2196ddcSgbtozers llvm::Optional<LocIndices>
getEntryValueBackup(DebugVariable Var)111420bb9fe5SJeremy Morse VarLocBasedLDV::OpenRangesSet::getEntryValueBackup(DebugVariable Var) {
1115fba06e3cSJeremy Morse auto It = EntryValuesBackupVars.find(Var);
1116fba06e3cSJeremy Morse if (It != EntryValuesBackupVars.end())
1117fba06e3cSJeremy Morse return It->second;
1118fba06e3cSJeremy Morse
1119fba06e3cSJeremy Morse return llvm::None;
1120fba06e3cSJeremy Morse }
1121fba06e3cSJeremy Morse
collectIDsForRegs(VarLocsInRange & Collected,const DefinedRegsSet & Regs,const VarLocSet & CollectFrom,const VarLocMap & VarLocIDs)1122e2196ddcSgbtozers void VarLocBasedLDV::collectIDsForRegs(VarLocsInRange &Collected,
1123fba06e3cSJeremy Morse const DefinedRegsSet &Regs,
1124e2196ddcSgbtozers const VarLocSet &CollectFrom,
1125e2196ddcSgbtozers const VarLocMap &VarLocIDs) {
1126fba06e3cSJeremy Morse assert(!Regs.empty() && "Nothing to collect");
1127e2196ddcSgbtozers SmallVector<Register, 32> SortedRegs;
11280da15ea5SKazu Hirata append_range(SortedRegs, Regs);
1129fba06e3cSJeremy Morse array_pod_sort(SortedRegs.begin(), SortedRegs.end());
1130fba06e3cSJeremy Morse auto It = CollectFrom.find(LocIndex::rawIndexForReg(SortedRegs.front()));
1131fba06e3cSJeremy Morse auto End = CollectFrom.end();
1132e2196ddcSgbtozers for (Register Reg : SortedRegs) {
1133e2196ddcSgbtozers // The half-open interval [FirstIndexForReg, FirstInvalidIndex) contains
1134e2196ddcSgbtozers // all possible VarLoc IDs for VarLocs with MLs of kind RegisterKind which
1135e2196ddcSgbtozers // live in Reg.
1136fba06e3cSJeremy Morse uint64_t FirstIndexForReg = LocIndex::rawIndexForReg(Reg);
1137fba06e3cSJeremy Morse uint64_t FirstInvalidIndex = LocIndex::rawIndexForReg(Reg + 1);
1138fba06e3cSJeremy Morse It.advanceToLowerBound(FirstIndexForReg);
1139fba06e3cSJeremy Morse
1140fba06e3cSJeremy Morse // Iterate through that half-open interval and collect all the set IDs.
1141e2196ddcSgbtozers for (; It != End && *It < FirstInvalidIndex; ++It) {
1142e2196ddcSgbtozers LocIndex ItIdx = LocIndex::fromRawInteger(*It);
1143e2196ddcSgbtozers const VarLoc &VL = VarLocIDs[ItIdx];
1144e2196ddcSgbtozers LocIndices LI = VarLocIDs.getAllIndices(VL);
1145e2196ddcSgbtozers // For now, the back index is always the universal location index.
1146e2196ddcSgbtozers assert(LI.back().Location == LocIndex::kUniversalLocation &&
1147e2196ddcSgbtozers "Unexpected order of LocIndices for VarLoc; was it inserted into "
1148e2196ddcSgbtozers "the VarLocMap correctly?");
1149e2196ddcSgbtozers Collected.insert(LI.back().Index);
1150e2196ddcSgbtozers }
1151fba06e3cSJeremy Morse
1152fba06e3cSJeremy Morse if (It == End)
1153fba06e3cSJeremy Morse return;
1154fba06e3cSJeremy Morse }
1155fba06e3cSJeremy Morse }
1156fba06e3cSJeremy Morse
getUsedRegs(const VarLocSet & CollectFrom,SmallVectorImpl<Register> & UsedRegs) const115720bb9fe5SJeremy Morse void VarLocBasedLDV::getUsedRegs(const VarLocSet &CollectFrom,
1158e2196ddcSgbtozers SmallVectorImpl<Register> &UsedRegs) const {
1159fba06e3cSJeremy Morse // All register-based VarLocs are assigned indices greater than or equal to
1160fba06e3cSJeremy Morse // FirstRegIndex.
1161e2196ddcSgbtozers uint64_t FirstRegIndex =
1162e2196ddcSgbtozers LocIndex::rawIndexForReg(LocIndex::kFirstRegLocation);
1163fba06e3cSJeremy Morse uint64_t FirstInvalidIndex =
1164fba06e3cSJeremy Morse LocIndex::rawIndexForReg(LocIndex::kFirstInvalidRegLocation);
1165fba06e3cSJeremy Morse for (auto It = CollectFrom.find(FirstRegIndex),
1166fba06e3cSJeremy Morse End = CollectFrom.find(FirstInvalidIndex);
1167fba06e3cSJeremy Morse It != End;) {
1168fba06e3cSJeremy Morse // We found a VarLoc ID for a VarLoc that lives in a register. Figure out
1169fba06e3cSJeremy Morse // which register and add it to UsedRegs.
1170fba06e3cSJeremy Morse uint32_t FoundReg = LocIndex::fromRawInteger(*It).Location;
1171fba06e3cSJeremy Morse assert((UsedRegs.empty() || FoundReg != UsedRegs.back()) &&
1172fba06e3cSJeremy Morse "Duplicate used reg");
1173fba06e3cSJeremy Morse UsedRegs.push_back(FoundReg);
1174fba06e3cSJeremy Morse
1175fba06e3cSJeremy Morse // Skip to the next /set/ register. Note that this finds a lower bound, so
1176fba06e3cSJeremy Morse // even if there aren't any VarLocs living in `FoundReg+1`, we're still
1177fba06e3cSJeremy Morse // guaranteed to move on to the next register (or to end()).
1178fba06e3cSJeremy Morse uint64_t NextRegIndex = LocIndex::rawIndexForReg(FoundReg + 1);
1179fba06e3cSJeremy Morse It.advanceToLowerBound(NextRegIndex);
1180fba06e3cSJeremy Morse }
1181fba06e3cSJeremy Morse }
1182fba06e3cSJeremy Morse
1183fba06e3cSJeremy Morse //===----------------------------------------------------------------------===//
1184fba06e3cSJeremy Morse // Debug Range Extension Implementation
1185fba06e3cSJeremy Morse //===----------------------------------------------------------------------===//
1186fba06e3cSJeremy Morse
1187fba06e3cSJeremy Morse #ifndef NDEBUG
printVarLocInMBB(const MachineFunction & MF,const VarLocInMBB & V,const VarLocMap & VarLocIDs,const char * msg,raw_ostream & Out) const118820bb9fe5SJeremy Morse void VarLocBasedLDV::printVarLocInMBB(const MachineFunction &MF,
1189fba06e3cSJeremy Morse const VarLocInMBB &V,
1190fba06e3cSJeremy Morse const VarLocMap &VarLocIDs,
1191fba06e3cSJeremy Morse const char *msg,
1192fba06e3cSJeremy Morse raw_ostream &Out) const {
1193fba06e3cSJeremy Morse Out << '\n' << msg << '\n';
1194fba06e3cSJeremy Morse for (const MachineBasicBlock &BB : MF) {
1195fba06e3cSJeremy Morse if (!V.count(&BB))
1196fba06e3cSJeremy Morse continue;
1197fba06e3cSJeremy Morse const VarLocSet &L = getVarLocsInMBB(&BB, V);
1198fba06e3cSJeremy Morse if (L.empty())
1199fba06e3cSJeremy Morse continue;
1200e2196ddcSgbtozers SmallVector<VarLoc, 32> VarLocs;
1201e2196ddcSgbtozers collectAllVarLocs(VarLocs, L, VarLocIDs);
1202fba06e3cSJeremy Morse Out << "MBB: " << BB.getNumber() << ":\n";
1203e2196ddcSgbtozers for (const VarLoc &VL : VarLocs) {
1204fba06e3cSJeremy Morse Out << " Var: " << VL.Var.getVariable()->getName();
1205fba06e3cSJeremy Morse Out << " MI: ";
1206fba06e3cSJeremy Morse VL.dump(TRI, Out);
1207fba06e3cSJeremy Morse }
1208fba06e3cSJeremy Morse }
1209fba06e3cSJeremy Morse Out << "\n";
1210fba06e3cSJeremy Morse }
1211fba06e3cSJeremy Morse #endif
1212fba06e3cSJeremy Morse
121320bb9fe5SJeremy Morse VarLocBasedLDV::VarLoc::SpillLoc
extractSpillBaseRegAndOffset(const MachineInstr & MI)121420bb9fe5SJeremy Morse VarLocBasedLDV::extractSpillBaseRegAndOffset(const MachineInstr &MI) {
1215fba06e3cSJeremy Morse assert(MI.hasOneMemOperand() &&
1216fba06e3cSJeremy Morse "Spill instruction does not have exactly one memory operand?");
1217fba06e3cSJeremy Morse auto MMOI = MI.memoperands_begin();
1218fba06e3cSJeremy Morse const PseudoSourceValue *PVal = (*MMOI)->getPseudoValue();
1219fba06e3cSJeremy Morse assert(PVal->kind() == PseudoSourceValue::FixedStack &&
1220fba06e3cSJeremy Morse "Inconsistent memory operand in spill instruction");
1221fba06e3cSJeremy Morse int FI = cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex();
1222fba06e3cSJeremy Morse const MachineBasicBlock *MBB = MI.getParent();
1223fba06e3cSJeremy Morse Register Reg;
1224d57bba7cSSander de Smalen StackOffset Offset = TFI->getFrameIndexReference(*MBB->getParent(), FI, Reg);
122584a11209SSander de Smalen return {Reg, Offset};
1226fba06e3cSJeremy Morse }
1227fba06e3cSJeremy Morse
122886f5288eSDjordje Todorovic /// Do cleanup of \p EntryValTransfers created by \p TRInst, by removing the
122986f5288eSDjordje Todorovic /// Transfer, which uses the to-be-deleted \p EntryVL.
cleanupEntryValueTransfers(const MachineInstr * TRInst,OpenRangesSet & OpenRanges,VarLocMap & VarLocIDs,const VarLoc & EntryVL,InstToEntryLocMap & EntryValTransfers)123086f5288eSDjordje Todorovic void VarLocBasedLDV::cleanupEntryValueTransfers(
123186f5288eSDjordje Todorovic const MachineInstr *TRInst, OpenRangesSet &OpenRanges, VarLocMap &VarLocIDs,
123286f5288eSDjordje Todorovic const VarLoc &EntryVL, InstToEntryLocMap &EntryValTransfers) {
123386f5288eSDjordje Todorovic if (EntryValTransfers.empty() || TRInst == nullptr)
123486f5288eSDjordje Todorovic return;
123586f5288eSDjordje Todorovic
123686f5288eSDjordje Todorovic auto TransRange = EntryValTransfers.equal_range(TRInst);
123786f5288eSDjordje Todorovic for (auto TDPair : llvm::make_range(TransRange.first, TransRange.second)) {
123886f5288eSDjordje Todorovic const VarLoc &EmittedEV = VarLocIDs[TDPair.second];
123986f5288eSDjordje Todorovic if (std::tie(EntryVL.Var, EntryVL.Locs[0].Value.RegNo, EntryVL.Expr) ==
124086f5288eSDjordje Todorovic std::tie(EmittedEV.Var, EmittedEV.Locs[0].Value.RegNo,
124186f5288eSDjordje Todorovic EmittedEV.Expr)) {
124286f5288eSDjordje Todorovic OpenRanges.erase(EmittedEV);
124386f5288eSDjordje Todorovic EntryValTransfers.erase(TRInst);
124486f5288eSDjordje Todorovic break;
124586f5288eSDjordje Todorovic }
124686f5288eSDjordje Todorovic }
124786f5288eSDjordje Todorovic }
124886f5288eSDjordje Todorovic
1249fba06e3cSJeremy Morse /// Try to salvage the debug entry value if we encounter a new debug value
1250fba06e3cSJeremy Morse /// describing the same parameter, otherwise stop tracking the value. Return
125186f5288eSDjordje Todorovic /// true if we should stop tracking the entry value and do the cleanup of
125286f5288eSDjordje Todorovic /// emitted Entry Value Transfers, otherwise return false.
removeEntryValue(const MachineInstr & MI,OpenRangesSet & OpenRanges,VarLocMap & VarLocIDs,const VarLoc & EntryVL,InstToEntryLocMap & EntryValTransfers,RegDefToInstMap & RegSetInstrs)125386f5288eSDjordje Todorovic void VarLocBasedLDV::removeEntryValue(const MachineInstr &MI,
1254fba06e3cSJeremy Morse OpenRangesSet &OpenRanges,
1255fba06e3cSJeremy Morse VarLocMap &VarLocIDs,
125686f5288eSDjordje Todorovic const VarLoc &EntryVL,
125786f5288eSDjordje Todorovic InstToEntryLocMap &EntryValTransfers,
125886f5288eSDjordje Todorovic RegDefToInstMap &RegSetInstrs) {
1259fba06e3cSJeremy Morse // Skip the DBG_VALUE which is the debug entry value itself.
126086f5288eSDjordje Todorovic if (&MI == &EntryVL.MI)
126186f5288eSDjordje Todorovic return;
1262fba06e3cSJeremy Morse
1263fba06e3cSJeremy Morse // If the parameter's location is not register location, we can not track
126486f5288eSDjordje Todorovic // the entry value any more. It doesn't have the TransferInst which defines
126586f5288eSDjordje Todorovic // register, so no Entry Value Transfers have been emitted already.
126686f5288eSDjordje Todorovic if (!MI.getDebugOperand(0).isReg())
126786f5288eSDjordje Todorovic return;
1268fba06e3cSJeremy Morse
126986f5288eSDjordje Todorovic // Try to get non-debug instruction responsible for the DBG_VALUE.
127086f5288eSDjordje Todorovic const MachineInstr *TransferInst = nullptr;
1271fba06e3cSJeremy Morse Register Reg = MI.getDebugOperand(0).getReg();
127286f5288eSDjordje Todorovic if (Reg.isValid() && RegSetInstrs.find(Reg) != RegSetInstrs.end())
127386f5288eSDjordje Todorovic TransferInst = RegSetInstrs.find(Reg)->second;
1274e2196ddcSgbtozers
127586f5288eSDjordje Todorovic // Case of the parameter's DBG_VALUE at the start of entry MBB.
127686f5288eSDjordje Todorovic if (!TransferInst && !LastNonDbgMI && MI.getParent()->isEntryBlock())
127786f5288eSDjordje Todorovic return;
127886f5288eSDjordje Todorovic
127986f5288eSDjordje Todorovic // If the debug expression from the DBG_VALUE is not empty, we can assume the
128086f5288eSDjordje Todorovic // parameter's value has changed indicating that we should stop tracking its
128186f5288eSDjordje Todorovic // entry value as well.
128286f5288eSDjordje Todorovic if (MI.getDebugExpression()->getNumElements() == 0 && TransferInst) {
128386f5288eSDjordje Todorovic // If the DBG_VALUE comes from a copy instruction that copies the entry
128486f5288eSDjordje Todorovic // value, it means the parameter's value has not changed and we should be
128586f5288eSDjordje Todorovic // able to use its entry value.
1286fba06e3cSJeremy Morse // TODO: Try to keep tracking of an entry value if we encounter a propagated
1287fba06e3cSJeremy Morse // DBG_VALUE describing the copy of the entry value. (Propagated entry value
1288fba06e3cSJeremy Morse // does not indicate the parameter modification.)
128986f5288eSDjordje Todorovic auto DestSrc = TII->isCopyInstr(*TransferInst);
129086f5288eSDjordje Todorovic if (DestSrc) {
129186f5288eSDjordje Todorovic const MachineOperand *SrcRegOp, *DestRegOp;
1292fba06e3cSJeremy Morse SrcRegOp = DestSrc->Source;
1293fba06e3cSJeremy Morse DestRegOp = DestSrc->Destination;
129486f5288eSDjordje Todorovic if (Reg == DestRegOp->getReg()) {
1295fba06e3cSJeremy Morse for (uint64_t ID : OpenRanges.getEntryValueBackupVarLocs()) {
1296fba06e3cSJeremy Morse const VarLoc &VL = VarLocIDs[LocIndex::fromRawInteger(ID)];
1297e2196ddcSgbtozers if (VL.isEntryValueCopyBackupReg(Reg) &&
1298e2196ddcSgbtozers // Entry Values should not be variadic.
1299fba06e3cSJeremy Morse VL.MI.getDebugOperand(0).getReg() == SrcRegOp->getReg())
130086f5288eSDjordje Todorovic return;
130186f5288eSDjordje Todorovic }
130286f5288eSDjordje Todorovic }
1303fba06e3cSJeremy Morse }
1304fba06e3cSJeremy Morse }
1305fba06e3cSJeremy Morse
130686f5288eSDjordje Todorovic LLVM_DEBUG(dbgs() << "Deleting a DBG entry value because of: ";
130786f5288eSDjordje Todorovic MI.print(dbgs(), /*IsStandalone*/ false,
130886f5288eSDjordje Todorovic /*SkipOpers*/ false, /*SkipDebugLoc*/ false,
130986f5288eSDjordje Todorovic /*AddNewLine*/ true, TII));
131086f5288eSDjordje Todorovic cleanupEntryValueTransfers(TransferInst, OpenRanges, VarLocIDs, EntryVL,
131186f5288eSDjordje Todorovic EntryValTransfers);
131286f5288eSDjordje Todorovic OpenRanges.erase(EntryVL);
1313fba06e3cSJeremy Morse }
1314fba06e3cSJeremy Morse
1315fba06e3cSJeremy Morse /// End all previous ranges related to @MI and start a new range from @MI
1316fba06e3cSJeremy Morse /// if it is a DBG_VALUE instr.
transferDebugValue(const MachineInstr & MI,OpenRangesSet & OpenRanges,VarLocMap & VarLocIDs,InstToEntryLocMap & EntryValTransfers,RegDefToInstMap & RegSetInstrs)131720bb9fe5SJeremy Morse void VarLocBasedLDV::transferDebugValue(const MachineInstr &MI,
1318fba06e3cSJeremy Morse OpenRangesSet &OpenRanges,
131986f5288eSDjordje Todorovic VarLocMap &VarLocIDs,
132086f5288eSDjordje Todorovic InstToEntryLocMap &EntryValTransfers,
132186f5288eSDjordje Todorovic RegDefToInstMap &RegSetInstrs) {
1322fba06e3cSJeremy Morse if (!MI.isDebugValue())
1323fba06e3cSJeremy Morse return;
1324fba06e3cSJeremy Morse const DILocalVariable *Var = MI.getDebugVariable();
1325fba06e3cSJeremy Morse const DIExpression *Expr = MI.getDebugExpression();
1326fba06e3cSJeremy Morse const DILocation *DebugLoc = MI.getDebugLoc();
1327fba06e3cSJeremy Morse const DILocation *InlinedAt = DebugLoc->getInlinedAt();
1328fba06e3cSJeremy Morse assert(Var->isValidLocationForIntrinsic(DebugLoc) &&
1329fba06e3cSJeremy Morse "Expected inlined-at fields to agree");
1330fba06e3cSJeremy Morse
1331fba06e3cSJeremy Morse DebugVariable V(Var, Expr, InlinedAt);
1332fba06e3cSJeremy Morse
1333fba06e3cSJeremy Morse // Check if this DBG_VALUE indicates a parameter's value changing.
1334fba06e3cSJeremy Morse // If that is the case, we should stop tracking its entry value.
1335fba06e3cSJeremy Morse auto EntryValBackupID = OpenRanges.getEntryValueBackup(V);
1336fba06e3cSJeremy Morse if (Var->isParameter() && EntryValBackupID) {
1337e2196ddcSgbtozers const VarLoc &EntryVL = VarLocIDs[EntryValBackupID->back()];
133886f5288eSDjordje Todorovic removeEntryValue(MI, OpenRanges, VarLocIDs, EntryVL, EntryValTransfers,
133986f5288eSDjordje Todorovic RegSetInstrs);
1340fba06e3cSJeremy Morse }
1341fba06e3cSJeremy Morse
1342e2196ddcSgbtozers if (all_of(MI.debug_operands(), [](const MachineOperand &MO) {
1343e2196ddcSgbtozers return (MO.isReg() && MO.getReg()) || MO.isImm() || MO.isFPImm() ||
1344e2196ddcSgbtozers MO.isCImm();
1345e2196ddcSgbtozers })) {
1346fba06e3cSJeremy Morse // Use normal VarLoc constructor for registers and immediates.
1347fba06e3cSJeremy Morse VarLoc VL(MI, LS);
1348fba06e3cSJeremy Morse // End all previous ranges of VL.Var.
1349fba06e3cSJeremy Morse OpenRanges.erase(VL);
1350fba06e3cSJeremy Morse
1351e2196ddcSgbtozers LocIndices IDs = VarLocIDs.insert(VL);
1352fba06e3cSJeremy Morse // Add the VarLoc to OpenRanges from this DBG_VALUE.
1353e2196ddcSgbtozers OpenRanges.insert(IDs, VL);
1354e2196ddcSgbtozers } else if (MI.memoperands().size() > 0) {
1355fba06e3cSJeremy Morse llvm_unreachable("DBG_VALUE with mem operand encountered after regalloc?");
1356fba06e3cSJeremy Morse } else {
1357fba06e3cSJeremy Morse // This must be an undefined location. If it has an open range, erase it.
1358e2196ddcSgbtozers assert(MI.isUndefDebugValue() &&
1359fba06e3cSJeremy Morse "Unexpected non-undef DBG_VALUE encountered");
1360fba06e3cSJeremy Morse VarLoc VL(MI, LS);
1361fba06e3cSJeremy Morse OpenRanges.erase(VL);
1362fba06e3cSJeremy Morse }
1363fba06e3cSJeremy Morse }
1364fba06e3cSJeremy Morse
1365e2196ddcSgbtozers // This should be removed later, doesn't fit the new design.
collectAllVarLocs(SmallVectorImpl<VarLoc> & Collected,const VarLocSet & CollectFrom,const VarLocMap & VarLocIDs)1366e2196ddcSgbtozers void VarLocBasedLDV::collectAllVarLocs(SmallVectorImpl<VarLoc> &Collected,
1367e2196ddcSgbtozers const VarLocSet &CollectFrom,
1368e2196ddcSgbtozers const VarLocMap &VarLocIDs) {
1369e2196ddcSgbtozers // The half-open interval [FirstIndexForReg, FirstInvalidIndex) contains all
1370e2196ddcSgbtozers // possible VarLoc IDs for VarLocs with MLs of kind RegisterKind which live
1371e2196ddcSgbtozers // in Reg.
1372e2196ddcSgbtozers uint64_t FirstIndex = LocIndex::rawIndexForReg(LocIndex::kUniversalLocation);
1373e2196ddcSgbtozers uint64_t FirstInvalidIndex =
1374e2196ddcSgbtozers LocIndex::rawIndexForReg(LocIndex::kUniversalLocation + 1);
1375e2196ddcSgbtozers // Iterate through that half-open interval and collect all the set IDs.
1376e2196ddcSgbtozers for (auto It = CollectFrom.find(FirstIndex), End = CollectFrom.end();
1377e2196ddcSgbtozers It != End && *It < FirstInvalidIndex; ++It) {
1378e2196ddcSgbtozers LocIndex RegIdx = LocIndex::fromRawInteger(*It);
1379e2196ddcSgbtozers Collected.push_back(VarLocIDs[RegIdx]);
1380e2196ddcSgbtozers }
1381e2196ddcSgbtozers }
1382e2196ddcSgbtozers
1383fba06e3cSJeremy Morse /// Turn the entry value backup locations into primary locations.
emitEntryValues(MachineInstr & MI,OpenRangesSet & OpenRanges,VarLocMap & VarLocIDs,InstToEntryLocMap & EntryValTransfers,VarLocsInRange & KillSet)138420bb9fe5SJeremy Morse void VarLocBasedLDV::emitEntryValues(MachineInstr &MI,
1385fba06e3cSJeremy Morse OpenRangesSet &OpenRanges,
1386fba06e3cSJeremy Morse VarLocMap &VarLocIDs,
138786f5288eSDjordje Todorovic InstToEntryLocMap &EntryValTransfers,
1388e2196ddcSgbtozers VarLocsInRange &KillSet) {
1389fba06e3cSJeremy Morse // Do not insert entry value locations after a terminator.
1390fba06e3cSJeremy Morse if (MI.isTerminator())
1391fba06e3cSJeremy Morse return;
1392fba06e3cSJeremy Morse
1393e2196ddcSgbtozers for (uint32_t ID : KillSet) {
1394e2196ddcSgbtozers // The KillSet IDs are indices for the universal location bucket.
1395e2196ddcSgbtozers LocIndex Idx = LocIndex(LocIndex::kUniversalLocation, ID);
1396fba06e3cSJeremy Morse const VarLoc &VL = VarLocIDs[Idx];
1397fba06e3cSJeremy Morse if (!VL.Var.getVariable()->isParameter())
1398fba06e3cSJeremy Morse continue;
1399fba06e3cSJeremy Morse
1400fba06e3cSJeremy Morse auto DebugVar = VL.Var;
1401e2196ddcSgbtozers Optional<LocIndices> EntryValBackupIDs =
1402fba06e3cSJeremy Morse OpenRanges.getEntryValueBackup(DebugVar);
1403fba06e3cSJeremy Morse
1404fba06e3cSJeremy Morse // If the parameter has the entry value backup, it means we should
1405fba06e3cSJeremy Morse // be able to use its entry value.
1406e2196ddcSgbtozers if (!EntryValBackupIDs)
1407fba06e3cSJeremy Morse continue;
1408fba06e3cSJeremy Morse
1409e2196ddcSgbtozers const VarLoc &EntryVL = VarLocIDs[EntryValBackupIDs->back()];
1410e2196ddcSgbtozers VarLoc EntryLoc = VarLoc::CreateEntryLoc(EntryVL.MI, LS, EntryVL.Expr,
1411e2196ddcSgbtozers EntryVL.Locs[0].Value.RegNo);
1412e2196ddcSgbtozers LocIndices EntryValueIDs = VarLocIDs.insert(EntryLoc);
141386f5288eSDjordje Todorovic assert(EntryValueIDs.size() == 1 &&
141486f5288eSDjordje Todorovic "EntryValue loc should not be variadic");
141586f5288eSDjordje Todorovic EntryValTransfers.insert({&MI, EntryValueIDs.back()});
1416e2196ddcSgbtozers OpenRanges.insert(EntryValueIDs, EntryLoc);
1417fba06e3cSJeremy Morse }
1418fba06e3cSJeremy Morse }
1419fba06e3cSJeremy Morse
1420fba06e3cSJeremy Morse /// Create new TransferDebugPair and insert it in \p Transfers. The VarLoc
1421fba06e3cSJeremy Morse /// with \p OldVarID should be deleted form \p OpenRanges and replaced with
1422fba06e3cSJeremy Morse /// new VarLoc. If \p NewReg is different than default zero value then the
1423fba06e3cSJeremy Morse /// new location will be register location created by the copy like instruction,
1424fba06e3cSJeremy Morse /// otherwise it is variable's location on the stack.
insertTransferDebugPair(MachineInstr & MI,OpenRangesSet & OpenRanges,TransferMap & Transfers,VarLocMap & VarLocIDs,LocIndex OldVarID,TransferKind Kind,const VarLoc::MachineLoc & OldLoc,Register NewReg)142520bb9fe5SJeremy Morse void VarLocBasedLDV::insertTransferDebugPair(
1426fba06e3cSJeremy Morse MachineInstr &MI, OpenRangesSet &OpenRanges, TransferMap &Transfers,
1427fba06e3cSJeremy Morse VarLocMap &VarLocIDs, LocIndex OldVarID, TransferKind Kind,
1428e2196ddcSgbtozers const VarLoc::MachineLoc &OldLoc, Register NewReg) {
1429e2196ddcSgbtozers const VarLoc &OldVarLoc = VarLocIDs[OldVarID];
1430fba06e3cSJeremy Morse
1431fba06e3cSJeremy Morse auto ProcessVarLoc = [&MI, &OpenRanges, &Transfers, &VarLocIDs](VarLoc &VL) {
1432e2196ddcSgbtozers LocIndices LocIds = VarLocIDs.insert(VL);
1433fba06e3cSJeremy Morse
1434fba06e3cSJeremy Morse // Close this variable's previous location range.
1435fba06e3cSJeremy Morse OpenRanges.erase(VL);
1436fba06e3cSJeremy Morse
1437fba06e3cSJeremy Morse // Record the new location as an open range, and a postponed transfer
1438fba06e3cSJeremy Morse // inserting a DBG_VALUE for this location.
1439e2196ddcSgbtozers OpenRanges.insert(LocIds, VL);
1440fba06e3cSJeremy Morse assert(!MI.isTerminator() && "Cannot insert DBG_VALUE after terminator");
1441e2196ddcSgbtozers TransferDebugPair MIP = {&MI, LocIds.back()};
1442fba06e3cSJeremy Morse Transfers.push_back(MIP);
1443fba06e3cSJeremy Morse };
1444fba06e3cSJeremy Morse
1445fba06e3cSJeremy Morse // End all previous ranges of VL.Var.
1446fba06e3cSJeremy Morse OpenRanges.erase(VarLocIDs[OldVarID]);
1447fba06e3cSJeremy Morse switch (Kind) {
1448fba06e3cSJeremy Morse case TransferKind::TransferCopy: {
1449fba06e3cSJeremy Morse assert(NewReg &&
1450fba06e3cSJeremy Morse "No register supplied when handling a copy of a debug value");
1451fba06e3cSJeremy Morse // Create a DBG_VALUE instruction to describe the Var in its new
1452fba06e3cSJeremy Morse // register location.
1453e2196ddcSgbtozers VarLoc VL = VarLoc::CreateCopyLoc(OldVarLoc, OldLoc, NewReg);
1454fba06e3cSJeremy Morse ProcessVarLoc(VL);
1455fba06e3cSJeremy Morse LLVM_DEBUG({
1456fba06e3cSJeremy Morse dbgs() << "Creating VarLoc for register copy:";
1457fba06e3cSJeremy Morse VL.dump(TRI);
1458fba06e3cSJeremy Morse });
1459fba06e3cSJeremy Morse return;
1460fba06e3cSJeremy Morse }
1461fba06e3cSJeremy Morse case TransferKind::TransferSpill: {
1462fba06e3cSJeremy Morse // Create a DBG_VALUE instruction to describe the Var in its spilled
1463fba06e3cSJeremy Morse // location.
1464fba06e3cSJeremy Morse VarLoc::SpillLoc SpillLocation = extractSpillBaseRegAndOffset(MI);
1465e2196ddcSgbtozers VarLoc VL = VarLoc::CreateSpillLoc(
1466e2196ddcSgbtozers OldVarLoc, OldLoc, SpillLocation.SpillBase, SpillLocation.SpillOffset);
1467fba06e3cSJeremy Morse ProcessVarLoc(VL);
1468fba06e3cSJeremy Morse LLVM_DEBUG({
1469fba06e3cSJeremy Morse dbgs() << "Creating VarLoc for spill:";
1470fba06e3cSJeremy Morse VL.dump(TRI);
1471fba06e3cSJeremy Morse });
1472fba06e3cSJeremy Morse return;
1473fba06e3cSJeremy Morse }
1474fba06e3cSJeremy Morse case TransferKind::TransferRestore: {
1475fba06e3cSJeremy Morse assert(NewReg &&
1476fba06e3cSJeremy Morse "No register supplied when handling a restore of a debug value");
1477fba06e3cSJeremy Morse // DebugInstr refers to the pre-spill location, therefore we can reuse
1478fba06e3cSJeremy Morse // its expression.
1479e2196ddcSgbtozers VarLoc VL = VarLoc::CreateCopyLoc(OldVarLoc, OldLoc, NewReg);
1480fba06e3cSJeremy Morse ProcessVarLoc(VL);
1481fba06e3cSJeremy Morse LLVM_DEBUG({
1482fba06e3cSJeremy Morse dbgs() << "Creating VarLoc for restore:";
1483fba06e3cSJeremy Morse VL.dump(TRI);
1484fba06e3cSJeremy Morse });
1485fba06e3cSJeremy Morse return;
1486fba06e3cSJeremy Morse }
1487fba06e3cSJeremy Morse }
1488fba06e3cSJeremy Morse llvm_unreachable("Invalid transfer kind");
1489fba06e3cSJeremy Morse }
1490fba06e3cSJeremy Morse
1491fba06e3cSJeremy Morse /// A definition of a register may mark the end of a range.
transferRegisterDef(MachineInstr & MI,OpenRangesSet & OpenRanges,VarLocMap & VarLocIDs,InstToEntryLocMap & EntryValTransfers,RegDefToInstMap & RegSetInstrs)149286f5288eSDjordje Todorovic void VarLocBasedLDV::transferRegisterDef(MachineInstr &MI,
149386f5288eSDjordje Todorovic OpenRangesSet &OpenRanges,
149486f5288eSDjordje Todorovic VarLocMap &VarLocIDs,
149586f5288eSDjordje Todorovic InstToEntryLocMap &EntryValTransfers,
149686f5288eSDjordje Todorovic RegDefToInstMap &RegSetInstrs) {
1497fba06e3cSJeremy Morse
1498fba06e3cSJeremy Morse // Meta Instructions do not affect the debug liveness of any register they
1499fba06e3cSJeremy Morse // define.
1500fba06e3cSJeremy Morse if (MI.isMetaInstruction())
1501fba06e3cSJeremy Morse return;
1502fba06e3cSJeremy Morse
1503fba06e3cSJeremy Morse MachineFunction *MF = MI.getMF();
1504fba06e3cSJeremy Morse const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
1505fba06e3cSJeremy Morse Register SP = TLI->getStackPointerRegisterToSaveRestore();
1506fba06e3cSJeremy Morse
1507fba06e3cSJeremy Morse // Find the regs killed by MI, and find regmasks of preserved regs.
1508fba06e3cSJeremy Morse DefinedRegsSet DeadRegs;
1509fba06e3cSJeremy Morse SmallVector<const uint32_t *, 4> RegMasks;
1510fba06e3cSJeremy Morse for (const MachineOperand &MO : MI.operands()) {
1511fba06e3cSJeremy Morse // Determine whether the operand is a register def.
1512fba06e3cSJeremy Morse if (MO.isReg() && MO.isDef() && MO.getReg() &&
1513fba06e3cSJeremy Morse Register::isPhysicalRegister(MO.getReg()) &&
1514fba06e3cSJeremy Morse !(MI.isCall() && MO.getReg() == SP)) {
1515fba06e3cSJeremy Morse // Remove ranges of all aliased registers.
1516fba06e3cSJeremy Morse for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI)
1517fba06e3cSJeremy Morse // FIXME: Can we break out of this loop early if no insertion occurs?
1518fba06e3cSJeremy Morse DeadRegs.insert(*RAI);
151986f5288eSDjordje Todorovic RegSetInstrs.erase(MO.getReg());
152086f5288eSDjordje Todorovic RegSetInstrs.insert({MO.getReg(), &MI});
1521fba06e3cSJeremy Morse } else if (MO.isRegMask()) {
1522fba06e3cSJeremy Morse RegMasks.push_back(MO.getRegMask());
1523fba06e3cSJeremy Morse }
1524fba06e3cSJeremy Morse }
1525fba06e3cSJeremy Morse
1526fba06e3cSJeremy Morse // Erase VarLocs which reside in one of the dead registers. For performance
1527fba06e3cSJeremy Morse // reasons, it's critical to not iterate over the full set of open VarLocs.
1528fba06e3cSJeremy Morse // Iterate over the set of dying/used regs instead.
1529fba06e3cSJeremy Morse if (!RegMasks.empty()) {
1530e2196ddcSgbtozers SmallVector<Register, 32> UsedRegs;
1531fba06e3cSJeremy Morse getUsedRegs(OpenRanges.getVarLocs(), UsedRegs);
1532e2196ddcSgbtozers for (Register Reg : UsedRegs) {
1533fba06e3cSJeremy Morse // Remove ranges of all clobbered registers. Register masks don't usually
1534fba06e3cSJeremy Morse // list SP as preserved. Assume that call instructions never clobber SP,
1535fba06e3cSJeremy Morse // because some backends (e.g., AArch64) never list SP in the regmask.
1536fba06e3cSJeremy Morse // While the debug info may be off for an instruction or two around
1537fba06e3cSJeremy Morse // callee-cleanup calls, transferring the DEBUG_VALUE across the call is
1538fba06e3cSJeremy Morse // still a better user experience.
1539fba06e3cSJeremy Morse if (Reg == SP)
1540fba06e3cSJeremy Morse continue;
1541fba06e3cSJeremy Morse bool AnyRegMaskKillsReg =
1542fba06e3cSJeremy Morse any_of(RegMasks, [Reg](const uint32_t *RegMask) {
1543fba06e3cSJeremy Morse return MachineOperand::clobbersPhysReg(RegMask, Reg);
1544fba06e3cSJeremy Morse });
1545fba06e3cSJeremy Morse if (AnyRegMaskKillsReg)
1546fba06e3cSJeremy Morse DeadRegs.insert(Reg);
154786f5288eSDjordje Todorovic if (AnyRegMaskKillsReg) {
154886f5288eSDjordje Todorovic RegSetInstrs.erase(Reg);
154986f5288eSDjordje Todorovic RegSetInstrs.insert({Reg, &MI});
155086f5288eSDjordje Todorovic }
1551fba06e3cSJeremy Morse }
1552fba06e3cSJeremy Morse }
1553fba06e3cSJeremy Morse
1554fba06e3cSJeremy Morse if (DeadRegs.empty())
1555fba06e3cSJeremy Morse return;
1556fba06e3cSJeremy Morse
1557e2196ddcSgbtozers VarLocsInRange KillSet;
1558e2196ddcSgbtozers collectIDsForRegs(KillSet, DeadRegs, OpenRanges.getVarLocs(), VarLocIDs);
1559e2196ddcSgbtozers OpenRanges.erase(KillSet, VarLocIDs, LocIndex::kUniversalLocation);
1560fba06e3cSJeremy Morse
156120bb9fe5SJeremy Morse if (TPC) {
1562fba06e3cSJeremy Morse auto &TM = TPC->getTM<TargetMachine>();
1563fba06e3cSJeremy Morse if (TM.Options.ShouldEmitDebugEntryValues())
156486f5288eSDjordje Todorovic emitEntryValues(MI, OpenRanges, VarLocIDs, EntryValTransfers, KillSet);
1565fba06e3cSJeremy Morse }
1566fba06e3cSJeremy Morse }
1567fba06e3cSJeremy Morse
isSpillInstruction(const MachineInstr & MI,MachineFunction * MF)156820bb9fe5SJeremy Morse bool VarLocBasedLDV::isSpillInstruction(const MachineInstr &MI,
1569fba06e3cSJeremy Morse MachineFunction *MF) {
1570fba06e3cSJeremy Morse // TODO: Handle multiple stores folded into one.
1571fba06e3cSJeremy Morse if (!MI.hasOneMemOperand())
1572fba06e3cSJeremy Morse return false;
1573fba06e3cSJeremy Morse
1574fba06e3cSJeremy Morse if (!MI.getSpillSize(TII) && !MI.getFoldedSpillSize(TII))
1575fba06e3cSJeremy Morse return false; // This is not a spill instruction, since no valid size was
1576fba06e3cSJeremy Morse // returned from either function.
1577fba06e3cSJeremy Morse
1578fba06e3cSJeremy Morse return true;
1579fba06e3cSJeremy Morse }
1580fba06e3cSJeremy Morse
isLocationSpill(const MachineInstr & MI,MachineFunction * MF,Register & Reg)158120bb9fe5SJeremy Morse bool VarLocBasedLDV::isLocationSpill(const MachineInstr &MI,
1582fba06e3cSJeremy Morse MachineFunction *MF, Register &Reg) {
1583fba06e3cSJeremy Morse if (!isSpillInstruction(MI, MF))
1584fba06e3cSJeremy Morse return false;
1585fba06e3cSJeremy Morse
1586fba06e3cSJeremy Morse auto isKilledReg = [&](const MachineOperand MO, Register &Reg) {
1587fba06e3cSJeremy Morse if (!MO.isReg() || !MO.isUse()) {
1588fba06e3cSJeremy Morse Reg = 0;
1589fba06e3cSJeremy Morse return false;
1590fba06e3cSJeremy Morse }
1591fba06e3cSJeremy Morse Reg = MO.getReg();
1592fba06e3cSJeremy Morse return MO.isKill();
1593fba06e3cSJeremy Morse };
1594fba06e3cSJeremy Morse
1595fba06e3cSJeremy Morse for (const MachineOperand &MO : MI.operands()) {
1596fba06e3cSJeremy Morse // In a spill instruction generated by the InlineSpiller the spilled
1597fba06e3cSJeremy Morse // register has its kill flag set.
1598fba06e3cSJeremy Morse if (isKilledReg(MO, Reg))
1599fba06e3cSJeremy Morse return true;
1600fba06e3cSJeremy Morse if (Reg != 0) {
1601fba06e3cSJeremy Morse // Check whether next instruction kills the spilled register.
1602fba06e3cSJeremy Morse // FIXME: Current solution does not cover search for killed register in
1603fba06e3cSJeremy Morse // bundles and instructions further down the chain.
1604fba06e3cSJeremy Morse auto NextI = std::next(MI.getIterator());
1605fba06e3cSJeremy Morse // Skip next instruction that points to basic block end iterator.
1606fba06e3cSJeremy Morse if (MI.getParent()->end() == NextI)
1607fba06e3cSJeremy Morse continue;
1608fba06e3cSJeremy Morse Register RegNext;
1609fba06e3cSJeremy Morse for (const MachineOperand &MONext : NextI->operands()) {
1610fba06e3cSJeremy Morse // Return true if we came across the register from the
1611fba06e3cSJeremy Morse // previous spill instruction that is killed in NextI.
1612fba06e3cSJeremy Morse if (isKilledReg(MONext, RegNext) && RegNext == Reg)
1613fba06e3cSJeremy Morse return true;
1614fba06e3cSJeremy Morse }
1615fba06e3cSJeremy Morse }
1616fba06e3cSJeremy Morse }
1617fba06e3cSJeremy Morse // Return false if we didn't find spilled register.
1618fba06e3cSJeremy Morse return false;
1619fba06e3cSJeremy Morse }
1620fba06e3cSJeremy Morse
162120bb9fe5SJeremy Morse Optional<VarLocBasedLDV::VarLoc::SpillLoc>
isRestoreInstruction(const MachineInstr & MI,MachineFunction * MF,Register & Reg)162220bb9fe5SJeremy Morse VarLocBasedLDV::isRestoreInstruction(const MachineInstr &MI,
1623fba06e3cSJeremy Morse MachineFunction *MF, Register &Reg) {
1624fba06e3cSJeremy Morse if (!MI.hasOneMemOperand())
1625fba06e3cSJeremy Morse return None;
1626fba06e3cSJeremy Morse
1627fba06e3cSJeremy Morse // FIXME: Handle folded restore instructions with more than one memory
1628fba06e3cSJeremy Morse // operand.
1629fba06e3cSJeremy Morse if (MI.getRestoreSize(TII)) {
1630fba06e3cSJeremy Morse Reg = MI.getOperand(0).getReg();
1631fba06e3cSJeremy Morse return extractSpillBaseRegAndOffset(MI);
1632fba06e3cSJeremy Morse }
1633fba06e3cSJeremy Morse return None;
1634fba06e3cSJeremy Morse }
1635fba06e3cSJeremy Morse
1636fba06e3cSJeremy Morse /// A spilled register may indicate that we have to end the current range of
1637fba06e3cSJeremy Morse /// a variable and create a new one for the spill location.
1638fba06e3cSJeremy Morse /// A restored register may indicate the reverse situation.
1639fba06e3cSJeremy Morse /// We don't want to insert any instructions in process(), so we just create
1640fba06e3cSJeremy Morse /// the DBG_VALUE without inserting it and keep track of it in \p Transfers.
1641fba06e3cSJeremy Morse /// It will be inserted into the BB when we're done iterating over the
1642fba06e3cSJeremy Morse /// instructions.
transferSpillOrRestoreInst(MachineInstr & MI,OpenRangesSet & OpenRanges,VarLocMap & VarLocIDs,TransferMap & Transfers)164320bb9fe5SJeremy Morse void VarLocBasedLDV::transferSpillOrRestoreInst(MachineInstr &MI,
1644fba06e3cSJeremy Morse OpenRangesSet &OpenRanges,
1645fba06e3cSJeremy Morse VarLocMap &VarLocIDs,
1646fba06e3cSJeremy Morse TransferMap &Transfers) {
1647fba06e3cSJeremy Morse MachineFunction *MF = MI.getMF();
1648fba06e3cSJeremy Morse TransferKind TKind;
1649fba06e3cSJeremy Morse Register Reg;
1650fba06e3cSJeremy Morse Optional<VarLoc::SpillLoc> Loc;
1651fba06e3cSJeremy Morse
1652fba06e3cSJeremy Morse LLVM_DEBUG(dbgs() << "Examining instruction: "; MI.dump(););
1653fba06e3cSJeremy Morse
1654fba06e3cSJeremy Morse // First, if there are any DBG_VALUEs pointing at a spill slot that is
1655fba06e3cSJeremy Morse // written to, then close the variable location. The value in memory
1656fba06e3cSJeremy Morse // will have changed.
1657e2196ddcSgbtozers VarLocsInRange KillSet;
1658fba06e3cSJeremy Morse if (isSpillInstruction(MI, MF)) {
1659fba06e3cSJeremy Morse Loc = extractSpillBaseRegAndOffset(MI);
1660fba06e3cSJeremy Morse for (uint64_t ID : OpenRanges.getSpillVarLocs()) {
1661fba06e3cSJeremy Morse LocIndex Idx = LocIndex::fromRawInteger(ID);
1662fba06e3cSJeremy Morse const VarLoc &VL = VarLocIDs[Idx];
1663e2196ddcSgbtozers assert(VL.containsSpillLocs() && "Broken VarLocSet?");
1664e2196ddcSgbtozers if (VL.usesSpillLoc(*Loc)) {
1665fba06e3cSJeremy Morse // This location is overwritten by the current instruction -- terminate
1666fba06e3cSJeremy Morse // the open range, and insert an explicit DBG_VALUE $noreg.
1667fba06e3cSJeremy Morse //
1668fba06e3cSJeremy Morse // Doing this at a later stage would require re-interpreting all
1669fba06e3cSJeremy Morse // DBG_VALUes and DIExpressions to identify whether they point at
1670fba06e3cSJeremy Morse // memory, and then analysing all memory writes to see if they
1671fba06e3cSJeremy Morse // overwrite that memory, which is expensive.
1672fba06e3cSJeremy Morse //
1673fba06e3cSJeremy Morse // At this stage, we already know which DBG_VALUEs are for spills and
1674fba06e3cSJeremy Morse // where they are located; it's best to fix handle overwrites now.
1675e2196ddcSgbtozers KillSet.insert(ID);
1676e2196ddcSgbtozers unsigned SpillLocIdx = VL.getSpillLocIdx(*Loc);
1677e2196ddcSgbtozers VarLoc::MachineLoc OldLoc = VL.Locs[SpillLocIdx];
1678e2196ddcSgbtozers VarLoc UndefVL = VarLoc::CreateCopyLoc(VL, OldLoc, 0);
1679e2196ddcSgbtozers LocIndices UndefLocIDs = VarLocIDs.insert(UndefVL);
1680e2196ddcSgbtozers Transfers.push_back({&MI, UndefLocIDs.back()});
1681fba06e3cSJeremy Morse }
1682fba06e3cSJeremy Morse }
1683e2196ddcSgbtozers OpenRanges.erase(KillSet, VarLocIDs, LocIndex::kSpillLocation);
1684fba06e3cSJeremy Morse }
1685fba06e3cSJeremy Morse
1686fba06e3cSJeremy Morse // Try to recognise spill and restore instructions that may create a new
1687fba06e3cSJeremy Morse // variable location.
1688fba06e3cSJeremy Morse if (isLocationSpill(MI, MF, Reg)) {
1689fba06e3cSJeremy Morse TKind = TransferKind::TransferSpill;
1690fba06e3cSJeremy Morse LLVM_DEBUG(dbgs() << "Recognized as spill: "; MI.dump(););
1691fba06e3cSJeremy Morse LLVM_DEBUG(dbgs() << "Register: " << Reg << " " << printReg(Reg, TRI)
1692fba06e3cSJeremy Morse << "\n");
1693fba06e3cSJeremy Morse } else {
1694fba06e3cSJeremy Morse if (!(Loc = isRestoreInstruction(MI, MF, Reg)))
1695fba06e3cSJeremy Morse return;
1696fba06e3cSJeremy Morse TKind = TransferKind::TransferRestore;
1697fba06e3cSJeremy Morse LLVM_DEBUG(dbgs() << "Recognized as restore: "; MI.dump(););
1698fba06e3cSJeremy Morse LLVM_DEBUG(dbgs() << "Register: " << Reg << " " << printReg(Reg, TRI)
1699fba06e3cSJeremy Morse << "\n");
1700fba06e3cSJeremy Morse }
1701fba06e3cSJeremy Morse // Check if the register or spill location is the location of a debug value.
1702fba06e3cSJeremy Morse auto TransferCandidates = OpenRanges.getEmptyVarLocRange();
1703fba06e3cSJeremy Morse if (TKind == TransferKind::TransferSpill)
1704fba06e3cSJeremy Morse TransferCandidates = OpenRanges.getRegisterVarLocs(Reg);
1705fba06e3cSJeremy Morse else if (TKind == TransferKind::TransferRestore)
1706fba06e3cSJeremy Morse TransferCandidates = OpenRanges.getSpillVarLocs();
1707fba06e3cSJeremy Morse for (uint64_t ID : TransferCandidates) {
1708fba06e3cSJeremy Morse LocIndex Idx = LocIndex::fromRawInteger(ID);
1709fba06e3cSJeremy Morse const VarLoc &VL = VarLocIDs[Idx];
1710e2196ddcSgbtozers unsigned LocIdx;
1711fba06e3cSJeremy Morse if (TKind == TransferKind::TransferSpill) {
1712e2196ddcSgbtozers assert(VL.usesReg(Reg) && "Broken VarLocSet?");
1713fba06e3cSJeremy Morse LLVM_DEBUG(dbgs() << "Spilling Register " << printReg(Reg, TRI) << '('
1714fba06e3cSJeremy Morse << VL.Var.getVariable()->getName() << ")\n");
1715e2196ddcSgbtozers LocIdx = VL.getRegIdx(Reg);
1716fba06e3cSJeremy Morse } else {
1717e2196ddcSgbtozers assert(TKind == TransferKind::TransferRestore && VL.containsSpillLocs() &&
1718e2196ddcSgbtozers "Broken VarLocSet?");
1719e2196ddcSgbtozers if (!VL.usesSpillLoc(*Loc))
1720fba06e3cSJeremy Morse // The spill location is not the location of a debug value.
1721fba06e3cSJeremy Morse continue;
1722fba06e3cSJeremy Morse LLVM_DEBUG(dbgs() << "Restoring Register " << printReg(Reg, TRI) << '('
1723fba06e3cSJeremy Morse << VL.Var.getVariable()->getName() << ")\n");
1724e2196ddcSgbtozers LocIdx = VL.getSpillLocIdx(*Loc);
1725fba06e3cSJeremy Morse }
1726e2196ddcSgbtozers VarLoc::MachineLoc MLoc = VL.Locs[LocIdx];
1727fba06e3cSJeremy Morse insertTransferDebugPair(MI, OpenRanges, Transfers, VarLocIDs, Idx, TKind,
1728e2196ddcSgbtozers MLoc, Reg);
1729fba06e3cSJeremy Morse // FIXME: A comment should explain why it's correct to return early here,
1730fba06e3cSJeremy Morse // if that is in fact correct.
1731fba06e3cSJeremy Morse return;
1732fba06e3cSJeremy Morse }
1733fba06e3cSJeremy Morse }
1734fba06e3cSJeremy Morse
1735fba06e3cSJeremy Morse /// If \p MI is a register copy instruction, that copies a previously tracked
1736fba06e3cSJeremy Morse /// value from one register to another register that is callee saved, we
1737fba06e3cSJeremy Morse /// create new DBG_VALUE instruction described with copy destination register.
transferRegisterCopy(MachineInstr & MI,OpenRangesSet & OpenRanges,VarLocMap & VarLocIDs,TransferMap & Transfers)173820bb9fe5SJeremy Morse void VarLocBasedLDV::transferRegisterCopy(MachineInstr &MI,
1739fba06e3cSJeremy Morse OpenRangesSet &OpenRanges,
1740fba06e3cSJeremy Morse VarLocMap &VarLocIDs,
1741fba06e3cSJeremy Morse TransferMap &Transfers) {
1742fba06e3cSJeremy Morse auto DestSrc = TII->isCopyInstr(MI);
1743fba06e3cSJeremy Morse if (!DestSrc)
1744fba06e3cSJeremy Morse return;
1745fba06e3cSJeremy Morse
1746fba06e3cSJeremy Morse const MachineOperand *DestRegOp = DestSrc->Destination;
1747fba06e3cSJeremy Morse const MachineOperand *SrcRegOp = DestSrc->Source;
1748fba06e3cSJeremy Morse
1749fba06e3cSJeremy Morse if (!DestRegOp->isDef())
1750fba06e3cSJeremy Morse return;
1751fba06e3cSJeremy Morse
1752fba06e3cSJeremy Morse auto isCalleeSavedReg = [&](Register Reg) {
1753fba06e3cSJeremy Morse for (MCRegAliasIterator RAI(Reg, TRI, true); RAI.isValid(); ++RAI)
1754fba06e3cSJeremy Morse if (CalleeSavedRegs.test(*RAI))
1755fba06e3cSJeremy Morse return true;
1756fba06e3cSJeremy Morse return false;
1757fba06e3cSJeremy Morse };
1758fba06e3cSJeremy Morse
1759fba06e3cSJeremy Morse Register SrcReg = SrcRegOp->getReg();
1760fba06e3cSJeremy Morse Register DestReg = DestRegOp->getReg();
1761fba06e3cSJeremy Morse
1762fba06e3cSJeremy Morse // We want to recognize instructions where destination register is callee
1763fba06e3cSJeremy Morse // saved register. If register that could be clobbered by the call is
1764fba06e3cSJeremy Morse // included, there would be a great chance that it is going to be clobbered
1765fba06e3cSJeremy Morse // soon. It is more likely that previous register location, which is callee
1766fba06e3cSJeremy Morse // saved, is going to stay unclobbered longer, even if it is killed.
1767fba06e3cSJeremy Morse if (!isCalleeSavedReg(DestReg))
1768fba06e3cSJeremy Morse return;
1769fba06e3cSJeremy Morse
1770fba06e3cSJeremy Morse // Remember an entry value movement. If we encounter a new debug value of
1771fba06e3cSJeremy Morse // a parameter describing only a moving of the value around, rather then
1772fba06e3cSJeremy Morse // modifying it, we are still able to use the entry value if needed.
1773fba06e3cSJeremy Morse if (isRegOtherThanSPAndFP(*DestRegOp, MI, TRI)) {
1774fba06e3cSJeremy Morse for (uint64_t ID : OpenRanges.getEntryValueBackupVarLocs()) {
1775fba06e3cSJeremy Morse LocIndex Idx = LocIndex::fromRawInteger(ID);
1776fba06e3cSJeremy Morse const VarLoc &VL = VarLocIDs[Idx];
1777e2196ddcSgbtozers if (VL.isEntryValueBackupReg(SrcReg)) {
1778fba06e3cSJeremy Morse LLVM_DEBUG(dbgs() << "Copy of the entry value: "; MI.dump(););
1779fba06e3cSJeremy Morse VarLoc EntryValLocCopyBackup =
1780fba06e3cSJeremy Morse VarLoc::CreateEntryCopyBackupLoc(VL.MI, LS, VL.Expr, DestReg);
1781fba06e3cSJeremy Morse // Stop tracking the original entry value.
1782fba06e3cSJeremy Morse OpenRanges.erase(VL);
1783fba06e3cSJeremy Morse
1784fba06e3cSJeremy Morse // Start tracking the entry value copy.
1785e2196ddcSgbtozers LocIndices EntryValCopyLocIDs = VarLocIDs.insert(EntryValLocCopyBackup);
1786e2196ddcSgbtozers OpenRanges.insert(EntryValCopyLocIDs, EntryValLocCopyBackup);
1787fba06e3cSJeremy Morse break;
1788fba06e3cSJeremy Morse }
1789fba06e3cSJeremy Morse }
1790fba06e3cSJeremy Morse }
1791fba06e3cSJeremy Morse
1792fba06e3cSJeremy Morse if (!SrcRegOp->isKill())
1793fba06e3cSJeremy Morse return;
1794fba06e3cSJeremy Morse
1795fba06e3cSJeremy Morse for (uint64_t ID : OpenRanges.getRegisterVarLocs(SrcReg)) {
1796fba06e3cSJeremy Morse LocIndex Idx = LocIndex::fromRawInteger(ID);
1797e2196ddcSgbtozers assert(VarLocIDs[Idx].usesReg(SrcReg) && "Broken VarLocSet?");
1798e2196ddcSgbtozers VarLoc::MachineLocValue Loc;
1799e2196ddcSgbtozers Loc.RegNo = SrcReg;
1800e2196ddcSgbtozers VarLoc::MachineLoc MLoc{VarLoc::MachineLocKind::RegisterKind, Loc};
1801fba06e3cSJeremy Morse insertTransferDebugPair(MI, OpenRanges, Transfers, VarLocIDs, Idx,
1802e2196ddcSgbtozers TransferKind::TransferCopy, MLoc, DestReg);
1803fba06e3cSJeremy Morse // FIXME: A comment should explain why it's correct to return early here,
1804fba06e3cSJeremy Morse // if that is in fact correct.
1805fba06e3cSJeremy Morse return;
1806fba06e3cSJeremy Morse }
1807fba06e3cSJeremy Morse }
1808fba06e3cSJeremy Morse
1809fba06e3cSJeremy Morse /// Terminate all open ranges at the end of the current basic block.
transferTerminator(MachineBasicBlock * CurMBB,OpenRangesSet & OpenRanges,VarLocInMBB & OutLocs,const VarLocMap & VarLocIDs)181020bb9fe5SJeremy Morse bool VarLocBasedLDV::transferTerminator(MachineBasicBlock *CurMBB,
1811fba06e3cSJeremy Morse OpenRangesSet &OpenRanges,
1812fba06e3cSJeremy Morse VarLocInMBB &OutLocs,
1813fba06e3cSJeremy Morse const VarLocMap &VarLocIDs) {
1814fba06e3cSJeremy Morse bool Changed = false;
1815e2196ddcSgbtozers LLVM_DEBUG({
1816e2196ddcSgbtozers VarVec VarLocs;
1817e2196ddcSgbtozers OpenRanges.getUniqueVarLocs(VarLocs, VarLocIDs);
1818e2196ddcSgbtozers for (VarLoc &VL : VarLocs) {
1819fba06e3cSJeremy Morse // Copy OpenRanges to OutLocs, if not already present.
1820fba06e3cSJeremy Morse dbgs() << "Add to OutLocs in MBB #" << CurMBB->getNumber() << ": ";
1821e2196ddcSgbtozers VL.dump(TRI);
1822e2196ddcSgbtozers }
1823fba06e3cSJeremy Morse });
1824fba06e3cSJeremy Morse VarLocSet &VLS = getVarLocsInMBB(CurMBB, OutLocs);
1825fba06e3cSJeremy Morse Changed = VLS != OpenRanges.getVarLocs();
1826fba06e3cSJeremy Morse // New OutLocs set may be different due to spill, restore or register
1827fba06e3cSJeremy Morse // copy instruction processing.
1828fba06e3cSJeremy Morse if (Changed)
1829fba06e3cSJeremy Morse VLS = OpenRanges.getVarLocs();
1830fba06e3cSJeremy Morse OpenRanges.clear();
1831fba06e3cSJeremy Morse return Changed;
1832fba06e3cSJeremy Morse }
1833fba06e3cSJeremy Morse
1834fba06e3cSJeremy Morse /// Accumulate a mapping between each DILocalVariable fragment and other
1835fba06e3cSJeremy Morse /// fragments of that DILocalVariable which overlap. This reduces work during
1836fba06e3cSJeremy Morse /// the data-flow stage from "Find any overlapping fragments" to "Check if the
1837fba06e3cSJeremy Morse /// known-to-overlap fragments are present".
1838fba06e3cSJeremy Morse /// \param MI A previously unprocessed DEBUG_VALUE instruction to analyze for
1839fba06e3cSJeremy Morse /// fragment usage.
1840fba06e3cSJeremy Morse /// \param SeenFragments Map from DILocalVariable to all fragments of that
1841fba06e3cSJeremy Morse /// Variable which are known to exist.
1842fba06e3cSJeremy Morse /// \param OverlappingFragments The overlap map being constructed, from one
1843fba06e3cSJeremy Morse /// Var/Fragment pair to a vector of fragments known to overlap.
accumulateFragmentMap(MachineInstr & MI,VarToFragments & SeenFragments,OverlapMap & OverlappingFragments)184420bb9fe5SJeremy Morse void VarLocBasedLDV::accumulateFragmentMap(MachineInstr &MI,
1845fba06e3cSJeremy Morse VarToFragments &SeenFragments,
1846fba06e3cSJeremy Morse OverlapMap &OverlappingFragments) {
1847fba06e3cSJeremy Morse DebugVariable MIVar(MI.getDebugVariable(), MI.getDebugExpression(),
1848fba06e3cSJeremy Morse MI.getDebugLoc()->getInlinedAt());
1849fba06e3cSJeremy Morse FragmentInfo ThisFragment = MIVar.getFragmentOrDefault();
1850fba06e3cSJeremy Morse
1851fba06e3cSJeremy Morse // If this is the first sighting of this variable, then we are guaranteed
1852fba06e3cSJeremy Morse // there are currently no overlapping fragments either. Initialize the set
1853fba06e3cSJeremy Morse // of seen fragments, record no overlaps for the current one, and return.
1854fba06e3cSJeremy Morse auto SeenIt = SeenFragments.find(MIVar.getVariable());
1855fba06e3cSJeremy Morse if (SeenIt == SeenFragments.end()) {
1856fba06e3cSJeremy Morse SmallSet<FragmentInfo, 4> OneFragment;
1857fba06e3cSJeremy Morse OneFragment.insert(ThisFragment);
1858fba06e3cSJeremy Morse SeenFragments.insert({MIVar.getVariable(), OneFragment});
1859fba06e3cSJeremy Morse
1860fba06e3cSJeremy Morse OverlappingFragments.insert({{MIVar.getVariable(), ThisFragment}, {}});
1861fba06e3cSJeremy Morse return;
1862fba06e3cSJeremy Morse }
1863fba06e3cSJeremy Morse
1864fba06e3cSJeremy Morse // If this particular Variable/Fragment pair already exists in the overlap
1865fba06e3cSJeremy Morse // map, it has already been accounted for.
1866fba06e3cSJeremy Morse auto IsInOLapMap =
1867fba06e3cSJeremy Morse OverlappingFragments.insert({{MIVar.getVariable(), ThisFragment}, {}});
1868fba06e3cSJeremy Morse if (!IsInOLapMap.second)
1869fba06e3cSJeremy Morse return;
1870fba06e3cSJeremy Morse
1871fba06e3cSJeremy Morse auto &ThisFragmentsOverlaps = IsInOLapMap.first->second;
1872fba06e3cSJeremy Morse auto &AllSeenFragments = SeenIt->second;
1873fba06e3cSJeremy Morse
1874fba06e3cSJeremy Morse // Otherwise, examine all other seen fragments for this variable, with "this"
1875fba06e3cSJeremy Morse // fragment being a previously unseen fragment. Record any pair of
1876fba06e3cSJeremy Morse // overlapping fragments.
1877*9e6d1f4bSKazu Hirata for (const auto &ASeenFragment : AllSeenFragments) {
1878fba06e3cSJeremy Morse // Does this previously seen fragment overlap?
1879fba06e3cSJeremy Morse if (DIExpression::fragmentsOverlap(ThisFragment, ASeenFragment)) {
1880fba06e3cSJeremy Morse // Yes: Mark the current fragment as being overlapped.
1881fba06e3cSJeremy Morse ThisFragmentsOverlaps.push_back(ASeenFragment);
1882fba06e3cSJeremy Morse // Mark the previously seen fragment as being overlapped by the current
1883fba06e3cSJeremy Morse // one.
1884fba06e3cSJeremy Morse auto ASeenFragmentsOverlaps =
1885fba06e3cSJeremy Morse OverlappingFragments.find({MIVar.getVariable(), ASeenFragment});
1886fba06e3cSJeremy Morse assert(ASeenFragmentsOverlaps != OverlappingFragments.end() &&
1887fba06e3cSJeremy Morse "Previously seen var fragment has no vector of overlaps");
1888fba06e3cSJeremy Morse ASeenFragmentsOverlaps->second.push_back(ThisFragment);
1889fba06e3cSJeremy Morse }
1890fba06e3cSJeremy Morse }
1891fba06e3cSJeremy Morse
1892fba06e3cSJeremy Morse AllSeenFragments.insert(ThisFragment);
1893fba06e3cSJeremy Morse }
1894fba06e3cSJeremy Morse
1895fba06e3cSJeremy Morse /// This routine creates OpenRanges.
process(MachineInstr & MI,OpenRangesSet & OpenRanges,VarLocMap & VarLocIDs,TransferMap & Transfers,InstToEntryLocMap & EntryValTransfers,RegDefToInstMap & RegSetInstrs)189620bb9fe5SJeremy Morse void VarLocBasedLDV::process(MachineInstr &MI, OpenRangesSet &OpenRanges,
189786f5288eSDjordje Todorovic VarLocMap &VarLocIDs, TransferMap &Transfers,
189886f5288eSDjordje Todorovic InstToEntryLocMap &EntryValTransfers,
189986f5288eSDjordje Todorovic RegDefToInstMap &RegSetInstrs) {
190086f5288eSDjordje Todorovic if (!MI.isDebugInstr())
190186f5288eSDjordje Todorovic LastNonDbgMI = &MI;
190286f5288eSDjordje Todorovic transferDebugValue(MI, OpenRanges, VarLocIDs, EntryValTransfers,
190386f5288eSDjordje Todorovic RegSetInstrs);
190486f5288eSDjordje Todorovic transferRegisterDef(MI, OpenRanges, VarLocIDs, EntryValTransfers,
190586f5288eSDjordje Todorovic RegSetInstrs);
1906fba06e3cSJeremy Morse transferRegisterCopy(MI, OpenRanges, VarLocIDs, Transfers);
1907fba06e3cSJeremy Morse transferSpillOrRestoreInst(MI, OpenRanges, VarLocIDs, Transfers);
1908fba06e3cSJeremy Morse }
1909fba06e3cSJeremy Morse
1910fba06e3cSJeremy Morse /// This routine joins the analysis results of all incoming edges in @MBB by
1911fba06e3cSJeremy Morse /// inserting a new DBG_VALUE instruction at the start of the @MBB - if the same
1912fba06e3cSJeremy Morse /// source variable in all the predecessors of @MBB reside in the same location.
join(MachineBasicBlock & MBB,VarLocInMBB & OutLocs,VarLocInMBB & InLocs,const VarLocMap & VarLocIDs,SmallPtrSet<const MachineBasicBlock *,16> & Visited,SmallPtrSetImpl<const MachineBasicBlock * > & ArtificialBlocks)191320bb9fe5SJeremy Morse bool VarLocBasedLDV::join(
1914fba06e3cSJeremy Morse MachineBasicBlock &MBB, VarLocInMBB &OutLocs, VarLocInMBB &InLocs,
1915fba06e3cSJeremy Morse const VarLocMap &VarLocIDs,
1916fba06e3cSJeremy Morse SmallPtrSet<const MachineBasicBlock *, 16> &Visited,
1917fba06e3cSJeremy Morse SmallPtrSetImpl<const MachineBasicBlock *> &ArtificialBlocks) {
1918fba06e3cSJeremy Morse LLVM_DEBUG(dbgs() << "join MBB: " << MBB.getNumber() << "\n");
1919fba06e3cSJeremy Morse
1920fba06e3cSJeremy Morse VarLocSet InLocsT(Alloc); // Temporary incoming locations.
1921fba06e3cSJeremy Morse
1922fba06e3cSJeremy Morse // For all predecessors of this MBB, find the set of VarLocs that
1923fba06e3cSJeremy Morse // can be joined.
1924fba06e3cSJeremy Morse int NumVisited = 0;
1925*9e6d1f4bSKazu Hirata for (auto *p : MBB.predecessors()) {
1926fba06e3cSJeremy Morse // Ignore backedges if we have not visited the predecessor yet. As the
1927fba06e3cSJeremy Morse // predecessor hasn't yet had locations propagated into it, most locations
1928fba06e3cSJeremy Morse // will not yet be valid, so treat them as all being uninitialized and
1929fba06e3cSJeremy Morse // potentially valid. If a location guessed to be correct here is
1930fba06e3cSJeremy Morse // invalidated later, we will remove it when we revisit this block.
1931fba06e3cSJeremy Morse if (!Visited.count(p)) {
1932fba06e3cSJeremy Morse LLVM_DEBUG(dbgs() << " ignoring unvisited pred MBB: " << p->getNumber()
1933fba06e3cSJeremy Morse << "\n");
1934fba06e3cSJeremy Morse continue;
1935fba06e3cSJeremy Morse }
1936fba06e3cSJeremy Morse auto OL = OutLocs.find(p);
1937fba06e3cSJeremy Morse // Join is null in case of empty OutLocs from any of the pred.
1938fba06e3cSJeremy Morse if (OL == OutLocs.end())
1939fba06e3cSJeremy Morse return false;
1940fba06e3cSJeremy Morse
1941fba06e3cSJeremy Morse // Just copy over the Out locs to incoming locs for the first visited
1942fba06e3cSJeremy Morse // predecessor, and for all other predecessors join the Out locs.
19431eada2adSKazu Hirata VarLocSet &OutLocVLS = *OL->second;
1944fba06e3cSJeremy Morse if (!NumVisited)
1945fba06e3cSJeremy Morse InLocsT = OutLocVLS;
1946fba06e3cSJeremy Morse else
1947fba06e3cSJeremy Morse InLocsT &= OutLocVLS;
1948fba06e3cSJeremy Morse
1949fba06e3cSJeremy Morse LLVM_DEBUG({
1950fba06e3cSJeremy Morse if (!InLocsT.empty()) {
1951e2196ddcSgbtozers VarVec VarLocs;
1952e2196ddcSgbtozers collectAllVarLocs(VarLocs, InLocsT, VarLocIDs);
1953e2196ddcSgbtozers for (const VarLoc &VL : VarLocs)
1954fba06e3cSJeremy Morse dbgs() << " gathered candidate incoming var: "
1955e2196ddcSgbtozers << VL.Var.getVariable()->getName() << "\n";
1956fba06e3cSJeremy Morse }
1957fba06e3cSJeremy Morse });
1958fba06e3cSJeremy Morse
1959fba06e3cSJeremy Morse NumVisited++;
1960fba06e3cSJeremy Morse }
1961fba06e3cSJeremy Morse
1962fba06e3cSJeremy Morse // Filter out DBG_VALUES that are out of scope.
1963fba06e3cSJeremy Morse VarLocSet KillSet(Alloc);
1964fba06e3cSJeremy Morse bool IsArtificial = ArtificialBlocks.count(&MBB);
1965fba06e3cSJeremy Morse if (!IsArtificial) {
1966fba06e3cSJeremy Morse for (uint64_t ID : InLocsT) {
1967fba06e3cSJeremy Morse LocIndex Idx = LocIndex::fromRawInteger(ID);
1968fba06e3cSJeremy Morse if (!VarLocIDs[Idx].dominates(LS, MBB)) {
1969fba06e3cSJeremy Morse KillSet.set(ID);
1970fba06e3cSJeremy Morse LLVM_DEBUG({
1971fba06e3cSJeremy Morse auto Name = VarLocIDs[Idx].Var.getVariable()->getName();
1972fba06e3cSJeremy Morse dbgs() << " killing " << Name << ", it doesn't dominate MBB\n";
1973fba06e3cSJeremy Morse });
1974fba06e3cSJeremy Morse }
1975fba06e3cSJeremy Morse }
1976fba06e3cSJeremy Morse }
1977fba06e3cSJeremy Morse InLocsT.intersectWithComplement(KillSet);
1978fba06e3cSJeremy Morse
1979fba06e3cSJeremy Morse // As we are processing blocks in reverse post-order we
1980fba06e3cSJeremy Morse // should have processed at least one predecessor, unless it
1981fba06e3cSJeremy Morse // is the entry block which has no predecessor.
1982fba06e3cSJeremy Morse assert((NumVisited || MBB.pred_empty()) &&
1983fba06e3cSJeremy Morse "Should have processed at least one predecessor");
1984fba06e3cSJeremy Morse
1985fba06e3cSJeremy Morse VarLocSet &ILS = getVarLocsInMBB(&MBB, InLocs);
1986fba06e3cSJeremy Morse bool Changed = false;
1987fba06e3cSJeremy Morse if (ILS != InLocsT) {
1988fba06e3cSJeremy Morse ILS = InLocsT;
1989fba06e3cSJeremy Morse Changed = true;
1990fba06e3cSJeremy Morse }
1991fba06e3cSJeremy Morse
1992fba06e3cSJeremy Morse return Changed;
1993fba06e3cSJeremy Morse }
1994fba06e3cSJeremy Morse
flushPendingLocs(VarLocInMBB & PendingInLocs,VarLocMap & VarLocIDs)199520bb9fe5SJeremy Morse void VarLocBasedLDV::flushPendingLocs(VarLocInMBB &PendingInLocs,
1996fba06e3cSJeremy Morse VarLocMap &VarLocIDs) {
1997fba06e3cSJeremy Morse // PendingInLocs records all locations propagated into blocks, which have
1998fba06e3cSJeremy Morse // not had DBG_VALUE insts created. Go through and create those insts now.
1999fba06e3cSJeremy Morse for (auto &Iter : PendingInLocs) {
2000fba06e3cSJeremy Morse // Map is keyed on a constant pointer, unwrap it so we can insert insts.
2001fba06e3cSJeremy Morse auto &MBB = const_cast<MachineBasicBlock &>(*Iter.first);
20021eada2adSKazu Hirata VarLocSet &Pending = *Iter.second;
2003fba06e3cSJeremy Morse
2004e2196ddcSgbtozers SmallVector<VarLoc, 32> VarLocs;
2005e2196ddcSgbtozers collectAllVarLocs(VarLocs, Pending, VarLocIDs);
2006e2196ddcSgbtozers
2007e2196ddcSgbtozers for (VarLoc DiffIt : VarLocs) {
2008fba06e3cSJeremy Morse // The ID location is live-in to MBB -- work out what kind of machine
2009fba06e3cSJeremy Morse // location it is and create a DBG_VALUE.
2010fba06e3cSJeremy Morse if (DiffIt.isEntryBackupLoc())
2011fba06e3cSJeremy Morse continue;
2012fba06e3cSJeremy Morse MachineInstr *MI = DiffIt.BuildDbgValue(*MBB.getParent());
2013fba06e3cSJeremy Morse MBB.insert(MBB.instr_begin(), MI);
2014fba06e3cSJeremy Morse
2015fba06e3cSJeremy Morse (void)MI;
2016fba06e3cSJeremy Morse LLVM_DEBUG(dbgs() << "Inserted: "; MI->dump(););
2017fba06e3cSJeremy Morse }
2018fba06e3cSJeremy Morse }
2019fba06e3cSJeremy Morse }
2020fba06e3cSJeremy Morse
isEntryValueCandidate(const MachineInstr & MI,const DefinedRegsSet & DefinedRegs) const202120bb9fe5SJeremy Morse bool VarLocBasedLDV::isEntryValueCandidate(
2022fba06e3cSJeremy Morse const MachineInstr &MI, const DefinedRegsSet &DefinedRegs) const {
2023fba06e3cSJeremy Morse assert(MI.isDebugValue() && "This must be DBG_VALUE.");
2024fba06e3cSJeremy Morse
2025fba06e3cSJeremy Morse // TODO: Add support for local variables that are expressed in terms of
2026fba06e3cSJeremy Morse // parameters entry values.
2027fba06e3cSJeremy Morse // TODO: Add support for modified arguments that can be expressed
2028fba06e3cSJeremy Morse // by using its entry value.
2029fba06e3cSJeremy Morse auto *DIVar = MI.getDebugVariable();
2030fba06e3cSJeremy Morse if (!DIVar->isParameter())
2031fba06e3cSJeremy Morse return false;
2032fba06e3cSJeremy Morse
2033fba06e3cSJeremy Morse // Do not consider parameters that belong to an inlined function.
2034fba06e3cSJeremy Morse if (MI.getDebugLoc()->getInlinedAt())
2035fba06e3cSJeremy Morse return false;
2036fba06e3cSJeremy Morse
2037fba06e3cSJeremy Morse // Only consider parameters that are described using registers. Parameters
2038fba06e3cSJeremy Morse // that are passed on the stack are not yet supported, so ignore debug
2039fba06e3cSJeremy Morse // values that are described by the frame or stack pointer.
2040fba06e3cSJeremy Morse if (!isRegOtherThanSPAndFP(MI.getDebugOperand(0), MI, TRI))
2041fba06e3cSJeremy Morse return false;
2042fba06e3cSJeremy Morse
2043fba06e3cSJeremy Morse // If a parameter's value has been propagated from the caller, then the
2044fba06e3cSJeremy Morse // parameter's DBG_VALUE may be described using a register defined by some
2045fba06e3cSJeremy Morse // instruction in the entry block, in which case we shouldn't create an
2046fba06e3cSJeremy Morse // entry value.
2047fba06e3cSJeremy Morse if (DefinedRegs.count(MI.getDebugOperand(0).getReg()))
2048fba06e3cSJeremy Morse return false;
2049fba06e3cSJeremy Morse
2050fba06e3cSJeremy Morse // TODO: Add support for parameters that have a pre-existing debug expressions
2051fba06e3cSJeremy Morse // (e.g. fragments).
2052fba06e3cSJeremy Morse if (MI.getDebugExpression()->getNumElements() > 0)
2053fba06e3cSJeremy Morse return false;
2054fba06e3cSJeremy Morse
2055fba06e3cSJeremy Morse return true;
2056fba06e3cSJeremy Morse }
2057fba06e3cSJeremy Morse
2058fba06e3cSJeremy Morse /// Collect all register defines (including aliases) for the given instruction.
collectRegDefs(const MachineInstr & MI,DefinedRegsSet & Regs,const TargetRegisterInfo * TRI)2059fba06e3cSJeremy Morse static void collectRegDefs(const MachineInstr &MI, DefinedRegsSet &Regs,
2060fba06e3cSJeremy Morse const TargetRegisterInfo *TRI) {
2061fba06e3cSJeremy Morse for (const MachineOperand &MO : MI.operands())
2062fba06e3cSJeremy Morse if (MO.isReg() && MO.isDef() && MO.getReg())
2063fba06e3cSJeremy Morse for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid(); ++AI)
2064fba06e3cSJeremy Morse Regs.insert(*AI);
2065fba06e3cSJeremy Morse }
2066fba06e3cSJeremy Morse
2067fba06e3cSJeremy Morse /// This routine records the entry values of function parameters. The values
2068fba06e3cSJeremy Morse /// could be used as backup values. If we loose the track of some unmodified
2069fba06e3cSJeremy Morse /// parameters, the backup values will be used as a primary locations.
recordEntryValue(const MachineInstr & MI,const DefinedRegsSet & DefinedRegs,OpenRangesSet & OpenRanges,VarLocMap & VarLocIDs)207020bb9fe5SJeremy Morse void VarLocBasedLDV::recordEntryValue(const MachineInstr &MI,
2071fba06e3cSJeremy Morse const DefinedRegsSet &DefinedRegs,
2072fba06e3cSJeremy Morse OpenRangesSet &OpenRanges,
2073fba06e3cSJeremy Morse VarLocMap &VarLocIDs) {
207420bb9fe5SJeremy Morse if (TPC) {
2075fba06e3cSJeremy Morse auto &TM = TPC->getTM<TargetMachine>();
2076fba06e3cSJeremy Morse if (!TM.Options.ShouldEmitDebugEntryValues())
2077fba06e3cSJeremy Morse return;
2078fba06e3cSJeremy Morse }
2079fba06e3cSJeremy Morse
2080fba06e3cSJeremy Morse DebugVariable V(MI.getDebugVariable(), MI.getDebugExpression(),
2081fba06e3cSJeremy Morse MI.getDebugLoc()->getInlinedAt());
2082fba06e3cSJeremy Morse
2083fba06e3cSJeremy Morse if (!isEntryValueCandidate(MI, DefinedRegs) ||
2084fba06e3cSJeremy Morse OpenRanges.getEntryValueBackup(V))
2085fba06e3cSJeremy Morse return;
2086fba06e3cSJeremy Morse
2087fba06e3cSJeremy Morse LLVM_DEBUG(dbgs() << "Creating the backup entry location: "; MI.dump(););
2088fba06e3cSJeremy Morse
2089fba06e3cSJeremy Morse // Create the entry value and use it as a backup location until it is
2090fba06e3cSJeremy Morse // valid. It is valid until a parameter is not changed.
2091fba06e3cSJeremy Morse DIExpression *NewExpr =
2092fba06e3cSJeremy Morse DIExpression::prepend(MI.getDebugExpression(), DIExpression::EntryValue);
2093fba06e3cSJeremy Morse VarLoc EntryValLocAsBackup = VarLoc::CreateEntryBackupLoc(MI, LS, NewExpr);
2094e2196ddcSgbtozers LocIndices EntryValLocIDs = VarLocIDs.insert(EntryValLocAsBackup);
2095e2196ddcSgbtozers OpenRanges.insert(EntryValLocIDs, EntryValLocAsBackup);
2096fba06e3cSJeremy Morse }
2097fba06e3cSJeremy Morse
2098fba06e3cSJeremy Morse /// Calculate the liveness information for the given machine function and
2099fba06e3cSJeremy Morse /// extend ranges across basic blocks.
ExtendRanges(MachineFunction & MF,MachineDominatorTree * DomTree,TargetPassConfig * TPC,unsigned InputBBLimit,unsigned InputDbgValLimit)2100a3936a6cSJeremy Morse bool VarLocBasedLDV::ExtendRanges(MachineFunction &MF,
2101a3936a6cSJeremy Morse MachineDominatorTree *DomTree,
2102a3936a6cSJeremy Morse TargetPassConfig *TPC, unsigned InputBBLimit,
2103708cbda5SJeremy Morse unsigned InputDbgValLimit) {
2104a3936a6cSJeremy Morse (void)DomTree;
2105fba06e3cSJeremy Morse LLVM_DEBUG(dbgs() << "\nDebug Range Extension\n");
2106fba06e3cSJeremy Morse
210720bb9fe5SJeremy Morse if (!MF.getFunction().getSubprogram())
210820bb9fe5SJeremy Morse // VarLocBaseLDV will already have removed all DBG_VALUEs.
210920bb9fe5SJeremy Morse return false;
211020bb9fe5SJeremy Morse
211120bb9fe5SJeremy Morse // Skip functions from NoDebug compilation units.
211220bb9fe5SJeremy Morse if (MF.getFunction().getSubprogram()->getUnit()->getEmissionKind() ==
211320bb9fe5SJeremy Morse DICompileUnit::NoDebug)
211420bb9fe5SJeremy Morse return false;
211520bb9fe5SJeremy Morse
211620bb9fe5SJeremy Morse TRI = MF.getSubtarget().getRegisterInfo();
211720bb9fe5SJeremy Morse TII = MF.getSubtarget().getInstrInfo();
211820bb9fe5SJeremy Morse TFI = MF.getSubtarget().getFrameLowering();
211920bb9fe5SJeremy Morse TFI->getCalleeSaves(MF, CalleeSavedRegs);
212020bb9fe5SJeremy Morse this->TPC = TPC;
212120bb9fe5SJeremy Morse LS.initialize(MF);
212220bb9fe5SJeremy Morse
2123fba06e3cSJeremy Morse bool Changed = false;
2124fba06e3cSJeremy Morse bool OLChanged = false;
2125fba06e3cSJeremy Morse bool MBBJoined = false;
2126fba06e3cSJeremy Morse
2127fba06e3cSJeremy Morse VarLocMap VarLocIDs; // Map VarLoc<>unique ID for use in bitvectors.
2128fba06e3cSJeremy Morse OverlapMap OverlapFragments; // Map of overlapping variable fragments.
2129fba06e3cSJeremy Morse OpenRangesSet OpenRanges(Alloc, OverlapFragments);
2130fba06e3cSJeremy Morse // Ranges that are open until end of bb.
2131fba06e3cSJeremy Morse VarLocInMBB OutLocs; // Ranges that exist beyond bb.
2132fba06e3cSJeremy Morse VarLocInMBB InLocs; // Ranges that are incoming after joining.
2133fba06e3cSJeremy Morse TransferMap Transfers; // DBG_VALUEs associated with transfers (such as
2134fba06e3cSJeremy Morse // spills, copies and restores).
213586f5288eSDjordje Todorovic // Map responsible MI to attached Transfer emitted from Backup Entry Value.
213686f5288eSDjordje Todorovic InstToEntryLocMap EntryValTransfers;
213786f5288eSDjordje Todorovic // Map a Register to the last MI which clobbered it.
213886f5288eSDjordje Todorovic RegDefToInstMap RegSetInstrs;
2139fba06e3cSJeremy Morse
2140fba06e3cSJeremy Morse VarToFragments SeenFragments;
2141fba06e3cSJeremy Morse
2142fba06e3cSJeremy Morse // Blocks which are artificial, i.e. blocks which exclusively contain
2143fba06e3cSJeremy Morse // instructions without locations, or with line 0 locations.
2144fba06e3cSJeremy Morse SmallPtrSet<const MachineBasicBlock *, 16> ArtificialBlocks;
2145fba06e3cSJeremy Morse
2146fba06e3cSJeremy Morse DenseMap<unsigned int, MachineBasicBlock *> OrderToBB;
2147fba06e3cSJeremy Morse DenseMap<MachineBasicBlock *, unsigned int> BBToOrder;
2148fba06e3cSJeremy Morse std::priority_queue<unsigned int, std::vector<unsigned int>,
2149fba06e3cSJeremy Morse std::greater<unsigned int>>
2150fba06e3cSJeremy Morse Worklist;
2151fba06e3cSJeremy Morse std::priority_queue<unsigned int, std::vector<unsigned int>,
2152fba06e3cSJeremy Morse std::greater<unsigned int>>
2153fba06e3cSJeremy Morse Pending;
2154fba06e3cSJeremy Morse
2155fba06e3cSJeremy Morse // Set of register defines that are seen when traversing the entry block
2156fba06e3cSJeremy Morse // looking for debug entry value candidates.
2157fba06e3cSJeremy Morse DefinedRegsSet DefinedRegs;
2158fba06e3cSJeremy Morse
2159fba06e3cSJeremy Morse // Only in the case of entry MBB collect DBG_VALUEs representing
2160fba06e3cSJeremy Morse // function parameters in order to generate debug entry values for them.
2161fba06e3cSJeremy Morse MachineBasicBlock &First_MBB = *(MF.begin());
2162fba06e3cSJeremy Morse for (auto &MI : First_MBB) {
2163fba06e3cSJeremy Morse collectRegDefs(MI, DefinedRegs, TRI);
2164fba06e3cSJeremy Morse if (MI.isDebugValue())
2165fba06e3cSJeremy Morse recordEntryValue(MI, DefinedRegs, OpenRanges, VarLocIDs);
2166fba06e3cSJeremy Morse }
2167fba06e3cSJeremy Morse
2168fba06e3cSJeremy Morse // Initialize per-block structures and scan for fragment overlaps.
2169fba06e3cSJeremy Morse for (auto &MBB : MF)
2170fba06e3cSJeremy Morse for (auto &MI : MBB)
2171fba06e3cSJeremy Morse if (MI.isDebugValue())
2172fba06e3cSJeremy Morse accumulateFragmentMap(MI, SeenFragments, OverlapFragments);
2173fba06e3cSJeremy Morse
2174fba06e3cSJeremy Morse auto hasNonArtificialLocation = [](const MachineInstr &MI) -> bool {
2175fba06e3cSJeremy Morse if (const DebugLoc &DL = MI.getDebugLoc())
2176fba06e3cSJeremy Morse return DL.getLine() != 0;
2177fba06e3cSJeremy Morse return false;
2178fba06e3cSJeremy Morse };
2179fba06e3cSJeremy Morse for (auto &MBB : MF)
2180fba06e3cSJeremy Morse if (none_of(MBB.instrs(), hasNonArtificialLocation))
2181fba06e3cSJeremy Morse ArtificialBlocks.insert(&MBB);
2182fba06e3cSJeremy Morse
2183fba06e3cSJeremy Morse LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs,
2184fba06e3cSJeremy Morse "OutLocs after initialization", dbgs()));
2185fba06e3cSJeremy Morse
2186fba06e3cSJeremy Morse ReversePostOrderTraversal<MachineFunction *> RPOT(&MF);
2187fba06e3cSJeremy Morse unsigned int RPONumber = 0;
2188d5adba10SKazu Hirata for (MachineBasicBlock *MBB : RPOT) {
2189d5adba10SKazu Hirata OrderToBB[RPONumber] = MBB;
2190d5adba10SKazu Hirata BBToOrder[MBB] = RPONumber;
2191fba06e3cSJeremy Morse Worklist.push(RPONumber);
2192fba06e3cSJeremy Morse ++RPONumber;
2193fba06e3cSJeremy Morse }
2194fba06e3cSJeremy Morse
2195fba06e3cSJeremy Morse if (RPONumber > InputBBLimit) {
2196fba06e3cSJeremy Morse unsigned NumInputDbgValues = 0;
2197fba06e3cSJeremy Morse for (auto &MBB : MF)
2198fba06e3cSJeremy Morse for (auto &MI : MBB)
2199fba06e3cSJeremy Morse if (MI.isDebugValue())
2200fba06e3cSJeremy Morse ++NumInputDbgValues;
2201708cbda5SJeremy Morse if (NumInputDbgValues > InputDbgValLimit) {
220220bb9fe5SJeremy Morse LLVM_DEBUG(dbgs() << "Disabling VarLocBasedLDV: " << MF.getName()
2203fba06e3cSJeremy Morse << " has " << RPONumber << " basic blocks and "
2204fba06e3cSJeremy Morse << NumInputDbgValues
2205fba06e3cSJeremy Morse << " input DBG_VALUEs, exceeding limits.\n");
2206fba06e3cSJeremy Morse return false;
2207fba06e3cSJeremy Morse }
2208fba06e3cSJeremy Morse }
2209fba06e3cSJeremy Morse
2210fba06e3cSJeremy Morse // This is a standard "union of predecessor outs" dataflow problem.
2211fba06e3cSJeremy Morse // To solve it, we perform join() and process() using the two worklist method
2212fba06e3cSJeremy Morse // until the ranges converge.
2213fba06e3cSJeremy Morse // Ranges have converged when both worklists are empty.
2214fba06e3cSJeremy Morse SmallPtrSet<const MachineBasicBlock *, 16> Visited;
2215fba06e3cSJeremy Morse while (!Worklist.empty() || !Pending.empty()) {
2216fba06e3cSJeremy Morse // We track what is on the pending worklist to avoid inserting the same
2217fba06e3cSJeremy Morse // thing twice. We could avoid this with a custom priority queue, but this
2218fba06e3cSJeremy Morse // is probably not worth it.
2219fba06e3cSJeremy Morse SmallPtrSet<MachineBasicBlock *, 16> OnPending;
2220fba06e3cSJeremy Morse LLVM_DEBUG(dbgs() << "Processing Worklist\n");
2221fba06e3cSJeremy Morse while (!Worklist.empty()) {
2222fba06e3cSJeremy Morse MachineBasicBlock *MBB = OrderToBB[Worklist.top()];
2223fba06e3cSJeremy Morse Worklist.pop();
2224fba06e3cSJeremy Morse MBBJoined = join(*MBB, OutLocs, InLocs, VarLocIDs, Visited,
2225fba06e3cSJeremy Morse ArtificialBlocks);
2226fba06e3cSJeremy Morse MBBJoined |= Visited.insert(MBB).second;
2227fba06e3cSJeremy Morse if (MBBJoined) {
2228fba06e3cSJeremy Morse MBBJoined = false;
2229fba06e3cSJeremy Morse Changed = true;
2230fba06e3cSJeremy Morse // Now that we have started to extend ranges across BBs we need to
2231fba06e3cSJeremy Morse // examine spill, copy and restore instructions to see whether they
2232fba06e3cSJeremy Morse // operate with registers that correspond to user variables.
2233fba06e3cSJeremy Morse // First load any pending inlocs.
2234fba06e3cSJeremy Morse OpenRanges.insertFromLocSet(getVarLocsInMBB(MBB, InLocs), VarLocIDs);
223586f5288eSDjordje Todorovic LastNonDbgMI = nullptr;
223686f5288eSDjordje Todorovic RegSetInstrs.clear();
2237fba06e3cSJeremy Morse for (auto &MI : *MBB)
223886f5288eSDjordje Todorovic process(MI, OpenRanges, VarLocIDs, Transfers, EntryValTransfers,
223986f5288eSDjordje Todorovic RegSetInstrs);
2240fba06e3cSJeremy Morse OLChanged |= transferTerminator(MBB, OpenRanges, OutLocs, VarLocIDs);
2241fba06e3cSJeremy Morse
2242fba06e3cSJeremy Morse LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs,
2243fba06e3cSJeremy Morse "OutLocs after propagating", dbgs()));
2244fba06e3cSJeremy Morse LLVM_DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs,
2245fba06e3cSJeremy Morse "InLocs after propagating", dbgs()));
2246fba06e3cSJeremy Morse
2247fba06e3cSJeremy Morse if (OLChanged) {
2248fba06e3cSJeremy Morse OLChanged = false;
2249*9e6d1f4bSKazu Hirata for (auto *s : MBB->successors())
2250fba06e3cSJeremy Morse if (OnPending.insert(s).second) {
2251fba06e3cSJeremy Morse Pending.push(BBToOrder[s]);
2252fba06e3cSJeremy Morse }
2253fba06e3cSJeremy Morse }
2254fba06e3cSJeremy Morse }
2255fba06e3cSJeremy Morse }
2256fba06e3cSJeremy Morse Worklist.swap(Pending);
2257fba06e3cSJeremy Morse // At this point, pending must be empty, since it was just the empty
2258fba06e3cSJeremy Morse // worklist
2259fba06e3cSJeremy Morse assert(Pending.empty() && "Pending should be empty");
2260fba06e3cSJeremy Morse }
2261fba06e3cSJeremy Morse
2262fba06e3cSJeremy Morse // Add any DBG_VALUE instructions created by location transfers.
2263fba06e3cSJeremy Morse for (auto &TR : Transfers) {
2264fba06e3cSJeremy Morse assert(!TR.TransferInst->isTerminator() &&
2265fba06e3cSJeremy Morse "Cannot insert DBG_VALUE after terminator");
2266fba06e3cSJeremy Morse MachineBasicBlock *MBB = TR.TransferInst->getParent();
2267fba06e3cSJeremy Morse const VarLoc &VL = VarLocIDs[TR.LocationID];
2268fba06e3cSJeremy Morse MachineInstr *MI = VL.BuildDbgValue(MF);
2269fba06e3cSJeremy Morse MBB->insertAfterBundle(TR.TransferInst->getIterator(), MI);
2270fba06e3cSJeremy Morse }
2271fba06e3cSJeremy Morse Transfers.clear();
2272fba06e3cSJeremy Morse
227386f5288eSDjordje Todorovic // Add DBG_VALUEs created using Backup Entry Value location.
227486f5288eSDjordje Todorovic for (auto &TR : EntryValTransfers) {
227586f5288eSDjordje Todorovic MachineInstr *TRInst = const_cast<MachineInstr *>(TR.first);
227686f5288eSDjordje Todorovic assert(!TRInst->isTerminator() &&
227786f5288eSDjordje Todorovic "Cannot insert DBG_VALUE after terminator");
227886f5288eSDjordje Todorovic MachineBasicBlock *MBB = TRInst->getParent();
227986f5288eSDjordje Todorovic const VarLoc &VL = VarLocIDs[TR.second];
228086f5288eSDjordje Todorovic MachineInstr *MI = VL.BuildDbgValue(MF);
228186f5288eSDjordje Todorovic MBB->insertAfterBundle(TRInst->getIterator(), MI);
228286f5288eSDjordje Todorovic }
228386f5288eSDjordje Todorovic EntryValTransfers.clear();
228486f5288eSDjordje Todorovic
2285fba06e3cSJeremy Morse // Deferred inlocs will not have had any DBG_VALUE insts created; do
2286fba06e3cSJeremy Morse // that now.
2287fba06e3cSJeremy Morse flushPendingLocs(InLocs, VarLocIDs);
2288fba06e3cSJeremy Morse
2289fba06e3cSJeremy Morse LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, "Final OutLocs", dbgs()));
2290fba06e3cSJeremy Morse LLVM_DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs, "Final InLocs", dbgs()));
2291fba06e3cSJeremy Morse return Changed;
2292fba06e3cSJeremy Morse }
2293fba06e3cSJeremy Morse
229420bb9fe5SJeremy Morse LDVImpl *
makeVarLocBasedLiveDebugValues()229520bb9fe5SJeremy Morse llvm::makeVarLocBasedLiveDebugValues()
229620bb9fe5SJeremy Morse {
229720bb9fe5SJeremy Morse return new VarLocBasedLDV();
2298fba06e3cSJeremy Morse }
2299