aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ssa.c
blob: 4d3ead0c4ea2ea8147e502b62aaf0854572934a0 (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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// SPDX-License-Identifier: MIT
//
// SSA conversion
// Copyright (C) 2005 Luc Van Oostenryck
//

#include <assert.h>
#include "ssa.h"
#include "lib.h"
#include "dominate.h"
#include "flowgraph.h"
#include "linearize.h"
#include "simplify.h"
#include "flow.h"


// Is it possible and desirable for this to be promoted to a pseudo?
static inline bool is_promotable(struct symbol *type)
{
	struct symbol *member;
	int bf_seen;
	int nbr;

	if (type->type == SYM_NODE)
		type = type->ctype.base_type;
	switch (type->type) {
	case SYM_ENUM:
	case SYM_BITFIELD:
	case SYM_PTR:
	case SYM_RESTRICT:	// OK, always integer types
		return 1;
	case SYM_STRUCT:
		// we allow a single scalar field
		// but a run of bitfields count for 1
		// (and packed bifields are excluded).
		if (type->packed)
			return 0;
		nbr = 0;
		bf_seen = 0;
		FOR_EACH_PTR(type->symbol_list, member) {
			if (is_bitfield_type(member)) {
				if (bf_seen)
					continue;
				bf_seen = 1;
			} else {
				bf_seen = 0;
			}
			if (!is_scalar_type(member))
				return 0;
			if (nbr++)
				return 0;
		} END_FOR_EACH_PTR(member);
		if (bf_seen && (type->bit_size > long_ctype.bit_size))
			return 0;
		return 1;
	case SYM_UNION:
		// FIXME: should be like struct but has problem
		//        when used with/for type cohercion
		// -----> OK if only same sized integral types
		FOR_EACH_PTR(type->symbol_list, member) {
			if (member->bit_size != type->bit_size)
				return 0;
			if (!is_integral_type(member))
				return 0;
		} END_FOR_EACH_PTR(member);
		return 1;
	default:
		break;
	}
	if (type->ctype.base_type == &int_type)
		return 1;
	if (type->ctype.base_type == &fp_type)
		return 1;
	return 0;
}

static void kill_store(struct instruction *insn)
{
	remove_use(&insn->src);
	remove_use(&insn->target);
	insn->bb = NULL;
}

static bool same_memop(struct instruction *a, struct instruction *b)
{
	if (a->size != b->size || a->offset != b->offset)
		return false;
	if (is_integral_type(a->type) && is_float_type(b->type))
		return false;
	if (is_float_type(a->type) && is_integral_type(b->type))
		return false;
	return true;
}

static void rewrite_local_var(struct basic_block *bb, pseudo_t addr, int nbr_stores, int nbr_uses)
{
	struct instruction *store = NULL;
	struct instruction *insn;
	bool remove = false;

	if (!bb)
		return;

	FOR_EACH_PTR(bb->insns, insn) {

		if (!insn->bb || insn->src != addr)
			continue;
		switch (insn->opcode) {
		case OP_LOAD:
			if (!store)
				replace_with_pseudo(insn, undef_pseudo());
			else if (same_memop(store, insn))
				replace_with_pseudo(insn, store->target);
			else
				remove = false;
			break;
		case OP_STORE:
			store = insn;
			remove = true;
			break;
		}
	} END_FOR_EACH_PTR(insn);
	if (remove)
		kill_store(store);
}

// we would like to know:
// is there one or more stores?
// are all loads & stores local/done in a single block?
static void ssa_convert_one_var(struct entrypoint *ep, struct symbol *var)
{
	unsigned long generation = ++bb_generation;
	struct basic_block_list *alpha = NULL;
	struct basic_block_list *idf = NULL;
	struct basic_block *samebb = NULL;
	struct basic_block *bb;
	struct pseudo_user *pu;
	unsigned long mod = var->ctype.modifiers;
	bool local = true;
	int nbr_stores = 0;
	int nbr_uses   = 0;
	pseudo_t addr;

	/* Never used as a symbol? */
	addr = var->pseudo;
	if (!addr)
		return;

	/* We don't do coverage analysis of volatiles.. */
	if (mod & MOD_VOLATILE)
		return;

	/* ..and symbols with external visibility need more care */
	mod &= (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE);
	if (mod)
		goto external_visibility;

	if (!is_promotable(var))
		return;

	// 1) insert in the worklist all BBs that may modify var
	FOR_EACH_PTR(addr->users, pu) {
		struct instruction *insn = pu->insn;
		struct basic_block *bb = insn->bb;

		switch (insn->opcode) {
		case OP_STORE:
			nbr_stores++;
			if (bb->generation != generation) {
				bb->generation = generation;
				add_bb(&alpha, bb);
			}
			/* fall through */
		case OP_LOAD:
			if (local) {
				if (!samebb)
					samebb = bb;
				else if (samebb != bb)
					local = false;
			}
			nbr_uses++;
			break;
		case OP_SYMADDR:
			mod |= MOD_ADDRESSABLE;
			goto external_visibility;
		default:
			warning(var->pos, "symbol '%s' pseudo used in unexpected way",
				show_ident(var->ident));
		}
	} END_FOR_EACH_PTR(pu);

	// if all uses are local to a single block
	// they can easily be rewritten and doesn't need phi-nodes
	// FIXME: could be done for extended BB too
	if (local) {
		rewrite_local_var(samebb, addr, nbr_stores, nbr_uses);
		return;
	}

	idf_compute(ep, &idf, alpha);
	FOR_EACH_PTR(idf, bb) {
		struct instruction *node = insert_phi_node(bb, var);
		node->phi_var = var->pseudo;
	} END_FOR_EACH_PTR(bb);
	var->torename = 1;

external_visibility:
	if (mod & (MOD_NONLOCAL | MOD_STATIC))
		return;
	kill_dead_stores(ep, addr, !mod);
}

static struct instruction *lookup_var(struct basic_block *bb, struct symbol *var)
{
	do {
		struct instruction *insn = phi_map_lookup(bb->phi_map, var);
		if (insn)
			return insn;
	} while ((bb = bb->idom));
	return NULL;
}

static struct instruction_list *phis_all;
static struct instruction_list *phis_used;
static struct instruction_list *stores;

static bool matching_load(struct instruction *def, struct instruction *insn)
{
	if (insn->size != def->size)
		return false;
	switch (def->opcode) {
	case OP_STORE:
	case OP_LOAD:
		if (insn->offset != def->offset)
			return false;
	case OP_PHI:
		break;
	default:
		return false;
	}
	return true;
}

static void ssa_rename_insn(struct basic_block *bb, struct instruction *insn)
{
	struct instruction *def;
	struct symbol *var;
	pseudo_t addr;
	pseudo_t val;

	switch (insn->opcode) {
	case OP_STORE:
		addr = insn->src;
		if (addr->type != PSEUDO_SYM)
			break;
		var = addr->sym;
		if (!var || !var->torename)
			break;
		phi_map_update(&bb->phi_map, var, insn);
		add_instruction(&stores, insn);
		break;
	case OP_LOAD:
		addr = insn->src;
		if (addr->type != PSEUDO_SYM)
			break;
		var = addr->sym;
		if (!var || !var->torename)
			break;
		def = lookup_var(bb, var);
		if (!def) {
			val = undef_pseudo();
		} else if (!matching_load(def, insn)) {
			var->torename = false;
			break;
		} else {
			val = def->target;
		}
		replace_with_pseudo(insn, val);
		break;
	case OP_PHI:
		var = insn->type;
		if (!var || !var->torename)
			break;
		phi_map_update(&bb->phi_map, var, insn);
		add_instruction(&phis_all, insn);
		break;
	}
}

static void ssa_rename_insns(struct entrypoint *ep)
{
	struct basic_block *bb;

	FOR_EACH_PTR(ep->bbs, bb) {
		struct instruction *insn;
		FOR_EACH_PTR(bb->insns, insn) {
			if (!insn->bb)
				continue;
			ssa_rename_insn(bb, insn);
		} END_FOR_EACH_PTR(insn);
	} END_FOR_EACH_PTR(bb);
}

static void mark_phi_used(pseudo_t val)
{
	struct instruction *node;

	if (val->type != PSEUDO_REG)
		return;
	node = val->def;
	if (node->opcode != OP_PHI)
		return;
	if (node->used)
		return;
	node->used = 1;
	add_instruction(&phis_used, node);
}

static void ssa_rename_phi(struct instruction *insn)
{
	struct basic_block *par;
	struct symbol *var;

	if (!insn->phi_var)
		return;
	var = insn->phi_var->sym;
	if (!var->torename)
		return;
	FOR_EACH_PTR(insn->bb->parents, par) {
		struct instruction *def = lookup_var(par, var);
		pseudo_t val = def ? def->target : undef_pseudo();
		struct instruction *phisrc = alloc_phisrc(val, var);
		pseudo_t phi = phisrc->target;
		phi->ident = var->ident;
		insert_last_instruction(par, phisrc);
		link_phi(insn, phi);
		mark_phi_used(val);
	} END_FOR_EACH_PTR(par);
}

static void ssa_rename_phis(struct entrypoint *ep)
{
	struct instruction *phi;

	phis_used = NULL;
	FOR_EACH_PTR(phis_all, phi) {
		if (has_users(phi->target)) {
			phi->used = 1;
			add_instruction(&phis_used, phi);
		}
	} END_FOR_EACH_PTR(phi);

	FOR_EACH_PTR(phis_used, phi) {
		if (!phi->bb)
			continue;
		ssa_rename_phi(phi);
	} END_FOR_EACH_PTR(phi);
}

static void remove_dead_stores(struct instruction_list *stores)
{
	struct instruction *store;

	FOR_EACH_PTR(stores, store) {
		struct symbol *var = store->addr->sym;

		if (var->torename)
			kill_store(store);
	} END_FOR_EACH_PTR(store);
}

void ssa_convert(struct entrypoint *ep)
{
	struct basic_block *bb;
	pseudo_t pseudo;
	int first, last;

	// calculate the number of BBs
	first = ep->entry->bb->nr;
	last = first;
	FOR_EACH_PTR(ep->bbs, bb) {
		int nr = bb->nr;
		if (nr > last)
			last = nr;
		bb->phi_map = NULL;
	} END_FOR_EACH_PTR(bb);

	// try to promote memory accesses to pseudos
	stores = NULL;
	FOR_EACH_PTR(ep->accesses, pseudo) {
		ssa_convert_one_var(ep, pseudo->sym);
	} END_FOR_EACH_PTR(pseudo);

	// rename the converted accesses
	phis_all = phis_used = NULL;
	ssa_rename_insns(ep);
	ssa_rename_phis(ep);

	// remove now dead stores
	remove_dead_stores(stores);
}