glsl: teach copy_propagation_elements to deal with whole variables
[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 #include "util/hash_table.h"
41 #include "util/set.h"
42
43 namespace {
44
45 class ir_copy_propagation_visitor : public ir_hierarchical_visitor {
46 public:
47 ir_copy_propagation_visitor()
48 {
49 progress = false;
50 mem_ctx = ralloc_context(0);
51 lin_ctx = linear_alloc_parent(mem_ctx, 0);
52 acp = _mesa_hash_table_create(mem_ctx, _mesa_hash_pointer,
53 _mesa_key_pointer_equal);
54 kills = _mesa_set_create(mem_ctx, _mesa_hash_pointer,
55 _mesa_key_pointer_equal);
56 killed_all = false;
57 }
58 ~ir_copy_propagation_visitor()
59 {
60 ralloc_free(mem_ctx);
61 }
62
63 virtual ir_visitor_status visit(class ir_dereference_variable *);
64 void handle_loop(class ir_loop *, bool keep_acp);
65 virtual ir_visitor_status visit_enter(class ir_loop *);
66 virtual ir_visitor_status visit_enter(class ir_function_signature *);
67 virtual ir_visitor_status visit_enter(class ir_function *);
68 virtual ir_visitor_status visit_leave(class ir_assignment *);
69 virtual ir_visitor_status visit_enter(class ir_call *);
70 virtual ir_visitor_status visit_enter(class ir_if *);
71
72 void add_copy(ir_assignment *ir);
73 void kill(ir_variable *ir);
74 void handle_if_block(exec_list *instructions);
75
76 /** Hash of lhs->rhs: The available copies to propagate */
77 hash_table *acp;
78
79 /**
80 * Set of ir_variables: Whose values were killed in this block.
81 */
82 set *kills;
83
84 bool progress;
85
86 bool killed_all;
87
88 void *mem_ctx;
89 void *lin_ctx;
90 };
91
92 } /* unnamed namespace */
93
94 ir_visitor_status
95 ir_copy_propagation_visitor::visit_enter(ir_function_signature *ir)
96 {
97 /* Treat entry into a function signature as a completely separate
98 * block. Any instructions at global scope will be shuffled into
99 * main() at link time, so they're irrelevant to us.
100 */
101 hash_table *orig_acp = this->acp;
102 set *orig_kills = this->kills;
103 bool orig_killed_all = this->killed_all;
104
105 acp = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
106 _mesa_key_pointer_equal);
107 kills = _mesa_set_create(NULL, _mesa_hash_pointer,
108 _mesa_key_pointer_equal);
109 this->killed_all = false;
110
111 visit_list_elements(this, &ir->body);
112
113 _mesa_hash_table_destroy(acp, NULL);
114 _mesa_set_destroy(kills, NULL);
115
116 this->kills = orig_kills;
117 this->acp = orig_acp;
118 this->killed_all = orig_killed_all;
119
120 return visit_continue_with_parent;
121 }
122
123 ir_visitor_status
124 ir_copy_propagation_visitor::visit_leave(ir_assignment *ir)
125 {
126 kill(ir->lhs->variable_referenced());
127
128 add_copy(ir);
129
130 return visit_continue;
131 }
132
133 ir_visitor_status
134 ir_copy_propagation_visitor::visit_enter(ir_function *ir)
135 {
136 (void) ir;
137 return visit_continue;
138 }
139
140 /**
141 * Replaces dereferences of ACP RHS variables with ACP LHS variables.
142 *
143 * This is where the actual copy propagation occurs. Note that the
144 * rewriting of ir_dereference means that the ir_dereference instance
145 * must not be shared by multiple IR operations!
146 */
147 ir_visitor_status
148 ir_copy_propagation_visitor::visit(ir_dereference_variable *ir)
149 {
150 if (this->in_assignee)
151 return visit_continue;
152
153 struct hash_entry *entry = _mesa_hash_table_search(acp, ir->var);
154 if (entry) {
155 ir->var = (ir_variable *) entry->data;
156 progress = true;
157 }
158
159 return visit_continue;
160 }
161
162
163 ir_visitor_status
164 ir_copy_propagation_visitor::visit_enter(ir_call *ir)
165 {
166 /* Do copy propagation on call parameters, but skip any out params */
167 foreach_two_lists(formal_node, &ir->callee->parameters,
168 actual_node, &ir->actual_parameters) {
169 ir_variable *sig_param = (ir_variable *) formal_node;
170 ir_rvalue *ir = (ir_rvalue *) actual_node;
171 if (sig_param->data.mode != ir_var_function_out
172 && sig_param->data.mode != ir_var_function_inout) {
173 ir->accept(this);
174 }
175 }
176
177 /* Since this pass can run when unlinked, we don't (necessarily) know
178 * the side effects of calls. (When linked, most calls are inlined
179 * anyway, so it doesn't matter much.)
180 *
181 * One place where this does matter is IR intrinsics. They're never
182 * inlined. We also know what they do - while some have side effects
183 * (such as image writes), none edit random global variables. So we
184 * can assume they're side-effect free (other than the return value
185 * and out parameters).
186 */
187 if (!ir->callee->is_intrinsic()) {
188 _mesa_hash_table_clear(acp, NULL);
189 this->killed_all = true;
190 } else {
191 if (ir->return_deref)
192 kill(ir->return_deref->var);
193
194 foreach_two_lists(formal_node, &ir->callee->parameters,
195 actual_node, &ir->actual_parameters) {
196 ir_variable *sig_param = (ir_variable *) formal_node;
197 if (sig_param->data.mode == ir_var_function_out ||
198 sig_param->data.mode == ir_var_function_inout) {
199 ir_rvalue *ir = (ir_rvalue *) actual_node;
200 ir_variable *var = ir->variable_referenced();
201 kill(var);
202 }
203 }
204 }
205
206 return visit_continue_with_parent;
207 }
208
209 void
210 ir_copy_propagation_visitor::handle_if_block(exec_list *instructions)
211 {
212 hash_table *orig_acp = this->acp;
213 set *orig_kills = this->kills;
214 bool orig_killed_all = this->killed_all;
215
216 kills = _mesa_set_create(NULL, _mesa_hash_pointer,
217 _mesa_key_pointer_equal);
218 this->killed_all = false;
219
220 /* Populate the initial acp with a copy of the original */
221 acp = _mesa_hash_table_clone(orig_acp, NULL);
222
223 visit_list_elements(this, instructions);
224
225 if (this->killed_all) {
226 _mesa_hash_table_clear(orig_acp, NULL);
227 }
228
229 set *new_kills = this->kills;
230 this->kills = orig_kills;
231 _mesa_hash_table_destroy(acp, NULL);
232 this->acp = orig_acp;
233 this->killed_all = this->killed_all || orig_killed_all;
234
235 struct set_entry *s_entry;
236 set_foreach(new_kills, s_entry) {
237 kill((ir_variable *) s_entry->key);
238 }
239
240 _mesa_set_destroy(new_kills, NULL);
241 }
242
243 ir_visitor_status
244 ir_copy_propagation_visitor::visit_enter(ir_if *ir)
245 {
246 ir->condition->accept(this);
247
248 handle_if_block(&ir->then_instructions);
249 handle_if_block(&ir->else_instructions);
250
251 /* handle_if_block() already descended into the children. */
252 return visit_continue_with_parent;
253 }
254
255 void
256 ir_copy_propagation_visitor::handle_loop(ir_loop *ir, bool keep_acp)
257 {
258 hash_table *orig_acp = this->acp;
259 set *orig_kills = this->kills;
260 bool orig_killed_all = this->killed_all;
261
262 kills = _mesa_set_create(NULL, _mesa_hash_pointer,
263 _mesa_key_pointer_equal);
264 this->killed_all = false;
265
266 if (keep_acp) {
267 acp = _mesa_hash_table_clone(orig_acp, NULL);
268 } else {
269 acp = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
270 _mesa_key_pointer_equal);
271 }
272
273 visit_list_elements(this, &ir->body_instructions);
274
275 if (this->killed_all) {
276 _mesa_hash_table_clear(orig_acp, NULL);
277 }
278
279 set *new_kills = this->kills;
280 this->kills = orig_kills;
281 _mesa_hash_table_destroy(acp, NULL);
282 this->acp = orig_acp;
283 this->killed_all = this->killed_all || orig_killed_all;
284
285 struct set_entry *entry;
286 set_foreach(new_kills, entry) {
287 kill((ir_variable *) entry->key);
288 }
289
290 _mesa_set_destroy(new_kills, NULL);
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 struct hash_entry *entry = _mesa_hash_table_search(acp, var);
317 if (entry) {
318 _mesa_hash_table_remove(acp, entry);
319 }
320
321 hash_table_foreach(acp, entry) {
322 if (var == (ir_variable *) entry->data) {
323 _mesa_hash_table_remove(acp, entry);
324 }
325 }
326
327 /* Add the LHS variable to the set of killed variables in this block. */
328 _mesa_set_add(kills, var);
329 }
330
331 /**
332 * Adds an entry to the available copy list if it's a plain assignment
333 * of a variable to a variable.
334 */
335 void
336 ir_copy_propagation_visitor::add_copy(ir_assignment *ir)
337 {
338 if (ir->condition)
339 return;
340
341 ir_variable *lhs_var = ir->whole_variable_written();
342 ir_variable *rhs_var = ir->rhs->whole_variable_referenced();
343
344 /* Don't try to remove a dumb assignment of a variable to itself. Removing
345 * it now would mess up the loop iteration calling us.
346 */
347 if (lhs_var != NULL && rhs_var != NULL && lhs_var != rhs_var) {
348 if (lhs_var->data.mode != ir_var_shader_storage &&
349 lhs_var->data.mode != ir_var_shader_shared &&
350 rhs_var->data.mode != ir_var_shader_storage &&
351 rhs_var->data.mode != ir_var_shader_shared &&
352 lhs_var->data.precise == rhs_var->data.precise) {
353 _mesa_hash_table_insert(acp, lhs_var, rhs_var);
354 }
355 }
356 }
357
358 /**
359 * Does a copy propagation pass on the code present in the instruction stream.
360 */
361 bool
362 do_copy_propagation(exec_list *instructions)
363 {
364 ir_copy_propagation_visitor v;
365
366 visit_list_elements(&v, instructions);
367
368 return v.progress;
369 }