glsl2: Catch pointless copies in copy propagation.
[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 within basic blocks.
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 class ir_copy_propagation_visitor : public ir_hierarchical_visitor {
57 public:
58 ir_copy_propagation_visitor(exec_list *acp)
59 {
60 progress = false;
61 in_lhs = false;
62 this->acp = acp;
63 }
64
65 virtual ir_visitor_status visit(class ir_dereference_variable *);
66 virtual ir_visitor_status visit_enter(class ir_loop *);
67 virtual ir_visitor_status visit_enter(class ir_function_signature *);
68 virtual ir_visitor_status visit_enter(class ir_function *);
69 virtual ir_visitor_status visit_enter(class ir_assignment *);
70 virtual ir_visitor_status visit_enter(class ir_call *);
71 virtual ir_visitor_status visit_enter(class ir_if *);
72
73 /** List of acp_entry */
74 exec_list *acp;
75 bool progress;
76
77 /** Currently in the LHS of an assignment? */
78 bool in_lhs;
79 };
80
81
82 ir_visitor_status
83 ir_copy_propagation_visitor::visit_enter(ir_loop *ir)
84 {
85 (void)ir;
86 return visit_continue_with_parent;
87 }
88
89 ir_visitor_status
90 ir_copy_propagation_visitor::visit_enter(ir_function_signature *ir)
91 {
92 (void)ir;
93 return visit_continue_with_parent;
94 }
95
96 ir_visitor_status
97 ir_copy_propagation_visitor::visit_enter(ir_assignment *ir)
98 {
99 ir_visitor_status s;
100
101 /* Inline the rest of ir_assignment::accept(ir_hv *v), wrapping the
102 * LHS part with setting in_lhs so that we can avoid copy
103 * propagating into the LHS.
104 *
105 * Note that this means we won't copy propagate into the derefs of
106 * an array index. Oh well.
107 */
108 this->in_lhs = true;
109 s = ir->lhs->accept(this);
110 this->in_lhs = false;
111 if (s != visit_continue)
112 return (s == visit_continue_with_parent) ? visit_continue : s;
113
114 s = ir->rhs->accept(this);
115 if (s != visit_continue)
116 return (s == visit_continue_with_parent) ? visit_continue : s;
117
118 if (ir->condition)
119 s = ir->condition->accept(this);
120
121 return (s == visit_stop) ? s : visit_continue_with_parent;
122 }
123
124 ir_visitor_status
125 ir_copy_propagation_visitor::visit_enter(ir_function *ir)
126 {
127 (void) ir;
128 return visit_continue_with_parent;
129 }
130
131 /**
132 * Replaces dereferences of ACP RHS variables with ACP LHS variables.
133 *
134 * This is where the actual copy propagation occurs. Note that the
135 * rewriting of ir_dereference means that the ir_dereference instance
136 * must not be shared by multiple IR operations!
137 */
138 ir_visitor_status
139 ir_copy_propagation_visitor::visit(ir_dereference_variable *ir)
140 {
141 /* Ignores the LHS. Don't want to rewrite the LHS to point at some
142 * other storage!
143 */
144 if (this->in_lhs) {
145 return visit_continue;
146 }
147
148 ir_variable *var = ir->variable_referenced();
149
150 foreach_iter(exec_list_iterator, iter, *this->acp) {
151 acp_entry *entry = (acp_entry *)iter.get();
152
153 if (var == entry->lhs) {
154 ir->var = entry->rhs;
155 this->progress = true;
156 break;
157 }
158 }
159
160 return visit_continue;
161 }
162
163
164 ir_visitor_status
165 ir_copy_propagation_visitor::visit_enter(ir_call *ir)
166 {
167 (void)ir;
168
169 /* Note, if we were to do copy propagation to parameters of calls, we'd
170 * have to be careful about out params.
171 */
172 return visit_continue_with_parent;
173 }
174
175
176 ir_visitor_status
177 ir_copy_propagation_visitor::visit_enter(ir_if *ir)
178 {
179 ir->condition->accept(this);
180
181 /* Do not traverse into the body of the if-statement since that is a
182 * different basic block.
183 */
184 return visit_continue_with_parent;
185 }
186
187 static bool
188 propagate_copies(ir_instruction *ir, exec_list *acp)
189 {
190 ir_copy_propagation_visitor v(acp);
191
192 ir->accept(&v);
193
194 return v.progress;
195 }
196
197 static void
198 kill_invalidated_copies(ir_assignment *ir, exec_list *acp)
199 {
200 ir_variable *var = ir->lhs->variable_referenced();
201 assert(var != NULL);
202
203 foreach_iter(exec_list_iterator, iter, *acp) {
204 acp_entry *entry = (acp_entry *)iter.get();
205
206 if (entry->lhs == var || entry->rhs == var) {
207 entry->remove();
208 }
209 }
210 }
211
212 /**
213 * Adds an entry to the available copy list if it's a plain assignment
214 * of a variable to a variable.
215 */
216 static bool
217 add_copy(void *ctx, ir_assignment *ir, exec_list *acp)
218 {
219 acp_entry *entry;
220
221 if (ir->condition) {
222 ir_constant *condition = ir->condition->as_constant();
223 if (!condition || !condition->value.b[0])
224 return false;
225 }
226
227 ir_variable *lhs_var = ir->whole_variable_written();
228 ir_variable *rhs_var = ir->rhs->whole_variable_referenced();
229
230 if ((lhs_var != NULL) && (rhs_var != NULL)) {
231 if (lhs_var == rhs_var) {
232 /* This is a dumb assignment, but we've conveniently noticed
233 * it here. Removing it now would mess up the loop iteration
234 * calling us. Just flag it to not execute, and someone else
235 * will clean up the mess.
236 */
237 ir->condition = new(talloc_parent(ir)) ir_constant(false);
238 return true;
239 } else {
240 entry = new(ctx) acp_entry(lhs_var, rhs_var);
241 acp->push_tail(entry);
242 }
243 }
244
245 return false;
246 }
247
248 static void
249 copy_propagation_basic_block(ir_instruction *first,
250 ir_instruction *last,
251 void *data)
252 {
253 ir_instruction *ir;
254 /* List of avaialble_copy */
255 exec_list acp;
256 bool *out_progress = (bool *)data;
257 bool progress = false;
258
259 void *ctx = talloc_new(NULL);
260 for (ir = first;; ir = (ir_instruction *)ir->next) {
261 ir_assignment *ir_assign = ir->as_assignment();
262
263 progress = propagate_copies(ir, &acp) || progress;
264
265 if (ir_assign) {
266 kill_invalidated_copies(ir_assign, &acp);
267
268 progress = add_copy(ctx, ir_assign, &acp) || progress;
269 }
270 if (ir == last)
271 break;
272 }
273 *out_progress = progress;
274 talloc_free(ctx);
275 }
276
277 /**
278 * Does a copy propagation pass on the code present in the instruction stream.
279 */
280 bool
281 do_copy_propagation(exec_list *instructions)
282 {
283 bool progress = false;
284
285 call_for_basic_blocks(instructions, copy_propagation_basic_block, &progress);
286
287 return progress;
288 }