glsl2: Refactor constant folding of rvalues to a function.
[mesa.git] / src / glsl / ir_constant_folding.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 ir_constant_folding.cpp
26 * Replace constant-valued expressions with references to constant values.
27 */
28
29 #include "ir.h"
30 #include "ir_visitor.h"
31 #include "ir_optimization.h"
32 #include "glsl_types.h"
33
34 /**
35 * Visitor class for replacing expressions with ir_constant values.
36 */
37
38 class ir_constant_folding_visitor : public ir_visitor {
39 public:
40 ir_constant_folding_visitor()
41 {
42 /* empty */
43 }
44
45 virtual ~ir_constant_folding_visitor()
46 {
47 /* empty */
48 }
49
50 /**
51 * \name Visit methods
52 *
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.
56 */
57 /*@{*/
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_discard *);
72 virtual void visit(ir_if *);
73 virtual void visit(ir_loop *);
74 virtual void visit(ir_loop_jump *);
75 /*@}*/
76
77 void fold_constant(ir_rvalue **rvalue);
78 };
79
80 void
81 ir_constant_folding_visitor::fold_constant(ir_rvalue **rvalue)
82 {
83 if ((*rvalue)->ir_type == ir_type_constant)
84 return;
85
86 ir_constant *constant = (*rvalue)->constant_expression_value();
87 if (constant) {
88 *rvalue = constant;
89 } else {
90 (*rvalue)->accept(this);
91 }
92 }
93
94 void
95 ir_constant_folding_visitor::visit(ir_variable *ir)
96 {
97 (void) ir;
98 }
99
100
101 void
102 ir_constant_folding_visitor::visit(ir_function_signature *ir)
103 {
104 visit_exec_list(&ir->body, this);
105 }
106
107
108 void
109 ir_constant_folding_visitor::visit(ir_function *ir)
110 {
111 foreach_iter(exec_list_iterator, iter, *ir) {
112 ir_function_signature *const sig = (ir_function_signature *) iter.get();
113 sig->accept(this);
114 }
115 }
116
117 void
118 ir_constant_folding_visitor::visit(ir_expression *ir)
119 {
120 unsigned int operand;
121
122 for (operand = 0; operand < ir->get_num_operands(); operand++) {
123 fold_constant(&ir->operands[operand]);
124 }
125 }
126
127
128 void
129 ir_constant_folding_visitor::visit(ir_texture *ir)
130 {
131 // FINISHME: Do stuff with texture lookups
132 (void) ir;
133 }
134
135
136 void
137 ir_constant_folding_visitor::visit(ir_swizzle *ir)
138 {
139 ir->val->accept(this);
140 }
141
142
143 void
144 ir_constant_folding_visitor::visit(ir_dereference_variable *ir)
145 {
146 (void) ir;
147 }
148
149
150 void
151 ir_constant_folding_visitor::visit(ir_dereference_array *ir)
152 {
153 fold_constant(&ir->array_index);
154 ir->array->accept(this);
155 }
156
157
158 void
159 ir_constant_folding_visitor::visit(ir_dereference_record *ir)
160 {
161 ir->record->accept(this);
162 }
163
164
165 void
166 ir_constant_folding_visitor::visit(ir_assignment *ir)
167 {
168 fold_constant(&ir->rhs);
169
170 if (ir->condition) {
171 /* If the condition is constant, either remove the condition or
172 * remove the never-executed assignment.
173 */
174 ir_constant *const_val = ir->condition->constant_expression_value();
175 if (const_val) {
176 if (const_val->value.b[0])
177 ir->condition = NULL;
178 else
179 ir->remove();
180 }
181 }
182 }
183
184
185 void
186 ir_constant_folding_visitor::visit(ir_constant *ir)
187 {
188 (void) ir;
189 }
190
191
192 void
193 ir_constant_folding_visitor::visit(ir_call *ir)
194 {
195 (void) ir;
196 }
197
198
199 void
200 ir_constant_folding_visitor::visit(ir_return *ir)
201 {
202 (void) ir;
203 }
204
205
206 void
207 ir_constant_folding_visitor::visit(ir_discard *ir)
208 {
209 (void) ir;
210 }
211
212
213 void
214 ir_constant_folding_visitor::visit(ir_if *ir)
215 {
216 fold_constant(&ir->condition);
217
218 visit_exec_list(&ir->then_instructions, this);
219 visit_exec_list(&ir->else_instructions, this);
220 }
221
222
223 void
224 ir_constant_folding_visitor::visit(ir_loop *ir)
225 {
226 (void) ir;
227 }
228
229
230 void
231 ir_constant_folding_visitor::visit(ir_loop_jump *ir)
232 {
233 (void) ir;
234 }
235
236 bool
237 do_constant_folding(exec_list *instructions)
238 {
239 ir_constant_folding_visitor constant_folding;
240
241 visit_exec_list(instructions, &constant_folding);
242
243 /* FINISHME: Return real progress. */
244 return false;
245 }