nir: Add a bit_size to nir_register and nir_ssa_def
[mesa.git] / src / compiler / nir / nir.h
1 /*
2 * Copyright © 2014 Connor Abbott
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 (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Connor Abbott (cwabbott0@gmail.com)
25 *
26 */
27
28 #pragma once
29
30 #include "util/hash_table.h"
31 #include "compiler/glsl/list.h"
32 #include "GL/gl.h" /* GLenum */
33 #include "util/list.h"
34 #include "util/ralloc.h"
35 #include "util/set.h"
36 #include "util/bitset.h"
37 #include "compiler/nir_types.h"
38 #include "compiler/shader_enums.h"
39 #include <stdio.h>
40
41 #include "nir_opcodes.h"
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 struct gl_program;
48 struct gl_shader_program;
49
50 #define NIR_FALSE 0u
51 #define NIR_TRUE (~0u)
52
53 /** Defines a cast function
54 *
55 * This macro defines a cast function from in_type to out_type where
56 * out_type is some structure type that contains a field of type out_type.
57 *
58 * Note that you have to be a bit careful as the generated cast function
59 * destroys constness.
60 */
61 #define NIR_DEFINE_CAST(name, in_type, out_type, field) \
62 static inline out_type * \
63 name(const in_type *parent) \
64 { \
65 return exec_node_data(out_type, parent, field); \
66 }
67
68 struct nir_function;
69 struct nir_shader;
70 struct nir_instr;
71
72
73 /**
74 * Description of built-in state associated with a uniform
75 *
76 * \sa nir_variable::state_slots
77 */
78 typedef struct {
79 int tokens[5];
80 int swizzle;
81 } nir_state_slot;
82
83 typedef enum {
84 nir_var_all = -1,
85 nir_var_shader_in,
86 nir_var_shader_out,
87 nir_var_global,
88 nir_var_local,
89 nir_var_uniform,
90 nir_var_shader_storage,
91 nir_var_system_value,
92 nir_var_param,
93 nir_var_shared,
94 } nir_variable_mode;
95
96 /**
97 * Data stored in an nir_constant
98 */
99 union nir_constant_data {
100 unsigned u[16];
101 int i[16];
102 float f[16];
103 bool b[16];
104 };
105
106 typedef struct nir_constant {
107 /**
108 * Value of the constant.
109 *
110 * The field used to back the values supplied by the constant is determined
111 * by the type associated with the \c nir_variable. Constants may be
112 * scalars, vectors, or matrices.
113 */
114 union nir_constant_data value;
115
116 /* we could get this from the var->type but makes clone *much* easier to
117 * not have to care about the type.
118 */
119 unsigned num_elements;
120
121 /* Array elements / Structure Fields */
122 struct nir_constant **elements;
123 } nir_constant;
124
125 /**
126 * \brief Layout qualifiers for gl_FragDepth.
127 *
128 * The AMD/ARB_conservative_depth extensions allow gl_FragDepth to be redeclared
129 * with a layout qualifier.
130 */
131 typedef enum {
132 nir_depth_layout_none, /**< No depth layout is specified. */
133 nir_depth_layout_any,
134 nir_depth_layout_greater,
135 nir_depth_layout_less,
136 nir_depth_layout_unchanged
137 } nir_depth_layout;
138
139 /**
140 * Either a uniform, global variable, shader input, or shader output. Based on
141 * ir_variable - it should be easy to translate between the two.
142 */
143
144 typedef struct nir_variable {
145 struct exec_node node;
146
147 /**
148 * Declared type of the variable
149 */
150 const struct glsl_type *type;
151
152 /**
153 * Declared name of the variable
154 */
155 char *name;
156
157 struct nir_variable_data {
158
159 /**
160 * Is the variable read-only?
161 *
162 * This is set for variables declared as \c const, shader inputs,
163 * and uniforms.
164 */
165 unsigned read_only:1;
166 unsigned centroid:1;
167 unsigned sample:1;
168 unsigned patch:1;
169 unsigned invariant:1;
170
171 /**
172 * Storage class of the variable.
173 *
174 * \sa nir_variable_mode
175 */
176 nir_variable_mode mode:5;
177
178 /**
179 * Interpolation mode for shader inputs / outputs
180 *
181 * \sa glsl_interp_qualifier
182 */
183 unsigned interpolation:2;
184
185 /**
186 * \name ARB_fragment_coord_conventions
187 * @{
188 */
189 unsigned origin_upper_left:1;
190 unsigned pixel_center_integer:1;
191 /*@}*/
192
193 /**
194 * Was the location explicitly set in the shader?
195 *
196 * If the location is explicitly set in the shader, it \b cannot be changed
197 * by the linker or by the API (e.g., calls to \c glBindAttribLocation have
198 * no effect).
199 */
200 unsigned explicit_location:1;
201 unsigned explicit_index:1;
202
203 /**
204 * Was an initial binding explicitly set in the shader?
205 *
206 * If so, constant_initializer contains an integer nir_constant
207 * representing the initial binding point.
208 */
209 unsigned explicit_binding:1;
210
211 /**
212 * Does this variable have an initializer?
213 *
214 * This is used by the linker to cross-validiate initializers of global
215 * variables.
216 */
217 unsigned has_initializer:1;
218
219 /**
220 * If non-zero, then this variable may be packed along with other variables
221 * into a single varying slot, so this offset should be applied when
222 * accessing components. For example, an offset of 1 means that the x
223 * component of this variable is actually stored in component y of the
224 * location specified by \c location.
225 */
226 unsigned location_frac:2;
227
228 /**
229 * \brief Layout qualifier for gl_FragDepth.
230 *
231 * This is not equal to \c ir_depth_layout_none if and only if this
232 * variable is \c gl_FragDepth and a layout qualifier is specified.
233 */
234 nir_depth_layout depth_layout;
235
236 /**
237 * Storage location of the base of this variable
238 *
239 * The precise meaning of this field depends on the nature of the variable.
240 *
241 * - Vertex shader input: one of the values from \c gl_vert_attrib.
242 * - Vertex shader output: one of the values from \c gl_varying_slot.
243 * - Geometry shader input: one of the values from \c gl_varying_slot.
244 * - Geometry shader output: one of the values from \c gl_varying_slot.
245 * - Fragment shader input: one of the values from \c gl_varying_slot.
246 * - Fragment shader output: one of the values from \c gl_frag_result.
247 * - Uniforms: Per-stage uniform slot number for default uniform block.
248 * - Uniforms: Index within the uniform block definition for UBO members.
249 * - Non-UBO Uniforms: uniform slot number.
250 * - Other: This field is not currently used.
251 *
252 * If the variable is a uniform, shader input, or shader output, and the
253 * slot has not been assigned, the value will be -1.
254 */
255 int location;
256
257 /**
258 * The actual location of the variable in the IR. Only valid for inputs
259 * and outputs.
260 */
261 unsigned int driver_location;
262
263 /**
264 * output index for dual source blending.
265 */
266 int index;
267
268 /**
269 * Initial binding point for a sampler or UBO.
270 *
271 * For array types, this represents the binding point for the first element.
272 */
273 int binding;
274
275 /**
276 * Location an atomic counter is stored at.
277 */
278 unsigned offset;
279
280 /**
281 * ARB_shader_image_load_store qualifiers.
282 */
283 struct {
284 bool read_only; /**< "readonly" qualifier. */
285 bool write_only; /**< "writeonly" qualifier. */
286 bool coherent;
287 bool _volatile;
288 bool restrict_flag;
289
290 /** Image internal format if specified explicitly, otherwise GL_NONE. */
291 GLenum format;
292 } image;
293
294 /**
295 * Highest element accessed with a constant expression array index
296 *
297 * Not used for non-array variables.
298 */
299 unsigned max_array_access;
300
301 } data;
302
303 /**
304 * Built-in state that backs this uniform
305 *
306 * Once set at variable creation, \c state_slots must remain invariant.
307 * This is because, ideally, this array would be shared by all clones of
308 * this variable in the IR tree. In other words, we'd really like for it
309 * to be a fly-weight.
310 *
311 * If the variable is not a uniform, \c num_state_slots will be zero and
312 * \c state_slots will be \c NULL.
313 */
314 /*@{*/
315 unsigned num_state_slots; /**< Number of state slots used */
316 nir_state_slot *state_slots; /**< State descriptors. */
317 /*@}*/
318
319 /**
320 * Constant expression assigned in the initializer of the variable
321 */
322 nir_constant *constant_initializer;
323
324 /**
325 * For variables that are in an interface block or are an instance of an
326 * interface block, this is the \c GLSL_TYPE_INTERFACE type for that block.
327 *
328 * \sa ir_variable::location
329 */
330 const struct glsl_type *interface_type;
331 } nir_variable;
332
333 #define nir_foreach_variable(var, var_list) \
334 foreach_list_typed(nir_variable, var, node, var_list)
335
336 static inline bool
337 nir_variable_is_global(const nir_variable *var)
338 {
339 return var->data.mode != nir_var_local && var->data.mode != nir_var_param;
340 }
341
342 typedef struct nir_register {
343 struct exec_node node;
344
345 unsigned num_components; /** < number of vector components */
346 unsigned num_array_elems; /** < size of array (0 for no array) */
347
348 /* The bit-size of each channel; must be one of 8, 16, 32, or 64 */
349 uint8_t bit_size;
350
351 /** generic register index. */
352 unsigned index;
353
354 /** only for debug purposes, can be NULL */
355 const char *name;
356
357 /** whether this register is local (per-function) or global (per-shader) */
358 bool is_global;
359
360 /**
361 * If this flag is set to true, then accessing channels >= num_components
362 * is well-defined, and simply spills over to the next array element. This
363 * is useful for backends that can do per-component accessing, in
364 * particular scalar backends. By setting this flag and making
365 * num_components equal to 1, structures can be packed tightly into
366 * registers and then registers can be accessed per-component to get to
367 * each structure member, even if it crosses vec4 boundaries.
368 */
369 bool is_packed;
370
371 /** set of nir_src's where this register is used (read from) */
372 struct list_head uses;
373
374 /** set of nir_dest's where this register is defined (written to) */
375 struct list_head defs;
376
377 /** set of nir_if's where this register is used as a condition */
378 struct list_head if_uses;
379 } nir_register;
380
381 typedef enum {
382 nir_instr_type_alu,
383 nir_instr_type_call,
384 nir_instr_type_tex,
385 nir_instr_type_intrinsic,
386 nir_instr_type_load_const,
387 nir_instr_type_jump,
388 nir_instr_type_ssa_undef,
389 nir_instr_type_phi,
390 nir_instr_type_parallel_copy,
391 } nir_instr_type;
392
393 typedef struct nir_instr {
394 struct exec_node node;
395 nir_instr_type type;
396 struct nir_block *block;
397
398 /** generic instruction index. */
399 unsigned index;
400
401 /* A temporary for optimization and analysis passes to use for storing
402 * flags. For instance, DCE uses this to store the "dead/live" info.
403 */
404 uint8_t pass_flags;
405 } nir_instr;
406
407 static inline nir_instr *
408 nir_instr_next(nir_instr *instr)
409 {
410 struct exec_node *next = exec_node_get_next(&instr->node);
411 if (exec_node_is_tail_sentinel(next))
412 return NULL;
413 else
414 return exec_node_data(nir_instr, next, node);
415 }
416
417 static inline nir_instr *
418 nir_instr_prev(nir_instr *instr)
419 {
420 struct exec_node *prev = exec_node_get_prev(&instr->node);
421 if (exec_node_is_head_sentinel(prev))
422 return NULL;
423 else
424 return exec_node_data(nir_instr, prev, node);
425 }
426
427 static inline bool
428 nir_instr_is_first(nir_instr *instr)
429 {
430 return exec_node_is_head_sentinel(exec_node_get_prev(&instr->node));
431 }
432
433 static inline bool
434 nir_instr_is_last(nir_instr *instr)
435 {
436 return exec_node_is_tail_sentinel(exec_node_get_next(&instr->node));
437 }
438
439 typedef struct nir_ssa_def {
440 /** for debugging only, can be NULL */
441 const char* name;
442
443 /** generic SSA definition index. */
444 unsigned index;
445
446 /** Index into the live_in and live_out bitfields */
447 unsigned live_index;
448
449 nir_instr *parent_instr;
450
451 /** set of nir_instr's where this register is used (read from) */
452 struct list_head uses;
453
454 /** set of nir_if's where this register is used as a condition */
455 struct list_head if_uses;
456
457 uint8_t num_components;
458
459 /* The bit-size of each channel; must be one of 8, 16, 32, or 64 */
460 uint8_t bit_size;
461 } nir_ssa_def;
462
463 struct nir_src;
464
465 typedef struct {
466 nir_register *reg;
467 struct nir_src *indirect; /** < NULL for no indirect offset */
468 unsigned base_offset;
469
470 /* TODO use-def chain goes here */
471 } nir_reg_src;
472
473 typedef struct {
474 nir_instr *parent_instr;
475 struct list_head def_link;
476
477 nir_register *reg;
478 struct nir_src *indirect; /** < NULL for no indirect offset */
479 unsigned base_offset;
480
481 /* TODO def-use chain goes here */
482 } nir_reg_dest;
483
484 struct nir_if;
485
486 typedef struct nir_src {
487 union {
488 nir_instr *parent_instr;
489 struct nir_if *parent_if;
490 };
491
492 struct list_head use_link;
493
494 union {
495 nir_reg_src reg;
496 nir_ssa_def *ssa;
497 };
498
499 bool is_ssa;
500 } nir_src;
501
502 #define NIR_SRC_INIT (nir_src) { { NULL } }
503
504 #define nir_foreach_use(reg_or_ssa_def, src) \
505 list_for_each_entry(nir_src, src, &(reg_or_ssa_def)->uses, use_link)
506
507 #define nir_foreach_use_safe(reg_or_ssa_def, src) \
508 list_for_each_entry_safe(nir_src, src, &(reg_or_ssa_def)->uses, use_link)
509
510 #define nir_foreach_if_use(reg_or_ssa_def, src) \
511 list_for_each_entry(nir_src, src, &(reg_or_ssa_def)->if_uses, use_link)
512
513 #define nir_foreach_if_use_safe(reg_or_ssa_def, src) \
514 list_for_each_entry_safe(nir_src, src, &(reg_or_ssa_def)->if_uses, use_link)
515
516 typedef struct {
517 union {
518 nir_reg_dest reg;
519 nir_ssa_def ssa;
520 };
521
522 bool is_ssa;
523 } nir_dest;
524
525 #define NIR_DEST_INIT (nir_dest) { { { NULL } } }
526
527 #define nir_foreach_def(reg, dest) \
528 list_for_each_entry(nir_dest, dest, &(reg)->defs, reg.def_link)
529
530 #define nir_foreach_def_safe(reg, dest) \
531 list_for_each_entry_safe(nir_dest, dest, &(reg)->defs, reg.def_link)
532
533 static inline nir_src
534 nir_src_for_ssa(nir_ssa_def *def)
535 {
536 nir_src src = NIR_SRC_INIT;
537
538 src.is_ssa = true;
539 src.ssa = def;
540
541 return src;
542 }
543
544 static inline nir_src
545 nir_src_for_reg(nir_register *reg)
546 {
547 nir_src src = NIR_SRC_INIT;
548
549 src.is_ssa = false;
550 src.reg.reg = reg;
551 src.reg.indirect = NULL;
552 src.reg.base_offset = 0;
553
554 return src;
555 }
556
557 static inline nir_dest
558 nir_dest_for_reg(nir_register *reg)
559 {
560 nir_dest dest = NIR_DEST_INIT;
561
562 dest.reg.reg = reg;
563
564 return dest;
565 }
566
567 void nir_src_copy(nir_src *dest, const nir_src *src, void *instr_or_if);
568 void nir_dest_copy(nir_dest *dest, const nir_dest *src, nir_instr *instr);
569
570 typedef struct {
571 nir_src src;
572
573 /**
574 * \name input modifiers
575 */
576 /*@{*/
577 /**
578 * For inputs interpreted as floating point, flips the sign bit. For
579 * inputs interpreted as integers, performs the two's complement negation.
580 */
581 bool negate;
582
583 /**
584 * Clears the sign bit for floating point values, and computes the integer
585 * absolute value for integers. Note that the negate modifier acts after
586 * the absolute value modifier, therefore if both are set then all inputs
587 * will become negative.
588 */
589 bool abs;
590 /*@}*/
591
592 /**
593 * For each input component, says which component of the register it is
594 * chosen from. Note that which elements of the swizzle are used and which
595 * are ignored are based on the write mask for most opcodes - for example,
596 * a statement like "foo.xzw = bar.zyx" would have a writemask of 1101b and
597 * a swizzle of {2, x, 1, 0} where x means "don't care."
598 */
599 uint8_t swizzle[4];
600 } nir_alu_src;
601
602 typedef struct {
603 nir_dest dest;
604
605 /**
606 * \name saturate output modifier
607 *
608 * Only valid for opcodes that output floating-point numbers. Clamps the
609 * output to between 0.0 and 1.0 inclusive.
610 */
611
612 bool saturate;
613
614 unsigned write_mask : 4; /* ignored if dest.is_ssa is true */
615 } nir_alu_dest;
616
617 typedef enum {
618 nir_type_invalid = 0, /* Not a valid type */
619 nir_type_float,
620 nir_type_int,
621 nir_type_uint,
622 nir_type_bool,
623 nir_type_bool32 = 32 | nir_type_bool,
624 nir_type_int8 = 8 | nir_type_int,
625 nir_type_int16 = 16 | nir_type_int,
626 nir_type_int32 = 32 | nir_type_int,
627 nir_type_int64 = 64 | nir_type_int,
628 nir_type_uint8 = 8 | nir_type_uint,
629 nir_type_uint16 = 16 | nir_type_uint,
630 nir_type_uint32 = 32 | nir_type_uint,
631 nir_type_uint64 = 64 | nir_type_uint,
632 nir_type_float16 = 16 | nir_type_float,
633 nir_type_float32 = 32 | nir_type_float,
634 nir_type_float64 = 64 | nir_type_float,
635 } nir_alu_type;
636
637 #define NIR_ALU_TYPE_SIZE_MASK 0xfffffff8
638 #define NIR_ALU_TYPE_BASE_TYPE_MASK 0x00000007
639
640 static inline unsigned
641 nir_alu_type_get_type_size(nir_alu_type type)
642 {
643 return type & NIR_ALU_TYPE_SIZE_MASK;
644 }
645
646 static inline unsigned
647 nir_alu_type_get_base_type(nir_alu_type type)
648 {
649 return type & NIR_ALU_TYPE_BASE_TYPE_MASK;
650 }
651
652 typedef enum {
653 NIR_OP_IS_COMMUTATIVE = (1 << 0),
654 NIR_OP_IS_ASSOCIATIVE = (1 << 1),
655 } nir_op_algebraic_property;
656
657 typedef struct {
658 const char *name;
659
660 unsigned num_inputs;
661
662 /**
663 * The number of components in the output
664 *
665 * If non-zero, this is the size of the output and input sizes are
666 * explicitly given; swizzle and writemask are still in effect, but if
667 * the output component is masked out, then the input component may
668 * still be in use.
669 *
670 * If zero, the opcode acts in the standard, per-component manner; the
671 * operation is performed on each component (except the ones that are
672 * masked out) with the input being taken from the input swizzle for
673 * that component.
674 *
675 * The size of some of the inputs may be given (i.e. non-zero) even
676 * though output_size is zero; in that case, the inputs with a zero
677 * size act per-component, while the inputs with non-zero size don't.
678 */
679 unsigned output_size;
680
681 /**
682 * The type of vector that the instruction outputs. Note that the
683 * staurate modifier is only allowed on outputs with the float type.
684 */
685
686 nir_alu_type output_type;
687
688 /**
689 * The number of components in each input
690 */
691 unsigned input_sizes[4];
692
693 /**
694 * The type of vector that each input takes. Note that negate and
695 * absolute value are only allowed on inputs with int or float type and
696 * behave differently on the two.
697 */
698 nir_alu_type input_types[4];
699
700 nir_op_algebraic_property algebraic_properties;
701 } nir_op_info;
702
703 extern const nir_op_info nir_op_infos[nir_num_opcodes];
704
705 typedef struct nir_alu_instr {
706 nir_instr instr;
707 nir_op op;
708 nir_alu_dest dest;
709 nir_alu_src src[];
710 } nir_alu_instr;
711
712 void nir_alu_src_copy(nir_alu_src *dest, const nir_alu_src *src,
713 nir_alu_instr *instr);
714 void nir_alu_dest_copy(nir_alu_dest *dest, const nir_alu_dest *src,
715 nir_alu_instr *instr);
716
717 /* is this source channel used? */
718 static inline bool
719 nir_alu_instr_channel_used(nir_alu_instr *instr, unsigned src, unsigned channel)
720 {
721 if (nir_op_infos[instr->op].input_sizes[src] > 0)
722 return channel < nir_op_infos[instr->op].input_sizes[src];
723
724 return (instr->dest.write_mask >> channel) & 1;
725 }
726
727 /*
728 * For instructions whose destinations are SSA, get the number of channels
729 * used for a source
730 */
731 static inline unsigned
732 nir_ssa_alu_instr_src_components(const nir_alu_instr *instr, unsigned src)
733 {
734 assert(instr->dest.dest.is_ssa);
735
736 if (nir_op_infos[instr->op].input_sizes[src] > 0)
737 return nir_op_infos[instr->op].input_sizes[src];
738
739 return instr->dest.dest.ssa.num_components;
740 }
741
742 typedef enum {
743 nir_deref_type_var,
744 nir_deref_type_array,
745 nir_deref_type_struct
746 } nir_deref_type;
747
748 typedef struct nir_deref {
749 nir_deref_type deref_type;
750 struct nir_deref *child;
751 const struct glsl_type *type;
752 } nir_deref;
753
754 typedef struct {
755 nir_deref deref;
756
757 nir_variable *var;
758 } nir_deref_var;
759
760 /* This enum describes how the array is referenced. If the deref is
761 * direct then the base_offset is used. If the deref is indirect then then
762 * offset is given by base_offset + indirect. If the deref is a wildcard
763 * then the deref refers to all of the elements of the array at the same
764 * time. Wildcard dereferences are only ever allowed in copy_var
765 * intrinsics and the source and destination derefs must have matching
766 * wildcards.
767 */
768 typedef enum {
769 nir_deref_array_type_direct,
770 nir_deref_array_type_indirect,
771 nir_deref_array_type_wildcard,
772 } nir_deref_array_type;
773
774 typedef struct {
775 nir_deref deref;
776
777 nir_deref_array_type deref_array_type;
778 unsigned base_offset;
779 nir_src indirect;
780 } nir_deref_array;
781
782 typedef struct {
783 nir_deref deref;
784
785 unsigned index;
786 } nir_deref_struct;
787
788 NIR_DEFINE_CAST(nir_deref_as_var, nir_deref, nir_deref_var, deref)
789 NIR_DEFINE_CAST(nir_deref_as_array, nir_deref, nir_deref_array, deref)
790 NIR_DEFINE_CAST(nir_deref_as_struct, nir_deref, nir_deref_struct, deref)
791
792 /* Returns the last deref in the chain. */
793 static inline nir_deref *
794 nir_deref_tail(nir_deref *deref)
795 {
796 while (deref->child)
797 deref = deref->child;
798 return deref;
799 }
800
801 typedef struct {
802 nir_instr instr;
803
804 unsigned num_params;
805 nir_deref_var **params;
806 nir_deref_var *return_deref;
807
808 struct nir_function *callee;
809 } nir_call_instr;
810
811 #define INTRINSIC(name, num_srcs, src_components, has_dest, dest_components, \
812 num_variables, num_indices, idx0, idx1, idx2, flags) \
813 nir_intrinsic_##name,
814
815 #define LAST_INTRINSIC(name) nir_last_intrinsic = nir_intrinsic_##name,
816
817 typedef enum {
818 #include "nir_intrinsics.h"
819 nir_num_intrinsics = nir_last_intrinsic + 1
820 } nir_intrinsic_op;
821
822 #undef INTRINSIC
823 #undef LAST_INTRINSIC
824
825 #define NIR_INTRINSIC_MAX_CONST_INDEX 3
826
827 /** Represents an intrinsic
828 *
829 * An intrinsic is an instruction type for handling things that are
830 * more-or-less regular operations but don't just consume and produce SSA
831 * values like ALU operations do. Intrinsics are not for things that have
832 * special semantic meaning such as phi nodes and parallel copies.
833 * Examples of intrinsics include variable load/store operations, system
834 * value loads, and the like. Even though texturing more-or-less falls
835 * under this category, texturing is its own instruction type because
836 * trying to represent texturing with intrinsics would lead to a
837 * combinatorial explosion of intrinsic opcodes.
838 *
839 * By having a single instruction type for handling a lot of different
840 * cases, optimization passes can look for intrinsics and, for the most
841 * part, completely ignore them. Each intrinsic type also has a few
842 * possible flags that govern whether or not they can be reordered or
843 * eliminated. That way passes like dead code elimination can still work
844 * on intrisics without understanding the meaning of each.
845 *
846 * Each intrinsic has some number of constant indices, some number of
847 * variables, and some number of sources. What these sources, variables,
848 * and indices mean depends on the intrinsic and is documented with the
849 * intrinsic declaration in nir_intrinsics.h. Intrinsics and texture
850 * instructions are the only types of instruction that can operate on
851 * variables.
852 */
853 typedef struct {
854 nir_instr instr;
855
856 nir_intrinsic_op intrinsic;
857
858 nir_dest dest;
859
860 /** number of components if this is a vectorized intrinsic
861 *
862 * Similarly to ALU operations, some intrinsics are vectorized.
863 * An intrinsic is vectorized if nir_intrinsic_infos.dest_components == 0.
864 * For vectorized intrinsics, the num_components field specifies the
865 * number of destination components and the number of source components
866 * for all sources with nir_intrinsic_infos.src_components[i] == 0.
867 */
868 uint8_t num_components;
869
870 int const_index[NIR_INTRINSIC_MAX_CONST_INDEX];
871
872 nir_deref_var *variables[2];
873
874 nir_src src[];
875 } nir_intrinsic_instr;
876
877 /**
878 * \name NIR intrinsics semantic flags
879 *
880 * information about what the compiler can do with the intrinsics.
881 *
882 * \sa nir_intrinsic_info::flags
883 */
884 typedef enum {
885 /**
886 * whether the intrinsic can be safely eliminated if none of its output
887 * value is not being used.
888 */
889 NIR_INTRINSIC_CAN_ELIMINATE = (1 << 0),
890
891 /**
892 * Whether the intrinsic can be reordered with respect to any other
893 * intrinsic, i.e. whether the only reordering dependencies of the
894 * intrinsic are due to the register reads/writes.
895 */
896 NIR_INTRINSIC_CAN_REORDER = (1 << 1),
897 } nir_intrinsic_semantic_flag;
898
899 /**
900 * \name NIR intrinsics const-index flag
901 *
902 * Indicates the usage of a const_index slot.
903 *
904 * \sa nir_intrinsic_info::index_map
905 */
906 typedef enum {
907 /**
908 * Generally instructions that take a offset src argument, can encode
909 * a constant 'base' value which is added to the offset.
910 */
911 NIR_INTRINSIC_BASE = 1,
912
913 /**
914 * For store instructions, a writemask for the store.
915 */
916 NIR_INTRINSIC_WRMASK = 2,
917
918 /**
919 * The stream-id for GS emit_vertex/end_primitive intrinsics.
920 */
921 NIR_INTRINSIC_STREAM_ID = 3,
922
923 /**
924 * The clip-plane id for load_user_clip_plane intrinsic.
925 */
926 NIR_INTRINSIC_UCP_ID = 4,
927
928 NIR_INTRINSIC_NUM_INDEX_FLAGS,
929
930 } nir_intrinsic_index_flag;
931
932 #define NIR_INTRINSIC_MAX_INPUTS 4
933
934 typedef struct {
935 const char *name;
936
937 unsigned num_srcs; /** < number of register/SSA inputs */
938
939 /** number of components of each input register
940 *
941 * If this value is 0, the number of components is given by the
942 * num_components field of nir_intrinsic_instr.
943 */
944 unsigned src_components[NIR_INTRINSIC_MAX_INPUTS];
945
946 bool has_dest;
947
948 /** number of components of the output register
949 *
950 * If this value is 0, the number of components is given by the
951 * num_components field of nir_intrinsic_instr.
952 */
953 unsigned dest_components;
954
955 /** the number of inputs/outputs that are variables */
956 unsigned num_variables;
957
958 /** the number of constant indices used by the intrinsic */
959 unsigned num_indices;
960
961 /** indicates the usage of intr->const_index[n] */
962 unsigned index_map[NIR_INTRINSIC_NUM_INDEX_FLAGS];
963
964 /** semantic flags for calls to this intrinsic */
965 nir_intrinsic_semantic_flag flags;
966 } nir_intrinsic_info;
967
968 extern const nir_intrinsic_info nir_intrinsic_infos[nir_num_intrinsics];
969
970
971 #define INTRINSIC_IDX_ACCESSORS(name, flag, type) \
972 static inline type \
973 nir_intrinsic_##name(nir_intrinsic_instr *instr) \
974 { \
975 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic]; \
976 assert(info->index_map[NIR_INTRINSIC_##flag] > 0); \
977 return instr->const_index[info->index_map[NIR_INTRINSIC_##flag] - 1]; \
978 } \
979 static inline void \
980 nir_intrinsic_set_##name(nir_intrinsic_instr *instr, type val) \
981 { \
982 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic]; \
983 assert(info->index_map[NIR_INTRINSIC_##flag] > 0); \
984 instr->const_index[info->index_map[NIR_INTRINSIC_##flag] - 1] = val; \
985 }
986
987 INTRINSIC_IDX_ACCESSORS(write_mask, WRMASK, unsigned)
988 INTRINSIC_IDX_ACCESSORS(base, BASE, int)
989 INTRINSIC_IDX_ACCESSORS(stream_id, STREAM_ID, unsigned)
990 INTRINSIC_IDX_ACCESSORS(ucp_id, UCP_ID, unsigned)
991
992 /**
993 * \group texture information
994 *
995 * This gives semantic information about textures which is useful to the
996 * frontend, the backend, and lowering passes, but not the optimizer.
997 */
998
999 typedef enum {
1000 nir_tex_src_coord,
1001 nir_tex_src_projector,
1002 nir_tex_src_comparitor, /* shadow comparitor */
1003 nir_tex_src_offset,
1004 nir_tex_src_bias,
1005 nir_tex_src_lod,
1006 nir_tex_src_ms_index, /* MSAA sample index */
1007 nir_tex_src_ddx,
1008 nir_tex_src_ddy,
1009 nir_tex_src_texture_offset, /* < dynamically uniform indirect offset */
1010 nir_tex_src_sampler_offset, /* < dynamically uniform indirect offset */
1011 nir_num_tex_src_types
1012 } nir_tex_src_type;
1013
1014 typedef struct {
1015 nir_src src;
1016 nir_tex_src_type src_type;
1017 } nir_tex_src;
1018
1019 typedef enum {
1020 nir_texop_tex, /**< Regular texture look-up */
1021 nir_texop_txb, /**< Texture look-up with LOD bias */
1022 nir_texop_txl, /**< Texture look-up with explicit LOD */
1023 nir_texop_txd, /**< Texture look-up with partial derivatvies */
1024 nir_texop_txf, /**< Texel fetch with explicit LOD */
1025 nir_texop_txf_ms, /**< Multisample texture fetch */
1026 nir_texop_txs, /**< Texture size */
1027 nir_texop_lod, /**< Texture lod query */
1028 nir_texop_tg4, /**< Texture gather */
1029 nir_texop_query_levels, /**< Texture levels query */
1030 nir_texop_texture_samples, /**< Texture samples query */
1031 nir_texop_samples_identical, /**< Query whether all samples are definitely
1032 * identical.
1033 */
1034 } nir_texop;
1035
1036 typedef struct {
1037 nir_instr instr;
1038
1039 enum glsl_sampler_dim sampler_dim;
1040 nir_alu_type dest_type;
1041
1042 nir_texop op;
1043 nir_dest dest;
1044 nir_tex_src *src;
1045 unsigned num_srcs, coord_components;
1046 bool is_array, is_shadow;
1047
1048 /**
1049 * If is_shadow is true, whether this is the old-style shadow that outputs 4
1050 * components or the new-style shadow that outputs 1 component.
1051 */
1052 bool is_new_style_shadow;
1053
1054 /* gather component selector */
1055 unsigned component : 2;
1056
1057 /** The texture index
1058 *
1059 * If this texture instruction has a nir_tex_src_texture_offset source,
1060 * then the texture index is given by texture_index + texture_offset.
1061 */
1062 unsigned texture_index;
1063
1064 /** The size of the texture array or 0 if it's not an array */
1065 unsigned texture_array_size;
1066
1067 /** The texture deref
1068 *
1069 * If this is null, use texture_index instead.
1070 */
1071 nir_deref_var *texture;
1072
1073 /** The sampler index
1074 *
1075 * The following operations do not require a sampler and, as such, this
1076 * field should be ignored:
1077 * - nir_texop_txf
1078 * - nir_texop_txf_ms
1079 * - nir_texop_txs
1080 * - nir_texop_lod
1081 * - nir_texop_tg4
1082 * - nir_texop_query_levels
1083 * - nir_texop_texture_samples
1084 * - nir_texop_samples_identical
1085 *
1086 * If this texture instruction has a nir_tex_src_sampler_offset source,
1087 * then the sampler index is given by sampler_index + sampler_offset.
1088 */
1089 unsigned sampler_index;
1090
1091 /** The sampler deref
1092 *
1093 * If this is null, use sampler_index instead.
1094 */
1095 nir_deref_var *sampler;
1096 } nir_tex_instr;
1097
1098 static inline unsigned
1099 nir_tex_instr_dest_size(nir_tex_instr *instr)
1100 {
1101 switch (instr->op) {
1102 case nir_texop_txs: {
1103 unsigned ret;
1104 switch (instr->sampler_dim) {
1105 case GLSL_SAMPLER_DIM_1D:
1106 case GLSL_SAMPLER_DIM_BUF:
1107 ret = 1;
1108 break;
1109 case GLSL_SAMPLER_DIM_2D:
1110 case GLSL_SAMPLER_DIM_CUBE:
1111 case GLSL_SAMPLER_DIM_MS:
1112 case GLSL_SAMPLER_DIM_RECT:
1113 case GLSL_SAMPLER_DIM_EXTERNAL:
1114 ret = 2;
1115 break;
1116 case GLSL_SAMPLER_DIM_3D:
1117 ret = 3;
1118 break;
1119 default:
1120 unreachable("not reached");
1121 }
1122 if (instr->is_array)
1123 ret++;
1124 return ret;
1125 }
1126
1127 case nir_texop_lod:
1128 return 2;
1129
1130 case nir_texop_texture_samples:
1131 case nir_texop_query_levels:
1132 case nir_texop_samples_identical:
1133 return 1;
1134
1135 default:
1136 if (instr->is_shadow && instr->is_new_style_shadow)
1137 return 1;
1138
1139 return 4;
1140 }
1141 }
1142
1143 /* Returns true if this texture operation queries something about the texture
1144 * rather than actually sampling it.
1145 */
1146 static inline bool
1147 nir_tex_instr_is_query(nir_tex_instr *instr)
1148 {
1149 switch (instr->op) {
1150 case nir_texop_txs:
1151 case nir_texop_lod:
1152 case nir_texop_texture_samples:
1153 case nir_texop_query_levels:
1154 return true;
1155 case nir_texop_tex:
1156 case nir_texop_txb:
1157 case nir_texop_txl:
1158 case nir_texop_txd:
1159 case nir_texop_txf:
1160 case nir_texop_txf_ms:
1161 case nir_texop_tg4:
1162 return false;
1163 default:
1164 unreachable("Invalid texture opcode");
1165 }
1166 }
1167
1168 static inline unsigned
1169 nir_tex_instr_src_size(nir_tex_instr *instr, unsigned src)
1170 {
1171 if (instr->src[src].src_type == nir_tex_src_coord)
1172 return instr->coord_components;
1173
1174
1175 if (instr->src[src].src_type == nir_tex_src_offset ||
1176 instr->src[src].src_type == nir_tex_src_ddx ||
1177 instr->src[src].src_type == nir_tex_src_ddy) {
1178 if (instr->is_array)
1179 return instr->coord_components - 1;
1180 else
1181 return instr->coord_components;
1182 }
1183
1184 return 1;
1185 }
1186
1187 static inline int
1188 nir_tex_instr_src_index(nir_tex_instr *instr, nir_tex_src_type type)
1189 {
1190 for (unsigned i = 0; i < instr->num_srcs; i++)
1191 if (instr->src[i].src_type == type)
1192 return (int) i;
1193
1194 return -1;
1195 }
1196
1197 typedef struct {
1198 union {
1199 float f[4];
1200 int32_t i[4];
1201 uint32_t u[4];
1202 };
1203 } nir_const_value;
1204
1205 typedef struct {
1206 nir_instr instr;
1207
1208 nir_const_value value;
1209
1210 nir_ssa_def def;
1211 } nir_load_const_instr;
1212
1213 typedef enum {
1214 nir_jump_return,
1215 nir_jump_break,
1216 nir_jump_continue,
1217 } nir_jump_type;
1218
1219 typedef struct {
1220 nir_instr instr;
1221 nir_jump_type type;
1222 } nir_jump_instr;
1223
1224 /* creates a new SSA variable in an undefined state */
1225
1226 typedef struct {
1227 nir_instr instr;
1228 nir_ssa_def def;
1229 } nir_ssa_undef_instr;
1230
1231 typedef struct {
1232 struct exec_node node;
1233
1234 /* The predecessor block corresponding to this source */
1235 struct nir_block *pred;
1236
1237 nir_src src;
1238 } nir_phi_src;
1239
1240 #define nir_foreach_phi_src(phi, entry) \
1241 foreach_list_typed(nir_phi_src, entry, node, &(phi)->srcs)
1242 #define nir_foreach_phi_src_safe(phi, entry) \
1243 foreach_list_typed_safe(nir_phi_src, entry, node, &(phi)->srcs)
1244
1245 typedef struct {
1246 nir_instr instr;
1247
1248 struct exec_list srcs; /** < list of nir_phi_src */
1249
1250 nir_dest dest;
1251 } nir_phi_instr;
1252
1253 typedef struct {
1254 struct exec_node node;
1255 nir_src src;
1256 nir_dest dest;
1257 } nir_parallel_copy_entry;
1258
1259 #define nir_foreach_parallel_copy_entry(pcopy, entry) \
1260 foreach_list_typed(nir_parallel_copy_entry, entry, node, &(pcopy)->entries)
1261
1262 typedef struct {
1263 nir_instr instr;
1264
1265 /* A list of nir_parallel_copy_entry's. The sources of all of the
1266 * entries are copied to the corresponding destinations "in parallel".
1267 * In other words, if we have two entries: a -> b and b -> a, the values
1268 * get swapped.
1269 */
1270 struct exec_list entries;
1271 } nir_parallel_copy_instr;
1272
1273 NIR_DEFINE_CAST(nir_instr_as_alu, nir_instr, nir_alu_instr, instr)
1274 NIR_DEFINE_CAST(nir_instr_as_call, nir_instr, nir_call_instr, instr)
1275 NIR_DEFINE_CAST(nir_instr_as_jump, nir_instr, nir_jump_instr, instr)
1276 NIR_DEFINE_CAST(nir_instr_as_tex, nir_instr, nir_tex_instr, instr)
1277 NIR_DEFINE_CAST(nir_instr_as_intrinsic, nir_instr, nir_intrinsic_instr, instr)
1278 NIR_DEFINE_CAST(nir_instr_as_load_const, nir_instr, nir_load_const_instr, instr)
1279 NIR_DEFINE_CAST(nir_instr_as_ssa_undef, nir_instr, nir_ssa_undef_instr, instr)
1280 NIR_DEFINE_CAST(nir_instr_as_phi, nir_instr, nir_phi_instr, instr)
1281 NIR_DEFINE_CAST(nir_instr_as_parallel_copy, nir_instr,
1282 nir_parallel_copy_instr, instr)
1283
1284 /*
1285 * Control flow
1286 *
1287 * Control flow consists of a tree of control flow nodes, which include
1288 * if-statements and loops. The leaves of the tree are basic blocks, lists of
1289 * instructions that always run start-to-finish. Each basic block also keeps
1290 * track of its successors (blocks which may run immediately after the current
1291 * block) and predecessors (blocks which could have run immediately before the
1292 * current block). Each function also has a start block and an end block which
1293 * all return statements point to (which is always empty). Together, all the
1294 * blocks with their predecessors and successors make up the control flow
1295 * graph (CFG) of the function. There are helpers that modify the tree of
1296 * control flow nodes while modifying the CFG appropriately; these should be
1297 * used instead of modifying the tree directly.
1298 */
1299
1300 typedef enum {
1301 nir_cf_node_block,
1302 nir_cf_node_if,
1303 nir_cf_node_loop,
1304 nir_cf_node_function
1305 } nir_cf_node_type;
1306
1307 typedef struct nir_cf_node {
1308 struct exec_node node;
1309 nir_cf_node_type type;
1310 struct nir_cf_node *parent;
1311 } nir_cf_node;
1312
1313 typedef struct nir_block {
1314 nir_cf_node cf_node;
1315
1316 struct exec_list instr_list; /** < list of nir_instr */
1317
1318 /** generic block index; generated by nir_index_blocks */
1319 unsigned index;
1320
1321 /*
1322 * Each block can only have up to 2 successors, so we put them in a simple
1323 * array - no need for anything more complicated.
1324 */
1325 struct nir_block *successors[2];
1326
1327 /* Set of nir_block predecessors in the CFG */
1328 struct set *predecessors;
1329
1330 /*
1331 * this node's immediate dominator in the dominance tree - set to NULL for
1332 * the start block.
1333 */
1334 struct nir_block *imm_dom;
1335
1336 /* This node's children in the dominance tree */
1337 unsigned num_dom_children;
1338 struct nir_block **dom_children;
1339
1340 /* Set of nir_block's on the dominance frontier of this block */
1341 struct set *dom_frontier;
1342
1343 /*
1344 * These two indices have the property that dom_{pre,post}_index for each
1345 * child of this block in the dominance tree will always be between
1346 * dom_pre_index and dom_post_index for this block, which makes testing if
1347 * a given block is dominated by another block an O(1) operation.
1348 */
1349 unsigned dom_pre_index, dom_post_index;
1350
1351 /* live in and out for this block; used for liveness analysis */
1352 BITSET_WORD *live_in;
1353 BITSET_WORD *live_out;
1354 } nir_block;
1355
1356 static inline nir_instr *
1357 nir_block_first_instr(nir_block *block)
1358 {
1359 struct exec_node *head = exec_list_get_head(&block->instr_list);
1360 return exec_node_data(nir_instr, head, node);
1361 }
1362
1363 static inline nir_instr *
1364 nir_block_last_instr(nir_block *block)
1365 {
1366 struct exec_node *tail = exec_list_get_tail(&block->instr_list);
1367 return exec_node_data(nir_instr, tail, node);
1368 }
1369
1370 #define nir_foreach_instr(block, instr) \
1371 foreach_list_typed(nir_instr, instr, node, &(block)->instr_list)
1372 #define nir_foreach_instr_reverse(block, instr) \
1373 foreach_list_typed_reverse(nir_instr, instr, node, &(block)->instr_list)
1374 #define nir_foreach_instr_safe(block, instr) \
1375 foreach_list_typed_safe(nir_instr, instr, node, &(block)->instr_list)
1376 #define nir_foreach_instr_reverse_safe(block, instr) \
1377 foreach_list_typed_reverse_safe(nir_instr, instr, node, &(block)->instr_list)
1378
1379 typedef struct nir_if {
1380 nir_cf_node cf_node;
1381 nir_src condition;
1382
1383 struct exec_list then_list; /** < list of nir_cf_node */
1384 struct exec_list else_list; /** < list of nir_cf_node */
1385 } nir_if;
1386
1387 static inline nir_cf_node *
1388 nir_if_first_then_node(nir_if *if_stmt)
1389 {
1390 struct exec_node *head = exec_list_get_head(&if_stmt->then_list);
1391 return exec_node_data(nir_cf_node, head, node);
1392 }
1393
1394 static inline nir_cf_node *
1395 nir_if_last_then_node(nir_if *if_stmt)
1396 {
1397 struct exec_node *tail = exec_list_get_tail(&if_stmt->then_list);
1398 return exec_node_data(nir_cf_node, tail, node);
1399 }
1400
1401 static inline nir_cf_node *
1402 nir_if_first_else_node(nir_if *if_stmt)
1403 {
1404 struct exec_node *head = exec_list_get_head(&if_stmt->else_list);
1405 return exec_node_data(nir_cf_node, head, node);
1406 }
1407
1408 static inline nir_cf_node *
1409 nir_if_last_else_node(nir_if *if_stmt)
1410 {
1411 struct exec_node *tail = exec_list_get_tail(&if_stmt->else_list);
1412 return exec_node_data(nir_cf_node, tail, node);
1413 }
1414
1415 typedef struct {
1416 nir_cf_node cf_node;
1417
1418 struct exec_list body; /** < list of nir_cf_node */
1419 } nir_loop;
1420
1421 static inline nir_cf_node *
1422 nir_loop_first_cf_node(nir_loop *loop)
1423 {
1424 return exec_node_data(nir_cf_node, exec_list_get_head(&loop->body), node);
1425 }
1426
1427 static inline nir_cf_node *
1428 nir_loop_last_cf_node(nir_loop *loop)
1429 {
1430 return exec_node_data(nir_cf_node, exec_list_get_tail(&loop->body), node);
1431 }
1432
1433 /**
1434 * Various bits of metadata that can may be created or required by
1435 * optimization and analysis passes
1436 */
1437 typedef enum {
1438 nir_metadata_none = 0x0,
1439 nir_metadata_block_index = 0x1,
1440 nir_metadata_dominance = 0x2,
1441 nir_metadata_live_ssa_defs = 0x4,
1442 nir_metadata_not_properly_reset = 0x8,
1443 } nir_metadata;
1444
1445 typedef struct {
1446 nir_cf_node cf_node;
1447
1448 /** pointer to the function of which this is an implementation */
1449 struct nir_function *function;
1450
1451 struct exec_list body; /** < list of nir_cf_node */
1452
1453 nir_block *end_block;
1454
1455 /** list for all local variables in the function */
1456 struct exec_list locals;
1457
1458 /** array of variables used as parameters */
1459 unsigned num_params;
1460 nir_variable **params;
1461
1462 /** variable used to hold the result of the function */
1463 nir_variable *return_var;
1464
1465 /** list of local registers in the function */
1466 struct exec_list registers;
1467
1468 /** next available local register index */
1469 unsigned reg_alloc;
1470
1471 /** next available SSA value index */
1472 unsigned ssa_alloc;
1473
1474 /* total number of basic blocks, only valid when block_index_dirty = false */
1475 unsigned num_blocks;
1476
1477 nir_metadata valid_metadata;
1478 } nir_function_impl;
1479
1480 static inline nir_block *
1481 nir_start_block(nir_function_impl *impl)
1482 {
1483 return (nir_block *) exec_list_get_head(&impl->body);
1484 }
1485
1486 static inline nir_cf_node *
1487 nir_cf_node_next(nir_cf_node *node)
1488 {
1489 struct exec_node *next = exec_node_get_next(&node->node);
1490 if (exec_node_is_tail_sentinel(next))
1491 return NULL;
1492 else
1493 return exec_node_data(nir_cf_node, next, node);
1494 }
1495
1496 static inline nir_cf_node *
1497 nir_cf_node_prev(nir_cf_node *node)
1498 {
1499 struct exec_node *prev = exec_node_get_prev(&node->node);
1500 if (exec_node_is_head_sentinel(prev))
1501 return NULL;
1502 else
1503 return exec_node_data(nir_cf_node, prev, node);
1504 }
1505
1506 static inline bool
1507 nir_cf_node_is_first(const nir_cf_node *node)
1508 {
1509 return exec_node_is_head_sentinel(node->node.prev);
1510 }
1511
1512 static inline bool
1513 nir_cf_node_is_last(const nir_cf_node *node)
1514 {
1515 return exec_node_is_tail_sentinel(node->node.next);
1516 }
1517
1518 NIR_DEFINE_CAST(nir_cf_node_as_block, nir_cf_node, nir_block, cf_node)
1519 NIR_DEFINE_CAST(nir_cf_node_as_if, nir_cf_node, nir_if, cf_node)
1520 NIR_DEFINE_CAST(nir_cf_node_as_loop, nir_cf_node, nir_loop, cf_node)
1521 NIR_DEFINE_CAST(nir_cf_node_as_function, nir_cf_node, nir_function_impl, cf_node)
1522
1523 typedef enum {
1524 nir_parameter_in,
1525 nir_parameter_out,
1526 nir_parameter_inout,
1527 } nir_parameter_type;
1528
1529 typedef struct {
1530 nir_parameter_type param_type;
1531 const struct glsl_type *type;
1532 } nir_parameter;
1533
1534 typedef struct nir_function {
1535 struct exec_node node;
1536
1537 const char *name;
1538 struct nir_shader *shader;
1539
1540 unsigned num_params;
1541 nir_parameter *params;
1542 const struct glsl_type *return_type;
1543
1544 /** The implementation of this function.
1545 *
1546 * If the function is only declared and not implemented, this is NULL.
1547 */
1548 nir_function_impl *impl;
1549 } nir_function;
1550
1551 typedef struct nir_shader_compiler_options {
1552 bool lower_fdiv;
1553 bool lower_ffma;
1554 bool lower_flrp;
1555 bool lower_fpow;
1556 bool lower_fsat;
1557 bool lower_fsqrt;
1558 bool lower_fmod;
1559 bool lower_bitfield_extract;
1560 bool lower_bitfield_insert;
1561 bool lower_uadd_carry;
1562 bool lower_usub_borrow;
1563 /** lowers fneg and ineg to fsub and isub. */
1564 bool lower_negate;
1565 /** lowers fsub and isub to fadd+fneg and iadd+ineg. */
1566 bool lower_sub;
1567
1568 /* lower {slt,sge,seq,sne} to {flt,fge,feq,fne} + b2f: */
1569 bool lower_scmp;
1570
1571 /* Does the native fdot instruction replicate its result for four
1572 * components? If so, then opt_algebraic_late will turn all fdotN
1573 * instructions into fdot_replicatedN instructions.
1574 */
1575 bool fdot_replicates;
1576
1577 /** lowers ffract to fsub+ffloor: */
1578 bool lower_ffract;
1579
1580 bool lower_pack_half_2x16;
1581 bool lower_pack_unorm_2x16;
1582 bool lower_pack_snorm_2x16;
1583 bool lower_pack_unorm_4x8;
1584 bool lower_pack_snorm_4x8;
1585 bool lower_unpack_half_2x16;
1586 bool lower_unpack_unorm_2x16;
1587 bool lower_unpack_snorm_2x16;
1588 bool lower_unpack_unorm_4x8;
1589 bool lower_unpack_snorm_4x8;
1590
1591 bool lower_extract_byte;
1592 bool lower_extract_word;
1593
1594 /**
1595 * Does the driver support real 32-bit integers? (Otherwise, integers
1596 * are simulated by floats.)
1597 */
1598 bool native_integers;
1599 } nir_shader_compiler_options;
1600
1601 typedef struct nir_shader_info {
1602 const char *name;
1603
1604 /* Descriptive name provided by the client; may be NULL */
1605 const char *label;
1606
1607 /* Number of textures used by this shader */
1608 unsigned num_textures;
1609 /* Number of uniform buffers used by this shader */
1610 unsigned num_ubos;
1611 /* Number of atomic buffers used by this shader */
1612 unsigned num_abos;
1613 /* Number of shader storage buffers used by this shader */
1614 unsigned num_ssbos;
1615 /* Number of images used by this shader */
1616 unsigned num_images;
1617
1618 /* Which inputs are actually read */
1619 uint64_t inputs_read;
1620 /* Which outputs are actually written */
1621 uint64_t outputs_written;
1622 /* Which system values are actually read */
1623 uint64_t system_values_read;
1624
1625 /* Which patch inputs are actually read */
1626 uint32_t patch_inputs_read;
1627 /* Which patch outputs are actually written */
1628 uint32_t patch_outputs_written;
1629
1630 /* Whether or not this shader ever uses textureGather() */
1631 bool uses_texture_gather;
1632
1633 /* Whether or not this shader uses the gl_ClipDistance output */
1634 bool uses_clip_distance_out;
1635
1636 /* Whether or not separate shader objects were used */
1637 bool separate_shader;
1638
1639 /** Was this shader linked with any transform feedback varyings? */
1640 bool has_transform_feedback_varyings;
1641
1642 union {
1643 struct {
1644 /** The number of vertices recieves per input primitive */
1645 unsigned vertices_in;
1646
1647 /** The output primitive type (GL enum value) */
1648 unsigned output_primitive;
1649
1650 /** The maximum number of vertices the geometry shader might write. */
1651 unsigned vertices_out;
1652
1653 /** 1 .. MAX_GEOMETRY_SHADER_INVOCATIONS */
1654 unsigned invocations;
1655
1656 /** Whether or not this shader uses EndPrimitive */
1657 bool uses_end_primitive;
1658
1659 /** Whether or not this shader uses non-zero streams */
1660 bool uses_streams;
1661 } gs;
1662
1663 struct {
1664 bool uses_discard;
1665
1666 /**
1667 * Whether early fragment tests are enabled as defined by
1668 * ARB_shader_image_load_store.
1669 */
1670 bool early_fragment_tests;
1671
1672 /** gl_FragDepth layout for ARB_conservative_depth. */
1673 enum gl_frag_depth_layout depth_layout;
1674 } fs;
1675
1676 struct {
1677 unsigned local_size[3];
1678 } cs;
1679
1680 struct {
1681 /** The number of vertices in the TCS output patch. */
1682 unsigned vertices_out;
1683 } tcs;
1684 };
1685 } nir_shader_info;
1686
1687 typedef struct nir_shader {
1688 /** list of uniforms (nir_variable) */
1689 struct exec_list uniforms;
1690
1691 /** list of inputs (nir_variable) */
1692 struct exec_list inputs;
1693
1694 /** list of outputs (nir_variable) */
1695 struct exec_list outputs;
1696
1697 /** list of shared compute variables (nir_variable) */
1698 struct exec_list shared;
1699
1700 /** Set of driver-specific options for the shader.
1701 *
1702 * The memory for the options is expected to be kept in a single static
1703 * copy by the driver.
1704 */
1705 const struct nir_shader_compiler_options *options;
1706
1707 /** Various bits of compile-time information about a given shader */
1708 struct nir_shader_info info;
1709
1710 /** list of global variables in the shader (nir_variable) */
1711 struct exec_list globals;
1712
1713 /** list of system value variables in the shader (nir_variable) */
1714 struct exec_list system_values;
1715
1716 struct exec_list functions; /** < list of nir_function */
1717
1718 /** list of global register in the shader */
1719 struct exec_list registers;
1720
1721 /** next available global register index */
1722 unsigned reg_alloc;
1723
1724 /**
1725 * the highest index a load_input_*, load_uniform_*, etc. intrinsic can
1726 * access plus one
1727 */
1728 unsigned num_inputs, num_uniforms, num_outputs, num_shared;
1729
1730 /** The shader stage, such as MESA_SHADER_VERTEX. */
1731 gl_shader_stage stage;
1732 } nir_shader;
1733
1734 #define nir_foreach_function(shader, func) \
1735 foreach_list_typed(nir_function, func, node, &(shader)->functions)
1736
1737 nir_shader *nir_shader_create(void *mem_ctx,
1738 gl_shader_stage stage,
1739 const nir_shader_compiler_options *options);
1740
1741 /** creates a register, including assigning it an index and adding it to the list */
1742 nir_register *nir_global_reg_create(nir_shader *shader);
1743
1744 nir_register *nir_local_reg_create(nir_function_impl *impl);
1745
1746 void nir_reg_remove(nir_register *reg);
1747
1748 /** Adds a variable to the appropreate list in nir_shader */
1749 void nir_shader_add_variable(nir_shader *shader, nir_variable *var);
1750
1751 static inline void
1752 nir_function_impl_add_variable(nir_function_impl *impl, nir_variable *var)
1753 {
1754 assert(var->data.mode == nir_var_local);
1755 exec_list_push_tail(&impl->locals, &var->node);
1756 }
1757
1758 /** creates a variable, sets a few defaults, and adds it to the list */
1759 nir_variable *nir_variable_create(nir_shader *shader,
1760 nir_variable_mode mode,
1761 const struct glsl_type *type,
1762 const char *name);
1763 /** creates a local variable and adds it to the list */
1764 nir_variable *nir_local_variable_create(nir_function_impl *impl,
1765 const struct glsl_type *type,
1766 const char *name);
1767
1768 /** creates a function and adds it to the shader's list of functions */
1769 nir_function *nir_function_create(nir_shader *shader, const char *name);
1770
1771 nir_function_impl *nir_function_impl_create(nir_function *func);
1772 /** creates a function_impl that isn't tied to any particular function */
1773 nir_function_impl *nir_function_impl_create_bare(nir_shader *shader);
1774
1775 nir_block *nir_block_create(nir_shader *shader);
1776 nir_if *nir_if_create(nir_shader *shader);
1777 nir_loop *nir_loop_create(nir_shader *shader);
1778
1779 nir_function_impl *nir_cf_node_get_function(nir_cf_node *node);
1780
1781 /** requests that the given pieces of metadata be generated */
1782 void nir_metadata_require(nir_function_impl *impl, nir_metadata required);
1783 /** dirties all but the preserved metadata */
1784 void nir_metadata_preserve(nir_function_impl *impl, nir_metadata preserved);
1785
1786 /** creates an instruction with default swizzle/writemask/etc. with NULL registers */
1787 nir_alu_instr *nir_alu_instr_create(nir_shader *shader, nir_op op);
1788
1789 nir_jump_instr *nir_jump_instr_create(nir_shader *shader, nir_jump_type type);
1790
1791 nir_load_const_instr *nir_load_const_instr_create(nir_shader *shader,
1792 unsigned num_components);
1793
1794 nir_intrinsic_instr *nir_intrinsic_instr_create(nir_shader *shader,
1795 nir_intrinsic_op op);
1796
1797 nir_call_instr *nir_call_instr_create(nir_shader *shader,
1798 nir_function *callee);
1799
1800 nir_tex_instr *nir_tex_instr_create(nir_shader *shader, unsigned num_srcs);
1801
1802 nir_phi_instr *nir_phi_instr_create(nir_shader *shader);
1803
1804 nir_parallel_copy_instr *nir_parallel_copy_instr_create(nir_shader *shader);
1805
1806 nir_ssa_undef_instr *nir_ssa_undef_instr_create(nir_shader *shader,
1807 unsigned num_components);
1808
1809 nir_deref_var *nir_deref_var_create(void *mem_ctx, nir_variable *var);
1810 nir_deref_array *nir_deref_array_create(void *mem_ctx);
1811 nir_deref_struct *nir_deref_struct_create(void *mem_ctx, unsigned field_index);
1812
1813 nir_deref *nir_copy_deref(void *mem_ctx, nir_deref *deref);
1814
1815 nir_load_const_instr *
1816 nir_deref_get_const_initializer_load(nir_shader *shader, nir_deref_var *deref);
1817
1818 /**
1819 * NIR Cursors and Instruction Insertion API
1820 * @{
1821 *
1822 * A tiny struct representing a point to insert/extract instructions or
1823 * control flow nodes. Helps reduce the combinatorial explosion of possible
1824 * points to insert/extract.
1825 *
1826 * \sa nir_control_flow.h
1827 */
1828 typedef enum {
1829 nir_cursor_before_block,
1830 nir_cursor_after_block,
1831 nir_cursor_before_instr,
1832 nir_cursor_after_instr,
1833 } nir_cursor_option;
1834
1835 typedef struct {
1836 nir_cursor_option option;
1837 union {
1838 nir_block *block;
1839 nir_instr *instr;
1840 };
1841 } nir_cursor;
1842
1843 static inline nir_cursor
1844 nir_before_block(nir_block *block)
1845 {
1846 nir_cursor cursor;
1847 cursor.option = nir_cursor_before_block;
1848 cursor.block = block;
1849 return cursor;
1850 }
1851
1852 static inline nir_cursor
1853 nir_after_block(nir_block *block)
1854 {
1855 nir_cursor cursor;
1856 cursor.option = nir_cursor_after_block;
1857 cursor.block = block;
1858 return cursor;
1859 }
1860
1861 static inline nir_cursor
1862 nir_before_instr(nir_instr *instr)
1863 {
1864 nir_cursor cursor;
1865 cursor.option = nir_cursor_before_instr;
1866 cursor.instr = instr;
1867 return cursor;
1868 }
1869
1870 static inline nir_cursor
1871 nir_after_instr(nir_instr *instr)
1872 {
1873 nir_cursor cursor;
1874 cursor.option = nir_cursor_after_instr;
1875 cursor.instr = instr;
1876 return cursor;
1877 }
1878
1879 static inline nir_cursor
1880 nir_after_block_before_jump(nir_block *block)
1881 {
1882 nir_instr *last_instr = nir_block_last_instr(block);
1883 if (last_instr && last_instr->type == nir_instr_type_jump) {
1884 return nir_before_instr(last_instr);
1885 } else {
1886 return nir_after_block(block);
1887 }
1888 }
1889
1890 static inline nir_cursor
1891 nir_before_cf_node(nir_cf_node *node)
1892 {
1893 if (node->type == nir_cf_node_block)
1894 return nir_before_block(nir_cf_node_as_block(node));
1895
1896 return nir_after_block(nir_cf_node_as_block(nir_cf_node_prev(node)));
1897 }
1898
1899 static inline nir_cursor
1900 nir_after_cf_node(nir_cf_node *node)
1901 {
1902 if (node->type == nir_cf_node_block)
1903 return nir_after_block(nir_cf_node_as_block(node));
1904
1905 return nir_before_block(nir_cf_node_as_block(nir_cf_node_next(node)));
1906 }
1907
1908 static inline nir_cursor
1909 nir_before_cf_list(struct exec_list *cf_list)
1910 {
1911 nir_cf_node *first_node = exec_node_data(nir_cf_node,
1912 exec_list_get_head(cf_list), node);
1913 return nir_before_cf_node(first_node);
1914 }
1915
1916 static inline nir_cursor
1917 nir_after_cf_list(struct exec_list *cf_list)
1918 {
1919 nir_cf_node *last_node = exec_node_data(nir_cf_node,
1920 exec_list_get_tail(cf_list), node);
1921 return nir_after_cf_node(last_node);
1922 }
1923
1924 /**
1925 * Insert a NIR instruction at the given cursor.
1926 *
1927 * Note: This does not update the cursor.
1928 */
1929 void nir_instr_insert(nir_cursor cursor, nir_instr *instr);
1930
1931 static inline void
1932 nir_instr_insert_before(nir_instr *instr, nir_instr *before)
1933 {
1934 nir_instr_insert(nir_before_instr(instr), before);
1935 }
1936
1937 static inline void
1938 nir_instr_insert_after(nir_instr *instr, nir_instr *after)
1939 {
1940 nir_instr_insert(nir_after_instr(instr), after);
1941 }
1942
1943 static inline void
1944 nir_instr_insert_before_block(nir_block *block, nir_instr *before)
1945 {
1946 nir_instr_insert(nir_before_block(block), before);
1947 }
1948
1949 static inline void
1950 nir_instr_insert_after_block(nir_block *block, nir_instr *after)
1951 {
1952 nir_instr_insert(nir_after_block(block), after);
1953 }
1954
1955 static inline void
1956 nir_instr_insert_before_cf(nir_cf_node *node, nir_instr *before)
1957 {
1958 nir_instr_insert(nir_before_cf_node(node), before);
1959 }
1960
1961 static inline void
1962 nir_instr_insert_after_cf(nir_cf_node *node, nir_instr *after)
1963 {
1964 nir_instr_insert(nir_after_cf_node(node), after);
1965 }
1966
1967 static inline void
1968 nir_instr_insert_before_cf_list(struct exec_list *list, nir_instr *before)
1969 {
1970 nir_instr_insert(nir_before_cf_list(list), before);
1971 }
1972
1973 static inline void
1974 nir_instr_insert_after_cf_list(struct exec_list *list, nir_instr *after)
1975 {
1976 nir_instr_insert(nir_after_cf_list(list), after);
1977 }
1978
1979 void nir_instr_remove(nir_instr *instr);
1980
1981 /** @} */
1982
1983 typedef bool (*nir_foreach_ssa_def_cb)(nir_ssa_def *def, void *state);
1984 typedef bool (*nir_foreach_dest_cb)(nir_dest *dest, void *state);
1985 typedef bool (*nir_foreach_src_cb)(nir_src *src, void *state);
1986 bool nir_foreach_ssa_def(nir_instr *instr, nir_foreach_ssa_def_cb cb,
1987 void *state);
1988 bool nir_foreach_dest(nir_instr *instr, nir_foreach_dest_cb cb, void *state);
1989 bool nir_foreach_src(nir_instr *instr, nir_foreach_src_cb cb, void *state);
1990
1991 nir_const_value *nir_src_as_const_value(nir_src src);
1992 bool nir_src_is_dynamically_uniform(nir_src src);
1993 bool nir_srcs_equal(nir_src src1, nir_src src2);
1994 void nir_instr_rewrite_src(nir_instr *instr, nir_src *src, nir_src new_src);
1995 void nir_instr_move_src(nir_instr *dest_instr, nir_src *dest, nir_src *src);
1996 void nir_if_rewrite_condition(nir_if *if_stmt, nir_src new_src);
1997 void nir_instr_rewrite_dest(nir_instr *instr, nir_dest *dest,
1998 nir_dest new_dest);
1999
2000 void nir_ssa_dest_init(nir_instr *instr, nir_dest *dest,
2001 unsigned num_components, const char *name);
2002 void nir_ssa_def_init(nir_instr *instr, nir_ssa_def *def,
2003 unsigned num_components, const char *name);
2004 void nir_ssa_def_rewrite_uses(nir_ssa_def *def, nir_src new_src);
2005 void nir_ssa_def_rewrite_uses_after(nir_ssa_def *def, nir_src new_src,
2006 nir_instr *after_me);
2007
2008 /* visits basic blocks in source-code order */
2009 typedef bool (*nir_foreach_block_cb)(nir_block *block, void *state);
2010 bool nir_foreach_block(nir_function_impl *impl, nir_foreach_block_cb cb,
2011 void *state);
2012 bool nir_foreach_block_reverse(nir_function_impl *impl, nir_foreach_block_cb cb,
2013 void *state);
2014 bool nir_foreach_block_in_cf_node(nir_cf_node *node, nir_foreach_block_cb cb,
2015 void *state);
2016
2017 /* If the following CF node is an if, this function returns that if.
2018 * Otherwise, it returns NULL.
2019 */
2020 nir_if *nir_block_get_following_if(nir_block *block);
2021
2022 nir_loop *nir_block_get_following_loop(nir_block *block);
2023
2024 void nir_index_local_regs(nir_function_impl *impl);
2025 void nir_index_global_regs(nir_shader *shader);
2026 void nir_index_ssa_defs(nir_function_impl *impl);
2027 unsigned nir_index_instrs(nir_function_impl *impl);
2028
2029 void nir_index_blocks(nir_function_impl *impl);
2030
2031 void nir_print_shader(nir_shader *shader, FILE *fp);
2032 void nir_print_instr(const nir_instr *instr, FILE *fp);
2033
2034 nir_shader * nir_shader_clone(void *mem_ctx, const nir_shader *s);
2035 nir_function_impl *nir_function_impl_clone(const nir_function_impl *fi);
2036
2037 #ifdef DEBUG
2038 void nir_validate_shader(nir_shader *shader);
2039 void nir_metadata_set_validation_flag(nir_shader *shader);
2040 void nir_metadata_check_validation_flag(nir_shader *shader);
2041
2042 #include "util/debug.h"
2043 static inline bool
2044 should_clone_nir(void)
2045 {
2046 static int should_clone = -1;
2047 if (should_clone < 0)
2048 should_clone = env_var_as_boolean("NIR_TEST_CLONE", false);
2049
2050 return should_clone;
2051 }
2052 #else
2053 static inline void nir_validate_shader(nir_shader *shader) { (void) shader; }
2054 static inline void nir_metadata_set_validation_flag(nir_shader *shader) { (void) shader; }
2055 static inline void nir_metadata_check_validation_flag(nir_shader *shader) { (void) shader; }
2056 static inline bool should_clone_nir(void) { return false; }
2057 #endif /* DEBUG */
2058
2059 #define _PASS(nir, do_pass) do { \
2060 do_pass \
2061 nir_validate_shader(nir); \
2062 if (should_clone_nir()) { \
2063 nir_shader *clone = nir_shader_clone(ralloc_parent(nir), nir); \
2064 ralloc_free(nir); \
2065 nir = clone; \
2066 } \
2067 } while (0)
2068
2069 #define NIR_PASS(progress, nir, pass, ...) _PASS(nir, \
2070 nir_metadata_set_validation_flag(nir); \
2071 if (pass(nir, ##__VA_ARGS__)) { \
2072 progress = true; \
2073 nir_metadata_check_validation_flag(nir); \
2074 } \
2075 )
2076
2077 #define NIR_PASS_V(nir, pass, ...) _PASS(nir, \
2078 pass(nir, ##__VA_ARGS__); \
2079 )
2080
2081 void nir_calc_dominance_impl(nir_function_impl *impl);
2082 void nir_calc_dominance(nir_shader *shader);
2083
2084 nir_block *nir_dominance_lca(nir_block *b1, nir_block *b2);
2085 bool nir_block_dominates(nir_block *parent, nir_block *child);
2086
2087 void nir_dump_dom_tree_impl(nir_function_impl *impl, FILE *fp);
2088 void nir_dump_dom_tree(nir_shader *shader, FILE *fp);
2089
2090 void nir_dump_dom_frontier_impl(nir_function_impl *impl, FILE *fp);
2091 void nir_dump_dom_frontier(nir_shader *shader, FILE *fp);
2092
2093 void nir_dump_cfg_impl(nir_function_impl *impl, FILE *fp);
2094 void nir_dump_cfg(nir_shader *shader, FILE *fp);
2095
2096 int nir_gs_count_vertices(const nir_shader *shader);
2097
2098 bool nir_split_var_copies(nir_shader *shader);
2099
2100 void nir_lower_var_copy_instr(nir_intrinsic_instr *copy, void *mem_ctx);
2101 void nir_lower_var_copies(nir_shader *shader);
2102
2103 bool nir_lower_global_vars_to_local(nir_shader *shader);
2104
2105 bool nir_lower_indirect_derefs(nir_shader *shader, uint32_t mode_mask);
2106
2107 bool nir_lower_locals_to_regs(nir_shader *shader);
2108
2109 void nir_lower_outputs_to_temporaries(nir_shader *shader);
2110
2111 void nir_assign_var_locations(struct exec_list *var_list,
2112 unsigned *size,
2113 int (*type_size)(const struct glsl_type *));
2114
2115 void nir_lower_io(nir_shader *shader,
2116 nir_variable_mode mode,
2117 int (*type_size)(const struct glsl_type *));
2118 nir_src *nir_get_io_offset_src(nir_intrinsic_instr *instr);
2119 nir_src *nir_get_io_vertex_index_src(nir_intrinsic_instr *instr);
2120
2121 void nir_lower_vars_to_ssa(nir_shader *shader);
2122
2123 bool nir_remove_dead_variables(nir_shader *shader);
2124
2125 void nir_move_vec_src_uses_to_dest(nir_shader *shader);
2126 bool nir_lower_vec_to_movs(nir_shader *shader);
2127 void nir_lower_alu_to_scalar(nir_shader *shader);
2128 void nir_lower_load_const_to_scalar(nir_shader *shader);
2129
2130 void nir_lower_phis_to_scalar(nir_shader *shader);
2131
2132 void nir_lower_samplers(nir_shader *shader,
2133 const struct gl_shader_program *shader_program);
2134
2135 bool nir_lower_system_values(nir_shader *shader);
2136
2137 typedef struct nir_lower_tex_options {
2138 /**
2139 * bitmask of (1 << GLSL_SAMPLER_DIM_x) to control for which
2140 * sampler types a texture projector is lowered.
2141 */
2142 unsigned lower_txp;
2143
2144 /**
2145 * If true, lower rect textures to 2D, using txs to fetch the
2146 * texture dimensions and dividing the texture coords by the
2147 * texture dims to normalize.
2148 */
2149 bool lower_rect;
2150
2151 /**
2152 * To emulate certain texture wrap modes, this can be used
2153 * to saturate the specified tex coord to [0.0, 1.0]. The
2154 * bits are according to sampler #, ie. if, for example:
2155 *
2156 * (conf->saturate_s & (1 << n))
2157 *
2158 * is true, then the s coord for sampler n is saturated.
2159 *
2160 * Note that clamping must happen *after* projector lowering
2161 * so any projected texture sample instruction with a clamped
2162 * coordinate gets automatically lowered, regardless of the
2163 * 'lower_txp' setting.
2164 */
2165 unsigned saturate_s;
2166 unsigned saturate_t;
2167 unsigned saturate_r;
2168
2169 /* Bitmask of textures that need swizzling.
2170 *
2171 * If (swizzle_result & (1 << texture_index)), then the swizzle in
2172 * swizzles[texture_index] is applied to the result of the texturing
2173 * operation.
2174 */
2175 unsigned swizzle_result;
2176
2177 /* A swizzle for each texture. Values 0-3 represent x, y, z, or w swizzles
2178 * while 4 and 5 represent 0 and 1 respectively.
2179 */
2180 uint8_t swizzles[32][4];
2181 } nir_lower_tex_options;
2182
2183 bool nir_lower_tex(nir_shader *shader,
2184 const nir_lower_tex_options *options);
2185
2186 void nir_lower_idiv(nir_shader *shader);
2187
2188 void nir_lower_clip_vs(nir_shader *shader, unsigned ucp_enables);
2189 void nir_lower_clip_fs(nir_shader *shader, unsigned ucp_enables);
2190
2191 void nir_lower_two_sided_color(nir_shader *shader);
2192
2193 void nir_lower_atomics(nir_shader *shader,
2194 const struct gl_shader_program *shader_program);
2195 void nir_lower_to_source_mods(nir_shader *shader);
2196
2197 bool nir_lower_gs_intrinsics(nir_shader *shader);
2198
2199 bool nir_normalize_cubemap_coords(nir_shader *shader);
2200
2201 void nir_live_ssa_defs_impl(nir_function_impl *impl);
2202 bool nir_ssa_defs_interfere(nir_ssa_def *a, nir_ssa_def *b);
2203
2204 void nir_convert_to_ssa_impl(nir_function_impl *impl);
2205 void nir_convert_to_ssa(nir_shader *shader);
2206
2207 /* If phi_webs_only is true, only convert SSA values involved in phi nodes to
2208 * registers. If false, convert all values (even those not involved in a phi
2209 * node) to registers.
2210 */
2211 void nir_convert_from_ssa(nir_shader *shader, bool phi_webs_only);
2212
2213 bool nir_opt_algebraic(nir_shader *shader);
2214 bool nir_opt_algebraic_late(nir_shader *shader);
2215 bool nir_opt_constant_folding(nir_shader *shader);
2216
2217 bool nir_opt_global_to_local(nir_shader *shader);
2218
2219 bool nir_copy_prop(nir_shader *shader);
2220
2221 bool nir_opt_cse(nir_shader *shader);
2222
2223 bool nir_opt_dce(nir_shader *shader);
2224
2225 bool nir_opt_dead_cf(nir_shader *shader);
2226
2227 void nir_opt_gcm(nir_shader *shader);
2228
2229 bool nir_opt_peephole_select(nir_shader *shader);
2230
2231 bool nir_opt_remove_phis(nir_shader *shader);
2232
2233 bool nir_opt_undef(nir_shader *shader);
2234
2235 void nir_sweep(nir_shader *shader);
2236
2237 nir_intrinsic_op nir_intrinsic_from_system_value(gl_system_value val);
2238 gl_system_value nir_system_value_from_intrinsic(nir_intrinsic_op intrin);
2239
2240 #ifdef __cplusplus
2241 } /* extern "C" */
2242 #endif