recognize some phis as copies
[qbe.git] / load.c
blob0d7944c42222b60a8ce5b402d6209d2b9d3c1ef2
1 #include "all.h"
3 #define MASK(w) (BIT(8*(w)-1)*2-1) /* must work when w==8 */
5 typedef struct Loc Loc;
6 typedef struct Slice Slice;
7 typedef struct Insert Insert;
10 struct Loc {
11 enum {
12 LRoot, /* right above the original load */
13 LLoad, /* inserting a load is allowed */
14 LNoLoad, /* only scalar operations allowed */
15 } type;
16 uint off;
17 Blk *blk;
20 struct Slice {
21 Ref ref;
22 short sz;
23 short cls; /* load class */
26 struct Insert {
27 uint isphi:1;
28 uint num:31;
29 uint bid;
30 uint off;
31 union {
32 Ins ins;
33 struct {
34 Slice m;
35 Phi *p;
36 } phi;
37 } new;
40 static Fn *curf;
41 static uint inum; /* current insertion number */
42 static Insert *ilog; /* global insertion log */
43 static uint nlog; /* number of entries in the log */
45 int
46 loadsz(Ins *l)
48 switch (l->op) {
49 case Oloadsb: case Oloadub: return 1;
50 case Oloadsh: case Oloaduh: return 2;
51 case Oloadsw: case Oloaduw: return 4;
52 case Oload: return KWIDE(l->cls) ? 8 : 4;
54 die("unreachable");
57 int
58 storesz(Ins *s)
60 switch (s->op) {
61 case Ostoreb: return 1;
62 case Ostoreh: return 2;
63 case Ostorew: case Ostores: return 4;
64 case Ostorel: case Ostored: return 8;
66 die("unreachable");
69 static Ref
70 iins(int cls, int op, Ref a0, Ref a1, Loc *l)
72 Insert *ist;
74 vgrow(&ilog, ++nlog);
75 ist = &ilog[nlog-1];
76 ist->isphi = 0;
77 ist->num = inum++;
78 ist->bid = l->blk->id;
79 ist->off = l->off;
80 ist->new.ins = (Ins){op, cls, R, {a0, a1}};
81 return ist->new.ins.to = newtmp("ld", cls, curf);
84 static void
85 cast(Ref *r, int cls, Loc *l)
87 int cls0;
89 if (rtype(*r) == RCon)
90 return;
91 assert(rtype(*r) == RTmp);
92 cls0 = curf->tmp[r->val].cls;
93 if (cls0 == cls || (cls == Kw && cls0 == Kl))
94 return;
95 if (KWIDE(cls0) < KWIDE(cls)) {
96 if (cls0 == Ks)
97 *r = iins(Kw, Ocast, *r, R, l);
98 *r = iins(Kl, Oextuw, *r, R, l);
99 if (cls == Kd)
100 *r = iins(Kd, Ocast, *r, R, l);
101 } else {
102 if (cls0 == Kd && cls != Kl)
103 *r = iins(Kl, Ocast, *r, R, l);
104 if (cls0 != Kd || cls != Kw)
105 *r = iins(cls, Ocast, *r, R, l);
109 static inline void
110 mask(int cls, Ref *r, bits msk, Loc *l)
112 cast(r, cls, l);
113 *r = iins(cls, Oand, *r, getcon(msk, curf), l);
116 static Ref
117 load(Slice sl, bits msk, Loc *l)
119 Alias *a;
120 Ref r, r1;
121 int ld, cls, all;
122 Con c;
124 ld = (int[]){
125 [1] = Oloadub,
126 [2] = Oloaduh,
127 [4] = Oloaduw,
128 [8] = Oload
129 }[sl.sz];
130 all = msk == MASK(sl.sz);
131 if (all)
132 cls = sl.cls;
133 else
134 cls = sl.sz > 4 ? Kl : Kw;
135 r = sl.ref;
136 /* sl.ref might not be live here,
137 * but its alias base ref will be
138 * (see killsl() below) */
139 if (rtype(r) == RTmp) {
140 a = &curf->tmp[r.val].alias;
141 switch (a->type) {
142 default:
143 die("unreachable");
144 case ALoc:
145 case AEsc:
146 case AUnk:
147 r = TMP(a->base);
148 if (!a->offset)
149 break;
150 r1 = getcon(a->offset, curf);
151 r = iins(Kl, Oadd, r, r1, l);
152 break;
153 case ACon:
154 case ASym:
155 c = curf->con[a->u.sym.con];
156 c.bits.i = a->offset;
157 r = newcon(&c, curf);
158 break;
161 r = iins(cls, ld, r, R, l);
162 if (!all)
163 mask(cls, &r, msk, l);
164 return r;
167 static int
168 killsl(Ref r, Slice sl)
170 Alias *a;
172 if (rtype(sl.ref) != RTmp)
173 return 0;
174 a = &curf->tmp[sl.ref.val].alias;
175 switch (a->type) {
176 default: die("unreachable");
177 case ALoc:
178 case AEsc:
179 case AUnk: return req(TMP(a->base), r);
180 case ACon:
181 case ASym: return 0;
185 /* returns a ref containing the contents of the slice
186 * passed as argument, all the bits set to 0 in the
187 * mask argument are zeroed in the result;
188 * the returned ref has an integer class when the
189 * mask does not cover all the bits of the slice,
190 * otherwise, it has class sl.cls
191 * the procedure returns R when it fails */
192 static Ref
193 def(Slice sl, bits msk, Blk *b, Ins *i, Loc *il)
195 Blk *bp;
196 bits msk1, msks;
197 int off, cls, cls1, op, sz, ld;
198 uint np, oldl, oldt;
199 Ref r, r1;
200 Phi *p;
201 Insert *ist;
202 Loc l;
204 /* invariants:
205 * -1- b dominates il->blk; so we can use
206 * temporaries of b in il->blk
207 * -2- if il->type != LNoLoad, then il->blk
208 * postdominates the original load; so it
209 * is safe to load in il->blk
210 * -3- if il->type != LNoLoad, then b
211 * postdominates il->blk (and by 2, the
212 * original load)
214 assert(dom(b, il->blk));
215 oldl = nlog;
216 oldt = curf->ntmp;
217 if (0) {
218 Load:
219 curf->ntmp = oldt;
220 nlog = oldl;
221 if (il->type != LLoad)
222 return R;
223 return load(sl, msk, il);
226 if (!i)
227 i = &b->ins[b->nins];
228 cls = sl.sz > 4 ? Kl : Kw;
229 msks = MASK(sl.sz);
231 while (i > b->ins) {
232 --i;
233 if (killsl(i->to, sl)
234 || (i->op == Ocall && escapes(sl.ref, curf)))
235 goto Load;
236 ld = isload(i->op);
237 if (ld) {
238 sz = loadsz(i);
239 r1 = i->arg[0];
240 r = i->to;
241 } else if (isstore(i->op)) {
242 sz = storesz(i);
243 r1 = i->arg[1];
244 r = i->arg[0];
245 } else
246 continue;
247 switch (alias(sl.ref, sl.sz, r1, sz, &off, curf)) {
248 case MustAlias:
249 if (off < 0) {
250 off = -off;
251 msk1 = (MASK(sz) << 8*off) & msks;
252 op = Oshl;
253 } else {
254 msk1 = (MASK(sz) >> 8*off) & msks;
255 op = Oshr;
257 if ((msk1 & msk) == 0)
258 break;
259 if (off) {
260 cls1 = cls;
261 if (op == Oshr && off + sl.sz > 4)
262 cls1 = Kl;
263 cast(&r, cls1, il);
264 r1 = getcon(8*off, curf);
265 r = iins(cls1, op, r, r1, il);
267 if ((msk1 & msk) != msk1 || off + sz < sl.sz)
268 mask(cls, &r, msk1 & msk, il);
269 if ((msk & ~msk1) != 0) {
270 r1 = def(sl, msk & ~msk1, b, i, il);
271 if (req(r1, R))
272 goto Load;
273 r = iins(cls, Oor, r, r1, il);
275 if (msk == msks)
276 cast(&r, sl.cls, il);
277 return r;
278 case MayAlias:
279 if (ld)
280 break;
281 else
282 goto Load;
283 case NoAlias:
284 break;
285 default:
286 die("unreachable");
290 for (ist=ilog; ist<&ilog[nlog]; ++ist)
291 if (ist->isphi && ist->bid == b->id)
292 if (req(ist->new.phi.m.ref, sl.ref))
293 if (ist->new.phi.m.sz == sl.sz) {
294 r = ist->new.phi.p->to;
295 if (msk != msks)
296 mask(cls, &r, msk, il);
297 else
298 cast(&r, sl.cls, il);
299 return r;
302 for (p=b->phi; p; p=p->link)
303 if (killsl(p->to, sl))
304 /* scanning predecessors in that
305 * case would be unsafe */
306 goto Load;
308 if (b->npred == 0)
309 goto Load;
310 if (b->npred == 1) {
311 bp = b->pred[0];
312 assert(bp->loop >= il->blk->loop);
313 l = *il;
314 if (bp->s2)
315 l.type = LNoLoad;
316 r1 = def(sl, msk, bp, 0, &l);
317 if (req(r1, R))
318 goto Load;
319 return r1;
322 r = newtmp("ld", sl.cls, curf);
323 p = alloc(sizeof *p);
324 vgrow(&ilog, ++nlog);
325 ist = &ilog[nlog-1];
326 ist->isphi = 1;
327 ist->bid = b->id;
328 ist->new.phi.m = sl;
329 ist->new.phi.p = p;
330 p->to = r;
331 p->cls = sl.cls;
332 p->narg = b->npred;
333 p->arg = vnew(p->narg, sizeof p->arg[0], PFn);
334 p->blk = vnew(p->narg, sizeof p->blk[0], PFn);
335 for (np=0; np<b->npred; ++np) {
336 bp = b->pred[np];
337 if (!bp->s2
338 && il->type != LNoLoad
339 && bp->loop < il->blk->loop)
340 l.type = LLoad;
341 else
342 l.type = LNoLoad;
343 l.blk = bp;
344 l.off = bp->nins;
345 r1 = def(sl, msks, bp, 0, &l);
346 if (req(r1, R))
347 goto Load;
348 p->arg[np] = r1;
349 p->blk[np] = bp;
351 if (msk != msks)
352 mask(cls, &r, msk, il);
353 return r;
356 static int
357 icmp(const void *pa, const void *pb)
359 Insert *a, *b;
360 int c;
362 a = (Insert *)pa;
363 b = (Insert *)pb;
364 if ((c = a->bid - b->bid))
365 return c;
366 if (a->isphi && b->isphi)
367 return 0;
368 if (a->isphi)
369 return -1;
370 if (b->isphi)
371 return +1;
372 if ((c = a->off - b->off))
373 return c;
374 return a->num - b->num;
377 /* require rpo ssa alias */
378 void
379 loadopt(Fn *fn)
381 Ins *i, *ib;
382 Blk *b;
383 int sz;
384 uint n, ni, ext, nt;
385 Insert *ist;
386 Slice sl;
387 Loc l;
389 curf = fn;
390 ilog = vnew(0, sizeof ilog[0], PHeap);
391 nlog = 0;
392 inum = 0;
393 for (b=fn->start; b; b=b->link)
394 for (i=b->ins; i<&b->ins[b->nins]; ++i) {
395 if (!isload(i->op))
396 continue;
397 sz = loadsz(i);
398 sl = (Slice){i->arg[0], sz, i->cls};
399 l = (Loc){LRoot, i-b->ins, b};
400 i->arg[1] = def(sl, MASK(sz), b, i, &l);
402 qsort(ilog, nlog, sizeof ilog[0], icmp);
403 vgrow(&ilog, nlog+1);
404 ilog[nlog].bid = fn->nblk; /* add a sentinel */
405 ib = vnew(0, sizeof(Ins), PHeap);
406 for (ist=ilog, n=0; n<fn->nblk; ++n) {
407 b = fn->rpo[n];
408 for (; ist->bid == n && ist->isphi; ++ist) {
409 ist->new.phi.p->link = b->phi;
410 b->phi = ist->new.phi.p;
412 ni = 0;
413 nt = 0;
414 for (;;) {
415 if (ist->bid == n && ist->off == ni)
416 i = &ist++->new.ins;
417 else {
418 if (ni == b->nins)
419 break;
420 i = &b->ins[ni++];
421 if (isload(i->op)
422 && !req(i->arg[1], R)) {
423 ext = Oextsb + i->op - Oloadsb;
424 switch (i->op) {
425 default:
426 die("unreachable");
427 case Oloadsb:
428 case Oloadub:
429 case Oloadsh:
430 case Oloaduh:
431 i->op = ext;
432 break;
433 case Oloadsw:
434 case Oloaduw:
435 if (i->cls == Kl) {
436 i->op = ext;
437 break;
439 /* fall through */
440 case Oload:
441 i->op = Ocopy;
442 break;
444 i->arg[0] = i->arg[1];
445 i->arg[1] = R;
448 vgrow(&ib, ++nt);
449 ib[nt-1] = *i;
451 b->nins = nt;
452 idup(&b->ins, ib, nt);
454 vfree(ib);
455 vfree(ilog);
456 if (debug['M']) {
457 fprintf(stderr, "\n> After load elimination:\n");
458 printfn(fn, stderr);