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