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 {
35 DenseSet<unsigned> RegArgs;
36 DenseSet<unsigned> RegRets;
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;
44 RegisterNameMap RegNames;
45 FrameMap FrameSymbols;
47 PTXParamManager ParamManager;
50 typedef DenseSet<unsigned>::const_iterator reg_iterator;
52 PTXMachineFunctionInfo(MachineFunction &MF)
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();
62 /// getParamManager - Returns the PTXParamManager instance for this function.
63 PTXParamManager& getParamManager() { return ParamManager; }
64 const PTXParamManager& getParamManager() const { return ParamManager; }
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; }
69 bool isKernel() const { return IsKernel; }
71 /// argreg_begin/argreg_end - Returns iterators to the set of registers
72 /// containing function arguments.
73 reg_iterator argreg_begin() const { return RegArgs.begin(); }
74 reg_iterator argreg_end() const { return RegArgs.end(); }
76 /// retreg_begin/retreg_end - Returns iterators to the set of registers
77 /// containing the function return values.
78 reg_iterator retreg_begin() const { return RegRets.begin(); }
79 reg_iterator retreg_end() const { return RegRets.end(); }
81 /// addRetReg - Adds a register to the set of return-value registers.
82 void addRetReg(unsigned Reg) {
83 if (!RegRets.count(Reg)) {
87 name += utostr(RegRets.size() - 1);
92 /// addArgReg - Adds a register to the set of function argument registers.
93 void addArgReg(unsigned Reg) {
97 name += utostr(RegArgs.size() - 1);
101 /// addVirtualRegister - Adds a virtual register to the set of all used
102 /// registers in the function.
103 void addVirtualRegister(const TargetRegisterClass *TRC, unsigned Reg) {
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)
111 else if (TRC == PTX::RegI16RegisterClass)
113 else if (TRC == PTX::RegI32RegisterClass)
115 else if (TRC == PTX::RegI64RegisterClass)
117 else if (TRC == PTX::RegF32RegisterClass)
119 else if (TRC == PTX::RegF64RegisterClass)
122 llvm_unreachable("Invalid register class");
124 name += utostr(UsedRegs[TRC].size() - 1);
125 RegNames[Reg] = name;
129 /// getRegisterName - Returns the name of the specified virtual register. This
130 /// name is used during PTX emission.
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)
137 llvm_unreachable("Register not in register name map");
140 /// getNumRegistersForClass - Returns the number of virtual registers that are
141 /// used for the specified register class.
142 unsigned getNumRegistersForClass(const TargetRegisterClass *TRC) const {
143 return UsedRegs.lookup(TRC).size();
146 /// getFrameSymbol - Returns the symbol name for the given FrameIndex.
147 const char* getFrameSymbol(int FrameIndex) {
148 if (FrameSymbols.count(FrameIndex)) {
149 return FrameSymbols.lookup(FrameIndex).c_str();
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();
161 }; // class PTXMachineFunctionInfo
164 #endif // PTX_MACHINE_FUNCTION_INFO_H