nvc0/ir: avoid generating illegal instructions for compute constbuf loads
[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 IMG_FORMAT_COUNT,
416 };
417
418 enum ImgType {
419 UINT,
420 SINT,
421 UNORM,
422 SNORM,
423 FLOAT,
424 };
425
426 enum SVSemantic
427 {
428 SV_POSITION, // WPOS
429 SV_VERTEX_ID,
430 SV_INSTANCE_ID,
431 SV_INVOCATION_ID,
432 SV_PRIMITIVE_ID,
433 SV_VERTEX_COUNT, // gl_PatchVerticesIn
434 SV_LAYER,
435 SV_VIEWPORT_INDEX,
436 SV_YDIR,
437 SV_FACE,
438 SV_POINT_SIZE,
439 SV_POINT_COORD,
440 SV_CLIP_DISTANCE,
441 SV_SAMPLE_INDEX,
442 SV_SAMPLE_POS,
443 SV_SAMPLE_MASK,
444 SV_TESS_OUTER,
445 SV_TESS_INNER,
446 SV_TESS_COORD,
447 SV_TID,
448 SV_CTAID,
449 SV_NTID,
450 SV_GRIDID,
451 SV_NCTAID,
452 SV_LANEID,
453 SV_PHYSID,
454 SV_NPHYSID,
455 SV_CLOCK,
456 SV_LBASE,
457 SV_SBASE,
458 SV_VERTEX_STRIDE,
459 SV_INVOCATION_INFO,
460 SV_THREAD_KILL,
461 SV_BASEVERTEX,
462 SV_BASEINSTANCE,
463 SV_DRAWID,
464 SV_UNDEFINED,
465 SV_LAST
466 };
467
468 class Program;
469 class Function;
470 class BasicBlock;
471
472 class Target;
473
474 class Instruction;
475 class CmpInstruction;
476 class TexInstruction;
477 class FlowInstruction;
478
479 class Value;
480 class LValue;
481 class Symbol;
482 class ImmediateValue;
483
484 struct Storage
485 {
486 DataFile file;
487 int8_t fileIndex; // signed, may be indirect for CONST[]
488 uint8_t size; // this should match the Instruction type's size
489 DataType type; // mainly for pretty printing
490 union {
491 uint64_t u64; // immediate values
492 uint32_t u32;
493 uint16_t u16;
494 uint8_t u8;
495 int64_t s64;
496 int32_t s32;
497 int16_t s16;
498 int8_t s8;
499 float f32;
500 double f64;
501 int32_t offset; // offset from 0 (base of address space)
502 int32_t id; // register id (< 0 if virtual/unassigned, in units <= 4)
503 struct {
504 SVSemantic sv;
505 int index;
506 } sv;
507 } data;
508 };
509
510 // precedence: NOT after SAT after NEG after ABS
511 #define NV50_IR_MOD_ABS (1 << 0)
512 #define NV50_IR_MOD_NEG (1 << 1)
513 #define NV50_IR_MOD_SAT (1 << 2)
514 #define NV50_IR_MOD_NOT (1 << 3)
515 #define NV50_IR_MOD_NEG_ABS (NV50_IR_MOD_NEG | NV50_IR_MOD_ABS)
516
517 #define NV50_IR_INTERP_MODE_MASK 0x3
518 #define NV50_IR_INTERP_LINEAR (0 << 0)
519 #define NV50_IR_INTERP_PERSPECTIVE (1 << 0)
520 #define NV50_IR_INTERP_FLAT (2 << 0)
521 #define NV50_IR_INTERP_SC (3 << 0) // what exactly is that ?
522 #define NV50_IR_INTERP_SAMPLE_MASK 0xc
523 #define NV50_IR_INTERP_DEFAULT (0 << 2)
524 #define NV50_IR_INTERP_CENTROID (1 << 2)
525 #define NV50_IR_INTERP_OFFSET (2 << 2)
526 #define NV50_IR_INTERP_SAMPLEID (3 << 2)
527
528 // do we really want this to be a class ?
529 class Modifier
530 {
531 public:
532 Modifier() : bits(0) { }
533 Modifier(unsigned int m) : bits(m) { }
534 Modifier(operation op);
535
536 // @return new Modifier applying a after b (asserts if unrepresentable)
537 Modifier operator*(const Modifier) const;
538 Modifier operator*=(const Modifier m) { *this = *this * m; return *this; }
539 Modifier operator==(const Modifier m) const { return m.bits == bits; }
540 Modifier operator!=(const Modifier m) const { return m.bits != bits; }
541
542 inline Modifier operator&(const Modifier m) const { return bits & m.bits; }
543 inline Modifier operator|(const Modifier m) const { return bits | m.bits; }
544 inline Modifier operator^(const Modifier m) const { return bits ^ m.bits; }
545
546 operation getOp() const;
547
548 inline int neg() const { return (bits & NV50_IR_MOD_NEG) ? 1 : 0; }
549 inline int abs() const { return (bits & NV50_IR_MOD_ABS) ? 1 : 0; }
550
551 inline operator bool() const { return bits ? true : false; }
552
553 void applyTo(ImmediateValue &imm) const;
554
555 int print(char *buf, size_t size) const;
556
557 private:
558 uint8_t bits;
559 };
560
561 class ValueRef
562 {
563 public:
564 ValueRef(Value * = NULL);
565 ValueRef(const ValueRef&);
566 ~ValueRef();
567
568 inline bool exists() const { return value != NULL; }
569
570 void set(Value *);
571 void set(const ValueRef&);
572 inline Value *get() const { return value; }
573 inline Value *rep() const;
574
575 inline Instruction *getInsn() const { return insn; }
576 inline void setInsn(Instruction *inst) { insn = inst; }
577
578 inline bool isIndirect(int dim) const { return indirect[dim] >= 0; }
579 inline const ValueRef *getIndirect(int dim) const;
580
581 inline DataFile getFile() const;
582 inline unsigned getSize() const;
583
584 // SSA: return eventual (traverse MOVs) literal value, if it exists
585 bool getImmediate(ImmediateValue&) const;
586
587 public:
588 Modifier mod;
589 int8_t indirect[2]; // >= 0 if relative to lvalue in insn->src(indirect[i])
590 uint8_t swizzle;
591
592 bool usedAsPtr; // for printing
593
594 private:
595 Value *value;
596 Instruction *insn;
597 };
598
599 class ValueDef
600 {
601 public:
602 ValueDef(Value * = NULL);
603 ValueDef(const ValueDef&);
604 ~ValueDef();
605
606 inline bool exists() const { return value != NULL; }
607
608 inline Value *get() const { return value; }
609 inline Value *rep() const;
610 void set(Value *);
611 bool mayReplace(const ValueRef &);
612 void replace(const ValueRef &, bool doSet); // replace all uses of the old value
613
614 inline Instruction *getInsn() const { return insn; }
615 inline void setInsn(Instruction *inst) { insn = inst; }
616
617 inline DataFile getFile() const;
618 inline unsigned getSize() const;
619
620 inline void setSSA(LValue *);
621 inline const LValue *preSSA() const;
622
623 private:
624 Value *value; // should make this LValue * ...
625 LValue *origin; // pre SSA value
626 Instruction *insn;
627 };
628
629 class Value
630 {
631 public:
632 Value();
633 virtual ~Value() { }
634
635 virtual Value *clone(ClonePolicy<Function>&) const = 0;
636
637 virtual int print(char *, size_t, DataType ty = TYPE_NONE) const = 0;
638
639 virtual bool equals(const Value *, bool strict = false) const;
640 virtual bool interfers(const Value *) const;
641 virtual bool isUniform() const { return true; }
642
643 inline Value *rep() const { return join; }
644
645 inline Instruction *getUniqueInsn() const;
646 inline Instruction *getInsn() const; // use when uniqueness is certain
647
648 inline int refCount() { return uses.size(); }
649
650 inline LValue *asLValue();
651 inline Symbol *asSym();
652 inline ImmediateValue *asImm();
653 inline const Symbol *asSym() const;
654 inline const ImmediateValue *asImm() const;
655
656 inline bool inFile(DataFile f) { return reg.file == f; }
657
658 static inline Value *get(Iterator&);
659
660 unordered_set<ValueRef *> uses;
661 std::list<ValueDef *> defs;
662 typedef unordered_set<ValueRef *>::iterator UseIterator;
663 typedef unordered_set<ValueRef *>::const_iterator UseCIterator;
664 typedef std::list<ValueDef *>::iterator DefIterator;
665 typedef std::list<ValueDef *>::const_iterator DefCIterator;
666
667 int id;
668 Storage reg;
669
670 // TODO: these should be in LValue:
671 Interval livei;
672 Value *join;
673 };
674
675 class LValue : public Value
676 {
677 public:
678 LValue(Function *, DataFile file);
679 LValue(Function *, LValue *);
680 ~LValue() { }
681
682 virtual bool isUniform() const;
683
684 virtual LValue *clone(ClonePolicy<Function>&) const;
685
686 virtual int print(char *, size_t, DataType ty = TYPE_NONE) const;
687
688 public:
689 unsigned compMask : 8; // compound/component mask
690 unsigned compound : 1; // used by RA, value involved in split/merge
691 unsigned ssa : 1;
692 unsigned fixedReg : 1; // set & used by RA, earlier just use (id < 0)
693 unsigned noSpill : 1; // do not spill (e.g. if spill temporary already)
694 };
695
696 class Symbol : public Value
697 {
698 public:
699 Symbol(Program *, DataFile file = FILE_MEMORY_CONST, ubyte fileIdx = 0);
700 ~Symbol() { }
701
702 virtual Symbol *clone(ClonePolicy<Function>&) const;
703
704 virtual bool equals(const Value *that, bool strict) const;
705
706 virtual bool isUniform() const;
707
708 virtual int print(char *, size_t, DataType ty = TYPE_NONE) const;
709
710 // print with indirect values
711 int print(char *, size_t, Value *, Value *, DataType ty = TYPE_NONE) const;
712
713 inline void setFile(DataFile file, ubyte fileIndex = 0)
714 {
715 reg.file = file;
716 reg.fileIndex = fileIndex;
717 }
718
719 inline void setOffset(int32_t offset);
720 inline void setAddress(Symbol *base, int32_t offset);
721 inline void setSV(SVSemantic sv, uint32_t idx = 0);
722
723 inline const Symbol *getBase() const { return baseSym; }
724
725 private:
726 Symbol *baseSym; // array base for Symbols representing array elements
727 };
728
729 class ImmediateValue : public Value
730 {
731 public:
732 ImmediateValue() { }
733 ImmediateValue(Program *, uint32_t);
734 ImmediateValue(Program *, float);
735 ImmediateValue(Program *, double);
736 // NOTE: not added to program with
737 ImmediateValue(const ImmediateValue *, DataType ty);
738 ~ImmediateValue() { };
739
740 virtual ImmediateValue *clone(ClonePolicy<Function>&) const;
741
742 virtual bool equals(const Value *that, bool strict) const;
743
744 // these only work if 'type' is valid (we mostly use untyped literals):
745 bool isInteger(const int ival) const; // ival is cast to this' type
746 bool isNegative() const;
747 bool isPow2() const;
748
749 void applyLog2();
750
751 // for constant folding:
752 ImmediateValue operator+(const ImmediateValue&) const;
753 ImmediateValue operator-(const ImmediateValue&) const;
754 ImmediateValue operator*(const ImmediateValue&) const;
755 ImmediateValue operator/(const ImmediateValue&) const;
756
757 ImmediateValue& operator=(const ImmediateValue&); // only sets value !
758
759 bool compare(CondCode cc, float fval) const;
760
761 virtual int print(char *, size_t, DataType ty = TYPE_NONE) const;
762 };
763
764 class Instruction
765 {
766 public:
767 Instruction();
768 Instruction(Function *, operation, DataType);
769 virtual ~Instruction();
770
771 virtual Instruction *clone(ClonePolicy<Function>&,
772 Instruction * = NULL) const;
773
774 void setDef(int i, Value *);
775 void setSrc(int s, Value *);
776 void setSrc(int s, const ValueRef&);
777 void swapSources(int a, int b);
778 void moveSources(int s, int delta);
779 bool setIndirect(int s, int dim, Value *);
780
781 inline ValueRef& src(int s) { return srcs[s]; }
782 inline ValueDef& def(int s) { return defs[s]; }
783 inline const ValueRef& src(int s) const { return srcs[s]; }
784 inline const ValueDef& def(int s) const { return defs[s]; }
785
786 inline Value *getDef(int d) const { return defs[d].get(); }
787 inline Value *getSrc(int s) const { return srcs[s].get(); }
788 inline Value *getIndirect(int s, int dim) const;
789
790 inline bool defExists(unsigned d) const
791 {
792 return d < defs.size() && defs[d].exists();
793 }
794 inline bool srcExists(unsigned s) const
795 {
796 return s < srcs.size() && srcs[s].exists();
797 }
798
799 inline bool constrainedDefs() const;
800
801 bool setPredicate(CondCode ccode, Value *);
802 inline Value *getPredicate() const;
803 bool writesPredicate() const;
804 inline bool isPredicated() const { return predSrc >= 0; }
805
806 inline void setFlagsSrc(int s, Value *);
807 inline void setFlagsDef(int d, Value *);
808 inline bool usesFlags() const { return flagsSrc >= 0; }
809
810 unsigned int defCount() const { return defs.size(); };
811 unsigned int defCount(unsigned int mask, bool singleFile = false) const;
812 unsigned int srcCount() const { return srcs.size(); };
813 unsigned int srcCount(unsigned int mask, bool singleFile = false) const;
814
815 // save & remove / set indirect[0,1] and predicate source
816 void takeExtraSources(int s, Value *[3]);
817 void putExtraSources(int s, Value *[3]);
818
819 inline void setType(DataType type) { dType = sType = type; }
820
821 inline void setType(DataType dtype, DataType stype)
822 {
823 dType = dtype;
824 sType = stype;
825 }
826
827 inline bool isPseudo() const { return op < OP_MOV; }
828 bool isDead() const;
829 bool isNop() const;
830 bool isCommutationLegal(const Instruction *) const; // must be adjacent !
831 bool isActionEqual(const Instruction *) const;
832 bool isResultEqual(const Instruction *) const;
833
834 void print() const;
835
836 inline CmpInstruction *asCmp();
837 inline TexInstruction *asTex();
838 inline FlowInstruction *asFlow();
839 inline const TexInstruction *asTex() const;
840 inline const CmpInstruction *asCmp() const;
841 inline const FlowInstruction *asFlow() const;
842
843 public:
844 Instruction *next;
845 Instruction *prev;
846 int id;
847 int serial; // CFG order
848
849 operation op;
850 DataType dType; // destination or defining type
851 DataType sType; // source or secondary type
852 CondCode cc;
853 RoundMode rnd;
854 CacheMode cache;
855
856 uint16_t subOp; // quadop, 1 for mul-high, etc.
857
858 unsigned encSize : 4; // encoding size in bytes
859 unsigned saturate : 1; // to [0.0f, 1.0f]
860 unsigned join : 1; // converge control flow (use OP_JOIN until end)
861 unsigned fixed : 1; // prevent dead code elimination
862 unsigned terminator : 1; // end of basic block
863 unsigned ftz : 1; // flush denormal to zero
864 unsigned dnz : 1; // denormals, NaN are zero
865 unsigned ipa : 4; // interpolation mode
866 unsigned lanes : 4;
867 unsigned perPatch : 1;
868 unsigned exit : 1; // terminate program after insn
869 unsigned mask : 4; // for vector ops
870
871 int8_t postFactor; // MUL/DIV(if < 0) by 1 << postFactor
872
873 int8_t predSrc;
874 int8_t flagsDef;
875 int8_t flagsSrc;
876
877 uint32_t sched; // scheduling data (NOTE: maybe move to separate storage)
878
879 BasicBlock *bb;
880
881 protected:
882 std::deque<ValueDef> defs; // no gaps !
883 std::deque<ValueRef> srcs; // no gaps !
884
885 // instruction specific methods:
886 // (don't want to subclass, would need more constructors and memory pools)
887 public:
888 inline void setInterpolate(unsigned int mode) { ipa = mode; }
889
890 unsigned int getInterpMode() const { return ipa & 0x3; }
891 unsigned int getSampleMode() const { return ipa & 0xc; }
892
893 private:
894 void init();
895 };
896
897 enum TexQuery
898 {
899 TXQ_DIMS, /* x, y, z, levels */
900 TXQ_TYPE, /* ?, ?, samples, ? */
901 TXQ_SAMPLE_POSITION,
902 TXQ_FILTER,
903 TXQ_LOD,
904 TXQ_WRAP,
905 TXQ_BORDER_COLOUR
906 };
907
908 class TexInstruction : public Instruction
909 {
910 public:
911 class Target
912 {
913 public:
914 Target(TexTarget targ = TEX_TARGET_2D) : target(targ) { }
915
916 const char *getName() const { return descTable[target].name; }
917 unsigned int getArgCount() const { return descTable[target].argc; }
918 unsigned int getDim() const { return descTable[target].dim; }
919 int isArray() const { return descTable[target].array ? 1 : 0; }
920 int isCube() const { return descTable[target].cube ? 1 : 0; }
921 int isShadow() const { return descTable[target].shadow ? 1 : 0; }
922 int isMS() const {
923 return target == TEX_TARGET_2D_MS || target == TEX_TARGET_2D_MS_ARRAY; }
924 void clearMS() {
925 if (isMS()) {
926 if (isArray())
927 target = TEX_TARGET_2D_ARRAY;
928 else
929 target = TEX_TARGET_2D;
930 }
931 }
932
933 Target& operator=(TexTarget targ)
934 {
935 assert(targ < TEX_TARGET_COUNT);
936 target = targ;
937 return *this;
938 }
939
940 inline bool operator==(TexTarget targ) const { return target == targ; }
941 inline bool operator!=(TexTarget targ) const { return target != targ; }
942
943 enum TexTarget getEnum() const { return target; }
944
945 private:
946 struct Desc
947 {
948 char name[19];
949 uint8_t dim;
950 uint8_t argc;
951 bool array;
952 bool cube;
953 bool shadow;
954 };
955
956 static const struct Desc descTable[TEX_TARGET_COUNT];
957
958 private:
959 enum TexTarget target;
960 };
961
962 public:
963 struct ImgFormatDesc
964 {
965 char name[19];
966 uint8_t components;
967 uint8_t bits[4];
968 ImgType type;
969 };
970
971 static const struct ImgFormatDesc formatTable[IMG_FORMAT_COUNT];
972
973 public:
974 TexInstruction(Function *, operation);
975 virtual ~TexInstruction();
976
977 virtual TexInstruction *clone(ClonePolicy<Function>&,
978 Instruction * = NULL) const;
979
980 inline void setTexture(Target targ, uint8_t r, uint8_t s)
981 {
982 tex.r = r;
983 tex.s = s;
984 tex.target = targ;
985 }
986
987 void setIndirectR(Value *);
988 void setIndirectS(Value *);
989 inline Value *getIndirectR() const;
990 inline Value *getIndirectS() const;
991
992 public:
993 struct {
994 Target target;
995
996 uint16_t r;
997 uint16_t s;
998 int8_t rIndirectSrc;
999 int8_t sIndirectSrc;
1000
1001 uint8_t mask;
1002 uint8_t gatherComp;
1003
1004 bool liveOnly; // only execute on live pixels of a quad (optimization)
1005 bool levelZero;
1006 bool derivAll;
1007
1008 int8_t useOffsets; // 0, 1, or 4 for textureGatherOffsets
1009 int8_t offset[3]; // only used on nv50
1010
1011 enum TexQuery query;
1012 const struct ImgFormatDesc *format;
1013 } tex;
1014
1015 ValueRef dPdx[3];
1016 ValueRef dPdy[3];
1017 ValueRef offset[4][3];
1018 };
1019
1020 class CmpInstruction : public Instruction
1021 {
1022 public:
1023 CmpInstruction(Function *, operation);
1024
1025 virtual CmpInstruction *clone(ClonePolicy<Function>&,
1026 Instruction * = NULL) const;
1027
1028 void setCondition(CondCode cond) { setCond = cond; }
1029 CondCode getCondition() const { return setCond; }
1030
1031 public:
1032 CondCode setCond;
1033 };
1034
1035 class FlowInstruction : public Instruction
1036 {
1037 public:
1038 FlowInstruction(Function *, operation, void *target);
1039
1040 virtual FlowInstruction *clone(ClonePolicy<Function>&,
1041 Instruction * = NULL) const;
1042
1043 public:
1044 unsigned allWarp : 1;
1045 unsigned absolute : 1;
1046 unsigned limit : 1;
1047 unsigned builtin : 1; // true for calls to emulation code
1048 unsigned indirect : 1; // target in src(0)
1049
1050 union {
1051 BasicBlock *bb;
1052 int builtin;
1053 Function *fn;
1054 } target;
1055 };
1056
1057 class BasicBlock
1058 {
1059 public:
1060 BasicBlock(Function *);
1061 ~BasicBlock();
1062
1063 BasicBlock *clone(ClonePolicy<Function>&) const;
1064
1065 inline int getId() const { return id; }
1066 inline unsigned int getInsnCount() const { return numInsns; }
1067 inline bool isTerminated() const { return exit && exit->terminator; }
1068
1069 bool dominatedBy(BasicBlock *bb);
1070 inline bool reachableBy(const BasicBlock *by, const BasicBlock *term);
1071
1072 // returns mask of conditional out blocks
1073 // e.g. 3 for IF { .. } ELSE { .. } ENDIF, 1 for IF { .. } ENDIF
1074 unsigned int initiatesSimpleConditional() const;
1075
1076 public:
1077 Function *getFunction() const { return func; }
1078 Program *getProgram() const { return program; }
1079
1080 Instruction *getEntry() const { return entry; } // first non-phi instruction
1081 Instruction *getPhi() const { return phi; }
1082 Instruction *getFirst() const { return phi ? phi : entry; }
1083 Instruction *getExit() const { return exit; }
1084
1085 void insertHead(Instruction *);
1086 void insertTail(Instruction *);
1087 void insertBefore(Instruction *, Instruction *);
1088 void insertAfter(Instruction *, Instruction *);
1089 void remove(Instruction *);
1090 void permuteAdjacent(Instruction *, Instruction *);
1091
1092 BasicBlock *idom() const;
1093
1094 // NOTE: currently does not rebuild the dominator tree
1095 BasicBlock *splitBefore(Instruction *, bool attach = true);
1096 BasicBlock *splitAfter(Instruction *, bool attach = true);
1097
1098 DLList& getDF() { return df; }
1099 DLList::Iterator iterDF() { return df.iterator(); }
1100
1101 static inline BasicBlock *get(Iterator&);
1102 static inline BasicBlock *get(Graph::Node *);
1103
1104 public:
1105 Graph::Node cfg; // first edge is branch *taken* (the ELSE branch)
1106 Graph::Node dom;
1107
1108 BitSet liveSet;
1109 BitSet defSet;
1110
1111 uint32_t binPos;
1112 uint32_t binSize;
1113
1114 Instruction *joinAt; // for quick reference
1115
1116 bool explicitCont; // loop headers: true if loop contains continue stmts
1117
1118 private:
1119 int id;
1120 DLList df;
1121
1122 Instruction *phi;
1123 Instruction *entry;
1124 Instruction *exit;
1125
1126 unsigned int numInsns;
1127
1128 private:
1129 Function *func;
1130 Program *program;
1131
1132 void splitCommon(Instruction *, BasicBlock *, bool attach);
1133 };
1134
1135 class Function
1136 {
1137 public:
1138 Function(Program *, const char *name, uint32_t label);
1139 ~Function();
1140
1141 static inline Function *get(Graph::Node *node);
1142
1143 inline Program *getProgram() const { return prog; }
1144 inline const char *getName() const { return name; }
1145 inline int getId() const { return id; }
1146 inline uint32_t getLabel() const { return label; }
1147
1148 void print();
1149 void printLiveIntervals() const;
1150 void printCFGraph(const char *filePath);
1151
1152 bool setEntry(BasicBlock *);
1153 bool setExit(BasicBlock *);
1154
1155 unsigned int orderInstructions(ArrayList&);
1156
1157 inline void add(BasicBlock *bb, int& id) { allBBlocks.insert(bb, id); }
1158 inline void add(Instruction *insn, int& id) { allInsns.insert(insn, id); }
1159 inline void add(LValue *lval, int& id) { allLValues.insert(lval, id); }
1160
1161 inline LValue *getLValue(int id);
1162
1163 void buildLiveSets();
1164 void buildDefSets();
1165 bool convertToSSA();
1166
1167 public:
1168 std::deque<ValueDef> ins;
1169 std::deque<ValueRef> outs;
1170 std::deque<Value *> clobbers;
1171
1172 Graph cfg;
1173 Graph::Node *cfgExit;
1174 Graph *domTree;
1175 Graph::Node call; // node in the call graph
1176
1177 BasicBlock **bbArray; // BBs in emission order
1178 int bbCount;
1179
1180 unsigned int loopNestingBound;
1181 int regClobberMax;
1182
1183 uint32_t binPos;
1184 uint32_t binSize;
1185
1186 Value *stackPtr;
1187
1188 uint32_t tlsBase; // base address for l[] space (if no stack pointer is used)
1189 uint32_t tlsSize;
1190
1191 ArrayList allBBlocks;
1192 ArrayList allInsns;
1193 ArrayList allLValues;
1194
1195 private:
1196 void buildLiveSetsPreSSA(BasicBlock *, const int sequence);
1197 void buildDefSetsPreSSA(BasicBlock *bb, const int seq);
1198
1199 private:
1200 uint32_t label;
1201 int id;
1202 const char *const name;
1203 Program *prog;
1204 };
1205
1206 enum CGStage
1207 {
1208 CG_STAGE_PRE_SSA,
1209 CG_STAGE_SSA, // expected directly before register allocation
1210 CG_STAGE_POST_RA
1211 };
1212
1213 class Program
1214 {
1215 public:
1216 enum Type
1217 {
1218 TYPE_VERTEX,
1219 TYPE_TESSELLATION_CONTROL,
1220 TYPE_TESSELLATION_EVAL,
1221 TYPE_GEOMETRY,
1222 TYPE_FRAGMENT,
1223 TYPE_COMPUTE
1224 };
1225
1226 Program(Type type, Target *targ);
1227 ~Program();
1228
1229 void print();
1230
1231 Type getType() const { return progType; }
1232
1233 inline void add(Function *fn, int& id) { allFuncs.insert(fn, id); }
1234 inline void del(Function *fn, int& id) { allFuncs.remove(id); }
1235 inline void add(Value *rval, int& id) { allRValues.insert(rval, id); }
1236
1237 bool makeFromTGSI(struct nv50_ir_prog_info *);
1238 bool makeFromSM4(struct nv50_ir_prog_info *);
1239 bool convertToSSA();
1240 bool optimizeSSA(int level);
1241 bool optimizePostRA(int level);
1242 bool registerAllocation();
1243 bool emitBinary(struct nv50_ir_prog_info *);
1244
1245 const Target *getTarget() const { return target; }
1246
1247 private:
1248 void emitSymbolTable(struct nv50_ir_prog_info *);
1249
1250 Type progType;
1251 Target *target;
1252
1253 public:
1254 Function *main;
1255 Graph calls;
1256
1257 ArrayList allFuncs;
1258 ArrayList allRValues;
1259
1260 uint32_t *code;
1261 uint32_t binSize;
1262 uint32_t tlsSize; // size required for FILE_MEMORY_LOCAL
1263
1264 int maxGPR;
1265
1266 MemoryPool mem_Instruction;
1267 MemoryPool mem_CmpInstruction;
1268 MemoryPool mem_TexInstruction;
1269 MemoryPool mem_FlowInstruction;
1270 MemoryPool mem_LValue;
1271 MemoryPool mem_Symbol;
1272 MemoryPool mem_ImmediateValue;
1273
1274 uint32_t dbgFlags;
1275 uint8_t optLevel;
1276
1277 void *targetPriv; // e.g. to carry information between passes
1278
1279 const struct nv50_ir_prog_info *driver; // for driver configuration
1280
1281 void releaseInstruction(Instruction *);
1282 void releaseValue(Value *);
1283 };
1284
1285 // TODO: add const version
1286 class Pass
1287 {
1288 public:
1289 bool run(Program *, bool ordered = false, bool skipPhi = false);
1290 bool run(Function *, bool ordered = false, bool skipPhi = false);
1291
1292 private:
1293 // return false to continue with next entity on next higher level
1294 virtual bool visit(Function *) { return true; }
1295 virtual bool visit(BasicBlock *) { return true; }
1296 virtual bool visit(Instruction *) { return false; }
1297
1298 bool doRun(Program *, bool ordered, bool skipPhi);
1299 bool doRun(Function *, bool ordered, bool skipPhi);
1300
1301 protected:
1302 bool err;
1303 Function *func;
1304 Program *prog;
1305 };
1306
1307 // =============================================================================
1308
1309 #include "codegen/nv50_ir_inlines.h"
1310
1311 } // namespace nv50_ir
1312
1313 #endif // __NV50_IR_H__