aboutsummaryrefslogtreecommitdiff
path: root/src/unreachable.c
blob: 90a65bd4d93f329e46080607c85f79e1c747526b (plain) (blame)
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
#include <qbt/unreachable.h>

void build_reachable_vec(struct vec *reachable, struct blk *cur)
{
	if (cur->reachable)
		return;

	cur->reachable = true;
	vec_append(reachable, &cur);

	if (cur->btype == RET)
		return;

	if (cur->btype == J)
		cur->s1 = cur->s2;

	if (cur->s1)
		build_reachable_vec(reachable, cur->s1);

	if (cur->s2)
		build_reachable_vec(reachable, cur->s2);
}

void unreachable(struct fn *f)
{
	struct vec reachable = vec_create(sizeof(struct blk *));
	struct blk *b = blk_at(f->blks, 0);
	build_reachable_vec(&reachable, b);
	foreach_blk(bi, f->blks) {
		struct blk *b = blk_at(f->blks, bi);
		if (!b->reachable)
			destroy_block(b);

	}

	vec_destroy(&f->blks);
	f->blks = reachable;
}