1 //===- InstrRefBasedImpl.h - Tracking Debug Value MIs ---------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLVM_LIB_CODEGEN_LIVEDEBUGVALUES_INSTRREFBASEDLDV_H 10 #define LLVM_LIB_CODEGEN_LIVEDEBUGVALUES_INSTRREFBASEDLDV_H 11 12 #include "llvm/ADT/DenseMap.h" 13 #include "llvm/ADT/SmallPtrSet.h" 14 #include "llvm/ADT/SmallVector.h" 15 #include "llvm/ADT/UniqueVector.h" 16 #include "llvm/CodeGen/LexicalScopes.h" 17 #include "llvm/CodeGen/MachineBasicBlock.h" 18 #include "llvm/CodeGen/MachineFrameInfo.h" 19 #include "llvm/CodeGen/MachineFunction.h" 20 #include "llvm/CodeGen/MachineInstr.h" 21 #include "llvm/CodeGen/TargetFrameLowering.h" 22 #include "llvm/CodeGen/TargetInstrInfo.h" 23 #include "llvm/CodeGen/TargetPassConfig.h" 24 #include "llvm/IR/DebugInfoMetadata.h" 25 26 #include "LiveDebugValues.h" 27 28 class TransferTracker; 29 30 // Forward dec of unit test class, so that we can peer into the LDV object. 31 class InstrRefLDVTest; 32 33 namespace LiveDebugValues { 34 35 class MLocTracker; 36 37 using namespace llvm; 38 39 /// Handle-class for a particular "location". This value-type uniquely 40 /// symbolises a register or stack location, allowing manipulation of locations 41 /// without concern for where that location is. Practically, this allows us to 42 /// treat the state of the machine at a particular point as an array of values, 43 /// rather than a map of values. 44 class LocIdx { 45 unsigned Location; 46 47 // Default constructor is private, initializing to an illegal location number. 48 // Use only for "not an entry" elements in IndexedMaps. 49 LocIdx() : Location(UINT_MAX) {} 50 51 public: 52 #define NUM_LOC_BITS 24 53 LocIdx(unsigned L) : Location(L) { 54 assert(L < (1 << NUM_LOC_BITS) && "Machine locations must fit in 24 bits"); 55 } 56 57 static LocIdx MakeIllegalLoc() { return LocIdx(); } 58 59 bool isIllegal() const { return Location == UINT_MAX; } 60 61 uint64_t asU64() const { return Location; } 62 63 bool operator==(unsigned L) const { return Location == L; } 64 65 bool operator==(const LocIdx &L) const { return Location == L.Location; } 66 67 bool operator!=(unsigned L) const { return !(*this == L); } 68 69 bool operator!=(const LocIdx &L) const { return !(*this == L); } 70 71 bool operator<(const LocIdx &Other) const { 72 return Location < Other.Location; 73 } 74 }; 75 76 // The location at which a spilled value resides. It consists of a register and 77 // an offset. 78 struct SpillLoc { 79 unsigned SpillBase; 80 StackOffset SpillOffset; 81 bool operator==(const SpillLoc &Other) const { 82 return std::make_pair(SpillBase, SpillOffset) == 83 std::make_pair(Other.SpillBase, Other.SpillOffset); 84 } 85 bool operator<(const SpillLoc &Other) const { 86 return std::make_tuple(SpillBase, SpillOffset.getFixed(), 87 SpillOffset.getScalable()) < 88 std::make_tuple(Other.SpillBase, Other.SpillOffset.getFixed(), 89 Other.SpillOffset.getScalable()); 90 } 91 }; 92 93 /// Unique identifier for a value defined by an instruction, as a value type. 94 /// Casts back and forth to a uint64_t. Probably replacable with something less 95 /// bit-constrained. Each value identifies the instruction and machine location 96 /// where the value is defined, although there may be no corresponding machine 97 /// operand for it (ex: regmasks clobbering values). The instructions are 98 /// one-based, and definitions that are PHIs have instruction number zero. 99 /// 100 /// The obvious limits of a 1M block function or 1M instruction blocks are 101 /// problematic; but by that point we should probably have bailed out of 102 /// trying to analyse the function. 103 class ValueIDNum { 104 uint64_t BlockNo : 20; /// The block where the def happens. 105 uint64_t InstNo : 20; /// The Instruction where the def happens. 106 /// One based, is distance from start of block. 107 uint64_t LocNo : NUM_LOC_BITS; /// The machine location where the def happens. 108 109 public: 110 // Default-initialize to EmptyValue. This is necessary to make IndexedMaps 111 // of values to work. 112 ValueIDNum() : BlockNo(0xFFFFF), InstNo(0xFFFFF), LocNo(0xFFFFFF) {} 113 114 ValueIDNum(uint64_t Block, uint64_t Inst, uint64_t Loc) 115 : BlockNo(Block), InstNo(Inst), LocNo(Loc) {} 116 117 ValueIDNum(uint64_t Block, uint64_t Inst, LocIdx Loc) 118 : BlockNo(Block), InstNo(Inst), LocNo(Loc.asU64()) {} 119 120 uint64_t getBlock() const { return BlockNo; } 121 uint64_t getInst() const { return InstNo; } 122 uint64_t getLoc() const { return LocNo; } 123 bool isPHI() const { return InstNo == 0; } 124 125 uint64_t asU64() const { 126 uint64_t TmpBlock = BlockNo; 127 uint64_t TmpInst = InstNo; 128 return TmpBlock << 44ull | TmpInst << NUM_LOC_BITS | LocNo; 129 } 130 131 static ValueIDNum fromU64(uint64_t v) { 132 uint64_t L = (v & 0x3FFF); 133 return {v >> 44ull, ((v >> NUM_LOC_BITS) & 0xFFFFF), L}; 134 } 135 136 bool operator<(const ValueIDNum &Other) const { 137 return asU64() < Other.asU64(); 138 } 139 140 bool operator==(const ValueIDNum &Other) const { 141 return std::tie(BlockNo, InstNo, LocNo) == 142 std::tie(Other.BlockNo, Other.InstNo, Other.LocNo); 143 } 144 145 bool operator!=(const ValueIDNum &Other) const { return !(*this == Other); } 146 147 std::string asString(const std::string &mlocname) const { 148 return Twine("Value{bb: ") 149 .concat(Twine(BlockNo).concat( 150 Twine(", inst: ") 151 .concat((InstNo ? Twine(InstNo) : Twine("live-in")) 152 .concat(Twine(", loc: ").concat(Twine(mlocname))) 153 .concat(Twine("}"))))) 154 .str(); 155 } 156 157 static ValueIDNum EmptyValue; 158 }; 159 160 /// Thin wrapper around an integer -- designed to give more type safety to 161 /// spill location numbers. 162 class SpillLocationNo { 163 public: 164 explicit SpillLocationNo(unsigned SpillNo) : SpillNo(SpillNo) {} 165 unsigned SpillNo; 166 unsigned id() const { return SpillNo; } 167 168 bool operator<(const SpillLocationNo &Other) const { 169 return SpillNo < Other.SpillNo; 170 } 171 172 bool operator==(const SpillLocationNo &Other) const { 173 return SpillNo == Other.SpillNo; 174 } 175 bool operator!=(const SpillLocationNo &Other) const { 176 return !(*this == Other); 177 } 178 }; 179 180 /// Meta qualifiers for a value. Pair of whatever expression is used to qualify 181 /// the the value, and Boolean of whether or not it's indirect. 182 class DbgValueProperties { 183 public: 184 DbgValueProperties(const DIExpression *DIExpr, bool Indirect) 185 : DIExpr(DIExpr), Indirect(Indirect) {} 186 187 /// Extract properties from an existing DBG_VALUE instruction. 188 DbgValueProperties(const MachineInstr &MI) { 189 assert(MI.isDebugValue()); 190 DIExpr = MI.getDebugExpression(); 191 Indirect = MI.getOperand(1).isImm(); 192 } 193 194 bool operator==(const DbgValueProperties &Other) const { 195 return std::tie(DIExpr, Indirect) == std::tie(Other.DIExpr, Other.Indirect); 196 } 197 198 bool operator!=(const DbgValueProperties &Other) const { 199 return !(*this == Other); 200 } 201 202 const DIExpression *DIExpr; 203 bool Indirect; 204 }; 205 206 /// Class recording the (high level) _value_ of a variable. Identifies either 207 /// the value of the variable as a ValueIDNum, or a constant MachineOperand. 208 /// This class also stores meta-information about how the value is qualified. 209 /// Used to reason about variable values when performing the second 210 /// (DebugVariable specific) dataflow analysis. 211 class DbgValue { 212 public: 213 /// If Kind is Def, the value number that this value is based on. VPHIs set 214 /// this field to EmptyValue if there is no machine-value for this VPHI, or 215 /// the corresponding machine-value if there is one. 216 ValueIDNum ID; 217 /// If Kind is Const, the MachineOperand defining this value. 218 Optional<MachineOperand> MO; 219 /// For a NoVal or VPHI DbgValue, which block it was generated in. 220 int BlockNo; 221 222 /// Qualifiers for the ValueIDNum above. 223 DbgValueProperties Properties; 224 225 typedef enum { 226 Undef, // Represents a DBG_VALUE $noreg in the transfer function only. 227 Def, // This value is defined by an inst, or is a PHI value. 228 Const, // A constant value contained in the MachineOperand field. 229 VPHI, // Incoming values to BlockNo differ, those values must be joined by 230 // a PHI in this block. 231 NoVal, // Empty DbgValue indicating an unknown value. Used as initializer, 232 // before dominating blocks values are propagated in. 233 } KindT; 234 /// Discriminator for whether this is a constant or an in-program value. 235 KindT Kind; 236 237 DbgValue(const ValueIDNum &Val, const DbgValueProperties &Prop, KindT Kind) 238 : ID(Val), MO(None), BlockNo(0), Properties(Prop), Kind(Kind) { 239 assert(Kind == Def); 240 } 241 242 DbgValue(unsigned BlockNo, const DbgValueProperties &Prop, KindT Kind) 243 : ID(ValueIDNum::EmptyValue), MO(None), BlockNo(BlockNo), 244 Properties(Prop), Kind(Kind) { 245 assert(Kind == NoVal || Kind == VPHI); 246 } 247 248 DbgValue(const MachineOperand &MO, const DbgValueProperties &Prop, KindT Kind) 249 : ID(ValueIDNum::EmptyValue), MO(MO), BlockNo(0), Properties(Prop), 250 Kind(Kind) { 251 assert(Kind == Const); 252 } 253 254 DbgValue(const DbgValueProperties &Prop, KindT Kind) 255 : ID(ValueIDNum::EmptyValue), MO(None), BlockNo(0), Properties(Prop), 256 Kind(Kind) { 257 assert(Kind == Undef && 258 "Empty DbgValue constructor must pass in Undef kind"); 259 } 260 261 #ifndef NDEBUG 262 void dump(const MLocTracker *MTrack) const; 263 #endif 264 265 bool operator==(const DbgValue &Other) const { 266 if (std::tie(Kind, Properties) != std::tie(Other.Kind, Other.Properties)) 267 return false; 268 else if (Kind == Def && ID != Other.ID) 269 return false; 270 else if (Kind == NoVal && BlockNo != Other.BlockNo) 271 return false; 272 else if (Kind == Const) 273 return MO->isIdenticalTo(*Other.MO); 274 else if (Kind == VPHI && BlockNo != Other.BlockNo) 275 return false; 276 else if (Kind == VPHI && ID != Other.ID) 277 return false; 278 279 return true; 280 } 281 282 bool operator!=(const DbgValue &Other) const { return !(*this == Other); } 283 }; 284 285 class LocIdxToIndexFunctor { 286 public: 287 using argument_type = LocIdx; 288 unsigned operator()(const LocIdx &L) const { return L.asU64(); } 289 }; 290 291 /// Tracker for what values are in machine locations. Listens to the Things 292 /// being Done by various instructions, and maintains a table of what machine 293 /// locations have what values (as defined by a ValueIDNum). 294 /// 295 /// There are potentially a much larger number of machine locations on the 296 /// target machine than the actual working-set size of the function. On x86 for 297 /// example, we're extremely unlikely to want to track values through control 298 /// or debug registers. To avoid doing so, MLocTracker has several layers of 299 /// indirection going on, described below, to avoid unnecessarily tracking 300 /// any location. 301 /// 302 /// Here's a sort of diagram of the indexes, read from the bottom up: 303 /// 304 /// Size on stack Offset on stack 305 /// \ / 306 /// Stack Idx (Where in slot is this?) 307 /// / 308 /// / 309 /// Slot Num (%stack.0) / 310 /// FrameIdx => SpillNum / 311 /// \ / 312 /// SpillID (int) Register number (int) 313 /// \ / 314 /// LocationID => LocIdx 315 /// | 316 /// LocIdx => ValueIDNum 317 /// 318 /// The aim here is that the LocIdx => ValueIDNum vector is just an array of 319 /// values in numbered locations, so that later analyses can ignore whether the 320 /// location is a register or otherwise. To map a register / spill location to 321 /// a LocIdx, you have to use the (sparse) LocationID => LocIdx map. And to 322 /// build a LocationID for a stack slot, you need to combine identifiers for 323 /// which stack slot it is and where within that slot is being described. 324 /// 325 /// Register mask operands cause trouble by technically defining every register; 326 /// various hacks are used to avoid tracking registers that are never read and 327 /// only written by regmasks. 328 class MLocTracker { 329 public: 330 MachineFunction &MF; 331 const TargetInstrInfo &TII; 332 const TargetRegisterInfo &TRI; 333 const TargetLowering &TLI; 334 335 /// IndexedMap type, mapping from LocIdx to ValueIDNum. 336 using LocToValueType = IndexedMap<ValueIDNum, LocIdxToIndexFunctor>; 337 338 /// Map of LocIdxes to the ValueIDNums that they store. This is tightly 339 /// packed, entries only exist for locations that are being tracked. 340 LocToValueType LocIdxToIDNum; 341 342 /// "Map" of machine location IDs (i.e., raw register or spill number) to the 343 /// LocIdx key / number for that location. There are always at least as many 344 /// as the number of registers on the target -- if the value in the register 345 /// is not being tracked, then the LocIdx value will be zero. New entries are 346 /// appended if a new spill slot begins being tracked. 347 /// This, and the corresponding reverse map persist for the analysis of the 348 /// whole function, and is necessarying for decoding various vectors of 349 /// values. 350 std::vector<LocIdx> LocIDToLocIdx; 351 352 /// Inverse map of LocIDToLocIdx. 353 IndexedMap<unsigned, LocIdxToIndexFunctor> LocIdxToLocID; 354 355 /// When clobbering register masks, we chose to not believe the machine model 356 /// and don't clobber SP. Do the same for SP aliases, and for efficiency, 357 /// keep a set of them here. 358 SmallSet<Register, 8> SPAliases; 359 360 /// Unique-ification of spill. Used to number them -- their LocID number is 361 /// the index in SpillLocs minus one plus NumRegs. 362 UniqueVector<SpillLoc> SpillLocs; 363 364 // If we discover a new machine location, assign it an mphi with this 365 // block number. 366 unsigned CurBB; 367 368 /// Cached local copy of the number of registers the target has. 369 unsigned NumRegs; 370 371 /// Number of slot indexes the target has -- distinct segments of a stack 372 /// slot that can take on the value of a subregister, when a super-register 373 /// is written to the stack. 374 unsigned NumSlotIdxes; 375 376 /// Collection of register mask operands that have been observed. Second part 377 /// of pair indicates the instruction that they happened in. Used to 378 /// reconstruct where defs happened if we start tracking a location later 379 /// on. 380 SmallVector<std::pair<const MachineOperand *, unsigned>, 32> Masks; 381 382 /// Pair for describing a position within a stack slot -- first the size in 383 /// bits, then the offset. 384 typedef std::pair<unsigned short, unsigned short> StackSlotPos; 385 386 /// Map from a size/offset pair describing a position in a stack slot, to a 387 /// numeric identifier for that position. Allows easier identification of 388 /// individual positions. 389 DenseMap<StackSlotPos, unsigned> StackSlotIdxes; 390 391 /// Inverse of StackSlotIdxes. 392 DenseMap<unsigned, StackSlotPos> StackIdxesToPos; 393 394 /// Iterator for locations and the values they contain. Dereferencing 395 /// produces a struct/pair containing the LocIdx key for this location, 396 /// and a reference to the value currently stored. Simplifies the process 397 /// of seeking a particular location. 398 class MLocIterator { 399 LocToValueType &ValueMap; 400 LocIdx Idx; 401 402 public: 403 class value_type { 404 public: 405 value_type(LocIdx Idx, ValueIDNum &Value) : Idx(Idx), Value(Value) {} 406 const LocIdx Idx; /// Read-only index of this location. 407 ValueIDNum &Value; /// Reference to the stored value at this location. 408 }; 409 410 MLocIterator(LocToValueType &ValueMap, LocIdx Idx) 411 : ValueMap(ValueMap), Idx(Idx) {} 412 413 bool operator==(const MLocIterator &Other) const { 414 assert(&ValueMap == &Other.ValueMap); 415 return Idx == Other.Idx; 416 } 417 418 bool operator!=(const MLocIterator &Other) const { 419 return !(*this == Other); 420 } 421 422 void operator++() { Idx = LocIdx(Idx.asU64() + 1); } 423 424 value_type operator*() { return value_type(Idx, ValueMap[LocIdx(Idx)]); } 425 }; 426 427 MLocTracker(MachineFunction &MF, const TargetInstrInfo &TII, 428 const TargetRegisterInfo &TRI, const TargetLowering &TLI); 429 430 /// Produce location ID number for a Register. Provides some small amount of 431 /// type safety. 432 /// \param Reg The register we're looking up. 433 unsigned getLocID(Register Reg) { return Reg.id(); } 434 435 /// Produce location ID number for a spill position. 436 /// \param Spill The number of the spill we're fetching the location for. 437 /// \param SpillSubReg Subregister within the spill we're addressing. 438 unsigned getLocID(SpillLocationNo Spill, unsigned SpillSubReg) { 439 unsigned short Size = TRI.getSubRegIdxSize(SpillSubReg); 440 unsigned short Offs = TRI.getSubRegIdxOffset(SpillSubReg); 441 return getLocID(Spill, {Size, Offs}); 442 } 443 444 /// Produce location ID number for a spill position. 445 /// \param Spill The number of the spill we're fetching the location for. 446 /// \apram SpillIdx size/offset within the spill slot to be addressed. 447 unsigned getLocID(SpillLocationNo Spill, StackSlotPos Idx) { 448 unsigned SlotNo = Spill.id() - 1; 449 SlotNo *= NumSlotIdxes; 450 assert(StackSlotIdxes.find(Idx) != StackSlotIdxes.end()); 451 SlotNo += StackSlotIdxes[Idx]; 452 SlotNo += NumRegs; 453 return SlotNo; 454 } 455 456 /// Given a spill number, and a slot within the spill, calculate the ID number 457 /// for that location. 458 unsigned getSpillIDWithIdx(SpillLocationNo Spill, unsigned Idx) { 459 unsigned SlotNo = Spill.id() - 1; 460 SlotNo *= NumSlotIdxes; 461 SlotNo += Idx; 462 SlotNo += NumRegs; 463 return SlotNo; 464 } 465 466 /// Return the spill number that a location ID corresponds to. 467 SpillLocationNo locIDToSpill(unsigned ID) const { 468 assert(ID >= NumRegs); 469 ID -= NumRegs; 470 // Truncate away the index part, leaving only the spill number. 471 ID /= NumSlotIdxes; 472 return SpillLocationNo(ID + 1); // The UniqueVector is one-based. 473 } 474 475 /// Returns the spill-slot size/offs that a location ID corresponds to. 476 StackSlotPos locIDToSpillIdx(unsigned ID) const { 477 assert(ID >= NumRegs); 478 ID -= NumRegs; 479 unsigned Idx = ID % NumSlotIdxes; 480 return StackIdxesToPos.find(Idx)->second; 481 } 482 483 unsigned getNumLocs(void) const { return LocIdxToIDNum.size(); } 484 485 /// Reset all locations to contain a PHI value at the designated block. Used 486 /// sometimes for actual PHI values, othertimes to indicate the block entry 487 /// value (before any more information is known). 488 void setMPhis(unsigned NewCurBB) { 489 CurBB = NewCurBB; 490 for (auto Location : locations()) 491 Location.Value = {CurBB, 0, Location.Idx}; 492 } 493 494 /// Load values for each location from array of ValueIDNums. Take current 495 /// bbnum just in case we read a value from a hitherto untouched register. 496 void loadFromArray(ValueIDNum *Locs, unsigned NewCurBB) { 497 CurBB = NewCurBB; 498 // Iterate over all tracked locations, and load each locations live-in 499 // value into our local index. 500 for (auto Location : locations()) 501 Location.Value = Locs[Location.Idx.asU64()]; 502 } 503 504 /// Wipe any un-necessary location records after traversing a block. 505 void reset(void) { 506 // We could reset all the location values too; however either loadFromArray 507 // or setMPhis should be called before this object is re-used. Just 508 // clear Masks, they're definitely not needed. 509 Masks.clear(); 510 } 511 512 /// Clear all data. Destroys the LocID <=> LocIdx map, which makes most of 513 /// the information in this pass uninterpretable. 514 void clear(void) { 515 reset(); 516 LocIDToLocIdx.clear(); 517 LocIdxToLocID.clear(); 518 LocIdxToIDNum.clear(); 519 // SpillLocs.reset(); XXX UniqueVector::reset assumes a SpillLoc casts from 520 // 0 521 SpillLocs = decltype(SpillLocs)(); 522 StackSlotIdxes.clear(); 523 StackIdxesToPos.clear(); 524 525 LocIDToLocIdx.resize(NumRegs, LocIdx::MakeIllegalLoc()); 526 } 527 528 /// Set a locaiton to a certain value. 529 void setMLoc(LocIdx L, ValueIDNum Num) { 530 assert(L.asU64() < LocIdxToIDNum.size()); 531 LocIdxToIDNum[L] = Num; 532 } 533 534 /// Read the value of a particular location 535 ValueIDNum readMLoc(LocIdx L) { 536 assert(L.asU64() < LocIdxToIDNum.size()); 537 return LocIdxToIDNum[L]; 538 } 539 540 /// Create a LocIdx for an untracked register ID. Initialize it to either an 541 /// mphi value representing a live-in, or a recent register mask clobber. 542 LocIdx trackRegister(unsigned ID); 543 544 LocIdx lookupOrTrackRegister(unsigned ID) { 545 LocIdx &Index = LocIDToLocIdx[ID]; 546 if (Index.isIllegal()) 547 Index = trackRegister(ID); 548 return Index; 549 } 550 551 /// Is register R currently tracked by MLocTracker? 552 bool isRegisterTracked(Register R) { 553 LocIdx &Index = LocIDToLocIdx[R]; 554 return !Index.isIllegal(); 555 } 556 557 /// Record a definition of the specified register at the given block / inst. 558 /// This doesn't take a ValueIDNum, because the definition and its location 559 /// are synonymous. 560 void defReg(Register R, unsigned BB, unsigned Inst) { 561 unsigned ID = getLocID(R); 562 LocIdx Idx = lookupOrTrackRegister(ID); 563 ValueIDNum ValueID = {BB, Inst, Idx}; 564 LocIdxToIDNum[Idx] = ValueID; 565 } 566 567 /// Set a register to a value number. To be used if the value number is 568 /// known in advance. 569 void setReg(Register R, ValueIDNum ValueID) { 570 unsigned ID = getLocID(R); 571 LocIdx Idx = lookupOrTrackRegister(ID); 572 LocIdxToIDNum[Idx] = ValueID; 573 } 574 575 ValueIDNum readReg(Register R) { 576 unsigned ID = getLocID(R); 577 LocIdx Idx = lookupOrTrackRegister(ID); 578 return LocIdxToIDNum[Idx]; 579 } 580 581 /// Reset a register value to zero / empty. Needed to replicate the 582 /// VarLoc implementation where a copy to/from a register effectively 583 /// clears the contents of the source register. (Values can only have one 584 /// machine location in VarLocBasedImpl). 585 void wipeRegister(Register R) { 586 unsigned ID = getLocID(R); 587 LocIdx Idx = LocIDToLocIdx[ID]; 588 LocIdxToIDNum[Idx] = ValueIDNum::EmptyValue; 589 } 590 591 /// Determine the LocIdx of an existing register. 592 LocIdx getRegMLoc(Register R) { 593 unsigned ID = getLocID(R); 594 assert(ID < LocIDToLocIdx.size()); 595 assert(LocIDToLocIdx[ID] != UINT_MAX); // Sentinal for IndexedMap. 596 return LocIDToLocIdx[ID]; 597 } 598 599 /// Record a RegMask operand being executed. Defs any register we currently 600 /// track, stores a pointer to the mask in case we have to account for it 601 /// later. 602 void writeRegMask(const MachineOperand *MO, unsigned CurBB, unsigned InstID); 603 604 /// Find LocIdx for SpillLoc \p L, creating a new one if it's not tracked. 605 SpillLocationNo getOrTrackSpillLoc(SpillLoc L); 606 607 // Get LocIdx of a spill ID. 608 LocIdx getSpillMLoc(unsigned SpillID) { 609 assert(LocIDToLocIdx[SpillID] != UINT_MAX); // Sentinal for IndexedMap. 610 return LocIDToLocIdx[SpillID]; 611 } 612 613 /// Return true if Idx is a spill machine location. 614 bool isSpill(LocIdx Idx) const { return LocIdxToLocID[Idx] >= NumRegs; } 615 616 MLocIterator begin() { return MLocIterator(LocIdxToIDNum, 0); } 617 618 MLocIterator end() { 619 return MLocIterator(LocIdxToIDNum, LocIdxToIDNum.size()); 620 } 621 622 /// Return a range over all locations currently tracked. 623 iterator_range<MLocIterator> locations() { 624 return llvm::make_range(begin(), end()); 625 } 626 627 std::string LocIdxToName(LocIdx Idx) const; 628 629 std::string IDAsString(const ValueIDNum &Num) const; 630 631 #ifndef NDEBUG 632 LLVM_DUMP_METHOD void dump(); 633 634 LLVM_DUMP_METHOD void dump_mloc_map(); 635 #endif 636 637 /// Create a DBG_VALUE based on machine location \p MLoc. Qualify it with the 638 /// information in \pProperties, for variable Var. Don't insert it anywhere, 639 /// just return the builder for it. 640 MachineInstrBuilder emitLoc(Optional<LocIdx> MLoc, const DebugVariable &Var, 641 const DbgValueProperties &Properties); 642 }; 643 644 /// Collection of DBG_VALUEs observed when traversing a block. Records each 645 /// variable and the value the DBG_VALUE refers to. Requires the machine value 646 /// location dataflow algorithm to have run already, so that values can be 647 /// identified. 648 class VLocTracker { 649 public: 650 /// Map DebugVariable to the latest Value it's defined to have. 651 /// Needs to be a MapVector because we determine order-in-the-input-MIR from 652 /// the order in this container. 653 /// We only retain the last DbgValue in each block for each variable, to 654 /// determine the blocks live-out variable value. The Vars container forms the 655 /// transfer function for this block, as part of the dataflow analysis. The 656 /// movement of values between locations inside of a block is handled at a 657 /// much later stage, in the TransferTracker class. 658 MapVector<DebugVariable, DbgValue> Vars; 659 DenseMap<DebugVariable, const DILocation *> Scopes; 660 MachineBasicBlock *MBB = nullptr; 661 662 public: 663 VLocTracker() {} 664 665 void defVar(const MachineInstr &MI, const DbgValueProperties &Properties, 666 Optional<ValueIDNum> ID) { 667 assert(MI.isDebugValue() || MI.isDebugRef()); 668 DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(), 669 MI.getDebugLoc()->getInlinedAt()); 670 DbgValue Rec = (ID) ? DbgValue(*ID, Properties, DbgValue::Def) 671 : DbgValue(Properties, DbgValue::Undef); 672 673 // Attempt insertion; overwrite if it's already mapped. 674 auto Result = Vars.insert(std::make_pair(Var, Rec)); 675 if (!Result.second) 676 Result.first->second = Rec; 677 Scopes[Var] = MI.getDebugLoc().get(); 678 } 679 680 void defVar(const MachineInstr &MI, const MachineOperand &MO) { 681 // Only DBG_VALUEs can define constant-valued variables. 682 assert(MI.isDebugValue()); 683 DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(), 684 MI.getDebugLoc()->getInlinedAt()); 685 DbgValueProperties Properties(MI); 686 DbgValue Rec = DbgValue(MO, Properties, DbgValue::Const); 687 688 // Attempt insertion; overwrite if it's already mapped. 689 auto Result = Vars.insert(std::make_pair(Var, Rec)); 690 if (!Result.second) 691 Result.first->second = Rec; 692 Scopes[Var] = MI.getDebugLoc().get(); 693 } 694 }; 695 696 /// Types for recording sets of variable fragments that overlap. For a given 697 /// local variable, we record all other fragments of that variable that could 698 /// overlap it, to reduce search time. 699 using FragmentOfVar = 700 std::pair<const DILocalVariable *, DIExpression::FragmentInfo>; 701 using OverlapMap = 702 DenseMap<FragmentOfVar, SmallVector<DIExpression::FragmentInfo, 1>>; 703 704 // XXX XXX docs 705 class InstrRefBasedLDV : public LDVImpl { 706 public: 707 friend class ::InstrRefLDVTest; 708 709 using FragmentInfo = DIExpression::FragmentInfo; 710 using OptFragmentInfo = Optional<DIExpression::FragmentInfo>; 711 712 // Helper while building OverlapMap, a map of all fragments seen for a given 713 // DILocalVariable. 714 using VarToFragments = 715 DenseMap<const DILocalVariable *, SmallSet<FragmentInfo, 4>>; 716 717 /// Machine location/value transfer function, a mapping of which locations 718 /// are assigned which new values. 719 using MLocTransferMap = std::map<LocIdx, ValueIDNum>; 720 721 /// Live in/out structure for the variable values: a per-block map of 722 /// variables to their values. 723 using LiveIdxT = DenseMap<const MachineBasicBlock *, DbgValue *>; 724 725 using VarAndLoc = std::pair<DebugVariable, DbgValue>; 726 727 /// Type for a live-in value: the predecessor block, and its value. 728 using InValueT = std::pair<MachineBasicBlock *, DbgValue *>; 729 730 /// Vector (per block) of a collection (inner smallvector) of live-ins. 731 /// Used as the result type for the variable value dataflow problem. 732 using LiveInsT = SmallVector<SmallVector<VarAndLoc, 8>, 8>; 733 734 private: 735 MachineDominatorTree *DomTree; 736 const TargetRegisterInfo *TRI; 737 const MachineRegisterInfo *MRI; 738 const TargetInstrInfo *TII; 739 const TargetFrameLowering *TFI; 740 const MachineFrameInfo *MFI; 741 BitVector CalleeSavedRegs; 742 LexicalScopes LS; 743 TargetPassConfig *TPC; 744 745 // An empty DIExpression. Used default / placeholder DbgValueProperties 746 // objects, as we can't have null expressions. 747 const DIExpression *EmptyExpr; 748 749 /// Object to track machine locations as we step through a block. Could 750 /// probably be a field rather than a pointer, as it's always used. 751 MLocTracker *MTracker = nullptr; 752 753 /// Number of the current block LiveDebugValues is stepping through. 754 unsigned CurBB; 755 756 /// Number of the current instruction LiveDebugValues is evaluating. 757 unsigned CurInst; 758 759 /// Variable tracker -- listens to DBG_VALUEs occurring as InstrRefBasedImpl 760 /// steps through a block. Reads the values at each location from the 761 /// MLocTracker object. 762 VLocTracker *VTracker = nullptr; 763 764 /// Tracker for transfers, listens to DBG_VALUEs and transfers of values 765 /// between locations during stepping, creates new DBG_VALUEs when values move 766 /// location. 767 TransferTracker *TTracker = nullptr; 768 769 /// Blocks which are artificial, i.e. blocks which exclusively contain 770 /// instructions without DebugLocs, or with line 0 locations. 771 SmallPtrSet<const MachineBasicBlock *, 16> ArtificialBlocks; 772 773 // Mapping of blocks to and from their RPOT order. 774 DenseMap<unsigned int, MachineBasicBlock *> OrderToBB; 775 DenseMap<const MachineBasicBlock *, unsigned int> BBToOrder; 776 DenseMap<unsigned, unsigned> BBNumToRPO; 777 778 /// Pair of MachineInstr, and its 1-based offset into the containing block. 779 using InstAndNum = std::pair<const MachineInstr *, unsigned>; 780 /// Map from debug instruction number to the MachineInstr labelled with that 781 /// number, and its location within the function. Used to transform 782 /// instruction numbers in DBG_INSTR_REFs into machine value numbers. 783 std::map<uint64_t, InstAndNum> DebugInstrNumToInstr; 784 785 /// Record of where we observed a DBG_PHI instruction. 786 class DebugPHIRecord { 787 public: 788 uint64_t InstrNum; ///< Instruction number of this DBG_PHI. 789 MachineBasicBlock *MBB; ///< Block where DBG_PHI occurred. 790 ValueIDNum ValueRead; ///< The value number read by the DBG_PHI. 791 LocIdx ReadLoc; ///< Register/Stack location the DBG_PHI reads. 792 793 operator unsigned() const { return InstrNum; } 794 }; 795 796 /// Map from instruction numbers defined by DBG_PHIs to a record of what that 797 /// DBG_PHI read and where. Populated and edited during the machine value 798 /// location problem -- we use LLVMs SSA Updater to fix changes by 799 /// optimizations that destroy PHI instructions. 800 SmallVector<DebugPHIRecord, 32> DebugPHINumToValue; 801 802 // Map of overlapping variable fragments. 803 OverlapMap OverlapFragments; 804 VarToFragments SeenFragments; 805 806 /// Tests whether this instruction is a spill to a stack slot. 807 bool isSpillInstruction(const MachineInstr &MI, MachineFunction *MF); 808 809 /// Decide if @MI is a spill instruction and return true if it is. We use 2 810 /// criteria to make this decision: 811 /// - Is this instruction a store to a spill slot? 812 /// - Is there a register operand that is both used and killed? 813 /// TODO: Store optimization can fold spills into other stores (including 814 /// other spills). We do not handle this yet (more than one memory operand). 815 bool isLocationSpill(const MachineInstr &MI, MachineFunction *MF, 816 unsigned &Reg); 817 818 /// If a given instruction is identified as a spill, return the spill slot 819 /// and set \p Reg to the spilled register. 820 Optional<SpillLocationNo> isRestoreInstruction(const MachineInstr &MI, 821 MachineFunction *MF, unsigned &Reg); 822 823 /// Given a spill instruction, extract the spill slot information, ensure it's 824 /// tracked, and return the spill number. 825 SpillLocationNo extractSpillBaseRegAndOffset(const MachineInstr &MI); 826 827 /// Observe a single instruction while stepping through a block. 828 void process(MachineInstr &MI, ValueIDNum **MLiveOuts = nullptr, 829 ValueIDNum **MLiveIns = nullptr); 830 831 /// Examines whether \p MI is a DBG_VALUE and notifies trackers. 832 /// \returns true if MI was recognized and processed. 833 bool transferDebugValue(const MachineInstr &MI); 834 835 /// Examines whether \p MI is a DBG_INSTR_REF and notifies trackers. 836 /// \returns true if MI was recognized and processed. 837 bool transferDebugInstrRef(MachineInstr &MI, ValueIDNum **MLiveOuts, 838 ValueIDNum **MLiveIns); 839 840 /// Stores value-information about where this PHI occurred, and what 841 /// instruction number is associated with it. 842 /// \returns true if MI was recognized and processed. 843 bool transferDebugPHI(MachineInstr &MI); 844 845 /// Examines whether \p MI is copy instruction, and notifies trackers. 846 /// \returns true if MI was recognized and processed. 847 bool transferRegisterCopy(MachineInstr &MI); 848 849 /// Examines whether \p MI is stack spill or restore instruction, and 850 /// notifies trackers. \returns true if MI was recognized and processed. 851 bool transferSpillOrRestoreInst(MachineInstr &MI); 852 853 /// Examines \p MI for any registers that it defines, and notifies trackers. 854 void transferRegisterDef(MachineInstr &MI); 855 856 /// Copy one location to the other, accounting for movement of subregisters 857 /// too. 858 void performCopy(Register Src, Register Dst); 859 860 void accumulateFragmentMap(MachineInstr &MI); 861 862 /// Determine the machine value number referred to by (potentially several) 863 /// DBG_PHI instructions. Block duplication and tail folding can duplicate 864 /// DBG_PHIs, shifting the position where values in registers merge, and 865 /// forming another mini-ssa problem to solve. 866 /// \p Here the position of a DBG_INSTR_REF seeking a machine value number 867 /// \p InstrNum Debug instruction number defined by DBG_PHI instructions. 868 /// \returns The machine value number at position Here, or None. 869 Optional<ValueIDNum> resolveDbgPHIs(MachineFunction &MF, 870 ValueIDNum **MLiveOuts, 871 ValueIDNum **MLiveIns, MachineInstr &Here, 872 uint64_t InstrNum); 873 874 /// Step through the function, recording register definitions and movements 875 /// in an MLocTracker. Convert the observations into a per-block transfer 876 /// function in \p MLocTransfer, suitable for using with the machine value 877 /// location dataflow problem. 878 void 879 produceMLocTransferFunction(MachineFunction &MF, 880 SmallVectorImpl<MLocTransferMap> &MLocTransfer, 881 unsigned MaxNumBlocks); 882 883 /// Solve the machine value location dataflow problem. Takes as input the 884 /// transfer functions in \p MLocTransfer. Writes the output live-in and 885 /// live-out arrays to the (initialized to zero) multidimensional arrays in 886 /// \p MInLocs and \p MOutLocs. The outer dimension is indexed by block 887 /// number, the inner by LocIdx. 888 void buildMLocValueMap(MachineFunction &MF, ValueIDNum **MInLocs, 889 ValueIDNum **MOutLocs, 890 SmallVectorImpl<MLocTransferMap> &MLocTransfer); 891 892 /// Install PHI values into the live-in array for each block, according to 893 /// the IDF of each register. 894 void placeMLocPHIs(MachineFunction &MF, 895 SmallPtrSetImpl<MachineBasicBlock *> &AllBlocks, 896 ValueIDNum **MInLocs, 897 SmallVectorImpl<MLocTransferMap> &MLocTransfer); 898 899 /// Calculate the iterated-dominance-frontier for a set of defs, using the 900 /// existing LLVM facilities for this. Works for a single "value" or 901 /// machine/variable location. 902 /// \p AllBlocks Set of blocks where we might consume the value. 903 /// \p DefBlocks Set of blocks where the value/location is defined. 904 /// \p PHIBlocks Output set of blocks where PHIs must be placed. 905 void BlockPHIPlacement(const SmallPtrSetImpl<MachineBasicBlock *> &AllBlocks, 906 const SmallPtrSetImpl<MachineBasicBlock *> &DefBlocks, 907 SmallVectorImpl<MachineBasicBlock *> &PHIBlocks); 908 909 /// Perform a control flow join (lattice value meet) of the values in machine 910 /// locations at \p MBB. Follows the algorithm described in the file-comment, 911 /// reading live-outs of predecessors from \p OutLocs, the current live ins 912 /// from \p InLocs, and assigning the newly computed live ins back into 913 /// \p InLocs. \returns two bools -- the first indicates whether a change 914 /// was made, the second whether a lattice downgrade occurred. If the latter 915 /// is true, revisiting this block is necessary. 916 bool mlocJoin(MachineBasicBlock &MBB, 917 SmallPtrSet<const MachineBasicBlock *, 16> &Visited, 918 ValueIDNum **OutLocs, ValueIDNum *InLocs); 919 920 /// Solve the variable value dataflow problem, for a single lexical scope. 921 /// Uses the algorithm from the file comment to resolve control flow joins 922 /// using PHI placement and value propagation. Reads the locations of machine 923 /// values from the \p MInLocs and \p MOutLocs arrays (see buildMLocValueMap) 924 /// and reads the variable values transfer function from \p AllTheVlocs. 925 /// Live-in and Live-out variable values are stored locally, with the live-ins 926 /// permanently stored to \p Output once a fixedpoint is reached. 927 /// \p VarsWeCareAbout contains a collection of the variables in \p Scope 928 /// that we should be tracking. 929 /// \p AssignBlocks contains the set of blocks that aren't in \p DILoc's 930 /// scope, but which do contain DBG_VALUEs, which VarLocBasedImpl tracks 931 /// locations through. 932 void buildVLocValueMap(const DILocation *DILoc, 933 const SmallSet<DebugVariable, 4> &VarsWeCareAbout, 934 SmallPtrSetImpl<MachineBasicBlock *> &AssignBlocks, 935 LiveInsT &Output, ValueIDNum **MOutLocs, 936 ValueIDNum **MInLocs, 937 SmallVectorImpl<VLocTracker> &AllTheVLocs); 938 939 /// Attempt to eliminate un-necessary PHIs on entry to a block. Examines the 940 /// live-in values coming from predecessors live-outs, and replaces any PHIs 941 /// already present in this blocks live-ins with a live-through value if the 942 /// PHI isn't needed. 943 /// \p LiveIn Old live-in value, overwritten with new one if live-in changes. 944 /// \returns true if any live-ins change value, either from value propagation 945 /// or PHI elimination. 946 bool vlocJoin(MachineBasicBlock &MBB, LiveIdxT &VLOCOutLocs, 947 SmallPtrSet<const MachineBasicBlock *, 8> &InScopeBlocks, 948 SmallPtrSet<const MachineBasicBlock *, 8> &BlocksToExplore, 949 DbgValue &LiveIn); 950 951 /// For the given block and live-outs feeding into it, try to find a 952 /// machine location where all the variable values join together. 953 /// \returns Value ID of a machine PHI if an appropriate one is available. 954 Optional<ValueIDNum> 955 pickVPHILoc(const MachineBasicBlock &MBB, const DebugVariable &Var, 956 const LiveIdxT &LiveOuts, ValueIDNum **MOutLocs, 957 const SmallVectorImpl<const MachineBasicBlock *> &BlockOrders); 958 959 /// Given the solutions to the two dataflow problems, machine value locations 960 /// in \p MInLocs and live-in variable values in \p SavedLiveIns, runs the 961 /// TransferTracker class over the function to produce live-in and transfer 962 /// DBG_VALUEs, then inserts them. Groups of DBG_VALUEs are inserted in the 963 /// order given by AllVarsNumbering -- this could be any stable order, but 964 /// right now "order of appearence in function, when explored in RPO", so 965 /// that we can compare explictly against VarLocBasedImpl. 966 void emitLocations(MachineFunction &MF, LiveInsT SavedLiveIns, 967 ValueIDNum **MOutLocs, ValueIDNum **MInLocs, 968 DenseMap<DebugVariable, unsigned> &AllVarsNumbering, 969 const TargetPassConfig &TPC); 970 971 /// Boilerplate computation of some initial sets, artifical blocks and 972 /// RPOT block ordering. 973 void initialSetup(MachineFunction &MF); 974 975 bool ExtendRanges(MachineFunction &MF, MachineDominatorTree *DomTree, 976 TargetPassConfig *TPC, unsigned InputBBLimit, 977 unsigned InputDbgValLimit) override; 978 979 public: 980 /// Default construct and initialize the pass. 981 InstrRefBasedLDV(); 982 983 LLVM_DUMP_METHOD 984 void dump_mloc_transfer(const MLocTransferMap &mloc_transfer) const; 985 986 bool isCalleeSaved(LocIdx L) const; 987 988 bool hasFoldedStackStore(const MachineInstr &MI) { 989 // Instruction must have a memory operand that's a stack slot, and isn't 990 // aliased, meaning it's a spill from regalloc instead of a variable. 991 // If it's aliased, we can't guarantee its value. 992 if (!MI.hasOneMemOperand()) 993 return false; 994 auto *MemOperand = *MI.memoperands_begin(); 995 return MemOperand->isStore() && 996 MemOperand->getPseudoValue() && 997 MemOperand->getPseudoValue()->kind() == PseudoSourceValue::FixedStack 998 && !MemOperand->getPseudoValue()->isAliased(MFI); 999 } 1000 1001 Optional<LocIdx> findLocationForMemOperand(const MachineInstr &MI); 1002 }; 1003 1004 } // namespace LiveDebugValues 1005 1006 #endif /* LLVM_LIB_CODEGEN_LIVEDEBUGVALUES_INSTRREFBASEDLDV_H */ 1007