4 Bot
= -2, /* lattice bottom */
5 Top
= -1, /* lattice top */
8 typedef struct Edge Edge
;
17 static Edge
*flowrk
, (*edge
)[2];
22 iscon(Con
*c
, int w
, uint64_t k
)
27 return (uint64_t)c
->bits
.i
== k
;
29 return (uint32_t)c
->bits
.i
== (uint32_t)k
;
46 latmerge(int v
, int m
)
48 return m
== Top
? v
: (v
== Top
|| v
== m
) ? m
: Bot
;
52 update(int t
, int m
, Fn
*fn
)
57 m
= latmerge(val
[t
], m
);
60 for (u
=0; u
<tmp
->nuse
; u
++) {
61 vgrow(&usewrk
, ++nuse
);
62 usewrk
[nuse
-1] = &tmp
->use
[u
];
69 deadedge(int s
, int d
)
74 if (e
[0].dest
== d
&& !e
[0].dead
)
76 if (e
[1].dest
== d
&& !e
[1].dead
)
82 visitphi(Phi
*p
, int n
, Fn
*fn
)
88 for (a
=0; a
<p
->narg
; a
++)
89 if (!deadedge(p
->blk
[a
]->id
, n
))
90 v
= latmerge(v
, latval(p
->arg
[a
]));
91 update(p
->to
.val
, v
, fn
);
94 static int opfold(int, int, Con
*, Con
*, Fn
*);
97 visitins(Ins
*i
, Fn
*fn
)
101 if (rtype(i
->to
) != RTmp
)
103 if (optab
[i
->op
].canfold
) {
104 l
= latval(i
->arg
[0]);
105 if (!req(i
->arg
[1], R
))
106 r
= latval(i
->arg
[1]);
109 if (l
== Bot
|| r
== Bot
)
111 else if (l
== Top
|| r
== Top
)
114 v
= opfold(i
->op
, i
->cls
, &fn
->con
[l
], &fn
->con
[r
], fn
);
117 /* fprintf(stderr, "\nvisiting %s (%p)", optab[i->op].name, (void *)i); */
118 update(i
->to
.val
, v
, fn
);
122 visitjmp(Blk
*b
, int n
, Fn
*fn
)
126 switch (b
->jmp
.type
) {
128 l
= latval(b
->jmp
.arg
);
129 assert(l
!= Top
&& "ssa invariant broken");
131 edge
[n
][1].work
= flowrk
;
132 edge
[n
][0].work
= &edge
[n
][1];
133 flowrk
= &edge
[n
][0];
135 else if (iscon(&fn
->con
[l
], 0, 0)) {
136 assert(edge
[n
][0].dead
);
137 edge
[n
][1].work
= flowrk
;
138 flowrk
= &edge
[n
][1];
141 assert(edge
[n
][1].dead
);
142 edge
[n
][0].work
= flowrk
;
143 flowrk
= &edge
[n
][0];
147 edge
[n
][0].work
= flowrk
;
148 flowrk
= &edge
[n
][0];
151 if (isret(b
->jmp
.type
))
158 initedge(Edge
*e
, Blk
*s
)
173 if (rtype(*r
) == RTmp
)
174 if ((l
=val
[r
->val
]) != Bot
) {
175 assert(l
!= Top
&& "ssa invariant broken");
182 /* require rpo, use, pred */
194 val
= emalloc(fn
->ntmp
* sizeof val
[0]);
195 edge
= emalloc(fn
->nblk
* sizeof edge
[0]);
196 usewrk
= vnew(0, sizeof usewrk
[0], Pheap
);
198 for (t
=0; t
<fn
->ntmp
; t
++)
200 for (n
=0; n
<fn
->nblk
; n
++) {
203 initedge(&edge
[n
][0], b
->s1
);
204 initedge(&edge
[n
][1], b
->s2
);
206 initedge(&start
, fn
->start
);
210 /* 1. find out constants and dead cfg edges */
216 if (e
->dest
== -1 || !e
->dead
)
221 for (p
=b
->phi
; p
; p
=p
->link
)
224 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++)
229 assert(b
->jmp
.type
!= Jjmp
231 || flowrk
== &edge
[n
][0]);
241 visitphi(u
->u
.phi
, u
->bid
, fn
);
244 visitins(u
->u
.ins
, fn
);
258 fprintf(stderr
, "\n> SCCP findings:");
259 for (t
=Tmp0
; t
<fn
->ntmp
; t
++) {
262 fprintf(stderr
, "\n%10s: ", fn
->tmp
[t
].name
);
264 fprintf(stderr
, "Top");
266 printref(CON(val
[t
]), fn
, stderr
);
268 fprintf(stderr
, "\n dead code: ");
271 /* 2. trim dead code, replace constants */
273 for (pb
=&fn
->start
; (b
=*pb
);) {
277 fprintf(stderr
, "%s ", b
->name
);
283 for (pp
=&b
->phi
; (p
=*pp
);)
284 if (val
[p
->to
.val
] != Bot
)
287 for (a
=0; a
<p
->narg
; a
++)
288 if (!deadedge(p
->blk
[a
]->id
, b
->id
))
292 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++)
294 *i
= (Ins
){.op
= Onop
};
299 if (b
->jmp
.type
== Jjnz
&& rtype(b
->jmp
.arg
) == RCon
) {
300 if (iscon(&fn
->con
[b
->jmp
.arg
.val
], 0, 0)) {
314 fprintf(stderr
, "(none)");
315 fprintf(stderr
, "\n\n> After constant folding:\n");
324 /* boring folding code */
327 foldint(Con
*res
, int op
, int w
, Con
*cl
, Con
*cr
)
344 if (cl
->type
== CAddr
) {
345 if (cr
->type
== CAddr
)
350 else if (cr
->type
== CAddr
) {
355 else if (op
== Osub
) {
356 if (cl
->type
== CAddr
) {
357 if (cr
->type
!= CAddr
) {
360 } else if (cl
->label
!= cr
->label
)
363 else if (cr
->type
== CAddr
)
366 else if (cl
->type
== CAddr
|| cr
->type
== CAddr
)
368 if (op
== Odiv
|| op
== Orem
|| op
== Oudiv
|| op
== Ourem
) {
371 if (op
== Odiv
|| op
== Orem
) {
372 x
= w
? INT64_MIN
: INT32_MIN
;
373 if (iscon(cr
, w
, -1))
379 case Oadd
: x
= l
.u
+ r
.u
; break;
380 case Osub
: x
= l
.u
- r
.u
; break;
381 case Oneg
: x
= -l
.u
; break;
382 case Odiv
: x
= w
? l
.s
/ r
.s
: (int32_t)l
.s
/ (int32_t)r
.s
; break;
383 case Orem
: x
= w
? l
.s
% r
.s
: (int32_t)l
.s
% (int32_t)r
.s
; break;
384 case Oudiv
: x
= w
? l
.u
/ r
.u
: (uint32_t)l
.u
/ (uint32_t)r
.u
; break;
385 case Ourem
: x
= w
? l
.u
% r
.u
: (uint32_t)l
.u
% (uint32_t)r
.u
; break;
386 case Omul
: x
= l
.u
* r
.u
; break;
387 case Oand
: x
= l
.u
& r
.u
; break;
388 case Oor
: x
= l
.u
| r
.u
; break;
389 case Oxor
: x
= l
.u
^ r
.u
; break;
390 case Osar
: x
= (w
? l
.s
: (int32_t)l
.s
) >> (r
.u
& (31|w
<<5)); break;
391 case Oshr
: x
= (w
? l
.u
: (uint32_t)l
.u
) >> (r
.u
& (31|w
<<5)); break;
392 case Oshl
: x
= l
.u
<< (r
.u
& (31|w
<<5)); break;
393 case Oextsb
: x
= (int8_t)l
.u
; break;
394 case Oextub
: x
= (uint8_t)l
.u
; break;
395 case Oextsh
: x
= (int16_t)l
.u
; break;
396 case Oextuh
: x
= (uint16_t)l
.u
; break;
397 case Oextsw
: x
= (int32_t)l
.u
; break;
398 case Oextuw
: x
= (uint32_t)l
.u
; break;
399 case Ostosi
: x
= w
? (int64_t)cl
->bits
.s
: (int32_t)cl
->bits
.s
; break;
400 case Ostoui
: x
= w
? (uint64_t)cl
->bits
.s
: (uint32_t)cl
->bits
.s
; break;
401 case Odtosi
: x
= w
? (int64_t)cl
->bits
.d
: (int32_t)cl
->bits
.d
; break;
402 case Odtoui
: x
= w
? (uint64_t)cl
->bits
.d
: (uint32_t)cl
->bits
.d
; break;
405 if (cl
->type
== CAddr
) {
411 if (Ocmpw
<= op
&& op
<= Ocmpl1
) {
417 switch (op
- Ocmpw
) {
418 case Ciule
: x
= l
.u
<= r
.u
; break;
419 case Ciult
: x
= l
.u
< r
.u
; break;
420 case Cisle
: x
= l
.s
<= r
.s
; break;
421 case Cislt
: x
= l
.s
< r
.s
; break;
422 case Cisgt
: x
= l
.s
> r
.s
; break;
423 case Cisge
: x
= l
.s
>= r
.s
; break;
424 case Ciugt
: x
= l
.u
> r
.u
; break;
425 case Ciuge
: x
= l
.u
>= r
.u
; break;
426 case Cieq
: x
= l
.u
== r
.u
; break;
427 case Cine
: x
= l
.u
!= r
.u
; break;
428 default: die("unreachable");
431 else if (Ocmps
<= op
&& op
<= Ocmps1
) {
432 switch (op
- Ocmps
) {
433 case Cfle
: x
= l
.fs
<= r
.fs
; break;
434 case Cflt
: x
= l
.fs
< r
.fs
; break;
435 case Cfgt
: x
= l
.fs
> r
.fs
; break;
436 case Cfge
: x
= l
.fs
>= r
.fs
; break;
437 case Cfne
: x
= l
.fs
!= r
.fs
; break;
438 case Cfeq
: x
= l
.fs
== r
.fs
; break;
439 case Cfo
: x
= l
.fs
< r
.fs
|| l
.fs
>= r
.fs
; break;
440 case Cfuo
: x
= !(l
.fs
< r
.fs
|| l
.fs
>= r
.fs
); break;
441 default: die("unreachable");
444 else if (Ocmpd
<= op
&& op
<= Ocmpd1
) {
445 switch (op
- Ocmpd
) {
446 case Cfle
: x
= l
.fd
<= r
.fd
; break;
447 case Cflt
: x
= l
.fd
< r
.fd
; break;
448 case Cfgt
: x
= l
.fd
> r
.fd
; break;
449 case Cfge
: x
= l
.fd
>= r
.fd
; break;
450 case Cfne
: x
= l
.fd
!= r
.fd
; break;
451 case Cfeq
: x
= l
.fd
== r
.fd
; break;
452 case Cfo
: x
= l
.fd
< r
.fd
|| l
.fd
>= r
.fd
; break;
453 case Cfuo
: x
= !(l
.fd
< r
.fd
|| l
.fd
>= r
.fd
); break;
454 default: die("unreachable");
460 *res
= (Con
){.type
=typ
, .label
=lab
, .bits
={.i
=x
}};
465 foldflt(Con
*res
, int op
, int w
, Con
*cl
, Con
*cr
)
470 if (cl
->type
!= CBits
|| cr
->type
!= CBits
)
471 err("invalid address operand for '%s'", optab
[op
].name
);
472 *res
= (Con
){.type
= CBits
};
473 memset(&res
->bits
, 0, sizeof(res
->bits
));
478 case Oadd
: xd
= ld
+ rd
; break;
479 case Osub
: xd
= ld
- rd
; break;
480 case Oneg
: xd
= -ld
; break;
481 case Odiv
: xd
= ld
/ rd
; break;
482 case Omul
: xd
= ld
* rd
; break;
483 case Oswtof
: xd
= (int32_t)cl
->bits
.i
; break;
484 case Ouwtof
: xd
= (uint32_t)cl
->bits
.i
; break;
485 case Osltof
: xd
= (int64_t)cl
->bits
.i
; break;
486 case Oultof
: xd
= (uint64_t)cl
->bits
.i
; break;
487 case Oexts
: xd
= cl
->bits
.s
; break;
488 case Ocast
: xd
= ld
; break;
489 default: die("unreachable");
497 case Oadd
: xs
= ls
+ rs
; break;
498 case Osub
: xs
= ls
- rs
; break;
499 case Oneg
: xs
= -ls
; break;
500 case Odiv
: xs
= ls
/ rs
; break;
501 case Omul
: xs
= ls
* rs
; break;
502 case Oswtof
: xs
= (int32_t)cl
->bits
.i
; break;
503 case Ouwtof
: xs
= (uint32_t)cl
->bits
.i
; break;
504 case Osltof
: xs
= (int64_t)cl
->bits
.i
; break;
505 case Oultof
: xs
= (uint64_t)cl
->bits
.i
; break;
506 case Otruncd
: xs
= cl
->bits
.d
; break;
507 case Ocast
: xs
= ls
; break;
508 default: die("unreachable");
516 opfold(int op
, int cls
, Con
*cl
, Con
*cr
, Fn
*fn
)
521 if (cls
== Kw
|| cls
== Kl
) {
522 if (foldint(&c
, op
, cls
== Kl
, cl
, cr
))
525 foldflt(&c
, op
, cls
== Kd
, cl
, cr
);
527 assert(!(cls
== Ks
|| cls
== Kd
) || c
.flt
);