1 //===-- llvm/CodeGen/AllocationOrder.cpp - Allocation Order ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements an allocation order for virtual registers.
12 // The preferred allocation order for a virtual register depends on allocation
13 // hints and target hooks. The AllocationOrder class encapsulates all of that.
15 //===----------------------------------------------------------------------===//
17 #include "AllocationOrder.h"
18 #include "llvm/CodeGen/MachineRegisterInfo.h"
19 #include "llvm/CodeGen/RegisterClassInfo.h"
20 #include "llvm/CodeGen/VirtRegMap.h"
24 // Compare VirtRegMap::getRegAllocPref().
25 AllocationOrder::AllocationOrder(unsigned VirtReg,
26 const VirtRegMap &VRM,
27 const RegisterClassInfo &RegClassInfo)
28 : Begin(0), End(0), Pos(0), RCI(RegClassInfo), OwnedBegin(false) {
29 const TargetRegisterClass *RC = VRM.getRegInfo().getRegClass(VirtReg);
30 std::pair<unsigned, unsigned> HintPair =
31 VRM.getRegInfo().getRegAllocationHint(VirtReg);
32 const MachineRegisterInfo &MRI = VRM.getRegInfo();
34 // HintPair.second is a register, phys or virt.
35 Hint = HintPair.second;
37 // Translate to physreg, or 0 if not assigned yet.
38 if (TargetRegisterInfo::isVirtualRegister(Hint))
39 Hint = VRM.getPhys(Hint);
41 // The first hint pair component indicates a target-specific hint.
43 const TargetRegisterInfo &TRI = VRM.getTargetRegInfo();
44 // The remaining allocation order may depend on the hint.
45 ArrayRef<MCPhysReg> Order =
46 TRI.getRawAllocationOrder(RC, HintPair.first, Hint,
47 VRM.getMachineFunction());
51 // Copy the allocation order with reserved registers removed.
53 MCPhysReg *P = new MCPhysReg[Order.size()];
55 for (unsigned i = 0; i != Order.size(); ++i)
56 if (!MRI.isReserved(Order[i]))
60 // Target-dependent hints require resolution.
61 Hint = TRI.ResolveRegAllocHint(HintPair.first, Hint,
62 VRM.getMachineFunction());
64 // If there is no hint or just a normal hint, use the cached allocation
65 // order from RegisterClassInfo.
66 ArrayRef<MCPhysReg> O = RCI.getOrder(RC);
71 // The hint must be a valid physreg for allocation.
72 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
73 !RC->contains(Hint) || MRI.isReserved(Hint)))
77 AllocationOrder::~AllocationOrder() {