LLVM API Documentation
00001 //===- TargetRegisterInfo.cpp - Target Register Information Implementation ===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the TargetRegisterInfo interface. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Target/TargetMachine.h" 00015 #include "llvm/Target/TargetRegisterInfo.h" 00016 #include "llvm/Target/TargetFrameInfo.h" 00017 #include "llvm/CodeGen/MachineFunction.h" 00018 #include "llvm/CodeGen/MachineFrameInfo.h" 00019 #include "llvm/ADT/BitVector.h" 00020 00021 using namespace llvm; 00022 00023 TargetRegisterInfo::TargetRegisterInfo(const TargetRegisterDesc *D, unsigned NR, 00024 regclass_iterator RCB, regclass_iterator RCE, 00025 int CFSO, int CFDO, 00026 const unsigned* subregs, const unsigned subregsize, 00027 const unsigned* superregs, const unsigned superregsize, 00028 const unsigned* aliases, const unsigned aliasessize) 00029 : SubregHash(subregs), SubregHashSize(subregsize), 00030 SuperregHash(superregs), SuperregHashSize(superregsize), 00031 AliasesHash(aliases), AliasesHashSize(aliasessize), 00032 Desc(D), NumRegs(NR), RegClassBegin(RCB), RegClassEnd(RCE) { 00033 assert(NumRegs < FirstVirtualRegister && 00034 "Target has too many physical registers!"); 00035 00036 CallFrameSetupOpcode = CFSO; 00037 CallFrameDestroyOpcode = CFDO; 00038 } 00039 00040 TargetRegisterInfo::~TargetRegisterInfo() {} 00041 00042 /// getPhysicalRegisterRegClass - Returns the Register Class of a physical 00043 /// register of the given type. If type is EVT::Other, then just return any 00044 /// register class the register belongs to. 00045 const TargetRegisterClass * 00046 TargetRegisterInfo::getPhysicalRegisterRegClass(unsigned reg, EVT VT) const { 00047 assert(isPhysicalRegister(reg) && "reg must be a physical register"); 00048 00049 // Pick the most super register class of the right type that contains 00050 // this physreg. 00051 const TargetRegisterClass* BestRC = 0; 00052 for (regclass_iterator I = regclass_begin(), E = regclass_end(); I != E; ++I){ 00053 const TargetRegisterClass* RC = *I; 00054 if ((VT == MVT::Other || RC->hasType(VT)) && RC->contains(reg) && 00055 (!BestRC || BestRC->hasSuperClass(RC))) 00056 BestRC = RC; 00057 } 00058 00059 assert(BestRC && "Couldn't find the register class"); 00060 return BestRC; 00061 } 00062 00063 /// getAllocatableSetForRC - Toggle the bits that represent allocatable 00064 /// registers for the specific register class. 00065 static void getAllocatableSetForRC(MachineFunction &MF, 00066 const TargetRegisterClass *RC, BitVector &R){ 00067 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF), 00068 E = RC->allocation_order_end(MF); I != E; ++I) 00069 R.set(*I); 00070 } 00071 00072 BitVector TargetRegisterInfo::getAllocatableSet(MachineFunction &MF, 00073 const TargetRegisterClass *RC) const { 00074 BitVector Allocatable(NumRegs); 00075 if (RC) { 00076 getAllocatableSetForRC(MF, RC, Allocatable); 00077 return Allocatable; 00078 } 00079 00080 for (TargetRegisterInfo::regclass_iterator I = regclass_begin(), 00081 E = regclass_end(); I != E; ++I) 00082 getAllocatableSetForRC(MF, *I, Allocatable); 00083 return Allocatable; 00084 } 00085 00086 /// getFrameIndexOffset - Returns the displacement from the frame register to 00087 /// the stack frame of the specified index. This is the default implementation 00088 /// which is likely incorrect for the target. 00089 int TargetRegisterInfo::getFrameIndexOffset(MachineFunction &MF, int FI) const { 00090 const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo(); 00091 MachineFrameInfo *MFI = MF.getFrameInfo(); 00092 return MFI->getObjectOffset(FI) + MFI->getStackSize() - 00093 TFI.getOffsetOfLocalArea() + MFI->getOffsetAdjustment(); 00094 } 00095 00096 /// getInitialFrameState - Returns a list of machine moves that are assumed 00097 /// on entry to a function. 00098 void 00099 TargetRegisterInfo::getInitialFrameState(std::vector<MachineMove> &Moves) const { 00100 // Default is to do nothing. 00101 } 00102 00103 const TargetRegisterClass * 00104 llvm::getCommonSubClass(const TargetRegisterClass *A, 00105 const TargetRegisterClass *B) { 00106 // First take care of the trivial cases 00107 if (A == B) 00108 return A; 00109 if (!A || !B) 00110 return 0; 00111 00112 // If B is a subclass of A, it will be handled in the loop below 00113 if (B->hasSubClass(A)) 00114 return A; 00115 00116 const TargetRegisterClass *Best = 0; 00117 for (TargetRegisterClass::sc_iterator I = A->subclasses_begin(); 00118 const TargetRegisterClass *X = *I; ++I) { 00119 if (X == B) 00120 return B; // B is a subclass of A 00121 00122 // X must be a common subclass of A and B 00123 if (!B->hasSubClass(X)) 00124 continue; 00125 00126 // A superclass is definitely better. 00127 if (!Best || Best->hasSuperClass(X)) { 00128 Best = X; 00129 continue; 00130 } 00131 00132 // A subclass is definitely worse 00133 if (Best->hasSubClass(X)) 00134 continue; 00135 00136 // Best and *I have no super/sub class relation - pick the larger class, or 00137 // the smaller spill size. 00138 int nb = std::distance(Best->begin(), Best->end()); 00139 int ni = std::distance(X->begin(), X->end()); 00140 if (ni>nb || (ni==nb && X->getSize() < Best->getSize())) 00141 Best = X; 00142 } 00143 return Best; 00144 }