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 tp
= phicls(tp
, fn
->tmp
);
65 for (a
=0; a
<p
->narg
; a
++)
66 if (rtype(p
->arg
[a
]) == RTmp
) {
68 adduse(&tmp
[t
], UPhi
, b
, p
);
69 t
= phicls(t
, fn
->tmp
);
74 for (i
=b
->ins
; i
-b
->ins
< b
->nins
; i
++) {
76 assert(rtype(i
->to
) == RTmp
);
78 if (isload(i
->op
) && i
->op
!= Oload
)
79 w
= Wsb
+ (i
->op
- Oloadsb
);
81 w
= Wsb
+ (i
->op
- Oextsb
);
82 if (w
== Wsw
|| w
== Wuw
)
91 if (rtype(i
->arg
[m
]) == RTmp
) {
93 adduse(&tmp
[t
], UIns
, b
, i
);
96 if (rtype(b
->jmp
.arg
) == RTmp
)
97 adduse(&tmp
[b
->jmp
.arg
.val
], UJmp
, b
);
102 refindex(int t
, Fn
*fn
)
104 return newtmp(fn
->tmp
[t
].name
, fn
->tmp
[t
].cls
, fn
);
111 Blk
*a
, *b
, **blist
, **be
, **bp
;
120 bsinit(defs
, fn
->nblk
);
121 blist
= emalloc(fn
->nblk
* sizeof blist
[0]);
122 be
= &blist
[fn
->nblk
];
124 for (t
=Tmp0
; t
<nt
; t
++) {
125 fn
->tmp
[t
].visit
= 0;
126 if (fn
->tmp
[t
].phi
!= 0)
131 for (b
=fn
->start
; b
; b
=b
->link
) {
134 for (i
=b
->ins
; i
-b
->ins
< b
->nins
; i
++) {
136 if (req(i
->arg
[0], TMP(t
)))
138 if (req(i
->arg
[1], TMP(t
)))
141 if (req(i
->to
, TMP(t
))) {
142 if (!bshas(b
->out
, t
)) {
143 if (fn
->tmp
[t
].ndef
== 1)
149 if (!bshas(u
, b
->id
)) {
153 if (clsmerge(&k
, i
->cls
))
154 die("invalid input");
158 if (!req(r
, R
) && req(b
->jmp
.arg
, TMP(t
)))
163 fn
->tmp
[t
].visit
= t
;
166 for (n
=0; n
<b
->nfron
; n
++) {
169 if (bshas(a
->in
, t
)) {
170 p
= alloc(sizeof *p
);
175 if (!bshas(defs
, a
->id
))
176 if (!bshas(u
, a
->id
)) {
187 typedef struct Name Name
;
197 nnew(Ref r
, Blk
*b
, Name
*up
)
205 /* could use alloc, here
206 * but namel should be reset
208 n
= emalloc(sizeof *n
);
223 rendef(Ref
*r
, Blk
*b
, Name
**stk
, Fn
*fn
)
229 if (req(*r
, R
) || !fn
->tmp
[t
].visit
)
231 r1
= refindex(t
, fn
);
232 fn
->tmp
[r1
.val
].visit
= t
;
233 stk
[t
] = nnew(r1
, b
, stk
[t
]);
238 getstk(int t
, Blk
*b
, Name
**stk
)
243 while (n
&& !dom(n
->b
, b
)) {
257 renblk(Blk
*b
, Name
**stk
, Fn
*fn
)
261 Blk
*s
, **ps
, *succ
[3];
264 for (p
=b
->phi
; p
; p
=p
->link
)
265 rendef(&p
->to
, b
, stk
, fn
);
266 for (i
=b
->ins
; i
-b
->ins
< b
->nins
; i
++) {
267 for (m
=0; m
<2; m
++) {
269 if (rtype(i
->arg
[m
]) == RTmp
)
270 if (fn
->tmp
[t
].visit
)
271 i
->arg
[m
] = getstk(t
, b
, stk
);
273 rendef(&i
->to
, b
, stk
, fn
);
276 if (rtype(b
->jmp
.arg
) == RTmp
)
277 if (fn
->tmp
[t
].visit
)
278 b
->jmp
.arg
= getstk(t
, b
, stk
);
280 succ
[1] = b
->s2
== b
->s1
? 0 : b
->s2
;
282 for (ps
=succ
; (s
=*ps
); ps
++)
283 for (p
=s
->phi
; p
; p
=p
->link
) {
285 if ((t
=fn
->tmp
[t
].visit
)) {
288 die("renblk, too many phi args");
289 p
->arg
[m
] = getstk(t
, b
, stk
);
293 for (s
=b
->dom
; s
; s
=s
->dlink
)
297 /* require rpo and ndef */
306 stk
= emalloc(nt
* sizeof stk
[0]);
311 fprintf(stderr
, "\n> Dominators:\n");
312 for (b1
=fn
->start
; b1
; b1
=b1
->link
) {
315 fprintf(stderr
, "%10s:", b1
->name
);
316 for (b
=b1
->dom
; b
; b
=b
->dlink
)
317 fprintf(stderr
, " %s", b
->name
);
318 fprintf(stderr
, "\n");
324 renblk(fn
->start
, stk
, fn
);
326 while ((n
=stk
[nt
])) {
333 fprintf(stderr
, "\n> After SSA construction:\n");
339 phicheck(Phi
*p
, Blk
*b
, Ref t
)
344 for (n
=0; n
<p
->narg
; n
++)
345 if (req(p
->arg
[n
], t
)) {
347 if (b1
!= b
&& !sdom(b
, b1
))
353 /* require use and ssa */
364 for (t
=&fn
->tmp
[Tmp0
]; t
-fn
->tmp
< fn
->ntmp
; t
++) {
366 err("ssa temporary %%%s defined more than once",
368 if (t
->nuse
> 0 && t
->ndef
== 0) {
369 bu
= fn
->rpo
[t
->use
[0].bid
];
373 for (b
=fn
->start
; b
; b
=b
->link
) {
374 for (p
=b
->phi
; p
; p
=p
->link
) {
377 for (u
=t
->use
; u
-t
->use
< t
->nuse
; u
++) {
378 bu
= fn
->rpo
[u
->bid
];
379 if (u
->type
== UPhi
) {
380 if (phicheck(u
->u
.phi
, b
, r
))
383 if (bu
!= b
&& !sdom(b
, bu
))
387 for (i
=b
->ins
; i
-b
->ins
< b
->nins
; i
++) {
388 if (rtype(i
->to
) != RTmp
)
392 for (u
=t
->use
; u
-t
->use
< t
->nuse
; u
++) {
393 bu
= fn
->rpo
[u
->bid
];
394 if (u
->type
== UPhi
) {
395 if (phicheck(u
->u
.phi
, b
, r
))
412 die("%%%s violates ssa invariant", t
->name
);
414 err("ssa temporary %%%s is used undefined in @%s",