blob: bad4912e72231f6b7aecf56857f0097d7ecfdd45 [file] [log] [blame]
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +00001//===-- 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 Olesenc9672cb2010-12-10 18:36:02 +000018#include "llvm/CodeGen/MachineRegisterInfo.h"
Andrew Trick15252602012-06-06 20:29:31 +000019#include "llvm/CodeGen/RegisterClassInfo.h"
Jakob Stoklund Olesen1ead68d2012-11-28 19:13:06 +000020#include "llvm/CodeGen/VirtRegMap.h"
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000021
22using namespace llvm;
23
24// Compare VirtRegMap::getRegAllocPref().
25AllocationOrder::AllocationOrder(unsigned VirtReg,
26 const VirtRegMap &VRM,
Jakob Stoklund Olesen5f2316a2011-06-03 20:34:53 +000027 const RegisterClassInfo &RegClassInfo)
Jakob Stoklund Olesena46a1002011-06-06 21:02:04 +000028 : Begin(0), End(0), Pos(0), RCI(RegClassInfo), OwnedBegin(false) {
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000029 const TargetRegisterClass *RC = VRM.getRegInfo().getRegClass(VirtReg);
30 std::pair<unsigned, unsigned> HintPair =
31 VRM.getRegInfo().getRegAllocationHint(VirtReg);
Jakob Stoklund Olesen14d1dd92012-10-15 22:41:03 +000032 const MachineRegisterInfo &MRI = VRM.getRegInfo();
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000033
34 // HintPair.second is a register, phys or virt.
35 Hint = HintPair.second;
36
37 // Translate to physreg, or 0 if not assigned yet.
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +000038 if (TargetRegisterInfo::isVirtualRegister(Hint))
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000039 Hint = VRM.getPhys(Hint);
40
Jakob Stoklund Olesena46a1002011-06-06 21:02:04 +000041 // The first hint pair component indicates a target-specific hint.
42 if (HintPair.first) {
43 const TargetRegisterInfo &TRI = VRM.getTargetRegInfo();
44 // The remaining allocation order may depend on the hint.
Jakob Stoklund Olesen39b5c0c2012-11-29 03:34:17 +000045 ArrayRef<MCPhysReg> Order =
Jakob Stoklund Olesendd5a8472011-06-16 23:31:16 +000046 TRI.getRawAllocationOrder(RC, HintPair.first, Hint,
47 VRM.getMachineFunction());
48 if (Order.empty())
Jakob Stoklund Olesena46a1002011-06-06 21:02:04 +000049 return;
50
51 // Copy the allocation order with reserved registers removed.
52 OwnedBegin = true;
Jakob Stoklund Olesen39b5c0c2012-11-29 03:34:17 +000053 MCPhysReg *P = new MCPhysReg[Order.size()];
Jakob Stoklund Olesena46a1002011-06-06 21:02:04 +000054 Begin = P;
Jakob Stoklund Olesendd5a8472011-06-16 23:31:16 +000055 for (unsigned i = 0; i != Order.size(); ++i)
Jakob Stoklund Olesen14d1dd92012-10-15 22:41:03 +000056 if (!MRI.isReserved(Order[i]))
Jakob Stoklund Olesendd5a8472011-06-16 23:31:16 +000057 *P++ = Order[i];
Jakob Stoklund Olesena46a1002011-06-06 21:02:04 +000058 End = P;
59
60 // Target-dependent hints require resolution.
61 Hint = TRI.ResolveRegAllocHint(HintPair.first, Hint,
62 VRM.getMachineFunction());
63 } else {
64 // If there is no hint or just a normal hint, use the cached allocation
65 // order from RegisterClassInfo.
Jakob Stoklund Olesen39b5c0c2012-11-29 03:34:17 +000066 ArrayRef<MCPhysReg> O = RCI.getOrder(RC);
Jakob Stoklund Olesena46a1002011-06-06 21:02:04 +000067 Begin = O.begin();
68 End = O.end();
69 }
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000070
71 // The hint must be a valid physreg for allocation.
72 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
Jakob Stoklund Olesen14d1dd92012-10-15 22:41:03 +000073 !RC->contains(Hint) || MRI.isReserved(Hint)))
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000074 Hint = 0;
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000075}
76
Jakob Stoklund Olesena46a1002011-06-06 21:02:04 +000077AllocationOrder::~AllocationOrder() {
78 if (OwnedBegin)
79 delete [] Begin;
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000080}