aboutsummaryrefslogtreecommitdiffstats
path: root/liveness.c
blob: 8b0dfd8e3504e469cca0cdd3fac5e71ae13fc0c0 (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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
 * Register - track pseudo usage, maybe eventually try to do register
 * allocation.
 *
 * Copyright (C) 2004 Linus Torvalds
 */

#include <assert.h>

#include "parse.h"
#include "expression.h"
#include "linearize.h"
#include "flow.h"

static void phi_defines(struct instruction * phi_node, pseudo_t target,
	void (*defines)(struct basic_block *, struct instruction *, pseudo_t))
{
	pseudo_t phi;
	FOR_EACH_PTR(phi_node->phi_list, phi) {
		struct instruction *def;
		if (phi == VOID)
			continue;
		def = phi->def;
		if (!def || !def->bb)
			continue;
		if (def->opcode == OP_PHI) {
			phi_defines(def, target, defines);
			continue;
		}
		defines(def->bb, phi->def, target);
	} END_FOR_EACH_PTR(phi);
}

static void asm_liveness(struct basic_block *bb, struct instruction *insn,
	void (*def)(struct basic_block *, struct instruction *, pseudo_t),
	void (*use)(struct basic_block *, struct instruction *, pseudo_t))
{
	struct asm_constraint *entry;

	FOR_EACH_PTR(insn->asm_rules->inputs, entry) {
		use(bb, insn, entry->pseudo);
	} END_FOR_EACH_PTR(entry);
		
	FOR_EACH_PTR(insn->asm_rules->outputs, entry) {
		def(bb, insn, entry->pseudo);
	} END_FOR_EACH_PTR(entry);
}

static void track_instruction_usage(struct basic_block *bb, struct instruction *insn,
	void (*def)(struct basic_block *, struct instruction *, pseudo_t),
	void (*use)(struct basic_block *, struct instruction *, pseudo_t))
{
	pseudo_t pseudo;

	#define USES(x)		use(bb, insn, insn->x)
	#define DEFINES(x)	def(bb, insn, insn->x)

	switch (insn->opcode) {
	case OP_RET:
		USES(src);
		break;

	case OP_BR: case OP_SWITCH:
		USES(cond);
		break;

	case OP_COMPUTEDGOTO:
		USES(target);
		break;
	
	/* Binary */
	case OP_BINARY ... OP_BINARY_END:
	case OP_BINCMP ... OP_BINCMP_END:
		USES(src1); USES(src2); DEFINES(target);
		break;

	/* Uni */
	case OP_NOT: case OP_NEG:
		USES(src1); DEFINES(target);
		break;

	case OP_SEL:
		USES(src1); USES(src2); USES(src3); DEFINES(target);
		break;
	
	/* Memory */
	case OP_LOAD:
		USES(src); DEFINES(target);
		break;

	case OP_STORE:
		USES(src); USES(target);
		break;

	case OP_SETVAL:
		DEFINES(target);
		break;

	case OP_SYMADDR:
		USES(symbol); DEFINES(target);
		break;

	/* Other */
	case OP_PHI:
		/* Phi-nodes are "backwards" nodes. Their def doesn't matter */
		phi_defines(insn, insn->target, def);
		break;

	case OP_PHISOURCE:
		/*
		 * We don't care about the phi-source define, they get set
		 * up and expanded by the OP_PHI
		 */
		USES(phi_src);
		break;

	case OP_CAST:
	case OP_SCAST:
	case OP_FPCAST:
	case OP_PTRCAST:
		USES(src); DEFINES(target);
		break;

	case OP_CALL:
		USES(func);
		if (insn->target != VOID)
			DEFINES(target);
		FOR_EACH_PTR(insn->arguments, pseudo) {
			use(bb, insn, pseudo);
		} END_FOR_EACH_PTR(pseudo);
		break;

	case OP_SLICE:
		USES(base); DEFINES(target);
		break;

	case OP_ASM:
		asm_liveness(bb, insn, def, use);
		break;

	case OP_RANGE:
		USES(src1); USES(src2); USES(src3);
		break;

	case OP_BADOP:
	case OP_INVOKE:
	case OP_UNWIND:
	case OP_MALLOC:
	case OP_FREE:
	case OP_ALLOCA:
	case OP_GET_ELEMENT_PTR:
	case OP_VANEXT:
	case OP_VAARG:
	case OP_SNOP:
	case OP_LNOP:
	case OP_NOP:
	case OP_CONTEXT:
		break;
	}
}

int pseudo_in_list(struct pseudo_list *list, pseudo_t pseudo)
{
	pseudo_t old;
	FOR_EACH_PTR(list,old) {
		if (old == pseudo)
			return 1;
	} END_FOR_EACH_PTR(old);   
	return 0;
}

static int liveness_changed;

static void add_pseudo_exclusive(struct pseudo_list **list, pseudo_t pseudo)
{
	if (!pseudo_in_list(*list, pseudo)) {
		liveness_changed = 1;
		add_pseudo(list, pseudo);
	}
}

static inline int trackable_pseudo(pseudo_t pseudo)
{
	return pseudo && (pseudo->type == PSEUDO_REG || pseudo->type == PSEUDO_ARG);
}

static void insn_uses(struct basic_block *bb, struct instruction *insn, pseudo_t pseudo)
{
	if (trackable_pseudo(pseudo)) {
		struct instruction *def = pseudo->def;
		if (pseudo->type != PSEUDO_REG || def->bb != bb || def->opcode == OP_PHI)
			add_pseudo_exclusive(&bb->needs, pseudo);
	}
}

static void insn_defines(struct basic_block *bb, struct instruction *insn, pseudo_t pseudo)
{
	assert(trackable_pseudo(pseudo));
	add_pseudo(&bb->defines, pseudo);
}

static void track_bb_liveness(struct basic_block *bb)
{
	pseudo_t needs;

	FOR_EACH_PTR(bb->needs, needs) {
		struct basic_block *parent;
		FOR_EACH_PTR(bb->parents, parent) {
			if (!pseudo_in_list(parent->defines, needs)) {
				add_pseudo_exclusive(&parent->needs, needs);
			}
		} END_FOR_EACH_PTR(parent);
	} END_FOR_EACH_PTR(needs);
}

/*
 * We need to clear the liveness information if we 
 * are going to re-run it.
 */
void clear_liveness(struct entrypoint *ep)
{
	struct basic_block *bb;

	FOR_EACH_PTR(ep->bbs, bb) {
		free_ptr_list(&bb->needs);
		free_ptr_list(&bb->defines);
	} END_FOR_EACH_PTR(bb);
}

/*
 * Track inter-bb pseudo liveness. The intra-bb case
 * is purely local information.
 */
void track_pseudo_liveness(struct entrypoint *ep)
{
	struct basic_block *bb;

	/* Add all the bb pseudo usage */
	FOR_EACH_PTR(ep->bbs, bb) {
		struct instruction *insn;
		FOR_EACH_PTR(bb->insns, insn) {
			if (!insn->bb)
				continue;
			assert(insn->bb == bb);
			track_instruction_usage(bb, insn, insn_defines, insn_uses);
		} END_FOR_EACH_PTR(insn);
	} END_FOR_EACH_PTR(bb);

	/* Calculate liveness.. */
	do {
		liveness_changed = 0;
		FOR_EACH_PTR_REVERSE(ep->bbs, bb) {
			track_bb_liveness(bb);
		} END_FOR_EACH_PTR_REVERSE(bb);
	} while (liveness_changed);

	/* Remove the pseudos from the "defines" list that are used internally */
	FOR_EACH_PTR(ep->bbs, bb) {
		pseudo_t def;
		FOR_EACH_PTR(bb->defines, def) {
			struct basic_block *child;
			FOR_EACH_PTR(bb->children, child) {
				if (pseudo_in_list(child->needs, def))
					goto is_used;
			} END_FOR_EACH_PTR(child);
			DELETE_CURRENT_PTR(def);
is_used:
		;
		} END_FOR_EACH_PTR(def);
		PACK_PTR_LIST(&bb->defines);
	} END_FOR_EACH_PTR(bb);
}

static void merge_pseudo_list(struct pseudo_list *src, struct pseudo_list **dest)
{
	pseudo_t pseudo;
	FOR_EACH_PTR(src, pseudo) {
		add_pseudo_exclusive(dest, pseudo);
	} END_FOR_EACH_PTR(pseudo);
}

static void track_phi_uses(struct instruction *insn)
{
	pseudo_t phi;
	FOR_EACH_PTR(insn->phi_list, phi) {
		struct instruction *def;
		if (phi == VOID || !phi->def)
			continue;
		def = phi->def;
		assert(def->opcode == OP_PHISOURCE);
		add_ptr_list(&def->phi_users, insn);
	} END_FOR_EACH_PTR(phi);
}

static void track_bb_phi_uses(struct basic_block *bb)
{
	struct instruction *insn;
	FOR_EACH_PTR(bb->insns, insn) {
		if (insn->bb && insn->opcode == OP_PHI)
			track_phi_uses(insn);
	} END_FOR_EACH_PTR(insn);
}

static struct pseudo_list **live_list;
static struct pseudo_list *dead_list;

static void death_def(struct basic_block *bb, struct instruction *insn, pseudo_t pseudo)
{
}

static void death_use(struct basic_block *bb, struct instruction *insn, pseudo_t pseudo)
{
	if (trackable_pseudo(pseudo) && !pseudo_in_list(*live_list, pseudo)) {
		add_pseudo(&dead_list, pseudo);
		add_pseudo(live_list, pseudo);
	}
}

static void track_pseudo_death_bb(struct basic_block *bb)
{
	struct pseudo_list *live = NULL;
	struct basic_block *child;
	struct instruction *insn;

	FOR_EACH_PTR(bb->children, child) {
		merge_pseudo_list(child->needs, &live);
	} END_FOR_EACH_PTR(child);

	live_list = &live;
	FOR_EACH_PTR_REVERSE(bb->insns, insn) {
		if (!insn->bb)
			continue;

		dead_list = NULL;
		track_instruction_usage(bb, insn, death_def, death_use);
		if (dead_list) {
			pseudo_t dead;
			FOR_EACH_PTR(dead_list, dead) {
				struct instruction *deathnote = __alloc_instruction(0);
				deathnote->bb = bb;
				deathnote->opcode = OP_DEATHNOTE;
				deathnote->target = dead;
				INSERT_CURRENT(deathnote, insn);
			} END_FOR_EACH_PTR(dead);
			free_ptr_list(&dead_list);
		}
	} END_FOR_EACH_PTR_REVERSE(insn);
	free_ptr_list(&live);
}

void track_pseudo_death(struct entrypoint *ep)
{
	struct basic_block *bb;

	FOR_EACH_PTR(ep->bbs, bb) {
		track_bb_phi_uses(bb);
	} END_FOR_EACH_PTR(bb);

	FOR_EACH_PTR(ep->bbs, bb) {
		track_pseudo_death_bb(bb);
	} END_FOR_EACH_PTR(bb);
}