66a92e9f3b632b72f5953c60ae936ff9b5234d46
[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
78 void
79 ir_constant_folding_visitor::visit(ir_variable *ir)
80 {
81 (void) ir;
82 }
83
84
85 void
86 ir_constant_folding_visitor::visit(ir_function_signature *ir)
87 {
88 visit_exec_list(&ir->body, this);
89 }
90
91
92 void
93 ir_constant_folding_visitor::visit(ir_function *ir)
94 {
95 foreach_iter(exec_list_iterator, iter, *ir) {
96 ir_function_signature *const sig = (ir_function_signature *) iter.get();
97 sig->accept(this);
98 }
99 }
100
101 void
102 ir_constant_folding_visitor::visit(ir_expression *ir)
103 {
104 ir_constant *op[2];
105 unsigned int operand;
106
107 for (operand = 0; operand < ir->get_num_operands(); operand++) {
108 op[operand] = ir->operands[operand]->constant_expression_value();
109 if (op[operand]) {
110 ir->operands[operand] = op[operand];
111 } else {
112 ir->operands[operand]->accept(this);
113 }
114 }
115 }
116
117
118 void
119 ir_constant_folding_visitor::visit(ir_texture *ir)
120 {
121 // FINISHME: Do stuff with texture lookups
122 (void) ir;
123 }
124
125
126 void
127 ir_constant_folding_visitor::visit(ir_swizzle *ir)
128 {
129 ir->val->accept(this);
130 }
131
132
133 void
134 ir_constant_folding_visitor::visit(ir_dereference_variable *ir)
135 {
136 (void) ir;
137 }
138
139
140 void
141 ir_constant_folding_visitor::visit(ir_dereference_array *ir)
142 {
143 ir_constant *const_val =
144 ir->array_index->constant_expression_value();
145
146 if (const_val)
147 ir->array_index = const_val;
148 else
149 ir->array_index->accept(this);
150
151 ir->array->accept(this);
152 }
153
154
155 void
156 ir_constant_folding_visitor::visit(ir_dereference_record *ir)
157 {
158 ir->record->accept(this);
159 }
160
161
162 void
163 ir_constant_folding_visitor::visit(ir_assignment *ir)
164 {
165 ir_constant *const_val = ir->rhs->constant_expression_value();
166 if (const_val)
167 ir->rhs = const_val;
168 else
169 ir->rhs->accept(this);
170
171 if (ir->condition) {
172 /* If the condition is constant, either remove the condition or
173 * remove the never-executed assignment.
174 */
175 const_val = ir->condition->constant_expression_value();
176 if (const_val) {
177 if (const_val->value.b[0])
178 ir->condition = NULL;
179 else
180 ir->remove();
181 }
182 }
183 }
184
185
186 void
187 ir_constant_folding_visitor::visit(ir_constant *ir)
188 {
189 (void) ir;
190 }
191
192
193 void
194 ir_constant_folding_visitor::visit(ir_call *ir)
195 {
196 (void) ir;
197 }
198
199
200 void
201 ir_constant_folding_visitor::visit(ir_return *ir)
202 {
203 (void) ir;
204 }
205
206
207 void
208 ir_constant_folding_visitor::visit(ir_discard *ir)
209 {
210 (void) ir;
211 }
212
213
214 void
215 ir_constant_folding_visitor::visit(ir_if *ir)
216 {
217 ir_constant *const_val = ir->condition->constant_expression_value();
218 if (const_val)
219 ir->condition = const_val;
220 else
221 ir->condition->accept(this);
222
223 visit_exec_list(&ir->then_instructions, this);
224 visit_exec_list(&ir->else_instructions, this);
225 }
226
227
228 void
229 ir_constant_folding_visitor::visit(ir_loop *ir)
230 {
231 (void) ir;
232 }
233
234
235 void
236 ir_constant_folding_visitor::visit(ir_loop_jump *ir)
237 {
238 (void) ir;
239 }
240
241 bool
242 do_constant_folding(exec_list *instructions)
243 {
244 ir_constant_folding_visitor constant_folding;
245
246 visit_exec_list(instructions, &constant_folding);
247
248 /* FINISHME: Return real progress. */
249 return false;
250 }