TargetRegisterInfo.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //===- TargetRegisterInfo.cpp - Target Register Information Implementation ===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the TargetRegisterInfo interface.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Target/TargetMachine.h"
  14. #include "llvm/Target/TargetRegisterInfo.h"
  15. #include "llvm/ADT/BitVector.h"
  16. #include "llvm/Support/raw_ostream.h"
  17. using namespace llvm;
  18. TargetRegisterInfo::TargetRegisterInfo(const TargetRegisterInfoDesc *ID,
  19. regclass_iterator RCB, regclass_iterator RCE,
  20. const char *const *subregindexnames)
  21. : InfoDesc(ID), SubRegIndexNames(subregindexnames),
  22. RegClassBegin(RCB), RegClassEnd(RCE) {
  23. }
  24. TargetRegisterInfo::~TargetRegisterInfo() {}
  25. void PrintReg::print(raw_ostream &OS) const {
  26. if (!Reg)
  27. OS << "%noreg";
  28. else if (TargetRegisterInfo::isStackSlot(Reg))
  29. OS << "SS#" << TargetRegisterInfo::stackSlot2Index(Reg);
  30. else if (TargetRegisterInfo::isVirtualRegister(Reg))
  31. OS << "%vreg" << TargetRegisterInfo::virtReg2Index(Reg);
  32. else if (TRI && Reg < TRI->getNumRegs())
  33. OS << '%' << TRI->getName(Reg);
  34. else
  35. OS << "%physreg" << Reg;
  36. if (SubIdx) {
  37. if (TRI)
  38. OS << ':' << TRI->getSubRegIndexName(SubIdx);
  39. else
  40. OS << ":sub(" << SubIdx << ')';
  41. }
  42. }
  43. /// getMinimalPhysRegClass - Returns the Register Class of a physical
  44. /// register of the given type, picking the most sub register class of
  45. /// the right type that contains this physreg.
  46. const TargetRegisterClass *
  47. TargetRegisterInfo::getMinimalPhysRegClass(unsigned reg, EVT VT) const {
  48. assert(isPhysicalRegister(reg) && "reg must be a physical register");
  49. // Pick the most sub register class of the right type that contains
  50. // this physreg.
  51. const TargetRegisterClass* BestRC = 0;
  52. for (regclass_iterator I = regclass_begin(), E = regclass_end(); I != E; ++I){
  53. const TargetRegisterClass* RC = *I;
  54. if ((VT == MVT::Other || RC->hasType(VT)) && RC->contains(reg) &&
  55. (!BestRC || BestRC->hasSubClass(RC)))
  56. BestRC = RC;
  57. }
  58. assert(BestRC && "Couldn't find the register class");
  59. return BestRC;
  60. }
  61. /// getAllocatableSetForRC - Toggle the bits that represent allocatable
  62. /// registers for the specific register class.
  63. static void getAllocatableSetForRC(const MachineFunction &MF,
  64. const TargetRegisterClass *RC, BitVector &R){
  65. ArrayRef<uint16_t> Order = RC->getRawAllocationOrder(MF);
  66. for (unsigned i = 0; i != Order.size(); ++i)
  67. R.set(Order[i]);
  68. }
  69. BitVector TargetRegisterInfo::getAllocatableSet(const MachineFunction &MF,
  70. const TargetRegisterClass *RC) const {
  71. BitVector Allocatable(getNumRegs());
  72. if (RC) {
  73. getAllocatableSetForRC(MF, RC, Allocatable);
  74. } else {
  75. for (TargetRegisterInfo::regclass_iterator I = regclass_begin(),
  76. E = regclass_end(); I != E; ++I)
  77. if ((*I)->isAllocatable())
  78. getAllocatableSetForRC(MF, *I, Allocatable);
  79. }
  80. // Mask out the reserved registers
  81. BitVector Reserved = getReservedRegs(MF);
  82. Allocatable &= Reserved.flip();
  83. return Allocatable;
  84. }
  85. const TargetRegisterClass *
  86. TargetRegisterInfo::getCommonSubClass(const TargetRegisterClass *A,
  87. const TargetRegisterClass *B) const {
  88. // First take care of the trivial cases.
  89. if (A == B)
  90. return A;
  91. if (!A || !B)
  92. return 0;
  93. // Register classes are ordered topologically, so the largest common
  94. // sub-class it the common sub-class with the smallest ID.
  95. const unsigned *SubA = A->getSubClassMask();
  96. const unsigned *SubB = B->getSubClassMask();
  97. // We could start the search from max(A.ID, B.ID), but we are only going to
  98. // execute 2-3 iterations anyway.
  99. for (unsigned Base = 0, BaseE = getNumRegClasses(); Base < BaseE; Base += 32)
  100. if (unsigned Common = *SubA++ & *SubB++)
  101. return getRegClass(Base + CountTrailingZeros_32(Common));
  102. // No common sub-class exists.
  103. return NULL;
  104. }