scons: Fix scons build.
[profile/ivi/mesa.git] / src / glsl / opt_algebraic.cpp
1 /*
2  * Copyright © 2010 Intel Corporation
3  *
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:
10  *
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
13  * Software.
14  *
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.
22  */
23
24 /**
25  * \file opt_algebraic.cpp
26  *
27  * Takes advantage of association, commutivity, and other algebraic
28  * properties to simplify expressions.
29  */
30
31 #include "ir.h"
32 #include "ir_visitor.h"
33 #include "ir_rvalue_visitor.h"
34 #include "ir_optimization.h"
35 #include "glsl_types.h"
36
37 namespace {
38
39 /**
40  * Visitor class for replacing expressions with ir_constant values.
41  */
42
43 class ir_algebraic_visitor : public ir_rvalue_visitor {
44 public:
45    ir_algebraic_visitor()
46    {
47       this->progress = false;
48       this->mem_ctx = NULL;
49    }
50
51    virtual ~ir_algebraic_visitor()
52    {
53    }
54
55    ir_rvalue *handle_expression(ir_expression *ir);
56    void handle_rvalue(ir_rvalue **rvalue);
57    bool reassociate_constant(ir_expression *ir1,
58                              int const_index,
59                              ir_constant *constant,
60                              ir_expression *ir2);
61    void reassociate_operands(ir_expression *ir1,
62                              int op1,
63                              ir_expression *ir2,
64                              int op2);
65    ir_rvalue *swizzle_if_required(ir_expression *expr,
66                                   ir_rvalue *operand);
67
68    void *mem_ctx;
69
70    bool progress;
71 };
72
73 } /* unnamed namespace */
74
75 static inline bool
76 is_vec_zero(ir_constant *ir)
77 {
78    return (ir == NULL) ? false : ir->is_zero();
79 }
80
81 static inline bool
82 is_vec_one(ir_constant *ir)
83 {
84    return (ir == NULL) ? false : ir->is_one();
85 }
86
87 static void
88 update_type(ir_expression *ir)
89 {
90    if (ir->operands[0]->type->is_vector())
91       ir->type = ir->operands[0]->type;
92    else
93       ir->type = ir->operands[1]->type;
94 }
95
96 void
97 ir_algebraic_visitor::reassociate_operands(ir_expression *ir1,
98                                            int op1,
99                                            ir_expression *ir2,
100                                            int op2)
101 {
102    ir_rvalue *temp = ir2->operands[op2];
103    ir2->operands[op2] = ir1->operands[op1];
104    ir1->operands[op1] = temp;
105
106    /* Update the type of ir2.  The type of ir1 won't have changed --
107     * base types matched, and at least one of the operands of the 2
108     * binops is still a vector if any of them were.
109     */
110    update_type(ir2);
111
112    this->progress = true;
113 }
114
115 /**
116  * Reassociates a constant down a tree of adds or multiplies.
117  *
118  * Consider (2 * (a * (b * 0.5))).  We want to send up with a * b.
119  */
120 bool
121 ir_algebraic_visitor::reassociate_constant(ir_expression *ir1, int const_index,
122                                            ir_constant *constant,
123                                            ir_expression *ir2)
124 {
125    if (!ir2 || ir1->operation != ir2->operation)
126       return false;
127
128    /* Don't want to even think about matrices. */
129    if (ir1->operands[0]->type->is_matrix() ||
130        ir1->operands[1]->type->is_matrix() ||
131        ir2->operands[0]->type->is_matrix() ||
132        ir2->operands[1]->type->is_matrix())
133       return false;
134
135    ir_constant *ir2_const[2];
136    ir2_const[0] = ir2->operands[0]->constant_expression_value();
137    ir2_const[1] = ir2->operands[1]->constant_expression_value();
138
139    if (ir2_const[0] && ir2_const[1])
140       return false;
141
142    if (ir2_const[0]) {
143       reassociate_operands(ir1, const_index, ir2, 1);
144       return true;
145    } else if (ir2_const[1]) {
146       reassociate_operands(ir1, const_index, ir2, 0);
147       return true;
148    }
149
150    if (reassociate_constant(ir1, const_index, constant,
151                             ir2->operands[0]->as_expression())) {
152       update_type(ir2);
153       return true;
154    }
155
156    if (reassociate_constant(ir1, const_index, constant,
157                             ir2->operands[1]->as_expression())) {
158       update_type(ir2);
159       return true;
160    }
161
162    return false;
163 }
164
165 /* When eliminating an expression and just returning one of its operands,
166  * we may need to swizzle that operand out to a vector if the expression was
167  * vector type.
168  */
169 ir_rvalue *
170 ir_algebraic_visitor::swizzle_if_required(ir_expression *expr,
171                                           ir_rvalue *operand)
172 {
173    if (expr->type->is_vector() && operand->type->is_scalar()) {
174       return new(mem_ctx) ir_swizzle(operand, 0, 0, 0, 0,
175                                      expr->type->vector_elements);
176    } else
177       return operand;
178 }
179
180 ir_rvalue *
181 ir_algebraic_visitor::handle_expression(ir_expression *ir)
182 {
183    ir_constant *op_const[2] = {NULL, NULL};
184    ir_expression *op_expr[2] = {NULL, NULL};
185    ir_expression *temp;
186    unsigned int i;
187
188    assert(ir->get_num_operands() <= 2);
189    for (i = 0; i < ir->get_num_operands(); i++) {
190       if (ir->operands[i]->type->is_matrix())
191          return ir;
192
193       op_const[i] = ir->operands[i]->constant_expression_value();
194       op_expr[i] = ir->operands[i]->as_expression();
195    }
196
197    if (this->mem_ctx == NULL)
198       this->mem_ctx = ralloc_parent(ir);
199
200    switch (ir->operation) {
201    case ir_unop_logic_not: {
202       enum ir_expression_operation new_op = ir_unop_logic_not;
203
204       if (op_expr[0] == NULL)
205          break;
206
207       switch (op_expr[0]->operation) {
208       case ir_binop_less:    new_op = ir_binop_gequal;  break;
209       case ir_binop_greater: new_op = ir_binop_lequal;  break;
210       case ir_binop_lequal:  new_op = ir_binop_greater; break;
211       case ir_binop_gequal:  new_op = ir_binop_less;    break;
212       case ir_binop_equal:   new_op = ir_binop_nequal;  break;
213       case ir_binop_nequal:  new_op = ir_binop_equal;   break;
214       case ir_binop_all_equal:   new_op = ir_binop_any_nequal;  break;
215       case ir_binop_any_nequal:  new_op = ir_binop_all_equal;   break;
216
217       default:
218          /* The default case handler is here to silence a warning from GCC.
219           */
220          break;
221       }
222
223       if (new_op != ir_unop_logic_not) {
224          this->progress = true;
225          return new(mem_ctx) ir_expression(new_op,
226                                            ir->type,
227                                            op_expr[0]->operands[0],
228                                            op_expr[0]->operands[1]);
229       }
230
231       break;
232    }
233
234    case ir_binop_add:
235       if (is_vec_zero(op_const[0])) {
236          this->progress = true;
237          return swizzle_if_required(ir, ir->operands[1]);
238       }
239       if (is_vec_zero(op_const[1])) {
240          this->progress = true;
241          return swizzle_if_required(ir, ir->operands[0]);
242       }
243
244       /* Reassociate addition of constants so that we can do constant
245        * folding.
246        */
247       if (op_const[0] && !op_const[1])
248          reassociate_constant(ir, 0, op_const[0],
249                               ir->operands[1]->as_expression());
250       if (op_const[1] && !op_const[0])
251          reassociate_constant(ir, 1, op_const[1],
252                               ir->operands[0]->as_expression());
253       break;
254
255    case ir_binop_sub:
256       if (is_vec_zero(op_const[0])) {
257          this->progress = true;
258          temp = new(mem_ctx) ir_expression(ir_unop_neg,
259                                            ir->operands[1]->type,
260                                            ir->operands[1],
261                                            NULL);
262          return swizzle_if_required(ir, temp);
263       }
264       if (is_vec_zero(op_const[1])) {
265          this->progress = true;
266          return swizzle_if_required(ir, ir->operands[0]);
267       }
268       break;
269
270    case ir_binop_mul:
271       if (is_vec_one(op_const[0])) {
272          this->progress = true;
273          return swizzle_if_required(ir, ir->operands[1]);
274       }
275       if (is_vec_one(op_const[1])) {
276          this->progress = true;
277          return swizzle_if_required(ir, ir->operands[0]);
278       }
279
280       if (is_vec_zero(op_const[0]) || is_vec_zero(op_const[1])) {
281          this->progress = true;
282          return ir_constant::zero(ir, ir->type);
283       }
284
285       /* Reassociate multiplication of constants so that we can do
286        * constant folding.
287        */
288       if (op_const[0] && !op_const[1])
289          reassociate_constant(ir, 0, op_const[0],
290                               ir->operands[1]->as_expression());
291       if (op_const[1] && !op_const[0])
292          reassociate_constant(ir, 1, op_const[1],
293                               ir->operands[0]->as_expression());
294
295       break;
296
297    case ir_binop_div:
298       if (is_vec_one(op_const[0]) && ir->type->base_type == GLSL_TYPE_FLOAT) {
299          this->progress = true;
300          temp = new(mem_ctx) ir_expression(ir_unop_rcp,
301                                            ir->operands[1]->type,
302                                            ir->operands[1],
303                                            NULL);
304          return swizzle_if_required(ir, temp);
305       }
306       if (is_vec_one(op_const[1])) {
307          this->progress = true;
308          return swizzle_if_required(ir, ir->operands[0]);
309       }
310       break;
311
312    case ir_binop_logic_and:
313       /* FINISHME: Also simplify (a && a) to (a). */
314       if (is_vec_one(op_const[0])) {
315          this->progress = true;
316          return ir->operands[1];
317       } else if (is_vec_one(op_const[1])) {
318          this->progress = true;
319          return ir->operands[0];
320       } else if (is_vec_zero(op_const[0]) || is_vec_zero(op_const[1])) {
321          this->progress = true;
322          return ir_constant::zero(mem_ctx, ir->type);
323       }
324       break;
325
326    case ir_binop_logic_xor:
327       /* FINISHME: Also simplify (a ^^ a) to (false). */
328       if (is_vec_zero(op_const[0])) {
329          this->progress = true;
330          return ir->operands[1];
331       } else if (is_vec_zero(op_const[1])) {
332          this->progress = true;
333          return ir->operands[0];
334       } else if (is_vec_one(op_const[0])) {
335          this->progress = true;
336          return new(mem_ctx) ir_expression(ir_unop_logic_not, ir->type,
337                                            ir->operands[1], NULL);
338       } else if (is_vec_one(op_const[1])) {
339          this->progress = true;
340          return new(mem_ctx) ir_expression(ir_unop_logic_not, ir->type,
341                                            ir->operands[0], NULL);
342       }
343       break;
344
345    case ir_binop_logic_or:
346       /* FINISHME: Also simplify (a || a) to (a). */
347       if (is_vec_zero(op_const[0])) {
348          this->progress = true;
349          return ir->operands[1];
350       } else if (is_vec_zero(op_const[1])) {
351          this->progress = true;
352          return ir->operands[0];
353       } else if (is_vec_one(op_const[0]) || is_vec_one(op_const[1])) {
354          ir_constant_data data;
355
356          for (unsigned i = 0; i < 16; i++)
357             data.b[i] = true;
358
359          this->progress = true;
360          return new(mem_ctx) ir_constant(ir->type, &data);
361       }
362       break;
363
364    case ir_unop_rcp:
365       if (op_expr[0] && op_expr[0]->operation == ir_unop_rcp) {
366          this->progress = true;
367          return op_expr[0]->operands[0];
368       }
369
370       /* FINISHME: We should do rcp(rsq(x)) -> sqrt(x) for some
371        * backends, except that some backends will have done sqrt ->
372        * rcp(rsq(x)) and we don't want to undo it for them.
373        */
374
375       /* As far as we know, all backends are OK with rsq. */
376       if (op_expr[0] && op_expr[0]->operation == ir_unop_sqrt) {
377          this->progress = true;
378          temp = new(mem_ctx) ir_expression(ir_unop_rsq,
379                                            op_expr[0]->operands[0]->type,
380                                            op_expr[0]->operands[0],
381                                            NULL);
382          return swizzle_if_required(ir, temp);
383       }
384
385       break;
386
387    default:
388       break;
389    }
390
391    return ir;
392 }
393
394 void
395 ir_algebraic_visitor::handle_rvalue(ir_rvalue **rvalue)
396 {
397    if (!*rvalue)
398       return;
399
400    ir_expression *expr = (*rvalue)->as_expression();
401    if (!expr || expr->operation == ir_quadop_vector)
402       return;
403
404    *rvalue = handle_expression(expr);
405 }
406
407 bool
408 do_algebraic(exec_list *instructions)
409 {
410    ir_algebraic_visitor v;
411
412    visit_list_elements(&v, instructions);
413
414    return v.progress;
415 }