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 * Connor Abbott (cwabbott0@gmail.com)
29 #include "nir_worklist.h"
31 /* SSA-based mark-and-sweep dead code elimination */
34 mark_and_push(nir_instr_worklist
*wl
, nir_instr
*instr
)
36 nir_instr_worklist_push_tail(wl
, instr
);
37 instr
->pass_flags
= 1;
41 mark_live_cb(nir_src
*src
, void *_state
)
43 nir_instr_worklist
*worklist
= (nir_instr_worklist
*) _state
;
45 if (src
->is_ssa
&& !src
->ssa
->parent_instr
->pass_flags
)
46 mark_and_push(worklist
, src
->ssa
->parent_instr
);
52 init_instr(nir_instr
*instr
, nir_instr_worklist
*worklist
)
54 nir_alu_instr
*alu_instr
;
55 nir_deref_instr
*deref_instr
;
56 nir_intrinsic_instr
*intrin_instr
;
57 nir_tex_instr
*tex_instr
;
59 /* We use the pass_flags to store the live/dead information. In DCE, we
60 * just treat it as a zero/non-zero boolean for whether or not the
61 * instruction is live.
63 instr
->pass_flags
= 0;
65 switch (instr
->type
) {
66 case nir_instr_type_call
:
67 case nir_instr_type_jump
:
68 mark_and_push(worklist
, instr
);
71 case nir_instr_type_alu
:
72 alu_instr
= nir_instr_as_alu(instr
);
73 if (!alu_instr
->dest
.dest
.is_ssa
)
74 mark_and_push(worklist
, instr
);
77 case nir_instr_type_deref
:
78 deref_instr
= nir_instr_as_deref(instr
);
79 if (!deref_instr
->dest
.is_ssa
)
80 mark_and_push(worklist
, instr
);
83 case nir_instr_type_intrinsic
:
84 intrin_instr
= nir_instr_as_intrinsic(instr
);
85 if (nir_intrinsic_infos
[intrin_instr
->intrinsic
].flags
&
86 NIR_INTRINSIC_CAN_ELIMINATE
) {
87 if (nir_intrinsic_infos
[intrin_instr
->intrinsic
].has_dest
&&
88 !intrin_instr
->dest
.is_ssa
) {
89 mark_and_push(worklist
, instr
);
92 mark_and_push(worklist
, instr
);
96 case nir_instr_type_tex
:
97 tex_instr
= nir_instr_as_tex(instr
);
98 if (!tex_instr
->dest
.is_ssa
)
99 mark_and_push(worklist
, instr
);
108 init_block(nir_block
*block
, nir_instr_worklist
*worklist
)
110 nir_foreach_instr(instr
, block
)
111 init_instr(instr
, worklist
);
113 nir_if
*following_if
= nir_block_get_following_if(block
);
115 if (following_if
->condition
.is_ssa
&&
116 !following_if
->condition
.ssa
->parent_instr
->pass_flags
)
117 mark_and_push(worklist
, following_if
->condition
.ssa
->parent_instr
);
124 nir_opt_dce_impl(nir_function_impl
*impl
)
126 nir_instr_worklist
*worklist
= nir_instr_worklist_create();
128 nir_foreach_block(block
, impl
) {
129 init_block(block
, worklist
);
132 nir_foreach_instr_in_worklist(instr
, worklist
)
133 nir_foreach_src(instr
, mark_live_cb
, worklist
);
135 nir_instr_worklist_destroy(worklist
);
137 bool progress
= false;
139 nir_foreach_block(block
, impl
) {
140 nir_foreach_instr_safe(instr
, block
) {
141 if (!instr
->pass_flags
) {
142 nir_instr_remove(instr
);
149 nir_metadata_preserve(impl
, nir_metadata_block_index
|
150 nir_metadata_dominance
);
153 impl
->valid_metadata
&= ~nir_metadata_not_properly_reset
;
161 nir_opt_dce(nir_shader
*shader
)
163 bool progress
= false;
164 nir_foreach_function(function
, shader
) {
165 if (function
->impl
&& nir_opt_dce_impl(function
->impl
))