OSDN Git Service

radeonsi: initial WIP SI code
[android-x86/external-mesa.git] / src / gallium / drivers / radeon / AMDILMachineFunctionInfo.h
1 //== AMDILMachineFunctionInfo.h - AMD il 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 AMDIL-specific per-machine-function information
11 //
12 //===----------------------------------------------------------------------===//
13 #ifndef _AMDILMACHINEFUNCTIONINFO_H_
14 #define _AMDILMACHINEFUNCTIONINFO_H_
15 #include "AMDIL.h"
16 #include "AMDILDevice.h"
17 #include "AMDILKernel.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/DenseSet.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/ADT/ValueMap.h"
23 #include "llvm/CodeGen/MachineBasicBlock.h"
24 #include "llvm/CodeGen/MachineFunction.h"
25 #include "llvm/Function.h"
26
27 #include <map>
28 #include <set>
29 #include <string>
30
31 namespace llvm
32 {
33   class AMDILSubtarget;
34   class PrintfInfo {
35     uint32_t mPrintfID;
36     SmallVector<uint32_t, DEFAULT_VEC_SLOTS> mOperands;
37     public:
38     void addOperand(size_t idx, uint32_t size);
39     uint32_t getPrintfID();
40     void setPrintfID(uint32_t idx);
41     size_t getNumOperands();
42     uint32_t getOperandID(uint32_t idx);
43   }; // class PrintfInfo
44
45   enum NameDecorationStyle
46   {
47     None,
48     StdCall,
49     FastCall
50   };
51   typedef struct SamplerInfoRec {
52     std::string name; // The name of the sampler
53     uint32_t val; // The value of the sampler
54     uint32_t idx; // The sampler resource id
55   } SamplerInfo;
56   // Some typedefs that will help with using the various iterators
57   // of the machine function info class.
58   typedef std::map<uint32_t, uint32_t>::iterator lit32_iterator;
59   typedef std::map<uint64_t, uint32_t>::iterator lit64_iterator;
60   typedef std::map<std::pair<uint64_t, uint64_t>, uint32_t>::iterator
61     lit128_iterator;
62   typedef StringMap<SamplerInfo>::iterator sampler_iterator;
63   typedef DenseSet<uint32_t>::iterator func_iterator;
64   typedef DenseSet<uint32_t>::iterator intr_iterator;
65   typedef DenseSet<uint32_t>::iterator uav_iterator;
66   typedef DenseSet<uint32_t>::iterator read_image2d_iterator;
67   typedef DenseSet<uint32_t>::iterator read_image3d_iterator;
68   typedef DenseSet<uint32_t>::iterator write_image2d_iterator;
69   typedef DenseSet<uint32_t>::iterator write_image3d_iterator;
70   typedef DenseSet<const char*>::iterator error_iterator;
71   typedef std::map<std::string, PrintfInfo*>::iterator printf_iterator;
72   typedef std::set<std::string>::iterator func_md_iterator;
73   typedef std::vector<std::string>::iterator kernel_md_iterator;
74   // AMDILMachineFunctionInfo - This class is
75   // derived from MachineFunction private
76   // amdil target-specific information for each MachineFunction
77   class AMDILMachineFunctionInfo : public MachineFunctionInfo
78   {
79     // CalleeSavedFrameSize - Size of the callee-saved
80     // register portion of the
81     // stack frame in bytes.
82     unsigned int CalleeSavedFrameSize;
83     // BytesToPopOnReturn - Number of bytes function pops on return.
84     // Used on windows platform for stdcall & fastcall name decoration
85     unsigned int BytesToPopOnReturn;
86     // DecorationStyle - If the function requires additional
87     // name decoration,
88     // DecorationStyle holds the right way to do so.
89     NameDecorationStyle DecorationStyle;
90     // ReturnAddrIndex - FrameIndex for return slot.
91     int ReturnAddrIndex;
92
93     // TailCallReturnAddrDelta - Delta the ReturnAddr stack slot is moved
94     // Used for creating an area before the register spill area
95     // on the stack
96     // the returnaddr can be savely move to this area
97     int TailCallReturnAddrDelta;
98
99     // SRetReturnReg - Some subtargets require that sret lowering includes
100     // returning the value of the returned struct in a register.
101     // This field holds the virtual register into which the sret
102     // argument is passed.
103     unsigned int SRetReturnReg;
104
105     // UsesLocal - Specifies that this function uses LDS memory and
106     // that it needs to be allocated.
107     bool UsesLDS;
108
109     // LDSArg - Flag that specifies if this function has an Local
110     // argument or not
111     bool LDSArg;
112
113     // UsesGDS - Specifies that this function uses GDS memory and
114     // that it needs to be allocated.
115     bool UsesGDS;
116
117     // GDSArg - Flag that specifies if this function has an Region
118     // argument or not
119     bool GDSArg;
120
121     // The size in bytes required to host all of the kernel arguments.
122     // -1 means this value has not been determined yet.
123     int32_t mArgSize;
124
125     // The size in bytes required to host the stack and the kernel arguments
126     // in private memory.
127     // -1 means this value has not been determined yet.
128     int32_t mScratchSize;
129
130     // The size in bytes required to host the the kernel arguments
131     // on the stack.
132     // -1 means this value has not been determined yet.
133     int32_t mStackSize;
134
135     /// A map of constant to literal mapping for all of the 32bit or
136     /// smaller literals in the current function.
137     std::map<uint32_t, uint32_t> mIntLits;
138
139     /// A map of constant to literal mapping for all of the 64bit
140     /// literals in the current function.
141     std::map<uint64_t, uint32_t> mLongLits;
142
143     /// A map of constant to literal mapping for all of the 128bit
144     /// literals in the current function.
145     std::map<std::pair<uint64_t, uint64_t>, uint32_t> mVecLits;
146
147     /// The number of literals that should be reserved.
148     /// TODO: Remove this when the wrapper emitter is added.
149     uint32_t mReservedLits;
150
151     /// A map of name to sampler information that is used to emit
152     /// metadata to the IL stream that the runtimes can use for
153     /// hardware setup.
154     StringMap<SamplerInfo> mSamplerMap;
155
156     /// Array of flags to specify if a specific memory type is used or not.
157     bool mUsedMem[AMDILDevice::MAX_IDS];
158
159     /// Set of all functions that this function calls.
160     DenseSet<uint32_t> mFuncs;
161
162     /// Set of all intrinsics that this function calls.
163     DenseSet<uint32_t> mIntrs;
164
165     /// Set of all read only 2D images.
166     DenseSet<uint32_t> mRO2D;
167     /// Set of all read only 3D images.
168     DenseSet<uint32_t> mRO3D;
169     /// Set of all write only 2D images.
170     DenseSet<uint32_t> mWO2D;
171     /// Set of all write only 3D images.
172     DenseSet<uint32_t> mWO3D;
173     /// Set of all the raw uavs.
174     DenseSet<uint32_t> mRawUAV;
175     /// Set of all the arena uavs.
176     DenseSet<uint32_t> mArenaUAV;
177
178     /// A set of all errors that occured in the backend for this function.
179     DenseSet<const char *> mErrors;
180
181     /// A mapping of printf data and the printf string
182     std::map<std::string, PrintfInfo*> mPrintfMap;
183
184     /// A set of all of the metadata that is used for the current function.
185     std::set<std::string> mMetadataFunc;
186
187     /// A set of all of the metadata that is used for the function wrapper.
188     std::vector<std::string> mMetadataKernel;
189
190     /// Information about the kernel, NULL if the function is not a kernel.
191     AMDILKernel *mKernel;
192
193     /// Pointer to the machine function that this information belongs to.
194     MachineFunction *mMF;
195
196     /// Pointer to the subtarget for this function.
197     const AMDILSubtarget *mSTM;
198     public:
199     AMDILMachineFunctionInfo();
200     AMDILMachineFunctionInfo(MachineFunction &MF);
201     virtual ~AMDILMachineFunctionInfo();
202     unsigned int
203       getCalleeSavedFrameSize() const;
204     void
205       setCalleeSavedFrameSize(unsigned int bytes);
206
207     unsigned int
208       getBytesToPopOnReturn() const;
209     void
210       setBytesToPopOnReturn (unsigned int bytes);
211
212     NameDecorationStyle
213       getDecorationStyle() const;
214     void
215       setDecorationStyle(NameDecorationStyle style);
216
217     int
218       getRAIndex() const;
219     void
220       setRAIndex(int Index);
221
222     int
223       getTCReturnAddrDelta() const;
224     void
225       setTCReturnAddrDelta(int delta);
226
227     unsigned int
228       getSRetReturnReg() const;
229     void
230       setSRetReturnReg(unsigned int Reg);
231
232     void 
233       setUsesLocal();
234     bool 
235       usesLocal() const;
236     void
237       setHasLocalArg();
238     bool 
239       hasLocalArg() const;
240
241     void 
242       setUsesRegion();
243     bool 
244       usesRegion() const;
245     void
246       setHasRegionArg();
247     bool 
248       hasRegionArg() const;
249
250     bool
251       usesHWConstant(std::string name) const;
252     uint32_t
253       getLocal(uint32_t);
254     bool
255       isKernel() const;
256     AMDILKernel*
257       getKernel();
258
259     std::string
260       getName();
261
262     /// Get the size in bytes that are required to host all of
263     /// arguments based on the argument alignment rules in the AMDIL 
264     /// Metadata spec.
265     uint32_t getArgSize();
266
267     /// Get the size in bytes that are required to host all of
268     /// arguments and stack memory in scratch.
269     uint32_t getScratchSize();
270
271     /// Get the size in bytes that is required to host all of
272     /// the arguments on the stack.
273     uint32_t getStackSize();
274
275     ///
276     /// @param val value to add the lookup table
277     /// @param Opcode opcode of the literal instruction
278     /// @brief adds the specified value of the type represented by the
279     /// Opcode
280     /// to the literal to integer and integer to literal mappings.
281     ///
282     /// Add a 32bit integer value to the literal table.
283     uint32_t addi32Literal(uint32_t val, int Opcode = AMDIL::LOADCONST_i32);
284
285     /// Add a 32bit floating point value to the literal table.
286     uint32_t addf32Literal(const ConstantFP *CFP);
287
288     /// Add a 64bit integer value to the literal table.
289     uint32_t addi64Literal(uint64_t val);
290
291     /// Add a 128 bit integer value to the literal table.
292     uint32_t addi128Literal(uint64_t val_lo, uint64_t val_hi);
293
294     /// Add a 64bit floating point literal as a 64bit integer value.
295     uint32_t addf64Literal(const ConstantFP *CFP);
296
297     /// Get the number of literals that have currently been allocated.
298     size_t getNumLiterals() const;
299
300     /// Get the literal ID of an Integer literal of the given offset.
301     uint32_t getIntLits(uint32_t lit);
302
303     /// Get the literal ID of a Long literal of the given offset.
304     uint32_t getLongLits(uint64_t lit);
305
306     /// Get the literal ID of a Long literal of the given offset.
307     uint32_t getVecLits(uint64_t low64, uint64_t high64);
308
309     /// Add some literals to the number of reserved literals.
310     void addReservedLiterals(uint32_t);
311
312     // Functions that return iterators to the beginning and end
313     // of the various literal maps.
314     // Functions that return the beginning and end of the 32bit literal map
315     lit32_iterator begin_32() { return mIntLits.begin(); }
316     lit32_iterator end_32() { return mIntLits.end(); }
317
318     // Functions that return the beginning and end of the 64bit literal map
319     lit64_iterator begin_64() { return mLongLits.begin(); }
320     lit64_iterator end_64() { return mLongLits.end(); }
321
322     // Functions that return the beginning and end of the 2x64bit literal map
323     lit128_iterator begin_128() { return mVecLits.begin(); }
324     lit128_iterator end_128() { return mVecLits.end(); }
325
326     // Add a sampler to the set of known samplers for the current kernel.
327     uint32_t addSampler(std::string name, uint32_t value);
328     
329     // Iterators that point to the beginning and end of the sampler map.
330     sampler_iterator sampler_begin() { return mSamplerMap.begin(); }
331     sampler_iterator sampler_end() { return mSamplerMap.end(); }
332
333
334     /// Set the flag for the memory ID to true for the current function.
335     void setUsesMem(unsigned);
336     /// Retrieve the flag for the memory ID.
337     bool usesMem(unsigned);
338
339     /// Add called functions to the set of all functions this function calls.
340     void addCalledFunc(uint32_t id) { mFuncs.insert(id); }
341     void eraseCalledFunc(uint32_t id) { mFuncs.erase(id); }
342     size_t func_size() { return mFuncs.size(); }
343     bool func_empty() { return mFuncs.empty(); }
344     func_iterator func_begin() { return mFuncs.begin(); }
345     func_iterator func_end() { return mFuncs.end(); }
346
347     /// Add called intrinsics to the set of all intrinscis this function calls.
348     void addCalledIntr(uint32_t id) { mIntrs.insert(id); }
349     size_t intr_size() { return mIntrs.size(); }
350     bool intr_empty() { return mIntrs.empty(); }
351     intr_iterator intr_begin() { return mIntrs.begin(); }
352     intr_iterator intr_end() { return mIntrs.end(); }
353
354     /// Add a 2D read_only image id.
355     void addROImage2D(uint32_t id) { mRO2D.insert(id); }
356     size_t read_image2d_size() { return mRO2D.size(); }
357     read_image2d_iterator read_image2d_begin() { return mRO2D.begin(); }
358     read_image2d_iterator read_image2d_end() { return mRO2D.end(); }
359
360     /// Add a 3D read_only image id.
361     void addROImage3D(uint32_t id) { mRO3D.insert(id); }
362     size_t read_image3d_size() { return mRO3D.size(); }
363     read_image3d_iterator read_image3d_begin() { return mRO3D.begin(); }
364     read_image3d_iterator read_image3d_end() { return mRO3D.end(); }
365
366     /// Add a 2D write_only image id.
367     void addWOImage2D(uint32_t id) { mWO2D.insert(id); }
368     size_t write_image2d_size() { return mWO2D.size(); }
369     write_image2d_iterator write_image2d_begin() { return mWO2D.begin(); }
370     write_image2d_iterator write_image2d_end() { return mWO2D.end(); }
371
372        /// Add a 3D write_only image id.
373     void addWOImage3D(uint32_t id) { mWO3D.insert(id); }
374     size_t write_image3d_size() { return mWO3D.size(); }
375     write_image3d_iterator write_image3d_begin() { return mWO3D.begin(); }
376     write_image3d_iterator write_image3d_end() { return mWO3D.end(); }
377
378     /// Add a raw uav id.
379     void uav_insert(uint32_t id) { mRawUAV.insert(id); }
380     bool uav_count(uint32_t id) { return mRawUAV.count(id); }
381     size_t uav_size() { return mRawUAV.size(); }
382     uav_iterator uav_begin() { return mRawUAV.begin(); }
383     uav_iterator uav_end() { return mRawUAV.end(); }
384
385     /// Add an arena uav id.
386     void arena_insert(uint32_t id) { mArenaUAV.insert(id); }
387     bool arena_count(uint32_t id) { return mArenaUAV.count(id); }
388     size_t arena_size() { return mArenaUAV.size(); }
389     uav_iterator arena_begin() { return mArenaUAV.begin(); }
390     uav_iterator arena_end() { return mArenaUAV.end(); }
391
392     // Add an error to the output for the current function.
393     typedef enum {
394       RELEASE_ONLY, /// Only emit error message in release mode.
395       DEBUG_ONLY, /// Only emit error message in debug mode.
396       ALWAYS /// Always emit the error message.
397     } ErrorMsgEnum;
398     /// Add an error message to the set of all error messages.
399     void addErrorMsg(const char* msg, ErrorMsgEnum val = ALWAYS);
400     bool errors_empty() { return mErrors.empty(); }
401     error_iterator errors_begin() { return mErrors.begin(); }
402     error_iterator errors_end() { return mErrors.end(); }
403
404     /// Add a string to the printf map
405     uint32_t addPrintfString(std::string &name, unsigned offset);
406     /// Add a operand to the printf string
407     void addPrintfOperand(std::string &name, size_t idx, uint32_t size);
408     bool printf_empty() { return mPrintfMap.empty(); }
409     size_t printf_size() { return mPrintfMap.size(); }
410     printf_iterator printf_begin() { return mPrintfMap.begin(); }
411     printf_iterator printf_end() { return mPrintfMap.end(); }
412
413     /// Add a string to the metadata set for a function/kernel wrapper
414     void addMetadata(const char *md, bool kernelOnly = false);
415     void addMetadata(std::string md, bool kernelOnly = false);
416     func_md_iterator func_md_begin() { return mMetadataFunc.begin(); }
417     func_md_iterator func_md_end() { return mMetadataFunc.end(); }
418     kernel_md_iterator kernel_md_begin() { return mMetadataKernel.begin(); }
419     kernel_md_iterator kernel_md_end() { return mMetadataKernel.end(); }
420   };
421 } // llvm namespace
422 #endif // _AMDILMACHINEFUNCTIONINFO_H_