aboutsummaryrefslogtreecommitdiffstats
path: root/src/freedreno/ir3/ir3_group.c
blob: c9859197a1e73118b4d4d2d36cc82320995142c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Authors:
 *    Rob Clark <robclark@freedesktop.org>
 */

#include "ir3.h"

/*
 * Find/group instruction neighbors:
 */

static void
insert_mov(struct ir3_instruction *collect, int idx)
{
	struct ir3_instruction *src = ssa(collect->regs[idx+1]);
	struct ir3_instruction *mov = ir3_MOV(src->block, src, TYPE_F32);
	collect->regs[idx+1]->instr = mov;

	/* if collect and src are in the same block, move the inserted mov
	 * to just before the collect to avoid a use-before-def.  Otherwise
	 * it should be safe to leave at the end of the block it is in:
	 */
	if (src->block == collect->block) {
		list_delinit(&mov->node);
		list_addtail(&mov->node, &collect->node);
	}
}

/* verify that cur != instr, but cur is also not in instr's neighbor-list: */
static bool
in_neighbor_list(struct ir3_instruction *instr, struct ir3_instruction *cur, int pos)
{
	int idx = 0;

	if (!instr)
		return false;

	if (instr == cur)
		return true;

	for (instr = ir3_neighbor_first(instr); instr; instr = instr->cp.right)
		if ((idx++ != pos) && (instr == cur))
			return true;

	return false;
}

static void
group_collect(struct ir3_instruction *collect)
{
	struct ir3_register **regs = &collect->regs[1];
	unsigned n = collect->regs_count - 1;

	/* first pass, figure out what has conflicts and needs a mov
	 * inserted.  Do this up front, before starting to setup
	 * left/right neighbor pointers.  Trying to do it in a single
	 * pass could result in a situation where we can't even setup
	 * the mov's right neighbor ptr if the next instr also needs
	 * a mov.
	 */
restart:
	for (unsigned i = 0; i < n; i++) {
		struct ir3_instruction *instr = ssa(regs[i]);
		if (instr) {
			struct ir3_instruction *left = (i > 0) ? ssa(regs[i - 1]) : NULL;
			struct ir3_instruction *right = (i < (n-1)) ? ssa(regs[i + 1]) : NULL;
			bool conflict;

			/* check for left/right neighbor conflicts: */
			conflict = conflicts(instr->cp.left, left) ||
				conflicts(instr->cp.right, right);

			/* Mixing array elements and higher register classes
			 * (ie. groups) doesn't really work out in RA.  See:
			 *
			 * https://trello.com/c/DqeDkeVf/156-bug-with-stk-70frag
			 */
			if (instr->regs[0]->flags & IR3_REG_ARRAY)
				conflict = true;

			/* we also can't have an instr twice in the group: */
			for (unsigned j = i + 1; (j < n) && !conflict; j++)
				if (in_neighbor_list(ssa(regs[j]), instr, i))
					conflict = true;

			if (conflict) {
				insert_mov(collect, i);
				/* inserting the mov may have caused a conflict
				 * against the previous:
				 */
				goto restart;
			}
		}
	}

	/* second pass, now that we've inserted mov's, fixup left/right
	 * neighbors.  This is guaranteed to succeed, since by definition
	 * the newly inserted mov's cannot conflict with anything.
	 */
	for (unsigned i = 0; i < n; i++) {
		struct ir3_instruction *instr = ssa(regs[i]);
		if (instr) {
			struct ir3_instruction *left = (i > 0) ? ssa(regs[i - 1]) : NULL;
			struct ir3_instruction *right = (i < (n-1)) ? ssa(regs[i + 1]) : NULL;

			debug_assert(!conflicts(instr->cp.left, left));
			if (left) {
				instr->cp.left_cnt++;
				instr->cp.left = left;
			}

			debug_assert(!conflicts(instr->cp.right, right));
			if (right) {
				instr->cp.right_cnt++;
				instr->cp.right = right;
			}
		}
	}
}

static bool
instr_find_neighbors(struct ir3_instruction *instr)
{
	struct ir3_instruction *src;
	bool progress = false;

	if (ir3_instr_check_mark(instr))
		return false;

	if (instr->opc == OPC_META_COLLECT) {
		group_collect(instr);
		progress = true;
	}

	foreach_ssa_src (src, instr)
		progress |= instr_find_neighbors(src);

	return progress;
}

static bool
find_neighbors(struct ir3 *ir)
{
	bool progress = false;
	unsigned i;

	struct ir3_instruction *out;
	foreach_output (out, ir)
		progress |= instr_find_neighbors(out);

	foreach_block (block, &ir->block_list) {
		for (i = 0; i < block->keeps_count; i++) {
			struct ir3_instruction *instr = block->keeps[i];
			progress |= instr_find_neighbors(instr);
		}

		/* We also need to account for if-condition: */
		if (block->condition)
			progress |= instr_find_neighbors(block->condition);
	}

	return progress;
}

bool
ir3_group(struct ir3 *ir)
{
	ir3_clear_mark(ir);
	return find_neighbors(ir);
}