Print symbolic SubRegIndex names on machine operands.
[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* superregs, const unsigned superregsize,
29                          const unsigned* aliases, const unsigned aliasessize)
30   : SubregHash(subregs), SubregHashSize(subregsize),
31     SuperregHash(superregs), SuperregHashSize(superregsize),
32     AliasesHash(aliases), AliasesHashSize(aliasessize),
33     Desc(D), SubRegIndexNames(subregindexnames), NumRegs(NR),
34     RegClassBegin(RCB), RegClassEnd(RCE) {
35   assert(NumRegs < FirstVirtualRegister &&
36          "Target has too many physical registers!");
37
38   CallFrameSetupOpcode   = CFSO;
39   CallFrameDestroyOpcode = CFDO;
40 }
41
42 TargetRegisterInfo::~TargetRegisterInfo() {}
43
44 /// getPhysicalRegisterRegClass - Returns the Register Class of a physical
45 /// register of the given type. If type is EVT::Other, then just return any
46 /// register class the register belongs to.
47 const TargetRegisterClass *
48 TargetRegisterInfo::getPhysicalRegisterRegClass(unsigned reg, EVT VT) const {
49   assert(isPhysicalRegister(reg) && "reg must be a physical register");
50
51   // Pick the most super register class of the right type that contains
52   // this physreg.
53   const TargetRegisterClass* BestRC = 0;
54   for (regclass_iterator I = regclass_begin(), E = regclass_end(); I != E; ++I){
55     const TargetRegisterClass* RC = *I;
56     if ((VT == MVT::Other || RC->hasType(VT)) && RC->contains(reg) &&
57         (!BestRC || BestRC->hasSuperClass(RC)))
58       BestRC = RC;
59   }
60
61   assert(BestRC && "Couldn't find the register class");
62   return BestRC;
63 }
64
65 /// getAllocatableSetForRC - Toggle the bits that represent allocatable
66 /// registers for the specific register class.
67 static void getAllocatableSetForRC(const MachineFunction &MF,
68                                    const TargetRegisterClass *RC, BitVector &R){  
69   for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
70          E = RC->allocation_order_end(MF); I != E; ++I)
71     R.set(*I);
72 }
73
74 BitVector TargetRegisterInfo::getAllocatableSet(const MachineFunction &MF,
75                                           const TargetRegisterClass *RC) const {
76   BitVector Allocatable(NumRegs);
77   if (RC) {
78     getAllocatableSetForRC(MF, RC, Allocatable);
79     return Allocatable;
80   }
81
82   for (TargetRegisterInfo::regclass_iterator I = regclass_begin(),
83          E = regclass_end(); I != E; ++I)
84     getAllocatableSetForRC(MF, *I, Allocatable);
85   return Allocatable;
86 }
87
88 /// getFrameIndexOffset - Returns the displacement from the frame register to
89 /// the stack frame of the specified index. This is the default implementation
90 /// which is overridden for some targets.
91 int TargetRegisterInfo::getFrameIndexOffset(const MachineFunction &MF,
92                                             int FI) const {
93   const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
94   const MachineFrameInfo *MFI = MF.getFrameInfo();
95   return MFI->getObjectOffset(FI) + MFI->getStackSize() -
96     TFI.getOffsetOfLocalArea() + MFI->getOffsetAdjustment();
97 }
98
99 /// getInitialFrameState - Returns a list of machine moves that are assumed
100 /// on entry to a function.
101 void
102 TargetRegisterInfo::getInitialFrameState(std::vector<MachineMove> &Moves) const{
103   // Default is to do nothing.
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 }