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