2 * Copyright © 2010 Intel Corporation
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:
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
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.
25 * \file opt_copy_propagation.cpp
27 * Moves usage of recently-copied variables to the previous copy of
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.
36 #include "ir_visitor.h"
37 #include "ir_basic_block.h"
38 #include "ir_optimization.h"
39 #include "glsl_types.h"
43 class acp_entry : public exec_node
46 acp_entry(ir_variable *lhs, ir_variable *rhs)
59 class kill_entry : public exec_node
62 kill_entry(ir_variable *var)
71 class ir_copy_propagation_visitor : public ir_hierarchical_visitor {
73 ir_copy_propagation_visitor()
76 mem_ctx = ralloc_context(0);
77 this->acp = new(mem_ctx) exec_list;
78 this->kills = new(mem_ctx) exec_list;
80 ~ir_copy_propagation_visitor()
85 virtual ir_visitor_status visit(class ir_dereference_variable *);
86 virtual ir_visitor_status visit_enter(class ir_loop *);
87 virtual ir_visitor_status visit_enter(class ir_function_signature *);
88 virtual ir_visitor_status visit_enter(class ir_function *);
89 virtual ir_visitor_status visit_leave(class ir_assignment *);
90 virtual ir_visitor_status visit_enter(class ir_call *);
91 virtual ir_visitor_status visit_enter(class ir_if *);
93 void add_copy(ir_assignment *ir);
94 void kill(ir_variable *ir);
95 void handle_if_block(exec_list *instructions);
97 /** List of acp_entry: The available copies to propagate */
100 * List of kill_entry: The variables whose values were killed in this
112 } /* unnamed namespace */
115 ir_copy_propagation_visitor::visit_enter(ir_function_signature *ir)
117 /* Treat entry into a function signature as a completely separate
118 * block. Any instructions at global scope will be shuffled into
119 * main() at link time, so they're irrelevant to us.
121 exec_list *orig_acp = this->acp;
122 exec_list *orig_kills = this->kills;
123 bool orig_killed_all = this->killed_all;
125 this->acp = new(mem_ctx) exec_list;
126 this->kills = new(mem_ctx) exec_list;
127 this->killed_all = false;
129 visit_list_elements(this, &ir->body);
131 this->kills = orig_kills;
132 this->acp = orig_acp;
133 this->killed_all = orig_killed_all;
135 return visit_continue_with_parent;
139 ir_copy_propagation_visitor::visit_leave(ir_assignment *ir)
141 kill(ir->lhs->variable_referenced());
145 return visit_continue;
149 ir_copy_propagation_visitor::visit_enter(ir_function *ir)
152 return visit_continue;
156 * Replaces dereferences of ACP RHS variables with ACP LHS variables.
158 * This is where the actual copy propagation occurs. Note that the
159 * rewriting of ir_dereference means that the ir_dereference instance
160 * must not be shared by multiple IR operations!
163 ir_copy_propagation_visitor::visit(ir_dereference_variable *ir)
165 if (this->in_assignee)
166 return visit_continue;
168 ir_variable *var = ir->var;
170 foreach_iter(exec_list_iterator, iter, *this->acp) {
171 acp_entry *entry = (acp_entry *)iter.get();
173 if (var == entry->lhs) {
174 ir->var = entry->rhs;
175 this->progress = true;
180 return visit_continue;
185 ir_copy_propagation_visitor::visit_enter(ir_call *ir)
187 /* Do copy propagation on call parameters, but skip any out params */
188 exec_list_iterator sig_param_iter = ir->callee->parameters.iterator();
189 foreach_iter(exec_list_iterator, iter, ir->actual_parameters) {
190 ir_variable *sig_param = (ir_variable *)sig_param_iter.get();
191 ir_instruction *ir = (ir_instruction *)iter.get();
192 if (sig_param->mode != ir_var_out && sig_param->mode != ir_var_inout) {
195 sig_param_iter.next();
198 /* Since we're unlinked, we don't (necessarily) know the side effects of
199 * this call. So kill all copies.
202 this->killed_all = true;
204 return visit_continue_with_parent;
208 ir_copy_propagation_visitor::handle_if_block(exec_list *instructions)
210 exec_list *orig_acp = this->acp;
211 exec_list *orig_kills = this->kills;
212 bool orig_killed_all = this->killed_all;
214 this->acp = new(mem_ctx) exec_list;
215 this->kills = new(mem_ctx) exec_list;
216 this->killed_all = false;
218 /* Populate the initial acp with a copy of the original */
219 foreach_iter(exec_list_iterator, iter, *orig_acp) {
220 acp_entry *a = (acp_entry *)iter.get();
221 this->acp->push_tail(new(this->mem_ctx) acp_entry(a->lhs, a->rhs));
224 visit_list_elements(this, instructions);
226 if (this->killed_all) {
227 orig_acp->make_empty();
230 exec_list *new_kills = this->kills;
231 this->kills = orig_kills;
232 this->acp = orig_acp;
233 this->killed_all = this->killed_all || orig_killed_all;
235 foreach_iter(exec_list_iterator, iter, *new_kills) {
236 kill_entry *k = (kill_entry *)iter.get();
242 ir_copy_propagation_visitor::visit_enter(ir_if *ir)
244 ir->condition->accept(this);
246 handle_if_block(&ir->then_instructions);
247 handle_if_block(&ir->else_instructions);
249 /* handle_if_block() already descended into the children. */
250 return visit_continue_with_parent;
254 ir_copy_propagation_visitor::visit_enter(ir_loop *ir)
256 exec_list *orig_acp = this->acp;
257 exec_list *orig_kills = this->kills;
258 bool orig_killed_all = this->killed_all;
260 /* FINISHME: For now, the initial acp for loops is totally empty.
261 * We could go through once, then go through again with the acp
262 * cloned minus the killed entries after the first run through.
264 this->acp = new(mem_ctx) exec_list;
265 this->kills = new(mem_ctx) exec_list;
266 this->killed_all = false;
268 visit_list_elements(this, &ir->body_instructions);
270 if (this->killed_all) {
271 orig_acp->make_empty();
274 exec_list *new_kills = this->kills;
275 this->kills = orig_kills;
276 this->acp = orig_acp;
277 this->killed_all = this->killed_all || orig_killed_all;
279 foreach_iter(exec_list_iterator, iter, *new_kills) {
280 kill_entry *k = (kill_entry *)iter.get();
284 /* already descended into the children. */
285 return visit_continue_with_parent;
289 ir_copy_propagation_visitor::kill(ir_variable *var)
293 /* Remove any entries currently in the ACP for this kill. */
294 foreach_iter(exec_list_iterator, iter, *acp) {
295 acp_entry *entry = (acp_entry *)iter.get();
297 if (entry->lhs == var || entry->rhs == var) {
302 /* Add the LHS variable to the list of killed variables in this block.
304 this->kills->push_tail(new(this->mem_ctx) kill_entry(var));
308 * Adds an entry to the available copy list if it's a plain assignment
309 * of a variable to a variable.
312 ir_copy_propagation_visitor::add_copy(ir_assignment *ir)
319 ir_variable *lhs_var = ir->whole_variable_written();
320 ir_variable *rhs_var = ir->rhs->whole_variable_referenced();
322 if ((lhs_var != NULL) && (rhs_var != NULL)) {
323 if (lhs_var == rhs_var) {
324 /* This is a dumb assignment, but we've conveniently noticed
325 * it here. Removing it now would mess up the loop iteration
326 * calling us. Just flag it to not execute, and someone else
327 * will clean up the mess.
329 ir->condition = new(ralloc_parent(ir)) ir_constant(false);
330 this->progress = true;
332 entry = new(this->mem_ctx) acp_entry(lhs_var, rhs_var);
333 this->acp->push_tail(entry);
339 * Does a copy propagation pass on the code present in the instruction stream.
342 do_copy_propagation(exec_list *instructions)
344 ir_copy_propagation_visitor v;
346 visit_list_elements(&v, instructions);