1 //===-- RegisterClassInfo.cpp - Dynamic Register Class Info ---------------===//
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 the RegisterClassInfo class which provides dynamic
11 // information about target register classes. Callee saved and reserved
12 // registers depends on calling conventions and other dynamic information, so
13 // some things cannot be determined statically.
15 //===----------------------------------------------------------------------===//
17 #include "RegisterClassInfo.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/Target/TargetMachine.h"
23 RegisterClassInfo::RegisterClassInfo() : Tag(0), MF(0), TRI(0), CalleeSaved(0)
26 void RegisterClassInfo::runOnMachineFunction(const MachineFunction &mf) {
30 // Allocate new array the first time we see a new target.
31 if (MF->getTarget().getRegisterInfo() != TRI) {
32 TRI = MF->getTarget().getRegisterInfo();
33 RegClass.reset(new RCInfo[TRI->getNumRegClasses()]);
37 // Does this MF have different CSRs?
38 const unsigned *CSR = TRI->getCalleeSavedRegs(MF);
39 if (Update || CSR != CalleeSaved) {
40 // Build a CSRNum map. Every CSR alias gets an entry pointing to the last
42 CSRNum.reset(new uint8_t[TRI->getNumRegs()]);
43 for (unsigned N = 0; unsigned Reg = CSR[N]; ++N)
44 for (const unsigned *AS = TRI->getOverlaps(Reg);
45 unsigned Alias = *AS; ++AS)
46 CSRNum[Alias] = N + 1; // 0 means no CSR, 1 means CalleeSaved[0], ...
51 // Different reserved registers?
52 BitVector RR = TRI->getReservedRegs(*MF);
57 // Invalidate cached information from previous function.
62 /// compute - Compute the preferred allocation order for RC with reserved
63 /// registers filtered out. Volatile registers come first followed by CSR
64 /// aliases ordered according to the CSR order specified by the target.
65 void RegisterClassInfo::compute(const TargetRegisterClass *RC) const {
66 RCInfo &RCI = RegClass[RC->getID()];
68 // Raw register count, including all reserved regs.
69 unsigned NumRegs = RC->getNumRegs();
72 RCI.Order.reset(new unsigned[NumRegs]);
75 SmallVector<std::pair<unsigned, unsigned>, 8> CSRAlias;
77 // FIXME: Once targets reserve registers instead of removing them from the
78 // allocation order, we can simply use begin/end here.
79 TargetRegisterClass::iterator AOB = RC->allocation_order_begin(*MF);
80 TargetRegisterClass::iterator AOE = RC->allocation_order_end(*MF);
82 for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
83 unsigned PhysReg = *I;
84 // Remove reserved registers from the allocation order.
85 if (Reserved.test(PhysReg))
87 if (unsigned CSR = CSRNum[PhysReg])
88 // PhysReg aliases a CSR, save it for later.
89 CSRAlias.push_back(std::make_pair(CSR, PhysReg));
91 RCI.Order[N++] = PhysReg;
93 RCI.NumRegs = N + CSRAlias.size();
94 assert (RCI.NumRegs <= NumRegs && "Allocation order larger than regclass");
96 // Sort CSR aliases acording to the CSR ordering.
97 if (CSRAlias.size() >= 2)
98 array_pod_sort(CSRAlias.begin(), CSRAlias.end());
100 for (unsigned i = 0, e = CSRAlias.size(); i != e; ++i)
101 RCI.Order[N++] = CSRAlias[i].second;
103 // RCI is now up-to-date.