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