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 332*1fd87a68SDimitry Andric } EVKind = EntryValueLocKind::NonEntryValueKind; 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()), 400*1fd87a68SDimitry Andric Expr(MI.getDebugExpression()), MI(MI) { 401e8d8bef9SDimitry Andric assert(MI.isDebugValue() && "not a DBG_VALUE"); 402fe6060f1SDimitry Andric assert((MI.isDebugValueList() || MI.getNumOperands() == 4) && 403fe6060f1SDimitry Andric "malformed DBG_VALUE"); 404fe6060f1SDimitry Andric for (const MachineOperand &Op : MI.debug_operands()) { 405fe6060f1SDimitry Andric MachineLoc ML = GetLocForOp(Op); 406fe6060f1SDimitry Andric auto It = find(Locs, ML); 407fe6060f1SDimitry Andric if (It == Locs.end()) { 408fe6060f1SDimitry Andric Locs.push_back(ML); 409fe6060f1SDimitry Andric OrigLocMap.push_back(MI.getDebugOperandIndex(&Op)); 410fe6060f1SDimitry Andric } else { 411fe6060f1SDimitry Andric // ML duplicates an element in Locs; replace references to Op 412fe6060f1SDimitry Andric // with references to the duplicating element. 413fe6060f1SDimitry Andric unsigned OpIdx = Locs.size(); 414fe6060f1SDimitry Andric unsigned DuplicatingIdx = std::distance(Locs.begin(), It); 415fe6060f1SDimitry Andric Expr = DIExpression::replaceArg(Expr, OpIdx, DuplicatingIdx); 416fe6060f1SDimitry Andric } 417e8d8bef9SDimitry Andric } 418e8d8bef9SDimitry Andric 419fe6060f1SDimitry Andric // We create the debug entry values from the factory functions rather 420fe6060f1SDimitry Andric // than from this ctor. 421fe6060f1SDimitry Andric assert(EVKind != EntryValueLocKind::EntryValueKind && 422fe6060f1SDimitry Andric !isEntryBackupLoc()); 423fe6060f1SDimitry Andric } 424fe6060f1SDimitry Andric 425fe6060f1SDimitry Andric static MachineLoc GetLocForOp(const MachineOperand &Op) { 426fe6060f1SDimitry Andric MachineLocKind Kind; 427fe6060f1SDimitry Andric MachineLocValue Loc; 428fe6060f1SDimitry Andric if (Op.isReg()) { 429fe6060f1SDimitry Andric Kind = MachineLocKind::RegisterKind; 430fe6060f1SDimitry Andric Loc.RegNo = Op.getReg(); 431fe6060f1SDimitry Andric } else if (Op.isImm()) { 432fe6060f1SDimitry Andric Kind = MachineLocKind::ImmediateKind; 433fe6060f1SDimitry Andric Loc.Immediate = Op.getImm(); 434fe6060f1SDimitry Andric } else if (Op.isFPImm()) { 435fe6060f1SDimitry Andric Kind = MachineLocKind::ImmediateKind; 436fe6060f1SDimitry Andric Loc.FPImm = Op.getFPImm(); 437fe6060f1SDimitry Andric } else if (Op.isCImm()) { 438fe6060f1SDimitry Andric Kind = MachineLocKind::ImmediateKind; 439fe6060f1SDimitry Andric Loc.CImm = Op.getCImm(); 440fe6060f1SDimitry Andric } else 441fe6060f1SDimitry Andric llvm_unreachable("Invalid Op kind for MachineLoc."); 442fe6060f1SDimitry Andric return {Kind, Loc}; 443e8d8bef9SDimitry Andric } 444e8d8bef9SDimitry Andric 445e8d8bef9SDimitry Andric /// Take the variable and machine-location in DBG_VALUE MI, and build an 446e8d8bef9SDimitry Andric /// entry location using the given expression. 447e8d8bef9SDimitry Andric static VarLoc CreateEntryLoc(const MachineInstr &MI, LexicalScopes &LS, 448e8d8bef9SDimitry Andric const DIExpression *EntryExpr, Register Reg) { 449e8d8bef9SDimitry Andric VarLoc VL(MI, LS); 450fe6060f1SDimitry Andric assert(VL.Locs.size() == 1 && 451fe6060f1SDimitry Andric VL.Locs[0].Kind == MachineLocKind::RegisterKind); 452fe6060f1SDimitry Andric VL.EVKind = EntryValueLocKind::EntryValueKind; 453e8d8bef9SDimitry Andric VL.Expr = EntryExpr; 454fe6060f1SDimitry Andric VL.Locs[0].Value.RegNo = Reg; 455e8d8bef9SDimitry Andric return VL; 456e8d8bef9SDimitry Andric } 457e8d8bef9SDimitry Andric 458e8d8bef9SDimitry Andric /// Take the variable and machine-location from the DBG_VALUE (from the 459e8d8bef9SDimitry Andric /// function entry), and build an entry value backup location. The backup 460e8d8bef9SDimitry Andric /// location will turn into the normal location if the backup is valid at 461e8d8bef9SDimitry Andric /// the time of the primary location clobbering. 462e8d8bef9SDimitry Andric static VarLoc CreateEntryBackupLoc(const MachineInstr &MI, 463e8d8bef9SDimitry Andric LexicalScopes &LS, 464e8d8bef9SDimitry Andric const DIExpression *EntryExpr) { 465e8d8bef9SDimitry Andric VarLoc VL(MI, LS); 466fe6060f1SDimitry Andric assert(VL.Locs.size() == 1 && 467fe6060f1SDimitry Andric VL.Locs[0].Kind == MachineLocKind::RegisterKind); 468fe6060f1SDimitry Andric VL.EVKind = EntryValueLocKind::EntryValueBackupKind; 469e8d8bef9SDimitry Andric VL.Expr = EntryExpr; 470e8d8bef9SDimitry Andric return VL; 471e8d8bef9SDimitry Andric } 472e8d8bef9SDimitry Andric 473e8d8bef9SDimitry Andric /// Take the variable and machine-location from the DBG_VALUE (from the 474e8d8bef9SDimitry Andric /// function entry), and build a copy of an entry value backup location by 475e8d8bef9SDimitry Andric /// setting the register location to NewReg. 476e8d8bef9SDimitry Andric static VarLoc CreateEntryCopyBackupLoc(const MachineInstr &MI, 477e8d8bef9SDimitry Andric LexicalScopes &LS, 478e8d8bef9SDimitry Andric const DIExpression *EntryExpr, 479e8d8bef9SDimitry Andric Register NewReg) { 480e8d8bef9SDimitry Andric VarLoc VL(MI, LS); 481fe6060f1SDimitry Andric assert(VL.Locs.size() == 1 && 482fe6060f1SDimitry Andric VL.Locs[0].Kind == MachineLocKind::RegisterKind); 483fe6060f1SDimitry Andric VL.EVKind = EntryValueLocKind::EntryValueCopyBackupKind; 484e8d8bef9SDimitry Andric VL.Expr = EntryExpr; 485fe6060f1SDimitry Andric VL.Locs[0].Value.RegNo = NewReg; 486e8d8bef9SDimitry Andric return VL; 487e8d8bef9SDimitry Andric } 488e8d8bef9SDimitry Andric 489e8d8bef9SDimitry Andric /// Copy the register location in DBG_VALUE MI, updating the register to 490e8d8bef9SDimitry Andric /// be NewReg. 491fe6060f1SDimitry Andric static VarLoc CreateCopyLoc(const VarLoc &OldVL, const MachineLoc &OldML, 492e8d8bef9SDimitry Andric Register NewReg) { 493fe6060f1SDimitry Andric VarLoc VL = OldVL; 4940eae32dcSDimitry Andric for (MachineLoc &ML : VL.Locs) 4950eae32dcSDimitry Andric if (ML == OldML) { 4960eae32dcSDimitry Andric ML.Kind = MachineLocKind::RegisterKind; 4970eae32dcSDimitry Andric ML.Value.RegNo = NewReg; 498e8d8bef9SDimitry Andric return VL; 499e8d8bef9SDimitry Andric } 500fe6060f1SDimitry Andric llvm_unreachable("Should have found OldML in new VarLoc."); 501fe6060f1SDimitry Andric } 502e8d8bef9SDimitry Andric 503fe6060f1SDimitry Andric /// Take the variable described by DBG_VALUE* MI, and create a VarLoc 504e8d8bef9SDimitry Andric /// locating it in the specified spill location. 505fe6060f1SDimitry Andric static VarLoc CreateSpillLoc(const VarLoc &OldVL, const MachineLoc &OldML, 506fe6060f1SDimitry Andric unsigned SpillBase, StackOffset SpillOffset) { 507fe6060f1SDimitry Andric VarLoc VL = OldVL; 5080eae32dcSDimitry Andric for (MachineLoc &ML : VL.Locs) 5090eae32dcSDimitry Andric if (ML == OldML) { 5100eae32dcSDimitry Andric ML.Kind = MachineLocKind::SpillLocKind; 5110eae32dcSDimitry Andric ML.Value.SpillLocation = {SpillBase, SpillOffset}; 512e8d8bef9SDimitry Andric return VL; 513e8d8bef9SDimitry Andric } 514fe6060f1SDimitry Andric llvm_unreachable("Should have found OldML in new VarLoc."); 515fe6060f1SDimitry Andric } 516e8d8bef9SDimitry Andric 517e8d8bef9SDimitry Andric /// Create a DBG_VALUE representing this VarLoc in the given function. 518e8d8bef9SDimitry Andric /// Copies variable-specific information such as DILocalVariable and 519e8d8bef9SDimitry Andric /// inlining information from the original DBG_VALUE instruction, which may 520e8d8bef9SDimitry Andric /// have been several transfers ago. 521e8d8bef9SDimitry Andric MachineInstr *BuildDbgValue(MachineFunction &MF) const { 522fe6060f1SDimitry Andric assert(!isEntryBackupLoc() && 523fe6060f1SDimitry Andric "Tried to produce DBG_VALUE for backup VarLoc"); 524e8d8bef9SDimitry Andric const DebugLoc &DbgLoc = MI.getDebugLoc(); 525e8d8bef9SDimitry Andric bool Indirect = MI.isIndirectDebugValue(); 526e8d8bef9SDimitry Andric const auto &IID = MI.getDesc(); 527e8d8bef9SDimitry Andric const DILocalVariable *Var = MI.getDebugVariable(); 528e8d8bef9SDimitry Andric NumInserted++; 529e8d8bef9SDimitry Andric 530fe6060f1SDimitry Andric const DIExpression *DIExpr = Expr; 531fe6060f1SDimitry Andric SmallVector<MachineOperand, 8> MOs; 532fe6060f1SDimitry Andric for (unsigned I = 0, E = Locs.size(); I < E; ++I) { 533fe6060f1SDimitry Andric MachineLocKind LocKind = Locs[I].Kind; 534fe6060f1SDimitry Andric MachineLocValue Loc = Locs[I].Value; 535fe6060f1SDimitry Andric const MachineOperand &Orig = MI.getDebugOperand(OrigLocMap[I]); 536fe6060f1SDimitry Andric switch (LocKind) { 537fe6060f1SDimitry Andric case MachineLocKind::RegisterKind: 538e8d8bef9SDimitry Andric // An entry value is a register location -- but with an updated 539fe6060f1SDimitry Andric // expression. The register location of such DBG_VALUE is always the 540fe6060f1SDimitry Andric // one from the entry DBG_VALUE, it does not matter if the entry value 541fe6060f1SDimitry Andric // was copied in to another register due to some optimizations. 542fe6060f1SDimitry Andric // Non-entry value register locations are like the source 543fe6060f1SDimitry Andric // DBG_VALUE, but with the register number from this VarLoc. 544fe6060f1SDimitry Andric MOs.push_back(MachineOperand::CreateReg( 545fe6060f1SDimitry Andric EVKind == EntryValueLocKind::EntryValueKind ? Orig.getReg() 546fe6060f1SDimitry Andric : Register(Loc.RegNo), 547fe6060f1SDimitry Andric false)); 548fe6060f1SDimitry Andric break; 549fe6060f1SDimitry Andric case MachineLocKind::SpillLocKind: { 550e8d8bef9SDimitry Andric // Spills are indirect DBG_VALUEs, with a base register and offset. 551e8d8bef9SDimitry Andric // Use the original DBG_VALUEs expression to build the spilt location 552e8d8bef9SDimitry Andric // on top of. FIXME: spill locations created before this pass runs 553e8d8bef9SDimitry Andric // are not recognized, and not handled here. 554e8d8bef9SDimitry Andric unsigned Base = Loc.SpillLocation.SpillBase; 555fe6060f1SDimitry Andric auto *TRI = MF.getSubtarget().getRegisterInfo(); 556fe6060f1SDimitry Andric if (MI.isNonListDebugValue()) { 557349cc55cSDimitry Andric auto Deref = Indirect ? DIExpression::DerefAfter : 0; 558349cc55cSDimitry Andric DIExpr = TRI->prependOffsetExpression( 559349cc55cSDimitry Andric DIExpr, DIExpression::ApplyOffset | Deref, 560fe6060f1SDimitry Andric Loc.SpillLocation.SpillOffset); 561fe6060f1SDimitry Andric Indirect = true; 562fe6060f1SDimitry Andric } else { 563fe6060f1SDimitry Andric SmallVector<uint64_t, 4> Ops; 564fe6060f1SDimitry Andric TRI->getOffsetOpcodes(Loc.SpillLocation.SpillOffset, Ops); 565fe6060f1SDimitry Andric Ops.push_back(dwarf::DW_OP_deref); 566fe6060f1SDimitry Andric DIExpr = DIExpression::appendOpsToArg(DIExpr, Ops, I); 567e8d8bef9SDimitry Andric } 568fe6060f1SDimitry Andric MOs.push_back(MachineOperand::CreateReg(Base, false)); 569fe6060f1SDimitry Andric break; 570e8d8bef9SDimitry Andric } 571fe6060f1SDimitry Andric case MachineLocKind::ImmediateKind: { 572fe6060f1SDimitry Andric MOs.push_back(Orig); 573fe6060f1SDimitry Andric break; 574e8d8bef9SDimitry Andric } 575fe6060f1SDimitry Andric case MachineLocKind::InvalidKind: 576fe6060f1SDimitry Andric llvm_unreachable("Tried to produce DBG_VALUE for invalid VarLoc"); 577fe6060f1SDimitry Andric } 578fe6060f1SDimitry Andric } 579fe6060f1SDimitry Andric return BuildMI(MF, DbgLoc, IID, Indirect, MOs, Var, DIExpr); 580e8d8bef9SDimitry Andric } 581e8d8bef9SDimitry Andric 582e8d8bef9SDimitry Andric /// Is the Loc field a constant or constant object? 583fe6060f1SDimitry Andric bool isConstant(MachineLocKind Kind) const { 584fe6060f1SDimitry Andric return Kind == MachineLocKind::ImmediateKind; 585fe6060f1SDimitry Andric } 586e8d8bef9SDimitry Andric 587e8d8bef9SDimitry Andric /// Check if the Loc field is an entry backup location. 588e8d8bef9SDimitry Andric bool isEntryBackupLoc() const { 589fe6060f1SDimitry Andric return EVKind == EntryValueLocKind::EntryValueBackupKind || 590fe6060f1SDimitry Andric EVKind == EntryValueLocKind::EntryValueCopyBackupKind; 591e8d8bef9SDimitry Andric } 592e8d8bef9SDimitry Andric 593fe6060f1SDimitry Andric /// If this variable is described by register \p Reg holding the entry 594fe6060f1SDimitry Andric /// value, return true. 595fe6060f1SDimitry Andric bool isEntryValueBackupReg(Register Reg) const { 596fe6060f1SDimitry Andric return EVKind == EntryValueLocKind::EntryValueBackupKind && usesReg(Reg); 597e8d8bef9SDimitry Andric } 598e8d8bef9SDimitry Andric 599fe6060f1SDimitry Andric /// If this variable is described by register \p Reg holding a copy of the 600fe6060f1SDimitry Andric /// entry value, return true. 601fe6060f1SDimitry Andric bool isEntryValueCopyBackupReg(Register Reg) const { 602fe6060f1SDimitry Andric return EVKind == EntryValueLocKind::EntryValueCopyBackupKind && 603fe6060f1SDimitry Andric usesReg(Reg); 604e8d8bef9SDimitry Andric } 605e8d8bef9SDimitry Andric 606fe6060f1SDimitry Andric /// If this variable is described in whole or part by \p Reg, return true. 607fe6060f1SDimitry Andric bool usesReg(Register Reg) const { 608fe6060f1SDimitry Andric MachineLoc RegML; 609fe6060f1SDimitry Andric RegML.Kind = MachineLocKind::RegisterKind; 610fe6060f1SDimitry Andric RegML.Value.RegNo = Reg; 611fe6060f1SDimitry Andric return is_contained(Locs, RegML); 612fe6060f1SDimitry Andric } 613fe6060f1SDimitry Andric 614fe6060f1SDimitry Andric /// If this variable is described in whole or part by \p Reg, return true. 615fe6060f1SDimitry Andric unsigned getRegIdx(Register Reg) const { 616fe6060f1SDimitry Andric for (unsigned Idx = 0; Idx < Locs.size(); ++Idx) 617fe6060f1SDimitry Andric if (Locs[Idx].Kind == MachineLocKind::RegisterKind && 618349cc55cSDimitry Andric Register{static_cast<unsigned>(Locs[Idx].Value.RegNo)} == Reg) 619fe6060f1SDimitry Andric return Idx; 620fe6060f1SDimitry Andric llvm_unreachable("Could not find given Reg in Locs"); 621fe6060f1SDimitry Andric } 622fe6060f1SDimitry Andric 623fe6060f1SDimitry Andric /// If this variable is described in whole or part by 1 or more registers, 624fe6060f1SDimitry Andric /// add each of them to \p Regs and return true. 625fe6060f1SDimitry Andric bool getDescribingRegs(SmallVectorImpl<uint32_t> &Regs) const { 626fe6060f1SDimitry Andric bool AnyRegs = false; 627349cc55cSDimitry Andric for (const auto &Loc : Locs) 628fe6060f1SDimitry Andric if (Loc.Kind == MachineLocKind::RegisterKind) { 629fe6060f1SDimitry Andric Regs.push_back(Loc.Value.RegNo); 630fe6060f1SDimitry Andric AnyRegs = true; 631fe6060f1SDimitry Andric } 632fe6060f1SDimitry Andric return AnyRegs; 633fe6060f1SDimitry Andric } 634fe6060f1SDimitry Andric 635fe6060f1SDimitry Andric bool containsSpillLocs() const { 636fe6060f1SDimitry Andric return any_of(Locs, [](VarLoc::MachineLoc ML) { 637fe6060f1SDimitry Andric return ML.Kind == VarLoc::MachineLocKind::SpillLocKind; 638fe6060f1SDimitry Andric }); 639fe6060f1SDimitry Andric } 640fe6060f1SDimitry Andric 641fe6060f1SDimitry Andric /// If this variable is described in whole or part by \p SpillLocation, 642fe6060f1SDimitry Andric /// return true. 643fe6060f1SDimitry Andric bool usesSpillLoc(SpillLoc SpillLocation) const { 644fe6060f1SDimitry Andric MachineLoc SpillML; 645fe6060f1SDimitry Andric SpillML.Kind = MachineLocKind::SpillLocKind; 646fe6060f1SDimitry Andric SpillML.Value.SpillLocation = SpillLocation; 647fe6060f1SDimitry Andric return is_contained(Locs, SpillML); 648fe6060f1SDimitry Andric } 649fe6060f1SDimitry Andric 650fe6060f1SDimitry Andric /// If this variable is described in whole or part by \p SpillLocation, 651fe6060f1SDimitry Andric /// return the index . 652fe6060f1SDimitry Andric unsigned getSpillLocIdx(SpillLoc SpillLocation) const { 653fe6060f1SDimitry Andric for (unsigned Idx = 0; Idx < Locs.size(); ++Idx) 654fe6060f1SDimitry Andric if (Locs[Idx].Kind == MachineLocKind::SpillLocKind && 655fe6060f1SDimitry Andric Locs[Idx].Value.SpillLocation == SpillLocation) 656fe6060f1SDimitry Andric return Idx; 657fe6060f1SDimitry Andric llvm_unreachable("Could not find given SpillLoc in Locs"); 658e8d8bef9SDimitry Andric } 659e8d8bef9SDimitry Andric 660e8d8bef9SDimitry Andric /// Determine whether the lexical scope of this value's debug location 661e8d8bef9SDimitry Andric /// dominates MBB. 662e8d8bef9SDimitry Andric bool dominates(LexicalScopes &LS, MachineBasicBlock &MBB) const { 663e8d8bef9SDimitry Andric return LS.dominates(MI.getDebugLoc().get(), &MBB); 664e8d8bef9SDimitry Andric } 665e8d8bef9SDimitry Andric 666e8d8bef9SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 667e8d8bef9SDimitry Andric // TRI can be null. 668e8d8bef9SDimitry Andric void dump(const TargetRegisterInfo *TRI, raw_ostream &Out = dbgs()) const { 669e8d8bef9SDimitry Andric Out << "VarLoc("; 670fe6060f1SDimitry Andric for (const MachineLoc &MLoc : Locs) { 671fe6060f1SDimitry Andric if (Locs.begin() != &MLoc) 672fe6060f1SDimitry Andric Out << ", "; 673fe6060f1SDimitry Andric switch (MLoc.Kind) { 674fe6060f1SDimitry Andric case MachineLocKind::RegisterKind: 675fe6060f1SDimitry Andric Out << printReg(MLoc.Value.RegNo, TRI); 676e8d8bef9SDimitry Andric break; 677fe6060f1SDimitry Andric case MachineLocKind::SpillLocKind: 678fe6060f1SDimitry Andric Out << printReg(MLoc.Value.SpillLocation.SpillBase, TRI); 679fe6060f1SDimitry Andric Out << "[" << MLoc.Value.SpillLocation.SpillOffset.getFixed() << " + " 680fe6060f1SDimitry Andric << MLoc.Value.SpillLocation.SpillOffset.getScalable() 681fe6060f1SDimitry Andric << "x vscale" 682e8d8bef9SDimitry Andric << "]"; 683e8d8bef9SDimitry Andric break; 684fe6060f1SDimitry Andric case MachineLocKind::ImmediateKind: 685fe6060f1SDimitry Andric Out << MLoc.Value.Immediate; 686e8d8bef9SDimitry Andric break; 687fe6060f1SDimitry Andric case MachineLocKind::InvalidKind: 688e8d8bef9SDimitry Andric llvm_unreachable("Invalid VarLoc in dump method"); 689e8d8bef9SDimitry Andric } 690fe6060f1SDimitry Andric } 691e8d8bef9SDimitry Andric 692e8d8bef9SDimitry Andric Out << ", \"" << Var.getVariable()->getName() << "\", " << *Expr << ", "; 693e8d8bef9SDimitry Andric if (Var.getInlinedAt()) 694e8d8bef9SDimitry Andric Out << "!" << Var.getInlinedAt()->getMetadataID() << ")\n"; 695e8d8bef9SDimitry Andric else 696e8d8bef9SDimitry Andric Out << "(null))"; 697e8d8bef9SDimitry Andric 698e8d8bef9SDimitry Andric if (isEntryBackupLoc()) 699e8d8bef9SDimitry Andric Out << " (backup loc)\n"; 700e8d8bef9SDimitry Andric else 701e8d8bef9SDimitry Andric Out << "\n"; 702e8d8bef9SDimitry Andric } 703e8d8bef9SDimitry Andric #endif 704e8d8bef9SDimitry Andric 705e8d8bef9SDimitry Andric bool operator==(const VarLoc &Other) const { 706fe6060f1SDimitry Andric return std::tie(EVKind, Var, Expr, Locs) == 707fe6060f1SDimitry Andric std::tie(Other.EVKind, Other.Var, Other.Expr, Other.Locs); 708e8d8bef9SDimitry Andric } 709e8d8bef9SDimitry Andric 710e8d8bef9SDimitry Andric /// This operator guarantees that VarLocs are sorted by Variable first. 711e8d8bef9SDimitry Andric bool operator<(const VarLoc &Other) const { 712fe6060f1SDimitry Andric return std::tie(Var, EVKind, Locs, Expr) < 713fe6060f1SDimitry Andric std::tie(Other.Var, Other.EVKind, Other.Locs, Other.Expr); 714e8d8bef9SDimitry Andric } 715e8d8bef9SDimitry Andric }; 716e8d8bef9SDimitry Andric 717fe6060f1SDimitry Andric #ifndef NDEBUG 718fe6060f1SDimitry Andric using VarVec = SmallVector<VarLoc, 32>; 719fe6060f1SDimitry Andric #endif 720fe6060f1SDimitry Andric 721e8d8bef9SDimitry Andric /// VarLocMap is used for two things: 722fe6060f1SDimitry Andric /// 1) Assigning LocIndices to a VarLoc. The LocIndices can be used to 723e8d8bef9SDimitry Andric /// virtually insert a VarLoc into a VarLocSet. 724e8d8bef9SDimitry Andric /// 2) Given a LocIndex, look up the unique associated VarLoc. 725e8d8bef9SDimitry Andric class VarLocMap { 726e8d8bef9SDimitry Andric /// Map a VarLoc to an index within the vector reserved for its location 727e8d8bef9SDimitry Andric /// within Loc2Vars. 728fe6060f1SDimitry Andric std::map<VarLoc, LocIndices> Var2Indices; 729e8d8bef9SDimitry Andric 730e8d8bef9SDimitry Andric /// Map a location to a vector which holds VarLocs which live in that 731e8d8bef9SDimitry Andric /// location. 732e8d8bef9SDimitry Andric SmallDenseMap<LocIndex::u32_location_t, std::vector<VarLoc>> Loc2Vars; 733e8d8bef9SDimitry Andric 734fe6060f1SDimitry Andric public: 735fe6060f1SDimitry Andric /// Retrieve LocIndices for \p VL. 736fe6060f1SDimitry Andric LocIndices insert(const VarLoc &VL) { 737fe6060f1SDimitry Andric LocIndices &Indices = Var2Indices[VL]; 738fe6060f1SDimitry Andric // If Indices is not empty, VL is already in the map. 739fe6060f1SDimitry Andric if (!Indices.empty()) 740fe6060f1SDimitry Andric return Indices; 741fe6060f1SDimitry Andric SmallVector<LocIndex::u32_location_t, 4> Locations; 742fe6060f1SDimitry Andric // LocIndices are determined by EVKind and MLs; each Register has a 743fe6060f1SDimitry Andric // unique location, while all SpillLocs use a single bucket, and any EV 744fe6060f1SDimitry Andric // VarLocs use only the Backup bucket or none at all (except the 745fe6060f1SDimitry Andric // compulsory entry at the universal location index). LocIndices will 746fe6060f1SDimitry Andric // always have an index at the universal location index as the last index. 747fe6060f1SDimitry Andric if (VL.EVKind == VarLoc::EntryValueLocKind::NonEntryValueKind) { 748fe6060f1SDimitry Andric VL.getDescribingRegs(Locations); 749fe6060f1SDimitry Andric assert(all_of(Locations, 750fe6060f1SDimitry Andric [](auto RegNo) { 751fe6060f1SDimitry Andric return RegNo < LocIndex::kFirstInvalidRegLocation; 752fe6060f1SDimitry Andric }) && 753e8d8bef9SDimitry Andric "Physreg out of range?"); 754fe6060f1SDimitry Andric if (VL.containsSpillLocs()) { 755fe6060f1SDimitry Andric LocIndex::u32_location_t Loc = LocIndex::kSpillLocation; 756fe6060f1SDimitry Andric Locations.push_back(Loc); 757e8d8bef9SDimitry Andric } 758fe6060f1SDimitry Andric } else if (VL.EVKind != VarLoc::EntryValueLocKind::EntryValueKind) { 759fe6060f1SDimitry Andric LocIndex::u32_location_t Loc = LocIndex::kEntryValueBackupLocation; 760fe6060f1SDimitry Andric Locations.push_back(Loc); 761fe6060f1SDimitry Andric } 762fe6060f1SDimitry Andric Locations.push_back(LocIndex::kUniversalLocation); 763fe6060f1SDimitry Andric for (LocIndex::u32_location_t Location : Locations) { 764fe6060f1SDimitry Andric auto &Vars = Loc2Vars[Location]; 765fe6060f1SDimitry Andric Indices.push_back( 766fe6060f1SDimitry Andric {Location, static_cast<LocIndex::u32_index_t>(Vars.size())}); 767fe6060f1SDimitry Andric Vars.push_back(VL); 768fe6060f1SDimitry Andric } 769fe6060f1SDimitry Andric return Indices; 770e8d8bef9SDimitry Andric } 771e8d8bef9SDimitry Andric 772fe6060f1SDimitry Andric LocIndices getAllIndices(const VarLoc &VL) const { 773fe6060f1SDimitry Andric auto IndIt = Var2Indices.find(VL); 774fe6060f1SDimitry Andric assert(IndIt != Var2Indices.end() && "VarLoc not tracked"); 775fe6060f1SDimitry Andric return IndIt->second; 776e8d8bef9SDimitry Andric } 777e8d8bef9SDimitry Andric 778e8d8bef9SDimitry Andric /// Retrieve the unique VarLoc associated with \p ID. 779e8d8bef9SDimitry Andric const VarLoc &operator[](LocIndex ID) const { 780e8d8bef9SDimitry Andric auto LocIt = Loc2Vars.find(ID.Location); 781e8d8bef9SDimitry Andric assert(LocIt != Loc2Vars.end() && "Location not tracked"); 782e8d8bef9SDimitry Andric return LocIt->second[ID.Index]; 783e8d8bef9SDimitry Andric } 784e8d8bef9SDimitry Andric }; 785e8d8bef9SDimitry Andric 786e8d8bef9SDimitry Andric using VarLocInMBB = 787e8d8bef9SDimitry Andric SmallDenseMap<const MachineBasicBlock *, std::unique_ptr<VarLocSet>>; 788e8d8bef9SDimitry Andric struct TransferDebugPair { 789e8d8bef9SDimitry Andric MachineInstr *TransferInst; ///< Instruction where this transfer occurs. 790e8d8bef9SDimitry Andric LocIndex LocationID; ///< Location number for the transfer dest. 791e8d8bef9SDimitry Andric }; 792e8d8bef9SDimitry Andric using TransferMap = SmallVector<TransferDebugPair, 4>; 793349cc55cSDimitry Andric // Types for recording Entry Var Locations emitted by a single MachineInstr, 794349cc55cSDimitry Andric // as well as recording MachineInstr which last defined a register. 795349cc55cSDimitry Andric using InstToEntryLocMap = std::multimap<const MachineInstr *, LocIndex>; 796349cc55cSDimitry Andric using RegDefToInstMap = DenseMap<Register, MachineInstr *>; 797e8d8bef9SDimitry Andric 798e8d8bef9SDimitry Andric // Types for recording sets of variable fragments that overlap. For a given 799e8d8bef9SDimitry Andric // local variable, we record all other fragments of that variable that could 800e8d8bef9SDimitry Andric // overlap it, to reduce search time. 801e8d8bef9SDimitry Andric using FragmentOfVar = 802e8d8bef9SDimitry Andric std::pair<const DILocalVariable *, DIExpression::FragmentInfo>; 803e8d8bef9SDimitry Andric using OverlapMap = 804e8d8bef9SDimitry Andric DenseMap<FragmentOfVar, SmallVector<DIExpression::FragmentInfo, 1>>; 805e8d8bef9SDimitry Andric 806e8d8bef9SDimitry Andric // Helper while building OverlapMap, a map of all fragments seen for a given 807e8d8bef9SDimitry Andric // DILocalVariable. 808e8d8bef9SDimitry Andric using VarToFragments = 809e8d8bef9SDimitry Andric DenseMap<const DILocalVariable *, SmallSet<FragmentInfo, 4>>; 810e8d8bef9SDimitry Andric 811fe6060f1SDimitry Andric /// Collects all VarLocs from \p CollectFrom. Each unique VarLoc is added 812fe6060f1SDimitry Andric /// to \p Collected once, in order of insertion into \p VarLocIDs. 813fe6060f1SDimitry Andric static void collectAllVarLocs(SmallVectorImpl<VarLoc> &Collected, 814fe6060f1SDimitry Andric const VarLocSet &CollectFrom, 815fe6060f1SDimitry Andric const VarLocMap &VarLocIDs); 816fe6060f1SDimitry Andric 817fe6060f1SDimitry Andric /// Get the registers which are used by VarLocs of kind RegisterKind tracked 818fe6060f1SDimitry Andric /// by \p CollectFrom. 819fe6060f1SDimitry Andric void getUsedRegs(const VarLocSet &CollectFrom, 820fe6060f1SDimitry Andric SmallVectorImpl<Register> &UsedRegs) const; 821fe6060f1SDimitry Andric 822e8d8bef9SDimitry Andric /// This holds the working set of currently open ranges. For fast 823e8d8bef9SDimitry Andric /// access, this is done both as a set of VarLocIDs, and a map of 824e8d8bef9SDimitry Andric /// DebugVariable to recent VarLocID. Note that a DBG_VALUE ends all 825e8d8bef9SDimitry Andric /// previous open ranges for the same variable. In addition, we keep 826e8d8bef9SDimitry Andric /// two different maps (Vars/EntryValuesBackupVars), so erase/insert 827e8d8bef9SDimitry Andric /// methods act differently depending on whether a VarLoc is primary 828e8d8bef9SDimitry Andric /// location or backup one. In the case the VarLoc is backup location 829e8d8bef9SDimitry Andric /// we will erase/insert from the EntryValuesBackupVars map, otherwise 830e8d8bef9SDimitry Andric /// we perform the operation on the Vars. 831e8d8bef9SDimitry Andric class OpenRangesSet { 832fe6060f1SDimitry Andric VarLocSet::Allocator &Alloc; 833e8d8bef9SDimitry Andric VarLocSet VarLocs; 834e8d8bef9SDimitry Andric // Map the DebugVariable to recent primary location ID. 835fe6060f1SDimitry Andric SmallDenseMap<DebugVariable, LocIndices, 8> Vars; 836e8d8bef9SDimitry Andric // Map the DebugVariable to recent backup location ID. 837fe6060f1SDimitry Andric SmallDenseMap<DebugVariable, LocIndices, 8> EntryValuesBackupVars; 838e8d8bef9SDimitry Andric OverlapMap &OverlappingFragments; 839e8d8bef9SDimitry Andric 840e8d8bef9SDimitry Andric public: 841e8d8bef9SDimitry Andric OpenRangesSet(VarLocSet::Allocator &Alloc, OverlapMap &_OLapMap) 842fe6060f1SDimitry Andric : Alloc(Alloc), VarLocs(Alloc), OverlappingFragments(_OLapMap) {} 843e8d8bef9SDimitry Andric 844e8d8bef9SDimitry Andric const VarLocSet &getVarLocs() const { return VarLocs; } 845e8d8bef9SDimitry Andric 846fe6060f1SDimitry Andric // Fetches all VarLocs in \p VarLocIDs and inserts them into \p Collected. 847fe6060f1SDimitry Andric // This method is needed to get every VarLoc once, as each VarLoc may have 848fe6060f1SDimitry Andric // multiple indices in a VarLocMap (corresponding to each applicable 849fe6060f1SDimitry Andric // location), but all VarLocs appear exactly once at the universal location 850fe6060f1SDimitry Andric // index. 851fe6060f1SDimitry Andric void getUniqueVarLocs(SmallVectorImpl<VarLoc> &Collected, 852fe6060f1SDimitry Andric const VarLocMap &VarLocIDs) const { 853fe6060f1SDimitry Andric collectAllVarLocs(Collected, VarLocs, VarLocIDs); 854fe6060f1SDimitry Andric } 855fe6060f1SDimitry Andric 856e8d8bef9SDimitry Andric /// Terminate all open ranges for VL.Var by removing it from the set. 857e8d8bef9SDimitry Andric void erase(const VarLoc &VL); 858e8d8bef9SDimitry Andric 859fe6060f1SDimitry Andric /// Terminate all open ranges listed as indices in \c KillSet with 860fe6060f1SDimitry Andric /// \c Location by removing them from the set. 861fe6060f1SDimitry Andric void erase(const VarLocsInRange &KillSet, const VarLocMap &VarLocIDs, 862fe6060f1SDimitry Andric LocIndex::u32_location_t Location); 863e8d8bef9SDimitry Andric 864e8d8bef9SDimitry Andric /// Insert a new range into the set. 865fe6060f1SDimitry Andric void insert(LocIndices VarLocIDs, const VarLoc &VL); 866e8d8bef9SDimitry Andric 867e8d8bef9SDimitry Andric /// Insert a set of ranges. 868fe6060f1SDimitry Andric void insertFromLocSet(const VarLocSet &ToLoad, const VarLocMap &Map); 869e8d8bef9SDimitry Andric 870fe6060f1SDimitry Andric llvm::Optional<LocIndices> getEntryValueBackup(DebugVariable Var); 871e8d8bef9SDimitry Andric 872e8d8bef9SDimitry Andric /// Empty the set. 873e8d8bef9SDimitry Andric void clear() { 874e8d8bef9SDimitry Andric VarLocs.clear(); 875e8d8bef9SDimitry Andric Vars.clear(); 876e8d8bef9SDimitry Andric EntryValuesBackupVars.clear(); 877e8d8bef9SDimitry Andric } 878e8d8bef9SDimitry Andric 879e8d8bef9SDimitry Andric /// Return whether the set is empty or not. 880e8d8bef9SDimitry Andric bool empty() const { 881e8d8bef9SDimitry Andric assert(Vars.empty() == EntryValuesBackupVars.empty() && 882e8d8bef9SDimitry Andric Vars.empty() == VarLocs.empty() && 883e8d8bef9SDimitry Andric "open ranges are inconsistent"); 884e8d8bef9SDimitry Andric return VarLocs.empty(); 885e8d8bef9SDimitry Andric } 886e8d8bef9SDimitry Andric 887e8d8bef9SDimitry Andric /// Get an empty range of VarLoc IDs. 888e8d8bef9SDimitry Andric auto getEmptyVarLocRange() const { 889e8d8bef9SDimitry Andric return iterator_range<VarLocSet::const_iterator>(getVarLocs().end(), 890e8d8bef9SDimitry Andric getVarLocs().end()); 891e8d8bef9SDimitry Andric } 892e8d8bef9SDimitry Andric 893fe6060f1SDimitry Andric /// Get all set IDs for VarLocs with MLs of kind RegisterKind in \p Reg. 894e8d8bef9SDimitry Andric auto getRegisterVarLocs(Register Reg) const { 895e8d8bef9SDimitry Andric return LocIndex::indexRangeForLocation(getVarLocs(), Reg); 896e8d8bef9SDimitry Andric } 897e8d8bef9SDimitry Andric 898fe6060f1SDimitry Andric /// Get all set IDs for VarLocs with MLs of kind SpillLocKind. 899e8d8bef9SDimitry Andric auto getSpillVarLocs() const { 900e8d8bef9SDimitry Andric return LocIndex::indexRangeForLocation(getVarLocs(), 901e8d8bef9SDimitry Andric LocIndex::kSpillLocation); 902e8d8bef9SDimitry Andric } 903e8d8bef9SDimitry Andric 904fe6060f1SDimitry Andric /// Get all set IDs for VarLocs of EVKind EntryValueBackupKind or 905e8d8bef9SDimitry Andric /// EntryValueCopyBackupKind. 906e8d8bef9SDimitry Andric auto getEntryValueBackupVarLocs() const { 907e8d8bef9SDimitry Andric return LocIndex::indexRangeForLocation( 908e8d8bef9SDimitry Andric getVarLocs(), LocIndex::kEntryValueBackupLocation); 909e8d8bef9SDimitry Andric } 910e8d8bef9SDimitry Andric }; 911e8d8bef9SDimitry Andric 912fe6060f1SDimitry Andric /// Collect all VarLoc IDs from \p CollectFrom for VarLocs with MLs of kind 913fe6060f1SDimitry Andric /// RegisterKind which are located in any reg in \p Regs. The IDs for each 914fe6060f1SDimitry Andric /// VarLoc correspond to entries in the universal location bucket, which every 915fe6060f1SDimitry Andric /// VarLoc has exactly 1 entry for. Insert collected IDs into \p Collected. 916fe6060f1SDimitry Andric static void collectIDsForRegs(VarLocsInRange &Collected, 917fe6060f1SDimitry Andric const DefinedRegsSet &Regs, 918fe6060f1SDimitry Andric const VarLocSet &CollectFrom, 919fe6060f1SDimitry Andric const VarLocMap &VarLocIDs); 920e8d8bef9SDimitry Andric 921e8d8bef9SDimitry Andric VarLocSet &getVarLocsInMBB(const MachineBasicBlock *MBB, VarLocInMBB &Locs) { 922e8d8bef9SDimitry Andric std::unique_ptr<VarLocSet> &VLS = Locs[MBB]; 923e8d8bef9SDimitry Andric if (!VLS) 924e8d8bef9SDimitry Andric VLS = std::make_unique<VarLocSet>(Alloc); 925e8d8bef9SDimitry Andric return *VLS.get(); 926e8d8bef9SDimitry Andric } 927e8d8bef9SDimitry Andric 928e8d8bef9SDimitry Andric const VarLocSet &getVarLocsInMBB(const MachineBasicBlock *MBB, 929e8d8bef9SDimitry Andric const VarLocInMBB &Locs) const { 930e8d8bef9SDimitry Andric auto It = Locs.find(MBB); 931e8d8bef9SDimitry Andric assert(It != Locs.end() && "MBB not in map"); 932e8d8bef9SDimitry Andric return *It->second.get(); 933e8d8bef9SDimitry Andric } 934e8d8bef9SDimitry Andric 935e8d8bef9SDimitry Andric /// Tests whether this instruction is a spill to a stack location. 936e8d8bef9SDimitry Andric bool isSpillInstruction(const MachineInstr &MI, MachineFunction *MF); 937e8d8bef9SDimitry Andric 938e8d8bef9SDimitry Andric /// Decide if @MI is a spill instruction and return true if it is. We use 2 939e8d8bef9SDimitry Andric /// criteria to make this decision: 940e8d8bef9SDimitry Andric /// - Is this instruction a store to a spill slot? 941e8d8bef9SDimitry Andric /// - Is there a register operand that is both used and killed? 942e8d8bef9SDimitry Andric /// TODO: Store optimization can fold spills into other stores (including 943e8d8bef9SDimitry Andric /// other spills). We do not handle this yet (more than one memory operand). 944e8d8bef9SDimitry Andric bool isLocationSpill(const MachineInstr &MI, MachineFunction *MF, 945e8d8bef9SDimitry Andric Register &Reg); 946e8d8bef9SDimitry Andric 947e8d8bef9SDimitry Andric /// Returns true if the given machine instruction is a debug value which we 948e8d8bef9SDimitry Andric /// can emit entry values for. 949e8d8bef9SDimitry Andric /// 950e8d8bef9SDimitry Andric /// Currently, we generate debug entry values only for parameters that are 951e8d8bef9SDimitry Andric /// unmodified throughout the function and located in a register. 952e8d8bef9SDimitry Andric bool isEntryValueCandidate(const MachineInstr &MI, 953e8d8bef9SDimitry Andric const DefinedRegsSet &Regs) const; 954e8d8bef9SDimitry Andric 955e8d8bef9SDimitry Andric /// If a given instruction is identified as a spill, return the spill location 956e8d8bef9SDimitry Andric /// and set \p Reg to the spilled register. 957e8d8bef9SDimitry Andric Optional<VarLoc::SpillLoc> isRestoreInstruction(const MachineInstr &MI, 958e8d8bef9SDimitry Andric MachineFunction *MF, 959e8d8bef9SDimitry Andric Register &Reg); 960e8d8bef9SDimitry Andric /// Given a spill instruction, extract the register and offset used to 961e8d8bef9SDimitry Andric /// address the spill location in a target independent way. 962e8d8bef9SDimitry Andric VarLoc::SpillLoc extractSpillBaseRegAndOffset(const MachineInstr &MI); 963e8d8bef9SDimitry Andric void insertTransferDebugPair(MachineInstr &MI, OpenRangesSet &OpenRanges, 964e8d8bef9SDimitry Andric TransferMap &Transfers, VarLocMap &VarLocIDs, 965e8d8bef9SDimitry Andric LocIndex OldVarID, TransferKind Kind, 966fe6060f1SDimitry Andric const VarLoc::MachineLoc &OldLoc, 967e8d8bef9SDimitry Andric Register NewReg = Register()); 968e8d8bef9SDimitry Andric 969e8d8bef9SDimitry Andric void transferDebugValue(const MachineInstr &MI, OpenRangesSet &OpenRanges, 970349cc55cSDimitry Andric VarLocMap &VarLocIDs, 971349cc55cSDimitry Andric InstToEntryLocMap &EntryValTransfers, 972349cc55cSDimitry Andric RegDefToInstMap &RegSetInstrs); 973e8d8bef9SDimitry Andric void transferSpillOrRestoreInst(MachineInstr &MI, OpenRangesSet &OpenRanges, 974e8d8bef9SDimitry Andric VarLocMap &VarLocIDs, TransferMap &Transfers); 975349cc55cSDimitry Andric void cleanupEntryValueTransfers(const MachineInstr *MI, 976349cc55cSDimitry Andric OpenRangesSet &OpenRanges, 977349cc55cSDimitry Andric VarLocMap &VarLocIDs, const VarLoc &EntryVL, 978349cc55cSDimitry Andric InstToEntryLocMap &EntryValTransfers); 979349cc55cSDimitry Andric void removeEntryValue(const MachineInstr &MI, OpenRangesSet &OpenRanges, 980349cc55cSDimitry Andric VarLocMap &VarLocIDs, const VarLoc &EntryVL, 981349cc55cSDimitry Andric InstToEntryLocMap &EntryValTransfers, 982349cc55cSDimitry Andric RegDefToInstMap &RegSetInstrs); 983e8d8bef9SDimitry Andric void emitEntryValues(MachineInstr &MI, OpenRangesSet &OpenRanges, 984349cc55cSDimitry Andric VarLocMap &VarLocIDs, 985349cc55cSDimitry Andric InstToEntryLocMap &EntryValTransfers, 986fe6060f1SDimitry Andric VarLocsInRange &KillSet); 987e8d8bef9SDimitry Andric void recordEntryValue(const MachineInstr &MI, 988e8d8bef9SDimitry Andric const DefinedRegsSet &DefinedRegs, 989e8d8bef9SDimitry Andric OpenRangesSet &OpenRanges, VarLocMap &VarLocIDs); 990e8d8bef9SDimitry Andric void transferRegisterCopy(MachineInstr &MI, OpenRangesSet &OpenRanges, 991e8d8bef9SDimitry Andric VarLocMap &VarLocIDs, TransferMap &Transfers); 992e8d8bef9SDimitry Andric void transferRegisterDef(MachineInstr &MI, OpenRangesSet &OpenRanges, 993349cc55cSDimitry Andric VarLocMap &VarLocIDs, 994349cc55cSDimitry Andric InstToEntryLocMap &EntryValTransfers, 995349cc55cSDimitry Andric RegDefToInstMap &RegSetInstrs); 996e8d8bef9SDimitry Andric bool transferTerminator(MachineBasicBlock *MBB, OpenRangesSet &OpenRanges, 997e8d8bef9SDimitry Andric VarLocInMBB &OutLocs, const VarLocMap &VarLocIDs); 998e8d8bef9SDimitry Andric 999e8d8bef9SDimitry Andric void process(MachineInstr &MI, OpenRangesSet &OpenRanges, 1000349cc55cSDimitry Andric VarLocMap &VarLocIDs, TransferMap &Transfers, 1001349cc55cSDimitry Andric InstToEntryLocMap &EntryValTransfers, 1002349cc55cSDimitry Andric RegDefToInstMap &RegSetInstrs); 1003e8d8bef9SDimitry Andric 1004e8d8bef9SDimitry Andric void accumulateFragmentMap(MachineInstr &MI, VarToFragments &SeenFragments, 1005e8d8bef9SDimitry Andric OverlapMap &OLapMap); 1006e8d8bef9SDimitry Andric 1007e8d8bef9SDimitry Andric bool join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs, VarLocInMBB &InLocs, 1008e8d8bef9SDimitry Andric const VarLocMap &VarLocIDs, 1009e8d8bef9SDimitry Andric SmallPtrSet<const MachineBasicBlock *, 16> &Visited, 1010e8d8bef9SDimitry Andric SmallPtrSetImpl<const MachineBasicBlock *> &ArtificialBlocks); 1011e8d8bef9SDimitry Andric 1012e8d8bef9SDimitry Andric /// Create DBG_VALUE insts for inlocs that have been propagated but 1013e8d8bef9SDimitry Andric /// had their instruction creation deferred. 1014e8d8bef9SDimitry Andric void flushPendingLocs(VarLocInMBB &PendingInLocs, VarLocMap &VarLocIDs); 1015e8d8bef9SDimitry Andric 1016349cc55cSDimitry Andric bool ExtendRanges(MachineFunction &MF, MachineDominatorTree *DomTree, 1017349cc55cSDimitry Andric TargetPassConfig *TPC, unsigned InputBBLimit, 1018349cc55cSDimitry Andric unsigned InputDbgValLimit) override; 1019e8d8bef9SDimitry Andric 1020e8d8bef9SDimitry Andric public: 1021e8d8bef9SDimitry Andric /// Default construct and initialize the pass. 1022e8d8bef9SDimitry Andric VarLocBasedLDV(); 1023e8d8bef9SDimitry Andric 1024e8d8bef9SDimitry Andric ~VarLocBasedLDV(); 1025e8d8bef9SDimitry Andric 1026e8d8bef9SDimitry Andric /// Print to ostream with a message. 1027e8d8bef9SDimitry Andric void printVarLocInMBB(const MachineFunction &MF, const VarLocInMBB &V, 1028e8d8bef9SDimitry Andric const VarLocMap &VarLocIDs, const char *msg, 1029e8d8bef9SDimitry Andric raw_ostream &Out) const; 1030e8d8bef9SDimitry Andric }; 1031e8d8bef9SDimitry Andric 1032e8d8bef9SDimitry Andric } // end anonymous namespace 1033e8d8bef9SDimitry Andric 1034e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 1035e8d8bef9SDimitry Andric // Implementation 1036e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 1037e8d8bef9SDimitry Andric 1038e8d8bef9SDimitry Andric VarLocBasedLDV::VarLocBasedLDV() { } 1039e8d8bef9SDimitry Andric 1040e8d8bef9SDimitry Andric VarLocBasedLDV::~VarLocBasedLDV() { } 1041e8d8bef9SDimitry Andric 1042e8d8bef9SDimitry Andric /// Erase a variable from the set of open ranges, and additionally erase any 1043e8d8bef9SDimitry Andric /// fragments that may overlap it. If the VarLoc is a backup location, erase 1044e8d8bef9SDimitry Andric /// the variable from the EntryValuesBackupVars set, indicating we should stop 1045e8d8bef9SDimitry Andric /// tracking its backup entry location. Otherwise, if the VarLoc is primary 1046e8d8bef9SDimitry Andric /// location, erase the variable from the Vars set. 1047e8d8bef9SDimitry Andric void VarLocBasedLDV::OpenRangesSet::erase(const VarLoc &VL) { 1048e8d8bef9SDimitry Andric // Erasure helper. 1049e8d8bef9SDimitry Andric auto DoErase = [VL, this](DebugVariable VarToErase) { 1050e8d8bef9SDimitry Andric auto *EraseFrom = VL.isEntryBackupLoc() ? &EntryValuesBackupVars : &Vars; 1051e8d8bef9SDimitry Andric auto It = EraseFrom->find(VarToErase); 1052e8d8bef9SDimitry Andric if (It != EraseFrom->end()) { 1053fe6060f1SDimitry Andric LocIndices IDs = It->second; 1054fe6060f1SDimitry Andric for (LocIndex ID : IDs) 1055e8d8bef9SDimitry Andric VarLocs.reset(ID.getAsRawInteger()); 1056e8d8bef9SDimitry Andric EraseFrom->erase(It); 1057e8d8bef9SDimitry Andric } 1058e8d8bef9SDimitry Andric }; 1059e8d8bef9SDimitry Andric 1060e8d8bef9SDimitry Andric DebugVariable Var = VL.Var; 1061e8d8bef9SDimitry Andric 1062e8d8bef9SDimitry Andric // Erase the variable/fragment that ends here. 1063e8d8bef9SDimitry Andric DoErase(Var); 1064e8d8bef9SDimitry Andric 1065e8d8bef9SDimitry Andric // Extract the fragment. Interpret an empty fragment as one that covers all 1066e8d8bef9SDimitry Andric // possible bits. 1067e8d8bef9SDimitry Andric FragmentInfo ThisFragment = Var.getFragmentOrDefault(); 1068e8d8bef9SDimitry Andric 1069e8d8bef9SDimitry Andric // There may be fragments that overlap the designated fragment. Look them up 1070e8d8bef9SDimitry Andric // in the pre-computed overlap map, and erase them too. 1071e8d8bef9SDimitry Andric auto MapIt = OverlappingFragments.find({Var.getVariable(), ThisFragment}); 1072e8d8bef9SDimitry Andric if (MapIt != OverlappingFragments.end()) { 1073e8d8bef9SDimitry Andric for (auto Fragment : MapIt->second) { 1074e8d8bef9SDimitry Andric VarLocBasedLDV::OptFragmentInfo FragmentHolder; 1075e8d8bef9SDimitry Andric if (!DebugVariable::isDefaultFragment(Fragment)) 1076e8d8bef9SDimitry Andric FragmentHolder = VarLocBasedLDV::OptFragmentInfo(Fragment); 1077e8d8bef9SDimitry Andric DoErase({Var.getVariable(), FragmentHolder, Var.getInlinedAt()}); 1078e8d8bef9SDimitry Andric } 1079e8d8bef9SDimitry Andric } 1080e8d8bef9SDimitry Andric } 1081e8d8bef9SDimitry Andric 1082fe6060f1SDimitry Andric void VarLocBasedLDV::OpenRangesSet::erase(const VarLocsInRange &KillSet, 1083fe6060f1SDimitry Andric const VarLocMap &VarLocIDs, 1084fe6060f1SDimitry Andric LocIndex::u32_location_t Location) { 1085fe6060f1SDimitry Andric VarLocSet RemoveSet(Alloc); 1086fe6060f1SDimitry Andric for (LocIndex::u32_index_t ID : KillSet) { 1087fe6060f1SDimitry Andric const VarLoc &VL = VarLocIDs[LocIndex(Location, ID)]; 1088fe6060f1SDimitry Andric auto *EraseFrom = VL.isEntryBackupLoc() ? &EntryValuesBackupVars : &Vars; 1089fe6060f1SDimitry Andric EraseFrom->erase(VL.Var); 1090fe6060f1SDimitry Andric LocIndices VLI = VarLocIDs.getAllIndices(VL); 1091fe6060f1SDimitry Andric for (LocIndex ID : VLI) 1092fe6060f1SDimitry Andric RemoveSet.set(ID.getAsRawInteger()); 1093fe6060f1SDimitry Andric } 1094fe6060f1SDimitry Andric VarLocs.intersectWithComplement(RemoveSet); 1095fe6060f1SDimitry Andric } 1096fe6060f1SDimitry Andric 1097fe6060f1SDimitry Andric void VarLocBasedLDV::OpenRangesSet::insertFromLocSet(const VarLocSet &ToLoad, 1098fe6060f1SDimitry Andric const VarLocMap &Map) { 1099fe6060f1SDimitry Andric VarLocsInRange UniqueVarLocIDs; 1100fe6060f1SDimitry Andric DefinedRegsSet Regs; 1101fe6060f1SDimitry Andric Regs.insert(LocIndex::kUniversalLocation); 1102fe6060f1SDimitry Andric collectIDsForRegs(UniqueVarLocIDs, Regs, ToLoad, Map); 1103fe6060f1SDimitry Andric for (uint64_t ID : UniqueVarLocIDs) { 1104fe6060f1SDimitry Andric LocIndex Idx = LocIndex::fromRawInteger(ID); 1105fe6060f1SDimitry Andric const VarLoc &VarL = Map[Idx]; 1106fe6060f1SDimitry Andric const LocIndices Indices = Map.getAllIndices(VarL); 1107fe6060f1SDimitry Andric insert(Indices, VarL); 1108e8d8bef9SDimitry Andric } 1109e8d8bef9SDimitry Andric } 1110e8d8bef9SDimitry Andric 1111fe6060f1SDimitry Andric void VarLocBasedLDV::OpenRangesSet::insert(LocIndices VarLocIDs, 1112e8d8bef9SDimitry Andric const VarLoc &VL) { 1113e8d8bef9SDimitry Andric auto *InsertInto = VL.isEntryBackupLoc() ? &EntryValuesBackupVars : &Vars; 1114fe6060f1SDimitry Andric for (LocIndex ID : VarLocIDs) 1115fe6060f1SDimitry Andric VarLocs.set(ID.getAsRawInteger()); 1116fe6060f1SDimitry Andric InsertInto->insert({VL.Var, VarLocIDs}); 1117e8d8bef9SDimitry Andric } 1118e8d8bef9SDimitry Andric 1119e8d8bef9SDimitry Andric /// Return the Loc ID of an entry value backup location, if it exists for the 1120e8d8bef9SDimitry Andric /// variable. 1121fe6060f1SDimitry Andric llvm::Optional<LocIndices> 1122e8d8bef9SDimitry Andric VarLocBasedLDV::OpenRangesSet::getEntryValueBackup(DebugVariable Var) { 1123e8d8bef9SDimitry Andric auto It = EntryValuesBackupVars.find(Var); 1124e8d8bef9SDimitry Andric if (It != EntryValuesBackupVars.end()) 1125e8d8bef9SDimitry Andric return It->second; 1126e8d8bef9SDimitry Andric 1127e8d8bef9SDimitry Andric return llvm::None; 1128e8d8bef9SDimitry Andric } 1129e8d8bef9SDimitry Andric 1130fe6060f1SDimitry Andric void VarLocBasedLDV::collectIDsForRegs(VarLocsInRange &Collected, 1131e8d8bef9SDimitry Andric const DefinedRegsSet &Regs, 1132fe6060f1SDimitry Andric const VarLocSet &CollectFrom, 1133fe6060f1SDimitry Andric const VarLocMap &VarLocIDs) { 1134e8d8bef9SDimitry Andric assert(!Regs.empty() && "Nothing to collect"); 1135fe6060f1SDimitry Andric SmallVector<Register, 32> SortedRegs; 1136fe6060f1SDimitry Andric append_range(SortedRegs, Regs); 1137e8d8bef9SDimitry Andric array_pod_sort(SortedRegs.begin(), SortedRegs.end()); 1138e8d8bef9SDimitry Andric auto It = CollectFrom.find(LocIndex::rawIndexForReg(SortedRegs.front())); 1139e8d8bef9SDimitry Andric auto End = CollectFrom.end(); 1140fe6060f1SDimitry Andric for (Register Reg : SortedRegs) { 1141fe6060f1SDimitry Andric // The half-open interval [FirstIndexForReg, FirstInvalidIndex) contains 1142fe6060f1SDimitry Andric // all possible VarLoc IDs for VarLocs with MLs of kind RegisterKind which 1143fe6060f1SDimitry Andric // live in Reg. 1144e8d8bef9SDimitry Andric uint64_t FirstIndexForReg = LocIndex::rawIndexForReg(Reg); 1145e8d8bef9SDimitry Andric uint64_t FirstInvalidIndex = LocIndex::rawIndexForReg(Reg + 1); 1146e8d8bef9SDimitry Andric It.advanceToLowerBound(FirstIndexForReg); 1147e8d8bef9SDimitry Andric 1148e8d8bef9SDimitry Andric // Iterate through that half-open interval and collect all the set IDs. 1149fe6060f1SDimitry Andric for (; It != End && *It < FirstInvalidIndex; ++It) { 1150fe6060f1SDimitry Andric LocIndex ItIdx = LocIndex::fromRawInteger(*It); 1151fe6060f1SDimitry Andric const VarLoc &VL = VarLocIDs[ItIdx]; 1152fe6060f1SDimitry Andric LocIndices LI = VarLocIDs.getAllIndices(VL); 1153fe6060f1SDimitry Andric // For now, the back index is always the universal location index. 1154fe6060f1SDimitry Andric assert(LI.back().Location == LocIndex::kUniversalLocation && 1155fe6060f1SDimitry Andric "Unexpected order of LocIndices for VarLoc; was it inserted into " 1156fe6060f1SDimitry Andric "the VarLocMap correctly?"); 1157fe6060f1SDimitry Andric Collected.insert(LI.back().Index); 1158fe6060f1SDimitry Andric } 1159e8d8bef9SDimitry Andric 1160e8d8bef9SDimitry Andric if (It == End) 1161e8d8bef9SDimitry Andric return; 1162e8d8bef9SDimitry Andric } 1163e8d8bef9SDimitry Andric } 1164e8d8bef9SDimitry Andric 1165e8d8bef9SDimitry Andric void VarLocBasedLDV::getUsedRegs(const VarLocSet &CollectFrom, 1166fe6060f1SDimitry Andric SmallVectorImpl<Register> &UsedRegs) const { 1167e8d8bef9SDimitry Andric // All register-based VarLocs are assigned indices greater than or equal to 1168e8d8bef9SDimitry Andric // FirstRegIndex. 1169fe6060f1SDimitry Andric uint64_t FirstRegIndex = 1170fe6060f1SDimitry Andric LocIndex::rawIndexForReg(LocIndex::kFirstRegLocation); 1171e8d8bef9SDimitry Andric uint64_t FirstInvalidIndex = 1172e8d8bef9SDimitry Andric LocIndex::rawIndexForReg(LocIndex::kFirstInvalidRegLocation); 1173e8d8bef9SDimitry Andric for (auto It = CollectFrom.find(FirstRegIndex), 1174e8d8bef9SDimitry Andric End = CollectFrom.find(FirstInvalidIndex); 1175e8d8bef9SDimitry Andric It != End;) { 1176e8d8bef9SDimitry Andric // We found a VarLoc ID for a VarLoc that lives in a register. Figure out 1177e8d8bef9SDimitry Andric // which register and add it to UsedRegs. 1178e8d8bef9SDimitry Andric uint32_t FoundReg = LocIndex::fromRawInteger(*It).Location; 1179e8d8bef9SDimitry Andric assert((UsedRegs.empty() || FoundReg != UsedRegs.back()) && 1180e8d8bef9SDimitry Andric "Duplicate used reg"); 1181e8d8bef9SDimitry Andric UsedRegs.push_back(FoundReg); 1182e8d8bef9SDimitry Andric 1183e8d8bef9SDimitry Andric // Skip to the next /set/ register. Note that this finds a lower bound, so 1184e8d8bef9SDimitry Andric // even if there aren't any VarLocs living in `FoundReg+1`, we're still 1185e8d8bef9SDimitry Andric // guaranteed to move on to the next register (or to end()). 1186e8d8bef9SDimitry Andric uint64_t NextRegIndex = LocIndex::rawIndexForReg(FoundReg + 1); 1187e8d8bef9SDimitry Andric It.advanceToLowerBound(NextRegIndex); 1188e8d8bef9SDimitry Andric } 1189e8d8bef9SDimitry Andric } 1190e8d8bef9SDimitry Andric 1191e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 1192e8d8bef9SDimitry Andric // Debug Range Extension Implementation 1193e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 1194e8d8bef9SDimitry Andric 1195e8d8bef9SDimitry Andric #ifndef NDEBUG 1196e8d8bef9SDimitry Andric void VarLocBasedLDV::printVarLocInMBB(const MachineFunction &MF, 1197e8d8bef9SDimitry Andric const VarLocInMBB &V, 1198e8d8bef9SDimitry Andric const VarLocMap &VarLocIDs, 1199e8d8bef9SDimitry Andric const char *msg, 1200e8d8bef9SDimitry Andric raw_ostream &Out) const { 1201e8d8bef9SDimitry Andric Out << '\n' << msg << '\n'; 1202e8d8bef9SDimitry Andric for (const MachineBasicBlock &BB : MF) { 1203e8d8bef9SDimitry Andric if (!V.count(&BB)) 1204e8d8bef9SDimitry Andric continue; 1205e8d8bef9SDimitry Andric const VarLocSet &L = getVarLocsInMBB(&BB, V); 1206e8d8bef9SDimitry Andric if (L.empty()) 1207e8d8bef9SDimitry Andric continue; 1208fe6060f1SDimitry Andric SmallVector<VarLoc, 32> VarLocs; 1209fe6060f1SDimitry Andric collectAllVarLocs(VarLocs, L, VarLocIDs); 1210e8d8bef9SDimitry Andric Out << "MBB: " << BB.getNumber() << ":\n"; 1211fe6060f1SDimitry Andric for (const VarLoc &VL : VarLocs) { 1212e8d8bef9SDimitry Andric Out << " Var: " << VL.Var.getVariable()->getName(); 1213e8d8bef9SDimitry Andric Out << " MI: "; 1214e8d8bef9SDimitry Andric VL.dump(TRI, Out); 1215e8d8bef9SDimitry Andric } 1216e8d8bef9SDimitry Andric } 1217e8d8bef9SDimitry Andric Out << "\n"; 1218e8d8bef9SDimitry Andric } 1219e8d8bef9SDimitry Andric #endif 1220e8d8bef9SDimitry Andric 1221e8d8bef9SDimitry Andric VarLocBasedLDV::VarLoc::SpillLoc 1222e8d8bef9SDimitry Andric VarLocBasedLDV::extractSpillBaseRegAndOffset(const MachineInstr &MI) { 1223e8d8bef9SDimitry Andric assert(MI.hasOneMemOperand() && 1224e8d8bef9SDimitry Andric "Spill instruction does not have exactly one memory operand?"); 1225e8d8bef9SDimitry Andric auto MMOI = MI.memoperands_begin(); 1226e8d8bef9SDimitry Andric const PseudoSourceValue *PVal = (*MMOI)->getPseudoValue(); 1227e8d8bef9SDimitry Andric assert(PVal->kind() == PseudoSourceValue::FixedStack && 1228e8d8bef9SDimitry Andric "Inconsistent memory operand in spill instruction"); 1229e8d8bef9SDimitry Andric int FI = cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex(); 1230e8d8bef9SDimitry Andric const MachineBasicBlock *MBB = MI.getParent(); 1231e8d8bef9SDimitry Andric Register Reg; 1232e8d8bef9SDimitry Andric StackOffset Offset = TFI->getFrameIndexReference(*MBB->getParent(), FI, Reg); 1233e8d8bef9SDimitry Andric return {Reg, Offset}; 1234e8d8bef9SDimitry Andric } 1235e8d8bef9SDimitry Andric 1236349cc55cSDimitry Andric /// Do cleanup of \p EntryValTransfers created by \p TRInst, by removing the 1237349cc55cSDimitry Andric /// Transfer, which uses the to-be-deleted \p EntryVL. 1238349cc55cSDimitry Andric void VarLocBasedLDV::cleanupEntryValueTransfers( 1239349cc55cSDimitry Andric const MachineInstr *TRInst, OpenRangesSet &OpenRanges, VarLocMap &VarLocIDs, 1240349cc55cSDimitry Andric const VarLoc &EntryVL, InstToEntryLocMap &EntryValTransfers) { 1241349cc55cSDimitry Andric if (EntryValTransfers.empty() || TRInst == nullptr) 1242349cc55cSDimitry Andric return; 1243349cc55cSDimitry Andric 1244349cc55cSDimitry Andric auto TransRange = EntryValTransfers.equal_range(TRInst); 1245349cc55cSDimitry Andric for (auto TDPair : llvm::make_range(TransRange.first, TransRange.second)) { 1246349cc55cSDimitry Andric const VarLoc &EmittedEV = VarLocIDs[TDPair.second]; 1247349cc55cSDimitry Andric if (std::tie(EntryVL.Var, EntryVL.Locs[0].Value.RegNo, EntryVL.Expr) == 1248349cc55cSDimitry Andric std::tie(EmittedEV.Var, EmittedEV.Locs[0].Value.RegNo, 1249349cc55cSDimitry Andric EmittedEV.Expr)) { 1250349cc55cSDimitry Andric OpenRanges.erase(EmittedEV); 1251349cc55cSDimitry Andric EntryValTransfers.erase(TRInst); 1252349cc55cSDimitry Andric break; 1253349cc55cSDimitry Andric } 1254349cc55cSDimitry Andric } 1255349cc55cSDimitry Andric } 1256349cc55cSDimitry Andric 1257e8d8bef9SDimitry Andric /// Try to salvage the debug entry value if we encounter a new debug value 1258e8d8bef9SDimitry Andric /// describing the same parameter, otherwise stop tracking the value. Return 1259349cc55cSDimitry Andric /// true if we should stop tracking the entry value and do the cleanup of 1260349cc55cSDimitry Andric /// emitted Entry Value Transfers, otherwise return false. 1261349cc55cSDimitry Andric void VarLocBasedLDV::removeEntryValue(const MachineInstr &MI, 1262e8d8bef9SDimitry Andric OpenRangesSet &OpenRanges, 1263e8d8bef9SDimitry Andric VarLocMap &VarLocIDs, 1264349cc55cSDimitry Andric const VarLoc &EntryVL, 1265349cc55cSDimitry Andric InstToEntryLocMap &EntryValTransfers, 1266349cc55cSDimitry Andric RegDefToInstMap &RegSetInstrs) { 1267e8d8bef9SDimitry Andric // Skip the DBG_VALUE which is the debug entry value itself. 1268349cc55cSDimitry Andric if (&MI == &EntryVL.MI) 1269349cc55cSDimitry Andric return; 1270e8d8bef9SDimitry Andric 1271e8d8bef9SDimitry Andric // If the parameter's location is not register location, we can not track 1272349cc55cSDimitry Andric // the entry value any more. It doesn't have the TransferInst which defines 1273349cc55cSDimitry Andric // register, so no Entry Value Transfers have been emitted already. 1274349cc55cSDimitry Andric if (!MI.getDebugOperand(0).isReg()) 1275349cc55cSDimitry Andric return; 1276e8d8bef9SDimitry Andric 1277349cc55cSDimitry Andric // Try to get non-debug instruction responsible for the DBG_VALUE. 1278349cc55cSDimitry Andric const MachineInstr *TransferInst = nullptr; 1279e8d8bef9SDimitry Andric Register Reg = MI.getDebugOperand(0).getReg(); 1280349cc55cSDimitry Andric if (Reg.isValid() && RegSetInstrs.find(Reg) != RegSetInstrs.end()) 1281349cc55cSDimitry Andric TransferInst = RegSetInstrs.find(Reg)->second; 1282fe6060f1SDimitry Andric 1283349cc55cSDimitry Andric // Case of the parameter's DBG_VALUE at the start of entry MBB. 1284349cc55cSDimitry Andric if (!TransferInst && !LastNonDbgMI && MI.getParent()->isEntryBlock()) 1285349cc55cSDimitry Andric return; 1286349cc55cSDimitry Andric 1287349cc55cSDimitry Andric // If the debug expression from the DBG_VALUE is not empty, we can assume the 1288349cc55cSDimitry Andric // parameter's value has changed indicating that we should stop tracking its 1289349cc55cSDimitry Andric // entry value as well. 1290349cc55cSDimitry Andric if (MI.getDebugExpression()->getNumElements() == 0 && TransferInst) { 1291349cc55cSDimitry Andric // If the DBG_VALUE comes from a copy instruction that copies the entry 1292349cc55cSDimitry Andric // value, it means the parameter's value has not changed and we should be 1293349cc55cSDimitry Andric // able to use its entry value. 1294e8d8bef9SDimitry Andric // TODO: Try to keep tracking of an entry value if we encounter a propagated 1295e8d8bef9SDimitry Andric // DBG_VALUE describing the copy of the entry value. (Propagated entry value 1296e8d8bef9SDimitry Andric // does not indicate the parameter modification.) 1297349cc55cSDimitry Andric auto DestSrc = TII->isCopyInstr(*TransferInst); 1298349cc55cSDimitry Andric if (DestSrc) { 1299349cc55cSDimitry Andric const MachineOperand *SrcRegOp, *DestRegOp; 1300e8d8bef9SDimitry Andric SrcRegOp = DestSrc->Source; 1301e8d8bef9SDimitry Andric DestRegOp = DestSrc->Destination; 1302349cc55cSDimitry Andric if (Reg == DestRegOp->getReg()) { 1303e8d8bef9SDimitry Andric for (uint64_t ID : OpenRanges.getEntryValueBackupVarLocs()) { 1304e8d8bef9SDimitry Andric const VarLoc &VL = VarLocIDs[LocIndex::fromRawInteger(ID)]; 1305fe6060f1SDimitry Andric if (VL.isEntryValueCopyBackupReg(Reg) && 1306fe6060f1SDimitry Andric // Entry Values should not be variadic. 1307e8d8bef9SDimitry Andric VL.MI.getDebugOperand(0).getReg() == SrcRegOp->getReg()) 1308349cc55cSDimitry Andric return; 1309349cc55cSDimitry Andric } 1310349cc55cSDimitry Andric } 1311e8d8bef9SDimitry Andric } 1312e8d8bef9SDimitry Andric } 1313e8d8bef9SDimitry Andric 1314349cc55cSDimitry Andric LLVM_DEBUG(dbgs() << "Deleting a DBG entry value because of: "; 1315349cc55cSDimitry Andric MI.print(dbgs(), /*IsStandalone*/ false, 1316349cc55cSDimitry Andric /*SkipOpers*/ false, /*SkipDebugLoc*/ false, 1317349cc55cSDimitry Andric /*AddNewLine*/ true, TII)); 1318349cc55cSDimitry Andric cleanupEntryValueTransfers(TransferInst, OpenRanges, VarLocIDs, EntryVL, 1319349cc55cSDimitry Andric EntryValTransfers); 1320349cc55cSDimitry Andric OpenRanges.erase(EntryVL); 1321e8d8bef9SDimitry Andric } 1322e8d8bef9SDimitry Andric 1323e8d8bef9SDimitry Andric /// End all previous ranges related to @MI and start a new range from @MI 1324e8d8bef9SDimitry Andric /// if it is a DBG_VALUE instr. 1325e8d8bef9SDimitry Andric void VarLocBasedLDV::transferDebugValue(const MachineInstr &MI, 1326e8d8bef9SDimitry Andric OpenRangesSet &OpenRanges, 1327349cc55cSDimitry Andric VarLocMap &VarLocIDs, 1328349cc55cSDimitry Andric InstToEntryLocMap &EntryValTransfers, 1329349cc55cSDimitry Andric RegDefToInstMap &RegSetInstrs) { 1330e8d8bef9SDimitry Andric if (!MI.isDebugValue()) 1331e8d8bef9SDimitry Andric return; 1332e8d8bef9SDimitry Andric const DILocalVariable *Var = MI.getDebugVariable(); 1333e8d8bef9SDimitry Andric const DIExpression *Expr = MI.getDebugExpression(); 1334e8d8bef9SDimitry Andric const DILocation *DebugLoc = MI.getDebugLoc(); 1335e8d8bef9SDimitry Andric const DILocation *InlinedAt = DebugLoc->getInlinedAt(); 1336e8d8bef9SDimitry Andric assert(Var->isValidLocationForIntrinsic(DebugLoc) && 1337e8d8bef9SDimitry Andric "Expected inlined-at fields to agree"); 1338e8d8bef9SDimitry Andric 1339e8d8bef9SDimitry Andric DebugVariable V(Var, Expr, InlinedAt); 1340e8d8bef9SDimitry Andric 1341e8d8bef9SDimitry Andric // Check if this DBG_VALUE indicates a parameter's value changing. 1342e8d8bef9SDimitry Andric // If that is the case, we should stop tracking its entry value. 1343e8d8bef9SDimitry Andric auto EntryValBackupID = OpenRanges.getEntryValueBackup(V); 1344e8d8bef9SDimitry Andric if (Var->isParameter() && EntryValBackupID) { 1345fe6060f1SDimitry Andric const VarLoc &EntryVL = VarLocIDs[EntryValBackupID->back()]; 1346349cc55cSDimitry Andric removeEntryValue(MI, OpenRanges, VarLocIDs, EntryVL, EntryValTransfers, 1347349cc55cSDimitry Andric RegSetInstrs); 1348e8d8bef9SDimitry Andric } 1349e8d8bef9SDimitry Andric 1350fe6060f1SDimitry Andric if (all_of(MI.debug_operands(), [](const MachineOperand &MO) { 1351fe6060f1SDimitry Andric return (MO.isReg() && MO.getReg()) || MO.isImm() || MO.isFPImm() || 1352fe6060f1SDimitry Andric MO.isCImm(); 1353fe6060f1SDimitry Andric })) { 1354e8d8bef9SDimitry Andric // Use normal VarLoc constructor for registers and immediates. 1355e8d8bef9SDimitry Andric VarLoc VL(MI, LS); 1356e8d8bef9SDimitry Andric // End all previous ranges of VL.Var. 1357e8d8bef9SDimitry Andric OpenRanges.erase(VL); 1358e8d8bef9SDimitry Andric 1359fe6060f1SDimitry Andric LocIndices IDs = VarLocIDs.insert(VL); 1360e8d8bef9SDimitry Andric // Add the VarLoc to OpenRanges from this DBG_VALUE. 1361fe6060f1SDimitry Andric OpenRanges.insert(IDs, VL); 1362fe6060f1SDimitry Andric } else if (MI.memoperands().size() > 0) { 1363e8d8bef9SDimitry Andric llvm_unreachable("DBG_VALUE with mem operand encountered after regalloc?"); 1364e8d8bef9SDimitry Andric } else { 1365e8d8bef9SDimitry Andric // This must be an undefined location. If it has an open range, erase it. 1366fe6060f1SDimitry Andric assert(MI.isUndefDebugValue() && 1367e8d8bef9SDimitry Andric "Unexpected non-undef DBG_VALUE encountered"); 1368e8d8bef9SDimitry Andric VarLoc VL(MI, LS); 1369e8d8bef9SDimitry Andric OpenRanges.erase(VL); 1370e8d8bef9SDimitry Andric } 1371e8d8bef9SDimitry Andric } 1372e8d8bef9SDimitry Andric 1373fe6060f1SDimitry Andric // This should be removed later, doesn't fit the new design. 1374fe6060f1SDimitry Andric void VarLocBasedLDV::collectAllVarLocs(SmallVectorImpl<VarLoc> &Collected, 1375fe6060f1SDimitry Andric const VarLocSet &CollectFrom, 1376fe6060f1SDimitry Andric const VarLocMap &VarLocIDs) { 1377fe6060f1SDimitry Andric // The half-open interval [FirstIndexForReg, FirstInvalidIndex) contains all 1378fe6060f1SDimitry Andric // possible VarLoc IDs for VarLocs with MLs of kind RegisterKind which live 1379fe6060f1SDimitry Andric // in Reg. 1380fe6060f1SDimitry Andric uint64_t FirstIndex = LocIndex::rawIndexForReg(LocIndex::kUniversalLocation); 1381fe6060f1SDimitry Andric uint64_t FirstInvalidIndex = 1382fe6060f1SDimitry Andric LocIndex::rawIndexForReg(LocIndex::kUniversalLocation + 1); 1383fe6060f1SDimitry Andric // Iterate through that half-open interval and collect all the set IDs. 1384fe6060f1SDimitry Andric for (auto It = CollectFrom.find(FirstIndex), End = CollectFrom.end(); 1385fe6060f1SDimitry Andric It != End && *It < FirstInvalidIndex; ++It) { 1386fe6060f1SDimitry Andric LocIndex RegIdx = LocIndex::fromRawInteger(*It); 1387fe6060f1SDimitry Andric Collected.push_back(VarLocIDs[RegIdx]); 1388fe6060f1SDimitry Andric } 1389fe6060f1SDimitry Andric } 1390fe6060f1SDimitry Andric 1391e8d8bef9SDimitry Andric /// Turn the entry value backup locations into primary locations. 1392e8d8bef9SDimitry Andric void VarLocBasedLDV::emitEntryValues(MachineInstr &MI, 1393e8d8bef9SDimitry Andric OpenRangesSet &OpenRanges, 1394e8d8bef9SDimitry Andric VarLocMap &VarLocIDs, 1395349cc55cSDimitry Andric InstToEntryLocMap &EntryValTransfers, 1396fe6060f1SDimitry Andric VarLocsInRange &KillSet) { 1397e8d8bef9SDimitry Andric // Do not insert entry value locations after a terminator. 1398e8d8bef9SDimitry Andric if (MI.isTerminator()) 1399e8d8bef9SDimitry Andric return; 1400e8d8bef9SDimitry Andric 1401fe6060f1SDimitry Andric for (uint32_t ID : KillSet) { 1402fe6060f1SDimitry Andric // The KillSet IDs are indices for the universal location bucket. 1403fe6060f1SDimitry Andric LocIndex Idx = LocIndex(LocIndex::kUniversalLocation, ID); 1404e8d8bef9SDimitry Andric const VarLoc &VL = VarLocIDs[Idx]; 1405e8d8bef9SDimitry Andric if (!VL.Var.getVariable()->isParameter()) 1406e8d8bef9SDimitry Andric continue; 1407e8d8bef9SDimitry Andric 1408e8d8bef9SDimitry Andric auto DebugVar = VL.Var; 1409fe6060f1SDimitry Andric Optional<LocIndices> EntryValBackupIDs = 1410e8d8bef9SDimitry Andric OpenRanges.getEntryValueBackup(DebugVar); 1411e8d8bef9SDimitry Andric 1412e8d8bef9SDimitry Andric // If the parameter has the entry value backup, it means we should 1413e8d8bef9SDimitry Andric // be able to use its entry value. 1414fe6060f1SDimitry Andric if (!EntryValBackupIDs) 1415e8d8bef9SDimitry Andric continue; 1416e8d8bef9SDimitry Andric 1417fe6060f1SDimitry Andric const VarLoc &EntryVL = VarLocIDs[EntryValBackupIDs->back()]; 1418fe6060f1SDimitry Andric VarLoc EntryLoc = VarLoc::CreateEntryLoc(EntryVL.MI, LS, EntryVL.Expr, 1419fe6060f1SDimitry Andric EntryVL.Locs[0].Value.RegNo); 1420fe6060f1SDimitry Andric LocIndices EntryValueIDs = VarLocIDs.insert(EntryLoc); 1421349cc55cSDimitry Andric assert(EntryValueIDs.size() == 1 && 1422349cc55cSDimitry Andric "EntryValue loc should not be variadic"); 1423349cc55cSDimitry Andric EntryValTransfers.insert({&MI, EntryValueIDs.back()}); 1424fe6060f1SDimitry Andric OpenRanges.insert(EntryValueIDs, EntryLoc); 1425e8d8bef9SDimitry Andric } 1426e8d8bef9SDimitry Andric } 1427e8d8bef9SDimitry Andric 1428e8d8bef9SDimitry Andric /// Create new TransferDebugPair and insert it in \p Transfers. The VarLoc 1429e8d8bef9SDimitry Andric /// with \p OldVarID should be deleted form \p OpenRanges and replaced with 1430e8d8bef9SDimitry Andric /// new VarLoc. If \p NewReg is different than default zero value then the 1431e8d8bef9SDimitry Andric /// new location will be register location created by the copy like instruction, 1432e8d8bef9SDimitry Andric /// otherwise it is variable's location on the stack. 1433e8d8bef9SDimitry Andric void VarLocBasedLDV::insertTransferDebugPair( 1434e8d8bef9SDimitry Andric MachineInstr &MI, OpenRangesSet &OpenRanges, TransferMap &Transfers, 1435e8d8bef9SDimitry Andric VarLocMap &VarLocIDs, LocIndex OldVarID, TransferKind Kind, 1436fe6060f1SDimitry Andric const VarLoc::MachineLoc &OldLoc, Register NewReg) { 1437fe6060f1SDimitry Andric const VarLoc &OldVarLoc = VarLocIDs[OldVarID]; 1438e8d8bef9SDimitry Andric 1439e8d8bef9SDimitry Andric auto ProcessVarLoc = [&MI, &OpenRanges, &Transfers, &VarLocIDs](VarLoc &VL) { 1440fe6060f1SDimitry Andric LocIndices LocIds = VarLocIDs.insert(VL); 1441e8d8bef9SDimitry Andric 1442e8d8bef9SDimitry Andric // Close this variable's previous location range. 1443e8d8bef9SDimitry Andric OpenRanges.erase(VL); 1444e8d8bef9SDimitry Andric 1445e8d8bef9SDimitry Andric // Record the new location as an open range, and a postponed transfer 1446e8d8bef9SDimitry Andric // inserting a DBG_VALUE for this location. 1447fe6060f1SDimitry Andric OpenRanges.insert(LocIds, VL); 1448e8d8bef9SDimitry Andric assert(!MI.isTerminator() && "Cannot insert DBG_VALUE after terminator"); 1449fe6060f1SDimitry Andric TransferDebugPair MIP = {&MI, LocIds.back()}; 1450e8d8bef9SDimitry Andric Transfers.push_back(MIP); 1451e8d8bef9SDimitry Andric }; 1452e8d8bef9SDimitry Andric 1453e8d8bef9SDimitry Andric // End all previous ranges of VL.Var. 1454e8d8bef9SDimitry Andric OpenRanges.erase(VarLocIDs[OldVarID]); 1455e8d8bef9SDimitry Andric switch (Kind) { 1456e8d8bef9SDimitry Andric case TransferKind::TransferCopy: { 1457e8d8bef9SDimitry Andric assert(NewReg && 1458e8d8bef9SDimitry Andric "No register supplied when handling a copy of a debug value"); 1459e8d8bef9SDimitry Andric // Create a DBG_VALUE instruction to describe the Var in its new 1460e8d8bef9SDimitry Andric // register location. 1461fe6060f1SDimitry Andric VarLoc VL = VarLoc::CreateCopyLoc(OldVarLoc, OldLoc, NewReg); 1462e8d8bef9SDimitry Andric ProcessVarLoc(VL); 1463e8d8bef9SDimitry Andric LLVM_DEBUG({ 1464e8d8bef9SDimitry Andric dbgs() << "Creating VarLoc for register copy:"; 1465e8d8bef9SDimitry Andric VL.dump(TRI); 1466e8d8bef9SDimitry Andric }); 1467e8d8bef9SDimitry Andric return; 1468e8d8bef9SDimitry Andric } 1469e8d8bef9SDimitry Andric case TransferKind::TransferSpill: { 1470e8d8bef9SDimitry Andric // Create a DBG_VALUE instruction to describe the Var in its spilled 1471e8d8bef9SDimitry Andric // location. 1472e8d8bef9SDimitry Andric VarLoc::SpillLoc SpillLocation = extractSpillBaseRegAndOffset(MI); 1473fe6060f1SDimitry Andric VarLoc VL = VarLoc::CreateSpillLoc( 1474fe6060f1SDimitry Andric OldVarLoc, OldLoc, SpillLocation.SpillBase, SpillLocation.SpillOffset); 1475e8d8bef9SDimitry Andric ProcessVarLoc(VL); 1476e8d8bef9SDimitry Andric LLVM_DEBUG({ 1477e8d8bef9SDimitry Andric dbgs() << "Creating VarLoc for spill:"; 1478e8d8bef9SDimitry Andric VL.dump(TRI); 1479e8d8bef9SDimitry Andric }); 1480e8d8bef9SDimitry Andric return; 1481e8d8bef9SDimitry Andric } 1482e8d8bef9SDimitry Andric case TransferKind::TransferRestore: { 1483e8d8bef9SDimitry Andric assert(NewReg && 1484e8d8bef9SDimitry Andric "No register supplied when handling a restore of a debug value"); 1485e8d8bef9SDimitry Andric // DebugInstr refers to the pre-spill location, therefore we can reuse 1486e8d8bef9SDimitry Andric // its expression. 1487fe6060f1SDimitry Andric VarLoc VL = VarLoc::CreateCopyLoc(OldVarLoc, OldLoc, NewReg); 1488e8d8bef9SDimitry Andric ProcessVarLoc(VL); 1489e8d8bef9SDimitry Andric LLVM_DEBUG({ 1490e8d8bef9SDimitry Andric dbgs() << "Creating VarLoc for restore:"; 1491e8d8bef9SDimitry Andric VL.dump(TRI); 1492e8d8bef9SDimitry Andric }); 1493e8d8bef9SDimitry Andric return; 1494e8d8bef9SDimitry Andric } 1495e8d8bef9SDimitry Andric } 1496e8d8bef9SDimitry Andric llvm_unreachable("Invalid transfer kind"); 1497e8d8bef9SDimitry Andric } 1498e8d8bef9SDimitry Andric 1499e8d8bef9SDimitry Andric /// A definition of a register may mark the end of a range. 1500349cc55cSDimitry Andric void VarLocBasedLDV::transferRegisterDef(MachineInstr &MI, 1501349cc55cSDimitry Andric OpenRangesSet &OpenRanges, 1502349cc55cSDimitry Andric VarLocMap &VarLocIDs, 1503349cc55cSDimitry Andric InstToEntryLocMap &EntryValTransfers, 1504349cc55cSDimitry Andric RegDefToInstMap &RegSetInstrs) { 1505e8d8bef9SDimitry Andric 1506e8d8bef9SDimitry Andric // Meta Instructions do not affect the debug liveness of any register they 1507e8d8bef9SDimitry Andric // define. 1508e8d8bef9SDimitry Andric if (MI.isMetaInstruction()) 1509e8d8bef9SDimitry Andric return; 1510e8d8bef9SDimitry Andric 1511e8d8bef9SDimitry Andric MachineFunction *MF = MI.getMF(); 1512e8d8bef9SDimitry Andric const TargetLowering *TLI = MF->getSubtarget().getTargetLowering(); 1513e8d8bef9SDimitry Andric Register SP = TLI->getStackPointerRegisterToSaveRestore(); 1514e8d8bef9SDimitry Andric 1515e8d8bef9SDimitry Andric // Find the regs killed by MI, and find regmasks of preserved regs. 1516e8d8bef9SDimitry Andric DefinedRegsSet DeadRegs; 1517e8d8bef9SDimitry Andric SmallVector<const uint32_t *, 4> RegMasks; 1518e8d8bef9SDimitry Andric for (const MachineOperand &MO : MI.operands()) { 1519e8d8bef9SDimitry Andric // Determine whether the operand is a register def. 1520e8d8bef9SDimitry Andric if (MO.isReg() && MO.isDef() && MO.getReg() && 1521e8d8bef9SDimitry Andric Register::isPhysicalRegister(MO.getReg()) && 1522e8d8bef9SDimitry Andric !(MI.isCall() && MO.getReg() == SP)) { 1523e8d8bef9SDimitry Andric // Remove ranges of all aliased registers. 1524e8d8bef9SDimitry Andric for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI) 1525e8d8bef9SDimitry Andric // FIXME: Can we break out of this loop early if no insertion occurs? 1526e8d8bef9SDimitry Andric DeadRegs.insert(*RAI); 1527349cc55cSDimitry Andric RegSetInstrs.erase(MO.getReg()); 1528349cc55cSDimitry Andric RegSetInstrs.insert({MO.getReg(), &MI}); 1529e8d8bef9SDimitry Andric } else if (MO.isRegMask()) { 1530e8d8bef9SDimitry Andric RegMasks.push_back(MO.getRegMask()); 1531e8d8bef9SDimitry Andric } 1532e8d8bef9SDimitry Andric } 1533e8d8bef9SDimitry Andric 1534e8d8bef9SDimitry Andric // Erase VarLocs which reside in one of the dead registers. For performance 1535e8d8bef9SDimitry Andric // reasons, it's critical to not iterate over the full set of open VarLocs. 1536e8d8bef9SDimitry Andric // Iterate over the set of dying/used regs instead. 1537e8d8bef9SDimitry Andric if (!RegMasks.empty()) { 1538fe6060f1SDimitry Andric SmallVector<Register, 32> UsedRegs; 1539e8d8bef9SDimitry Andric getUsedRegs(OpenRanges.getVarLocs(), UsedRegs); 1540fe6060f1SDimitry Andric for (Register Reg : UsedRegs) { 1541e8d8bef9SDimitry Andric // Remove ranges of all clobbered registers. Register masks don't usually 1542e8d8bef9SDimitry Andric // list SP as preserved. Assume that call instructions never clobber SP, 1543e8d8bef9SDimitry Andric // because some backends (e.g., AArch64) never list SP in the regmask. 1544e8d8bef9SDimitry Andric // While the debug info may be off for an instruction or two around 1545e8d8bef9SDimitry Andric // callee-cleanup calls, transferring the DEBUG_VALUE across the call is 1546e8d8bef9SDimitry Andric // still a better user experience. 1547e8d8bef9SDimitry Andric if (Reg == SP) 1548e8d8bef9SDimitry Andric continue; 1549e8d8bef9SDimitry Andric bool AnyRegMaskKillsReg = 1550e8d8bef9SDimitry Andric any_of(RegMasks, [Reg](const uint32_t *RegMask) { 1551e8d8bef9SDimitry Andric return MachineOperand::clobbersPhysReg(RegMask, Reg); 1552e8d8bef9SDimitry Andric }); 1553e8d8bef9SDimitry Andric if (AnyRegMaskKillsReg) 1554e8d8bef9SDimitry Andric DeadRegs.insert(Reg); 1555349cc55cSDimitry Andric if (AnyRegMaskKillsReg) { 1556349cc55cSDimitry Andric RegSetInstrs.erase(Reg); 1557349cc55cSDimitry Andric RegSetInstrs.insert({Reg, &MI}); 1558349cc55cSDimitry Andric } 1559e8d8bef9SDimitry Andric } 1560e8d8bef9SDimitry Andric } 1561e8d8bef9SDimitry Andric 1562e8d8bef9SDimitry Andric if (DeadRegs.empty()) 1563e8d8bef9SDimitry Andric return; 1564e8d8bef9SDimitry Andric 1565fe6060f1SDimitry Andric VarLocsInRange KillSet; 1566fe6060f1SDimitry Andric collectIDsForRegs(KillSet, DeadRegs, OpenRanges.getVarLocs(), VarLocIDs); 1567fe6060f1SDimitry Andric OpenRanges.erase(KillSet, VarLocIDs, LocIndex::kUniversalLocation); 1568e8d8bef9SDimitry Andric 1569e8d8bef9SDimitry Andric if (TPC) { 1570e8d8bef9SDimitry Andric auto &TM = TPC->getTM<TargetMachine>(); 1571e8d8bef9SDimitry Andric if (TM.Options.ShouldEmitDebugEntryValues()) 1572349cc55cSDimitry Andric emitEntryValues(MI, OpenRanges, VarLocIDs, EntryValTransfers, KillSet); 1573e8d8bef9SDimitry Andric } 1574e8d8bef9SDimitry Andric } 1575e8d8bef9SDimitry Andric 1576e8d8bef9SDimitry Andric bool VarLocBasedLDV::isSpillInstruction(const MachineInstr &MI, 1577e8d8bef9SDimitry Andric MachineFunction *MF) { 1578e8d8bef9SDimitry Andric // TODO: Handle multiple stores folded into one. 1579e8d8bef9SDimitry Andric if (!MI.hasOneMemOperand()) 1580e8d8bef9SDimitry Andric return false; 1581e8d8bef9SDimitry Andric 1582e8d8bef9SDimitry Andric if (!MI.getSpillSize(TII) && !MI.getFoldedSpillSize(TII)) 1583e8d8bef9SDimitry Andric return false; // This is not a spill instruction, since no valid size was 1584e8d8bef9SDimitry Andric // returned from either function. 1585e8d8bef9SDimitry Andric 1586e8d8bef9SDimitry Andric return true; 1587e8d8bef9SDimitry Andric } 1588e8d8bef9SDimitry Andric 1589e8d8bef9SDimitry Andric bool VarLocBasedLDV::isLocationSpill(const MachineInstr &MI, 1590e8d8bef9SDimitry Andric MachineFunction *MF, Register &Reg) { 1591e8d8bef9SDimitry Andric if (!isSpillInstruction(MI, MF)) 1592e8d8bef9SDimitry Andric return false; 1593e8d8bef9SDimitry Andric 1594e8d8bef9SDimitry Andric auto isKilledReg = [&](const MachineOperand MO, Register &Reg) { 1595e8d8bef9SDimitry Andric if (!MO.isReg() || !MO.isUse()) { 1596e8d8bef9SDimitry Andric Reg = 0; 1597e8d8bef9SDimitry Andric return false; 1598e8d8bef9SDimitry Andric } 1599e8d8bef9SDimitry Andric Reg = MO.getReg(); 1600e8d8bef9SDimitry Andric return MO.isKill(); 1601e8d8bef9SDimitry Andric }; 1602e8d8bef9SDimitry Andric 1603e8d8bef9SDimitry Andric for (const MachineOperand &MO : MI.operands()) { 1604e8d8bef9SDimitry Andric // In a spill instruction generated by the InlineSpiller the spilled 1605e8d8bef9SDimitry Andric // register has its kill flag set. 1606e8d8bef9SDimitry Andric if (isKilledReg(MO, Reg)) 1607e8d8bef9SDimitry Andric return true; 1608e8d8bef9SDimitry Andric if (Reg != 0) { 1609e8d8bef9SDimitry Andric // Check whether next instruction kills the spilled register. 1610e8d8bef9SDimitry Andric // FIXME: Current solution does not cover search for killed register in 1611e8d8bef9SDimitry Andric // bundles and instructions further down the chain. 1612e8d8bef9SDimitry Andric auto NextI = std::next(MI.getIterator()); 1613e8d8bef9SDimitry Andric // Skip next instruction that points to basic block end iterator. 1614e8d8bef9SDimitry Andric if (MI.getParent()->end() == NextI) 1615e8d8bef9SDimitry Andric continue; 1616e8d8bef9SDimitry Andric Register RegNext; 1617e8d8bef9SDimitry Andric for (const MachineOperand &MONext : NextI->operands()) { 1618e8d8bef9SDimitry Andric // Return true if we came across the register from the 1619e8d8bef9SDimitry Andric // previous spill instruction that is killed in NextI. 1620e8d8bef9SDimitry Andric if (isKilledReg(MONext, RegNext) && RegNext == Reg) 1621e8d8bef9SDimitry Andric return true; 1622e8d8bef9SDimitry Andric } 1623e8d8bef9SDimitry Andric } 1624e8d8bef9SDimitry Andric } 1625e8d8bef9SDimitry Andric // Return false if we didn't find spilled register. 1626e8d8bef9SDimitry Andric return false; 1627e8d8bef9SDimitry Andric } 1628e8d8bef9SDimitry Andric 1629e8d8bef9SDimitry Andric Optional<VarLocBasedLDV::VarLoc::SpillLoc> 1630e8d8bef9SDimitry Andric VarLocBasedLDV::isRestoreInstruction(const MachineInstr &MI, 1631e8d8bef9SDimitry Andric MachineFunction *MF, Register &Reg) { 1632e8d8bef9SDimitry Andric if (!MI.hasOneMemOperand()) 1633e8d8bef9SDimitry Andric return None; 1634e8d8bef9SDimitry Andric 1635e8d8bef9SDimitry Andric // FIXME: Handle folded restore instructions with more than one memory 1636e8d8bef9SDimitry Andric // operand. 1637e8d8bef9SDimitry Andric if (MI.getRestoreSize(TII)) { 1638e8d8bef9SDimitry Andric Reg = MI.getOperand(0).getReg(); 1639e8d8bef9SDimitry Andric return extractSpillBaseRegAndOffset(MI); 1640e8d8bef9SDimitry Andric } 1641e8d8bef9SDimitry Andric return None; 1642e8d8bef9SDimitry Andric } 1643e8d8bef9SDimitry Andric 1644e8d8bef9SDimitry Andric /// A spilled register may indicate that we have to end the current range of 1645e8d8bef9SDimitry Andric /// a variable and create a new one for the spill location. 1646e8d8bef9SDimitry Andric /// A restored register may indicate the reverse situation. 1647e8d8bef9SDimitry Andric /// We don't want to insert any instructions in process(), so we just create 1648e8d8bef9SDimitry Andric /// the DBG_VALUE without inserting it and keep track of it in \p Transfers. 1649e8d8bef9SDimitry Andric /// It will be inserted into the BB when we're done iterating over the 1650e8d8bef9SDimitry Andric /// instructions. 1651e8d8bef9SDimitry Andric void VarLocBasedLDV::transferSpillOrRestoreInst(MachineInstr &MI, 1652e8d8bef9SDimitry Andric OpenRangesSet &OpenRanges, 1653e8d8bef9SDimitry Andric VarLocMap &VarLocIDs, 1654e8d8bef9SDimitry Andric TransferMap &Transfers) { 1655e8d8bef9SDimitry Andric MachineFunction *MF = MI.getMF(); 1656e8d8bef9SDimitry Andric TransferKind TKind; 1657e8d8bef9SDimitry Andric Register Reg; 1658e8d8bef9SDimitry Andric Optional<VarLoc::SpillLoc> Loc; 1659e8d8bef9SDimitry Andric 1660e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Examining instruction: "; MI.dump();); 1661e8d8bef9SDimitry Andric 1662e8d8bef9SDimitry Andric // First, if there are any DBG_VALUEs pointing at a spill slot that is 1663e8d8bef9SDimitry Andric // written to, then close the variable location. The value in memory 1664e8d8bef9SDimitry Andric // will have changed. 1665fe6060f1SDimitry Andric VarLocsInRange KillSet; 1666e8d8bef9SDimitry Andric if (isSpillInstruction(MI, MF)) { 1667e8d8bef9SDimitry Andric Loc = extractSpillBaseRegAndOffset(MI); 1668e8d8bef9SDimitry Andric for (uint64_t ID : OpenRanges.getSpillVarLocs()) { 1669e8d8bef9SDimitry Andric LocIndex Idx = LocIndex::fromRawInteger(ID); 1670e8d8bef9SDimitry Andric const VarLoc &VL = VarLocIDs[Idx]; 1671fe6060f1SDimitry Andric assert(VL.containsSpillLocs() && "Broken VarLocSet?"); 1672fe6060f1SDimitry Andric if (VL.usesSpillLoc(*Loc)) { 1673e8d8bef9SDimitry Andric // This location is overwritten by the current instruction -- terminate 1674e8d8bef9SDimitry Andric // the open range, and insert an explicit DBG_VALUE $noreg. 1675e8d8bef9SDimitry Andric // 1676e8d8bef9SDimitry Andric // Doing this at a later stage would require re-interpreting all 1677e8d8bef9SDimitry Andric // DBG_VALUes and DIExpressions to identify whether they point at 1678e8d8bef9SDimitry Andric // memory, and then analysing all memory writes to see if they 1679e8d8bef9SDimitry Andric // overwrite that memory, which is expensive. 1680e8d8bef9SDimitry Andric // 1681e8d8bef9SDimitry Andric // At this stage, we already know which DBG_VALUEs are for spills and 1682e8d8bef9SDimitry Andric // where they are located; it's best to fix handle overwrites now. 1683fe6060f1SDimitry Andric KillSet.insert(ID); 1684fe6060f1SDimitry Andric unsigned SpillLocIdx = VL.getSpillLocIdx(*Loc); 1685fe6060f1SDimitry Andric VarLoc::MachineLoc OldLoc = VL.Locs[SpillLocIdx]; 1686fe6060f1SDimitry Andric VarLoc UndefVL = VarLoc::CreateCopyLoc(VL, OldLoc, 0); 1687fe6060f1SDimitry Andric LocIndices UndefLocIDs = VarLocIDs.insert(UndefVL); 1688fe6060f1SDimitry Andric Transfers.push_back({&MI, UndefLocIDs.back()}); 1689e8d8bef9SDimitry Andric } 1690e8d8bef9SDimitry Andric } 1691fe6060f1SDimitry Andric OpenRanges.erase(KillSet, VarLocIDs, LocIndex::kSpillLocation); 1692e8d8bef9SDimitry Andric } 1693e8d8bef9SDimitry Andric 1694e8d8bef9SDimitry Andric // Try to recognise spill and restore instructions that may create a new 1695e8d8bef9SDimitry Andric // variable location. 1696e8d8bef9SDimitry Andric if (isLocationSpill(MI, MF, Reg)) { 1697e8d8bef9SDimitry Andric TKind = TransferKind::TransferSpill; 1698e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Recognized as spill: "; MI.dump();); 1699e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Register: " << Reg << " " << printReg(Reg, TRI) 1700e8d8bef9SDimitry Andric << "\n"); 1701e8d8bef9SDimitry Andric } else { 1702e8d8bef9SDimitry Andric if (!(Loc = isRestoreInstruction(MI, MF, Reg))) 1703e8d8bef9SDimitry Andric return; 1704e8d8bef9SDimitry Andric TKind = TransferKind::TransferRestore; 1705e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Recognized as restore: "; MI.dump();); 1706e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Register: " << Reg << " " << printReg(Reg, TRI) 1707e8d8bef9SDimitry Andric << "\n"); 1708e8d8bef9SDimitry Andric } 1709e8d8bef9SDimitry Andric // Check if the register or spill location is the location of a debug value. 1710e8d8bef9SDimitry Andric auto TransferCandidates = OpenRanges.getEmptyVarLocRange(); 1711e8d8bef9SDimitry Andric if (TKind == TransferKind::TransferSpill) 1712e8d8bef9SDimitry Andric TransferCandidates = OpenRanges.getRegisterVarLocs(Reg); 1713e8d8bef9SDimitry Andric else if (TKind == TransferKind::TransferRestore) 1714e8d8bef9SDimitry Andric TransferCandidates = OpenRanges.getSpillVarLocs(); 1715e8d8bef9SDimitry Andric for (uint64_t ID : TransferCandidates) { 1716e8d8bef9SDimitry Andric LocIndex Idx = LocIndex::fromRawInteger(ID); 1717e8d8bef9SDimitry Andric const VarLoc &VL = VarLocIDs[Idx]; 1718fe6060f1SDimitry Andric unsigned LocIdx; 1719e8d8bef9SDimitry Andric if (TKind == TransferKind::TransferSpill) { 1720fe6060f1SDimitry Andric assert(VL.usesReg(Reg) && "Broken VarLocSet?"); 1721e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Spilling Register " << printReg(Reg, TRI) << '(' 1722e8d8bef9SDimitry Andric << VL.Var.getVariable()->getName() << ")\n"); 1723fe6060f1SDimitry Andric LocIdx = VL.getRegIdx(Reg); 1724e8d8bef9SDimitry Andric } else { 1725fe6060f1SDimitry Andric assert(TKind == TransferKind::TransferRestore && VL.containsSpillLocs() && 1726fe6060f1SDimitry Andric "Broken VarLocSet?"); 1727fe6060f1SDimitry Andric if (!VL.usesSpillLoc(*Loc)) 1728e8d8bef9SDimitry Andric // The spill location is not the location of a debug value. 1729e8d8bef9SDimitry Andric continue; 1730e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Restoring Register " << printReg(Reg, TRI) << '(' 1731e8d8bef9SDimitry Andric << VL.Var.getVariable()->getName() << ")\n"); 1732fe6060f1SDimitry Andric LocIdx = VL.getSpillLocIdx(*Loc); 1733e8d8bef9SDimitry Andric } 1734fe6060f1SDimitry Andric VarLoc::MachineLoc MLoc = VL.Locs[LocIdx]; 1735e8d8bef9SDimitry Andric insertTransferDebugPair(MI, OpenRanges, Transfers, VarLocIDs, Idx, TKind, 1736fe6060f1SDimitry Andric MLoc, Reg); 1737e8d8bef9SDimitry Andric // FIXME: A comment should explain why it's correct to return early here, 1738e8d8bef9SDimitry Andric // if that is in fact correct. 1739e8d8bef9SDimitry Andric return; 1740e8d8bef9SDimitry Andric } 1741e8d8bef9SDimitry Andric } 1742e8d8bef9SDimitry Andric 1743e8d8bef9SDimitry Andric /// If \p MI is a register copy instruction, that copies a previously tracked 1744e8d8bef9SDimitry Andric /// value from one register to another register that is callee saved, we 1745e8d8bef9SDimitry Andric /// create new DBG_VALUE instruction described with copy destination register. 1746e8d8bef9SDimitry Andric void VarLocBasedLDV::transferRegisterCopy(MachineInstr &MI, 1747e8d8bef9SDimitry Andric OpenRangesSet &OpenRanges, 1748e8d8bef9SDimitry Andric VarLocMap &VarLocIDs, 1749e8d8bef9SDimitry Andric TransferMap &Transfers) { 1750e8d8bef9SDimitry Andric auto DestSrc = TII->isCopyInstr(MI); 1751e8d8bef9SDimitry Andric if (!DestSrc) 1752e8d8bef9SDimitry Andric return; 1753e8d8bef9SDimitry Andric 1754e8d8bef9SDimitry Andric const MachineOperand *DestRegOp = DestSrc->Destination; 1755e8d8bef9SDimitry Andric const MachineOperand *SrcRegOp = DestSrc->Source; 1756e8d8bef9SDimitry Andric 1757e8d8bef9SDimitry Andric if (!DestRegOp->isDef()) 1758e8d8bef9SDimitry Andric return; 1759e8d8bef9SDimitry Andric 1760e8d8bef9SDimitry Andric auto isCalleeSavedReg = [&](Register Reg) { 1761e8d8bef9SDimitry Andric for (MCRegAliasIterator RAI(Reg, TRI, true); RAI.isValid(); ++RAI) 1762e8d8bef9SDimitry Andric if (CalleeSavedRegs.test(*RAI)) 1763e8d8bef9SDimitry Andric return true; 1764e8d8bef9SDimitry Andric return false; 1765e8d8bef9SDimitry Andric }; 1766e8d8bef9SDimitry Andric 1767e8d8bef9SDimitry Andric Register SrcReg = SrcRegOp->getReg(); 1768e8d8bef9SDimitry Andric Register DestReg = DestRegOp->getReg(); 1769e8d8bef9SDimitry Andric 1770e8d8bef9SDimitry Andric // We want to recognize instructions where destination register is callee 1771e8d8bef9SDimitry Andric // saved register. If register that could be clobbered by the call is 1772e8d8bef9SDimitry Andric // included, there would be a great chance that it is going to be clobbered 1773e8d8bef9SDimitry Andric // soon. It is more likely that previous register location, which is callee 1774e8d8bef9SDimitry Andric // saved, is going to stay unclobbered longer, even if it is killed. 1775e8d8bef9SDimitry Andric if (!isCalleeSavedReg(DestReg)) 1776e8d8bef9SDimitry Andric return; 1777e8d8bef9SDimitry Andric 1778e8d8bef9SDimitry Andric // Remember an entry value movement. If we encounter a new debug value of 1779e8d8bef9SDimitry Andric // a parameter describing only a moving of the value around, rather then 1780e8d8bef9SDimitry Andric // modifying it, we are still able to use the entry value if needed. 1781e8d8bef9SDimitry Andric if (isRegOtherThanSPAndFP(*DestRegOp, MI, TRI)) { 1782e8d8bef9SDimitry Andric for (uint64_t ID : OpenRanges.getEntryValueBackupVarLocs()) { 1783e8d8bef9SDimitry Andric LocIndex Idx = LocIndex::fromRawInteger(ID); 1784e8d8bef9SDimitry Andric const VarLoc &VL = VarLocIDs[Idx]; 1785fe6060f1SDimitry Andric if (VL.isEntryValueBackupReg(SrcReg)) { 1786e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Copy of the entry value: "; MI.dump();); 1787e8d8bef9SDimitry Andric VarLoc EntryValLocCopyBackup = 1788e8d8bef9SDimitry Andric VarLoc::CreateEntryCopyBackupLoc(VL.MI, LS, VL.Expr, DestReg); 1789e8d8bef9SDimitry Andric // Stop tracking the original entry value. 1790e8d8bef9SDimitry Andric OpenRanges.erase(VL); 1791e8d8bef9SDimitry Andric 1792e8d8bef9SDimitry Andric // Start tracking the entry value copy. 1793fe6060f1SDimitry Andric LocIndices EntryValCopyLocIDs = VarLocIDs.insert(EntryValLocCopyBackup); 1794fe6060f1SDimitry Andric OpenRanges.insert(EntryValCopyLocIDs, EntryValLocCopyBackup); 1795e8d8bef9SDimitry Andric break; 1796e8d8bef9SDimitry Andric } 1797e8d8bef9SDimitry Andric } 1798e8d8bef9SDimitry Andric } 1799e8d8bef9SDimitry Andric 1800e8d8bef9SDimitry Andric if (!SrcRegOp->isKill()) 1801e8d8bef9SDimitry Andric return; 1802e8d8bef9SDimitry Andric 1803e8d8bef9SDimitry Andric for (uint64_t ID : OpenRanges.getRegisterVarLocs(SrcReg)) { 1804e8d8bef9SDimitry Andric LocIndex Idx = LocIndex::fromRawInteger(ID); 1805fe6060f1SDimitry Andric assert(VarLocIDs[Idx].usesReg(SrcReg) && "Broken VarLocSet?"); 1806fe6060f1SDimitry Andric VarLoc::MachineLocValue Loc; 1807fe6060f1SDimitry Andric Loc.RegNo = SrcReg; 1808fe6060f1SDimitry Andric VarLoc::MachineLoc MLoc{VarLoc::MachineLocKind::RegisterKind, Loc}; 1809e8d8bef9SDimitry Andric insertTransferDebugPair(MI, OpenRanges, Transfers, VarLocIDs, Idx, 1810fe6060f1SDimitry Andric TransferKind::TransferCopy, MLoc, DestReg); 1811e8d8bef9SDimitry Andric // FIXME: A comment should explain why it's correct to return early here, 1812e8d8bef9SDimitry Andric // if that is in fact correct. 1813e8d8bef9SDimitry Andric return; 1814e8d8bef9SDimitry Andric } 1815e8d8bef9SDimitry Andric } 1816e8d8bef9SDimitry Andric 1817e8d8bef9SDimitry Andric /// Terminate all open ranges at the end of the current basic block. 1818e8d8bef9SDimitry Andric bool VarLocBasedLDV::transferTerminator(MachineBasicBlock *CurMBB, 1819e8d8bef9SDimitry Andric OpenRangesSet &OpenRanges, 1820e8d8bef9SDimitry Andric VarLocInMBB &OutLocs, 1821e8d8bef9SDimitry Andric const VarLocMap &VarLocIDs) { 1822e8d8bef9SDimitry Andric bool Changed = false; 1823fe6060f1SDimitry Andric LLVM_DEBUG({ 1824fe6060f1SDimitry Andric VarVec VarLocs; 1825fe6060f1SDimitry Andric OpenRanges.getUniqueVarLocs(VarLocs, VarLocIDs); 1826fe6060f1SDimitry Andric for (VarLoc &VL : VarLocs) { 1827e8d8bef9SDimitry Andric // Copy OpenRanges to OutLocs, if not already present. 1828e8d8bef9SDimitry Andric dbgs() << "Add to OutLocs in MBB #" << CurMBB->getNumber() << ": "; 1829fe6060f1SDimitry Andric VL.dump(TRI); 1830fe6060f1SDimitry Andric } 1831e8d8bef9SDimitry Andric }); 1832e8d8bef9SDimitry Andric VarLocSet &VLS = getVarLocsInMBB(CurMBB, OutLocs); 1833e8d8bef9SDimitry Andric Changed = VLS != OpenRanges.getVarLocs(); 1834e8d8bef9SDimitry Andric // New OutLocs set may be different due to spill, restore or register 1835e8d8bef9SDimitry Andric // copy instruction processing. 1836e8d8bef9SDimitry Andric if (Changed) 1837e8d8bef9SDimitry Andric VLS = OpenRanges.getVarLocs(); 1838e8d8bef9SDimitry Andric OpenRanges.clear(); 1839e8d8bef9SDimitry Andric return Changed; 1840e8d8bef9SDimitry Andric } 1841e8d8bef9SDimitry Andric 1842e8d8bef9SDimitry Andric /// Accumulate a mapping between each DILocalVariable fragment and other 1843e8d8bef9SDimitry Andric /// fragments of that DILocalVariable which overlap. This reduces work during 1844e8d8bef9SDimitry Andric /// the data-flow stage from "Find any overlapping fragments" to "Check if the 1845e8d8bef9SDimitry Andric /// known-to-overlap fragments are present". 1846e8d8bef9SDimitry Andric /// \param MI A previously unprocessed DEBUG_VALUE instruction to analyze for 1847e8d8bef9SDimitry Andric /// fragment usage. 1848e8d8bef9SDimitry Andric /// \param SeenFragments Map from DILocalVariable to all fragments of that 1849e8d8bef9SDimitry Andric /// Variable which are known to exist. 1850e8d8bef9SDimitry Andric /// \param OverlappingFragments The overlap map being constructed, from one 1851e8d8bef9SDimitry Andric /// Var/Fragment pair to a vector of fragments known to overlap. 1852e8d8bef9SDimitry Andric void VarLocBasedLDV::accumulateFragmentMap(MachineInstr &MI, 1853e8d8bef9SDimitry Andric VarToFragments &SeenFragments, 1854e8d8bef9SDimitry Andric OverlapMap &OverlappingFragments) { 1855e8d8bef9SDimitry Andric DebugVariable MIVar(MI.getDebugVariable(), MI.getDebugExpression(), 1856e8d8bef9SDimitry Andric MI.getDebugLoc()->getInlinedAt()); 1857e8d8bef9SDimitry Andric FragmentInfo ThisFragment = MIVar.getFragmentOrDefault(); 1858e8d8bef9SDimitry Andric 1859e8d8bef9SDimitry Andric // If this is the first sighting of this variable, then we are guaranteed 1860e8d8bef9SDimitry Andric // there are currently no overlapping fragments either. Initialize the set 1861e8d8bef9SDimitry Andric // of seen fragments, record no overlaps for the current one, and return. 1862e8d8bef9SDimitry Andric auto SeenIt = SeenFragments.find(MIVar.getVariable()); 1863e8d8bef9SDimitry Andric if (SeenIt == SeenFragments.end()) { 1864e8d8bef9SDimitry Andric SmallSet<FragmentInfo, 4> OneFragment; 1865e8d8bef9SDimitry Andric OneFragment.insert(ThisFragment); 1866e8d8bef9SDimitry Andric SeenFragments.insert({MIVar.getVariable(), OneFragment}); 1867e8d8bef9SDimitry Andric 1868e8d8bef9SDimitry Andric OverlappingFragments.insert({{MIVar.getVariable(), ThisFragment}, {}}); 1869e8d8bef9SDimitry Andric return; 1870e8d8bef9SDimitry Andric } 1871e8d8bef9SDimitry Andric 1872e8d8bef9SDimitry Andric // If this particular Variable/Fragment pair already exists in the overlap 1873e8d8bef9SDimitry Andric // map, it has already been accounted for. 1874e8d8bef9SDimitry Andric auto IsInOLapMap = 1875e8d8bef9SDimitry Andric OverlappingFragments.insert({{MIVar.getVariable(), ThisFragment}, {}}); 1876e8d8bef9SDimitry Andric if (!IsInOLapMap.second) 1877e8d8bef9SDimitry Andric return; 1878e8d8bef9SDimitry Andric 1879e8d8bef9SDimitry Andric auto &ThisFragmentsOverlaps = IsInOLapMap.first->second; 1880e8d8bef9SDimitry Andric auto &AllSeenFragments = SeenIt->second; 1881e8d8bef9SDimitry Andric 1882e8d8bef9SDimitry Andric // Otherwise, examine all other seen fragments for this variable, with "this" 1883e8d8bef9SDimitry Andric // fragment being a previously unseen fragment. Record any pair of 1884e8d8bef9SDimitry Andric // overlapping fragments. 1885e8d8bef9SDimitry Andric for (auto &ASeenFragment : AllSeenFragments) { 1886e8d8bef9SDimitry Andric // Does this previously seen fragment overlap? 1887e8d8bef9SDimitry Andric if (DIExpression::fragmentsOverlap(ThisFragment, ASeenFragment)) { 1888e8d8bef9SDimitry Andric // Yes: Mark the current fragment as being overlapped. 1889e8d8bef9SDimitry Andric ThisFragmentsOverlaps.push_back(ASeenFragment); 1890e8d8bef9SDimitry Andric // Mark the previously seen fragment as being overlapped by the current 1891e8d8bef9SDimitry Andric // one. 1892e8d8bef9SDimitry Andric auto ASeenFragmentsOverlaps = 1893e8d8bef9SDimitry Andric OverlappingFragments.find({MIVar.getVariable(), ASeenFragment}); 1894e8d8bef9SDimitry Andric assert(ASeenFragmentsOverlaps != OverlappingFragments.end() && 1895e8d8bef9SDimitry Andric "Previously seen var fragment has no vector of overlaps"); 1896e8d8bef9SDimitry Andric ASeenFragmentsOverlaps->second.push_back(ThisFragment); 1897e8d8bef9SDimitry Andric } 1898e8d8bef9SDimitry Andric } 1899e8d8bef9SDimitry Andric 1900e8d8bef9SDimitry Andric AllSeenFragments.insert(ThisFragment); 1901e8d8bef9SDimitry Andric } 1902e8d8bef9SDimitry Andric 1903e8d8bef9SDimitry Andric /// This routine creates OpenRanges. 1904e8d8bef9SDimitry Andric void VarLocBasedLDV::process(MachineInstr &MI, OpenRangesSet &OpenRanges, 1905349cc55cSDimitry Andric VarLocMap &VarLocIDs, TransferMap &Transfers, 1906349cc55cSDimitry Andric InstToEntryLocMap &EntryValTransfers, 1907349cc55cSDimitry Andric RegDefToInstMap &RegSetInstrs) { 1908349cc55cSDimitry Andric if (!MI.isDebugInstr()) 1909349cc55cSDimitry Andric LastNonDbgMI = &MI; 1910349cc55cSDimitry Andric transferDebugValue(MI, OpenRanges, VarLocIDs, EntryValTransfers, 1911349cc55cSDimitry Andric RegSetInstrs); 1912349cc55cSDimitry Andric transferRegisterDef(MI, OpenRanges, VarLocIDs, EntryValTransfers, 1913349cc55cSDimitry Andric RegSetInstrs); 1914e8d8bef9SDimitry Andric transferRegisterCopy(MI, OpenRanges, VarLocIDs, Transfers); 1915e8d8bef9SDimitry Andric transferSpillOrRestoreInst(MI, OpenRanges, VarLocIDs, Transfers); 1916e8d8bef9SDimitry Andric } 1917e8d8bef9SDimitry Andric 1918e8d8bef9SDimitry Andric /// This routine joins the analysis results of all incoming edges in @MBB by 1919e8d8bef9SDimitry Andric /// inserting a new DBG_VALUE instruction at the start of the @MBB - if the same 1920e8d8bef9SDimitry Andric /// source variable in all the predecessors of @MBB reside in the same location. 1921e8d8bef9SDimitry Andric bool VarLocBasedLDV::join( 1922e8d8bef9SDimitry Andric MachineBasicBlock &MBB, VarLocInMBB &OutLocs, VarLocInMBB &InLocs, 1923e8d8bef9SDimitry Andric const VarLocMap &VarLocIDs, 1924e8d8bef9SDimitry Andric SmallPtrSet<const MachineBasicBlock *, 16> &Visited, 1925e8d8bef9SDimitry Andric SmallPtrSetImpl<const MachineBasicBlock *> &ArtificialBlocks) { 1926e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "join MBB: " << MBB.getNumber() << "\n"); 1927e8d8bef9SDimitry Andric 1928e8d8bef9SDimitry Andric VarLocSet InLocsT(Alloc); // Temporary incoming locations. 1929e8d8bef9SDimitry Andric 1930e8d8bef9SDimitry Andric // For all predecessors of this MBB, find the set of VarLocs that 1931e8d8bef9SDimitry Andric // can be joined. 1932e8d8bef9SDimitry Andric int NumVisited = 0; 1933e8d8bef9SDimitry Andric for (auto p : MBB.predecessors()) { 1934e8d8bef9SDimitry Andric // Ignore backedges if we have not visited the predecessor yet. As the 1935e8d8bef9SDimitry Andric // predecessor hasn't yet had locations propagated into it, most locations 1936e8d8bef9SDimitry Andric // will not yet be valid, so treat them as all being uninitialized and 1937e8d8bef9SDimitry Andric // potentially valid. If a location guessed to be correct here is 1938e8d8bef9SDimitry Andric // invalidated later, we will remove it when we revisit this block. 1939e8d8bef9SDimitry Andric if (!Visited.count(p)) { 1940e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << " ignoring unvisited pred MBB: " << p->getNumber() 1941e8d8bef9SDimitry Andric << "\n"); 1942e8d8bef9SDimitry Andric continue; 1943e8d8bef9SDimitry Andric } 1944e8d8bef9SDimitry Andric auto OL = OutLocs.find(p); 1945e8d8bef9SDimitry Andric // Join is null in case of empty OutLocs from any of the pred. 1946e8d8bef9SDimitry Andric if (OL == OutLocs.end()) 1947e8d8bef9SDimitry Andric return false; 1948e8d8bef9SDimitry Andric 1949e8d8bef9SDimitry Andric // Just copy over the Out locs to incoming locs for the first visited 1950e8d8bef9SDimitry Andric // predecessor, and for all other predecessors join the Out locs. 1951e8d8bef9SDimitry Andric VarLocSet &OutLocVLS = *OL->second.get(); 1952e8d8bef9SDimitry Andric if (!NumVisited) 1953e8d8bef9SDimitry Andric InLocsT = OutLocVLS; 1954e8d8bef9SDimitry Andric else 1955e8d8bef9SDimitry Andric InLocsT &= OutLocVLS; 1956e8d8bef9SDimitry Andric 1957e8d8bef9SDimitry Andric LLVM_DEBUG({ 1958e8d8bef9SDimitry Andric if (!InLocsT.empty()) { 1959fe6060f1SDimitry Andric VarVec VarLocs; 1960fe6060f1SDimitry Andric collectAllVarLocs(VarLocs, InLocsT, VarLocIDs); 1961fe6060f1SDimitry Andric for (const VarLoc &VL : VarLocs) 1962e8d8bef9SDimitry Andric dbgs() << " gathered candidate incoming var: " 1963fe6060f1SDimitry Andric << VL.Var.getVariable()->getName() << "\n"; 1964e8d8bef9SDimitry Andric } 1965e8d8bef9SDimitry Andric }); 1966e8d8bef9SDimitry Andric 1967e8d8bef9SDimitry Andric NumVisited++; 1968e8d8bef9SDimitry Andric } 1969e8d8bef9SDimitry Andric 1970e8d8bef9SDimitry Andric // Filter out DBG_VALUES that are out of scope. 1971e8d8bef9SDimitry Andric VarLocSet KillSet(Alloc); 1972e8d8bef9SDimitry Andric bool IsArtificial = ArtificialBlocks.count(&MBB); 1973e8d8bef9SDimitry Andric if (!IsArtificial) { 1974e8d8bef9SDimitry Andric for (uint64_t ID : InLocsT) { 1975e8d8bef9SDimitry Andric LocIndex Idx = LocIndex::fromRawInteger(ID); 1976e8d8bef9SDimitry Andric if (!VarLocIDs[Idx].dominates(LS, MBB)) { 1977e8d8bef9SDimitry Andric KillSet.set(ID); 1978e8d8bef9SDimitry Andric LLVM_DEBUG({ 1979e8d8bef9SDimitry Andric auto Name = VarLocIDs[Idx].Var.getVariable()->getName(); 1980e8d8bef9SDimitry Andric dbgs() << " killing " << Name << ", it doesn't dominate MBB\n"; 1981e8d8bef9SDimitry Andric }); 1982e8d8bef9SDimitry Andric } 1983e8d8bef9SDimitry Andric } 1984e8d8bef9SDimitry Andric } 1985e8d8bef9SDimitry Andric InLocsT.intersectWithComplement(KillSet); 1986e8d8bef9SDimitry Andric 1987e8d8bef9SDimitry Andric // As we are processing blocks in reverse post-order we 1988e8d8bef9SDimitry Andric // should have processed at least one predecessor, unless it 1989e8d8bef9SDimitry Andric // is the entry block which has no predecessor. 1990e8d8bef9SDimitry Andric assert((NumVisited || MBB.pred_empty()) && 1991e8d8bef9SDimitry Andric "Should have processed at least one predecessor"); 1992e8d8bef9SDimitry Andric 1993e8d8bef9SDimitry Andric VarLocSet &ILS = getVarLocsInMBB(&MBB, InLocs); 1994e8d8bef9SDimitry Andric bool Changed = false; 1995e8d8bef9SDimitry Andric if (ILS != InLocsT) { 1996e8d8bef9SDimitry Andric ILS = InLocsT; 1997e8d8bef9SDimitry Andric Changed = true; 1998e8d8bef9SDimitry Andric } 1999e8d8bef9SDimitry Andric 2000e8d8bef9SDimitry Andric return Changed; 2001e8d8bef9SDimitry Andric } 2002e8d8bef9SDimitry Andric 2003e8d8bef9SDimitry Andric void VarLocBasedLDV::flushPendingLocs(VarLocInMBB &PendingInLocs, 2004e8d8bef9SDimitry Andric VarLocMap &VarLocIDs) { 2005e8d8bef9SDimitry Andric // PendingInLocs records all locations propagated into blocks, which have 2006e8d8bef9SDimitry Andric // not had DBG_VALUE insts created. Go through and create those insts now. 2007e8d8bef9SDimitry Andric for (auto &Iter : PendingInLocs) { 2008e8d8bef9SDimitry Andric // Map is keyed on a constant pointer, unwrap it so we can insert insts. 2009e8d8bef9SDimitry Andric auto &MBB = const_cast<MachineBasicBlock &>(*Iter.first); 2010e8d8bef9SDimitry Andric VarLocSet &Pending = *Iter.second.get(); 2011e8d8bef9SDimitry Andric 2012fe6060f1SDimitry Andric SmallVector<VarLoc, 32> VarLocs; 2013fe6060f1SDimitry Andric collectAllVarLocs(VarLocs, Pending, VarLocIDs); 2014fe6060f1SDimitry Andric 2015fe6060f1SDimitry Andric for (VarLoc DiffIt : VarLocs) { 2016e8d8bef9SDimitry Andric // The ID location is live-in to MBB -- work out what kind of machine 2017e8d8bef9SDimitry Andric // location it is and create a DBG_VALUE. 2018e8d8bef9SDimitry Andric if (DiffIt.isEntryBackupLoc()) 2019e8d8bef9SDimitry Andric continue; 2020e8d8bef9SDimitry Andric MachineInstr *MI = DiffIt.BuildDbgValue(*MBB.getParent()); 2021e8d8bef9SDimitry Andric MBB.insert(MBB.instr_begin(), MI); 2022e8d8bef9SDimitry Andric 2023e8d8bef9SDimitry Andric (void)MI; 2024e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Inserted: "; MI->dump();); 2025e8d8bef9SDimitry Andric } 2026e8d8bef9SDimitry Andric } 2027e8d8bef9SDimitry Andric } 2028e8d8bef9SDimitry Andric 2029e8d8bef9SDimitry Andric bool VarLocBasedLDV::isEntryValueCandidate( 2030e8d8bef9SDimitry Andric const MachineInstr &MI, const DefinedRegsSet &DefinedRegs) const { 2031e8d8bef9SDimitry Andric assert(MI.isDebugValue() && "This must be DBG_VALUE."); 2032e8d8bef9SDimitry Andric 2033e8d8bef9SDimitry Andric // TODO: Add support for local variables that are expressed in terms of 2034e8d8bef9SDimitry Andric // parameters entry values. 2035e8d8bef9SDimitry Andric // TODO: Add support for modified arguments that can be expressed 2036e8d8bef9SDimitry Andric // by using its entry value. 2037e8d8bef9SDimitry Andric auto *DIVar = MI.getDebugVariable(); 2038e8d8bef9SDimitry Andric if (!DIVar->isParameter()) 2039e8d8bef9SDimitry Andric return false; 2040e8d8bef9SDimitry Andric 2041e8d8bef9SDimitry Andric // Do not consider parameters that belong to an inlined function. 2042e8d8bef9SDimitry Andric if (MI.getDebugLoc()->getInlinedAt()) 2043e8d8bef9SDimitry Andric return false; 2044e8d8bef9SDimitry Andric 2045e8d8bef9SDimitry Andric // Only consider parameters that are described using registers. Parameters 2046e8d8bef9SDimitry Andric // that are passed on the stack are not yet supported, so ignore debug 2047e8d8bef9SDimitry Andric // values that are described by the frame or stack pointer. 2048e8d8bef9SDimitry Andric if (!isRegOtherThanSPAndFP(MI.getDebugOperand(0), MI, TRI)) 2049e8d8bef9SDimitry Andric return false; 2050e8d8bef9SDimitry Andric 2051e8d8bef9SDimitry Andric // If a parameter's value has been propagated from the caller, then the 2052e8d8bef9SDimitry Andric // parameter's DBG_VALUE may be described using a register defined by some 2053e8d8bef9SDimitry Andric // instruction in the entry block, in which case we shouldn't create an 2054e8d8bef9SDimitry Andric // entry value. 2055e8d8bef9SDimitry Andric if (DefinedRegs.count(MI.getDebugOperand(0).getReg())) 2056e8d8bef9SDimitry Andric return false; 2057e8d8bef9SDimitry Andric 2058e8d8bef9SDimitry Andric // TODO: Add support for parameters that have a pre-existing debug expressions 2059e8d8bef9SDimitry Andric // (e.g. fragments). 2060e8d8bef9SDimitry Andric if (MI.getDebugExpression()->getNumElements() > 0) 2061e8d8bef9SDimitry Andric return false; 2062e8d8bef9SDimitry Andric 2063e8d8bef9SDimitry Andric return true; 2064e8d8bef9SDimitry Andric } 2065e8d8bef9SDimitry Andric 2066e8d8bef9SDimitry Andric /// Collect all register defines (including aliases) for the given instruction. 2067e8d8bef9SDimitry Andric static void collectRegDefs(const MachineInstr &MI, DefinedRegsSet &Regs, 2068e8d8bef9SDimitry Andric const TargetRegisterInfo *TRI) { 2069e8d8bef9SDimitry Andric for (const MachineOperand &MO : MI.operands()) 2070e8d8bef9SDimitry Andric if (MO.isReg() && MO.isDef() && MO.getReg()) 2071e8d8bef9SDimitry Andric for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid(); ++AI) 2072e8d8bef9SDimitry Andric Regs.insert(*AI); 2073e8d8bef9SDimitry Andric } 2074e8d8bef9SDimitry Andric 2075e8d8bef9SDimitry Andric /// This routine records the entry values of function parameters. The values 2076e8d8bef9SDimitry Andric /// could be used as backup values. If we loose the track of some unmodified 2077e8d8bef9SDimitry Andric /// parameters, the backup values will be used as a primary locations. 2078e8d8bef9SDimitry Andric void VarLocBasedLDV::recordEntryValue(const MachineInstr &MI, 2079e8d8bef9SDimitry Andric const DefinedRegsSet &DefinedRegs, 2080e8d8bef9SDimitry Andric OpenRangesSet &OpenRanges, 2081e8d8bef9SDimitry Andric VarLocMap &VarLocIDs) { 2082e8d8bef9SDimitry Andric if (TPC) { 2083e8d8bef9SDimitry Andric auto &TM = TPC->getTM<TargetMachine>(); 2084e8d8bef9SDimitry Andric if (!TM.Options.ShouldEmitDebugEntryValues()) 2085e8d8bef9SDimitry Andric return; 2086e8d8bef9SDimitry Andric } 2087e8d8bef9SDimitry Andric 2088e8d8bef9SDimitry Andric DebugVariable V(MI.getDebugVariable(), MI.getDebugExpression(), 2089e8d8bef9SDimitry Andric MI.getDebugLoc()->getInlinedAt()); 2090e8d8bef9SDimitry Andric 2091e8d8bef9SDimitry Andric if (!isEntryValueCandidate(MI, DefinedRegs) || 2092e8d8bef9SDimitry Andric OpenRanges.getEntryValueBackup(V)) 2093e8d8bef9SDimitry Andric return; 2094e8d8bef9SDimitry Andric 2095e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Creating the backup entry location: "; MI.dump();); 2096e8d8bef9SDimitry Andric 2097e8d8bef9SDimitry Andric // Create the entry value and use it as a backup location until it is 2098e8d8bef9SDimitry Andric // valid. It is valid until a parameter is not changed. 2099e8d8bef9SDimitry Andric DIExpression *NewExpr = 2100e8d8bef9SDimitry Andric DIExpression::prepend(MI.getDebugExpression(), DIExpression::EntryValue); 2101e8d8bef9SDimitry Andric VarLoc EntryValLocAsBackup = VarLoc::CreateEntryBackupLoc(MI, LS, NewExpr); 2102fe6060f1SDimitry Andric LocIndices EntryValLocIDs = VarLocIDs.insert(EntryValLocAsBackup); 2103fe6060f1SDimitry Andric OpenRanges.insert(EntryValLocIDs, EntryValLocAsBackup); 2104e8d8bef9SDimitry Andric } 2105e8d8bef9SDimitry Andric 2106e8d8bef9SDimitry Andric /// Calculate the liveness information for the given machine function and 2107e8d8bef9SDimitry Andric /// extend ranges across basic blocks. 2108349cc55cSDimitry Andric bool VarLocBasedLDV::ExtendRanges(MachineFunction &MF, 2109349cc55cSDimitry Andric MachineDominatorTree *DomTree, 2110349cc55cSDimitry Andric TargetPassConfig *TPC, unsigned InputBBLimit, 2111349cc55cSDimitry Andric unsigned InputDbgValLimit) { 2112349cc55cSDimitry Andric (void)DomTree; 2113e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "\nDebug Range Extension\n"); 2114e8d8bef9SDimitry Andric 2115e8d8bef9SDimitry Andric if (!MF.getFunction().getSubprogram()) 2116e8d8bef9SDimitry Andric // VarLocBaseLDV will already have removed all DBG_VALUEs. 2117e8d8bef9SDimitry Andric return false; 2118e8d8bef9SDimitry Andric 2119e8d8bef9SDimitry Andric // Skip functions from NoDebug compilation units. 2120e8d8bef9SDimitry Andric if (MF.getFunction().getSubprogram()->getUnit()->getEmissionKind() == 2121e8d8bef9SDimitry Andric DICompileUnit::NoDebug) 2122e8d8bef9SDimitry Andric return false; 2123e8d8bef9SDimitry Andric 2124e8d8bef9SDimitry Andric TRI = MF.getSubtarget().getRegisterInfo(); 2125e8d8bef9SDimitry Andric TII = MF.getSubtarget().getInstrInfo(); 2126e8d8bef9SDimitry Andric TFI = MF.getSubtarget().getFrameLowering(); 2127e8d8bef9SDimitry Andric TFI->getCalleeSaves(MF, CalleeSavedRegs); 2128e8d8bef9SDimitry Andric this->TPC = TPC; 2129e8d8bef9SDimitry Andric LS.initialize(MF); 2130e8d8bef9SDimitry Andric 2131e8d8bef9SDimitry Andric bool Changed = false; 2132e8d8bef9SDimitry Andric bool OLChanged = false; 2133e8d8bef9SDimitry Andric bool MBBJoined = false; 2134e8d8bef9SDimitry Andric 2135e8d8bef9SDimitry Andric VarLocMap VarLocIDs; // Map VarLoc<>unique ID for use in bitvectors. 2136e8d8bef9SDimitry Andric OverlapMap OverlapFragments; // Map of overlapping variable fragments. 2137e8d8bef9SDimitry Andric OpenRangesSet OpenRanges(Alloc, OverlapFragments); 2138e8d8bef9SDimitry Andric // Ranges that are open until end of bb. 2139e8d8bef9SDimitry Andric VarLocInMBB OutLocs; // Ranges that exist beyond bb. 2140e8d8bef9SDimitry Andric VarLocInMBB InLocs; // Ranges that are incoming after joining. 2141e8d8bef9SDimitry Andric TransferMap Transfers; // DBG_VALUEs associated with transfers (such as 2142e8d8bef9SDimitry Andric // spills, copies and restores). 2143349cc55cSDimitry Andric // Map responsible MI to attached Transfer emitted from Backup Entry Value. 2144349cc55cSDimitry Andric InstToEntryLocMap EntryValTransfers; 2145349cc55cSDimitry Andric // Map a Register to the last MI which clobbered it. 2146349cc55cSDimitry Andric RegDefToInstMap RegSetInstrs; 2147e8d8bef9SDimitry Andric 2148e8d8bef9SDimitry Andric VarToFragments SeenFragments; 2149e8d8bef9SDimitry Andric 2150e8d8bef9SDimitry Andric // Blocks which are artificial, i.e. blocks which exclusively contain 2151e8d8bef9SDimitry Andric // instructions without locations, or with line 0 locations. 2152e8d8bef9SDimitry Andric SmallPtrSet<const MachineBasicBlock *, 16> ArtificialBlocks; 2153e8d8bef9SDimitry Andric 2154e8d8bef9SDimitry Andric DenseMap<unsigned int, MachineBasicBlock *> OrderToBB; 2155e8d8bef9SDimitry Andric DenseMap<MachineBasicBlock *, unsigned int> BBToOrder; 2156e8d8bef9SDimitry Andric std::priority_queue<unsigned int, std::vector<unsigned int>, 2157e8d8bef9SDimitry Andric std::greater<unsigned int>> 2158e8d8bef9SDimitry Andric Worklist; 2159e8d8bef9SDimitry Andric std::priority_queue<unsigned int, std::vector<unsigned int>, 2160e8d8bef9SDimitry Andric std::greater<unsigned int>> 2161e8d8bef9SDimitry Andric Pending; 2162e8d8bef9SDimitry Andric 2163e8d8bef9SDimitry Andric // Set of register defines that are seen when traversing the entry block 2164e8d8bef9SDimitry Andric // looking for debug entry value candidates. 2165e8d8bef9SDimitry Andric DefinedRegsSet DefinedRegs; 2166e8d8bef9SDimitry Andric 2167e8d8bef9SDimitry Andric // Only in the case of entry MBB collect DBG_VALUEs representing 2168e8d8bef9SDimitry Andric // function parameters in order to generate debug entry values for them. 2169e8d8bef9SDimitry Andric MachineBasicBlock &First_MBB = *(MF.begin()); 2170e8d8bef9SDimitry Andric for (auto &MI : First_MBB) { 2171e8d8bef9SDimitry Andric collectRegDefs(MI, DefinedRegs, TRI); 2172e8d8bef9SDimitry Andric if (MI.isDebugValue()) 2173e8d8bef9SDimitry Andric recordEntryValue(MI, DefinedRegs, OpenRanges, VarLocIDs); 2174e8d8bef9SDimitry Andric } 2175e8d8bef9SDimitry Andric 2176e8d8bef9SDimitry Andric // Initialize per-block structures and scan for fragment overlaps. 2177e8d8bef9SDimitry Andric for (auto &MBB : MF) 2178e8d8bef9SDimitry Andric for (auto &MI : MBB) 2179e8d8bef9SDimitry Andric if (MI.isDebugValue()) 2180e8d8bef9SDimitry Andric accumulateFragmentMap(MI, SeenFragments, OverlapFragments); 2181e8d8bef9SDimitry Andric 2182e8d8bef9SDimitry Andric auto hasNonArtificialLocation = [](const MachineInstr &MI) -> bool { 2183e8d8bef9SDimitry Andric if (const DebugLoc &DL = MI.getDebugLoc()) 2184e8d8bef9SDimitry Andric return DL.getLine() != 0; 2185e8d8bef9SDimitry Andric return false; 2186e8d8bef9SDimitry Andric }; 2187e8d8bef9SDimitry Andric for (auto &MBB : MF) 2188e8d8bef9SDimitry Andric if (none_of(MBB.instrs(), hasNonArtificialLocation)) 2189e8d8bef9SDimitry Andric ArtificialBlocks.insert(&MBB); 2190e8d8bef9SDimitry Andric 2191e8d8bef9SDimitry Andric LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, 2192e8d8bef9SDimitry Andric "OutLocs after initialization", dbgs())); 2193e8d8bef9SDimitry Andric 2194e8d8bef9SDimitry Andric ReversePostOrderTraversal<MachineFunction *> RPOT(&MF); 2195e8d8bef9SDimitry Andric unsigned int RPONumber = 0; 2196fe6060f1SDimitry Andric for (MachineBasicBlock *MBB : RPOT) { 2197fe6060f1SDimitry Andric OrderToBB[RPONumber] = MBB; 2198fe6060f1SDimitry Andric BBToOrder[MBB] = RPONumber; 2199e8d8bef9SDimitry Andric Worklist.push(RPONumber); 2200e8d8bef9SDimitry Andric ++RPONumber; 2201e8d8bef9SDimitry Andric } 2202e8d8bef9SDimitry Andric 2203e8d8bef9SDimitry Andric if (RPONumber > InputBBLimit) { 2204e8d8bef9SDimitry Andric unsigned NumInputDbgValues = 0; 2205e8d8bef9SDimitry Andric for (auto &MBB : MF) 2206e8d8bef9SDimitry Andric for (auto &MI : MBB) 2207e8d8bef9SDimitry Andric if (MI.isDebugValue()) 2208e8d8bef9SDimitry Andric ++NumInputDbgValues; 2209349cc55cSDimitry Andric if (NumInputDbgValues > InputDbgValLimit) { 2210e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Disabling VarLocBasedLDV: " << MF.getName() 2211e8d8bef9SDimitry Andric << " has " << RPONumber << " basic blocks and " 2212e8d8bef9SDimitry Andric << NumInputDbgValues 2213e8d8bef9SDimitry Andric << " input DBG_VALUEs, exceeding limits.\n"); 2214e8d8bef9SDimitry Andric return false; 2215e8d8bef9SDimitry Andric } 2216e8d8bef9SDimitry Andric } 2217e8d8bef9SDimitry Andric 2218e8d8bef9SDimitry Andric // This is a standard "union of predecessor outs" dataflow problem. 2219e8d8bef9SDimitry Andric // To solve it, we perform join() and process() using the two worklist method 2220e8d8bef9SDimitry Andric // until the ranges converge. 2221e8d8bef9SDimitry Andric // Ranges have converged when both worklists are empty. 2222e8d8bef9SDimitry Andric SmallPtrSet<const MachineBasicBlock *, 16> Visited; 2223e8d8bef9SDimitry Andric while (!Worklist.empty() || !Pending.empty()) { 2224e8d8bef9SDimitry Andric // We track what is on the pending worklist to avoid inserting the same 2225e8d8bef9SDimitry Andric // thing twice. We could avoid this with a custom priority queue, but this 2226e8d8bef9SDimitry Andric // is probably not worth it. 2227e8d8bef9SDimitry Andric SmallPtrSet<MachineBasicBlock *, 16> OnPending; 2228e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Processing Worklist\n"); 2229e8d8bef9SDimitry Andric while (!Worklist.empty()) { 2230e8d8bef9SDimitry Andric MachineBasicBlock *MBB = OrderToBB[Worklist.top()]; 2231e8d8bef9SDimitry Andric Worklist.pop(); 2232e8d8bef9SDimitry Andric MBBJoined = join(*MBB, OutLocs, InLocs, VarLocIDs, Visited, 2233e8d8bef9SDimitry Andric ArtificialBlocks); 2234e8d8bef9SDimitry Andric MBBJoined |= Visited.insert(MBB).second; 2235e8d8bef9SDimitry Andric if (MBBJoined) { 2236e8d8bef9SDimitry Andric MBBJoined = false; 2237e8d8bef9SDimitry Andric Changed = true; 2238e8d8bef9SDimitry Andric // Now that we have started to extend ranges across BBs we need to 2239e8d8bef9SDimitry Andric // examine spill, copy and restore instructions to see whether they 2240e8d8bef9SDimitry Andric // operate with registers that correspond to user variables. 2241e8d8bef9SDimitry Andric // First load any pending inlocs. 2242e8d8bef9SDimitry Andric OpenRanges.insertFromLocSet(getVarLocsInMBB(MBB, InLocs), VarLocIDs); 2243349cc55cSDimitry Andric LastNonDbgMI = nullptr; 2244349cc55cSDimitry Andric RegSetInstrs.clear(); 2245e8d8bef9SDimitry Andric for (auto &MI : *MBB) 2246349cc55cSDimitry Andric process(MI, OpenRanges, VarLocIDs, Transfers, EntryValTransfers, 2247349cc55cSDimitry Andric RegSetInstrs); 2248e8d8bef9SDimitry Andric OLChanged |= transferTerminator(MBB, OpenRanges, OutLocs, VarLocIDs); 2249e8d8bef9SDimitry Andric 2250e8d8bef9SDimitry Andric LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, 2251e8d8bef9SDimitry Andric "OutLocs after propagating", dbgs())); 2252e8d8bef9SDimitry Andric LLVM_DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs, 2253e8d8bef9SDimitry Andric "InLocs after propagating", dbgs())); 2254e8d8bef9SDimitry Andric 2255e8d8bef9SDimitry Andric if (OLChanged) { 2256e8d8bef9SDimitry Andric OLChanged = false; 2257e8d8bef9SDimitry Andric for (auto s : MBB->successors()) 2258e8d8bef9SDimitry Andric if (OnPending.insert(s).second) { 2259e8d8bef9SDimitry Andric Pending.push(BBToOrder[s]); 2260e8d8bef9SDimitry Andric } 2261e8d8bef9SDimitry Andric } 2262e8d8bef9SDimitry Andric } 2263e8d8bef9SDimitry Andric } 2264e8d8bef9SDimitry Andric Worklist.swap(Pending); 2265e8d8bef9SDimitry Andric // At this point, pending must be empty, since it was just the empty 2266e8d8bef9SDimitry Andric // worklist 2267e8d8bef9SDimitry Andric assert(Pending.empty() && "Pending should be empty"); 2268e8d8bef9SDimitry Andric } 2269e8d8bef9SDimitry Andric 2270e8d8bef9SDimitry Andric // Add any DBG_VALUE instructions created by location transfers. 2271e8d8bef9SDimitry Andric for (auto &TR : Transfers) { 2272e8d8bef9SDimitry Andric assert(!TR.TransferInst->isTerminator() && 2273e8d8bef9SDimitry Andric "Cannot insert DBG_VALUE after terminator"); 2274e8d8bef9SDimitry Andric MachineBasicBlock *MBB = TR.TransferInst->getParent(); 2275e8d8bef9SDimitry Andric const VarLoc &VL = VarLocIDs[TR.LocationID]; 2276e8d8bef9SDimitry Andric MachineInstr *MI = VL.BuildDbgValue(MF); 2277e8d8bef9SDimitry Andric MBB->insertAfterBundle(TR.TransferInst->getIterator(), MI); 2278e8d8bef9SDimitry Andric } 2279e8d8bef9SDimitry Andric Transfers.clear(); 2280e8d8bef9SDimitry Andric 2281349cc55cSDimitry Andric // Add DBG_VALUEs created using Backup Entry Value location. 2282349cc55cSDimitry Andric for (auto &TR : EntryValTransfers) { 2283349cc55cSDimitry Andric MachineInstr *TRInst = const_cast<MachineInstr *>(TR.first); 2284349cc55cSDimitry Andric assert(!TRInst->isTerminator() && 2285349cc55cSDimitry Andric "Cannot insert DBG_VALUE after terminator"); 2286349cc55cSDimitry Andric MachineBasicBlock *MBB = TRInst->getParent(); 2287349cc55cSDimitry Andric const VarLoc &VL = VarLocIDs[TR.second]; 2288349cc55cSDimitry Andric MachineInstr *MI = VL.BuildDbgValue(MF); 2289349cc55cSDimitry Andric MBB->insertAfterBundle(TRInst->getIterator(), MI); 2290349cc55cSDimitry Andric } 2291349cc55cSDimitry Andric EntryValTransfers.clear(); 2292349cc55cSDimitry Andric 2293e8d8bef9SDimitry Andric // Deferred inlocs will not have had any DBG_VALUE insts created; do 2294e8d8bef9SDimitry Andric // that now. 2295e8d8bef9SDimitry Andric flushPendingLocs(InLocs, VarLocIDs); 2296e8d8bef9SDimitry Andric 2297e8d8bef9SDimitry Andric LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, "Final OutLocs", dbgs())); 2298e8d8bef9SDimitry Andric LLVM_DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs, "Final InLocs", dbgs())); 2299e8d8bef9SDimitry Andric return Changed; 2300e8d8bef9SDimitry Andric } 2301e8d8bef9SDimitry Andric 2302e8d8bef9SDimitry Andric LDVImpl * 2303e8d8bef9SDimitry Andric llvm::makeVarLocBasedLiveDebugValues() 2304e8d8bef9SDimitry Andric { 2305e8d8bef9SDimitry Andric return new VarLocBasedLDV(); 2306e8d8bef9SDimitry Andric } 2307