Replace the dynamically computed std::set lookup method for subregisters with a hasht...
[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                              int CFSO, int CFDO)
26   : Desc(D), NumRegs(NR), RegClassBegin(RCB), RegClassEnd(RCE) {
27   assert(NumRegs < FirstVirtualRegister &&
28          "Target has too many physical registers!");
29
30   CallFrameSetupOpcode   = CFSO;
31   CallFrameDestroyOpcode = CFDO;
32 }
33
34 TargetRegisterInfo::~TargetRegisterInfo() {}
35
36 namespace {
37   // Sort according to super- / sub- class relations.
38   // i.e. super- register class < sub- register class.
39   struct RCCompare {
40     bool operator()(const TargetRegisterClass* const &LHS,
41                     const TargetRegisterClass* const &RHS) {
42       return RHS->hasSuperClass(LHS);
43     }
44   };
45 }
46
47 /// getPhysicalRegisterRegClass - Returns the Register Class of a physical
48 /// register of the given type. If type is MVT::Other, then just return any
49 /// register class the register belongs to.
50 const TargetRegisterClass *
51 TargetRegisterInfo::getPhysicalRegisterRegClass(unsigned reg, MVT VT) const {
52   assert(isPhysicalRegister(reg) && "reg must be a physical register");
53
54   // Pick the register class of the right type that contains this physreg.
55   SmallVector<const TargetRegisterClass*, 4> RCs;
56   for (regclass_iterator I = regclass_begin(), E = regclass_end(); I != E; ++I){
57     if ((VT == MVT::Other || (*I)->hasType(VT)) && (*I)->contains(reg))
58       RCs.push_back(*I);
59   }
60
61   if (RCs.size() == 1)
62     return RCs[0];
63
64   if (RCs.size()) {
65     // Multiple compatible register classes. Get the super- class.
66     std::stable_sort(RCs.begin(), RCs.end(), RCCompare());
67     return RCs[0];
68   }
69
70   assert(false && "Couldn't find the register class");
71   return 0;
72 }
73
74 /// getAllocatableSetForRC - Toggle the bits that represent allocatable
75 /// registers for the specific register class.
76 static void getAllocatableSetForRC(MachineFunction &MF,
77                                    const TargetRegisterClass *RC, BitVector &R){  
78   for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
79          E = RC->allocation_order_end(MF); I != E; ++I)
80     R.set(*I);
81 }
82
83 BitVector TargetRegisterInfo::getAllocatableSet(MachineFunction &MF,
84                                           const TargetRegisterClass *RC) const {
85   BitVector Allocatable(NumRegs);
86   if (RC) {
87     getAllocatableSetForRC(MF, RC, Allocatable);
88     return Allocatable;
89   }
90
91   for (TargetRegisterInfo::regclass_iterator I = regclass_begin(),
92          E = regclass_end(); I != E; ++I)
93     getAllocatableSetForRC(MF, *I, Allocatable);
94   return Allocatable;
95 }
96
97 /// getFrameIndexOffset - Returns the displacement from the frame register to
98 /// the stack frame of the specified index. This is the default implementation
99 /// which is likely incorrect for the target.
100 int TargetRegisterInfo::getFrameIndexOffset(MachineFunction &MF, int FI) const {
101   const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
102   MachineFrameInfo *MFI = MF.getFrameInfo();
103   return MFI->getObjectOffset(FI) + MFI->getStackSize() -
104     TFI.getOffsetOfLocalArea() + MFI->getOffsetAdjustment();
105 }
106
107 /// getInitialFrameState - Returns a list of machine moves that are assumed
108 /// on entry to a function.
109 void
110 TargetRegisterInfo::getInitialFrameState(std::vector<MachineMove> &Moves) const {
111   // Default is to do nothing.
112 }
113