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
->overload
->function
->shader
;
47 nir_builder_instr_insert(nir_builder
*build
, nir_instr
*instr
)
49 nir_instr_insert(build
->cursor
, instr
);
51 /* Move the cursor forward. */
52 build
->cursor
= nir_after_instr(instr
);
56 nir_builder_cf_insert(nir_builder
*build
, nir_cf_node
*cf
)
58 nir_cf_node_insert(build
->cursor
, cf
);
61 static inline nir_ssa_def
*
62 nir_build_imm(nir_builder
*build
, unsigned num_components
, nir_const_value value
)
64 nir_load_const_instr
*load_const
=
65 nir_load_const_instr_create(build
->shader
, num_components
);
69 load_const
->value
= value
;
71 nir_builder_instr_insert(build
, &load_const
->instr
);
73 return &load_const
->def
;
76 static inline nir_ssa_def
*
77 nir_imm_float(nir_builder
*build
, float x
)
81 memset(&v
, 0, sizeof(v
));
84 return nir_build_imm(build
, 1, v
);
87 static inline nir_ssa_def
*
88 nir_imm_vec4(nir_builder
*build
, float x
, float y
, float z
, float w
)
92 memset(&v
, 0, sizeof(v
));
98 return nir_build_imm(build
, 4, v
);
101 static inline nir_ssa_def
*
102 nir_imm_int(nir_builder
*build
, int x
)
106 memset(&v
, 0, sizeof(v
));
109 return nir_build_imm(build
, 1, v
);
112 static inline nir_ssa_def
*
113 nir_build_alu(nir_builder
*build
, nir_op op
, nir_ssa_def
*src0
,
114 nir_ssa_def
*src1
, nir_ssa_def
*src2
, nir_ssa_def
*src3
)
116 const nir_op_info
*op_info
= &nir_op_infos
[op
];
117 nir_alu_instr
*instr
= nir_alu_instr_create(build
->shader
, op
);
121 instr
->src
[0].src
= nir_src_for_ssa(src0
);
123 instr
->src
[1].src
= nir_src_for_ssa(src1
);
125 instr
->src
[2].src
= nir_src_for_ssa(src2
);
127 instr
->src
[3].src
= nir_src_for_ssa(src3
);
129 /* Guess the number of components the destination temporary should have
130 * based on our input sizes, if it's not fixed for the op.
132 unsigned num_components
= op_info
->output_size
;
133 if (num_components
== 0) {
134 for (unsigned i
= 0; i
< op_info
->num_inputs
; i
++) {
135 if (op_info
->input_sizes
[i
] == 0)
136 num_components
= MAX2(num_components
,
137 instr
->src
[i
].src
.ssa
->num_components
);
140 assert(num_components
!= 0);
142 /* Make sure we don't swizzle from outside of our source vector (like if a
143 * scalar value was passed into a multiply with a vector).
145 for (unsigned i
= 0; i
< op_info
->num_inputs
; i
++) {
146 for (unsigned j
= instr
->src
[i
].src
.ssa
->num_components
; j
< 4; j
++) {
147 instr
->src
[i
].swizzle
[j
] = instr
->src
[i
].src
.ssa
->num_components
- 1;
151 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
.dest
, num_components
, NULL
);
152 instr
->dest
.write_mask
= (1 << num_components
) - 1;
154 nir_builder_instr_insert(build
, &instr
->instr
);
156 return &instr
->dest
.dest
.ssa
;
160 static inline nir_ssa_def * \
161 nir_##op(nir_builder *build, nir_ssa_def *src0) \
163 return nir_build_alu(build, nir_op_##op, src0, NULL, NULL, NULL); \
167 static inline nir_ssa_def * \
168 nir_##op(nir_builder *build, nir_ssa_def *src0, nir_ssa_def *src1) \
170 return nir_build_alu(build, nir_op_##op, src0, src1, NULL, NULL); \
174 static inline nir_ssa_def * \
175 nir_##op(nir_builder *build, nir_ssa_def *src0, \
176 nir_ssa_def *src1, nir_ssa_def *src2) \
178 return nir_build_alu(build, nir_op_##op, src0, src1, src2, NULL); \
182 static inline nir_ssa_def * \
183 nir_##op(nir_builder *build, nir_ssa_def *src0, \
184 nir_ssa_def *src1, nir_ssa_def *src2, nir_ssa_def *src3) \
186 return nir_build_alu(build, nir_op_##op, src0, src1, src2, src3); \
189 #include "nir_builder_opcodes.h"
191 static inline nir_ssa_def
*
192 nir_vec(nir_builder
*build
, nir_ssa_def
**comp
, unsigned num_components
)
194 switch (num_components
) {
196 return nir_vec4(build
, comp
[0], comp
[1], comp
[2], comp
[3]);
198 return nir_vec3(build
, comp
[0], comp
[1], comp
[2]);
200 return nir_vec2(build
, comp
[0], comp
[1]);
204 unreachable("bad component count");
210 * Similar to nir_fmov, but takes a nir_alu_src instead of a nir_ssa_def.
212 static inline nir_ssa_def
*
213 nir_fmov_alu(nir_builder
*build
, nir_alu_src src
, unsigned num_components
)
215 nir_alu_instr
*mov
= nir_alu_instr_create(build
->shader
, nir_op_fmov
);
216 nir_ssa_dest_init(&mov
->instr
, &mov
->dest
.dest
, num_components
, NULL
);
217 mov
->dest
.write_mask
= (1 << num_components
) - 1;
219 nir_builder_instr_insert(build
, &mov
->instr
);
221 return &mov
->dest
.dest
.ssa
;
224 static inline nir_ssa_def
*
225 nir_imov_alu(nir_builder
*build
, nir_alu_src src
, unsigned num_components
)
227 nir_alu_instr
*mov
= nir_alu_instr_create(build
->shader
, nir_op_imov
);
228 nir_ssa_dest_init(&mov
->instr
, &mov
->dest
.dest
, num_components
, NULL
);
229 mov
->dest
.write_mask
= (1 << num_components
) - 1;
231 nir_builder_instr_insert(build
, &mov
->instr
);
233 return &mov
->dest
.dest
.ssa
;
237 * Construct an fmov or imov that reswizzles the source's components.
239 static inline nir_ssa_def
*
240 nir_swizzle(nir_builder
*build
, nir_ssa_def
*src
, unsigned swiz
[4],
241 unsigned num_components
, bool use_fmov
)
243 nir_alu_src alu_src
= { NIR_SRC_INIT
};
244 alu_src
.src
= nir_src_for_ssa(src
);
245 for (int i
= 0; i
< 4; i
++)
246 alu_src
.swizzle
[i
] = swiz
[i
];
248 return use_fmov
? nir_fmov_alu(build
, alu_src
, num_components
) :
249 nir_imov_alu(build
, alu_src
, num_components
);
252 /* Selects the right fdot given the number of components in each source. */
253 static inline nir_ssa_def
*
254 nir_fdot(nir_builder
*build
, nir_ssa_def
*src0
, nir_ssa_def
*src1
)
256 assert(src0
->num_components
== src1
->num_components
);
257 switch (src0
->num_components
) {
258 case 1: return nir_fmul(build
, src0
, src1
);
259 case 2: return nir_fdot2(build
, src0
, src1
);
260 case 3: return nir_fdot3(build
, src0
, src1
);
261 case 4: return nir_fdot4(build
, src0
, src1
);
263 unreachable("bad component size");
269 static inline nir_ssa_def
*
270 nir_channel(nir_builder
*b
, nir_ssa_def
*def
, unsigned c
)
272 unsigned swizzle
[4] = {c
, c
, c
, c
};
273 return nir_swizzle(b
, def
, swizzle
, 1, false);
277 * Turns a nir_src into a nir_ssa_def * so it can be passed to
278 * nir_build_alu()-based builder calls.
280 static inline nir_ssa_def
*
281 nir_ssa_for_src(nir_builder
*build
, nir_src src
, int num_components
)
283 if (src
.is_ssa
&& src
.ssa
->num_components
== num_components
)
286 nir_alu_src alu
= { NIR_SRC_INIT
};
288 for (int j
= 0; j
< 4; j
++)
291 return nir_imov_alu(build
, alu
, num_components
);
294 static inline nir_ssa_def
*
295 nir_load_var(nir_builder
*build
, nir_variable
*var
)
297 const unsigned num_components
= glsl_get_vector_elements(var
->type
);
299 nir_intrinsic_instr
*load
=
300 nir_intrinsic_instr_create(build
->shader
, nir_intrinsic_load_var
);
301 load
->num_components
= num_components
;
302 load
->variables
[0] = nir_deref_var_create(load
, var
);
303 nir_ssa_dest_init(&load
->instr
, &load
->dest
, num_components
, NULL
);
304 nir_builder_instr_insert(build
, &load
->instr
);
305 return &load
->dest
.ssa
;
309 nir_store_var(nir_builder
*build
, nir_variable
*var
, nir_ssa_def
*value
)
311 const unsigned num_components
= glsl_get_vector_elements(var
->type
);
313 nir_intrinsic_instr
*store
=
314 nir_intrinsic_instr_create(build
->shader
, nir_intrinsic_store_var
);
315 store
->num_components
= num_components
;
316 store
->variables
[0] = nir_deref_var_create(store
, var
);
317 store
->src
[0] = nir_src_for_ssa(value
);
318 nir_builder_instr_insert(build
, &store
->instr
);
321 #endif /* NIR_BUILDER_H */