nir: fix gather_ssa_types
[mesa.git] / src / compiler / nir / nir_opt_rematerialize_compares.c
1 /*
2 * Copyright © 2019 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
24 #include "nir.h"
25 #include "nir/nir_builder.h"
26 #include "nir_constant_expressions.h"
27 #include "nir_control_flow.h"
28 #include "nir_loop_analyze.h"
29
30 static bool
31 is_two_src_comparison(const nir_alu_instr *instr)
32 {
33 switch (instr->op) {
34 case nir_op_flt:
35 case nir_op_flt32:
36 case nir_op_fge:
37 case nir_op_fge32:
38 case nir_op_feq:
39 case nir_op_feq32:
40 case nir_op_fne:
41 case nir_op_fne32:
42 case nir_op_ilt:
43 case nir_op_ilt32:
44 case nir_op_ult:
45 case nir_op_ult32:
46 case nir_op_ige:
47 case nir_op_ige32:
48 case nir_op_uge:
49 case nir_op_uge32:
50 case nir_op_ieq:
51 case nir_op_ieq32:
52 case nir_op_ine:
53 case nir_op_ine32:
54 return true;
55 default:
56 return false;
57 }
58 }
59
60 static bool
61 all_uses_are_bcsel(const nir_alu_instr *instr)
62 {
63 if (!instr->dest.dest.is_ssa)
64 return false;
65
66 nir_foreach_use(use, &instr->dest.dest.ssa) {
67 if (use->parent_instr->type != nir_instr_type_alu)
68 return false;
69
70 nir_alu_instr *const alu = nir_instr_as_alu(use->parent_instr);
71 if (alu->op != nir_op_bcsel &&
72 alu->op != nir_op_b32csel)
73 return false;
74
75 /* Not only must the result be used by a bcsel, but it must be used as
76 * the first source (the condition).
77 */
78 if (alu->src[0].src.ssa != &instr->dest.dest.ssa)
79 return false;
80 }
81
82 return true;
83 }
84
85 static bool
86 nir_opt_rematerialize_compares_impl(nir_shader *shader, nir_function_impl *impl)
87 {
88 bool progress = false;
89
90 nir_foreach_block(block, impl) {
91 nir_foreach_instr(instr, block) {
92 if (instr->type != nir_instr_type_alu)
93 continue;
94
95 nir_alu_instr *const alu = nir_instr_as_alu(instr);
96 if (!is_two_src_comparison(alu))
97 continue;
98
99 if (!all_uses_are_bcsel(alu))
100 continue;
101
102 /* At this point it is known that alu is a comparison instruction
103 * that is only used by nir_op_bcsel and possibly by if-statements
104 * (though the latter has not been explicitly checked).
105 *
106 * Iterate through each use of the comparison. For every use (or use
107 * by an if-statement) that is in a different block, emit a copy of
108 * the comparison. Care must be taken here. The original
109 * instruction must be duplicated only once in each block because CSE
110 * cannot be run after this pass.
111 */
112 nir_foreach_use_safe(use, &alu->dest.dest.ssa) {
113 nir_instr *const use_instr = use->parent_instr;
114
115 /* If the use is in the same block as the def, don't
116 * rematerialize.
117 */
118 if (use_instr->block == alu->instr.block)
119 continue;
120
121 nir_alu_instr *clone = nir_alu_instr_clone(shader, alu);
122
123 nir_instr_insert_before(use_instr, &clone->instr);
124
125 nir_alu_instr *const use_alu = nir_instr_as_alu(use_instr);
126 for (unsigned i = 0; i < nir_op_infos[use_alu->op].num_inputs; i++) {
127 if (use_alu->src[i].src.ssa == &alu->dest.dest.ssa) {
128 nir_instr_rewrite_src(&use_alu->instr,
129 &use_alu->src[i].src,
130 nir_src_for_ssa(&clone->dest.dest.ssa));
131 progress = true;
132 }
133 }
134 }
135
136 nir_foreach_if_use_safe(use, &alu->dest.dest.ssa) {
137 nir_if *const if_stmt = use->parent_if;
138
139 nir_block *const prev_block =
140 nir_cf_node_as_block(nir_cf_node_prev(&if_stmt->cf_node));
141
142 /* If the compare is from the previous block, don't
143 * rematerialize.
144 */
145 if (prev_block == alu->instr.block)
146 continue;
147
148 nir_alu_instr *clone = nir_alu_instr_clone(shader, alu);
149
150 nir_instr_insert_after_block(prev_block, &clone->instr);
151
152 nir_if_rewrite_condition(if_stmt,
153 nir_src_for_ssa(&clone->dest.dest.ssa));
154 progress = true;
155 }
156 }
157 }
158
159 if (progress) {
160 nir_metadata_preserve(impl, nir_metadata_block_index |
161 nir_metadata_dominance);
162 }
163
164 return progress;
165 }
166
167 bool
168 nir_opt_rematerialize_compares(nir_shader *shader)
169 {
170 bool progress = false;
171
172 nir_foreach_function(function, shader) {
173 if (function->impl == NULL)
174 continue;
175
176 progress = nir_opt_rematerialize_compares_impl(shader, function->impl)
177 || progress;
178 }
179
180 return progress;
181 }