freedreno/ir3: rename s/frag_pos/frag_vcoord/g
[mesa.git] / src / gallium / drivers / freedreno / ir3 / ir3_depth.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29 #include "util/u_math.h"
30
31 #include "ir3.h"
32
33 /*
34 * Instruction Depth:
35 *
36 * Calculates weighted instruction depth, ie. the sum of # of needed
37 * instructions plus delay slots back to original input (ie INPUT or
38 * CONST). That is to say, an instructions depth is:
39 *
40 * depth(instr) {
41 * d = 0;
42 * // for each src register:
43 * foreach (src in instr->regs[1..n])
44 * d = max(d, delayslots(src->instr, n) + depth(src->instr));
45 * return d + 1;
46 * }
47 *
48 * After an instruction's depth is calculated, it is inserted into the
49 * blocks depth sorted list, which is used by the scheduling pass.
50 */
51
52 /* generally don't count false dependencies, since this can just be
53 * something like a barrier, or SSBO store. The exception is array
54 * dependencies if the assigner is an array write and the consumer
55 * reads the same array.
56 */
57 static bool
58 ignore_dep(struct ir3_instruction *assigner,
59 struct ir3_instruction *consumer, unsigned n)
60 {
61 if (!__is_false_dep(consumer, n))
62 return false;
63
64 if (assigner->barrier_class & IR3_BARRIER_ARRAY_W) {
65 struct ir3_register *dst = assigner->regs[0];
66 struct ir3_register *src;
67
68 debug_assert(dst->flags & IR3_REG_ARRAY);
69
70 foreach_src(src, consumer) {
71 if ((src->flags & IR3_REG_ARRAY) &&
72 (dst->array.id == src->array.id)) {
73 return false;
74 }
75 }
76 }
77
78 return true;
79 }
80
81 /* calculate required # of delay slots between the instruction that
82 * assigns a value and the one that consumes
83 */
84 int ir3_delayslots(struct ir3_instruction *assigner,
85 struct ir3_instruction *consumer, unsigned n)
86 {
87 if (ignore_dep(assigner, consumer, n))
88 return 0;
89
90 /* worst case is cat1-3 (alu) -> cat4/5 needing 6 cycles, normal
91 * alu -> alu needs 3 cycles, cat4 -> alu and texture fetch
92 * handled with sync bits
93 */
94
95 if (is_meta(assigner))
96 return 0;
97
98 if (writes_addr(assigner))
99 return 6;
100
101 /* handled via sync flags: */
102 if (is_sfu(assigner) || is_tex(assigner) || is_mem(assigner))
103 return 0;
104
105 /* assigner must be alu: */
106 if (is_flow(consumer) || is_sfu(consumer) || is_tex(consumer) ||
107 is_mem(consumer)) {
108 return 6;
109 } else if ((is_mad(consumer->opc) || is_madsh(consumer->opc)) &&
110 (n == 3)) {
111 /* special case, 3rd src to cat3 not required on first cycle */
112 return 1;
113 } else {
114 return 3;
115 }
116 }
117
118 void
119 ir3_insert_by_depth(struct ir3_instruction *instr, struct list_head *list)
120 {
121 /* remove from existing spot in list: */
122 list_delinit(&instr->node);
123
124 /* find where to re-insert instruction: */
125 list_for_each_entry (struct ir3_instruction, pos, list, node) {
126 if (pos->depth > instr->depth) {
127 list_add(&instr->node, &pos->node);
128 return;
129 }
130 }
131 /* if we get here, we didn't find an insertion spot: */
132 list_addtail(&instr->node, list);
133 }
134
135 static void
136 ir3_instr_depth(struct ir3_instruction *instr, unsigned boost, bool falsedep)
137 {
138 struct ir3_instruction *src;
139
140 /* don't mark falsedep's as used, but otherwise process them normally: */
141 if (!falsedep)
142 instr->flags &= ~IR3_INSTR_UNUSED;
143
144 if (ir3_instr_check_mark(instr))
145 return;
146
147 instr->depth = 0;
148
149 foreach_ssa_src_n(src, i, instr) {
150 unsigned sd;
151
152 /* visit child to compute it's depth: */
153 ir3_instr_depth(src, boost, __is_false_dep(instr, i));
154
155 /* for array writes, no need to delay on previous write: */
156 if (i == 0)
157 continue;
158
159 sd = ir3_delayslots(src, instr, i) + src->depth;
160 sd += boost;
161
162 instr->depth = MAX2(instr->depth, sd);
163 }
164
165 if (!is_meta(instr))
166 instr->depth++;
167
168 ir3_insert_by_depth(instr, &instr->block->instr_list);
169 }
170
171 static bool
172 remove_unused_by_block(struct ir3_block *block)
173 {
174 bool progress = false;
175 list_for_each_entry_safe (struct ir3_instruction, instr, &block->instr_list, node) {
176 if (instr->opc == OPC_END)
177 continue;
178 if (instr->flags & IR3_INSTR_UNUSED) {
179 list_delinit(&instr->node);
180 progress = true;
181 }
182 }
183 return progress;
184 }
185
186 static bool
187 compute_depth_and_remove_unused(struct ir3 *ir)
188 {
189 unsigned i;
190 bool progress = false;
191
192 ir3_clear_mark(ir);
193
194 /* initially mark everything as unused, we'll clear the flag as we
195 * visit the instructions:
196 */
197 list_for_each_entry (struct ir3_block, block, &ir->block_list, node) {
198 list_for_each_entry (struct ir3_instruction, instr, &block->instr_list, node) {
199 instr->flags |= IR3_INSTR_UNUSED;
200 }
201 }
202
203 for (i = 0; i < ir->noutputs; i++)
204 if (ir->outputs[i])
205 ir3_instr_depth(ir->outputs[i], 0, false);
206
207 list_for_each_entry (struct ir3_block, block, &ir->block_list, node) {
208 for (i = 0; i < block->keeps_count; i++)
209 ir3_instr_depth(block->keeps[i], 0, false);
210
211 /* We also need to account for if-condition: */
212 if (block->condition)
213 ir3_instr_depth(block->condition, 6, false);
214 }
215
216 /* mark un-used instructions: */
217 list_for_each_entry (struct ir3_block, block, &ir->block_list, node) {
218 progress |= remove_unused_by_block(block);
219 }
220
221 /* note that we can end up with unused indirects, but we should
222 * not end up with unused predicates.
223 */
224 for (i = 0; i < ir->indirects_count; i++) {
225 struct ir3_instruction *instr = ir->indirects[i];
226 if (instr && (instr->flags & IR3_INSTR_UNUSED))
227 ir->indirects[i] = NULL;
228 }
229
230 /* cleanup unused inputs: */
231 for (i = 0; i < ir->ninputs; i++) {
232 struct ir3_instruction *in = ir->inputs[i];
233 if (in && (in->flags & IR3_INSTR_UNUSED))
234 ir->inputs[i] = NULL;
235 }
236
237 return progress;
238 }
239
240 void
241 ir3_depth(struct ir3 *ir)
242 {
243 bool progress;
244 do {
245 progress = compute_depth_and_remove_unused(ir);
246 } while (progress);
247 }