1c0981da4SDimitry Andric //===- InstrRefBasedImpl.h - Tracking Debug Value MIs ---------------------===// 2c0981da4SDimitry Andric // 3c0981da4SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4c0981da4SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5c0981da4SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6c0981da4SDimitry Andric // 7c0981da4SDimitry Andric //===----------------------------------------------------------------------===// 8c0981da4SDimitry Andric 9c0981da4SDimitry Andric #ifndef LLVM_LIB_CODEGEN_LIVEDEBUGVALUES_INSTRREFBASEDLDV_H 10c0981da4SDimitry Andric #define LLVM_LIB_CODEGEN_LIVEDEBUGVALUES_INSTRREFBASEDLDV_H 11c0981da4SDimitry Andric 12c0981da4SDimitry Andric #include "llvm/ADT/DenseMap.h" 13145449b1SDimitry Andric #include "llvm/ADT/IndexedMap.h" 14c0981da4SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 15c0981da4SDimitry Andric #include "llvm/ADT/SmallVector.h" 16c0981da4SDimitry Andric #include "llvm/ADT/UniqueVector.h" 17c0981da4SDimitry Andric #include "llvm/CodeGen/LexicalScopes.h" 18c0981da4SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 19c0981da4SDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 20145449b1SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 21c0981da4SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h" 22e3b55780SDimitry Andric #include <optional> 23c0981da4SDimitry Andric 24c0981da4SDimitry Andric #include "LiveDebugValues.h" 25c0981da4SDimitry Andric 26c0981da4SDimitry Andric class TransferTracker; 27c0981da4SDimitry Andric 28c0981da4SDimitry Andric // Forward dec of unit test class, so that we can peer into the LDV object. 29c0981da4SDimitry Andric class InstrRefLDVTest; 30c0981da4SDimitry Andric 31c0981da4SDimitry Andric namespace LiveDebugValues { 32c0981da4SDimitry Andric 33c0981da4SDimitry Andric class MLocTracker; 34e3b55780SDimitry Andric class DbgOpIDMap; 35c0981da4SDimitry Andric 36c0981da4SDimitry Andric using namespace llvm; 37c0981da4SDimitry Andric 38ac9a064cSDimitry Andric using DebugVariableID = unsigned; 39ac9a064cSDimitry Andric using VarAndLoc = std::pair<DebugVariable, const DILocation *>; 40ac9a064cSDimitry Andric 41ac9a064cSDimitry Andric /// Mapping from DebugVariable to/from a unique identifying number. Each 42ac9a064cSDimitry Andric /// DebugVariable consists of three pointers, and after a small amount of 43ac9a064cSDimitry Andric /// work to identify overlapping fragments of variables we mostly only use 44ac9a064cSDimitry Andric /// DebugVariables as identities of variables. It's much more compile-time 45ac9a064cSDimitry Andric /// efficient to use an ID number instead, which this class provides. 46ac9a064cSDimitry Andric class DebugVariableMap { 47ac9a064cSDimitry Andric DenseMap<DebugVariable, unsigned> VarToIdx; 48ac9a064cSDimitry Andric SmallVector<VarAndLoc> IdxToVar; 49ac9a064cSDimitry Andric 50ac9a064cSDimitry Andric public: getDVID(const DebugVariable & Var)51ac9a064cSDimitry Andric DebugVariableID getDVID(const DebugVariable &Var) const { 52ac9a064cSDimitry Andric auto It = VarToIdx.find(Var); 53ac9a064cSDimitry Andric assert(It != VarToIdx.end()); 54ac9a064cSDimitry Andric return It->second; 55ac9a064cSDimitry Andric } 56ac9a064cSDimitry Andric insertDVID(DebugVariable & Var,const DILocation * Loc)57ac9a064cSDimitry Andric DebugVariableID insertDVID(DebugVariable &Var, const DILocation *Loc) { 58ac9a064cSDimitry Andric unsigned Size = VarToIdx.size(); 59ac9a064cSDimitry Andric auto ItPair = VarToIdx.insert({Var, Size}); 60ac9a064cSDimitry Andric if (ItPair.second) { 61ac9a064cSDimitry Andric IdxToVar.push_back({Var, Loc}); 62ac9a064cSDimitry Andric return Size; 63ac9a064cSDimitry Andric } 64ac9a064cSDimitry Andric 65ac9a064cSDimitry Andric return ItPair.first->second; 66ac9a064cSDimitry Andric } 67ac9a064cSDimitry Andric lookupDVID(DebugVariableID ID)68ac9a064cSDimitry Andric const VarAndLoc &lookupDVID(DebugVariableID ID) const { return IdxToVar[ID]; } 69ac9a064cSDimitry Andric clear()70ac9a064cSDimitry Andric void clear() { 71ac9a064cSDimitry Andric VarToIdx.clear(); 72ac9a064cSDimitry Andric IdxToVar.clear(); 73ac9a064cSDimitry Andric } 74ac9a064cSDimitry Andric }; 75ac9a064cSDimitry Andric 76c0981da4SDimitry Andric /// Handle-class for a particular "location". This value-type uniquely 77c0981da4SDimitry Andric /// symbolises a register or stack location, allowing manipulation of locations 78c0981da4SDimitry Andric /// without concern for where that location is. Practically, this allows us to 79c0981da4SDimitry Andric /// treat the state of the machine at a particular point as an array of values, 80c0981da4SDimitry Andric /// rather than a map of values. 81c0981da4SDimitry Andric class LocIdx { 82c0981da4SDimitry Andric unsigned Location; 83c0981da4SDimitry Andric 84c0981da4SDimitry Andric // Default constructor is private, initializing to an illegal location number. 85c0981da4SDimitry Andric // Use only for "not an entry" elements in IndexedMaps. LocIdx()86c0981da4SDimitry Andric LocIdx() : Location(UINT_MAX) {} 87c0981da4SDimitry Andric 88c0981da4SDimitry Andric public: 89c0981da4SDimitry Andric #define NUM_LOC_BITS 24 LocIdx(unsigned L)90c0981da4SDimitry Andric LocIdx(unsigned L) : Location(L) { 91c0981da4SDimitry Andric assert(L < (1 << NUM_LOC_BITS) && "Machine locations must fit in 24 bits"); 92c0981da4SDimitry Andric } 93c0981da4SDimitry Andric MakeIllegalLoc()94c0981da4SDimitry Andric static LocIdx MakeIllegalLoc() { return LocIdx(); } MakeTombstoneLoc()95c0981da4SDimitry Andric static LocIdx MakeTombstoneLoc() { 96c0981da4SDimitry Andric LocIdx L = LocIdx(); 97c0981da4SDimitry Andric --L.Location; 98c0981da4SDimitry Andric return L; 99c0981da4SDimitry Andric } 100c0981da4SDimitry Andric isIllegal()101c0981da4SDimitry Andric bool isIllegal() const { return Location == UINT_MAX; } 102c0981da4SDimitry Andric asU64()103c0981da4SDimitry Andric uint64_t asU64() const { return Location; } 104c0981da4SDimitry Andric 105c0981da4SDimitry Andric bool operator==(unsigned L) const { return Location == L; } 106c0981da4SDimitry Andric 107c0981da4SDimitry Andric bool operator==(const LocIdx &L) const { return Location == L.Location; } 108c0981da4SDimitry Andric 109c0981da4SDimitry Andric bool operator!=(unsigned L) const { return !(*this == L); } 110c0981da4SDimitry Andric 111c0981da4SDimitry Andric bool operator!=(const LocIdx &L) const { return !(*this == L); } 112c0981da4SDimitry Andric 113c0981da4SDimitry Andric bool operator<(const LocIdx &Other) const { 114c0981da4SDimitry Andric return Location < Other.Location; 115c0981da4SDimitry Andric } 116c0981da4SDimitry Andric }; 117c0981da4SDimitry Andric 118c0981da4SDimitry Andric // The location at which a spilled value resides. It consists of a register and 119c0981da4SDimitry Andric // an offset. 120c0981da4SDimitry Andric struct SpillLoc { 121c0981da4SDimitry Andric unsigned SpillBase; 122c0981da4SDimitry Andric StackOffset SpillOffset; 123c0981da4SDimitry Andric bool operator==(const SpillLoc &Other) const { 124c0981da4SDimitry Andric return std::make_pair(SpillBase, SpillOffset) == 125c0981da4SDimitry Andric std::make_pair(Other.SpillBase, Other.SpillOffset); 126c0981da4SDimitry Andric } 127c0981da4SDimitry Andric bool operator<(const SpillLoc &Other) const { 128c0981da4SDimitry Andric return std::make_tuple(SpillBase, SpillOffset.getFixed(), 129c0981da4SDimitry Andric SpillOffset.getScalable()) < 130c0981da4SDimitry Andric std::make_tuple(Other.SpillBase, Other.SpillOffset.getFixed(), 131c0981da4SDimitry Andric Other.SpillOffset.getScalable()); 132c0981da4SDimitry Andric } 133c0981da4SDimitry Andric }; 134c0981da4SDimitry Andric 135c0981da4SDimitry Andric /// Unique identifier for a value defined by an instruction, as a value type. 136c0981da4SDimitry Andric /// Casts back and forth to a uint64_t. Probably replacable with something less 137c0981da4SDimitry Andric /// bit-constrained. Each value identifies the instruction and machine location 138c0981da4SDimitry Andric /// where the value is defined, although there may be no corresponding machine 139c0981da4SDimitry Andric /// operand for it (ex: regmasks clobbering values). The instructions are 140c0981da4SDimitry Andric /// one-based, and definitions that are PHIs have instruction number zero. 141c0981da4SDimitry Andric /// 142c0981da4SDimitry Andric /// The obvious limits of a 1M block function or 1M instruction blocks are 143c0981da4SDimitry Andric /// problematic; but by that point we should probably have bailed out of 144c0981da4SDimitry Andric /// trying to analyse the function. 145c0981da4SDimitry Andric class ValueIDNum { 146c0981da4SDimitry Andric union { 147c0981da4SDimitry Andric struct { 148c0981da4SDimitry Andric uint64_t BlockNo : 20; /// The block where the def happens. 149c0981da4SDimitry Andric uint64_t InstNo : 20; /// The Instruction where the def happens. 150c0981da4SDimitry Andric /// One based, is distance from start of block. 151c0981da4SDimitry Andric uint64_t LocNo 152c0981da4SDimitry Andric : NUM_LOC_BITS; /// The machine location where the def happens. 153c0981da4SDimitry Andric } s; 154c0981da4SDimitry Andric uint64_t Value; 155c0981da4SDimitry Andric } u; 156c0981da4SDimitry Andric 157c0981da4SDimitry Andric static_assert(sizeof(u) == 8, "Badly packed ValueIDNum?"); 158c0981da4SDimitry Andric 159c0981da4SDimitry Andric public: 160c0981da4SDimitry Andric // Default-initialize to EmptyValue. This is necessary to make IndexedMaps 161c0981da4SDimitry Andric // of values to work. ValueIDNum()162c0981da4SDimitry Andric ValueIDNum() { u.Value = EmptyValue.asU64(); } 163c0981da4SDimitry Andric ValueIDNum(uint64_t Block,uint64_t Inst,uint64_t Loc)164c0981da4SDimitry Andric ValueIDNum(uint64_t Block, uint64_t Inst, uint64_t Loc) { 165c0981da4SDimitry Andric u.s = {Block, Inst, Loc}; 166c0981da4SDimitry Andric } 167c0981da4SDimitry Andric ValueIDNum(uint64_t Block,uint64_t Inst,LocIdx Loc)168c0981da4SDimitry Andric ValueIDNum(uint64_t Block, uint64_t Inst, LocIdx Loc) { 169c0981da4SDimitry Andric u.s = {Block, Inst, Loc.asU64()}; 170c0981da4SDimitry Andric } 171c0981da4SDimitry Andric getBlock()172c0981da4SDimitry Andric uint64_t getBlock() const { return u.s.BlockNo; } getInst()173c0981da4SDimitry Andric uint64_t getInst() const { return u.s.InstNo; } getLoc()174c0981da4SDimitry Andric uint64_t getLoc() const { return u.s.LocNo; } isPHI()175c0981da4SDimitry Andric bool isPHI() const { return u.s.InstNo == 0; } 176c0981da4SDimitry Andric asU64()177c0981da4SDimitry Andric uint64_t asU64() const { return u.Value; } 178c0981da4SDimitry Andric fromU64(uint64_t v)179c0981da4SDimitry Andric static ValueIDNum fromU64(uint64_t v) { 180c0981da4SDimitry Andric ValueIDNum Val; 181c0981da4SDimitry Andric Val.u.Value = v; 182c0981da4SDimitry Andric return Val; 183c0981da4SDimitry Andric } 184c0981da4SDimitry Andric 185c0981da4SDimitry Andric bool operator<(const ValueIDNum &Other) const { 186c0981da4SDimitry Andric return asU64() < Other.asU64(); 187c0981da4SDimitry Andric } 188c0981da4SDimitry Andric 189c0981da4SDimitry Andric bool operator==(const ValueIDNum &Other) const { 190c0981da4SDimitry Andric return u.Value == Other.u.Value; 191c0981da4SDimitry Andric } 192c0981da4SDimitry Andric 193c0981da4SDimitry Andric bool operator!=(const ValueIDNum &Other) const { return !(*this == Other); } 194c0981da4SDimitry Andric asString(const std::string & mlocname)195c0981da4SDimitry Andric std::string asString(const std::string &mlocname) const { 196c0981da4SDimitry Andric return Twine("Value{bb: ") 197c0981da4SDimitry Andric .concat(Twine(u.s.BlockNo) 198c0981da4SDimitry Andric .concat(Twine(", inst: ") 199c0981da4SDimitry Andric .concat((u.s.InstNo ? Twine(u.s.InstNo) 200c0981da4SDimitry Andric : Twine("live-in")) 201c0981da4SDimitry Andric .concat(Twine(", loc: ").concat( 202c0981da4SDimitry Andric Twine(mlocname))) 203c0981da4SDimitry Andric .concat(Twine("}"))))) 204c0981da4SDimitry Andric .str(); 205c0981da4SDimitry Andric } 206c0981da4SDimitry Andric 207c0981da4SDimitry Andric static ValueIDNum EmptyValue; 208c0981da4SDimitry Andric static ValueIDNum TombstoneValue; 209c0981da4SDimitry Andric }; 210c0981da4SDimitry Andric 211e3b55780SDimitry Andric } // End namespace LiveDebugValues 212e3b55780SDimitry Andric 213e3b55780SDimitry Andric namespace llvm { 214e3b55780SDimitry Andric using namespace LiveDebugValues; 215e3b55780SDimitry Andric 216e3b55780SDimitry Andric template <> struct DenseMapInfo<LocIdx> { 217e3b55780SDimitry Andric static inline LocIdx getEmptyKey() { return LocIdx::MakeIllegalLoc(); } 218e3b55780SDimitry Andric static inline LocIdx getTombstoneKey() { return LocIdx::MakeTombstoneLoc(); } 219e3b55780SDimitry Andric 220e3b55780SDimitry Andric static unsigned getHashValue(const LocIdx &Loc) { return Loc.asU64(); } 221e3b55780SDimitry Andric 222e3b55780SDimitry Andric static bool isEqual(const LocIdx &A, const LocIdx &B) { return A == B; } 223e3b55780SDimitry Andric }; 224e3b55780SDimitry Andric 225e3b55780SDimitry Andric template <> struct DenseMapInfo<ValueIDNum> { 226e3b55780SDimitry Andric static inline ValueIDNum getEmptyKey() { return ValueIDNum::EmptyValue; } 227e3b55780SDimitry Andric static inline ValueIDNum getTombstoneKey() { 228e3b55780SDimitry Andric return ValueIDNum::TombstoneValue; 229e3b55780SDimitry Andric } 230e3b55780SDimitry Andric 231e3b55780SDimitry Andric static unsigned getHashValue(const ValueIDNum &Val) { 232e3b55780SDimitry Andric return hash_value(Val.asU64()); 233e3b55780SDimitry Andric } 234e3b55780SDimitry Andric 235e3b55780SDimitry Andric static bool isEqual(const ValueIDNum &A, const ValueIDNum &B) { 236e3b55780SDimitry Andric return A == B; 237e3b55780SDimitry Andric } 238e3b55780SDimitry Andric }; 239e3b55780SDimitry Andric 240e3b55780SDimitry Andric } // end namespace llvm 241e3b55780SDimitry Andric 242e3b55780SDimitry Andric namespace LiveDebugValues { 243e3b55780SDimitry Andric using namespace llvm; 244e3b55780SDimitry Andric 245145449b1SDimitry Andric /// Type for a table of values in a block. 246312c0ed1SDimitry Andric using ValueTable = SmallVector<ValueIDNum, 0>; 247145449b1SDimitry Andric 24899aabd70SDimitry Andric /// A collection of ValueTables, one per BB in a function, with convenient 24999aabd70SDimitry Andric /// accessor methods. 25099aabd70SDimitry Andric struct FuncValueTable { 25199aabd70SDimitry Andric FuncValueTable(int NumBBs, int NumLocs) { 25299aabd70SDimitry Andric Storage.reserve(NumBBs); 25399aabd70SDimitry Andric for (int i = 0; i != NumBBs; ++i) 25499aabd70SDimitry Andric Storage.push_back( 25599aabd70SDimitry Andric std::make_unique<ValueTable>(NumLocs, ValueIDNum::EmptyValue)); 25699aabd70SDimitry Andric } 25799aabd70SDimitry Andric 25899aabd70SDimitry Andric /// Returns the ValueTable associated with MBB. 25999aabd70SDimitry Andric ValueTable &operator[](const MachineBasicBlock &MBB) const { 26099aabd70SDimitry Andric return (*this)[MBB.getNumber()]; 26199aabd70SDimitry Andric } 26299aabd70SDimitry Andric 26399aabd70SDimitry Andric /// Returns the ValueTable associated with the MachineBasicBlock whose number 26499aabd70SDimitry Andric /// is MBBNum. 26599aabd70SDimitry Andric ValueTable &operator[](int MBBNum) const { 26699aabd70SDimitry Andric auto &TablePtr = Storage[MBBNum]; 26799aabd70SDimitry Andric assert(TablePtr && "Trying to access a deleted table"); 26899aabd70SDimitry Andric return *TablePtr; 26999aabd70SDimitry Andric } 27099aabd70SDimitry Andric 27199aabd70SDimitry Andric /// Returns the ValueTable associated with the entry MachineBasicBlock. 27299aabd70SDimitry Andric ValueTable &tableForEntryMBB() const { return (*this)[0]; } 27399aabd70SDimitry Andric 27499aabd70SDimitry Andric /// Returns true if the ValueTable associated with MBB has not been freed. 27599aabd70SDimitry Andric bool hasTableFor(MachineBasicBlock &MBB) const { 27699aabd70SDimitry Andric return Storage[MBB.getNumber()] != nullptr; 27799aabd70SDimitry Andric } 27899aabd70SDimitry Andric 27999aabd70SDimitry Andric /// Frees the memory of the ValueTable associated with MBB. 28099aabd70SDimitry Andric void ejectTableForBlock(const MachineBasicBlock &MBB) { 28199aabd70SDimitry Andric Storage[MBB.getNumber()].reset(); 28299aabd70SDimitry Andric } 28399aabd70SDimitry Andric 28499aabd70SDimitry Andric private: 28599aabd70SDimitry Andric /// ValueTables are stored as unique_ptrs to allow for deallocation during 28699aabd70SDimitry Andric /// LDV; this was measured to have a significant impact on compiler memory 28799aabd70SDimitry Andric /// usage. 28899aabd70SDimitry Andric SmallVector<std::unique_ptr<ValueTable>, 0> Storage; 28999aabd70SDimitry Andric }; 290145449b1SDimitry Andric 291c0981da4SDimitry Andric /// Thin wrapper around an integer -- designed to give more type safety to 292c0981da4SDimitry Andric /// spill location numbers. 293c0981da4SDimitry Andric class SpillLocationNo { 294c0981da4SDimitry Andric public: 295c0981da4SDimitry Andric explicit SpillLocationNo(unsigned SpillNo) : SpillNo(SpillNo) {} 296c0981da4SDimitry Andric unsigned SpillNo; 297c0981da4SDimitry Andric unsigned id() const { return SpillNo; } 298c0981da4SDimitry Andric 299c0981da4SDimitry Andric bool operator<(const SpillLocationNo &Other) const { 300c0981da4SDimitry Andric return SpillNo < Other.SpillNo; 301c0981da4SDimitry Andric } 302c0981da4SDimitry Andric 303c0981da4SDimitry Andric bool operator==(const SpillLocationNo &Other) const { 304c0981da4SDimitry Andric return SpillNo == Other.SpillNo; 305c0981da4SDimitry Andric } 306c0981da4SDimitry Andric bool operator!=(const SpillLocationNo &Other) const { 307c0981da4SDimitry Andric return !(*this == Other); 308c0981da4SDimitry Andric } 309c0981da4SDimitry Andric }; 310c0981da4SDimitry Andric 311c0981da4SDimitry Andric /// Meta qualifiers for a value. Pair of whatever expression is used to qualify 312145449b1SDimitry Andric /// the value, and Boolean of whether or not it's indirect. 313c0981da4SDimitry Andric class DbgValueProperties { 314c0981da4SDimitry Andric public: 315e3b55780SDimitry Andric DbgValueProperties(const DIExpression *DIExpr, bool Indirect, bool IsVariadic) 316e3b55780SDimitry Andric : DIExpr(DIExpr), Indirect(Indirect), IsVariadic(IsVariadic) {} 317c0981da4SDimitry Andric 318c0981da4SDimitry Andric /// Extract properties from an existing DBG_VALUE instruction. 319c0981da4SDimitry Andric DbgValueProperties(const MachineInstr &MI) { 320c0981da4SDimitry Andric assert(MI.isDebugValue()); 321e3b55780SDimitry Andric assert(MI.getDebugExpression()->getNumLocationOperands() == 0 || 322e3b55780SDimitry Andric MI.isDebugValueList() || MI.isUndefDebugValue()); 323e3b55780SDimitry Andric IsVariadic = MI.isDebugValueList(); 324c0981da4SDimitry Andric DIExpr = MI.getDebugExpression(); 325e3b55780SDimitry Andric Indirect = MI.isDebugOffsetImm(); 326e3b55780SDimitry Andric } 327e3b55780SDimitry Andric 328e3b55780SDimitry Andric bool isJoinable(const DbgValueProperties &Other) const { 329e3b55780SDimitry Andric return DIExpression::isEqualExpression(DIExpr, Indirect, Other.DIExpr, 330e3b55780SDimitry Andric Other.Indirect); 331c0981da4SDimitry Andric } 332c0981da4SDimitry Andric 333c0981da4SDimitry Andric bool operator==(const DbgValueProperties &Other) const { 334e3b55780SDimitry Andric return std::tie(DIExpr, Indirect, IsVariadic) == 335e3b55780SDimitry Andric std::tie(Other.DIExpr, Other.Indirect, Other.IsVariadic); 336c0981da4SDimitry Andric } 337c0981da4SDimitry Andric 338c0981da4SDimitry Andric bool operator!=(const DbgValueProperties &Other) const { 339c0981da4SDimitry Andric return !(*this == Other); 340c0981da4SDimitry Andric } 341c0981da4SDimitry Andric 342e3b55780SDimitry Andric unsigned getLocationOpCount() const { 343e3b55780SDimitry Andric return IsVariadic ? DIExpr->getNumLocationOperands() : 1; 344e3b55780SDimitry Andric } 345e3b55780SDimitry Andric 346c0981da4SDimitry Andric const DIExpression *DIExpr; 347c0981da4SDimitry Andric bool Indirect; 348e3b55780SDimitry Andric bool IsVariadic; 349c0981da4SDimitry Andric }; 350c0981da4SDimitry Andric 351e3b55780SDimitry Andric /// TODO: Might pack better if we changed this to a Struct of Arrays, since 352e3b55780SDimitry Andric /// MachineOperand is width 32, making this struct width 33. We could also 353e3b55780SDimitry Andric /// potentially avoid storing the whole MachineOperand (sizeof=32), instead 354e3b55780SDimitry Andric /// choosing to store just the contents portion (sizeof=8) and a Kind enum, 355e3b55780SDimitry Andric /// since we already know it is some type of immediate value. 356e3b55780SDimitry Andric /// Stores a single debug operand, which can either be a MachineOperand for 357e3b55780SDimitry Andric /// directly storing immediate values, or a ValueIDNum representing some value 358e3b55780SDimitry Andric /// computed at some point in the program. IsConst is used as a discriminator. 359e3b55780SDimitry Andric struct DbgOp { 360e3b55780SDimitry Andric union { 361e3b55780SDimitry Andric ValueIDNum ID; 362e3b55780SDimitry Andric MachineOperand MO; 363e3b55780SDimitry Andric }; 364e3b55780SDimitry Andric bool IsConst; 365e3b55780SDimitry Andric 366e3b55780SDimitry Andric DbgOp() : ID(ValueIDNum::EmptyValue), IsConst(false) {} 367e3b55780SDimitry Andric DbgOp(ValueIDNum ID) : ID(ID), IsConst(false) {} 368e3b55780SDimitry Andric DbgOp(MachineOperand MO) : MO(MO), IsConst(true) {} 369e3b55780SDimitry Andric 370e3b55780SDimitry Andric bool isUndef() const { return !IsConst && ID == ValueIDNum::EmptyValue; } 371e3b55780SDimitry Andric 372e3b55780SDimitry Andric #ifndef NDEBUG 373e3b55780SDimitry Andric void dump(const MLocTracker *MTrack) const; 374e3b55780SDimitry Andric #endif 375e3b55780SDimitry Andric }; 376e3b55780SDimitry Andric 377e3b55780SDimitry Andric /// A DbgOp whose ID (if any) has resolved to an actual location, LocIdx. Used 378e3b55780SDimitry Andric /// when working with concrete debug values, i.e. when joining MLocs and VLocs 379e3b55780SDimitry Andric /// in the TransferTracker or emitting DBG_VALUE/DBG_VALUE_LIST instructions in 380e3b55780SDimitry Andric /// the MLocTracker. 381e3b55780SDimitry Andric struct ResolvedDbgOp { 382e3b55780SDimitry Andric union { 383e3b55780SDimitry Andric LocIdx Loc; 384e3b55780SDimitry Andric MachineOperand MO; 385e3b55780SDimitry Andric }; 386e3b55780SDimitry Andric bool IsConst; 387e3b55780SDimitry Andric 388e3b55780SDimitry Andric ResolvedDbgOp(LocIdx Loc) : Loc(Loc), IsConst(false) {} 389e3b55780SDimitry Andric ResolvedDbgOp(MachineOperand MO) : MO(MO), IsConst(true) {} 390e3b55780SDimitry Andric 391e3b55780SDimitry Andric bool operator==(const ResolvedDbgOp &Other) const { 392e3b55780SDimitry Andric if (IsConst != Other.IsConst) 393e3b55780SDimitry Andric return false; 394e3b55780SDimitry Andric if (IsConst) 395e3b55780SDimitry Andric return MO.isIdenticalTo(Other.MO); 396e3b55780SDimitry Andric return Loc == Other.Loc; 397e3b55780SDimitry Andric } 398e3b55780SDimitry Andric 399e3b55780SDimitry Andric #ifndef NDEBUG 400e3b55780SDimitry Andric void dump(const MLocTracker *MTrack) const; 401e3b55780SDimitry Andric #endif 402e3b55780SDimitry Andric }; 403e3b55780SDimitry Andric 404e3b55780SDimitry Andric /// An ID used in the DbgOpIDMap (below) to lookup a stored DbgOp. This is used 405e3b55780SDimitry Andric /// in place of actual DbgOps inside of a DbgValue to reduce its size, as 406e3b55780SDimitry Andric /// DbgValue is very frequently used and passed around, and the actual DbgOp is 407e3b55780SDimitry Andric /// over 8x larger than this class, due to storing a MachineOperand. This ID 408e3b55780SDimitry Andric /// should be equal for all equal DbgOps, and also encodes whether the mapped 409e3b55780SDimitry Andric /// DbgOp is a constant, meaning that for simple equality or const-ness checks 410e3b55780SDimitry Andric /// it is not necessary to lookup this ID. 411e3b55780SDimitry Andric struct DbgOpID { 412e3b55780SDimitry Andric struct IsConstIndexPair { 413e3b55780SDimitry Andric uint32_t IsConst : 1; 414e3b55780SDimitry Andric uint32_t Index : 31; 415e3b55780SDimitry Andric }; 416e3b55780SDimitry Andric 417e3b55780SDimitry Andric union { 418e3b55780SDimitry Andric struct IsConstIndexPair ID; 419e3b55780SDimitry Andric uint32_t RawID; 420e3b55780SDimitry Andric }; 421e3b55780SDimitry Andric 422e3b55780SDimitry Andric DbgOpID() : RawID(UndefID.RawID) { 423e3b55780SDimitry Andric static_assert(sizeof(DbgOpID) == 4, "DbgOpID should fit within 4 bytes."); 424e3b55780SDimitry Andric } 425e3b55780SDimitry Andric DbgOpID(uint32_t RawID) : RawID(RawID) {} 426e3b55780SDimitry Andric DbgOpID(bool IsConst, uint32_t Index) : ID({IsConst, Index}) {} 427e3b55780SDimitry Andric 428e3b55780SDimitry Andric static DbgOpID UndefID; 429e3b55780SDimitry Andric 430e3b55780SDimitry Andric bool operator==(const DbgOpID &Other) const { return RawID == Other.RawID; } 431e3b55780SDimitry Andric bool operator!=(const DbgOpID &Other) const { return !(*this == Other); } 432e3b55780SDimitry Andric 433e3b55780SDimitry Andric uint32_t asU32() const { return RawID; } 434e3b55780SDimitry Andric 435e3b55780SDimitry Andric bool isUndef() const { return *this == UndefID; } 436e3b55780SDimitry Andric bool isConst() const { return ID.IsConst && !isUndef(); } 437e3b55780SDimitry Andric uint32_t getIndex() const { return ID.Index; } 438e3b55780SDimitry Andric 439e3b55780SDimitry Andric #ifndef NDEBUG 440e3b55780SDimitry Andric void dump(const MLocTracker *MTrack, const DbgOpIDMap *OpStore) const; 441e3b55780SDimitry Andric #endif 442e3b55780SDimitry Andric }; 443e3b55780SDimitry Andric 444e3b55780SDimitry Andric /// Class storing the complete set of values that are observed by DbgValues 445e3b55780SDimitry Andric /// within the current function. Allows 2-way lookup, with `find` returning the 446e3b55780SDimitry Andric /// Op for a given ID and `insert` returning the ID for a given Op (creating one 447e3b55780SDimitry Andric /// if none exists). 448e3b55780SDimitry Andric class DbgOpIDMap { 449e3b55780SDimitry Andric 450e3b55780SDimitry Andric SmallVector<ValueIDNum, 0> ValueOps; 451e3b55780SDimitry Andric SmallVector<MachineOperand, 0> ConstOps; 452e3b55780SDimitry Andric 453e3b55780SDimitry Andric DenseMap<ValueIDNum, DbgOpID> ValueOpToID; 454e3b55780SDimitry Andric DenseMap<MachineOperand, DbgOpID> ConstOpToID; 455e3b55780SDimitry Andric 456e3b55780SDimitry Andric public: 457e3b55780SDimitry Andric /// If \p Op does not already exist in this map, it is inserted and the 458e3b55780SDimitry Andric /// corresponding DbgOpID is returned. If Op already exists in this map, then 459e3b55780SDimitry Andric /// no change is made and the existing ID for Op is returned. 460e3b55780SDimitry Andric /// Calling this with the undef DbgOp will always return DbgOpID::UndefID. 461e3b55780SDimitry Andric DbgOpID insert(DbgOp Op) { 462e3b55780SDimitry Andric if (Op.isUndef()) 463e3b55780SDimitry Andric return DbgOpID::UndefID; 464e3b55780SDimitry Andric if (Op.IsConst) 465e3b55780SDimitry Andric return insertConstOp(Op.MO); 466e3b55780SDimitry Andric return insertValueOp(Op.ID); 467e3b55780SDimitry Andric } 468e3b55780SDimitry Andric /// Returns the DbgOp associated with \p ID. Should only be used for IDs 469e3b55780SDimitry Andric /// returned from calling `insert` from this map or DbgOpID::UndefID. 470e3b55780SDimitry Andric DbgOp find(DbgOpID ID) const { 471e3b55780SDimitry Andric if (ID == DbgOpID::UndefID) 472e3b55780SDimitry Andric return DbgOp(); 473e3b55780SDimitry Andric if (ID.isConst()) 474e3b55780SDimitry Andric return DbgOp(ConstOps[ID.getIndex()]); 475e3b55780SDimitry Andric return DbgOp(ValueOps[ID.getIndex()]); 476e3b55780SDimitry Andric } 477e3b55780SDimitry Andric 478e3b55780SDimitry Andric void clear() { 479e3b55780SDimitry Andric ValueOps.clear(); 480e3b55780SDimitry Andric ConstOps.clear(); 481e3b55780SDimitry Andric ValueOpToID.clear(); 482e3b55780SDimitry Andric ConstOpToID.clear(); 483e3b55780SDimitry Andric } 484e3b55780SDimitry Andric 485e3b55780SDimitry Andric private: 486e3b55780SDimitry Andric DbgOpID insertConstOp(MachineOperand &MO) { 487e3b55780SDimitry Andric auto ExistingIt = ConstOpToID.find(MO); 488e3b55780SDimitry Andric if (ExistingIt != ConstOpToID.end()) 489e3b55780SDimitry Andric return ExistingIt->second; 490e3b55780SDimitry Andric DbgOpID ID(true, ConstOps.size()); 491e3b55780SDimitry Andric ConstOpToID.insert(std::make_pair(MO, ID)); 492e3b55780SDimitry Andric ConstOps.push_back(MO); 493e3b55780SDimitry Andric return ID; 494e3b55780SDimitry Andric } 495e3b55780SDimitry Andric DbgOpID insertValueOp(ValueIDNum VID) { 496e3b55780SDimitry Andric auto ExistingIt = ValueOpToID.find(VID); 497e3b55780SDimitry Andric if (ExistingIt != ValueOpToID.end()) 498e3b55780SDimitry Andric return ExistingIt->second; 499e3b55780SDimitry Andric DbgOpID ID(false, ValueOps.size()); 500e3b55780SDimitry Andric ValueOpToID.insert(std::make_pair(VID, ID)); 501e3b55780SDimitry Andric ValueOps.push_back(VID); 502e3b55780SDimitry Andric return ID; 503e3b55780SDimitry Andric } 504e3b55780SDimitry Andric }; 505e3b55780SDimitry Andric 506e3b55780SDimitry Andric // We set the maximum number of operands that we will handle to keep DbgValue 507e3b55780SDimitry Andric // within a reasonable size (64 bytes), as we store and pass a lot of them 508e3b55780SDimitry Andric // around. 509e3b55780SDimitry Andric #define MAX_DBG_OPS 8 510e3b55780SDimitry Andric 511e3b55780SDimitry Andric /// Class recording the (high level) _value_ of a variable. Identifies the value 512e3b55780SDimitry Andric /// of the variable as a list of ValueIDNums and constant MachineOperands, or as 513e3b55780SDimitry Andric /// an empty list for undef debug values or VPHI values which we have not found 514e3b55780SDimitry Andric /// valid locations for. 515c0981da4SDimitry Andric /// This class also stores meta-information about how the value is qualified. 516c0981da4SDimitry Andric /// Used to reason about variable values when performing the second 517c0981da4SDimitry Andric /// (DebugVariable specific) dataflow analysis. 518c0981da4SDimitry Andric class DbgValue { 519e3b55780SDimitry Andric private: 520e3b55780SDimitry Andric /// If Kind is Def or VPHI, the set of IDs corresponding to the DbgOps that 521e3b55780SDimitry Andric /// are used. VPHIs set every ID to EmptyID when we have not found a valid 522e3b55780SDimitry Andric /// machine-value for every operand, and sets them to the corresponding 523e3b55780SDimitry Andric /// machine-values when we have found all of them. 524e3b55780SDimitry Andric DbgOpID DbgOps[MAX_DBG_OPS]; 525e3b55780SDimitry Andric unsigned OpCount; 526e3b55780SDimitry Andric 527c0981da4SDimitry Andric public: 528c0981da4SDimitry Andric /// For a NoVal or VPHI DbgValue, which block it was generated in. 529c0981da4SDimitry Andric int BlockNo; 530c0981da4SDimitry Andric 531c0981da4SDimitry Andric /// Qualifiers for the ValueIDNum above. 532c0981da4SDimitry Andric DbgValueProperties Properties; 533c0981da4SDimitry Andric 534c0981da4SDimitry Andric typedef enum { 535c0981da4SDimitry Andric Undef, // Represents a DBG_VALUE $noreg in the transfer function only. 536e3b55780SDimitry Andric Def, // This value is defined by some combination of constants, 537e3b55780SDimitry Andric // instructions, or PHI values. 538c0981da4SDimitry Andric VPHI, // Incoming values to BlockNo differ, those values must be joined by 539c0981da4SDimitry Andric // a PHI in this block. 540c0981da4SDimitry Andric NoVal, // Empty DbgValue indicating an unknown value. Used as initializer, 541c0981da4SDimitry Andric // before dominating blocks values are propagated in. 542c0981da4SDimitry Andric } KindT; 543c0981da4SDimitry Andric /// Discriminator for whether this is a constant or an in-program value. 544c0981da4SDimitry Andric KindT Kind; 545c0981da4SDimitry Andric 546e3b55780SDimitry Andric DbgValue(ArrayRef<DbgOpID> DbgOps, const DbgValueProperties &Prop) 547e3b55780SDimitry Andric : OpCount(DbgOps.size()), BlockNo(0), Properties(Prop), Kind(Def) { 548e3b55780SDimitry Andric static_assert(sizeof(DbgValue) <= 64, 549e3b55780SDimitry Andric "DbgValue should fit within 64 bytes."); 550e3b55780SDimitry Andric assert(DbgOps.size() == Prop.getLocationOpCount()); 551e3b55780SDimitry Andric if (DbgOps.size() > MAX_DBG_OPS || 552e3b55780SDimitry Andric any_of(DbgOps, [](DbgOpID ID) { return ID.isUndef(); })) { 553e3b55780SDimitry Andric Kind = Undef; 554e3b55780SDimitry Andric OpCount = 0; 555e3b55780SDimitry Andric #define DEBUG_TYPE "LiveDebugValues" 556e3b55780SDimitry Andric if (DbgOps.size() > MAX_DBG_OPS) { 557e3b55780SDimitry Andric LLVM_DEBUG(dbgs() << "Found DbgValue with more than maximum allowed " 558e3b55780SDimitry Andric "operands.\n"); 559e3b55780SDimitry Andric } 560e3b55780SDimitry Andric #undef DEBUG_TYPE 561e3b55780SDimitry Andric } else { 562e3b55780SDimitry Andric for (unsigned Idx = 0; Idx < DbgOps.size(); ++Idx) 563e3b55780SDimitry Andric this->DbgOps[Idx] = DbgOps[Idx]; 564e3b55780SDimitry Andric } 565c0981da4SDimitry Andric } 566c0981da4SDimitry Andric 567c0981da4SDimitry Andric DbgValue(unsigned BlockNo, const DbgValueProperties &Prop, KindT Kind) 568e3b55780SDimitry Andric : OpCount(0), BlockNo(BlockNo), Properties(Prop), Kind(Kind) { 569c0981da4SDimitry Andric assert(Kind == NoVal || Kind == VPHI); 570c0981da4SDimitry Andric } 571c0981da4SDimitry Andric 572c0981da4SDimitry Andric DbgValue(const DbgValueProperties &Prop, KindT Kind) 573e3b55780SDimitry Andric : OpCount(0), BlockNo(0), Properties(Prop), Kind(Kind) { 574c0981da4SDimitry Andric assert(Kind == Undef && 575c0981da4SDimitry Andric "Empty DbgValue constructor must pass in Undef kind"); 576c0981da4SDimitry Andric } 577c0981da4SDimitry Andric 578c0981da4SDimitry Andric #ifndef NDEBUG 579e3b55780SDimitry Andric void dump(const MLocTracker *MTrack = nullptr, 580e3b55780SDimitry Andric const DbgOpIDMap *OpStore = nullptr) const; 581c0981da4SDimitry Andric #endif 582c0981da4SDimitry Andric 583c0981da4SDimitry Andric bool operator==(const DbgValue &Other) const { 584c0981da4SDimitry Andric if (std::tie(Kind, Properties) != std::tie(Other.Kind, Other.Properties)) 585c0981da4SDimitry Andric return false; 586e3b55780SDimitry Andric else if (Kind == Def && !equal(getDbgOpIDs(), Other.getDbgOpIDs())) 587c0981da4SDimitry Andric return false; 588c0981da4SDimitry Andric else if (Kind == NoVal && BlockNo != Other.BlockNo) 589c0981da4SDimitry Andric return false; 590c0981da4SDimitry Andric else if (Kind == VPHI && BlockNo != Other.BlockNo) 591c0981da4SDimitry Andric return false; 592e3b55780SDimitry Andric else if (Kind == VPHI && !equal(getDbgOpIDs(), Other.getDbgOpIDs())) 593c0981da4SDimitry Andric return false; 594c0981da4SDimitry Andric 595c0981da4SDimitry Andric return true; 596c0981da4SDimitry Andric } 597c0981da4SDimitry Andric 598c0981da4SDimitry Andric bool operator!=(const DbgValue &Other) const { return !(*this == Other); } 599e3b55780SDimitry Andric 600e3b55780SDimitry Andric // Returns an array of all the machine values used to calculate this variable 601e3b55780SDimitry Andric // value, or an empty list for an Undef or unjoined VPHI. 602e3b55780SDimitry Andric ArrayRef<DbgOpID> getDbgOpIDs() const { return {DbgOps, OpCount}; } 603e3b55780SDimitry Andric 604e3b55780SDimitry Andric // Returns either DbgOps[Index] if this DbgValue has Debug Operands, or 605e3b55780SDimitry Andric // the ID for ValueIDNum::EmptyValue otherwise (i.e. if this is an Undef, 606e3b55780SDimitry Andric // NoVal, or an unjoined VPHI). 607e3b55780SDimitry Andric DbgOpID getDbgOpID(unsigned Index) const { 608e3b55780SDimitry Andric if (!OpCount) 609e3b55780SDimitry Andric return DbgOpID::UndefID; 610e3b55780SDimitry Andric assert(Index < OpCount); 611e3b55780SDimitry Andric return DbgOps[Index]; 612e3b55780SDimitry Andric } 613e3b55780SDimitry Andric // Replaces this DbgValue's existing DbgOpIDs (if any) with the contents of 614e3b55780SDimitry Andric // \p NewIDs. The number of DbgOpIDs passed must be equal to the number of 615e3b55780SDimitry Andric // arguments expected by this DbgValue's properties (the return value of 616e3b55780SDimitry Andric // `getLocationOpCount()`). 617e3b55780SDimitry Andric void setDbgOpIDs(ArrayRef<DbgOpID> NewIDs) { 618e3b55780SDimitry Andric // We can go from no ops to some ops, but not from some ops to no ops. 619e3b55780SDimitry Andric assert(NewIDs.size() == getLocationOpCount() && 620e3b55780SDimitry Andric "Incorrect number of Debug Operands for this DbgValue."); 621e3b55780SDimitry Andric OpCount = NewIDs.size(); 622e3b55780SDimitry Andric for (unsigned Idx = 0; Idx < NewIDs.size(); ++Idx) 623e3b55780SDimitry Andric DbgOps[Idx] = NewIDs[Idx]; 624e3b55780SDimitry Andric } 625e3b55780SDimitry Andric 626e3b55780SDimitry Andric // The number of debug operands expected by this DbgValue's expression. 627e3b55780SDimitry Andric // getDbgOpIDs() should return an array of this length, unless this is an 628e3b55780SDimitry Andric // Undef or an unjoined VPHI. 629e3b55780SDimitry Andric unsigned getLocationOpCount() const { 630e3b55780SDimitry Andric return Properties.getLocationOpCount(); 631e3b55780SDimitry Andric } 632e3b55780SDimitry Andric 633e3b55780SDimitry Andric // Returns true if this or Other are unjoined PHIs, which do not have defined 634e3b55780SDimitry Andric // Loc Ops, or if the `n`th Loc Op for this has a different constness to the 635e3b55780SDimitry Andric // `n`th Loc Op for Other. 636e3b55780SDimitry Andric bool hasJoinableLocOps(const DbgValue &Other) const { 637e3b55780SDimitry Andric if (isUnjoinedPHI() || Other.isUnjoinedPHI()) 638e3b55780SDimitry Andric return true; 639e3b55780SDimitry Andric for (unsigned Idx = 0; Idx < getLocationOpCount(); ++Idx) { 640e3b55780SDimitry Andric if (getDbgOpID(Idx).isConst() != Other.getDbgOpID(Idx).isConst()) 641e3b55780SDimitry Andric return false; 642e3b55780SDimitry Andric } 643e3b55780SDimitry Andric return true; 644e3b55780SDimitry Andric } 645e3b55780SDimitry Andric 646e3b55780SDimitry Andric bool isUnjoinedPHI() const { return Kind == VPHI && OpCount == 0; } 647e3b55780SDimitry Andric 648e3b55780SDimitry Andric bool hasIdenticalValidLocOps(const DbgValue &Other) const { 649e3b55780SDimitry Andric if (!OpCount) 650e3b55780SDimitry Andric return false; 651e3b55780SDimitry Andric return equal(getDbgOpIDs(), Other.getDbgOpIDs()); 652e3b55780SDimitry Andric } 653c0981da4SDimitry Andric }; 654c0981da4SDimitry Andric 655c0981da4SDimitry Andric class LocIdxToIndexFunctor { 656c0981da4SDimitry Andric public: 657c0981da4SDimitry Andric using argument_type = LocIdx; 658c0981da4SDimitry Andric unsigned operator()(const LocIdx &L) const { return L.asU64(); } 659c0981da4SDimitry Andric }; 660c0981da4SDimitry Andric 661c0981da4SDimitry Andric /// Tracker for what values are in machine locations. Listens to the Things 662c0981da4SDimitry Andric /// being Done by various instructions, and maintains a table of what machine 663c0981da4SDimitry Andric /// locations have what values (as defined by a ValueIDNum). 664c0981da4SDimitry Andric /// 665c0981da4SDimitry Andric /// There are potentially a much larger number of machine locations on the 666c0981da4SDimitry Andric /// target machine than the actual working-set size of the function. On x86 for 667c0981da4SDimitry Andric /// example, we're extremely unlikely to want to track values through control 668c0981da4SDimitry Andric /// or debug registers. To avoid doing so, MLocTracker has several layers of 669c0981da4SDimitry Andric /// indirection going on, described below, to avoid unnecessarily tracking 670c0981da4SDimitry Andric /// any location. 671c0981da4SDimitry Andric /// 672c0981da4SDimitry Andric /// Here's a sort of diagram of the indexes, read from the bottom up: 673c0981da4SDimitry Andric /// 674c0981da4SDimitry Andric /// Size on stack Offset on stack 675c0981da4SDimitry Andric /// \ / 676c0981da4SDimitry Andric /// Stack Idx (Where in slot is this?) 677c0981da4SDimitry Andric /// / 678c0981da4SDimitry Andric /// / 679c0981da4SDimitry Andric /// Slot Num (%stack.0) / 680c0981da4SDimitry Andric /// FrameIdx => SpillNum / 681c0981da4SDimitry Andric /// \ / 682c0981da4SDimitry Andric /// SpillID (int) Register number (int) 683c0981da4SDimitry Andric /// \ / 684c0981da4SDimitry Andric /// LocationID => LocIdx 685c0981da4SDimitry Andric /// | 686c0981da4SDimitry Andric /// LocIdx => ValueIDNum 687c0981da4SDimitry Andric /// 688c0981da4SDimitry Andric /// The aim here is that the LocIdx => ValueIDNum vector is just an array of 689c0981da4SDimitry Andric /// values in numbered locations, so that later analyses can ignore whether the 690c0981da4SDimitry Andric /// location is a register or otherwise. To map a register / spill location to 691c0981da4SDimitry Andric /// a LocIdx, you have to use the (sparse) LocationID => LocIdx map. And to 692c0981da4SDimitry Andric /// build a LocationID for a stack slot, you need to combine identifiers for 693c0981da4SDimitry Andric /// which stack slot it is and where within that slot is being described. 694c0981da4SDimitry Andric /// 695c0981da4SDimitry Andric /// Register mask operands cause trouble by technically defining every register; 696c0981da4SDimitry Andric /// various hacks are used to avoid tracking registers that are never read and 697c0981da4SDimitry Andric /// only written by regmasks. 698c0981da4SDimitry Andric class MLocTracker { 699c0981da4SDimitry Andric public: 700c0981da4SDimitry Andric MachineFunction &MF; 701c0981da4SDimitry Andric const TargetInstrInfo &TII; 702c0981da4SDimitry Andric const TargetRegisterInfo &TRI; 703c0981da4SDimitry Andric const TargetLowering &TLI; 704c0981da4SDimitry Andric 705c0981da4SDimitry Andric /// IndexedMap type, mapping from LocIdx to ValueIDNum. 706c0981da4SDimitry Andric using LocToValueType = IndexedMap<ValueIDNum, LocIdxToIndexFunctor>; 707c0981da4SDimitry Andric 708c0981da4SDimitry Andric /// Map of LocIdxes to the ValueIDNums that they store. This is tightly 709c0981da4SDimitry Andric /// packed, entries only exist for locations that are being tracked. 710c0981da4SDimitry Andric LocToValueType LocIdxToIDNum; 711c0981da4SDimitry Andric 712c0981da4SDimitry Andric /// "Map" of machine location IDs (i.e., raw register or spill number) to the 713c0981da4SDimitry Andric /// LocIdx key / number for that location. There are always at least as many 714c0981da4SDimitry Andric /// as the number of registers on the target -- if the value in the register 715c0981da4SDimitry Andric /// is not being tracked, then the LocIdx value will be zero. New entries are 716c0981da4SDimitry Andric /// appended if a new spill slot begins being tracked. 717c0981da4SDimitry Andric /// This, and the corresponding reverse map persist for the analysis of the 718c0981da4SDimitry Andric /// whole function, and is necessarying for decoding various vectors of 719c0981da4SDimitry Andric /// values. 720c0981da4SDimitry Andric std::vector<LocIdx> LocIDToLocIdx; 721c0981da4SDimitry Andric 722c0981da4SDimitry Andric /// Inverse map of LocIDToLocIdx. 723c0981da4SDimitry Andric IndexedMap<unsigned, LocIdxToIndexFunctor> LocIdxToLocID; 724c0981da4SDimitry Andric 725c0981da4SDimitry Andric /// When clobbering register masks, we chose to not believe the machine model 726c0981da4SDimitry Andric /// and don't clobber SP. Do the same for SP aliases, and for efficiency, 727c0981da4SDimitry Andric /// keep a set of them here. 728c0981da4SDimitry Andric SmallSet<Register, 8> SPAliases; 729c0981da4SDimitry Andric 730c0981da4SDimitry Andric /// Unique-ification of spill. Used to number them -- their LocID number is 731c0981da4SDimitry Andric /// the index in SpillLocs minus one plus NumRegs. 732c0981da4SDimitry Andric UniqueVector<SpillLoc> SpillLocs; 733c0981da4SDimitry Andric 734c0981da4SDimitry Andric // If we discover a new machine location, assign it an mphi with this 735c0981da4SDimitry Andric // block number. 7367fa27ce4SDimitry Andric unsigned CurBB = -1; 737c0981da4SDimitry Andric 738c0981da4SDimitry Andric /// Cached local copy of the number of registers the target has. 739c0981da4SDimitry Andric unsigned NumRegs; 740c0981da4SDimitry Andric 741c0981da4SDimitry Andric /// Number of slot indexes the target has -- distinct segments of a stack 742c0981da4SDimitry Andric /// slot that can take on the value of a subregister, when a super-register 743c0981da4SDimitry Andric /// is written to the stack. 744c0981da4SDimitry Andric unsigned NumSlotIdxes; 745c0981da4SDimitry Andric 746c0981da4SDimitry Andric /// Collection of register mask operands that have been observed. Second part 747c0981da4SDimitry Andric /// of pair indicates the instruction that they happened in. Used to 748c0981da4SDimitry Andric /// reconstruct where defs happened if we start tracking a location later 749c0981da4SDimitry Andric /// on. 750c0981da4SDimitry Andric SmallVector<std::pair<const MachineOperand *, unsigned>, 32> Masks; 751c0981da4SDimitry Andric 752c0981da4SDimitry Andric /// Pair for describing a position within a stack slot -- first the size in 753c0981da4SDimitry Andric /// bits, then the offset. 754c0981da4SDimitry Andric typedef std::pair<unsigned short, unsigned short> StackSlotPos; 755c0981da4SDimitry Andric 756c0981da4SDimitry Andric /// Map from a size/offset pair describing a position in a stack slot, to a 757c0981da4SDimitry Andric /// numeric identifier for that position. Allows easier identification of 758c0981da4SDimitry Andric /// individual positions. 759c0981da4SDimitry Andric DenseMap<StackSlotPos, unsigned> StackSlotIdxes; 760c0981da4SDimitry Andric 761c0981da4SDimitry Andric /// Inverse of StackSlotIdxes. 762c0981da4SDimitry Andric DenseMap<unsigned, StackSlotPos> StackIdxesToPos; 763c0981da4SDimitry Andric 764c0981da4SDimitry Andric /// Iterator for locations and the values they contain. Dereferencing 765c0981da4SDimitry Andric /// produces a struct/pair containing the LocIdx key for this location, 766c0981da4SDimitry Andric /// and a reference to the value currently stored. Simplifies the process 767c0981da4SDimitry Andric /// of seeking a particular location. 768c0981da4SDimitry Andric class MLocIterator { 769c0981da4SDimitry Andric LocToValueType &ValueMap; 770c0981da4SDimitry Andric LocIdx Idx; 771c0981da4SDimitry Andric 772c0981da4SDimitry Andric public: 773c0981da4SDimitry Andric class value_type { 774c0981da4SDimitry Andric public: 775c0981da4SDimitry Andric value_type(LocIdx Idx, ValueIDNum &Value) : Idx(Idx), Value(Value) {} 776c0981da4SDimitry Andric const LocIdx Idx; /// Read-only index of this location. 777c0981da4SDimitry Andric ValueIDNum &Value; /// Reference to the stored value at this location. 778c0981da4SDimitry Andric }; 779c0981da4SDimitry Andric 780c0981da4SDimitry Andric MLocIterator(LocToValueType &ValueMap, LocIdx Idx) 781c0981da4SDimitry Andric : ValueMap(ValueMap), Idx(Idx) {} 782c0981da4SDimitry Andric 783c0981da4SDimitry Andric bool operator==(const MLocIterator &Other) const { 784c0981da4SDimitry Andric assert(&ValueMap == &Other.ValueMap); 785c0981da4SDimitry Andric return Idx == Other.Idx; 786c0981da4SDimitry Andric } 787c0981da4SDimitry Andric 788c0981da4SDimitry Andric bool operator!=(const MLocIterator &Other) const { 789c0981da4SDimitry Andric return !(*this == Other); 790c0981da4SDimitry Andric } 791c0981da4SDimitry Andric 792c0981da4SDimitry Andric void operator++() { Idx = LocIdx(Idx.asU64() + 1); } 793c0981da4SDimitry Andric 794c0981da4SDimitry Andric value_type operator*() { return value_type(Idx, ValueMap[LocIdx(Idx)]); } 795c0981da4SDimitry Andric }; 796c0981da4SDimitry Andric 797c0981da4SDimitry Andric MLocTracker(MachineFunction &MF, const TargetInstrInfo &TII, 798c0981da4SDimitry Andric const TargetRegisterInfo &TRI, const TargetLowering &TLI); 799c0981da4SDimitry Andric 800c0981da4SDimitry Andric /// Produce location ID number for a Register. Provides some small amount of 801c0981da4SDimitry Andric /// type safety. 802c0981da4SDimitry Andric /// \param Reg The register we're looking up. 803c0981da4SDimitry Andric unsigned getLocID(Register Reg) { return Reg.id(); } 804c0981da4SDimitry Andric 805c0981da4SDimitry Andric /// Produce location ID number for a spill position. 806c0981da4SDimitry Andric /// \param Spill The number of the spill we're fetching the location for. 807c0981da4SDimitry Andric /// \param SpillSubReg Subregister within the spill we're addressing. 808c0981da4SDimitry Andric unsigned getLocID(SpillLocationNo Spill, unsigned SpillSubReg) { 809c0981da4SDimitry Andric unsigned short Size = TRI.getSubRegIdxSize(SpillSubReg); 810c0981da4SDimitry Andric unsigned short Offs = TRI.getSubRegIdxOffset(SpillSubReg); 811c0981da4SDimitry Andric return getLocID(Spill, {Size, Offs}); 812c0981da4SDimitry Andric } 813c0981da4SDimitry Andric 814c0981da4SDimitry Andric /// Produce location ID number for a spill position. 815c0981da4SDimitry Andric /// \param Spill The number of the spill we're fetching the location for. 816c0981da4SDimitry Andric /// \apram SpillIdx size/offset within the spill slot to be addressed. 817c0981da4SDimitry Andric unsigned getLocID(SpillLocationNo Spill, StackSlotPos Idx) { 818c0981da4SDimitry Andric unsigned SlotNo = Spill.id() - 1; 819c0981da4SDimitry Andric SlotNo *= NumSlotIdxes; 8207fa27ce4SDimitry Andric assert(StackSlotIdxes.contains(Idx)); 821c0981da4SDimitry Andric SlotNo += StackSlotIdxes[Idx]; 822c0981da4SDimitry Andric SlotNo += NumRegs; 823c0981da4SDimitry Andric return SlotNo; 824c0981da4SDimitry Andric } 825c0981da4SDimitry Andric 826c0981da4SDimitry Andric /// Given a spill number, and a slot within the spill, calculate the ID number 827c0981da4SDimitry Andric /// for that location. 828c0981da4SDimitry Andric unsigned getSpillIDWithIdx(SpillLocationNo Spill, unsigned Idx) { 829c0981da4SDimitry Andric unsigned SlotNo = Spill.id() - 1; 830c0981da4SDimitry Andric SlotNo *= NumSlotIdxes; 831c0981da4SDimitry Andric SlotNo += Idx; 832c0981da4SDimitry Andric SlotNo += NumRegs; 833c0981da4SDimitry Andric return SlotNo; 834c0981da4SDimitry Andric } 835c0981da4SDimitry Andric 836c0981da4SDimitry Andric /// Return the spill number that a location ID corresponds to. 837c0981da4SDimitry Andric SpillLocationNo locIDToSpill(unsigned ID) const { 838c0981da4SDimitry Andric assert(ID >= NumRegs); 839c0981da4SDimitry Andric ID -= NumRegs; 840c0981da4SDimitry Andric // Truncate away the index part, leaving only the spill number. 841c0981da4SDimitry Andric ID /= NumSlotIdxes; 842c0981da4SDimitry Andric return SpillLocationNo(ID + 1); // The UniqueVector is one-based. 843c0981da4SDimitry Andric } 844c0981da4SDimitry Andric 845c0981da4SDimitry Andric /// Returns the spill-slot size/offs that a location ID corresponds to. 846c0981da4SDimitry Andric StackSlotPos locIDToSpillIdx(unsigned ID) const { 847c0981da4SDimitry Andric assert(ID >= NumRegs); 848c0981da4SDimitry Andric ID -= NumRegs; 849c0981da4SDimitry Andric unsigned Idx = ID % NumSlotIdxes; 850c0981da4SDimitry Andric return StackIdxesToPos.find(Idx)->second; 851c0981da4SDimitry Andric } 852c0981da4SDimitry Andric 8536f8fc217SDimitry Andric unsigned getNumLocs() const { return LocIdxToIDNum.size(); } 854c0981da4SDimitry Andric 855c0981da4SDimitry Andric /// Reset all locations to contain a PHI value at the designated block. Used 856c0981da4SDimitry Andric /// sometimes for actual PHI values, othertimes to indicate the block entry 857c0981da4SDimitry Andric /// value (before any more information is known). 858c0981da4SDimitry Andric void setMPhis(unsigned NewCurBB) { 859c0981da4SDimitry Andric CurBB = NewCurBB; 860c0981da4SDimitry Andric for (auto Location : locations()) 861c0981da4SDimitry Andric Location.Value = {CurBB, 0, Location.Idx}; 862c0981da4SDimitry Andric } 863c0981da4SDimitry Andric 864c0981da4SDimitry Andric /// Load values for each location from array of ValueIDNums. Take current 865c0981da4SDimitry Andric /// bbnum just in case we read a value from a hitherto untouched register. 866145449b1SDimitry Andric void loadFromArray(ValueTable &Locs, unsigned NewCurBB) { 867c0981da4SDimitry Andric CurBB = NewCurBB; 868c0981da4SDimitry Andric // Iterate over all tracked locations, and load each locations live-in 869c0981da4SDimitry Andric // value into our local index. 870c0981da4SDimitry Andric for (auto Location : locations()) 871c0981da4SDimitry Andric Location.Value = Locs[Location.Idx.asU64()]; 872c0981da4SDimitry Andric } 873c0981da4SDimitry Andric 874c0981da4SDimitry Andric /// Wipe any un-necessary location records after traversing a block. 8756f8fc217SDimitry Andric void reset() { 876c0981da4SDimitry Andric // We could reset all the location values too; however either loadFromArray 877c0981da4SDimitry Andric // or setMPhis should be called before this object is re-used. Just 878c0981da4SDimitry Andric // clear Masks, they're definitely not needed. 879c0981da4SDimitry Andric Masks.clear(); 880c0981da4SDimitry Andric } 881c0981da4SDimitry Andric 882c0981da4SDimitry Andric /// Clear all data. Destroys the LocID <=> LocIdx map, which makes most of 883c0981da4SDimitry Andric /// the information in this pass uninterpretable. 8846f8fc217SDimitry Andric void clear() { 885c0981da4SDimitry Andric reset(); 886c0981da4SDimitry Andric LocIDToLocIdx.clear(); 887c0981da4SDimitry Andric LocIdxToLocID.clear(); 888c0981da4SDimitry Andric LocIdxToIDNum.clear(); 889c0981da4SDimitry Andric // SpillLocs.reset(); XXX UniqueVector::reset assumes a SpillLoc casts from 890c0981da4SDimitry Andric // 0 891c0981da4SDimitry Andric SpillLocs = decltype(SpillLocs)(); 892c0981da4SDimitry Andric StackSlotIdxes.clear(); 893c0981da4SDimitry Andric StackIdxesToPos.clear(); 894c0981da4SDimitry Andric 895c0981da4SDimitry Andric LocIDToLocIdx.resize(NumRegs, LocIdx::MakeIllegalLoc()); 896c0981da4SDimitry Andric } 897c0981da4SDimitry Andric 898c0981da4SDimitry Andric /// Set a locaiton to a certain value. 899c0981da4SDimitry Andric void setMLoc(LocIdx L, ValueIDNum Num) { 900c0981da4SDimitry Andric assert(L.asU64() < LocIdxToIDNum.size()); 901c0981da4SDimitry Andric LocIdxToIDNum[L] = Num; 902c0981da4SDimitry Andric } 903c0981da4SDimitry Andric 904c0981da4SDimitry Andric /// Read the value of a particular location 905c0981da4SDimitry Andric ValueIDNum readMLoc(LocIdx L) { 906c0981da4SDimitry Andric assert(L.asU64() < LocIdxToIDNum.size()); 907c0981da4SDimitry Andric return LocIdxToIDNum[L]; 908c0981da4SDimitry Andric } 909c0981da4SDimitry Andric 910c0981da4SDimitry Andric /// Create a LocIdx for an untracked register ID. Initialize it to either an 911c0981da4SDimitry Andric /// mphi value representing a live-in, or a recent register mask clobber. 912c0981da4SDimitry Andric LocIdx trackRegister(unsigned ID); 913c0981da4SDimitry Andric 914c0981da4SDimitry Andric LocIdx lookupOrTrackRegister(unsigned ID) { 915c0981da4SDimitry Andric LocIdx &Index = LocIDToLocIdx[ID]; 916c0981da4SDimitry Andric if (Index.isIllegal()) 917c0981da4SDimitry Andric Index = trackRegister(ID); 918c0981da4SDimitry Andric return Index; 919c0981da4SDimitry Andric } 920c0981da4SDimitry Andric 921c0981da4SDimitry Andric /// Is register R currently tracked by MLocTracker? 922c0981da4SDimitry Andric bool isRegisterTracked(Register R) { 923c0981da4SDimitry Andric LocIdx &Index = LocIDToLocIdx[R]; 924c0981da4SDimitry Andric return !Index.isIllegal(); 925c0981da4SDimitry Andric } 926c0981da4SDimitry Andric 927c0981da4SDimitry Andric /// Record a definition of the specified register at the given block / inst. 928c0981da4SDimitry Andric /// This doesn't take a ValueIDNum, because the definition and its location 929c0981da4SDimitry Andric /// are synonymous. 930c0981da4SDimitry Andric void defReg(Register R, unsigned BB, unsigned Inst) { 931c0981da4SDimitry Andric unsigned ID = getLocID(R); 932c0981da4SDimitry Andric LocIdx Idx = lookupOrTrackRegister(ID); 933c0981da4SDimitry Andric ValueIDNum ValueID = {BB, Inst, Idx}; 934c0981da4SDimitry Andric LocIdxToIDNum[Idx] = ValueID; 935c0981da4SDimitry Andric } 936c0981da4SDimitry Andric 937c0981da4SDimitry Andric /// Set a register to a value number. To be used if the value number is 938c0981da4SDimitry Andric /// known in advance. 939c0981da4SDimitry Andric void setReg(Register R, ValueIDNum ValueID) { 940c0981da4SDimitry Andric unsigned ID = getLocID(R); 941c0981da4SDimitry Andric LocIdx Idx = lookupOrTrackRegister(ID); 942c0981da4SDimitry Andric LocIdxToIDNum[Idx] = ValueID; 943c0981da4SDimitry Andric } 944c0981da4SDimitry Andric 945c0981da4SDimitry Andric ValueIDNum readReg(Register R) { 946c0981da4SDimitry Andric unsigned ID = getLocID(R); 947c0981da4SDimitry Andric LocIdx Idx = lookupOrTrackRegister(ID); 948c0981da4SDimitry Andric return LocIdxToIDNum[Idx]; 949c0981da4SDimitry Andric } 950c0981da4SDimitry Andric 951c0981da4SDimitry Andric /// Reset a register value to zero / empty. Needed to replicate the 952c0981da4SDimitry Andric /// VarLoc implementation where a copy to/from a register effectively 953c0981da4SDimitry Andric /// clears the contents of the source register. (Values can only have one 954c0981da4SDimitry Andric /// machine location in VarLocBasedImpl). 955c0981da4SDimitry Andric void wipeRegister(Register R) { 956c0981da4SDimitry Andric unsigned ID = getLocID(R); 957c0981da4SDimitry Andric LocIdx Idx = LocIDToLocIdx[ID]; 958c0981da4SDimitry Andric LocIdxToIDNum[Idx] = ValueIDNum::EmptyValue; 959c0981da4SDimitry Andric } 960c0981da4SDimitry Andric 961c0981da4SDimitry Andric /// Determine the LocIdx of an existing register. 962c0981da4SDimitry Andric LocIdx getRegMLoc(Register R) { 963c0981da4SDimitry Andric unsigned ID = getLocID(R); 964c0981da4SDimitry Andric assert(ID < LocIDToLocIdx.size()); 9654df029ccSDimitry Andric assert(LocIDToLocIdx[ID] != UINT_MAX); // Sentinel for IndexedMap. 966c0981da4SDimitry Andric return LocIDToLocIdx[ID]; 967c0981da4SDimitry Andric } 968c0981da4SDimitry Andric 969c0981da4SDimitry Andric /// Record a RegMask operand being executed. Defs any register we currently 970c0981da4SDimitry Andric /// track, stores a pointer to the mask in case we have to account for it 971c0981da4SDimitry Andric /// later. 972c0981da4SDimitry Andric void writeRegMask(const MachineOperand *MO, unsigned CurBB, unsigned InstID); 973c0981da4SDimitry Andric 974c0981da4SDimitry Andric /// Find LocIdx for SpillLoc \p L, creating a new one if it's not tracked. 975e3b55780SDimitry Andric /// Returns std::nullopt when in scenarios where a spill slot could be 976e3b55780SDimitry Andric /// tracked, but we would likely run into resource limitations. 977e3b55780SDimitry Andric std::optional<SpillLocationNo> getOrTrackSpillLoc(SpillLoc L); 978c0981da4SDimitry Andric 979c0981da4SDimitry Andric // Get LocIdx of a spill ID. 980c0981da4SDimitry Andric LocIdx getSpillMLoc(unsigned SpillID) { 9814df029ccSDimitry Andric assert(LocIDToLocIdx[SpillID] != UINT_MAX); // Sentinel for IndexedMap. 982c0981da4SDimitry Andric return LocIDToLocIdx[SpillID]; 983c0981da4SDimitry Andric } 984c0981da4SDimitry Andric 985c0981da4SDimitry Andric /// Return true if Idx is a spill machine location. 986c0981da4SDimitry Andric bool isSpill(LocIdx Idx) const { return LocIdxToLocID[Idx] >= NumRegs; } 987c0981da4SDimitry Andric 988145449b1SDimitry Andric /// How large is this location (aka, how wide is a value defined there?). 989145449b1SDimitry Andric unsigned getLocSizeInBits(LocIdx L) const { 990145449b1SDimitry Andric unsigned ID = LocIdxToLocID[L]; 991145449b1SDimitry Andric if (!isSpill(L)) { 992145449b1SDimitry Andric return TRI.getRegSizeInBits(Register(ID), MF.getRegInfo()); 993145449b1SDimitry Andric } else { 994145449b1SDimitry Andric // The slot location on the stack is uninteresting, we care about the 995145449b1SDimitry Andric // position of the value within the slot (which comes with a size). 996145449b1SDimitry Andric StackSlotPos Pos = locIDToSpillIdx(ID); 997145449b1SDimitry Andric return Pos.first; 998145449b1SDimitry Andric } 999145449b1SDimitry Andric } 1000145449b1SDimitry Andric 1001c0981da4SDimitry Andric MLocIterator begin() { return MLocIterator(LocIdxToIDNum, 0); } 1002c0981da4SDimitry Andric 1003c0981da4SDimitry Andric MLocIterator end() { 1004c0981da4SDimitry Andric return MLocIterator(LocIdxToIDNum, LocIdxToIDNum.size()); 1005c0981da4SDimitry Andric } 1006c0981da4SDimitry Andric 1007c0981da4SDimitry Andric /// Return a range over all locations currently tracked. 1008c0981da4SDimitry Andric iterator_range<MLocIterator> locations() { 1009c0981da4SDimitry Andric return llvm::make_range(begin(), end()); 1010c0981da4SDimitry Andric } 1011c0981da4SDimitry Andric 1012c0981da4SDimitry Andric std::string LocIdxToName(LocIdx Idx) const; 1013c0981da4SDimitry Andric 1014c0981da4SDimitry Andric std::string IDAsString(const ValueIDNum &Num) const; 1015c0981da4SDimitry Andric 1016c0981da4SDimitry Andric #ifndef NDEBUG 1017c0981da4SDimitry Andric LLVM_DUMP_METHOD void dump(); 1018c0981da4SDimitry Andric 1019c0981da4SDimitry Andric LLVM_DUMP_METHOD void dump_mloc_map(); 1020c0981da4SDimitry Andric #endif 1021c0981da4SDimitry Andric 1022e3b55780SDimitry Andric /// Create a DBG_VALUE based on debug operands \p DbgOps. Qualify it with the 1023c0981da4SDimitry Andric /// information in \pProperties, for variable Var. Don't insert it anywhere, 1024c0981da4SDimitry Andric /// just return the builder for it. 1025e3b55780SDimitry Andric MachineInstrBuilder emitLoc(const SmallVectorImpl<ResolvedDbgOp> &DbgOps, 1026ac9a064cSDimitry Andric const DebugVariable &Var, const DILocation *DILoc, 1027c0981da4SDimitry Andric const DbgValueProperties &Properties); 1028c0981da4SDimitry Andric }; 1029c0981da4SDimitry Andric 1030f65dcba8SDimitry Andric /// Types for recording sets of variable fragments that overlap. For a given 1031f65dcba8SDimitry Andric /// local variable, we record all other fragments of that variable that could 1032f65dcba8SDimitry Andric /// overlap it, to reduce search time. 1033f65dcba8SDimitry Andric using FragmentOfVar = 1034f65dcba8SDimitry Andric std::pair<const DILocalVariable *, DIExpression::FragmentInfo>; 1035f65dcba8SDimitry Andric using OverlapMap = 1036f65dcba8SDimitry Andric DenseMap<FragmentOfVar, SmallVector<DIExpression::FragmentInfo, 1>>; 1037f65dcba8SDimitry Andric 1038c0981da4SDimitry Andric /// Collection of DBG_VALUEs observed when traversing a block. Records each 1039c0981da4SDimitry Andric /// variable and the value the DBG_VALUE refers to. Requires the machine value 1040c0981da4SDimitry Andric /// location dataflow algorithm to have run already, so that values can be 1041c0981da4SDimitry Andric /// identified. 1042c0981da4SDimitry Andric class VLocTracker { 1043c0981da4SDimitry Andric public: 1044ac9a064cSDimitry Andric /// Ref to function-wide map of DebugVariable <=> ID-numbers. 1045ac9a064cSDimitry Andric DebugVariableMap &DVMap; 1046c0981da4SDimitry Andric /// Map DebugVariable to the latest Value it's defined to have. 1047c0981da4SDimitry Andric /// Needs to be a MapVector because we determine order-in-the-input-MIR from 1048ac9a064cSDimitry Andric /// the order in this container. (FIXME: likely no longer true as the ordering 1049ac9a064cSDimitry Andric /// is now provided by DebugVariableMap). 1050c0981da4SDimitry Andric /// We only retain the last DbgValue in each block for each variable, to 1051c0981da4SDimitry Andric /// determine the blocks live-out variable value. The Vars container forms the 1052c0981da4SDimitry Andric /// transfer function for this block, as part of the dataflow analysis. The 1053c0981da4SDimitry Andric /// movement of values between locations inside of a block is handled at a 1054c0981da4SDimitry Andric /// much later stage, in the TransferTracker class. 1055ac9a064cSDimitry Andric MapVector<DebugVariableID, DbgValue> Vars; 1056ac9a064cSDimitry Andric SmallDenseMap<DebugVariableID, const DILocation *, 8> Scopes; 1057c0981da4SDimitry Andric MachineBasicBlock *MBB = nullptr; 1058f65dcba8SDimitry Andric const OverlapMap &OverlappingFragments; 1059f65dcba8SDimitry Andric DbgValueProperties EmptyProperties; 1060c0981da4SDimitry Andric 1061c0981da4SDimitry Andric public: 1062ac9a064cSDimitry Andric VLocTracker(DebugVariableMap &DVMap, const OverlapMap &O, 1063ac9a064cSDimitry Andric const DIExpression *EmptyExpr) 1064ac9a064cSDimitry Andric : DVMap(DVMap), OverlappingFragments(O), 1065ac9a064cSDimitry Andric EmptyProperties(EmptyExpr, false, false) {} 1066c0981da4SDimitry Andric 1067c0981da4SDimitry Andric void defVar(const MachineInstr &MI, const DbgValueProperties &Properties, 1068e3b55780SDimitry Andric const SmallVectorImpl<DbgOpID> &DebugOps) { 1069e3b55780SDimitry Andric assert(MI.isDebugValueLike()); 1070c0981da4SDimitry Andric DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(), 1071c0981da4SDimitry Andric MI.getDebugLoc()->getInlinedAt()); 1072ac9a064cSDimitry Andric // Either insert or fetch an ID number for this variable. 1073ac9a064cSDimitry Andric DebugVariableID VarID = DVMap.insertDVID(Var, MI.getDebugLoc().get()); 1074e3b55780SDimitry Andric DbgValue Rec = (DebugOps.size() > 0) 1075e3b55780SDimitry Andric ? DbgValue(DebugOps, Properties) 1076c0981da4SDimitry Andric : DbgValue(Properties, DbgValue::Undef); 1077c0981da4SDimitry Andric 1078c0981da4SDimitry Andric // Attempt insertion; overwrite if it's already mapped. 1079ac9a064cSDimitry Andric auto Result = Vars.insert(std::make_pair(VarID, Rec)); 1080c0981da4SDimitry Andric if (!Result.second) 1081c0981da4SDimitry Andric Result.first->second = Rec; 1082ac9a064cSDimitry Andric Scopes[VarID] = MI.getDebugLoc().get(); 1083f65dcba8SDimitry Andric 1084f65dcba8SDimitry Andric considerOverlaps(Var, MI.getDebugLoc().get()); 1085c0981da4SDimitry Andric } 1086c0981da4SDimitry Andric 1087f65dcba8SDimitry Andric void considerOverlaps(const DebugVariable &Var, const DILocation *Loc) { 1088f65dcba8SDimitry Andric auto Overlaps = OverlappingFragments.find( 1089f65dcba8SDimitry Andric {Var.getVariable(), Var.getFragmentOrDefault()}); 1090f65dcba8SDimitry Andric if (Overlaps == OverlappingFragments.end()) 1091f65dcba8SDimitry Andric return; 1092f65dcba8SDimitry Andric 1093f65dcba8SDimitry Andric // Otherwise: terminate any overlapped variable locations. 1094f65dcba8SDimitry Andric for (auto FragmentInfo : Overlaps->second) { 1095f65dcba8SDimitry Andric // The "empty" fragment is stored as DebugVariable::DefaultFragment, so 1096f65dcba8SDimitry Andric // that it overlaps with everything, however its cannonical representation 1097f65dcba8SDimitry Andric // in a DebugVariable is as "None". 1098e3b55780SDimitry Andric std::optional<DIExpression::FragmentInfo> OptFragmentInfo = FragmentInfo; 1099f65dcba8SDimitry Andric if (DebugVariable::isDefaultFragment(FragmentInfo)) 1100e3b55780SDimitry Andric OptFragmentInfo = std::nullopt; 1101f65dcba8SDimitry Andric 1102f65dcba8SDimitry Andric DebugVariable Overlapped(Var.getVariable(), OptFragmentInfo, 1103f65dcba8SDimitry Andric Var.getInlinedAt()); 1104ac9a064cSDimitry Andric // Produce an ID number for this overlapping fragment of a variable. 1105ac9a064cSDimitry Andric DebugVariableID OverlappedID = DVMap.insertDVID(Overlapped, Loc); 1106f65dcba8SDimitry Andric DbgValue Rec = DbgValue(EmptyProperties, DbgValue::Undef); 1107f65dcba8SDimitry Andric 1108f65dcba8SDimitry Andric // Attempt insertion; overwrite if it's already mapped. 1109ac9a064cSDimitry Andric auto Result = Vars.insert(std::make_pair(OverlappedID, Rec)); 1110f65dcba8SDimitry Andric if (!Result.second) 1111f65dcba8SDimitry Andric Result.first->second = Rec; 1112ac9a064cSDimitry Andric Scopes[OverlappedID] = Loc; 1113f65dcba8SDimitry Andric } 1114c0981da4SDimitry Andric } 1115145449b1SDimitry Andric 1116145449b1SDimitry Andric void clear() { 1117145449b1SDimitry Andric Vars.clear(); 1118145449b1SDimitry Andric Scopes.clear(); 1119145449b1SDimitry Andric } 1120c0981da4SDimitry Andric }; 1121c0981da4SDimitry Andric 1122c0981da4SDimitry Andric // XXX XXX docs 1123c0981da4SDimitry Andric class InstrRefBasedLDV : public LDVImpl { 1124c0981da4SDimitry Andric public: 1125c0981da4SDimitry Andric friend class ::InstrRefLDVTest; 1126c0981da4SDimitry Andric 1127c0981da4SDimitry Andric using FragmentInfo = DIExpression::FragmentInfo; 1128e3b55780SDimitry Andric using OptFragmentInfo = std::optional<DIExpression::FragmentInfo>; 1129c0981da4SDimitry Andric 1130c0981da4SDimitry Andric // Helper while building OverlapMap, a map of all fragments seen for a given 1131c0981da4SDimitry Andric // DILocalVariable. 1132c0981da4SDimitry Andric using VarToFragments = 1133c0981da4SDimitry Andric DenseMap<const DILocalVariable *, SmallSet<FragmentInfo, 4>>; 1134c0981da4SDimitry Andric 1135c0981da4SDimitry Andric /// Machine location/value transfer function, a mapping of which locations 1136c0981da4SDimitry Andric /// are assigned which new values. 1137c0981da4SDimitry Andric using MLocTransferMap = SmallDenseMap<LocIdx, ValueIDNum>; 1138c0981da4SDimitry Andric 1139c0981da4SDimitry Andric /// Live in/out structure for the variable values: a per-block map of 1140c0981da4SDimitry Andric /// variables to their values. 1141c0981da4SDimitry Andric using LiveIdxT = DenseMap<const MachineBasicBlock *, DbgValue *>; 1142c0981da4SDimitry Andric 1143ac9a064cSDimitry Andric using VarAndLoc = std::pair<DebugVariableID, DbgValue>; 1144c0981da4SDimitry Andric 1145c0981da4SDimitry Andric /// Type for a live-in value: the predecessor block, and its value. 1146c0981da4SDimitry Andric using InValueT = std::pair<MachineBasicBlock *, DbgValue *>; 1147c0981da4SDimitry Andric 1148c0981da4SDimitry Andric /// Vector (per block) of a collection (inner smallvector) of live-ins. 1149c0981da4SDimitry Andric /// Used as the result type for the variable value dataflow problem. 1150c0981da4SDimitry Andric using LiveInsT = SmallVector<SmallVector<VarAndLoc, 8>, 8>; 1151c0981da4SDimitry Andric 1152ecbca9f5SDimitry Andric /// Mapping from lexical scopes to a DILocation in that scope. 1153ecbca9f5SDimitry Andric using ScopeToDILocT = DenseMap<const LexicalScope *, const DILocation *>; 1154ecbca9f5SDimitry Andric 1155ecbca9f5SDimitry Andric /// Mapping from lexical scopes to variables in that scope. 1156ac9a064cSDimitry Andric using ScopeToVarsT = 1157ac9a064cSDimitry Andric DenseMap<const LexicalScope *, SmallSet<DebugVariableID, 4>>; 1158ecbca9f5SDimitry Andric 1159ecbca9f5SDimitry Andric /// Mapping from lexical scopes to blocks where variables in that scope are 1160ecbca9f5SDimitry Andric /// assigned. Such blocks aren't necessarily "in" the lexical scope, it's 1161ecbca9f5SDimitry Andric /// just a block where an assignment happens. 1162ecbca9f5SDimitry Andric using ScopeToAssignBlocksT = DenseMap<const LexicalScope *, SmallPtrSet<MachineBasicBlock *, 4>>; 1163ecbca9f5SDimitry Andric 1164c0981da4SDimitry Andric private: 1165c0981da4SDimitry Andric MachineDominatorTree *DomTree; 1166c0981da4SDimitry Andric const TargetRegisterInfo *TRI; 1167c0981da4SDimitry Andric const MachineRegisterInfo *MRI; 1168c0981da4SDimitry Andric const TargetInstrInfo *TII; 1169c0981da4SDimitry Andric const TargetFrameLowering *TFI; 1170c0981da4SDimitry Andric const MachineFrameInfo *MFI; 1171c0981da4SDimitry Andric BitVector CalleeSavedRegs; 1172c0981da4SDimitry Andric LexicalScopes LS; 1173c0981da4SDimitry Andric TargetPassConfig *TPC; 1174c0981da4SDimitry Andric 1175c0981da4SDimitry Andric // An empty DIExpression. Used default / placeholder DbgValueProperties 1176c0981da4SDimitry Andric // objects, as we can't have null expressions. 1177c0981da4SDimitry Andric const DIExpression *EmptyExpr; 1178c0981da4SDimitry Andric 1179c0981da4SDimitry Andric /// Object to track machine locations as we step through a block. Could 1180c0981da4SDimitry Andric /// probably be a field rather than a pointer, as it's always used. 1181c0981da4SDimitry Andric MLocTracker *MTracker = nullptr; 1182c0981da4SDimitry Andric 1183c0981da4SDimitry Andric /// Number of the current block LiveDebugValues is stepping through. 11847fa27ce4SDimitry Andric unsigned CurBB = -1; 1185c0981da4SDimitry Andric 1186c0981da4SDimitry Andric /// Number of the current instruction LiveDebugValues is evaluating. 1187c0981da4SDimitry Andric unsigned CurInst; 1188c0981da4SDimitry Andric 1189c0981da4SDimitry Andric /// Variable tracker -- listens to DBG_VALUEs occurring as InstrRefBasedImpl 1190c0981da4SDimitry Andric /// steps through a block. Reads the values at each location from the 1191c0981da4SDimitry Andric /// MLocTracker object. 1192c0981da4SDimitry Andric VLocTracker *VTracker = nullptr; 1193c0981da4SDimitry Andric 1194c0981da4SDimitry Andric /// Tracker for transfers, listens to DBG_VALUEs and transfers of values 1195c0981da4SDimitry Andric /// between locations during stepping, creates new DBG_VALUEs when values move 1196c0981da4SDimitry Andric /// location. 1197c0981da4SDimitry Andric TransferTracker *TTracker = nullptr; 1198c0981da4SDimitry Andric 1199c0981da4SDimitry Andric /// Blocks which are artificial, i.e. blocks which exclusively contain 1200c0981da4SDimitry Andric /// instructions without DebugLocs, or with line 0 locations. 1201ecbca9f5SDimitry Andric SmallPtrSet<MachineBasicBlock *, 16> ArtificialBlocks; 1202c0981da4SDimitry Andric 1203c0981da4SDimitry Andric // Mapping of blocks to and from their RPOT order. 1204ac9a064cSDimitry Andric SmallVector<MachineBasicBlock *> OrderToBB; 1205c0981da4SDimitry Andric DenseMap<const MachineBasicBlock *, unsigned int> BBToOrder; 1206c0981da4SDimitry Andric DenseMap<unsigned, unsigned> BBNumToRPO; 1207c0981da4SDimitry Andric 1208c0981da4SDimitry Andric /// Pair of MachineInstr, and its 1-based offset into the containing block. 1209c0981da4SDimitry Andric using InstAndNum = std::pair<const MachineInstr *, unsigned>; 1210c0981da4SDimitry Andric /// Map from debug instruction number to the MachineInstr labelled with that 1211c0981da4SDimitry Andric /// number, and its location within the function. Used to transform 1212c0981da4SDimitry Andric /// instruction numbers in DBG_INSTR_REFs into machine value numbers. 1213c0981da4SDimitry Andric std::map<uint64_t, InstAndNum> DebugInstrNumToInstr; 1214c0981da4SDimitry Andric 1215c0981da4SDimitry Andric /// Record of where we observed a DBG_PHI instruction. 1216c0981da4SDimitry Andric class DebugPHIRecord { 1217c0981da4SDimitry Andric public: 1218145449b1SDimitry Andric /// Instruction number of this DBG_PHI. 1219145449b1SDimitry Andric uint64_t InstrNum; 1220145449b1SDimitry Andric /// Block where DBG_PHI occurred. 1221145449b1SDimitry Andric MachineBasicBlock *MBB; 1222e3b55780SDimitry Andric /// The value number read by the DBG_PHI -- or std::nullopt if it didn't 1223e3b55780SDimitry Andric /// refer to a value. 1224e3b55780SDimitry Andric std::optional<ValueIDNum> ValueRead; 1225e3b55780SDimitry Andric /// Register/Stack location the DBG_PHI reads -- or std::nullopt if it 1226e3b55780SDimitry Andric /// referred to something unexpected. 1227e3b55780SDimitry Andric std::optional<LocIdx> ReadLoc; 1228c0981da4SDimitry Andric 1229c0981da4SDimitry Andric operator unsigned() const { return InstrNum; } 1230c0981da4SDimitry Andric }; 1231c0981da4SDimitry Andric 1232c0981da4SDimitry Andric /// Map from instruction numbers defined by DBG_PHIs to a record of what that 1233c0981da4SDimitry Andric /// DBG_PHI read and where. Populated and edited during the machine value 1234c0981da4SDimitry Andric /// location problem -- we use LLVMs SSA Updater to fix changes by 1235c0981da4SDimitry Andric /// optimizations that destroy PHI instructions. 1236c0981da4SDimitry Andric SmallVector<DebugPHIRecord, 32> DebugPHINumToValue; 1237c0981da4SDimitry Andric 1238c0981da4SDimitry Andric // Map of overlapping variable fragments. 1239c0981da4SDimitry Andric OverlapMap OverlapFragments; 1240c0981da4SDimitry Andric VarToFragments SeenFragments; 1241c0981da4SDimitry Andric 1242145449b1SDimitry Andric /// Mapping of DBG_INSTR_REF instructions to their values, for those 1243145449b1SDimitry Andric /// DBG_INSTR_REFs that call resolveDbgPHIs. These variable references solve 1244145449b1SDimitry Andric /// a mini SSA problem caused by DBG_PHIs being cloned, this collection caches 1245145449b1SDimitry Andric /// the result. 1246e3b55780SDimitry Andric DenseMap<std::pair<MachineInstr *, unsigned>, std::optional<ValueIDNum>> 1247e3b55780SDimitry Andric SeenDbgPHIs; 1248e3b55780SDimitry Andric 1249e3b55780SDimitry Andric DbgOpIDMap DbgOpStore; 1250145449b1SDimitry Andric 1251ac9a064cSDimitry Andric /// Mapping between DebugVariables and unique ID numbers. This is a more 1252ac9a064cSDimitry Andric /// efficient way to represent the identity of a variable, versus a plain 1253ac9a064cSDimitry Andric /// DebugVariable. 1254ac9a064cSDimitry Andric DebugVariableMap DVMap; 1255ac9a064cSDimitry Andric 1256f65dcba8SDimitry Andric /// True if we need to examine call instructions for stack clobbers. We 1257f65dcba8SDimitry Andric /// normally assume that they don't clobber SP, but stack probes on Windows 1258f65dcba8SDimitry Andric /// do. 1259f65dcba8SDimitry Andric bool AdjustsStackInCalls = false; 1260f65dcba8SDimitry Andric 1261f65dcba8SDimitry Andric /// If AdjustsStackInCalls is true, this holds the name of the target's stack 1262f65dcba8SDimitry Andric /// probe function, which is the function we expect will alter the stack 1263f65dcba8SDimitry Andric /// pointer. 1264f65dcba8SDimitry Andric StringRef StackProbeSymbolName; 1265f65dcba8SDimitry Andric 1266c0981da4SDimitry Andric /// Tests whether this instruction is a spill to a stack slot. 1267e3b55780SDimitry Andric std::optional<SpillLocationNo> isSpillInstruction(const MachineInstr &MI, 1268145449b1SDimitry Andric MachineFunction *MF); 1269c0981da4SDimitry Andric 1270c0981da4SDimitry Andric /// Decide if @MI is a spill instruction and return true if it is. We use 2 1271c0981da4SDimitry Andric /// criteria to make this decision: 1272c0981da4SDimitry Andric /// - Is this instruction a store to a spill slot? 1273c0981da4SDimitry Andric /// - Is there a register operand that is both used and killed? 1274c0981da4SDimitry Andric /// TODO: Store optimization can fold spills into other stores (including 1275c0981da4SDimitry Andric /// other spills). We do not handle this yet (more than one memory operand). 1276c0981da4SDimitry Andric bool isLocationSpill(const MachineInstr &MI, MachineFunction *MF, 1277c0981da4SDimitry Andric unsigned &Reg); 1278c0981da4SDimitry Andric 1279c0981da4SDimitry Andric /// If a given instruction is identified as a spill, return the spill slot 1280c0981da4SDimitry Andric /// and set \p Reg to the spilled register. 1281e3b55780SDimitry Andric std::optional<SpillLocationNo> isRestoreInstruction(const MachineInstr &MI, 1282e3b55780SDimitry Andric MachineFunction *MF, 1283e3b55780SDimitry Andric unsigned &Reg); 1284c0981da4SDimitry Andric 1285c0981da4SDimitry Andric /// Given a spill instruction, extract the spill slot information, ensure it's 1286c0981da4SDimitry Andric /// tracked, and return the spill number. 1287e3b55780SDimitry Andric std::optional<SpillLocationNo> 1288145449b1SDimitry Andric extractSpillBaseRegAndOffset(const MachineInstr &MI); 1289c0981da4SDimitry Andric 1290e3b55780SDimitry Andric /// For an instruction reference given by \p InstNo and \p OpNo in instruction 1291e3b55780SDimitry Andric /// \p MI returns the Value pointed to by that instruction reference if any 12927fa27ce4SDimitry Andric /// exists, otherwise returns std::nullopt. 1293e3b55780SDimitry Andric std::optional<ValueIDNum> getValueForInstrRef(unsigned InstNo, unsigned OpNo, 1294e3b55780SDimitry Andric MachineInstr &MI, 1295312c0ed1SDimitry Andric const FuncValueTable *MLiveOuts, 1296312c0ed1SDimitry Andric const FuncValueTable *MLiveIns); 1297e3b55780SDimitry Andric 1298c0981da4SDimitry Andric /// Observe a single instruction while stepping through a block. 1299312c0ed1SDimitry Andric void process(MachineInstr &MI, const FuncValueTable *MLiveOuts, 1300312c0ed1SDimitry Andric const FuncValueTable *MLiveIns); 1301c0981da4SDimitry Andric 1302c0981da4SDimitry Andric /// Examines whether \p MI is a DBG_VALUE and notifies trackers. 1303c0981da4SDimitry Andric /// \returns true if MI was recognized and processed. 1304c0981da4SDimitry Andric bool transferDebugValue(const MachineInstr &MI); 1305c0981da4SDimitry Andric 1306c0981da4SDimitry Andric /// Examines whether \p MI is a DBG_INSTR_REF and notifies trackers. 1307c0981da4SDimitry Andric /// \returns true if MI was recognized and processed. 1308312c0ed1SDimitry Andric bool transferDebugInstrRef(MachineInstr &MI, const FuncValueTable *MLiveOuts, 1309312c0ed1SDimitry Andric const FuncValueTable *MLiveIns); 1310c0981da4SDimitry Andric 1311c0981da4SDimitry Andric /// Stores value-information about where this PHI occurred, and what 1312c0981da4SDimitry Andric /// instruction number is associated with it. 1313c0981da4SDimitry Andric /// \returns true if MI was recognized and processed. 1314c0981da4SDimitry Andric bool transferDebugPHI(MachineInstr &MI); 1315c0981da4SDimitry Andric 1316c0981da4SDimitry Andric /// Examines whether \p MI is copy instruction, and notifies trackers. 1317c0981da4SDimitry Andric /// \returns true if MI was recognized and processed. 1318c0981da4SDimitry Andric bool transferRegisterCopy(MachineInstr &MI); 1319c0981da4SDimitry Andric 1320c0981da4SDimitry Andric /// Examines whether \p MI is stack spill or restore instruction, and 1321c0981da4SDimitry Andric /// notifies trackers. \returns true if MI was recognized and processed. 1322c0981da4SDimitry Andric bool transferSpillOrRestoreInst(MachineInstr &MI); 1323c0981da4SDimitry Andric 1324c0981da4SDimitry Andric /// Examines \p MI for any registers that it defines, and notifies trackers. 1325c0981da4SDimitry Andric void transferRegisterDef(MachineInstr &MI); 1326c0981da4SDimitry Andric 1327c0981da4SDimitry Andric /// Copy one location to the other, accounting for movement of subregisters 1328c0981da4SDimitry Andric /// too. 1329c0981da4SDimitry Andric void performCopy(Register Src, Register Dst); 1330c0981da4SDimitry Andric 1331c0981da4SDimitry Andric void accumulateFragmentMap(MachineInstr &MI); 1332c0981da4SDimitry Andric 1333c0981da4SDimitry Andric /// Determine the machine value number referred to by (potentially several) 1334c0981da4SDimitry Andric /// DBG_PHI instructions. Block duplication and tail folding can duplicate 1335c0981da4SDimitry Andric /// DBG_PHIs, shifting the position where values in registers merge, and 1336c0981da4SDimitry Andric /// forming another mini-ssa problem to solve. 1337c0981da4SDimitry Andric /// \p Here the position of a DBG_INSTR_REF seeking a machine value number 1338c0981da4SDimitry Andric /// \p InstrNum Debug instruction number defined by DBG_PHI instructions. 1339e3b55780SDimitry Andric /// \returns The machine value number at position Here, or std::nullopt. 1340e3b55780SDimitry Andric std::optional<ValueIDNum> resolveDbgPHIs(MachineFunction &MF, 1341312c0ed1SDimitry Andric const FuncValueTable &MLiveOuts, 1342312c0ed1SDimitry Andric const FuncValueTable &MLiveIns, 1343e3b55780SDimitry Andric MachineInstr &Here, 1344e3b55780SDimitry Andric uint64_t InstrNum); 1345145449b1SDimitry Andric 1346e3b55780SDimitry Andric std::optional<ValueIDNum> resolveDbgPHIsImpl(MachineFunction &MF, 1347312c0ed1SDimitry Andric const FuncValueTable &MLiveOuts, 1348312c0ed1SDimitry Andric const FuncValueTable &MLiveIns, 1349145449b1SDimitry Andric MachineInstr &Here, 1350c0981da4SDimitry Andric uint64_t InstrNum); 1351c0981da4SDimitry Andric 1352c0981da4SDimitry Andric /// Step through the function, recording register definitions and movements 1353c0981da4SDimitry Andric /// in an MLocTracker. Convert the observations into a per-block transfer 1354c0981da4SDimitry Andric /// function in \p MLocTransfer, suitable for using with the machine value 1355c0981da4SDimitry Andric /// location dataflow problem. 1356c0981da4SDimitry Andric void 1357c0981da4SDimitry Andric produceMLocTransferFunction(MachineFunction &MF, 1358c0981da4SDimitry Andric SmallVectorImpl<MLocTransferMap> &MLocTransfer, 1359c0981da4SDimitry Andric unsigned MaxNumBlocks); 1360c0981da4SDimitry Andric 1361c0981da4SDimitry Andric /// Solve the machine value location dataflow problem. Takes as input the 1362c0981da4SDimitry Andric /// transfer functions in \p MLocTransfer. Writes the output live-in and 1363c0981da4SDimitry Andric /// live-out arrays to the (initialized to zero) multidimensional arrays in 1364c0981da4SDimitry Andric /// \p MInLocs and \p MOutLocs. The outer dimension is indexed by block 1365c0981da4SDimitry Andric /// number, the inner by LocIdx. 1366145449b1SDimitry Andric void buildMLocValueMap(MachineFunction &MF, FuncValueTable &MInLocs, 1367145449b1SDimitry Andric FuncValueTable &MOutLocs, 1368c0981da4SDimitry Andric SmallVectorImpl<MLocTransferMap> &MLocTransfer); 1369c0981da4SDimitry Andric 1370c0981da4SDimitry Andric /// Examine the stack indexes (i.e. offsets within the stack) to find the 1371c0981da4SDimitry Andric /// basic units of interference -- like reg units, but for the stack. 1372c0981da4SDimitry Andric void findStackIndexInterference(SmallVectorImpl<unsigned> &Slots); 1373c0981da4SDimitry Andric 1374c0981da4SDimitry Andric /// Install PHI values into the live-in array for each block, according to 1375c0981da4SDimitry Andric /// the IDF of each register. 1376c0981da4SDimitry Andric void placeMLocPHIs(MachineFunction &MF, 1377c0981da4SDimitry Andric SmallPtrSetImpl<MachineBasicBlock *> &AllBlocks, 1378145449b1SDimitry Andric FuncValueTable &MInLocs, 1379c0981da4SDimitry Andric SmallVectorImpl<MLocTransferMap> &MLocTransfer); 1380c0981da4SDimitry Andric 1381ecbca9f5SDimitry Andric /// Propagate variable values to blocks in the common case where there's 1382ecbca9f5SDimitry Andric /// only one value assigned to the variable. This function has better 1383ecbca9f5SDimitry Andric /// performance as it doesn't have to find the dominance frontier between 1384ecbca9f5SDimitry Andric /// different assignments. 1385ecbca9f5SDimitry Andric void placePHIsForSingleVarDefinition( 1386ecbca9f5SDimitry Andric const SmallPtrSetImpl<MachineBasicBlock *> &InScopeBlocks, 1387ecbca9f5SDimitry Andric MachineBasicBlock *MBB, SmallVectorImpl<VLocTracker> &AllTheVLocs, 1388ac9a064cSDimitry Andric DebugVariableID Var, LiveInsT &Output); 1389ecbca9f5SDimitry Andric 1390c0981da4SDimitry Andric /// Calculate the iterated-dominance-frontier for a set of defs, using the 1391c0981da4SDimitry Andric /// existing LLVM facilities for this. Works for a single "value" or 1392c0981da4SDimitry Andric /// machine/variable location. 1393c0981da4SDimitry Andric /// \p AllBlocks Set of blocks where we might consume the value. 1394c0981da4SDimitry Andric /// \p DefBlocks Set of blocks where the value/location is defined. 1395c0981da4SDimitry Andric /// \p PHIBlocks Output set of blocks where PHIs must be placed. 1396c0981da4SDimitry Andric void BlockPHIPlacement(const SmallPtrSetImpl<MachineBasicBlock *> &AllBlocks, 1397c0981da4SDimitry Andric const SmallPtrSetImpl<MachineBasicBlock *> &DefBlocks, 1398c0981da4SDimitry Andric SmallVectorImpl<MachineBasicBlock *> &PHIBlocks); 1399c0981da4SDimitry Andric 1400c0981da4SDimitry Andric /// Perform a control flow join (lattice value meet) of the values in machine 1401c0981da4SDimitry Andric /// locations at \p MBB. Follows the algorithm described in the file-comment, 1402c0981da4SDimitry Andric /// reading live-outs of predecessors from \p OutLocs, the current live ins 1403c0981da4SDimitry Andric /// from \p InLocs, and assigning the newly computed live ins back into 1404c0981da4SDimitry Andric /// \p InLocs. \returns two bools -- the first indicates whether a change 1405c0981da4SDimitry Andric /// was made, the second whether a lattice downgrade occurred. If the latter 1406c0981da4SDimitry Andric /// is true, revisiting this block is necessary. 1407c0981da4SDimitry Andric bool mlocJoin(MachineBasicBlock &MBB, 1408c0981da4SDimitry Andric SmallPtrSet<const MachineBasicBlock *, 16> &Visited, 1409145449b1SDimitry Andric FuncValueTable &OutLocs, ValueTable &InLocs); 1410c0981da4SDimitry Andric 1411ecbca9f5SDimitry Andric /// Produce a set of blocks that are in the current lexical scope. This means 1412ecbca9f5SDimitry Andric /// those blocks that contain instructions "in" the scope, blocks where 1413ecbca9f5SDimitry Andric /// assignments to variables in scope occur, and artificial blocks that are 1414ecbca9f5SDimitry Andric /// successors to any of the earlier blocks. See https://llvm.org/PR48091 for 1415ecbca9f5SDimitry Andric /// more commentry on what "in scope" means. 1416ecbca9f5SDimitry Andric /// \p DILoc A location in the scope that we're fetching blocks for. 1417ecbca9f5SDimitry Andric /// \p Output Set to put in-scope-blocks into. 1418ecbca9f5SDimitry Andric /// \p AssignBlocks Blocks known to contain assignments of variables in scope. 1419ecbca9f5SDimitry Andric void 1420ecbca9f5SDimitry Andric getBlocksForScope(const DILocation *DILoc, 1421ecbca9f5SDimitry Andric SmallPtrSetImpl<const MachineBasicBlock *> &Output, 1422ecbca9f5SDimitry Andric const SmallPtrSetImpl<MachineBasicBlock *> &AssignBlocks); 1423ecbca9f5SDimitry Andric 1424c0981da4SDimitry Andric /// Solve the variable value dataflow problem, for a single lexical scope. 1425c0981da4SDimitry Andric /// Uses the algorithm from the file comment to resolve control flow joins 1426c0981da4SDimitry Andric /// using PHI placement and value propagation. Reads the locations of machine 1427c0981da4SDimitry Andric /// values from the \p MInLocs and \p MOutLocs arrays (see buildMLocValueMap) 1428c0981da4SDimitry Andric /// and reads the variable values transfer function from \p AllTheVlocs. 1429c0981da4SDimitry Andric /// Live-in and Live-out variable values are stored locally, with the live-ins 1430c0981da4SDimitry Andric /// permanently stored to \p Output once a fixedpoint is reached. 1431c0981da4SDimitry Andric /// \p VarsWeCareAbout contains a collection of the variables in \p Scope 1432c0981da4SDimitry Andric /// that we should be tracking. 1433c0981da4SDimitry Andric /// \p AssignBlocks contains the set of blocks that aren't in \p DILoc's 1434c0981da4SDimitry Andric /// scope, but which do contain DBG_VALUEs, which VarLocBasedImpl tracks 1435c0981da4SDimitry Andric /// locations through. 1436c0981da4SDimitry Andric void buildVLocValueMap(const DILocation *DILoc, 1437ac9a064cSDimitry Andric const SmallSet<DebugVariableID, 4> &VarsWeCareAbout, 1438c0981da4SDimitry Andric SmallPtrSetImpl<MachineBasicBlock *> &AssignBlocks, 1439145449b1SDimitry Andric LiveInsT &Output, FuncValueTable &MOutLocs, 1440145449b1SDimitry Andric FuncValueTable &MInLocs, 1441c0981da4SDimitry Andric SmallVectorImpl<VLocTracker> &AllTheVLocs); 1442c0981da4SDimitry Andric 1443c0981da4SDimitry Andric /// Attempt to eliminate un-necessary PHIs on entry to a block. Examines the 1444c0981da4SDimitry Andric /// live-in values coming from predecessors live-outs, and replaces any PHIs 1445c0981da4SDimitry Andric /// already present in this blocks live-ins with a live-through value if the 1446c0981da4SDimitry Andric /// PHI isn't needed. 1447c0981da4SDimitry Andric /// \p LiveIn Old live-in value, overwritten with new one if live-in changes. 1448c0981da4SDimitry Andric /// \returns true if any live-ins change value, either from value propagation 1449c0981da4SDimitry Andric /// or PHI elimination. 1450c0981da4SDimitry Andric bool vlocJoin(MachineBasicBlock &MBB, LiveIdxT &VLOCOutLocs, 1451c0981da4SDimitry Andric SmallPtrSet<const MachineBasicBlock *, 8> &BlocksToExplore, 1452c0981da4SDimitry Andric DbgValue &LiveIn); 1453c0981da4SDimitry Andric 1454e3b55780SDimitry Andric /// For the given block and live-outs feeding into it, try to find 1455e3b55780SDimitry Andric /// machine locations for each debug operand where all the values feeding 1456e3b55780SDimitry Andric /// into that operand join together. 1457e3b55780SDimitry Andric /// \returns true if a joined location was found for every value that needed 1458e3b55780SDimitry Andric /// to be joined. 1459e3b55780SDimitry Andric bool 1460e3b55780SDimitry Andric pickVPHILoc(SmallVectorImpl<DbgOpID> &OutValues, const MachineBasicBlock &MBB, 1461145449b1SDimitry Andric const LiveIdxT &LiveOuts, FuncValueTable &MOutLocs, 1462c0981da4SDimitry Andric const SmallVectorImpl<const MachineBasicBlock *> &BlockOrders); 1463c0981da4SDimitry Andric 1464e3b55780SDimitry Andric std::optional<ValueIDNum> pickOperandPHILoc( 1465e3b55780SDimitry Andric unsigned DbgOpIdx, const MachineBasicBlock &MBB, const LiveIdxT &LiveOuts, 1466e3b55780SDimitry Andric FuncValueTable &MOutLocs, 1467e3b55780SDimitry Andric const SmallVectorImpl<const MachineBasicBlock *> &BlockOrders); 1468e3b55780SDimitry Andric 1469ecbca9f5SDimitry Andric /// Take collections of DBG_VALUE instructions stored in TTracker, and 1470ac9a064cSDimitry Andric /// install them into their output blocks. 1471ac9a064cSDimitry Andric bool emitTransfers(); 1472ecbca9f5SDimitry Andric 1473c0981da4SDimitry Andric /// Boilerplate computation of some initial sets, artifical blocks and 1474c0981da4SDimitry Andric /// RPOT block ordering. 1475c0981da4SDimitry Andric void initialSetup(MachineFunction &MF); 1476c0981da4SDimitry Andric 1477145449b1SDimitry Andric /// Produce a map of the last lexical scope that uses a block, using the 1478145449b1SDimitry Andric /// scopes DFSOut number. Mapping is block-number to DFSOut. 1479145449b1SDimitry Andric /// \p EjectionMap Pre-allocated vector in which to install the built ma. 1480145449b1SDimitry Andric /// \p ScopeToDILocation Mapping of LexicalScopes to their DILocations. 1481145449b1SDimitry Andric /// \p AssignBlocks Map of blocks where assignments happen for a scope. 1482145449b1SDimitry Andric void makeDepthFirstEjectionMap(SmallVectorImpl<unsigned> &EjectionMap, 1483145449b1SDimitry Andric const ScopeToDILocT &ScopeToDILocation, 1484145449b1SDimitry Andric ScopeToAssignBlocksT &AssignBlocks); 1485145449b1SDimitry Andric 1486145449b1SDimitry Andric /// When determining per-block variable values and emitting to DBG_VALUEs, 1487145449b1SDimitry Andric /// this function explores by lexical scope depth. Doing so means that per 1488145449b1SDimitry Andric /// block information can be fully computed before exploration finishes, 1489145449b1SDimitry Andric /// allowing us to emit it and free data structures earlier than otherwise. 1490145449b1SDimitry Andric /// It's also good for locality. 1491ac9a064cSDimitry Andric bool depthFirstVLocAndEmit(unsigned MaxNumBlocks, 1492ac9a064cSDimitry Andric const ScopeToDILocT &ScopeToDILocation, 1493ac9a064cSDimitry Andric const ScopeToVarsT &ScopeToVars, 1494ac9a064cSDimitry Andric ScopeToAssignBlocksT &ScopeToBlocks, 1495ac9a064cSDimitry Andric LiveInsT &Output, FuncValueTable &MOutLocs, 1496ac9a064cSDimitry Andric FuncValueTable &MInLocs, 1497ac9a064cSDimitry Andric SmallVectorImpl<VLocTracker> &AllTheVLocs, 1498ac9a064cSDimitry Andric MachineFunction &MF, const TargetPassConfig &TPC); 1499145449b1SDimitry Andric 1500c0981da4SDimitry Andric bool ExtendRanges(MachineFunction &MF, MachineDominatorTree *DomTree, 1501c0981da4SDimitry Andric TargetPassConfig *TPC, unsigned InputBBLimit, 1502c0981da4SDimitry Andric unsigned InputDbgValLimit) override; 1503c0981da4SDimitry Andric 1504c0981da4SDimitry Andric public: 1505c0981da4SDimitry Andric /// Default construct and initialize the pass. 1506c0981da4SDimitry Andric InstrRefBasedLDV(); 1507c0981da4SDimitry Andric 1508c0981da4SDimitry Andric LLVM_DUMP_METHOD 1509c0981da4SDimitry Andric void dump_mloc_transfer(const MLocTransferMap &mloc_transfer) const; 1510c0981da4SDimitry Andric 1511c0981da4SDimitry Andric bool isCalleeSaved(LocIdx L) const; 1512e3b55780SDimitry Andric bool isCalleeSavedReg(Register R) const; 1513c0981da4SDimitry Andric 1514c0981da4SDimitry Andric bool hasFoldedStackStore(const MachineInstr &MI) { 1515c0981da4SDimitry Andric // Instruction must have a memory operand that's a stack slot, and isn't 1516c0981da4SDimitry Andric // aliased, meaning it's a spill from regalloc instead of a variable. 1517c0981da4SDimitry Andric // If it's aliased, we can't guarantee its value. 1518c0981da4SDimitry Andric if (!MI.hasOneMemOperand()) 1519c0981da4SDimitry Andric return false; 1520c0981da4SDimitry Andric auto *MemOperand = *MI.memoperands_begin(); 1521c0981da4SDimitry Andric return MemOperand->isStore() && 1522c0981da4SDimitry Andric MemOperand->getPseudoValue() && 1523c0981da4SDimitry Andric MemOperand->getPseudoValue()->kind() == PseudoSourceValue::FixedStack 1524c0981da4SDimitry Andric && !MemOperand->getPseudoValue()->isAliased(MFI); 1525c0981da4SDimitry Andric } 1526c0981da4SDimitry Andric 1527e3b55780SDimitry Andric std::optional<LocIdx> findLocationForMemOperand(const MachineInstr &MI); 1528ac9a064cSDimitry Andric 1529ac9a064cSDimitry Andric // Utility for unit testing, don't use directly. 1530ac9a064cSDimitry Andric DebugVariableMap &getDVMap() { 1531ac9a064cSDimitry Andric return DVMap; 1532ac9a064cSDimitry Andric } 1533c0981da4SDimitry Andric }; 1534c0981da4SDimitry Andric 1535c0981da4SDimitry Andric } // namespace LiveDebugValues 1536c0981da4SDimitry Andric 1537c0981da4SDimitry Andric #endif /* LLVM_LIB_CODEGEN_LIVEDEBUGVALUES_INSTRREFBASEDLDV_H */ 1538