Replace the statically generated hashtables for checking register relationships with...
[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/CodeGen/MachineFunction.h"
17 #include "llvm/CodeGen/MachineFrameInfo.h"
18 #include "llvm/ADT/BitVector.h"
19 #include "llvm/Support/raw_ostream.h"
20
21 using namespace llvm;
22
23 TargetRegisterInfo::TargetRegisterInfo(const TargetRegisterDesc *D, unsigned NR,
24                              regclass_iterator RCB, regclass_iterator RCE,
25                              const char *const *subregindexnames,
26                              int CFSO, int CFDO)
27   : Desc(D), SubRegIndexNames(subregindexnames), NumRegs(NR),
28     RegClassBegin(RCB), RegClassEnd(RCE) {
29   assert(isPhysicalRegister(NumRegs) &&
30          "Target has too many physical registers!");
31
32   CallFrameSetupOpcode   = CFSO;
33   CallFrameDestroyOpcode = CFDO;
34 }
35
36 TargetRegisterInfo::~TargetRegisterInfo() {}
37
38 void PrintReg::print(raw_ostream &OS) const {
39   if (!Reg)
40     OS << "%noreg";
41   else if (TargetRegisterInfo::isStackSlot(Reg))
42     OS << "SS#" << TargetRegisterInfo::stackSlot2Index(Reg);
43   else if (TargetRegisterInfo::isVirtualRegister(Reg))
44     OS << "%vreg" << TargetRegisterInfo::virtReg2Index(Reg);
45   else if (TRI && Reg < TRI->getNumRegs())
46     OS << '%' << TRI->getName(Reg);
47   else
48     OS << "%physreg" << Reg;
49   if (SubIdx) {
50     if (TRI)
51       OS << ':' << TRI->getSubRegIndexName(SubIdx);
52     else
53       OS << ":sub(" << SubIdx << ')';
54   }
55 }
56
57 /// getMinimalPhysRegClass - Returns the Register Class of a physical
58 /// register of the given type, picking the most sub register class of
59 /// the right type that contains this physreg.
60 const TargetRegisterClass *
61 TargetRegisterInfo::getMinimalPhysRegClass(unsigned reg, EVT VT) const {
62   assert(isPhysicalRegister(reg) && "reg must be a physical register");
63
64   // Pick the most sub register class of the right type that contains
65   // this physreg.
66   const TargetRegisterClass* BestRC = 0;
67   for (regclass_iterator I = regclass_begin(), E = regclass_end(); I != E; ++I){
68     const TargetRegisterClass* RC = *I;
69     if ((VT == MVT::Other || RC->hasType(VT)) && RC->contains(reg) &&
70         (!BestRC || BestRC->hasSubClass(RC)))
71       BestRC = RC;
72   }
73
74   assert(BestRC && "Couldn't find the register class");
75   return BestRC;
76 }
77
78 /// getAllocatableSetForRC - Toggle the bits that represent allocatable
79 /// registers for the specific register class.
80 static void getAllocatableSetForRC(const MachineFunction &MF,
81                                    const TargetRegisterClass *RC, BitVector &R){
82   for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
83          E = RC->allocation_order_end(MF); I != E; ++I)
84     R.set(*I);
85 }
86
87 BitVector TargetRegisterInfo::getAllocatableSet(const MachineFunction &MF,
88                                           const TargetRegisterClass *RC) const {
89   BitVector Allocatable(NumRegs);
90   if (RC) {
91     getAllocatableSetForRC(MF, RC, Allocatable);
92   } else {
93     for (TargetRegisterInfo::regclass_iterator I = regclass_begin(),
94          E = regclass_end(); I != E; ++I)
95       if ((*I)->isAllocatable())
96         getAllocatableSetForRC(MF, *I, Allocatable);
97   }
98
99   // Mask out the reserved registers
100   BitVector Reserved = getReservedRegs(MF);
101   Allocatable &= Reserved.flip();
102
103   return Allocatable;
104 }
105
106 const TargetRegisterClass *
107 llvm::getCommonSubClass(const TargetRegisterClass *A,
108                         const TargetRegisterClass *B) {
109   // First take care of the trivial cases
110   if (A == B)
111     return A;
112   if (!A || !B)
113     return 0;
114
115   // If B is a subclass of A, it will be handled in the loop below
116   if (B->hasSubClass(A))
117     return A;
118
119   const TargetRegisterClass *Best = 0;
120   for (TargetRegisterClass::sc_iterator I = A->subclasses_begin();
121        const TargetRegisterClass *X = *I; ++I) {
122     if (X == B)
123       return B;                 // B is a subclass of A
124
125     // X must be a common subclass of A and B
126     if (!B->hasSubClass(X))
127       continue;
128
129     // A superclass is definitely better.
130     if (!Best || Best->hasSuperClass(X)) {
131       Best = X;
132       continue;
133     }
134
135     // A subclass is definitely worse
136     if (Best->hasSubClass(X))
137       continue;
138
139     // Best and *I have no super/sub class relation - pick the larger class, or
140     // the smaller spill size.
141     int nb = std::distance(Best->begin(), Best->end());
142     int ni = std::distance(X->begin(), X->end());
143     if (ni>nb || (ni==nb && X->getSize() < Best->getSize()))
144       Best = X;
145   }
146   return Best;
147 }