summaryrefslogtreecommitdiff
path: root/src/freedreno/ir3/ir3_array_to_ssa.c
blob: d515f0a8cd54433548c0bb5d71cf2acb292b6000 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 * Copyright (C) 2021 Valve Corporation
 *
 * 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.
 */

/* This pass lowers array accesses to SSA.
 *
 * After this pass, instructions writing arrays implicitly read the contents of
 * the array defined in instr->regs[0]->def (possibly a phi node), perform the
 * operation, and store to instr->regs[0].
 *
 * This makes arrays appear like "normal" SSA values, even if the false
 * dependencies mean that they always stay in CSSA form (i.e. able to removed
 * out-of-SSA with no copies.) While hopefully they shouldn't induce copies in
 * most cases, we can't make that guarantee while also splitting spilling from
 * RA and guaranteeing a certain number of registers are used, so we have to
 * insert the phi nodes to be able to know when copying should happen.
 *
 * The implementation is based on the idea in "Simple and Efficient Construction
 * of Static Single Assignment Form" of scanning backwards to find the
 * definition. However, since we're not doing this on-the-fly we can simplify
 * things a little by doing a pre-pass to get the last definition of each array
 * in each block. Then we optimize trivial phis in a separate pass, "on the fly"
 * so that we don't have to rewrite (and keep track of) users.
 */

#include "ir3.h"
#include <stdlib.h>

struct array_state {
	struct ir3_register *live_in_definition;
	struct ir3_register *live_out_definition;
	bool constructed;
	bool optimized;
};

struct array_ctx {
	struct array_state *states;
	struct ir3 *ir;
	unsigned array_count;
};

static struct array_state *
get_state(struct array_ctx *ctx, struct ir3_block *block, unsigned id)
{
	return &ctx->states[ctx->array_count * block->index + id];
}

static struct ir3_register *
read_value_beginning(struct array_ctx *ctx, struct ir3_block *block, struct ir3_array *arr);

static struct ir3_register *
read_value_end(struct array_ctx *ctx, struct ir3_block *block, struct ir3_array *arr)
{
	struct array_state *state = get_state(ctx, block, arr->id);
	if (state->live_out_definition)
		return state->live_out_definition;

	state->live_out_definition = read_value_beginning(ctx, block, arr);
	return state->live_out_definition;
}

/* Roughly equivalent to readValueRecursive from the paper: */
static struct ir3_register *
read_value_beginning(struct array_ctx *ctx, struct ir3_block *block, struct ir3_array *arr)
{
	struct array_state *state = get_state(ctx, block, arr->id);

	if (state->constructed)
		return state->live_in_definition;

	if (block->predecessors_count == 0) {
		state->constructed = true;
		return NULL;
	}

	if (block->predecessors_count == 1) {
		state->live_in_definition = read_value_end(ctx, block->predecessors[0], arr);
		state->constructed = true;
		return state->live_in_definition;
	}

	unsigned flags = IR3_REG_ARRAY | (arr->half ? IR3_REG_HALF : 0);
	struct ir3_instruction *phi =
		ir3_instr_create(block, OPC_META_PHI, block->predecessors_count + 1);
	list_del(&phi->node);
	list_add(&phi->node, &block->instr_list);

	struct ir3_register *dst = __ssa_dst(phi);
	dst->flags |= flags;
	dst->array.id = arr->id;
	dst->size = arr->length;

	state->live_in_definition = phi->regs[0];
	state->constructed = true;

	for (unsigned i = 0; i < block->predecessors_count; i++) {
		struct ir3_register *src = read_value_end(ctx, block->predecessors[i], arr);
		struct ir3_register *src_reg;
		if (src) {
			src_reg = __ssa_src(phi, src->instr, flags);
		} else {
			src_reg = ir3_src_create(phi, INVALID_REG, flags | IR3_REG_SSA);
		}
		src_reg->array.id = arr->id;
		src_reg->size = arr->length;
	}
	return phi->regs[0];
}

static struct ir3_register *
remove_trivial_phi(struct ir3_instruction *phi)
{
	/* Break cycles */
	if (phi->data)
		return phi->data;
	
	phi->data = phi->regs[0];

	struct ir3_register *unique_def = NULL;
	bool unique = true;
	for (unsigned i = 0; i < phi->block->predecessors_count; i++) {
		struct ir3_register *src = phi->regs[i + 1];

		/* If there are any undef sources, then the remaining sources may not
		 * dominate the phi node, even if they are all equal. So we need to
		 * bail out in this case.
		 *
		 * This seems to be a bug in the original paper.
		 */
		if (!src->def) {
			unique = false;
			break;
		}

		struct ir3_instruction *src_instr = src->def->instr;
		
		/* phi sources which point to the phi itself don't count for
		 * figuring out if the phi is trivial
		 */
		if (src_instr == phi)
			continue;

		if (src_instr->opc == OPC_META_PHI) {
			src->def = remove_trivial_phi(src->def->instr);
		}

		if (unique_def) {
			if (unique_def != src->def) {
				unique = false;
				break;
			}
		} else {
			unique_def = src->def;
		}
	}

	if (unique) {
		phi->data = unique_def;
		return unique_def;
	} else {
		return phi->regs[0];
	}
}

static struct ir3_register *
lookup_value(struct ir3_register *reg)
{
	if (reg->instr->opc == OPC_META_PHI)
		return reg->instr->data;
	return reg;
}

static struct ir3_register *
lookup_live_in(struct array_ctx *ctx, struct ir3_block *block, unsigned id)
{
	struct array_state *state = get_state(ctx, block, id);
	if (state->live_in_definition)
		return lookup_value(state->live_in_definition);

	return NULL;
}

bool
ir3_array_to_ssa(struct ir3 *ir)
{
	struct array_ctx ctx = {};

	foreach_array (array, &ir->array_list) {
		ctx.array_count = MAX2(ctx.array_count, array->id + 1);
	}

	if (ctx.array_count == 0)
		return false;

	unsigned i = 0;
	foreach_block (block, &ir->block_list) {
		block->index = i++;
	}

	ctx.ir = ir;
	ctx.states = calloc(ctx.array_count * i, sizeof(struct array_state));

	foreach_block (block, &ir->block_list) {
		foreach_instr (instr, &block->instr_list) {
			for (unsigned i = 0; i < instr->regs_count; i++) {
				if ((instr->regs[i]->flags & IR3_REG_ARRAY) &&
					(instr->regs[i]->flags & IR3_REG_DEST)) {
					struct array_state *state =
						get_state(&ctx, block, instr->regs[i]->array.id);
					state->live_out_definition = instr->regs[i];
				}
			}
		}
	}

	foreach_block (block, &ir->block_list) {
		foreach_instr (instr, &block->instr_list) {
			if (instr->opc == OPC_META_PHI)
				continue;

			for (unsigned i = 0; i < instr->regs_count; i++) {
				struct ir3_register *reg = instr->regs[i];
				if ((reg->flags & IR3_REG_ARRAY) &&
					(((reg->flags & IR3_REG_DEST) && !reg->tied) ||
					 (!(reg->flags & IR3_REG_DEST) && !reg->def))) {
					struct ir3_array *arr = ir3_lookup_array(ir, reg->array.id);

					/* Construct any phi nodes necessary to read this value */
					read_value_beginning(&ctx, block, arr);
				}
			}
		}
	}

	foreach_block (block, &ir->block_list) {
		foreach_instr_safe (instr, &block->instr_list) {
			if (instr->opc == OPC_META_PHI)
				remove_trivial_phi(instr);
			else
				break;
		}
	}

	foreach_block (block, &ir->block_list) {
		foreach_instr_safe (instr, &block->instr_list) {
			if (instr->opc == OPC_META_PHI) {
				if (!(instr->flags & IR3_REG_ARRAY))
					continue;
				if (instr->data != instr->regs[0]) {
					list_del(&instr->node);
					continue;
				}
				for (unsigned i = 1; i < instr->regs_count; i++) {
					instr->regs[i] = lookup_value(instr->regs[i]);
				}
			} else {
				for (unsigned i = 0; i < instr->regs_count; i++) {
					struct ir3_register *reg = instr->regs[i];
					if ((reg->flags & IR3_REG_ARRAY)) {
						/* It is assumed that before calling
						 * ir3_array_to_ssa(), reg->def was set to the
						 * previous writer of the array within the current
						 * block or NULL if none.
						 */
						if (!(reg->flags & IR3_REG_DEST) && !reg->def) {
							reg->def = lookup_live_in(&ctx, block, reg->array.id);
						} else if ((reg->flags & IR3_REG_DEST) && !reg->tied) {
							struct ir3_register *def =
								lookup_live_in(&ctx, block, reg->array.id);
							if (def)
								ir3_reg_set_last_array(instr, reg, def);
						}
						reg->flags |= IR3_REG_SSA;
					}
				}
			}
		}
	}

	free(ctx.states);
	return true;
}