1 //===- PTXMachineFuctionInfo.h - PTX machine function info -------*- C++ -*-==// 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 declares PTX-specific per-machine-function information. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef PTX_MACHINE_FUNCTION_INFO_H 15 #define PTX_MACHINE_FUNCTION_INFO_H 16 17 #include "PTX.h" 18 #include "PTXParamManager.h" 19 #include "PTXRegisterInfo.h" 20 #include "llvm/ADT/DenseMap.h" 21 #include "llvm/ADT/DenseSet.h" 22 #include "llvm/ADT/StringExtras.h" 23 #include "llvm/CodeGen/MachineFunction.h" 24 #include "llvm/Support/Debug.h" 25 #include "llvm/Support/raw_ostream.h" 26 27 namespace llvm { 28 29 /// PTXMachineFunctionInfo - This class is derived from MachineFunction and 30 /// contains private PTX target-specific information for each MachineFunction. 31 /// 32 class PTXMachineFunctionInfo : public MachineFunctionInfo { 33 private: 34 bool IsKernel; 35 DenseSet<unsigned> RegArgs; 36 DenseSet<unsigned> RegRets; 37 38 typedef std::vector<unsigned> RegisterList; 39 typedef DenseMap<const TargetRegisterClass*, RegisterList> RegisterMap; 40 typedef DenseMap<unsigned, std::string> RegisterNameMap; 41 typedef DenseMap<int, std::string> FrameMap; 42 43 RegisterMap UsedRegs; 44 RegisterNameMap RegNames; 45 FrameMap FrameSymbols; 46 47 PTXParamManager ParamManager; 48 49 public: 50 typedef DenseSet<unsigned>::const_iterator reg_iterator; 51 PTXMachineFunctionInfo(MachineFunction & MF)52 PTXMachineFunctionInfo(MachineFunction &MF) 53 : IsKernel(false) { 54 UsedRegs[PTX::RegPredRegisterClass] = RegisterList(); 55 UsedRegs[PTX::RegI16RegisterClass] = RegisterList(); 56 UsedRegs[PTX::RegI32RegisterClass] = RegisterList(); 57 UsedRegs[PTX::RegI64RegisterClass] = RegisterList(); 58 UsedRegs[PTX::RegF32RegisterClass] = RegisterList(); 59 UsedRegs[PTX::RegF64RegisterClass] = RegisterList(); 60 } 61 62 /// getParamManager - Returns the PTXParamManager instance for this function. getParamManager()63 PTXParamManager& getParamManager() { return ParamManager; } getParamManager()64 const PTXParamManager& getParamManager() const { return ParamManager; } 65 66 /// setKernel/isKernel - Gets/sets a flag that indicates if this function is 67 /// a PTX kernel function. 68 void setKernel(bool _IsKernel=true) { IsKernel = _IsKernel; } isKernel()69 bool isKernel() const { return IsKernel; } 70 71 /// argreg_begin/argreg_end - Returns iterators to the set of registers 72 /// containing function arguments. argreg_begin()73 reg_iterator argreg_begin() const { return RegArgs.begin(); } argreg_end()74 reg_iterator argreg_end() const { return RegArgs.end(); } 75 76 /// retreg_begin/retreg_end - Returns iterators to the set of registers 77 /// containing the function return values. retreg_begin()78 reg_iterator retreg_begin() const { return RegRets.begin(); } retreg_end()79 reg_iterator retreg_end() const { return RegRets.end(); } 80 81 /// addRetReg - Adds a register to the set of return-value registers. addRetReg(unsigned Reg)82 void addRetReg(unsigned Reg) { 83 if (!RegRets.count(Reg)) { 84 RegRets.insert(Reg); 85 std::string name; 86 name = "%ret"; 87 name += utostr(RegRets.size() - 1); 88 RegNames[Reg] = name; 89 } 90 } 91 92 /// addArgReg - Adds a register to the set of function argument registers. addArgReg(unsigned Reg)93 void addArgReg(unsigned Reg) { 94 RegArgs.insert(Reg); 95 std::string name; 96 name = "%param"; 97 name += utostr(RegArgs.size() - 1); 98 RegNames[Reg] = name; 99 } 100 101 /// addVirtualRegister - Adds a virtual register to the set of all used 102 /// registers in the function. addVirtualRegister(const TargetRegisterClass * TRC,unsigned Reg)103 void addVirtualRegister(const TargetRegisterClass *TRC, unsigned Reg) { 104 std::string name; 105 106 // Do not count registers that are argument/return registers. 107 if (!RegRets.count(Reg) && !RegArgs.count(Reg)) { 108 UsedRegs[TRC].push_back(Reg); 109 if (TRC == PTX::RegPredRegisterClass) 110 name = "%p"; 111 else if (TRC == PTX::RegI16RegisterClass) 112 name = "%rh"; 113 else if (TRC == PTX::RegI32RegisterClass) 114 name = "%r"; 115 else if (TRC == PTX::RegI64RegisterClass) 116 name = "%rd"; 117 else if (TRC == PTX::RegF32RegisterClass) 118 name = "%f"; 119 else if (TRC == PTX::RegF64RegisterClass) 120 name = "%fd"; 121 else 122 llvm_unreachable("Invalid register class"); 123 124 name += utostr(UsedRegs[TRC].size() - 1); 125 RegNames[Reg] = name; 126 } 127 } 128 129 /// getRegisterName - Returns the name of the specified virtual register. This 130 /// name is used during PTX emission. getRegisterName(unsigned Reg)131 const char *getRegisterName(unsigned Reg) const { 132 if (RegNames.count(Reg)) 133 return RegNames.find(Reg)->second.c_str(); 134 else if (Reg == PTX::NoRegister) 135 return "%noreg"; 136 else 137 llvm_unreachable("Register not in register name map"); 138 } 139 140 /// getNumRegistersForClass - Returns the number of virtual registers that are 141 /// used for the specified register class. getNumRegistersForClass(const TargetRegisterClass * TRC)142 unsigned getNumRegistersForClass(const TargetRegisterClass *TRC) const { 143 return UsedRegs.lookup(TRC).size(); 144 } 145 146 /// getFrameSymbol - Returns the symbol name for the given FrameIndex. getFrameSymbol(int FrameIndex)147 const char* getFrameSymbol(int FrameIndex) { 148 if (FrameSymbols.count(FrameIndex)) { 149 return FrameSymbols.lookup(FrameIndex).c_str(); 150 } else { 151 std::string Name = "__local"; 152 Name += utostr(FrameIndex); 153 // The whole point of caching this name is to ensure the pointer we pass 154 // to any getExternalSymbol() calls will remain valid for the lifetime of 155 // the back-end instance. This is to work around an issue in SelectionDAG 156 // where symbol names are expected to be life-long strings. 157 FrameSymbols[FrameIndex] = Name; 158 return FrameSymbols[FrameIndex].c_str(); 159 } 160 } 161 }; // class PTXMachineFunctionInfo 162 } // namespace llvm 163 164 #endif // PTX_MACHINE_FUNCTION_INFO_H 165