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
++) {
56 tmp
[t
].use
= vnew(0, sizeof(Use
), Pfn
);
58 for (b
=fn
->start
; b
; b
=b
->link
) {
59 for (p
=b
->phi
; p
; p
=p
->link
) {
60 assert(rtype(p
->to
) == RTmp
);
64 tmp
[t
].phi
= p
->to
.val
;
65 for (a
=0; a
<p
->narg
; a
++)
66 if (rtype(p
->arg
[a
]) == RTmp
) {
68 adduse(&tmp
[t
], UPhi
, b
, p
);
70 tmp
[t
].phi
= p
->to
.val
;
73 for (i
=b
->ins
; i
-b
->ins
< b
->nins
; i
++) {
75 assert(rtype(i
->to
) == RTmp
);
77 if (isload(i
->op
) && i
->op
!= Oload
)
78 w
= Wsb
+ (i
->op
- Oloadsb
);
80 w
= Wsb
+ (i
->op
- Oextsb
);
81 if (w
== Wsw
|| w
== Wuw
)
90 if (rtype(i
->arg
[m
]) == RTmp
) {
92 adduse(&tmp
[t
], UIns
, b
, i
);
95 if (rtype(b
->jmp
.arg
) == RTmp
)
96 adduse(&tmp
[b
->jmp
.arg
.val
], UJmp
, b
);
101 refindex(int t
, Fn
*fn
)
103 return newtmp(fn
->tmp
[t
].name
, fn
->tmp
[t
].cls
, fn
);
110 Blk
*a
, *b
, **blist
, **be
, **bp
;
119 bsinit(defs
, fn
->nblk
);
120 blist
= emalloc(fn
->nblk
* sizeof blist
[0]);
121 be
= &blist
[fn
->nblk
];
123 for (t
=Tmp0
; t
<nt
; t
++) {
124 fn
->tmp
[t
].visit
= 0;
125 if (fn
->tmp
[t
].phi
!= 0)
130 for (b
=fn
->start
; b
; b
=b
->link
) {
133 for (i
=b
->ins
; i
-b
->ins
< b
->nins
; i
++) {
135 if (req(i
->arg
[0], TMP(t
)))
137 if (req(i
->arg
[1], TMP(t
)))
140 if (req(i
->to
, TMP(t
))) {
141 if (!bshas(b
->out
, t
)) {
142 if (fn
->tmp
[t
].ndef
== 1)
148 if (!bshas(u
, b
->id
)) {
152 if (clsmerge(&k
, i
->cls
))
153 die("invalid input");
157 if (!req(r
, R
) && req(b
->jmp
.arg
, TMP(t
)))
162 fn
->tmp
[t
].visit
= t
;
165 for (n
=0; n
<b
->nfron
; n
++) {
168 if (bshas(a
->in
, t
)) {
169 p
= alloc(sizeof *p
);
174 if (!bshas(defs
, a
->id
))
175 if (!bshas(u
, a
->id
)) {
186 typedef struct Name Name
;
196 nnew(Ref r
, Blk
*b
, Name
*up
)
204 /* could use alloc, here
205 * but namel should be reset
207 n
= emalloc(sizeof *n
);
222 rendef(Ref
*r
, Blk
*b
, Name
**stk
, Fn
*fn
)
228 if (req(*r
, R
) || !fn
->tmp
[t
].visit
)
230 r1
= refindex(t
, fn
);
231 fn
->tmp
[r1
.val
].visit
= t
;
232 stk
[t
] = nnew(r1
, b
, stk
[t
]);
237 getstk(int t
, Blk
*b
, Name
**stk
)
242 while (n
&& !dom(n
->b
, b
)) {
256 renblk(Blk
*b
, Name
**stk
, Fn
*fn
)
260 Blk
*s
, **ps
, *succ
[3];
263 for (p
=b
->phi
; p
; p
=p
->link
)
264 rendef(&p
->to
, b
, stk
, fn
);
265 for (i
=b
->ins
; i
-b
->ins
< b
->nins
; i
++) {
266 for (m
=0; m
<2; m
++) {
268 if (rtype(i
->arg
[m
]) == RTmp
)
269 if (fn
->tmp
[t
].visit
)
270 i
->arg
[m
] = getstk(t
, b
, stk
);
272 rendef(&i
->to
, b
, stk
, fn
);
275 if (rtype(b
->jmp
.arg
) == RTmp
)
276 if (fn
->tmp
[t
].visit
)
277 b
->jmp
.arg
= getstk(t
, b
, stk
);
279 succ
[1] = b
->s2
== b
->s1
? 0 : b
->s2
;
281 for (ps
=succ
; (s
=*ps
); ps
++)
282 for (p
=s
->phi
; p
; p
=p
->link
) {
284 if ((t
=fn
->tmp
[t
].visit
)) {
287 die("renblk, too many phi args");
288 p
->arg
[m
] = getstk(t
, b
, stk
);
292 for (s
=b
->dom
; s
; s
=s
->dlink
)
296 /* require rpo and ndef */
305 stk
= emalloc(nt
* sizeof stk
[0]);
310 fprintf(stderr
, "\n> Dominators:\n");
311 for (b1
=fn
->start
; b1
; b1
=b1
->link
) {
314 fprintf(stderr
, "%10s:", b1
->name
);
315 for (b
=b1
->dom
; b
; b
=b
->dlink
)
316 fprintf(stderr
, " %s", b
->name
);
317 fprintf(stderr
, "\n");
323 renblk(fn
->start
, stk
, fn
);
325 while ((n
=stk
[nt
])) {
332 fprintf(stderr
, "\n> After SSA construction:\n");
338 phicheck(Phi
*p
, Blk
*b
, Ref t
)
343 for (n
=0; n
<p
->narg
; n
++)
344 if (req(p
->arg
[n
], t
)) {
346 if (b1
!= b
&& !sdom(b
, b1
))
352 /* require use and ssa */
363 for (t
=&fn
->tmp
[Tmp0
]; t
-fn
->tmp
< fn
->ntmp
; t
++) {
365 err("ssa temporary %%%s defined more than once",
367 if (t
->nuse
> 0 && t
->ndef
== 0) {
368 bu
= fn
->rpo
[t
->use
[0].bid
];
372 for (b
=fn
->start
; b
; b
=b
->link
) {
373 for (p
=b
->phi
; p
; p
=p
->link
) {
376 for (u
=t
->use
; u
-t
->use
< t
->nuse
; u
++) {
377 bu
= fn
->rpo
[u
->bid
];
378 if (u
->type
== UPhi
) {
379 if (phicheck(u
->u
.phi
, b
, r
))
382 if (bu
!= b
&& !sdom(b
, bu
))
386 for (i
=b
->ins
; i
-b
->ins
< b
->nins
; i
++) {
387 if (rtype(i
->to
) != RTmp
)
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
))
411 die("%%%s violates ssa invariant", t
->name
);
413 err("ssa temporary %%%s is used undefined in @%s",