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