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 "compiler/glsl_types.h"
40 #include "util/hash_table.h"
44 class kill_entry
: public exec_node
47 kill_entry(ir_variable
*var
)
56 class ir_copy_propagation_visitor
: public ir_hierarchical_visitor
{
58 ir_copy_propagation_visitor()
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
;
67 ~ir_copy_propagation_visitor()
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
*);
81 void add_copy(ir_assignment
*ir
);
82 void kill(ir_variable
*ir
);
83 void handle_if_block(exec_list
*instructions
);
85 /** Hash of lhs->rhs: The available copies to propagate */
88 * List of kill_entry: The variables whose values were killed in this
100 } /* unnamed namespace */
103 ir_copy_propagation_visitor::visit_enter(ir_function_signature
*ir
)
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.
109 hash_table
*orig_acp
= this->acp
;
110 exec_list
*orig_kills
= this->kills
;
111 bool orig_killed_all
= this->killed_all
;
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;
118 visit_list_elements(this, &ir
->body
);
120 _mesa_hash_table_destroy(acp
, NULL
);
121 ralloc_free(this->kills
);
123 this->kills
= orig_kills
;
124 this->acp
= orig_acp
;
125 this->killed_all
= orig_killed_all
;
127 return visit_continue_with_parent
;
131 ir_copy_propagation_visitor::visit_leave(ir_assignment
*ir
)
133 kill(ir
->lhs
->variable_referenced());
137 return visit_continue
;
141 ir_copy_propagation_visitor::visit_enter(ir_function
*ir
)
144 return visit_continue
;
148 * Replaces dereferences of ACP RHS variables with ACP LHS variables.
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!
155 ir_copy_propagation_visitor::visit(ir_dereference_variable
*ir
)
157 if (this->in_assignee
)
158 return visit_continue
;
160 struct hash_entry
*entry
= _mesa_hash_table_search(acp
, ir
->var
);
162 ir
->var
= (ir_variable
*) entry
->data
;
166 return visit_continue
;
171 ir_copy_propagation_visitor::visit_enter(ir_call
*ir
)
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
) {
184 /* Since we're unlinked, we don't (necessarily) know the side effects of
185 * this call. So kill all copies.
187 _mesa_hash_table_clear(acp
, NULL
);
188 this->killed_all
= true;
190 return visit_continue_with_parent
;
194 ir_copy_propagation_visitor::handle_if_block(exec_list
*instructions
)
196 hash_table
*orig_acp
= this->acp
;
197 exec_list
*orig_kills
= this->kills
;
198 bool orig_killed_all
= this->killed_all
;
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;
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
);
211 visit_list_elements(this, instructions
);
213 if (this->killed_all
) {
214 _mesa_hash_table_clear(orig_acp
, NULL
);
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
;
223 foreach_in_list(kill_entry
, k
, new_kills
) {
227 ralloc_free(new_kills
);
231 ir_copy_propagation_visitor::visit_enter(ir_if
*ir
)
233 ir
->condition
->accept(this);
235 handle_if_block(&ir
->then_instructions
);
236 handle_if_block(&ir
->else_instructions
);
238 /* handle_if_block() already descended into the children. */
239 return visit_continue_with_parent
;
243 ir_copy_propagation_visitor::handle_loop(ir_loop
*ir
, bool keep_acp
)
245 hash_table
*orig_acp
= this->acp
;
246 exec_list
*orig_kills
= this->kills
;
247 bool orig_killed_all
= this->killed_all
;
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;
255 struct hash_entry
*entry
;
256 hash_table_foreach(orig_acp
, entry
) {
257 _mesa_hash_table_insert(acp
, entry
->key
, entry
->data
);
261 visit_list_elements(this, &ir
->body_instructions
);
263 if (this->killed_all
) {
264 _mesa_hash_table_clear(orig_acp
, NULL
);
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
;
273 foreach_in_list(kill_entry
, k
, new_kills
) {
277 ralloc_free(new_kills
);
281 ir_copy_propagation_visitor::visit_enter(ir_loop
*ir
)
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.
286 handle_loop(ir
, false);
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.
291 handle_loop(ir
, true);
293 /* already descended into the children. */
294 return visit_continue_with_parent
;
298 ir_copy_propagation_visitor::kill(ir_variable
*var
)
302 /* Remove any entries currently in the ACP for this kill. */
303 struct hash_entry
*entry
= _mesa_hash_table_search(acp
, var
);
305 _mesa_hash_table_remove(acp
, entry
);
308 hash_table_foreach(acp
, entry
) {
309 if (var
== (ir_variable
*) entry
->data
) {
310 _mesa_hash_table_remove(acp
, entry
);
314 /* Add the LHS variable to the list of killed variables in this block.
316 this->kills
->push_tail(new(this->kills
) kill_entry(var
));
320 * Adds an entry to the available copy list if it's a plain assignment
321 * of a variable to a variable.
324 ir_copy_propagation_visitor::add_copy(ir_assignment
*ir
)
329 ir_variable
*lhs_var
= ir
->whole_variable_written();
330 ir_variable
*rhs_var
= ir
->rhs
->whole_variable_referenced();
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.
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
) {
346 _mesa_hash_table_insert(acp
, lhs_var
, rhs_var
);
352 * Does a copy propagation pass on the code present in the instruction stream.
355 do_copy_propagation(exec_list
*instructions
)
357 ir_copy_propagation_visitor v
;
359 visit_list_elements(&v
, instructions
);