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"
45 class ir_copy_propagation_visitor
: public ir_hierarchical_visitor
{
47 ir_copy_propagation_visitor()
50 mem_ctx
= ralloc_context(0);
51 lin_ctx
= linear_alloc_parent(mem_ctx
, 0);
52 acp
= _mesa_hash_table_create(mem_ctx
, _mesa_hash_pointer
,
53 _mesa_key_pointer_equal
);
54 kills
= _mesa_set_create(mem_ctx
, _mesa_hash_pointer
,
55 _mesa_key_pointer_equal
);
58 ~ir_copy_propagation_visitor()
63 virtual ir_visitor_status
visit(class ir_dereference_variable
*);
64 void handle_loop(class ir_loop
*, bool keep_acp
);
65 virtual ir_visitor_status
visit_enter(class ir_loop
*);
66 virtual ir_visitor_status
visit_enter(class ir_function_signature
*);
67 virtual ir_visitor_status
visit_enter(class ir_function
*);
68 virtual ir_visitor_status
visit_leave(class ir_assignment
*);
69 virtual ir_visitor_status
visit_enter(class ir_call
*);
70 virtual ir_visitor_status
visit_enter(class ir_if
*);
72 void add_copy(ir_assignment
*ir
);
73 void kill(ir_variable
*ir
);
74 void handle_if_block(exec_list
*instructions
);
76 /** Hash of lhs->rhs: The available copies to propagate */
80 * Set of ir_variables: Whose values were killed in this block.
92 } /* unnamed namespace */
95 ir_copy_propagation_visitor::visit_enter(ir_function_signature
*ir
)
97 /* Treat entry into a function signature as a completely separate
98 * block. Any instructions at global scope will be shuffled into
99 * main() at link time, so they're irrelevant to us.
101 hash_table
*orig_acp
= this->acp
;
102 set
*orig_kills
= this->kills
;
103 bool orig_killed_all
= this->killed_all
;
105 acp
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
106 _mesa_key_pointer_equal
);
107 kills
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
108 _mesa_key_pointer_equal
);
109 this->killed_all
= false;
111 visit_list_elements(this, &ir
->body
);
113 _mesa_hash_table_destroy(acp
, NULL
);
114 _mesa_set_destroy(kills
, NULL
);
116 this->kills
= orig_kills
;
117 this->acp
= orig_acp
;
118 this->killed_all
= orig_killed_all
;
120 return visit_continue_with_parent
;
124 ir_copy_propagation_visitor::visit_leave(ir_assignment
*ir
)
126 kill(ir
->lhs
->variable_referenced());
130 return visit_continue
;
134 ir_copy_propagation_visitor::visit_enter(ir_function
*ir
)
137 return visit_continue
;
141 * Replaces dereferences of ACP RHS variables with ACP LHS variables.
143 * This is where the actual copy propagation occurs. Note that the
144 * rewriting of ir_dereference means that the ir_dereference instance
145 * must not be shared by multiple IR operations!
148 ir_copy_propagation_visitor::visit(ir_dereference_variable
*ir
)
150 if (this->in_assignee
)
151 return visit_continue
;
153 struct hash_entry
*entry
= _mesa_hash_table_search(acp
, ir
->var
);
155 ir
->var
= (ir_variable
*) entry
->data
;
159 return visit_continue
;
164 ir_copy_propagation_visitor::visit_enter(ir_call
*ir
)
166 /* Do copy propagation on call parameters, but skip any out params */
167 foreach_two_lists(formal_node
, &ir
->callee
->parameters
,
168 actual_node
, &ir
->actual_parameters
) {
169 ir_variable
*sig_param
= (ir_variable
*) formal_node
;
170 ir_rvalue
*ir
= (ir_rvalue
*) actual_node
;
171 if (sig_param
->data
.mode
!= ir_var_function_out
172 && sig_param
->data
.mode
!= ir_var_function_inout
) {
177 /* Since this pass can run when unlinked, we don't (necessarily) know
178 * the side effects of calls. (When linked, most calls are inlined
179 * anyway, so it doesn't matter much.)
181 * One place where this does matter is IR intrinsics. They're never
182 * inlined. We also know what they do - while some have side effects
183 * (such as image writes), none edit random global variables. So we
184 * can assume they're side-effect free (other than the return value
185 * and out parameters).
187 if (!ir
->callee
->is_intrinsic()) {
188 _mesa_hash_table_clear(acp
, NULL
);
189 this->killed_all
= true;
191 if (ir
->return_deref
)
192 kill(ir
->return_deref
->var
);
194 foreach_two_lists(formal_node
, &ir
->callee
->parameters
,
195 actual_node
, &ir
->actual_parameters
) {
196 ir_variable
*sig_param
= (ir_variable
*) formal_node
;
197 if (sig_param
->data
.mode
== ir_var_function_out
||
198 sig_param
->data
.mode
== ir_var_function_inout
) {
199 ir_rvalue
*ir
= (ir_rvalue
*) actual_node
;
200 ir_variable
*var
= ir
->variable_referenced();
206 return visit_continue_with_parent
;
210 ir_copy_propagation_visitor::handle_if_block(exec_list
*instructions
)
212 hash_table
*orig_acp
= this->acp
;
213 set
*orig_kills
= this->kills
;
214 bool orig_killed_all
= this->killed_all
;
216 acp
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
217 _mesa_key_pointer_equal
);
218 kills
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
219 _mesa_key_pointer_equal
);
220 this->killed_all
= false;
222 /* Populate the initial acp with a copy of the original */
223 struct hash_entry
*entry
;
224 hash_table_foreach(orig_acp
, entry
) {
225 _mesa_hash_table_insert(acp
, entry
->key
, entry
->data
);
228 visit_list_elements(this, instructions
);
230 if (this->killed_all
) {
231 _mesa_hash_table_clear(orig_acp
, NULL
);
234 set
*new_kills
= this->kills
;
235 this->kills
= orig_kills
;
236 _mesa_hash_table_destroy(acp
, NULL
);
237 this->acp
= orig_acp
;
238 this->killed_all
= this->killed_all
|| orig_killed_all
;
240 struct set_entry
*s_entry
;
241 set_foreach(new_kills
, s_entry
) {
242 kill((ir_variable
*) s_entry
->key
);
245 _mesa_set_destroy(new_kills
, NULL
);
249 ir_copy_propagation_visitor::visit_enter(ir_if
*ir
)
251 ir
->condition
->accept(this);
253 handle_if_block(&ir
->then_instructions
);
254 handle_if_block(&ir
->else_instructions
);
256 /* handle_if_block() already descended into the children. */
257 return visit_continue_with_parent
;
261 ir_copy_propagation_visitor::handle_loop(ir_loop
*ir
, bool keep_acp
)
263 hash_table
*orig_acp
= this->acp
;
264 set
*orig_kills
= this->kills
;
265 bool orig_killed_all
= this->killed_all
;
267 acp
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
268 _mesa_key_pointer_equal
);
269 kills
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
270 _mesa_key_pointer_equal
);
271 this->killed_all
= false;
274 struct hash_entry
*entry
;
275 hash_table_foreach(orig_acp
, entry
) {
276 _mesa_hash_table_insert(acp
, entry
->key
, entry
->data
);
280 visit_list_elements(this, &ir
->body_instructions
);
282 if (this->killed_all
) {
283 _mesa_hash_table_clear(orig_acp
, NULL
);
286 set
*new_kills
= this->kills
;
287 this->kills
= orig_kills
;
288 _mesa_hash_table_destroy(acp
, NULL
);
289 this->acp
= orig_acp
;
290 this->killed_all
= this->killed_all
|| orig_killed_all
;
292 struct set_entry
*entry
;
293 set_foreach(new_kills
, entry
) {
294 kill((ir_variable
*) entry
->key
);
297 _mesa_set_destroy(new_kills
, NULL
);
301 ir_copy_propagation_visitor::visit_enter(ir_loop
*ir
)
303 /* Make a conservative first pass over the loop with an empty ACP set.
304 * This also removes any killed entries from the original ACP set.
306 handle_loop(ir
, false);
308 /* Then, run it again with the real ACP set, minus any killed entries.
309 * This takes care of propagating values from before the loop into it.
311 handle_loop(ir
, true);
313 /* already descended into the children. */
314 return visit_continue_with_parent
;
318 ir_copy_propagation_visitor::kill(ir_variable
*var
)
322 /* Remove any entries currently in the ACP for this kill. */
323 struct hash_entry
*entry
= _mesa_hash_table_search(acp
, var
);
325 _mesa_hash_table_remove(acp
, entry
);
328 hash_table_foreach(acp
, entry
) {
329 if (var
== (ir_variable
*) entry
->data
) {
330 _mesa_hash_table_remove(acp
, entry
);
334 /* Add the LHS variable to the set of killed variables in this block. */
335 _mesa_set_add(kills
, var
);
339 * Adds an entry to the available copy list if it's a plain assignment
340 * of a variable to a variable.
343 ir_copy_propagation_visitor::add_copy(ir_assignment
*ir
)
348 ir_variable
*lhs_var
= ir
->whole_variable_written();
349 ir_variable
*rhs_var
= ir
->rhs
->whole_variable_referenced();
351 /* Don't try to remove a dumb assignment of a variable to itself. Removing
352 * it now would mess up the loop iteration calling us.
354 if (lhs_var
!= NULL
&& rhs_var
!= NULL
&& lhs_var
!= rhs_var
) {
355 if (lhs_var
->data
.mode
!= ir_var_shader_storage
&&
356 lhs_var
->data
.mode
!= ir_var_shader_shared
&&
357 lhs_var
->data
.precise
== rhs_var
->data
.precise
) {
358 _mesa_hash_table_insert(acp
, lhs_var
, rhs_var
);
364 * Does a copy propagation pass on the code present in the instruction stream.
367 do_copy_propagation(exec_list
*instructions
)
369 ir_copy_propagation_visitor v
;
371 visit_list_elements(&v
, instructions
);