2 * Copyright © 2014 Intel Corporation
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:
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
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
24 * Jason Ekstrand (jason@jlekstrand.net)
28 #include "nir_constant_expressions.h"
32 * Implements SSA-based constant folding.
35 struct constant_fold_state
{
37 nir_function_impl
*impl
;
42 constant_fold_alu_instr(nir_alu_instr
*instr
, void *mem_ctx
)
44 nir_const_value src
[4];
46 if (!instr
->dest
.dest
.is_ssa
)
49 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
50 if (!instr
->src
[i
].src
.is_ssa
)
53 nir_instr
*src_instr
= instr
->src
[i
].src
.ssa
->parent_instr
;
55 if (src_instr
->type
!= nir_instr_type_load_const
)
57 nir_load_const_instr
* load_const
= nir_instr_as_load_const(src_instr
);
59 for (unsigned j
= 0; j
< nir_ssa_alu_instr_src_components(instr
, i
);
61 src
[i
].u
[j
] = load_const
->value
.u
[instr
->src
[i
].swizzle
[j
]];
64 /* We shouldn't have any source modifiers in the optimization loop. */
65 assert(!instr
->src
[i
].abs
&& !instr
->src
[i
].negate
);
68 /* We shouldn't have any saturate modifiers in the optimization loop. */
69 assert(!instr
->dest
.saturate
);
71 nir_const_value dest
=
72 nir_eval_const_opcode(instr
->op
, instr
->dest
.dest
.ssa
.num_components
,
75 nir_load_const_instr
*new_instr
=
76 nir_load_const_instr_create(mem_ctx
,
77 instr
->dest
.dest
.ssa
.num_components
);
79 new_instr
->value
= dest
;
81 nir_instr_insert_before(&instr
->instr
, &new_instr
->instr
);
83 nir_ssa_def_rewrite_uses(&instr
->dest
.dest
.ssa
,
84 nir_src_for_ssa(&new_instr
->def
));
86 nir_instr_remove(&instr
->instr
);
93 constant_fold_deref(nir_instr
*instr
, nir_deref_var
*deref
)
95 bool progress
= false;
97 for (nir_deref
*tail
= deref
->deref
.child
; tail
; tail
= tail
->child
) {
98 if (tail
->deref_type
!= nir_deref_type_array
)
101 nir_deref_array
*arr
= nir_deref_as_array(tail
);
103 if (arr
->deref_array_type
== nir_deref_array_type_indirect
&&
104 arr
->indirect
.is_ssa
&&
105 arr
->indirect
.ssa
->parent_instr
->type
== nir_instr_type_load_const
) {
106 nir_load_const_instr
*indirect
=
107 nir_instr_as_load_const(arr
->indirect
.ssa
->parent_instr
);
109 arr
->base_offset
+= indirect
->value
.u
[0];
111 /* Clear out the source */
112 nir_instr_rewrite_src(instr
, &arr
->indirect
, nir_src_for_ssa(NULL
));
114 arr
->deref_array_type
= nir_deref_array_type_direct
;
124 constant_fold_intrinsic_instr(nir_intrinsic_instr
*instr
)
126 bool progress
= false;
128 unsigned num_vars
= nir_intrinsic_infos
[instr
->intrinsic
].num_variables
;
129 for (unsigned i
= 0; i
< num_vars
; i
++) {
130 progress
|= constant_fold_deref(&instr
->instr
, instr
->variables
[i
]);
137 constant_fold_tex_instr(nir_tex_instr
*instr
)
139 bool progress
= false;
142 progress
|= constant_fold_deref(&instr
->instr
, instr
->texture
);
145 progress
|= constant_fold_deref(&instr
->instr
, instr
->sampler
);
151 constant_fold_block(nir_block
*block
, void *void_state
)
153 struct constant_fold_state
*state
= void_state
;
155 nir_foreach_instr_safe(block
, instr
) {
156 switch (instr
->type
) {
157 case nir_instr_type_alu
:
158 state
->progress
|= constant_fold_alu_instr(nir_instr_as_alu(instr
),
161 case nir_instr_type_intrinsic
:
163 constant_fold_intrinsic_instr(nir_instr_as_intrinsic(instr
));
165 case nir_instr_type_tex
:
166 state
->progress
|= constant_fold_tex_instr(nir_instr_as_tex(instr
));
169 /* Don't know how to constant fold */
178 nir_opt_constant_folding_impl(nir_function_impl
*impl
)
180 struct constant_fold_state state
;
182 state
.mem_ctx
= ralloc_parent(impl
);
184 state
.progress
= false;
186 nir_foreach_block(impl
, constant_fold_block
, &state
);
189 nir_metadata_preserve(impl
, nir_metadata_block_index
|
190 nir_metadata_dominance
);
192 return state
.progress
;
196 nir_opt_constant_folding(nir_shader
*shader
)
198 bool progress
= false;
200 nir_foreach_function(shader
, function
) {
202 progress
|= nir_opt_constant_folding_impl(function
->impl
);