15 edgedel(Blk
*bs
, Blk
**pbd
)
23 mult
= 1 + (bs
->s1
== bs
->s2
);
27 for (p
=bd
->phi
; p
; p
=p
->link
) {
28 for (a
=0; p
->blk
[a
]!=bs
; a
++)
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
));
37 for (a
=0; bd
->pred
[a
]!=bs
; a
++)
38 assert(a
+1<bd
->npred
);
40 memmove(&bd
->pred
[a
], &bd
->pred
[a
+1],
41 sizeof bd
->pred
[0] * (bd
->npred
-a
));
46 addpred(Blk
*bp
, Blk
*bc
)
49 bc
->pred
= alloc(bc
->npred
* sizeof bc
->pred
[0]);
52 bc
->pred
[bc
->visit
++] = bp
;
55 /* fill predecessors information in blocks */
61 for (b
=f
->start
; b
; b
=b
->link
) {
65 for (b
=f
->start
; b
; b
=b
->link
) {
68 if (b
->s2
&& b
->s2
!= b
->s1
)
71 for (b
=f
->start
; b
; b
=b
->link
) {
74 if (b
->s2
&& b
->s2
!= b
->s1
)
80 rporec(Blk
*b
, uint x
)
84 if (!b
|| b
->id
!= -1u)
89 if (s1
&& s2
&& s1
->loop
> s2
->loop
) {
100 /* fill the rpo information */
107 for (b
=f
->start
; b
; b
=b
->link
)
109 n
= 1 + rporec(f
->start
, f
->nblk
-1);
111 f
->rpo
= alloc(f
->nblk
* sizeof f
->rpo
[0]);
112 for (p
=&f
->start
; (b
=*p
);) {
125 /* for dominators computation, read
126 * "A Simple, Fast Dominance Algorithm"
127 * by K. Cooper, T. Harvey, and K. Kennedy.
131 inter(Blk
*b1
, Blk
*b2
)
138 if (b1
->id
< b2
->id
) {
143 while (b1
->id
> b2
->id
) {
158 for (b
=fn
->start
; b
; b
=b
->link
) {
165 for (n
=1; n
<fn
->nblk
; n
++) {
168 for (p
=0; p
<b
->npred
; p
++)
170 || b
->pred
[p
] == fn
->start
)
171 d
= inter(d
, b
->pred
[p
]);
178 for (b
=fn
->start
; b
; b
=b
->link
)
187 sdom(Blk
*b1
, Blk
*b2
)
192 while (b2
->id
> b1
->id
)
198 dom(Blk
*b1
, Blk
*b2
)
200 return b1
== b2
|| sdom(b1
, b2
);
204 addfron(Blk
*a
, Blk
*b
)
208 for (n
=0; n
<a
->nfron
; n
++)
212 a
->fron
= vnew(++a
->nfron
, sizeof a
->fron
[0], PFn
);
214 vgrow(&a
->fron
, ++a
->nfron
);
215 a
->fron
[a
->nfron
-1] = b
;
218 /* fill the dominance frontier */
224 for (b
=fn
->start
; b
; b
=b
->link
)
226 for (b
=fn
->start
; b
; b
=b
->link
) {
228 for (a
=b
; !sdom(a
, b
->s1
); a
=a
->idom
)
231 for (a
=b
; !sdom(a
, b
->s2
); a
=a
->idom
)
237 loopmark(Blk
*hd
, Blk
*b
, void f(Blk
*, Blk
*))
241 if (b
->id
< hd
->id
|| b
->visit
== hd
->id
)
245 for (p
=0; p
<b
->npred
; ++p
)
246 loopmark(hd
, b
->pred
[p
], f
);
250 loopiter(Fn
*fn
, void f(Blk
*, Blk
*))
255 for (b
=fn
->start
; b
; b
=b
->link
)
257 for (n
=0; n
<fn
->nblk
; ++n
) {
259 for (p
=0; p
<b
->npred
; ++p
)
260 if (b
->pred
[p
]->id
>= n
)
261 loopmark(b
, b
->pred
[p
], f
);
266 multloop(Blk
*hd
, Blk
*b
)
277 for (b
=fn
->start
; b
; b
=b
->link
)
279 loopiter(fn
, multloop
);
283 uffind(Blk
**pb
, Blk
**uf
)
287 pb1
= &uf
[(*pb
)->id
];
294 /* requires rpo and no phis, breaks cfg */
299 Blk
**uf
; /* union-find */
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
) {
308 if (b
->jmp
.type
== Jret0
) {
313 if (b
->jmp
.type
== Jjmp
) {
319 for (p
=&fn
->start
; (b
=*p
); p
=&b
->link
) {
324 if (b
->s1
&& b
->s1
== b
->s2
) {