2 * Copyright © 2014 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 DEALINGS
24 * Jason Ekstrand (jason@jlekstrand.net)
29 * This lowering pass detects when a global variable is only being used by
30 * one function and makes it local to that function
36 register_var_use(nir_variable
*var
, nir_function_impl
*impl
,
37 struct hash_table
*var_func_table
)
39 if (var
->data
.mode
!= nir_var_shader_temp
)
42 struct hash_entry
*entry
=
43 _mesa_hash_table_search(var_func_table
, var
);
46 if (entry
->data
!= impl
)
49 _mesa_hash_table_insert(var_func_table
, var
, impl
);
54 mark_global_var_uses_block(nir_block
*block
, nir_function_impl
*impl
,
55 struct hash_table
*var_func_table
)
57 nir_foreach_instr(instr
, block
) {
58 if (instr
->type
== nir_instr_type_deref
) {
59 nir_deref_instr
*deref
= nir_instr_as_deref(instr
);
60 if (deref
->deref_type
== nir_deref_type_var
)
61 register_var_use(deref
->var
, impl
, var_func_table
);
69 nir_lower_global_vars_to_local(nir_shader
*shader
)
71 bool progress
= false;
73 /* A hash table keyed on variable pointers that stores the unique
74 * nir_function_impl that uses the given variable. If a variable is
75 * used in multiple functions, the data for the given key will be NULL.
77 struct hash_table
*var_func_table
= _mesa_pointer_hash_table_create(NULL
);
79 nir_foreach_function(function
, shader
) {
81 nir_foreach_block(block
, function
->impl
)
82 mark_global_var_uses_block(block
, function
->impl
, var_func_table
);
86 nir_foreach_variable_safe(var
, &shader
->globals
) {
87 struct hash_entry
*entry
= _mesa_hash_table_search(var_func_table
, var
);
91 nir_function_impl
*impl
= entry
->data
;
93 assert(var
->data
.mode
== nir_var_shader_temp
);
96 exec_node_remove(&var
->node
);
97 var
->data
.mode
= nir_var_function_temp
;
98 exec_list_push_tail(&impl
->locals
, &var
->node
);
99 nir_metadata_preserve(impl
, nir_metadata_block_index
|
100 nir_metadata_dominance
|
101 nir_metadata_live_ssa_defs
);
106 _mesa_hash_table_destroy(var_func_table
, NULL
);
109 nir_fixup_deref_modes(shader
);
112 nir_foreach_function(function
, shader
) {
113 if (function
->impl
) {
114 function
->impl
->valid_metadata
&= ~nir_metadata_not_properly_reset
;