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