nir/register: Add a parent_instr field
[mesa.git] / src / glsl / 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 "../list.h"
32 #include "GL/gl.h" /* GLenum */
33 #include "util/ralloc.h"
34 #include "util/set.h"
35 #include "util/bitset.h"
36 #include "nir_types.h"
37 #include <stdio.h>
38
39 #include "nir_opcodes.h"
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44
45 struct gl_program;
46 struct gl_shader_program;
47
48 #define NIR_FALSE 0u
49 #define NIR_TRUE (~0u)
50
51 /** Defines a cast function
52 *
53 * This macro defines a cast function from in_type to out_type where
54 * out_type is some structure type that contains a field of type out_type.
55 *
56 * Note that you have to be a bit careful as the generated cast function
57 * destroys constness.
58 */
59 #define NIR_DEFINE_CAST(name, in_type, out_type, field) \
60 static inline out_type * \
61 name(const in_type *parent) \
62 { \
63 return exec_node_data(out_type, parent, field); \
64 }
65
66 struct nir_function_overload;
67 struct nir_function;
68 struct nir_shader;
69 struct nir_instr;
70
71
72 /**
73 * Description of built-in state associated with a uniform
74 *
75 * \sa nir_variable::state_slots
76 */
77 typedef struct {
78 int tokens[5];
79 int swizzle;
80 } nir_state_slot;
81
82 typedef enum {
83 nir_var_shader_in,
84 nir_var_shader_out,
85 nir_var_global,
86 nir_var_local,
87 nir_var_uniform,
88 nir_var_system_value
89 } nir_variable_mode;
90
91 /**
92 * Data stored in an nir_constant
93 */
94 union nir_constant_data {
95 unsigned u[16];
96 int i[16];
97 float f[16];
98 bool b[16];
99 };
100
101 typedef struct nir_constant {
102 /**
103 * Value of the constant.
104 *
105 * The field used to back the values supplied by the constant is determined
106 * by the type associated with the \c nir_variable. Constants may be
107 * scalars, vectors, or matrices.
108 */
109 union nir_constant_data value;
110
111 /* Array elements / Structure Fields */
112 struct nir_constant **elements;
113 } nir_constant;
114
115 /**
116 * \brief Layout qualifiers for gl_FragDepth.
117 *
118 * The AMD/ARB_conservative_depth extensions allow gl_FragDepth to be redeclared
119 * with a layout qualifier.
120 */
121 typedef enum {
122 nir_depth_layout_none, /**< No depth layout is specified. */
123 nir_depth_layout_any,
124 nir_depth_layout_greater,
125 nir_depth_layout_less,
126 nir_depth_layout_unchanged
127 } nir_depth_layout;
128
129 /**
130 * Either a uniform, global variable, shader input, or shader output. Based on
131 * ir_variable - it should be easy to translate between the two.
132 */
133
134 typedef struct {
135 struct exec_node node;
136
137 /**
138 * Declared type of the variable
139 */
140 const struct glsl_type *type;
141
142 /**
143 * Declared name of the variable
144 */
145 char *name;
146
147 /**
148 * For variables which satisfy the is_interface_instance() predicate, this
149 * points to an array of integers such that if the ith member of the
150 * interface block is an array, max_ifc_array_access[i] is the maximum
151 * array element of that member that has been accessed. If the ith member
152 * of the interface block is not an array, max_ifc_array_access[i] is
153 * unused.
154 *
155 * For variables whose type is not an interface block, this pointer is
156 * NULL.
157 */
158 unsigned *max_ifc_array_access;
159
160 struct nir_variable_data {
161
162 /**
163 * Is the variable read-only?
164 *
165 * This is set for variables declared as \c const, shader inputs,
166 * and uniforms.
167 */
168 unsigned read_only:1;
169 unsigned centroid:1;
170 unsigned sample:1;
171 unsigned invariant:1;
172
173 /**
174 * Storage class of the variable.
175 *
176 * \sa nir_variable_mode
177 */
178 nir_variable_mode mode:4;
179
180 /**
181 * Interpolation mode for shader inputs / outputs
182 *
183 * \sa glsl_interp_qualifier
184 */
185 unsigned interpolation:2;
186
187 /**
188 * \name ARB_fragment_coord_conventions
189 * @{
190 */
191 unsigned origin_upper_left:1;
192 unsigned pixel_center_integer:1;
193 /*@}*/
194
195 /**
196 * Was the location explicitly set in the shader?
197 *
198 * If the location is explicitly set in the shader, it \b cannot be changed
199 * by the linker or by the API (e.g., calls to \c glBindAttribLocation have
200 * no effect).
201 */
202 unsigned explicit_location:1;
203 unsigned explicit_index:1;
204
205 /**
206 * Was an initial binding explicitly set in the shader?
207 *
208 * If so, constant_initializer contains an integer nir_constant
209 * representing the initial binding point.
210 */
211 unsigned explicit_binding:1;
212
213 /**
214 * Does this variable have an initializer?
215 *
216 * This is used by the linker to cross-validiate initializers of global
217 * variables.
218 */
219 unsigned has_initializer:1;
220
221 /**
222 * Is this variable a generic output or input that has not yet been matched
223 * up to a variable in another stage of the pipeline?
224 *
225 * This is used by the linker as scratch storage while assigning locations
226 * to generic inputs and outputs.
227 */
228 unsigned is_unmatched_generic_inout:1;
229
230 /**
231 * If non-zero, then this variable may be packed along with other variables
232 * into a single varying slot, so this offset should be applied when
233 * accessing components. For example, an offset of 1 means that the x
234 * component of this variable is actually stored in component y of the
235 * location specified by \c location.
236 */
237 unsigned location_frac:2;
238
239 /**
240 * Non-zero if this variable was created by lowering a named interface
241 * block which was not an array.
242 *
243 * Note that this variable and \c from_named_ifc_block_array will never
244 * both be non-zero.
245 */
246 unsigned from_named_ifc_block_nonarray:1;
247
248 /**
249 * Non-zero if this variable was created by lowering a named interface
250 * block which was an array.
251 *
252 * Note that this variable and \c from_named_ifc_block_nonarray will never
253 * both be non-zero.
254 */
255 unsigned from_named_ifc_block_array:1;
256
257 /**
258 * \brief Layout qualifier for gl_FragDepth.
259 *
260 * This is not equal to \c ir_depth_layout_none if and only if this
261 * variable is \c gl_FragDepth and a layout qualifier is specified.
262 */
263 nir_depth_layout depth_layout;
264
265 /**
266 * Storage location of the base of this variable
267 *
268 * The precise meaning of this field depends on the nature of the variable.
269 *
270 * - Vertex shader input: one of the values from \c gl_vert_attrib.
271 * - Vertex shader output: one of the values from \c gl_varying_slot.
272 * - Geometry shader input: one of the values from \c gl_varying_slot.
273 * - Geometry shader output: one of the values from \c gl_varying_slot.
274 * - Fragment shader input: one of the values from \c gl_varying_slot.
275 * - Fragment shader output: one of the values from \c gl_frag_result.
276 * - Uniforms: Per-stage uniform slot number for default uniform block.
277 * - Uniforms: Index within the uniform block definition for UBO members.
278 * - Other: This field is not currently used.
279 *
280 * If the variable is a uniform, shader input, or shader output, and the
281 * slot has not been assigned, the value will be -1.
282 */
283 int location;
284
285 /**
286 * The actual location of the variable in the IR. Only valid for inputs
287 * and outputs.
288 */
289 unsigned int driver_location;
290
291 /**
292 * output index for dual source blending.
293 */
294 int index;
295
296 /**
297 * Initial binding point for a sampler or UBO.
298 *
299 * For array types, this represents the binding point for the first element.
300 */
301 int binding;
302
303 /**
304 * Location an atomic counter is stored at.
305 */
306 struct {
307 unsigned buffer_index;
308 unsigned offset;
309 } atomic;
310
311 /**
312 * ARB_shader_image_load_store qualifiers.
313 */
314 struct {
315 bool read_only; /**< "readonly" qualifier. */
316 bool write_only; /**< "writeonly" qualifier. */
317 bool coherent;
318 bool _volatile;
319 bool restrict_flag;
320
321 /** Image internal format if specified explicitly, otherwise GL_NONE. */
322 GLenum format;
323 } image;
324
325 /**
326 * Highest element accessed with a constant expression array index
327 *
328 * Not used for non-array variables.
329 */
330 unsigned max_array_access;
331
332 } data;
333
334 /**
335 * Built-in state that backs this uniform
336 *
337 * Once set at variable creation, \c state_slots must remain invariant.
338 * This is because, ideally, this array would be shared by all clones of
339 * this variable in the IR tree. In other words, we'd really like for it
340 * to be a fly-weight.
341 *
342 * If the variable is not a uniform, \c num_state_slots will be zero and
343 * \c state_slots will be \c NULL.
344 */
345 /*@{*/
346 unsigned num_state_slots; /**< Number of state slots used */
347 nir_state_slot *state_slots; /**< State descriptors. */
348 /*@}*/
349
350 /**
351 * Constant expression assigned in the initializer of the variable
352 */
353 nir_constant *constant_initializer;
354
355 /**
356 * For variables that are in an interface block or are an instance of an
357 * interface block, this is the \c GLSL_TYPE_INTERFACE type for that block.
358 *
359 * \sa ir_variable::location
360 */
361 const struct glsl_type *interface_type;
362 } nir_variable;
363
364 typedef struct {
365 struct exec_node node;
366
367 unsigned num_components; /** < number of vector components */
368 unsigned num_array_elems; /** < size of array (0 for no array) */
369
370 /** generic register index. */
371 unsigned index;
372
373 /** only for debug purposes, can be NULL */
374 const char *name;
375
376 /** whether this register is local (per-function) or global (per-shader) */
377 bool is_global;
378
379 /**
380 * If this flag is set to true, then accessing channels >= num_components
381 * is well-defined, and simply spills over to the next array element. This
382 * is useful for backends that can do per-component accessing, in
383 * particular scalar backends. By setting this flag and making
384 * num_components equal to 1, structures can be packed tightly into
385 * registers and then registers can be accessed per-component to get to
386 * each structure member, even if it crosses vec4 boundaries.
387 */
388 bool is_packed;
389
390 /**
391 * If this pointer is non-NULL then this register has exactly one
392 * definition and that definition dominates all of its uses. This is
393 * set by the out-of-SSA pass so that backends can get SSA-like
394 * information even once they have gone out of SSA.
395 */
396 struct nir_instr *parent_instr;
397
398 /** set of nir_instr's where this register is used (read from) */
399 struct set *uses;
400
401 /** set of nir_instr's where this register is defined (written to) */
402 struct set *defs;
403
404 /** set of nir_if's where this register is used as a condition */
405 struct set *if_uses;
406 } nir_register;
407
408 typedef enum {
409 nir_instr_type_alu,
410 nir_instr_type_call,
411 nir_instr_type_tex,
412 nir_instr_type_intrinsic,
413 nir_instr_type_load_const,
414 nir_instr_type_jump,
415 nir_instr_type_ssa_undef,
416 nir_instr_type_phi,
417 nir_instr_type_parallel_copy,
418 } nir_instr_type;
419
420 typedef struct nir_instr {
421 struct exec_node node;
422 nir_instr_type type;
423 struct nir_block *block;
424
425 /* A temporary for optimization and analysis passes to use for storing
426 * flags. For instance, DCE uses this to store the "dead/live" info.
427 */
428 uint8_t pass_flags;
429 } nir_instr;
430
431 static inline nir_instr *
432 nir_instr_next(nir_instr *instr)
433 {
434 struct exec_node *next = exec_node_get_next(&instr->node);
435 if (exec_node_is_tail_sentinel(next))
436 return NULL;
437 else
438 return exec_node_data(nir_instr, next, node);
439 }
440
441 static inline nir_instr *
442 nir_instr_prev(nir_instr *instr)
443 {
444 struct exec_node *prev = exec_node_get_prev(&instr->node);
445 if (exec_node_is_head_sentinel(prev))
446 return NULL;
447 else
448 return exec_node_data(nir_instr, prev, node);
449 }
450
451 typedef struct {
452 /** for debugging only, can be NULL */
453 const char* name;
454
455 /** generic SSA definition index. */
456 unsigned index;
457
458 /** Index into the live_in and live_out bitfields */
459 unsigned live_index;
460
461 nir_instr *parent_instr;
462
463 /** set of nir_instr's where this register is used (read from) */
464 struct set *uses;
465
466 /** set of nir_if's where this register is used as a condition */
467 struct set *if_uses;
468
469 uint8_t num_components;
470 } nir_ssa_def;
471
472 struct nir_src;
473
474 typedef struct {
475 nir_register *reg;
476 struct nir_src *indirect; /** < NULL for no indirect offset */
477 unsigned base_offset;
478
479 /* TODO use-def chain goes here */
480 } nir_reg_src;
481
482 typedef struct {
483 nir_register *reg;
484 struct nir_src *indirect; /** < NULL for no indirect offset */
485 unsigned base_offset;
486
487 /* TODO def-use chain goes here */
488 } nir_reg_dest;
489
490 typedef struct nir_src {
491 union {
492 nir_reg_src reg;
493 nir_ssa_def *ssa;
494 };
495
496 bool is_ssa;
497 } nir_src;
498
499 typedef struct {
500 union {
501 nir_reg_dest reg;
502 nir_ssa_def ssa;
503 };
504
505 bool is_ssa;
506 } nir_dest;
507
508 static inline nir_src
509 nir_src_for_ssa(nir_ssa_def *def)
510 {
511 nir_src src;
512
513 src.is_ssa = true;
514 src.ssa = def;
515
516 return src;
517 }
518
519 static inline nir_src
520 nir_src_for_reg(nir_register *reg)
521 {
522 nir_src src;
523
524 src.is_ssa = false;
525 src.reg.reg = reg;
526 src.reg.indirect = NULL;
527 src.reg.base_offset = 0;
528
529 return src;
530 }
531
532 static inline nir_dest
533 nir_dest_for_reg(nir_register *reg)
534 {
535 nir_dest dest;
536
537 dest.is_ssa = false;
538 dest.reg.reg = reg;
539 dest.reg.indirect = NULL;
540 dest.reg.base_offset = 0;
541
542 return dest;
543 }
544
545 void nir_src_copy(nir_src *dest, const nir_src *src, void *mem_ctx);
546 void nir_dest_copy(nir_dest *dest, const nir_dest *src, void *mem_ctx);
547
548 typedef struct {
549 nir_src src;
550
551 /**
552 * \name input modifiers
553 */
554 /*@{*/
555 /**
556 * For inputs interpreted as floating point, flips the sign bit. For
557 * inputs interpreted as integers, performs the two's complement negation.
558 */
559 bool negate;
560
561 /**
562 * Clears the sign bit for floating point values, and computes the integer
563 * absolute value for integers. Note that the negate modifier acts after
564 * the absolute value modifier, therefore if both are set then all inputs
565 * will become negative.
566 */
567 bool abs;
568 /*@}*/
569
570 /**
571 * For each input component, says which component of the register it is
572 * chosen from. Note that which elements of the swizzle are used and which
573 * are ignored are based on the write mask for most opcodes - for example,
574 * a statement like "foo.xzw = bar.zyx" would have a writemask of 1101b and
575 * a swizzle of {2, x, 1, 0} where x means "don't care."
576 */
577 uint8_t swizzle[4];
578 } nir_alu_src;
579
580 typedef struct {
581 nir_dest dest;
582
583 /**
584 * \name saturate output modifier
585 *
586 * Only valid for opcodes that output floating-point numbers. Clamps the
587 * output to between 0.0 and 1.0 inclusive.
588 */
589
590 bool saturate;
591
592 unsigned write_mask : 4; /* ignored if dest.is_ssa is true */
593 } nir_alu_dest;
594
595 void nir_alu_src_copy(nir_alu_src *dest, const nir_alu_src *src, void *mem_ctx);
596 void nir_alu_dest_copy(nir_alu_dest *dest, const nir_alu_dest *src,
597 void *mem_ctx);
598
599 typedef enum {
600 nir_type_invalid = 0, /* Not a valid type */
601 nir_type_float,
602 nir_type_int,
603 nir_type_unsigned,
604 nir_type_bool
605 } nir_alu_type;
606
607 typedef enum {
608 NIR_OP_IS_COMMUTATIVE = (1 << 0),
609 NIR_OP_IS_ASSOCIATIVE = (1 << 1),
610 } nir_op_algebraic_property;
611
612 typedef struct {
613 const char *name;
614
615 unsigned num_inputs;
616
617 /**
618 * The number of components in the output
619 *
620 * If non-zero, this is the size of the output and input sizes are
621 * explicitly given; swizzle and writemask are still in effect, but if
622 * the output component is masked out, then the input component may
623 * still be in use.
624 *
625 * If zero, the opcode acts in the standard, per-component manner; the
626 * operation is performed on each component (except the ones that are
627 * masked out) with the input being taken from the input swizzle for
628 * that component.
629 *
630 * The size of some of the inputs may be given (i.e. non-zero) even
631 * though output_size is zero; in that case, the inputs with a zero
632 * size act per-component, while the inputs with non-zero size don't.
633 */
634 unsigned output_size;
635
636 /**
637 * The type of vector that the instruction outputs. Note that the
638 * staurate modifier is only allowed on outputs with the float type.
639 */
640
641 nir_alu_type output_type;
642
643 /**
644 * The number of components in each input
645 */
646 unsigned input_sizes[4];
647
648 /**
649 * The type of vector that each input takes. Note that negate and
650 * absolute value are only allowed on inputs with int or float type and
651 * behave differently on the two.
652 */
653 nir_alu_type input_types[4];
654
655 nir_op_algebraic_property algebraic_properties;
656 } nir_op_info;
657
658 extern const nir_op_info nir_op_infos[nir_num_opcodes];
659
660 typedef struct nir_alu_instr {
661 nir_instr instr;
662 nir_op op;
663 nir_alu_dest dest;
664 nir_alu_src src[];
665 } nir_alu_instr;
666
667 /* is this source channel used? */
668 static inline bool
669 nir_alu_instr_channel_used(nir_alu_instr *instr, unsigned src, unsigned channel)
670 {
671 if (nir_op_infos[instr->op].input_sizes[src] > 0)
672 return channel < nir_op_infos[instr->op].input_sizes[src];
673
674 return (instr->dest.write_mask >> channel) & 1;
675 }
676
677 /*
678 * For instructions whose destinations are SSA, get the number of channels
679 * used for a source
680 */
681 static inline unsigned
682 nir_ssa_alu_instr_src_components(nir_alu_instr *instr, unsigned src)
683 {
684 assert(instr->dest.dest.is_ssa);
685
686 if (nir_op_infos[instr->op].input_sizes[src] > 0)
687 return nir_op_infos[instr->op].input_sizes[src];
688
689 return instr->dest.dest.ssa.num_components;
690 }
691
692 typedef enum {
693 nir_deref_type_var,
694 nir_deref_type_array,
695 nir_deref_type_struct
696 } nir_deref_type;
697
698 typedef struct nir_deref {
699 nir_deref_type deref_type;
700 struct nir_deref *child;
701 const struct glsl_type *type;
702 } nir_deref;
703
704 typedef struct {
705 nir_deref deref;
706
707 nir_variable *var;
708 } nir_deref_var;
709
710 /* This enum describes how the array is referenced. If the deref is
711 * direct then the base_offset is used. If the deref is indirect then then
712 * offset is given by base_offset + indirect. If the deref is a wildcard
713 * then the deref refers to all of the elements of the array at the same
714 * time. Wildcard dereferences are only ever allowed in copy_var
715 * intrinsics and the source and destination derefs must have matching
716 * wildcards.
717 */
718 typedef enum {
719 nir_deref_array_type_direct,
720 nir_deref_array_type_indirect,
721 nir_deref_array_type_wildcard,
722 } nir_deref_array_type;
723
724 typedef struct {
725 nir_deref deref;
726
727 nir_deref_array_type deref_array_type;
728 unsigned base_offset;
729 nir_src indirect;
730 } nir_deref_array;
731
732 typedef struct {
733 nir_deref deref;
734
735 unsigned index;
736 } nir_deref_struct;
737
738 NIR_DEFINE_CAST(nir_deref_as_var, nir_deref, nir_deref_var, deref)
739 NIR_DEFINE_CAST(nir_deref_as_array, nir_deref, nir_deref_array, deref)
740 NIR_DEFINE_CAST(nir_deref_as_struct, nir_deref, nir_deref_struct, deref)
741
742 typedef struct {
743 nir_instr instr;
744
745 unsigned num_params;
746 nir_deref_var **params;
747 nir_deref_var *return_deref;
748
749 struct nir_function_overload *callee;
750 } nir_call_instr;
751
752 #define INTRINSIC(name, num_srcs, src_components, has_dest, dest_components, \
753 num_variables, num_indices, flags) \
754 nir_intrinsic_##name,
755
756 #define LAST_INTRINSIC(name) nir_last_intrinsic = nir_intrinsic_##name,
757
758 typedef enum {
759 #include "nir_intrinsics.h"
760 nir_num_intrinsics = nir_last_intrinsic + 1
761 } nir_intrinsic_op;
762
763 #undef INTRINSIC
764 #undef LAST_INTRINSIC
765
766 /** Represents an intrinsic
767 *
768 * An intrinsic is an instruction type for handling things that are
769 * more-or-less regular operations but don't just consume and produce SSA
770 * values like ALU operations do. Intrinsics are not for things that have
771 * special semantic meaning such as phi nodes and parallel copies.
772 * Examples of intrinsics include variable load/store operations, system
773 * value loads, and the like. Even though texturing more-or-less falls
774 * under this category, texturing is its own instruction type because
775 * trying to represent texturing with intrinsics would lead to a
776 * combinatorial explosion of intrinsic opcodes.
777 *
778 * By having a single instruction type for handling a lot of different
779 * cases, optimization passes can look for intrinsics and, for the most
780 * part, completely ignore them. Each intrinsic type also has a few
781 * possible flags that govern whether or not they can be reordered or
782 * eliminated. That way passes like dead code elimination can still work
783 * on intrisics without understanding the meaning of each.
784 *
785 * Each intrinsic has some number of constant indices, some number of
786 * variables, and some number of sources. What these sources, variables,
787 * and indices mean depends on the intrinsic and is documented with the
788 * intrinsic declaration in nir_intrinsics.h. Intrinsics and texture
789 * instructions are the only types of instruction that can operate on
790 * variables.
791 */
792 typedef struct {
793 nir_instr instr;
794
795 nir_intrinsic_op intrinsic;
796
797 nir_dest dest;
798
799 /** number of components if this is a vectorized intrinsic
800 *
801 * Similarly to ALU operations, some intrinsics are vectorized.
802 * An intrinsic is vectorized if nir_intrinsic_infos.dest_components == 0.
803 * For vectorized intrinsics, the num_components field specifies the
804 * number of destination components and the number of source components
805 * for all sources with nir_intrinsic_infos.src_components[i] == 0.
806 */
807 uint8_t num_components;
808
809 int const_index[3];
810
811 nir_deref_var *variables[2];
812
813 nir_src src[];
814 } nir_intrinsic_instr;
815
816 /**
817 * \name NIR intrinsics semantic flags
818 *
819 * information about what the compiler can do with the intrinsics.
820 *
821 * \sa nir_intrinsic_info::flags
822 */
823 typedef enum {
824 /**
825 * whether the intrinsic can be safely eliminated if none of its output
826 * value is not being used.
827 */
828 NIR_INTRINSIC_CAN_ELIMINATE = (1 << 0),
829
830 /**
831 * Whether the intrinsic can be reordered with respect to any other
832 * intrinsic, i.e. whether the only reordering dependencies of the
833 * intrinsic are due to the register reads/writes.
834 */
835 NIR_INTRINSIC_CAN_REORDER = (1 << 1),
836 } nir_intrinsic_semantic_flag;
837
838 #define NIR_INTRINSIC_MAX_INPUTS 4
839
840 typedef struct {
841 const char *name;
842
843 unsigned num_srcs; /** < number of register/SSA inputs */
844
845 /** number of components of each input register
846 *
847 * If this value is 0, the number of components is given by the
848 * num_components field of nir_intrinsic_instr.
849 */
850 unsigned src_components[NIR_INTRINSIC_MAX_INPUTS];
851
852 bool has_dest;
853
854 /** number of components of the output register
855 *
856 * If this value is 0, the number of components is given by the
857 * num_components field of nir_intrinsic_instr.
858 */
859 unsigned dest_components;
860
861 /** the number of inputs/outputs that are variables */
862 unsigned num_variables;
863
864 /** the number of constant indices used by the intrinsic */
865 unsigned num_indices;
866
867 /** semantic flags for calls to this intrinsic */
868 nir_intrinsic_semantic_flag flags;
869 } nir_intrinsic_info;
870
871 extern const nir_intrinsic_info nir_intrinsic_infos[nir_num_intrinsics];
872
873 /**
874 * \group texture information
875 *
876 * This gives semantic information about textures which is useful to the
877 * frontend, the backend, and lowering passes, but not the optimizer.
878 */
879
880 typedef enum {
881 nir_tex_src_coord,
882 nir_tex_src_projector,
883 nir_tex_src_comparitor, /* shadow comparitor */
884 nir_tex_src_offset,
885 nir_tex_src_bias,
886 nir_tex_src_lod,
887 nir_tex_src_ms_index, /* MSAA sample index */
888 nir_tex_src_ddx,
889 nir_tex_src_ddy,
890 nir_tex_src_sampler_offset, /* < dynamically uniform indirect offset */
891 nir_num_tex_src_types
892 } nir_tex_src_type;
893
894 typedef struct {
895 nir_src src;
896 nir_tex_src_type src_type;
897 } nir_tex_src;
898
899 typedef enum {
900 nir_texop_tex, /**< Regular texture look-up */
901 nir_texop_txb, /**< Texture look-up with LOD bias */
902 nir_texop_txl, /**< Texture look-up with explicit LOD */
903 nir_texop_txd, /**< Texture look-up with partial derivatvies */
904 nir_texop_txf, /**< Texel fetch with explicit LOD */
905 nir_texop_txf_ms, /**< Multisample texture fetch */
906 nir_texop_txs, /**< Texture size */
907 nir_texop_lod, /**< Texture lod query */
908 nir_texop_tg4, /**< Texture gather */
909 nir_texop_query_levels /**< Texture levels query */
910 } nir_texop;
911
912 typedef struct {
913 nir_instr instr;
914
915 enum glsl_sampler_dim sampler_dim;
916 nir_alu_type dest_type;
917
918 nir_texop op;
919 nir_dest dest;
920 nir_tex_src *src;
921 unsigned num_srcs, coord_components;
922 bool is_array, is_shadow;
923
924 /**
925 * If is_shadow is true, whether this is the old-style shadow that outputs 4
926 * components or the new-style shadow that outputs 1 component.
927 */
928 bool is_new_style_shadow;
929
930 /* constant offset - must be 0 if the offset source is used */
931 int const_offset[4];
932
933 /* gather component selector */
934 unsigned component : 2;
935
936 /** The sampler index
937 *
938 * If this texture instruction has a nir_tex_src_sampler_offset source,
939 * then the sampler index is given by sampler_index + sampler_offset.
940 */
941 unsigned sampler_index;
942
943 /** The size of the sampler array or 0 if it's not an array */
944 unsigned sampler_array_size;
945
946 nir_deref_var *sampler; /* if this is NULL, use sampler_index instead */
947 } nir_tex_instr;
948
949 static inline unsigned
950 nir_tex_instr_dest_size(nir_tex_instr *instr)
951 {
952 if (instr->op == nir_texop_txs) {
953 unsigned ret;
954 switch (instr->sampler_dim) {
955 case GLSL_SAMPLER_DIM_1D:
956 case GLSL_SAMPLER_DIM_BUF:
957 ret = 1;
958 break;
959 case GLSL_SAMPLER_DIM_2D:
960 case GLSL_SAMPLER_DIM_CUBE:
961 case GLSL_SAMPLER_DIM_MS:
962 case GLSL_SAMPLER_DIM_RECT:
963 case GLSL_SAMPLER_DIM_EXTERNAL:
964 ret = 2;
965 break;
966 case GLSL_SAMPLER_DIM_3D:
967 ret = 3;
968 break;
969 default:
970 unreachable("not reached");
971 }
972 if (instr->is_array)
973 ret++;
974 return ret;
975 }
976
977 if (instr->op == nir_texop_query_levels)
978 return 2;
979
980 if (instr->is_shadow && instr->is_new_style_shadow)
981 return 1;
982
983 return 4;
984 }
985
986 static inline unsigned
987 nir_tex_instr_src_size(nir_tex_instr *instr, unsigned src)
988 {
989 if (instr->src[src].src_type == nir_tex_src_coord)
990 return instr->coord_components;
991
992
993 if (instr->src[src].src_type == nir_tex_src_offset ||
994 instr->src[src].src_type == nir_tex_src_ddx ||
995 instr->src[src].src_type == nir_tex_src_ddy) {
996 if (instr->is_array)
997 return instr->coord_components - 1;
998 else
999 return instr->coord_components;
1000 }
1001
1002 return 1;
1003 }
1004
1005 static inline int
1006 nir_tex_instr_src_index(nir_tex_instr *instr, nir_tex_src_type type)
1007 {
1008 for (unsigned i = 0; i < instr->num_srcs; i++)
1009 if (instr->src[i].src_type == type)
1010 return (int) i;
1011
1012 return -1;
1013 }
1014
1015 typedef struct {
1016 union {
1017 float f[4];
1018 int32_t i[4];
1019 uint32_t u[4];
1020 };
1021 } nir_const_value;
1022
1023 typedef struct {
1024 nir_instr instr;
1025
1026 nir_const_value value;
1027
1028 nir_ssa_def def;
1029 } nir_load_const_instr;
1030
1031 typedef enum {
1032 nir_jump_return,
1033 nir_jump_break,
1034 nir_jump_continue,
1035 } nir_jump_type;
1036
1037 typedef struct {
1038 nir_instr instr;
1039 nir_jump_type type;
1040 } nir_jump_instr;
1041
1042 /* creates a new SSA variable in an undefined state */
1043
1044 typedef struct {
1045 nir_instr instr;
1046 nir_ssa_def def;
1047 } nir_ssa_undef_instr;
1048
1049 typedef struct {
1050 struct exec_node node;
1051
1052 /* The predecessor block corresponding to this source */
1053 struct nir_block *pred;
1054
1055 nir_src src;
1056 } nir_phi_src;
1057
1058 #define nir_foreach_phi_src(phi, entry) \
1059 foreach_list_typed(nir_phi_src, entry, node, &(phi)->srcs)
1060
1061 typedef struct {
1062 nir_instr instr;
1063
1064 struct exec_list srcs; /** < list of nir_phi_src */
1065
1066 nir_dest dest;
1067 } nir_phi_instr;
1068
1069 typedef struct {
1070 struct exec_node node;
1071 nir_src src;
1072 nir_dest dest;
1073 } nir_parallel_copy_entry;
1074
1075 #define nir_foreach_parallel_copy_entry(pcopy, entry) \
1076 foreach_list_typed(nir_parallel_copy_entry, entry, node, &(pcopy)->entries)
1077
1078 typedef struct {
1079 nir_instr instr;
1080
1081 /* A list of nir_parallel_copy_entry's. The sources of all of the
1082 * entries are copied to the corresponding destinations "in parallel".
1083 * In other words, if we have two entries: a -> b and b -> a, the values
1084 * get swapped.
1085 */
1086 struct exec_list entries;
1087 } nir_parallel_copy_instr;
1088
1089 NIR_DEFINE_CAST(nir_instr_as_alu, nir_instr, nir_alu_instr, instr)
1090 NIR_DEFINE_CAST(nir_instr_as_call, nir_instr, nir_call_instr, instr)
1091 NIR_DEFINE_CAST(nir_instr_as_jump, nir_instr, nir_jump_instr, instr)
1092 NIR_DEFINE_CAST(nir_instr_as_tex, nir_instr, nir_tex_instr, instr)
1093 NIR_DEFINE_CAST(nir_instr_as_intrinsic, nir_instr, nir_intrinsic_instr, instr)
1094 NIR_DEFINE_CAST(nir_instr_as_load_const, nir_instr, nir_load_const_instr, instr)
1095 NIR_DEFINE_CAST(nir_instr_as_ssa_undef, nir_instr, nir_ssa_undef_instr, instr)
1096 NIR_DEFINE_CAST(nir_instr_as_phi, nir_instr, nir_phi_instr, instr)
1097 NIR_DEFINE_CAST(nir_instr_as_parallel_copy, nir_instr,
1098 nir_parallel_copy_instr, instr)
1099
1100 /*
1101 * Control flow
1102 *
1103 * Control flow consists of a tree of control flow nodes, which include
1104 * if-statements and loops. The leaves of the tree are basic blocks, lists of
1105 * instructions that always run start-to-finish. Each basic block also keeps
1106 * track of its successors (blocks which may run immediately after the current
1107 * block) and predecessors (blocks which could have run immediately before the
1108 * current block). Each function also has a start block and an end block which
1109 * all return statements point to (which is always empty). Together, all the
1110 * blocks with their predecessors and successors make up the control flow
1111 * graph (CFG) of the function. There are helpers that modify the tree of
1112 * control flow nodes while modifying the CFG appropriately; these should be
1113 * used instead of modifying the tree directly.
1114 */
1115
1116 typedef enum {
1117 nir_cf_node_block,
1118 nir_cf_node_if,
1119 nir_cf_node_loop,
1120 nir_cf_node_function
1121 } nir_cf_node_type;
1122
1123 typedef struct nir_cf_node {
1124 struct exec_node node;
1125 nir_cf_node_type type;
1126 struct nir_cf_node *parent;
1127 } nir_cf_node;
1128
1129 typedef struct nir_block {
1130 nir_cf_node cf_node;
1131
1132 struct exec_list instr_list; /** < list of nir_instr */
1133
1134 /** generic block index; generated by nir_index_blocks */
1135 unsigned index;
1136
1137 /*
1138 * Each block can only have up to 2 successors, so we put them in a simple
1139 * array - no need for anything more complicated.
1140 */
1141 struct nir_block *successors[2];
1142
1143 /* Set of nir_block predecessors in the CFG */
1144 struct set *predecessors;
1145
1146 /*
1147 * this node's immediate dominator in the dominance tree - set to NULL for
1148 * the start block.
1149 */
1150 struct nir_block *imm_dom;
1151
1152 /* This node's children in the dominance tree */
1153 unsigned num_dom_children;
1154 struct nir_block **dom_children;
1155
1156 /* Set of nir_block's on the dominance frontier of this block */
1157 struct set *dom_frontier;
1158
1159 /*
1160 * These two indices have the property that dom_{pre,post}_index for each
1161 * child of this block in the dominance tree will always be between
1162 * dom_pre_index and dom_post_index for this block, which makes testing if
1163 * a given block is dominated by another block an O(1) operation.
1164 */
1165 unsigned dom_pre_index, dom_post_index;
1166
1167 /* live in and out for this block; used for liveness analysis */
1168 BITSET_WORD *live_in;
1169 BITSET_WORD *live_out;
1170 } nir_block;
1171
1172 static inline nir_instr *
1173 nir_block_first_instr(nir_block *block)
1174 {
1175 struct exec_node *head = exec_list_get_head(&block->instr_list);
1176 return exec_node_data(nir_instr, head, node);
1177 }
1178
1179 static inline nir_instr *
1180 nir_block_last_instr(nir_block *block)
1181 {
1182 struct exec_node *tail = exec_list_get_tail(&block->instr_list);
1183 return exec_node_data(nir_instr, tail, node);
1184 }
1185
1186 #define nir_foreach_instr(block, instr) \
1187 foreach_list_typed(nir_instr, instr, node, &(block)->instr_list)
1188 #define nir_foreach_instr_reverse(block, instr) \
1189 foreach_list_typed_reverse(nir_instr, instr, node, &(block)->instr_list)
1190 #define nir_foreach_instr_safe(block, instr) \
1191 foreach_list_typed_safe(nir_instr, instr, node, &(block)->instr_list)
1192
1193 typedef struct {
1194 nir_cf_node cf_node;
1195 nir_src condition;
1196
1197 struct exec_list then_list; /** < list of nir_cf_node */
1198 struct exec_list else_list; /** < list of nir_cf_node */
1199 } nir_if;
1200
1201 static inline nir_cf_node *
1202 nir_if_first_then_node(nir_if *if_stmt)
1203 {
1204 struct exec_node *head = exec_list_get_head(&if_stmt->then_list);
1205 return exec_node_data(nir_cf_node, head, node);
1206 }
1207
1208 static inline nir_cf_node *
1209 nir_if_last_then_node(nir_if *if_stmt)
1210 {
1211 struct exec_node *tail = exec_list_get_tail(&if_stmt->then_list);
1212 return exec_node_data(nir_cf_node, tail, node);
1213 }
1214
1215 static inline nir_cf_node *
1216 nir_if_first_else_node(nir_if *if_stmt)
1217 {
1218 struct exec_node *head = exec_list_get_head(&if_stmt->else_list);
1219 return exec_node_data(nir_cf_node, head, node);
1220 }
1221
1222 static inline nir_cf_node *
1223 nir_if_last_else_node(nir_if *if_stmt)
1224 {
1225 struct exec_node *tail = exec_list_get_tail(&if_stmt->else_list);
1226 return exec_node_data(nir_cf_node, tail, node);
1227 }
1228
1229 typedef struct {
1230 nir_cf_node cf_node;
1231
1232 struct exec_list body; /** < list of nir_cf_node */
1233 } nir_loop;
1234
1235 static inline nir_cf_node *
1236 nir_loop_first_cf_node(nir_loop *loop)
1237 {
1238 return exec_node_data(nir_cf_node, exec_list_get_head(&loop->body), node);
1239 }
1240
1241 static inline nir_cf_node *
1242 nir_loop_last_cf_node(nir_loop *loop)
1243 {
1244 return exec_node_data(nir_cf_node, exec_list_get_tail(&loop->body), node);
1245 }
1246
1247 /**
1248 * Various bits of metadata that can may be created or required by
1249 * optimization and analysis passes
1250 */
1251 typedef enum {
1252 nir_metadata_none = 0x0,
1253 nir_metadata_block_index = 0x1,
1254 nir_metadata_dominance = 0x2,
1255 nir_metadata_live_variables = 0x4,
1256 } nir_metadata;
1257
1258 typedef struct {
1259 nir_cf_node cf_node;
1260
1261 /** pointer to the overload of which this is an implementation */
1262 struct nir_function_overload *overload;
1263
1264 struct exec_list body; /** < list of nir_cf_node */
1265
1266 nir_block *start_block, *end_block;
1267
1268 /** list for all local variables in the function */
1269 struct exec_list locals;
1270
1271 /** array of variables used as parameters */
1272 unsigned num_params;
1273 nir_variable **params;
1274
1275 /** variable used to hold the result of the function */
1276 nir_variable *return_var;
1277
1278 /** list of local registers in the function */
1279 struct exec_list registers;
1280
1281 /** next available local register index */
1282 unsigned reg_alloc;
1283
1284 /** next available SSA value index */
1285 unsigned ssa_alloc;
1286
1287 /* total number of basic blocks, only valid when block_index_dirty = false */
1288 unsigned num_blocks;
1289
1290 nir_metadata valid_metadata;
1291 } nir_function_impl;
1292
1293 static inline nir_cf_node *
1294 nir_cf_node_next(nir_cf_node *node)
1295 {
1296 struct exec_node *next = exec_node_get_next(&node->node);
1297 if (exec_node_is_tail_sentinel(next))
1298 return NULL;
1299 else
1300 return exec_node_data(nir_cf_node, next, node);
1301 }
1302
1303 static inline nir_cf_node *
1304 nir_cf_node_prev(nir_cf_node *node)
1305 {
1306 struct exec_node *prev = exec_node_get_prev(&node->node);
1307 if (exec_node_is_head_sentinel(prev))
1308 return NULL;
1309 else
1310 return exec_node_data(nir_cf_node, prev, node);
1311 }
1312
1313 static inline bool
1314 nir_cf_node_is_first(const nir_cf_node *node)
1315 {
1316 return exec_node_is_head_sentinel(node->node.prev);
1317 }
1318
1319 static inline bool
1320 nir_cf_node_is_last(const nir_cf_node *node)
1321 {
1322 return exec_node_is_tail_sentinel(node->node.next);
1323 }
1324
1325 NIR_DEFINE_CAST(nir_cf_node_as_block, nir_cf_node, nir_block, cf_node)
1326 NIR_DEFINE_CAST(nir_cf_node_as_if, nir_cf_node, nir_if, cf_node)
1327 NIR_DEFINE_CAST(nir_cf_node_as_loop, nir_cf_node, nir_loop, cf_node)
1328 NIR_DEFINE_CAST(nir_cf_node_as_function, nir_cf_node, nir_function_impl, cf_node)
1329
1330 typedef enum {
1331 nir_parameter_in,
1332 nir_parameter_out,
1333 nir_parameter_inout,
1334 } nir_parameter_type;
1335
1336 typedef struct {
1337 nir_parameter_type param_type;
1338 const struct glsl_type *type;
1339 } nir_parameter;
1340
1341 typedef struct nir_function_overload {
1342 struct exec_node node;
1343
1344 unsigned num_params;
1345 nir_parameter *params;
1346 const struct glsl_type *return_type;
1347
1348 nir_function_impl *impl; /** < NULL if the overload is only declared yet */
1349
1350 /** pointer to the function of which this is an overload */
1351 struct nir_function *function;
1352 } nir_function_overload;
1353
1354 typedef struct nir_function {
1355 struct exec_node node;
1356
1357 struct exec_list overload_list; /** < list of nir_function_overload */
1358 const char *name;
1359 struct nir_shader *shader;
1360 } nir_function;
1361
1362 #define nir_function_first_overload(func) \
1363 exec_node_data(nir_function_overload, \
1364 exec_list_get_head(&(func)->overload_list), node)
1365
1366 typedef struct nir_shader_compiler_options {
1367 bool lower_ffma;
1368 bool lower_fpow;
1369 bool lower_fsat;
1370 bool lower_fsqrt;
1371 /** lowers fneg and ineg to fsub and isub. */
1372 bool lower_negate;
1373 } nir_shader_compiler_options;
1374
1375 typedef struct nir_shader {
1376 /** hash table of name -> uniform nir_variable */
1377 struct hash_table *uniforms;
1378
1379 /** hash table of name -> input nir_variable */
1380 struct hash_table *inputs;
1381
1382 /** hash table of name -> output nir_variable */
1383 struct hash_table *outputs;
1384
1385 /** Set of driver-specific options for the shader.
1386 *
1387 * The memory for the options is expected to be kept in a single static
1388 * copy by the driver.
1389 */
1390 const struct nir_shader_compiler_options *options;
1391
1392 /** list of global variables in the shader */
1393 struct exec_list globals;
1394
1395 /** list of system value variables in the shader */
1396 struct exec_list system_values;
1397
1398 struct exec_list functions; /** < list of nir_function */
1399
1400 /** list of global register in the shader */
1401 struct exec_list registers;
1402
1403 /** structures used in this shader */
1404 unsigned num_user_structures;
1405 struct glsl_type **user_structures;
1406
1407 /** next available global register index */
1408 unsigned reg_alloc;
1409
1410 /**
1411 * the highest index a load_input_*, load_uniform_*, etc. intrinsic can
1412 * access plus one
1413 */
1414 unsigned num_inputs, num_uniforms, num_outputs;
1415 } nir_shader;
1416
1417 #define nir_foreach_overload(shader, overload) \
1418 foreach_list_typed(nir_function, func, node, &(shader)->functions) \
1419 foreach_list_typed(nir_function_overload, overload, node, \
1420 &(func)->overload_list)
1421
1422 nir_shader *nir_shader_create(void *mem_ctx,
1423 const nir_shader_compiler_options *options);
1424
1425 /** creates a register, including assigning it an index and adding it to the list */
1426 nir_register *nir_global_reg_create(nir_shader *shader);
1427
1428 nir_register *nir_local_reg_create(nir_function_impl *impl);
1429
1430 void nir_reg_remove(nir_register *reg);
1431
1432 /** creates a function and adds it to the shader's list of functions */
1433 nir_function *nir_function_create(nir_shader *shader, const char *name);
1434
1435 /** creates a null function returning null */
1436 nir_function_overload *nir_function_overload_create(nir_function *func);
1437
1438 nir_function_impl *nir_function_impl_create(nir_function_overload *func);
1439
1440 nir_block *nir_block_create(void *mem_ctx);
1441 nir_if *nir_if_create(void *mem_ctx);
1442 nir_loop *nir_loop_create(void *mem_ctx);
1443
1444 nir_function_impl *nir_cf_node_get_function(nir_cf_node *node);
1445
1446 /** puts a control flow node immediately after another control flow node */
1447 void nir_cf_node_insert_after(nir_cf_node *node, nir_cf_node *after);
1448
1449 /** puts a control flow node immediately before another control flow node */
1450 void nir_cf_node_insert_before(nir_cf_node *node, nir_cf_node *before);
1451
1452 /** puts a control flow node at the beginning of a list from an if, loop, or function */
1453 void nir_cf_node_insert_begin(struct exec_list *list, nir_cf_node *node);
1454
1455 /** puts a control flow node at the end of a list from an if, loop, or function */
1456 void nir_cf_node_insert_end(struct exec_list *list, nir_cf_node *node);
1457
1458 /** removes a control flow node, doing any cleanup necessary */
1459 void nir_cf_node_remove(nir_cf_node *node);
1460
1461 /** requests that the given pieces of metadata be generated */
1462 void nir_metadata_require(nir_function_impl *impl, nir_metadata required);
1463 /** dirties all but the preserved metadata */
1464 void nir_metadata_preserve(nir_function_impl *impl, nir_metadata preserved);
1465
1466 /** creates an instruction with default swizzle/writemask/etc. with NULL registers */
1467 nir_alu_instr *nir_alu_instr_create(void *mem_ctx, nir_op op);
1468
1469 nir_jump_instr *nir_jump_instr_create(void *mem_ctx, nir_jump_type type);
1470
1471 nir_load_const_instr *nir_load_const_instr_create(void *mem_ctx,
1472 unsigned num_components);
1473
1474 nir_intrinsic_instr *nir_intrinsic_instr_create(void *mem_ctx,
1475 nir_intrinsic_op op);
1476
1477 nir_call_instr *nir_call_instr_create(void *mem_ctx,
1478 nir_function_overload *callee);
1479
1480 nir_tex_instr *nir_tex_instr_create(void *mem_ctx, unsigned num_srcs);
1481
1482 nir_phi_instr *nir_phi_instr_create(void *mem_ctx);
1483
1484 nir_parallel_copy_instr *nir_parallel_copy_instr_create(void *mem_ctx);
1485
1486 nir_ssa_undef_instr *nir_ssa_undef_instr_create(void *mem_ctx,
1487 unsigned num_components);
1488
1489 nir_deref_var *nir_deref_var_create(void *mem_ctx, nir_variable *var);
1490 nir_deref_array *nir_deref_array_create(void *mem_ctx);
1491 nir_deref_struct *nir_deref_struct_create(void *mem_ctx, unsigned field_index);
1492
1493 nir_deref *nir_copy_deref(void *mem_ctx, nir_deref *deref);
1494
1495 void nir_instr_insert_before(nir_instr *instr, nir_instr *before);
1496 void nir_instr_insert_after(nir_instr *instr, nir_instr *after);
1497
1498 void nir_instr_insert_before_block(nir_block *block, nir_instr *before);
1499 void nir_instr_insert_after_block(nir_block *block, nir_instr *after);
1500
1501 void nir_instr_insert_before_cf(nir_cf_node *node, nir_instr *before);
1502 void nir_instr_insert_after_cf(nir_cf_node *node, nir_instr *after);
1503
1504 void nir_instr_insert_before_cf_list(struct exec_list *list, nir_instr *before);
1505 void nir_instr_insert_after_cf_list(struct exec_list *list, nir_instr *after);
1506
1507 void nir_instr_remove(nir_instr *instr);
1508
1509 typedef bool (*nir_foreach_ssa_def_cb)(nir_ssa_def *def, void *state);
1510 typedef bool (*nir_foreach_dest_cb)(nir_dest *dest, void *state);
1511 typedef bool (*nir_foreach_src_cb)(nir_src *src, void *state);
1512 bool nir_foreach_ssa_def(nir_instr *instr, nir_foreach_ssa_def_cb cb,
1513 void *state);
1514 bool nir_foreach_dest(nir_instr *instr, nir_foreach_dest_cb cb, void *state);
1515 bool nir_foreach_src(nir_instr *instr, nir_foreach_src_cb cb, void *state);
1516
1517 nir_const_value *nir_src_as_const_value(nir_src src);
1518 bool nir_srcs_equal(nir_src src1, nir_src src2);
1519 void nir_instr_rewrite_src(nir_instr *instr, nir_src *src, nir_src new_src);
1520
1521 void nir_ssa_dest_init(nir_instr *instr, nir_dest *dest,
1522 unsigned num_components, const char *name);
1523 void nir_ssa_def_init(nir_instr *instr, nir_ssa_def *def,
1524 unsigned num_components, const char *name);
1525 void nir_ssa_def_rewrite_uses(nir_ssa_def *def, nir_src new_src, void *mem_ctx);
1526
1527 /* visits basic blocks in source-code order */
1528 typedef bool (*nir_foreach_block_cb)(nir_block *block, void *state);
1529 bool nir_foreach_block(nir_function_impl *impl, nir_foreach_block_cb cb,
1530 void *state);
1531 bool nir_foreach_block_reverse(nir_function_impl *impl, nir_foreach_block_cb cb,
1532 void *state);
1533
1534 /* If the following CF node is an if, this function returns that if.
1535 * Otherwise, it returns NULL.
1536 */
1537 nir_if *nir_block_get_following_if(nir_block *block);
1538
1539 void nir_index_local_regs(nir_function_impl *impl);
1540 void nir_index_global_regs(nir_shader *shader);
1541 void nir_index_ssa_defs(nir_function_impl *impl);
1542
1543 void nir_index_blocks(nir_function_impl *impl);
1544
1545 void nir_print_shader(nir_shader *shader, FILE *fp);
1546 void nir_print_instr(const nir_instr *instr, FILE *fp);
1547
1548 #ifdef DEBUG
1549 void nir_validate_shader(nir_shader *shader);
1550 #else
1551 static inline void nir_validate_shader(nir_shader *shader) { }
1552 #endif /* DEBUG */
1553
1554 void nir_calc_dominance_impl(nir_function_impl *impl);
1555 void nir_calc_dominance(nir_shader *shader);
1556
1557 nir_block *nir_dominance_lca(nir_block *b1, nir_block *b2);
1558 bool nir_block_dominates(nir_block *parent, nir_block *child);
1559
1560 void nir_dump_dom_tree_impl(nir_function_impl *impl, FILE *fp);
1561 void nir_dump_dom_tree(nir_shader *shader, FILE *fp);
1562
1563 void nir_dump_dom_frontier_impl(nir_function_impl *impl, FILE *fp);
1564 void nir_dump_dom_frontier(nir_shader *shader, FILE *fp);
1565
1566 void nir_dump_cfg_impl(nir_function_impl *impl, FILE *fp);
1567 void nir_dump_cfg(nir_shader *shader, FILE *fp);
1568
1569 void nir_split_var_copies(nir_shader *shader);
1570
1571 void nir_lower_var_copy_instr(nir_intrinsic_instr *copy, void *mem_ctx);
1572 void nir_lower_var_copies(nir_shader *shader);
1573
1574 void nir_lower_global_vars_to_local(nir_shader *shader);
1575
1576 void nir_lower_locals_to_regs(nir_shader *shader);
1577
1578 void nir_lower_io(nir_shader *shader);
1579
1580 void nir_lower_vars_to_ssa(nir_shader *shader);
1581
1582 void nir_remove_dead_variables(nir_shader *shader);
1583
1584 void nir_lower_vec_to_movs(nir_shader *shader);
1585 void nir_lower_alu_to_scalar(nir_shader *shader);
1586
1587 void nir_lower_phis_to_scalar(nir_shader *shader);
1588
1589 void nir_lower_samplers(nir_shader *shader,
1590 struct gl_shader_program *shader_program,
1591 struct gl_program *prog);
1592
1593 void nir_lower_system_values(nir_shader *shader);
1594
1595 void nir_lower_atomics(nir_shader *shader);
1596 void nir_lower_to_source_mods(nir_shader *shader);
1597
1598 void nir_live_variables_impl(nir_function_impl *impl);
1599 bool nir_ssa_defs_interfere(nir_ssa_def *a, nir_ssa_def *b);
1600
1601 void nir_convert_to_ssa_impl(nir_function_impl *impl);
1602 void nir_convert_to_ssa(nir_shader *shader);
1603 void nir_convert_from_ssa(nir_shader *shader);
1604
1605 bool nir_opt_algebraic(nir_shader *shader);
1606 bool nir_opt_constant_folding(nir_shader *shader);
1607
1608 bool nir_opt_global_to_local(nir_shader *shader);
1609
1610 bool nir_copy_prop_impl(nir_function_impl *impl);
1611 bool nir_copy_prop(nir_shader *shader);
1612
1613 bool nir_opt_cse(nir_shader *shader);
1614
1615 bool nir_opt_dce_impl(nir_function_impl *impl);
1616 bool nir_opt_dce(nir_shader *shader);
1617
1618 void nir_opt_gcm(nir_shader *shader);
1619
1620 bool nir_opt_peephole_select(nir_shader *shader);
1621 bool nir_opt_peephole_ffma(nir_shader *shader);
1622
1623 bool nir_opt_remove_phis(nir_shader *shader);
1624
1625 #ifdef __cplusplus
1626 } /* extern "C" */
1627 #endif