2 * Copyright © 2014-2015 Broadcom
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
27 #include "nir_control_flow.h"
31 typedef struct nir_builder {
35 nir_function_impl *impl;
39 nir_builder_init(nir_builder *build, nir_function_impl *impl)
41 memset(build, 0, sizeof(*build));
43 build->shader = impl->function->shader;
47 nir_builder_init_simple_shader(nir_builder *build, void *mem_ctx,
48 gl_shader_stage stage,
49 const nir_shader_compiler_options *options)
51 build->shader = nir_shader_create(mem_ctx, stage, options);
52 nir_function *func = nir_function_create(build->shader, "main");
53 build->impl = nir_function_impl_create(func);
54 build->cursor = nir_after_cf_list(&build->impl->body);
58 nir_builder_instr_insert(nir_builder *build, nir_instr *instr)
60 nir_instr_insert(build->cursor, instr);
62 /* Move the cursor forward. */
63 build->cursor = nir_after_instr(instr);
67 nir_builder_cf_insert(nir_builder *build, nir_cf_node *cf)
69 nir_cf_node_insert(build->cursor, cf);
72 static inline nir_ssa_def *
73 nir_build_imm(nir_builder *build, unsigned num_components, nir_const_value value)
75 nir_load_const_instr *load_const =
76 nir_load_const_instr_create(build->shader, num_components);
80 load_const->value = value;
82 nir_builder_instr_insert(build, &load_const->instr);
84 return &load_const->def;
87 static inline nir_ssa_def *
88 nir_imm_float(nir_builder *build, float x)
92 memset(&v, 0, sizeof(v));
95 return nir_build_imm(build, 1, v);
98 static inline nir_ssa_def *
99 nir_imm_vec4(nir_builder *build, float x, float y, float z, float w)
103 memset(&v, 0, sizeof(v));
109 return nir_build_imm(build, 4, v);
112 static inline nir_ssa_def *
113 nir_imm_int(nir_builder *build, int x)
117 memset(&v, 0, sizeof(v));
120 return nir_build_imm(build, 1, v);
123 static inline nir_ssa_def *
124 nir_imm_ivec4(nir_builder *build, int x, int y, int z, int w)
128 memset(&v, 0, sizeof(v));
134 return nir_build_imm(build, 4, v);
137 static inline nir_ssa_def *
138 nir_build_alu(nir_builder *build, nir_op op, nir_ssa_def *src0,
139 nir_ssa_def *src1, nir_ssa_def *src2, nir_ssa_def *src3)
141 const nir_op_info *op_info = &nir_op_infos[op];
142 nir_alu_instr *instr = nir_alu_instr_create(build->shader, op);
146 instr->src[0].src = nir_src_for_ssa(src0);
148 instr->src[1].src = nir_src_for_ssa(src1);
150 instr->src[2].src = nir_src_for_ssa(src2);
152 instr->src[3].src = nir_src_for_ssa(src3);
154 /* Guess the number of components the destination temporary should have
155 * based on our input sizes, if it's not fixed for the op.
157 unsigned num_components = op_info->output_size;
158 if (num_components == 0) {
159 for (unsigned i = 0; i < op_info->num_inputs; i++) {
160 if (op_info->input_sizes[i] == 0)
161 num_components = MAX2(num_components,
162 instr->src[i].src.ssa->num_components);
165 assert(num_components != 0);
167 /* Make sure we don't swizzle from outside of our source vector (like if a
168 * scalar value was passed into a multiply with a vector).
170 for (unsigned i = 0; i < op_info->num_inputs; i++) {
171 for (unsigned j = instr->src[i].src.ssa->num_components; j < 4; j++) {
172 instr->src[i].swizzle[j] = instr->src[i].src.ssa->num_components - 1;
176 nir_ssa_dest_init(&instr->instr, &instr->dest.dest, num_components, NULL);
177 instr->dest.write_mask = (1 << num_components) - 1;
179 nir_builder_instr_insert(build, &instr->instr);
181 return &instr->dest.dest.ssa;
185 static inline nir_ssa_def * \
186 nir_##op(nir_builder *build, nir_ssa_def *src0) \
188 return nir_build_alu(build, nir_op_##op, src0, NULL, NULL, NULL); \
192 static inline nir_ssa_def * \
193 nir_##op(nir_builder *build, nir_ssa_def *src0, nir_ssa_def *src1) \
195 return nir_build_alu(build, nir_op_##op, src0, src1, NULL, NULL); \
199 static inline nir_ssa_def * \
200 nir_##op(nir_builder *build, nir_ssa_def *src0, \
201 nir_ssa_def *src1, nir_ssa_def *src2) \
203 return nir_build_alu(build, nir_op_##op, src0, src1, src2, NULL); \
207 static inline nir_ssa_def * \
208 nir_##op(nir_builder *build, nir_ssa_def *src0, \
209 nir_ssa_def *src1, nir_ssa_def *src2, nir_ssa_def *src3) \
211 return nir_build_alu(build, nir_op_##op, src0, src1, src2, src3); \
214 #include "nir_builder_opcodes.h"
216 static inline nir_ssa_def *
217 nir_vec(nir_builder *build, nir_ssa_def **comp, unsigned num_components)
219 switch (num_components) {
221 return nir_vec4(build, comp[0], comp[1], comp[2], comp[3]);
223 return nir_vec3(build, comp[0], comp[1], comp[2]);
225 return nir_vec2(build, comp[0], comp[1]);
229 unreachable("bad component count");
235 * Similar to nir_fmov, but takes a nir_alu_src instead of a nir_ssa_def.
237 static inline nir_ssa_def *
238 nir_fmov_alu(nir_builder *build, nir_alu_src src, unsigned num_components)
240 nir_alu_instr *mov = nir_alu_instr_create(build->shader, nir_op_fmov);
241 nir_ssa_dest_init(&mov->instr, &mov->dest.dest, num_components, NULL);
242 mov->dest.write_mask = (1 << num_components) - 1;
244 nir_builder_instr_insert(build, &mov->instr);
246 return &mov->dest.dest.ssa;
249 static inline nir_ssa_def *
250 nir_imov_alu(nir_builder *build, nir_alu_src src, unsigned num_components)
252 nir_alu_instr *mov = nir_alu_instr_create(build->shader, nir_op_imov);
253 nir_ssa_dest_init(&mov->instr, &mov->dest.dest, num_components, NULL);
254 mov->dest.write_mask = (1 << num_components) - 1;
256 nir_builder_instr_insert(build, &mov->instr);
258 return &mov->dest.dest.ssa;
262 * Construct an fmov or imov that reswizzles the source's components.
264 static inline nir_ssa_def *
265 nir_swizzle(nir_builder *build, nir_ssa_def *src, unsigned swiz[4],
266 unsigned num_components, bool use_fmov)
268 nir_alu_src alu_src = { NIR_SRC_INIT };
269 alu_src.src = nir_src_for_ssa(src);
270 for (unsigned i = 0; i < num_components; i++)
271 alu_src.swizzle[i] = swiz[i];
273 return use_fmov ? nir_fmov_alu(build, alu_src, num_components) :
274 nir_imov_alu(build, alu_src, num_components);
277 static inline nir_ssa_def *
278 nir_channel(nir_builder *b, nir_ssa_def *def, unsigned c)
280 unsigned swizzle[4] = {c, c, c, c};
281 return nir_swizzle(b, def, swizzle, 1, false);
285 * Turns a nir_src into a nir_ssa_def * so it can be passed to
286 * nir_build_alu()-based builder calls.
288 * See nir_ssa_for_alu_src() for alu instructions.
290 static inline nir_ssa_def *
291 nir_ssa_for_src(nir_builder *build, nir_src src, int num_components)
293 if (src.is_ssa && src.ssa->num_components == num_components)
296 nir_alu_src alu = { NIR_SRC_INIT };
298 for (int j = 0; j < 4; j++)
301 return nir_imov_alu(build, alu, num_components);
305 * Similar to nir_ssa_for_src(), but for alu src's, respecting the
306 * nir_alu_src's swizzle.
308 static inline nir_ssa_def *
309 nir_ssa_for_alu_src(nir_builder *build, nir_alu_instr *instr, unsigned srcn)
311 static uint8_t trivial_swizzle[4] = { 0, 1, 2, 3 };
312 nir_alu_src *src = &instr->src[srcn];
313 unsigned num_components = nir_ssa_alu_instr_src_components(instr, srcn);
315 if (src->src.is_ssa && (src->src.ssa->num_components == num_components) &&
316 !src->abs && !src->negate &&
317 (memcmp(src->swizzle, trivial_swizzle, num_components) == 0))
320 return nir_imov_alu(build, *src, num_components);
323 static inline nir_ssa_def *
324 nir_load_var(nir_builder *build, nir_variable *var)
326 const unsigned num_components = glsl_get_vector_elements(var->type);
328 nir_intrinsic_instr *load =
329 nir_intrinsic_instr_create(build->shader, nir_intrinsic_load_var);
330 load->num_components = num_components;
331 load->variables[0] = nir_deref_var_create(load, var);
332 nir_ssa_dest_init(&load->instr, &load->dest, num_components, NULL);
333 nir_builder_instr_insert(build, &load->instr);
334 return &load->dest.ssa;
338 nir_store_var(nir_builder *build, nir_variable *var, nir_ssa_def *value,
341 const unsigned num_components = glsl_get_vector_elements(var->type);
343 nir_intrinsic_instr *store =
344 nir_intrinsic_instr_create(build->shader, nir_intrinsic_store_var);
345 store->num_components = num_components;
346 store->const_index[0] = writemask;
347 store->variables[0] = nir_deref_var_create(store, var);
348 store->src[0] = nir_src_for_ssa(value);
349 nir_builder_instr_insert(build, &store->instr);
352 static inline nir_ssa_def *
353 nir_load_system_value(nir_builder *build, nir_intrinsic_op op, int index)
355 nir_intrinsic_instr *load = nir_intrinsic_instr_create(build->shader, op);
356 load->num_components = nir_intrinsic_infos[op].dest_components;
357 load->const_index[0] = index;
358 nir_ssa_dest_init(&load->instr, &load->dest,
359 nir_intrinsic_infos[op].dest_components, NULL);
360 nir_builder_instr_insert(build, &load->instr);
361 return &load->dest.ssa;
364 #endif /* NIR_BUILDER_H */