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