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
);
81 w
= Wsb
+ (i
->op
- Oparsb
);
82 if (isload(i
->op
) && i
->op
!= Oload
)
83 w
= Wsb
+ (i
->op
- Oloadsb
);
85 w
= Wsb
+ (i
->op
- Oextsb
);
86 if (w
== Wsw
|| w
== Wuw
)
96 if (rtype(i
->arg
[m
]) == RTmp
) {
98 adduse(&tmp
[t
], UIns
, b
, i
);
101 if (rtype(b
->jmp
.arg
) == RTmp
)
102 adduse(&tmp
[b
->jmp
.arg
.val
], UJmp
, b
);
107 refindex(int t
, Fn
*fn
)
109 return newtmp(fn
->tmp
[t
].name
, fn
->tmp
[t
].cls
, fn
);
116 Blk
*a
, *b
, **blist
, **be
, **bp
;
126 bsinit(defs
, fn
->nblk
);
127 blist
= emalloc(fn
->nblk
* sizeof blist
[0]);
128 be
= &blist
[fn
->nblk
];
130 for (t
=Tmp0
; t
<nt
; t
++) {
131 fn
->tmp
[t
].visit
= 0;
132 if (fn
->tmp
[t
].phi
!= 0)
134 if (fn
->tmp
[t
].ndef
== 1) {
136 defb
= fn
->tmp
[t
].bid
;
137 use
= fn
->tmp
[t
].use
;
138 for (n
=fn
->tmp
[t
].nuse
; n
--; use
++)
139 ok
&= use
->bid
== defb
;
140 if (ok
|| defb
== fn
->start
->id
)
146 for (b
=fn
->start
; b
; b
=b
->link
) {
149 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++) {
151 if (req(i
->arg
[0], TMP(t
)))
153 if (req(i
->arg
[1], TMP(t
)))
156 if (req(i
->to
, TMP(t
))) {
157 if (!bshas(b
->out
, t
)) {
161 if (!bshas(u
, b
->id
)) {
165 if (clsmerge(&k
, i
->cls
))
166 die("invalid input");
170 if (!req(r
, R
) && req(b
->jmp
.arg
, TMP(t
)))
175 fn
->tmp
[t
].visit
= t
;
178 for (n
=0; n
<b
->nfron
; n
++) {
181 if (bshas(a
->in
, t
)) {
182 p
= alloc(sizeof *p
);
186 p
->arg
= vnew(0, sizeof p
->arg
[0], Pfn
);
187 p
->blk
= vnew(0, sizeof p
->blk
[0], Pfn
);
189 if (!bshas(defs
, a
->id
))
190 if (!bshas(u
, a
->id
)) {
201 typedef struct Name Name
;
211 nnew(Ref r
, Blk
*b
, Name
*up
)
219 /* could use alloc, here
220 * but namel should be reset
222 n
= emalloc(sizeof *n
);
237 rendef(Ref
*r
, Blk
*b
, Name
**stk
, Fn
*fn
)
243 if (req(*r
, R
) || !fn
->tmp
[t
].visit
)
245 r1
= refindex(t
, fn
);
246 fn
->tmp
[r1
.val
].visit
= t
;
247 stk
[t
] = nnew(r1
, b
, stk
[t
]);
252 getstk(int t
, Blk
*b
, Name
**stk
)
257 while (n
&& !dom(n
->b
, b
)) {
271 renblk(Blk
*b
, Name
**stk
, Fn
*fn
)
275 Blk
*s
, **ps
, *succ
[3];
278 for (p
=b
->phi
; p
; p
=p
->link
)
279 rendef(&p
->to
, b
, stk
, fn
);
280 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++) {
281 for (m
=0; m
<2; m
++) {
283 if (rtype(i
->arg
[m
]) == RTmp
)
284 if (fn
->tmp
[t
].visit
)
285 i
->arg
[m
] = getstk(t
, b
, stk
);
287 rendef(&i
->to
, b
, stk
, fn
);
290 if (rtype(b
->jmp
.arg
) == RTmp
)
291 if (fn
->tmp
[t
].visit
)
292 b
->jmp
.arg
= getstk(t
, b
, stk
);
294 succ
[1] = b
->s2
== b
->s1
? 0 : b
->s2
;
296 for (ps
=succ
; (s
=*ps
); ps
++)
297 for (p
=s
->phi
; p
; p
=p
->link
) {
299 if ((t
=fn
->tmp
[t
].visit
)) {
301 vgrow(&p
->arg
, p
->narg
);
302 vgrow(&p
->blk
, p
->narg
);
303 p
->arg
[m
] = getstk(t
, b
, stk
);
307 for (s
=b
->dom
; s
; s
=s
->dlink
)
311 /* require rpo and use */
320 stk
= emalloc(nt
* sizeof stk
[0]);
325 fprintf(stderr
, "\n> Dominators:\n");
326 for (b1
=fn
->start
; b1
; b1
=b1
->link
) {
329 fprintf(stderr
, "%10s:", b1
->name
);
330 for (b
=b1
->dom
; b
; b
=b
->dlink
)
331 fprintf(stderr
, " %s", b
->name
);
332 fprintf(stderr
, "\n");
338 renblk(fn
->start
, stk
, fn
);
340 while ((n
=stk
[nt
])) {
347 fprintf(stderr
, "\n> After SSA construction:\n");
353 phicheck(Phi
*p
, Blk
*b
, Ref t
)
358 for (n
=0; n
<p
->narg
; n
++)
359 if (req(p
->arg
[n
], t
)) {
361 if (b1
!= b
&& !sdom(b
, b1
))
367 /* require use and ssa */
378 for (t
=&fn
->tmp
[Tmp0
]; t
-fn
->tmp
< fn
->ntmp
; t
++) {
380 err("ssa temporary %%%s defined more than once",
382 if (t
->nuse
> 0 && t
->ndef
== 0) {
383 bu
= fn
->rpo
[t
->use
[0].bid
];
387 for (b
=fn
->start
; b
; b
=b
->link
) {
388 for (p
=b
->phi
; p
; p
=p
->link
) {
391 for (u
=t
->use
; u
<&t
->use
[t
->nuse
]; u
++) {
392 bu
= fn
->rpo
[u
->bid
];
393 if (u
->type
== UPhi
) {
394 if (phicheck(u
->u
.phi
, b
, r
))
397 if (bu
!= b
&& !sdom(b
, bu
))
401 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++) {
402 if (rtype(i
->to
) != RTmp
)
406 for (u
=t
->use
; u
<&t
->use
[t
->nuse
]; u
++) {
407 bu
= fn
->rpo
[u
->bid
];
408 if (u
->type
== UPhi
) {
409 if (phicheck(u
->u
.phi
, b
, r
))
426 die("%%%s violates ssa invariant", t
->name
);
428 err("ssa temporary %%%s is used undefined in @%s",