blob: f7a0599f425e4f2dc5fe262925d8b4b603c591f0 [file] [log] [blame]
Eric Anholt8bebbeb2010-08-09 17:03:46 -07001/*
2 * Constantright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * constant 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, constant, 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 constantright 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 CONSTANTRIGHT 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_propagation.cpp
26 *
27 * Tracks assignments of constants to channels of variables, and
28 * usage of those constant channels with direct usage of the constants.
29 *
30 * This can lead to constant folding and algebraic optimizations in
31 * those later expressions, while causing no increase in instruction
32 * count (due to constants being generally free to load from a
33 * constant push buffer or as instruction immediate values) and
34 * possibly reducing register pressure.
35 */
36
37#include "ir.h"
38#include "ir_visitor.h"
Eric Anholt42cab132010-08-13 20:50:10 -070039#include "ir_rvalue_visitor.h"
Eric Anholt8bebbeb2010-08-09 17:03:46 -070040#include "ir_basic_block.h"
41#include "ir_optimization.h"
42#include "glsl_types.h"
43
44class acp_entry : public exec_node
45{
46public:
47 acp_entry(ir_variable *var, unsigned write_mask, ir_constant *constant)
48 {
49 assert(var);
50 assert(constant);
51 this->var = var;
52 this->write_mask = write_mask;
53 this->constant = constant;
54 }
55
56 ir_variable *var;
57 ir_constant *constant;
58 unsigned write_mask;
59};
60
61
62class kill_entry : public exec_node
63{
64public:
65 kill_entry(ir_variable *var, unsigned write_mask)
66 {
67 assert(var);
68 this->var = var;
69 this->write_mask = write_mask;
70 }
71
72 ir_variable *var;
73 unsigned write_mask;
74};
75
Eric Anholt42cab132010-08-13 20:50:10 -070076class ir_constant_propagation_visitor : public ir_rvalue_visitor {
Eric Anholt8bebbeb2010-08-09 17:03:46 -070077public:
78 ir_constant_propagation_visitor()
79 {
80 progress = false;
81 mem_ctx = talloc_new(0);
82 this->acp = new(mem_ctx) exec_list;
83 this->kills = new(mem_ctx) exec_list;
84 }
85 ~ir_constant_propagation_visitor()
86 {
87 talloc_free(mem_ctx);
88 }
89
90 virtual ir_visitor_status visit_enter(class ir_loop *);
91 virtual ir_visitor_status visit_enter(class ir_function_signature *);
92 virtual ir_visitor_status visit_enter(class ir_function *);
Ian Romanick4e5b41c2010-08-27 16:22:36 -070093 virtual ir_visitor_status visit_leave(class ir_assignment *);
Eric Anholt8bebbeb2010-08-09 17:03:46 -070094 virtual ir_visitor_status visit_enter(class ir_call *);
95 virtual ir_visitor_status visit_enter(class ir_if *);
Eric Anholt8bebbeb2010-08-09 17:03:46 -070096
97 void add_constant(ir_assignment *ir);
98 void kill(ir_variable *ir, unsigned write_mask);
99 void handle_if_block(exec_list *instructions);
100 void handle_rvalue(ir_rvalue **rvalue);
101
102 /** List of acp_entry: The available constants to propagate */
103 exec_list *acp;
104
105 /**
106 * List of kill_entry: The masks of variables whose values were
107 * killed in this block.
108 */
109 exec_list *kills;
110
111 bool progress;
112
113 bool killed_all;
114
115 void *mem_ctx;
116};
117
118
119void
120ir_constant_propagation_visitor::handle_rvalue(ir_rvalue **rvalue)
121{
Ian Romanick4e5b41c2010-08-27 16:22:36 -0700122 if (this->in_assignee || !*rvalue)
Eric Anholt8bebbeb2010-08-09 17:03:46 -0700123 return;
124
125 const glsl_type *type = (*rvalue)->type;
126 if (!type->is_scalar() && !type->is_vector())
127 return;
128
129 ir_swizzle *swiz = NULL;
130 ir_dereference_variable *deref = (*rvalue)->as_dereference_variable();
131 if (!deref) {
132 swiz = (*rvalue)->as_swizzle();
133 if (!swiz)
134 return;
135
136 deref = swiz->val->as_dereference_variable();
137 if (!deref)
138 return;
139 }
140
141 ir_constant_data data;
142 memset(&data, 0, sizeof(data));
143
144 for (unsigned int i = 0; i < type->components(); i++) {
145 int channel;
146 acp_entry *found = NULL;
147
148 if (swiz) {
149 switch (i) {
150 case 0: channel = swiz->mask.x; break;
151 case 1: channel = swiz->mask.y; break;
152 case 2: channel = swiz->mask.z; break;
153 case 3: channel = swiz->mask.w; break;
154 default: assert(!"shouldn't be reached"); channel = 0; break;
155 }
156 } else {
157 channel = i;
158 }
159
160 foreach_iter(exec_list_iterator, iter, *this->acp) {
161 acp_entry *entry = (acp_entry *)iter.get();
162 if (entry->var == deref->var && entry->write_mask & (1 << channel)) {
163 found = entry;
164 break;
165 }
166 }
167
168 if (!found)
169 return;
170
171 switch (type->base_type) {
172 case GLSL_TYPE_FLOAT:
173 data.f[i] = found->constant->value.f[channel];
174 break;
175 case GLSL_TYPE_INT:
176 data.i[i] = found->constant->value.i[channel];
177 break;
178 case GLSL_TYPE_UINT:
179 data.u[i] = found->constant->value.u[channel];
180 break;
181 case GLSL_TYPE_BOOL:
182 data.b[i] = found->constant->value.b[channel];
183 break;
184 default:
185 assert(!"not reached");
186 break;
187 }
188 }
189
190 *rvalue = new(talloc_parent(deref)) ir_constant(type, &data);
191 this->progress = true;
192}
193
194ir_visitor_status
195ir_constant_propagation_visitor::visit_enter(ir_function_signature *ir)
196{
197 /* Treat entry into a function signature as a completely separate
198 * block. Any instructions at global scope will be shuffled into
199 * main() at link time, so they're irrelevant to us.
200 */
201 exec_list *orig_acp = this->acp;
202 exec_list *orig_kills = this->kills;
203 bool orig_killed_all = this->killed_all;
204
205 this->acp = new(mem_ctx) exec_list;
206 this->kills = new(mem_ctx) exec_list;
207 this->killed_all = false;
208
209 visit_list_elements(this, &ir->body);
210
211 this->kills = orig_kills;
212 this->acp = orig_acp;
213 this->killed_all = orig_killed_all;
214
215 return visit_continue_with_parent;
216}
217
218ir_visitor_status
Ian Romanick4e5b41c2010-08-27 16:22:36 -0700219ir_constant_propagation_visitor::visit_leave(ir_assignment *ir)
Eric Anholt8bebbeb2010-08-09 17:03:46 -0700220{
Ian Romanick4e5b41c2010-08-27 16:22:36 -0700221 if (this->in_assignee)
222 return visit_continue;
Eric Anholt8bebbeb2010-08-09 17:03:46 -0700223
Eric Anholt8bebbeb2010-08-09 17:03:46 -0700224 kill(ir->lhs->variable_referenced(), ir->write_mask);
225
226 add_constant(ir);
227
Ian Romanick4e5b41c2010-08-27 16:22:36 -0700228 return visit_continue;
Eric Anholt8bebbeb2010-08-09 17:03:46 -0700229}
230
231ir_visitor_status
232ir_constant_propagation_visitor::visit_enter(ir_function *ir)
233{
234 (void) ir;
235 return visit_continue;
236}
237
238ir_visitor_status
239ir_constant_propagation_visitor::visit_enter(ir_call *ir)
240{
241 /* Do constant propagation on call parameters, but skip any out params */
242 exec_list_iterator sig_param_iter = ir->get_callee()->parameters.iterator();
243 foreach_iter(exec_list_iterator, iter, ir->actual_parameters) {
244 ir_variable *sig_param = (ir_variable *)sig_param_iter.get();
245 ir_rvalue *param = (ir_rvalue *)iter.get();
246 if (sig_param->mode != ir_var_out && sig_param->mode != ir_var_inout) {
247 ir_rvalue *new_param = param;
248 handle_rvalue(&new_param);
249 if (new_param != param)
250 param->replace_with(new_param);
251 else
252 param->accept(this);
253 }
254 sig_param_iter.next();
255 }
256
257 /* Since we're unlinked, we don't (necssarily) know the side effects of
258 * this call. So kill all copies.
259 */
260 acp->make_empty();
261 this->killed_all = true;
262
263 return visit_continue_with_parent;
264}
265
266void
267ir_constant_propagation_visitor::handle_if_block(exec_list *instructions)
268{
269 exec_list *orig_acp = this->acp;
270 exec_list *orig_kills = this->kills;
271 bool orig_killed_all = this->killed_all;
272
273 this->acp = new(mem_ctx) exec_list;
274 this->kills = new(mem_ctx) exec_list;
275 this->killed_all = false;
276
277 /* Populate the initial acp with a constant of the original */
278 foreach_iter(exec_list_iterator, iter, *orig_acp) {
279 acp_entry *a = (acp_entry *)iter.get();
280 this->acp->push_tail(new(this->mem_ctx) acp_entry(a->var, a->write_mask,
281 a->constant));
282 }
283
284 visit_list_elements(this, instructions);
285
286 if (this->killed_all) {
287 orig_acp->make_empty();
288 }
289
290 exec_list *new_kills = this->kills;
291 this->kills = orig_kills;
292 this->acp = orig_acp;
293 this->killed_all = this->killed_all || orig_killed_all;
294
295 foreach_iter(exec_list_iterator, iter, *new_kills) {
296 kill_entry *k = (kill_entry *)iter.get();
297 kill(k->var, k->write_mask);
298 }
299}
300
301ir_visitor_status
302ir_constant_propagation_visitor::visit_enter(ir_if *ir)
303{
304 ir->condition->accept(this);
305 handle_rvalue(&ir->condition);
306
307 handle_if_block(&ir->then_instructions);
308 handle_if_block(&ir->else_instructions);
309
310 /* handle_if_block() already descended into the children. */
311 return visit_continue_with_parent;
312}
313
314ir_visitor_status
Eric Anholt8bebbeb2010-08-09 17:03:46 -0700315ir_constant_propagation_visitor::visit_enter(ir_loop *ir)
316{
317 exec_list *orig_acp = this->acp;
318 exec_list *orig_kills = this->kills;
319 bool orig_killed_all = this->killed_all;
320
321 /* FINISHME: For now, the initial acp for loops is totally empty.
322 * We could go through once, then go through again with the acp
323 * cloned minus the killed entries after the first run through.
324 */
325 this->acp = new(mem_ctx) exec_list;
326 this->kills = new(mem_ctx) exec_list;
327 this->killed_all = false;
328
329 visit_list_elements(this, &ir->body_instructions);
330
331 if (this->killed_all) {
332 orig_acp->make_empty();
333 }
334
335 exec_list *new_kills = this->kills;
336 this->kills = orig_kills;
337 this->acp = orig_acp;
338 this->killed_all = this->killed_all || orig_killed_all;
339
340 foreach_iter(exec_list_iterator, iter, *new_kills) {
341 kill_entry *k = (kill_entry *)iter.get();
342 kill(k->var, k->write_mask);
343 }
344
345 /* already descended into the children. */
346 return visit_continue_with_parent;
347}
348
349void
350ir_constant_propagation_visitor::kill(ir_variable *var, unsigned write_mask)
351{
352 assert(var != NULL);
353
354 /* We don't track non-vectors. */
355 if (!var->type->is_vector() && !var->type->is_scalar())
356 return;
357
358 /* Remove any entries currently in the ACP for this kill. */
359 foreach_iter(exec_list_iterator, iter, *this->acp) {
360 acp_entry *entry = (acp_entry *)iter.get();
361
362 if (entry->var == var) {
363 entry->write_mask &= ~write_mask;
364 if (entry->write_mask == 0)
365 entry->remove();
366 }
367 }
368
369 /* Add this writemask of the variable to the list of killed
370 * variables in this block.
371 */
372 foreach_iter(exec_list_iterator, iter, *this->kills) {
373 kill_entry *entry = (kill_entry *)iter.get();
374
375 if (entry->var == var) {
376 entry->write_mask |= write_mask;
377 return;
378 }
379 }
380 /* Not already in the list. Make new entry. */
381 this->kills->push_tail(new(this->mem_ctx) kill_entry(var, write_mask));
382}
383
384/**
385 * Adds an entry to the available constant list if it's a plain assignment
386 * of a variable to a variable.
387 */
388void
389ir_constant_propagation_visitor::add_constant(ir_assignment *ir)
390{
391 acp_entry *entry;
392
393 if (ir->condition) {
394 ir_constant *condition = ir->condition->as_constant();
395 if (!condition || !condition->value.b[0])
396 return;
397 }
398
399 if (!ir->write_mask)
400 return;
401
402 ir_dereference_variable *deref = ir->lhs->as_dereference_variable();
403 ir_constant *constant = ir->rhs->as_constant();
404
405 if (!deref || !constant)
406 return;
407
408 /* Only do constant propagation on vectors. Constant matrices,
409 * arrays, or structures would require more work elsewhere.
410 */
411 if (!deref->var->type->is_vector() && !deref->var->type->is_scalar())
412 return;
413
414 entry = new(this->mem_ctx) acp_entry(deref->var, ir->write_mask, constant);
415 this->acp->push_tail(entry);
416}
417
418/**
419 * Does a constant propagation pass on the code present in the instruction stream.
420 */
421bool
422do_constant_propagation(exec_list *instructions)
423{
424 ir_constant_propagation_visitor v;
425
426 visit_list_elements(&v, instructions);
427
428 return v.progress;
429}