• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 Christoph Bumiller
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef __NV50_IR_H__
24 #define __NV50_IR_H__
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdint.h>
29 #include <deque>
30 #include <list>
31 #include <unordered_set>
32 #include <vector>
33 
34 #include "nv50_ir_util.h"
35 #include "nv50_ir_graph.h"
36 
37 #include "nv50_ir_driver.h"
38 
39 namespace nv50_ir {
40 
41 enum operation
42 {
43    OP_NOP = 0,
44    OP_PHI,
45    OP_UNION, // unify a new definition and several source values
46    OP_SPLIT, // $r0d -> { $r0, $r1 } ($r0d and $r0/$r1 will be coalesced)
47    OP_MERGE, // opposite of split, e.g. combine 2 32 bit into a 64 bit value
48    OP_CONSTRAINT, // copy values into consecutive registers
49    OP_MOV, // simple copy, no modifiers allowed
50    OP_LOAD,
51    OP_STORE,
52    OP_ADD, // NOTE: add u64 + u32 is legal for targets w/o 64-bit integer adds
53    OP_SUB,
54    OP_MUL,
55    OP_DIV,
56    OP_MOD,
57    OP_MAD,
58    OP_FMA,
59    OP_SAD, // abs(src0 - src1) + src2
60    OP_SHLADD,
61    // extended multiply-add (GM107+), does a lot of things.
62    // see envytools for detailed documentation
63    OP_XMAD,
64    OP_ABS,
65    OP_NEG,
66    OP_NOT,
67    OP_AND,
68    OP_OR,
69    OP_XOR,
70    OP_LOP3_LUT,
71    OP_SHL,
72    OP_SHR,
73    OP_SHF,
74    OP_MAX,
75    OP_MIN,
76    OP_SAT, // CLAMP(f32, 0.0, 1.0)
77    OP_CEIL,
78    OP_FLOOR,
79    OP_TRUNC,
80    OP_CVT,
81    OP_SET_AND, // dst = (src0 CMP src1) & src2
82    OP_SET_OR,
83    OP_SET_XOR,
84    OP_SET,
85    OP_SELP, // dst = src2 ? src0 : src1
86    OP_SLCT, // dst = (src2 CMP 0) ? src0 : src1
87    OP_RCP,
88    OP_RSQ,
89    OP_LG2,
90    OP_SIN,
91    OP_COS,
92    OP_EX2,
93    OP_EXP, // exponential (base M_E)
94    OP_LOG, // natural logarithm
95    OP_PRESIN,
96    OP_PREEX2,
97    OP_SQRT,
98    OP_POW,
99    OP_BRA,
100    OP_CALL,
101    OP_RET,
102    OP_CONT,
103    OP_BREAK,
104    OP_PRERET,
105    OP_PRECONT,
106    OP_PREBREAK,
107    OP_BRKPT,     // breakpoint (not related to loops)
108    OP_JOINAT,    // push control flow convergence point
109    OP_JOIN,      // converge
110    OP_DISCARD,
111    OP_EXIT,
112    OP_MEMBAR, // memory barrier (mfence, lfence, sfence)
113    OP_VFETCH, // indirection 0 in attribute space, indirection 1 is vertex base
114    OP_PFETCH, // fetch base address of vertex src0 (immediate) [+ src1]
115    OP_AFETCH, // fetch base address of shader input (a[%r1+0x10])
116    OP_EXPORT,
117    OP_LINTERP,
118    OP_PINTERP,
119    OP_EMIT,    // emit vertex
120    OP_RESTART, // restart primitive
121    OP_FINAL, // finish emitting primitives
122    OP_TEX,
123    OP_TXB, // texture bias
124    OP_TXL, // texture lod
125    OP_TXF, // texel fetch
126    OP_TXQ, // texture size query
127    OP_TXD, // texture derivatives
128    OP_TXG, // texture gather
129    OP_TXLQ, // texture query lod
130    OP_TEXCSAA, // texture op for coverage sampling
131    OP_TEXPREP, // turn cube map array into 2d array coordinates
132    OP_SULDB, // surface load (raw)
133    OP_SULDP, // surface load (formatted)
134    OP_SUSTB, // surface store (raw)
135    OP_SUSTP, // surface store (formatted)
136    OP_SUREDB,
137    OP_SUREDP, // surface reduction (atomic op)
138    OP_SULEA,   // surface load effective address
139    OP_SUBFM,   // surface bitfield manipulation
140    OP_SUCLAMP, // clamp surface coordinates
141    OP_SUEAU,   // surface effective address
142    OP_SUQ,     // surface query
143    OP_MADSP,   // special integer multiply-add
144    OP_TEXBAR, // texture dependency barrier
145    OP_DFDX,
146    OP_DFDY,
147    OP_RDSV, // read system value
148    OP_WRSV, // write system value
149    OP_PIXLD, // get info about raster object or surfaces
150    OP_QUADOP,
151    OP_QUADON,
152    OP_QUADPOP,
153    OP_POPCNT, // bitcount(src0 & src1)
154    OP_INSBF,  // insert first src1[8:15] bits of src0 into src2 at src1[0:7]
155    OP_EXTBF,  // place bits [K,K+N) of src0 into dst, src1 = 0xNNKK
156    OP_BFIND,  // find highest/lowest set bit
157    OP_BREV,   // bitfield reverse
158    OP_BMSK,   // bitfield mask
159    OP_PERMT,  // dst = bytes from src2,src0 selected by src1 (nvc0's src order)
160    OP_SGXT,
161    OP_ATOM,
162    OP_BAR,    // execution barrier, sources = { id, thread count, predicate }
163    OP_VADD,   // byte/word vector operations
164    OP_VAVG,
165    OP_VMIN,
166    OP_VMAX,
167    OP_VSAD,
168    OP_VSET,
169    OP_VSHR,
170    OP_VSHL,
171    OP_VSEL,
172    OP_CCTL, // cache control
173    OP_SHFL, // warp shuffle
174    OP_VOTE,
175    OP_BUFQ, // buffer query
176    OP_WARPSYNC,
177    OP_LAST
178 };
179 
180 // various instruction-specific modifier definitions Instruction::subOp
181 // MOV_FINAL marks a MOV originating from an EXPORT (used for placing TEXBARs)
182 #define NV50_IR_SUBOP_MUL_HIGH     1
183 #define NV50_IR_SUBOP_EMIT_RESTART 1
184 #define NV50_IR_SUBOP_LDC_IL       1
185 #define NV50_IR_SUBOP_LDC_IS       2
186 #define NV50_IR_SUBOP_LDC_ISL      3
187 #define NV50_IR_SUBOP_SHIFT_WRAP   1
188 #define NV50_IR_SUBOP_SHIFT_HIGH   2
189 #define NV50_IR_SUBOP_EMU_PRERET   1
190 #define NV50_IR_SUBOP_TEXBAR(n)    n
191 #define NV50_IR_SUBOP_MOV_FINAL    1
192 #define NV50_IR_SUBOP_EXTBF_REV    1
193 #define NV50_IR_SUBOP_BFIND_SAMT   1
194 #define NV50_IR_SUBOP_RCPRSQ_64H   1
195 #define NV50_IR_SUBOP_PERMT_F4E    1
196 #define NV50_IR_SUBOP_PERMT_B4E    2
197 #define NV50_IR_SUBOP_PERMT_RC8    3
198 #define NV50_IR_SUBOP_PERMT_ECL    4
199 #define NV50_IR_SUBOP_PERMT_ECR    5
200 #define NV50_IR_SUBOP_PERMT_RC16   6
201 #define NV50_IR_SUBOP_BAR_SYNC     0
202 #define NV50_IR_SUBOP_BAR_ARRIVE   1
203 #define NV50_IR_SUBOP_BAR_RED_AND  2
204 #define NV50_IR_SUBOP_BAR_RED_OR   3
205 #define NV50_IR_SUBOP_BAR_RED_POPC 4
206 #define NV50_IR_SUBOP_MEMBAR_L     1
207 #define NV50_IR_SUBOP_MEMBAR_S     2
208 #define NV50_IR_SUBOP_MEMBAR_M     3
209 #define NV50_IR_SUBOP_MEMBAR_CTA  (0 << 2)
210 #define NV50_IR_SUBOP_MEMBAR_GL   (1 << 2)
211 #define NV50_IR_SUBOP_MEMBAR_SYS  (2 << 2)
212 #define NV50_IR_SUBOP_MEMBAR_DIR(m)   ((m) & 0x3)
213 #define NV50_IR_SUBOP_MEMBAR_SCOPE(m) ((m) & ~0x3)
214 #define NV50_IR_SUBOP_MEMBAR(d,s) \
215    (NV50_IR_SUBOP_MEMBAR_##d | NV50_IR_SUBOP_MEMBAR_##s)
216 #define NV50_IR_SUBOP_ATOM_ADD      0
217 #define NV50_IR_SUBOP_ATOM_MIN      1
218 #define NV50_IR_SUBOP_ATOM_MAX      2
219 #define NV50_IR_SUBOP_ATOM_INC      3
220 #define NV50_IR_SUBOP_ATOM_DEC      4
221 #define NV50_IR_SUBOP_ATOM_AND      5
222 #define NV50_IR_SUBOP_ATOM_OR       6
223 #define NV50_IR_SUBOP_ATOM_XOR      7
224 #define NV50_IR_SUBOP_ATOM_CAS      8
225 #define NV50_IR_SUBOP_ATOM_EXCH     9
226 #define NV50_IR_SUBOP_CCTL_IV      5
227 #define NV50_IR_SUBOP_CCTL_IVALL   6
228 #define NV50_IR_SUBOP_SUST_IGN     0
229 #define NV50_IR_SUBOP_SUST_TRAP    1
230 #define NV50_IR_SUBOP_SUST_SDCL    3
231 #define NV50_IR_SUBOP_SULD_ZERO    0
232 #define NV50_IR_SUBOP_SULD_TRAP    1
233 #define NV50_IR_SUBOP_SULD_SDCL    3
234 #define NV50_IR_SUBOP_SUBFM_3D     1
235 #define NV50_IR_SUBOP_SUCLAMP_2D   0x10
236 #define NV50_IR_SUBOP_SUCLAMP_SD(r, d) (( 0 + (r)) | ((d == 2) ? 0x10 : 0))
237 #define NV50_IR_SUBOP_SUCLAMP_PL(r, d) (( 5 + (r)) | ((d == 2) ? 0x10 : 0))
238 #define NV50_IR_SUBOP_SUCLAMP_BL(r, d) ((10 + (r)) | ((d == 2) ? 0x10 : 0))
239 #define NV50_IR_SUBOP_PIXLD_COUNT       0
240 #define NV50_IR_SUBOP_PIXLD_COVMASK     1
241 #define NV50_IR_SUBOP_PIXLD_COVERED     2
242 #define NV50_IR_SUBOP_PIXLD_OFFSET      3
243 #define NV50_IR_SUBOP_PIXLD_CENT_OFFSET 4
244 #define NV50_IR_SUBOP_PIXLD_SAMPLEID    5
245 #define NV50_IR_SUBOP_SHFL_IDX  0
246 #define NV50_IR_SUBOP_SHFL_UP   1
247 #define NV50_IR_SUBOP_SHFL_DOWN 2
248 #define NV50_IR_SUBOP_SHFL_BFLY 3
249 #define NV50_IR_SUBOP_LOAD_LOCKED    1
250 #define NV50_IR_SUBOP_STORE_UNLOCKED 2
251 #define NV50_IR_SUBOP_MADSP_SD     0xffff
252 // Yes, we could represent those with DataType.
253 // Or put the type into operation and have a couple 1000 values in that enum.
254 // This will have to do for now.
255 // The bitfields are supposed to correspond to nve4 ISA.
256 #define NV50_IR_SUBOP_MADSP(a,b,c) (((c) << 8) | ((b) << 4) | (a))
257 #define NV50_IR_SUBOP_V1(d,a,b)    (((d) << 10) | ((b) << 5) | (a) | 0x0000)
258 #define NV50_IR_SUBOP_V2(d,a,b)    (((d) << 10) | ((b) << 5) | (a) | 0x4000)
259 #define NV50_IR_SUBOP_V4(d,a,b)    (((d) << 10) | ((b) << 5) | (a) | 0x8000)
260 #define NV50_IR_SUBOP_Vn(n)        ((n) >> 14)
261 #define NV50_IR_SUBOP_VOTE_ALL 0
262 #define NV50_IR_SUBOP_VOTE_ANY 1
263 #define NV50_IR_SUBOP_VOTE_UNI 2
264 #define NV50_IR_SUBOP_LOP3_LUT_SRC0 0xf0
265 #define NV50_IR_SUBOP_LOP3_LUT_SRC1 0xcc
266 #define NV50_IR_SUBOP_LOP3_LUT_SRC2 0xaa
267 #define NV50_IR_SUBOP_LOP3_LUT(exp) ({         \
268       uint8_t a = NV50_IR_SUBOP_LOP3_LUT_SRC0; \
269       uint8_t b = NV50_IR_SUBOP_LOP3_LUT_SRC1; \
270       uint8_t c = NV50_IR_SUBOP_LOP3_LUT_SRC2; \
271       (uint8_t)(exp);                          \
272 })
273 #define NV50_IR_SUBOP_BMSK_C (0 << 0)
274 #define NV50_IR_SUBOP_BMSK_W (1 << 0)
275 
276 #define NV50_IR_SUBOP_MINMAX_LOW  1
277 #define NV50_IR_SUBOP_MINMAX_MED  2
278 #define NV50_IR_SUBOP_MINMAX_HIGH 3
279 
280 #define NV50_IR_SUBOP_SHF_L  (0 << 0)
281 #define NV50_IR_SUBOP_SHF_R  (1 << 0)
282 #define NV50_IR_SUBOP_SHF_LO (0 << 1)
283 #define NV50_IR_SUBOP_SHF_HI (1 << 1)
284 #define NV50_IR_SUBOP_SHF_C  (0 << 2)
285 #define NV50_IR_SUBOP_SHF_W  (1 << 2)
286 
287 #define NV50_IR_SUBOP_VFETCH_PHYS 1
288 
289 // xmad(src0, src1, 0) << 16 + src2
290 #define NV50_IR_SUBOP_XMAD_PSL (1 << 0)
291 // (xmad(src0, src1, src2) & 0xffff) | (src1 << 16)
292 #define NV50_IR_SUBOP_XMAD_MRG (1 << 1)
293 // xmad(src0, src1, src2.lo)
294 #define NV50_IR_SUBOP_XMAD_CLO (1 << 2)
295 // xmad(src0, src1, src2.hi)
296 #define NV50_IR_SUBOP_XMAD_CHI (2 << 2)
297 // if both operands to the multiplication are non-zero, subtract 65536 for each
298 // negative operand
299 #define NV50_IR_SUBOP_XMAD_CSFU (3 << 2)
300 // xmad(src0, src1, src2) + src1 << 16
301 #define NV50_IR_SUBOP_XMAD_CBCC (4 << 2)
302 #define NV50_IR_SUBOP_XMAD_CMODE_SHIFT 2
303 #define NV50_IR_SUBOP_XMAD_CMODE_MASK (0x7 << NV50_IR_SUBOP_XMAD_CMODE_SHIFT)
304 
305 // use the high 16 bits instead of the low 16 bits for the multiplication.
306 // if the instruction's sType is signed, sign extend the operand from 16 bits
307 // to 32 before multiplication.
308 #define NV50_IR_SUBOP_XMAD_H1_SHIFT 5
309 #define NV50_IR_SUBOP_XMAD_H1(i) (1 << (NV50_IR_SUBOP_XMAD_H1_SHIFT + (i)))
310 #define NV50_IR_SUBOP_XMAD_H1_MASK (0x3 << NV50_IR_SUBOP_XMAD_H1_SHIFT)
311 
312 enum DataType
313 {
314    TYPE_NONE,
315    TYPE_U8,
316    TYPE_S8,
317    TYPE_U16,
318    TYPE_S16,
319    TYPE_U32,
320    TYPE_S32,
321    TYPE_U64, // 64 bit operations are only lowered after register allocation
322    TYPE_S64,
323    TYPE_F16,
324    TYPE_F32,
325    TYPE_F64,
326    TYPE_B96,
327    TYPE_B128
328 };
329 
330 enum CondCode
331 {
332    CC_FL = 0,
333    CC_NEVER = CC_FL, // when used with FILE_FLAGS
334    CC_LT = 1,
335    CC_EQ = 2,
336    CC_NOT_P = CC_EQ, // when used with FILE_PREDICATE
337    CC_LE = 3,
338    CC_GT = 4,
339    CC_NE = 5,
340    CC_P  = CC_NE,
341    CC_GE = 6,
342    CC_TR = 7,
343    CC_ALWAYS = CC_TR,
344    CC_U  = 8,
345    CC_LTU = 9,
346    CC_EQU = 10,
347    CC_LEU = 11,
348    CC_GTU = 12,
349    CC_NEU = 13,
350    CC_GEU = 14,
351    CC_NO = 0x10,
352    CC_NC = 0x11,
353    CC_NS = 0x12,
354    CC_NA = 0x13,
355    CC_A  = 0x14,
356    CC_S  = 0x15,
357    CC_C  = 0x16,
358    CC_O  = 0x17
359 };
360 
361 enum RoundMode
362 {
363    ROUND_N, // nearest
364    ROUND_M, // towards -inf
365    ROUND_Z, // towards 0
366    ROUND_P, // towards +inf
367    ROUND_NI, // nearest integer
368    ROUND_MI, // to integer towards -inf
369    ROUND_ZI, // to integer towards 0
370    ROUND_PI, // to integer towards +inf
371 };
372 
373 enum CacheMode
374 {
375    CACHE_CA,            // cache at all levels
376    CACHE_WB = CACHE_CA, // cache write back
377    CACHE_CG,            // cache at global level
378    CACHE_CS,            // cache streaming
379    CACHE_CV,            // cache as volatile
380    CACHE_WT = CACHE_CV  // cache write-through
381 };
382 
383 enum DataFile
384 {
385    FILE_NULL = 0,
386    FILE_GPR,
387    FILE_PREDICATE,       // boolean predicate
388    FILE_FLAGS,           // zero/sign/carry/overflow bits
389    FILE_ADDRESS,
390    FILE_BARRIER,
391    LAST_REGISTER_FILE = FILE_BARRIER,
392    FILE_IMMEDIATE,
393    FILE_MEMORY_CONST,
394    FILE_SHADER_INPUT,
395    FILE_SHADER_OUTPUT,
396    FILE_MEMORY_BUFFER,
397    FILE_MEMORY_GLOBAL,
398    FILE_MEMORY_SHARED,
399    FILE_MEMORY_LOCAL,
400    FILE_SYSTEM_VALUE,
401    FILE_THREAD_STATE,           // "special" barrier registers
402    DATA_FILE_COUNT
403 };
404 
405 enum TexTarget
406 {
407    TEX_TARGET_1D,
408    TEX_TARGET_2D,
409    TEX_TARGET_2D_MS,
410    TEX_TARGET_3D,
411    TEX_TARGET_CUBE,
412    TEX_TARGET_1D_SHADOW,
413    TEX_TARGET_2D_SHADOW,
414    TEX_TARGET_CUBE_SHADOW,
415    TEX_TARGET_1D_ARRAY,
416    TEX_TARGET_2D_ARRAY,
417    TEX_TARGET_2D_MS_ARRAY,
418    TEX_TARGET_CUBE_ARRAY,
419    TEX_TARGET_1D_ARRAY_SHADOW,
420    TEX_TARGET_2D_ARRAY_SHADOW,
421    TEX_TARGET_RECT,
422    TEX_TARGET_RECT_SHADOW,
423    TEX_TARGET_CUBE_ARRAY_SHADOW,
424    TEX_TARGET_BUFFER,
425    TEX_TARGET_COUNT
426 };
427 
428 enum ImgFormat
429 {
430    FMT_NONE,
431 
432    FMT_RGBA32F,
433    FMT_RGBA16F,
434    FMT_RG32F,
435    FMT_RG16F,
436    FMT_R11G11B10F,
437    FMT_R32F,
438    FMT_R16F,
439 
440    FMT_RGBA32UI,
441    FMT_RGBA16UI,
442    FMT_RGB10A2UI,
443    FMT_RGBA8UI,
444    FMT_RG32UI,
445    FMT_RG16UI,
446    FMT_RG8UI,
447    FMT_R32UI,
448    FMT_R16UI,
449    FMT_R8UI,
450 
451    FMT_RGBA32I,
452    FMT_RGBA16I,
453    FMT_RGBA8I,
454    FMT_RG32I,
455    FMT_RG16I,
456    FMT_RG8I,
457    FMT_R32I,
458    FMT_R16I,
459    FMT_R8I,
460 
461    FMT_RGBA16,
462    FMT_RGB10A2,
463    FMT_RGBA8,
464    FMT_RG16,
465    FMT_RG8,
466    FMT_R16,
467    FMT_R8,
468 
469    FMT_RGBA16_SNORM,
470    FMT_RGBA8_SNORM,
471    FMT_RG16_SNORM,
472    FMT_RG8_SNORM,
473    FMT_R16_SNORM,
474    FMT_R8_SNORM,
475 
476    FMT_BGRA8,
477 
478    IMG_FORMAT_COUNT,
479 };
480 
481 enum ImgType {
482    UINT,
483    SINT,
484    UNORM,
485    SNORM,
486    FLOAT,
487 };
488 
489 enum SVSemantic
490 {
491    SV_POSITION, // WPOS
492    SV_VERTEX_ID,
493    SV_INSTANCE_ID,
494    SV_INVOCATION_ID,
495    SV_PRIMITIVE_ID,
496    SV_VERTEX_COUNT, // gl_PatchVerticesIn
497    SV_LAYER,
498    SV_VIEWPORT_INDEX,
499    SV_VIEWPORT_MASK,
500    SV_YDIR,
501    SV_FACE,
502    SV_POINT_SIZE,
503    SV_POINT_COORD,
504    SV_CLIP_DISTANCE,
505    SV_SAMPLE_INDEX,
506    SV_SAMPLE_POS,
507    SV_SAMPLE_MASK,
508    SV_TESS_OUTER,
509    SV_TESS_INNER,
510    SV_TESS_COORD,
511    SV_TID,
512    SV_COMBINED_TID,
513    SV_CTAID,
514    SV_NTID,
515    SV_GRIDID,
516    SV_NCTAID,
517    SV_LANEID,
518    SV_PHYSID,
519    SV_NPHYSID,
520    SV_CLOCK,
521    SV_LBASE,
522    SV_SBASE,
523    SV_VERTEX_STRIDE,
524    SV_INVOCATION_INFO,
525    SV_THREAD_KILL,
526    SV_BASEVERTEX,
527    SV_BASEINSTANCE,
528    SV_DRAWID,
529    SV_WORK_DIM,
530    SV_LANEMASK_EQ,
531    SV_LANEMASK_LT,
532    SV_LANEMASK_LE,
533    SV_LANEMASK_GT,
534    SV_LANEMASK_GE,
535    SV_UNDEFINED,
536    SV_LAST
537 };
538 
539 enum TSSemantic
540 {
541    // 0-15 are fixed ones on Volta/Turing
542    TS_THREAD_STATE_ENUM0 = 0,
543    TS_THREAD_STATE_ENUM1 = 1,
544    TS_THREAD_STATE_ENUM2 = 2,
545    TS_THREAD_STATE_ENUM3 = 3,
546    TS_THREAD_STATE_ENUM4 = 4,
547    TS_TRAP_RETURN_PC_LO  = 5,
548    TS_TRAP_RETURN_PC_HI  = 6,
549    TS_TRAP_RETURN_MASK   = 7,
550    TS_MEXITED            = 8,
551    TS_MKILL              = 9,
552    TS_MACTIVE            = 10,
553    TS_MATEXIT            = 11,
554    TS_OPT_STACK          = 12,
555    TS_API_CALL_DEPTH     = 13,
556    TS_ATEXIT_PC_LO       = 14,
557    TS_ATEXIT_PC_HI       = 15,
558    // special ones to make our life easier
559    TS_PQUAD_MACTIVE,
560 };
561 
562 class Program;
563 class Function;
564 class BasicBlock;
565 
566 class Target;
567 
568 class Instruction;
569 class CmpInstruction;
570 class TexInstruction;
571 class FlowInstruction;
572 
573 class Value;
574 class LValue;
575 class Symbol;
576 class ImmediateValue;
577 
578 struct Storage
579 {
580    DataFile file;
581    int8_t fileIndex; // signed, may be indirect for CONST[]
582    uint8_t size; // this should match the Instruction type's size
583    DataType type; // mainly for pretty printing
584    union {
585       uint64_t u64;    // immediate values
586       uint32_t u32;
587       uint16_t u16;
588       uint8_t u8;
589       int64_t s64;
590       int32_t s32;
591       int16_t s16;
592       int8_t s8;
593       float f32;
594       double f64;
595       int32_t offset; // offset from 0 (base of address space)
596       int32_t id;     // register id (< 0 if virtual/unassigned, in units <= 4)
597       struct {
598          SVSemantic sv;
599          int index;
600       } sv;
601       TSSemantic ts;
602    } data;
603 };
604 
605 // precedence: NOT after SAT after NEG after ABS
606 #define NV50_IR_MOD_ABS (1 << 0)
607 #define NV50_IR_MOD_NEG (1 << 1)
608 #define NV50_IR_MOD_SAT (1 << 2)
609 #define NV50_IR_MOD_NOT (1 << 3)
610 #define NV50_IR_MOD_NEG_ABS (NV50_IR_MOD_NEG | NV50_IR_MOD_ABS)
611 
612 #define NV50_IR_INTERP_MODE_MASK   0x3
613 #define NV50_IR_INTERP_LINEAR      (0 << 0)
614 #define NV50_IR_INTERP_PERSPECTIVE (1 << 0)
615 #define NV50_IR_INTERP_FLAT        (2 << 0)
616 #define NV50_IR_INTERP_SC          (3 << 0) // what exactly is that ?
617 #define NV50_IR_INTERP_SAMPLE_MASK 0xc
618 #define NV50_IR_INTERP_DEFAULT     (0 << 2)
619 #define NV50_IR_INTERP_CENTROID    (1 << 2)
620 #define NV50_IR_INTERP_OFFSET      (2 << 2)
621 #define NV50_IR_INTERP_SAMPLEID    (3 << 2)
622 
623 // do we really want this to be a class ?
624 class Modifier
625 {
626 public:
Modifier()627    Modifier() : bits(0) { }
Modifier(unsigned int m)628    Modifier(unsigned int m) : bits(m) { }
629    Modifier(operation op);
630 
631    // @return new Modifier applying a after b (asserts if unrepresentable)
632    Modifier operator*(const Modifier) const;
633    Modifier operator*=(const Modifier m) { *this = *this * m; return *this; }
634    Modifier operator==(const Modifier m) const { return m.bits == bits; }
635    Modifier operator!=(const Modifier m) const { return m.bits != bits; }
636 
637    inline Modifier operator&(const Modifier m) const { return bits & m.bits; }
638    inline Modifier operator|(const Modifier m) const { return bits | m.bits; }
639    inline Modifier operator^(const Modifier m) const { return bits ^ m.bits; }
640 
641    operation getOp() const;
642 
neg()643    inline int neg() const { return (bits & NV50_IR_MOD_NEG) ? 1 : 0; }
abs()644    inline int abs() const { return (bits & NV50_IR_MOD_ABS) ? 1 : 0; }
645 
646    inline operator bool() const { return bits ? true : false; }
647 
648    void applyTo(ImmediateValue &imm) const;
649 
650    int print(char *buf, size_t size) const;
651 
652 private:
653    uint8_t bits;
654 };
655 
656 class ValueRef
657 {
658 public:
659    ValueRef(Value * = NULL);
660    ValueRef(const ValueRef&);
661    ~ValueRef();
662 
exists()663    inline bool exists() const { return value != NULL; }
664 
665    void set(Value *);
666    void set(const ValueRef&);
get()667    inline Value *get() const { return value; }
668    inline Value *rep() const;
669 
getInsn()670    inline Instruction *getInsn() const { return insn; }
setInsn(Instruction * inst)671    inline void setInsn(Instruction *inst) { insn = inst; }
672 
isIndirect(int dim)673    inline bool isIndirect(int dim) const { return indirect[dim] >= 0; }
674    inline const ValueRef *getIndirect(int dim) const;
675 
676    inline DataFile getFile() const;
677    inline unsigned getSize() const;
678 
679    // SSA: return eventual (traverse MOVs) literal value, if it exists
680    bool getImmediate(ImmediateValue&) const;
681 
682 public:
683    Modifier mod;
684    int8_t indirect[2]; // >= 0 if relative to lvalue in insn->src(indirect[i])
685 
686    bool usedAsPtr; // for printing
687 
688 private:
689    Value *value;
690    Instruction *insn;
691 };
692 
693 class ValueDef
694 {
695 public:
696    ValueDef(Value * = NULL);
697    ValueDef(const ValueDef&);
698    ~ValueDef();
699 
exists()700    inline bool exists() const { return value != NULL; }
701 
get()702    inline Value *get() const { return value; }
703    inline Value *rep() const;
704    void set(Value *);
705    bool mayReplace(const ValueRef &);
706    void replace(const ValueRef &, bool doSet); // replace all uses of the old value
707 
getInsn()708    inline Instruction *getInsn() const { return insn; }
setInsn(Instruction * inst)709    inline void setInsn(Instruction *inst) { insn = inst; }
710 
711    inline DataFile getFile() const;
712    inline unsigned getSize() const;
713 
714    inline void setSSA(LValue *);
715    inline const LValue *preSSA() const;
716 
717 private:
718    Value *value;   // should make this LValue * ...
719    LValue *origin; // pre SSA value
720    Instruction *insn;
721 };
722 
723 class Value
724 {
725 public:
726    Value();
~Value()727    virtual ~Value() { }
728 
729    virtual Value *clone(ClonePolicy<Function>&) const = 0;
730 
731    virtual int print(char *, size_t, DataType ty = TYPE_NONE) const = 0;
732 
733    virtual bool equals(const Value *, bool strict = false) const;
734    virtual bool interfers(const Value *) const;
isUniform()735    virtual bool isUniform() const { return true; }
736 
rep()737    inline Value *rep() const { return join; }
738 
739    inline Instruction *getUniqueInsn() const;
740    inline Instruction *getInsn() const; // use when uniqueness is certain
741 
refCount()742    inline int refCount() { return uses.size(); }
743 
744    inline LValue *asLValue();
745    inline Symbol *asSym();
746    inline ImmediateValue *asImm();
747    inline const Symbol *asSym() const;
748    inline const ImmediateValue *asImm() const;
749 
inFile(DataFile f)750    inline bool inFile(DataFile f) const { return reg.file == f; }
751 
752    static inline Value *get(Iterator&);
753 
754    std::unordered_set<ValueRef *> uses;
755    std::list<ValueDef *> defs;
756    typedef std::unordered_set<ValueRef *>::iterator UseIterator;
757    typedef std::unordered_set<ValueRef *>::const_iterator UseCIterator;
758    typedef std::list<ValueDef *>::iterator DefIterator;
759    typedef std::list<ValueDef *>::const_iterator DefCIterator;
760 
761    int id;
762    Storage reg;
763 
764    // TODO: these should be in LValue:
765    Interval livei;
766    Value *join;
767 };
768 
769 class LValue : public Value
770 {
771 public:
772    LValue(Function *, DataFile file);
773    LValue(Function *, LValue *);
~LValue()774    ~LValue() { }
775 
776    virtual bool isUniform() const;
777 
778    virtual LValue *clone(ClonePolicy<Function>&) const;
779 
780    virtual int print(char *, size_t, DataType ty = TYPE_NONE) const;
781 
782 public:
783    unsigned compMask : 8; // compound/component mask
784    unsigned compound : 1; // used by RA, value involved in split/merge
785    unsigned ssa      : 1;
786    unsigned fixedReg : 1; // set & used by RA, earlier just use (id < 0)
787    unsigned noSpill  : 1; // do not spill (e.g. if spill temporary already)
788 };
789 
790 class Symbol : public Value
791 {
792 public:
793    Symbol(Program *, DataFile file = FILE_MEMORY_CONST, uint8_t fileIdx = 0);
~Symbol()794    ~Symbol() { }
795 
796    virtual Symbol *clone(ClonePolicy<Function>&) const;
797 
798    virtual bool equals(const Value *that, bool strict) const;
799 
800    virtual bool isUniform() const;
801 
802    virtual int print(char *, size_t, DataType ty = TYPE_NONE) const;
803 
804    // print with indirect values
805    int print(char *, size_t, Value *, Value *, DataType ty = TYPE_NONE) const;
806 
807    inline void setFile(DataFile file, uint8_t fileIndex = 0)
808    {
809       reg.file = file;
810       reg.fileIndex = fileIndex;
811    }
812 
813    inline void setOffset(int32_t offset);
814    inline void setAddress(Symbol *base, int32_t offset);
815    inline void setSV(SVSemantic sv, uint32_t idx = 0);
816 
getBase()817    inline const Symbol *getBase() const { return baseSym; }
818 
819 private:
820    Symbol *baseSym; // array base for Symbols representing array elements
821 };
822 
823 class ImmediateValue : public Value
824 {
825 public:
ImmediateValue()826    ImmediateValue() { }
827    ImmediateValue(Program *, uint32_t);
828    ImmediateValue(Program *, float);
829    ImmediateValue(Program *, double);
830    // NOTE: not added to program with
831    ImmediateValue(const ImmediateValue *, DataType ty);
~ImmediateValue()832    ~ImmediateValue() { };
833 
834    virtual ImmediateValue *clone(ClonePolicy<Function>&) const;
835 
836    virtual bool equals(const Value *that, bool strict) const;
837 
838    // these only work if 'type' is valid (we mostly use untyped literals):
839    bool isInteger(const int ival) const; // ival is cast to this' type
840    bool isNegative() const;
841    bool isPow2() const;
842 
843    void applyLog2();
844 
845    // for constant folding:
846    ImmediateValue operator+(const ImmediateValue&) const;
847    ImmediateValue operator-(const ImmediateValue&) const;
848    ImmediateValue operator*(const ImmediateValue&) const;
849    ImmediateValue operator/(const ImmediateValue&) const;
850 
851    ImmediateValue& operator=(const ImmediateValue&); // only sets value !
852 
853    bool compare(CondCode cc, float fval) const;
854 
855    virtual int print(char *, size_t, DataType ty = TYPE_NONE) const;
856 };
857 
858 class Instruction
859 {
860 public:
861    Instruction();
862    Instruction(Function *, operation, DataType);
863    virtual ~Instruction();
864 
865    virtual Instruction *clone(ClonePolicy<Function>&,
866                               Instruction * = NULL) const;
867 
868    void setDef(int i, Value *);
869    void setSrc(int s, Value *);
870    void setSrc(int s, const ValueRef&);
871    void swapSources(int a, int b);
872    void moveSources(int s, int delta);
873    bool setIndirect(int s, int dim, Value *);
874 
src(int s)875    inline ValueRef& src(int s) { return srcs[s]; }
def(int s)876    inline ValueDef& def(int s) { return defs[s]; }
src(int s)877    inline const ValueRef& src(int s) const { return srcs[s]; }
def(int s)878    inline const ValueDef& def(int s) const { return defs[s]; }
879 
getDef(int d)880    inline Value *getDef(int d) const { return defs[d].get(); }
getSrc(int s)881    inline Value *getSrc(int s) const { return srcs[s].get(); }
882    inline Value *getIndirect(int s, int dim) const;
883 
defExists(unsigned d)884    inline bool defExists(unsigned d) const
885    {
886       return d < defs.size() && defs[d].exists();
887    }
srcExists(unsigned s)888    inline bool srcExists(unsigned s) const
889    {
890       return s < srcs.size() && srcs[s].exists();
891    }
892 
893    inline bool constrainedDefs() const;
894 
895    bool setPredicate(CondCode ccode, Value *);
896    inline Value *getPredicate() const;
897    bool writesPredicate() const;
isPredicated()898    inline bool isPredicated() const { return predSrc >= 0; }
899 
900    inline void setFlagsSrc(int s, Value *);
901    inline void setFlagsDef(int d, Value *);
usesFlags()902    inline bool usesFlags() const { return flagsSrc >= 0; }
903 
defCount()904    unsigned int defCount() const { return defs.size(); };
905    unsigned int defCount(unsigned int mask, bool singleFile = false) const;
srcCount()906    unsigned int srcCount() const { return srcs.size(); };
907    unsigned int srcCount(unsigned int mask, bool singleFile = false) const;
908 
909    // save & remove / set indirect[0,1] and predicate source
910    void takeExtraSources(int s, Value *[3]);
911    void putExtraSources(int s, Value *[3]);
912 
setType(DataType type)913    inline void setType(DataType type) { dType = sType = type; }
914 
setType(DataType dtype,DataType stype)915    inline void setType(DataType dtype, DataType stype)
916    {
917       dType = dtype;
918       sType = stype;
919    }
920 
isPseudo()921    inline bool isPseudo() const { return op < OP_MOV; }
922    bool isDead() const;
923    bool isNop() const;
924    bool isCommutationLegal(const Instruction *) const; // must be adjacent !
925    bool isActionEqual(const Instruction *) const;
926    bool isResultEqual(const Instruction *) const;
927 
928    // check whether the defs interfere with srcs and defs of another instruction
929    bool canCommuteDefDef(const Instruction *) const;
930    bool canCommuteDefSrc(const Instruction *) const;
931 
932    void print() const;
933 
934    inline CmpInstruction *asCmp();
935    inline TexInstruction *asTex();
936    inline FlowInstruction *asFlow();
937    inline const TexInstruction *asTex() const;
938    inline const CmpInstruction *asCmp() const;
939    inline const FlowInstruction *asFlow() const;
940 
941 public:
942    Instruction *next;
943    Instruction *prev;
944    int id;
945    int serial; // CFG order
946 
947    operation op;
948    DataType dType; // destination or defining type
949    DataType sType; // source or secondary type
950    CondCode cc;
951    RoundMode rnd;
952    CacheMode cache;
953 
954    uint16_t subOp; // quadop, 1 for mul-high, etc.
955 
956    unsigned encSize    : 5; // encoding size in bytes
957    unsigned saturate   : 1; // to [0.0f, 1.0f]
958    unsigned join       : 1; // converge control flow (use OP_JOIN until end)
959    unsigned fixed      : 1; // prevent dead code elimination
960    unsigned terminator : 1; // end of basic block
961    unsigned ftz        : 1; // flush denormal to zero
962    unsigned dnz        : 1; // denormals, NaN are zero
963    unsigned ipa        : 4; // interpolation mode
964    unsigned lanes      : 4;
965    unsigned perPatch   : 1;
966    unsigned exit       : 1; // terminate program after insn
967    unsigned mask       : 4; // for vector ops
968    // prevent algebraic optimisations that aren't bit-for-bit identical
969    unsigned precise    : 1;
970 
971    int8_t postFactor; // MUL/DIV(if < 0) by 1 << postFactor
972 
973    int8_t predSrc;
974    int8_t flagsDef;
975    int8_t flagsSrc;
976 
977    uint32_t sched; // scheduling data (NOTE: maybe move to separate storage)
978 
979    BasicBlock *bb;
980 
981 protected:
982    std::deque<ValueDef> defs; // no gaps !
983    std::deque<ValueRef> srcs; // no gaps !
984 
985    // instruction specific methods:
986    // (don't want to subclass, would need more constructors and memory pools)
987 public:
setInterpolate(unsigned int mode)988    inline void setInterpolate(unsigned int mode) { ipa = mode; }
989 
getInterpMode()990    unsigned int getInterpMode() const { return ipa & 0x3; }
getSampleMode()991    unsigned int getSampleMode() const { return ipa & 0xc; }
992 
993 private:
994    void init();
995 };
996 
997 enum TexQuery
998 {
999    TXQ_DIMS, /* x, y, z, levels */
1000    TXQ_TYPE, /* ?, ?, samples, ? */
1001    TXQ_SAMPLE_POSITION,
1002    TXQ_FILTER,
1003    TXQ_LOD,
1004    TXQ_WRAP,
1005    TXQ_BORDER_COLOUR
1006 };
1007 
1008 class TexInstruction : public Instruction
1009 {
1010 public:
1011    class Target
1012    {
1013    public:
target(targ)1014       Target(TexTarget targ = TEX_TARGET_1D) : target(targ) { }
1015 
getName()1016       const char *getName() const { return descTable[target].name; }
getArgCount()1017       unsigned int getArgCount() const { return descTable[target].argc; }
getDim()1018       unsigned int getDim() const { return descTable[target].dim; }
isArray()1019       int isArray() const { return descTable[target].array ? 1 : 0; }
isCube()1020       int isCube() const { return descTable[target].cube ? 1 : 0; }
isShadow()1021       int isShadow() const { return descTable[target].shadow ? 1 : 0; }
isMS()1022       int isMS() const {
1023         return target == TEX_TARGET_2D_MS || target == TEX_TARGET_2D_MS_ARRAY; }
clearMS()1024       void clearMS() {
1025          if (isMS()) {
1026             if (isArray())
1027                target = TEX_TARGET_2D_ARRAY;
1028             else
1029                target = TEX_TARGET_2D;
1030          }
1031       }
1032 
1033       Target& operator=(TexTarget targ)
1034       {
1035          assert(targ < TEX_TARGET_COUNT);
1036          target = targ;
1037          return *this;
1038       }
1039 
1040       inline bool operator==(TexTarget targ) const { return target == targ; }
1041       inline bool operator!=(TexTarget targ) const { return target != targ; }
1042 
getEnum()1043       enum TexTarget getEnum() const { return target; }
1044 
1045    private:
1046       struct Desc
1047       {
1048          char name[19];
1049          uint8_t dim;
1050          uint8_t argc;
1051          bool array;
1052          bool cube;
1053          bool shadow;
1054       };
1055 
1056       static const struct Desc descTable[TEX_TARGET_COUNT];
1057 
1058    private:
1059       enum TexTarget target;
1060    };
1061 
1062 public:
1063    struct ImgFormatDesc
1064    {
1065       char name[19];
1066       uint8_t components;
1067       uint8_t bits[4];
1068       ImgType type;
1069       bool bgra;
1070    };
1071 
1072    static const struct ImgFormatDesc formatTable[IMG_FORMAT_COUNT];
1073    static const struct ImgFormatDesc *translateImgFormat(
1074          enum pipe_format format);
1075 
1076 public:
1077    TexInstruction(Function *, operation);
1078    virtual ~TexInstruction();
1079 
1080    virtual TexInstruction *clone(ClonePolicy<Function>&,
1081                                  Instruction * = NULL) const;
1082 
setTexture(Target targ,uint8_t r,uint8_t s)1083    inline void setTexture(Target targ, uint8_t r, uint8_t s)
1084    {
1085       tex.r = r;
1086       tex.s = s;
1087       tex.target = targ;
1088    }
1089 
1090    void setIndirectR(Value *);
1091    void setIndirectS(Value *);
1092    inline Value *getIndirectR() const;
1093    inline Value *getIndirectS() const;
1094 
1095 public:
1096    struct {
1097       Target target;
1098 
1099       uint16_t r;
1100       uint16_t s;
1101       int8_t rIndirectSrc;
1102       int8_t sIndirectSrc;
1103 
1104       uint8_t mask;
1105       uint8_t gatherComp;
1106 
1107       bool liveOnly; // only execute on live pixels of a quad (optimization)
1108       bool levelZero;
1109       bool derivAll;
1110       bool bindless;
1111 
1112       int8_t useOffsets; // 0, 1, or 4 for textureGatherOffsets
1113       int8_t offset[3]; // only used on nv50
1114 
1115       enum TexQuery query;
1116       const struct ImgFormatDesc *format;
1117 
1118       bool scalar; // for GM107s TEXS, TLDS, TLD4S
1119    } tex;
1120 
1121    ValueRef dPdx[3];
1122    ValueRef dPdy[3];
1123    ValueRef offset[4][3];
1124 };
1125 
1126 class CmpInstruction : public Instruction
1127 {
1128 public:
1129    CmpInstruction(Function *, operation);
1130 
1131    virtual CmpInstruction *clone(ClonePolicy<Function>&,
1132                                  Instruction * = NULL) const;
1133 
setCondition(CondCode cond)1134    void setCondition(CondCode cond) { setCond = cond; }
getCondition()1135    CondCode getCondition() const { return setCond; }
1136 
1137 public:
1138    CondCode setCond;
1139 };
1140 
1141 class FlowInstruction : public Instruction
1142 {
1143 public:
1144    FlowInstruction(Function *, operation, void *target);
1145 
1146    virtual FlowInstruction *clone(ClonePolicy<Function>&,
1147                                   Instruction * = NULL) const;
1148 
1149 public:
1150    unsigned allWarp  : 1;
1151    unsigned absolute : 1;
1152    unsigned limit    : 1;
1153    unsigned builtin  : 1; // true for calls to emulation code
1154    unsigned indirect : 1; // target in src(0)
1155 
1156    union {
1157       BasicBlock *bb;
1158       int builtin;
1159       Function *fn;
1160    } target;
1161 };
1162 
1163 class BasicBlock
1164 {
1165 public:
1166    BasicBlock(Function *);
1167    ~BasicBlock();
1168 
1169    BasicBlock *clone(ClonePolicy<Function>&) const;
1170 
getId()1171    inline int getId() const { return id; }
getInsnCount()1172    inline unsigned int getInsnCount() const { return numInsns; }
isTerminated()1173    inline bool isTerminated() const { return exit && exit->terminator; }
1174 
1175    bool dominatedBy(BasicBlock *bb);
1176    inline bool reachableBy(const BasicBlock *by, const BasicBlock *term);
1177 
1178    // returns mask of conditional out blocks
1179    // e.g. 3 for IF { .. } ELSE { .. } ENDIF, 1 for IF { .. } ENDIF
1180    unsigned int initiatesSimpleConditional() const;
1181 
1182 public:
getFunction()1183    Function *getFunction() const { return func; }
getProgram()1184    Program *getProgram() const { return program; }
1185 
getEntry()1186    Instruction *getEntry() const { return entry; } // first non-phi instruction
getPhi()1187    Instruction *getPhi() const { return phi; }
getFirst()1188    Instruction *getFirst() const { return phi ? phi : entry; }
getExit()1189    Instruction *getExit() const { return exit; }
1190 
1191    void insertHead(Instruction *);
1192    void insertTail(Instruction *);
1193    void insertBefore(Instruction *, Instruction *);
1194    void insertAfter(Instruction *, Instruction *);
1195    void remove(Instruction *);
1196    void permuteAdjacent(Instruction *, Instruction *);
1197 
1198    BasicBlock *idom() const;
1199 
1200    // NOTE: currently does not rebuild the dominator tree
1201    BasicBlock *splitBefore(Instruction *, bool attach = true);
1202    BasicBlock *splitAfter(Instruction *, bool attach = true);
1203 
getDF()1204    DLList& getDF() { return df; }
iterDF()1205    DLList::Iterator iterDF() { return df.iterator(); }
1206 
1207    static inline BasicBlock *get(Iterator&);
1208    static inline BasicBlock *get(Graph::Node *);
1209 
1210 public:
1211    Graph::Node cfg; // first edge is branch *taken* (the ELSE branch)
1212    Graph::Node dom;
1213 
1214    BitSet liveSet;
1215    BitSet defSet;
1216 
1217    uint32_t binPos;
1218    uint32_t binSize;
1219 
1220    Instruction *joinAt; // for quick reference
1221 
1222    bool explicitCont; // loop headers: true if loop contains continue stmts
1223 
1224 private:
1225    int id;
1226    DLList df;
1227 
1228    Instruction *phi;
1229    Instruction *entry;
1230    Instruction *exit;
1231 
1232    unsigned int numInsns;
1233 
1234 private:
1235    Function *func;
1236    Program *program;
1237 
1238    void splitCommon(Instruction *, BasicBlock *, bool attach);
1239 };
1240 
1241 class Function
1242 {
1243 public:
1244    Function(Program *, const char *name, uint32_t label);
1245    ~Function();
1246 
1247    static inline Function *get(Graph::Node *node);
1248 
getProgram()1249    inline Program *getProgram() const { return prog; }
getName()1250    inline const char *getName() const { return name; }
getId()1251    inline int getId() const { return id; }
getLabel()1252    inline uint32_t getLabel() const { return label; }
1253 
1254    void print();
1255    void printLiveIntervals() const;
1256    void printCFGraph(const char *filePath);
1257 
1258    bool setEntry(BasicBlock *);
1259    bool setExit(BasicBlock *);
1260 
1261    unsigned int orderInstructions(ArrayList&);
1262 
add(BasicBlock * bb,int & id)1263    inline void add(BasicBlock *bb, int& id) { allBBlocks.insert(bb, id); }
add(Instruction * insn,int & id)1264    inline void add(Instruction *insn, int& id) { allInsns.insert(insn, id); }
add(LValue * lval,int & id)1265    inline void add(LValue *lval, int& id) { allLValues.insert(lval, id); }
1266 
1267    inline LValue *getLValue(int id);
1268 
1269    void buildLiveSets();
1270    void buildDefSets();
1271    bool convertToSSA();
1272 
1273 public:
1274    std::deque<ValueDef> ins;
1275    std::deque<ValueRef> outs;
1276    std::deque<Value *> clobbers;
1277 
1278    Graph cfg;
1279    Graph::Node *cfgExit;
1280    Graph *domTree;
1281    Graph::Node call; // node in the call graph
1282 
1283    BasicBlock **bbArray; // BBs in emission order
1284    int bbCount;
1285 
1286    unsigned int loopNestingBound;
1287    int regClobberMax;
1288 
1289    uint32_t binPos;
1290    uint32_t binSize;
1291 
1292    Value *stackPtr;
1293 
1294    uint32_t tlsBase; // base address for l[] space (if no stack pointer is used)
1295    uint32_t tlsSize;
1296 
1297    ArrayList allBBlocks;
1298    ArrayList allInsns;
1299    ArrayList allLValues;
1300 
1301 private:
1302    void buildLiveSetsPreSSA(BasicBlock *, const int sequence);
1303    void buildDefSetsPreSSA(BasicBlock *bb, const int seq);
1304 
1305 private:
1306    uint32_t label;
1307    int id;
1308    const char *const name;
1309    Program *prog;
1310 };
1311 
1312 enum CGStage
1313 {
1314    CG_STAGE_PRE_SSA,
1315    CG_STAGE_SSA, // expected directly before register allocation
1316    CG_STAGE_POST_RA
1317 };
1318 
1319 class Program
1320 {
1321 public:
1322    enum Type
1323    {
1324       TYPE_VERTEX,
1325       TYPE_TESSELLATION_CONTROL,
1326       TYPE_TESSELLATION_EVAL,
1327       TYPE_GEOMETRY,
1328       TYPE_FRAGMENT,
1329       TYPE_COMPUTE
1330    };
1331 
1332    Program(Type type, Target *targ);
1333    ~Program();
1334 
1335    void print();
1336 
getType()1337    Type getType() const { return progType; }
1338 
add(Function * fn,int & id)1339    inline void add(Function *fn, int& id) { allFuncs.insert(fn, id); }
del(Function * fn,int & id)1340    inline void del(Function *fn, int& id) { allFuncs.remove(id); }
add(Value * rval,int & id)1341    inline void add(Value *rval, int& id) { allRValues.insert(rval, id); }
1342 
1343    bool makeFromNIR(struct nv50_ir_prog_info *,
1344                     struct nv50_ir_prog_info_out *);
1345    bool makeFromTGSI(struct nv50_ir_prog_info *,
1346                      struct nv50_ir_prog_info_out *);
1347    bool convertToSSA();
1348    bool optimizeSSA(int level);
1349    bool optimizePostRA(int level);
1350    bool registerAllocation();
1351    bool emitBinary(struct nv50_ir_prog_info_out *);
1352 
getTarget()1353    const Target *getTarget() const { return target; }
1354 
1355 private:
1356    Type progType;
1357    Target *target;
1358 
1359 public:
1360    Function *main;
1361    Graph calls;
1362 
1363    ArrayList allFuncs;
1364    ArrayList allRValues;
1365 
1366    uint32_t *code;
1367    uint32_t binSize;
1368    uint32_t tlsSize; // size required for FILE_MEMORY_LOCAL
1369 
1370    int maxGPR;
1371    bool fp64;
1372    bool persampleInvocation;
1373 
1374    MemoryPool mem_Instruction;
1375    MemoryPool mem_CmpInstruction;
1376    MemoryPool mem_TexInstruction;
1377    MemoryPool mem_FlowInstruction;
1378    MemoryPool mem_LValue;
1379    MemoryPool mem_Symbol;
1380    MemoryPool mem_ImmediateValue;
1381 
1382    uint32_t dbgFlags;
1383    uint8_t  optLevel;
1384 
1385    void *targetPriv; // e.g. to carry information between passes
1386 
1387    const struct nv50_ir_prog_info *driver; // for driver configuration
1388    const struct nv50_ir_prog_info_out *driver_out; // for driver configuration
1389 
1390    void releaseInstruction(Instruction *);
1391    void releaseValue(Value *);
1392 };
1393 
1394 // TODO: add const version
1395 class Pass
1396 {
1397 public:
1398    bool run(Program *, bool ordered = false, bool skipPhi = false);
1399    bool run(Function *, bool ordered = false, bool skipPhi = false);
1400 
1401 private:
1402    // return false to continue with next entity on next higher level
visit(Function *)1403    virtual bool visit(Function *) { return true; }
visit(BasicBlock *)1404    virtual bool visit(BasicBlock *) { return true; }
visit(Instruction *)1405    virtual bool visit(Instruction *) { return false; }
1406 
1407    bool doRun(Program *, bool ordered, bool skipPhi);
1408    bool doRun(Function *, bool ordered, bool skipPhi);
1409 
1410 protected:
1411    bool err;
1412    Function *func;
1413    Program *prog;
1414 };
1415 
1416 // =============================================================================
1417 
1418 #include "nv50_ir_inlines.h"
1419 
1420 } // namespace nv50_ir
1421 
1422 #endif // __NV50_IR_H__
1423