• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
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 FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  *
23  * Authors:
24  *    Rob Clark <robclark@freedesktop.org>
25  */
26 
27 #include "util/u_math.h"
28 
29 #include "ir3.h"
30 #include "ir3_shader.h"
31 
32 /*
33  * Dead code elimination:
34  */
35 
36 static void
mark_array_use(struct ir3_instruction * instr,struct ir3_register * reg)37 mark_array_use(struct ir3_instruction *instr, struct ir3_register *reg)
38 {
39 	if (reg->flags & IR3_REG_ARRAY) {
40 		struct ir3_array *arr =
41 			ir3_lookup_array(instr->block->shader, reg->array.id);
42 		arr->unused = false;
43 	}
44 }
45 
46 static void
instr_dce(struct ir3_instruction * instr,bool falsedep)47 instr_dce(struct ir3_instruction *instr, bool falsedep)
48 {
49 	/* don't mark falsedep's as used, but otherwise process them normally: */
50 	if (!falsedep)
51 		instr->flags &= ~IR3_INSTR_UNUSED;
52 
53 	if (ir3_instr_check_mark(instr))
54 		return;
55 
56 	if (writes_gpr(instr))
57 		mark_array_use(instr, instr->regs[0]);   /* dst */
58 
59 	foreach_src (reg, instr)
60 		mark_array_use(instr, reg);              /* src */
61 
62 	foreach_ssa_src_n (src, i, instr) {
63 		instr_dce(src, __is_false_dep(instr, i));
64 	}
65 }
66 
67 static bool
remove_unused_by_block(struct ir3_block * block)68 remove_unused_by_block(struct ir3_block *block)
69 {
70 	bool progress = false;
71 	foreach_instr_safe (instr, &block->instr_list) {
72 		if (instr->opc == OPC_END || instr->opc == OPC_CHSH || instr->opc == OPC_CHMASK)
73 			continue;
74 		if (instr->flags & IR3_INSTR_UNUSED) {
75 			if (instr->opc == OPC_META_SPLIT) {
76 				struct ir3_instruction *src = ssa(instr->regs[1]);
77 				/* tex (cat5) instructions have a writemask, so we can
78 				 * mask off unused components.  Other instructions do not.
79 				 */
80 				if (src && is_tex_or_prefetch(src) && (src->regs[0]->wrmask > 1)) {
81 					src->regs[0]->wrmask &= ~(1 << instr->split.off);
82 
83 					/* prune no-longer needed right-neighbors.  We could
84 					 * probably do the same for left-neighbors (ie. tex
85 					 * fetch that only need .yw components), but that
86 					 * makes RA a bit more confusing than it already is
87 					 */
88 					struct ir3_instruction *n = instr;
89 					while (n && n->cp.right)
90 						n = n->cp.right;
91 					while (n->flags & IR3_INSTR_UNUSED) {
92 						n = n->cp.left;
93 						if (!n)
94 							break;
95 						n->cp.right = NULL;
96 					}
97 				}
98 			}
99 
100 			/* prune false-deps, etc: */
101 			foreach_ssa_use (use, instr)
102 				foreach_ssa_srcp_n (srcp, n, use)
103 					if (*srcp == instr)
104 						*srcp = NULL;
105 
106 			list_delinit(&instr->node);
107 			progress = true;
108 		}
109 	}
110 	return progress;
111 }
112 
113 static bool
find_and_remove_unused(struct ir3 * ir,struct ir3_shader_variant * so)114 find_and_remove_unused(struct ir3 *ir, struct ir3_shader_variant *so)
115 {
116 	unsigned i;
117 	bool progress = false;
118 
119 	ir3_clear_mark(ir);
120 
121 	/* initially mark everything as unused, we'll clear the flag as we
122 	 * visit the instructions:
123 	 */
124 	foreach_block (block, &ir->block_list) {
125 		foreach_instr (instr, &block->instr_list) {
126 			/* special case, if pre-fs texture fetch used, we cannot
127 			 * eliminate the barycentric i/j input
128 			 */
129 			if (so->num_sampler_prefetch &&
130 					(instr->opc == OPC_META_INPUT) &&
131 					(instr->input.sysval == SYSTEM_VALUE_BARYCENTRIC_PERSP_PIXEL))
132 				continue;
133 			instr->flags |= IR3_INSTR_UNUSED;
134 		}
135 	}
136 
137 	foreach_array (arr, &ir->array_list)
138 		arr->unused = true;
139 
140 	foreach_output (out, ir)
141 		instr_dce(out, false);
142 
143 	foreach_block (block, &ir->block_list) {
144 		for (i = 0; i < block->keeps_count; i++)
145 			instr_dce(block->keeps[i], false);
146 
147 		/* We also need to account for if-condition: */
148 		if (block->condition)
149 			instr_dce(block->condition, false);
150 	}
151 
152 	/* remove un-used instructions: */
153 	foreach_block (block, &ir->block_list) {
154 		progress |= remove_unused_by_block(block);
155 	}
156 
157 	/* remove un-used arrays: */
158 	foreach_array_safe (arr, &ir->array_list) {
159 		if (arr->unused)
160 			list_delinit(&arr->node);
161 	}
162 
163 	/* fixup wrmask of split instructions to account for adjusted tex
164 	 * wrmask's:
165 	 */
166 	foreach_block (block, &ir->block_list) {
167 		foreach_instr (instr, &block->instr_list) {
168 			if (instr->opc != OPC_META_SPLIT)
169 				continue;
170 
171 			struct ir3_instruction *src = ssa(instr->regs[1]);
172 			if (!is_tex_or_prefetch(src))
173 				continue;
174 
175 			instr->regs[1]->wrmask = src->regs[0]->wrmask;
176 		}
177 	}
178 
179 	/* note that we can end up with unused indirects, but we should
180 	 * not end up with unused predicates.
181 	 */
182 	for (i = 0; i < ir->a0_users_count; i++) {
183 		struct ir3_instruction *instr = ir->a0_users[i];
184 		if (instr && (instr->flags & IR3_INSTR_UNUSED))
185 			ir->a0_users[i] = NULL;
186 	}
187 
188 	for (i = 0; i < ir->a1_users_count; i++) {
189 		struct ir3_instruction *instr = ir->a1_users[i];
190 		if (instr && (instr->flags & IR3_INSTR_UNUSED))
191 			ir->a1_users[i] = NULL;
192 	}
193 
194 	/* cleanup unused inputs: */
195 	foreach_input_n (in, n, ir)
196 		if (in->flags & IR3_INSTR_UNUSED)
197 			ir->inputs[n] = NULL;
198 
199 	return progress;
200 }
201 
202 bool
ir3_dce(struct ir3 * ir,struct ir3_shader_variant * so)203 ir3_dce(struct ir3 *ir, struct ir3_shader_variant *so)
204 {
205 	void *mem_ctx = ralloc_context(NULL);
206 	bool progress, made_progress = false;
207 
208 	ir3_find_ssa_uses(ir, mem_ctx, true);
209 
210 	do {
211 		progress = find_and_remove_unused(ir, so);
212 		made_progress |= progress;
213 	} while (progress);
214 
215 	ralloc_free(mem_ctx);
216 
217 	return made_progress;
218 }
219