4acbe729d74c9c38442cf5d00f4fa1890327d81e
[oota-llvm.git] / lib / Target / TargetRegisterInfo.cpp
1 //===- TargetRegisterInfo.cpp - Target Register Information Implementation ===//
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 TargetRegisterInfo interface.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Target/TargetMachine.h"
15 #include "llvm/Target/TargetRegisterInfo.h"
16 #include "llvm/ADT/BitVector.h"
17 #include "llvm/Support/raw_ostream.h"
18
19 using namespace llvm;
20
21 TargetRegisterInfo::TargetRegisterInfo(const TargetRegisterInfoDesc *ID,
22                              regclass_iterator RCB, regclass_iterator RCE,
23                              const char *const *subregindexnames)
24   : InfoDesc(ID), SubRegIndexNames(subregindexnames),
25     RegClassBegin(RCB), RegClassEnd(RCE) {
26 }
27
28 TargetRegisterInfo::~TargetRegisterInfo() {}
29
30 void PrintReg::print(raw_ostream &OS) const {
31   if (!Reg)
32     OS << "%noreg";
33   else if (TargetRegisterInfo::isStackSlot(Reg))
34     OS << "SS#" << TargetRegisterInfo::stackSlot2Index(Reg);
35   else if (TargetRegisterInfo::isVirtualRegister(Reg))
36     OS << "%vreg" << TargetRegisterInfo::virtReg2Index(Reg);
37   else if (TRI && Reg < TRI->getNumRegs())
38     OS << '%' << TRI->getName(Reg);
39   else
40     OS << "%physreg" << Reg;
41   if (SubIdx) {
42     if (TRI)
43       OS << ':' << TRI->getSubRegIndexName(SubIdx);
44     else
45       OS << ":sub(" << SubIdx << ')';
46   }
47 }
48
49 /// getAllocatableClass - Return the maximal subclass of the given register
50 /// class that is alloctable, or NULL.
51 const TargetRegisterClass *
52 TargetRegisterInfo::getAllocatableClass(const TargetRegisterClass *RC) const {
53   if (!RC || RC->isAllocatable())
54     return RC;
55
56   const unsigned *SubClass = RC->getSubClassMask();
57   for (unsigned Base = 0, BaseE = getNumRegClasses();
58        Base < BaseE; Base += 32) {
59     unsigned Idx = Base;
60     for (unsigned Mask = *SubClass++; Mask; Mask >>= 1) {
61       unsigned Offset = CountTrailingZeros_32(Mask);
62       const TargetRegisterClass *SubRC = getRegClass(Idx + Offset);
63       if (SubRC->isAllocatable())
64         return SubRC;
65       Mask >>= Offset;
66       Idx += Offset + 1;
67     }
68   }
69   return NULL;
70 }
71
72 /// getMinimalPhysRegClass - Returns the Register Class of a physical
73 /// register of the given type, picking the most sub register class of
74 /// the right type that contains this physreg.
75 const TargetRegisterClass *
76 TargetRegisterInfo::getMinimalPhysRegClass(unsigned reg, EVT VT) const {
77   assert(isPhysicalRegister(reg) && "reg must be a physical register");
78
79   // Pick the most sub register class of the right type that contains
80   // this physreg.
81   const TargetRegisterClass* BestRC = 0;
82   for (regclass_iterator I = regclass_begin(), E = regclass_end(); I != E; ++I){
83     const TargetRegisterClass* RC = *I;
84     if ((VT == MVT::Other || RC->hasType(VT)) && RC->contains(reg) &&
85         (!BestRC || BestRC->hasSubClass(RC)))
86       BestRC = RC;
87   }
88
89   assert(BestRC && "Couldn't find the register class");
90   return BestRC;
91 }
92
93 /// getAllocatableSetForRC - Toggle the bits that represent allocatable
94 /// registers for the specific register class.
95 static void getAllocatableSetForRC(const MachineFunction &MF,
96                                    const TargetRegisterClass *RC, BitVector &R){
97   assert(RC->isAllocatable() && "invalid for nonallocatable sets");
98   ArrayRef<uint16_t> Order = RC->getRawAllocationOrder(MF);
99   for (unsigned i = 0; i != Order.size(); ++i)
100     R.set(Order[i]);
101 }
102
103 BitVector TargetRegisterInfo::getAllocatableSet(const MachineFunction &MF,
104                                           const TargetRegisterClass *RC) const {
105   BitVector Allocatable(getNumRegs());
106   if (RC) {
107     // A register class with no allocatable subclass returns an empty set.
108     const TargetRegisterClass *SubClass = getAllocatableClass(RC);
109     if (SubClass)
110       getAllocatableSetForRC(MF, SubClass, Allocatable);
111   } else {
112     for (TargetRegisterInfo::regclass_iterator I = regclass_begin(),
113          E = regclass_end(); I != E; ++I)
114       if ((*I)->isAllocatable())
115         getAllocatableSetForRC(MF, *I, Allocatable);
116   }
117
118   // Mask out the reserved registers
119   BitVector Reserved = getReservedRegs(MF);
120   Allocatable &= Reserved.flip();
121
122   return Allocatable;
123 }
124
125 static inline
126 const TargetRegisterClass *firstCommonClass(const uint32_t *A,
127                                             const uint32_t *B,
128                                             const TargetRegisterInfo *TRI) {
129   for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; I += 32)
130     if (unsigned Common = *A++ & *B++)
131       return TRI->getRegClass(I + CountTrailingZeros_32(Common));
132   return 0;
133 }
134
135 const TargetRegisterClass *
136 TargetRegisterInfo::getCommonSubClass(const TargetRegisterClass *A,
137                                       const TargetRegisterClass *B) const {
138   // First take care of the trivial cases.
139   if (A == B)
140     return A;
141   if (!A || !B)
142     return 0;
143
144   // Register classes are ordered topologically, so the largest common
145   // sub-class it the common sub-class with the smallest ID.
146   const unsigned *SubA = A->getSubClassMask();
147   const unsigned *SubB = B->getSubClassMask();
148
149   // We could start the search from max(A.ID, B.ID), but we are only going to
150   // execute 2-3 iterations anyway.
151   for (unsigned Base = 0, BaseE = getNumRegClasses(); Base < BaseE; Base += 32)
152     if (unsigned Common = *SubA++ & *SubB++)
153       return getRegClass(Base + CountTrailingZeros_32(Common));
154
155   // No common sub-class exists.
156   return NULL;
157 }
158
159 const TargetRegisterClass *
160 TargetRegisterInfo::getMatchingSuperRegClass(const TargetRegisterClass *A,
161                                              const TargetRegisterClass *B,
162                                              unsigned Idx) const {
163   assert(A && B && "Missing register class");
164   assert(Idx && "Bad sub-register index");
165
166   // Find Idx in the list of super-register indices.
167   const uint32_t *Mask = 0;
168   for (SuperRegClassIterator RCI(B, this); RCI.isValid(); ++RCI)
169     if (RCI.getSubReg() == Idx) {
170       Mask = RCI.getMask();
171       break;
172     }
173   if (!Mask)
174     return 0;
175
176   // The bit mask contains all register classes that are projected into B by
177   // Idx. Find a class that is also a sub-class of A.
178   const uint32_t *SC = A->getSubClassMask();
179
180   // Find the first common register class in TV and SC.
181   for (unsigned Base = 0, BaseE = getNumRegClasses(); Base < BaseE; Base += 32)
182     if (unsigned Common = *Mask++ & *SC++)
183       return getRegClass(Base + CountTrailingZeros_32(Common));
184   return 0;
185 }
186
187 const TargetRegisterClass *TargetRegisterInfo::
188 getCommonSuperRegClass(const TargetRegisterClass *RCA, unsigned SubA,
189                        const TargetRegisterClass *RCB, unsigned SubB,
190                        unsigned &PreA, unsigned &PreB) const {
191   assert(RCA && SubA && RCB && SubB && "Invalid arguments");
192
193   // Search all pairs of sub-register indices that project into RCA and RCB
194   // respectively. This is quadratic, but usually the sets are very small. On
195   // most targets like X86, there will only be a single sub-register index
196   // (e.g., sub_16bit projecting into GR16).
197   //
198   // The worst case is a register class like DPR on ARM.
199   // We have indices dsub_0..dsub_7 projecting into that class.
200   //
201   // It is very common that one register class is a sub-register of the other.
202   // Arrange for RCA to be the larger register so the answer will be found in
203   // the first iteration. This makes the search linear for the most common
204   // case.
205   const TargetRegisterClass *BestRC = 0;
206   unsigned *BestPreA = &PreA;
207   unsigned *BestPreB = &PreB;
208   if (RCA->getSize() < RCB->getSize()) {
209     std::swap(RCA, RCB);
210     std::swap(SubA, SubB);
211     std::swap(BestPreA, BestPreB);
212   }
213
214   // Also terminate the search one we have found a register class as small as
215   // RCA.
216   unsigned MinSize = RCA->getSize();
217
218   for (SuperRegClassIterator IA(RCA, this, true); IA.isValid(); ++IA) {
219     unsigned FinalA = composeSubRegIndices(IA.getSubReg(), SubA);
220     for (SuperRegClassIterator IB(RCB, this, true); IB.isValid(); ++IB) {
221       // Check if a common super-register class exists for this index pair.
222       const TargetRegisterClass *RC =
223         firstCommonClass(IA.getMask(), IB.getMask(), this);
224       if (!RC || RC->getSize() < MinSize)
225         continue;
226
227       // The indexes must compose identically: PreA+SubA == PreB+SubB.
228       unsigned FinalB = composeSubRegIndices(IB.getSubReg(), SubB);
229       if (FinalA != FinalB)
230         continue;
231
232       // Is RC a better candidate than BestRC?
233       if (BestRC && RC->getSize() >= BestRC->getSize())
234         continue;
235
236       // Yes, RC is the smallest super-register seen so far.
237       BestRC = RC;
238       *BestPreA = IA.getSubReg();
239       *BestPreB = IB.getSubReg();
240
241       // Bail early if we reached MinSize. We won't find a better candidate.
242       if (BestRC->getSize() == MinSize)
243         return BestRC;
244     }
245   }
246   return BestRC;
247 }