1 //===-- PTXMachineFuctionInfo.h - PTX machine function info ------*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares PTX-specific per-machine-function information.
12 //===----------------------------------------------------------------------===//
14 #ifndef PTX_MACHINE_FUNCTION_INFO_H
15 #define PTX_MACHINE_FUNCTION_INFO_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"
29 /// PTXMachineFunctionInfo - This class is derived from MachineFunction and
30 /// contains private PTX target-specific information for each MachineFunction.
32 class PTXMachineFunctionInfo : public MachineFunctionInfo {
33 virtual void anchor();
35 DenseSet<unsigned> RegArgs;
36 DenseSet<unsigned> RegRets;
38 typedef DenseMap<int, std::string> FrameMap;
40 FrameMap FrameSymbols;
50 typedef DenseMap<unsigned, RegisterInfo> RegisterInfoMap;
52 RegisterInfoMap RegInfo;
54 PTXParamManager ParamManager;
57 typedef DenseSet<unsigned>::const_iterator reg_iterator;
59 PTXMachineFunctionInfo(MachineFunction &MF)
63 /// getParamManager - Returns the PTXParamManager instance for this function.
64 PTXParamManager& getParamManager() { return ParamManager; }
65 const PTXParamManager& getParamManager() const { return ParamManager; }
67 /// setKernel/isKernel - Gets/sets a flag that indicates if this function is
68 /// a PTX kernel function.
69 void setKernel(bool _IsKernel=true) { IsKernel = _IsKernel; }
70 bool isKernel() const { return IsKernel; }
72 /// argreg_begin/argreg_end - Returns iterators to the set of registers
73 /// containing function arguments.
74 reg_iterator argreg_begin() const { return RegArgs.begin(); }
75 reg_iterator argreg_end() const { return RegArgs.end(); }
77 /// retreg_begin/retreg_end - Returns iterators to the set of registers
78 /// containing the function return values.
79 reg_iterator retreg_begin() const { return RegRets.begin(); }
80 reg_iterator retreg_end() const { return RegRets.end(); }
82 /// addRegister - Adds a virtual register to the set of all used registers
83 void addRegister(unsigned Reg, unsigned RegType, unsigned RegSpace) {
84 if (!RegInfo.count(Reg)) {
88 Info.Space = RegSpace;
90 // Determine register offset
92 for(RegisterInfoMap::const_iterator i = RegInfo.begin(),
93 e = RegInfo.end(); i != e; ++i) {
94 const RegisterInfo& RI = i->second;
95 if (RI.Space == RegSpace)
96 if (RI.Space != PTXRegisterSpace::Reg || RI.Type == Info.Type)
100 // Encode the register data into a single register number
101 Info.Encoded = (Info.Offset << 6) | (Info.Type << 3) | Info.Space;
105 if (RegSpace == PTXRegisterSpace::Argument)
107 else if (RegSpace == PTXRegisterSpace::Return)
112 /// countRegisters - Returns the number of registers of the given type and
114 unsigned countRegisters(unsigned RegType, unsigned RegSpace) const {
116 for(RegisterInfoMap::const_iterator i = RegInfo.begin(), e = RegInfo.end();
118 const RegisterInfo& RI = i->second;
119 if (RI.Type == RegType && RI.Space == RegSpace)
125 /// getEncodedRegister - Returns the encoded value of the register.
126 unsigned getEncodedRegister(unsigned Reg) const {
127 return RegInfo.lookup(Reg).Encoded;
130 /// addRetReg - Adds a register to the set of return-value registers.
131 void addRetReg(unsigned Reg) {
132 if (!RegRets.count(Reg)) {
137 /// addArgReg - Adds a register to the set of function argument registers.
138 void addArgReg(unsigned Reg) {
142 /// getRegisterName - Returns the name of the specified virtual register. This
143 /// name is used during PTX emission.
144 std::string getRegisterName(unsigned Reg) const {
145 if (RegInfo.count(Reg)) {
146 const RegisterInfo& RI = RegInfo.lookup(Reg);
148 raw_string_ostream NameStr(Name);
149 decodeRegisterName(NameStr, RI.Encoded);
153 else if (Reg == PTX::NoRegister)
156 llvm_unreachable("Register not in register name map");
159 /// getEncodedRegisterName - Returns the name of the encoded register.
160 std::string getEncodedRegisterName(unsigned EncodedReg) const {
162 raw_string_ostream NameStr(Name);
163 decodeRegisterName(NameStr, EncodedReg);
168 /// getRegisterType - Returns the type of the specified virtual register.
169 unsigned getRegisterType(unsigned Reg) const {
170 if (RegInfo.count(Reg))
171 return RegInfo.lookup(Reg).Type;
173 llvm_unreachable("Unknown register");
176 /// getOffsetForRegister - Returns the offset of the virtual register
177 unsigned getOffsetForRegister(unsigned Reg) const {
178 if (RegInfo.count(Reg))
179 return RegInfo.lookup(Reg).Offset;
184 /// getFrameSymbol - Returns the symbol name for the given FrameIndex.
185 const char* getFrameSymbol(int FrameIndex) {
186 if (FrameSymbols.count(FrameIndex)) {
187 return FrameSymbols.lookup(FrameIndex).c_str();
189 std::string Name = "__local";
190 Name += utostr(FrameIndex);
191 // The whole point of caching this name is to ensure the pointer we pass
192 // to any getExternalSymbol() calls will remain valid for the lifetime of
193 // the back-end instance. This is to work around an issue in SelectionDAG
194 // where symbol names are expected to be life-long strings.
195 FrameSymbols[FrameIndex] = Name;
196 return FrameSymbols[FrameIndex].c_str();
199 }; // class PTXMachineFunctionInfo
202 #endif // PTX_MACHINE_FUNCTION_INFO_H