nir: Implement a nir_sweep() pass.
[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_instr *
533 nir_src_get_parent_instr(const nir_src *src)
534 {
535 if (src->is_ssa) {
536 return src->ssa->parent_instr;
537 } else {
538 return src->reg.reg->parent_instr;
539 }
540 }
541
542 static inline nir_dest
543 nir_dest_for_reg(nir_register *reg)
544 {
545 nir_dest dest;
546
547 dest.is_ssa = false;
548 dest.reg.reg = reg;
549 dest.reg.indirect = NULL;
550 dest.reg.base_offset = 0;
551
552 return dest;
553 }
554
555 void nir_src_copy(nir_src *dest, const nir_src *src, void *mem_ctx);
556 void nir_dest_copy(nir_dest *dest, const nir_dest *src, void *mem_ctx);
557
558 typedef struct {
559 nir_src src;
560
561 /**
562 * \name input modifiers
563 */
564 /*@{*/
565 /**
566 * For inputs interpreted as floating point, flips the sign bit. For
567 * inputs interpreted as integers, performs the two's complement negation.
568 */
569 bool negate;
570
571 /**
572 * Clears the sign bit for floating point values, and computes the integer
573 * absolute value for integers. Note that the negate modifier acts after
574 * the absolute value modifier, therefore if both are set then all inputs
575 * will become negative.
576 */
577 bool abs;
578 /*@}*/
579
580 /**
581 * For each input component, says which component of the register it is
582 * chosen from. Note that which elements of the swizzle are used and which
583 * are ignored are based on the write mask for most opcodes - for example,
584 * a statement like "foo.xzw = bar.zyx" would have a writemask of 1101b and
585 * a swizzle of {2, x, 1, 0} where x means "don't care."
586 */
587 uint8_t swizzle[4];
588 } nir_alu_src;
589
590 typedef struct {
591 nir_dest dest;
592
593 /**
594 * \name saturate output modifier
595 *
596 * Only valid for opcodes that output floating-point numbers. Clamps the
597 * output to between 0.0 and 1.0 inclusive.
598 */
599
600 bool saturate;
601
602 unsigned write_mask : 4; /* ignored if dest.is_ssa is true */
603 } nir_alu_dest;
604
605 void nir_alu_src_copy(nir_alu_src *dest, const nir_alu_src *src, void *mem_ctx);
606 void nir_alu_dest_copy(nir_alu_dest *dest, const nir_alu_dest *src,
607 void *mem_ctx);
608
609 typedef enum {
610 nir_type_invalid = 0, /* Not a valid type */
611 nir_type_float,
612 nir_type_int,
613 nir_type_unsigned,
614 nir_type_bool
615 } nir_alu_type;
616
617 typedef enum {
618 NIR_OP_IS_COMMUTATIVE = (1 << 0),
619 NIR_OP_IS_ASSOCIATIVE = (1 << 1),
620 } nir_op_algebraic_property;
621
622 typedef struct {
623 const char *name;
624
625 unsigned num_inputs;
626
627 /**
628 * The number of components in the output
629 *
630 * If non-zero, this is the size of the output and input sizes are
631 * explicitly given; swizzle and writemask are still in effect, but if
632 * the output component is masked out, then the input component may
633 * still be in use.
634 *
635 * If zero, the opcode acts in the standard, per-component manner; the
636 * operation is performed on each component (except the ones that are
637 * masked out) with the input being taken from the input swizzle for
638 * that component.
639 *
640 * The size of some of the inputs may be given (i.e. non-zero) even
641 * though output_size is zero; in that case, the inputs with a zero
642 * size act per-component, while the inputs with non-zero size don't.
643 */
644 unsigned output_size;
645
646 /**
647 * The type of vector that the instruction outputs. Note that the
648 * staurate modifier is only allowed on outputs with the float type.
649 */
650
651 nir_alu_type output_type;
652
653 /**
654 * The number of components in each input
655 */
656 unsigned input_sizes[4];
657
658 /**
659 * The type of vector that each input takes. Note that negate and
660 * absolute value are only allowed on inputs with int or float type and
661 * behave differently on the two.
662 */
663 nir_alu_type input_types[4];
664
665 nir_op_algebraic_property algebraic_properties;
666 } nir_op_info;
667
668 extern const nir_op_info nir_op_infos[nir_num_opcodes];
669
670 typedef struct nir_alu_instr {
671 nir_instr instr;
672 nir_op op;
673 nir_alu_dest dest;
674 nir_alu_src src[];
675 } nir_alu_instr;
676
677 /* is this source channel used? */
678 static inline bool
679 nir_alu_instr_channel_used(nir_alu_instr *instr, unsigned src, unsigned channel)
680 {
681 if (nir_op_infos[instr->op].input_sizes[src] > 0)
682 return channel < nir_op_infos[instr->op].input_sizes[src];
683
684 return (instr->dest.write_mask >> channel) & 1;
685 }
686
687 /*
688 * For instructions whose destinations are SSA, get the number of channels
689 * used for a source
690 */
691 static inline unsigned
692 nir_ssa_alu_instr_src_components(nir_alu_instr *instr, unsigned src)
693 {
694 assert(instr->dest.dest.is_ssa);
695
696 if (nir_op_infos[instr->op].input_sizes[src] > 0)
697 return nir_op_infos[instr->op].input_sizes[src];
698
699 return instr->dest.dest.ssa.num_components;
700 }
701
702 typedef enum {
703 nir_deref_type_var,
704 nir_deref_type_array,
705 nir_deref_type_struct
706 } nir_deref_type;
707
708 typedef struct nir_deref {
709 nir_deref_type deref_type;
710 struct nir_deref *child;
711 const struct glsl_type *type;
712 } nir_deref;
713
714 typedef struct {
715 nir_deref deref;
716
717 nir_variable *var;
718 } nir_deref_var;
719
720 /* This enum describes how the array is referenced. If the deref is
721 * direct then the base_offset is used. If the deref is indirect then then
722 * offset is given by base_offset + indirect. If the deref is a wildcard
723 * then the deref refers to all of the elements of the array at the same
724 * time. Wildcard dereferences are only ever allowed in copy_var
725 * intrinsics and the source and destination derefs must have matching
726 * wildcards.
727 */
728 typedef enum {
729 nir_deref_array_type_direct,
730 nir_deref_array_type_indirect,
731 nir_deref_array_type_wildcard,
732 } nir_deref_array_type;
733
734 typedef struct {
735 nir_deref deref;
736
737 nir_deref_array_type deref_array_type;
738 unsigned base_offset;
739 nir_src indirect;
740 } nir_deref_array;
741
742 typedef struct {
743 nir_deref deref;
744
745 unsigned index;
746 } nir_deref_struct;
747
748 NIR_DEFINE_CAST(nir_deref_as_var, nir_deref, nir_deref_var, deref)
749 NIR_DEFINE_CAST(nir_deref_as_array, nir_deref, nir_deref_array, deref)
750 NIR_DEFINE_CAST(nir_deref_as_struct, nir_deref, nir_deref_struct, deref)
751
752 typedef struct {
753 nir_instr instr;
754
755 unsigned num_params;
756 nir_deref_var **params;
757 nir_deref_var *return_deref;
758
759 struct nir_function_overload *callee;
760 } nir_call_instr;
761
762 #define INTRINSIC(name, num_srcs, src_components, has_dest, dest_components, \
763 num_variables, num_indices, flags) \
764 nir_intrinsic_##name,
765
766 #define LAST_INTRINSIC(name) nir_last_intrinsic = nir_intrinsic_##name,
767
768 typedef enum {
769 #include "nir_intrinsics.h"
770 nir_num_intrinsics = nir_last_intrinsic + 1
771 } nir_intrinsic_op;
772
773 #undef INTRINSIC
774 #undef LAST_INTRINSIC
775
776 /** Represents an intrinsic
777 *
778 * An intrinsic is an instruction type for handling things that are
779 * more-or-less regular operations but don't just consume and produce SSA
780 * values like ALU operations do. Intrinsics are not for things that have
781 * special semantic meaning such as phi nodes and parallel copies.
782 * Examples of intrinsics include variable load/store operations, system
783 * value loads, and the like. Even though texturing more-or-less falls
784 * under this category, texturing is its own instruction type because
785 * trying to represent texturing with intrinsics would lead to a
786 * combinatorial explosion of intrinsic opcodes.
787 *
788 * By having a single instruction type for handling a lot of different
789 * cases, optimization passes can look for intrinsics and, for the most
790 * part, completely ignore them. Each intrinsic type also has a few
791 * possible flags that govern whether or not they can be reordered or
792 * eliminated. That way passes like dead code elimination can still work
793 * on intrisics without understanding the meaning of each.
794 *
795 * Each intrinsic has some number of constant indices, some number of
796 * variables, and some number of sources. What these sources, variables,
797 * and indices mean depends on the intrinsic and is documented with the
798 * intrinsic declaration in nir_intrinsics.h. Intrinsics and texture
799 * instructions are the only types of instruction that can operate on
800 * variables.
801 */
802 typedef struct {
803 nir_instr instr;
804
805 nir_intrinsic_op intrinsic;
806
807 nir_dest dest;
808
809 /** number of components if this is a vectorized intrinsic
810 *
811 * Similarly to ALU operations, some intrinsics are vectorized.
812 * An intrinsic is vectorized if nir_intrinsic_infos.dest_components == 0.
813 * For vectorized intrinsics, the num_components field specifies the
814 * number of destination components and the number of source components
815 * for all sources with nir_intrinsic_infos.src_components[i] == 0.
816 */
817 uint8_t num_components;
818
819 int const_index[3];
820
821 nir_deref_var *variables[2];
822
823 nir_src src[];
824 } nir_intrinsic_instr;
825
826 /**
827 * \name NIR intrinsics semantic flags
828 *
829 * information about what the compiler can do with the intrinsics.
830 *
831 * \sa nir_intrinsic_info::flags
832 */
833 typedef enum {
834 /**
835 * whether the intrinsic can be safely eliminated if none of its output
836 * value is not being used.
837 */
838 NIR_INTRINSIC_CAN_ELIMINATE = (1 << 0),
839
840 /**
841 * Whether the intrinsic can be reordered with respect to any other
842 * intrinsic, i.e. whether the only reordering dependencies of the
843 * intrinsic are due to the register reads/writes.
844 */
845 NIR_INTRINSIC_CAN_REORDER = (1 << 1),
846 } nir_intrinsic_semantic_flag;
847
848 #define NIR_INTRINSIC_MAX_INPUTS 4
849
850 typedef struct {
851 const char *name;
852
853 unsigned num_srcs; /** < number of register/SSA inputs */
854
855 /** number of components of each input register
856 *
857 * If this value is 0, the number of components is given by the
858 * num_components field of nir_intrinsic_instr.
859 */
860 unsigned src_components[NIR_INTRINSIC_MAX_INPUTS];
861
862 bool has_dest;
863
864 /** number of components of the output register
865 *
866 * If this value is 0, the number of components is given by the
867 * num_components field of nir_intrinsic_instr.
868 */
869 unsigned dest_components;
870
871 /** the number of inputs/outputs that are variables */
872 unsigned num_variables;
873
874 /** the number of constant indices used by the intrinsic */
875 unsigned num_indices;
876
877 /** semantic flags for calls to this intrinsic */
878 nir_intrinsic_semantic_flag flags;
879 } nir_intrinsic_info;
880
881 extern const nir_intrinsic_info nir_intrinsic_infos[nir_num_intrinsics];
882
883 /**
884 * \group texture information
885 *
886 * This gives semantic information about textures which is useful to the
887 * frontend, the backend, and lowering passes, but not the optimizer.
888 */
889
890 typedef enum {
891 nir_tex_src_coord,
892 nir_tex_src_projector,
893 nir_tex_src_comparitor, /* shadow comparitor */
894 nir_tex_src_offset,
895 nir_tex_src_bias,
896 nir_tex_src_lod,
897 nir_tex_src_ms_index, /* MSAA sample index */
898 nir_tex_src_ddx,
899 nir_tex_src_ddy,
900 nir_tex_src_sampler_offset, /* < dynamically uniform indirect offset */
901 nir_num_tex_src_types
902 } nir_tex_src_type;
903
904 typedef struct {
905 nir_src src;
906 nir_tex_src_type src_type;
907 } nir_tex_src;
908
909 typedef enum {
910 nir_texop_tex, /**< Regular texture look-up */
911 nir_texop_txb, /**< Texture look-up with LOD bias */
912 nir_texop_txl, /**< Texture look-up with explicit LOD */
913 nir_texop_txd, /**< Texture look-up with partial derivatvies */
914 nir_texop_txf, /**< Texel fetch with explicit LOD */
915 nir_texop_txf_ms, /**< Multisample texture fetch */
916 nir_texop_txs, /**< Texture size */
917 nir_texop_lod, /**< Texture lod query */
918 nir_texop_tg4, /**< Texture gather */
919 nir_texop_query_levels /**< Texture levels query */
920 } nir_texop;
921
922 typedef struct {
923 nir_instr instr;
924
925 enum glsl_sampler_dim sampler_dim;
926 nir_alu_type dest_type;
927
928 nir_texop op;
929 nir_dest dest;
930 nir_tex_src *src;
931 unsigned num_srcs, coord_components;
932 bool is_array, is_shadow;
933
934 /**
935 * If is_shadow is true, whether this is the old-style shadow that outputs 4
936 * components or the new-style shadow that outputs 1 component.
937 */
938 bool is_new_style_shadow;
939
940 /* constant offset - must be 0 if the offset source is used */
941 int const_offset[4];
942
943 /* gather component selector */
944 unsigned component : 2;
945
946 /** The sampler index
947 *
948 * If this texture instruction has a nir_tex_src_sampler_offset source,
949 * then the sampler index is given by sampler_index + sampler_offset.
950 */
951 unsigned sampler_index;
952
953 /** The size of the sampler array or 0 if it's not an array */
954 unsigned sampler_array_size;
955
956 nir_deref_var *sampler; /* if this is NULL, use sampler_index instead */
957 } nir_tex_instr;
958
959 static inline unsigned
960 nir_tex_instr_dest_size(nir_tex_instr *instr)
961 {
962 if (instr->op == nir_texop_txs) {
963 unsigned ret;
964 switch (instr->sampler_dim) {
965 case GLSL_SAMPLER_DIM_1D:
966 case GLSL_SAMPLER_DIM_BUF:
967 ret = 1;
968 break;
969 case GLSL_SAMPLER_DIM_2D:
970 case GLSL_SAMPLER_DIM_CUBE:
971 case GLSL_SAMPLER_DIM_MS:
972 case GLSL_SAMPLER_DIM_RECT:
973 case GLSL_SAMPLER_DIM_EXTERNAL:
974 ret = 2;
975 break;
976 case GLSL_SAMPLER_DIM_3D:
977 ret = 3;
978 break;
979 default:
980 unreachable("not reached");
981 }
982 if (instr->is_array)
983 ret++;
984 return ret;
985 }
986
987 if (instr->op == nir_texop_query_levels)
988 return 2;
989
990 if (instr->is_shadow && instr->is_new_style_shadow)
991 return 1;
992
993 return 4;
994 }
995
996 static inline unsigned
997 nir_tex_instr_src_size(nir_tex_instr *instr, unsigned src)
998 {
999 if (instr->src[src].src_type == nir_tex_src_coord)
1000 return instr->coord_components;
1001
1002
1003 if (instr->src[src].src_type == nir_tex_src_offset ||
1004 instr->src[src].src_type == nir_tex_src_ddx ||
1005 instr->src[src].src_type == nir_tex_src_ddy) {
1006 if (instr->is_array)
1007 return instr->coord_components - 1;
1008 else
1009 return instr->coord_components;
1010 }
1011
1012 return 1;
1013 }
1014
1015 static inline int
1016 nir_tex_instr_src_index(nir_tex_instr *instr, nir_tex_src_type type)
1017 {
1018 for (unsigned i = 0; i < instr->num_srcs; i++)
1019 if (instr->src[i].src_type == type)
1020 return (int) i;
1021
1022 return -1;
1023 }
1024
1025 typedef struct {
1026 union {
1027 float f[4];
1028 int32_t i[4];
1029 uint32_t u[4];
1030 };
1031 } nir_const_value;
1032
1033 typedef struct {
1034 nir_instr instr;
1035
1036 nir_const_value value;
1037
1038 nir_ssa_def def;
1039 } nir_load_const_instr;
1040
1041 typedef enum {
1042 nir_jump_return,
1043 nir_jump_break,
1044 nir_jump_continue,
1045 } nir_jump_type;
1046
1047 typedef struct {
1048 nir_instr instr;
1049 nir_jump_type type;
1050 } nir_jump_instr;
1051
1052 /* creates a new SSA variable in an undefined state */
1053
1054 typedef struct {
1055 nir_instr instr;
1056 nir_ssa_def def;
1057 } nir_ssa_undef_instr;
1058
1059 typedef struct {
1060 struct exec_node node;
1061
1062 /* The predecessor block corresponding to this source */
1063 struct nir_block *pred;
1064
1065 nir_src src;
1066 } nir_phi_src;
1067
1068 #define nir_foreach_phi_src(phi, entry) \
1069 foreach_list_typed(nir_phi_src, entry, node, &(phi)->srcs)
1070
1071 typedef struct {
1072 nir_instr instr;
1073
1074 struct exec_list srcs; /** < list of nir_phi_src */
1075
1076 nir_dest dest;
1077 } nir_phi_instr;
1078
1079 typedef struct {
1080 struct exec_node node;
1081 nir_src src;
1082 nir_dest dest;
1083 } nir_parallel_copy_entry;
1084
1085 #define nir_foreach_parallel_copy_entry(pcopy, entry) \
1086 foreach_list_typed(nir_parallel_copy_entry, entry, node, &(pcopy)->entries)
1087
1088 typedef struct {
1089 nir_instr instr;
1090
1091 /* A list of nir_parallel_copy_entry's. The sources of all of the
1092 * entries are copied to the corresponding destinations "in parallel".
1093 * In other words, if we have two entries: a -> b and b -> a, the values
1094 * get swapped.
1095 */
1096 struct exec_list entries;
1097 } nir_parallel_copy_instr;
1098
1099 NIR_DEFINE_CAST(nir_instr_as_alu, nir_instr, nir_alu_instr, instr)
1100 NIR_DEFINE_CAST(nir_instr_as_call, nir_instr, nir_call_instr, instr)
1101 NIR_DEFINE_CAST(nir_instr_as_jump, nir_instr, nir_jump_instr, instr)
1102 NIR_DEFINE_CAST(nir_instr_as_tex, nir_instr, nir_tex_instr, instr)
1103 NIR_DEFINE_CAST(nir_instr_as_intrinsic, nir_instr, nir_intrinsic_instr, instr)
1104 NIR_DEFINE_CAST(nir_instr_as_load_const, nir_instr, nir_load_const_instr, instr)
1105 NIR_DEFINE_CAST(nir_instr_as_ssa_undef, nir_instr, nir_ssa_undef_instr, instr)
1106 NIR_DEFINE_CAST(nir_instr_as_phi, nir_instr, nir_phi_instr, instr)
1107 NIR_DEFINE_CAST(nir_instr_as_parallel_copy, nir_instr,
1108 nir_parallel_copy_instr, instr)
1109
1110 /*
1111 * Control flow
1112 *
1113 * Control flow consists of a tree of control flow nodes, which include
1114 * if-statements and loops. The leaves of the tree are basic blocks, lists of
1115 * instructions that always run start-to-finish. Each basic block also keeps
1116 * track of its successors (blocks which may run immediately after the current
1117 * block) and predecessors (blocks which could have run immediately before the
1118 * current block). Each function also has a start block and an end block which
1119 * all return statements point to (which is always empty). Together, all the
1120 * blocks with their predecessors and successors make up the control flow
1121 * graph (CFG) of the function. There are helpers that modify the tree of
1122 * control flow nodes while modifying the CFG appropriately; these should be
1123 * used instead of modifying the tree directly.
1124 */
1125
1126 typedef enum {
1127 nir_cf_node_block,
1128 nir_cf_node_if,
1129 nir_cf_node_loop,
1130 nir_cf_node_function
1131 } nir_cf_node_type;
1132
1133 typedef struct nir_cf_node {
1134 struct exec_node node;
1135 nir_cf_node_type type;
1136 struct nir_cf_node *parent;
1137 } nir_cf_node;
1138
1139 typedef struct nir_block {
1140 nir_cf_node cf_node;
1141
1142 struct exec_list instr_list; /** < list of nir_instr */
1143
1144 /** generic block index; generated by nir_index_blocks */
1145 unsigned index;
1146
1147 /*
1148 * Each block can only have up to 2 successors, so we put them in a simple
1149 * array - no need for anything more complicated.
1150 */
1151 struct nir_block *successors[2];
1152
1153 /* Set of nir_block predecessors in the CFG */
1154 struct set *predecessors;
1155
1156 /*
1157 * this node's immediate dominator in the dominance tree - set to NULL for
1158 * the start block.
1159 */
1160 struct nir_block *imm_dom;
1161
1162 /* This node's children in the dominance tree */
1163 unsigned num_dom_children;
1164 struct nir_block **dom_children;
1165
1166 /* Set of nir_block's on the dominance frontier of this block */
1167 struct set *dom_frontier;
1168
1169 /*
1170 * These two indices have the property that dom_{pre,post}_index for each
1171 * child of this block in the dominance tree will always be between
1172 * dom_pre_index and dom_post_index for this block, which makes testing if
1173 * a given block is dominated by another block an O(1) operation.
1174 */
1175 unsigned dom_pre_index, dom_post_index;
1176
1177 /* live in and out for this block; used for liveness analysis */
1178 BITSET_WORD *live_in;
1179 BITSET_WORD *live_out;
1180 } nir_block;
1181
1182 static inline nir_instr *
1183 nir_block_first_instr(nir_block *block)
1184 {
1185 struct exec_node *head = exec_list_get_head(&block->instr_list);
1186 return exec_node_data(nir_instr, head, node);
1187 }
1188
1189 static inline nir_instr *
1190 nir_block_last_instr(nir_block *block)
1191 {
1192 struct exec_node *tail = exec_list_get_tail(&block->instr_list);
1193 return exec_node_data(nir_instr, tail, node);
1194 }
1195
1196 #define nir_foreach_instr(block, instr) \
1197 foreach_list_typed(nir_instr, instr, node, &(block)->instr_list)
1198 #define nir_foreach_instr_reverse(block, instr) \
1199 foreach_list_typed_reverse(nir_instr, instr, node, &(block)->instr_list)
1200 #define nir_foreach_instr_safe(block, instr) \
1201 foreach_list_typed_safe(nir_instr, instr, node, &(block)->instr_list)
1202
1203 typedef struct {
1204 nir_cf_node cf_node;
1205 nir_src condition;
1206
1207 struct exec_list then_list; /** < list of nir_cf_node */
1208 struct exec_list else_list; /** < list of nir_cf_node */
1209 } nir_if;
1210
1211 static inline nir_cf_node *
1212 nir_if_first_then_node(nir_if *if_stmt)
1213 {
1214 struct exec_node *head = exec_list_get_head(&if_stmt->then_list);
1215 return exec_node_data(nir_cf_node, head, node);
1216 }
1217
1218 static inline nir_cf_node *
1219 nir_if_last_then_node(nir_if *if_stmt)
1220 {
1221 struct exec_node *tail = exec_list_get_tail(&if_stmt->then_list);
1222 return exec_node_data(nir_cf_node, tail, node);
1223 }
1224
1225 static inline nir_cf_node *
1226 nir_if_first_else_node(nir_if *if_stmt)
1227 {
1228 struct exec_node *head = exec_list_get_head(&if_stmt->else_list);
1229 return exec_node_data(nir_cf_node, head, node);
1230 }
1231
1232 static inline nir_cf_node *
1233 nir_if_last_else_node(nir_if *if_stmt)
1234 {
1235 struct exec_node *tail = exec_list_get_tail(&if_stmt->else_list);
1236 return exec_node_data(nir_cf_node, tail, node);
1237 }
1238
1239 typedef struct {
1240 nir_cf_node cf_node;
1241
1242 struct exec_list body; /** < list of nir_cf_node */
1243 } nir_loop;
1244
1245 static inline nir_cf_node *
1246 nir_loop_first_cf_node(nir_loop *loop)
1247 {
1248 return exec_node_data(nir_cf_node, exec_list_get_head(&loop->body), node);
1249 }
1250
1251 static inline nir_cf_node *
1252 nir_loop_last_cf_node(nir_loop *loop)
1253 {
1254 return exec_node_data(nir_cf_node, exec_list_get_tail(&loop->body), node);
1255 }
1256
1257 /**
1258 * Various bits of metadata that can may be created or required by
1259 * optimization and analysis passes
1260 */
1261 typedef enum {
1262 nir_metadata_none = 0x0,
1263 nir_metadata_block_index = 0x1,
1264 nir_metadata_dominance = 0x2,
1265 nir_metadata_live_variables = 0x4,
1266 } nir_metadata;
1267
1268 typedef struct {
1269 nir_cf_node cf_node;
1270
1271 /** pointer to the overload of which this is an implementation */
1272 struct nir_function_overload *overload;
1273
1274 struct exec_list body; /** < list of nir_cf_node */
1275
1276 nir_block *start_block, *end_block;
1277
1278 /** list for all local variables in the function */
1279 struct exec_list locals;
1280
1281 /** array of variables used as parameters */
1282 unsigned num_params;
1283 nir_variable **params;
1284
1285 /** variable used to hold the result of the function */
1286 nir_variable *return_var;
1287
1288 /** list of local registers in the function */
1289 struct exec_list registers;
1290
1291 /** next available local register index */
1292 unsigned reg_alloc;
1293
1294 /** next available SSA value index */
1295 unsigned ssa_alloc;
1296
1297 /* total number of basic blocks, only valid when block_index_dirty = false */
1298 unsigned num_blocks;
1299
1300 nir_metadata valid_metadata;
1301 } nir_function_impl;
1302
1303 static inline nir_cf_node *
1304 nir_cf_node_next(nir_cf_node *node)
1305 {
1306 struct exec_node *next = exec_node_get_next(&node->node);
1307 if (exec_node_is_tail_sentinel(next))
1308 return NULL;
1309 else
1310 return exec_node_data(nir_cf_node, next, node);
1311 }
1312
1313 static inline nir_cf_node *
1314 nir_cf_node_prev(nir_cf_node *node)
1315 {
1316 struct exec_node *prev = exec_node_get_prev(&node->node);
1317 if (exec_node_is_head_sentinel(prev))
1318 return NULL;
1319 else
1320 return exec_node_data(nir_cf_node, prev, node);
1321 }
1322
1323 static inline bool
1324 nir_cf_node_is_first(const nir_cf_node *node)
1325 {
1326 return exec_node_is_head_sentinel(node->node.prev);
1327 }
1328
1329 static inline bool
1330 nir_cf_node_is_last(const nir_cf_node *node)
1331 {
1332 return exec_node_is_tail_sentinel(node->node.next);
1333 }
1334
1335 NIR_DEFINE_CAST(nir_cf_node_as_block, nir_cf_node, nir_block, cf_node)
1336 NIR_DEFINE_CAST(nir_cf_node_as_if, nir_cf_node, nir_if, cf_node)
1337 NIR_DEFINE_CAST(nir_cf_node_as_loop, nir_cf_node, nir_loop, cf_node)
1338 NIR_DEFINE_CAST(nir_cf_node_as_function, nir_cf_node, nir_function_impl, cf_node)
1339
1340 typedef enum {
1341 nir_parameter_in,
1342 nir_parameter_out,
1343 nir_parameter_inout,
1344 } nir_parameter_type;
1345
1346 typedef struct {
1347 nir_parameter_type param_type;
1348 const struct glsl_type *type;
1349 } nir_parameter;
1350
1351 typedef struct nir_function_overload {
1352 struct exec_node node;
1353
1354 unsigned num_params;
1355 nir_parameter *params;
1356 const struct glsl_type *return_type;
1357
1358 nir_function_impl *impl; /** < NULL if the overload is only declared yet */
1359
1360 /** pointer to the function of which this is an overload */
1361 struct nir_function *function;
1362 } nir_function_overload;
1363
1364 typedef struct nir_function {
1365 struct exec_node node;
1366
1367 struct exec_list overload_list; /** < list of nir_function_overload */
1368 const char *name;
1369 struct nir_shader *shader;
1370 } nir_function;
1371
1372 #define nir_function_first_overload(func) \
1373 exec_node_data(nir_function_overload, \
1374 exec_list_get_head(&(func)->overload_list), node)
1375
1376 typedef struct nir_shader_compiler_options {
1377 bool lower_ffma;
1378 bool lower_flrp;
1379 bool lower_fpow;
1380 bool lower_fsat;
1381 bool lower_fsqrt;
1382 /** lowers fneg and ineg to fsub and isub. */
1383 bool lower_negate;
1384
1385 /* lower {slt,sge,seq,sne} to {flt,fge,feq,fne} + b2f: */
1386 bool lower_scmp;
1387
1388 /**
1389 * Does the driver support real 32-bit integers? (Otherwise, integers
1390 * are simulated by floats.)
1391 */
1392 bool native_integers;
1393 } nir_shader_compiler_options;
1394
1395 typedef struct nir_shader {
1396 /** hash table of name -> uniform nir_variable */
1397 struct exec_list uniforms;
1398
1399 /** hash table of name -> input nir_variable */
1400 struct exec_list inputs;
1401
1402 /** hash table of name -> output nir_variable */
1403 struct exec_list outputs;
1404
1405 /** Set of driver-specific options for the shader.
1406 *
1407 * The memory for the options is expected to be kept in a single static
1408 * copy by the driver.
1409 */
1410 const struct nir_shader_compiler_options *options;
1411
1412 /** list of global variables in the shader */
1413 struct exec_list globals;
1414
1415 /** list of system value variables in the shader */
1416 struct exec_list system_values;
1417
1418 struct exec_list functions; /** < list of nir_function */
1419
1420 /** list of global register in the shader */
1421 struct exec_list registers;
1422
1423 /** next available global register index */
1424 unsigned reg_alloc;
1425
1426 /**
1427 * the highest index a load_input_*, load_uniform_*, etc. intrinsic can
1428 * access plus one
1429 */
1430 unsigned num_inputs, num_uniforms, num_outputs;
1431 } nir_shader;
1432
1433 #define nir_foreach_overload(shader, overload) \
1434 foreach_list_typed(nir_function, func, node, &(shader)->functions) \
1435 foreach_list_typed(nir_function_overload, overload, node, \
1436 &(func)->overload_list)
1437
1438 nir_shader *nir_shader_create(void *mem_ctx,
1439 const nir_shader_compiler_options *options);
1440
1441 /** creates a register, including assigning it an index and adding it to the list */
1442 nir_register *nir_global_reg_create(nir_shader *shader);
1443
1444 nir_register *nir_local_reg_create(nir_function_impl *impl);
1445
1446 void nir_reg_remove(nir_register *reg);
1447
1448 /** creates a function and adds it to the shader's list of functions */
1449 nir_function *nir_function_create(nir_shader *shader, const char *name);
1450
1451 /** creates a null function returning null */
1452 nir_function_overload *nir_function_overload_create(nir_function *func);
1453
1454 nir_function_impl *nir_function_impl_create(nir_function_overload *func);
1455
1456 nir_block *nir_block_create(void *mem_ctx);
1457 nir_if *nir_if_create(void *mem_ctx);
1458 nir_loop *nir_loop_create(void *mem_ctx);
1459
1460 nir_function_impl *nir_cf_node_get_function(nir_cf_node *node);
1461
1462 /** puts a control flow node immediately after another control flow node */
1463 void nir_cf_node_insert_after(nir_cf_node *node, nir_cf_node *after);
1464
1465 /** puts a control flow node immediately before another control flow node */
1466 void nir_cf_node_insert_before(nir_cf_node *node, nir_cf_node *before);
1467
1468 /** puts a control flow node at the beginning of a list from an if, loop, or function */
1469 void nir_cf_node_insert_begin(struct exec_list *list, nir_cf_node *node);
1470
1471 /** puts a control flow node at the end of a list from an if, loop, or function */
1472 void nir_cf_node_insert_end(struct exec_list *list, nir_cf_node *node);
1473
1474 /** removes a control flow node, doing any cleanup necessary */
1475 void nir_cf_node_remove(nir_cf_node *node);
1476
1477 /** requests that the given pieces of metadata be generated */
1478 void nir_metadata_require(nir_function_impl *impl, nir_metadata required);
1479 /** dirties all but the preserved metadata */
1480 void nir_metadata_preserve(nir_function_impl *impl, nir_metadata preserved);
1481
1482 /** creates an instruction with default swizzle/writemask/etc. with NULL registers */
1483 nir_alu_instr *nir_alu_instr_create(void *mem_ctx, nir_op op);
1484
1485 nir_jump_instr *nir_jump_instr_create(void *mem_ctx, nir_jump_type type);
1486
1487 nir_load_const_instr *nir_load_const_instr_create(void *mem_ctx,
1488 unsigned num_components);
1489
1490 nir_intrinsic_instr *nir_intrinsic_instr_create(void *mem_ctx,
1491 nir_intrinsic_op op);
1492
1493 nir_call_instr *nir_call_instr_create(void *mem_ctx,
1494 nir_function_overload *callee);
1495
1496 nir_tex_instr *nir_tex_instr_create(void *mem_ctx, unsigned num_srcs);
1497
1498 nir_phi_instr *nir_phi_instr_create(void *mem_ctx);
1499
1500 nir_parallel_copy_instr *nir_parallel_copy_instr_create(void *mem_ctx);
1501
1502 nir_ssa_undef_instr *nir_ssa_undef_instr_create(void *mem_ctx,
1503 unsigned num_components);
1504
1505 nir_deref_var *nir_deref_var_create(void *mem_ctx, nir_variable *var);
1506 nir_deref_array *nir_deref_array_create(void *mem_ctx);
1507 nir_deref_struct *nir_deref_struct_create(void *mem_ctx, unsigned field_index);
1508
1509 nir_deref *nir_copy_deref(void *mem_ctx, nir_deref *deref);
1510
1511 void nir_instr_insert_before(nir_instr *instr, nir_instr *before);
1512 void nir_instr_insert_after(nir_instr *instr, nir_instr *after);
1513
1514 void nir_instr_insert_before_block(nir_block *block, nir_instr *before);
1515 void nir_instr_insert_after_block(nir_block *block, nir_instr *after);
1516
1517 void nir_instr_insert_before_cf(nir_cf_node *node, nir_instr *before);
1518 void nir_instr_insert_after_cf(nir_cf_node *node, nir_instr *after);
1519
1520 void nir_instr_insert_before_cf_list(struct exec_list *list, nir_instr *before);
1521 void nir_instr_insert_after_cf_list(struct exec_list *list, nir_instr *after);
1522
1523 void nir_instr_remove(nir_instr *instr);
1524
1525 typedef bool (*nir_foreach_ssa_def_cb)(nir_ssa_def *def, void *state);
1526 typedef bool (*nir_foreach_dest_cb)(nir_dest *dest, void *state);
1527 typedef bool (*nir_foreach_src_cb)(nir_src *src, void *state);
1528 bool nir_foreach_ssa_def(nir_instr *instr, nir_foreach_ssa_def_cb cb,
1529 void *state);
1530 bool nir_foreach_dest(nir_instr *instr, nir_foreach_dest_cb cb, void *state);
1531 bool nir_foreach_src(nir_instr *instr, nir_foreach_src_cb cb, void *state);
1532
1533 nir_const_value *nir_src_as_const_value(nir_src src);
1534 bool nir_srcs_equal(nir_src src1, nir_src src2);
1535 void nir_instr_rewrite_src(nir_instr *instr, nir_src *src, nir_src new_src);
1536
1537 void nir_ssa_dest_init(nir_instr *instr, nir_dest *dest,
1538 unsigned num_components, const char *name);
1539 void nir_ssa_def_init(nir_instr *instr, nir_ssa_def *def,
1540 unsigned num_components, const char *name);
1541 void nir_ssa_def_rewrite_uses(nir_ssa_def *def, nir_src new_src, void *mem_ctx);
1542
1543 /* visits basic blocks in source-code order */
1544 typedef bool (*nir_foreach_block_cb)(nir_block *block, void *state);
1545 bool nir_foreach_block(nir_function_impl *impl, nir_foreach_block_cb cb,
1546 void *state);
1547 bool nir_foreach_block_reverse(nir_function_impl *impl, nir_foreach_block_cb cb,
1548 void *state);
1549
1550 /* If the following CF node is an if, this function returns that if.
1551 * Otherwise, it returns NULL.
1552 */
1553 nir_if *nir_block_get_following_if(nir_block *block);
1554
1555 void nir_index_local_regs(nir_function_impl *impl);
1556 void nir_index_global_regs(nir_shader *shader);
1557 void nir_index_ssa_defs(nir_function_impl *impl);
1558
1559 void nir_index_blocks(nir_function_impl *impl);
1560
1561 void nir_print_shader(nir_shader *shader, FILE *fp);
1562 void nir_print_instr(const nir_instr *instr, FILE *fp);
1563
1564 #ifdef DEBUG
1565 void nir_validate_shader(nir_shader *shader);
1566 #else
1567 static inline void nir_validate_shader(nir_shader *shader) { }
1568 #endif /* DEBUG */
1569
1570 void nir_calc_dominance_impl(nir_function_impl *impl);
1571 void nir_calc_dominance(nir_shader *shader);
1572
1573 nir_block *nir_dominance_lca(nir_block *b1, nir_block *b2);
1574 bool nir_block_dominates(nir_block *parent, nir_block *child);
1575
1576 void nir_dump_dom_tree_impl(nir_function_impl *impl, FILE *fp);
1577 void nir_dump_dom_tree(nir_shader *shader, FILE *fp);
1578
1579 void nir_dump_dom_frontier_impl(nir_function_impl *impl, FILE *fp);
1580 void nir_dump_dom_frontier(nir_shader *shader, FILE *fp);
1581
1582 void nir_dump_cfg_impl(nir_function_impl *impl, FILE *fp);
1583 void nir_dump_cfg(nir_shader *shader, FILE *fp);
1584
1585 void nir_split_var_copies(nir_shader *shader);
1586
1587 void nir_lower_var_copy_instr(nir_intrinsic_instr *copy, void *mem_ctx);
1588 void nir_lower_var_copies(nir_shader *shader);
1589
1590 void nir_lower_global_vars_to_local(nir_shader *shader);
1591
1592 void nir_lower_locals_to_regs(nir_shader *shader);
1593
1594 void nir_assign_var_locations_scalar(struct exec_list *var_list,
1595 unsigned *size);
1596 void nir_assign_var_locations_scalar_direct_first(nir_shader *shader,
1597 struct exec_list *var_list,
1598 unsigned *direct_size,
1599 unsigned *size);
1600
1601 void nir_lower_io(nir_shader *shader);
1602
1603 void nir_lower_vars_to_ssa(nir_shader *shader);
1604
1605 void nir_remove_dead_variables(nir_shader *shader);
1606
1607 void nir_lower_vec_to_movs(nir_shader *shader);
1608 void nir_lower_alu_to_scalar(nir_shader *shader);
1609
1610 void nir_lower_phis_to_scalar(nir_shader *shader);
1611
1612 void nir_lower_samplers(nir_shader *shader,
1613 struct gl_shader_program *shader_program,
1614 struct gl_program *prog);
1615
1616 void nir_lower_system_values(nir_shader *shader);
1617 void nir_lower_tex_projector(nir_shader *shader);
1618 void nir_lower_idiv(nir_shader *shader);
1619
1620 void nir_lower_atomics(nir_shader *shader);
1621 void nir_lower_to_source_mods(nir_shader *shader);
1622
1623 void nir_normalize_cubemap_coords(nir_shader *shader);
1624
1625 void nir_live_variables_impl(nir_function_impl *impl);
1626 bool nir_ssa_defs_interfere(nir_ssa_def *a, nir_ssa_def *b);
1627
1628 void nir_convert_to_ssa_impl(nir_function_impl *impl);
1629 void nir_convert_to_ssa(nir_shader *shader);
1630 void nir_convert_from_ssa(nir_shader *shader);
1631
1632 bool nir_opt_algebraic(nir_shader *shader);
1633 bool nir_opt_algebraic_late(nir_shader *shader);
1634 bool nir_opt_constant_folding(nir_shader *shader);
1635
1636 bool nir_opt_global_to_local(nir_shader *shader);
1637
1638 bool nir_copy_prop_impl(nir_function_impl *impl);
1639 bool nir_copy_prop(nir_shader *shader);
1640
1641 bool nir_opt_cse(nir_shader *shader);
1642
1643 bool nir_opt_dce_impl(nir_function_impl *impl);
1644 bool nir_opt_dce(nir_shader *shader);
1645
1646 void nir_opt_gcm(nir_shader *shader);
1647
1648 bool nir_opt_peephole_select(nir_shader *shader);
1649 bool nir_opt_peephole_ffma(nir_shader *shader);
1650
1651 bool nir_opt_remove_phis(nir_shader *shader);
1652
1653 void nir_sweep(nir_shader *shader);
1654
1655 #ifdef __cplusplus
1656 } /* extern "C" */
1657 #endif