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