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