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