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