Jakob Stoklund Olesen | c9672cb | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 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 | |
| 17 | #include "AllocationOrder.h" |
Jakob Stoklund Olesen | 5f2316a | 2011-06-03 20:34:53 +0000 | [diff] [blame] | 18 | #include "RegisterClassInfo.h" |
Jakob Stoklund Olesen | c9672cb | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 19 | #include "VirtRegMap.h" |
| 20 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 21 | |
| 22 | using namespace llvm; |
| 23 | |
| 24 | // Compare VirtRegMap::getRegAllocPref(). |
| 25 | AllocationOrder::AllocationOrder(unsigned VirtReg, |
| 26 | const VirtRegMap &VRM, |
Jakob Stoklund Olesen | 5f2316a | 2011-06-03 20:34:53 +0000 | [diff] [blame] | 27 | const RegisterClassInfo &RegClassInfo) |
Jakob Stoklund Olesen | a46a100 | 2011-06-06 21:02:04 +0000 | [diff] [blame] | 28 | : Begin(0), End(0), Pos(0), RCI(RegClassInfo), OwnedBegin(false) { |
Jakob Stoklund Olesen | c9672cb | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 29 | const TargetRegisterClass *RC = VRM.getRegInfo().getRegClass(VirtReg); |
| 30 | std::pair<unsigned, unsigned> HintPair = |
| 31 | VRM.getRegInfo().getRegAllocationHint(VirtReg); |
| 32 | |
| 33 | // HintPair.second is a register, phys or virt. |
| 34 | Hint = HintPair.second; |
| 35 | |
| 36 | // Translate to physreg, or 0 if not assigned yet. |
Jakob Stoklund Olesen | c9df025 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 37 | if (TargetRegisterInfo::isVirtualRegister(Hint)) |
Jakob Stoklund Olesen | c9672cb | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 38 | Hint = VRM.getPhys(Hint); |
| 39 | |
Jakob Stoklund Olesen | a46a100 | 2011-06-06 21:02:04 +0000 | [diff] [blame] | 40 | // The first hint pair component indicates a target-specific hint. |
| 41 | if (HintPair.first) { |
| 42 | const TargetRegisterInfo &TRI = VRM.getTargetRegInfo(); |
| 43 | // The remaining allocation order may depend on the hint. |
Jakob Stoklund Olesen | dd5a847 | 2011-06-16 23:31:16 +0000 | [diff] [blame^] | 44 | ArrayRef<unsigned> Order = |
| 45 | TRI.getRawAllocationOrder(RC, HintPair.first, Hint, |
| 46 | VRM.getMachineFunction()); |
| 47 | if (Order.empty()) |
Jakob Stoklund Olesen | a46a100 | 2011-06-06 21:02:04 +0000 | [diff] [blame] | 48 | return; |
| 49 | |
| 50 | // Copy the allocation order with reserved registers removed. |
| 51 | OwnedBegin = true; |
Jakob Stoklund Olesen | dd5a847 | 2011-06-16 23:31:16 +0000 | [diff] [blame^] | 52 | unsigned *P = new unsigned[Order.size()]; |
Jakob Stoklund Olesen | a46a100 | 2011-06-06 21:02:04 +0000 | [diff] [blame] | 53 | Begin = P; |
Jakob Stoklund Olesen | dd5a847 | 2011-06-16 23:31:16 +0000 | [diff] [blame^] | 54 | for (unsigned i = 0; i != Order.size(); ++i) |
| 55 | if (!RCI.isReserved(Order[i])) |
| 56 | *P++ = Order[i]; |
Jakob Stoklund Olesen | a46a100 | 2011-06-06 21:02:04 +0000 | [diff] [blame] | 57 | End = P; |
| 58 | |
| 59 | // Target-dependent hints require resolution. |
| 60 | Hint = TRI.ResolveRegAllocHint(HintPair.first, Hint, |
| 61 | VRM.getMachineFunction()); |
| 62 | } else { |
| 63 | // If there is no hint or just a normal hint, use the cached allocation |
| 64 | // order from RegisterClassInfo. |
| 65 | ArrayRef<unsigned> O = RCI.getOrder(RC); |
| 66 | Begin = O.begin(); |
| 67 | End = O.end(); |
| 68 | } |
Jakob Stoklund Olesen | c9672cb | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 69 | |
| 70 | // The hint must be a valid physreg for allocation. |
| 71 | if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) || |
Jakob Stoklund Olesen | 5f2316a | 2011-06-03 20:34:53 +0000 | [diff] [blame] | 72 | !RC->contains(Hint) || RCI.isReserved(Hint))) |
Jakob Stoklund Olesen | c9672cb | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 73 | Hint = 0; |
Jakob Stoklund Olesen | c9672cb | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Jakob Stoklund Olesen | a46a100 | 2011-06-06 21:02:04 +0000 | [diff] [blame] | 76 | AllocationOrder::~AllocationOrder() { |
| 77 | if (OwnedBegin) |
| 78 | delete [] Begin; |
Jakob Stoklund Olesen | c9672cb | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 79 | } |