• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /* calculate required # of delay slots between the instruction that
53  * assigns a value and the one that consumes
54  */
ir3_delayslots(struct ir3_instruction * assigner,struct ir3_instruction * consumer,unsigned n)55 int ir3_delayslots(struct ir3_instruction *assigner,
56 		struct ir3_instruction *consumer, unsigned n)
57 {
58 	/* don't count false-dependencies: */
59 	if (__is_false_dep(consumer, n))
60 		return 0;
61 
62 	/* worst case is cat1-3 (alu) -> cat4/5 needing 6 cycles, normal
63 	 * alu -> alu needs 3 cycles, cat4 -> alu and texture fetch
64 	 * handled with sync bits
65 	 */
66 
67 	if (is_meta(assigner))
68 		return 0;
69 
70 	if (writes_addr(assigner))
71 		return 6;
72 
73 	/* handled via sync flags: */
74 	if (is_sfu(assigner) || is_tex(assigner) || is_mem(assigner))
75 		return 0;
76 
77 	/* assigner must be alu: */
78 	if (is_flow(consumer) || is_sfu(consumer) || is_tex(consumer) ||
79 			is_mem(consumer)) {
80 		return 6;
81 	} else if ((is_mad(consumer->opc) || is_madsh(consumer->opc)) &&
82 			(n == 3)) {
83 		/* special case, 3rd src to cat3 not required on first cycle */
84 		return 1;
85 	} else {
86 		return 3;
87 	}
88 }
89 
90 void
ir3_insert_by_depth(struct ir3_instruction * instr,struct list_head * list)91 ir3_insert_by_depth(struct ir3_instruction *instr, struct list_head *list)
92 {
93 	/* remove from existing spot in list: */
94 	list_delinit(&instr->node);
95 
96 	/* find where to re-insert instruction: */
97 	list_for_each_entry (struct ir3_instruction, pos, list, node) {
98 		if (pos->depth > instr->depth) {
99 			list_add(&instr->node, &pos->node);
100 			return;
101 		}
102 	}
103 	/* if we get here, we didn't find an insertion spot: */
104 	list_addtail(&instr->node, list);
105 }
106 
107 static void
ir3_instr_depth(struct ir3_instruction * instr)108 ir3_instr_depth(struct ir3_instruction *instr)
109 {
110 	struct ir3_instruction *src;
111 
112 	/* if we've already visited this instruction, bail now: */
113 	if (ir3_instr_check_mark(instr))
114 		return;
115 
116 	instr->depth = 0;
117 
118 	foreach_ssa_src_n(src, i, instr) {
119 		unsigned sd;
120 
121 		/* visit child to compute it's depth: */
122 		ir3_instr_depth(src);
123 
124 		/* for array writes, no need to delay on previous write: */
125 		if (i == 0)
126 			continue;
127 
128 		sd = ir3_delayslots(src, instr, i) + src->depth;
129 
130 		instr->depth = MAX2(instr->depth, sd);
131 	}
132 
133 	if (!is_meta(instr))
134 		instr->depth++;
135 
136 	ir3_insert_by_depth(instr, &instr->block->instr_list);
137 }
138 
139 static void
remove_unused_by_block(struct ir3_block * block)140 remove_unused_by_block(struct ir3_block *block)
141 {
142 	list_for_each_entry_safe (struct ir3_instruction, instr, &block->instr_list, node) {
143 		if (!ir3_instr_check_mark(instr)) {
144 			if (instr->opc == OPC_END)
145 				continue;
146 			/* mark it, in case it is input, so we can
147 			 * remove unused inputs:
148 			 */
149 			instr->flags |= IR3_INSTR_UNUSED;
150 			/* and remove from instruction list: */
151 			list_delinit(&instr->node);
152 		}
153 	}
154 }
155 
156 void
ir3_depth(struct ir3 * ir)157 ir3_depth(struct ir3 *ir)
158 {
159 	unsigned i;
160 
161 	ir3_clear_mark(ir);
162 	for (i = 0; i < ir->noutputs; i++)
163 		if (ir->outputs[i])
164 			ir3_instr_depth(ir->outputs[i]);
165 
166 	list_for_each_entry (struct ir3_block, block, &ir->block_list, node) {
167 		for (i = 0; i < block->keeps_count; i++)
168 			ir3_instr_depth(block->keeps[i]);
169 
170 		/* We also need to account for if-condition: */
171 		if (block->condition)
172 			ir3_instr_depth(block->condition);
173 	}
174 
175 	/* mark un-used instructions: */
176 	list_for_each_entry (struct ir3_block, block, &ir->block_list, node) {
177 		remove_unused_by_block(block);
178 	}
179 
180 	/* note that we can end up with unused indirects, but we should
181 	 * not end up with unused predicates.
182 	 */
183 	for (i = 0; i < ir->indirects_count; i++) {
184 		struct ir3_instruction *instr = ir->indirects[i];
185 		if (instr->flags & IR3_INSTR_UNUSED)
186 			ir->indirects[i] = NULL;
187 	}
188 
189 	/* cleanup unused inputs: */
190 	for (i = 0; i < ir->ninputs; i++) {
191 		struct ir3_instruction *in = ir->inputs[i];
192 		if (in && (in->flags & IR3_INSTR_UNUSED))
193 			ir->inputs[i] = NULL;
194 	}
195 }
196