1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
/*
* Copyright © 2014 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Authors:
* Jason Ekstrand (jason@jlekstrand.net)
*
*/
#include "nir.h"
/*
* Implements a small peephole optimization that looks for
*
* if (cond) {
* <empty>
* } else {
* <empty>
* }
* phi
* ...
* phi
*
* and replaces it with a series of selects. It can also handle the case
* where, instead of being empty, the if may contain some move operations
* whose only use is one of the following phi nodes. This happens all the
* time when the SSA form comes from a conditional assignment with a
* swizzle.
*/
struct peephole_select_state {
void *mem_ctx;
bool progress;
};
static bool
are_all_move_to_phi(nir_block *block)
{
nir_foreach_instr(block, instr) {
if (instr->type != nir_instr_type_alu)
return false;
/* It must be a move operation */
nir_alu_instr *mov = nir_instr_as_alu(instr);
if (mov->op != nir_op_fmov && mov->op != nir_op_imov)
return false;
/* Can't handle saturate */
if (mov->dest.saturate)
return false;
/* It must be SSA */
if (!mov->dest.dest.is_ssa)
return false;
/* It cannot have any if-uses */
if (mov->dest.dest.ssa.if_uses->entries != 0)
return false;
/* The only uses of this definition must be phi's in the successor */
struct set_entry *entry;
set_foreach(mov->dest.dest.ssa.uses, entry) {
const nir_instr *dest_instr = entry->key;
if (dest_instr->type != nir_instr_type_phi ||
dest_instr->block != block->successors[0])
return false;
}
}
return true;
}
static bool
nir_opt_peephole_select_block(nir_block *block, void *void_state)
{
struct peephole_select_state *state = void_state;
/* If the block is empty, then it certainly doesn't have any phi nodes,
* so we can skip it. This also ensures that we do an early skip on the
* end block of the function which isn't actually attached to the CFG.
*/
if (exec_list_is_empty(&block->instr_list))
return true;
if (nir_cf_node_is_first(&block->cf_node))
return true;
nir_cf_node *prev_node = nir_cf_node_prev(&block->cf_node);
if (prev_node->type != nir_cf_node_if)
return true;
nir_if *if_stmt = nir_cf_node_as_if(prev_node);
nir_cf_node *then_node = nir_if_first_then_node(if_stmt);
nir_cf_node *else_node = nir_if_first_else_node(if_stmt);
/* We can only have one block in each side ... */
if (nir_if_last_then_node(if_stmt) != then_node ||
nir_if_last_else_node(if_stmt) != else_node)
return true;
nir_block *then_block = nir_cf_node_as_block(then_node);
nir_block *else_block = nir_cf_node_as_block(else_node);
/* ... and those blocks must only contain move-to-phi. */
if (!are_all_move_to_phi(then_block) || !are_all_move_to_phi(else_block))
return true;
/* At this point, we know that the previous CFG node is an if-then
* statement containing only moves to phi nodes in this block. We can
* just remove that entire CF node and replace all of the phi nodes with
* selects.
*/
nir_foreach_instr_safe(block, instr) {
if (instr->type != nir_instr_type_phi)
break;
nir_phi_instr *phi = nir_instr_as_phi(instr);
nir_alu_instr *sel = nir_alu_instr_create(state->mem_ctx, nir_op_bcsel);
nir_src_copy(&sel->src[0].src, &if_stmt->condition, state->mem_ctx);
/* Splat the condition to all channels */
memset(sel->src[0].swizzle, 0, sizeof sel->src[0].swizzle);
assert(exec_list_length(&phi->srcs) == 2);
nir_foreach_phi_src(phi, src) {
assert(src->pred == then_block || src->pred == else_block);
assert(src->src.is_ssa);
unsigned idx = src->pred == then_block ? 1 : 2;
if (src->src.ssa->parent_instr->block == src->pred) {
/* We already know that this instruction must be a move with
* this phi's in this block as its only users.
*/
nir_alu_instr *mov = nir_instr_as_alu(src->src.ssa->parent_instr);
assert(mov->instr.type == nir_instr_type_alu);
assert(mov->op == nir_op_fmov || mov->op == nir_op_imov);
nir_alu_src_copy(&sel->src[idx], &mov->src[0], state->mem_ctx);
} else {
nir_src_copy(&sel->src[idx].src, &src->src, state->mem_ctx);
}
}
nir_ssa_dest_init(&sel->instr, &sel->dest.dest,
phi->dest.ssa.num_components, phi->dest.ssa.name);
sel->dest.write_mask = (1 << phi->dest.ssa.num_components) - 1;
nir_ssa_def_rewrite_uses(&phi->dest.ssa,
nir_src_for_ssa(&sel->dest.dest.ssa),
state->mem_ctx);
nir_instr_insert_before(&phi->instr, &sel->instr);
nir_instr_remove(&phi->instr);
}
nir_cf_node_remove(&if_stmt->cf_node);
state->progress = true;
return true;
}
static bool
nir_opt_peephole_select_impl(nir_function_impl *impl)
{
struct peephole_select_state state;
state.mem_ctx = ralloc_parent(impl);
state.progress = false;
nir_foreach_block(impl, nir_opt_peephole_select_block, &state);
if (state.progress)
nir_metadata_preserve(impl, nir_metadata_none);
return state.progress;
}
bool
nir_opt_peephole_select(nir_shader *shader)
{
bool progress = false;
nir_foreach_overload(shader, overload) {
if (overload->impl)
progress |= nir_opt_peephole_select_impl(overload->impl);
}
return progress;
}
|