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 "ir_builder.h"
36 #include "glsl_types.h"
38 using namespace ir_builder
;
43 * Visitor class for replacing expressions with ir_constant values.
46 class ir_algebraic_visitor
: public ir_rvalue_visitor
{
48 ir_algebraic_visitor()
50 this->progress
= false;
54 virtual ~ir_algebraic_visitor()
58 ir_rvalue
*handle_expression(ir_expression
*ir
);
59 void handle_rvalue(ir_rvalue
**rvalue
);
60 bool reassociate_constant(ir_expression
*ir1
,
62 ir_constant
*constant
,
64 void reassociate_operands(ir_expression
*ir1
,
68 ir_rvalue
*swizzle_if_required(ir_expression
*expr
,
76 } /* unnamed namespace */
79 is_vec_zero(ir_constant
*ir
)
81 return (ir
== NULL
) ? false : ir
->is_zero();
85 is_vec_one(ir_constant
*ir
)
87 return (ir
== NULL
) ? false : ir
->is_one();
91 is_vec_two(ir_constant
*ir
)
93 return (ir
== NULL
) ? false : ir
->is_value(2.0, 2);
97 is_vec_negative_one(ir_constant
*ir
)
99 return (ir
== NULL
) ? false : ir
->is_negative_one();
103 is_vec_basis(ir_constant
*ir
)
105 return (ir
== NULL
) ? false : ir
->is_basis();
109 update_type(ir_expression
*ir
)
111 if (ir
->operands
[0]->type
->is_vector())
112 ir
->type
= ir
->operands
[0]->type
;
114 ir
->type
= ir
->operands
[1]->type
;
118 ir_algebraic_visitor::reassociate_operands(ir_expression
*ir1
,
123 ir_rvalue
*temp
= ir2
->operands
[op2
];
124 ir2
->operands
[op2
] = ir1
->operands
[op1
];
125 ir1
->operands
[op1
] = temp
;
127 /* Update the type of ir2. The type of ir1 won't have changed --
128 * base types matched, and at least one of the operands of the 2
129 * binops is still a vector if any of them were.
133 this->progress
= true;
137 * Reassociates a constant down a tree of adds or multiplies.
139 * Consider (2 * (a * (b * 0.5))). We want to send up with a * b.
142 ir_algebraic_visitor::reassociate_constant(ir_expression
*ir1
, int const_index
,
143 ir_constant
*constant
,
146 if (!ir2
|| ir1
->operation
!= ir2
->operation
)
149 /* Don't want to even think about matrices. */
150 if (ir1
->operands
[0]->type
->is_matrix() ||
151 ir1
->operands
[1]->type
->is_matrix() ||
152 ir2
->operands
[0]->type
->is_matrix() ||
153 ir2
->operands
[1]->type
->is_matrix())
156 ir_constant
*ir2_const
[2];
157 ir2_const
[0] = ir2
->operands
[0]->constant_expression_value();
158 ir2_const
[1] = ir2
->operands
[1]->constant_expression_value();
160 if (ir2_const
[0] && ir2_const
[1])
164 reassociate_operands(ir1
, const_index
, ir2
, 1);
166 } else if (ir2_const
[1]) {
167 reassociate_operands(ir1
, const_index
, ir2
, 0);
171 if (reassociate_constant(ir1
, const_index
, constant
,
172 ir2
->operands
[0]->as_expression())) {
177 if (reassociate_constant(ir1
, const_index
, constant
,
178 ir2
->operands
[1]->as_expression())) {
186 /* When eliminating an expression and just returning one of its operands,
187 * we may need to swizzle that operand out to a vector if the expression was
191 ir_algebraic_visitor::swizzle_if_required(ir_expression
*expr
,
194 if (expr
->type
->is_vector() && operand
->type
->is_scalar()) {
195 return new(mem_ctx
) ir_swizzle(operand
, 0, 0, 0, 0,
196 expr
->type
->vector_elements
);
202 ir_algebraic_visitor::handle_expression(ir_expression
*ir
)
204 ir_constant
*op_const
[4] = {NULL
, NULL
, NULL
, NULL
};
205 ir_expression
*op_expr
[4] = {NULL
, NULL
, NULL
, NULL
};
208 assert(ir
->get_num_operands() <= 4);
209 for (i
= 0; i
< ir
->get_num_operands(); i
++) {
210 if (ir
->operands
[i
]->type
->is_matrix())
213 op_const
[i
] = ir
->operands
[i
]->constant_expression_value();
214 op_expr
[i
] = ir
->operands
[i
]->as_expression();
217 if (this->mem_ctx
== NULL
)
218 this->mem_ctx
= ralloc_parent(ir
);
220 switch (ir
->operation
) {
222 if (op_expr
[0] == NULL
)
225 switch (op_expr
[0]->operation
) {
228 return abs(op_expr
[0]->operands
[0]);
235 if (op_expr
[0] == NULL
)
238 if (op_expr
[0]->operation
== ir_unop_neg
) {
239 return op_expr
[0]->operands
[0];
243 case ir_unop_logic_not
: {
244 enum ir_expression_operation new_op
= ir_unop_logic_not
;
246 if (op_expr
[0] == NULL
)
249 switch (op_expr
[0]->operation
) {
250 case ir_binop_less
: new_op
= ir_binop_gequal
; break;
251 case ir_binop_greater
: new_op
= ir_binop_lequal
; break;
252 case ir_binop_lequal
: new_op
= ir_binop_greater
; break;
253 case ir_binop_gequal
: new_op
= ir_binop_less
; break;
254 case ir_binop_equal
: new_op
= ir_binop_nequal
; break;
255 case ir_binop_nequal
: new_op
= ir_binop_equal
; break;
256 case ir_binop_all_equal
: new_op
= ir_binop_any_nequal
; break;
257 case ir_binop_any_nequal
: new_op
= ir_binop_all_equal
; break;
260 /* The default case handler is here to silence a warning from GCC.
265 if (new_op
!= ir_unop_logic_not
) {
266 return new(mem_ctx
) ir_expression(new_op
,
268 op_expr
[0]->operands
[0],
269 op_expr
[0]->operands
[1]);
276 if (is_vec_zero(op_const
[0]))
277 return ir
->operands
[1];
278 if (is_vec_zero(op_const
[1]))
279 return ir
->operands
[0];
281 /* Reassociate addition of constants so that we can do constant
284 if (op_const
[0] && !op_const
[1])
285 reassociate_constant(ir
, 0, op_const
[0], op_expr
[1]);
286 if (op_const
[1] && !op_const
[0])
287 reassociate_constant(ir
, 1, op_const
[1], op_expr
[0]);
291 if (is_vec_zero(op_const
[0]))
292 return neg(ir
->operands
[1]);
293 if (is_vec_zero(op_const
[1]))
294 return ir
->operands
[0];
298 if (is_vec_one(op_const
[0]))
299 return ir
->operands
[1];
300 if (is_vec_one(op_const
[1]))
301 return ir
->operands
[0];
303 if (is_vec_zero(op_const
[0]) || is_vec_zero(op_const
[1]))
304 return ir_constant::zero(ir
, ir
->type
);
306 if (is_vec_negative_one(op_const
[0]))
307 return neg(ir
->operands
[1]);
308 if (is_vec_negative_one(op_const
[1]))
309 return neg(ir
->operands
[0]);
312 /* Reassociate multiplication of constants so that we can do
315 if (op_const
[0] && !op_const
[1])
316 reassociate_constant(ir
, 0, op_const
[0], op_expr
[1]);
317 if (op_const
[1] && !op_const
[0])
318 reassociate_constant(ir
, 1, op_const
[1], op_expr
[0]);
323 if (is_vec_one(op_const
[0]) && ir
->type
->base_type
== GLSL_TYPE_FLOAT
) {
324 return new(mem_ctx
) ir_expression(ir_unop_rcp
,
325 ir
->operands
[1]->type
,
329 if (is_vec_one(op_const
[1]))
330 return ir
->operands
[0];
334 if (is_vec_zero(op_const
[0]) || is_vec_zero(op_const
[1]))
335 return ir_constant::zero(mem_ctx
, ir
->type
);
337 if (is_vec_basis(op_const
[0])) {
338 unsigned component
= 0;
339 for (unsigned c
= 0; c
< op_const
[0]->type
->vector_elements
; c
++) {
340 if (op_const
[0]->value
.f
[c
] == 1.0)
343 return new(mem_ctx
) ir_swizzle(ir
->operands
[1], component
, 0, 0, 0, 1);
345 if (is_vec_basis(op_const
[1])) {
346 unsigned component
= 0;
347 for (unsigned c
= 0; c
< op_const
[1]->type
->vector_elements
; c
++) {
348 if (op_const
[1]->value
.f
[c
] == 1.0)
351 return new(mem_ctx
) ir_swizzle(ir
->operands
[0], component
, 0, 0, 0, 1);
355 case ir_binop_rshift
:
356 case ir_binop_lshift
:
358 if (is_vec_zero(op_const
[0]))
359 return ir
->operands
[0];
361 if (is_vec_zero(op_const
[1]))
362 return ir
->operands
[0];
365 case ir_binop_logic_and
:
366 if (is_vec_one(op_const
[0])) {
367 return ir
->operands
[1];
368 } else if (is_vec_one(op_const
[1])) {
369 return ir
->operands
[0];
370 } else if (is_vec_zero(op_const
[0]) || is_vec_zero(op_const
[1])) {
371 return ir_constant::zero(mem_ctx
, ir
->type
);
372 } else if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_logic_not
&&
373 op_expr
[1] && op_expr
[1]->operation
== ir_unop_logic_not
) {
375 * (not A) and (not B) === not (A or B)
377 return logic_not(logic_or(op_expr
[0]->operands
[0],
378 op_expr
[1]->operands
[0]));
379 } else if (ir
->operands
[0]->equals(ir
->operands
[1])) {
381 return ir
->operands
[0];
385 case ir_binop_logic_xor
:
386 if (is_vec_zero(op_const
[0])) {
387 return ir
->operands
[1];
388 } else if (is_vec_zero(op_const
[1])) {
389 return ir
->operands
[0];
390 } else if (is_vec_one(op_const
[0])) {
391 return logic_not(ir
->operands
[1]);
392 } else if (is_vec_one(op_const
[1])) {
393 return logic_not(ir
->operands
[0]);
394 } else if (ir
->operands
[0]->equals(ir
->operands
[1])) {
395 /* (a ^^ a) == false */
396 return ir_constant::zero(mem_ctx
, ir
->type
);
400 case ir_binop_logic_or
:
401 if (is_vec_zero(op_const
[0])) {
402 return ir
->operands
[1];
403 } else if (is_vec_zero(op_const
[1])) {
404 return ir
->operands
[0];
405 } else if (is_vec_one(op_const
[0]) || is_vec_one(op_const
[1])) {
406 ir_constant_data data
;
408 for (unsigned i
= 0; i
< 16; i
++)
411 return new(mem_ctx
) ir_constant(ir
->type
, &data
);
412 } else if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_logic_not
&&
413 op_expr
[1] && op_expr
[1]->operation
== ir_unop_logic_not
) {
415 * (not A) or (not B) === not (A and B)
417 return logic_not(logic_and(op_expr
[0]->operands
[0],
418 op_expr
[1]->operands
[0]));
419 } else if (ir
->operands
[0]->equals(ir
->operands
[1])) {
421 return ir
->operands
[0];
427 if (is_vec_one(op_const
[0]))
430 /* pow(2,x) == exp2(x) */
431 if (is_vec_two(op_const
[0]))
432 return expr(ir_unop_exp2
, ir
->operands
[1]);
437 if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_rcp
)
438 return op_expr
[0]->operands
[0];
440 /* While ir_to_mesa.cpp will lower sqrt(x) to rcp(rsq(x)), it does so at
441 * its IR level, so we can always apply this transformation.
443 if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_rsq
)
444 return sqrt(op_expr
[0]->operands
[0]);
446 /* As far as we know, all backends are OK with rsq. */
447 if (op_expr
[0] && op_expr
[0]->operation
== ir_unop_sqrt
) {
448 return rsq(op_expr
[0]->operands
[0]);
454 /* Operands are (x, y, a). */
455 if (is_vec_zero(op_const
[2])) {
456 return ir
->operands
[0];
457 } else if (is_vec_one(op_const
[2])) {
458 return ir
->operands
[1];
470 ir_algebraic_visitor::handle_rvalue(ir_rvalue
**rvalue
)
475 ir_expression
*expr
= (*rvalue
)->as_expression();
476 if (!expr
|| expr
->operation
== ir_quadop_vector
)
479 ir_rvalue
*new_rvalue
= handle_expression(expr
);
480 if (new_rvalue
== *rvalue
)
483 /* If the expr used to be some vec OP scalar returning a vector, and the
484 * optimization gave us back a scalar, we still need to turn it into a
487 *rvalue
= swizzle_if_required(expr
, new_rvalue
);
489 this->progress
= true;
493 do_algebraic(exec_list
*instructions
)
495 ir_algebraic_visitor v
;
497 visit_list_elements(&v
, instructions
);