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.
31 #include "ir_visitor.h"
32 #include "ir_optimization.h"
33 #include "glsl_types.h"
36 * Visitor class for replacing expressions with ir_constant values.
39 class ir_constant_folding_visitor
: public ir_visitor
{
41 ir_constant_folding_visitor()
46 virtual ~ir_constant_folding_visitor()
54 * As typical for the visitor pattern, there must be one \c visit method for
55 * each concrete subclass of \c ir_instruction. Virtual base classes within
56 * the hierarchy should not have \c visit methods.
59 virtual void visit(ir_variable
*);
60 virtual void visit(ir_function_signature
*);
61 virtual void visit(ir_function
*);
62 virtual void visit(ir_expression
*);
63 virtual void visit(ir_swizzle
*);
64 virtual void visit(ir_dereference
*);
65 virtual void visit(ir_assignment
*);
66 virtual void visit(ir_constant
*);
67 virtual void visit(ir_call
*);
68 virtual void visit(ir_return
*);
69 virtual void visit(ir_if
*);
70 virtual void visit(ir_loop
*);
71 virtual void visit(ir_loop_jump
*);
76 ir_constant_folding_visitor::visit(ir_variable
*ir
)
83 ir_constant_folding_visitor::visit(ir_function_signature
*ir
)
85 visit_exec_list(&ir
->body
, this);
90 ir_constant_folding_visitor::visit(ir_function
*ir
)
96 ir_constant_folding_visitor::visit(ir_expression
*ir
)
101 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
102 op
[operand
] = ir
->operands
[operand
]->constant_expression_value();
104 ir
->operands
[operand
] = op
[operand
];
106 ir
->operands
[operand
]->accept(this);
113 ir_constant_folding_visitor::visit(ir_swizzle
*ir
)
115 ir
->val
->accept(this);
120 ir_constant_folding_visitor::visit(ir_dereference
*ir
)
122 if (ir
->mode
== ir_dereference::ir_reference_array
) {
123 ir_constant
*const_val
= ir
->selector
.array_index
->constant_expression_value();
125 ir
->selector
.array_index
= const_val
;
127 ir
->selector
.array_index
->accept(this);
129 ir
->var
->accept(this);
134 ir_constant_folding_visitor::visit(ir_assignment
*ir
)
136 ir_constant
*const_val
= ir
->rhs
->constant_expression_value();
140 ir
->rhs
->accept(this);
145 ir_constant_folding_visitor::visit(ir_constant
*ir
)
152 ir_constant_folding_visitor::visit(ir_call
*ir
)
159 ir_constant_folding_visitor::visit(ir_return
*ir
)
166 ir_constant_folding_visitor::visit(ir_if
*ir
)
168 ir_constant
*const_val
= ir
->condition
->constant_expression_value();
170 ir
->condition
= const_val
;
172 ir
->condition
->accept(this);
174 visit_exec_list(&ir
->then_instructions
, this);
175 visit_exec_list(&ir
->else_instructions
, this);
180 ir_constant_folding_visitor::visit(ir_loop
*ir
)
187 ir_constant_folding_visitor::visit(ir_loop_jump
*ir
)
193 do_constant_folding(exec_list
*instructions
)
195 ir_constant_folding_visitor constant_folding
;
197 visit_exec_list(instructions
, &constant_folding
);
199 /* FINISHME: Return real progress. */