Add a RegisterClassInfo class that lazily caches information about
[oota-llvm.git] / lib / CodeGen / RegisterClassInfo.cpp
1 //===-- RegisterClassInfo.cpp - Dynamic Register Class Info ---------------===//
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 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.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "RegisterClassInfo.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/Target/TargetMachine.h"
20
21 using namespace llvm;
22
23 RegisterClassInfo::RegisterClassInfo() : Tag(0), TRI(0) {}
24
25 void RegisterClassInfo::runOnMachineFunction(const MachineFunction &mf) {
26   bool Update = false;
27   MF = &mf;
28
29   // Allocate new array the first time we see a new target.
30   if (MF->getTarget().getRegisterInfo() != TRI) {
31     TRI = MF->getTarget().getRegisterInfo();
32     RegClass.reset(new RCInfo[TRI->getNumRegClasses()]);
33     Update = true;
34   }
35
36   // Does this MF have different CSRs?
37   const unsigned *CSR = TRI->getCalleeSavedRegs(MF);
38   if (CSR != CalleeSaved) {
39     // Build a CSRNum map. Every CSR alias gets an entry pointing to the last
40     // overlapping CSR.
41     CSRNum.reset(new uint8_t[TRI->getNumRegs()]);
42     for (unsigned N = 0; unsigned Reg = CSR[N]; ++N)
43       for (const unsigned *AS = TRI->getOverlaps(Reg);
44            unsigned Alias = *AS; ++AS)
45         CSRNum[Alias] = N + 1; // 0 means no CSR, 1 means CalleeSaved[0], ...
46     Update = true;
47   }
48   CalleeSaved = CSR;
49
50   // Different reserved registers?
51   BitVector RR = TRI->getReservedRegs(*MF);
52   if (RR != Reserved)
53     Update = true;
54   Reserved = RR;
55
56   // Invalidate cached information from previous function.
57   if (Update)
58     ++Tag;
59 }
60
61 /// compute - Compute the preferred allocation order for RC with reserved
62 /// registers filtered out. Volatile registers come first followed by CSR
63 /// aliases ordered according to the CSR order specified by the target.
64 void RegisterClassInfo::compute(const TargetRegisterClass *RC) const {
65   RCInfo &RCI = RegClass[RC->getID()];
66
67   // Raw register count, including all reserved regs.
68   unsigned NumRegs = RC->getNumRegs();
69
70   if (!RCI.Order)
71     RCI.Order.reset(new unsigned[NumRegs]);
72
73   unsigned N = 0;
74   SmallVector<std::pair<unsigned, unsigned>, 8> CSRAlias;
75
76   // FIXME: Once targets reserve registers instead of removing them from the
77   // allocation order, we can simply use begin/end here.
78   TargetRegisterClass::iterator AOB = RC->allocation_order_begin(*MF);
79   TargetRegisterClass::iterator AOE = RC->allocation_order_end(*MF);
80
81   for (TargetRegisterClass::iterator I = AOB; I != AOE; ++I) {
82     unsigned PhysReg = *I;
83     // Remove reserved registers from the allocation order.
84     if (Reserved.test(PhysReg))
85       continue;
86     if (unsigned CSR = CSRNum[PhysReg])
87       // PhysReg aliases a CSR, save it for later.
88       CSRAlias.push_back(std::make_pair(CSR, PhysReg));
89     else
90       RCI.Order[N++] = PhysReg;
91   }
92   RCI.NumRegs = N + CSRAlias.size();
93   assert (RCI.NumRegs <= NumRegs && "Allocation order larger than regclass");
94
95   // Sort CSR aliases acording to the CSR ordering.
96   if (CSRAlias.size() >= 2)
97     array_pod_sort(CSRAlias.begin(), CSRAlias.end());
98
99   for (unsigned i = 0, e = CSRAlias.size(); i != e; ++i)
100       RCI.Order[N++] = CSRAlias[i].second;
101
102   // RCI is now up-to-date.
103   RCI.Tag = Tag;
104 }
105