1349cc55cSDimitry Andric //===- InstrRefBasedImpl.h - Tracking Debug Value MIs ---------------------===// 2349cc55cSDimitry Andric // 3349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6349cc55cSDimitry Andric // 7349cc55cSDimitry Andric //===----------------------------------------------------------------------===// 8349cc55cSDimitry Andric 9349cc55cSDimitry Andric #ifndef LLVM_LIB_CODEGEN_LIVEDEBUGVALUES_INSTRREFBASEDLDV_H 10349cc55cSDimitry Andric #define LLVM_LIB_CODEGEN_LIVEDEBUGVALUES_INSTRREFBASEDLDV_H 11349cc55cSDimitry Andric 12349cc55cSDimitry Andric #include "llvm/ADT/DenseMap.h" 1381ad6265SDimitry Andric #include "llvm/ADT/IndexedMap.h" 14349cc55cSDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 15349cc55cSDimitry Andric #include "llvm/ADT/SmallVector.h" 16349cc55cSDimitry Andric #include "llvm/ADT/UniqueVector.h" 17349cc55cSDimitry Andric #include "llvm/CodeGen/LexicalScopes.h" 18349cc55cSDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 19349cc55cSDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 2081ad6265SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 21349cc55cSDimitry Andric #include "llvm/IR/DebugInfoMetadata.h" 22bdd1243dSDimitry Andric #include <optional> 23349cc55cSDimitry Andric 24349cc55cSDimitry Andric #include "LiveDebugValues.h" 25349cc55cSDimitry Andric 26349cc55cSDimitry Andric class TransferTracker; 27349cc55cSDimitry Andric 28349cc55cSDimitry Andric // Forward dec of unit test class, so that we can peer into the LDV object. 29349cc55cSDimitry Andric class InstrRefLDVTest; 30349cc55cSDimitry Andric 31349cc55cSDimitry Andric namespace LiveDebugValues { 32349cc55cSDimitry Andric 33349cc55cSDimitry Andric class MLocTracker; 34bdd1243dSDimitry Andric class DbgOpIDMap; 35349cc55cSDimitry Andric 36349cc55cSDimitry Andric using namespace llvm; 37349cc55cSDimitry Andric 38*0fca6ea1SDimitry Andric using DebugVariableID = unsigned; 39*0fca6ea1SDimitry Andric using VarAndLoc = std::pair<DebugVariable, const DILocation *>; 40*0fca6ea1SDimitry Andric 41*0fca6ea1SDimitry Andric /// Mapping from DebugVariable to/from a unique identifying number. Each 42*0fca6ea1SDimitry Andric /// DebugVariable consists of three pointers, and after a small amount of 43*0fca6ea1SDimitry Andric /// work to identify overlapping fragments of variables we mostly only use 44*0fca6ea1SDimitry Andric /// DebugVariables as identities of variables. It's much more compile-time 45*0fca6ea1SDimitry Andric /// efficient to use an ID number instead, which this class provides. 46*0fca6ea1SDimitry Andric class DebugVariableMap { 47*0fca6ea1SDimitry Andric DenseMap<DebugVariable, unsigned> VarToIdx; 48*0fca6ea1SDimitry Andric SmallVector<VarAndLoc> IdxToVar; 49*0fca6ea1SDimitry Andric 50*0fca6ea1SDimitry Andric public: 51*0fca6ea1SDimitry Andric DebugVariableID getDVID(const DebugVariable &Var) const { 52*0fca6ea1SDimitry Andric auto It = VarToIdx.find(Var); 53*0fca6ea1SDimitry Andric assert(It != VarToIdx.end()); 54*0fca6ea1SDimitry Andric return It->second; 55*0fca6ea1SDimitry Andric } 56*0fca6ea1SDimitry Andric 57*0fca6ea1SDimitry Andric DebugVariableID insertDVID(DebugVariable &Var, const DILocation *Loc) { 58*0fca6ea1SDimitry Andric unsigned Size = VarToIdx.size(); 59*0fca6ea1SDimitry Andric auto ItPair = VarToIdx.insert({Var, Size}); 60*0fca6ea1SDimitry Andric if (ItPair.second) { 61*0fca6ea1SDimitry Andric IdxToVar.push_back({Var, Loc}); 62*0fca6ea1SDimitry Andric return Size; 63*0fca6ea1SDimitry Andric } 64*0fca6ea1SDimitry Andric 65*0fca6ea1SDimitry Andric return ItPair.first->second; 66*0fca6ea1SDimitry Andric } 67*0fca6ea1SDimitry Andric 68*0fca6ea1SDimitry Andric const VarAndLoc &lookupDVID(DebugVariableID ID) const { return IdxToVar[ID]; } 69*0fca6ea1SDimitry Andric 70*0fca6ea1SDimitry Andric void clear() { 71*0fca6ea1SDimitry Andric VarToIdx.clear(); 72*0fca6ea1SDimitry Andric IdxToVar.clear(); 73*0fca6ea1SDimitry Andric } 74*0fca6ea1SDimitry Andric }; 75*0fca6ea1SDimitry Andric 76349cc55cSDimitry Andric /// Handle-class for a particular "location". This value-type uniquely 77349cc55cSDimitry Andric /// symbolises a register or stack location, allowing manipulation of locations 78349cc55cSDimitry Andric /// without concern for where that location is. Practically, this allows us to 79349cc55cSDimitry Andric /// treat the state of the machine at a particular point as an array of values, 80349cc55cSDimitry Andric /// rather than a map of values. 81349cc55cSDimitry Andric class LocIdx { 82349cc55cSDimitry Andric unsigned Location; 83349cc55cSDimitry Andric 84349cc55cSDimitry Andric // Default constructor is private, initializing to an illegal location number. 85349cc55cSDimitry Andric // Use only for "not an entry" elements in IndexedMaps. 86349cc55cSDimitry Andric LocIdx() : Location(UINT_MAX) {} 87349cc55cSDimitry Andric 88349cc55cSDimitry Andric public: 89349cc55cSDimitry Andric #define NUM_LOC_BITS 24 90349cc55cSDimitry Andric LocIdx(unsigned L) : Location(L) { 91349cc55cSDimitry Andric assert(L < (1 << NUM_LOC_BITS) && "Machine locations must fit in 24 bits"); 92349cc55cSDimitry Andric } 93349cc55cSDimitry Andric 94349cc55cSDimitry Andric static LocIdx MakeIllegalLoc() { return LocIdx(); } 95349cc55cSDimitry Andric static LocIdx MakeTombstoneLoc() { 96349cc55cSDimitry Andric LocIdx L = LocIdx(); 97349cc55cSDimitry Andric --L.Location; 98349cc55cSDimitry Andric return L; 99349cc55cSDimitry Andric } 100349cc55cSDimitry Andric 101349cc55cSDimitry Andric bool isIllegal() const { return Location == UINT_MAX; } 102349cc55cSDimitry Andric 103349cc55cSDimitry Andric uint64_t asU64() const { return Location; } 104349cc55cSDimitry Andric 105349cc55cSDimitry Andric bool operator==(unsigned L) const { return Location == L; } 106349cc55cSDimitry Andric 107349cc55cSDimitry Andric bool operator==(const LocIdx &L) const { return Location == L.Location; } 108349cc55cSDimitry Andric 109349cc55cSDimitry Andric bool operator!=(unsigned L) const { return !(*this == L); } 110349cc55cSDimitry Andric 111349cc55cSDimitry Andric bool operator!=(const LocIdx &L) const { return !(*this == L); } 112349cc55cSDimitry Andric 113349cc55cSDimitry Andric bool operator<(const LocIdx &Other) const { 114349cc55cSDimitry Andric return Location < Other.Location; 115349cc55cSDimitry Andric } 116349cc55cSDimitry Andric }; 117349cc55cSDimitry Andric 118349cc55cSDimitry Andric // The location at which a spilled value resides. It consists of a register and 119349cc55cSDimitry Andric // an offset. 120349cc55cSDimitry Andric struct SpillLoc { 121349cc55cSDimitry Andric unsigned SpillBase; 122349cc55cSDimitry Andric StackOffset SpillOffset; 123349cc55cSDimitry Andric bool operator==(const SpillLoc &Other) const { 124349cc55cSDimitry Andric return std::make_pair(SpillBase, SpillOffset) == 125349cc55cSDimitry Andric std::make_pair(Other.SpillBase, Other.SpillOffset); 126349cc55cSDimitry Andric } 127349cc55cSDimitry Andric bool operator<(const SpillLoc &Other) const { 128349cc55cSDimitry Andric return std::make_tuple(SpillBase, SpillOffset.getFixed(), 129349cc55cSDimitry Andric SpillOffset.getScalable()) < 130349cc55cSDimitry Andric std::make_tuple(Other.SpillBase, Other.SpillOffset.getFixed(), 131349cc55cSDimitry Andric Other.SpillOffset.getScalable()); 132349cc55cSDimitry Andric } 133349cc55cSDimitry Andric }; 134349cc55cSDimitry Andric 135349cc55cSDimitry Andric /// Unique identifier for a value defined by an instruction, as a value type. 136349cc55cSDimitry Andric /// Casts back and forth to a uint64_t. Probably replacable with something less 137349cc55cSDimitry Andric /// bit-constrained. Each value identifies the instruction and machine location 138349cc55cSDimitry Andric /// where the value is defined, although there may be no corresponding machine 139349cc55cSDimitry Andric /// operand for it (ex: regmasks clobbering values). The instructions are 140349cc55cSDimitry Andric /// one-based, and definitions that are PHIs have instruction number zero. 141349cc55cSDimitry Andric /// 142349cc55cSDimitry Andric /// The obvious limits of a 1M block function or 1M instruction blocks are 143349cc55cSDimitry Andric /// problematic; but by that point we should probably have bailed out of 144349cc55cSDimitry Andric /// trying to analyse the function. 145349cc55cSDimitry Andric class ValueIDNum { 146349cc55cSDimitry Andric union { 147349cc55cSDimitry Andric struct { 148349cc55cSDimitry Andric uint64_t BlockNo : 20; /// The block where the def happens. 149349cc55cSDimitry Andric uint64_t InstNo : 20; /// The Instruction where the def happens. 150349cc55cSDimitry Andric /// One based, is distance from start of block. 151349cc55cSDimitry Andric uint64_t LocNo 152349cc55cSDimitry Andric : NUM_LOC_BITS; /// The machine location where the def happens. 153349cc55cSDimitry Andric } s; 154349cc55cSDimitry Andric uint64_t Value; 155349cc55cSDimitry Andric } u; 156349cc55cSDimitry Andric 157349cc55cSDimitry Andric static_assert(sizeof(u) == 8, "Badly packed ValueIDNum?"); 158349cc55cSDimitry Andric 159349cc55cSDimitry Andric public: 160349cc55cSDimitry Andric // Default-initialize to EmptyValue. This is necessary to make IndexedMaps 161349cc55cSDimitry Andric // of values to work. 162349cc55cSDimitry Andric ValueIDNum() { u.Value = EmptyValue.asU64(); } 163349cc55cSDimitry Andric 164349cc55cSDimitry Andric ValueIDNum(uint64_t Block, uint64_t Inst, uint64_t Loc) { 165349cc55cSDimitry Andric u.s = {Block, Inst, Loc}; 166349cc55cSDimitry Andric } 167349cc55cSDimitry Andric 168349cc55cSDimitry Andric ValueIDNum(uint64_t Block, uint64_t Inst, LocIdx Loc) { 169349cc55cSDimitry Andric u.s = {Block, Inst, Loc.asU64()}; 170349cc55cSDimitry Andric } 171349cc55cSDimitry Andric 172349cc55cSDimitry Andric uint64_t getBlock() const { return u.s.BlockNo; } 173349cc55cSDimitry Andric uint64_t getInst() const { return u.s.InstNo; } 174349cc55cSDimitry Andric uint64_t getLoc() const { return u.s.LocNo; } 175349cc55cSDimitry Andric bool isPHI() const { return u.s.InstNo == 0; } 176349cc55cSDimitry Andric 177349cc55cSDimitry Andric uint64_t asU64() const { return u.Value; } 178349cc55cSDimitry Andric 179349cc55cSDimitry Andric static ValueIDNum fromU64(uint64_t v) { 180349cc55cSDimitry Andric ValueIDNum Val; 181349cc55cSDimitry Andric Val.u.Value = v; 182349cc55cSDimitry Andric return Val; 183349cc55cSDimitry Andric } 184349cc55cSDimitry Andric 185349cc55cSDimitry Andric bool operator<(const ValueIDNum &Other) const { 186349cc55cSDimitry Andric return asU64() < Other.asU64(); 187349cc55cSDimitry Andric } 188349cc55cSDimitry Andric 189349cc55cSDimitry Andric bool operator==(const ValueIDNum &Other) const { 190349cc55cSDimitry Andric return u.Value == Other.u.Value; 191349cc55cSDimitry Andric } 192349cc55cSDimitry Andric 193349cc55cSDimitry Andric bool operator!=(const ValueIDNum &Other) const { return !(*this == Other); } 194349cc55cSDimitry Andric 195349cc55cSDimitry Andric std::string asString(const std::string &mlocname) const { 196349cc55cSDimitry Andric return Twine("Value{bb: ") 197349cc55cSDimitry Andric .concat(Twine(u.s.BlockNo) 198349cc55cSDimitry Andric .concat(Twine(", inst: ") 199349cc55cSDimitry Andric .concat((u.s.InstNo ? Twine(u.s.InstNo) 200349cc55cSDimitry Andric : Twine("live-in")) 201349cc55cSDimitry Andric .concat(Twine(", loc: ").concat( 202349cc55cSDimitry Andric Twine(mlocname))) 203349cc55cSDimitry Andric .concat(Twine("}"))))) 204349cc55cSDimitry Andric .str(); 205349cc55cSDimitry Andric } 206349cc55cSDimitry Andric 207349cc55cSDimitry Andric static ValueIDNum EmptyValue; 208349cc55cSDimitry Andric static ValueIDNum TombstoneValue; 209349cc55cSDimitry Andric }; 210349cc55cSDimitry Andric 211bdd1243dSDimitry Andric } // End namespace LiveDebugValues 212bdd1243dSDimitry Andric 213bdd1243dSDimitry Andric namespace llvm { 214bdd1243dSDimitry Andric using namespace LiveDebugValues; 215bdd1243dSDimitry Andric 216bdd1243dSDimitry Andric template <> struct DenseMapInfo<LocIdx> { 217bdd1243dSDimitry Andric static inline LocIdx getEmptyKey() { return LocIdx::MakeIllegalLoc(); } 218bdd1243dSDimitry Andric static inline LocIdx getTombstoneKey() { return LocIdx::MakeTombstoneLoc(); } 219bdd1243dSDimitry Andric 220bdd1243dSDimitry Andric static unsigned getHashValue(const LocIdx &Loc) { return Loc.asU64(); } 221bdd1243dSDimitry Andric 222bdd1243dSDimitry Andric static bool isEqual(const LocIdx &A, const LocIdx &B) { return A == B; } 223bdd1243dSDimitry Andric }; 224bdd1243dSDimitry Andric 225bdd1243dSDimitry Andric template <> struct DenseMapInfo<ValueIDNum> { 226bdd1243dSDimitry Andric static inline ValueIDNum getEmptyKey() { return ValueIDNum::EmptyValue; } 227bdd1243dSDimitry Andric static inline ValueIDNum getTombstoneKey() { 228bdd1243dSDimitry Andric return ValueIDNum::TombstoneValue; 229bdd1243dSDimitry Andric } 230bdd1243dSDimitry Andric 231bdd1243dSDimitry Andric static unsigned getHashValue(const ValueIDNum &Val) { 232bdd1243dSDimitry Andric return hash_value(Val.asU64()); 233bdd1243dSDimitry Andric } 234bdd1243dSDimitry Andric 235bdd1243dSDimitry Andric static bool isEqual(const ValueIDNum &A, const ValueIDNum &B) { 236bdd1243dSDimitry Andric return A == B; 237bdd1243dSDimitry Andric } 238bdd1243dSDimitry Andric }; 239bdd1243dSDimitry Andric 240bdd1243dSDimitry Andric } // end namespace llvm 241bdd1243dSDimitry Andric 242bdd1243dSDimitry Andric namespace LiveDebugValues { 243bdd1243dSDimitry Andric using namespace llvm; 244bdd1243dSDimitry Andric 24581ad6265SDimitry Andric /// Type for a table of values in a block. 2465f757f3fSDimitry Andric using ValueTable = SmallVector<ValueIDNum, 0>; 24781ad6265SDimitry Andric 248cb14a3feSDimitry Andric /// A collection of ValueTables, one per BB in a function, with convenient 249cb14a3feSDimitry Andric /// accessor methods. 250cb14a3feSDimitry Andric struct FuncValueTable { 251cb14a3feSDimitry Andric FuncValueTable(int NumBBs, int NumLocs) { 252cb14a3feSDimitry Andric Storage.reserve(NumBBs); 253cb14a3feSDimitry Andric for (int i = 0; i != NumBBs; ++i) 254cb14a3feSDimitry Andric Storage.push_back( 255cb14a3feSDimitry Andric std::make_unique<ValueTable>(NumLocs, ValueIDNum::EmptyValue)); 256cb14a3feSDimitry Andric } 257cb14a3feSDimitry Andric 258cb14a3feSDimitry Andric /// Returns the ValueTable associated with MBB. 259cb14a3feSDimitry Andric ValueTable &operator[](const MachineBasicBlock &MBB) const { 260cb14a3feSDimitry Andric return (*this)[MBB.getNumber()]; 261cb14a3feSDimitry Andric } 262cb14a3feSDimitry Andric 263cb14a3feSDimitry Andric /// Returns the ValueTable associated with the MachineBasicBlock whose number 264cb14a3feSDimitry Andric /// is MBBNum. 265cb14a3feSDimitry Andric ValueTable &operator[](int MBBNum) const { 266cb14a3feSDimitry Andric auto &TablePtr = Storage[MBBNum]; 267cb14a3feSDimitry Andric assert(TablePtr && "Trying to access a deleted table"); 268cb14a3feSDimitry Andric return *TablePtr; 269cb14a3feSDimitry Andric } 270cb14a3feSDimitry Andric 271cb14a3feSDimitry Andric /// Returns the ValueTable associated with the entry MachineBasicBlock. 272cb14a3feSDimitry Andric ValueTable &tableForEntryMBB() const { return (*this)[0]; } 273cb14a3feSDimitry Andric 274cb14a3feSDimitry Andric /// Returns true if the ValueTable associated with MBB has not been freed. 275cb14a3feSDimitry Andric bool hasTableFor(MachineBasicBlock &MBB) const { 276cb14a3feSDimitry Andric return Storage[MBB.getNumber()] != nullptr; 277cb14a3feSDimitry Andric } 278cb14a3feSDimitry Andric 279cb14a3feSDimitry Andric /// Frees the memory of the ValueTable associated with MBB. 280cb14a3feSDimitry Andric void ejectTableForBlock(const MachineBasicBlock &MBB) { 281cb14a3feSDimitry Andric Storage[MBB.getNumber()].reset(); 282cb14a3feSDimitry Andric } 283cb14a3feSDimitry Andric 284cb14a3feSDimitry Andric private: 285cb14a3feSDimitry Andric /// ValueTables are stored as unique_ptrs to allow for deallocation during 286cb14a3feSDimitry Andric /// LDV; this was measured to have a significant impact on compiler memory 287cb14a3feSDimitry Andric /// usage. 288cb14a3feSDimitry Andric SmallVector<std::unique_ptr<ValueTable>, 0> Storage; 289cb14a3feSDimitry Andric }; 29081ad6265SDimitry Andric 291349cc55cSDimitry Andric /// Thin wrapper around an integer -- designed to give more type safety to 292349cc55cSDimitry Andric /// spill location numbers. 293349cc55cSDimitry Andric class SpillLocationNo { 294349cc55cSDimitry Andric public: 295349cc55cSDimitry Andric explicit SpillLocationNo(unsigned SpillNo) : SpillNo(SpillNo) {} 296349cc55cSDimitry Andric unsigned SpillNo; 297349cc55cSDimitry Andric unsigned id() const { return SpillNo; } 298349cc55cSDimitry Andric 299349cc55cSDimitry Andric bool operator<(const SpillLocationNo &Other) const { 300349cc55cSDimitry Andric return SpillNo < Other.SpillNo; 301349cc55cSDimitry Andric } 302349cc55cSDimitry Andric 303349cc55cSDimitry Andric bool operator==(const SpillLocationNo &Other) const { 304349cc55cSDimitry Andric return SpillNo == Other.SpillNo; 305349cc55cSDimitry Andric } 306349cc55cSDimitry Andric bool operator!=(const SpillLocationNo &Other) const { 307349cc55cSDimitry Andric return !(*this == Other); 308349cc55cSDimitry Andric } 309349cc55cSDimitry Andric }; 310349cc55cSDimitry Andric 311349cc55cSDimitry Andric /// Meta qualifiers for a value. Pair of whatever expression is used to qualify 31281ad6265SDimitry Andric /// the value, and Boolean of whether or not it's indirect. 313349cc55cSDimitry Andric class DbgValueProperties { 314349cc55cSDimitry Andric public: 315bdd1243dSDimitry Andric DbgValueProperties(const DIExpression *DIExpr, bool Indirect, bool IsVariadic) 316bdd1243dSDimitry Andric : DIExpr(DIExpr), Indirect(Indirect), IsVariadic(IsVariadic) {} 317349cc55cSDimitry Andric 318349cc55cSDimitry Andric /// Extract properties from an existing DBG_VALUE instruction. 319349cc55cSDimitry Andric DbgValueProperties(const MachineInstr &MI) { 320349cc55cSDimitry Andric assert(MI.isDebugValue()); 321bdd1243dSDimitry Andric assert(MI.getDebugExpression()->getNumLocationOperands() == 0 || 322bdd1243dSDimitry Andric MI.isDebugValueList() || MI.isUndefDebugValue()); 323bdd1243dSDimitry Andric IsVariadic = MI.isDebugValueList(); 324349cc55cSDimitry Andric DIExpr = MI.getDebugExpression(); 325bdd1243dSDimitry Andric Indirect = MI.isDebugOffsetImm(); 326bdd1243dSDimitry Andric } 327bdd1243dSDimitry Andric 328bdd1243dSDimitry Andric bool isJoinable(const DbgValueProperties &Other) const { 329bdd1243dSDimitry Andric return DIExpression::isEqualExpression(DIExpr, Indirect, Other.DIExpr, 330bdd1243dSDimitry Andric Other.Indirect); 331349cc55cSDimitry Andric } 332349cc55cSDimitry Andric 333349cc55cSDimitry Andric bool operator==(const DbgValueProperties &Other) const { 334bdd1243dSDimitry Andric return std::tie(DIExpr, Indirect, IsVariadic) == 335bdd1243dSDimitry Andric std::tie(Other.DIExpr, Other.Indirect, Other.IsVariadic); 336349cc55cSDimitry Andric } 337349cc55cSDimitry Andric 338349cc55cSDimitry Andric bool operator!=(const DbgValueProperties &Other) const { 339349cc55cSDimitry Andric return !(*this == Other); 340349cc55cSDimitry Andric } 341349cc55cSDimitry Andric 342bdd1243dSDimitry Andric unsigned getLocationOpCount() const { 343bdd1243dSDimitry Andric return IsVariadic ? DIExpr->getNumLocationOperands() : 1; 344bdd1243dSDimitry Andric } 345bdd1243dSDimitry Andric 346349cc55cSDimitry Andric const DIExpression *DIExpr; 347349cc55cSDimitry Andric bool Indirect; 348bdd1243dSDimitry Andric bool IsVariadic; 349349cc55cSDimitry Andric }; 350349cc55cSDimitry Andric 351bdd1243dSDimitry Andric /// TODO: Might pack better if we changed this to a Struct of Arrays, since 352bdd1243dSDimitry Andric /// MachineOperand is width 32, making this struct width 33. We could also 353bdd1243dSDimitry Andric /// potentially avoid storing the whole MachineOperand (sizeof=32), instead 354bdd1243dSDimitry Andric /// choosing to store just the contents portion (sizeof=8) and a Kind enum, 355bdd1243dSDimitry Andric /// since we already know it is some type of immediate value. 356bdd1243dSDimitry Andric /// Stores a single debug operand, which can either be a MachineOperand for 357bdd1243dSDimitry Andric /// directly storing immediate values, or a ValueIDNum representing some value 358bdd1243dSDimitry Andric /// computed at some point in the program. IsConst is used as a discriminator. 359bdd1243dSDimitry Andric struct DbgOp { 360bdd1243dSDimitry Andric union { 361bdd1243dSDimitry Andric ValueIDNum ID; 362bdd1243dSDimitry Andric MachineOperand MO; 363bdd1243dSDimitry Andric }; 364bdd1243dSDimitry Andric bool IsConst; 365bdd1243dSDimitry Andric 366bdd1243dSDimitry Andric DbgOp() : ID(ValueIDNum::EmptyValue), IsConst(false) {} 367bdd1243dSDimitry Andric DbgOp(ValueIDNum ID) : ID(ID), IsConst(false) {} 368bdd1243dSDimitry Andric DbgOp(MachineOperand MO) : MO(MO), IsConst(true) {} 369bdd1243dSDimitry Andric 370bdd1243dSDimitry Andric bool isUndef() const { return !IsConst && ID == ValueIDNum::EmptyValue; } 371bdd1243dSDimitry Andric 372bdd1243dSDimitry Andric #ifndef NDEBUG 373bdd1243dSDimitry Andric void dump(const MLocTracker *MTrack) const; 374bdd1243dSDimitry Andric #endif 375bdd1243dSDimitry Andric }; 376bdd1243dSDimitry Andric 377bdd1243dSDimitry Andric /// A DbgOp whose ID (if any) has resolved to an actual location, LocIdx. Used 378bdd1243dSDimitry Andric /// when working with concrete debug values, i.e. when joining MLocs and VLocs 379bdd1243dSDimitry Andric /// in the TransferTracker or emitting DBG_VALUE/DBG_VALUE_LIST instructions in 380bdd1243dSDimitry Andric /// the MLocTracker. 381bdd1243dSDimitry Andric struct ResolvedDbgOp { 382bdd1243dSDimitry Andric union { 383bdd1243dSDimitry Andric LocIdx Loc; 384bdd1243dSDimitry Andric MachineOperand MO; 385bdd1243dSDimitry Andric }; 386bdd1243dSDimitry Andric bool IsConst; 387bdd1243dSDimitry Andric 388bdd1243dSDimitry Andric ResolvedDbgOp(LocIdx Loc) : Loc(Loc), IsConst(false) {} 389bdd1243dSDimitry Andric ResolvedDbgOp(MachineOperand MO) : MO(MO), IsConst(true) {} 390bdd1243dSDimitry Andric 391bdd1243dSDimitry Andric bool operator==(const ResolvedDbgOp &Other) const { 392bdd1243dSDimitry Andric if (IsConst != Other.IsConst) 393bdd1243dSDimitry Andric return false; 394bdd1243dSDimitry Andric if (IsConst) 395bdd1243dSDimitry Andric return MO.isIdenticalTo(Other.MO); 396bdd1243dSDimitry Andric return Loc == Other.Loc; 397bdd1243dSDimitry Andric } 398bdd1243dSDimitry Andric 399bdd1243dSDimitry Andric #ifndef NDEBUG 400bdd1243dSDimitry Andric void dump(const MLocTracker *MTrack) const; 401bdd1243dSDimitry Andric #endif 402bdd1243dSDimitry Andric }; 403bdd1243dSDimitry Andric 404bdd1243dSDimitry Andric /// An ID used in the DbgOpIDMap (below) to lookup a stored DbgOp. This is used 405bdd1243dSDimitry Andric /// in place of actual DbgOps inside of a DbgValue to reduce its size, as 406bdd1243dSDimitry Andric /// DbgValue is very frequently used and passed around, and the actual DbgOp is 407bdd1243dSDimitry Andric /// over 8x larger than this class, due to storing a MachineOperand. This ID 408bdd1243dSDimitry Andric /// should be equal for all equal DbgOps, and also encodes whether the mapped 409bdd1243dSDimitry Andric /// DbgOp is a constant, meaning that for simple equality or const-ness checks 410bdd1243dSDimitry Andric /// it is not necessary to lookup this ID. 411bdd1243dSDimitry Andric struct DbgOpID { 412bdd1243dSDimitry Andric struct IsConstIndexPair { 413bdd1243dSDimitry Andric uint32_t IsConst : 1; 414bdd1243dSDimitry Andric uint32_t Index : 31; 415bdd1243dSDimitry Andric }; 416bdd1243dSDimitry Andric 417bdd1243dSDimitry Andric union { 418bdd1243dSDimitry Andric struct IsConstIndexPair ID; 419bdd1243dSDimitry Andric uint32_t RawID; 420bdd1243dSDimitry Andric }; 421bdd1243dSDimitry Andric 422bdd1243dSDimitry Andric DbgOpID() : RawID(UndefID.RawID) { 423bdd1243dSDimitry Andric static_assert(sizeof(DbgOpID) == 4, "DbgOpID should fit within 4 bytes."); 424bdd1243dSDimitry Andric } 425bdd1243dSDimitry Andric DbgOpID(uint32_t RawID) : RawID(RawID) {} 426bdd1243dSDimitry Andric DbgOpID(bool IsConst, uint32_t Index) : ID({IsConst, Index}) {} 427bdd1243dSDimitry Andric 428bdd1243dSDimitry Andric static DbgOpID UndefID; 429bdd1243dSDimitry Andric 430bdd1243dSDimitry Andric bool operator==(const DbgOpID &Other) const { return RawID == Other.RawID; } 431bdd1243dSDimitry Andric bool operator!=(const DbgOpID &Other) const { return !(*this == Other); } 432bdd1243dSDimitry Andric 433bdd1243dSDimitry Andric uint32_t asU32() const { return RawID; } 434bdd1243dSDimitry Andric 435bdd1243dSDimitry Andric bool isUndef() const { return *this == UndefID; } 436bdd1243dSDimitry Andric bool isConst() const { return ID.IsConst && !isUndef(); } 437bdd1243dSDimitry Andric uint32_t getIndex() const { return ID.Index; } 438bdd1243dSDimitry Andric 439bdd1243dSDimitry Andric #ifndef NDEBUG 440bdd1243dSDimitry Andric void dump(const MLocTracker *MTrack, const DbgOpIDMap *OpStore) const; 441bdd1243dSDimitry Andric #endif 442bdd1243dSDimitry Andric }; 443bdd1243dSDimitry Andric 444bdd1243dSDimitry Andric /// Class storing the complete set of values that are observed by DbgValues 445bdd1243dSDimitry Andric /// within the current function. Allows 2-way lookup, with `find` returning the 446bdd1243dSDimitry Andric /// Op for a given ID and `insert` returning the ID for a given Op (creating one 447bdd1243dSDimitry Andric /// if none exists). 448bdd1243dSDimitry Andric class DbgOpIDMap { 449bdd1243dSDimitry Andric 450bdd1243dSDimitry Andric SmallVector<ValueIDNum, 0> ValueOps; 451bdd1243dSDimitry Andric SmallVector<MachineOperand, 0> ConstOps; 452bdd1243dSDimitry Andric 453bdd1243dSDimitry Andric DenseMap<ValueIDNum, DbgOpID> ValueOpToID; 454bdd1243dSDimitry Andric DenseMap<MachineOperand, DbgOpID> ConstOpToID; 455bdd1243dSDimitry Andric 456bdd1243dSDimitry Andric public: 457bdd1243dSDimitry Andric /// If \p Op does not already exist in this map, it is inserted and the 458bdd1243dSDimitry Andric /// corresponding DbgOpID is returned. If Op already exists in this map, then 459bdd1243dSDimitry Andric /// no change is made and the existing ID for Op is returned. 460bdd1243dSDimitry Andric /// Calling this with the undef DbgOp will always return DbgOpID::UndefID. 461bdd1243dSDimitry Andric DbgOpID insert(DbgOp Op) { 462bdd1243dSDimitry Andric if (Op.isUndef()) 463bdd1243dSDimitry Andric return DbgOpID::UndefID; 464bdd1243dSDimitry Andric if (Op.IsConst) 465bdd1243dSDimitry Andric return insertConstOp(Op.MO); 466bdd1243dSDimitry Andric return insertValueOp(Op.ID); 467bdd1243dSDimitry Andric } 468bdd1243dSDimitry Andric /// Returns the DbgOp associated with \p ID. Should only be used for IDs 469bdd1243dSDimitry Andric /// returned from calling `insert` from this map or DbgOpID::UndefID. 470bdd1243dSDimitry Andric DbgOp find(DbgOpID ID) const { 471bdd1243dSDimitry Andric if (ID == DbgOpID::UndefID) 472bdd1243dSDimitry Andric return DbgOp(); 473bdd1243dSDimitry Andric if (ID.isConst()) 474bdd1243dSDimitry Andric return DbgOp(ConstOps[ID.getIndex()]); 475bdd1243dSDimitry Andric return DbgOp(ValueOps[ID.getIndex()]); 476bdd1243dSDimitry Andric } 477bdd1243dSDimitry Andric 478bdd1243dSDimitry Andric void clear() { 479bdd1243dSDimitry Andric ValueOps.clear(); 480bdd1243dSDimitry Andric ConstOps.clear(); 481bdd1243dSDimitry Andric ValueOpToID.clear(); 482bdd1243dSDimitry Andric ConstOpToID.clear(); 483bdd1243dSDimitry Andric } 484bdd1243dSDimitry Andric 485bdd1243dSDimitry Andric private: 486bdd1243dSDimitry Andric DbgOpID insertConstOp(MachineOperand &MO) { 487bdd1243dSDimitry Andric auto ExistingIt = ConstOpToID.find(MO); 488bdd1243dSDimitry Andric if (ExistingIt != ConstOpToID.end()) 489bdd1243dSDimitry Andric return ExistingIt->second; 490bdd1243dSDimitry Andric DbgOpID ID(true, ConstOps.size()); 491bdd1243dSDimitry Andric ConstOpToID.insert(std::make_pair(MO, ID)); 492bdd1243dSDimitry Andric ConstOps.push_back(MO); 493bdd1243dSDimitry Andric return ID; 494bdd1243dSDimitry Andric } 495bdd1243dSDimitry Andric DbgOpID insertValueOp(ValueIDNum VID) { 496bdd1243dSDimitry Andric auto ExistingIt = ValueOpToID.find(VID); 497bdd1243dSDimitry Andric if (ExistingIt != ValueOpToID.end()) 498bdd1243dSDimitry Andric return ExistingIt->second; 499bdd1243dSDimitry Andric DbgOpID ID(false, ValueOps.size()); 500bdd1243dSDimitry Andric ValueOpToID.insert(std::make_pair(VID, ID)); 501bdd1243dSDimitry Andric ValueOps.push_back(VID); 502bdd1243dSDimitry Andric return ID; 503bdd1243dSDimitry Andric } 504bdd1243dSDimitry Andric }; 505bdd1243dSDimitry Andric 506bdd1243dSDimitry Andric // We set the maximum number of operands that we will handle to keep DbgValue 507bdd1243dSDimitry Andric // within a reasonable size (64 bytes), as we store and pass a lot of them 508bdd1243dSDimitry Andric // around. 509bdd1243dSDimitry Andric #define MAX_DBG_OPS 8 510bdd1243dSDimitry Andric 511bdd1243dSDimitry Andric /// Class recording the (high level) _value_ of a variable. Identifies the value 512bdd1243dSDimitry Andric /// of the variable as a list of ValueIDNums and constant MachineOperands, or as 513bdd1243dSDimitry Andric /// an empty list for undef debug values or VPHI values which we have not found 514bdd1243dSDimitry Andric /// valid locations for. 515349cc55cSDimitry Andric /// This class also stores meta-information about how the value is qualified. 516349cc55cSDimitry Andric /// Used to reason about variable values when performing the second 517349cc55cSDimitry Andric /// (DebugVariable specific) dataflow analysis. 518349cc55cSDimitry Andric class DbgValue { 519bdd1243dSDimitry Andric private: 520bdd1243dSDimitry Andric /// If Kind is Def or VPHI, the set of IDs corresponding to the DbgOps that 521bdd1243dSDimitry Andric /// are used. VPHIs set every ID to EmptyID when we have not found a valid 522bdd1243dSDimitry Andric /// machine-value for every operand, and sets them to the corresponding 523bdd1243dSDimitry Andric /// machine-values when we have found all of them. 524bdd1243dSDimitry Andric DbgOpID DbgOps[MAX_DBG_OPS]; 525bdd1243dSDimitry Andric unsigned OpCount; 526bdd1243dSDimitry Andric 527349cc55cSDimitry Andric public: 528349cc55cSDimitry Andric /// For a NoVal or VPHI DbgValue, which block it was generated in. 529349cc55cSDimitry Andric int BlockNo; 530349cc55cSDimitry Andric 531349cc55cSDimitry Andric /// Qualifiers for the ValueIDNum above. 532349cc55cSDimitry Andric DbgValueProperties Properties; 533349cc55cSDimitry Andric 534349cc55cSDimitry Andric typedef enum { 535349cc55cSDimitry Andric Undef, // Represents a DBG_VALUE $noreg in the transfer function only. 536bdd1243dSDimitry Andric Def, // This value is defined by some combination of constants, 537bdd1243dSDimitry Andric // instructions, or PHI values. 538349cc55cSDimitry Andric VPHI, // Incoming values to BlockNo differ, those values must be joined by 539349cc55cSDimitry Andric // a PHI in this block. 540349cc55cSDimitry Andric NoVal, // Empty DbgValue indicating an unknown value. Used as initializer, 541349cc55cSDimitry Andric // before dominating blocks values are propagated in. 542349cc55cSDimitry Andric } KindT; 543349cc55cSDimitry Andric /// Discriminator for whether this is a constant or an in-program value. 544349cc55cSDimitry Andric KindT Kind; 545349cc55cSDimitry Andric 546bdd1243dSDimitry Andric DbgValue(ArrayRef<DbgOpID> DbgOps, const DbgValueProperties &Prop) 547bdd1243dSDimitry Andric : OpCount(DbgOps.size()), BlockNo(0), Properties(Prop), Kind(Def) { 548bdd1243dSDimitry Andric static_assert(sizeof(DbgValue) <= 64, 549bdd1243dSDimitry Andric "DbgValue should fit within 64 bytes."); 550bdd1243dSDimitry Andric assert(DbgOps.size() == Prop.getLocationOpCount()); 551bdd1243dSDimitry Andric if (DbgOps.size() > MAX_DBG_OPS || 552bdd1243dSDimitry Andric any_of(DbgOps, [](DbgOpID ID) { return ID.isUndef(); })) { 553bdd1243dSDimitry Andric Kind = Undef; 554bdd1243dSDimitry Andric OpCount = 0; 555bdd1243dSDimitry Andric #define DEBUG_TYPE "LiveDebugValues" 556bdd1243dSDimitry Andric if (DbgOps.size() > MAX_DBG_OPS) { 557bdd1243dSDimitry Andric LLVM_DEBUG(dbgs() << "Found DbgValue with more than maximum allowed " 558bdd1243dSDimitry Andric "operands.\n"); 559bdd1243dSDimitry Andric } 560bdd1243dSDimitry Andric #undef DEBUG_TYPE 561bdd1243dSDimitry Andric } else { 562bdd1243dSDimitry Andric for (unsigned Idx = 0; Idx < DbgOps.size(); ++Idx) 563bdd1243dSDimitry Andric this->DbgOps[Idx] = DbgOps[Idx]; 564bdd1243dSDimitry Andric } 565349cc55cSDimitry Andric } 566349cc55cSDimitry Andric 567349cc55cSDimitry Andric DbgValue(unsigned BlockNo, const DbgValueProperties &Prop, KindT Kind) 568bdd1243dSDimitry Andric : OpCount(0), BlockNo(BlockNo), Properties(Prop), Kind(Kind) { 569349cc55cSDimitry Andric assert(Kind == NoVal || Kind == VPHI); 570349cc55cSDimitry Andric } 571349cc55cSDimitry Andric 572349cc55cSDimitry Andric DbgValue(const DbgValueProperties &Prop, KindT Kind) 573bdd1243dSDimitry Andric : OpCount(0), BlockNo(0), Properties(Prop), Kind(Kind) { 574349cc55cSDimitry Andric assert(Kind == Undef && 575349cc55cSDimitry Andric "Empty DbgValue constructor must pass in Undef kind"); 576349cc55cSDimitry Andric } 577349cc55cSDimitry Andric 578349cc55cSDimitry Andric #ifndef NDEBUG 579bdd1243dSDimitry Andric void dump(const MLocTracker *MTrack = nullptr, 580bdd1243dSDimitry Andric const DbgOpIDMap *OpStore = nullptr) const; 581349cc55cSDimitry Andric #endif 582349cc55cSDimitry Andric 583349cc55cSDimitry Andric bool operator==(const DbgValue &Other) const { 584349cc55cSDimitry Andric if (std::tie(Kind, Properties) != std::tie(Other.Kind, Other.Properties)) 585349cc55cSDimitry Andric return false; 586bdd1243dSDimitry Andric else if (Kind == Def && !equal(getDbgOpIDs(), Other.getDbgOpIDs())) 587349cc55cSDimitry Andric return false; 588349cc55cSDimitry Andric else if (Kind == NoVal && BlockNo != Other.BlockNo) 589349cc55cSDimitry Andric return false; 590349cc55cSDimitry Andric else if (Kind == VPHI && BlockNo != Other.BlockNo) 591349cc55cSDimitry Andric return false; 592bdd1243dSDimitry Andric else if (Kind == VPHI && !equal(getDbgOpIDs(), Other.getDbgOpIDs())) 593349cc55cSDimitry Andric return false; 594349cc55cSDimitry Andric 595349cc55cSDimitry Andric return true; 596349cc55cSDimitry Andric } 597349cc55cSDimitry Andric 598349cc55cSDimitry Andric bool operator!=(const DbgValue &Other) const { return !(*this == Other); } 599bdd1243dSDimitry Andric 600bdd1243dSDimitry Andric // Returns an array of all the machine values used to calculate this variable 601bdd1243dSDimitry Andric // value, or an empty list for an Undef or unjoined VPHI. 602bdd1243dSDimitry Andric ArrayRef<DbgOpID> getDbgOpIDs() const { return {DbgOps, OpCount}; } 603bdd1243dSDimitry Andric 604bdd1243dSDimitry Andric // Returns either DbgOps[Index] if this DbgValue has Debug Operands, or 605bdd1243dSDimitry Andric // the ID for ValueIDNum::EmptyValue otherwise (i.e. if this is an Undef, 606bdd1243dSDimitry Andric // NoVal, or an unjoined VPHI). 607bdd1243dSDimitry Andric DbgOpID getDbgOpID(unsigned Index) const { 608bdd1243dSDimitry Andric if (!OpCount) 609bdd1243dSDimitry Andric return DbgOpID::UndefID; 610bdd1243dSDimitry Andric assert(Index < OpCount); 611bdd1243dSDimitry Andric return DbgOps[Index]; 612bdd1243dSDimitry Andric } 613bdd1243dSDimitry Andric // Replaces this DbgValue's existing DbgOpIDs (if any) with the contents of 614bdd1243dSDimitry Andric // \p NewIDs. The number of DbgOpIDs passed must be equal to the number of 615bdd1243dSDimitry Andric // arguments expected by this DbgValue's properties (the return value of 616bdd1243dSDimitry Andric // `getLocationOpCount()`). 617bdd1243dSDimitry Andric void setDbgOpIDs(ArrayRef<DbgOpID> NewIDs) { 618bdd1243dSDimitry Andric // We can go from no ops to some ops, but not from some ops to no ops. 619bdd1243dSDimitry Andric assert(NewIDs.size() == getLocationOpCount() && 620bdd1243dSDimitry Andric "Incorrect number of Debug Operands for this DbgValue."); 621bdd1243dSDimitry Andric OpCount = NewIDs.size(); 622bdd1243dSDimitry Andric for (unsigned Idx = 0; Idx < NewIDs.size(); ++Idx) 623bdd1243dSDimitry Andric DbgOps[Idx] = NewIDs[Idx]; 624bdd1243dSDimitry Andric } 625bdd1243dSDimitry Andric 626bdd1243dSDimitry Andric // The number of debug operands expected by this DbgValue's expression. 627bdd1243dSDimitry Andric // getDbgOpIDs() should return an array of this length, unless this is an 628bdd1243dSDimitry Andric // Undef or an unjoined VPHI. 629bdd1243dSDimitry Andric unsigned getLocationOpCount() const { 630bdd1243dSDimitry Andric return Properties.getLocationOpCount(); 631bdd1243dSDimitry Andric } 632bdd1243dSDimitry Andric 633bdd1243dSDimitry Andric // Returns true if this or Other are unjoined PHIs, which do not have defined 634bdd1243dSDimitry Andric // Loc Ops, or if the `n`th Loc Op for this has a different constness to the 635bdd1243dSDimitry Andric // `n`th Loc Op for Other. 636bdd1243dSDimitry Andric bool hasJoinableLocOps(const DbgValue &Other) const { 637bdd1243dSDimitry Andric if (isUnjoinedPHI() || Other.isUnjoinedPHI()) 638bdd1243dSDimitry Andric return true; 639bdd1243dSDimitry Andric for (unsigned Idx = 0; Idx < getLocationOpCount(); ++Idx) { 640bdd1243dSDimitry Andric if (getDbgOpID(Idx).isConst() != Other.getDbgOpID(Idx).isConst()) 641bdd1243dSDimitry Andric return false; 642bdd1243dSDimitry Andric } 643bdd1243dSDimitry Andric return true; 644bdd1243dSDimitry Andric } 645bdd1243dSDimitry Andric 646bdd1243dSDimitry Andric bool isUnjoinedPHI() const { return Kind == VPHI && OpCount == 0; } 647bdd1243dSDimitry Andric 648bdd1243dSDimitry Andric bool hasIdenticalValidLocOps(const DbgValue &Other) const { 649bdd1243dSDimitry Andric if (!OpCount) 650bdd1243dSDimitry Andric return false; 651bdd1243dSDimitry Andric return equal(getDbgOpIDs(), Other.getDbgOpIDs()); 652bdd1243dSDimitry Andric } 653349cc55cSDimitry Andric }; 654349cc55cSDimitry Andric 655349cc55cSDimitry Andric class LocIdxToIndexFunctor { 656349cc55cSDimitry Andric public: 657349cc55cSDimitry Andric using argument_type = LocIdx; 658349cc55cSDimitry Andric unsigned operator()(const LocIdx &L) const { return L.asU64(); } 659349cc55cSDimitry Andric }; 660349cc55cSDimitry Andric 661349cc55cSDimitry Andric /// Tracker for what values are in machine locations. Listens to the Things 662349cc55cSDimitry Andric /// being Done by various instructions, and maintains a table of what machine 663349cc55cSDimitry Andric /// locations have what values (as defined by a ValueIDNum). 664349cc55cSDimitry Andric /// 665349cc55cSDimitry Andric /// There are potentially a much larger number of machine locations on the 666349cc55cSDimitry Andric /// target machine than the actual working-set size of the function. On x86 for 667349cc55cSDimitry Andric /// example, we're extremely unlikely to want to track values through control 668349cc55cSDimitry Andric /// or debug registers. To avoid doing so, MLocTracker has several layers of 669349cc55cSDimitry Andric /// indirection going on, described below, to avoid unnecessarily tracking 670349cc55cSDimitry Andric /// any location. 671349cc55cSDimitry Andric /// 672349cc55cSDimitry Andric /// Here's a sort of diagram of the indexes, read from the bottom up: 673349cc55cSDimitry Andric /// 674349cc55cSDimitry Andric /// Size on stack Offset on stack 675349cc55cSDimitry Andric /// \ / 676349cc55cSDimitry Andric /// Stack Idx (Where in slot is this?) 677349cc55cSDimitry Andric /// / 678349cc55cSDimitry Andric /// / 679349cc55cSDimitry Andric /// Slot Num (%stack.0) / 680349cc55cSDimitry Andric /// FrameIdx => SpillNum / 681349cc55cSDimitry Andric /// \ / 682349cc55cSDimitry Andric /// SpillID (int) Register number (int) 683349cc55cSDimitry Andric /// \ / 684349cc55cSDimitry Andric /// LocationID => LocIdx 685349cc55cSDimitry Andric /// | 686349cc55cSDimitry Andric /// LocIdx => ValueIDNum 687349cc55cSDimitry Andric /// 688349cc55cSDimitry Andric /// The aim here is that the LocIdx => ValueIDNum vector is just an array of 689349cc55cSDimitry Andric /// values in numbered locations, so that later analyses can ignore whether the 690349cc55cSDimitry Andric /// location is a register or otherwise. To map a register / spill location to 691349cc55cSDimitry Andric /// a LocIdx, you have to use the (sparse) LocationID => LocIdx map. And to 692349cc55cSDimitry Andric /// build a LocationID for a stack slot, you need to combine identifiers for 693349cc55cSDimitry Andric /// which stack slot it is and where within that slot is being described. 694349cc55cSDimitry Andric /// 695349cc55cSDimitry Andric /// Register mask operands cause trouble by technically defining every register; 696349cc55cSDimitry Andric /// various hacks are used to avoid tracking registers that are never read and 697349cc55cSDimitry Andric /// only written by regmasks. 698349cc55cSDimitry Andric class MLocTracker { 699349cc55cSDimitry Andric public: 700349cc55cSDimitry Andric MachineFunction &MF; 701349cc55cSDimitry Andric const TargetInstrInfo &TII; 702349cc55cSDimitry Andric const TargetRegisterInfo &TRI; 703349cc55cSDimitry Andric const TargetLowering &TLI; 704349cc55cSDimitry Andric 705349cc55cSDimitry Andric /// IndexedMap type, mapping from LocIdx to ValueIDNum. 706349cc55cSDimitry Andric using LocToValueType = IndexedMap<ValueIDNum, LocIdxToIndexFunctor>; 707349cc55cSDimitry Andric 708349cc55cSDimitry Andric /// Map of LocIdxes to the ValueIDNums that they store. This is tightly 709349cc55cSDimitry Andric /// packed, entries only exist for locations that are being tracked. 710349cc55cSDimitry Andric LocToValueType LocIdxToIDNum; 711349cc55cSDimitry Andric 712349cc55cSDimitry Andric /// "Map" of machine location IDs (i.e., raw register or spill number) to the 713349cc55cSDimitry Andric /// LocIdx key / number for that location. There are always at least as many 714349cc55cSDimitry Andric /// as the number of registers on the target -- if the value in the register 715349cc55cSDimitry Andric /// is not being tracked, then the LocIdx value will be zero. New entries are 716349cc55cSDimitry Andric /// appended if a new spill slot begins being tracked. 717349cc55cSDimitry Andric /// This, and the corresponding reverse map persist for the analysis of the 718349cc55cSDimitry Andric /// whole function, and is necessarying for decoding various vectors of 719349cc55cSDimitry Andric /// values. 720349cc55cSDimitry Andric std::vector<LocIdx> LocIDToLocIdx; 721349cc55cSDimitry Andric 722349cc55cSDimitry Andric /// Inverse map of LocIDToLocIdx. 723349cc55cSDimitry Andric IndexedMap<unsigned, LocIdxToIndexFunctor> LocIdxToLocID; 724349cc55cSDimitry Andric 725349cc55cSDimitry Andric /// When clobbering register masks, we chose to not believe the machine model 726349cc55cSDimitry Andric /// and don't clobber SP. Do the same for SP aliases, and for efficiency, 727349cc55cSDimitry Andric /// keep a set of them here. 728349cc55cSDimitry Andric SmallSet<Register, 8> SPAliases; 729349cc55cSDimitry Andric 730349cc55cSDimitry Andric /// Unique-ification of spill. Used to number them -- their LocID number is 731349cc55cSDimitry Andric /// the index in SpillLocs minus one plus NumRegs. 732349cc55cSDimitry Andric UniqueVector<SpillLoc> SpillLocs; 733349cc55cSDimitry Andric 734349cc55cSDimitry Andric // If we discover a new machine location, assign it an mphi with this 735349cc55cSDimitry Andric // block number. 73606c3fb27SDimitry Andric unsigned CurBB = -1; 737349cc55cSDimitry Andric 738349cc55cSDimitry Andric /// Cached local copy of the number of registers the target has. 739349cc55cSDimitry Andric unsigned NumRegs; 740349cc55cSDimitry Andric 741349cc55cSDimitry Andric /// Number of slot indexes the target has -- distinct segments of a stack 742349cc55cSDimitry Andric /// slot that can take on the value of a subregister, when a super-register 743349cc55cSDimitry Andric /// is written to the stack. 744349cc55cSDimitry Andric unsigned NumSlotIdxes; 745349cc55cSDimitry Andric 746349cc55cSDimitry Andric /// Collection of register mask operands that have been observed. Second part 747349cc55cSDimitry Andric /// of pair indicates the instruction that they happened in. Used to 748349cc55cSDimitry Andric /// reconstruct where defs happened if we start tracking a location later 749349cc55cSDimitry Andric /// on. 750349cc55cSDimitry Andric SmallVector<std::pair<const MachineOperand *, unsigned>, 32> Masks; 751349cc55cSDimitry Andric 752349cc55cSDimitry Andric /// Pair for describing a position within a stack slot -- first the size in 753349cc55cSDimitry Andric /// bits, then the offset. 754349cc55cSDimitry Andric typedef std::pair<unsigned short, unsigned short> StackSlotPos; 755349cc55cSDimitry Andric 756349cc55cSDimitry Andric /// Map from a size/offset pair describing a position in a stack slot, to a 757349cc55cSDimitry Andric /// numeric identifier for that position. Allows easier identification of 758349cc55cSDimitry Andric /// individual positions. 759349cc55cSDimitry Andric DenseMap<StackSlotPos, unsigned> StackSlotIdxes; 760349cc55cSDimitry Andric 761349cc55cSDimitry Andric /// Inverse of StackSlotIdxes. 762349cc55cSDimitry Andric DenseMap<unsigned, StackSlotPos> StackIdxesToPos; 763349cc55cSDimitry Andric 764349cc55cSDimitry Andric /// Iterator for locations and the values they contain. Dereferencing 765349cc55cSDimitry Andric /// produces a struct/pair containing the LocIdx key for this location, 766349cc55cSDimitry Andric /// and a reference to the value currently stored. Simplifies the process 767349cc55cSDimitry Andric /// of seeking a particular location. 768349cc55cSDimitry Andric class MLocIterator { 769349cc55cSDimitry Andric LocToValueType &ValueMap; 770349cc55cSDimitry Andric LocIdx Idx; 771349cc55cSDimitry Andric 772349cc55cSDimitry Andric public: 773349cc55cSDimitry Andric class value_type { 774349cc55cSDimitry Andric public: 775349cc55cSDimitry Andric value_type(LocIdx Idx, ValueIDNum &Value) : Idx(Idx), Value(Value) {} 776349cc55cSDimitry Andric const LocIdx Idx; /// Read-only index of this location. 777349cc55cSDimitry Andric ValueIDNum &Value; /// Reference to the stored value at this location. 778349cc55cSDimitry Andric }; 779349cc55cSDimitry Andric 780349cc55cSDimitry Andric MLocIterator(LocToValueType &ValueMap, LocIdx Idx) 781349cc55cSDimitry Andric : ValueMap(ValueMap), Idx(Idx) {} 782349cc55cSDimitry Andric 783349cc55cSDimitry Andric bool operator==(const MLocIterator &Other) const { 784349cc55cSDimitry Andric assert(&ValueMap == &Other.ValueMap); 785349cc55cSDimitry Andric return Idx == Other.Idx; 786349cc55cSDimitry Andric } 787349cc55cSDimitry Andric 788349cc55cSDimitry Andric bool operator!=(const MLocIterator &Other) const { 789349cc55cSDimitry Andric return !(*this == Other); 790349cc55cSDimitry Andric } 791349cc55cSDimitry Andric 792349cc55cSDimitry Andric void operator++() { Idx = LocIdx(Idx.asU64() + 1); } 793349cc55cSDimitry Andric 794349cc55cSDimitry Andric value_type operator*() { return value_type(Idx, ValueMap[LocIdx(Idx)]); } 795349cc55cSDimitry Andric }; 796349cc55cSDimitry Andric 797349cc55cSDimitry Andric MLocTracker(MachineFunction &MF, const TargetInstrInfo &TII, 798349cc55cSDimitry Andric const TargetRegisterInfo &TRI, const TargetLowering &TLI); 799349cc55cSDimitry Andric 800349cc55cSDimitry Andric /// Produce location ID number for a Register. Provides some small amount of 801349cc55cSDimitry Andric /// type safety. 802349cc55cSDimitry Andric /// \param Reg The register we're looking up. 803349cc55cSDimitry Andric unsigned getLocID(Register Reg) { return Reg.id(); } 804349cc55cSDimitry Andric 805349cc55cSDimitry Andric /// Produce location ID number for a spill position. 806349cc55cSDimitry Andric /// \param Spill The number of the spill we're fetching the location for. 807349cc55cSDimitry Andric /// \param SpillSubReg Subregister within the spill we're addressing. 808349cc55cSDimitry Andric unsigned getLocID(SpillLocationNo Spill, unsigned SpillSubReg) { 809349cc55cSDimitry Andric unsigned short Size = TRI.getSubRegIdxSize(SpillSubReg); 810349cc55cSDimitry Andric unsigned short Offs = TRI.getSubRegIdxOffset(SpillSubReg); 811349cc55cSDimitry Andric return getLocID(Spill, {Size, Offs}); 812349cc55cSDimitry Andric } 813349cc55cSDimitry Andric 814349cc55cSDimitry Andric /// Produce location ID number for a spill position. 815349cc55cSDimitry Andric /// \param Spill The number of the spill we're fetching the location for. 816349cc55cSDimitry Andric /// \apram SpillIdx size/offset within the spill slot to be addressed. 817349cc55cSDimitry Andric unsigned getLocID(SpillLocationNo Spill, StackSlotPos Idx) { 818349cc55cSDimitry Andric unsigned SlotNo = Spill.id() - 1; 819349cc55cSDimitry Andric SlotNo *= NumSlotIdxes; 82006c3fb27SDimitry Andric assert(StackSlotIdxes.contains(Idx)); 821349cc55cSDimitry Andric SlotNo += StackSlotIdxes[Idx]; 822349cc55cSDimitry Andric SlotNo += NumRegs; 823349cc55cSDimitry Andric return SlotNo; 824349cc55cSDimitry Andric } 825349cc55cSDimitry Andric 826349cc55cSDimitry Andric /// Given a spill number, and a slot within the spill, calculate the ID number 827349cc55cSDimitry Andric /// for that location. 828349cc55cSDimitry Andric unsigned getSpillIDWithIdx(SpillLocationNo Spill, unsigned Idx) { 829349cc55cSDimitry Andric unsigned SlotNo = Spill.id() - 1; 830349cc55cSDimitry Andric SlotNo *= NumSlotIdxes; 831349cc55cSDimitry Andric SlotNo += Idx; 832349cc55cSDimitry Andric SlotNo += NumRegs; 833349cc55cSDimitry Andric return SlotNo; 834349cc55cSDimitry Andric } 835349cc55cSDimitry Andric 836349cc55cSDimitry Andric /// Return the spill number that a location ID corresponds to. 837349cc55cSDimitry Andric SpillLocationNo locIDToSpill(unsigned ID) const { 838349cc55cSDimitry Andric assert(ID >= NumRegs); 839349cc55cSDimitry Andric ID -= NumRegs; 840349cc55cSDimitry Andric // Truncate away the index part, leaving only the spill number. 841349cc55cSDimitry Andric ID /= NumSlotIdxes; 842349cc55cSDimitry Andric return SpillLocationNo(ID + 1); // The UniqueVector is one-based. 843349cc55cSDimitry Andric } 844349cc55cSDimitry Andric 845349cc55cSDimitry Andric /// Returns the spill-slot size/offs that a location ID corresponds to. 846349cc55cSDimitry Andric StackSlotPos locIDToSpillIdx(unsigned ID) const { 847349cc55cSDimitry Andric assert(ID >= NumRegs); 848349cc55cSDimitry Andric ID -= NumRegs; 849349cc55cSDimitry Andric unsigned Idx = ID % NumSlotIdxes; 850349cc55cSDimitry Andric return StackIdxesToPos.find(Idx)->second; 851349cc55cSDimitry Andric } 852349cc55cSDimitry Andric 85304eeddc0SDimitry Andric unsigned getNumLocs() const { return LocIdxToIDNum.size(); } 854349cc55cSDimitry Andric 855349cc55cSDimitry Andric /// Reset all locations to contain a PHI value at the designated block. Used 856349cc55cSDimitry Andric /// sometimes for actual PHI values, othertimes to indicate the block entry 857349cc55cSDimitry Andric /// value (before any more information is known). 858349cc55cSDimitry Andric void setMPhis(unsigned NewCurBB) { 859349cc55cSDimitry Andric CurBB = NewCurBB; 860349cc55cSDimitry Andric for (auto Location : locations()) 861349cc55cSDimitry Andric Location.Value = {CurBB, 0, Location.Idx}; 862349cc55cSDimitry Andric } 863349cc55cSDimitry Andric 864349cc55cSDimitry Andric /// Load values for each location from array of ValueIDNums. Take current 865349cc55cSDimitry Andric /// bbnum just in case we read a value from a hitherto untouched register. 86681ad6265SDimitry Andric void loadFromArray(ValueTable &Locs, unsigned NewCurBB) { 867349cc55cSDimitry Andric CurBB = NewCurBB; 868349cc55cSDimitry Andric // Iterate over all tracked locations, and load each locations live-in 869349cc55cSDimitry Andric // value into our local index. 870349cc55cSDimitry Andric for (auto Location : locations()) 871349cc55cSDimitry Andric Location.Value = Locs[Location.Idx.asU64()]; 872349cc55cSDimitry Andric } 873349cc55cSDimitry Andric 874349cc55cSDimitry Andric /// Wipe any un-necessary location records after traversing a block. 87504eeddc0SDimitry Andric void reset() { 876349cc55cSDimitry Andric // We could reset all the location values too; however either loadFromArray 877349cc55cSDimitry Andric // or setMPhis should be called before this object is re-used. Just 878349cc55cSDimitry Andric // clear Masks, they're definitely not needed. 879349cc55cSDimitry Andric Masks.clear(); 880349cc55cSDimitry Andric } 881349cc55cSDimitry Andric 882349cc55cSDimitry Andric /// Clear all data. Destroys the LocID <=> LocIdx map, which makes most of 883349cc55cSDimitry Andric /// the information in this pass uninterpretable. 88404eeddc0SDimitry Andric void clear() { 885349cc55cSDimitry Andric reset(); 886349cc55cSDimitry Andric LocIDToLocIdx.clear(); 887349cc55cSDimitry Andric LocIdxToLocID.clear(); 888349cc55cSDimitry Andric LocIdxToIDNum.clear(); 889349cc55cSDimitry Andric // SpillLocs.reset(); XXX UniqueVector::reset assumes a SpillLoc casts from 890349cc55cSDimitry Andric // 0 891349cc55cSDimitry Andric SpillLocs = decltype(SpillLocs)(); 892349cc55cSDimitry Andric StackSlotIdxes.clear(); 893349cc55cSDimitry Andric StackIdxesToPos.clear(); 894349cc55cSDimitry Andric 895349cc55cSDimitry Andric LocIDToLocIdx.resize(NumRegs, LocIdx::MakeIllegalLoc()); 896349cc55cSDimitry Andric } 897349cc55cSDimitry Andric 898349cc55cSDimitry Andric /// Set a locaiton to a certain value. 899349cc55cSDimitry Andric void setMLoc(LocIdx L, ValueIDNum Num) { 900349cc55cSDimitry Andric assert(L.asU64() < LocIdxToIDNum.size()); 901349cc55cSDimitry Andric LocIdxToIDNum[L] = Num; 902349cc55cSDimitry Andric } 903349cc55cSDimitry Andric 904349cc55cSDimitry Andric /// Read the value of a particular location 905349cc55cSDimitry Andric ValueIDNum readMLoc(LocIdx L) { 906349cc55cSDimitry Andric assert(L.asU64() < LocIdxToIDNum.size()); 907349cc55cSDimitry Andric return LocIdxToIDNum[L]; 908349cc55cSDimitry Andric } 909349cc55cSDimitry Andric 910349cc55cSDimitry Andric /// Create a LocIdx for an untracked register ID. Initialize it to either an 911349cc55cSDimitry Andric /// mphi value representing a live-in, or a recent register mask clobber. 912349cc55cSDimitry Andric LocIdx trackRegister(unsigned ID); 913349cc55cSDimitry Andric 914349cc55cSDimitry Andric LocIdx lookupOrTrackRegister(unsigned ID) { 915349cc55cSDimitry Andric LocIdx &Index = LocIDToLocIdx[ID]; 916349cc55cSDimitry Andric if (Index.isIllegal()) 917349cc55cSDimitry Andric Index = trackRegister(ID); 918349cc55cSDimitry Andric return Index; 919349cc55cSDimitry Andric } 920349cc55cSDimitry Andric 921349cc55cSDimitry Andric /// Is register R currently tracked by MLocTracker? 922349cc55cSDimitry Andric bool isRegisterTracked(Register R) { 923349cc55cSDimitry Andric LocIdx &Index = LocIDToLocIdx[R]; 924349cc55cSDimitry Andric return !Index.isIllegal(); 925349cc55cSDimitry Andric } 926349cc55cSDimitry Andric 927349cc55cSDimitry Andric /// Record a definition of the specified register at the given block / inst. 928349cc55cSDimitry Andric /// This doesn't take a ValueIDNum, because the definition and its location 929349cc55cSDimitry Andric /// are synonymous. 930349cc55cSDimitry Andric void defReg(Register R, unsigned BB, unsigned Inst) { 931349cc55cSDimitry Andric unsigned ID = getLocID(R); 932349cc55cSDimitry Andric LocIdx Idx = lookupOrTrackRegister(ID); 933349cc55cSDimitry Andric ValueIDNum ValueID = {BB, Inst, Idx}; 934349cc55cSDimitry Andric LocIdxToIDNum[Idx] = ValueID; 935349cc55cSDimitry Andric } 936349cc55cSDimitry Andric 937349cc55cSDimitry Andric /// Set a register to a value number. To be used if the value number is 938349cc55cSDimitry Andric /// known in advance. 939349cc55cSDimitry Andric void setReg(Register R, ValueIDNum ValueID) { 940349cc55cSDimitry Andric unsigned ID = getLocID(R); 941349cc55cSDimitry Andric LocIdx Idx = lookupOrTrackRegister(ID); 942349cc55cSDimitry Andric LocIdxToIDNum[Idx] = ValueID; 943349cc55cSDimitry Andric } 944349cc55cSDimitry Andric 945349cc55cSDimitry Andric ValueIDNum readReg(Register R) { 946349cc55cSDimitry Andric unsigned ID = getLocID(R); 947349cc55cSDimitry Andric LocIdx Idx = lookupOrTrackRegister(ID); 948349cc55cSDimitry Andric return LocIdxToIDNum[Idx]; 949349cc55cSDimitry Andric } 950349cc55cSDimitry Andric 951349cc55cSDimitry Andric /// Reset a register value to zero / empty. Needed to replicate the 952349cc55cSDimitry Andric /// VarLoc implementation where a copy to/from a register effectively 953349cc55cSDimitry Andric /// clears the contents of the source register. (Values can only have one 954349cc55cSDimitry Andric /// machine location in VarLocBasedImpl). 955349cc55cSDimitry Andric void wipeRegister(Register R) { 956349cc55cSDimitry Andric unsigned ID = getLocID(R); 957349cc55cSDimitry Andric LocIdx Idx = LocIDToLocIdx[ID]; 958349cc55cSDimitry Andric LocIdxToIDNum[Idx] = ValueIDNum::EmptyValue; 959349cc55cSDimitry Andric } 960349cc55cSDimitry Andric 961349cc55cSDimitry Andric /// Determine the LocIdx of an existing register. 962349cc55cSDimitry Andric LocIdx getRegMLoc(Register R) { 963349cc55cSDimitry Andric unsigned ID = getLocID(R); 964349cc55cSDimitry Andric assert(ID < LocIDToLocIdx.size()); 9657a6dacacSDimitry Andric assert(LocIDToLocIdx[ID] != UINT_MAX); // Sentinel for IndexedMap. 966349cc55cSDimitry Andric return LocIDToLocIdx[ID]; 967349cc55cSDimitry Andric } 968349cc55cSDimitry Andric 969349cc55cSDimitry Andric /// Record a RegMask operand being executed. Defs any register we currently 970349cc55cSDimitry Andric /// track, stores a pointer to the mask in case we have to account for it 971349cc55cSDimitry Andric /// later. 972349cc55cSDimitry Andric void writeRegMask(const MachineOperand *MO, unsigned CurBB, unsigned InstID); 973349cc55cSDimitry Andric 974349cc55cSDimitry Andric /// Find LocIdx for SpillLoc \p L, creating a new one if it's not tracked. 975bdd1243dSDimitry Andric /// Returns std::nullopt when in scenarios where a spill slot could be 976bdd1243dSDimitry Andric /// tracked, but we would likely run into resource limitations. 977bdd1243dSDimitry Andric std::optional<SpillLocationNo> getOrTrackSpillLoc(SpillLoc L); 978349cc55cSDimitry Andric 979349cc55cSDimitry Andric // Get LocIdx of a spill ID. 980349cc55cSDimitry Andric LocIdx getSpillMLoc(unsigned SpillID) { 9817a6dacacSDimitry Andric assert(LocIDToLocIdx[SpillID] != UINT_MAX); // Sentinel for IndexedMap. 982349cc55cSDimitry Andric return LocIDToLocIdx[SpillID]; 983349cc55cSDimitry Andric } 984349cc55cSDimitry Andric 985349cc55cSDimitry Andric /// Return true if Idx is a spill machine location. 986349cc55cSDimitry Andric bool isSpill(LocIdx Idx) const { return LocIdxToLocID[Idx] >= NumRegs; } 987349cc55cSDimitry Andric 98881ad6265SDimitry Andric /// How large is this location (aka, how wide is a value defined there?). 98981ad6265SDimitry Andric unsigned getLocSizeInBits(LocIdx L) const { 99081ad6265SDimitry Andric unsigned ID = LocIdxToLocID[L]; 99181ad6265SDimitry Andric if (!isSpill(L)) { 99281ad6265SDimitry Andric return TRI.getRegSizeInBits(Register(ID), MF.getRegInfo()); 99381ad6265SDimitry Andric } else { 99481ad6265SDimitry Andric // The slot location on the stack is uninteresting, we care about the 99581ad6265SDimitry Andric // position of the value within the slot (which comes with a size). 99681ad6265SDimitry Andric StackSlotPos Pos = locIDToSpillIdx(ID); 99781ad6265SDimitry Andric return Pos.first; 99881ad6265SDimitry Andric } 99981ad6265SDimitry Andric } 100081ad6265SDimitry Andric 1001349cc55cSDimitry Andric MLocIterator begin() { return MLocIterator(LocIdxToIDNum, 0); } 1002349cc55cSDimitry Andric 1003349cc55cSDimitry Andric MLocIterator end() { 1004349cc55cSDimitry Andric return MLocIterator(LocIdxToIDNum, LocIdxToIDNum.size()); 1005349cc55cSDimitry Andric } 1006349cc55cSDimitry Andric 1007349cc55cSDimitry Andric /// Return a range over all locations currently tracked. 1008349cc55cSDimitry Andric iterator_range<MLocIterator> locations() { 1009349cc55cSDimitry Andric return llvm::make_range(begin(), end()); 1010349cc55cSDimitry Andric } 1011349cc55cSDimitry Andric 1012349cc55cSDimitry Andric std::string LocIdxToName(LocIdx Idx) const; 1013349cc55cSDimitry Andric 1014349cc55cSDimitry Andric std::string IDAsString(const ValueIDNum &Num) const; 1015349cc55cSDimitry Andric 1016349cc55cSDimitry Andric #ifndef NDEBUG 1017349cc55cSDimitry Andric LLVM_DUMP_METHOD void dump(); 1018349cc55cSDimitry Andric 1019349cc55cSDimitry Andric LLVM_DUMP_METHOD void dump_mloc_map(); 1020349cc55cSDimitry Andric #endif 1021349cc55cSDimitry Andric 1022bdd1243dSDimitry Andric /// Create a DBG_VALUE based on debug operands \p DbgOps. Qualify it with the 1023349cc55cSDimitry Andric /// information in \pProperties, for variable Var. Don't insert it anywhere, 1024349cc55cSDimitry Andric /// just return the builder for it. 1025bdd1243dSDimitry Andric MachineInstrBuilder emitLoc(const SmallVectorImpl<ResolvedDbgOp> &DbgOps, 1026*0fca6ea1SDimitry Andric const DebugVariable &Var, const DILocation *DILoc, 1027349cc55cSDimitry Andric const DbgValueProperties &Properties); 1028349cc55cSDimitry Andric }; 1029349cc55cSDimitry Andric 10304824e7fdSDimitry Andric /// Types for recording sets of variable fragments that overlap. For a given 10314824e7fdSDimitry Andric /// local variable, we record all other fragments of that variable that could 10324824e7fdSDimitry Andric /// overlap it, to reduce search time. 10334824e7fdSDimitry Andric using FragmentOfVar = 10344824e7fdSDimitry Andric std::pair<const DILocalVariable *, DIExpression::FragmentInfo>; 10354824e7fdSDimitry Andric using OverlapMap = 10364824e7fdSDimitry Andric DenseMap<FragmentOfVar, SmallVector<DIExpression::FragmentInfo, 1>>; 10374824e7fdSDimitry Andric 1038349cc55cSDimitry Andric /// Collection of DBG_VALUEs observed when traversing a block. Records each 1039349cc55cSDimitry Andric /// variable and the value the DBG_VALUE refers to. Requires the machine value 1040349cc55cSDimitry Andric /// location dataflow algorithm to have run already, so that values can be 1041349cc55cSDimitry Andric /// identified. 1042349cc55cSDimitry Andric class VLocTracker { 1043349cc55cSDimitry Andric public: 1044*0fca6ea1SDimitry Andric /// Ref to function-wide map of DebugVariable <=> ID-numbers. 1045*0fca6ea1SDimitry Andric DebugVariableMap &DVMap; 1046349cc55cSDimitry Andric /// Map DebugVariable to the latest Value it's defined to have. 1047349cc55cSDimitry Andric /// Needs to be a MapVector because we determine order-in-the-input-MIR from 1048*0fca6ea1SDimitry Andric /// the order in this container. (FIXME: likely no longer true as the ordering 1049*0fca6ea1SDimitry Andric /// is now provided by DebugVariableMap). 1050349cc55cSDimitry Andric /// We only retain the last DbgValue in each block for each variable, to 1051349cc55cSDimitry Andric /// determine the blocks live-out variable value. The Vars container forms the 1052349cc55cSDimitry Andric /// transfer function for this block, as part of the dataflow analysis. The 1053349cc55cSDimitry Andric /// movement of values between locations inside of a block is handled at a 1054349cc55cSDimitry Andric /// much later stage, in the TransferTracker class. 1055*0fca6ea1SDimitry Andric MapVector<DebugVariableID, DbgValue> Vars; 1056*0fca6ea1SDimitry Andric SmallDenseMap<DebugVariableID, const DILocation *, 8> Scopes; 1057349cc55cSDimitry Andric MachineBasicBlock *MBB = nullptr; 10584824e7fdSDimitry Andric const OverlapMap &OverlappingFragments; 10594824e7fdSDimitry Andric DbgValueProperties EmptyProperties; 1060349cc55cSDimitry Andric 1061349cc55cSDimitry Andric public: 1062*0fca6ea1SDimitry Andric VLocTracker(DebugVariableMap &DVMap, const OverlapMap &O, 1063*0fca6ea1SDimitry Andric const DIExpression *EmptyExpr) 1064*0fca6ea1SDimitry Andric : DVMap(DVMap), OverlappingFragments(O), 1065*0fca6ea1SDimitry Andric EmptyProperties(EmptyExpr, false, false) {} 1066349cc55cSDimitry Andric 1067349cc55cSDimitry Andric void defVar(const MachineInstr &MI, const DbgValueProperties &Properties, 1068bdd1243dSDimitry Andric const SmallVectorImpl<DbgOpID> &DebugOps) { 1069bdd1243dSDimitry Andric assert(MI.isDebugValueLike()); 1070349cc55cSDimitry Andric DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(), 1071349cc55cSDimitry Andric MI.getDebugLoc()->getInlinedAt()); 1072*0fca6ea1SDimitry Andric // Either insert or fetch an ID number for this variable. 1073*0fca6ea1SDimitry Andric DebugVariableID VarID = DVMap.insertDVID(Var, MI.getDebugLoc().get()); 1074bdd1243dSDimitry Andric DbgValue Rec = (DebugOps.size() > 0) 1075bdd1243dSDimitry Andric ? DbgValue(DebugOps, Properties) 1076349cc55cSDimitry Andric : DbgValue(Properties, DbgValue::Undef); 1077349cc55cSDimitry Andric 1078349cc55cSDimitry Andric // Attempt insertion; overwrite if it's already mapped. 1079*0fca6ea1SDimitry Andric auto Result = Vars.insert(std::make_pair(VarID, Rec)); 1080349cc55cSDimitry Andric if (!Result.second) 1081349cc55cSDimitry Andric Result.first->second = Rec; 1082*0fca6ea1SDimitry Andric Scopes[VarID] = MI.getDebugLoc().get(); 10834824e7fdSDimitry Andric 10844824e7fdSDimitry Andric considerOverlaps(Var, MI.getDebugLoc().get()); 1085349cc55cSDimitry Andric } 1086349cc55cSDimitry Andric 10874824e7fdSDimitry Andric void considerOverlaps(const DebugVariable &Var, const DILocation *Loc) { 10884824e7fdSDimitry Andric auto Overlaps = OverlappingFragments.find( 10894824e7fdSDimitry Andric {Var.getVariable(), Var.getFragmentOrDefault()}); 10904824e7fdSDimitry Andric if (Overlaps == OverlappingFragments.end()) 10914824e7fdSDimitry Andric return; 10924824e7fdSDimitry Andric 10934824e7fdSDimitry Andric // Otherwise: terminate any overlapped variable locations. 10944824e7fdSDimitry Andric for (auto FragmentInfo : Overlaps->second) { 10954824e7fdSDimitry Andric // The "empty" fragment is stored as DebugVariable::DefaultFragment, so 10964824e7fdSDimitry Andric // that it overlaps with everything, however its cannonical representation 10974824e7fdSDimitry Andric // in a DebugVariable is as "None". 1098bdd1243dSDimitry Andric std::optional<DIExpression::FragmentInfo> OptFragmentInfo = FragmentInfo; 10994824e7fdSDimitry Andric if (DebugVariable::isDefaultFragment(FragmentInfo)) 1100bdd1243dSDimitry Andric OptFragmentInfo = std::nullopt; 11014824e7fdSDimitry Andric 11024824e7fdSDimitry Andric DebugVariable Overlapped(Var.getVariable(), OptFragmentInfo, 11034824e7fdSDimitry Andric Var.getInlinedAt()); 1104*0fca6ea1SDimitry Andric // Produce an ID number for this overlapping fragment of a variable. 1105*0fca6ea1SDimitry Andric DebugVariableID OverlappedID = DVMap.insertDVID(Overlapped, Loc); 11064824e7fdSDimitry Andric DbgValue Rec = DbgValue(EmptyProperties, DbgValue::Undef); 11074824e7fdSDimitry Andric 11084824e7fdSDimitry Andric // Attempt insertion; overwrite if it's already mapped. 1109*0fca6ea1SDimitry Andric auto Result = Vars.insert(std::make_pair(OverlappedID, Rec)); 11104824e7fdSDimitry Andric if (!Result.second) 11114824e7fdSDimitry Andric Result.first->second = Rec; 1112*0fca6ea1SDimitry Andric Scopes[OverlappedID] = Loc; 11134824e7fdSDimitry Andric } 1114349cc55cSDimitry Andric } 1115d56accc7SDimitry Andric 1116d56accc7SDimitry Andric void clear() { 1117d56accc7SDimitry Andric Vars.clear(); 1118d56accc7SDimitry Andric Scopes.clear(); 1119d56accc7SDimitry Andric } 1120349cc55cSDimitry Andric }; 1121349cc55cSDimitry Andric 1122349cc55cSDimitry Andric // XXX XXX docs 1123349cc55cSDimitry Andric class InstrRefBasedLDV : public LDVImpl { 1124349cc55cSDimitry Andric public: 1125349cc55cSDimitry Andric friend class ::InstrRefLDVTest; 1126349cc55cSDimitry Andric 1127349cc55cSDimitry Andric using FragmentInfo = DIExpression::FragmentInfo; 1128bdd1243dSDimitry Andric using OptFragmentInfo = std::optional<DIExpression::FragmentInfo>; 1129349cc55cSDimitry Andric 1130349cc55cSDimitry Andric // Helper while building OverlapMap, a map of all fragments seen for a given 1131349cc55cSDimitry Andric // DILocalVariable. 1132349cc55cSDimitry Andric using VarToFragments = 1133349cc55cSDimitry Andric DenseMap<const DILocalVariable *, SmallSet<FragmentInfo, 4>>; 1134349cc55cSDimitry Andric 1135349cc55cSDimitry Andric /// Machine location/value transfer function, a mapping of which locations 1136349cc55cSDimitry Andric /// are assigned which new values. 1137349cc55cSDimitry Andric using MLocTransferMap = SmallDenseMap<LocIdx, ValueIDNum>; 1138349cc55cSDimitry Andric 1139349cc55cSDimitry Andric /// Live in/out structure for the variable values: a per-block map of 1140349cc55cSDimitry Andric /// variables to their values. 1141349cc55cSDimitry Andric using LiveIdxT = DenseMap<const MachineBasicBlock *, DbgValue *>; 1142349cc55cSDimitry Andric 1143*0fca6ea1SDimitry Andric using VarAndLoc = std::pair<DebugVariableID, DbgValue>; 1144349cc55cSDimitry Andric 1145349cc55cSDimitry Andric /// Type for a live-in value: the predecessor block, and its value. 1146349cc55cSDimitry Andric using InValueT = std::pair<MachineBasicBlock *, DbgValue *>; 1147349cc55cSDimitry Andric 1148349cc55cSDimitry Andric /// Vector (per block) of a collection (inner smallvector) of live-ins. 1149349cc55cSDimitry Andric /// Used as the result type for the variable value dataflow problem. 1150349cc55cSDimitry Andric using LiveInsT = SmallVector<SmallVector<VarAndLoc, 8>, 8>; 1151349cc55cSDimitry Andric 11521fd87a68SDimitry Andric /// Mapping from lexical scopes to a DILocation in that scope. 11531fd87a68SDimitry Andric using ScopeToDILocT = DenseMap<const LexicalScope *, const DILocation *>; 11541fd87a68SDimitry Andric 11551fd87a68SDimitry Andric /// Mapping from lexical scopes to variables in that scope. 1156*0fca6ea1SDimitry Andric using ScopeToVarsT = 1157*0fca6ea1SDimitry Andric DenseMap<const LexicalScope *, SmallSet<DebugVariableID, 4>>; 11581fd87a68SDimitry Andric 11591fd87a68SDimitry Andric /// Mapping from lexical scopes to blocks where variables in that scope are 11601fd87a68SDimitry Andric /// assigned. Such blocks aren't necessarily "in" the lexical scope, it's 11611fd87a68SDimitry Andric /// just a block where an assignment happens. 11621fd87a68SDimitry Andric using ScopeToAssignBlocksT = DenseMap<const LexicalScope *, SmallPtrSet<MachineBasicBlock *, 4>>; 11631fd87a68SDimitry Andric 1164349cc55cSDimitry Andric private: 1165349cc55cSDimitry Andric MachineDominatorTree *DomTree; 1166349cc55cSDimitry Andric const TargetRegisterInfo *TRI; 1167349cc55cSDimitry Andric const MachineRegisterInfo *MRI; 1168349cc55cSDimitry Andric const TargetInstrInfo *TII; 1169349cc55cSDimitry Andric const TargetFrameLowering *TFI; 1170349cc55cSDimitry Andric const MachineFrameInfo *MFI; 1171349cc55cSDimitry Andric BitVector CalleeSavedRegs; 1172349cc55cSDimitry Andric LexicalScopes LS; 1173349cc55cSDimitry Andric TargetPassConfig *TPC; 1174349cc55cSDimitry Andric 1175349cc55cSDimitry Andric // An empty DIExpression. Used default / placeholder DbgValueProperties 1176349cc55cSDimitry Andric // objects, as we can't have null expressions. 1177349cc55cSDimitry Andric const DIExpression *EmptyExpr; 1178349cc55cSDimitry Andric 1179349cc55cSDimitry Andric /// Object to track machine locations as we step through a block. Could 1180349cc55cSDimitry Andric /// probably be a field rather than a pointer, as it's always used. 1181349cc55cSDimitry Andric MLocTracker *MTracker = nullptr; 1182349cc55cSDimitry Andric 1183349cc55cSDimitry Andric /// Number of the current block LiveDebugValues is stepping through. 118406c3fb27SDimitry Andric unsigned CurBB = -1; 1185349cc55cSDimitry Andric 1186349cc55cSDimitry Andric /// Number of the current instruction LiveDebugValues is evaluating. 1187349cc55cSDimitry Andric unsigned CurInst; 1188349cc55cSDimitry Andric 1189349cc55cSDimitry Andric /// Variable tracker -- listens to DBG_VALUEs occurring as InstrRefBasedImpl 1190349cc55cSDimitry Andric /// steps through a block. Reads the values at each location from the 1191349cc55cSDimitry Andric /// MLocTracker object. 1192349cc55cSDimitry Andric VLocTracker *VTracker = nullptr; 1193349cc55cSDimitry Andric 1194349cc55cSDimitry Andric /// Tracker for transfers, listens to DBG_VALUEs and transfers of values 1195349cc55cSDimitry Andric /// between locations during stepping, creates new DBG_VALUEs when values move 1196349cc55cSDimitry Andric /// location. 1197349cc55cSDimitry Andric TransferTracker *TTracker = nullptr; 1198349cc55cSDimitry Andric 1199349cc55cSDimitry Andric /// Blocks which are artificial, i.e. blocks which exclusively contain 1200349cc55cSDimitry Andric /// instructions without DebugLocs, or with line 0 locations. 12011fd87a68SDimitry Andric SmallPtrSet<MachineBasicBlock *, 16> ArtificialBlocks; 1202349cc55cSDimitry Andric 1203349cc55cSDimitry Andric // Mapping of blocks to and from their RPOT order. 1204*0fca6ea1SDimitry Andric SmallVector<MachineBasicBlock *> OrderToBB; 1205349cc55cSDimitry Andric DenseMap<const MachineBasicBlock *, unsigned int> BBToOrder; 1206349cc55cSDimitry Andric DenseMap<unsigned, unsigned> BBNumToRPO; 1207349cc55cSDimitry Andric 1208349cc55cSDimitry Andric /// Pair of MachineInstr, and its 1-based offset into the containing block. 1209349cc55cSDimitry Andric using InstAndNum = std::pair<const MachineInstr *, unsigned>; 1210349cc55cSDimitry Andric /// Map from debug instruction number to the MachineInstr labelled with that 1211349cc55cSDimitry Andric /// number, and its location within the function. Used to transform 1212349cc55cSDimitry Andric /// instruction numbers in DBG_INSTR_REFs into machine value numbers. 1213349cc55cSDimitry Andric std::map<uint64_t, InstAndNum> DebugInstrNumToInstr; 1214349cc55cSDimitry Andric 1215349cc55cSDimitry Andric /// Record of where we observed a DBG_PHI instruction. 1216349cc55cSDimitry Andric class DebugPHIRecord { 1217349cc55cSDimitry Andric public: 121881ad6265SDimitry Andric /// Instruction number of this DBG_PHI. 121981ad6265SDimitry Andric uint64_t InstrNum; 122081ad6265SDimitry Andric /// Block where DBG_PHI occurred. 122181ad6265SDimitry Andric MachineBasicBlock *MBB; 1222bdd1243dSDimitry Andric /// The value number read by the DBG_PHI -- or std::nullopt if it didn't 1223bdd1243dSDimitry Andric /// refer to a value. 1224bdd1243dSDimitry Andric std::optional<ValueIDNum> ValueRead; 1225bdd1243dSDimitry Andric /// Register/Stack location the DBG_PHI reads -- or std::nullopt if it 1226bdd1243dSDimitry Andric /// referred to something unexpected. 1227bdd1243dSDimitry Andric std::optional<LocIdx> ReadLoc; 1228349cc55cSDimitry Andric 1229349cc55cSDimitry Andric operator unsigned() const { return InstrNum; } 1230349cc55cSDimitry Andric }; 1231349cc55cSDimitry Andric 1232349cc55cSDimitry Andric /// Map from instruction numbers defined by DBG_PHIs to a record of what that 1233349cc55cSDimitry Andric /// DBG_PHI read and where. Populated and edited during the machine value 1234349cc55cSDimitry Andric /// location problem -- we use LLVMs SSA Updater to fix changes by 1235349cc55cSDimitry Andric /// optimizations that destroy PHI instructions. 1236349cc55cSDimitry Andric SmallVector<DebugPHIRecord, 32> DebugPHINumToValue; 1237349cc55cSDimitry Andric 1238349cc55cSDimitry Andric // Map of overlapping variable fragments. 1239349cc55cSDimitry Andric OverlapMap OverlapFragments; 1240349cc55cSDimitry Andric VarToFragments SeenFragments; 1241349cc55cSDimitry Andric 1242d56accc7SDimitry Andric /// Mapping of DBG_INSTR_REF instructions to their values, for those 1243d56accc7SDimitry Andric /// DBG_INSTR_REFs that call resolveDbgPHIs. These variable references solve 1244d56accc7SDimitry Andric /// a mini SSA problem caused by DBG_PHIs being cloned, this collection caches 1245d56accc7SDimitry Andric /// the result. 1246bdd1243dSDimitry Andric DenseMap<std::pair<MachineInstr *, unsigned>, std::optional<ValueIDNum>> 1247bdd1243dSDimitry Andric SeenDbgPHIs; 1248bdd1243dSDimitry Andric 1249bdd1243dSDimitry Andric DbgOpIDMap DbgOpStore; 1250d56accc7SDimitry Andric 1251*0fca6ea1SDimitry Andric /// Mapping between DebugVariables and unique ID numbers. This is a more 1252*0fca6ea1SDimitry Andric /// efficient way to represent the identity of a variable, versus a plain 1253*0fca6ea1SDimitry Andric /// DebugVariable. 1254*0fca6ea1SDimitry Andric DebugVariableMap DVMap; 1255*0fca6ea1SDimitry Andric 12564824e7fdSDimitry Andric /// True if we need to examine call instructions for stack clobbers. We 12574824e7fdSDimitry Andric /// normally assume that they don't clobber SP, but stack probes on Windows 12584824e7fdSDimitry Andric /// do. 12594824e7fdSDimitry Andric bool AdjustsStackInCalls = false; 12604824e7fdSDimitry Andric 12614824e7fdSDimitry Andric /// If AdjustsStackInCalls is true, this holds the name of the target's stack 12624824e7fdSDimitry Andric /// probe function, which is the function we expect will alter the stack 12634824e7fdSDimitry Andric /// pointer. 12644824e7fdSDimitry Andric StringRef StackProbeSymbolName; 12654824e7fdSDimitry Andric 1266349cc55cSDimitry Andric /// Tests whether this instruction is a spill to a stack slot. 1267bdd1243dSDimitry Andric std::optional<SpillLocationNo> isSpillInstruction(const MachineInstr &MI, 1268d56accc7SDimitry Andric MachineFunction *MF); 1269349cc55cSDimitry Andric 1270349cc55cSDimitry Andric /// Decide if @MI is a spill instruction and return true if it is. We use 2 1271349cc55cSDimitry Andric /// criteria to make this decision: 1272349cc55cSDimitry Andric /// - Is this instruction a store to a spill slot? 1273349cc55cSDimitry Andric /// - Is there a register operand that is both used and killed? 1274349cc55cSDimitry Andric /// TODO: Store optimization can fold spills into other stores (including 1275349cc55cSDimitry Andric /// other spills). We do not handle this yet (more than one memory operand). 1276349cc55cSDimitry Andric bool isLocationSpill(const MachineInstr &MI, MachineFunction *MF, 1277349cc55cSDimitry Andric unsigned &Reg); 1278349cc55cSDimitry Andric 1279349cc55cSDimitry Andric /// If a given instruction is identified as a spill, return the spill slot 1280349cc55cSDimitry Andric /// and set \p Reg to the spilled register. 1281bdd1243dSDimitry Andric std::optional<SpillLocationNo> isRestoreInstruction(const MachineInstr &MI, 1282bdd1243dSDimitry Andric MachineFunction *MF, 1283bdd1243dSDimitry Andric unsigned &Reg); 1284349cc55cSDimitry Andric 1285349cc55cSDimitry Andric /// Given a spill instruction, extract the spill slot information, ensure it's 1286349cc55cSDimitry Andric /// tracked, and return the spill number. 1287bdd1243dSDimitry Andric std::optional<SpillLocationNo> 1288d56accc7SDimitry Andric extractSpillBaseRegAndOffset(const MachineInstr &MI); 1289349cc55cSDimitry Andric 1290bdd1243dSDimitry Andric /// For an instruction reference given by \p InstNo and \p OpNo in instruction 1291bdd1243dSDimitry Andric /// \p MI returns the Value pointed to by that instruction reference if any 129206c3fb27SDimitry Andric /// exists, otherwise returns std::nullopt. 1293bdd1243dSDimitry Andric std::optional<ValueIDNum> getValueForInstrRef(unsigned InstNo, unsigned OpNo, 1294bdd1243dSDimitry Andric MachineInstr &MI, 12955f757f3fSDimitry Andric const FuncValueTable *MLiveOuts, 12965f757f3fSDimitry Andric const FuncValueTable *MLiveIns); 1297bdd1243dSDimitry Andric 1298349cc55cSDimitry Andric /// Observe a single instruction while stepping through a block. 12995f757f3fSDimitry Andric void process(MachineInstr &MI, const FuncValueTable *MLiveOuts, 13005f757f3fSDimitry Andric const FuncValueTable *MLiveIns); 1301349cc55cSDimitry Andric 1302349cc55cSDimitry Andric /// Examines whether \p MI is a DBG_VALUE and notifies trackers. 1303349cc55cSDimitry Andric /// \returns true if MI was recognized and processed. 1304349cc55cSDimitry Andric bool transferDebugValue(const MachineInstr &MI); 1305349cc55cSDimitry Andric 1306349cc55cSDimitry Andric /// Examines whether \p MI is a DBG_INSTR_REF and notifies trackers. 1307349cc55cSDimitry Andric /// \returns true if MI was recognized and processed. 13085f757f3fSDimitry Andric bool transferDebugInstrRef(MachineInstr &MI, const FuncValueTable *MLiveOuts, 13095f757f3fSDimitry Andric const FuncValueTable *MLiveIns); 1310349cc55cSDimitry Andric 1311349cc55cSDimitry Andric /// Stores value-information about where this PHI occurred, and what 1312349cc55cSDimitry Andric /// instruction number is associated with it. 1313349cc55cSDimitry Andric /// \returns true if MI was recognized and processed. 1314349cc55cSDimitry Andric bool transferDebugPHI(MachineInstr &MI); 1315349cc55cSDimitry Andric 1316349cc55cSDimitry Andric /// Examines whether \p MI is copy instruction, and notifies trackers. 1317349cc55cSDimitry Andric /// \returns true if MI was recognized and processed. 1318349cc55cSDimitry Andric bool transferRegisterCopy(MachineInstr &MI); 1319349cc55cSDimitry Andric 1320349cc55cSDimitry Andric /// Examines whether \p MI is stack spill or restore instruction, and 1321349cc55cSDimitry Andric /// notifies trackers. \returns true if MI was recognized and processed. 1322349cc55cSDimitry Andric bool transferSpillOrRestoreInst(MachineInstr &MI); 1323349cc55cSDimitry Andric 1324349cc55cSDimitry Andric /// Examines \p MI for any registers that it defines, and notifies trackers. 1325349cc55cSDimitry Andric void transferRegisterDef(MachineInstr &MI); 1326349cc55cSDimitry Andric 1327349cc55cSDimitry Andric /// Copy one location to the other, accounting for movement of subregisters 1328349cc55cSDimitry Andric /// too. 1329349cc55cSDimitry Andric void performCopy(Register Src, Register Dst); 1330349cc55cSDimitry Andric 1331349cc55cSDimitry Andric void accumulateFragmentMap(MachineInstr &MI); 1332349cc55cSDimitry Andric 1333349cc55cSDimitry Andric /// Determine the machine value number referred to by (potentially several) 1334349cc55cSDimitry Andric /// DBG_PHI instructions. Block duplication and tail folding can duplicate 1335349cc55cSDimitry Andric /// DBG_PHIs, shifting the position where values in registers merge, and 1336349cc55cSDimitry Andric /// forming another mini-ssa problem to solve. 1337349cc55cSDimitry Andric /// \p Here the position of a DBG_INSTR_REF seeking a machine value number 1338349cc55cSDimitry Andric /// \p InstrNum Debug instruction number defined by DBG_PHI instructions. 1339bdd1243dSDimitry Andric /// \returns The machine value number at position Here, or std::nullopt. 1340bdd1243dSDimitry Andric std::optional<ValueIDNum> resolveDbgPHIs(MachineFunction &MF, 13415f757f3fSDimitry Andric const FuncValueTable &MLiveOuts, 13425f757f3fSDimitry Andric const FuncValueTable &MLiveIns, 1343bdd1243dSDimitry Andric MachineInstr &Here, 1344bdd1243dSDimitry Andric uint64_t InstrNum); 1345349cc55cSDimitry Andric 1346bdd1243dSDimitry Andric std::optional<ValueIDNum> resolveDbgPHIsImpl(MachineFunction &MF, 13475f757f3fSDimitry Andric const FuncValueTable &MLiveOuts, 13485f757f3fSDimitry Andric const FuncValueTable &MLiveIns, 1349d56accc7SDimitry Andric MachineInstr &Here, 1350d56accc7SDimitry Andric uint64_t InstrNum); 1351d56accc7SDimitry Andric 1352349cc55cSDimitry Andric /// Step through the function, recording register definitions and movements 1353349cc55cSDimitry Andric /// in an MLocTracker. Convert the observations into a per-block transfer 1354349cc55cSDimitry Andric /// function in \p MLocTransfer, suitable for using with the machine value 1355349cc55cSDimitry Andric /// location dataflow problem. 1356349cc55cSDimitry Andric void 1357349cc55cSDimitry Andric produceMLocTransferFunction(MachineFunction &MF, 1358349cc55cSDimitry Andric SmallVectorImpl<MLocTransferMap> &MLocTransfer, 1359349cc55cSDimitry Andric unsigned MaxNumBlocks); 1360349cc55cSDimitry Andric 1361349cc55cSDimitry Andric /// Solve the machine value location dataflow problem. Takes as input the 1362349cc55cSDimitry Andric /// transfer functions in \p MLocTransfer. Writes the output live-in and 1363349cc55cSDimitry Andric /// live-out arrays to the (initialized to zero) multidimensional arrays in 1364349cc55cSDimitry Andric /// \p MInLocs and \p MOutLocs. The outer dimension is indexed by block 1365349cc55cSDimitry Andric /// number, the inner by LocIdx. 136681ad6265SDimitry Andric void buildMLocValueMap(MachineFunction &MF, FuncValueTable &MInLocs, 136781ad6265SDimitry Andric FuncValueTable &MOutLocs, 1368349cc55cSDimitry Andric SmallVectorImpl<MLocTransferMap> &MLocTransfer); 1369349cc55cSDimitry Andric 1370349cc55cSDimitry Andric /// Examine the stack indexes (i.e. offsets within the stack) to find the 1371349cc55cSDimitry Andric /// basic units of interference -- like reg units, but for the stack. 1372349cc55cSDimitry Andric void findStackIndexInterference(SmallVectorImpl<unsigned> &Slots); 1373349cc55cSDimitry Andric 1374349cc55cSDimitry Andric /// Install PHI values into the live-in array for each block, according to 1375349cc55cSDimitry Andric /// the IDF of each register. 1376349cc55cSDimitry Andric void placeMLocPHIs(MachineFunction &MF, 1377349cc55cSDimitry Andric SmallPtrSetImpl<MachineBasicBlock *> &AllBlocks, 137881ad6265SDimitry Andric FuncValueTable &MInLocs, 1379349cc55cSDimitry Andric SmallVectorImpl<MLocTransferMap> &MLocTransfer); 1380349cc55cSDimitry Andric 13811fd87a68SDimitry Andric /// Propagate variable values to blocks in the common case where there's 13821fd87a68SDimitry Andric /// only one value assigned to the variable. This function has better 13831fd87a68SDimitry Andric /// performance as it doesn't have to find the dominance frontier between 13841fd87a68SDimitry Andric /// different assignments. 13851fd87a68SDimitry Andric void placePHIsForSingleVarDefinition( 13861fd87a68SDimitry Andric const SmallPtrSetImpl<MachineBasicBlock *> &InScopeBlocks, 13871fd87a68SDimitry Andric MachineBasicBlock *MBB, SmallVectorImpl<VLocTracker> &AllTheVLocs, 1388*0fca6ea1SDimitry Andric DebugVariableID Var, LiveInsT &Output); 13891fd87a68SDimitry Andric 1390349cc55cSDimitry Andric /// Calculate the iterated-dominance-frontier for a set of defs, using the 1391349cc55cSDimitry Andric /// existing LLVM facilities for this. Works for a single "value" or 1392349cc55cSDimitry Andric /// machine/variable location. 1393349cc55cSDimitry Andric /// \p AllBlocks Set of blocks where we might consume the value. 1394349cc55cSDimitry Andric /// \p DefBlocks Set of blocks where the value/location is defined. 1395349cc55cSDimitry Andric /// \p PHIBlocks Output set of blocks where PHIs must be placed. 1396349cc55cSDimitry Andric void BlockPHIPlacement(const SmallPtrSetImpl<MachineBasicBlock *> &AllBlocks, 1397349cc55cSDimitry Andric const SmallPtrSetImpl<MachineBasicBlock *> &DefBlocks, 1398349cc55cSDimitry Andric SmallVectorImpl<MachineBasicBlock *> &PHIBlocks); 1399349cc55cSDimitry Andric 1400349cc55cSDimitry Andric /// Perform a control flow join (lattice value meet) of the values in machine 1401349cc55cSDimitry Andric /// locations at \p MBB. Follows the algorithm described in the file-comment, 1402349cc55cSDimitry Andric /// reading live-outs of predecessors from \p OutLocs, the current live ins 1403349cc55cSDimitry Andric /// from \p InLocs, and assigning the newly computed live ins back into 1404349cc55cSDimitry Andric /// \p InLocs. \returns two bools -- the first indicates whether a change 1405349cc55cSDimitry Andric /// was made, the second whether a lattice downgrade occurred. If the latter 1406349cc55cSDimitry Andric /// is true, revisiting this block is necessary. 1407349cc55cSDimitry Andric bool mlocJoin(MachineBasicBlock &MBB, 1408349cc55cSDimitry Andric SmallPtrSet<const MachineBasicBlock *, 16> &Visited, 140981ad6265SDimitry Andric FuncValueTable &OutLocs, ValueTable &InLocs); 1410349cc55cSDimitry Andric 14111fd87a68SDimitry Andric /// Produce a set of blocks that are in the current lexical scope. This means 14121fd87a68SDimitry Andric /// those blocks that contain instructions "in" the scope, blocks where 14131fd87a68SDimitry Andric /// assignments to variables in scope occur, and artificial blocks that are 14141fd87a68SDimitry Andric /// successors to any of the earlier blocks. See https://llvm.org/PR48091 for 14151fd87a68SDimitry Andric /// more commentry on what "in scope" means. 14161fd87a68SDimitry Andric /// \p DILoc A location in the scope that we're fetching blocks for. 14171fd87a68SDimitry Andric /// \p Output Set to put in-scope-blocks into. 14181fd87a68SDimitry Andric /// \p AssignBlocks Blocks known to contain assignments of variables in scope. 14191fd87a68SDimitry Andric void 14201fd87a68SDimitry Andric getBlocksForScope(const DILocation *DILoc, 14211fd87a68SDimitry Andric SmallPtrSetImpl<const MachineBasicBlock *> &Output, 14221fd87a68SDimitry Andric const SmallPtrSetImpl<MachineBasicBlock *> &AssignBlocks); 14231fd87a68SDimitry Andric 1424349cc55cSDimitry Andric /// Solve the variable value dataflow problem, for a single lexical scope. 1425349cc55cSDimitry Andric /// Uses the algorithm from the file comment to resolve control flow joins 1426349cc55cSDimitry Andric /// using PHI placement and value propagation. Reads the locations of machine 1427349cc55cSDimitry Andric /// values from the \p MInLocs and \p MOutLocs arrays (see buildMLocValueMap) 1428349cc55cSDimitry Andric /// and reads the variable values transfer function from \p AllTheVlocs. 1429349cc55cSDimitry Andric /// Live-in and Live-out variable values are stored locally, with the live-ins 1430349cc55cSDimitry Andric /// permanently stored to \p Output once a fixedpoint is reached. 1431349cc55cSDimitry Andric /// \p VarsWeCareAbout contains a collection of the variables in \p Scope 1432349cc55cSDimitry Andric /// that we should be tracking. 1433349cc55cSDimitry Andric /// \p AssignBlocks contains the set of blocks that aren't in \p DILoc's 1434349cc55cSDimitry Andric /// scope, but which do contain DBG_VALUEs, which VarLocBasedImpl tracks 1435349cc55cSDimitry Andric /// locations through. 1436349cc55cSDimitry Andric void buildVLocValueMap(const DILocation *DILoc, 1437*0fca6ea1SDimitry Andric const SmallSet<DebugVariableID, 4> &VarsWeCareAbout, 1438349cc55cSDimitry Andric SmallPtrSetImpl<MachineBasicBlock *> &AssignBlocks, 143981ad6265SDimitry Andric LiveInsT &Output, FuncValueTable &MOutLocs, 144081ad6265SDimitry Andric FuncValueTable &MInLocs, 1441349cc55cSDimitry Andric SmallVectorImpl<VLocTracker> &AllTheVLocs); 1442349cc55cSDimitry Andric 1443349cc55cSDimitry Andric /// Attempt to eliminate un-necessary PHIs on entry to a block. Examines the 1444349cc55cSDimitry Andric /// live-in values coming from predecessors live-outs, and replaces any PHIs 1445349cc55cSDimitry Andric /// already present in this blocks live-ins with a live-through value if the 1446349cc55cSDimitry Andric /// PHI isn't needed. 1447349cc55cSDimitry Andric /// \p LiveIn Old live-in value, overwritten with new one if live-in changes. 1448349cc55cSDimitry Andric /// \returns true if any live-ins change value, either from value propagation 1449349cc55cSDimitry Andric /// or PHI elimination. 1450349cc55cSDimitry Andric bool vlocJoin(MachineBasicBlock &MBB, LiveIdxT &VLOCOutLocs, 1451349cc55cSDimitry Andric SmallPtrSet<const MachineBasicBlock *, 8> &BlocksToExplore, 1452349cc55cSDimitry Andric DbgValue &LiveIn); 1453349cc55cSDimitry Andric 1454bdd1243dSDimitry Andric /// For the given block and live-outs feeding into it, try to find 1455bdd1243dSDimitry Andric /// machine locations for each debug operand where all the values feeding 1456bdd1243dSDimitry Andric /// into that operand join together. 1457bdd1243dSDimitry Andric /// \returns true if a joined location was found for every value that needed 1458bdd1243dSDimitry Andric /// to be joined. 1459bdd1243dSDimitry Andric bool 1460bdd1243dSDimitry Andric pickVPHILoc(SmallVectorImpl<DbgOpID> &OutValues, const MachineBasicBlock &MBB, 146181ad6265SDimitry Andric const LiveIdxT &LiveOuts, FuncValueTable &MOutLocs, 1462349cc55cSDimitry Andric const SmallVectorImpl<const MachineBasicBlock *> &BlockOrders); 1463349cc55cSDimitry Andric 1464bdd1243dSDimitry Andric std::optional<ValueIDNum> pickOperandPHILoc( 1465bdd1243dSDimitry Andric unsigned DbgOpIdx, const MachineBasicBlock &MBB, const LiveIdxT &LiveOuts, 1466bdd1243dSDimitry Andric FuncValueTable &MOutLocs, 1467bdd1243dSDimitry Andric const SmallVectorImpl<const MachineBasicBlock *> &BlockOrders); 1468bdd1243dSDimitry Andric 14691fd87a68SDimitry Andric /// Take collections of DBG_VALUE instructions stored in TTracker, and 1470*0fca6ea1SDimitry Andric /// install them into their output blocks. 1471*0fca6ea1SDimitry Andric bool emitTransfers(); 14721fd87a68SDimitry Andric 1473349cc55cSDimitry Andric /// Boilerplate computation of some initial sets, artifical blocks and 1474349cc55cSDimitry Andric /// RPOT block ordering. 1475349cc55cSDimitry Andric void initialSetup(MachineFunction &MF); 1476349cc55cSDimitry Andric 1477d56accc7SDimitry Andric /// Produce a map of the last lexical scope that uses a block, using the 1478d56accc7SDimitry Andric /// scopes DFSOut number. Mapping is block-number to DFSOut. 1479d56accc7SDimitry Andric /// \p EjectionMap Pre-allocated vector in which to install the built ma. 1480d56accc7SDimitry Andric /// \p ScopeToDILocation Mapping of LexicalScopes to their DILocations. 1481d56accc7SDimitry Andric /// \p AssignBlocks Map of blocks where assignments happen for a scope. 1482d56accc7SDimitry Andric void makeDepthFirstEjectionMap(SmallVectorImpl<unsigned> &EjectionMap, 1483d56accc7SDimitry Andric const ScopeToDILocT &ScopeToDILocation, 1484d56accc7SDimitry Andric ScopeToAssignBlocksT &AssignBlocks); 1485d56accc7SDimitry Andric 1486d56accc7SDimitry Andric /// When determining per-block variable values and emitting to DBG_VALUEs, 1487d56accc7SDimitry Andric /// this function explores by lexical scope depth. Doing so means that per 1488d56accc7SDimitry Andric /// block information can be fully computed before exploration finishes, 1489d56accc7SDimitry Andric /// allowing us to emit it and free data structures earlier than otherwise. 1490d56accc7SDimitry Andric /// It's also good for locality. 1491*0fca6ea1SDimitry Andric bool depthFirstVLocAndEmit(unsigned MaxNumBlocks, 1492*0fca6ea1SDimitry Andric const ScopeToDILocT &ScopeToDILocation, 1493*0fca6ea1SDimitry Andric const ScopeToVarsT &ScopeToVars, 1494*0fca6ea1SDimitry Andric ScopeToAssignBlocksT &ScopeToBlocks, 1495*0fca6ea1SDimitry Andric LiveInsT &Output, FuncValueTable &MOutLocs, 1496*0fca6ea1SDimitry Andric FuncValueTable &MInLocs, 1497*0fca6ea1SDimitry Andric SmallVectorImpl<VLocTracker> &AllTheVLocs, 1498*0fca6ea1SDimitry Andric MachineFunction &MF, const TargetPassConfig &TPC); 1499d56accc7SDimitry Andric 1500349cc55cSDimitry Andric bool ExtendRanges(MachineFunction &MF, MachineDominatorTree *DomTree, 1501349cc55cSDimitry Andric TargetPassConfig *TPC, unsigned InputBBLimit, 1502349cc55cSDimitry Andric unsigned InputDbgValLimit) override; 1503349cc55cSDimitry Andric 1504349cc55cSDimitry Andric public: 1505349cc55cSDimitry Andric /// Default construct and initialize the pass. 1506349cc55cSDimitry Andric InstrRefBasedLDV(); 1507349cc55cSDimitry Andric 1508349cc55cSDimitry Andric LLVM_DUMP_METHOD 1509349cc55cSDimitry Andric void dump_mloc_transfer(const MLocTransferMap &mloc_transfer) const; 1510349cc55cSDimitry Andric 1511349cc55cSDimitry Andric bool isCalleeSaved(LocIdx L) const; 1512bdd1243dSDimitry Andric bool isCalleeSavedReg(Register R) const; 1513349cc55cSDimitry Andric 1514349cc55cSDimitry Andric bool hasFoldedStackStore(const MachineInstr &MI) { 1515349cc55cSDimitry Andric // Instruction must have a memory operand that's a stack slot, and isn't 1516349cc55cSDimitry Andric // aliased, meaning it's a spill from regalloc instead of a variable. 1517349cc55cSDimitry Andric // If it's aliased, we can't guarantee its value. 1518349cc55cSDimitry Andric if (!MI.hasOneMemOperand()) 1519349cc55cSDimitry Andric return false; 1520349cc55cSDimitry Andric auto *MemOperand = *MI.memoperands_begin(); 1521349cc55cSDimitry Andric return MemOperand->isStore() && 1522349cc55cSDimitry Andric MemOperand->getPseudoValue() && 1523349cc55cSDimitry Andric MemOperand->getPseudoValue()->kind() == PseudoSourceValue::FixedStack 1524349cc55cSDimitry Andric && !MemOperand->getPseudoValue()->isAliased(MFI); 1525349cc55cSDimitry Andric } 1526349cc55cSDimitry Andric 1527bdd1243dSDimitry Andric std::optional<LocIdx> findLocationForMemOperand(const MachineInstr &MI); 1528*0fca6ea1SDimitry Andric 1529*0fca6ea1SDimitry Andric // Utility for unit testing, don't use directly. 1530*0fca6ea1SDimitry Andric DebugVariableMap &getDVMap() { 1531*0fca6ea1SDimitry Andric return DVMap; 1532*0fca6ea1SDimitry Andric } 1533349cc55cSDimitry Andric }; 1534349cc55cSDimitry Andric 1535349cc55cSDimitry Andric } // namespace LiveDebugValues 1536349cc55cSDimitry Andric 1537349cc55cSDimitry Andric #endif /* LLVM_LIB_CODEGEN_LIVEDEBUGVALUES_INSTRREFBASEDLDV_H */ 1538