RegisterAliasing.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //===-- RegisterAliasingTracker.h -------------------------------*- C++ -*-===//
  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. /// \file
  10. /// Defines classes to keep track of register aliasing.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_TOOLS_LLVM_EXEGESIS_ALIASINGTRACKER_H
  14. #define LLVM_TOOLS_LLVM_EXEGESIS_ALIASINGTRACKER_H
  15. #include <memory>
  16. #include <unordered_map>
  17. #include "llvm/ADT/BitVector.h"
  18. #include "llvm/ADT/PackedVector.h"
  19. #include "llvm/MC/MCRegisterInfo.h"
  20. namespace llvm {
  21. namespace exegesis {
  22. // Returns the registers that are aliased by the ones set in SourceBits.
  23. BitVector getAliasedBits(const MCRegisterInfo &RegInfo,
  24. const BitVector &SourceBits);
  25. // Keeps track of a mapping from one register (or a register class) to its
  26. // aliased registers.
  27. //
  28. // e.g.
  29. // RegisterAliasingTracker Tracker(RegInfo, X86::EAX);
  30. // Tracker.sourceBits() == { X86::EAX }
  31. // Tracker.aliasedBits() == { X86::AL, X86::AH, X86::AX,
  32. // X86::EAX,X86::HAX, X86::RAX }
  33. // Tracker.getOrigin(X86::AL) == X86::EAX;
  34. // Tracker.getOrigin(X86::BX) == -1;
  35. struct RegisterAliasingTracker {
  36. // Construct a tracker from an MCRegisterClass.
  37. RegisterAliasingTracker(const MCRegisterInfo &RegInfo,
  38. const BitVector &ReservedReg,
  39. const MCRegisterClass &RegClass);
  40. // Construct a tracker from an MCPhysReg.
  41. RegisterAliasingTracker(const MCRegisterInfo &RegInfo,
  42. const MCPhysReg Register);
  43. const BitVector &sourceBits() const { return SourceBits; }
  44. // Retrieves all the touched registers as a BitVector.
  45. const BitVector &aliasedBits() const { return AliasedBits; }
  46. // Returns the origin of this register or -1.
  47. int getOrigin(MCPhysReg Aliased) const {
  48. if (!AliasedBits[Aliased])
  49. return -1;
  50. return Origins[Aliased];
  51. }
  52. private:
  53. RegisterAliasingTracker(const MCRegisterInfo &RegInfo);
  54. RegisterAliasingTracker(const RegisterAliasingTracker &) = delete;
  55. void FillOriginAndAliasedBits(const MCRegisterInfo &RegInfo,
  56. const BitVector &OriginalBits);
  57. BitVector SourceBits;
  58. BitVector AliasedBits;
  59. PackedVector<size_t, 10> Origins; // Max 1024 physical registers.
  60. };
  61. // A cache of existing trackers.
  62. struct RegisterAliasingTrackerCache {
  63. // RegInfo must outlive the cache.
  64. RegisterAliasingTrackerCache(const MCRegisterInfo &RegInfo,
  65. const BitVector &ReservedReg);
  66. // Convenient function to retrieve a BitVector of the right size.
  67. const BitVector &emptyRegisters() const { return EmptyRegisters; }
  68. // Convenient function to retrieve the registers the function body can't use.
  69. const BitVector &reservedRegisters() const { return ReservedReg; }
  70. // Convenient function to retrieve the underlying MCRegInfo.
  71. const MCRegisterInfo &regInfo() const { return RegInfo; }
  72. // Retrieves the RegisterAliasingTracker for this particular register.
  73. const RegisterAliasingTracker &getRegister(MCPhysReg Reg) const;
  74. // Retrieves the RegisterAliasingTracker for this particular register class.
  75. const RegisterAliasingTracker &getRegisterClass(unsigned RegClassIndex) const;
  76. private:
  77. const MCRegisterInfo &RegInfo;
  78. const BitVector ReservedReg;
  79. const BitVector EmptyRegisters;
  80. mutable std::unordered_map<unsigned, std::unique_ptr<RegisterAliasingTracker>>
  81. Registers;
  82. mutable std::unordered_map<unsigned, std::unique_ptr<RegisterAliasingTracker>>
  83. RegisterClasses;
  84. };
  85. // `a = a & ~b`, optimized for few bit sets in B and no allocation.
  86. inline void remove(BitVector &A, const BitVector &B) {
  87. assert(A.size() == B.size());
  88. for (auto I : B.set_bits())
  89. A.reset(I);
  90. }
  91. // Returns a debug string for the list of registers.
  92. std::string debugString(const MCRegisterInfo &RegInfo, const BitVector &Regs);
  93. } // namespace exegesis
  94. } // namespace llvm
  95. #endif // LLVM_TOOLS_LLVM_EXEGESIS_ALIASINGTRACKER_H