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;
273 case ir_binop_all_equal
: new_op
= ir_binop_any_nequal
; break;
274 case ir_binop_any_nequal
: new_op
= ir_binop_all_equal
; break;
277 /* The default case handler is here to silence a warning from GCC.
282 if (new_op
!= ir_unop_logic_not
) {
283 this->progress
= true;
284 return new(mem_ctx
) ir_expression(new_op
,
286 op_expr
[0]->operands
[0],
287 op_expr
[0]->operands
[1]);
294 if (is_vec_zero(op_const
[0])) {
295 this->progress
= true;
296 return swizzle_if_required(ir
, ir
->operands
[1]);
298 if (is_vec_zero(op_const
[1])) {
299 this->progress
= true;
300 return swizzle_if_required(ir
, ir
->operands
[0]);
303 /* Reassociate addition of constants so that we can do constant
306 if (op_const
[0] && !op_const
[1])
307 reassociate_constant(ir
, 0, op_const
[0],
308 ir
->operands
[1]->as_expression());
309 if (op_const
[1] && !op_const
[0])
310 reassociate_constant(ir
, 1, op_const
[1],
311 ir
->operands
[0]->as_expression());
315 if (is_vec_zero(op_const
[0])) {
316 this->progress
= true;
317 temp
= new(mem_ctx
) ir_expression(ir_unop_neg
,
318 ir
->operands
[1]->type
,
321 return swizzle_if_required(ir
, temp
);
323 if (is_vec_zero(op_const
[1])) {
324 this->progress
= true;
325 return swizzle_if_required(ir
, ir
->operands
[0]);
330 if (is_vec_one(op_const
[0])) {
331 this->progress
= true;
332 return swizzle_if_required(ir
, ir
->operands
[1]);
334 if (is_vec_one(op_const
[1])) {
335 this->progress
= true;
336 return swizzle_if_required(ir
, ir
->operands
[0]);
339 if (is_vec_zero(op_const
[0]) || is_vec_zero(op_const
[1])) {
340 this->progress
= true;
341 return ir_constant::zero(ir
, ir
->type
);
344 /* Reassociate multiplication of constants so that we can do
347 if (op_const
[0] && !op_const
[1])
348 reassociate_constant(ir
, 0, op_const
[0],
349 ir
->operands
[1]->as_expression());
350 if (op_const
[1] && !op_const
[0])
351 reassociate_constant(ir
, 1, op_const
[1],
352 ir
->operands
[0]->as_expression());
357 if (is_vec_one(op_const
[0]) && ir
->type
->base_type
== GLSL_TYPE_FLOAT
) {
358 this->progress
= true;
359 temp
= new(mem_ctx
) ir_expression(ir_unop_rcp
,
360 ir
->operands
[1]->type
,
363 return swizzle_if_required(ir
, temp
);
365 if (is_vec_one(op_const
[1])) {
366 this->progress
= true;
367 return swizzle_if_required(ir
, ir
->operands
[0]);
371 case ir_binop_logic_and
:
372 /* FINISHME: Also simplify (a && a) to (a). */
373 if (is_vec_one(op_const
[0])) {
374 this->progress
= true;
375 return ir
->operands
[1];
376 } else if (is_vec_one(op_const
[1])) {
377 this->progress
= true;
378 return ir
->operands
[0];
379 } else if (is_vec_zero(op_const
[0]) || is_vec_zero(op_const
[1])) {
380 this->progress
= true;
381 return ir_constant::zero(mem_ctx
, ir
->type
);
385 case ir_binop_logic_xor
:
386 /* FINISHME: Also simplify (a ^^ a) to (false). */
387 if (is_vec_zero(op_const
[0])) {
388 this->progress
= true;
389 return ir
->operands
[1];
390 } else if (is_vec_zero(op_const
[1])) {
391 this->progress
= true;
392 return ir
->operands
[0];
393 } else if (is_vec_one(op_const
[0])) {
394 this->progress
= true;
395 return new(mem_ctx
) ir_expression(ir_unop_logic_not
, ir
->type
,
396 ir
->operands
[1], NULL
);
397 } else if (is_vec_one(op_const
[1])) {
398 this->progress
= true;
399 return new(mem_ctx
) ir_expression(ir_unop_logic_not
, ir
->type
,
400 ir
->operands
[0], NULL
);
404 case ir_binop_logic_or
:
405 /* FINISHME: Also simplify (a || a) to (a). */
406 if (is_vec_zero(op_const
[0])) {
407 this->progress
= true;
408 return ir
->operands
[1];
409 } else if (is_vec_zero(op_const
[1])) {
410 this->progress
= true;
411 return ir
->operands
[0];
412 } else if (is_vec_one(op_const
[0]) || is_vec_one(op_const
[1])) {
413 ir_constant_data data
;
415 for (unsigned i
= 0; i
< 16; i
++)
418 this->progress
= true;
419 return new(mem_ctx
) ir_constant(ir
->type
, &data
);
424 if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_rcp
) {
425 this->progress
= true;
426 return op_expr
[0]->operands
[0];
429 /* FINISHME: We should do rcp(rsq(x)) -> sqrt(x) for some
430 * backends, except that some backends will have done sqrt ->
431 * rcp(rsq(x)) and we don't want to undo it for them.
434 /* As far as we know, all backends are OK with rsq. */
435 if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_sqrt
) {
436 this->progress
= true;
437 temp
= new(mem_ctx
) ir_expression(ir_unop_rsq
,
438 op_expr
[0]->operands
[0]->type
,
439 op_expr
[0]->operands
[0],
441 return swizzle_if_required(ir
, temp
);
454 ir_algebraic_visitor::handle_rvalue(ir_rvalue
**rvalue
)
459 ir_expression
*expr
= (*rvalue
)->as_expression();
463 *rvalue
= handle_expression(expr
);
467 do_algebraic(exec_list
*instructions
)
469 ir_algebraic_visitor v
;
471 visit_list_elements(&v
, instructions
);