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_constant_variable.cpp
27 * Marks variables assigned a single constant value over the course
28 * of the program as constant.
30 * The goal here is to trigger further constant folding and then dead
31 * code elimination. This is common with vector/matrix constructors
32 * and calls to builtin functions.
36 #include "ir_visitor.h"
37 #include "ir_optimization.h"
38 #include "compiler/glsl_types.h"
39 #include "util/hash_table.h"
43 struct assignment_entry
{
46 ir_constant
*constval
;
50 class ir_constant_variable_visitor
: public ir_hierarchical_visitor
{
52 virtual ir_visitor_status
visit_enter(ir_dereference_variable
*);
53 virtual ir_visitor_status
visit(ir_variable
*);
54 virtual ir_visitor_status
visit_enter(ir_assignment
*);
55 virtual ir_visitor_status
visit_enter(ir_call
*);
57 struct hash_table
*ht
;
60 } /* unnamed namespace */
62 static struct assignment_entry
*
63 get_assignment_entry(ir_variable
*var
, struct hash_table
*ht
)
65 struct hash_entry
*hte
= _mesa_hash_table_search(ht
, var
);
66 struct assignment_entry
*entry
;
69 entry
= (struct assignment_entry
*) hte
->data
;
71 entry
= (struct assignment_entry
*) calloc(1, sizeof(*entry
));
73 _mesa_hash_table_insert(ht
, var
, entry
);
80 ir_constant_variable_visitor::visit(ir_variable
*ir
)
82 struct assignment_entry
*entry
= get_assignment_entry(ir
, this->ht
);
83 entry
->our_scope
= true;
84 return visit_continue
;
87 /* Skip derefs of variables so that we can detect declarations. */
89 ir_constant_variable_visitor::visit_enter(ir_dereference_variable
*ir
)
92 return visit_continue_with_parent
;
96 ir_constant_variable_visitor::visit_enter(ir_assignment
*ir
)
98 ir_constant
*constval
;
99 struct assignment_entry
*entry
;
101 entry
= get_assignment_entry(ir
->lhs
->variable_referenced(), this->ht
);
103 entry
->assignment_count
++;
105 /* If there's more than one assignment, don't bother - we won't do anything
106 * with this variable anyway, and continuing just wastes memory cloning
107 * constant expressions.
109 if (entry
->assignment_count
> 1)
110 return visit_continue
;
112 /* If it's already constant, don't do the work. */
113 if (entry
->var
->constant_value
)
114 return visit_continue
;
116 /* OK, now find if we actually have all the right conditions for
117 * this to be a constant value assigned to the var.
120 return visit_continue
;
122 ir_variable
*var
= ir
->whole_variable_written();
124 return visit_continue
;
126 /* Ignore buffer variables, since the underlying storage is shared
127 * and we can't be sure that this variable won't be written by another
130 if (var
->data
.mode
== ir_var_shader_storage
||
131 var
->data
.mode
== ir_var_shader_shared
)
132 return visit_continue
;
134 constval
= ir
->rhs
->constant_expression_value();
136 return visit_continue
;
138 /* Mark this entry as having a constant assignment (if the
139 * assignment count doesn't go >1). do_constant_variable will fix
140 * up the variable with the constant value later.
142 entry
->constval
= constval
;
144 return visit_continue
;
148 ir_constant_variable_visitor::visit_enter(ir_call
*ir
)
150 /* Mark any out parameters as assigned to */
151 foreach_two_lists(formal_node
, &ir
->callee
->parameters
,
152 actual_node
, &ir
->actual_parameters
) {
153 ir_rvalue
*param_rval
= (ir_rvalue
*) actual_node
;
154 ir_variable
*param
= (ir_variable
*) formal_node
;
156 if (param
->data
.mode
== ir_var_function_out
||
157 param
->data
.mode
== ir_var_function_inout
) {
158 ir_variable
*var
= param_rval
->variable_referenced();
159 struct assignment_entry
*entry
;
162 entry
= get_assignment_entry(var
, this->ht
);
163 entry
->assignment_count
++;
167 /* Mark the return storage as having been assigned to */
168 if (ir
->return_deref
!= NULL
) {
169 ir_variable
*var
= ir
->return_deref
->variable_referenced();
170 struct assignment_entry
*entry
;
173 entry
= get_assignment_entry(var
, this->ht
);
174 entry
->assignment_count
++;
177 return visit_continue
;
181 * Does a copy propagation pass on the code present in the instruction stream.
184 do_constant_variable(exec_list
*instructions
)
186 bool progress
= false;
187 ir_constant_variable_visitor v
;
189 v
.ht
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
190 _mesa_key_pointer_equal
);
193 struct hash_entry
*hte
;
194 hash_table_foreach(v
.ht
, hte
) {
195 struct assignment_entry
*entry
= (struct assignment_entry
*) hte
->data
;
197 if (entry
->assignment_count
== 1 && entry
->constval
&& entry
->our_scope
) {
198 entry
->var
->constant_value
= entry
->constval
;
204 _mesa_hash_table_destroy(v
.ht
, NULL
);
210 do_constant_variable_unlinked(exec_list
*instructions
)
212 bool progress
= false;
214 foreach_in_list(ir_instruction
, ir
, instructions
) {
215 ir_function
*f
= ir
->as_function();
217 foreach_in_list(ir_function_signature
, sig
, &f
->signatures
) {
218 if (do_constant_variable(&sig
->body
))