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