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
{
34 /* Whether new ALU instructions will be marked "exact" */
38 nir_function_impl
*impl
;
42 nir_builder_init(nir_builder
*build
, nir_function_impl
*impl
)
44 memset(build
, 0, sizeof(*build
));
47 build
->shader
= impl
->function
->shader
;
51 nir_builder_init_simple_shader(nir_builder
*build
, void *mem_ctx
,
52 gl_shader_stage stage
,
53 const nir_shader_compiler_options
*options
)
55 build
->shader
= nir_shader_create(mem_ctx
, stage
, options
);
56 nir_function
*func
= nir_function_create(build
->shader
, "main");
58 build
->impl
= nir_function_impl_create(func
);
59 build
->cursor
= nir_after_cf_list(&build
->impl
->body
);
63 nir_builder_instr_insert(nir_builder
*build
, nir_instr
*instr
)
65 nir_instr_insert(build
->cursor
, instr
);
67 /* Move the cursor forward. */
68 build
->cursor
= nir_after_instr(instr
);
72 nir_builder_cf_insert(nir_builder
*build
, nir_cf_node
*cf
)
74 nir_cf_node_insert(build
->cursor
, cf
);
77 static inline nir_ssa_def
*
78 nir_ssa_undef(nir_builder
*build
, unsigned num_components
, unsigned bit_size
)
80 nir_ssa_undef_instr
*undef
=
81 nir_ssa_undef_instr_create(build
->shader
, num_components
, bit_size
);
85 nir_instr_insert(nir_before_cf_list(&build
->impl
->body
), &undef
->instr
);
90 static inline nir_ssa_def
*
91 nir_build_imm(nir_builder
*build
, unsigned num_components
,
92 unsigned bit_size
, nir_const_value value
)
94 nir_load_const_instr
*load_const
=
95 nir_load_const_instr_create(build
->shader
, num_components
, bit_size
);
99 load_const
->value
= value
;
101 nir_builder_instr_insert(build
, &load_const
->instr
);
103 return &load_const
->def
;
106 static inline nir_ssa_def
*
107 nir_imm_float(nir_builder
*build
, float x
)
111 memset(&v
, 0, sizeof(v
));
114 return nir_build_imm(build
, 1, 32, v
);
117 static inline nir_ssa_def
*
118 nir_imm_double(nir_builder
*build
, double x
)
122 memset(&v
, 0, sizeof(v
));
125 return nir_build_imm(build
, 1, 64, v
);
128 static inline nir_ssa_def
*
129 nir_imm_vec4(nir_builder
*build
, float x
, float y
, float z
, float w
)
133 memset(&v
, 0, sizeof(v
));
139 return nir_build_imm(build
, 4, 32, v
);
142 static inline nir_ssa_def
*
143 nir_imm_int(nir_builder
*build
, int x
)
147 memset(&v
, 0, sizeof(v
));
150 return nir_build_imm(build
, 1, 32, v
);
153 static inline nir_ssa_def
*
154 nir_imm_ivec4(nir_builder
*build
, int x
, int y
, int z
, int w
)
158 memset(&v
, 0, sizeof(v
));
164 return nir_build_imm(build
, 4, 32, v
);
167 static inline nir_ssa_def
*
168 nir_build_alu(nir_builder
*build
, nir_op op
, nir_ssa_def
*src0
,
169 nir_ssa_def
*src1
, nir_ssa_def
*src2
, nir_ssa_def
*src3
)
171 const nir_op_info
*op_info
= &nir_op_infos
[op
];
172 nir_alu_instr
*instr
= nir_alu_instr_create(build
->shader
, op
);
176 instr
->exact
= build
->exact
;
178 instr
->src
[0].src
= nir_src_for_ssa(src0
);
180 instr
->src
[1].src
= nir_src_for_ssa(src1
);
182 instr
->src
[2].src
= nir_src_for_ssa(src2
);
184 instr
->src
[3].src
= nir_src_for_ssa(src3
);
186 /* Guess the number of components the destination temporary should have
187 * based on our input sizes, if it's not fixed for the op.
189 unsigned num_components
= op_info
->output_size
;
190 if (num_components
== 0) {
191 for (unsigned i
= 0; i
< op_info
->num_inputs
; i
++) {
192 if (op_info
->input_sizes
[i
] == 0)
193 num_components
= MAX2(num_components
,
194 instr
->src
[i
].src
.ssa
->num_components
);
197 assert(num_components
!= 0);
199 /* Figure out the bitwidth based on the source bitwidth if the instruction
202 unsigned bit_size
= nir_alu_type_get_type_size(op_info
->output_type
);
204 for (unsigned i
= 0; i
< op_info
->num_inputs
; i
++) {
205 unsigned src_bit_size
= instr
->src
[i
].src
.ssa
->bit_size
;
206 if (nir_alu_type_get_type_size(op_info
->input_types
[i
]) == 0) {
208 assert(src_bit_size
== bit_size
);
210 bit_size
= src_bit_size
;
212 assert(src_bit_size
==
213 nir_alu_type_get_type_size(op_info
->input_types
[i
]));
218 /* Make sure we don't swizzle from outside of our source vector (like if a
219 * scalar value was passed into a multiply with a vector).
221 for (unsigned i
= 0; i
< op_info
->num_inputs
; i
++) {
222 for (unsigned j
= instr
->src
[i
].src
.ssa
->num_components
; j
< 4; j
++) {
223 instr
->src
[i
].swizzle
[j
] = instr
->src
[i
].src
.ssa
->num_components
- 1;
227 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
.dest
, num_components
,
229 instr
->dest
.write_mask
= (1 << num_components
) - 1;
231 nir_builder_instr_insert(build
, &instr
->instr
);
233 return &instr
->dest
.dest
.ssa
;
236 #include "nir_builder_opcodes.h"
238 static inline nir_ssa_def
*
239 nir_vec(nir_builder
*build
, nir_ssa_def
**comp
, unsigned num_components
)
241 switch (num_components
) {
243 return nir_vec4(build
, comp
[0], comp
[1], comp
[2], comp
[3]);
245 return nir_vec3(build
, comp
[0], comp
[1], comp
[2]);
247 return nir_vec2(build
, comp
[0], comp
[1]);
251 unreachable("bad component count");
257 * Similar to nir_fmov, but takes a nir_alu_src instead of a nir_ssa_def.
259 static inline nir_ssa_def
*
260 nir_fmov_alu(nir_builder
*build
, nir_alu_src src
, unsigned num_components
)
262 nir_alu_instr
*mov
= nir_alu_instr_create(build
->shader
, nir_op_fmov
);
263 nir_ssa_dest_init(&mov
->instr
, &mov
->dest
.dest
, num_components
,
264 nir_src_bit_size(src
.src
), NULL
);
265 mov
->exact
= build
->exact
;
266 mov
->dest
.write_mask
= (1 << num_components
) - 1;
268 nir_builder_instr_insert(build
, &mov
->instr
);
270 return &mov
->dest
.dest
.ssa
;
273 static inline nir_ssa_def
*
274 nir_imov_alu(nir_builder
*build
, nir_alu_src src
, unsigned num_components
)
276 nir_alu_instr
*mov
= nir_alu_instr_create(build
->shader
, nir_op_imov
);
277 nir_ssa_dest_init(&mov
->instr
, &mov
->dest
.dest
, num_components
,
278 nir_src_bit_size(src
.src
), NULL
);
279 mov
->exact
= build
->exact
;
280 mov
->dest
.write_mask
= (1 << num_components
) - 1;
282 nir_builder_instr_insert(build
, &mov
->instr
);
284 return &mov
->dest
.dest
.ssa
;
288 * Construct an fmov or imov that reswizzles the source's components.
290 static inline nir_ssa_def
*
291 nir_swizzle(nir_builder
*build
, nir_ssa_def
*src
, const unsigned swiz
[4],
292 unsigned num_components
, bool use_fmov
)
294 nir_alu_src alu_src
= { NIR_SRC_INIT
};
295 alu_src
.src
= nir_src_for_ssa(src
);
296 for (unsigned i
= 0; i
< num_components
; i
++)
297 alu_src
.swizzle
[i
] = swiz
[i
];
299 return use_fmov
? nir_fmov_alu(build
, alu_src
, num_components
) :
300 nir_imov_alu(build
, alu_src
, num_components
);
303 /* Selects the right fdot given the number of components in each source. */
304 static inline nir_ssa_def
*
305 nir_fdot(nir_builder
*build
, nir_ssa_def
*src0
, nir_ssa_def
*src1
)
307 assert(src0
->num_components
== src1
->num_components
);
308 switch (src0
->num_components
) {
309 case 1: return nir_fmul(build
, src0
, src1
);
310 case 2: return nir_fdot2(build
, src0
, src1
);
311 case 3: return nir_fdot3(build
, src0
, src1
);
312 case 4: return nir_fdot4(build
, src0
, src1
);
314 unreachable("bad component size");
320 static inline nir_ssa_def
*
321 nir_channel(nir_builder
*b
, nir_ssa_def
*def
, unsigned c
)
323 unsigned swizzle
[4] = {c
, c
, c
, c
};
324 return nir_swizzle(b
, def
, swizzle
, 1, false);
327 static inline nir_ssa_def
*
328 nir_channels(nir_builder
*b
, nir_ssa_def
*def
, unsigned mask
)
330 unsigned num_channels
= 0, swizzle
[4] = { 0, 0, 0, 0 };
332 for (unsigned i
= 0; i
< 4; i
++) {
333 if ((mask
& (1 << i
)) == 0)
335 swizzle
[num_channels
++] = i
;
338 return nir_swizzle(b
, def
, swizzle
, num_channels
, false);
342 * Turns a nir_src into a nir_ssa_def * so it can be passed to
343 * nir_build_alu()-based builder calls.
345 * See nir_ssa_for_alu_src() for alu instructions.
347 static inline nir_ssa_def
*
348 nir_ssa_for_src(nir_builder
*build
, nir_src src
, int num_components
)
350 if (src
.is_ssa
&& src
.ssa
->num_components
== num_components
)
353 nir_alu_src alu
= { NIR_SRC_INIT
};
355 for (int j
= 0; j
< 4; j
++)
358 return nir_imov_alu(build
, alu
, num_components
);
362 * Similar to nir_ssa_for_src(), but for alu src's, respecting the
363 * nir_alu_src's swizzle.
365 static inline nir_ssa_def
*
366 nir_ssa_for_alu_src(nir_builder
*build
, nir_alu_instr
*instr
, unsigned srcn
)
368 static uint8_t trivial_swizzle
[4] = { 0, 1, 2, 3 };
369 nir_alu_src
*src
= &instr
->src
[srcn
];
370 unsigned num_components
= nir_ssa_alu_instr_src_components(instr
, srcn
);
372 if (src
->src
.is_ssa
&& (src
->src
.ssa
->num_components
== num_components
) &&
373 !src
->abs
&& !src
->negate
&&
374 (memcmp(src
->swizzle
, trivial_swizzle
, num_components
) == 0))
377 return nir_imov_alu(build
, *src
, num_components
);
380 static inline nir_ssa_def
*
381 nir_load_var(nir_builder
*build
, nir_variable
*var
)
383 const unsigned num_components
= glsl_get_vector_elements(var
->type
);
385 nir_intrinsic_instr
*load
=
386 nir_intrinsic_instr_create(build
->shader
, nir_intrinsic_load_var
);
387 load
->num_components
= num_components
;
388 load
->variables
[0] = nir_deref_var_create(load
, var
);
389 nir_ssa_dest_init(&load
->instr
, &load
->dest
, num_components
,
390 glsl_get_bit_size(var
->type
), NULL
);
391 nir_builder_instr_insert(build
, &load
->instr
);
392 return &load
->dest
.ssa
;
396 nir_store_var(nir_builder
*build
, nir_variable
*var
, nir_ssa_def
*value
,
399 const unsigned num_components
= glsl_get_vector_elements(var
->type
);
401 nir_intrinsic_instr
*store
=
402 nir_intrinsic_instr_create(build
->shader
, nir_intrinsic_store_var
);
403 store
->num_components
= num_components
;
404 nir_intrinsic_set_write_mask(store
, writemask
);
405 store
->variables
[0] = nir_deref_var_create(store
, var
);
406 store
->src
[0] = nir_src_for_ssa(value
);
407 nir_builder_instr_insert(build
, &store
->instr
);
411 nir_store_deref_var(nir_builder
*build
, nir_deref_var
*deref
,
412 nir_ssa_def
*value
, unsigned writemask
)
414 const unsigned num_components
=
415 glsl_get_vector_elements(nir_deref_tail(&deref
->deref
)->type
);
417 nir_intrinsic_instr
*store
=
418 nir_intrinsic_instr_create(build
->shader
, nir_intrinsic_store_var
);
419 store
->num_components
= num_components
;
420 store
->const_index
[0] = writemask
& ((1 << num_components
) - 1);
421 store
->variables
[0] = nir_deref_as_var(nir_copy_deref(store
, &deref
->deref
));
422 store
->src
[0] = nir_src_for_ssa(value
);
423 nir_builder_instr_insert(build
, &store
->instr
);
427 nir_copy_deref_var(nir_builder
*build
, nir_deref_var
*dest
, nir_deref_var
*src
)
429 assert(nir_deref_tail(&dest
->deref
)->type
==
430 nir_deref_tail(&src
->deref
)->type
);
432 nir_intrinsic_instr
*copy
=
433 nir_intrinsic_instr_create(build
->shader
, nir_intrinsic_copy_var
);
434 copy
->variables
[0] = nir_deref_as_var(nir_copy_deref(copy
, &dest
->deref
));
435 copy
->variables
[1] = nir_deref_as_var(nir_copy_deref(copy
, &src
->deref
));
436 nir_builder_instr_insert(build
, ©
->instr
);
440 nir_copy_var(nir_builder
*build
, nir_variable
*dest
, nir_variable
*src
)
442 nir_intrinsic_instr
*copy
=
443 nir_intrinsic_instr_create(build
->shader
, nir_intrinsic_copy_var
);
444 copy
->variables
[0] = nir_deref_var_create(copy
, dest
);
445 copy
->variables
[1] = nir_deref_var_create(copy
, src
);
446 nir_builder_instr_insert(build
, ©
->instr
);
449 static inline nir_ssa_def
*
450 nir_load_system_value(nir_builder
*build
, nir_intrinsic_op op
, int index
)
452 nir_intrinsic_instr
*load
= nir_intrinsic_instr_create(build
->shader
, op
);
453 load
->num_components
= nir_intrinsic_infos
[op
].dest_components
;
454 load
->const_index
[0] = index
;
455 nir_ssa_dest_init(&load
->instr
, &load
->dest
,
456 nir_intrinsic_infos
[op
].dest_components
, 32, NULL
);
457 nir_builder_instr_insert(build
, &load
->instr
);
458 return &load
->dest
.ssa
;
462 nir_jump(nir_builder
*build
, nir_jump_type jump_type
)
464 nir_jump_instr
*jump
= nir_jump_instr_create(build
->shader
, jump_type
);
465 nir_builder_instr_insert(build
, &jump
->instr
);
468 #endif /* NIR_BUILDER_H */