glsl: Make opt_copy_propagation actually propagate into loops.
[mesa.git] / src / compiler / glsl / opt_copy_propagation.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 opt_copy_propagation.cpp
26 *
27 * Moves usage of recently-copied variables to the previous copy of
28 * the variable.
29 *
30 * This should reduce the number of MOV instructions in the generated
31 * programs unless copy propagation is also done on the LIR, and may
32 * help anyway by triggering other optimizations that live in the HIR.
33 */
34
35 #include "ir.h"
36 #include "ir_visitor.h"
37 #include "ir_basic_block.h"
38 #include "ir_optimization.h"
39 #include "compiler/glsl_types.h"
40
41 namespace {
42
43 class acp_entry : public exec_node
44 {
45 public:
46 acp_entry(ir_variable *lhs, ir_variable *rhs)
47 {
48 assert(lhs);
49 assert(rhs);
50 this->lhs = lhs;
51 this->rhs = rhs;
52 }
53
54 ir_variable *lhs;
55 ir_variable *rhs;
56 };
57
58
59 class kill_entry : public exec_node
60 {
61 public:
62 kill_entry(ir_variable *var)
63 {
64 assert(var);
65 this->var = var;
66 }
67
68 ir_variable *var;
69 };
70
71 class ir_copy_propagation_visitor : public ir_hierarchical_visitor {
72 public:
73 ir_copy_propagation_visitor()
74 {
75 progress = false;
76 mem_ctx = ralloc_context(0);
77 this->acp = new(mem_ctx) exec_list;
78 this->kills = new(mem_ctx) exec_list;
79 }
80 ~ir_copy_propagation_visitor()
81 {
82 ralloc_free(mem_ctx);
83 }
84
85 virtual ir_visitor_status visit(class ir_dereference_variable *);
86 void handle_loop(class ir_loop *, bool keep_acp);
87 virtual ir_visitor_status visit_enter(class ir_loop *);
88 virtual ir_visitor_status visit_enter(class ir_function_signature *);
89 virtual ir_visitor_status visit_enter(class ir_function *);
90 virtual ir_visitor_status visit_leave(class ir_assignment *);
91 virtual ir_visitor_status visit_enter(class ir_call *);
92 virtual ir_visitor_status visit_enter(class ir_if *);
93
94 void add_copy(ir_assignment *ir);
95 void kill(ir_variable *ir);
96 void handle_if_block(exec_list *instructions);
97
98 /** List of acp_entry: The available copies to propagate */
99 exec_list *acp;
100 /**
101 * List of kill_entry: The variables whose values were killed in this
102 * block.
103 */
104 exec_list *kills;
105
106 bool progress;
107
108 bool killed_all;
109
110 void *mem_ctx;
111 };
112
113 } /* unnamed namespace */
114
115 ir_visitor_status
116 ir_copy_propagation_visitor::visit_enter(ir_function_signature *ir)
117 {
118 /* Treat entry into a function signature as a completely separate
119 * block. Any instructions at global scope will be shuffled into
120 * main() at link time, so they're irrelevant to us.
121 */
122 exec_list *orig_acp = this->acp;
123 exec_list *orig_kills = this->kills;
124 bool orig_killed_all = this->killed_all;
125
126 this->acp = new(mem_ctx) exec_list;
127 this->kills = new(mem_ctx) exec_list;
128 this->killed_all = false;
129
130 visit_list_elements(this, &ir->body);
131
132 ralloc_free(this->acp);
133 ralloc_free(this->kills);
134
135 this->kills = orig_kills;
136 this->acp = orig_acp;
137 this->killed_all = orig_killed_all;
138
139 return visit_continue_with_parent;
140 }
141
142 ir_visitor_status
143 ir_copy_propagation_visitor::visit_leave(ir_assignment *ir)
144 {
145 kill(ir->lhs->variable_referenced());
146
147 add_copy(ir);
148
149 return visit_continue;
150 }
151
152 ir_visitor_status
153 ir_copy_propagation_visitor::visit_enter(ir_function *ir)
154 {
155 (void) ir;
156 return visit_continue;
157 }
158
159 /**
160 * Replaces dereferences of ACP RHS variables with ACP LHS variables.
161 *
162 * This is where the actual copy propagation occurs. Note that the
163 * rewriting of ir_dereference means that the ir_dereference instance
164 * must not be shared by multiple IR operations!
165 */
166 ir_visitor_status
167 ir_copy_propagation_visitor::visit(ir_dereference_variable *ir)
168 {
169 if (this->in_assignee)
170 return visit_continue;
171
172 ir_variable *var = ir->var;
173
174 foreach_in_list(acp_entry, entry, this->acp) {
175 if (var == entry->lhs) {
176 ir->var = entry->rhs;
177 this->progress = true;
178 break;
179 }
180 }
181
182 return visit_continue;
183 }
184
185
186 ir_visitor_status
187 ir_copy_propagation_visitor::visit_enter(ir_call *ir)
188 {
189 /* Do copy propagation on call parameters, but skip any out params */
190 foreach_two_lists(formal_node, &ir->callee->parameters,
191 actual_node, &ir->actual_parameters) {
192 ir_variable *sig_param = (ir_variable *) formal_node;
193 ir_rvalue *ir = (ir_rvalue *) actual_node;
194 if (sig_param->data.mode != ir_var_function_out
195 && sig_param->data.mode != ir_var_function_inout) {
196 ir->accept(this);
197 }
198 }
199
200 /* Since we're unlinked, we don't (necessarily) know the side effects of
201 * this call. So kill all copies.
202 */
203 acp->make_empty();
204 this->killed_all = true;
205
206 return visit_continue_with_parent;
207 }
208
209 void
210 ir_copy_propagation_visitor::handle_if_block(exec_list *instructions)
211 {
212 exec_list *orig_acp = this->acp;
213 exec_list *orig_kills = this->kills;
214 bool orig_killed_all = this->killed_all;
215
216 this->acp = new(mem_ctx) exec_list;
217 this->kills = new(mem_ctx) exec_list;
218 this->killed_all = false;
219
220 /* Populate the initial acp with a copy of the original */
221 foreach_in_list(acp_entry, a, orig_acp) {
222 this->acp->push_tail(new(this->acp) acp_entry(a->lhs, a->rhs));
223 }
224
225 visit_list_elements(this, instructions);
226
227 if (this->killed_all) {
228 orig_acp->make_empty();
229 }
230
231 exec_list *new_kills = this->kills;
232 this->kills = orig_kills;
233 ralloc_free(this->acp);
234 this->acp = orig_acp;
235 this->killed_all = this->killed_all || orig_killed_all;
236
237 foreach_in_list(kill_entry, k, new_kills) {
238 kill(k->var);
239 }
240
241 ralloc_free(new_kills);
242 }
243
244 ir_visitor_status
245 ir_copy_propagation_visitor::visit_enter(ir_if *ir)
246 {
247 ir->condition->accept(this);
248
249 handle_if_block(&ir->then_instructions);
250 handle_if_block(&ir->else_instructions);
251
252 /* handle_if_block() already descended into the children. */
253 return visit_continue_with_parent;
254 }
255
256 void
257 ir_copy_propagation_visitor::handle_loop(ir_loop *ir, bool keep_acp)
258 {
259 exec_list *orig_acp = this->acp;
260 exec_list *orig_kills = this->kills;
261 bool orig_killed_all = this->killed_all;
262
263 this->acp = new(mem_ctx) exec_list;
264 this->kills = new(mem_ctx) exec_list;
265 this->killed_all = false;
266
267 if (keep_acp) {
268 /* Populate the initial acp with a copy of the original */
269 foreach_in_list(acp_entry, a, orig_acp) {
270 this->acp->push_tail(new(this->acp) acp_entry(a->lhs, a->rhs));
271 }
272 }
273
274 visit_list_elements(this, &ir->body_instructions);
275
276 if (this->killed_all) {
277 orig_acp->make_empty();
278 }
279
280 exec_list *new_kills = this->kills;
281 this->kills = orig_kills;
282 ralloc_free(this->acp);
283 this->acp = orig_acp;
284 this->killed_all = this->killed_all || orig_killed_all;
285
286 foreach_in_list(kill_entry, k, new_kills) {
287 kill(k->var);
288 }
289
290 ralloc_free(new_kills);
291 }
292
293 ir_visitor_status
294 ir_copy_propagation_visitor::visit_enter(ir_loop *ir)
295 {
296 /* Make a conservative first pass over the loop with an empty ACP set.
297 * This also removes any killed entries from the original ACP set.
298 */
299 handle_loop(ir, false);
300
301 /* Then, run it again with the real ACP set, minus any killed entries.
302 * This takes care of propagating values from before the loop into it.
303 */
304 handle_loop(ir, true);
305
306 /* already descended into the children. */
307 return visit_continue_with_parent;
308 }
309
310 void
311 ir_copy_propagation_visitor::kill(ir_variable *var)
312 {
313 assert(var != NULL);
314
315 /* Remove any entries currently in the ACP for this kill. */
316 foreach_in_list_safe(acp_entry, entry, acp) {
317 if (entry->lhs == var || entry->rhs == var) {
318 entry->remove();
319 }
320 }
321
322 /* Add the LHS variable to the list of killed variables in this block.
323 */
324 this->kills->push_tail(new(this->kills) kill_entry(var));
325 }
326
327 /**
328 * Adds an entry to the available copy list if it's a plain assignment
329 * of a variable to a variable.
330 */
331 void
332 ir_copy_propagation_visitor::add_copy(ir_assignment *ir)
333 {
334 acp_entry *entry;
335
336 if (ir->condition)
337 return;
338
339 ir_variable *lhs_var = ir->whole_variable_written();
340 ir_variable *rhs_var = ir->rhs->whole_variable_referenced();
341
342 if ((lhs_var != NULL) && (rhs_var != NULL)) {
343 if (lhs_var == rhs_var) {
344 /* This is a dumb assignment, but we've conveniently noticed
345 * it here. Removing it now would mess up the loop iteration
346 * calling us. Just flag it to not execute, and someone else
347 * will clean up the mess.
348 */
349 ir->condition = new(ralloc_parent(ir)) ir_constant(false);
350 this->progress = true;
351 } else if (lhs_var->data.mode != ir_var_shader_storage &&
352 lhs_var->data.mode != ir_var_shader_shared &&
353 lhs_var->data.precise == rhs_var->data.precise) {
354 entry = new(this->acp) acp_entry(lhs_var, rhs_var);
355 this->acp->push_tail(entry);
356 }
357 }
358 }
359
360 /**
361 * Does a copy propagation pass on the code present in the instruction stream.
362 */
363 bool
364 do_copy_propagation(exec_list *instructions)
365 {
366 ir_copy_propagation_visitor v;
367
368 visit_list_elements(&v, instructions);
369
370 return v.progress;
371 }