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