5 adduse(Tmp
*tmp
, int ty
, Blk
*b
, ...)
15 vgrow(&tmp
->use
, ++tmp
->nuse
);
21 u
->u
.phi
= va_arg(ap
, Phi
*);
24 u
->u
.ins
= va_arg(ap
, Ins
*);
34 /* fill usage, width, phi, and class information
35 * must not change .visit fields
47 /* todo, is this the correct file? */
49 for (t
=Tmp0
; t
<fn
->ntmp
; t
++) {
57 tmp
[t
].use
= vnew(0, sizeof(Use
), Pfn
);
59 for (b
=fn
->start
; b
; b
=b
->link
) {
60 for (p
=b
->phi
; p
; p
=p
->link
) {
61 assert(rtype(p
->to
) == RTmp
);
66 tp
= phicls(tp
, fn
->tmp
);
67 for (a
=0; a
<p
->narg
; a
++)
68 if (rtype(p
->arg
[a
]) == RTmp
) {
70 adduse(&tmp
[t
], UPhi
, b
, p
);
71 t
= phicls(t
, fn
->tmp
);
76 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++) {
78 assert(rtype(i
->to
) == RTmp
);
80 if (isload(i
->op
) && i
->op
!= Oload
)
81 w
= Wsb
+ (i
->op
- Oloadsb
);
83 w
= Wsb
+ (i
->op
- Oextsb
);
84 if (w
== Wsw
|| w
== Wuw
)
94 if (rtype(i
->arg
[m
]) == RTmp
) {
96 adduse(&tmp
[t
], UIns
, b
, i
);
99 if (rtype(b
->jmp
.arg
) == RTmp
)
100 adduse(&tmp
[b
->jmp
.arg
.val
], UJmp
, b
);
105 refindex(int t
, Fn
*fn
)
107 return newtmp(fn
->tmp
[t
].name
, fn
->tmp
[t
].cls
, fn
);
114 Blk
*a
, *b
, **blist
, **be
, **bp
;
124 bsinit(defs
, fn
->nblk
);
125 blist
= emalloc(fn
->nblk
* sizeof blist
[0]);
126 be
= &blist
[fn
->nblk
];
128 for (t
=Tmp0
; t
<nt
; t
++) {
129 fn
->tmp
[t
].visit
= 0;
130 if (fn
->tmp
[t
].phi
!= 0)
132 if (fn
->tmp
[t
].ndef
== 1) {
134 defb
= fn
->tmp
[t
].bid
;
135 use
= fn
->tmp
[t
].use
;
136 for (n
=fn
->tmp
[t
].nuse
; n
--; use
++)
137 ok
&= use
->bid
== defb
;
138 if (ok
|| defb
== fn
->start
->id
)
144 for (b
=fn
->start
; b
; b
=b
->link
) {
147 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++) {
149 if (req(i
->arg
[0], TMP(t
)))
151 if (req(i
->arg
[1], TMP(t
)))
154 if (req(i
->to
, TMP(t
))) {
155 if (!bshas(b
->out
, t
)) {
159 if (!bshas(u
, b
->id
)) {
163 if (clsmerge(&k
, i
->cls
))
164 die("invalid input");
168 if (!req(r
, R
) && req(b
->jmp
.arg
, TMP(t
)))
173 fn
->tmp
[t
].visit
= t
;
176 for (n
=0; n
<b
->nfron
; n
++) {
179 if (bshas(a
->in
, t
)) {
180 p
= alloc(sizeof *p
);
184 p
->arg
= vnew(0, sizeof p
->arg
[0], Pfn
);
185 p
->blk
= vnew(0, sizeof p
->blk
[0], Pfn
);
187 if (!bshas(defs
, a
->id
))
188 if (!bshas(u
, a
->id
)) {
199 typedef struct Name Name
;
209 nnew(Ref r
, Blk
*b
, Name
*up
)
217 /* could use alloc, here
218 * but namel should be reset
220 n
= emalloc(sizeof *n
);
235 rendef(Ref
*r
, Blk
*b
, Name
**stk
, Fn
*fn
)
241 if (req(*r
, R
) || !fn
->tmp
[t
].visit
)
243 r1
= refindex(t
, fn
);
244 fn
->tmp
[r1
.val
].visit
= t
;
245 stk
[t
] = nnew(r1
, b
, stk
[t
]);
250 getstk(int t
, Blk
*b
, Name
**stk
)
255 while (n
&& !dom(n
->b
, b
)) {
269 renblk(Blk
*b
, Name
**stk
, Fn
*fn
)
273 Blk
*s
, **ps
, *succ
[3];
276 for (p
=b
->phi
; p
; p
=p
->link
)
277 rendef(&p
->to
, b
, stk
, fn
);
278 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++) {
279 for (m
=0; m
<2; m
++) {
281 if (rtype(i
->arg
[m
]) == RTmp
)
282 if (fn
->tmp
[t
].visit
)
283 i
->arg
[m
] = getstk(t
, b
, stk
);
285 rendef(&i
->to
, b
, stk
, fn
);
288 if (rtype(b
->jmp
.arg
) == RTmp
)
289 if (fn
->tmp
[t
].visit
)
290 b
->jmp
.arg
= getstk(t
, b
, stk
);
292 succ
[1] = b
->s2
== b
->s1
? 0 : b
->s2
;
294 for (ps
=succ
; (s
=*ps
); ps
++)
295 for (p
=s
->phi
; p
; p
=p
->link
) {
297 if ((t
=fn
->tmp
[t
].visit
)) {
299 vgrow(&p
->arg
, p
->narg
);
300 vgrow(&p
->blk
, p
->narg
);
301 p
->arg
[m
] = getstk(t
, b
, stk
);
305 for (s
=b
->dom
; s
; s
=s
->dlink
)
309 /* require rpo and use */
318 stk
= emalloc(nt
* sizeof stk
[0]);
323 fprintf(stderr
, "\n> Dominators:\n");
324 for (b1
=fn
->start
; b1
; b1
=b1
->link
) {
327 fprintf(stderr
, "%10s:", b1
->name
);
328 for (b
=b1
->dom
; b
; b
=b
->dlink
)
329 fprintf(stderr
, " %s", b
->name
);
330 fprintf(stderr
, "\n");
336 renblk(fn
->start
, stk
, fn
);
338 while ((n
=stk
[nt
])) {
345 fprintf(stderr
, "\n> After SSA construction:\n");
351 phicheck(Phi
*p
, Blk
*b
, Ref t
)
356 for (n
=0; n
<p
->narg
; n
++)
357 if (req(p
->arg
[n
], t
)) {
359 if (b1
!= b
&& !sdom(b
, b1
))
365 /* require use and ssa */
376 for (t
=&fn
->tmp
[Tmp0
]; t
-fn
->tmp
< fn
->ntmp
; t
++) {
378 err("ssa temporary %%%s defined more than once",
380 if (t
->nuse
> 0 && t
->ndef
== 0) {
381 bu
= fn
->rpo
[t
->use
[0].bid
];
385 for (b
=fn
->start
; b
; b
=b
->link
) {
386 for (p
=b
->phi
; p
; p
=p
->link
) {
389 for (u
=t
->use
; u
<&t
->use
[t
->nuse
]; u
++) {
390 bu
= fn
->rpo
[u
->bid
];
391 if (u
->type
== UPhi
) {
392 if (phicheck(u
->u
.phi
, b
, r
))
395 if (bu
!= b
&& !sdom(b
, bu
))
399 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++) {
400 if (rtype(i
->to
) != RTmp
)
404 for (u
=t
->use
; u
<&t
->use
[t
->nuse
]; u
++) {
405 bu
= fn
->rpo
[u
->bid
];
406 if (u
->type
== UPhi
) {
407 if (phicheck(u
->u
.phi
, b
, r
))
424 die("%%%s violates ssa invariant", t
->name
);
426 err("ssa temporary %%%s is used undefined in @%s",