rename Tmp.ins to be more descriptive
[qbe.git] / cfg.c
blobd12f6c801c1711d1db0f1bcbeeab1a6284c3c3ee
1 #include "all.h"
3 Blk *
4 blknew()
6 static Blk z;
7 Blk *b;
9 b = alloc(sizeof *b);
10 *b = z;
11 return b;
14 void
15 edgedel(Blk *bs, Blk **pbd)
17 Blk *bd;
18 Phi *p;
19 uint a;
20 int mult;
22 bd = *pbd;
23 mult = 1 + (bs->s1 == bs->s2);
24 *pbd = 0;
25 if (!bd || mult > 1)
26 return;
27 for (p=bd->phi; p; p=p->link) {
28 for (a=0; p->blk[a]!=bs; a++)
29 assert(a+1<p->narg);
30 p->narg--;
31 memmove(&p->blk[a], &p->blk[a+1],
32 sizeof p->blk[0] * (p->narg-a));
33 memmove(&p->arg[a], &p->arg[a+1],
34 sizeof p->arg[0] * (p->narg-a));
36 if (bd->npred != 0) {
37 for (a=0; bd->pred[a]!=bs; a++)
38 assert(a+1<bd->npred);
39 bd->npred--;
40 memmove(&bd->pred[a], &bd->pred[a+1],
41 sizeof bd->pred[0] * (bd->npred-a));
45 static void
46 addpred(Blk *bp, Blk *bc)
48 if (!bc->pred) {
49 bc->pred = alloc(bc->npred * sizeof bc->pred[0]);
50 bc->visit = 0;
52 bc->pred[bc->visit++] = bp;
55 /* fill predecessors information in blocks */
56 void
57 fillpreds(Fn *f)
59 Blk *b;
61 for (b=f->start; b; b=b->link) {
62 b->npred = 0;
63 b->pred = 0;
65 for (b=f->start; b; b=b->link) {
66 if (b->s1)
67 b->s1->npred++;
68 if (b->s2 && b->s2 != b->s1)
69 b->s2->npred++;
71 for (b=f->start; b; b=b->link) {
72 if (b->s1)
73 addpred(b, b->s1);
74 if (b->s2 && b->s2 != b->s1)
75 addpred(b, b->s2);
79 static int
80 rporec(Blk *b, uint x)
82 Blk *s1, *s2;
84 if (!b || b->id != -1u)
85 return x;
86 b->id = 1;
87 s1 = b->s1;
88 s2 = b->s2;
89 if (s1 && s2 && s1->loop > s2->loop) {
90 s1 = b->s2;
91 s2 = b->s1;
93 x = rporec(s1, x);
94 x = rporec(s2, x);
95 b->id = x;
96 assert(x != -1u);
97 return x - 1;
100 /* fill the rpo information */
101 void
102 fillrpo(Fn *f)
104 uint n;
105 Blk *b, **p;
107 for (b=f->start; b; b=b->link)
108 b->id = -1u;
109 n = 1 + rporec(f->start, f->nblk-1);
110 f->nblk -= n;
111 f->rpo = alloc(f->nblk * sizeof f->rpo[0]);
112 for (p=&f->start; (b=*p);) {
113 if (b->id == -1u) {
114 edgedel(b, &b->s1);
115 edgedel(b, &b->s2);
116 *p = b->link;
117 } else {
118 b->id -= n;
119 f->rpo[b->id] = b;
120 p = &b->link;
125 /* for dominators computation, read
126 * "A Simple, Fast Dominance Algorithm"
127 * by K. Cooper, T. Harvey, and K. Kennedy.
130 static Blk *
131 inter(Blk *b1, Blk *b2)
133 Blk *bt;
135 if (b1 == 0)
136 return b2;
137 while (b1 != b2) {
138 if (b1->id < b2->id) {
139 bt = b1;
140 b1 = b2;
141 b2 = bt;
143 while (b1->id > b2->id) {
144 b1 = b1->idom;
145 assert(b1);
148 return b1;
151 void
152 filldom(Fn *fn)
154 Blk *b, *d;
155 int ch;
156 uint n, p;
158 for (b=fn->start; b; b=b->link) {
159 b->idom = 0;
160 b->dom = 0;
161 b->dlink = 0;
163 do {
164 ch = 0;
165 for (n=1; n<fn->nblk; n++) {
166 b = fn->rpo[n];
167 d = 0;
168 for (p=0; p<b->npred; p++)
169 if (b->pred[p]->idom
170 || b->pred[p] == fn->start)
171 d = inter(d, b->pred[p]);
172 if (d != b->idom) {
173 ch++;
174 b->idom = d;
177 } while (ch);
178 for (b=fn->start; b; b=b->link)
179 if ((d=b->idom)) {
180 assert(d != b);
181 b->dlink = d->dom;
182 d->dom = b;
187 sdom(Blk *b1, Blk *b2)
189 assert(b1 && b2);
190 if (b1 == b2)
191 return 0;
192 while (b2->id > b1->id)
193 b2 = b2->idom;
194 return b1 == b2;
198 dom(Blk *b1, Blk *b2)
200 return b1 == b2 || sdom(b1, b2);
203 static void
204 addfron(Blk *a, Blk *b)
206 uint n;
208 for (n=0; n<a->nfron; n++)
209 if (a->fron[n] == b)
210 return;
211 if (!a->nfron)
212 a->fron = vnew(++a->nfron, sizeof a->fron[0], PFn);
213 else
214 vgrow(&a->fron, ++a->nfron);
215 a->fron[a->nfron-1] = b;
218 /* fill the dominance frontier */
219 void
220 fillfron(Fn *fn)
222 Blk *a, *b;
224 for (b=fn->start; b; b=b->link)
225 b->nfron = 0;
226 for (b=fn->start; b; b=b->link) {
227 if (b->s1)
228 for (a=b; !sdom(a, b->s1); a=a->idom)
229 addfron(a, b->s1);
230 if (b->s2)
231 for (a=b; !sdom(a, b->s2); a=a->idom)
232 addfron(a, b->s2);
236 static void
237 loopmark(Blk *hd, Blk *b, void f(Blk *, Blk *))
239 uint p;
241 if (b->id < hd->id || b->visit == hd->id)
242 return;
243 b->visit = hd->id;
244 f(hd, b);
245 for (p=0; p<b->npred; ++p)
246 loopmark(hd, b->pred[p], f);
249 void
250 loopiter(Fn *fn, void f(Blk *, Blk *))
252 uint n, p;
253 Blk *b;
255 for (b=fn->start; b; b=b->link)
256 b->visit = -1u;
257 for (n=0; n<fn->nblk; ++n) {
258 b = fn->rpo[n];
259 for (p=0; p<b->npred; ++p)
260 if (b->pred[p]->id >= n)
261 loopmark(b, b->pred[p], f);
265 void
266 multloop(Blk *hd, Blk *b)
268 (void)hd;
269 b->loop *= 10;
272 void
273 fillloop(Fn *fn)
275 Blk *b;
277 for (b=fn->start; b; b=b->link)
278 b->loop = 1;
279 loopiter(fn, multloop);
282 static void
283 uffind(Blk **pb, Blk **uf)
285 Blk **pb1;
287 pb1 = &uf[(*pb)->id];
288 if (*pb1) {
289 uffind(pb1, uf);
290 *pb = *pb1;
294 /* requires rpo and no phis, breaks cfg */
295 void
296 simpljmp(Fn *fn)
299 Blk **uf; /* union-find */
300 Blk **p, *b, *ret;
302 ret = blknew();
303 ret->id = fn->nblk++;
304 ret->jmp.type = Jret0;
305 uf = emalloc(fn->nblk * sizeof uf[0]);
306 for (b=fn->start; b; b=b->link) {
307 assert(!b->phi);
308 if (b->jmp.type == Jret0) {
309 b->jmp.type = Jjmp;
310 b->s1 = ret;
312 if (b->nins == 0)
313 if (b->jmp.type == Jjmp) {
314 uffind(&b->s1, uf);
315 if (b->s1 != b)
316 uf[b->id] = b->s1;
319 for (p=&fn->start; (b=*p); p=&b->link) {
320 if (b->s1)
321 uffind(&b->s1, uf);
322 if (b->s2)
323 uffind(&b->s2, uf);
324 if (b->s1 && b->s1 == b->s2) {
325 b->jmp.type = Jjmp;
326 b->s2 = 0;
329 *p = ret;
330 free(uf);