AllocationOrder.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 "RegisterClassInfo.h"
  18. #include "VirtRegMap.h"
  19. #include "llvm/CodeGen/MachineRegisterInfo.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. // HintPair.second is a register, phys or virt.
  30. Hint = HintPair.second;
  31. // Translate to physreg, or 0 if not assigned yet.
  32. if (TargetRegisterInfo::isVirtualRegister(Hint))
  33. Hint = VRM.getPhys(Hint);
  34. // The first hint pair component indicates a target-specific hint.
  35. if (HintPair.first) {
  36. const TargetRegisterInfo &TRI = VRM.getTargetRegInfo();
  37. // The remaining allocation order may depend on the hint.
  38. ArrayRef<uint16_t> Order =
  39. TRI.getRawAllocationOrder(RC, HintPair.first, Hint,
  40. VRM.getMachineFunction());
  41. if (Order.empty())
  42. return;
  43. // Copy the allocation order with reserved registers removed.
  44. OwnedBegin = true;
  45. unsigned *P = new unsigned[Order.size()];
  46. Begin = P;
  47. for (unsigned i = 0; i != Order.size(); ++i)
  48. if (!RCI.isReserved(Order[i]))
  49. *P++ = Order[i];
  50. End = P;
  51. // Target-dependent hints require resolution.
  52. Hint = TRI.ResolveRegAllocHint(HintPair.first, Hint,
  53. VRM.getMachineFunction());
  54. } else {
  55. // If there is no hint or just a normal hint, use the cached allocation
  56. // order from RegisterClassInfo.
  57. ArrayRef<unsigned> O = RCI.getOrder(RC);
  58. Begin = O.begin();
  59. End = O.end();
  60. }
  61. // The hint must be a valid physreg for allocation.
  62. if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
  63. !RC->contains(Hint) || RCI.isReserved(Hint)))
  64. Hint = 0;
  65. }
  66. AllocationOrder::~AllocationOrder() {
  67. if (OwnedBegin)
  68. delete [] Begin;
  69. }