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