570e43000c08a5d9367404c7bccef460b737b5b2
[mesa.git] / nir_opt_dce.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 #include "nir_worklist.h"
30
31 /* SSA-based mark-and-sweep dead code elimination */
32
33 static void
34 mark_and_push(nir_instr_worklist *wl, nir_instr *instr)
35 {
36 nir_instr_worklist_push_tail(wl, instr);
37 instr->pass_flags = 1;
38 }
39
40 static bool
41 mark_live_cb(nir_src *src, void *_state)
42 {
43 nir_instr_worklist *worklist = (nir_instr_worklist *) _state;
44
45 if (src->is_ssa && !src->ssa->parent_instr->pass_flags)
46 mark_and_push(worklist, src->ssa->parent_instr);
47
48 return true;
49 }
50
51 static void
52 init_instr(nir_instr *instr, nir_instr_worklist *worklist)
53 {
54 nir_alu_instr *alu_instr;
55 nir_intrinsic_instr *intrin_instr;
56 nir_tex_instr *tex_instr;
57
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.
61 */
62 instr->pass_flags = 0;
63
64 switch (instr->type) {
65 case nir_instr_type_call:
66 case nir_instr_type_jump:
67 mark_and_push(worklist, instr);
68 break;
69
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);
74 break;
75
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);
83 }
84 } else {
85 mark_and_push(worklist, instr);
86 }
87 break;
88
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);
93 break;
94
95 default:
96 break;
97 }
98 }
99
100 static bool
101 init_block(nir_block *block, nir_instr_worklist *worklist)
102 {
103 nir_foreach_instr(instr, block)
104 init_instr(instr, worklist);
105
106 nir_if *following_if = nir_block_get_following_if(block);
107 if (following_if) {
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);
111 }
112
113 return true;
114 }
115
116 static bool
117 nir_opt_dce_impl(nir_function_impl *impl)
118 {
119 nir_instr_worklist *worklist = nir_instr_worklist_create();
120
121 nir_foreach_block(block, impl) {
122 init_block(block, worklist);
123 }
124
125 nir_instr *instr = NULL;
126 nir_instr_worklist_foreach(worklist, instr)
127 nir_foreach_src(instr, mark_live_cb, worklist);
128
129 nir_instr_worklist_destroy(worklist);
130
131 bool progress = false;
132
133 nir_foreach_block(block, impl) {
134 nir_foreach_instr_safe(instr, block) {
135 if (!instr->pass_flags) {
136 nir_instr_remove(instr);
137 progress = true;
138 }
139 }
140 }
141
142 if (progress)
143 nir_metadata_preserve(impl, nir_metadata_block_index |
144 nir_metadata_dominance);
145
146 return progress;
147 }
148
149 bool
150 nir_opt_dce(nir_shader *shader)
151 {
152 bool progress = false;
153 nir_foreach_function(function, shader) {
154 if (function->impl && nir_opt_dce_impl(function->impl))
155 progress = true;
156 }
157
158 return progress;
159 }