3 typedef struct RList RList
;
10 copyof(Ref r
, Ref
*cp
)
19 update(Ref r
, Ref rcp
, Ref
*cp
, RList
***pw
)
23 if (!req(cp
[r
.val
], rcp
)) {
25 l
= emalloc(sizeof *l
);
34 visitphi(Phi
*p
, Ref
*cp
, RList
***pw
)
40 for (a
=0; a
<p
->narg
; a
++) {
41 r1
= copyof(p
->arg
[a
], cp
);
42 if (req(r1
, R
) || req(r1
, p
->to
))
44 if (req(r
, R
) || req(r
, r1
))
51 update(p
->to
, r
, cp
, pw
);
55 iscopy(Ins
*i
, Ref r
, Fn
*fn
)
57 static bits extcpy
[] = {
59 [Wsb
] = BIT(Wsb
) | BIT(Wsh
) | BIT(Wsw
),
60 [Wub
] = BIT(Wub
) | BIT(Wuh
) | BIT(Wuw
),
61 [Wsh
] = BIT(Wsh
) | BIT(Wsw
),
62 [Wuh
] = BIT(Wuh
) | BIT(Wuw
),
71 if (!isext(i
->op
) || rtype(r
) != RTmp
)
73 if (i
->op
== Oextsw
|| i
->op
== Oextuw
)
78 assert(KBASE(t
->cls
) == 0);
79 if (i
->cls
== Kl
&& t
->cls
== Kw
)
82 return (BIT(Wsb
+ (i
->op
-Oextsb
)) & b
) != 0;
86 visitins(Ins
*i
, Ref
*cp
, RList
***pw
, Fn
*fn
)
90 r
= copyof(i
->arg
[0], cp
);
91 if (iscopy(i
, r
, fn
)) {
92 update(i
->to
, r
, cp
, pw
);
93 } else if (!req(i
->to
, R
)) {
94 assert(rtype(i
->to
) == RTmp
);
95 update(i
->to
, i
->to
, cp
, pw
);
100 subst(Ref
*r
, Ref
*cp
)
102 assert((rtype(*r
) != RTmp
|| !req(copyof(*r
, cp
), R
)) && "ssa invariant broken");
120 cp
= emalloc(fn
->ntmp
* sizeof cp
[0]);
121 for (b
=fn
->start
; b
; b
=b
->link
) {
122 for (p
=b
->phi
; p
; p
=p
->link
)
123 visitphi(p
, cp
, &pw
);
124 for (i
=b
->ins
; i
-b
->ins
< b
->nins
; i
++)
125 visitins(i
, cp
, &pw
, fn
);
130 u1
= u
+ fn
->tmp
[t
].nuse
;
134 visitphi(u
->u
.phi
, cp
, &pw
);
137 visitins(u
->u
.ins
, cp
, &pw
, fn
);
142 die("invalid use %d", u
->type
);
147 for (b
=fn
->start
; b
; b
=b
->link
) {
148 for (pp
=&b
->phi
; (p
=*pp
);) {
150 if (!req(r
, p
->to
)) {
154 for (a
=0; a
<p
->narg
; a
++)
155 subst(&p
->arg
[a
], cp
);
158 for (i
=b
->ins
; i
-b
->ins
< b
->nins
; i
++) {
159 r
= copyof(i
->to
, cp
);
160 if (!req(r
, i
->to
)) {
161 *i
= (Ins
){.op
= Onop
};
165 subst(&i
->arg
[a
], cp
);
167 subst(&b
->jmp
.arg
, cp
);
170 fprintf(stderr
, "\n> Copy information:");
171 for (t
=Tmp0
; t
<fn
->ntmp
; t
++) {
173 fprintf(stderr
, "\n%10s not seen!",
176 else if (!req(cp
[t
], TMP(t
))) {
177 fprintf(stderr
, "\n%10s copy of ",
179 printref(cp
[t
], fn
, stderr
);
182 fprintf(stderr
, "\n\n> After copy elimination:\n");