AllocationOrder.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //===-- llvm/CodeGen/AllocationOrder.cpp - Allocation Order ---------------===//
  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 an allocation order for virtual registers.
  11. //
  12. // The preferred allocation order for a virtual register depends on allocation
  13. // hints and target hooks. The AllocationOrder class encapsulates all of that.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "AllocationOrder.h"
  17. #include "VirtRegMap.h"
  18. #include "llvm/CodeGen/MachineRegisterInfo.h"
  19. #include "llvm/CodeGen/RegisterClassInfo.h"
  20. using namespace llvm;
  21. // Compare VirtRegMap::getRegAllocPref().
  22. AllocationOrder::AllocationOrder(unsigned VirtReg,
  23. const VirtRegMap &VRM,
  24. const RegisterClassInfo &RegClassInfo)
  25. : Begin(0), End(0), Pos(0), RCI(RegClassInfo), OwnedBegin(false) {
  26. const TargetRegisterClass *RC = VRM.getRegInfo().getRegClass(VirtReg);
  27. std::pair<unsigned, unsigned> HintPair =
  28. VRM.getRegInfo().getRegAllocationHint(VirtReg);
  29. const MachineRegisterInfo &MRI = VRM.getRegInfo();
  30. // HintPair.second is a register, phys or virt.
  31. Hint = HintPair.second;
  32. // Translate to physreg, or 0 if not assigned yet.
  33. if (TargetRegisterInfo::isVirtualRegister(Hint))
  34. Hint = VRM.getPhys(Hint);
  35. // The first hint pair component indicates a target-specific hint.
  36. if (HintPair.first) {
  37. const TargetRegisterInfo &TRI = VRM.getTargetRegInfo();
  38. // The remaining allocation order may depend on the hint.
  39. ArrayRef<uint16_t> Order =
  40. TRI.getRawAllocationOrder(RC, HintPair.first, Hint,
  41. VRM.getMachineFunction());
  42. if (Order.empty())
  43. return;
  44. // Copy the allocation order with reserved registers removed.
  45. OwnedBegin = true;
  46. unsigned *P = new unsigned[Order.size()];
  47. Begin = P;
  48. for (unsigned i = 0; i != Order.size(); ++i)
  49. if (!MRI.isReserved(Order[i]))
  50. *P++ = Order[i];
  51. End = P;
  52. // Target-dependent hints require resolution.
  53. Hint = TRI.ResolveRegAllocHint(HintPair.first, Hint,
  54. VRM.getMachineFunction());
  55. } else {
  56. // If there is no hint or just a normal hint, use the cached allocation
  57. // order from RegisterClassInfo.
  58. ArrayRef<unsigned> O = RCI.getOrder(RC);
  59. Begin = O.begin();
  60. End = O.end();
  61. }
  62. // The hint must be a valid physreg for allocation.
  63. if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
  64. !RC->contains(Hint) || MRI.isReserved(Hint)))
  65. Hint = 0;
  66. }
  67. AllocationOrder::~AllocationOrder() {
  68. if (OwnedBegin)
  69. delete [] Begin;
  70. }