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
29 typedef struct nir_builder
{
30 struct exec_list
*cf_node_list
;
32 nir_function_impl
*impl
;
36 nir_builder_init(nir_builder
*build
, nir_function_impl
*impl
)
38 memset(build
, 0, sizeof(*build
));
40 build
->shader
= impl
->overload
->function
->shader
;
44 nir_builder_insert_after_cf_list(nir_builder
*build
,
45 struct exec_list
*cf_node_list
)
47 build
->cf_node_list
= cf_node_list
;
51 static inline nir_ssa_def
*
52 nir_build_alu(nir_builder
*build
, nir_op op
, nir_ssa_def
*src0
,
53 nir_ssa_def
*src1
, nir_ssa_def
*src2
, nir_ssa_def
*src3
)
55 const nir_op_info
*op_info
= &nir_op_infos
[op
];
56 nir_alu_instr
*instr
= nir_alu_instr_create(build
->shader
, op
);
60 instr
->src
[0].src
= nir_src_for_ssa(src0
);
62 instr
->src
[1].src
= nir_src_for_ssa(src1
);
64 instr
->src
[2].src
= nir_src_for_ssa(src2
);
66 instr
->src
[3].src
= nir_src_for_ssa(src3
);
68 /* Guess the number of components the destination temporary should have
69 * based on our input sizes, if it's not fixed for the op.
71 unsigned num_components
= op_info
->output_size
;
72 if (num_components
== 0) {
73 for (unsigned i
= 0; i
< op_info
->num_inputs
; i
++) {
74 if (op_info
->input_sizes
[i
] == 0)
75 num_components
= MAX2(num_components
,
76 instr
->src
[i
].src
.ssa
->num_components
);
79 assert(num_components
!= 0);
81 /* Make sure we don't swizzle from outside of our source vector (like if a
82 * scalar value was passed into a multiply with a vector).
84 for (unsigned i
= 0; i
< op_info
->num_inputs
; i
++) {
85 for (unsigned j
= instr
->src
[i
].src
.ssa
->num_components
; j
< 4; j
++) {
86 instr
->src
[i
].swizzle
[j
] = instr
->src
[i
].src
.ssa
->num_components
- 1;
90 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
.dest
, num_components
, NULL
);
91 instr
->dest
.write_mask
= (1 << num_components
) - 1;
93 nir_instr_insert_after_cf_list(build
->cf_node_list
, &instr
->instr
);
95 return &instr
->dest
.dest
.ssa
;
99 static inline nir_ssa_def * \
100 nir_##op(nir_builder *build, nir_ssa_def *src0) \
102 return nir_build_alu(build, nir_op_##op, src0, NULL, NULL, NULL); \
106 static inline nir_ssa_def * \
107 nir_##op(nir_builder *build, nir_ssa_def *src0, nir_ssa_def *src1) \
109 return nir_build_alu(build, nir_op_##op, src0, src1, NULL, NULL); \
113 static inline nir_ssa_def * \
114 nir_##op(nir_builder *build, nir_ssa_def *src0, \
115 nir_ssa_def *src1, nir_ssa_def *src2) \
117 return nir_build_alu(build, nir_op_##op, src0, src1, src2, NULL); \
121 static inline nir_ssa_def * \
122 nir_##op(nir_builder *build, nir_ssa_def *src0, \
123 nir_ssa_def *src1, nir_ssa_def *src2, nir_ssa_def *src3) \
125 return nir_build_alu(build, nir_op_##op, src0, src1, src2, src3); \
128 #include "nir_builder_opcodes.h"
130 #endif /* NIR_BUILDER_H */