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