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