AllocationOrder.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. using namespace llvm;
  20. // Compare VirtRegMap::getRegAllocPref().
  21. AllocationOrder::AllocationOrder(unsigned VirtReg,
  22. const VirtRegMap &VRM,
  23. const BitVector &ReservedRegs)
  24. : Pos(0), Reserved(ReservedRegs) {
  25. const TargetRegisterClass *RC = VRM.getRegInfo().getRegClass(VirtReg);
  26. std::pair<unsigned, unsigned> HintPair =
  27. VRM.getRegInfo().getRegAllocationHint(VirtReg);
  28. // HintPair.second is a register, phys or virt.
  29. Hint = HintPair.second;
  30. // Translate to physreg, or 0 if not assigned yet.
  31. if (Hint && TargetRegisterInfo::isVirtualRegister(Hint))
  32. Hint = VRM.getPhys(Hint);
  33. // The remaining allocation order may depend on the hint.
  34. tie(Begin, End) = VRM.getTargetRegInfo()
  35. .getAllocationOrder(RC, HintPair.first, Hint, VRM.getMachineFunction());
  36. // Target-dependent hints require resolution.
  37. if (HintPair.first)
  38. Hint = VRM.getTargetRegInfo().ResolveRegAllocHint(HintPair.first, Hint,
  39. VRM.getMachineFunction());
  40. // The hint must be a valid physreg for allocation.
  41. if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
  42. !RC->contains(Hint) || ReservedRegs.test(Hint)))
  43. Hint = 0;
  44. }
  45. unsigned AllocationOrder::next() {
  46. // First take the hint.
  47. if (!Pos) {
  48. Pos = Begin;
  49. if (Hint)
  50. return Hint;
  51. }
  52. // Then look at the order from TRI.
  53. while(Pos != End) {
  54. unsigned Reg = *Pos++;
  55. if (Reg != Hint && !Reserved.test(Reg))
  56. return Reg;
  57. }
  58. return 0;
  59. }