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
[NIR_MAX_VEC_COMPONENTS
];
46 if (!instr
->dest
.dest
.is_ssa
)
49 /* In the case that any outputs/inputs have unsized types, then we need to
50 * guess the bit-size. In this case, the validator ensures that all
51 * bit-sizes match so we can just take the bit-size from first
52 * output/input with an unsized type. If all the outputs/inputs are sized
53 * then we don't need to guess the bit-size at all because the code we
54 * generate for constant opcodes in this case already knows the sizes of
55 * the types involved and does not need the provided bit-size for anything
56 * (although it still requires to receive a valid bit-size).
58 unsigned bit_size
= 0;
59 if (!nir_alu_type_get_type_size(nir_op_infos
[instr
->op
].output_type
))
60 bit_size
= instr
->dest
.dest
.ssa
.bit_size
;
62 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
63 if (!instr
->src
[i
].src
.is_ssa
)
67 !nir_alu_type_get_type_size(nir_op_infos
[instr
->op
].input_types
[i
]))
68 bit_size
= instr
->src
[i
].src
.ssa
->bit_size
;
70 nir_instr
*src_instr
= instr
->src
[i
].src
.ssa
->parent_instr
;
72 if (src_instr
->type
!= nir_instr_type_load_const
)
74 nir_load_const_instr
* load_const
= nir_instr_as_load_const(src_instr
);
76 for (unsigned j
= 0; j
< nir_ssa_alu_instr_src_components(instr
, i
);
78 switch(load_const
->def
.bit_size
) {
80 src
[i
].u64
[j
] = load_const
->value
.u64
[instr
->src
[i
].swizzle
[j
]];
83 src
[i
].u32
[j
] = load_const
->value
.u32
[instr
->src
[i
].swizzle
[j
]];
86 src
[i
].u16
[j
] = load_const
->value
.u16
[instr
->src
[i
].swizzle
[j
]];
89 src
[i
].u8
[j
] = load_const
->value
.u8
[instr
->src
[i
].swizzle
[j
]];
92 unreachable("Invalid bit size");
96 /* We shouldn't have any source modifiers in the optimization loop. */
97 assert(!instr
->src
[i
].abs
&& !instr
->src
[i
].negate
);
103 /* We shouldn't have any saturate modifiers in the optimization loop. */
104 assert(!instr
->dest
.saturate
);
106 nir_const_value dest
=
107 nir_eval_const_opcode(instr
->op
, instr
->dest
.dest
.ssa
.num_components
,
110 nir_load_const_instr
*new_instr
=
111 nir_load_const_instr_create(mem_ctx
,
112 instr
->dest
.dest
.ssa
.num_components
,
113 instr
->dest
.dest
.ssa
.bit_size
);
115 new_instr
->value
= dest
;
117 nir_instr_insert_before(&instr
->instr
, &new_instr
->instr
);
119 nir_ssa_def_rewrite_uses(&instr
->dest
.dest
.ssa
,
120 nir_src_for_ssa(&new_instr
->def
));
122 nir_instr_remove(&instr
->instr
);
129 constant_fold_intrinsic_instr(nir_intrinsic_instr
*instr
)
131 bool progress
= false;
133 if (instr
->intrinsic
== nir_intrinsic_discard_if
&&
134 nir_src_is_const(instr
->src
[0])) {
135 if (nir_src_as_bool(instr
->src
[0])) {
136 /* This method of getting a nir_shader * from a nir_instr is
137 * admittedly gross, but given the rarity of hitting this case I think
138 * it's preferable to plumbing an otherwise unused nir_shader *
139 * parameter through four functions to get here.
141 nir_cf_node
*cf_node
= &instr
->instr
.block
->cf_node
;
142 nir_function_impl
*impl
= nir_cf_node_get_function(cf_node
);
143 nir_shader
*shader
= impl
->function
->shader
;
145 nir_intrinsic_instr
*discard
=
146 nir_intrinsic_instr_create(shader
, nir_intrinsic_discard
);
147 nir_instr_insert_before(&instr
->instr
, &discard
->instr
);
148 nir_instr_remove(&instr
->instr
);
151 /* We're not discarding, just delete the instruction */
152 nir_instr_remove(&instr
->instr
);
161 constant_fold_block(nir_block
*block
, void *mem_ctx
)
163 bool progress
= false;
165 nir_foreach_instr_safe(instr
, block
) {
166 switch (instr
->type
) {
167 case nir_instr_type_alu
:
168 progress
|= constant_fold_alu_instr(nir_instr_as_alu(instr
), mem_ctx
);
170 case nir_instr_type_intrinsic
:
172 constant_fold_intrinsic_instr(nir_instr_as_intrinsic(instr
));
175 /* Don't know how to constant fold */
184 nir_opt_constant_folding_impl(nir_function_impl
*impl
)
186 void *mem_ctx
= ralloc_parent(impl
);
187 bool progress
= false;
189 nir_foreach_block(block
, impl
) {
190 progress
|= constant_fold_block(block
, mem_ctx
);
194 nir_metadata_preserve(impl
, nir_metadata_block_index
|
195 nir_metadata_dominance
);
201 nir_opt_constant_folding(nir_shader
*shader
)
203 bool progress
= false;
205 nir_foreach_function(function
, shader
) {
207 progress
|= nir_opt_constant_folding_impl(function
->impl
);