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
++) {
58 tmp
[t
].use
= vnew(0, sizeof(Use
), PFn
);
60 for (b
=fn
->start
; b
; b
=b
->link
) {
61 for (p
=b
->phi
; p
; p
=p
->link
) {
62 assert(rtype(p
->to
) == RTmp
);
67 tp
= phicls(tp
, fn
->tmp
);
68 for (a
=0; a
<p
->narg
; a
++)
69 if (rtype(p
->arg
[a
]) == RTmp
) {
71 adduse(&tmp
[t
], UPhi
, b
, p
);
72 t
= phicls(t
, fn
->tmp
);
77 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++) {
79 assert(rtype(i
->to
) == RTmp
);
82 w
= Wsb
+ (i
->op
- Oparsb
);
83 if (isload(i
->op
) && i
->op
!= Oload
)
84 w
= Wsb
+ (i
->op
- Oloadsb
);
86 w
= Wsb
+ (i
->op
- Oextsb
);
87 if (w
== Wsw
|| w
== Wuw
)
98 if (rtype(i
->arg
[m
]) == RTmp
) {
100 adduse(&tmp
[t
], UIns
, b
, i
);
103 if (rtype(b
->jmp
.arg
) == RTmp
)
104 adduse(&tmp
[b
->jmp
.arg
.val
], UJmp
, b
);
109 refindex(int t
, Fn
*fn
)
111 return newtmp(fn
->tmp
[t
].name
, fn
->tmp
[t
].cls
, fn
);
118 Blk
*a
, *b
, **blist
, **be
, **bp
;
128 bsinit(defs
, fn
->nblk
);
129 blist
= emalloc(fn
->nblk
* sizeof blist
[0]);
130 be
= &blist
[fn
->nblk
];
132 for (t
=Tmp0
; t
<nt
; t
++) {
133 fn
->tmp
[t
].visit
= 0;
134 if (fn
->tmp
[t
].phi
!= 0)
136 if (fn
->tmp
[t
].ndef
== 1) {
138 defb
= fn
->tmp
[t
].bid
;
139 use
= fn
->tmp
[t
].use
;
140 for (n
=fn
->tmp
[t
].nuse
; n
--; use
++)
141 ok
&= use
->bid
== defb
;
142 if (ok
|| defb
== fn
->start
->id
)
148 for (b
=fn
->start
; b
; b
=b
->link
) {
151 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++) {
153 if (req(i
->arg
[0], TMP(t
)))
155 if (req(i
->arg
[1], TMP(t
)))
158 if (req(i
->to
, TMP(t
))) {
159 if (!bshas(b
->out
, t
)) {
163 if (!bshas(u
, b
->id
)) {
167 if (clsmerge(&k
, i
->cls
))
168 die("invalid input");
172 if (!req(r
, R
) && req(b
->jmp
.arg
, TMP(t
)))
177 fn
->tmp
[t
].visit
= t
;
180 for (n
=0; n
<b
->nfron
; n
++) {
183 if (bshas(a
->in
, t
)) {
184 p
= alloc(sizeof *p
);
188 p
->arg
= vnew(0, sizeof p
->arg
[0], PFn
);
189 p
->blk
= vnew(0, sizeof p
->blk
[0], PFn
);
191 if (!bshas(defs
, a
->id
))
192 if (!bshas(u
, a
->id
)) {
203 typedef struct Name Name
;
213 nnew(Ref r
, Blk
*b
, Name
*up
)
221 /* could use alloc, here
222 * but namel should be reset
224 n
= emalloc(sizeof *n
);
239 rendef(Ref
*r
, Blk
*b
, Name
**stk
, Fn
*fn
)
245 if (req(*r
, R
) || !fn
->tmp
[t
].visit
)
247 r1
= refindex(t
, fn
);
248 fn
->tmp
[r1
.val
].visit
= t
;
249 stk
[t
] = nnew(r1
, b
, stk
[t
]);
254 getstk(int t
, Blk
*b
, Name
**stk
)
259 while (n
&& !dom(n
->b
, b
)) {
273 renblk(Blk
*b
, Name
**stk
, Fn
*fn
)
277 Blk
*s
, **ps
, *succ
[3];
280 for (p
=b
->phi
; p
; p
=p
->link
)
281 rendef(&p
->to
, b
, stk
, fn
);
282 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++) {
283 for (m
=0; m
<2; m
++) {
285 if (rtype(i
->arg
[m
]) == RTmp
)
286 if (fn
->tmp
[t
].visit
)
287 i
->arg
[m
] = getstk(t
, b
, stk
);
289 rendef(&i
->to
, b
, stk
, fn
);
292 if (rtype(b
->jmp
.arg
) == RTmp
)
293 if (fn
->tmp
[t
].visit
)
294 b
->jmp
.arg
= getstk(t
, b
, stk
);
296 succ
[1] = b
->s2
== b
->s1
? 0 : b
->s2
;
298 for (ps
=succ
; (s
=*ps
); ps
++)
299 for (p
=s
->phi
; p
; p
=p
->link
) {
301 if ((t
=fn
->tmp
[t
].visit
)) {
303 vgrow(&p
->arg
, p
->narg
);
304 vgrow(&p
->blk
, p
->narg
);
305 p
->arg
[m
] = getstk(t
, b
, stk
);
309 for (s
=b
->dom
; s
; s
=s
->dlink
)
313 /* require rpo and use */
322 stk
= emalloc(nt
* sizeof stk
[0]);
327 fprintf(stderr
, "\n> Dominators:\n");
328 for (b1
=fn
->start
; b1
; b1
=b1
->link
) {
331 fprintf(stderr
, "%10s:", b1
->name
);
332 for (b
=b1
->dom
; b
; b
=b
->dlink
)
333 fprintf(stderr
, " %s", b
->name
);
334 fprintf(stderr
, "\n");
340 renblk(fn
->start
, stk
, fn
);
342 while ((n
=stk
[nt
])) {
349 fprintf(stderr
, "\n> After SSA construction:\n");
355 phicheck(Phi
*p
, Blk
*b
, Ref t
)
360 for (n
=0; n
<p
->narg
; n
++)
361 if (req(p
->arg
[n
], t
)) {
363 if (b1
!= b
&& !sdom(b
, b1
))
369 /* require use and ssa */
380 for (t
=&fn
->tmp
[Tmp0
]; t
-fn
->tmp
< fn
->ntmp
; t
++) {
382 err("ssa temporary %%%s defined more than once",
384 if (t
->nuse
> 0 && t
->ndef
== 0) {
385 bu
= fn
->rpo
[t
->use
[0].bid
];
389 for (b
=fn
->start
; b
; b
=b
->link
) {
390 for (p
=b
->phi
; p
; p
=p
->link
) {
393 for (u
=t
->use
; u
<&t
->use
[t
->nuse
]; u
++) {
394 bu
= fn
->rpo
[u
->bid
];
395 if (u
->type
== UPhi
) {
396 if (phicheck(u
->u
.phi
, b
, r
))
399 if (bu
!= b
&& !sdom(b
, bu
))
403 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++) {
404 if (rtype(i
->to
) != RTmp
)
408 for (u
=t
->use
; u
<&t
->use
[t
->nuse
]; u
++) {
409 bu
= fn
->rpo
[u
->bid
];
410 if (u
->type
== UPhi
) {
411 if (phicheck(u
->u
.phi
, b
, r
))
428 die("%%%s violates ssa invariant", t
->name
);
430 err("ssa temporary %%%s is used undefined in @%s",