2 * Copyright © 2010 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
21 * DEALINGS IN THE SOFTWARE.
25 * \file opt_algebraic.cpp
27 * Takes advantage of association, commutivity, and other algebraic
28 * properties to simplify expressions.
32 #include "ir_visitor.h"
33 #include "ir_rvalue_visitor.h"
34 #include "ir_optimization.h"
35 #include "glsl_types.h"
38 * Visitor class for replacing expressions with ir_constant values.
41 class ir_algebraic_visitor : public ir_rvalue_visitor {
43 ir_algebraic_visitor()
45 this->progress = false;
49 virtual ~ir_algebraic_visitor()
53 ir_rvalue *handle_expression(ir_expression *ir);
54 void handle_rvalue(ir_rvalue **rvalue);
55 bool reassociate_constant(ir_expression *ir1,
57 ir_constant *constant,
59 void reassociate_operands(ir_expression *ir1,
63 ir_rvalue *swizzle_if_required(ir_expression *expr,
72 is_vec_zero(ir_constant *ir)
74 return (ir == NULL) ? false : ir->is_zero();
78 is_vec_one(ir_constant *ir)
80 return (ir == NULL) ? false : ir->is_one();
84 update_type(ir_expression *ir)
86 if (ir->operands[0]->type->is_vector())
87 ir->type = ir->operands[0]->type;
89 ir->type = ir->operands[1]->type;
93 ir_algebraic_visitor::reassociate_operands(ir_expression *ir1,
98 ir_rvalue *temp = ir2->operands[op2];
99 ir2->operands[op2] = ir1->operands[op1];
100 ir1->operands[op1] = temp;
102 /* Update the type of ir2. The type of ir1 won't have changed --
103 * base types matched, and at least one of the operands of the 2
104 * binops is still a vector if any of them were.
108 this->progress = true;
112 * Reassociates a constant down a tree of adds or multiplies.
114 * Consider (2 * (a * (b * 0.5))). We want to send up with a * b.
117 ir_algebraic_visitor::reassociate_constant(ir_expression *ir1, int const_index,
118 ir_constant *constant,
121 if (!ir2 || ir1->operation != ir2->operation)
124 /* Don't want to even think about matrices. */
125 if (ir1->operands[0]->type->is_matrix() ||
126 ir1->operands[1]->type->is_matrix() ||
127 ir2->operands[0]->type->is_matrix() ||
128 ir2->operands[1]->type->is_matrix())
131 ir_constant *ir2_const[2];
132 ir2_const[0] = ir2->operands[0]->constant_expression_value();
133 ir2_const[1] = ir2->operands[1]->constant_expression_value();
135 if (ir2_const[0] && ir2_const[1])
139 reassociate_operands(ir1, const_index, ir2, 1);
141 } else if (ir2_const[1]) {
142 reassociate_operands(ir1, const_index, ir2, 0);
146 if (reassociate_constant(ir1, const_index, constant,
147 ir2->operands[0]->as_expression())) {
152 if (reassociate_constant(ir1, const_index, constant,
153 ir2->operands[1]->as_expression())) {
161 /* When eliminating an expression and just returning one of its operands,
162 * we may need to swizzle that operand out to a vector if the expression was
166 ir_algebraic_visitor::swizzle_if_required(ir_expression *expr,
169 if (expr->type->is_vector() && operand->type->is_scalar()) {
170 return new(mem_ctx) ir_swizzle(operand, 0, 0, 0, 0,
171 expr->type->vector_elements);
177 ir_algebraic_visitor::handle_expression(ir_expression *ir)
179 ir_constant *op_const[2] = {NULL, NULL};
180 ir_expression *op_expr[2] = {NULL, NULL};
184 assert(ir->get_num_operands() <= 2);
185 for (i = 0; i < ir->get_num_operands(); i++) {
186 if (ir->operands[i]->type->is_matrix())
189 op_const[i] = ir->operands[i]->constant_expression_value();
190 op_expr[i] = ir->operands[i]->as_expression();
193 if (this->mem_ctx == NULL)
194 this->mem_ctx = ralloc_parent(ir);
196 switch (ir->operation) {
197 case ir_unop_logic_not: {
198 enum ir_expression_operation new_op = ir_unop_logic_not;
200 if (op_expr[0] == NULL)
203 switch (op_expr[0]->operation) {
204 case ir_binop_less: new_op = ir_binop_gequal; break;
205 case ir_binop_greater: new_op = ir_binop_lequal; break;
206 case ir_binop_lequal: new_op = ir_binop_greater; break;
207 case ir_binop_gequal: new_op = ir_binop_less; break;
208 case ir_binop_equal: new_op = ir_binop_nequal; break;
209 case ir_binop_nequal: new_op = ir_binop_equal; break;
210 case ir_binop_all_equal: new_op = ir_binop_any_nequal; break;
211 case ir_binop_any_nequal: new_op = ir_binop_all_equal; break;
214 /* The default case handler is here to silence a warning from GCC.
219 if (new_op != ir_unop_logic_not) {
220 this->progress = true;
221 return new(mem_ctx) ir_expression(new_op,
223 op_expr[0]->operands[0],
224 op_expr[0]->operands[1]);
231 if (is_vec_zero(op_const[0])) {
232 this->progress = true;
233 return swizzle_if_required(ir, ir->operands[1]);
235 if (is_vec_zero(op_const[1])) {
236 this->progress = true;
237 return swizzle_if_required(ir, ir->operands[0]);
240 /* Reassociate addition of constants so that we can do constant
243 if (op_const[0] && !op_const[1])
244 reassociate_constant(ir, 0, op_const[0],
245 ir->operands[1]->as_expression());
246 if (op_const[1] && !op_const[0])
247 reassociate_constant(ir, 1, op_const[1],
248 ir->operands[0]->as_expression());
252 if (is_vec_zero(op_const[0])) {
253 this->progress = true;
254 temp = new(mem_ctx) ir_expression(ir_unop_neg,
255 ir->operands[1]->type,
258 return swizzle_if_required(ir, temp);
260 if (is_vec_zero(op_const[1])) {
261 this->progress = true;
262 return swizzle_if_required(ir, ir->operands[0]);
267 if (is_vec_one(op_const[0])) {
268 this->progress = true;
269 return swizzle_if_required(ir, ir->operands[1]);
271 if (is_vec_one(op_const[1])) {
272 this->progress = true;
273 return swizzle_if_required(ir, ir->operands[0]);
276 if (is_vec_zero(op_const[0]) || is_vec_zero(op_const[1])) {
277 this->progress = true;
278 return ir_constant::zero(ir, ir->type);
281 /* Reassociate multiplication of constants so that we can do
284 if (op_const[0] && !op_const[1])
285 reassociate_constant(ir, 0, op_const[0],
286 ir->operands[1]->as_expression());
287 if (op_const[1] && !op_const[0])
288 reassociate_constant(ir, 1, op_const[1],
289 ir->operands[0]->as_expression());
294 if (is_vec_one(op_const[0]) && ir->type->base_type == GLSL_TYPE_FLOAT) {
295 this->progress = true;
296 temp = new(mem_ctx) ir_expression(ir_unop_rcp,
297 ir->operands[1]->type,
300 return swizzle_if_required(ir, temp);
302 if (is_vec_one(op_const[1])) {
303 this->progress = true;
304 return swizzle_if_required(ir, ir->operands[0]);
308 case ir_binop_logic_and:
309 /* FINISHME: Also simplify (a && a) to (a). */
310 if (is_vec_one(op_const[0])) {
311 this->progress = true;
312 return ir->operands[1];
313 } else if (is_vec_one(op_const[1])) {
314 this->progress = true;
315 return ir->operands[0];
316 } else if (is_vec_zero(op_const[0]) || is_vec_zero(op_const[1])) {
317 this->progress = true;
318 return ir_constant::zero(mem_ctx, ir->type);
322 case ir_binop_logic_xor:
323 /* FINISHME: Also simplify (a ^^ a) to (false). */
324 if (is_vec_zero(op_const[0])) {
325 this->progress = true;
326 return ir->operands[1];
327 } else if (is_vec_zero(op_const[1])) {
328 this->progress = true;
329 return ir->operands[0];
330 } else if (is_vec_one(op_const[0])) {
331 this->progress = true;
332 return new(mem_ctx) ir_expression(ir_unop_logic_not, ir->type,
333 ir->operands[1], NULL);
334 } else if (is_vec_one(op_const[1])) {
335 this->progress = true;
336 return new(mem_ctx) ir_expression(ir_unop_logic_not, ir->type,
337 ir->operands[0], NULL);
341 case ir_binop_logic_or:
342 /* FINISHME: Also simplify (a || a) to (a). */
343 if (is_vec_zero(op_const[0])) {
344 this->progress = true;
345 return ir->operands[1];
346 } else if (is_vec_zero(op_const[1])) {
347 this->progress = true;
348 return ir->operands[0];
349 } else if (is_vec_one(op_const[0]) || is_vec_one(op_const[1])) {
350 ir_constant_data data;
352 for (unsigned i = 0; i < 16; i++)
355 this->progress = true;
356 return new(mem_ctx) ir_constant(ir->type, &data);
361 if (op_expr[0] && op_expr[0]->operation == ir_unop_rcp) {
362 this->progress = true;
363 return op_expr[0]->operands[0];
366 /* FINISHME: We should do rcp(rsq(x)) -> sqrt(x) for some
367 * backends, except that some backends will have done sqrt ->
368 * rcp(rsq(x)) and we don't want to undo it for them.
371 /* As far as we know, all backends are OK with rsq. */
372 if (op_expr[0] && op_expr[0]->operation == ir_unop_sqrt) {
373 this->progress = true;
374 temp = new(mem_ctx) ir_expression(ir_unop_rsq,
375 op_expr[0]->operands[0]->type,
376 op_expr[0]->operands[0],
378 return swizzle_if_required(ir, temp);
391 ir_algebraic_visitor::handle_rvalue(ir_rvalue **rvalue)
396 ir_expression *expr = (*rvalue)->as_expression();
397 if (!expr || expr->operation == ir_quadop_vector)
400 *rvalue = handle_expression(expr);
404 do_algebraic(exec_list *instructions)
406 ir_algebraic_visitor v;
408 visit_list_elements(&v, instructions);