1e8d8bef9SDimitry Andric //===- VarLocBasedImpl.cpp - Tracking Debug Value MIs with VarLoc class----===// 2e8d8bef9SDimitry Andric // 3e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6e8d8bef9SDimitry Andric // 7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 8e8d8bef9SDimitry Andric /// 9e8d8bef9SDimitry Andric /// \file VarLocBasedImpl.cpp 10e8d8bef9SDimitry Andric /// 11e8d8bef9SDimitry Andric /// LiveDebugValues is an optimistic "available expressions" dataflow 12e8d8bef9SDimitry Andric /// algorithm. The set of expressions is the set of machine locations 13bdd1243dSDimitry Andric /// (registers, spill slots, constants, and target indices) that a variable 14bdd1243dSDimitry Andric /// fragment might be located, qualified by a DIExpression and indirect-ness 15bdd1243dSDimitry Andric /// flag, while each variable is identified by a DebugVariable object. The 16bdd1243dSDimitry Andric /// availability of an expression begins when a DBG_VALUE instruction specifies 17bdd1243dSDimitry Andric /// the location of a DebugVariable, and continues until that location is 18bdd1243dSDimitry Andric /// clobbered or re-specified by a different DBG_VALUE for the same 19bdd1243dSDimitry Andric /// DebugVariable. 20e8d8bef9SDimitry Andric /// 21e8d8bef9SDimitry Andric /// The output of LiveDebugValues is additional DBG_VALUE instructions, 22e8d8bef9SDimitry Andric /// placed to extend variable locations as far they're available. This file 23e8d8bef9SDimitry Andric /// and the VarLocBasedLDV class is an implementation that explicitly tracks 24e8d8bef9SDimitry Andric /// locations, using the VarLoc class. 25e8d8bef9SDimitry Andric /// 26e8d8bef9SDimitry Andric /// The canonical "available expressions" problem doesn't have expression 27e8d8bef9SDimitry Andric /// clobbering, instead when a variable is re-assigned, any expressions using 28e8d8bef9SDimitry Andric /// that variable get invalidated. LiveDebugValues can map onto "available 29e8d8bef9SDimitry Andric /// expressions" by having every register represented by a variable, which is 30e8d8bef9SDimitry Andric /// used in an expression that becomes available at a DBG_VALUE instruction. 31e8d8bef9SDimitry Andric /// When the register is clobbered, its variable is effectively reassigned, and 32e8d8bef9SDimitry Andric /// expressions computed from it become unavailable. A similar construct is 33e8d8bef9SDimitry Andric /// needed when a DebugVariable has its location re-specified, to invalidate 34e8d8bef9SDimitry Andric /// all other locations for that DebugVariable. 35e8d8bef9SDimitry Andric /// 36e8d8bef9SDimitry Andric /// Using the dataflow analysis to compute the available expressions, we create 37e8d8bef9SDimitry Andric /// a DBG_VALUE at the beginning of each block where the expression is 38e8d8bef9SDimitry Andric /// live-in. This propagates variable locations into every basic block where 39e8d8bef9SDimitry Andric /// the location can be determined, rather than only having DBG_VALUEs in blocks 40e8d8bef9SDimitry Andric /// where locations are specified due to an assignment or some optimization. 41e8d8bef9SDimitry Andric /// Movements of values between registers and spill slots are annotated with 42e8d8bef9SDimitry Andric /// DBG_VALUEs too to track variable values bewteen locations. All this allows 43e8d8bef9SDimitry Andric /// DbgEntityHistoryCalculator to focus on only the locations within individual 44e8d8bef9SDimitry Andric /// blocks, facilitating testing and improving modularity. 45e8d8bef9SDimitry Andric /// 46e8d8bef9SDimitry Andric /// We follow an optimisic dataflow approach, with this lattice: 47e8d8bef9SDimitry Andric /// 48e8d8bef9SDimitry Andric /// \verbatim 49e8d8bef9SDimitry Andric /// ┬ "Unknown" 50e8d8bef9SDimitry Andric /// | 51e8d8bef9SDimitry Andric /// v 52e8d8bef9SDimitry Andric /// True 53e8d8bef9SDimitry Andric /// | 54e8d8bef9SDimitry Andric /// v 55e8d8bef9SDimitry Andric /// ⊥ False 56e8d8bef9SDimitry Andric /// \endverbatim With "True" signifying that the expression is available (and 57e8d8bef9SDimitry Andric /// thus a DebugVariable's location is the corresponding register), while 58e8d8bef9SDimitry Andric /// "False" signifies that the expression is unavailable. "Unknown"s never 59e8d8bef9SDimitry Andric /// survive to the end of the analysis (see below). 60e8d8bef9SDimitry Andric /// 61e8d8bef9SDimitry Andric /// Formally, all DebugVariable locations that are live-out of a block are 62e8d8bef9SDimitry Andric /// initialized to \top. A blocks live-in values take the meet of the lattice 63e8d8bef9SDimitry Andric /// value for every predecessors live-outs, except for the entry block, where 64e8d8bef9SDimitry Andric /// all live-ins are \bot. The usual dataflow propagation occurs: the transfer 65e8d8bef9SDimitry Andric /// function for a block assigns an expression for a DebugVariable to be "True" 66e8d8bef9SDimitry Andric /// if a DBG_VALUE in the block specifies it; "False" if the location is 67e8d8bef9SDimitry Andric /// clobbered; or the live-in value if it is unaffected by the block. We 68e8d8bef9SDimitry Andric /// visit each block in reverse post order until a fixedpoint is reached. The 69e8d8bef9SDimitry Andric /// solution produced is maximal. 70e8d8bef9SDimitry Andric /// 71e8d8bef9SDimitry Andric /// Intuitively, we start by assuming that every expression / variable location 72e8d8bef9SDimitry Andric /// is at least "True", and then propagate "False" from the entry block and any 73e8d8bef9SDimitry Andric /// clobbers until there are no more changes to make. This gives us an accurate 74e8d8bef9SDimitry Andric /// solution because all incorrect locations will have a "False" propagated into 75e8d8bef9SDimitry Andric /// them. It also gives us a solution that copes well with loops by assuming 76e8d8bef9SDimitry Andric /// that variable locations are live-through every loop, and then removing those 77e8d8bef9SDimitry Andric /// that are not through dataflow. 78e8d8bef9SDimitry Andric /// 79e8d8bef9SDimitry Andric /// Within LiveDebugValues: each variable location is represented by a 80fe6060f1SDimitry Andric /// VarLoc object that identifies the source variable, the set of 81fe6060f1SDimitry Andric /// machine-locations that currently describe it (a single location for 82fe6060f1SDimitry Andric /// DBG_VALUE or multiple for DBG_VALUE_LIST), and the DBG_VALUE inst that 83fe6060f1SDimitry Andric /// specifies the location. Each VarLoc is indexed in the (function-scope) \p 84fe6060f1SDimitry Andric /// VarLocMap, giving each VarLoc a set of unique indexes, each of which 85fe6060f1SDimitry Andric /// corresponds to one of the VarLoc's machine-locations and can be used to 86fe6060f1SDimitry Andric /// lookup the VarLoc in the VarLocMap. Rather than operate directly on machine 87fe6060f1SDimitry Andric /// locations, the dataflow analysis in this pass identifies locations by their 88fe6060f1SDimitry Andric /// indices in the VarLocMap, meaning all the variable locations in a block can 89*0fca6ea1SDimitry Andric /// be described by a sparse vector of VarLocMap indices. 90e8d8bef9SDimitry Andric /// 91e8d8bef9SDimitry Andric /// All the storage for the dataflow analysis is local to the ExtendRanges 92e8d8bef9SDimitry Andric /// method and passed down to helper methods. "OutLocs" and "InLocs" record the 93e8d8bef9SDimitry Andric /// in and out lattice values for each block. "OpenRanges" maintains a list of 94e8d8bef9SDimitry Andric /// variable locations and, with the "process" method, evaluates the transfer 95fe6060f1SDimitry Andric /// function of each block. "flushPendingLocs" installs debug value instructions 96fe6060f1SDimitry Andric /// for each live-in location at the start of blocks, while "Transfers" records 97e8d8bef9SDimitry Andric /// transfers of values between machine-locations. 98e8d8bef9SDimitry Andric /// 99e8d8bef9SDimitry Andric /// We avoid explicitly representing the "Unknown" (\top) lattice value in the 100e8d8bef9SDimitry Andric /// implementation. Instead, unvisited blocks implicitly have all lattice 101e8d8bef9SDimitry Andric /// values set as "Unknown". After being visited, there will be path back to 102e8d8bef9SDimitry Andric /// the entry block where the lattice value is "False", and as the transfer 103e8d8bef9SDimitry Andric /// function cannot make new "Unknown" locations, there are no scenarios where 104e8d8bef9SDimitry Andric /// a block can have an "Unknown" location after being visited. Similarly, we 105e8d8bef9SDimitry Andric /// don't enumerate all possible variable locations before exploring the 106e8d8bef9SDimitry Andric /// function: when a new location is discovered, all blocks previously explored 107e8d8bef9SDimitry Andric /// were implicitly "False" but unrecorded, and become explicitly "False" when 108e8d8bef9SDimitry Andric /// a new VarLoc is created with its bit not set in predecessor InLocs or 109e8d8bef9SDimitry Andric /// OutLocs. 110e8d8bef9SDimitry Andric /// 111e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 112e8d8bef9SDimitry Andric 113e8d8bef9SDimitry Andric #include "LiveDebugValues.h" 114e8d8bef9SDimitry Andric 115e8d8bef9SDimitry Andric #include "llvm/ADT/CoalescingBitVector.h" 116e8d8bef9SDimitry Andric #include "llvm/ADT/DenseMap.h" 117e8d8bef9SDimitry Andric #include "llvm/ADT/PostOrderIterator.h" 118e8d8bef9SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 119e8d8bef9SDimitry Andric #include "llvm/ADT/SmallSet.h" 120e8d8bef9SDimitry Andric #include "llvm/ADT/SmallVector.h" 121e8d8bef9SDimitry Andric #include "llvm/ADT/Statistic.h" 12281ad6265SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h" 123e8d8bef9SDimitry Andric #include "llvm/CodeGen/LexicalScopes.h" 124e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 125e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 126e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 127e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 128e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineMemOperand.h" 129e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineOperand.h" 130e8d8bef9SDimitry Andric #include "llvm/CodeGen/PseudoSourceValue.h" 131e8d8bef9SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h" 132e8d8bef9SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h" 133e8d8bef9SDimitry Andric #include "llvm/CodeGen/TargetLowering.h" 134e8d8bef9SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h" 135e8d8bef9SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 136e8d8bef9SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 137e8d8bef9SDimitry Andric #include "llvm/Config/llvm-config.h" 138e8d8bef9SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h" 139e8d8bef9SDimitry Andric #include "llvm/IR/DebugLoc.h" 140e8d8bef9SDimitry Andric #include "llvm/IR/Function.h" 141e8d8bef9SDimitry Andric #include "llvm/MC/MCRegisterInfo.h" 142e8d8bef9SDimitry Andric #include "llvm/Support/Casting.h" 143e8d8bef9SDimitry Andric #include "llvm/Support/Debug.h" 144e8d8bef9SDimitry Andric #include "llvm/Support/TypeSize.h" 145e8d8bef9SDimitry Andric #include "llvm/Support/raw_ostream.h" 146e8d8bef9SDimitry Andric #include "llvm/Target/TargetMachine.h" 147e8d8bef9SDimitry Andric #include <algorithm> 148e8d8bef9SDimitry Andric #include <cassert> 149e8d8bef9SDimitry Andric #include <cstdint> 150e8d8bef9SDimitry Andric #include <functional> 151349cc55cSDimitry Andric #include <map> 152bdd1243dSDimitry Andric #include <optional> 153e8d8bef9SDimitry Andric #include <queue> 154e8d8bef9SDimitry Andric #include <tuple> 155e8d8bef9SDimitry Andric #include <utility> 156e8d8bef9SDimitry Andric #include <vector> 157e8d8bef9SDimitry Andric 158e8d8bef9SDimitry Andric using namespace llvm; 159e8d8bef9SDimitry Andric 160e8d8bef9SDimitry Andric #define DEBUG_TYPE "livedebugvalues" 161e8d8bef9SDimitry Andric 162e8d8bef9SDimitry Andric STATISTIC(NumInserted, "Number of DBG_VALUE instructions inserted"); 163e8d8bef9SDimitry Andric 164e8d8bef9SDimitry Andric /// If \p Op is a stack or frame register return true, otherwise return false. 165e8d8bef9SDimitry Andric /// This is used to avoid basing the debug entry values on the registers, since 166e8d8bef9SDimitry Andric /// we do not support it at the moment. 167e8d8bef9SDimitry Andric static bool isRegOtherThanSPAndFP(const MachineOperand &Op, 168e8d8bef9SDimitry Andric const MachineInstr &MI, 169e8d8bef9SDimitry Andric const TargetRegisterInfo *TRI) { 170e8d8bef9SDimitry Andric if (!Op.isReg()) 171e8d8bef9SDimitry Andric return false; 172e8d8bef9SDimitry Andric 173e8d8bef9SDimitry Andric const MachineFunction *MF = MI.getParent()->getParent(); 174e8d8bef9SDimitry Andric const TargetLowering *TLI = MF->getSubtarget().getTargetLowering(); 175e8d8bef9SDimitry Andric Register SP = TLI->getStackPointerRegisterToSaveRestore(); 176e8d8bef9SDimitry Andric Register FP = TRI->getFrameRegister(*MF); 177e8d8bef9SDimitry Andric Register Reg = Op.getReg(); 178e8d8bef9SDimitry Andric 179e8d8bef9SDimitry Andric return Reg && Reg != SP && Reg != FP; 180e8d8bef9SDimitry Andric } 181e8d8bef9SDimitry Andric 182e8d8bef9SDimitry Andric namespace { 183e8d8bef9SDimitry Andric 184e8d8bef9SDimitry Andric // Max out the number of statically allocated elements in DefinedRegsSet, as 185e8d8bef9SDimitry Andric // this prevents fallback to std::set::count() operations. 186e8d8bef9SDimitry Andric using DefinedRegsSet = SmallSet<Register, 32>; 187e8d8bef9SDimitry Andric 188fe6060f1SDimitry Andric // The IDs in this set correspond to MachineLocs in VarLocs, as well as VarLocs 189fe6060f1SDimitry Andric // that represent Entry Values; every VarLoc in the set will also appear 190fe6060f1SDimitry Andric // exactly once at Location=0. 191fe6060f1SDimitry Andric // As a result, each VarLoc may appear more than once in this "set", but each 192fe6060f1SDimitry Andric // range corresponding to a Reg, SpillLoc, or EntryValue type will still be a 193fe6060f1SDimitry Andric // "true" set (i.e. each VarLoc may appear only once), and the range Location=0 194fe6060f1SDimitry Andric // is the set of all VarLocs. 195e8d8bef9SDimitry Andric using VarLocSet = CoalescingBitVector<uint64_t>; 196e8d8bef9SDimitry Andric 197e8d8bef9SDimitry Andric /// A type-checked pair of {Register Location (or 0), Index}, used to index 198e8d8bef9SDimitry Andric /// into a \ref VarLocMap. This can be efficiently converted to a 64-bit int 199e8d8bef9SDimitry Andric /// for insertion into a \ref VarLocSet, and efficiently converted back. The 200e8d8bef9SDimitry Andric /// type-checker helps ensure that the conversions aren't lossy. 201e8d8bef9SDimitry Andric /// 202e8d8bef9SDimitry Andric /// Why encode a location /into/ the VarLocMap index? This makes it possible 203e8d8bef9SDimitry Andric /// to find the open VarLocs killed by a register def very quickly. This is a 204e8d8bef9SDimitry Andric /// performance-critical operation for LiveDebugValues. 205e8d8bef9SDimitry Andric struct LocIndex { 206e8d8bef9SDimitry Andric using u32_location_t = uint32_t; 207e8d8bef9SDimitry Andric using u32_index_t = uint32_t; 208e8d8bef9SDimitry Andric 209e8d8bef9SDimitry Andric u32_location_t Location; // Physical registers live in the range [1;2^30) (see 210e8d8bef9SDimitry Andric // \ref MCRegister), so we have plenty of range left 211e8d8bef9SDimitry Andric // here to encode non-register locations. 212e8d8bef9SDimitry Andric u32_index_t Index; 213e8d8bef9SDimitry Andric 214fe6060f1SDimitry Andric /// The location that has an entry for every VarLoc in the map. 215fe6060f1SDimitry Andric static constexpr u32_location_t kUniversalLocation = 0; 216fe6060f1SDimitry Andric 217fe6060f1SDimitry Andric /// The first location that is reserved for VarLocs with locations of kind 218fe6060f1SDimitry Andric /// RegisterKind. 219fe6060f1SDimitry Andric static constexpr u32_location_t kFirstRegLocation = 1; 220fe6060f1SDimitry Andric 221fe6060f1SDimitry Andric /// The first location greater than 0 that is not reserved for VarLocs with 222fe6060f1SDimitry Andric /// locations of kind RegisterKind. 223e8d8bef9SDimitry Andric static constexpr u32_location_t kFirstInvalidRegLocation = 1 << 30; 224e8d8bef9SDimitry Andric 225fe6060f1SDimitry Andric /// A special location reserved for VarLocs with locations of kind 226fe6060f1SDimitry Andric /// SpillLocKind. 227e8d8bef9SDimitry Andric static constexpr u32_location_t kSpillLocation = kFirstInvalidRegLocation; 228e8d8bef9SDimitry Andric 229e8d8bef9SDimitry Andric /// A special location reserved for VarLocs of kind EntryValueBackupKind and 230e8d8bef9SDimitry Andric /// EntryValueCopyBackupKind. 231e8d8bef9SDimitry Andric static constexpr u32_location_t kEntryValueBackupLocation = 232e8d8bef9SDimitry Andric kFirstInvalidRegLocation + 1; 233e8d8bef9SDimitry Andric 234bdd1243dSDimitry Andric /// A special location reserved for VarLocs with locations of kind 235bdd1243dSDimitry Andric /// WasmLocKind. 236bdd1243dSDimitry Andric /// TODO Placing all Wasm target index locations in this single kWasmLocation 237bdd1243dSDimitry Andric /// may cause slowdown in compilation time in very large functions. Consider 238bdd1243dSDimitry Andric /// giving a each target index/offset pair its own u32_location_t if this 239bdd1243dSDimitry Andric /// becomes a problem. 240bdd1243dSDimitry Andric static constexpr u32_location_t kWasmLocation = kFirstInvalidRegLocation + 2; 241bdd1243dSDimitry Andric 242e8d8bef9SDimitry Andric LocIndex(u32_location_t Location, u32_index_t Index) 243e8d8bef9SDimitry Andric : Location(Location), Index(Index) {} 244e8d8bef9SDimitry Andric 245e8d8bef9SDimitry Andric uint64_t getAsRawInteger() const { 246e8d8bef9SDimitry Andric return (static_cast<uint64_t>(Location) << 32) | Index; 247e8d8bef9SDimitry Andric } 248e8d8bef9SDimitry Andric 249e8d8bef9SDimitry Andric template<typename IntT> static LocIndex fromRawInteger(IntT ID) { 250bdd1243dSDimitry Andric static_assert(std::is_unsigned_v<IntT> && sizeof(ID) == sizeof(uint64_t), 251e8d8bef9SDimitry Andric "Cannot convert raw integer to LocIndex"); 252e8d8bef9SDimitry Andric return {static_cast<u32_location_t>(ID >> 32), 253e8d8bef9SDimitry Andric static_cast<u32_index_t>(ID)}; 254e8d8bef9SDimitry Andric } 255e8d8bef9SDimitry Andric 256e8d8bef9SDimitry Andric /// Get the start of the interval reserved for VarLocs of kind RegisterKind 257e8d8bef9SDimitry Andric /// which reside in \p Reg. The end is at rawIndexForReg(Reg+1)-1. 258fe6060f1SDimitry Andric static uint64_t rawIndexForReg(Register Reg) { 259e8d8bef9SDimitry Andric return LocIndex(Reg, 0).getAsRawInteger(); 260e8d8bef9SDimitry Andric } 261e8d8bef9SDimitry Andric 262e8d8bef9SDimitry Andric /// Return a range covering all set indices in the interval reserved for 263e8d8bef9SDimitry Andric /// \p Location in \p Set. 264e8d8bef9SDimitry Andric static auto indexRangeForLocation(const VarLocSet &Set, 265e8d8bef9SDimitry Andric u32_location_t Location) { 266e8d8bef9SDimitry Andric uint64_t Start = LocIndex(Location, 0).getAsRawInteger(); 267e8d8bef9SDimitry Andric uint64_t End = LocIndex(Location + 1, 0).getAsRawInteger(); 268e8d8bef9SDimitry Andric return Set.half_open_range(Start, End); 269e8d8bef9SDimitry Andric } 270e8d8bef9SDimitry Andric }; 271e8d8bef9SDimitry Andric 272fe6060f1SDimitry Andric // Simple Set for storing all the VarLoc Indices at a Location bucket. 273fe6060f1SDimitry Andric using VarLocsInRange = SmallSet<LocIndex::u32_index_t, 32>; 274fe6060f1SDimitry Andric // Vector of all `LocIndex`s for a given VarLoc; the same Location should not 275fe6060f1SDimitry Andric // appear in any two of these, as each VarLoc appears at most once in any 276fe6060f1SDimitry Andric // Location bucket. 277fe6060f1SDimitry Andric using LocIndices = SmallVector<LocIndex, 2>; 278fe6060f1SDimitry Andric 279e8d8bef9SDimitry Andric class VarLocBasedLDV : public LDVImpl { 280e8d8bef9SDimitry Andric private: 281e8d8bef9SDimitry Andric const TargetRegisterInfo *TRI; 282e8d8bef9SDimitry Andric const TargetInstrInfo *TII; 283e8d8bef9SDimitry Andric const TargetFrameLowering *TFI; 284e8d8bef9SDimitry Andric TargetPassConfig *TPC; 285e8d8bef9SDimitry Andric BitVector CalleeSavedRegs; 286e8d8bef9SDimitry Andric LexicalScopes LS; 287e8d8bef9SDimitry Andric VarLocSet::Allocator Alloc; 288e8d8bef9SDimitry Andric 289349cc55cSDimitry Andric const MachineInstr *LastNonDbgMI; 290349cc55cSDimitry Andric 291e8d8bef9SDimitry Andric enum struct TransferKind { TransferCopy, TransferSpill, TransferRestore }; 292e8d8bef9SDimitry Andric 293e8d8bef9SDimitry Andric using FragmentInfo = DIExpression::FragmentInfo; 294bdd1243dSDimitry Andric using OptFragmentInfo = std::optional<DIExpression::FragmentInfo>; 295e8d8bef9SDimitry Andric 296e8d8bef9SDimitry Andric /// A pair of debug variable and value location. 297e8d8bef9SDimitry Andric struct VarLoc { 298e8d8bef9SDimitry Andric // The location at which a spilled variable resides. It consists of a 299e8d8bef9SDimitry Andric // register and an offset. 300e8d8bef9SDimitry Andric struct SpillLoc { 301e8d8bef9SDimitry Andric unsigned SpillBase; 302e8d8bef9SDimitry Andric StackOffset SpillOffset; 303e8d8bef9SDimitry Andric bool operator==(const SpillLoc &Other) const { 304e8d8bef9SDimitry Andric return SpillBase == Other.SpillBase && SpillOffset == Other.SpillOffset; 305e8d8bef9SDimitry Andric } 306e8d8bef9SDimitry Andric bool operator!=(const SpillLoc &Other) const { 307e8d8bef9SDimitry Andric return !(*this == Other); 308e8d8bef9SDimitry Andric } 309e8d8bef9SDimitry Andric }; 310e8d8bef9SDimitry Andric 311bdd1243dSDimitry Andric // Target indices used for wasm-specific locations. 312bdd1243dSDimitry Andric struct WasmLoc { 313bdd1243dSDimitry Andric // One of TargetIndex values defined in WebAssembly.h. We deal with 314bdd1243dSDimitry Andric // local-related TargetIndex in this analysis (TI_LOCAL and 315bdd1243dSDimitry Andric // TI_LOCAL_INDIRECT). Stack operands (TI_OPERAND_STACK) will be handled 316bdd1243dSDimitry Andric // separately WebAssemblyDebugFixup pass, and we don't associate debug 317bdd1243dSDimitry Andric // info with values in global operands (TI_GLOBAL_RELOC) at the moment. 318bdd1243dSDimitry Andric int Index; 319bdd1243dSDimitry Andric int64_t Offset; 320bdd1243dSDimitry Andric bool operator==(const WasmLoc &Other) const { 321bdd1243dSDimitry Andric return Index == Other.Index && Offset == Other.Offset; 322bdd1243dSDimitry Andric } 323bdd1243dSDimitry Andric bool operator!=(const WasmLoc &Other) const { return !(*this == Other); } 324bdd1243dSDimitry Andric }; 325bdd1243dSDimitry Andric 326e8d8bef9SDimitry Andric /// Identity of the variable at this location. 327e8d8bef9SDimitry Andric const DebugVariable Var; 328e8d8bef9SDimitry Andric 329e8d8bef9SDimitry Andric /// The expression applied to this location. 330e8d8bef9SDimitry Andric const DIExpression *Expr; 331e8d8bef9SDimitry Andric 332e8d8bef9SDimitry Andric /// DBG_VALUE to clone var/expr information from if this location 333e8d8bef9SDimitry Andric /// is moved. 334e8d8bef9SDimitry Andric const MachineInstr &MI; 335e8d8bef9SDimitry Andric 336fe6060f1SDimitry Andric enum class MachineLocKind { 337e8d8bef9SDimitry Andric InvalidKind = 0, 338e8d8bef9SDimitry Andric RegisterKind, 339e8d8bef9SDimitry Andric SpillLocKind, 340bdd1243dSDimitry Andric ImmediateKind, 341bdd1243dSDimitry Andric WasmLocKind 342fe6060f1SDimitry Andric }; 343fe6060f1SDimitry Andric 344fe6060f1SDimitry Andric enum class EntryValueLocKind { 345fe6060f1SDimitry Andric NonEntryValueKind = 0, 346e8d8bef9SDimitry Andric EntryValueKind, 347e8d8bef9SDimitry Andric EntryValueBackupKind, 348e8d8bef9SDimitry Andric EntryValueCopyBackupKind 3491fd87a68SDimitry Andric } EVKind = EntryValueLocKind::NonEntryValueKind; 350e8d8bef9SDimitry Andric 351e8d8bef9SDimitry Andric /// The value location. Stored separately to avoid repeatedly 352e8d8bef9SDimitry Andric /// extracting it from MI. 353fe6060f1SDimitry Andric union MachineLocValue { 354e8d8bef9SDimitry Andric uint64_t RegNo; 355e8d8bef9SDimitry Andric SpillLoc SpillLocation; 356e8d8bef9SDimitry Andric uint64_t Hash; 357e8d8bef9SDimitry Andric int64_t Immediate; 358e8d8bef9SDimitry Andric const ConstantFP *FPImm; 359e8d8bef9SDimitry Andric const ConstantInt *CImm; 360bdd1243dSDimitry Andric WasmLoc WasmLocation; 361fe6060f1SDimitry Andric MachineLocValue() : Hash(0) {} 362fe6060f1SDimitry Andric }; 363fe6060f1SDimitry Andric 364fe6060f1SDimitry Andric /// A single machine location; its Kind is either a register, spill 365fe6060f1SDimitry Andric /// location, or immediate value. 366fe6060f1SDimitry Andric /// If the VarLoc is not a NonEntryValueKind, then it will use only a 367fe6060f1SDimitry Andric /// single MachineLoc of RegisterKind. 368fe6060f1SDimitry Andric struct MachineLoc { 369fe6060f1SDimitry Andric MachineLocKind Kind; 370fe6060f1SDimitry Andric MachineLocValue Value; 371fe6060f1SDimitry Andric bool operator==(const MachineLoc &Other) const { 372fe6060f1SDimitry Andric if (Kind != Other.Kind) 373fe6060f1SDimitry Andric return false; 374fe6060f1SDimitry Andric switch (Kind) { 375fe6060f1SDimitry Andric case MachineLocKind::SpillLocKind: 376fe6060f1SDimitry Andric return Value.SpillLocation == Other.Value.SpillLocation; 377bdd1243dSDimitry Andric case MachineLocKind::WasmLocKind: 378bdd1243dSDimitry Andric return Value.WasmLocation == Other.Value.WasmLocation; 379fe6060f1SDimitry Andric case MachineLocKind::RegisterKind: 380fe6060f1SDimitry Andric case MachineLocKind::ImmediateKind: 381fe6060f1SDimitry Andric return Value.Hash == Other.Value.Hash; 382fe6060f1SDimitry Andric default: 383fe6060f1SDimitry Andric llvm_unreachable("Invalid kind"); 384fe6060f1SDimitry Andric } 385fe6060f1SDimitry Andric } 386fe6060f1SDimitry Andric bool operator<(const MachineLoc &Other) const { 387fe6060f1SDimitry Andric switch (Kind) { 388fe6060f1SDimitry Andric case MachineLocKind::SpillLocKind: 389fe6060f1SDimitry Andric return std::make_tuple( 390fe6060f1SDimitry Andric Kind, Value.SpillLocation.SpillBase, 391fe6060f1SDimitry Andric Value.SpillLocation.SpillOffset.getFixed(), 392fe6060f1SDimitry Andric Value.SpillLocation.SpillOffset.getScalable()) < 393fe6060f1SDimitry Andric std::make_tuple( 394fe6060f1SDimitry Andric Other.Kind, Other.Value.SpillLocation.SpillBase, 395fe6060f1SDimitry Andric Other.Value.SpillLocation.SpillOffset.getFixed(), 396fe6060f1SDimitry Andric Other.Value.SpillLocation.SpillOffset.getScalable()); 397bdd1243dSDimitry Andric case MachineLocKind::WasmLocKind: 398bdd1243dSDimitry Andric return std::make_tuple(Kind, Value.WasmLocation.Index, 399bdd1243dSDimitry Andric Value.WasmLocation.Offset) < 400bdd1243dSDimitry Andric std::make_tuple(Other.Kind, Other.Value.WasmLocation.Index, 401bdd1243dSDimitry Andric Other.Value.WasmLocation.Offset); 402fe6060f1SDimitry Andric case MachineLocKind::RegisterKind: 403fe6060f1SDimitry Andric case MachineLocKind::ImmediateKind: 404fe6060f1SDimitry Andric return std::tie(Kind, Value.Hash) < 405fe6060f1SDimitry Andric std::tie(Other.Kind, Other.Value.Hash); 406fe6060f1SDimitry Andric default: 407fe6060f1SDimitry Andric llvm_unreachable("Invalid kind"); 408fe6060f1SDimitry Andric } 409fe6060f1SDimitry Andric } 410fe6060f1SDimitry Andric }; 411fe6060f1SDimitry Andric 412fe6060f1SDimitry Andric /// The set of machine locations used to determine the variable's value, in 413fe6060f1SDimitry Andric /// conjunction with Expr. Initially populated with MI's debug operands, 414fe6060f1SDimitry Andric /// but may be transformed independently afterwards. 415fe6060f1SDimitry Andric SmallVector<MachineLoc, 8> Locs; 416fe6060f1SDimitry Andric /// Used to map the index of each location in Locs back to the index of its 417fe6060f1SDimitry Andric /// original debug operand in MI. Used when multiple location operands are 418fe6060f1SDimitry Andric /// coalesced and the original MI's operands need to be accessed while 419fe6060f1SDimitry Andric /// emitting a debug value. 420fe6060f1SDimitry Andric SmallVector<unsigned, 8> OrigLocMap; 421e8d8bef9SDimitry Andric 422bdd1243dSDimitry Andric VarLoc(const MachineInstr &MI) 423e8d8bef9SDimitry Andric : Var(MI.getDebugVariable(), MI.getDebugExpression(), 424e8d8bef9SDimitry Andric MI.getDebugLoc()->getInlinedAt()), 4251fd87a68SDimitry Andric Expr(MI.getDebugExpression()), MI(MI) { 426e8d8bef9SDimitry Andric assert(MI.isDebugValue() && "not a DBG_VALUE"); 427fe6060f1SDimitry Andric assert((MI.isDebugValueList() || MI.getNumOperands() == 4) && 428fe6060f1SDimitry Andric "malformed DBG_VALUE"); 429fe6060f1SDimitry Andric for (const MachineOperand &Op : MI.debug_operands()) { 430fe6060f1SDimitry Andric MachineLoc ML = GetLocForOp(Op); 431fe6060f1SDimitry Andric auto It = find(Locs, ML); 432fe6060f1SDimitry Andric if (It == Locs.end()) { 433fe6060f1SDimitry Andric Locs.push_back(ML); 434fe6060f1SDimitry Andric OrigLocMap.push_back(MI.getDebugOperandIndex(&Op)); 435fe6060f1SDimitry Andric } else { 436fe6060f1SDimitry Andric // ML duplicates an element in Locs; replace references to Op 437fe6060f1SDimitry Andric // with references to the duplicating element. 438fe6060f1SDimitry Andric unsigned OpIdx = Locs.size(); 439fe6060f1SDimitry Andric unsigned DuplicatingIdx = std::distance(Locs.begin(), It); 440fe6060f1SDimitry Andric Expr = DIExpression::replaceArg(Expr, OpIdx, DuplicatingIdx); 441fe6060f1SDimitry Andric } 442e8d8bef9SDimitry Andric } 443e8d8bef9SDimitry Andric 444fe6060f1SDimitry Andric // We create the debug entry values from the factory functions rather 445fe6060f1SDimitry Andric // than from this ctor. 446fe6060f1SDimitry Andric assert(EVKind != EntryValueLocKind::EntryValueKind && 447fe6060f1SDimitry Andric !isEntryBackupLoc()); 448fe6060f1SDimitry Andric } 449fe6060f1SDimitry Andric 450fe6060f1SDimitry Andric static MachineLoc GetLocForOp(const MachineOperand &Op) { 451fe6060f1SDimitry Andric MachineLocKind Kind; 452fe6060f1SDimitry Andric MachineLocValue Loc; 453fe6060f1SDimitry Andric if (Op.isReg()) { 454fe6060f1SDimitry Andric Kind = MachineLocKind::RegisterKind; 455fe6060f1SDimitry Andric Loc.RegNo = Op.getReg(); 456fe6060f1SDimitry Andric } else if (Op.isImm()) { 457fe6060f1SDimitry Andric Kind = MachineLocKind::ImmediateKind; 458fe6060f1SDimitry Andric Loc.Immediate = Op.getImm(); 459fe6060f1SDimitry Andric } else if (Op.isFPImm()) { 460fe6060f1SDimitry Andric Kind = MachineLocKind::ImmediateKind; 461fe6060f1SDimitry Andric Loc.FPImm = Op.getFPImm(); 462fe6060f1SDimitry Andric } else if (Op.isCImm()) { 463fe6060f1SDimitry Andric Kind = MachineLocKind::ImmediateKind; 464fe6060f1SDimitry Andric Loc.CImm = Op.getCImm(); 465bdd1243dSDimitry Andric } else if (Op.isTargetIndex()) { 466bdd1243dSDimitry Andric Kind = MachineLocKind::WasmLocKind; 467bdd1243dSDimitry Andric Loc.WasmLocation = {Op.getIndex(), Op.getOffset()}; 468fe6060f1SDimitry Andric } else 469fe6060f1SDimitry Andric llvm_unreachable("Invalid Op kind for MachineLoc."); 470fe6060f1SDimitry Andric return {Kind, Loc}; 471e8d8bef9SDimitry Andric } 472e8d8bef9SDimitry Andric 473e8d8bef9SDimitry Andric /// Take the variable and machine-location in DBG_VALUE MI, and build an 474e8d8bef9SDimitry Andric /// entry location using the given expression. 475bdd1243dSDimitry Andric static VarLoc CreateEntryLoc(const MachineInstr &MI, 476e8d8bef9SDimitry Andric const DIExpression *EntryExpr, Register Reg) { 477bdd1243dSDimitry Andric VarLoc VL(MI); 478fe6060f1SDimitry Andric assert(VL.Locs.size() == 1 && 479fe6060f1SDimitry Andric VL.Locs[0].Kind == MachineLocKind::RegisterKind); 480fe6060f1SDimitry Andric VL.EVKind = EntryValueLocKind::EntryValueKind; 481e8d8bef9SDimitry Andric VL.Expr = EntryExpr; 482fe6060f1SDimitry Andric VL.Locs[0].Value.RegNo = Reg; 483e8d8bef9SDimitry Andric return VL; 484e8d8bef9SDimitry Andric } 485e8d8bef9SDimitry Andric 486e8d8bef9SDimitry Andric /// Take the variable and machine-location from the DBG_VALUE (from the 487e8d8bef9SDimitry Andric /// function entry), and build an entry value backup location. The backup 488e8d8bef9SDimitry Andric /// location will turn into the normal location if the backup is valid at 489e8d8bef9SDimitry Andric /// the time of the primary location clobbering. 490e8d8bef9SDimitry Andric static VarLoc CreateEntryBackupLoc(const MachineInstr &MI, 491e8d8bef9SDimitry Andric const DIExpression *EntryExpr) { 492bdd1243dSDimitry Andric VarLoc VL(MI); 493fe6060f1SDimitry Andric assert(VL.Locs.size() == 1 && 494fe6060f1SDimitry Andric VL.Locs[0].Kind == MachineLocKind::RegisterKind); 495fe6060f1SDimitry Andric VL.EVKind = EntryValueLocKind::EntryValueBackupKind; 496e8d8bef9SDimitry Andric VL.Expr = EntryExpr; 497e8d8bef9SDimitry Andric return VL; 498e8d8bef9SDimitry Andric } 499e8d8bef9SDimitry Andric 500e8d8bef9SDimitry Andric /// Take the variable and machine-location from the DBG_VALUE (from the 501e8d8bef9SDimitry Andric /// function entry), and build a copy of an entry value backup location by 502e8d8bef9SDimitry Andric /// setting the register location to NewReg. 503e8d8bef9SDimitry Andric static VarLoc CreateEntryCopyBackupLoc(const MachineInstr &MI, 504e8d8bef9SDimitry Andric const DIExpression *EntryExpr, 505e8d8bef9SDimitry Andric Register NewReg) { 506bdd1243dSDimitry Andric VarLoc VL(MI); 507fe6060f1SDimitry Andric assert(VL.Locs.size() == 1 && 508fe6060f1SDimitry Andric VL.Locs[0].Kind == MachineLocKind::RegisterKind); 509fe6060f1SDimitry Andric VL.EVKind = EntryValueLocKind::EntryValueCopyBackupKind; 510e8d8bef9SDimitry Andric VL.Expr = EntryExpr; 511fe6060f1SDimitry Andric VL.Locs[0].Value.RegNo = NewReg; 512e8d8bef9SDimitry Andric return VL; 513e8d8bef9SDimitry Andric } 514e8d8bef9SDimitry Andric 515e8d8bef9SDimitry Andric /// Copy the register location in DBG_VALUE MI, updating the register to 516e8d8bef9SDimitry Andric /// be NewReg. 517fe6060f1SDimitry Andric static VarLoc CreateCopyLoc(const VarLoc &OldVL, const MachineLoc &OldML, 518e8d8bef9SDimitry Andric Register NewReg) { 519fe6060f1SDimitry Andric VarLoc VL = OldVL; 5200eae32dcSDimitry Andric for (MachineLoc &ML : VL.Locs) 5210eae32dcSDimitry Andric if (ML == OldML) { 5220eae32dcSDimitry Andric ML.Kind = MachineLocKind::RegisterKind; 5230eae32dcSDimitry Andric ML.Value.RegNo = NewReg; 524e8d8bef9SDimitry Andric return VL; 525e8d8bef9SDimitry Andric } 526fe6060f1SDimitry Andric llvm_unreachable("Should have found OldML in new VarLoc."); 527fe6060f1SDimitry Andric } 528e8d8bef9SDimitry Andric 529fe6060f1SDimitry Andric /// Take the variable described by DBG_VALUE* MI, and create a VarLoc 530e8d8bef9SDimitry Andric /// locating it in the specified spill location. 531fe6060f1SDimitry Andric static VarLoc CreateSpillLoc(const VarLoc &OldVL, const MachineLoc &OldML, 532fe6060f1SDimitry Andric unsigned SpillBase, StackOffset SpillOffset) { 533fe6060f1SDimitry Andric VarLoc VL = OldVL; 5340eae32dcSDimitry Andric for (MachineLoc &ML : VL.Locs) 5350eae32dcSDimitry Andric if (ML == OldML) { 5360eae32dcSDimitry Andric ML.Kind = MachineLocKind::SpillLocKind; 5370eae32dcSDimitry Andric ML.Value.SpillLocation = {SpillBase, SpillOffset}; 538e8d8bef9SDimitry Andric return VL; 539e8d8bef9SDimitry Andric } 540fe6060f1SDimitry Andric llvm_unreachable("Should have found OldML in new VarLoc."); 541fe6060f1SDimitry Andric } 542e8d8bef9SDimitry Andric 543e8d8bef9SDimitry Andric /// Create a DBG_VALUE representing this VarLoc in the given function. 544e8d8bef9SDimitry Andric /// Copies variable-specific information such as DILocalVariable and 545e8d8bef9SDimitry Andric /// inlining information from the original DBG_VALUE instruction, which may 546e8d8bef9SDimitry Andric /// have been several transfers ago. 547e8d8bef9SDimitry Andric MachineInstr *BuildDbgValue(MachineFunction &MF) const { 548fe6060f1SDimitry Andric assert(!isEntryBackupLoc() && 549fe6060f1SDimitry Andric "Tried to produce DBG_VALUE for backup VarLoc"); 550e8d8bef9SDimitry Andric const DebugLoc &DbgLoc = MI.getDebugLoc(); 551e8d8bef9SDimitry Andric bool Indirect = MI.isIndirectDebugValue(); 552e8d8bef9SDimitry Andric const auto &IID = MI.getDesc(); 553e8d8bef9SDimitry Andric const DILocalVariable *Var = MI.getDebugVariable(); 554e8d8bef9SDimitry Andric NumInserted++; 555e8d8bef9SDimitry Andric 556fe6060f1SDimitry Andric const DIExpression *DIExpr = Expr; 557fe6060f1SDimitry Andric SmallVector<MachineOperand, 8> MOs; 558fe6060f1SDimitry Andric for (unsigned I = 0, E = Locs.size(); I < E; ++I) { 559fe6060f1SDimitry Andric MachineLocKind LocKind = Locs[I].Kind; 560fe6060f1SDimitry Andric MachineLocValue Loc = Locs[I].Value; 561fe6060f1SDimitry Andric const MachineOperand &Orig = MI.getDebugOperand(OrigLocMap[I]); 562fe6060f1SDimitry Andric switch (LocKind) { 563fe6060f1SDimitry Andric case MachineLocKind::RegisterKind: 564e8d8bef9SDimitry Andric // An entry value is a register location -- but with an updated 565fe6060f1SDimitry Andric // expression. The register location of such DBG_VALUE is always the 566fe6060f1SDimitry Andric // one from the entry DBG_VALUE, it does not matter if the entry value 567fe6060f1SDimitry Andric // was copied in to another register due to some optimizations. 568fe6060f1SDimitry Andric // Non-entry value register locations are like the source 569fe6060f1SDimitry Andric // DBG_VALUE, but with the register number from this VarLoc. 570fe6060f1SDimitry Andric MOs.push_back(MachineOperand::CreateReg( 571fe6060f1SDimitry Andric EVKind == EntryValueLocKind::EntryValueKind ? Orig.getReg() 572fe6060f1SDimitry Andric : Register(Loc.RegNo), 573fe6060f1SDimitry Andric false)); 574fe6060f1SDimitry Andric break; 575fe6060f1SDimitry Andric case MachineLocKind::SpillLocKind: { 576e8d8bef9SDimitry Andric // Spills are indirect DBG_VALUEs, with a base register and offset. 577e8d8bef9SDimitry Andric // Use the original DBG_VALUEs expression to build the spilt location 578e8d8bef9SDimitry Andric // on top of. FIXME: spill locations created before this pass runs 579e8d8bef9SDimitry Andric // are not recognized, and not handled here. 580e8d8bef9SDimitry Andric unsigned Base = Loc.SpillLocation.SpillBase; 581fe6060f1SDimitry Andric auto *TRI = MF.getSubtarget().getRegisterInfo(); 582fe6060f1SDimitry Andric if (MI.isNonListDebugValue()) { 583349cc55cSDimitry Andric auto Deref = Indirect ? DIExpression::DerefAfter : 0; 584349cc55cSDimitry Andric DIExpr = TRI->prependOffsetExpression( 585349cc55cSDimitry Andric DIExpr, DIExpression::ApplyOffset | Deref, 586fe6060f1SDimitry Andric Loc.SpillLocation.SpillOffset); 587fe6060f1SDimitry Andric Indirect = true; 588fe6060f1SDimitry Andric } else { 589fe6060f1SDimitry Andric SmallVector<uint64_t, 4> Ops; 590fe6060f1SDimitry Andric TRI->getOffsetOpcodes(Loc.SpillLocation.SpillOffset, Ops); 591fe6060f1SDimitry Andric Ops.push_back(dwarf::DW_OP_deref); 592fe6060f1SDimitry Andric DIExpr = DIExpression::appendOpsToArg(DIExpr, Ops, I); 593e8d8bef9SDimitry Andric } 594fe6060f1SDimitry Andric MOs.push_back(MachineOperand::CreateReg(Base, false)); 595fe6060f1SDimitry Andric break; 596e8d8bef9SDimitry Andric } 597fe6060f1SDimitry Andric case MachineLocKind::ImmediateKind: { 598fe6060f1SDimitry Andric MOs.push_back(Orig); 599fe6060f1SDimitry Andric break; 600e8d8bef9SDimitry Andric } 601bdd1243dSDimitry Andric case MachineLocKind::WasmLocKind: { 602bdd1243dSDimitry Andric MOs.push_back(Orig); 603bdd1243dSDimitry Andric break; 604bdd1243dSDimitry Andric } 605fe6060f1SDimitry Andric case MachineLocKind::InvalidKind: 606fe6060f1SDimitry Andric llvm_unreachable("Tried to produce DBG_VALUE for invalid VarLoc"); 607fe6060f1SDimitry Andric } 608fe6060f1SDimitry Andric } 609fe6060f1SDimitry Andric return BuildMI(MF, DbgLoc, IID, Indirect, MOs, Var, DIExpr); 610e8d8bef9SDimitry Andric } 611e8d8bef9SDimitry Andric 612e8d8bef9SDimitry Andric /// Is the Loc field a constant or constant object? 613fe6060f1SDimitry Andric bool isConstant(MachineLocKind Kind) const { 614fe6060f1SDimitry Andric return Kind == MachineLocKind::ImmediateKind; 615fe6060f1SDimitry Andric } 616e8d8bef9SDimitry Andric 617e8d8bef9SDimitry Andric /// Check if the Loc field is an entry backup location. 618e8d8bef9SDimitry Andric bool isEntryBackupLoc() const { 619fe6060f1SDimitry Andric return EVKind == EntryValueLocKind::EntryValueBackupKind || 620fe6060f1SDimitry Andric EVKind == EntryValueLocKind::EntryValueCopyBackupKind; 621e8d8bef9SDimitry Andric } 622e8d8bef9SDimitry Andric 623fe6060f1SDimitry Andric /// If this variable is described by register \p Reg holding the entry 624fe6060f1SDimitry Andric /// value, return true. 625fe6060f1SDimitry Andric bool isEntryValueBackupReg(Register Reg) const { 626fe6060f1SDimitry Andric return EVKind == EntryValueLocKind::EntryValueBackupKind && usesReg(Reg); 627e8d8bef9SDimitry Andric } 628e8d8bef9SDimitry Andric 629fe6060f1SDimitry Andric /// If this variable is described by register \p Reg holding a copy of the 630fe6060f1SDimitry Andric /// entry value, return true. 631fe6060f1SDimitry Andric bool isEntryValueCopyBackupReg(Register Reg) const { 632fe6060f1SDimitry Andric return EVKind == EntryValueLocKind::EntryValueCopyBackupKind && 633fe6060f1SDimitry Andric usesReg(Reg); 634e8d8bef9SDimitry Andric } 635e8d8bef9SDimitry Andric 636fe6060f1SDimitry Andric /// If this variable is described in whole or part by \p Reg, return true. 637fe6060f1SDimitry Andric bool usesReg(Register Reg) const { 638fe6060f1SDimitry Andric MachineLoc RegML; 639fe6060f1SDimitry Andric RegML.Kind = MachineLocKind::RegisterKind; 640fe6060f1SDimitry Andric RegML.Value.RegNo = Reg; 641fe6060f1SDimitry Andric return is_contained(Locs, RegML); 642fe6060f1SDimitry Andric } 643fe6060f1SDimitry Andric 644fe6060f1SDimitry Andric /// If this variable is described in whole or part by \p Reg, return true. 645fe6060f1SDimitry Andric unsigned getRegIdx(Register Reg) const { 646fe6060f1SDimitry Andric for (unsigned Idx = 0; Idx < Locs.size(); ++Idx) 647fe6060f1SDimitry Andric if (Locs[Idx].Kind == MachineLocKind::RegisterKind && 648349cc55cSDimitry Andric Register{static_cast<unsigned>(Locs[Idx].Value.RegNo)} == Reg) 649fe6060f1SDimitry Andric return Idx; 650fe6060f1SDimitry Andric llvm_unreachable("Could not find given Reg in Locs"); 651fe6060f1SDimitry Andric } 652fe6060f1SDimitry Andric 653fe6060f1SDimitry Andric /// If this variable is described in whole or part by 1 or more registers, 654fe6060f1SDimitry Andric /// add each of them to \p Regs and return true. 655fe6060f1SDimitry Andric bool getDescribingRegs(SmallVectorImpl<uint32_t> &Regs) const { 656fe6060f1SDimitry Andric bool AnyRegs = false; 657349cc55cSDimitry Andric for (const auto &Loc : Locs) 658fe6060f1SDimitry Andric if (Loc.Kind == MachineLocKind::RegisterKind) { 659fe6060f1SDimitry Andric Regs.push_back(Loc.Value.RegNo); 660fe6060f1SDimitry Andric AnyRegs = true; 661fe6060f1SDimitry Andric } 662fe6060f1SDimitry Andric return AnyRegs; 663fe6060f1SDimitry Andric } 664fe6060f1SDimitry Andric 665fe6060f1SDimitry Andric bool containsSpillLocs() const { 666fe6060f1SDimitry Andric return any_of(Locs, [](VarLoc::MachineLoc ML) { 667fe6060f1SDimitry Andric return ML.Kind == VarLoc::MachineLocKind::SpillLocKind; 668fe6060f1SDimitry Andric }); 669fe6060f1SDimitry Andric } 670fe6060f1SDimitry Andric 671fe6060f1SDimitry Andric /// If this variable is described in whole or part by \p SpillLocation, 672fe6060f1SDimitry Andric /// return true. 673fe6060f1SDimitry Andric bool usesSpillLoc(SpillLoc SpillLocation) const { 674fe6060f1SDimitry Andric MachineLoc SpillML; 675fe6060f1SDimitry Andric SpillML.Kind = MachineLocKind::SpillLocKind; 676fe6060f1SDimitry Andric SpillML.Value.SpillLocation = SpillLocation; 677fe6060f1SDimitry Andric return is_contained(Locs, SpillML); 678fe6060f1SDimitry Andric } 679fe6060f1SDimitry Andric 680fe6060f1SDimitry Andric /// If this variable is described in whole or part by \p SpillLocation, 681fe6060f1SDimitry Andric /// return the index . 682fe6060f1SDimitry Andric unsigned getSpillLocIdx(SpillLoc SpillLocation) const { 683fe6060f1SDimitry Andric for (unsigned Idx = 0; Idx < Locs.size(); ++Idx) 684fe6060f1SDimitry Andric if (Locs[Idx].Kind == MachineLocKind::SpillLocKind && 685fe6060f1SDimitry Andric Locs[Idx].Value.SpillLocation == SpillLocation) 686fe6060f1SDimitry Andric return Idx; 687fe6060f1SDimitry Andric llvm_unreachable("Could not find given SpillLoc in Locs"); 688e8d8bef9SDimitry Andric } 689e8d8bef9SDimitry Andric 690bdd1243dSDimitry Andric bool containsWasmLocs() const { 691bdd1243dSDimitry Andric return any_of(Locs, [](VarLoc::MachineLoc ML) { 692bdd1243dSDimitry Andric return ML.Kind == VarLoc::MachineLocKind::WasmLocKind; 693bdd1243dSDimitry Andric }); 694bdd1243dSDimitry Andric } 695bdd1243dSDimitry Andric 696bdd1243dSDimitry Andric /// If this variable is described in whole or part by \p WasmLocation, 697bdd1243dSDimitry Andric /// return true. 698bdd1243dSDimitry Andric bool usesWasmLoc(WasmLoc WasmLocation) const { 699bdd1243dSDimitry Andric MachineLoc WasmML; 700bdd1243dSDimitry Andric WasmML.Kind = MachineLocKind::WasmLocKind; 701bdd1243dSDimitry Andric WasmML.Value.WasmLocation = WasmLocation; 702bdd1243dSDimitry Andric return is_contained(Locs, WasmML); 703bdd1243dSDimitry Andric } 704bdd1243dSDimitry Andric 705e8d8bef9SDimitry Andric /// Determine whether the lexical scope of this value's debug location 706e8d8bef9SDimitry Andric /// dominates MBB. 707e8d8bef9SDimitry Andric bool dominates(LexicalScopes &LS, MachineBasicBlock &MBB) const { 708e8d8bef9SDimitry Andric return LS.dominates(MI.getDebugLoc().get(), &MBB); 709e8d8bef9SDimitry Andric } 710e8d8bef9SDimitry Andric 711e8d8bef9SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 712bdd1243dSDimitry Andric // TRI and TII can be null. 713bdd1243dSDimitry Andric void dump(const TargetRegisterInfo *TRI, const TargetInstrInfo *TII, 714bdd1243dSDimitry Andric raw_ostream &Out = dbgs()) const { 715e8d8bef9SDimitry Andric Out << "VarLoc("; 716fe6060f1SDimitry Andric for (const MachineLoc &MLoc : Locs) { 717fe6060f1SDimitry Andric if (Locs.begin() != &MLoc) 718fe6060f1SDimitry Andric Out << ", "; 719fe6060f1SDimitry Andric switch (MLoc.Kind) { 720fe6060f1SDimitry Andric case MachineLocKind::RegisterKind: 721fe6060f1SDimitry Andric Out << printReg(MLoc.Value.RegNo, TRI); 722e8d8bef9SDimitry Andric break; 723fe6060f1SDimitry Andric case MachineLocKind::SpillLocKind: 724fe6060f1SDimitry Andric Out << printReg(MLoc.Value.SpillLocation.SpillBase, TRI); 725fe6060f1SDimitry Andric Out << "[" << MLoc.Value.SpillLocation.SpillOffset.getFixed() << " + " 726fe6060f1SDimitry Andric << MLoc.Value.SpillLocation.SpillOffset.getScalable() 727fe6060f1SDimitry Andric << "x vscale" 728e8d8bef9SDimitry Andric << "]"; 729e8d8bef9SDimitry Andric break; 730fe6060f1SDimitry Andric case MachineLocKind::ImmediateKind: 731fe6060f1SDimitry Andric Out << MLoc.Value.Immediate; 732e8d8bef9SDimitry Andric break; 733bdd1243dSDimitry Andric case MachineLocKind::WasmLocKind: { 734bdd1243dSDimitry Andric if (TII) { 735bdd1243dSDimitry Andric auto Indices = TII->getSerializableTargetIndices(); 736bdd1243dSDimitry Andric auto Found = 737bdd1243dSDimitry Andric find_if(Indices, [&](const std::pair<int, const char *> &I) { 738bdd1243dSDimitry Andric return I.first == MLoc.Value.WasmLocation.Index; 739bdd1243dSDimitry Andric }); 740bdd1243dSDimitry Andric assert(Found != Indices.end()); 741bdd1243dSDimitry Andric Out << Found->second; 742bdd1243dSDimitry Andric if (MLoc.Value.WasmLocation.Offset > 0) 743bdd1243dSDimitry Andric Out << " + " << MLoc.Value.WasmLocation.Offset; 744bdd1243dSDimitry Andric } else { 745bdd1243dSDimitry Andric Out << "WasmLoc"; 746bdd1243dSDimitry Andric } 747bdd1243dSDimitry Andric break; 748bdd1243dSDimitry Andric } 749fe6060f1SDimitry Andric case MachineLocKind::InvalidKind: 750e8d8bef9SDimitry Andric llvm_unreachable("Invalid VarLoc in dump method"); 751e8d8bef9SDimitry Andric } 752fe6060f1SDimitry Andric } 753e8d8bef9SDimitry Andric 754e8d8bef9SDimitry Andric Out << ", \"" << Var.getVariable()->getName() << "\", " << *Expr << ", "; 755e8d8bef9SDimitry Andric if (Var.getInlinedAt()) 756e8d8bef9SDimitry Andric Out << "!" << Var.getInlinedAt()->getMetadataID() << ")\n"; 757e8d8bef9SDimitry Andric else 758e8d8bef9SDimitry Andric Out << "(null))"; 759e8d8bef9SDimitry Andric 760e8d8bef9SDimitry Andric if (isEntryBackupLoc()) 761e8d8bef9SDimitry Andric Out << " (backup loc)\n"; 762e8d8bef9SDimitry Andric else 763e8d8bef9SDimitry Andric Out << "\n"; 764e8d8bef9SDimitry Andric } 765e8d8bef9SDimitry Andric #endif 766e8d8bef9SDimitry Andric 767e8d8bef9SDimitry Andric bool operator==(const VarLoc &Other) const { 768fe6060f1SDimitry Andric return std::tie(EVKind, Var, Expr, Locs) == 769fe6060f1SDimitry Andric std::tie(Other.EVKind, Other.Var, Other.Expr, Other.Locs); 770e8d8bef9SDimitry Andric } 771e8d8bef9SDimitry Andric 772e8d8bef9SDimitry Andric /// This operator guarantees that VarLocs are sorted by Variable first. 773e8d8bef9SDimitry Andric bool operator<(const VarLoc &Other) const { 774fe6060f1SDimitry Andric return std::tie(Var, EVKind, Locs, Expr) < 775fe6060f1SDimitry Andric std::tie(Other.Var, Other.EVKind, Other.Locs, Other.Expr); 776e8d8bef9SDimitry Andric } 777e8d8bef9SDimitry Andric }; 778e8d8bef9SDimitry Andric 779fe6060f1SDimitry Andric #ifndef NDEBUG 780fe6060f1SDimitry Andric using VarVec = SmallVector<VarLoc, 32>; 781fe6060f1SDimitry Andric #endif 782fe6060f1SDimitry Andric 783e8d8bef9SDimitry Andric /// VarLocMap is used for two things: 784fe6060f1SDimitry Andric /// 1) Assigning LocIndices to a VarLoc. The LocIndices can be used to 785e8d8bef9SDimitry Andric /// virtually insert a VarLoc into a VarLocSet. 786e8d8bef9SDimitry Andric /// 2) Given a LocIndex, look up the unique associated VarLoc. 787e8d8bef9SDimitry Andric class VarLocMap { 788e8d8bef9SDimitry Andric /// Map a VarLoc to an index within the vector reserved for its location 789e8d8bef9SDimitry Andric /// within Loc2Vars. 790fe6060f1SDimitry Andric std::map<VarLoc, LocIndices> Var2Indices; 791e8d8bef9SDimitry Andric 792e8d8bef9SDimitry Andric /// Map a location to a vector which holds VarLocs which live in that 793e8d8bef9SDimitry Andric /// location. 794e8d8bef9SDimitry Andric SmallDenseMap<LocIndex::u32_location_t, std::vector<VarLoc>> Loc2Vars; 795e8d8bef9SDimitry Andric 796fe6060f1SDimitry Andric public: 797fe6060f1SDimitry Andric /// Retrieve LocIndices for \p VL. 798fe6060f1SDimitry Andric LocIndices insert(const VarLoc &VL) { 799fe6060f1SDimitry Andric LocIndices &Indices = Var2Indices[VL]; 800fe6060f1SDimitry Andric // If Indices is not empty, VL is already in the map. 801fe6060f1SDimitry Andric if (!Indices.empty()) 802fe6060f1SDimitry Andric return Indices; 803fe6060f1SDimitry Andric SmallVector<LocIndex::u32_location_t, 4> Locations; 804fe6060f1SDimitry Andric // LocIndices are determined by EVKind and MLs; each Register has a 805fe6060f1SDimitry Andric // unique location, while all SpillLocs use a single bucket, and any EV 806fe6060f1SDimitry Andric // VarLocs use only the Backup bucket or none at all (except the 807fe6060f1SDimitry Andric // compulsory entry at the universal location index). LocIndices will 808fe6060f1SDimitry Andric // always have an index at the universal location index as the last index. 809fe6060f1SDimitry Andric if (VL.EVKind == VarLoc::EntryValueLocKind::NonEntryValueKind) { 810fe6060f1SDimitry Andric VL.getDescribingRegs(Locations); 811fe6060f1SDimitry Andric assert(all_of(Locations, 812fe6060f1SDimitry Andric [](auto RegNo) { 813fe6060f1SDimitry Andric return RegNo < LocIndex::kFirstInvalidRegLocation; 814fe6060f1SDimitry Andric }) && 815e8d8bef9SDimitry Andric "Physreg out of range?"); 816bdd1243dSDimitry Andric if (VL.containsSpillLocs()) 817bdd1243dSDimitry Andric Locations.push_back(LocIndex::kSpillLocation); 818bdd1243dSDimitry Andric if (VL.containsWasmLocs()) 819bdd1243dSDimitry Andric Locations.push_back(LocIndex::kWasmLocation); 820fe6060f1SDimitry Andric } else if (VL.EVKind != VarLoc::EntryValueLocKind::EntryValueKind) { 821fe6060f1SDimitry Andric LocIndex::u32_location_t Loc = LocIndex::kEntryValueBackupLocation; 822fe6060f1SDimitry Andric Locations.push_back(Loc); 823fe6060f1SDimitry Andric } 824fe6060f1SDimitry Andric Locations.push_back(LocIndex::kUniversalLocation); 825fe6060f1SDimitry Andric for (LocIndex::u32_location_t Location : Locations) { 826fe6060f1SDimitry Andric auto &Vars = Loc2Vars[Location]; 827fe6060f1SDimitry Andric Indices.push_back( 828fe6060f1SDimitry Andric {Location, static_cast<LocIndex::u32_index_t>(Vars.size())}); 829fe6060f1SDimitry Andric Vars.push_back(VL); 830fe6060f1SDimitry Andric } 831fe6060f1SDimitry Andric return Indices; 832e8d8bef9SDimitry Andric } 833e8d8bef9SDimitry Andric 834fe6060f1SDimitry Andric LocIndices getAllIndices(const VarLoc &VL) const { 835fe6060f1SDimitry Andric auto IndIt = Var2Indices.find(VL); 836fe6060f1SDimitry Andric assert(IndIt != Var2Indices.end() && "VarLoc not tracked"); 837fe6060f1SDimitry Andric return IndIt->second; 838e8d8bef9SDimitry Andric } 839e8d8bef9SDimitry Andric 840e8d8bef9SDimitry Andric /// Retrieve the unique VarLoc associated with \p ID. 841e8d8bef9SDimitry Andric const VarLoc &operator[](LocIndex ID) const { 842e8d8bef9SDimitry Andric auto LocIt = Loc2Vars.find(ID.Location); 843e8d8bef9SDimitry Andric assert(LocIt != Loc2Vars.end() && "Location not tracked"); 844e8d8bef9SDimitry Andric return LocIt->second[ID.Index]; 845e8d8bef9SDimitry Andric } 846e8d8bef9SDimitry Andric }; 847e8d8bef9SDimitry Andric 848e8d8bef9SDimitry Andric using VarLocInMBB = 849e8d8bef9SDimitry Andric SmallDenseMap<const MachineBasicBlock *, std::unique_ptr<VarLocSet>>; 850e8d8bef9SDimitry Andric struct TransferDebugPair { 851e8d8bef9SDimitry Andric MachineInstr *TransferInst; ///< Instruction where this transfer occurs. 852e8d8bef9SDimitry Andric LocIndex LocationID; ///< Location number for the transfer dest. 853e8d8bef9SDimitry Andric }; 854e8d8bef9SDimitry Andric using TransferMap = SmallVector<TransferDebugPair, 4>; 855349cc55cSDimitry Andric // Types for recording Entry Var Locations emitted by a single MachineInstr, 856349cc55cSDimitry Andric // as well as recording MachineInstr which last defined a register. 857349cc55cSDimitry Andric using InstToEntryLocMap = std::multimap<const MachineInstr *, LocIndex>; 858349cc55cSDimitry Andric using RegDefToInstMap = DenseMap<Register, MachineInstr *>; 859e8d8bef9SDimitry Andric 860e8d8bef9SDimitry Andric // Types for recording sets of variable fragments that overlap. For a given 861e8d8bef9SDimitry Andric // local variable, we record all other fragments of that variable that could 862e8d8bef9SDimitry Andric // overlap it, to reduce search time. 863e8d8bef9SDimitry Andric using FragmentOfVar = 864e8d8bef9SDimitry Andric std::pair<const DILocalVariable *, DIExpression::FragmentInfo>; 865e8d8bef9SDimitry Andric using OverlapMap = 866e8d8bef9SDimitry Andric DenseMap<FragmentOfVar, SmallVector<DIExpression::FragmentInfo, 1>>; 867e8d8bef9SDimitry Andric 868e8d8bef9SDimitry Andric // Helper while building OverlapMap, a map of all fragments seen for a given 869e8d8bef9SDimitry Andric // DILocalVariable. 870e8d8bef9SDimitry Andric using VarToFragments = 871e8d8bef9SDimitry Andric DenseMap<const DILocalVariable *, SmallSet<FragmentInfo, 4>>; 872e8d8bef9SDimitry Andric 873fe6060f1SDimitry Andric /// Collects all VarLocs from \p CollectFrom. Each unique VarLoc is added 874fe6060f1SDimitry Andric /// to \p Collected once, in order of insertion into \p VarLocIDs. 875fe6060f1SDimitry Andric static void collectAllVarLocs(SmallVectorImpl<VarLoc> &Collected, 876fe6060f1SDimitry Andric const VarLocSet &CollectFrom, 877fe6060f1SDimitry Andric const VarLocMap &VarLocIDs); 878fe6060f1SDimitry Andric 879fe6060f1SDimitry Andric /// Get the registers which are used by VarLocs of kind RegisterKind tracked 880fe6060f1SDimitry Andric /// by \p CollectFrom. 881fe6060f1SDimitry Andric void getUsedRegs(const VarLocSet &CollectFrom, 882fe6060f1SDimitry Andric SmallVectorImpl<Register> &UsedRegs) const; 883fe6060f1SDimitry Andric 884e8d8bef9SDimitry Andric /// This holds the working set of currently open ranges. For fast 885e8d8bef9SDimitry Andric /// access, this is done both as a set of VarLocIDs, and a map of 886e8d8bef9SDimitry Andric /// DebugVariable to recent VarLocID. Note that a DBG_VALUE ends all 887e8d8bef9SDimitry Andric /// previous open ranges for the same variable. In addition, we keep 888e8d8bef9SDimitry Andric /// two different maps (Vars/EntryValuesBackupVars), so erase/insert 889e8d8bef9SDimitry Andric /// methods act differently depending on whether a VarLoc is primary 890e8d8bef9SDimitry Andric /// location or backup one. In the case the VarLoc is backup location 891e8d8bef9SDimitry Andric /// we will erase/insert from the EntryValuesBackupVars map, otherwise 892e8d8bef9SDimitry Andric /// we perform the operation on the Vars. 893e8d8bef9SDimitry Andric class OpenRangesSet { 894fe6060f1SDimitry Andric VarLocSet::Allocator &Alloc; 895e8d8bef9SDimitry Andric VarLocSet VarLocs; 896e8d8bef9SDimitry Andric // Map the DebugVariable to recent primary location ID. 897fe6060f1SDimitry Andric SmallDenseMap<DebugVariable, LocIndices, 8> Vars; 898e8d8bef9SDimitry Andric // Map the DebugVariable to recent backup location ID. 899fe6060f1SDimitry Andric SmallDenseMap<DebugVariable, LocIndices, 8> EntryValuesBackupVars; 900e8d8bef9SDimitry Andric OverlapMap &OverlappingFragments; 901e8d8bef9SDimitry Andric 902e8d8bef9SDimitry Andric public: 903e8d8bef9SDimitry Andric OpenRangesSet(VarLocSet::Allocator &Alloc, OverlapMap &_OLapMap) 904fe6060f1SDimitry Andric : Alloc(Alloc), VarLocs(Alloc), OverlappingFragments(_OLapMap) {} 905e8d8bef9SDimitry Andric 906e8d8bef9SDimitry Andric const VarLocSet &getVarLocs() const { return VarLocs; } 907e8d8bef9SDimitry Andric 908fe6060f1SDimitry Andric // Fetches all VarLocs in \p VarLocIDs and inserts them into \p Collected. 909fe6060f1SDimitry Andric // This method is needed to get every VarLoc once, as each VarLoc may have 910fe6060f1SDimitry Andric // multiple indices in a VarLocMap (corresponding to each applicable 911fe6060f1SDimitry Andric // location), but all VarLocs appear exactly once at the universal location 912fe6060f1SDimitry Andric // index. 913fe6060f1SDimitry Andric void getUniqueVarLocs(SmallVectorImpl<VarLoc> &Collected, 914fe6060f1SDimitry Andric const VarLocMap &VarLocIDs) const { 915fe6060f1SDimitry Andric collectAllVarLocs(Collected, VarLocs, VarLocIDs); 916fe6060f1SDimitry Andric } 917fe6060f1SDimitry Andric 918e8d8bef9SDimitry Andric /// Terminate all open ranges for VL.Var by removing it from the set. 919e8d8bef9SDimitry Andric void erase(const VarLoc &VL); 920e8d8bef9SDimitry Andric 921fe6060f1SDimitry Andric /// Terminate all open ranges listed as indices in \c KillSet with 922fe6060f1SDimitry Andric /// \c Location by removing them from the set. 923fe6060f1SDimitry Andric void erase(const VarLocsInRange &KillSet, const VarLocMap &VarLocIDs, 924fe6060f1SDimitry Andric LocIndex::u32_location_t Location); 925e8d8bef9SDimitry Andric 926e8d8bef9SDimitry Andric /// Insert a new range into the set. 927fe6060f1SDimitry Andric void insert(LocIndices VarLocIDs, const VarLoc &VL); 928e8d8bef9SDimitry Andric 929e8d8bef9SDimitry Andric /// Insert a set of ranges. 930fe6060f1SDimitry Andric void insertFromLocSet(const VarLocSet &ToLoad, const VarLocMap &Map); 931e8d8bef9SDimitry Andric 932bdd1243dSDimitry Andric std::optional<LocIndices> getEntryValueBackup(DebugVariable Var); 933e8d8bef9SDimitry Andric 934e8d8bef9SDimitry Andric /// Empty the set. 935e8d8bef9SDimitry Andric void clear() { 936e8d8bef9SDimitry Andric VarLocs.clear(); 937e8d8bef9SDimitry Andric Vars.clear(); 938e8d8bef9SDimitry Andric EntryValuesBackupVars.clear(); 939e8d8bef9SDimitry Andric } 940e8d8bef9SDimitry Andric 941e8d8bef9SDimitry Andric /// Return whether the set is empty or not. 942e8d8bef9SDimitry Andric bool empty() const { 943e8d8bef9SDimitry Andric assert(Vars.empty() == EntryValuesBackupVars.empty() && 944e8d8bef9SDimitry Andric Vars.empty() == VarLocs.empty() && 945e8d8bef9SDimitry Andric "open ranges are inconsistent"); 946e8d8bef9SDimitry Andric return VarLocs.empty(); 947e8d8bef9SDimitry Andric } 948e8d8bef9SDimitry Andric 949e8d8bef9SDimitry Andric /// Get an empty range of VarLoc IDs. 950e8d8bef9SDimitry Andric auto getEmptyVarLocRange() const { 951e8d8bef9SDimitry Andric return iterator_range<VarLocSet::const_iterator>(getVarLocs().end(), 952e8d8bef9SDimitry Andric getVarLocs().end()); 953e8d8bef9SDimitry Andric } 954e8d8bef9SDimitry Andric 955fe6060f1SDimitry Andric /// Get all set IDs for VarLocs with MLs of kind RegisterKind in \p Reg. 956e8d8bef9SDimitry Andric auto getRegisterVarLocs(Register Reg) const { 957e8d8bef9SDimitry Andric return LocIndex::indexRangeForLocation(getVarLocs(), Reg); 958e8d8bef9SDimitry Andric } 959e8d8bef9SDimitry Andric 960fe6060f1SDimitry Andric /// Get all set IDs for VarLocs with MLs of kind SpillLocKind. 961e8d8bef9SDimitry Andric auto getSpillVarLocs() const { 962e8d8bef9SDimitry Andric return LocIndex::indexRangeForLocation(getVarLocs(), 963e8d8bef9SDimitry Andric LocIndex::kSpillLocation); 964e8d8bef9SDimitry Andric } 965e8d8bef9SDimitry Andric 966fe6060f1SDimitry Andric /// Get all set IDs for VarLocs of EVKind EntryValueBackupKind or 967e8d8bef9SDimitry Andric /// EntryValueCopyBackupKind. 968e8d8bef9SDimitry Andric auto getEntryValueBackupVarLocs() const { 969e8d8bef9SDimitry Andric return LocIndex::indexRangeForLocation( 970e8d8bef9SDimitry Andric getVarLocs(), LocIndex::kEntryValueBackupLocation); 971e8d8bef9SDimitry Andric } 972bdd1243dSDimitry Andric 973bdd1243dSDimitry Andric /// Get all set IDs for VarLocs with MLs of kind WasmLocKind. 974bdd1243dSDimitry Andric auto getWasmVarLocs() const { 975bdd1243dSDimitry Andric return LocIndex::indexRangeForLocation(getVarLocs(), 976bdd1243dSDimitry Andric LocIndex::kWasmLocation); 977bdd1243dSDimitry Andric } 978e8d8bef9SDimitry Andric }; 979e8d8bef9SDimitry Andric 980fe6060f1SDimitry Andric /// Collect all VarLoc IDs from \p CollectFrom for VarLocs with MLs of kind 981fe6060f1SDimitry Andric /// RegisterKind which are located in any reg in \p Regs. The IDs for each 982fe6060f1SDimitry Andric /// VarLoc correspond to entries in the universal location bucket, which every 983fe6060f1SDimitry Andric /// VarLoc has exactly 1 entry for. Insert collected IDs into \p Collected. 984fe6060f1SDimitry Andric static void collectIDsForRegs(VarLocsInRange &Collected, 985fe6060f1SDimitry Andric const DefinedRegsSet &Regs, 986fe6060f1SDimitry Andric const VarLocSet &CollectFrom, 987fe6060f1SDimitry Andric const VarLocMap &VarLocIDs); 988e8d8bef9SDimitry Andric 989e8d8bef9SDimitry Andric VarLocSet &getVarLocsInMBB(const MachineBasicBlock *MBB, VarLocInMBB &Locs) { 990e8d8bef9SDimitry Andric std::unique_ptr<VarLocSet> &VLS = Locs[MBB]; 991e8d8bef9SDimitry Andric if (!VLS) 992e8d8bef9SDimitry Andric VLS = std::make_unique<VarLocSet>(Alloc); 99381ad6265SDimitry Andric return *VLS; 994e8d8bef9SDimitry Andric } 995e8d8bef9SDimitry Andric 996e8d8bef9SDimitry Andric const VarLocSet &getVarLocsInMBB(const MachineBasicBlock *MBB, 997e8d8bef9SDimitry Andric const VarLocInMBB &Locs) const { 998e8d8bef9SDimitry Andric auto It = Locs.find(MBB); 999e8d8bef9SDimitry Andric assert(It != Locs.end() && "MBB not in map"); 100081ad6265SDimitry Andric return *It->second; 1001e8d8bef9SDimitry Andric } 1002e8d8bef9SDimitry Andric 1003e8d8bef9SDimitry Andric /// Tests whether this instruction is a spill to a stack location. 1004e8d8bef9SDimitry Andric bool isSpillInstruction(const MachineInstr &MI, MachineFunction *MF); 1005e8d8bef9SDimitry Andric 1006e8d8bef9SDimitry Andric /// Decide if @MI is a spill instruction and return true if it is. We use 2 1007e8d8bef9SDimitry Andric /// criteria to make this decision: 1008e8d8bef9SDimitry Andric /// - Is this instruction a store to a spill slot? 1009e8d8bef9SDimitry Andric /// - Is there a register operand that is both used and killed? 1010e8d8bef9SDimitry Andric /// TODO: Store optimization can fold spills into other stores (including 1011e8d8bef9SDimitry Andric /// other spills). We do not handle this yet (more than one memory operand). 1012e8d8bef9SDimitry Andric bool isLocationSpill(const MachineInstr &MI, MachineFunction *MF, 1013e8d8bef9SDimitry Andric Register &Reg); 1014e8d8bef9SDimitry Andric 1015e8d8bef9SDimitry Andric /// Returns true if the given machine instruction is a debug value which we 1016e8d8bef9SDimitry Andric /// can emit entry values for. 1017e8d8bef9SDimitry Andric /// 1018e8d8bef9SDimitry Andric /// Currently, we generate debug entry values only for parameters that are 1019e8d8bef9SDimitry Andric /// unmodified throughout the function and located in a register. 1020e8d8bef9SDimitry Andric bool isEntryValueCandidate(const MachineInstr &MI, 1021e8d8bef9SDimitry Andric const DefinedRegsSet &Regs) const; 1022e8d8bef9SDimitry Andric 1023e8d8bef9SDimitry Andric /// If a given instruction is identified as a spill, return the spill location 1024e8d8bef9SDimitry Andric /// and set \p Reg to the spilled register. 1025bdd1243dSDimitry Andric std::optional<VarLoc::SpillLoc> isRestoreInstruction(const MachineInstr &MI, 1026e8d8bef9SDimitry Andric MachineFunction *MF, 1027e8d8bef9SDimitry Andric Register &Reg); 1028e8d8bef9SDimitry Andric /// Given a spill instruction, extract the register and offset used to 1029e8d8bef9SDimitry Andric /// address the spill location in a target independent way. 1030e8d8bef9SDimitry Andric VarLoc::SpillLoc extractSpillBaseRegAndOffset(const MachineInstr &MI); 1031e8d8bef9SDimitry Andric void insertTransferDebugPair(MachineInstr &MI, OpenRangesSet &OpenRanges, 1032e8d8bef9SDimitry Andric TransferMap &Transfers, VarLocMap &VarLocIDs, 1033e8d8bef9SDimitry Andric LocIndex OldVarID, TransferKind Kind, 1034fe6060f1SDimitry Andric const VarLoc::MachineLoc &OldLoc, 1035e8d8bef9SDimitry Andric Register NewReg = Register()); 1036e8d8bef9SDimitry Andric 1037e8d8bef9SDimitry Andric void transferDebugValue(const MachineInstr &MI, OpenRangesSet &OpenRanges, 1038349cc55cSDimitry Andric VarLocMap &VarLocIDs, 1039349cc55cSDimitry Andric InstToEntryLocMap &EntryValTransfers, 1040349cc55cSDimitry Andric RegDefToInstMap &RegSetInstrs); 1041e8d8bef9SDimitry Andric void transferSpillOrRestoreInst(MachineInstr &MI, OpenRangesSet &OpenRanges, 1042e8d8bef9SDimitry Andric VarLocMap &VarLocIDs, TransferMap &Transfers); 1043349cc55cSDimitry Andric void cleanupEntryValueTransfers(const MachineInstr *MI, 1044349cc55cSDimitry Andric OpenRangesSet &OpenRanges, 1045349cc55cSDimitry Andric VarLocMap &VarLocIDs, const VarLoc &EntryVL, 1046349cc55cSDimitry Andric InstToEntryLocMap &EntryValTransfers); 1047349cc55cSDimitry Andric void removeEntryValue(const MachineInstr &MI, OpenRangesSet &OpenRanges, 1048349cc55cSDimitry Andric VarLocMap &VarLocIDs, const VarLoc &EntryVL, 1049349cc55cSDimitry Andric InstToEntryLocMap &EntryValTransfers, 1050349cc55cSDimitry Andric RegDefToInstMap &RegSetInstrs); 1051e8d8bef9SDimitry Andric void emitEntryValues(MachineInstr &MI, OpenRangesSet &OpenRanges, 1052349cc55cSDimitry Andric VarLocMap &VarLocIDs, 1053349cc55cSDimitry Andric InstToEntryLocMap &EntryValTransfers, 1054fe6060f1SDimitry Andric VarLocsInRange &KillSet); 1055e8d8bef9SDimitry Andric void recordEntryValue(const MachineInstr &MI, 1056e8d8bef9SDimitry Andric const DefinedRegsSet &DefinedRegs, 1057e8d8bef9SDimitry Andric OpenRangesSet &OpenRanges, VarLocMap &VarLocIDs); 1058e8d8bef9SDimitry Andric void transferRegisterCopy(MachineInstr &MI, OpenRangesSet &OpenRanges, 1059e8d8bef9SDimitry Andric VarLocMap &VarLocIDs, TransferMap &Transfers); 1060e8d8bef9SDimitry Andric void transferRegisterDef(MachineInstr &MI, OpenRangesSet &OpenRanges, 1061349cc55cSDimitry Andric VarLocMap &VarLocIDs, 1062349cc55cSDimitry Andric InstToEntryLocMap &EntryValTransfers, 1063349cc55cSDimitry Andric RegDefToInstMap &RegSetInstrs); 1064bdd1243dSDimitry Andric void transferWasmDef(MachineInstr &MI, OpenRangesSet &OpenRanges, 1065bdd1243dSDimitry Andric VarLocMap &VarLocIDs); 1066e8d8bef9SDimitry Andric bool transferTerminator(MachineBasicBlock *MBB, OpenRangesSet &OpenRanges, 1067e8d8bef9SDimitry Andric VarLocInMBB &OutLocs, const VarLocMap &VarLocIDs); 1068e8d8bef9SDimitry Andric 1069e8d8bef9SDimitry Andric void process(MachineInstr &MI, OpenRangesSet &OpenRanges, 1070349cc55cSDimitry Andric VarLocMap &VarLocIDs, TransferMap &Transfers, 1071349cc55cSDimitry Andric InstToEntryLocMap &EntryValTransfers, 1072349cc55cSDimitry Andric RegDefToInstMap &RegSetInstrs); 1073e8d8bef9SDimitry Andric 1074e8d8bef9SDimitry Andric void accumulateFragmentMap(MachineInstr &MI, VarToFragments &SeenFragments, 1075e8d8bef9SDimitry Andric OverlapMap &OLapMap); 1076e8d8bef9SDimitry Andric 1077e8d8bef9SDimitry Andric bool join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs, VarLocInMBB &InLocs, 1078e8d8bef9SDimitry Andric const VarLocMap &VarLocIDs, 1079e8d8bef9SDimitry Andric SmallPtrSet<const MachineBasicBlock *, 16> &Visited, 1080e8d8bef9SDimitry Andric SmallPtrSetImpl<const MachineBasicBlock *> &ArtificialBlocks); 1081e8d8bef9SDimitry Andric 1082e8d8bef9SDimitry Andric /// Create DBG_VALUE insts for inlocs that have been propagated but 1083e8d8bef9SDimitry Andric /// had their instruction creation deferred. 1084e8d8bef9SDimitry Andric void flushPendingLocs(VarLocInMBB &PendingInLocs, VarLocMap &VarLocIDs); 1085e8d8bef9SDimitry Andric 1086349cc55cSDimitry Andric bool ExtendRanges(MachineFunction &MF, MachineDominatorTree *DomTree, 1087349cc55cSDimitry Andric TargetPassConfig *TPC, unsigned InputBBLimit, 1088349cc55cSDimitry Andric unsigned InputDbgValLimit) override; 1089e8d8bef9SDimitry Andric 1090e8d8bef9SDimitry Andric public: 1091e8d8bef9SDimitry Andric /// Default construct and initialize the pass. 1092e8d8bef9SDimitry Andric VarLocBasedLDV(); 1093e8d8bef9SDimitry Andric 1094e8d8bef9SDimitry Andric ~VarLocBasedLDV(); 1095e8d8bef9SDimitry Andric 1096e8d8bef9SDimitry Andric /// Print to ostream with a message. 1097e8d8bef9SDimitry Andric void printVarLocInMBB(const MachineFunction &MF, const VarLocInMBB &V, 1098e8d8bef9SDimitry Andric const VarLocMap &VarLocIDs, const char *msg, 1099e8d8bef9SDimitry Andric raw_ostream &Out) const; 1100e8d8bef9SDimitry Andric }; 1101e8d8bef9SDimitry Andric 1102e8d8bef9SDimitry Andric } // end anonymous namespace 1103e8d8bef9SDimitry Andric 1104e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 1105e8d8bef9SDimitry Andric // Implementation 1106e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 1107e8d8bef9SDimitry Andric 110881ad6265SDimitry Andric VarLocBasedLDV::VarLocBasedLDV() = default; 1109e8d8bef9SDimitry Andric 111081ad6265SDimitry Andric VarLocBasedLDV::~VarLocBasedLDV() = default; 1111e8d8bef9SDimitry Andric 1112e8d8bef9SDimitry Andric /// Erase a variable from the set of open ranges, and additionally erase any 1113e8d8bef9SDimitry Andric /// fragments that may overlap it. If the VarLoc is a backup location, erase 1114e8d8bef9SDimitry Andric /// the variable from the EntryValuesBackupVars set, indicating we should stop 1115e8d8bef9SDimitry Andric /// tracking its backup entry location. Otherwise, if the VarLoc is primary 1116e8d8bef9SDimitry Andric /// location, erase the variable from the Vars set. 1117e8d8bef9SDimitry Andric void VarLocBasedLDV::OpenRangesSet::erase(const VarLoc &VL) { 1118e8d8bef9SDimitry Andric // Erasure helper. 111906c3fb27SDimitry Andric auto DoErase = [&VL, this](DebugVariable VarToErase) { 1120e8d8bef9SDimitry Andric auto *EraseFrom = VL.isEntryBackupLoc() ? &EntryValuesBackupVars : &Vars; 1121e8d8bef9SDimitry Andric auto It = EraseFrom->find(VarToErase); 1122e8d8bef9SDimitry Andric if (It != EraseFrom->end()) { 1123fe6060f1SDimitry Andric LocIndices IDs = It->second; 1124fe6060f1SDimitry Andric for (LocIndex ID : IDs) 1125e8d8bef9SDimitry Andric VarLocs.reset(ID.getAsRawInteger()); 1126e8d8bef9SDimitry Andric EraseFrom->erase(It); 1127e8d8bef9SDimitry Andric } 1128e8d8bef9SDimitry Andric }; 1129e8d8bef9SDimitry Andric 1130e8d8bef9SDimitry Andric DebugVariable Var = VL.Var; 1131e8d8bef9SDimitry Andric 1132e8d8bef9SDimitry Andric // Erase the variable/fragment that ends here. 1133e8d8bef9SDimitry Andric DoErase(Var); 1134e8d8bef9SDimitry Andric 1135e8d8bef9SDimitry Andric // Extract the fragment. Interpret an empty fragment as one that covers all 1136e8d8bef9SDimitry Andric // possible bits. 1137e8d8bef9SDimitry Andric FragmentInfo ThisFragment = Var.getFragmentOrDefault(); 1138e8d8bef9SDimitry Andric 1139e8d8bef9SDimitry Andric // There may be fragments that overlap the designated fragment. Look them up 1140e8d8bef9SDimitry Andric // in the pre-computed overlap map, and erase them too. 1141e8d8bef9SDimitry Andric auto MapIt = OverlappingFragments.find({Var.getVariable(), ThisFragment}); 1142e8d8bef9SDimitry Andric if (MapIt != OverlappingFragments.end()) { 1143e8d8bef9SDimitry Andric for (auto Fragment : MapIt->second) { 1144e8d8bef9SDimitry Andric VarLocBasedLDV::OptFragmentInfo FragmentHolder; 1145e8d8bef9SDimitry Andric if (!DebugVariable::isDefaultFragment(Fragment)) 1146e8d8bef9SDimitry Andric FragmentHolder = VarLocBasedLDV::OptFragmentInfo(Fragment); 1147e8d8bef9SDimitry Andric DoErase({Var.getVariable(), FragmentHolder, Var.getInlinedAt()}); 1148e8d8bef9SDimitry Andric } 1149e8d8bef9SDimitry Andric } 1150e8d8bef9SDimitry Andric } 1151e8d8bef9SDimitry Andric 1152fe6060f1SDimitry Andric void VarLocBasedLDV::OpenRangesSet::erase(const VarLocsInRange &KillSet, 1153fe6060f1SDimitry Andric const VarLocMap &VarLocIDs, 1154fe6060f1SDimitry Andric LocIndex::u32_location_t Location) { 1155fe6060f1SDimitry Andric VarLocSet RemoveSet(Alloc); 1156fe6060f1SDimitry Andric for (LocIndex::u32_index_t ID : KillSet) { 1157fe6060f1SDimitry Andric const VarLoc &VL = VarLocIDs[LocIndex(Location, ID)]; 1158fe6060f1SDimitry Andric auto *EraseFrom = VL.isEntryBackupLoc() ? &EntryValuesBackupVars : &Vars; 1159fe6060f1SDimitry Andric EraseFrom->erase(VL.Var); 1160fe6060f1SDimitry Andric LocIndices VLI = VarLocIDs.getAllIndices(VL); 1161fe6060f1SDimitry Andric for (LocIndex ID : VLI) 1162fe6060f1SDimitry Andric RemoveSet.set(ID.getAsRawInteger()); 1163fe6060f1SDimitry Andric } 1164fe6060f1SDimitry Andric VarLocs.intersectWithComplement(RemoveSet); 1165fe6060f1SDimitry Andric } 1166fe6060f1SDimitry Andric 1167fe6060f1SDimitry Andric void VarLocBasedLDV::OpenRangesSet::insertFromLocSet(const VarLocSet &ToLoad, 1168fe6060f1SDimitry Andric const VarLocMap &Map) { 1169fe6060f1SDimitry Andric VarLocsInRange UniqueVarLocIDs; 1170fe6060f1SDimitry Andric DefinedRegsSet Regs; 1171fe6060f1SDimitry Andric Regs.insert(LocIndex::kUniversalLocation); 1172fe6060f1SDimitry Andric collectIDsForRegs(UniqueVarLocIDs, Regs, ToLoad, Map); 1173fe6060f1SDimitry Andric for (uint64_t ID : UniqueVarLocIDs) { 1174fe6060f1SDimitry Andric LocIndex Idx = LocIndex::fromRawInteger(ID); 1175fe6060f1SDimitry Andric const VarLoc &VarL = Map[Idx]; 1176fe6060f1SDimitry Andric const LocIndices Indices = Map.getAllIndices(VarL); 1177fe6060f1SDimitry Andric insert(Indices, VarL); 1178e8d8bef9SDimitry Andric } 1179e8d8bef9SDimitry Andric } 1180e8d8bef9SDimitry Andric 1181fe6060f1SDimitry Andric void VarLocBasedLDV::OpenRangesSet::insert(LocIndices VarLocIDs, 1182e8d8bef9SDimitry Andric const VarLoc &VL) { 1183e8d8bef9SDimitry Andric auto *InsertInto = VL.isEntryBackupLoc() ? &EntryValuesBackupVars : &Vars; 1184fe6060f1SDimitry Andric for (LocIndex ID : VarLocIDs) 1185fe6060f1SDimitry Andric VarLocs.set(ID.getAsRawInteger()); 1186fe6060f1SDimitry Andric InsertInto->insert({VL.Var, VarLocIDs}); 1187e8d8bef9SDimitry Andric } 1188e8d8bef9SDimitry Andric 1189e8d8bef9SDimitry Andric /// Return the Loc ID of an entry value backup location, if it exists for the 1190e8d8bef9SDimitry Andric /// variable. 1191bdd1243dSDimitry Andric std::optional<LocIndices> 1192e8d8bef9SDimitry Andric VarLocBasedLDV::OpenRangesSet::getEntryValueBackup(DebugVariable Var) { 1193e8d8bef9SDimitry Andric auto It = EntryValuesBackupVars.find(Var); 1194e8d8bef9SDimitry Andric if (It != EntryValuesBackupVars.end()) 1195e8d8bef9SDimitry Andric return It->second; 1196e8d8bef9SDimitry Andric 1197bdd1243dSDimitry Andric return std::nullopt; 1198e8d8bef9SDimitry Andric } 1199e8d8bef9SDimitry Andric 1200fe6060f1SDimitry Andric void VarLocBasedLDV::collectIDsForRegs(VarLocsInRange &Collected, 1201e8d8bef9SDimitry Andric const DefinedRegsSet &Regs, 1202fe6060f1SDimitry Andric const VarLocSet &CollectFrom, 1203fe6060f1SDimitry Andric const VarLocMap &VarLocIDs) { 1204e8d8bef9SDimitry Andric assert(!Regs.empty() && "Nothing to collect"); 1205fe6060f1SDimitry Andric SmallVector<Register, 32> SortedRegs; 1206fe6060f1SDimitry Andric append_range(SortedRegs, Regs); 1207e8d8bef9SDimitry Andric array_pod_sort(SortedRegs.begin(), SortedRegs.end()); 1208e8d8bef9SDimitry Andric auto It = CollectFrom.find(LocIndex::rawIndexForReg(SortedRegs.front())); 1209e8d8bef9SDimitry Andric auto End = CollectFrom.end(); 1210fe6060f1SDimitry Andric for (Register Reg : SortedRegs) { 1211fe6060f1SDimitry Andric // The half-open interval [FirstIndexForReg, FirstInvalidIndex) contains 1212fe6060f1SDimitry Andric // all possible VarLoc IDs for VarLocs with MLs of kind RegisterKind which 1213fe6060f1SDimitry Andric // live in Reg. 1214e8d8bef9SDimitry Andric uint64_t FirstIndexForReg = LocIndex::rawIndexForReg(Reg); 1215e8d8bef9SDimitry Andric uint64_t FirstInvalidIndex = LocIndex::rawIndexForReg(Reg + 1); 1216e8d8bef9SDimitry Andric It.advanceToLowerBound(FirstIndexForReg); 1217e8d8bef9SDimitry Andric 1218e8d8bef9SDimitry Andric // Iterate through that half-open interval and collect all the set IDs. 1219fe6060f1SDimitry Andric for (; It != End && *It < FirstInvalidIndex; ++It) { 1220fe6060f1SDimitry Andric LocIndex ItIdx = LocIndex::fromRawInteger(*It); 1221fe6060f1SDimitry Andric const VarLoc &VL = VarLocIDs[ItIdx]; 1222fe6060f1SDimitry Andric LocIndices LI = VarLocIDs.getAllIndices(VL); 1223fe6060f1SDimitry Andric // For now, the back index is always the universal location index. 1224fe6060f1SDimitry Andric assert(LI.back().Location == LocIndex::kUniversalLocation && 1225fe6060f1SDimitry Andric "Unexpected order of LocIndices for VarLoc; was it inserted into " 1226fe6060f1SDimitry Andric "the VarLocMap correctly?"); 1227fe6060f1SDimitry Andric Collected.insert(LI.back().Index); 1228fe6060f1SDimitry Andric } 1229e8d8bef9SDimitry Andric 1230e8d8bef9SDimitry Andric if (It == End) 1231e8d8bef9SDimitry Andric return; 1232e8d8bef9SDimitry Andric } 1233e8d8bef9SDimitry Andric } 1234e8d8bef9SDimitry Andric 1235e8d8bef9SDimitry Andric void VarLocBasedLDV::getUsedRegs(const VarLocSet &CollectFrom, 1236fe6060f1SDimitry Andric SmallVectorImpl<Register> &UsedRegs) const { 1237e8d8bef9SDimitry Andric // All register-based VarLocs are assigned indices greater than or equal to 1238e8d8bef9SDimitry Andric // FirstRegIndex. 1239fe6060f1SDimitry Andric uint64_t FirstRegIndex = 1240fe6060f1SDimitry Andric LocIndex::rawIndexForReg(LocIndex::kFirstRegLocation); 1241e8d8bef9SDimitry Andric uint64_t FirstInvalidIndex = 1242e8d8bef9SDimitry Andric LocIndex::rawIndexForReg(LocIndex::kFirstInvalidRegLocation); 1243e8d8bef9SDimitry Andric for (auto It = CollectFrom.find(FirstRegIndex), 1244e8d8bef9SDimitry Andric End = CollectFrom.find(FirstInvalidIndex); 1245e8d8bef9SDimitry Andric It != End;) { 1246e8d8bef9SDimitry Andric // We found a VarLoc ID for a VarLoc that lives in a register. Figure out 1247e8d8bef9SDimitry Andric // which register and add it to UsedRegs. 1248e8d8bef9SDimitry Andric uint32_t FoundReg = LocIndex::fromRawInteger(*It).Location; 1249e8d8bef9SDimitry Andric assert((UsedRegs.empty() || FoundReg != UsedRegs.back()) && 1250e8d8bef9SDimitry Andric "Duplicate used reg"); 1251e8d8bef9SDimitry Andric UsedRegs.push_back(FoundReg); 1252e8d8bef9SDimitry Andric 1253e8d8bef9SDimitry Andric // Skip to the next /set/ register. Note that this finds a lower bound, so 1254e8d8bef9SDimitry Andric // even if there aren't any VarLocs living in `FoundReg+1`, we're still 1255e8d8bef9SDimitry Andric // guaranteed to move on to the next register (or to end()). 1256e8d8bef9SDimitry Andric uint64_t NextRegIndex = LocIndex::rawIndexForReg(FoundReg + 1); 1257e8d8bef9SDimitry Andric It.advanceToLowerBound(NextRegIndex); 1258e8d8bef9SDimitry Andric } 1259e8d8bef9SDimitry Andric } 1260e8d8bef9SDimitry Andric 1261e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 1262e8d8bef9SDimitry Andric // Debug Range Extension Implementation 1263e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 1264e8d8bef9SDimitry Andric 1265e8d8bef9SDimitry Andric #ifndef NDEBUG 1266e8d8bef9SDimitry Andric void VarLocBasedLDV::printVarLocInMBB(const MachineFunction &MF, 1267e8d8bef9SDimitry Andric const VarLocInMBB &V, 1268e8d8bef9SDimitry Andric const VarLocMap &VarLocIDs, 1269e8d8bef9SDimitry Andric const char *msg, 1270e8d8bef9SDimitry Andric raw_ostream &Out) const { 1271e8d8bef9SDimitry Andric Out << '\n' << msg << '\n'; 1272e8d8bef9SDimitry Andric for (const MachineBasicBlock &BB : MF) { 1273e8d8bef9SDimitry Andric if (!V.count(&BB)) 1274e8d8bef9SDimitry Andric continue; 1275e8d8bef9SDimitry Andric const VarLocSet &L = getVarLocsInMBB(&BB, V); 1276e8d8bef9SDimitry Andric if (L.empty()) 1277e8d8bef9SDimitry Andric continue; 1278fe6060f1SDimitry Andric SmallVector<VarLoc, 32> VarLocs; 1279fe6060f1SDimitry Andric collectAllVarLocs(VarLocs, L, VarLocIDs); 1280e8d8bef9SDimitry Andric Out << "MBB: " << BB.getNumber() << ":\n"; 1281fe6060f1SDimitry Andric for (const VarLoc &VL : VarLocs) { 1282e8d8bef9SDimitry Andric Out << " Var: " << VL.Var.getVariable()->getName(); 1283e8d8bef9SDimitry Andric Out << " MI: "; 1284bdd1243dSDimitry Andric VL.dump(TRI, TII, Out); 1285e8d8bef9SDimitry Andric } 1286e8d8bef9SDimitry Andric } 1287e8d8bef9SDimitry Andric Out << "\n"; 1288e8d8bef9SDimitry Andric } 1289e8d8bef9SDimitry Andric #endif 1290e8d8bef9SDimitry Andric 1291e8d8bef9SDimitry Andric VarLocBasedLDV::VarLoc::SpillLoc 1292e8d8bef9SDimitry Andric VarLocBasedLDV::extractSpillBaseRegAndOffset(const MachineInstr &MI) { 1293e8d8bef9SDimitry Andric assert(MI.hasOneMemOperand() && 1294e8d8bef9SDimitry Andric "Spill instruction does not have exactly one memory operand?"); 1295e8d8bef9SDimitry Andric auto MMOI = MI.memoperands_begin(); 1296e8d8bef9SDimitry Andric const PseudoSourceValue *PVal = (*MMOI)->getPseudoValue(); 1297e8d8bef9SDimitry Andric assert(PVal->kind() == PseudoSourceValue::FixedStack && 1298e8d8bef9SDimitry Andric "Inconsistent memory operand in spill instruction"); 1299e8d8bef9SDimitry Andric int FI = cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex(); 1300e8d8bef9SDimitry Andric const MachineBasicBlock *MBB = MI.getParent(); 1301e8d8bef9SDimitry Andric Register Reg; 1302e8d8bef9SDimitry Andric StackOffset Offset = TFI->getFrameIndexReference(*MBB->getParent(), FI, Reg); 1303e8d8bef9SDimitry Andric return {Reg, Offset}; 1304e8d8bef9SDimitry Andric } 1305e8d8bef9SDimitry Andric 1306349cc55cSDimitry Andric /// Do cleanup of \p EntryValTransfers created by \p TRInst, by removing the 1307349cc55cSDimitry Andric /// Transfer, which uses the to-be-deleted \p EntryVL. 1308349cc55cSDimitry Andric void VarLocBasedLDV::cleanupEntryValueTransfers( 1309349cc55cSDimitry Andric const MachineInstr *TRInst, OpenRangesSet &OpenRanges, VarLocMap &VarLocIDs, 1310349cc55cSDimitry Andric const VarLoc &EntryVL, InstToEntryLocMap &EntryValTransfers) { 1311349cc55cSDimitry Andric if (EntryValTransfers.empty() || TRInst == nullptr) 1312349cc55cSDimitry Andric return; 1313349cc55cSDimitry Andric 1314349cc55cSDimitry Andric auto TransRange = EntryValTransfers.equal_range(TRInst); 131506c3fb27SDimitry Andric for (auto &TDPair : llvm::make_range(TransRange.first, TransRange.second)) { 1316349cc55cSDimitry Andric const VarLoc &EmittedEV = VarLocIDs[TDPair.second]; 1317349cc55cSDimitry Andric if (std::tie(EntryVL.Var, EntryVL.Locs[0].Value.RegNo, EntryVL.Expr) == 1318349cc55cSDimitry Andric std::tie(EmittedEV.Var, EmittedEV.Locs[0].Value.RegNo, 1319349cc55cSDimitry Andric EmittedEV.Expr)) { 1320349cc55cSDimitry Andric OpenRanges.erase(EmittedEV); 1321349cc55cSDimitry Andric EntryValTransfers.erase(TRInst); 1322349cc55cSDimitry Andric break; 1323349cc55cSDimitry Andric } 1324349cc55cSDimitry Andric } 1325349cc55cSDimitry Andric } 1326349cc55cSDimitry Andric 1327e8d8bef9SDimitry Andric /// Try to salvage the debug entry value if we encounter a new debug value 1328e8d8bef9SDimitry Andric /// describing the same parameter, otherwise stop tracking the value. Return 1329349cc55cSDimitry Andric /// true if we should stop tracking the entry value and do the cleanup of 1330349cc55cSDimitry Andric /// emitted Entry Value Transfers, otherwise return false. 1331349cc55cSDimitry Andric void VarLocBasedLDV::removeEntryValue(const MachineInstr &MI, 1332e8d8bef9SDimitry Andric OpenRangesSet &OpenRanges, 1333e8d8bef9SDimitry Andric VarLocMap &VarLocIDs, 1334349cc55cSDimitry Andric const VarLoc &EntryVL, 1335349cc55cSDimitry Andric InstToEntryLocMap &EntryValTransfers, 1336349cc55cSDimitry Andric RegDefToInstMap &RegSetInstrs) { 1337e8d8bef9SDimitry Andric // Skip the DBG_VALUE which is the debug entry value itself. 1338349cc55cSDimitry Andric if (&MI == &EntryVL.MI) 1339349cc55cSDimitry Andric return; 1340e8d8bef9SDimitry Andric 1341e8d8bef9SDimitry Andric // If the parameter's location is not register location, we can not track 1342349cc55cSDimitry Andric // the entry value any more. It doesn't have the TransferInst which defines 1343349cc55cSDimitry Andric // register, so no Entry Value Transfers have been emitted already. 1344349cc55cSDimitry Andric if (!MI.getDebugOperand(0).isReg()) 1345349cc55cSDimitry Andric return; 1346e8d8bef9SDimitry Andric 1347349cc55cSDimitry Andric // Try to get non-debug instruction responsible for the DBG_VALUE. 1348349cc55cSDimitry Andric const MachineInstr *TransferInst = nullptr; 1349e8d8bef9SDimitry Andric Register Reg = MI.getDebugOperand(0).getReg(); 135006c3fb27SDimitry Andric if (Reg.isValid() && RegSetInstrs.contains(Reg)) 1351349cc55cSDimitry Andric TransferInst = RegSetInstrs.find(Reg)->second; 1352fe6060f1SDimitry Andric 1353349cc55cSDimitry Andric // Case of the parameter's DBG_VALUE at the start of entry MBB. 1354349cc55cSDimitry Andric if (!TransferInst && !LastNonDbgMI && MI.getParent()->isEntryBlock()) 1355349cc55cSDimitry Andric return; 1356349cc55cSDimitry Andric 1357349cc55cSDimitry Andric // If the debug expression from the DBG_VALUE is not empty, we can assume the 1358349cc55cSDimitry Andric // parameter's value has changed indicating that we should stop tracking its 1359349cc55cSDimitry Andric // entry value as well. 1360349cc55cSDimitry Andric if (MI.getDebugExpression()->getNumElements() == 0 && TransferInst) { 1361349cc55cSDimitry Andric // If the DBG_VALUE comes from a copy instruction that copies the entry 1362349cc55cSDimitry Andric // value, it means the parameter's value has not changed and we should be 1363349cc55cSDimitry Andric // able to use its entry value. 1364e8d8bef9SDimitry Andric // TODO: Try to keep tracking of an entry value if we encounter a propagated 1365e8d8bef9SDimitry Andric // DBG_VALUE describing the copy of the entry value. (Propagated entry value 1366e8d8bef9SDimitry Andric // does not indicate the parameter modification.) 13675f757f3fSDimitry Andric auto DestSrc = TII->isCopyLikeInstr(*TransferInst); 1368349cc55cSDimitry Andric if (DestSrc) { 1369349cc55cSDimitry Andric const MachineOperand *SrcRegOp, *DestRegOp; 1370e8d8bef9SDimitry Andric SrcRegOp = DestSrc->Source; 1371e8d8bef9SDimitry Andric DestRegOp = DestSrc->Destination; 1372349cc55cSDimitry Andric if (Reg == DestRegOp->getReg()) { 1373e8d8bef9SDimitry Andric for (uint64_t ID : OpenRanges.getEntryValueBackupVarLocs()) { 1374e8d8bef9SDimitry Andric const VarLoc &VL = VarLocIDs[LocIndex::fromRawInteger(ID)]; 1375fe6060f1SDimitry Andric if (VL.isEntryValueCopyBackupReg(Reg) && 1376fe6060f1SDimitry Andric // Entry Values should not be variadic. 1377e8d8bef9SDimitry Andric VL.MI.getDebugOperand(0).getReg() == SrcRegOp->getReg()) 1378349cc55cSDimitry Andric return; 1379349cc55cSDimitry Andric } 1380349cc55cSDimitry Andric } 1381e8d8bef9SDimitry Andric } 1382e8d8bef9SDimitry Andric } 1383e8d8bef9SDimitry Andric 1384349cc55cSDimitry Andric LLVM_DEBUG(dbgs() << "Deleting a DBG entry value because of: "; 1385349cc55cSDimitry Andric MI.print(dbgs(), /*IsStandalone*/ false, 1386349cc55cSDimitry Andric /*SkipOpers*/ false, /*SkipDebugLoc*/ false, 1387349cc55cSDimitry Andric /*AddNewLine*/ true, TII)); 1388349cc55cSDimitry Andric cleanupEntryValueTransfers(TransferInst, OpenRanges, VarLocIDs, EntryVL, 1389349cc55cSDimitry Andric EntryValTransfers); 1390349cc55cSDimitry Andric OpenRanges.erase(EntryVL); 1391e8d8bef9SDimitry Andric } 1392e8d8bef9SDimitry Andric 1393e8d8bef9SDimitry Andric /// End all previous ranges related to @MI and start a new range from @MI 1394e8d8bef9SDimitry Andric /// if it is a DBG_VALUE instr. 1395e8d8bef9SDimitry Andric void VarLocBasedLDV::transferDebugValue(const MachineInstr &MI, 1396e8d8bef9SDimitry Andric OpenRangesSet &OpenRanges, 1397349cc55cSDimitry Andric VarLocMap &VarLocIDs, 1398349cc55cSDimitry Andric InstToEntryLocMap &EntryValTransfers, 1399349cc55cSDimitry Andric RegDefToInstMap &RegSetInstrs) { 1400e8d8bef9SDimitry Andric if (!MI.isDebugValue()) 1401e8d8bef9SDimitry Andric return; 1402e8d8bef9SDimitry Andric const DILocalVariable *Var = MI.getDebugVariable(); 1403e8d8bef9SDimitry Andric const DIExpression *Expr = MI.getDebugExpression(); 1404e8d8bef9SDimitry Andric const DILocation *DebugLoc = MI.getDebugLoc(); 1405e8d8bef9SDimitry Andric const DILocation *InlinedAt = DebugLoc->getInlinedAt(); 1406e8d8bef9SDimitry Andric assert(Var->isValidLocationForIntrinsic(DebugLoc) && 1407e8d8bef9SDimitry Andric "Expected inlined-at fields to agree"); 1408e8d8bef9SDimitry Andric 1409e8d8bef9SDimitry Andric DebugVariable V(Var, Expr, InlinedAt); 1410e8d8bef9SDimitry Andric 1411e8d8bef9SDimitry Andric // Check if this DBG_VALUE indicates a parameter's value changing. 1412e8d8bef9SDimitry Andric // If that is the case, we should stop tracking its entry value. 1413e8d8bef9SDimitry Andric auto EntryValBackupID = OpenRanges.getEntryValueBackup(V); 1414e8d8bef9SDimitry Andric if (Var->isParameter() && EntryValBackupID) { 1415fe6060f1SDimitry Andric const VarLoc &EntryVL = VarLocIDs[EntryValBackupID->back()]; 1416349cc55cSDimitry Andric removeEntryValue(MI, OpenRanges, VarLocIDs, EntryVL, EntryValTransfers, 1417349cc55cSDimitry Andric RegSetInstrs); 1418e8d8bef9SDimitry Andric } 1419e8d8bef9SDimitry Andric 1420fe6060f1SDimitry Andric if (all_of(MI.debug_operands(), [](const MachineOperand &MO) { 1421fe6060f1SDimitry Andric return (MO.isReg() && MO.getReg()) || MO.isImm() || MO.isFPImm() || 1422bdd1243dSDimitry Andric MO.isCImm() || MO.isTargetIndex(); 1423fe6060f1SDimitry Andric })) { 1424e8d8bef9SDimitry Andric // Use normal VarLoc constructor for registers and immediates. 1425bdd1243dSDimitry Andric VarLoc VL(MI); 1426e8d8bef9SDimitry Andric // End all previous ranges of VL.Var. 1427e8d8bef9SDimitry Andric OpenRanges.erase(VL); 1428e8d8bef9SDimitry Andric 1429fe6060f1SDimitry Andric LocIndices IDs = VarLocIDs.insert(VL); 1430e8d8bef9SDimitry Andric // Add the VarLoc to OpenRanges from this DBG_VALUE. 1431fe6060f1SDimitry Andric OpenRanges.insert(IDs, VL); 1432fe6060f1SDimitry Andric } else if (MI.memoperands().size() > 0) { 1433e8d8bef9SDimitry Andric llvm_unreachable("DBG_VALUE with mem operand encountered after regalloc?"); 1434e8d8bef9SDimitry Andric } else { 1435e8d8bef9SDimitry Andric // This must be an undefined location. If it has an open range, erase it. 1436fe6060f1SDimitry Andric assert(MI.isUndefDebugValue() && 1437e8d8bef9SDimitry Andric "Unexpected non-undef DBG_VALUE encountered"); 1438bdd1243dSDimitry Andric VarLoc VL(MI); 1439e8d8bef9SDimitry Andric OpenRanges.erase(VL); 1440e8d8bef9SDimitry Andric } 1441e8d8bef9SDimitry Andric } 1442e8d8bef9SDimitry Andric 1443fe6060f1SDimitry Andric // This should be removed later, doesn't fit the new design. 1444fe6060f1SDimitry Andric void VarLocBasedLDV::collectAllVarLocs(SmallVectorImpl<VarLoc> &Collected, 1445fe6060f1SDimitry Andric const VarLocSet &CollectFrom, 1446fe6060f1SDimitry Andric const VarLocMap &VarLocIDs) { 1447fe6060f1SDimitry Andric // The half-open interval [FirstIndexForReg, FirstInvalidIndex) contains all 1448fe6060f1SDimitry Andric // possible VarLoc IDs for VarLocs with MLs of kind RegisterKind which live 1449fe6060f1SDimitry Andric // in Reg. 1450fe6060f1SDimitry Andric uint64_t FirstIndex = LocIndex::rawIndexForReg(LocIndex::kUniversalLocation); 1451fe6060f1SDimitry Andric uint64_t FirstInvalidIndex = 1452fe6060f1SDimitry Andric LocIndex::rawIndexForReg(LocIndex::kUniversalLocation + 1); 1453fe6060f1SDimitry Andric // Iterate through that half-open interval and collect all the set IDs. 1454fe6060f1SDimitry Andric for (auto It = CollectFrom.find(FirstIndex), End = CollectFrom.end(); 1455fe6060f1SDimitry Andric It != End && *It < FirstInvalidIndex; ++It) { 1456fe6060f1SDimitry Andric LocIndex RegIdx = LocIndex::fromRawInteger(*It); 1457fe6060f1SDimitry Andric Collected.push_back(VarLocIDs[RegIdx]); 1458fe6060f1SDimitry Andric } 1459fe6060f1SDimitry Andric } 1460fe6060f1SDimitry Andric 1461e8d8bef9SDimitry Andric /// Turn the entry value backup locations into primary locations. 1462e8d8bef9SDimitry Andric void VarLocBasedLDV::emitEntryValues(MachineInstr &MI, 1463e8d8bef9SDimitry Andric OpenRangesSet &OpenRanges, 1464e8d8bef9SDimitry Andric VarLocMap &VarLocIDs, 1465349cc55cSDimitry Andric InstToEntryLocMap &EntryValTransfers, 1466fe6060f1SDimitry Andric VarLocsInRange &KillSet) { 1467e8d8bef9SDimitry Andric // Do not insert entry value locations after a terminator. 1468e8d8bef9SDimitry Andric if (MI.isTerminator()) 1469e8d8bef9SDimitry Andric return; 1470e8d8bef9SDimitry Andric 1471fe6060f1SDimitry Andric for (uint32_t ID : KillSet) { 1472fe6060f1SDimitry Andric // The KillSet IDs are indices for the universal location bucket. 1473fe6060f1SDimitry Andric LocIndex Idx = LocIndex(LocIndex::kUniversalLocation, ID); 1474e8d8bef9SDimitry Andric const VarLoc &VL = VarLocIDs[Idx]; 1475e8d8bef9SDimitry Andric if (!VL.Var.getVariable()->isParameter()) 1476e8d8bef9SDimitry Andric continue; 1477e8d8bef9SDimitry Andric 1478e8d8bef9SDimitry Andric auto DebugVar = VL.Var; 1479bdd1243dSDimitry Andric std::optional<LocIndices> EntryValBackupIDs = 1480e8d8bef9SDimitry Andric OpenRanges.getEntryValueBackup(DebugVar); 1481e8d8bef9SDimitry Andric 1482e8d8bef9SDimitry Andric // If the parameter has the entry value backup, it means we should 1483e8d8bef9SDimitry Andric // be able to use its entry value. 1484fe6060f1SDimitry Andric if (!EntryValBackupIDs) 1485e8d8bef9SDimitry Andric continue; 1486e8d8bef9SDimitry Andric 1487fe6060f1SDimitry Andric const VarLoc &EntryVL = VarLocIDs[EntryValBackupIDs->back()]; 1488bdd1243dSDimitry Andric VarLoc EntryLoc = VarLoc::CreateEntryLoc(EntryVL.MI, EntryVL.Expr, 1489fe6060f1SDimitry Andric EntryVL.Locs[0].Value.RegNo); 1490fe6060f1SDimitry Andric LocIndices EntryValueIDs = VarLocIDs.insert(EntryLoc); 1491349cc55cSDimitry Andric assert(EntryValueIDs.size() == 1 && 1492349cc55cSDimitry Andric "EntryValue loc should not be variadic"); 1493349cc55cSDimitry Andric EntryValTransfers.insert({&MI, EntryValueIDs.back()}); 1494fe6060f1SDimitry Andric OpenRanges.insert(EntryValueIDs, EntryLoc); 1495e8d8bef9SDimitry Andric } 1496e8d8bef9SDimitry Andric } 1497e8d8bef9SDimitry Andric 1498e8d8bef9SDimitry Andric /// Create new TransferDebugPair and insert it in \p Transfers. The VarLoc 1499e8d8bef9SDimitry Andric /// with \p OldVarID should be deleted form \p OpenRanges and replaced with 1500e8d8bef9SDimitry Andric /// new VarLoc. If \p NewReg is different than default zero value then the 1501e8d8bef9SDimitry Andric /// new location will be register location created by the copy like instruction, 1502e8d8bef9SDimitry Andric /// otherwise it is variable's location on the stack. 1503e8d8bef9SDimitry Andric void VarLocBasedLDV::insertTransferDebugPair( 1504e8d8bef9SDimitry Andric MachineInstr &MI, OpenRangesSet &OpenRanges, TransferMap &Transfers, 1505e8d8bef9SDimitry Andric VarLocMap &VarLocIDs, LocIndex OldVarID, TransferKind Kind, 1506fe6060f1SDimitry Andric const VarLoc::MachineLoc &OldLoc, Register NewReg) { 1507fe6060f1SDimitry Andric const VarLoc &OldVarLoc = VarLocIDs[OldVarID]; 1508e8d8bef9SDimitry Andric 1509e8d8bef9SDimitry Andric auto ProcessVarLoc = [&MI, &OpenRanges, &Transfers, &VarLocIDs](VarLoc &VL) { 1510fe6060f1SDimitry Andric LocIndices LocIds = VarLocIDs.insert(VL); 1511e8d8bef9SDimitry Andric 1512e8d8bef9SDimitry Andric // Close this variable's previous location range. 1513e8d8bef9SDimitry Andric OpenRanges.erase(VL); 1514e8d8bef9SDimitry Andric 1515e8d8bef9SDimitry Andric // Record the new location as an open range, and a postponed transfer 1516e8d8bef9SDimitry Andric // inserting a DBG_VALUE for this location. 1517fe6060f1SDimitry Andric OpenRanges.insert(LocIds, VL); 1518e8d8bef9SDimitry Andric assert(!MI.isTerminator() && "Cannot insert DBG_VALUE after terminator"); 1519fe6060f1SDimitry Andric TransferDebugPair MIP = {&MI, LocIds.back()}; 1520e8d8bef9SDimitry Andric Transfers.push_back(MIP); 1521e8d8bef9SDimitry Andric }; 1522e8d8bef9SDimitry Andric 1523e8d8bef9SDimitry Andric // End all previous ranges of VL.Var. 1524e8d8bef9SDimitry Andric OpenRanges.erase(VarLocIDs[OldVarID]); 1525e8d8bef9SDimitry Andric switch (Kind) { 1526e8d8bef9SDimitry Andric case TransferKind::TransferCopy: { 1527e8d8bef9SDimitry Andric assert(NewReg && 1528e8d8bef9SDimitry Andric "No register supplied when handling a copy of a debug value"); 1529e8d8bef9SDimitry Andric // Create a DBG_VALUE instruction to describe the Var in its new 1530e8d8bef9SDimitry Andric // register location. 1531fe6060f1SDimitry Andric VarLoc VL = VarLoc::CreateCopyLoc(OldVarLoc, OldLoc, NewReg); 1532e8d8bef9SDimitry Andric ProcessVarLoc(VL); 1533e8d8bef9SDimitry Andric LLVM_DEBUG({ 1534e8d8bef9SDimitry Andric dbgs() << "Creating VarLoc for register copy:"; 1535bdd1243dSDimitry Andric VL.dump(TRI, TII); 1536e8d8bef9SDimitry Andric }); 1537e8d8bef9SDimitry Andric return; 1538e8d8bef9SDimitry Andric } 1539e8d8bef9SDimitry Andric case TransferKind::TransferSpill: { 1540e8d8bef9SDimitry Andric // Create a DBG_VALUE instruction to describe the Var in its spilled 1541e8d8bef9SDimitry Andric // location. 1542e8d8bef9SDimitry Andric VarLoc::SpillLoc SpillLocation = extractSpillBaseRegAndOffset(MI); 1543fe6060f1SDimitry Andric VarLoc VL = VarLoc::CreateSpillLoc( 1544fe6060f1SDimitry Andric OldVarLoc, OldLoc, SpillLocation.SpillBase, SpillLocation.SpillOffset); 1545e8d8bef9SDimitry Andric ProcessVarLoc(VL); 1546e8d8bef9SDimitry Andric LLVM_DEBUG({ 1547e8d8bef9SDimitry Andric dbgs() << "Creating VarLoc for spill:"; 1548bdd1243dSDimitry Andric VL.dump(TRI, TII); 1549e8d8bef9SDimitry Andric }); 1550e8d8bef9SDimitry Andric return; 1551e8d8bef9SDimitry Andric } 1552e8d8bef9SDimitry Andric case TransferKind::TransferRestore: { 1553e8d8bef9SDimitry Andric assert(NewReg && 1554e8d8bef9SDimitry Andric "No register supplied when handling a restore of a debug value"); 1555e8d8bef9SDimitry Andric // DebugInstr refers to the pre-spill location, therefore we can reuse 1556e8d8bef9SDimitry Andric // its expression. 1557fe6060f1SDimitry Andric VarLoc VL = VarLoc::CreateCopyLoc(OldVarLoc, OldLoc, NewReg); 1558e8d8bef9SDimitry Andric ProcessVarLoc(VL); 1559e8d8bef9SDimitry Andric LLVM_DEBUG({ 1560e8d8bef9SDimitry Andric dbgs() << "Creating VarLoc for restore:"; 1561bdd1243dSDimitry Andric VL.dump(TRI, TII); 1562e8d8bef9SDimitry Andric }); 1563e8d8bef9SDimitry Andric return; 1564e8d8bef9SDimitry Andric } 1565e8d8bef9SDimitry Andric } 1566e8d8bef9SDimitry Andric llvm_unreachable("Invalid transfer kind"); 1567e8d8bef9SDimitry Andric } 1568e8d8bef9SDimitry Andric 1569e8d8bef9SDimitry Andric /// A definition of a register may mark the end of a range. 1570349cc55cSDimitry Andric void VarLocBasedLDV::transferRegisterDef(MachineInstr &MI, 1571349cc55cSDimitry Andric OpenRangesSet &OpenRanges, 1572349cc55cSDimitry Andric VarLocMap &VarLocIDs, 1573349cc55cSDimitry Andric InstToEntryLocMap &EntryValTransfers, 1574349cc55cSDimitry Andric RegDefToInstMap &RegSetInstrs) { 1575e8d8bef9SDimitry Andric 1576e8d8bef9SDimitry Andric // Meta Instructions do not affect the debug liveness of any register they 1577e8d8bef9SDimitry Andric // define. 1578e8d8bef9SDimitry Andric if (MI.isMetaInstruction()) 1579e8d8bef9SDimitry Andric return; 1580e8d8bef9SDimitry Andric 1581e8d8bef9SDimitry Andric MachineFunction *MF = MI.getMF(); 1582e8d8bef9SDimitry Andric const TargetLowering *TLI = MF->getSubtarget().getTargetLowering(); 1583e8d8bef9SDimitry Andric Register SP = TLI->getStackPointerRegisterToSaveRestore(); 1584e8d8bef9SDimitry Andric 1585e8d8bef9SDimitry Andric // Find the regs killed by MI, and find regmasks of preserved regs. 1586e8d8bef9SDimitry Andric DefinedRegsSet DeadRegs; 1587e8d8bef9SDimitry Andric SmallVector<const uint32_t *, 4> RegMasks; 1588e8d8bef9SDimitry Andric for (const MachineOperand &MO : MI.operands()) { 1589e8d8bef9SDimitry Andric // Determine whether the operand is a register def. 1590bdd1243dSDimitry Andric if (MO.isReg() && MO.isDef() && MO.getReg() && MO.getReg().isPhysical() && 1591e8d8bef9SDimitry Andric !(MI.isCall() && MO.getReg() == SP)) { 1592e8d8bef9SDimitry Andric // Remove ranges of all aliased registers. 1593e8d8bef9SDimitry Andric for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI) 1594e8d8bef9SDimitry Andric // FIXME: Can we break out of this loop early if no insertion occurs? 1595e8d8bef9SDimitry Andric DeadRegs.insert(*RAI); 1596349cc55cSDimitry Andric RegSetInstrs.erase(MO.getReg()); 1597349cc55cSDimitry Andric RegSetInstrs.insert({MO.getReg(), &MI}); 1598e8d8bef9SDimitry Andric } else if (MO.isRegMask()) { 1599e8d8bef9SDimitry Andric RegMasks.push_back(MO.getRegMask()); 1600e8d8bef9SDimitry Andric } 1601e8d8bef9SDimitry Andric } 1602e8d8bef9SDimitry Andric 1603e8d8bef9SDimitry Andric // Erase VarLocs which reside in one of the dead registers. For performance 1604e8d8bef9SDimitry Andric // reasons, it's critical to not iterate over the full set of open VarLocs. 1605e8d8bef9SDimitry Andric // Iterate over the set of dying/used regs instead. 1606e8d8bef9SDimitry Andric if (!RegMasks.empty()) { 1607fe6060f1SDimitry Andric SmallVector<Register, 32> UsedRegs; 1608e8d8bef9SDimitry Andric getUsedRegs(OpenRanges.getVarLocs(), UsedRegs); 1609fe6060f1SDimitry Andric for (Register Reg : UsedRegs) { 1610e8d8bef9SDimitry Andric // Remove ranges of all clobbered registers. Register masks don't usually 1611e8d8bef9SDimitry Andric // list SP as preserved. Assume that call instructions never clobber SP, 1612e8d8bef9SDimitry Andric // because some backends (e.g., AArch64) never list SP in the regmask. 1613e8d8bef9SDimitry Andric // While the debug info may be off for an instruction or two around 1614e8d8bef9SDimitry Andric // callee-cleanup calls, transferring the DEBUG_VALUE across the call is 1615e8d8bef9SDimitry Andric // still a better user experience. 1616e8d8bef9SDimitry Andric if (Reg == SP) 1617e8d8bef9SDimitry Andric continue; 1618e8d8bef9SDimitry Andric bool AnyRegMaskKillsReg = 1619e8d8bef9SDimitry Andric any_of(RegMasks, [Reg](const uint32_t *RegMask) { 1620e8d8bef9SDimitry Andric return MachineOperand::clobbersPhysReg(RegMask, Reg); 1621e8d8bef9SDimitry Andric }); 1622e8d8bef9SDimitry Andric if (AnyRegMaskKillsReg) 1623e8d8bef9SDimitry Andric DeadRegs.insert(Reg); 1624349cc55cSDimitry Andric if (AnyRegMaskKillsReg) { 1625349cc55cSDimitry Andric RegSetInstrs.erase(Reg); 1626349cc55cSDimitry Andric RegSetInstrs.insert({Reg, &MI}); 1627349cc55cSDimitry Andric } 1628e8d8bef9SDimitry Andric } 1629e8d8bef9SDimitry Andric } 1630e8d8bef9SDimitry Andric 1631e8d8bef9SDimitry Andric if (DeadRegs.empty()) 1632e8d8bef9SDimitry Andric return; 1633e8d8bef9SDimitry Andric 1634fe6060f1SDimitry Andric VarLocsInRange KillSet; 1635fe6060f1SDimitry Andric collectIDsForRegs(KillSet, DeadRegs, OpenRanges.getVarLocs(), VarLocIDs); 1636fe6060f1SDimitry Andric OpenRanges.erase(KillSet, VarLocIDs, LocIndex::kUniversalLocation); 1637e8d8bef9SDimitry Andric 1638e8d8bef9SDimitry Andric if (TPC) { 1639e8d8bef9SDimitry Andric auto &TM = TPC->getTM<TargetMachine>(); 1640e8d8bef9SDimitry Andric if (TM.Options.ShouldEmitDebugEntryValues()) 1641349cc55cSDimitry Andric emitEntryValues(MI, OpenRanges, VarLocIDs, EntryValTransfers, KillSet); 1642e8d8bef9SDimitry Andric } 1643e8d8bef9SDimitry Andric } 1644e8d8bef9SDimitry Andric 1645bdd1243dSDimitry Andric void VarLocBasedLDV::transferWasmDef(MachineInstr &MI, 1646bdd1243dSDimitry Andric OpenRangesSet &OpenRanges, 1647bdd1243dSDimitry Andric VarLocMap &VarLocIDs) { 1648bdd1243dSDimitry Andric // If this is not a Wasm local.set or local.tee, which sets local values, 1649bdd1243dSDimitry Andric // return. 1650bdd1243dSDimitry Andric int Index; 1651bdd1243dSDimitry Andric int64_t Offset; 1652bdd1243dSDimitry Andric if (!TII->isExplicitTargetIndexDef(MI, Index, Offset)) 1653bdd1243dSDimitry Andric return; 1654bdd1243dSDimitry Andric 1655bdd1243dSDimitry Andric // Find the target indices killed by MI, and delete those variable locations 1656bdd1243dSDimitry Andric // from the open range. 1657bdd1243dSDimitry Andric VarLocsInRange KillSet; 1658bdd1243dSDimitry Andric VarLoc::WasmLoc Loc{Index, Offset}; 1659bdd1243dSDimitry Andric for (uint64_t ID : OpenRanges.getWasmVarLocs()) { 1660bdd1243dSDimitry Andric LocIndex Idx = LocIndex::fromRawInteger(ID); 1661bdd1243dSDimitry Andric const VarLoc &VL = VarLocIDs[Idx]; 1662bdd1243dSDimitry Andric assert(VL.containsWasmLocs() && "Broken VarLocSet?"); 1663bdd1243dSDimitry Andric if (VL.usesWasmLoc(Loc)) 1664bdd1243dSDimitry Andric KillSet.insert(ID); 1665bdd1243dSDimitry Andric } 1666bdd1243dSDimitry Andric OpenRanges.erase(KillSet, VarLocIDs, LocIndex::kWasmLocation); 1667bdd1243dSDimitry Andric } 1668bdd1243dSDimitry Andric 1669e8d8bef9SDimitry Andric bool VarLocBasedLDV::isSpillInstruction(const MachineInstr &MI, 1670e8d8bef9SDimitry Andric MachineFunction *MF) { 1671e8d8bef9SDimitry Andric // TODO: Handle multiple stores folded into one. 1672e8d8bef9SDimitry Andric if (!MI.hasOneMemOperand()) 1673e8d8bef9SDimitry Andric return false; 1674e8d8bef9SDimitry Andric 1675e8d8bef9SDimitry Andric if (!MI.getSpillSize(TII) && !MI.getFoldedSpillSize(TII)) 1676e8d8bef9SDimitry Andric return false; // This is not a spill instruction, since no valid size was 1677e8d8bef9SDimitry Andric // returned from either function. 1678e8d8bef9SDimitry Andric 1679e8d8bef9SDimitry Andric return true; 1680e8d8bef9SDimitry Andric } 1681e8d8bef9SDimitry Andric 1682e8d8bef9SDimitry Andric bool VarLocBasedLDV::isLocationSpill(const MachineInstr &MI, 1683e8d8bef9SDimitry Andric MachineFunction *MF, Register &Reg) { 1684e8d8bef9SDimitry Andric if (!isSpillInstruction(MI, MF)) 1685e8d8bef9SDimitry Andric return false; 1686e8d8bef9SDimitry Andric 1687e8d8bef9SDimitry Andric auto isKilledReg = [&](const MachineOperand MO, Register &Reg) { 1688e8d8bef9SDimitry Andric if (!MO.isReg() || !MO.isUse()) { 1689e8d8bef9SDimitry Andric Reg = 0; 1690e8d8bef9SDimitry Andric return false; 1691e8d8bef9SDimitry Andric } 1692e8d8bef9SDimitry Andric Reg = MO.getReg(); 1693e8d8bef9SDimitry Andric return MO.isKill(); 1694e8d8bef9SDimitry Andric }; 1695e8d8bef9SDimitry Andric 1696e8d8bef9SDimitry Andric for (const MachineOperand &MO : MI.operands()) { 1697e8d8bef9SDimitry Andric // In a spill instruction generated by the InlineSpiller the spilled 1698e8d8bef9SDimitry Andric // register has its kill flag set. 1699e8d8bef9SDimitry Andric if (isKilledReg(MO, Reg)) 1700e8d8bef9SDimitry Andric return true; 1701e8d8bef9SDimitry Andric if (Reg != 0) { 1702e8d8bef9SDimitry Andric // Check whether next instruction kills the spilled register. 1703e8d8bef9SDimitry Andric // FIXME: Current solution does not cover search for killed register in 1704e8d8bef9SDimitry Andric // bundles and instructions further down the chain. 1705e8d8bef9SDimitry Andric auto NextI = std::next(MI.getIterator()); 1706e8d8bef9SDimitry Andric // Skip next instruction that points to basic block end iterator. 1707e8d8bef9SDimitry Andric if (MI.getParent()->end() == NextI) 1708e8d8bef9SDimitry Andric continue; 1709e8d8bef9SDimitry Andric Register RegNext; 1710e8d8bef9SDimitry Andric for (const MachineOperand &MONext : NextI->operands()) { 1711e8d8bef9SDimitry Andric // Return true if we came across the register from the 1712e8d8bef9SDimitry Andric // previous spill instruction that is killed in NextI. 1713e8d8bef9SDimitry Andric if (isKilledReg(MONext, RegNext) && RegNext == Reg) 1714e8d8bef9SDimitry Andric return true; 1715e8d8bef9SDimitry Andric } 1716e8d8bef9SDimitry Andric } 1717e8d8bef9SDimitry Andric } 1718e8d8bef9SDimitry Andric // Return false if we didn't find spilled register. 1719e8d8bef9SDimitry Andric return false; 1720e8d8bef9SDimitry Andric } 1721e8d8bef9SDimitry Andric 1722bdd1243dSDimitry Andric std::optional<VarLocBasedLDV::VarLoc::SpillLoc> 1723e8d8bef9SDimitry Andric VarLocBasedLDV::isRestoreInstruction(const MachineInstr &MI, 1724e8d8bef9SDimitry Andric MachineFunction *MF, Register &Reg) { 1725e8d8bef9SDimitry Andric if (!MI.hasOneMemOperand()) 1726bdd1243dSDimitry Andric return std::nullopt; 1727e8d8bef9SDimitry Andric 1728e8d8bef9SDimitry Andric // FIXME: Handle folded restore instructions with more than one memory 1729e8d8bef9SDimitry Andric // operand. 1730e8d8bef9SDimitry Andric if (MI.getRestoreSize(TII)) { 1731e8d8bef9SDimitry Andric Reg = MI.getOperand(0).getReg(); 1732e8d8bef9SDimitry Andric return extractSpillBaseRegAndOffset(MI); 1733e8d8bef9SDimitry Andric } 1734bdd1243dSDimitry Andric return std::nullopt; 1735e8d8bef9SDimitry Andric } 1736e8d8bef9SDimitry Andric 1737e8d8bef9SDimitry Andric /// A spilled register may indicate that we have to end the current range of 1738e8d8bef9SDimitry Andric /// a variable and create a new one for the spill location. 1739e8d8bef9SDimitry Andric /// A restored register may indicate the reverse situation. 1740e8d8bef9SDimitry Andric /// We don't want to insert any instructions in process(), so we just create 1741e8d8bef9SDimitry Andric /// the DBG_VALUE without inserting it and keep track of it in \p Transfers. 1742e8d8bef9SDimitry Andric /// It will be inserted into the BB when we're done iterating over the 1743e8d8bef9SDimitry Andric /// instructions. 1744e8d8bef9SDimitry Andric void VarLocBasedLDV::transferSpillOrRestoreInst(MachineInstr &MI, 1745e8d8bef9SDimitry Andric OpenRangesSet &OpenRanges, 1746e8d8bef9SDimitry Andric VarLocMap &VarLocIDs, 1747e8d8bef9SDimitry Andric TransferMap &Transfers) { 1748e8d8bef9SDimitry Andric MachineFunction *MF = MI.getMF(); 1749e8d8bef9SDimitry Andric TransferKind TKind; 1750e8d8bef9SDimitry Andric Register Reg; 1751bdd1243dSDimitry Andric std::optional<VarLoc::SpillLoc> Loc; 1752e8d8bef9SDimitry Andric 1753e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Examining instruction: "; MI.dump();); 1754e8d8bef9SDimitry Andric 1755e8d8bef9SDimitry Andric // First, if there are any DBG_VALUEs pointing at a spill slot that is 1756e8d8bef9SDimitry Andric // written to, then close the variable location. The value in memory 1757e8d8bef9SDimitry Andric // will have changed. 1758fe6060f1SDimitry Andric VarLocsInRange KillSet; 1759e8d8bef9SDimitry Andric if (isSpillInstruction(MI, MF)) { 1760e8d8bef9SDimitry Andric Loc = extractSpillBaseRegAndOffset(MI); 1761e8d8bef9SDimitry Andric for (uint64_t ID : OpenRanges.getSpillVarLocs()) { 1762e8d8bef9SDimitry Andric LocIndex Idx = LocIndex::fromRawInteger(ID); 1763e8d8bef9SDimitry Andric const VarLoc &VL = VarLocIDs[Idx]; 1764fe6060f1SDimitry Andric assert(VL.containsSpillLocs() && "Broken VarLocSet?"); 1765fe6060f1SDimitry Andric if (VL.usesSpillLoc(*Loc)) { 1766e8d8bef9SDimitry Andric // This location is overwritten by the current instruction -- terminate 1767e8d8bef9SDimitry Andric // the open range, and insert an explicit DBG_VALUE $noreg. 1768e8d8bef9SDimitry Andric // 1769e8d8bef9SDimitry Andric // Doing this at a later stage would require re-interpreting all 1770e8d8bef9SDimitry Andric // DBG_VALUes and DIExpressions to identify whether they point at 1771e8d8bef9SDimitry Andric // memory, and then analysing all memory writes to see if they 1772e8d8bef9SDimitry Andric // overwrite that memory, which is expensive. 1773e8d8bef9SDimitry Andric // 1774e8d8bef9SDimitry Andric // At this stage, we already know which DBG_VALUEs are for spills and 1775e8d8bef9SDimitry Andric // where they are located; it's best to fix handle overwrites now. 1776fe6060f1SDimitry Andric KillSet.insert(ID); 1777fe6060f1SDimitry Andric unsigned SpillLocIdx = VL.getSpillLocIdx(*Loc); 1778fe6060f1SDimitry Andric VarLoc::MachineLoc OldLoc = VL.Locs[SpillLocIdx]; 1779fe6060f1SDimitry Andric VarLoc UndefVL = VarLoc::CreateCopyLoc(VL, OldLoc, 0); 1780fe6060f1SDimitry Andric LocIndices UndefLocIDs = VarLocIDs.insert(UndefVL); 1781fe6060f1SDimitry Andric Transfers.push_back({&MI, UndefLocIDs.back()}); 1782e8d8bef9SDimitry Andric } 1783e8d8bef9SDimitry Andric } 1784fe6060f1SDimitry Andric OpenRanges.erase(KillSet, VarLocIDs, LocIndex::kSpillLocation); 1785e8d8bef9SDimitry Andric } 1786e8d8bef9SDimitry Andric 1787e8d8bef9SDimitry Andric // Try to recognise spill and restore instructions that may create a new 1788e8d8bef9SDimitry Andric // variable location. 1789e8d8bef9SDimitry Andric if (isLocationSpill(MI, MF, Reg)) { 1790e8d8bef9SDimitry Andric TKind = TransferKind::TransferSpill; 1791e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Recognized as spill: "; MI.dump();); 1792e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Register: " << Reg << " " << printReg(Reg, TRI) 1793e8d8bef9SDimitry Andric << "\n"); 1794e8d8bef9SDimitry Andric } else { 1795e8d8bef9SDimitry Andric if (!(Loc = isRestoreInstruction(MI, MF, Reg))) 1796e8d8bef9SDimitry Andric return; 1797e8d8bef9SDimitry Andric TKind = TransferKind::TransferRestore; 1798e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Recognized as restore: "; MI.dump();); 1799e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Register: " << Reg << " " << printReg(Reg, TRI) 1800e8d8bef9SDimitry Andric << "\n"); 1801e8d8bef9SDimitry Andric } 1802e8d8bef9SDimitry Andric // Check if the register or spill location is the location of a debug value. 1803e8d8bef9SDimitry Andric auto TransferCandidates = OpenRanges.getEmptyVarLocRange(); 1804e8d8bef9SDimitry Andric if (TKind == TransferKind::TransferSpill) 1805e8d8bef9SDimitry Andric TransferCandidates = OpenRanges.getRegisterVarLocs(Reg); 1806e8d8bef9SDimitry Andric else if (TKind == TransferKind::TransferRestore) 1807e8d8bef9SDimitry Andric TransferCandidates = OpenRanges.getSpillVarLocs(); 1808e8d8bef9SDimitry Andric for (uint64_t ID : TransferCandidates) { 1809e8d8bef9SDimitry Andric LocIndex Idx = LocIndex::fromRawInteger(ID); 1810e8d8bef9SDimitry Andric const VarLoc &VL = VarLocIDs[Idx]; 1811fe6060f1SDimitry Andric unsigned LocIdx; 1812e8d8bef9SDimitry Andric if (TKind == TransferKind::TransferSpill) { 1813fe6060f1SDimitry Andric assert(VL.usesReg(Reg) && "Broken VarLocSet?"); 1814e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Spilling Register " << printReg(Reg, TRI) << '(' 1815e8d8bef9SDimitry Andric << VL.Var.getVariable()->getName() << ")\n"); 1816fe6060f1SDimitry Andric LocIdx = VL.getRegIdx(Reg); 1817e8d8bef9SDimitry Andric } else { 1818fe6060f1SDimitry Andric assert(TKind == TransferKind::TransferRestore && VL.containsSpillLocs() && 1819fe6060f1SDimitry Andric "Broken VarLocSet?"); 1820fe6060f1SDimitry Andric if (!VL.usesSpillLoc(*Loc)) 1821e8d8bef9SDimitry Andric // The spill location is not the location of a debug value. 1822e8d8bef9SDimitry Andric continue; 1823e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Restoring Register " << printReg(Reg, TRI) << '(' 1824e8d8bef9SDimitry Andric << VL.Var.getVariable()->getName() << ")\n"); 1825fe6060f1SDimitry Andric LocIdx = VL.getSpillLocIdx(*Loc); 1826e8d8bef9SDimitry Andric } 1827fe6060f1SDimitry Andric VarLoc::MachineLoc MLoc = VL.Locs[LocIdx]; 1828e8d8bef9SDimitry Andric insertTransferDebugPair(MI, OpenRanges, Transfers, VarLocIDs, Idx, TKind, 1829fe6060f1SDimitry Andric MLoc, Reg); 1830e8d8bef9SDimitry Andric // FIXME: A comment should explain why it's correct to return early here, 1831e8d8bef9SDimitry Andric // if that is in fact correct. 1832e8d8bef9SDimitry Andric return; 1833e8d8bef9SDimitry Andric } 1834e8d8bef9SDimitry Andric } 1835e8d8bef9SDimitry Andric 1836e8d8bef9SDimitry Andric /// If \p MI is a register copy instruction, that copies a previously tracked 1837e8d8bef9SDimitry Andric /// value from one register to another register that is callee saved, we 1838e8d8bef9SDimitry Andric /// create new DBG_VALUE instruction described with copy destination register. 1839e8d8bef9SDimitry Andric void VarLocBasedLDV::transferRegisterCopy(MachineInstr &MI, 1840e8d8bef9SDimitry Andric OpenRangesSet &OpenRanges, 1841e8d8bef9SDimitry Andric VarLocMap &VarLocIDs, 1842e8d8bef9SDimitry Andric TransferMap &Transfers) { 18435f757f3fSDimitry Andric auto DestSrc = TII->isCopyLikeInstr(MI); 1844e8d8bef9SDimitry Andric if (!DestSrc) 1845e8d8bef9SDimitry Andric return; 1846e8d8bef9SDimitry Andric 1847e8d8bef9SDimitry Andric const MachineOperand *DestRegOp = DestSrc->Destination; 1848e8d8bef9SDimitry Andric const MachineOperand *SrcRegOp = DestSrc->Source; 1849e8d8bef9SDimitry Andric 1850e8d8bef9SDimitry Andric if (!DestRegOp->isDef()) 1851e8d8bef9SDimitry Andric return; 1852e8d8bef9SDimitry Andric 1853e8d8bef9SDimitry Andric auto isCalleeSavedReg = [&](Register Reg) { 1854e8d8bef9SDimitry Andric for (MCRegAliasIterator RAI(Reg, TRI, true); RAI.isValid(); ++RAI) 1855e8d8bef9SDimitry Andric if (CalleeSavedRegs.test(*RAI)) 1856e8d8bef9SDimitry Andric return true; 1857e8d8bef9SDimitry Andric return false; 1858e8d8bef9SDimitry Andric }; 1859e8d8bef9SDimitry Andric 1860e8d8bef9SDimitry Andric Register SrcReg = SrcRegOp->getReg(); 1861e8d8bef9SDimitry Andric Register DestReg = DestRegOp->getReg(); 1862e8d8bef9SDimitry Andric 1863e8d8bef9SDimitry Andric // We want to recognize instructions where destination register is callee 1864e8d8bef9SDimitry Andric // saved register. If register that could be clobbered by the call is 1865e8d8bef9SDimitry Andric // included, there would be a great chance that it is going to be clobbered 1866e8d8bef9SDimitry Andric // soon. It is more likely that previous register location, which is callee 1867e8d8bef9SDimitry Andric // saved, is going to stay unclobbered longer, even if it is killed. 1868e8d8bef9SDimitry Andric if (!isCalleeSavedReg(DestReg)) 1869e8d8bef9SDimitry Andric return; 1870e8d8bef9SDimitry Andric 1871e8d8bef9SDimitry Andric // Remember an entry value movement. If we encounter a new debug value of 1872e8d8bef9SDimitry Andric // a parameter describing only a moving of the value around, rather then 1873e8d8bef9SDimitry Andric // modifying it, we are still able to use the entry value if needed. 1874e8d8bef9SDimitry Andric if (isRegOtherThanSPAndFP(*DestRegOp, MI, TRI)) { 1875e8d8bef9SDimitry Andric for (uint64_t ID : OpenRanges.getEntryValueBackupVarLocs()) { 1876e8d8bef9SDimitry Andric LocIndex Idx = LocIndex::fromRawInteger(ID); 1877e8d8bef9SDimitry Andric const VarLoc &VL = VarLocIDs[Idx]; 1878fe6060f1SDimitry Andric if (VL.isEntryValueBackupReg(SrcReg)) { 1879e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Copy of the entry value: "; MI.dump();); 1880e8d8bef9SDimitry Andric VarLoc EntryValLocCopyBackup = 1881bdd1243dSDimitry Andric VarLoc::CreateEntryCopyBackupLoc(VL.MI, VL.Expr, DestReg); 1882e8d8bef9SDimitry Andric // Stop tracking the original entry value. 1883e8d8bef9SDimitry Andric OpenRanges.erase(VL); 1884e8d8bef9SDimitry Andric 1885e8d8bef9SDimitry Andric // Start tracking the entry value copy. 1886fe6060f1SDimitry Andric LocIndices EntryValCopyLocIDs = VarLocIDs.insert(EntryValLocCopyBackup); 1887fe6060f1SDimitry Andric OpenRanges.insert(EntryValCopyLocIDs, EntryValLocCopyBackup); 1888e8d8bef9SDimitry Andric break; 1889e8d8bef9SDimitry Andric } 1890e8d8bef9SDimitry Andric } 1891e8d8bef9SDimitry Andric } 1892e8d8bef9SDimitry Andric 1893e8d8bef9SDimitry Andric if (!SrcRegOp->isKill()) 1894e8d8bef9SDimitry Andric return; 1895e8d8bef9SDimitry Andric 1896e8d8bef9SDimitry Andric for (uint64_t ID : OpenRanges.getRegisterVarLocs(SrcReg)) { 1897e8d8bef9SDimitry Andric LocIndex Idx = LocIndex::fromRawInteger(ID); 1898fe6060f1SDimitry Andric assert(VarLocIDs[Idx].usesReg(SrcReg) && "Broken VarLocSet?"); 1899fe6060f1SDimitry Andric VarLoc::MachineLocValue Loc; 1900fe6060f1SDimitry Andric Loc.RegNo = SrcReg; 1901fe6060f1SDimitry Andric VarLoc::MachineLoc MLoc{VarLoc::MachineLocKind::RegisterKind, Loc}; 1902e8d8bef9SDimitry Andric insertTransferDebugPair(MI, OpenRanges, Transfers, VarLocIDs, Idx, 1903fe6060f1SDimitry Andric TransferKind::TransferCopy, MLoc, DestReg); 1904e8d8bef9SDimitry Andric // FIXME: A comment should explain why it's correct to return early here, 1905e8d8bef9SDimitry Andric // if that is in fact correct. 1906e8d8bef9SDimitry Andric return; 1907e8d8bef9SDimitry Andric } 1908e8d8bef9SDimitry Andric } 1909e8d8bef9SDimitry Andric 1910e8d8bef9SDimitry Andric /// Terminate all open ranges at the end of the current basic block. 1911e8d8bef9SDimitry Andric bool VarLocBasedLDV::transferTerminator(MachineBasicBlock *CurMBB, 1912e8d8bef9SDimitry Andric OpenRangesSet &OpenRanges, 1913e8d8bef9SDimitry Andric VarLocInMBB &OutLocs, 1914e8d8bef9SDimitry Andric const VarLocMap &VarLocIDs) { 1915e8d8bef9SDimitry Andric bool Changed = false; 1916fe6060f1SDimitry Andric LLVM_DEBUG({ 1917fe6060f1SDimitry Andric VarVec VarLocs; 1918fe6060f1SDimitry Andric OpenRanges.getUniqueVarLocs(VarLocs, VarLocIDs); 1919fe6060f1SDimitry Andric for (VarLoc &VL : VarLocs) { 1920e8d8bef9SDimitry Andric // Copy OpenRanges to OutLocs, if not already present. 1921e8d8bef9SDimitry Andric dbgs() << "Add to OutLocs in MBB #" << CurMBB->getNumber() << ": "; 1922bdd1243dSDimitry Andric VL.dump(TRI, TII); 1923fe6060f1SDimitry Andric } 1924e8d8bef9SDimitry Andric }); 1925e8d8bef9SDimitry Andric VarLocSet &VLS = getVarLocsInMBB(CurMBB, OutLocs); 1926e8d8bef9SDimitry Andric Changed = VLS != OpenRanges.getVarLocs(); 1927e8d8bef9SDimitry Andric // New OutLocs set may be different due to spill, restore or register 1928e8d8bef9SDimitry Andric // copy instruction processing. 1929e8d8bef9SDimitry Andric if (Changed) 1930e8d8bef9SDimitry Andric VLS = OpenRanges.getVarLocs(); 1931e8d8bef9SDimitry Andric OpenRanges.clear(); 1932e8d8bef9SDimitry Andric return Changed; 1933e8d8bef9SDimitry Andric } 1934e8d8bef9SDimitry Andric 1935e8d8bef9SDimitry Andric /// Accumulate a mapping between each DILocalVariable fragment and other 1936e8d8bef9SDimitry Andric /// fragments of that DILocalVariable which overlap. This reduces work during 1937e8d8bef9SDimitry Andric /// the data-flow stage from "Find any overlapping fragments" to "Check if the 1938e8d8bef9SDimitry Andric /// known-to-overlap fragments are present". 1939e8d8bef9SDimitry Andric /// \param MI A previously unprocessed DEBUG_VALUE instruction to analyze for 1940e8d8bef9SDimitry Andric /// fragment usage. 1941e8d8bef9SDimitry Andric /// \param SeenFragments Map from DILocalVariable to all fragments of that 1942e8d8bef9SDimitry Andric /// Variable which are known to exist. 1943e8d8bef9SDimitry Andric /// \param OverlappingFragments The overlap map being constructed, from one 1944e8d8bef9SDimitry Andric /// Var/Fragment pair to a vector of fragments known to overlap. 1945e8d8bef9SDimitry Andric void VarLocBasedLDV::accumulateFragmentMap(MachineInstr &MI, 1946e8d8bef9SDimitry Andric VarToFragments &SeenFragments, 1947e8d8bef9SDimitry Andric OverlapMap &OverlappingFragments) { 1948e8d8bef9SDimitry Andric DebugVariable MIVar(MI.getDebugVariable(), MI.getDebugExpression(), 1949e8d8bef9SDimitry Andric MI.getDebugLoc()->getInlinedAt()); 1950e8d8bef9SDimitry Andric FragmentInfo ThisFragment = MIVar.getFragmentOrDefault(); 1951e8d8bef9SDimitry Andric 1952e8d8bef9SDimitry Andric // If this is the first sighting of this variable, then we are guaranteed 1953e8d8bef9SDimitry Andric // there are currently no overlapping fragments either. Initialize the set 1954e8d8bef9SDimitry Andric // of seen fragments, record no overlaps for the current one, and return. 1955e8d8bef9SDimitry Andric auto SeenIt = SeenFragments.find(MIVar.getVariable()); 1956e8d8bef9SDimitry Andric if (SeenIt == SeenFragments.end()) { 1957e8d8bef9SDimitry Andric SmallSet<FragmentInfo, 4> OneFragment; 1958e8d8bef9SDimitry Andric OneFragment.insert(ThisFragment); 1959e8d8bef9SDimitry Andric SeenFragments.insert({MIVar.getVariable(), OneFragment}); 1960e8d8bef9SDimitry Andric 1961e8d8bef9SDimitry Andric OverlappingFragments.insert({{MIVar.getVariable(), ThisFragment}, {}}); 1962e8d8bef9SDimitry Andric return; 1963e8d8bef9SDimitry Andric } 1964e8d8bef9SDimitry Andric 1965e8d8bef9SDimitry Andric // If this particular Variable/Fragment pair already exists in the overlap 1966e8d8bef9SDimitry Andric // map, it has already been accounted for. 1967e8d8bef9SDimitry Andric auto IsInOLapMap = 1968e8d8bef9SDimitry Andric OverlappingFragments.insert({{MIVar.getVariable(), ThisFragment}, {}}); 1969e8d8bef9SDimitry Andric if (!IsInOLapMap.second) 1970e8d8bef9SDimitry Andric return; 1971e8d8bef9SDimitry Andric 1972e8d8bef9SDimitry Andric auto &ThisFragmentsOverlaps = IsInOLapMap.first->second; 1973e8d8bef9SDimitry Andric auto &AllSeenFragments = SeenIt->second; 1974e8d8bef9SDimitry Andric 1975e8d8bef9SDimitry Andric // Otherwise, examine all other seen fragments for this variable, with "this" 1976e8d8bef9SDimitry Andric // fragment being a previously unseen fragment. Record any pair of 1977e8d8bef9SDimitry Andric // overlapping fragments. 1978fcaf7f86SDimitry Andric for (const auto &ASeenFragment : AllSeenFragments) { 1979e8d8bef9SDimitry Andric // Does this previously seen fragment overlap? 1980e8d8bef9SDimitry Andric if (DIExpression::fragmentsOverlap(ThisFragment, ASeenFragment)) { 1981e8d8bef9SDimitry Andric // Yes: Mark the current fragment as being overlapped. 1982e8d8bef9SDimitry Andric ThisFragmentsOverlaps.push_back(ASeenFragment); 1983e8d8bef9SDimitry Andric // Mark the previously seen fragment as being overlapped by the current 1984e8d8bef9SDimitry Andric // one. 1985e8d8bef9SDimitry Andric auto ASeenFragmentsOverlaps = 1986e8d8bef9SDimitry Andric OverlappingFragments.find({MIVar.getVariable(), ASeenFragment}); 1987e8d8bef9SDimitry Andric assert(ASeenFragmentsOverlaps != OverlappingFragments.end() && 1988e8d8bef9SDimitry Andric "Previously seen var fragment has no vector of overlaps"); 1989e8d8bef9SDimitry Andric ASeenFragmentsOverlaps->second.push_back(ThisFragment); 1990e8d8bef9SDimitry Andric } 1991e8d8bef9SDimitry Andric } 1992e8d8bef9SDimitry Andric 1993e8d8bef9SDimitry Andric AllSeenFragments.insert(ThisFragment); 1994e8d8bef9SDimitry Andric } 1995e8d8bef9SDimitry Andric 1996e8d8bef9SDimitry Andric /// This routine creates OpenRanges. 1997e8d8bef9SDimitry Andric void VarLocBasedLDV::process(MachineInstr &MI, OpenRangesSet &OpenRanges, 1998349cc55cSDimitry Andric VarLocMap &VarLocIDs, TransferMap &Transfers, 1999349cc55cSDimitry Andric InstToEntryLocMap &EntryValTransfers, 2000349cc55cSDimitry Andric RegDefToInstMap &RegSetInstrs) { 2001349cc55cSDimitry Andric if (!MI.isDebugInstr()) 2002349cc55cSDimitry Andric LastNonDbgMI = &MI; 2003349cc55cSDimitry Andric transferDebugValue(MI, OpenRanges, VarLocIDs, EntryValTransfers, 2004349cc55cSDimitry Andric RegSetInstrs); 2005349cc55cSDimitry Andric transferRegisterDef(MI, OpenRanges, VarLocIDs, EntryValTransfers, 2006349cc55cSDimitry Andric RegSetInstrs); 2007bdd1243dSDimitry Andric transferWasmDef(MI, OpenRanges, VarLocIDs); 2008e8d8bef9SDimitry Andric transferRegisterCopy(MI, OpenRanges, VarLocIDs, Transfers); 2009e8d8bef9SDimitry Andric transferSpillOrRestoreInst(MI, OpenRanges, VarLocIDs, Transfers); 2010e8d8bef9SDimitry Andric } 2011e8d8bef9SDimitry Andric 2012e8d8bef9SDimitry Andric /// This routine joins the analysis results of all incoming edges in @MBB by 2013e8d8bef9SDimitry Andric /// inserting a new DBG_VALUE instruction at the start of the @MBB - if the same 2014e8d8bef9SDimitry Andric /// source variable in all the predecessors of @MBB reside in the same location. 2015e8d8bef9SDimitry Andric bool VarLocBasedLDV::join( 2016e8d8bef9SDimitry Andric MachineBasicBlock &MBB, VarLocInMBB &OutLocs, VarLocInMBB &InLocs, 2017e8d8bef9SDimitry Andric const VarLocMap &VarLocIDs, 2018e8d8bef9SDimitry Andric SmallPtrSet<const MachineBasicBlock *, 16> &Visited, 2019e8d8bef9SDimitry Andric SmallPtrSetImpl<const MachineBasicBlock *> &ArtificialBlocks) { 2020e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "join MBB: " << MBB.getNumber() << "\n"); 2021e8d8bef9SDimitry Andric 2022e8d8bef9SDimitry Andric VarLocSet InLocsT(Alloc); // Temporary incoming locations. 2023e8d8bef9SDimitry Andric 2024e8d8bef9SDimitry Andric // For all predecessors of this MBB, find the set of VarLocs that 2025e8d8bef9SDimitry Andric // can be joined. 2026e8d8bef9SDimitry Andric int NumVisited = 0; 2027fcaf7f86SDimitry Andric for (auto *p : MBB.predecessors()) { 2028e8d8bef9SDimitry Andric // Ignore backedges if we have not visited the predecessor yet. As the 2029e8d8bef9SDimitry Andric // predecessor hasn't yet had locations propagated into it, most locations 2030e8d8bef9SDimitry Andric // will not yet be valid, so treat them as all being uninitialized and 2031e8d8bef9SDimitry Andric // potentially valid. If a location guessed to be correct here is 2032e8d8bef9SDimitry Andric // invalidated later, we will remove it when we revisit this block. 2033e8d8bef9SDimitry Andric if (!Visited.count(p)) { 2034e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << " ignoring unvisited pred MBB: " << p->getNumber() 2035e8d8bef9SDimitry Andric << "\n"); 2036e8d8bef9SDimitry Andric continue; 2037e8d8bef9SDimitry Andric } 2038e8d8bef9SDimitry Andric auto OL = OutLocs.find(p); 2039e8d8bef9SDimitry Andric // Join is null in case of empty OutLocs from any of the pred. 2040e8d8bef9SDimitry Andric if (OL == OutLocs.end()) 2041e8d8bef9SDimitry Andric return false; 2042e8d8bef9SDimitry Andric 2043e8d8bef9SDimitry Andric // Just copy over the Out locs to incoming locs for the first visited 2044e8d8bef9SDimitry Andric // predecessor, and for all other predecessors join the Out locs. 204581ad6265SDimitry Andric VarLocSet &OutLocVLS = *OL->second; 2046e8d8bef9SDimitry Andric if (!NumVisited) 2047e8d8bef9SDimitry Andric InLocsT = OutLocVLS; 2048e8d8bef9SDimitry Andric else 2049e8d8bef9SDimitry Andric InLocsT &= OutLocVLS; 2050e8d8bef9SDimitry Andric 2051e8d8bef9SDimitry Andric LLVM_DEBUG({ 2052e8d8bef9SDimitry Andric if (!InLocsT.empty()) { 2053fe6060f1SDimitry Andric VarVec VarLocs; 2054fe6060f1SDimitry Andric collectAllVarLocs(VarLocs, InLocsT, VarLocIDs); 2055fe6060f1SDimitry Andric for (const VarLoc &VL : VarLocs) 2056e8d8bef9SDimitry Andric dbgs() << " gathered candidate incoming var: " 2057fe6060f1SDimitry Andric << VL.Var.getVariable()->getName() << "\n"; 2058e8d8bef9SDimitry Andric } 2059e8d8bef9SDimitry Andric }); 2060e8d8bef9SDimitry Andric 2061e8d8bef9SDimitry Andric NumVisited++; 2062e8d8bef9SDimitry Andric } 2063e8d8bef9SDimitry Andric 2064e8d8bef9SDimitry Andric // Filter out DBG_VALUES that are out of scope. 2065e8d8bef9SDimitry Andric VarLocSet KillSet(Alloc); 2066e8d8bef9SDimitry Andric bool IsArtificial = ArtificialBlocks.count(&MBB); 2067e8d8bef9SDimitry Andric if (!IsArtificial) { 2068e8d8bef9SDimitry Andric for (uint64_t ID : InLocsT) { 2069e8d8bef9SDimitry Andric LocIndex Idx = LocIndex::fromRawInteger(ID); 2070e8d8bef9SDimitry Andric if (!VarLocIDs[Idx].dominates(LS, MBB)) { 2071e8d8bef9SDimitry Andric KillSet.set(ID); 2072e8d8bef9SDimitry Andric LLVM_DEBUG({ 2073e8d8bef9SDimitry Andric auto Name = VarLocIDs[Idx].Var.getVariable()->getName(); 2074e8d8bef9SDimitry Andric dbgs() << " killing " << Name << ", it doesn't dominate MBB\n"; 2075e8d8bef9SDimitry Andric }); 2076e8d8bef9SDimitry Andric } 2077e8d8bef9SDimitry Andric } 2078e8d8bef9SDimitry Andric } 2079e8d8bef9SDimitry Andric InLocsT.intersectWithComplement(KillSet); 2080e8d8bef9SDimitry Andric 2081e8d8bef9SDimitry Andric // As we are processing blocks in reverse post-order we 2082e8d8bef9SDimitry Andric // should have processed at least one predecessor, unless it 2083e8d8bef9SDimitry Andric // is the entry block which has no predecessor. 2084e8d8bef9SDimitry Andric assert((NumVisited || MBB.pred_empty()) && 2085e8d8bef9SDimitry Andric "Should have processed at least one predecessor"); 2086e8d8bef9SDimitry Andric 2087e8d8bef9SDimitry Andric VarLocSet &ILS = getVarLocsInMBB(&MBB, InLocs); 2088e8d8bef9SDimitry Andric bool Changed = false; 2089e8d8bef9SDimitry Andric if (ILS != InLocsT) { 2090e8d8bef9SDimitry Andric ILS = InLocsT; 2091e8d8bef9SDimitry Andric Changed = true; 2092e8d8bef9SDimitry Andric } 2093e8d8bef9SDimitry Andric 2094e8d8bef9SDimitry Andric return Changed; 2095e8d8bef9SDimitry Andric } 2096e8d8bef9SDimitry Andric 2097e8d8bef9SDimitry Andric void VarLocBasedLDV::flushPendingLocs(VarLocInMBB &PendingInLocs, 2098e8d8bef9SDimitry Andric VarLocMap &VarLocIDs) { 2099e8d8bef9SDimitry Andric // PendingInLocs records all locations propagated into blocks, which have 2100e8d8bef9SDimitry Andric // not had DBG_VALUE insts created. Go through and create those insts now. 2101e8d8bef9SDimitry Andric for (auto &Iter : PendingInLocs) { 2102e8d8bef9SDimitry Andric // Map is keyed on a constant pointer, unwrap it so we can insert insts. 2103e8d8bef9SDimitry Andric auto &MBB = const_cast<MachineBasicBlock &>(*Iter.first); 210481ad6265SDimitry Andric VarLocSet &Pending = *Iter.second; 2105e8d8bef9SDimitry Andric 2106fe6060f1SDimitry Andric SmallVector<VarLoc, 32> VarLocs; 2107fe6060f1SDimitry Andric collectAllVarLocs(VarLocs, Pending, VarLocIDs); 2108fe6060f1SDimitry Andric 2109fe6060f1SDimitry Andric for (VarLoc DiffIt : VarLocs) { 2110e8d8bef9SDimitry Andric // The ID location is live-in to MBB -- work out what kind of machine 2111e8d8bef9SDimitry Andric // location it is and create a DBG_VALUE. 2112e8d8bef9SDimitry Andric if (DiffIt.isEntryBackupLoc()) 2113e8d8bef9SDimitry Andric continue; 2114e8d8bef9SDimitry Andric MachineInstr *MI = DiffIt.BuildDbgValue(*MBB.getParent()); 2115e8d8bef9SDimitry Andric MBB.insert(MBB.instr_begin(), MI); 2116e8d8bef9SDimitry Andric 2117e8d8bef9SDimitry Andric (void)MI; 2118e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Inserted: "; MI->dump();); 2119e8d8bef9SDimitry Andric } 2120e8d8bef9SDimitry Andric } 2121e8d8bef9SDimitry Andric } 2122e8d8bef9SDimitry Andric 2123e8d8bef9SDimitry Andric bool VarLocBasedLDV::isEntryValueCandidate( 2124e8d8bef9SDimitry Andric const MachineInstr &MI, const DefinedRegsSet &DefinedRegs) const { 2125e8d8bef9SDimitry Andric assert(MI.isDebugValue() && "This must be DBG_VALUE."); 2126e8d8bef9SDimitry Andric 2127e8d8bef9SDimitry Andric // TODO: Add support for local variables that are expressed in terms of 2128e8d8bef9SDimitry Andric // parameters entry values. 2129e8d8bef9SDimitry Andric // TODO: Add support for modified arguments that can be expressed 2130e8d8bef9SDimitry Andric // by using its entry value. 2131e8d8bef9SDimitry Andric auto *DIVar = MI.getDebugVariable(); 2132e8d8bef9SDimitry Andric if (!DIVar->isParameter()) 2133e8d8bef9SDimitry Andric return false; 2134e8d8bef9SDimitry Andric 2135e8d8bef9SDimitry Andric // Do not consider parameters that belong to an inlined function. 2136e8d8bef9SDimitry Andric if (MI.getDebugLoc()->getInlinedAt()) 2137e8d8bef9SDimitry Andric return false; 2138e8d8bef9SDimitry Andric 2139e8d8bef9SDimitry Andric // Only consider parameters that are described using registers. Parameters 2140e8d8bef9SDimitry Andric // that are passed on the stack are not yet supported, so ignore debug 2141e8d8bef9SDimitry Andric // values that are described by the frame or stack pointer. 2142e8d8bef9SDimitry Andric if (!isRegOtherThanSPAndFP(MI.getDebugOperand(0), MI, TRI)) 2143e8d8bef9SDimitry Andric return false; 2144e8d8bef9SDimitry Andric 2145e8d8bef9SDimitry Andric // If a parameter's value has been propagated from the caller, then the 2146e8d8bef9SDimitry Andric // parameter's DBG_VALUE may be described using a register defined by some 2147e8d8bef9SDimitry Andric // instruction in the entry block, in which case we shouldn't create an 2148e8d8bef9SDimitry Andric // entry value. 2149e8d8bef9SDimitry Andric if (DefinedRegs.count(MI.getDebugOperand(0).getReg())) 2150e8d8bef9SDimitry Andric return false; 2151e8d8bef9SDimitry Andric 2152e8d8bef9SDimitry Andric // TODO: Add support for parameters that have a pre-existing debug expressions 2153e8d8bef9SDimitry Andric // (e.g. fragments). 215406c3fb27SDimitry Andric // A simple deref expression is equivalent to an indirect debug value. 215506c3fb27SDimitry Andric const DIExpression *Expr = MI.getDebugExpression(); 215606c3fb27SDimitry Andric if (Expr->getNumElements() > 0 && !Expr->isDeref()) 2157e8d8bef9SDimitry Andric return false; 2158e8d8bef9SDimitry Andric 2159e8d8bef9SDimitry Andric return true; 2160e8d8bef9SDimitry Andric } 2161e8d8bef9SDimitry Andric 2162e8d8bef9SDimitry Andric /// Collect all register defines (including aliases) for the given instruction. 2163e8d8bef9SDimitry Andric static void collectRegDefs(const MachineInstr &MI, DefinedRegsSet &Regs, 2164e8d8bef9SDimitry Andric const TargetRegisterInfo *TRI) { 216506c3fb27SDimitry Andric for (const MachineOperand &MO : MI.all_defs()) { 216606c3fb27SDimitry Andric if (MO.getReg() && MO.getReg().isPhysical()) { 2167bdd1243dSDimitry Andric Regs.insert(MO.getReg()); 2168e8d8bef9SDimitry Andric for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid(); ++AI) 2169e8d8bef9SDimitry Andric Regs.insert(*AI); 2170e8d8bef9SDimitry Andric } 2171bdd1243dSDimitry Andric } 2172bdd1243dSDimitry Andric } 2173e8d8bef9SDimitry Andric 2174e8d8bef9SDimitry Andric /// This routine records the entry values of function parameters. The values 2175e8d8bef9SDimitry Andric /// could be used as backup values. If we loose the track of some unmodified 2176e8d8bef9SDimitry Andric /// parameters, the backup values will be used as a primary locations. 2177e8d8bef9SDimitry Andric void VarLocBasedLDV::recordEntryValue(const MachineInstr &MI, 2178e8d8bef9SDimitry Andric const DefinedRegsSet &DefinedRegs, 2179e8d8bef9SDimitry Andric OpenRangesSet &OpenRanges, 2180e8d8bef9SDimitry Andric VarLocMap &VarLocIDs) { 2181e8d8bef9SDimitry Andric if (TPC) { 2182e8d8bef9SDimitry Andric auto &TM = TPC->getTM<TargetMachine>(); 2183e8d8bef9SDimitry Andric if (!TM.Options.ShouldEmitDebugEntryValues()) 2184e8d8bef9SDimitry Andric return; 2185e8d8bef9SDimitry Andric } 2186e8d8bef9SDimitry Andric 2187e8d8bef9SDimitry Andric DebugVariable V(MI.getDebugVariable(), MI.getDebugExpression(), 2188e8d8bef9SDimitry Andric MI.getDebugLoc()->getInlinedAt()); 2189e8d8bef9SDimitry Andric 2190e8d8bef9SDimitry Andric if (!isEntryValueCandidate(MI, DefinedRegs) || 2191e8d8bef9SDimitry Andric OpenRanges.getEntryValueBackup(V)) 2192e8d8bef9SDimitry Andric return; 2193e8d8bef9SDimitry Andric 2194e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Creating the backup entry location: "; MI.dump();); 2195e8d8bef9SDimitry Andric 2196e8d8bef9SDimitry Andric // Create the entry value and use it as a backup location until it is 2197e8d8bef9SDimitry Andric // valid. It is valid until a parameter is not changed. 2198e8d8bef9SDimitry Andric DIExpression *NewExpr = 2199e8d8bef9SDimitry Andric DIExpression::prepend(MI.getDebugExpression(), DIExpression::EntryValue); 2200bdd1243dSDimitry Andric VarLoc EntryValLocAsBackup = VarLoc::CreateEntryBackupLoc(MI, NewExpr); 2201fe6060f1SDimitry Andric LocIndices EntryValLocIDs = VarLocIDs.insert(EntryValLocAsBackup); 2202fe6060f1SDimitry Andric OpenRanges.insert(EntryValLocIDs, EntryValLocAsBackup); 2203e8d8bef9SDimitry Andric } 2204e8d8bef9SDimitry Andric 2205e8d8bef9SDimitry Andric /// Calculate the liveness information for the given machine function and 2206e8d8bef9SDimitry Andric /// extend ranges across basic blocks. 2207349cc55cSDimitry Andric bool VarLocBasedLDV::ExtendRanges(MachineFunction &MF, 2208349cc55cSDimitry Andric MachineDominatorTree *DomTree, 2209349cc55cSDimitry Andric TargetPassConfig *TPC, unsigned InputBBLimit, 2210349cc55cSDimitry Andric unsigned InputDbgValLimit) { 2211349cc55cSDimitry Andric (void)DomTree; 2212bdd1243dSDimitry Andric LLVM_DEBUG(dbgs() << "\nDebug Range Extension: " << MF.getName() << "\n"); 2213e8d8bef9SDimitry Andric 2214e8d8bef9SDimitry Andric if (!MF.getFunction().getSubprogram()) 2215e8d8bef9SDimitry Andric // VarLocBaseLDV will already have removed all DBG_VALUEs. 2216e8d8bef9SDimitry Andric return false; 2217e8d8bef9SDimitry Andric 2218e8d8bef9SDimitry Andric // Skip functions from NoDebug compilation units. 2219e8d8bef9SDimitry Andric if (MF.getFunction().getSubprogram()->getUnit()->getEmissionKind() == 2220e8d8bef9SDimitry Andric DICompileUnit::NoDebug) 2221e8d8bef9SDimitry Andric return false; 2222e8d8bef9SDimitry Andric 2223e8d8bef9SDimitry Andric TRI = MF.getSubtarget().getRegisterInfo(); 2224e8d8bef9SDimitry Andric TII = MF.getSubtarget().getInstrInfo(); 2225e8d8bef9SDimitry Andric TFI = MF.getSubtarget().getFrameLowering(); 2226e8d8bef9SDimitry Andric TFI->getCalleeSaves(MF, CalleeSavedRegs); 2227e8d8bef9SDimitry Andric this->TPC = TPC; 2228e8d8bef9SDimitry Andric LS.initialize(MF); 2229e8d8bef9SDimitry Andric 2230e8d8bef9SDimitry Andric bool Changed = false; 2231e8d8bef9SDimitry Andric bool OLChanged = false; 2232e8d8bef9SDimitry Andric bool MBBJoined = false; 2233e8d8bef9SDimitry Andric 2234e8d8bef9SDimitry Andric VarLocMap VarLocIDs; // Map VarLoc<>unique ID for use in bitvectors. 2235e8d8bef9SDimitry Andric OverlapMap OverlapFragments; // Map of overlapping variable fragments. 2236e8d8bef9SDimitry Andric OpenRangesSet OpenRanges(Alloc, OverlapFragments); 2237e8d8bef9SDimitry Andric // Ranges that are open until end of bb. 2238e8d8bef9SDimitry Andric VarLocInMBB OutLocs; // Ranges that exist beyond bb. 2239e8d8bef9SDimitry Andric VarLocInMBB InLocs; // Ranges that are incoming after joining. 2240e8d8bef9SDimitry Andric TransferMap Transfers; // DBG_VALUEs associated with transfers (such as 2241e8d8bef9SDimitry Andric // spills, copies and restores). 2242349cc55cSDimitry Andric // Map responsible MI to attached Transfer emitted from Backup Entry Value. 2243349cc55cSDimitry Andric InstToEntryLocMap EntryValTransfers; 2244349cc55cSDimitry Andric // Map a Register to the last MI which clobbered it. 2245349cc55cSDimitry Andric RegDefToInstMap RegSetInstrs; 2246e8d8bef9SDimitry Andric 2247e8d8bef9SDimitry Andric VarToFragments SeenFragments; 2248e8d8bef9SDimitry Andric 2249e8d8bef9SDimitry Andric // Blocks which are artificial, i.e. blocks which exclusively contain 2250e8d8bef9SDimitry Andric // instructions without locations, or with line 0 locations. 2251e8d8bef9SDimitry Andric SmallPtrSet<const MachineBasicBlock *, 16> ArtificialBlocks; 2252e8d8bef9SDimitry Andric 2253e8d8bef9SDimitry Andric DenseMap<unsigned int, MachineBasicBlock *> OrderToBB; 2254e8d8bef9SDimitry Andric DenseMap<MachineBasicBlock *, unsigned int> BBToOrder; 2255e8d8bef9SDimitry Andric std::priority_queue<unsigned int, std::vector<unsigned int>, 2256e8d8bef9SDimitry Andric std::greater<unsigned int>> 2257e8d8bef9SDimitry Andric Worklist; 2258e8d8bef9SDimitry Andric std::priority_queue<unsigned int, std::vector<unsigned int>, 2259e8d8bef9SDimitry Andric std::greater<unsigned int>> 2260e8d8bef9SDimitry Andric Pending; 2261e8d8bef9SDimitry Andric 2262e8d8bef9SDimitry Andric // Set of register defines that are seen when traversing the entry block 2263e8d8bef9SDimitry Andric // looking for debug entry value candidates. 2264e8d8bef9SDimitry Andric DefinedRegsSet DefinedRegs; 2265e8d8bef9SDimitry Andric 2266e8d8bef9SDimitry Andric // Only in the case of entry MBB collect DBG_VALUEs representing 2267e8d8bef9SDimitry Andric // function parameters in order to generate debug entry values for them. 2268e8d8bef9SDimitry Andric MachineBasicBlock &First_MBB = *(MF.begin()); 2269e8d8bef9SDimitry Andric for (auto &MI : First_MBB) { 2270e8d8bef9SDimitry Andric collectRegDefs(MI, DefinedRegs, TRI); 2271e8d8bef9SDimitry Andric if (MI.isDebugValue()) 2272e8d8bef9SDimitry Andric recordEntryValue(MI, DefinedRegs, OpenRanges, VarLocIDs); 2273e8d8bef9SDimitry Andric } 2274e8d8bef9SDimitry Andric 2275e8d8bef9SDimitry Andric // Initialize per-block structures and scan for fragment overlaps. 2276e8d8bef9SDimitry Andric for (auto &MBB : MF) 2277e8d8bef9SDimitry Andric for (auto &MI : MBB) 2278e8d8bef9SDimitry Andric if (MI.isDebugValue()) 2279e8d8bef9SDimitry Andric accumulateFragmentMap(MI, SeenFragments, OverlapFragments); 2280e8d8bef9SDimitry Andric 2281e8d8bef9SDimitry Andric auto hasNonArtificialLocation = [](const MachineInstr &MI) -> bool { 2282e8d8bef9SDimitry Andric if (const DebugLoc &DL = MI.getDebugLoc()) 2283e8d8bef9SDimitry Andric return DL.getLine() != 0; 2284e8d8bef9SDimitry Andric return false; 2285e8d8bef9SDimitry Andric }; 2286e8d8bef9SDimitry Andric for (auto &MBB : MF) 2287e8d8bef9SDimitry Andric if (none_of(MBB.instrs(), hasNonArtificialLocation)) 2288e8d8bef9SDimitry Andric ArtificialBlocks.insert(&MBB); 2289e8d8bef9SDimitry Andric 2290e8d8bef9SDimitry Andric LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, 2291e8d8bef9SDimitry Andric "OutLocs after initialization", dbgs())); 2292e8d8bef9SDimitry Andric 2293e8d8bef9SDimitry Andric ReversePostOrderTraversal<MachineFunction *> RPOT(&MF); 2294e8d8bef9SDimitry Andric unsigned int RPONumber = 0; 2295fe6060f1SDimitry Andric for (MachineBasicBlock *MBB : RPOT) { 2296fe6060f1SDimitry Andric OrderToBB[RPONumber] = MBB; 2297fe6060f1SDimitry Andric BBToOrder[MBB] = RPONumber; 2298e8d8bef9SDimitry Andric Worklist.push(RPONumber); 2299e8d8bef9SDimitry Andric ++RPONumber; 2300e8d8bef9SDimitry Andric } 2301e8d8bef9SDimitry Andric 2302e8d8bef9SDimitry Andric if (RPONumber > InputBBLimit) { 2303e8d8bef9SDimitry Andric unsigned NumInputDbgValues = 0; 2304e8d8bef9SDimitry Andric for (auto &MBB : MF) 2305e8d8bef9SDimitry Andric for (auto &MI : MBB) 2306e8d8bef9SDimitry Andric if (MI.isDebugValue()) 2307e8d8bef9SDimitry Andric ++NumInputDbgValues; 2308349cc55cSDimitry Andric if (NumInputDbgValues > InputDbgValLimit) { 2309e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Disabling VarLocBasedLDV: " << MF.getName() 2310e8d8bef9SDimitry Andric << " has " << RPONumber << " basic blocks and " 2311e8d8bef9SDimitry Andric << NumInputDbgValues 2312e8d8bef9SDimitry Andric << " input DBG_VALUEs, exceeding limits.\n"); 2313e8d8bef9SDimitry Andric return false; 2314e8d8bef9SDimitry Andric } 2315e8d8bef9SDimitry Andric } 2316e8d8bef9SDimitry Andric 2317e8d8bef9SDimitry Andric // This is a standard "union of predecessor outs" dataflow problem. 2318e8d8bef9SDimitry Andric // To solve it, we perform join() and process() using the two worklist method 2319e8d8bef9SDimitry Andric // until the ranges converge. 2320e8d8bef9SDimitry Andric // Ranges have converged when both worklists are empty. 2321e8d8bef9SDimitry Andric SmallPtrSet<const MachineBasicBlock *, 16> Visited; 2322e8d8bef9SDimitry Andric while (!Worklist.empty() || !Pending.empty()) { 2323e8d8bef9SDimitry Andric // We track what is on the pending worklist to avoid inserting the same 2324e8d8bef9SDimitry Andric // thing twice. We could avoid this with a custom priority queue, but this 2325e8d8bef9SDimitry Andric // is probably not worth it. 2326e8d8bef9SDimitry Andric SmallPtrSet<MachineBasicBlock *, 16> OnPending; 2327e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Processing Worklist\n"); 2328e8d8bef9SDimitry Andric while (!Worklist.empty()) { 2329e8d8bef9SDimitry Andric MachineBasicBlock *MBB = OrderToBB[Worklist.top()]; 2330e8d8bef9SDimitry Andric Worklist.pop(); 2331e8d8bef9SDimitry Andric MBBJoined = join(*MBB, OutLocs, InLocs, VarLocIDs, Visited, 2332e8d8bef9SDimitry Andric ArtificialBlocks); 2333e8d8bef9SDimitry Andric MBBJoined |= Visited.insert(MBB).second; 2334e8d8bef9SDimitry Andric if (MBBJoined) { 2335e8d8bef9SDimitry Andric MBBJoined = false; 2336e8d8bef9SDimitry Andric Changed = true; 2337e8d8bef9SDimitry Andric // Now that we have started to extend ranges across BBs we need to 2338e8d8bef9SDimitry Andric // examine spill, copy and restore instructions to see whether they 2339e8d8bef9SDimitry Andric // operate with registers that correspond to user variables. 2340e8d8bef9SDimitry Andric // First load any pending inlocs. 2341e8d8bef9SDimitry Andric OpenRanges.insertFromLocSet(getVarLocsInMBB(MBB, InLocs), VarLocIDs); 2342349cc55cSDimitry Andric LastNonDbgMI = nullptr; 2343349cc55cSDimitry Andric RegSetInstrs.clear(); 2344e8d8bef9SDimitry Andric for (auto &MI : *MBB) 2345349cc55cSDimitry Andric process(MI, OpenRanges, VarLocIDs, Transfers, EntryValTransfers, 2346349cc55cSDimitry Andric RegSetInstrs); 2347e8d8bef9SDimitry Andric OLChanged |= transferTerminator(MBB, OpenRanges, OutLocs, VarLocIDs); 2348e8d8bef9SDimitry Andric 2349e8d8bef9SDimitry Andric LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, 2350e8d8bef9SDimitry Andric "OutLocs after propagating", dbgs())); 2351e8d8bef9SDimitry Andric LLVM_DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs, 2352e8d8bef9SDimitry Andric "InLocs after propagating", dbgs())); 2353e8d8bef9SDimitry Andric 2354e8d8bef9SDimitry Andric if (OLChanged) { 2355e8d8bef9SDimitry Andric OLChanged = false; 2356fcaf7f86SDimitry Andric for (auto *s : MBB->successors()) 2357e8d8bef9SDimitry Andric if (OnPending.insert(s).second) { 2358e8d8bef9SDimitry Andric Pending.push(BBToOrder[s]); 2359e8d8bef9SDimitry Andric } 2360e8d8bef9SDimitry Andric } 2361e8d8bef9SDimitry Andric } 2362e8d8bef9SDimitry Andric } 2363e8d8bef9SDimitry Andric Worklist.swap(Pending); 2364e8d8bef9SDimitry Andric // At this point, pending must be empty, since it was just the empty 2365e8d8bef9SDimitry Andric // worklist 2366e8d8bef9SDimitry Andric assert(Pending.empty() && "Pending should be empty"); 2367e8d8bef9SDimitry Andric } 2368e8d8bef9SDimitry Andric 2369e8d8bef9SDimitry Andric // Add any DBG_VALUE instructions created by location transfers. 2370e8d8bef9SDimitry Andric for (auto &TR : Transfers) { 2371e8d8bef9SDimitry Andric assert(!TR.TransferInst->isTerminator() && 2372e8d8bef9SDimitry Andric "Cannot insert DBG_VALUE after terminator"); 2373e8d8bef9SDimitry Andric MachineBasicBlock *MBB = TR.TransferInst->getParent(); 2374e8d8bef9SDimitry Andric const VarLoc &VL = VarLocIDs[TR.LocationID]; 2375e8d8bef9SDimitry Andric MachineInstr *MI = VL.BuildDbgValue(MF); 2376e8d8bef9SDimitry Andric MBB->insertAfterBundle(TR.TransferInst->getIterator(), MI); 2377e8d8bef9SDimitry Andric } 2378e8d8bef9SDimitry Andric Transfers.clear(); 2379e8d8bef9SDimitry Andric 2380349cc55cSDimitry Andric // Add DBG_VALUEs created using Backup Entry Value location. 2381349cc55cSDimitry Andric for (auto &TR : EntryValTransfers) { 2382349cc55cSDimitry Andric MachineInstr *TRInst = const_cast<MachineInstr *>(TR.first); 2383349cc55cSDimitry Andric assert(!TRInst->isTerminator() && 2384349cc55cSDimitry Andric "Cannot insert DBG_VALUE after terminator"); 2385349cc55cSDimitry Andric MachineBasicBlock *MBB = TRInst->getParent(); 2386349cc55cSDimitry Andric const VarLoc &VL = VarLocIDs[TR.second]; 2387349cc55cSDimitry Andric MachineInstr *MI = VL.BuildDbgValue(MF); 2388349cc55cSDimitry Andric MBB->insertAfterBundle(TRInst->getIterator(), MI); 2389349cc55cSDimitry Andric } 2390349cc55cSDimitry Andric EntryValTransfers.clear(); 2391349cc55cSDimitry Andric 2392e8d8bef9SDimitry Andric // Deferred inlocs will not have had any DBG_VALUE insts created; do 2393e8d8bef9SDimitry Andric // that now. 2394e8d8bef9SDimitry Andric flushPendingLocs(InLocs, VarLocIDs); 2395e8d8bef9SDimitry Andric 2396e8d8bef9SDimitry Andric LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, "Final OutLocs", dbgs())); 2397e8d8bef9SDimitry Andric LLVM_DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs, "Final InLocs", dbgs())); 2398e8d8bef9SDimitry Andric return Changed; 2399e8d8bef9SDimitry Andric } 2400e8d8bef9SDimitry Andric 2401e8d8bef9SDimitry Andric LDVImpl * 2402e8d8bef9SDimitry Andric llvm::makeVarLocBasedLiveDebugValues() 2403e8d8bef9SDimitry Andric { 2404e8d8bef9SDimitry Andric return new VarLocBasedLDV(); 2405e8d8bef9SDimitry Andric } 2406