glsl: Remove spurious assertions
[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 acp = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
217 _mesa_key_pointer_equal);
218 kills = _mesa_set_create(NULL, _mesa_hash_pointer,
219 _mesa_key_pointer_equal);
220 this->killed_all = false;
221
222 /* Populate the initial acp with a copy of the original */
223 struct hash_entry *entry;
224 hash_table_foreach(orig_acp, entry) {
225 _mesa_hash_table_insert(acp, entry->key, entry->data);
226 }
227
228 visit_list_elements(this, instructions);
229
230 if (this->killed_all) {
231 _mesa_hash_table_clear(orig_acp, NULL);
232 }
233
234 set *new_kills = this->kills;
235 this->kills = orig_kills;
236 _mesa_hash_table_destroy(acp, NULL);
237 this->acp = orig_acp;
238 this->killed_all = this->killed_all || orig_killed_all;
239
240 struct set_entry *s_entry;
241 set_foreach(new_kills, s_entry) {
242 kill((ir_variable *) s_entry->key);
243 }
244
245 _mesa_set_destroy(new_kills, NULL);
246 }
247
248 ir_visitor_status
249 ir_copy_propagation_visitor::visit_enter(ir_if *ir)
250 {
251 ir->condition->accept(this);
252
253 handle_if_block(&ir->then_instructions);
254 handle_if_block(&ir->else_instructions);
255
256 /* handle_if_block() already descended into the children. */
257 return visit_continue_with_parent;
258 }
259
260 void
261 ir_copy_propagation_visitor::handle_loop(ir_loop *ir, bool keep_acp)
262 {
263 hash_table *orig_acp = this->acp;
264 set *orig_kills = this->kills;
265 bool orig_killed_all = this->killed_all;
266
267 acp = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
268 _mesa_key_pointer_equal);
269 kills = _mesa_set_create(NULL, _mesa_hash_pointer,
270 _mesa_key_pointer_equal);
271 this->killed_all = false;
272
273 if (keep_acp) {
274 struct hash_entry *entry;
275 hash_table_foreach(orig_acp, entry) {
276 _mesa_hash_table_insert(acp, entry->key, entry->data);
277 }
278 }
279
280 visit_list_elements(this, &ir->body_instructions);
281
282 if (this->killed_all) {
283 _mesa_hash_table_clear(orig_acp, NULL);
284 }
285
286 set *new_kills = this->kills;
287 this->kills = orig_kills;
288 _mesa_hash_table_destroy(acp, NULL);
289 this->acp = orig_acp;
290 this->killed_all = this->killed_all || orig_killed_all;
291
292 struct set_entry *entry;
293 set_foreach(new_kills, entry) {
294 kill((ir_variable *) entry->key);
295 }
296
297 _mesa_set_destroy(new_kills, NULL);
298 }
299
300 ir_visitor_status
301 ir_copy_propagation_visitor::visit_enter(ir_loop *ir)
302 {
303 /* Make a conservative first pass over the loop with an empty ACP set.
304 * This also removes any killed entries from the original ACP set.
305 */
306 handle_loop(ir, false);
307
308 /* Then, run it again with the real ACP set, minus any killed entries.
309 * This takes care of propagating values from before the loop into it.
310 */
311 handle_loop(ir, true);
312
313 /* already descended into the children. */
314 return visit_continue_with_parent;
315 }
316
317 void
318 ir_copy_propagation_visitor::kill(ir_variable *var)
319 {
320 assert(var != NULL);
321
322 /* Remove any entries currently in the ACP for this kill. */
323 struct hash_entry *entry = _mesa_hash_table_search(acp, var);
324 if (entry) {
325 _mesa_hash_table_remove(acp, entry);
326 }
327
328 hash_table_foreach(acp, entry) {
329 if (var == (ir_variable *) entry->data) {
330 _mesa_hash_table_remove(acp, entry);
331 }
332 }
333
334 /* Add the LHS variable to the set of killed variables in this block. */
335 _mesa_set_add(kills, var);
336 }
337
338 /**
339 * Adds an entry to the available copy list if it's a plain assignment
340 * of a variable to a variable.
341 */
342 void
343 ir_copy_propagation_visitor::add_copy(ir_assignment *ir)
344 {
345 if (ir->condition)
346 return;
347
348 ir_variable *lhs_var = ir->whole_variable_written();
349 ir_variable *rhs_var = ir->rhs->whole_variable_referenced();
350
351 /* Don't try to remove a dumb assignment of a variable to itself. Removing
352 * it now would mess up the loop iteration calling us.
353 */
354 if (lhs_var != NULL && rhs_var != NULL && lhs_var != rhs_var) {
355 if (lhs_var->data.mode != ir_var_shader_storage &&
356 lhs_var->data.mode != ir_var_shader_shared &&
357 lhs_var->data.precise == rhs_var->data.precise) {
358 _mesa_hash_table_insert(acp, lhs_var, rhs_var);
359 }
360 }
361 }
362
363 /**
364 * Does a copy propagation pass on the code present in the instruction stream.
365 */
366 bool
367 do_copy_propagation(exec_list *instructions)
368 {
369 ir_copy_propagation_visitor v;
370
371 visit_list_elements(&v, instructions);
372
373 return v.progress;
374 }