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_constant_folding.cpp
26 * Replace constant-valued expressions with references to constant values.
30 #include "ir_visitor.h"
31 #include "ir_optimization.h"
32 #include "glsl_types.h"
35 * Visitor class for replacing expressions with ir_constant values.
38 class ir_constant_folding_visitor
: public ir_visitor
{
40 ir_constant_folding_visitor()
45 virtual ~ir_constant_folding_visitor()
53 * As typical for the visitor pattern, there must be one \c visit method for
54 * each concrete subclass of \c ir_instruction. Virtual base classes within
55 * the hierarchy should not have \c visit methods.
58 virtual void visit(ir_variable
*);
59 virtual void visit(ir_function_signature
*);
60 virtual void visit(ir_function
*);
61 virtual void visit(ir_expression
*);
62 virtual void visit(ir_texture
*);
63 virtual void visit(ir_swizzle
*);
64 virtual void visit(ir_dereference_variable
*);
65 virtual void visit(ir_dereference_array
*);
66 virtual void visit(ir_dereference_record
*);
67 virtual void visit(ir_assignment
*);
68 virtual void visit(ir_constant
*);
69 virtual void visit(ir_call
*);
70 virtual void visit(ir_return
*);
71 virtual void visit(ir_if
*);
72 virtual void visit(ir_loop
*);
73 virtual void visit(ir_loop_jump
*);
78 ir_constant_folding_visitor::visit(ir_variable
*ir
)
85 ir_constant_folding_visitor::visit(ir_function_signature
*ir
)
87 visit_exec_list(&ir
->body
, this);
92 ir_constant_folding_visitor::visit(ir_function
*ir
)
94 foreach_iter(exec_list_iterator
, iter
, *ir
) {
95 ir_function_signature
*const sig
= (ir_function_signature
*) iter
.get();
101 ir_constant_folding_visitor::visit(ir_expression
*ir
)
104 unsigned int operand
;
106 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
107 op
[operand
] = ir
->operands
[operand
]->constant_expression_value();
109 ir
->operands
[operand
] = op
[operand
];
111 ir
->operands
[operand
]->accept(this);
118 ir_constant_folding_visitor::visit(ir_texture
*ir
)
120 // FINISHME: Do stuff with texture lookups
126 ir_constant_folding_visitor::visit(ir_swizzle
*ir
)
128 ir
->val
->accept(this);
133 ir_constant_folding_visitor::visit(ir_dereference_variable
*ir
)
140 ir_constant_folding_visitor::visit(ir_dereference_array
*ir
)
142 ir_constant
*const_val
=
143 ir
->array_index
->constant_expression_value();
146 ir
->array_index
= const_val
;
148 ir
->array_index
->accept(this);
150 ir
->array
->accept(this);
155 ir_constant_folding_visitor::visit(ir_dereference_record
*ir
)
157 ir
->record
->accept(this);
162 ir_constant_folding_visitor::visit(ir_assignment
*ir
)
164 ir_constant
*const_val
= ir
->rhs
->constant_expression_value();
168 ir
->rhs
->accept(this);
173 ir_constant_folding_visitor::visit(ir_constant
*ir
)
180 ir_constant_folding_visitor::visit(ir_call
*ir
)
187 ir_constant_folding_visitor::visit(ir_return
*ir
)
194 ir_constant_folding_visitor::visit(ir_if
*ir
)
196 ir_constant
*const_val
= ir
->condition
->constant_expression_value();
198 ir
->condition
= const_val
;
200 ir
->condition
->accept(this);
202 visit_exec_list(&ir
->then_instructions
, this);
203 visit_exec_list(&ir
->else_instructions
, this);
208 ir_constant_folding_visitor::visit(ir_loop
*ir
)
215 ir_constant_folding_visitor::visit(ir_loop_jump
*ir
)
221 do_constant_folding(exec_list
*instructions
)
223 ir_constant_folding_visitor constant_folding
;
225 visit_exec_list(instructions
, &constant_folding
);
227 /* FINISHME: Return real progress. */