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"
40 * Visitor class for replacing expressions with ir_constant values.
43 class ir_algebraic_visitor
: public ir_rvalue_visitor
{
45 ir_algebraic_visitor()
47 this->progress
= false;
51 virtual ~ir_algebraic_visitor()
55 ir_rvalue
*handle_expression(ir_expression
*ir
);
56 void handle_rvalue(ir_rvalue
**rvalue
);
57 bool reassociate_constant(ir_expression
*ir1
,
59 ir_constant
*constant
,
61 void reassociate_operands(ir_expression
*ir1
,
65 ir_rvalue
*swizzle_if_required(ir_expression
*expr
,
73 } /* unnamed namespace */
76 is_vec_zero(ir_constant
*ir
)
78 return (ir
== NULL
) ? false : ir
->is_zero();
82 is_vec_one(ir_constant
*ir
)
84 return (ir
== NULL
) ? false : ir
->is_one();
88 is_vec_negative_one(ir_constant
*ir
)
90 return (ir
== NULL
) ? false : ir
->is_negative_one();
94 is_vec_basis(ir_constant
*ir
)
96 return (ir
== NULL
) ? false : ir
->is_basis();
100 update_type(ir_expression
*ir
)
102 if (ir
->operands
[0]->type
->is_vector())
103 ir
->type
= ir
->operands
[0]->type
;
105 ir
->type
= ir
->operands
[1]->type
;
109 ir_algebraic_visitor::reassociate_operands(ir_expression
*ir1
,
114 ir_rvalue
*temp
= ir2
->operands
[op2
];
115 ir2
->operands
[op2
] = ir1
->operands
[op1
];
116 ir1
->operands
[op1
] = temp
;
118 /* Update the type of ir2. The type of ir1 won't have changed --
119 * base types matched, and at least one of the operands of the 2
120 * binops is still a vector if any of them were.
124 this->progress
= true;
128 * Reassociates a constant down a tree of adds or multiplies.
130 * Consider (2 * (a * (b * 0.5))). We want to send up with a * b.
133 ir_algebraic_visitor::reassociate_constant(ir_expression
*ir1
, int const_index
,
134 ir_constant
*constant
,
137 if (!ir2
|| ir1
->operation
!= ir2
->operation
)
140 /* Don't want to even think about matrices. */
141 if (ir1
->operands
[0]->type
->is_matrix() ||
142 ir1
->operands
[1]->type
->is_matrix() ||
143 ir2
->operands
[0]->type
->is_matrix() ||
144 ir2
->operands
[1]->type
->is_matrix())
147 ir_constant
*ir2_const
[2];
148 ir2_const
[0] = ir2
->operands
[0]->constant_expression_value();
149 ir2_const
[1] = ir2
->operands
[1]->constant_expression_value();
151 if (ir2_const
[0] && ir2_const
[1])
155 reassociate_operands(ir1
, const_index
, ir2
, 1);
157 } else if (ir2_const
[1]) {
158 reassociate_operands(ir1
, const_index
, ir2
, 0);
162 if (reassociate_constant(ir1
, const_index
, constant
,
163 ir2
->operands
[0]->as_expression())) {
168 if (reassociate_constant(ir1
, const_index
, constant
,
169 ir2
->operands
[1]->as_expression())) {
177 /* When eliminating an expression and just returning one of its operands,
178 * we may need to swizzle that operand out to a vector if the expression was
182 ir_algebraic_visitor::swizzle_if_required(ir_expression
*expr
,
185 if (expr
->type
->is_vector() && operand
->type
->is_scalar()) {
186 return new(mem_ctx
) ir_swizzle(operand
, 0, 0, 0, 0,
187 expr
->type
->vector_elements
);
193 ir_algebraic_visitor::handle_expression(ir_expression
*ir
)
195 ir_constant
*op_const
[4] = {NULL
, NULL
, NULL
, NULL
};
196 ir_expression
*op_expr
[4] = {NULL
, NULL
, NULL
, NULL
};
200 assert(ir
->get_num_operands() <= 4);
201 for (i
= 0; i
< ir
->get_num_operands(); i
++) {
202 if (ir
->operands
[i
]->type
->is_matrix())
205 op_const
[i
] = ir
->operands
[i
]->constant_expression_value();
206 op_expr
[i
] = ir
->operands
[i
]->as_expression();
209 if (this->mem_ctx
== NULL
)
210 this->mem_ctx
= ralloc_parent(ir
);
212 switch (ir
->operation
) {
213 case ir_unop_logic_not
: {
214 enum ir_expression_operation new_op
= ir_unop_logic_not
;
216 if (op_expr
[0] == NULL
)
219 switch (op_expr
[0]->operation
) {
220 case ir_binop_less
: new_op
= ir_binop_gequal
; break;
221 case ir_binop_greater
: new_op
= ir_binop_lequal
; break;
222 case ir_binop_lequal
: new_op
= ir_binop_greater
; break;
223 case ir_binop_gequal
: new_op
= ir_binop_less
; break;
224 case ir_binop_equal
: new_op
= ir_binop_nequal
; break;
225 case ir_binop_nequal
: new_op
= ir_binop_equal
; break;
226 case ir_binop_all_equal
: new_op
= ir_binop_any_nequal
; break;
227 case ir_binop_any_nequal
: new_op
= ir_binop_all_equal
; break;
230 /* The default case handler is here to silence a warning from GCC.
235 if (new_op
!= ir_unop_logic_not
) {
236 this->progress
= true;
237 return new(mem_ctx
) ir_expression(new_op
,
239 op_expr
[0]->operands
[0],
240 op_expr
[0]->operands
[1]);
247 if (is_vec_zero(op_const
[0])) {
248 this->progress
= true;
249 return swizzle_if_required(ir
, ir
->operands
[1]);
251 if (is_vec_zero(op_const
[1])) {
252 this->progress
= true;
253 return swizzle_if_required(ir
, ir
->operands
[0]);
256 /* Reassociate addition of constants so that we can do constant
259 if (op_const
[0] && !op_const
[1])
260 reassociate_constant(ir
, 0, op_const
[0],
261 ir
->operands
[1]->as_expression());
262 if (op_const
[1] && !op_const
[0])
263 reassociate_constant(ir
, 1, op_const
[1],
264 ir
->operands
[0]->as_expression());
268 if (is_vec_zero(op_const
[0])) {
269 this->progress
= true;
270 temp
= new(mem_ctx
) ir_expression(ir_unop_neg
,
271 ir
->operands
[1]->type
,
274 return swizzle_if_required(ir
, temp
);
276 if (is_vec_zero(op_const
[1])) {
277 this->progress
= true;
278 return swizzle_if_required(ir
, ir
->operands
[0]);
283 if (is_vec_one(op_const
[0])) {
284 this->progress
= true;
285 return swizzle_if_required(ir
, ir
->operands
[1]);
287 if (is_vec_one(op_const
[1])) {
288 this->progress
= true;
289 return swizzle_if_required(ir
, ir
->operands
[0]);
292 if (is_vec_zero(op_const
[0]) || is_vec_zero(op_const
[1])) {
293 this->progress
= true;
294 return ir_constant::zero(ir
, ir
->type
);
296 if (is_vec_negative_one(op_const
[0])) {
297 this->progress
= true;
298 temp
= new(mem_ctx
) ir_expression(ir_unop_neg
,
299 ir
->operands
[1]->type
,
302 return swizzle_if_required(ir
, temp
);
304 if (is_vec_negative_one(op_const
[1])) {
305 this->progress
= true;
306 temp
= new(mem_ctx
) ir_expression(ir_unop_neg
,
307 ir
->operands
[0]->type
,
310 return swizzle_if_required(ir
, temp
);
314 /* Reassociate multiplication of constants so that we can do
317 if (op_const
[0] && !op_const
[1])
318 reassociate_constant(ir
, 0, op_const
[0],
319 ir
->operands
[1]->as_expression());
320 if (op_const
[1] && !op_const
[0])
321 reassociate_constant(ir
, 1, op_const
[1],
322 ir
->operands
[0]->as_expression());
327 if (is_vec_one(op_const
[0]) && ir
->type
->base_type
== GLSL_TYPE_FLOAT
) {
328 this->progress
= true;
329 temp
= new(mem_ctx
) ir_expression(ir_unop_rcp
,
330 ir
->operands
[1]->type
,
333 return swizzle_if_required(ir
, temp
);
335 if (is_vec_one(op_const
[1])) {
336 this->progress
= true;
337 return swizzle_if_required(ir
, ir
->operands
[0]);
342 if (is_vec_zero(op_const
[0]) || is_vec_zero(op_const
[1])) {
343 this->progress
= true;
344 return ir_constant::zero(mem_ctx
, ir
->type
);
346 if (is_vec_basis(op_const
[0])) {
347 this->progress
= true;
348 unsigned component
= 0;
349 for (unsigned c
= 0; c
< op_const
[0]->type
->vector_elements
; c
++) {
350 if (op_const
[0]->value
.f
[c
] == 1.0)
353 return new(mem_ctx
) ir_swizzle(ir
->operands
[1], component
, 0, 0, 0, 1);
355 if (is_vec_basis(op_const
[1])) {
356 this->progress
= true;
357 unsigned component
= 0;
358 for (unsigned c
= 0; c
< op_const
[1]->type
->vector_elements
; c
++) {
359 if (op_const
[1]->value
.f
[c
] == 1.0)
362 return new(mem_ctx
) ir_swizzle(ir
->operands
[0], component
, 0, 0, 0, 1);
366 case ir_binop_logic_and
:
367 /* FINISHME: Also simplify (a && a) to (a). */
368 if (is_vec_one(op_const
[0])) {
369 this->progress
= true;
370 return ir
->operands
[1];
371 } else if (is_vec_one(op_const
[1])) {
372 this->progress
= true;
373 return ir
->operands
[0];
374 } else if (is_vec_zero(op_const
[0]) || is_vec_zero(op_const
[1])) {
375 this->progress
= true;
376 return ir_constant::zero(mem_ctx
, ir
->type
);
380 case ir_binop_logic_xor
:
381 /* FINISHME: Also simplify (a ^^ a) to (false). */
382 if (is_vec_zero(op_const
[0])) {
383 this->progress
= true;
384 return ir
->operands
[1];
385 } else if (is_vec_zero(op_const
[1])) {
386 this->progress
= true;
387 return ir
->operands
[0];
388 } else if (is_vec_one(op_const
[0])) {
389 this->progress
= true;
390 return new(mem_ctx
) ir_expression(ir_unop_logic_not
, ir
->type
,
391 ir
->operands
[1], NULL
);
392 } else if (is_vec_one(op_const
[1])) {
393 this->progress
= true;
394 return new(mem_ctx
) ir_expression(ir_unop_logic_not
, ir
->type
,
395 ir
->operands
[0], NULL
);
399 case ir_binop_logic_or
:
400 /* FINISHME: Also simplify (a || a) to (a). */
401 if (is_vec_zero(op_const
[0])) {
402 this->progress
= true;
403 return ir
->operands
[1];
404 } else if (is_vec_zero(op_const
[1])) {
405 this->progress
= true;
406 return ir
->operands
[0];
407 } else if (is_vec_one(op_const
[0]) || is_vec_one(op_const
[1])) {
408 ir_constant_data data
;
410 for (unsigned i
= 0; i
< 16; i
++)
413 this->progress
= true;
414 return new(mem_ctx
) ir_constant(ir
->type
, &data
);
419 if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_rcp
) {
420 this->progress
= true;
421 return op_expr
[0]->operands
[0];
424 /* FINISHME: We should do rcp(rsq(x)) -> sqrt(x) for some
425 * backends, except that some backends will have done sqrt ->
426 * rcp(rsq(x)) and we don't want to undo it for them.
429 /* As far as we know, all backends are OK with rsq. */
430 if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_sqrt
) {
431 this->progress
= true;
432 temp
= new(mem_ctx
) ir_expression(ir_unop_rsq
,
433 op_expr
[0]->operands
[0]->type
,
434 op_expr
[0]->operands
[0],
436 return swizzle_if_required(ir
, temp
);
442 /* Operands are (x, y, a). */
443 if (is_vec_zero(op_const
[2])) {
444 this->progress
= true;
445 return swizzle_if_required(ir
, ir
->operands
[0]);
446 } else if (is_vec_one(op_const
[2])) {
447 this->progress
= true;
448 return swizzle_if_required(ir
, ir
->operands
[1]);
460 ir_algebraic_visitor::handle_rvalue(ir_rvalue
**rvalue
)
465 ir_expression
*expr
= (*rvalue
)->as_expression();
466 if (!expr
|| expr
->operation
== ir_quadop_vector
)
469 *rvalue
= handle_expression(expr
);
473 do_algebraic(exec_list
*instructions
)
475 ir_algebraic_visitor v
;
477 visit_list_elements(&v
, instructions
);