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 kills
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
217 _mesa_key_pointer_equal
);
218 this->killed_all
= false;
220 /* Populate the initial acp with a copy of the original */
221 acp
= _mesa_hash_table_clone(orig_acp
, NULL
);
223 visit_list_elements(this, instructions
);
225 if (this->killed_all
) {
226 _mesa_hash_table_clear(orig_acp
, NULL
);
229 set
*new_kills
= this->kills
;
230 this->kills
= orig_kills
;
231 _mesa_hash_table_destroy(acp
, NULL
);
232 this->acp
= orig_acp
;
233 this->killed_all
= this->killed_all
|| orig_killed_all
;
235 struct set_entry
*s_entry
;
236 set_foreach(new_kills
, s_entry
) {
237 kill((ir_variable
*) s_entry
->key
);
240 _mesa_set_destroy(new_kills
, NULL
);
244 ir_copy_propagation_visitor::visit_enter(ir_if
*ir
)
246 ir
->condition
->accept(this);
248 handle_if_block(&ir
->then_instructions
);
249 handle_if_block(&ir
->else_instructions
);
251 /* handle_if_block() already descended into the children. */
252 return visit_continue_with_parent
;
256 ir_copy_propagation_visitor::handle_loop(ir_loop
*ir
, bool keep_acp
)
258 hash_table
*orig_acp
= this->acp
;
259 set
*orig_kills
= this->kills
;
260 bool orig_killed_all
= this->killed_all
;
262 kills
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
263 _mesa_key_pointer_equal
);
264 this->killed_all
= false;
267 acp
= _mesa_hash_table_clone(orig_acp
, NULL
);
269 acp
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
270 _mesa_key_pointer_equal
);
273 visit_list_elements(this, &ir
->body_instructions
);
275 if (this->killed_all
) {
276 _mesa_hash_table_clear(orig_acp
, NULL
);
279 set
*new_kills
= this->kills
;
280 this->kills
= orig_kills
;
281 _mesa_hash_table_destroy(acp
, NULL
);
282 this->acp
= orig_acp
;
283 this->killed_all
= this->killed_all
|| orig_killed_all
;
285 struct set_entry
*entry
;
286 set_foreach(new_kills
, entry
) {
287 kill((ir_variable
*) entry
->key
);
290 _mesa_set_destroy(new_kills
, NULL
);
294 ir_copy_propagation_visitor::visit_enter(ir_loop
*ir
)
296 /* Make a conservative first pass over the loop with an empty ACP set.
297 * This also removes any killed entries from the original ACP set.
299 handle_loop(ir
, false);
301 /* Then, run it again with the real ACP set, minus any killed entries.
302 * This takes care of propagating values from before the loop into it.
304 handle_loop(ir
, true);
306 /* already descended into the children. */
307 return visit_continue_with_parent
;
311 ir_copy_propagation_visitor::kill(ir_variable
*var
)
315 /* Remove any entries currently in the ACP for this kill. */
316 struct hash_entry
*entry
= _mesa_hash_table_search(acp
, var
);
318 _mesa_hash_table_remove(acp
, entry
);
321 hash_table_foreach(acp
, entry
) {
322 if (var
== (ir_variable
*) entry
->data
) {
323 _mesa_hash_table_remove(acp
, entry
);
327 /* Add the LHS variable to the set of killed variables in this block. */
328 _mesa_set_add(kills
, var
);
332 * Adds an entry to the available copy list if it's a plain assignment
333 * of a variable to a variable.
336 ir_copy_propagation_visitor::add_copy(ir_assignment
*ir
)
341 ir_variable
*lhs_var
= ir
->whole_variable_written();
342 ir_variable
*rhs_var
= ir
->rhs
->whole_variable_referenced();
344 /* Don't try to remove a dumb assignment of a variable to itself. Removing
345 * it now would mess up the loop iteration calling us.
347 if (lhs_var
!= NULL
&& rhs_var
!= NULL
&& lhs_var
!= rhs_var
) {
348 if (lhs_var
->data
.mode
!= ir_var_shader_storage
&&
349 lhs_var
->data
.mode
!= ir_var_shader_shared
&&
350 rhs_var
->data
.mode
!= ir_var_shader_storage
&&
351 rhs_var
->data
.mode
!= ir_var_shader_shared
&&
352 lhs_var
->data
.precise
== rhs_var
->data
.precise
) {
353 _mesa_hash_table_insert(acp
, lhs_var
, rhs_var
);
359 * Does a copy propagation pass on the code present in the instruction stream.
362 do_copy_propagation(exec_list
*instructions
)
364 ir_copy_propagation_visitor v
;
366 visit_list_elements(&v
, instructions
);