nir: Rename parallel_copy_copy to parallel_copy_entry and add a foreach macro
[mesa.git] / src / glsl / nir / nir_opt_global_to_local.c
1 /*
2 * Copyright © 2014 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Connor Abbott (cwabbott0@gmail.com)
25 *
26 */
27
28 #include "nir.h"
29
30 static bool
31 global_to_local(nir_register *reg)
32 {
33 nir_function_impl *impl = NULL;
34
35 assert(reg->is_global);
36
37 struct set_entry *entry;
38 set_foreach(reg->defs, entry) {
39 nir_instr *instr = (nir_instr *) entry->key;
40 nir_function_impl *instr_impl =
41 nir_cf_node_get_function(&instr->block->cf_node);
42 if (impl != NULL) {
43 if (impl != instr_impl)
44 return false;
45 } else {
46 impl = instr_impl;
47 }
48 }
49
50 set_foreach(reg->uses, entry) {
51 nir_instr *instr = (nir_instr *) entry->key;
52 nir_function_impl *instr_impl =
53 nir_cf_node_get_function(&instr->block->cf_node);
54 if (impl != NULL) {
55 if (impl != instr_impl)
56 return false;
57 } else {
58 impl = instr_impl;
59 }
60 }
61
62 set_foreach(reg->if_uses, entry) {
63 nir_if *if_stmt = (nir_if *) entry->key;
64 nir_function_impl *if_impl = nir_cf_node_get_function(&if_stmt->cf_node);
65 if (impl != NULL) {
66 if (impl != if_impl)
67 return false;
68 } else {
69 impl = if_impl;
70 }
71 }
72
73 if (impl == NULL) {
74 /* this instruction is never used/defined, delete it */
75 nir_reg_remove(reg);
76 return true;
77 }
78
79 /*
80 * if we've gotten to this point, the register is always used/defined in
81 * the same implementation so we can move it to be local to that
82 * implementation.
83 */
84
85 exec_node_remove(&reg->node);
86 exec_list_push_tail(&impl->registers, &reg->node);
87 reg->index = impl->reg_alloc++;
88 reg->is_global = false;
89 return true;
90 }
91
92 bool
93 nir_opt_global_to_local(nir_shader *shader)
94 {
95 bool progress = false;
96
97 foreach_list_typed_safe(nir_register, reg, node, &shader->registers) {
98 if (global_to_local(reg))
99 progress = true;
100 }
101
102 return progress;
103 }