Move getInitialFrameState() to TargetFrameInfo
[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
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                              const unsigned* subregs, const unsigned subregsize,
28                          const unsigned* aliases, const unsigned aliasessize)
29   : SubregHash(subregs), SubregHashSize(subregsize),
30     AliasesHash(aliases), AliasesHashSize(aliasessize),
31     Desc(D), SubRegIndexNames(subregindexnames), NumRegs(NR),
32     RegClassBegin(RCB), RegClassEnd(RCE) {
33   assert(NumRegs < FirstVirtualRegister &&
34          "Target has too many physical registers!");
35
36   CallFrameSetupOpcode   = CFSO;
37   CallFrameDestroyOpcode = CFDO;
38 }
39
40 TargetRegisterInfo::~TargetRegisterInfo() {}
41
42 /// getMinimalPhysRegClass - Returns the Register Class of a physical
43 /// register of the given type, picking the most sub register class of
44 /// the right type that contains this physreg.
45 const TargetRegisterClass *
46 TargetRegisterInfo::getMinimalPhysRegClass(unsigned reg, EVT VT) const {
47   assert(isPhysicalRegister(reg) && "reg must be a physical register");
48
49   // Pick the most sub register class of the right type that contains
50   // this physreg.
51   const TargetRegisterClass* BestRC = 0;
52   for (regclass_iterator I = regclass_begin(), E = regclass_end(); I != E; ++I){
53     const TargetRegisterClass* RC = *I;
54     if ((VT == MVT::Other || RC->hasType(VT)) && RC->contains(reg) &&
55         (!BestRC || BestRC->hasSubClass(RC)))
56       BestRC = RC;
57   }
58
59   assert(BestRC && "Couldn't find the register class");
60   return BestRC;
61 }
62
63 /// getAllocatableSetForRC - Toggle the bits that represent allocatable
64 /// registers for the specific register class.
65 static void getAllocatableSetForRC(const MachineFunction &MF,
66                                    const TargetRegisterClass *RC, BitVector &R){
67   for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
68          E = RC->allocation_order_end(MF); I != E; ++I)
69     R.set(*I);
70 }
71
72 BitVector TargetRegisterInfo::getAllocatableSet(const MachineFunction &MF,
73                                           const TargetRegisterClass *RC) const {
74   BitVector Allocatable(NumRegs);
75   if (RC) {
76     getAllocatableSetForRC(MF, RC, Allocatable);
77   } else {
78     for (TargetRegisterInfo::regclass_iterator I = regclass_begin(),
79          E = regclass_end(); I != E; ++I)
80       getAllocatableSetForRC(MF, *I, Allocatable);
81   }
82
83   // Mask out the reserved registers
84   BitVector Reserved = getReservedRegs(MF);
85   Allocatable &= Reserved.flip();
86
87   return Allocatable;
88 }
89
90 /// getFrameIndexOffset - Returns the displacement from the frame register to
91 /// the stack frame of the specified index. This is the default implementation
92 /// which is overridden for some targets.
93 int TargetRegisterInfo::getFrameIndexOffset(const MachineFunction &MF,
94                                             int FI) const {
95   const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
96   const MachineFrameInfo *MFI = MF.getFrameInfo();
97   return MFI->getObjectOffset(FI) + MFI->getStackSize() -
98     TFI.getOffsetOfLocalArea() + MFI->getOffsetAdjustment();
99 }
100
101 const TargetRegisterClass *
102 llvm::getCommonSubClass(const TargetRegisterClass *A,
103                         const TargetRegisterClass *B) {
104   // First take care of the trivial cases
105   if (A == B)
106     return A;
107   if (!A || !B)
108     return 0;
109
110   // If B is a subclass of A, it will be handled in the loop below
111   if (B->hasSubClass(A))
112     return A;
113
114   const TargetRegisterClass *Best = 0;
115   for (TargetRegisterClass::sc_iterator I = A->subclasses_begin();
116        const TargetRegisterClass *X = *I; ++I) {
117     if (X == B)
118       return B;                 // B is a subclass of A
119
120     // X must be a common subclass of A and B
121     if (!B->hasSubClass(X))
122       continue;
123
124     // A superclass is definitely better.
125     if (!Best || Best->hasSuperClass(X)) {
126       Best = X;
127       continue;
128     }
129
130     // A subclass is definitely worse
131     if (Best->hasSubClass(X))
132       continue;
133
134     // Best and *I have no super/sub class relation - pick the larger class, or
135     // the smaller spill size.
136     int nb = std::distance(Best->begin(), Best->end());
137     int ni = std::distance(X->begin(), X->end());
138     if (ni>nb || (ni==nb && X->getSize() < Best->getSize()))
139       Best = X;
140   }
141   return Best;
142 }