1 //===- TargetRegisterInfo.cpp - Target Register Information Implementation ===//
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 TargetRegisterInfo interface.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Target/TargetRegisterInfo.h"
15 #include "llvm/ADT/BitVector.h"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/CodeGen/MachineRegisterInfo.h"
18 #include "llvm/CodeGen/VirtRegMap.h"
19 #include "llvm/Support/raw_ostream.h"
23 TargetRegisterInfo::TargetRegisterInfo(const TargetRegisterInfoDesc *ID,
24 regclass_iterator RCB, regclass_iterator RCE,
25 const char *const *SRINames,
26 const unsigned *SRILaneMasks,
27 unsigned SRICoveringLanes)
28 : InfoDesc(ID), SubRegIndexNames(SRINames),
29 SubRegIndexLaneMasks(SRILaneMasks),
30 RegClassBegin(RCB), RegClassEnd(RCE),
31 CoveringLanes(SRICoveringLanes) {
34 TargetRegisterInfo::~TargetRegisterInfo() {}
36 void PrintReg::print(raw_ostream &OS) const {
39 else if (TargetRegisterInfo::isStackSlot(Reg))
40 OS << "SS#" << TargetRegisterInfo::stackSlot2Index(Reg);
41 else if (TargetRegisterInfo::isVirtualRegister(Reg))
42 OS << "%vreg" << TargetRegisterInfo::virtReg2Index(Reg);
43 else if (TRI && Reg < TRI->getNumRegs())
44 OS << '%' << TRI->getName(Reg);
46 OS << "%physreg" << Reg;
49 OS << ':' << TRI->getSubRegIndexName(SubIdx);
51 OS << ":sub(" << SubIdx << ')';
55 void PrintRegUnit::print(raw_ostream &OS) const {
56 // Generic printout when TRI is missing.
58 OS << "Unit~" << Unit;
62 // Check for invalid register units.
63 if (Unit >= TRI->getNumRegUnits()) {
64 OS << "BadUnit~" << Unit;
68 // Normal units have at least one root.
69 MCRegUnitRootIterator Roots(Unit, TRI);
70 assert(Roots.isValid() && "Unit has no roots.");
71 OS << TRI->getName(*Roots);
72 for (++Roots; Roots.isValid(); ++Roots)
73 OS << '~' << TRI->getName(*Roots);
76 /// getAllocatableClass - Return the maximal subclass of the given register
77 /// class that is alloctable, or NULL.
78 const TargetRegisterClass *
79 TargetRegisterInfo::getAllocatableClass(const TargetRegisterClass *RC) const {
80 if (!RC || RC->isAllocatable())
83 const unsigned *SubClass = RC->getSubClassMask();
84 for (unsigned Base = 0, BaseE = getNumRegClasses();
85 Base < BaseE; Base += 32) {
87 for (unsigned Mask = *SubClass++; Mask; Mask >>= 1) {
88 unsigned Offset = countTrailingZeros(Mask);
89 const TargetRegisterClass *SubRC = getRegClass(Idx + Offset);
90 if (SubRC->isAllocatable())
99 /// getMinimalPhysRegClass - Returns the Register Class of a physical
100 /// register of the given type, picking the most sub register class of
101 /// the right type that contains this physreg.
102 const TargetRegisterClass *
103 TargetRegisterInfo::getMinimalPhysRegClass(unsigned reg, EVT VT) const {
104 assert(isPhysicalRegister(reg) && "reg must be a physical register");
106 // Pick the most sub register class of the right type that contains
108 const TargetRegisterClass* BestRC = 0;
109 for (regclass_iterator I = regclass_begin(), E = regclass_end(); I != E; ++I){
110 const TargetRegisterClass* RC = *I;
111 if ((VT == MVT::Other || RC->hasType(VT)) && RC->contains(reg) &&
112 (!BestRC || BestRC->hasSubClass(RC)))
116 assert(BestRC && "Couldn't find the register class");
120 /// getAllocatableSetForRC - Toggle the bits that represent allocatable
121 /// registers for the specific register class.
122 static void getAllocatableSetForRC(const MachineFunction &MF,
123 const TargetRegisterClass *RC, BitVector &R){
124 assert(RC->isAllocatable() && "invalid for nonallocatable sets");
125 ArrayRef<uint16_t> Order = RC->getRawAllocationOrder(MF);
126 for (unsigned i = 0; i != Order.size(); ++i)
130 BitVector TargetRegisterInfo::getAllocatableSet(const MachineFunction &MF,
131 const TargetRegisterClass *RC) const {
132 BitVector Allocatable(getNumRegs());
134 // A register class with no allocatable subclass returns an empty set.
135 const TargetRegisterClass *SubClass = getAllocatableClass(RC);
137 getAllocatableSetForRC(MF, SubClass, Allocatable);
139 for (TargetRegisterInfo::regclass_iterator I = regclass_begin(),
140 E = regclass_end(); I != E; ++I)
141 if ((*I)->isAllocatable())
142 getAllocatableSetForRC(MF, *I, Allocatable);
145 // Mask out the reserved registers
146 BitVector Reserved = getReservedRegs(MF);
147 Allocatable &= Reserved.flip();
153 const TargetRegisterClass *firstCommonClass(const uint32_t *A,
155 const TargetRegisterInfo *TRI) {
156 for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; I += 32)
157 if (unsigned Common = *A++ & *B++)
158 return TRI->getRegClass(I + countTrailingZeros(Common));
162 const TargetRegisterClass *
163 TargetRegisterInfo::getCommonSubClass(const TargetRegisterClass *A,
164 const TargetRegisterClass *B) const {
165 // First take care of the trivial cases.
171 // Register classes are ordered topologically, so the largest common
172 // sub-class it the common sub-class with the smallest ID.
173 return firstCommonClass(A->getSubClassMask(), B->getSubClassMask(), this);
176 const TargetRegisterClass *
177 TargetRegisterInfo::getMatchingSuperRegClass(const TargetRegisterClass *A,
178 const TargetRegisterClass *B,
179 unsigned Idx) const {
180 assert(A && B && "Missing register class");
181 assert(Idx && "Bad sub-register index");
183 // Find Idx in the list of super-register indices.
184 for (SuperRegClassIterator RCI(B, this); RCI.isValid(); ++RCI)
185 if (RCI.getSubReg() == Idx)
186 // The bit mask contains all register classes that are projected into B
187 // by Idx. Find a class that is also a sub-class of A.
188 return firstCommonClass(RCI.getMask(), A->getSubClassMask(), this);
192 const TargetRegisterClass *TargetRegisterInfo::
193 getCommonSuperRegClass(const TargetRegisterClass *RCA, unsigned SubA,
194 const TargetRegisterClass *RCB, unsigned SubB,
195 unsigned &PreA, unsigned &PreB) const {
196 assert(RCA && SubA && RCB && SubB && "Invalid arguments");
198 // Search all pairs of sub-register indices that project into RCA and RCB
199 // respectively. This is quadratic, but usually the sets are very small. On
200 // most targets like X86, there will only be a single sub-register index
201 // (e.g., sub_16bit projecting into GR16).
203 // The worst case is a register class like DPR on ARM.
204 // We have indices dsub_0..dsub_7 projecting into that class.
206 // It is very common that one register class is a sub-register of the other.
207 // Arrange for RCA to be the larger register so the answer will be found in
208 // the first iteration. This makes the search linear for the most common
210 const TargetRegisterClass *BestRC = 0;
211 unsigned *BestPreA = &PreA;
212 unsigned *BestPreB = &PreB;
213 if (RCA->getSize() < RCB->getSize()) {
215 std::swap(SubA, SubB);
216 std::swap(BestPreA, BestPreB);
219 // Also terminate the search one we have found a register class as small as
221 unsigned MinSize = RCA->getSize();
223 for (SuperRegClassIterator IA(RCA, this, true); IA.isValid(); ++IA) {
224 unsigned FinalA = composeSubRegIndices(IA.getSubReg(), SubA);
225 for (SuperRegClassIterator IB(RCB, this, true); IB.isValid(); ++IB) {
226 // Check if a common super-register class exists for this index pair.
227 const TargetRegisterClass *RC =
228 firstCommonClass(IA.getMask(), IB.getMask(), this);
229 if (!RC || RC->getSize() < MinSize)
232 // The indexes must compose identically: PreA+SubA == PreB+SubB.
233 unsigned FinalB = composeSubRegIndices(IB.getSubReg(), SubB);
234 if (FinalA != FinalB)
237 // Is RC a better candidate than BestRC?
238 if (BestRC && RC->getSize() >= BestRC->getSize())
241 // Yes, RC is the smallest super-register seen so far.
243 *BestPreA = IA.getSubReg();
244 *BestPreB = IB.getSubReg();
246 // Bail early if we reached MinSize. We won't find a better candidate.
247 if (BestRC->getSize() == MinSize)
254 // Compute target-independent register allocator hints to help eliminate copies.
256 TargetRegisterInfo::getRegAllocationHints(unsigned VirtReg,
257 ArrayRef<MCPhysReg> Order,
258 SmallVectorImpl<MCPhysReg> &Hints,
259 const MachineFunction &MF,
260 const VirtRegMap *VRM) const {
261 const MachineRegisterInfo &MRI = MF.getRegInfo();
262 std::pair<unsigned, unsigned> Hint = MRI.getRegAllocationHint(VirtReg);
264 // Hints with HintType != 0 were set by target-dependent code.
265 // Such targets must provide their own implementation of
266 // TRI::getRegAllocationHints to interpret those hint types.
267 assert(Hint.first == 0 && "Target must implement TRI::getRegAllocationHints");
269 // Target-independent hints are either a physical or a virtual register.
270 unsigned Phys = Hint.second;
271 if (VRM && isVirtualRegister(Phys))
272 Phys = VRM->getPhys(Phys);
274 // Check that Phys is a valid hint in VirtReg's register class.
275 if (!isPhysicalRegister(Phys))
277 if (MRI.isReserved(Phys))
279 // Check that Phys is in the allocation order. We shouldn't heed hints
280 // from VirtReg's register class if they aren't in the allocation order. The
281 // target probably has a reason for removing the register.
282 if (std::find(Order.begin(), Order.end(), Phys) == Order.end())
285 // All clear, tell the register allocator to prefer this register.
286 Hints.push_back(Phys);