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