9c0bfa11b0e1d998a49f671b84a2453cc110dc5e
[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::isVirtualRegister(Reg))
47     OS << "%vreg" << TargetRegisterInfo::virtReg2Index(Reg);
48   else if (TRI)
49     OS << '%' << TRI->getName(Reg);
50   else
51     OS << "%physreg" << Reg;
52   if (SubIdx) {
53     if (TRI)
54       OS << ':' << TRI->getSubRegIndexName(SubIdx);
55     else
56       OS << ":sub(" << SubIdx << ')';
57   }
58 }
59
60 /// getMinimalPhysRegClass - Returns the Register Class of a physical
61 /// register of the given type, picking the most sub register class of
62 /// the right type that contains this physreg.
63 const TargetRegisterClass *
64 TargetRegisterInfo::getMinimalPhysRegClass(unsigned reg, EVT VT) const {
65   assert(isPhysicalRegister(reg) && "reg must be a physical register");
66
67   // Pick the most sub register class of the right type that contains
68   // this physreg.
69   const TargetRegisterClass* BestRC = 0;
70   for (regclass_iterator I = regclass_begin(), E = regclass_end(); I != E; ++I){
71     const TargetRegisterClass* RC = *I;
72     if ((VT == MVT::Other || RC->hasType(VT)) && RC->contains(reg) &&
73         (!BestRC || BestRC->hasSubClass(RC)))
74       BestRC = RC;
75   }
76
77   assert(BestRC && "Couldn't find the register class");
78   return BestRC;
79 }
80
81 /// getAllocatableSetForRC - Toggle the bits that represent allocatable
82 /// registers for the specific register class.
83 static void getAllocatableSetForRC(const MachineFunction &MF,
84                                    const TargetRegisterClass *RC, BitVector &R){
85   for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
86          E = RC->allocation_order_end(MF); I != E; ++I)
87     R.set(*I);
88 }
89
90 BitVector TargetRegisterInfo::getAllocatableSet(const MachineFunction &MF,
91                                           const TargetRegisterClass *RC) const {
92   BitVector Allocatable(NumRegs);
93   if (RC) {
94     getAllocatableSetForRC(MF, RC, Allocatable);
95   } else {
96     for (TargetRegisterInfo::regclass_iterator I = regclass_begin(),
97          E = regclass_end(); I != E; ++I)
98       getAllocatableSetForRC(MF, *I, Allocatable);
99   }
100
101   // Mask out the reserved registers
102   BitVector Reserved = getReservedRegs(MF);
103   Allocatable &= Reserved.flip();
104
105   return Allocatable;
106 }
107
108 const TargetRegisterClass *
109 llvm::getCommonSubClass(const TargetRegisterClass *A,
110                         const TargetRegisterClass *B) {
111   // First take care of the trivial cases
112   if (A == B)
113     return A;
114   if (!A || !B)
115     return 0;
116
117   // If B is a subclass of A, it will be handled in the loop below
118   if (B->hasSubClass(A))
119     return A;
120
121   const TargetRegisterClass *Best = 0;
122   for (TargetRegisterClass::sc_iterator I = A->subclasses_begin();
123        const TargetRegisterClass *X = *I; ++I) {
124     if (X == B)
125       return B;                 // B is a subclass of A
126
127     // X must be a common subclass of A and B
128     if (!B->hasSubClass(X))
129       continue;
130
131     // A superclass is definitely better.
132     if (!Best || Best->hasSuperClass(X)) {
133       Best = X;
134       continue;
135     }
136
137     // A subclass is definitely worse
138     if (Best->hasSubClass(X))
139       continue;
140
141     // Best and *I have no super/sub class relation - pick the larger class, or
142     // the smaller spill size.
143     int nb = std::distance(Best->begin(), Best->end());
144     int ni = std::distance(X->begin(), X->end());
145     if (ni>nb || (ni==nb && X->getSize() < Best->getSize()))
146       Best = X;
147   }
148   return Best;
149 }