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