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 ir_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
)
78 if (!ir
->type
->is_scalar() &&
79 !ir
->type
->is_vector())
82 for (c
= 0; c
< ir
->type
->vector_elements
; c
++) {
83 switch (ir
->type
->base_type
) {
85 if (ir
->value
.f
[c
] != 0.0)
89 if (ir
->value
.i
[c
] != 0)
93 if (ir
->value
.u
[c
] != 0)
97 if (ir
->value
.b
[c
] != false)
101 assert(!"bad base type");
110 is_vec_one(ir_constant
*ir
)
116 if (!ir
->type
->is_scalar() &&
117 !ir
->type
->is_vector())
120 for (c
= 0; c
< ir
->type
->vector_elements
; c
++) {
121 switch (ir
->type
->base_type
) {
122 case GLSL_TYPE_FLOAT
:
123 if (ir
->value
.f
[c
] != 1.0)
127 if (ir
->value
.i
[c
] != 1)
131 if (ir
->value
.u
[c
] != 1)
135 if (ir
->value
.b
[c
] != true)
139 assert(!"bad base type");
148 update_type(ir_expression
*ir
)
150 if (ir
->operands
[0]->type
->is_vector())
151 ir
->type
= ir
->operands
[0]->type
;
153 ir
->type
= ir
->operands
[1]->type
;
157 ir_algebraic_visitor::reassociate_operands(ir_expression
*ir1
,
162 ir_rvalue
*temp
= ir2
->operands
[op2
];
163 ir2
->operands
[op2
] = ir1
->operands
[op1
];
164 ir1
->operands
[op1
] = temp
;
166 /* Update the type of ir2. The type of ir1 won't have changed --
167 * base types matched, and at least one of the operands of the 2
168 * binops is still a vector if any of them were.
172 this->progress
= true;
176 * Reassociates a constant down a tree of adds or multiplies.
178 * Consider (2 * (a * (b * 0.5))). We want to send up with a * b.
181 ir_algebraic_visitor::reassociate_constant(ir_expression
*ir1
, int const_index
,
182 ir_constant
*constant
,
185 if (!ir2
|| ir1
->operation
!= ir2
->operation
)
188 /* Don't want to even think about matrices. */
189 if (ir1
->operands
[0]->type
->is_matrix() ||
190 ir1
->operands
[0]->type
->is_matrix() ||
191 ir2
->operands
[1]->type
->is_matrix() ||
192 ir2
->operands
[1]->type
->is_matrix())
195 ir_constant
*ir2_const
[2];
196 ir2_const
[0] = ir2
->operands
[0]->constant_expression_value();
197 ir2_const
[1] = ir2
->operands
[1]->constant_expression_value();
199 if (ir2_const
[0] && ir2_const
[1])
203 reassociate_operands(ir1
, const_index
, ir2
, 1);
205 } else if (ir2_const
[1]) {
206 reassociate_operands(ir1
, const_index
, ir2
, 0);
210 if (reassociate_constant(ir1
, const_index
, constant
,
211 ir2
->operands
[0]->as_expression())) {
216 if (reassociate_constant(ir1
, const_index
, constant
,
217 ir2
->operands
[1]->as_expression())) {
225 /* When eliminating an expression and just returning one of its operands,
226 * we may need to swizzle that operand out to a vector if the expression was
230 ir_algebraic_visitor::swizzle_if_required(ir_expression
*expr
,
233 if (expr
->type
->is_vector() && operand
->type
->is_scalar()) {
234 return new(mem_ctx
) ir_swizzle(operand
, 0, 0, 0, 0,
235 expr
->type
->vector_elements
);
241 ir_algebraic_visitor::handle_expression(ir_expression
*ir
)
243 ir_constant
*op_const
[2] = {NULL
, NULL
};
244 ir_expression
*op_expr
[2] = {NULL
, NULL
};
248 for (i
= 0; i
< ir
->get_num_operands(); i
++) {
249 if (ir
->operands
[i
]->type
->is_matrix())
252 op_const
[i
] = ir
->operands
[i
]->constant_expression_value();
253 op_expr
[i
] = ir
->operands
[i
]->as_expression();
256 if (this->mem_ctx
== NULL
)
257 this->mem_ctx
= talloc_parent(ir
);
259 switch (ir
->operation
) {
260 case ir_unop_logic_not
: {
261 enum ir_expression_operation new_op
= ir_unop_logic_not
;
263 if (op_expr
[0] == NULL
)
266 switch (op_expr
[0]->operation
) {
267 case ir_binop_less
: new_op
= ir_binop_gequal
; break;
268 case ir_binop_greater
: new_op
= ir_binop_lequal
; break;
269 case ir_binop_lequal
: new_op
= ir_binop_greater
; break;
270 case ir_binop_gequal
: new_op
= ir_binop_less
; break;
271 case ir_binop_equal
: new_op
= ir_binop_nequal
; break;
272 case ir_binop_nequal
: new_op
= ir_binop_equal
; break;
275 /* The default case handler is here to silence a warning from GCC.
280 if (new_op
!= ir_unop_logic_not
) {
281 this->progress
= true;
282 return new(mem_ctx
) ir_expression(new_op
,
284 op_expr
[0]->operands
[0],
285 op_expr
[0]->operands
[1]);
292 if (is_vec_zero(op_const
[0])) {
293 this->progress
= true;
294 return swizzle_if_required(ir
, ir
->operands
[1]);
296 if (is_vec_zero(op_const
[1])) {
297 this->progress
= true;
298 return swizzle_if_required(ir
, ir
->operands
[0]);
301 /* Reassociate addition of constants so that we can do constant
304 if (op_const
[0] && !op_const
[1])
305 reassociate_constant(ir
, 0, op_const
[0],
306 ir
->operands
[1]->as_expression());
307 if (op_const
[1] && !op_const
[0])
308 reassociate_constant(ir
, 1, op_const
[1],
309 ir
->operands
[0]->as_expression());
313 if (is_vec_zero(op_const
[0])) {
314 this->progress
= true;
315 temp
= new(mem_ctx
) ir_expression(ir_unop_neg
,
316 ir
->operands
[1]->type
,
319 return swizzle_if_required(ir
, temp
);
321 if (is_vec_zero(op_const
[1])) {
322 this->progress
= true;
323 return swizzle_if_required(ir
, ir
->operands
[0]);
328 if (is_vec_one(op_const
[0])) {
329 this->progress
= true;
330 return swizzle_if_required(ir
, ir
->operands
[1]);
332 if (is_vec_one(op_const
[1])) {
333 this->progress
= true;
334 return swizzle_if_required(ir
, ir
->operands
[0]);
337 if (is_vec_zero(op_const
[0]) || is_vec_zero(op_const
[1])) {
338 this->progress
= true;
339 return ir_constant::zero(ir
, ir
->type
);
342 /* Reassociate multiplication of constants so that we can do
345 if (op_const
[0] && !op_const
[1])
346 reassociate_constant(ir
, 0, op_const
[0],
347 ir
->operands
[1]->as_expression());
348 if (op_const
[1] && !op_const
[0])
349 reassociate_constant(ir
, 1, op_const
[1],
350 ir
->operands
[0]->as_expression());
355 if (is_vec_one(op_const
[0]) && ir
->type
->base_type
== GLSL_TYPE_FLOAT
) {
356 this->progress
= true;
357 temp
= new(mem_ctx
) ir_expression(ir_unop_rcp
,
358 ir
->operands
[1]->type
,
361 return swizzle_if_required(ir
, temp
);
363 if (is_vec_one(op_const
[1])) {
364 this->progress
= true;
365 return swizzle_if_required(ir
, ir
->operands
[0]);
369 case ir_binop_logic_and
:
370 /* FINISHME: Also simplify (a && a) to (a). */
371 if (is_vec_one(op_const
[0])) {
372 this->progress
= true;
373 return ir
->operands
[1];
374 } else if (is_vec_one(op_const
[1])) {
375 this->progress
= true;
376 return ir
->operands
[0];
377 } else if (is_vec_zero(op_const
[0]) || is_vec_zero(op_const
[1])) {
378 this->progress
= true;
379 return ir_constant::zero(mem_ctx
, ir
->type
);
383 case ir_binop_logic_xor
:
384 /* FINISHME: Also simplify (a ^^ a) to (false). */
385 if (is_vec_zero(op_const
[0])) {
386 this->progress
= true;
387 return ir
->operands
[1];
388 } else if (is_vec_zero(op_const
[1])) {
389 this->progress
= true;
390 return ir
->operands
[0];
391 } else if (is_vec_one(op_const
[0])) {
392 this->progress
= true;
393 return new(mem_ctx
) ir_expression(ir_unop_logic_not
, ir
->type
,
394 ir
->operands
[1], NULL
);
395 } else if (is_vec_one(op_const
[1])) {
396 this->progress
= true;
397 return new(mem_ctx
) ir_expression(ir_unop_logic_not
, ir
->type
,
398 ir
->operands
[0], NULL
);
402 case ir_binop_logic_or
:
403 /* FINISHME: Also simplify (a || a) to (a). */
404 if (is_vec_zero(op_const
[0])) {
405 this->progress
= true;
406 return ir
->operands
[1];
407 } else if (is_vec_zero(op_const
[1])) {
408 this->progress
= true;
409 return ir
->operands
[0];
410 } else if (is_vec_one(op_const
[0]) || is_vec_one(op_const
[1])) {
411 ir_constant_data data
;
413 for (unsigned i
= 0; i
< 16; i
++)
416 this->progress
= true;
417 return new(mem_ctx
) ir_constant(ir
->type
, &data
);
422 if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_rcp
) {
423 this->progress
= true;
424 return op_expr
[0]->operands
[0];
427 /* FINISHME: We should do rcp(rsq(x)) -> sqrt(x) for some
428 * backends, except that some backends will have done sqrt ->
429 * rcp(rsq(x)) and we don't want to undo it for them.
432 /* As far as we know, all backends are OK with rsq. */
433 if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_sqrt
) {
434 this->progress
= true;
435 temp
= new(mem_ctx
) ir_expression(ir_unop_rsq
,
436 op_expr
[0]->operands
[0]->type
,
437 op_expr
[0]->operands
[0],
439 return swizzle_if_required(ir
, temp
);
452 ir_algebraic_visitor::handle_rvalue(ir_rvalue
**rvalue
)
457 ir_expression
*expr
= (*rvalue
)->as_expression();
461 *rvalue
= handle_expression(expr
);
465 do_algebraic(exec_list
*instructions
)
467 ir_algebraic_visitor v
;
469 visit_list_elements(&v
, instructions
);