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_intrinsic_instr
*intrin_instr
;
56 nir_tex_instr
*tex_instr
;
58 /* We use the pass_flags to store the live/dead information. In DCE, we
59 * just treat it as a zero/non-zero boolean for whether or not the
60 * instruction is live.
62 instr
->pass_flags
= 0;
64 switch (instr
->type
) {
65 case nir_instr_type_call
:
66 case nir_instr_type_jump
:
67 mark_and_push(worklist
, instr
);
70 case nir_instr_type_alu
:
71 alu_instr
= nir_instr_as_alu(instr
);
72 if (!alu_instr
->dest
.dest
.is_ssa
)
73 mark_and_push(worklist
, instr
);
76 case nir_instr_type_intrinsic
:
77 intrin_instr
= nir_instr_as_intrinsic(instr
);
78 if (nir_intrinsic_infos
[intrin_instr
->intrinsic
].flags
&
79 NIR_INTRINSIC_CAN_ELIMINATE
) {
80 if (nir_intrinsic_infos
[intrin_instr
->intrinsic
].has_dest
&&
81 !intrin_instr
->dest
.is_ssa
) {
82 mark_and_push(worklist
, instr
);
85 mark_and_push(worklist
, instr
);
89 case nir_instr_type_tex
:
90 tex_instr
= nir_instr_as_tex(instr
);
91 if (!tex_instr
->dest
.is_ssa
)
92 mark_and_push(worklist
, instr
);
101 init_block(nir_block
*block
, nir_instr_worklist
*worklist
)
103 nir_foreach_instr(instr
, block
)
104 init_instr(instr
, worklist
);
106 nir_if
*following_if
= nir_block_get_following_if(block
);
108 if (following_if
->condition
.is_ssa
&&
109 !following_if
->condition
.ssa
->parent_instr
->pass_flags
)
110 mark_and_push(worklist
, following_if
->condition
.ssa
->parent_instr
);
117 nir_opt_dce_impl(nir_function_impl
*impl
)
119 nir_instr_worklist
*worklist
= nir_instr_worklist_create();
121 nir_foreach_block(block
, impl
) {
122 init_block(block
, worklist
);
125 nir_instr
*instr
= NULL
;
126 nir_instr_worklist_foreach(worklist
, instr
)
127 nir_foreach_src(instr
, mark_live_cb
, worklist
);
129 nir_instr_worklist_destroy(worklist
);
131 bool progress
= false;
133 nir_foreach_block(block
, impl
) {
134 nir_foreach_instr_safe(instr
, block
) {
135 if (!instr
->pass_flags
) {
136 nir_instr_remove(instr
);
143 nir_metadata_preserve(impl
, nir_metadata_block_index
|
144 nir_metadata_dominance
);
150 nir_opt_dce(nir_shader
*shader
)
152 bool progress
= false;
153 nir_foreach_function(function
, shader
) {
154 if (function
->impl
&& nir_opt_dce_impl(function
->impl
))