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"
41 class acp_entry
: public exec_node
44 acp_entry(ir_variable
*lhs
, ir_variable
*rhs
)
57 class kill_entry
: public exec_node
60 kill_entry(ir_variable
*var
)
69 class ir_copy_propagation_visitor
: public ir_hierarchical_visitor
{
71 ir_copy_propagation_visitor()
74 mem_ctx
= ralloc_context(0);
75 this->acp
= new(mem_ctx
) exec_list
;
76 this->kills
= new(mem_ctx
) exec_list
;
78 ~ir_copy_propagation_visitor()
83 virtual ir_visitor_status
visit(class ir_dereference_variable
*);
84 virtual ir_visitor_status
visit_enter(class ir_loop
*);
85 virtual ir_visitor_status
visit_enter(class ir_function_signature
*);
86 virtual ir_visitor_status
visit_enter(class ir_function
*);
87 virtual ir_visitor_status
visit_leave(class ir_assignment
*);
88 virtual ir_visitor_status
visit_enter(class ir_call
*);
89 virtual ir_visitor_status
visit_enter(class ir_if
*);
91 void add_copy(ir_assignment
*ir
);
92 void kill(ir_variable
*ir
);
93 void handle_if_block(exec_list
*instructions
);
95 /** List of acp_entry: The available copies to propagate */
98 * List of kill_entry: The variables whose values were killed in this
111 ir_copy_propagation_visitor::visit_enter(ir_function_signature
*ir
)
113 /* Treat entry into a function signature as a completely separate
114 * block. Any instructions at global scope will be shuffled into
115 * main() at link time, so they're irrelevant to us.
117 exec_list
*orig_acp
= this->acp
;
118 exec_list
*orig_kills
= this->kills
;
119 bool orig_killed_all
= this->killed_all
;
121 this->acp
= new(mem_ctx
) exec_list
;
122 this->kills
= new(mem_ctx
) exec_list
;
123 this->killed_all
= false;
125 visit_list_elements(this, &ir
->body
);
127 this->kills
= orig_kills
;
128 this->acp
= orig_acp
;
129 this->killed_all
= orig_killed_all
;
131 return visit_continue_with_parent
;
135 ir_copy_propagation_visitor::visit_leave(ir_assignment
*ir
)
137 kill(ir
->lhs
->variable_referenced());
141 return visit_continue
;
145 ir_copy_propagation_visitor::visit_enter(ir_function
*ir
)
148 return visit_continue
;
152 * Replaces dereferences of ACP RHS variables with ACP LHS variables.
154 * This is where the actual copy propagation occurs. Note that the
155 * rewriting of ir_dereference means that the ir_dereference instance
156 * must not be shared by multiple IR operations!
159 ir_copy_propagation_visitor::visit(ir_dereference_variable
*ir
)
161 if (this->in_assignee
)
162 return visit_continue
;
164 ir_variable
*var
= ir
->var
;
166 foreach_iter(exec_list_iterator
, iter
, *this->acp
) {
167 acp_entry
*entry
= (acp_entry
*)iter
.get();
169 if (var
== entry
->lhs
) {
170 ir
->var
= entry
->rhs
;
171 this->progress
= true;
176 return visit_continue
;
181 ir_copy_propagation_visitor::visit_enter(ir_call
*ir
)
183 /* Do copy propagation on call parameters, but skip any out params */
184 exec_list_iterator sig_param_iter
= ir
->get_callee()->parameters
.iterator();
185 foreach_iter(exec_list_iterator
, iter
, ir
->actual_parameters
) {
186 ir_variable
*sig_param
= (ir_variable
*)sig_param_iter
.get();
187 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
188 if (sig_param
->mode
!= ir_var_out
&& sig_param
->mode
!= ir_var_inout
) {
191 sig_param_iter
.next();
194 /* Since we're unlinked, we don't (necessarily) know the side effects of
195 * this call. So kill all copies.
198 this->killed_all
= true;
200 return visit_continue_with_parent
;
204 ir_copy_propagation_visitor::handle_if_block(exec_list
*instructions
)
206 exec_list
*orig_acp
= this->acp
;
207 exec_list
*orig_kills
= this->kills
;
208 bool orig_killed_all
= this->killed_all
;
210 this->acp
= new(mem_ctx
) exec_list
;
211 this->kills
= new(mem_ctx
) exec_list
;
212 this->killed_all
= false;
214 /* Populate the initial acp with a copy of the original */
215 foreach_iter(exec_list_iterator
, iter
, *orig_acp
) {
216 acp_entry
*a
= (acp_entry
*)iter
.get();
217 this->acp
->push_tail(new(this->mem_ctx
) acp_entry(a
->lhs
, a
->rhs
));
220 visit_list_elements(this, instructions
);
222 if (this->killed_all
) {
223 orig_acp
->make_empty();
226 exec_list
*new_kills
= this->kills
;
227 this->kills
= orig_kills
;
228 this->acp
= orig_acp
;
229 this->killed_all
= this->killed_all
|| orig_killed_all
;
231 foreach_iter(exec_list_iterator
, iter
, *new_kills
) {
232 kill_entry
*k
= (kill_entry
*)iter
.get();
238 ir_copy_propagation_visitor::visit_enter(ir_if
*ir
)
240 ir
->condition
->accept(this);
242 handle_if_block(&ir
->then_instructions
);
243 handle_if_block(&ir
->else_instructions
);
245 /* handle_if_block() already descended into the children. */
246 return visit_continue_with_parent
;
250 ir_copy_propagation_visitor::visit_enter(ir_loop
*ir
)
252 exec_list
*orig_acp
= this->acp
;
253 exec_list
*orig_kills
= this->kills
;
254 bool orig_killed_all
= this->killed_all
;
256 /* FINISHME: For now, the initial acp for loops is totally empty.
257 * We could go through once, then go through again with the acp
258 * cloned minus the killed entries after the first run through.
260 this->acp
= new(mem_ctx
) exec_list
;
261 this->kills
= new(mem_ctx
) exec_list
;
262 this->killed_all
= false;
264 visit_list_elements(this, &ir
->body_instructions
);
266 if (this->killed_all
) {
267 orig_acp
->make_empty();
270 exec_list
*new_kills
= this->kills
;
271 this->kills
= orig_kills
;
272 this->acp
= orig_acp
;
273 this->killed_all
= this->killed_all
|| orig_killed_all
;
275 foreach_iter(exec_list_iterator
, iter
, *new_kills
) {
276 kill_entry
*k
= (kill_entry
*)iter
.get();
280 /* already descended into the children. */
281 return visit_continue_with_parent
;
285 ir_copy_propagation_visitor::kill(ir_variable
*var
)
289 /* Remove any entries currently in the ACP for this kill. */
290 foreach_iter(exec_list_iterator
, iter
, *acp
) {
291 acp_entry
*entry
= (acp_entry
*)iter
.get();
293 if (entry
->lhs
== var
|| entry
->rhs
== var
) {
298 /* Add the LHS variable to the list of killed variables in this block.
300 this->kills
->push_tail(new(this->mem_ctx
) kill_entry(var
));
304 * Adds an entry to the available copy list if it's a plain assignment
305 * of a variable to a variable.
308 ir_copy_propagation_visitor::add_copy(ir_assignment
*ir
)
315 ir_variable
*lhs_var
= ir
->whole_variable_written();
316 ir_variable
*rhs_var
= ir
->rhs
->whole_variable_referenced();
318 if ((lhs_var
!= NULL
) && (rhs_var
!= NULL
)) {
319 if (lhs_var
== rhs_var
) {
320 /* This is a dumb assignment, but we've conveniently noticed
321 * it here. Removing it now would mess up the loop iteration
322 * calling us. Just flag it to not execute, and someone else
323 * will clean up the mess.
325 ir
->condition
= new(ralloc_parent(ir
)) ir_constant(false);
326 this->progress
= true;
328 entry
= new(this->mem_ctx
) acp_entry(lhs_var
, rhs_var
);
329 this->acp
->push_tail(entry
);
335 * Does a copy propagation pass on the code present in the instruction stream.
338 do_copy_propagation(exec_list
*instructions
)
340 ir_copy_propagation_visitor v
;
342 visit_list_elements(&v
, instructions
);