RegisterAliasing.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //===-- RegisterAliasing.cpp ------------------------------------*- 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. #include "RegisterAliasing.h"
  9. namespace llvm {
  10. namespace exegesis {
  11. BitVector getAliasedBits(const MCRegisterInfo &RegInfo,
  12. const BitVector &SourceBits) {
  13. BitVector AliasedBits(RegInfo.getNumRegs());
  14. for (const size_t PhysReg : SourceBits.set_bits()) {
  15. using RegAliasItr = MCRegAliasIterator;
  16. for (auto Itr = RegAliasItr(PhysReg, &RegInfo, true); Itr.isValid();
  17. ++Itr) {
  18. AliasedBits.set(*Itr);
  19. }
  20. }
  21. return AliasedBits;
  22. }
  23. RegisterAliasingTracker::RegisterAliasingTracker(const MCRegisterInfo &RegInfo)
  24. : SourceBits(RegInfo.getNumRegs()), AliasedBits(RegInfo.getNumRegs()),
  25. Origins(RegInfo.getNumRegs()) {}
  26. RegisterAliasingTracker::RegisterAliasingTracker(
  27. const MCRegisterInfo &RegInfo, const BitVector &ReservedReg,
  28. const MCRegisterClass &RegClass)
  29. : RegisterAliasingTracker(RegInfo) {
  30. for (MCPhysReg PhysReg : RegClass)
  31. if (!ReservedReg[PhysReg]) // Removing reserved registers.
  32. SourceBits.set(PhysReg);
  33. FillOriginAndAliasedBits(RegInfo, SourceBits);
  34. }
  35. RegisterAliasingTracker::RegisterAliasingTracker(const MCRegisterInfo &RegInfo,
  36. const MCPhysReg PhysReg)
  37. : RegisterAliasingTracker(RegInfo) {
  38. SourceBits.set(PhysReg);
  39. FillOriginAndAliasedBits(RegInfo, SourceBits);
  40. }
  41. void RegisterAliasingTracker::FillOriginAndAliasedBits(
  42. const MCRegisterInfo &RegInfo, const BitVector &SourceBits) {
  43. using RegAliasItr = MCRegAliasIterator;
  44. for (const size_t PhysReg : SourceBits.set_bits()) {
  45. for (auto Itr = RegAliasItr(PhysReg, &RegInfo, true); Itr.isValid();
  46. ++Itr) {
  47. AliasedBits.set(*Itr);
  48. Origins[*Itr] = PhysReg;
  49. }
  50. }
  51. }
  52. RegisterAliasingTrackerCache::RegisterAliasingTrackerCache(
  53. const MCRegisterInfo &RegInfo, const BitVector &ReservedReg)
  54. : RegInfo(RegInfo), ReservedReg(ReservedReg),
  55. EmptyRegisters(RegInfo.getNumRegs()) {}
  56. const RegisterAliasingTracker &
  57. RegisterAliasingTrackerCache::getRegister(MCPhysReg PhysReg) const {
  58. auto &Found = Registers[PhysReg];
  59. if (!Found)
  60. Found.reset(new RegisterAliasingTracker(RegInfo, PhysReg));
  61. return *Found;
  62. }
  63. const RegisterAliasingTracker &
  64. RegisterAliasingTrackerCache::getRegisterClass(unsigned RegClassIndex) const {
  65. auto &Found = RegisterClasses[RegClassIndex];
  66. const auto &RegClass = RegInfo.getRegClass(RegClassIndex);
  67. if (!Found)
  68. Found.reset(new RegisterAliasingTracker(RegInfo, ReservedReg, RegClass));
  69. return *Found;
  70. }
  71. std::string debugString(const MCRegisterInfo &RegInfo, const BitVector &Regs) {
  72. std::string Result;
  73. for (const unsigned Reg : Regs.set_bits()) {
  74. Result.append(RegInfo.getName(Reg));
  75. Result.push_back(' ');
  76. }
  77. return Result;
  78. }
  79. } // namespace exegesis
  80. } // namespace llvm