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 <vector>
32 
33 #include "codegen/unordered_set.h"
34 #include "codegen/nv50_ir_util.h"
35 #include "codegen/nv50_ir_graph.h"
36 
37 #include "codegen/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, // texure 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 // xmad(src0, src1, 0) << 16 + src2
288 #define NV50_IR_SUBOP_XMAD_PSL (1 << 0)
289 // (xmad(src0, src1, src2) & 0xffff) | (src1 << 16)
290 #define NV50_IR_SUBOP_XMAD_MRG (1 << 1)
291 // xmad(src0, src1, src2.lo)
292 #define NV50_IR_SUBOP_XMAD_CLO (1 << 2)
293 // xmad(src0, src1, src2.hi)
294 #define NV50_IR_SUBOP_XMAD_CHI (2 << 2)
295 // if both operands to the multiplication are non-zero, subtract 65536 for each
296 // negative operand
297 #define NV50_IR_SUBOP_XMAD_CSFU (3 << 2)
298 // xmad(src0, src1, src2) + src1 << 16
299 #define NV50_IR_SUBOP_XMAD_CBCC (4 << 2)
300 #define NV50_IR_SUBOP_XMAD_CMODE_SHIFT 2
301 #define NV50_IR_SUBOP_XMAD_CMODE_MASK (0x7 << NV50_IR_SUBOP_XMAD_CMODE_SHIFT)
302 
303 // use the high 16 bits instead of the low 16 bits for the multiplication.
304 // if the instruction's sType is signed, sign extend the operand from 16 bits
305 // to 32 before multiplication.
306 #define NV50_IR_SUBOP_XMAD_H1_SHIFT 5
307 #define NV50_IR_SUBOP_XMAD_H1(i) (1 << (NV50_IR_SUBOP_XMAD_H1_SHIFT + (i)))
308 #define NV50_IR_SUBOP_XMAD_H1_MASK (0x3 << NV50_IR_SUBOP_XMAD_H1_SHIFT)
309 
310 enum DataType
311 {
312    TYPE_NONE,
313    TYPE_U8,
314    TYPE_S8,
315    TYPE_U16,
316    TYPE_S16,
317    TYPE_U32,
318    TYPE_S32,
319    TYPE_U64, // 64 bit operations are only lowered after register allocation
320    TYPE_S64,
321    TYPE_F16,
322    TYPE_F32,
323    TYPE_F64,
324    TYPE_B96,
325    TYPE_B128
326 };
327 
328 enum CondCode
329 {
330    CC_FL = 0,
331    CC_NEVER = CC_FL, // when used with FILE_FLAGS
332    CC_LT = 1,
333    CC_EQ = 2,
334    CC_NOT_P = CC_EQ, // when used with FILE_PREDICATE
335    CC_LE = 3,
336    CC_GT = 4,
337    CC_NE = 5,
338    CC_P  = CC_NE,
339    CC_GE = 6,
340    CC_TR = 7,
341    CC_ALWAYS = CC_TR,
342    CC_U  = 8,
343    CC_LTU = 9,
344    CC_EQU = 10,
345    CC_LEU = 11,
346    CC_GTU = 12,
347    CC_NEU = 13,
348    CC_GEU = 14,
349    CC_NO = 0x10,
350    CC_NC = 0x11,
351    CC_NS = 0x12,
352    CC_NA = 0x13,
353    CC_A  = 0x14,
354    CC_S  = 0x15,
355    CC_C  = 0x16,
356    CC_O  = 0x17
357 };
358 
359 enum RoundMode
360 {
361    ROUND_N, // nearest
362    ROUND_M, // towards -inf
363    ROUND_Z, // towards 0
364    ROUND_P, // towards +inf
365    ROUND_NI, // nearest integer
366    ROUND_MI, // to integer towards -inf
367    ROUND_ZI, // to integer towards 0
368    ROUND_PI, // to integer towards +inf
369 };
370 
371 enum CacheMode
372 {
373    CACHE_CA,            // cache at all levels
374    CACHE_WB = CACHE_CA, // cache write back
375    CACHE_CG,            // cache at global level
376    CACHE_CS,            // cache streaming
377    CACHE_CV,            // cache as volatile
378    CACHE_WT = CACHE_CV  // cache write-through
379 };
380 
381 enum DataFile
382 {
383    FILE_NULL = 0,
384    FILE_GPR,
385    FILE_PREDICATE,       // boolean predicate
386    FILE_FLAGS,           // zero/sign/carry/overflow bits
387    FILE_ADDRESS,
388    LAST_REGISTER_FILE = FILE_ADDRESS,
389    FILE_IMMEDIATE,
390    FILE_MEMORY_CONST,
391    FILE_SHADER_INPUT,
392    FILE_SHADER_OUTPUT,
393    FILE_MEMORY_BUFFER,
394    FILE_MEMORY_GLOBAL,
395    FILE_MEMORY_SHARED,
396    FILE_MEMORY_LOCAL,
397    FILE_SYSTEM_VALUE,
398    DATA_FILE_COUNT
399 };
400 
401 enum TexTarget
402 {
403    TEX_TARGET_1D,
404    TEX_TARGET_2D,
405    TEX_TARGET_2D_MS,
406    TEX_TARGET_3D,
407    TEX_TARGET_CUBE,
408    TEX_TARGET_1D_SHADOW,
409    TEX_TARGET_2D_SHADOW,
410    TEX_TARGET_CUBE_SHADOW,
411    TEX_TARGET_1D_ARRAY,
412    TEX_TARGET_2D_ARRAY,
413    TEX_TARGET_2D_MS_ARRAY,
414    TEX_TARGET_CUBE_ARRAY,
415    TEX_TARGET_1D_ARRAY_SHADOW,
416    TEX_TARGET_2D_ARRAY_SHADOW,
417    TEX_TARGET_RECT,
418    TEX_TARGET_RECT_SHADOW,
419    TEX_TARGET_CUBE_ARRAY_SHADOW,
420    TEX_TARGET_BUFFER,
421    TEX_TARGET_COUNT
422 };
423 
424 enum ImgFormat
425 {
426    FMT_NONE,
427 
428    FMT_RGBA32F,
429    FMT_RGBA16F,
430    FMT_RG32F,
431    FMT_RG16F,
432    FMT_R11G11B10F,
433    FMT_R32F,
434    FMT_R16F,
435 
436    FMT_RGBA32UI,
437    FMT_RGBA16UI,
438    FMT_RGB10A2UI,
439    FMT_RGBA8UI,
440    FMT_RG32UI,
441    FMT_RG16UI,
442    FMT_RG8UI,
443    FMT_R32UI,
444    FMT_R16UI,
445    FMT_R8UI,
446 
447    FMT_RGBA32I,
448    FMT_RGBA16I,
449    FMT_RGBA8I,
450    FMT_RG32I,
451    FMT_RG16I,
452    FMT_RG8I,
453    FMT_R32I,
454    FMT_R16I,
455    FMT_R8I,
456 
457    FMT_RGBA16,
458    FMT_RGB10A2,
459    FMT_RGBA8,
460    FMT_RG16,
461    FMT_RG8,
462    FMT_R16,
463    FMT_R8,
464 
465    FMT_RGBA16_SNORM,
466    FMT_RGBA8_SNORM,
467    FMT_RG16_SNORM,
468    FMT_RG8_SNORM,
469    FMT_R16_SNORM,
470    FMT_R8_SNORM,
471 
472    FMT_BGRA8,
473 
474    IMG_FORMAT_COUNT,
475 };
476 
477 enum ImgType {
478    UINT,
479    SINT,
480    UNORM,
481    SNORM,
482    FLOAT,
483 };
484 
485 enum SVSemantic
486 {
487    SV_POSITION, // WPOS
488    SV_VERTEX_ID,
489    SV_INSTANCE_ID,
490    SV_INVOCATION_ID,
491    SV_PRIMITIVE_ID,
492    SV_VERTEX_COUNT, // gl_PatchVerticesIn
493    SV_LAYER,
494    SV_VIEWPORT_INDEX,
495    SV_VIEWPORT_MASK,
496    SV_YDIR,
497    SV_FACE,
498    SV_POINT_SIZE,
499    SV_POINT_COORD,
500    SV_CLIP_DISTANCE,
501    SV_SAMPLE_INDEX,
502    SV_SAMPLE_POS,
503    SV_SAMPLE_MASK,
504    SV_TESS_OUTER,
505    SV_TESS_INNER,
506    SV_TESS_COORD,
507    SV_TID,
508    SV_COMBINED_TID,
509    SV_CTAID,
510    SV_NTID,
511    SV_GRIDID,
512    SV_NCTAID,
513    SV_LANEID,
514    SV_PHYSID,
515    SV_NPHYSID,
516    SV_CLOCK,
517    SV_LBASE,
518    SV_SBASE,
519    SV_VERTEX_STRIDE,
520    SV_INVOCATION_INFO,
521    SV_THREAD_KILL,
522    SV_BASEVERTEX,
523    SV_BASEINSTANCE,
524    SV_DRAWID,
525    SV_WORK_DIM,
526    SV_LANEMASK_EQ,
527    SV_LANEMASK_LT,
528    SV_LANEMASK_LE,
529    SV_LANEMASK_GT,
530    SV_LANEMASK_GE,
531    SV_UNDEFINED,
532    SV_LAST
533 };
534 
535 class Program;
536 class Function;
537 class BasicBlock;
538 
539 class Target;
540 
541 class Instruction;
542 class CmpInstruction;
543 class TexInstruction;
544 class FlowInstruction;
545 
546 class Value;
547 class LValue;
548 class Symbol;
549 class ImmediateValue;
550 
551 struct Storage
552 {
553    DataFile file;
554    int8_t fileIndex; // signed, may be indirect for CONST[]
555    uint8_t size; // this should match the Instruction type's size
556    DataType type; // mainly for pretty printing
557    union {
558       uint64_t u64;    // immediate values
559       uint32_t u32;
560       uint16_t u16;
561       uint8_t u8;
562       int64_t s64;
563       int32_t s32;
564       int16_t s16;
565       int8_t s8;
566       float f32;
567       double f64;
568       int32_t offset; // offset from 0 (base of address space)
569       int32_t id;     // register id (< 0 if virtual/unassigned, in units <= 4)
570       struct {
571          SVSemantic sv;
572          int index;
573       } sv;
574    } data;
575 };
576 
577 // precedence: NOT after SAT after NEG after ABS
578 #define NV50_IR_MOD_ABS (1 << 0)
579 #define NV50_IR_MOD_NEG (1 << 1)
580 #define NV50_IR_MOD_SAT (1 << 2)
581 #define NV50_IR_MOD_NOT (1 << 3)
582 #define NV50_IR_MOD_NEG_ABS (NV50_IR_MOD_NEG | NV50_IR_MOD_ABS)
583 
584 #define NV50_IR_INTERP_MODE_MASK   0x3
585 #define NV50_IR_INTERP_LINEAR      (0 << 0)
586 #define NV50_IR_INTERP_PERSPECTIVE (1 << 0)
587 #define NV50_IR_INTERP_FLAT        (2 << 0)
588 #define NV50_IR_INTERP_SC          (3 << 0) // what exactly is that ?
589 #define NV50_IR_INTERP_SAMPLE_MASK 0xc
590 #define NV50_IR_INTERP_DEFAULT     (0 << 2)
591 #define NV50_IR_INTERP_CENTROID    (1 << 2)
592 #define NV50_IR_INTERP_OFFSET      (2 << 2)
593 #define NV50_IR_INTERP_SAMPLEID    (3 << 2)
594 
595 // do we really want this to be a class ?
596 class Modifier
597 {
598 public:
Modifier()599    Modifier() : bits(0) { }
Modifier(unsigned int m)600    Modifier(unsigned int m) : bits(m) { }
601    Modifier(operation op);
602 
603    // @return new Modifier applying a after b (asserts if unrepresentable)
604    Modifier operator*(const Modifier) const;
605    Modifier operator*=(const Modifier m) { *this = *this * m; return *this; }
606    Modifier operator==(const Modifier m) const { return m.bits == bits; }
607    Modifier operator!=(const Modifier m) const { return m.bits != bits; }
608 
609    inline Modifier operator&(const Modifier m) const { return bits & m.bits; }
610    inline Modifier operator|(const Modifier m) const { return bits | m.bits; }
611    inline Modifier operator^(const Modifier m) const { return bits ^ m.bits; }
612 
613    operation getOp() const;
614 
neg()615    inline int neg() const { return (bits & NV50_IR_MOD_NEG) ? 1 : 0; }
abs()616    inline int abs() const { return (bits & NV50_IR_MOD_ABS) ? 1 : 0; }
617 
618    inline operator bool() const { return bits ? true : false; }
619 
620    void applyTo(ImmediateValue &imm) const;
621 
622    int print(char *buf, size_t size) const;
623 
624 private:
625    uint8_t bits;
626 };
627 
628 class ValueRef
629 {
630 public:
631    ValueRef(Value * = NULL);
632    ValueRef(const ValueRef&);
633    ~ValueRef();
634 
exists()635    inline bool exists() const { return value != NULL; }
636 
637    void set(Value *);
638    void set(const ValueRef&);
get()639    inline Value *get() const { return value; }
640    inline Value *rep() const;
641 
getInsn()642    inline Instruction *getInsn() const { return insn; }
setInsn(Instruction * inst)643    inline void setInsn(Instruction *inst) { insn = inst; }
644 
isIndirect(int dim)645    inline bool isIndirect(int dim) const { return indirect[dim] >= 0; }
646    inline const ValueRef *getIndirect(int dim) const;
647 
648    inline DataFile getFile() const;
649    inline unsigned getSize() const;
650 
651    // SSA: return eventual (traverse MOVs) literal value, if it exists
652    bool getImmediate(ImmediateValue&) const;
653 
654 public:
655    Modifier mod;
656    int8_t indirect[2]; // >= 0 if relative to lvalue in insn->src(indirect[i])
657 
658    bool usedAsPtr; // for printing
659 
660 private:
661    Value *value;
662    Instruction *insn;
663 };
664 
665 class ValueDef
666 {
667 public:
668    ValueDef(Value * = NULL);
669    ValueDef(const ValueDef&);
670    ~ValueDef();
671 
exists()672    inline bool exists() const { return value != NULL; }
673 
get()674    inline Value *get() const { return value; }
675    inline Value *rep() const;
676    void set(Value *);
677    bool mayReplace(const ValueRef &);
678    void replace(const ValueRef &, bool doSet); // replace all uses of the old value
679 
getInsn()680    inline Instruction *getInsn() const { return insn; }
setInsn(Instruction * inst)681    inline void setInsn(Instruction *inst) { insn = inst; }
682 
683    inline DataFile getFile() const;
684    inline unsigned getSize() const;
685 
686    inline void setSSA(LValue *);
687    inline const LValue *preSSA() const;
688 
689 private:
690    Value *value;   // should make this LValue * ...
691    LValue *origin; // pre SSA value
692    Instruction *insn;
693 };
694 
695 class Value
696 {
697 public:
698    Value();
~Value()699    virtual ~Value() { }
700 
701    virtual Value *clone(ClonePolicy<Function>&) const = 0;
702 
703    virtual int print(char *, size_t, DataType ty = TYPE_NONE) const = 0;
704 
705    virtual bool equals(const Value *, bool strict = false) const;
706    virtual bool interfers(const Value *) const;
isUniform()707    virtual bool isUniform() const { return true; }
708 
rep()709    inline Value *rep() const { return join; }
710 
711    inline Instruction *getUniqueInsn() const;
712    inline Instruction *getInsn() const; // use when uniqueness is certain
713 
refCount()714    inline int refCount() { return uses.size(); }
715 
716    inline LValue *asLValue();
717    inline Symbol *asSym();
718    inline ImmediateValue *asImm();
719    inline const Symbol *asSym() const;
720    inline const ImmediateValue *asImm() const;
721 
inFile(DataFile f)722    inline bool inFile(DataFile f) const { return reg.file == f; }
723 
724    static inline Value *get(Iterator&);
725 
726    unordered_set<ValueRef *> uses;
727    std::list<ValueDef *> defs;
728    typedef unordered_set<ValueRef *>::iterator UseIterator;
729    typedef unordered_set<ValueRef *>::const_iterator UseCIterator;
730    typedef std::list<ValueDef *>::iterator DefIterator;
731    typedef std::list<ValueDef *>::const_iterator DefCIterator;
732 
733    int id;
734    Storage reg;
735 
736    // TODO: these should be in LValue:
737    Interval livei;
738    Value *join;
739 };
740 
741 class LValue : public Value
742 {
743 public:
744    LValue(Function *, DataFile file);
745    LValue(Function *, LValue *);
~LValue()746    ~LValue() { }
747 
748    virtual bool isUniform() const;
749 
750    virtual LValue *clone(ClonePolicy<Function>&) const;
751 
752    virtual int print(char *, size_t, DataType ty = TYPE_NONE) const;
753 
754 public:
755    unsigned compMask : 8; // compound/component mask
756    unsigned compound : 1; // used by RA, value involved in split/merge
757    unsigned ssa      : 1;
758    unsigned fixedReg : 1; // set & used by RA, earlier just use (id < 0)
759    unsigned noSpill  : 1; // do not spill (e.g. if spill temporary already)
760 };
761 
762 class Symbol : public Value
763 {
764 public:
765    Symbol(Program *, DataFile file = FILE_MEMORY_CONST, ubyte fileIdx = 0);
~Symbol()766    ~Symbol() { }
767 
768    virtual Symbol *clone(ClonePolicy<Function>&) const;
769 
770    virtual bool equals(const Value *that, bool strict) const;
771 
772    virtual bool isUniform() const;
773 
774    virtual int print(char *, size_t, DataType ty = TYPE_NONE) const;
775 
776    // print with indirect values
777    int print(char *, size_t, Value *, Value *, DataType ty = TYPE_NONE) const;
778 
779    inline void setFile(DataFile file, ubyte fileIndex = 0)
780    {
781       reg.file = file;
782       reg.fileIndex = fileIndex;
783    }
784 
785    inline void setOffset(int32_t offset);
786    inline void setAddress(Symbol *base, int32_t offset);
787    inline void setSV(SVSemantic sv, uint32_t idx = 0);
788 
getBase()789    inline const Symbol *getBase() const { return baseSym; }
790 
791 private:
792    Symbol *baseSym; // array base for Symbols representing array elements
793 };
794 
795 class ImmediateValue : public Value
796 {
797 public:
ImmediateValue()798    ImmediateValue() { }
799    ImmediateValue(Program *, uint32_t);
800    ImmediateValue(Program *, float);
801    ImmediateValue(Program *, double);
802    // NOTE: not added to program with
803    ImmediateValue(const ImmediateValue *, DataType ty);
~ImmediateValue()804    ~ImmediateValue() { };
805 
806    virtual ImmediateValue *clone(ClonePolicy<Function>&) const;
807 
808    virtual bool equals(const Value *that, bool strict) const;
809 
810    // these only work if 'type' is valid (we mostly use untyped literals):
811    bool isInteger(const int ival) const; // ival is cast to this' type
812    bool isNegative() const;
813    bool isPow2() const;
814 
815    void applyLog2();
816 
817    // for constant folding:
818    ImmediateValue operator+(const ImmediateValue&) const;
819    ImmediateValue operator-(const ImmediateValue&) const;
820    ImmediateValue operator*(const ImmediateValue&) const;
821    ImmediateValue operator/(const ImmediateValue&) const;
822 
823    ImmediateValue& operator=(const ImmediateValue&); // only sets value !
824 
825    bool compare(CondCode cc, float fval) const;
826 
827    virtual int print(char *, size_t, DataType ty = TYPE_NONE) const;
828 };
829 
830 class Instruction
831 {
832 public:
833    Instruction();
834    Instruction(Function *, operation, DataType);
835    virtual ~Instruction();
836 
837    virtual Instruction *clone(ClonePolicy<Function>&,
838                               Instruction * = NULL) const;
839 
840    void setDef(int i, Value *);
841    void setSrc(int s, Value *);
842    void setSrc(int s, const ValueRef&);
843    void swapSources(int a, int b);
844    void moveSources(int s, int delta);
845    bool setIndirect(int s, int dim, Value *);
846 
src(int s)847    inline ValueRef& src(int s) { return srcs[s]; }
def(int s)848    inline ValueDef& def(int s) { return defs[s]; }
src(int s)849    inline const ValueRef& src(int s) const { return srcs[s]; }
def(int s)850    inline const ValueDef& def(int s) const { return defs[s]; }
851 
getDef(int d)852    inline Value *getDef(int d) const { return defs[d].get(); }
getSrc(int s)853    inline Value *getSrc(int s) const { return srcs[s].get(); }
854    inline Value *getIndirect(int s, int dim) const;
855 
defExists(unsigned d)856    inline bool defExists(unsigned d) const
857    {
858       return d < defs.size() && defs[d].exists();
859    }
srcExists(unsigned s)860    inline bool srcExists(unsigned s) const
861    {
862       return s < srcs.size() && srcs[s].exists();
863    }
864 
865    inline bool constrainedDefs() const;
866 
867    bool setPredicate(CondCode ccode, Value *);
868    inline Value *getPredicate() const;
869    bool writesPredicate() const;
isPredicated()870    inline bool isPredicated() const { return predSrc >= 0; }
871 
872    inline void setFlagsSrc(int s, Value *);
873    inline void setFlagsDef(int d, Value *);
usesFlags()874    inline bool usesFlags() const { return flagsSrc >= 0; }
875 
defCount()876    unsigned int defCount() const { return defs.size(); };
877    unsigned int defCount(unsigned int mask, bool singleFile = false) const;
srcCount()878    unsigned int srcCount() const { return srcs.size(); };
879    unsigned int srcCount(unsigned int mask, bool singleFile = false) const;
880 
881    // save & remove / set indirect[0,1] and predicate source
882    void takeExtraSources(int s, Value *[3]);
883    void putExtraSources(int s, Value *[3]);
884 
setType(DataType type)885    inline void setType(DataType type) { dType = sType = type; }
886 
setType(DataType dtype,DataType stype)887    inline void setType(DataType dtype, DataType stype)
888    {
889       dType = dtype;
890       sType = stype;
891    }
892 
isPseudo()893    inline bool isPseudo() const { return op < OP_MOV; }
894    bool isDead() const;
895    bool isNop() const;
896    bool isCommutationLegal(const Instruction *) const; // must be adjacent !
897    bool isActionEqual(const Instruction *) const;
898    bool isResultEqual(const Instruction *) const;
899 
900    // check whether the defs interfere with srcs and defs of another instruction
901    bool canCommuteDefDef(const Instruction *) const;
902    bool canCommuteDefSrc(const Instruction *) const;
903 
904    void print() const;
905 
906    inline CmpInstruction *asCmp();
907    inline TexInstruction *asTex();
908    inline FlowInstruction *asFlow();
909    inline const TexInstruction *asTex() const;
910    inline const CmpInstruction *asCmp() const;
911    inline const FlowInstruction *asFlow() const;
912 
913 public:
914    Instruction *next;
915    Instruction *prev;
916    int id;
917    int serial; // CFG order
918 
919    operation op;
920    DataType dType; // destination or defining type
921    DataType sType; // source or secondary type
922    CondCode cc;
923    RoundMode rnd;
924    CacheMode cache;
925 
926    uint16_t subOp; // quadop, 1 for mul-high, etc.
927 
928    unsigned encSize    : 5; // encoding size in bytes
929    unsigned saturate   : 1; // to [0.0f, 1.0f]
930    unsigned join       : 1; // converge control flow (use OP_JOIN until end)
931    unsigned fixed      : 1; // prevent dead code elimination
932    unsigned terminator : 1; // end of basic block
933    unsigned ftz        : 1; // flush denormal to zero
934    unsigned dnz        : 1; // denormals, NaN are zero
935    unsigned ipa        : 4; // interpolation mode
936    unsigned lanes      : 4;
937    unsigned perPatch   : 1;
938    unsigned exit       : 1; // terminate program after insn
939    unsigned mask       : 4; // for vector ops
940    // prevent algebraic optimisations that aren't bit-for-bit identical
941    unsigned precise    : 1;
942 
943    int8_t postFactor; // MUL/DIV(if < 0) by 1 << postFactor
944 
945    int8_t predSrc;
946    int8_t flagsDef;
947    int8_t flagsSrc;
948 
949    uint32_t sched; // scheduling data (NOTE: maybe move to separate storage)
950 
951    BasicBlock *bb;
952 
953 protected:
954    std::deque<ValueDef> defs; // no gaps !
955    std::deque<ValueRef> srcs; // no gaps !
956 
957    // instruction specific methods:
958    // (don't want to subclass, would need more constructors and memory pools)
959 public:
setInterpolate(unsigned int mode)960    inline void setInterpolate(unsigned int mode) { ipa = mode; }
961 
getInterpMode()962    unsigned int getInterpMode() const { return ipa & 0x3; }
getSampleMode()963    unsigned int getSampleMode() const { return ipa & 0xc; }
964 
965 private:
966    void init();
967 };
968 
969 enum TexQuery
970 {
971    TXQ_DIMS, /* x, y, z, levels */
972    TXQ_TYPE, /* ?, ?, samples, ? */
973    TXQ_SAMPLE_POSITION,
974    TXQ_FILTER,
975    TXQ_LOD,
976    TXQ_WRAP,
977    TXQ_BORDER_COLOUR
978 };
979 
980 class TexInstruction : public Instruction
981 {
982 public:
983    class Target
984    {
985    public:
target(targ)986       Target(TexTarget targ = TEX_TARGET_1D) : target(targ) { }
987 
getName()988       const char *getName() const { return descTable[target].name; }
getArgCount()989       unsigned int getArgCount() const { return descTable[target].argc; }
getDim()990       unsigned int getDim() const { return descTable[target].dim; }
isArray()991       int isArray() const { return descTable[target].array ? 1 : 0; }
isCube()992       int isCube() const { return descTable[target].cube ? 1 : 0; }
isShadow()993       int isShadow() const { return descTable[target].shadow ? 1 : 0; }
isMS()994       int isMS() const {
995         return target == TEX_TARGET_2D_MS || target == TEX_TARGET_2D_MS_ARRAY; }
clearMS()996       void clearMS() {
997          if (isMS()) {
998             if (isArray())
999                target = TEX_TARGET_2D_ARRAY;
1000             else
1001                target = TEX_TARGET_2D;
1002          }
1003       }
1004 
1005       Target& operator=(TexTarget targ)
1006       {
1007          assert(targ < TEX_TARGET_COUNT);
1008          target = targ;
1009          return *this;
1010       }
1011 
1012       inline bool operator==(TexTarget targ) const { return target == targ; }
1013       inline bool operator!=(TexTarget targ) const { return target != targ; }
1014 
getEnum()1015       enum TexTarget getEnum() const { return target; }
1016 
1017    private:
1018       struct Desc
1019       {
1020          char name[19];
1021          uint8_t dim;
1022          uint8_t argc;
1023          bool array;
1024          bool cube;
1025          bool shadow;
1026       };
1027 
1028       static const struct Desc descTable[TEX_TARGET_COUNT];
1029 
1030    private:
1031       enum TexTarget target;
1032    };
1033 
1034 public:
1035    struct ImgFormatDesc
1036    {
1037       char name[19];
1038       uint8_t components;
1039       uint8_t bits[4];
1040       ImgType type;
1041       bool bgra;
1042    };
1043 
1044    static const struct ImgFormatDesc formatTable[IMG_FORMAT_COUNT];
1045    static const struct ImgFormatDesc *translateImgFormat(
1046          enum pipe_format format);
1047 
1048 public:
1049    TexInstruction(Function *, operation);
1050    virtual ~TexInstruction();
1051 
1052    virtual TexInstruction *clone(ClonePolicy<Function>&,
1053                                  Instruction * = NULL) const;
1054 
setTexture(Target targ,uint8_t r,uint8_t s)1055    inline void setTexture(Target targ, uint8_t r, uint8_t s)
1056    {
1057       tex.r = r;
1058       tex.s = s;
1059       tex.target = targ;
1060    }
1061 
1062    void setIndirectR(Value *);
1063    void setIndirectS(Value *);
1064    inline Value *getIndirectR() const;
1065    inline Value *getIndirectS() const;
1066 
1067 public:
1068    struct {
1069       Target target;
1070 
1071       uint16_t r;
1072       uint16_t s;
1073       int8_t rIndirectSrc;
1074       int8_t sIndirectSrc;
1075 
1076       uint8_t mask;
1077       uint8_t gatherComp;
1078 
1079       bool liveOnly; // only execute on live pixels of a quad (optimization)
1080       bool levelZero;
1081       bool derivAll;
1082       bool bindless;
1083 
1084       int8_t useOffsets; // 0, 1, or 4 for textureGatherOffsets
1085       int8_t offset[3]; // only used on nv50
1086 
1087       enum TexQuery query;
1088       const struct ImgFormatDesc *format;
1089 
1090       bool scalar; // for GM107s TEXS, TLDS, TLD4S
1091    } tex;
1092 
1093    ValueRef dPdx[3];
1094    ValueRef dPdy[3];
1095    ValueRef offset[4][3];
1096 };
1097 
1098 class CmpInstruction : public Instruction
1099 {
1100 public:
1101    CmpInstruction(Function *, operation);
1102 
1103    virtual CmpInstruction *clone(ClonePolicy<Function>&,
1104                                  Instruction * = NULL) const;
1105 
setCondition(CondCode cond)1106    void setCondition(CondCode cond) { setCond = cond; }
getCondition()1107    CondCode getCondition() const { return setCond; }
1108 
1109 public:
1110    CondCode setCond;
1111 };
1112 
1113 class FlowInstruction : public Instruction
1114 {
1115 public:
1116    FlowInstruction(Function *, operation, void *target);
1117 
1118    virtual FlowInstruction *clone(ClonePolicy<Function>&,
1119                                   Instruction * = NULL) const;
1120 
1121 public:
1122    unsigned allWarp  : 1;
1123    unsigned absolute : 1;
1124    unsigned limit    : 1;
1125    unsigned builtin  : 1; // true for calls to emulation code
1126    unsigned indirect : 1; // target in src(0)
1127 
1128    union {
1129       BasicBlock *bb;
1130       int builtin;
1131       Function *fn;
1132    } target;
1133 };
1134 
1135 class BasicBlock
1136 {
1137 public:
1138    BasicBlock(Function *);
1139    ~BasicBlock();
1140 
1141    BasicBlock *clone(ClonePolicy<Function>&) const;
1142 
getId()1143    inline int getId() const { return id; }
getInsnCount()1144    inline unsigned int getInsnCount() const { return numInsns; }
isTerminated()1145    inline bool isTerminated() const { return exit && exit->terminator; }
1146 
1147    bool dominatedBy(BasicBlock *bb);
1148    inline bool reachableBy(const BasicBlock *by, const BasicBlock *term);
1149 
1150    // returns mask of conditional out blocks
1151    // e.g. 3 for IF { .. } ELSE { .. } ENDIF, 1 for IF { .. } ENDIF
1152    unsigned int initiatesSimpleConditional() const;
1153 
1154 public:
getFunction()1155    Function *getFunction() const { return func; }
getProgram()1156    Program *getProgram() const { return program; }
1157 
getEntry()1158    Instruction *getEntry() const { return entry; } // first non-phi instruction
getPhi()1159    Instruction *getPhi() const { return phi; }
getFirst()1160    Instruction *getFirst() const { return phi ? phi : entry; }
getExit()1161    Instruction *getExit() const { return exit; }
1162 
1163    void insertHead(Instruction *);
1164    void insertTail(Instruction *);
1165    void insertBefore(Instruction *, Instruction *);
1166    void insertAfter(Instruction *, Instruction *);
1167    void remove(Instruction *);
1168    void permuteAdjacent(Instruction *, Instruction *);
1169 
1170    BasicBlock *idom() const;
1171 
1172    // NOTE: currently does not rebuild the dominator tree
1173    BasicBlock *splitBefore(Instruction *, bool attach = true);
1174    BasicBlock *splitAfter(Instruction *, bool attach = true);
1175 
getDF()1176    DLList& getDF() { return df; }
iterDF()1177    DLList::Iterator iterDF() { return df.iterator(); }
1178 
1179    static inline BasicBlock *get(Iterator&);
1180    static inline BasicBlock *get(Graph::Node *);
1181 
1182 public:
1183    Graph::Node cfg; // first edge is branch *taken* (the ELSE branch)
1184    Graph::Node dom;
1185 
1186    BitSet liveSet;
1187    BitSet defSet;
1188 
1189    uint32_t binPos;
1190    uint32_t binSize;
1191 
1192    Instruction *joinAt; // for quick reference
1193 
1194    bool explicitCont; // loop headers: true if loop contains continue stmts
1195 
1196 private:
1197    int id;
1198    DLList df;
1199 
1200    Instruction *phi;
1201    Instruction *entry;
1202    Instruction *exit;
1203 
1204    unsigned int numInsns;
1205 
1206 private:
1207    Function *func;
1208    Program *program;
1209 
1210    void splitCommon(Instruction *, BasicBlock *, bool attach);
1211 };
1212 
1213 class Function
1214 {
1215 public:
1216    Function(Program *, const char *name, uint32_t label);
1217    ~Function();
1218 
1219    static inline Function *get(Graph::Node *node);
1220 
getProgram()1221    inline Program *getProgram() const { return prog; }
getName()1222    inline const char *getName() const { return name; }
getId()1223    inline int getId() const { return id; }
getLabel()1224    inline uint32_t getLabel() const { return label; }
1225 
1226    void print();
1227    void printLiveIntervals() const;
1228    void printCFGraph(const char *filePath);
1229 
1230    bool setEntry(BasicBlock *);
1231    bool setExit(BasicBlock *);
1232 
1233    unsigned int orderInstructions(ArrayList&);
1234 
add(BasicBlock * bb,int & id)1235    inline void add(BasicBlock *bb, int& id) { allBBlocks.insert(bb, id); }
add(Instruction * insn,int & id)1236    inline void add(Instruction *insn, int& id) { allInsns.insert(insn, id); }
add(LValue * lval,int & id)1237    inline void add(LValue *lval, int& id) { allLValues.insert(lval, id); }
1238 
1239    inline LValue *getLValue(int id);
1240 
1241    void buildLiveSets();
1242    void buildDefSets();
1243    bool convertToSSA();
1244 
1245 public:
1246    std::deque<ValueDef> ins;
1247    std::deque<ValueRef> outs;
1248    std::deque<Value *> clobbers;
1249 
1250    Graph cfg;
1251    Graph::Node *cfgExit;
1252    Graph *domTree;
1253    Graph::Node call; // node in the call graph
1254 
1255    BasicBlock **bbArray; // BBs in emission order
1256    int bbCount;
1257 
1258    unsigned int loopNestingBound;
1259    int regClobberMax;
1260 
1261    uint32_t binPos;
1262    uint32_t binSize;
1263 
1264    Value *stackPtr;
1265 
1266    uint32_t tlsBase; // base address for l[] space (if no stack pointer is used)
1267    uint32_t tlsSize;
1268 
1269    ArrayList allBBlocks;
1270    ArrayList allInsns;
1271    ArrayList allLValues;
1272 
1273 private:
1274    void buildLiveSetsPreSSA(BasicBlock *, const int sequence);
1275    void buildDefSetsPreSSA(BasicBlock *bb, const int seq);
1276 
1277 private:
1278    uint32_t label;
1279    int id;
1280    const char *const name;
1281    Program *prog;
1282 };
1283 
1284 enum CGStage
1285 {
1286    CG_STAGE_PRE_SSA,
1287    CG_STAGE_SSA, // expected directly before register allocation
1288    CG_STAGE_POST_RA
1289 };
1290 
1291 class Program
1292 {
1293 public:
1294    enum Type
1295    {
1296       TYPE_VERTEX,
1297       TYPE_TESSELLATION_CONTROL,
1298       TYPE_TESSELLATION_EVAL,
1299       TYPE_GEOMETRY,
1300       TYPE_FRAGMENT,
1301       TYPE_COMPUTE
1302    };
1303 
1304    Program(Type type, Target *targ);
1305    ~Program();
1306 
1307    void print();
1308 
getType()1309    Type getType() const { return progType; }
1310 
add(Function * fn,int & id)1311    inline void add(Function *fn, int& id) { allFuncs.insert(fn, id); }
del(Function * fn,int & id)1312    inline void del(Function *fn, int& id) { allFuncs.remove(id); }
add(Value * rval,int & id)1313    inline void add(Value *rval, int& id) { allRValues.insert(rval, id); }
1314 
1315    bool makeFromNIR(struct nv50_ir_prog_info *,
1316                     struct nv50_ir_prog_info_out *);
1317    bool makeFromTGSI(struct nv50_ir_prog_info *,
1318                      struct nv50_ir_prog_info_out *);
1319    bool convertToSSA();
1320    bool optimizeSSA(int level);
1321    bool optimizePostRA(int level);
1322    bool registerAllocation();
1323    bool emitBinary(struct nv50_ir_prog_info_out *);
1324 
getTarget()1325    const Target *getTarget() const { return target; }
1326 
1327 private:
1328    Type progType;
1329    Target *target;
1330 
1331 public:
1332    Function *main;
1333    Graph calls;
1334 
1335    ArrayList allFuncs;
1336    ArrayList allRValues;
1337 
1338    uint32_t *code;
1339    uint32_t binSize;
1340    uint32_t tlsSize; // size required for FILE_MEMORY_LOCAL
1341 
1342    int maxGPR;
1343    bool fp64;
1344    bool persampleInvocation;
1345 
1346    MemoryPool mem_Instruction;
1347    MemoryPool mem_CmpInstruction;
1348    MemoryPool mem_TexInstruction;
1349    MemoryPool mem_FlowInstruction;
1350    MemoryPool mem_LValue;
1351    MemoryPool mem_Symbol;
1352    MemoryPool mem_ImmediateValue;
1353 
1354    uint32_t dbgFlags;
1355    uint8_t  optLevel;
1356 
1357    void *targetPriv; // e.g. to carry information between passes
1358 
1359    const struct nv50_ir_prog_info *driver; // for driver configuration
1360    const struct nv50_ir_prog_info_out *driver_out; // for driver configuration
1361 
1362    void releaseInstruction(Instruction *);
1363    void releaseValue(Value *);
1364 };
1365 
1366 // TODO: add const version
1367 class Pass
1368 {
1369 public:
1370    bool run(Program *, bool ordered = false, bool skipPhi = false);
1371    bool run(Function *, bool ordered = false, bool skipPhi = false);
1372 
1373 private:
1374    // return false to continue with next entity on next higher level
visit(Function *)1375    virtual bool visit(Function *) { return true; }
visit(BasicBlock *)1376    virtual bool visit(BasicBlock *) { return true; }
visit(Instruction *)1377    virtual bool visit(Instruction *) { return false; }
1378 
1379    bool doRun(Program *, bool ordered, bool skipPhi);
1380    bool doRun(Function *, bool ordered, bool skipPhi);
1381 
1382 protected:
1383    bool err;
1384    Function *func;
1385    Program *prog;
1386 };
1387 
1388 // =============================================================================
1389 
1390 #include "codegen/nv50_ir_inlines.h"
1391 
1392 } // namespace nv50_ir
1393 
1394 #endif // __NV50_IR_H__
1395