amd64/isel: Error if alloc size doesn't fit in Tmp slot type
[qbe.git] / amd64 / isel.c
blob22e1e142bc212db617c19426976d67498e8f5481
1 #include "all.h"
2 #include <limits.h>
4 /* For x86_64, do the following:
6 * - check that constants are used only in
7 * places allowed
8 * - ensure immediates always fit in 32b
9 * - expose machine register contraints
10 * on instructions like division.
11 * - implement fast locals (the streak of
12 * constant allocX in the first basic block)
13 * - recognize complex addressing modes
15 * Invariant: the use counts that are used
16 * in sel() must be sound. This
17 * is not so trivial, maybe the
18 * dce should be moved out...
21 typedef struct ANum ANum;
23 struct ANum {
24 char n, l, r;
25 Ins *i;
28 static int amatch(Addr *, Ref, int, ANum *, Fn *);
30 static int
31 noimm(Ref r, Fn *fn)
33 int64_t val;
35 if (rtype(r) != RCon)
36 return 0;
37 switch (fn->con[r.val].type) {
38 case CAddr:
39 /* we only support the 'small'
40 * code model of the ABI, this
41 * means that we can always
42 * address data with 32bits
44 return 0;
45 case CBits:
46 val = fn->con[r.val].bits.i;
47 return (val < INT32_MIN || val > INT32_MAX);
48 default:
49 die("invalid constant");
53 static int
54 rslot(Ref r, Fn *fn)
56 if (rtype(r) != RTmp)
57 return -1;
58 return fn->tmp[r.val].slot;
61 static void
62 fixarg(Ref *r, int k, int op, Fn *fn)
64 char buf[32];
65 Addr a, *m;
66 Ref r0, r1;
67 int s, n, cpy, mem;
69 r1 = r0 = *r;
70 s = rslot(r0, fn);
71 cpy = op == Ocopy || op == -1;
72 mem = isstore(op) || isload(op) || op == Ocall;
73 if (KBASE(k) == 1 && rtype(r0) == RCon) {
74 /* load floating points from memory
75 * slots, they can't be used as
76 * immediates
78 r1 = MEM(fn->nmem);
79 vgrow(&fn->mem, ++fn->nmem);
80 memset(&a, 0, sizeof a);
81 a.offset.type = CAddr;
82 a.offset.local = 1;
83 n = gasstash(&fn->con[r0.val].bits, KWIDE(k) ? 8 : 4);
84 sprintf(buf, "fp%d", n);
85 a.offset.label = intern(buf);
86 fn->mem[fn->nmem-1] = a;
88 else if (!cpy && k == Kl && noimm(r0, fn)) {
89 /* load constants that do not fit in
90 * a 32bit signed integer into a
91 * long temporary
93 r1 = newtmp("isel", Kl, fn);
94 emit(Ocopy, Kl, r1, r0, R);
96 else if (s != -1) {
97 /* load fast locals' addresses into
98 * temporaries right before the
99 * instruction
101 r1 = newtmp("isel", Kl, fn);
102 emit(Oaddr, Kl, r1, SLOT(s), R);
104 else if (!mem && rtype(r0) == RCon
105 && fn->con[r0.val].type == CAddr) {
106 /* apple as does not support 32-bit
107 * absolute addressing, use a rip-
108 * relative leaq instead
110 r1 = newtmp("isel", Kl, fn);
111 emit(Oaddr, Kl, r1, r0, R);
113 else if (rtype(r0) == RMem) {
114 /* eliminate memory operands of
115 * the form $foo(%rip, ...)
117 m = &fn->mem[r0.val];
118 if (req(m->base, R))
119 if (m->offset.type == CAddr) {
120 n = fn->ncon;
121 vgrow(&fn->con, ++fn->ncon);
122 fn->con[n] = m->offset;
123 m->offset.type = CUndef;
124 r0 = newtmp("isel", Kl, fn);
125 emit(Oaddr, Kl, r0, CON(n), R);
126 m->base = r0;
129 *r = r1;
132 static void
133 seladdr(Ref *r, ANum *an, Fn *fn)
135 Addr a;
136 Ref r0;
138 r0 = *r;
139 if (rtype(r0) == RTmp) {
140 memset(&a, 0, sizeof a);
141 if (!amatch(&a, r0, an[r0.val].n, an, fn))
142 return;
143 if (!req(a.base, R))
144 if (a.offset.type == CAddr) {
145 /* apple as does not support
146 * $foo(%r0, %r1, M); try to
147 * rewrite it or bail out if
148 * impossible
150 if (!req(a.index, R))
151 return;
152 else {
153 a.index = a.base;
154 a.scale = 1;
155 a.base = R;
158 chuse(r0, -1, fn);
159 vgrow(&fn->mem, ++fn->nmem);
160 fn->mem[fn->nmem-1] = a;
161 chuse(a.base, +1, fn);
162 chuse(a.index, +1, fn);
163 *r = MEM(fn->nmem-1);
167 static int
168 selcmp(Ref arg[2], int k, Fn *fn)
170 int swap;
171 Ref r, *iarg;
173 swap = rtype(arg[0]) == RCon;
174 if (swap) {
175 r = arg[1];
176 arg[1] = arg[0];
177 arg[0] = r;
179 emit(Oxcmp, k, R, arg[1], arg[0]);
180 iarg = curi->arg;
181 if (rtype(arg[0]) == RCon) {
182 assert(k == Kl);
183 iarg[1] = newtmp("isel", k, fn);
184 emit(Ocopy, k, iarg[1], arg[0], R);
186 fixarg(&iarg[0], k, Oxcmp, fn);
187 fixarg(&iarg[1], k, Oxcmp, fn);
188 return swap;
191 static void
192 sel(Ins i, ANum *an, Fn *fn)
194 Ref r0, r1, *iarg;
195 int x, k, kc;
196 int64_t sz;
197 Ins *i0, *i1;
199 if (rtype(i.to) == RTmp)
200 if (!isreg(i.to) && !isreg(i.arg[0]) && !isreg(i.arg[1]))
201 if (fn->tmp[i.to.val].nuse == 0) {
202 chuse(i.arg[0], -1, fn);
203 chuse(i.arg[1], -1, fn);
204 return;
206 i0 = curi;
207 k = i.cls;
208 switch (i.op) {
209 case Odiv:
210 case Orem:
211 case Oudiv:
212 case Ourem:
213 if (KBASE(k) == 1)
214 goto Emit;
215 if (i.op == Odiv || i.op == Oudiv)
216 r0 = TMP(RAX), r1 = TMP(RDX);
217 else
218 r0 = TMP(RDX), r1 = TMP(RAX);
219 emit(Ocopy, k, i.to, r0, R);
220 emit(Ocopy, k, R, r1, R);
221 if (rtype(i.arg[1]) == RCon) {
222 /* immediates not allowed for
223 * divisions in x86
225 r0 = newtmp("isel", k, fn);
226 } else
227 r0 = i.arg[1];
228 if (fn->tmp[r0.val].slot != -1)
229 err("unlikely argument %%%s in %s",
230 fn->tmp[r0.val].name, optab[i.op].name);
231 if (i.op == Odiv || i.op == Orem) {
232 emit(Oxidiv, k, R, r0, R);
233 emit(Osign, k, TMP(RDX), TMP(RAX), R);
234 } else {
235 emit(Oxdiv, k, R, r0, R);
236 emit(Ocopy, k, TMP(RDX), CON_Z, R);
238 emit(Ocopy, k, TMP(RAX), i.arg[0], R);
239 fixarg(&curi->arg[0], k, Ocopy, fn);
240 if (rtype(i.arg[1]) == RCon)
241 emit(Ocopy, k, r0, i.arg[1], R);
242 break;
243 case Osar:
244 case Oshr:
245 case Oshl:
246 if (rtype(i.arg[1]) == RCon)
247 goto Emit;
248 r0 = i.arg[1];
249 i.arg[1] = TMP(RCX);
250 emit(Ocopy, Kw, R, TMP(RCX), R);
251 emiti(i);
252 emit(Ocopy, Kw, TMP(RCX), r0, R);
253 break;
254 case Onop:
255 break;
256 case Ostored:
257 case Ostores:
258 case Ostorel:
259 case Ostorew:
260 case Ostoreh:
261 case Ostoreb:
262 if (rtype(i.arg[0]) == RCon) {
263 if (i.op == Ostored)
264 i.op = Ostorel;
265 if (i.op == Ostores)
266 i.op = Ostorew;
268 seladdr(&i.arg[1], an, fn);
269 goto Emit;
270 case_Oload:
271 seladdr(&i.arg[0], an, fn);
272 goto Emit;
273 case Ocall:
274 case Osalloc:
275 case Ocopy:
276 case Oadd:
277 case Osub:
278 case Omul:
279 case Oand:
280 case Oor:
281 case Oxor:
282 case Oxtest:
283 case Ostosi:
284 case Odtosi:
285 case Oswtof:
286 case Osltof:
287 case Oexts:
288 case Otruncd:
289 case Ocast:
290 case_OExt:
291 Emit:
292 emiti(i);
293 iarg = curi->arg; /* fixarg() can change curi */
294 fixarg(&iarg[0], argcls(&i, 0), i.op, fn);
295 fixarg(&iarg[1], argcls(&i, 1), i.op, fn);
296 break;
297 case Oalloc:
298 case Oalloc+1:
299 case Oalloc+2: /* == Oalloc1 */
300 /* we need to make sure
301 * the stack remains aligned
302 * (rsp = 0) mod 16
304 fn->dynalloc = 1;
305 if (rtype(i.arg[0]) == RCon) {
306 sz = fn->con[i.arg[0].val].bits.i;
307 if (sz < 0 || sz >= INT_MAX-15)
308 err("invalid alloc size %"PRId64, sz);
309 sz = (sz + 15) & -16;
310 emit(Osalloc, Kl, i.to, getcon(sz, fn), R);
311 } else {
312 /* r0 = (i.arg[0] + 15) & -16 */
313 r0 = newtmp("isel", Kl, fn);
314 r1 = newtmp("isel", Kl, fn);
315 emit(Osalloc, Kl, i.to, r0, R);
316 emit(Oand, Kl, r0, r1, getcon(-16, fn));
317 emit(Oadd, Kl, r1, i.arg[0], getcon(15, fn));
318 if (fn->tmp[i.arg[0].val].slot != -1)
319 err("unlikely argument %%%s in %s",
320 fn->tmp[i.arg[0].val].name, optab[i.op].name);
322 break;
323 default:
324 if (isext(i.op))
325 goto case_OExt;
326 if (isload(i.op))
327 goto case_Oload;
328 if (iscmp(i.op, &kc, &x)) {
329 emit(Oflag+x, k, i.to, R, R);
330 i1 = curi;
331 if (selcmp(i.arg, kc, fn))
332 i1->op = Oflag + cmpop(x);
333 break;
335 die("unknown instruction %s", optab[i.op].name);
338 while (i0 > curi && --i0) {
339 assert(rslot(i0->arg[0], fn) == -1);
340 assert(rslot(i0->arg[1], fn) == -1);
344 static Ins *
345 flagi(Ins *i0, Ins *i)
347 while (i>i0) {
348 i--;
349 if (amd64_op[i->op].zflag)
350 return i;
351 if (amd64_op[i->op].lflag)
352 continue;
353 return 0;
355 return 0;
358 static void
359 seljmp(Blk *b, Fn *fn)
361 Ref r;
362 int c, k;
363 Ins *fi;
364 Tmp *t;
366 if (b->jmp.type == Jret0 || b->jmp.type == Jjmp)
367 return;
368 assert(b->jmp.type == Jjnz);
369 r = b->jmp.arg;
370 t = &fn->tmp[r.val];
371 b->jmp.arg = R;
372 assert(!req(r, R) && rtype(r) != RCon);
373 if (b->s1 == b->s2) {
374 chuse(r, -1, fn);
375 b->jmp.type = Jjmp;
376 b->s2 = 0;
377 return;
379 fi = flagi(b->ins, &b->ins[b->nins]);
380 if (!fi || !req(fi->to, r)) {
381 selcmp((Ref[2]){r, CON_Z}, Kw, fn); /* todo, long jnz */
382 b->jmp.type = Jjf + Cine;
384 else if (iscmp(fi->op, &k, &c)) {
385 if (t->nuse == 1) {
386 if (selcmp(fi->arg, k, fn))
387 c = cmpop(c);
388 *fi = (Ins){.op = Onop};
390 b->jmp.type = Jjf + c;
392 else if (fi->op == Oand && t->nuse == 1
393 && (rtype(fi->arg[0]) == RTmp ||
394 rtype(fi->arg[1]) == RTmp)) {
395 fi->op = Oxtest;
396 fi->to = R;
397 b->jmp.type = Jjf + Cine;
398 if (rtype(fi->arg[1]) == RCon) {
399 r = fi->arg[1];
400 fi->arg[1] = fi->arg[0];
401 fi->arg[0] = r;
404 else {
405 /* since flags are not tracked in liveness,
406 * the result of the flag-setting instruction
407 * has to be marked as live
409 if (t->nuse == 1)
410 emit(Ocopy, Kw, R, r, R);
411 b->jmp.type = Jjf + Cine;
415 static int
416 aref(Ref r, ANum *ai)
418 switch (rtype(r)) {
419 case RCon:
420 return 2;
421 case RTmp:
422 return ai[r.val].n;
423 default:
424 die("constant or temporary expected");
428 static int
429 ascale(Ref r, Con *con)
431 int64_t n;
433 if (rtype(r) != RCon)
434 return 0;
435 if (con[r.val].type != CBits)
436 return 0;
437 n = con[r.val].bits.i;
438 return n == 1 || n == 2 || n == 4 || n == 8;
441 static void
442 anumber(ANum *ai, Blk *b, Con *con)
444 /* This should be made obsolete by a proper
445 * reassoc pass.
447 * Rules:
449 * RTmp(_) -> 0 tmp
450 * ( RTmp(_) -> 1 slot )
451 * RCon(_) -> 2 con
452 * 0 * 2 -> 3 s * i (when constant is 1,2,4,8)
454 static char add[10][10] = {
455 [2] [2] = 2, /* folding */
456 [2] [4] = 4, [4] [2] = 4,
457 [2] [6] = 6, [6] [2] = 6,
458 [2] [7] = 7, [7] [2] = 7,
459 [0] [2] = 4, [2] [0] = 4, /* 4: o + b */
460 [0] [0] = 5, /* 5: b + s * i */
461 [0] [3] = 5, [3] [0] = 5,
462 [2] [3] = 6, [3] [2] = 6, /* 6: o + s * i */
463 [2] [5] = 7, [5] [2] = 7, /* 7: o + b + s * i */
464 [0] [6] = 7, [6] [0] = 7,
465 [4] [3] = 7, [3] [4] = 7,
467 int a, a1, a2, n1, n2, t1, t2;
468 Ins *i;
470 for (i=b->ins; i<&b->ins[b->nins]; i++) {
471 if (rtype(i->to) == RTmp)
472 ai[i->to.val].i = i;
473 if (i->op != Oadd && i->op != Omul)
474 continue;
475 a1 = aref(i->arg[0], ai);
476 a2 = aref(i->arg[1], ai);
477 t1 = a1 != 1 && a1 != 2;
478 t2 = a2 != 1 && a2 != 2;
479 if (i->op == Oadd) {
480 a = add[n1 = a1][n2 = a2];
481 if (t1 && a < add[0][a2])
482 a = add[n1 = 0][n2 = a2];
483 if (t2 && a < add[a1][0])
484 a = add[n1 = a1][n2 = 0];
485 if (t1 && t2 && a < add[0][0])
486 a = add[n1 = 0][n2 = 0];
487 } else {
488 n1 = n2 = a = 0;
489 if (ascale(i->arg[0], con) && t2)
490 a = 3, n1 = 2, n2 = 0;
491 if (t1 && ascale(i->arg[1], con))
492 a = 3, n1 = 0, n2 = 2;
494 ai[i->to.val].n = a;
495 ai[i->to.val].l = n1;
496 ai[i->to.val].r = n2;
500 static int
501 amatch(Addr *a, Ref r, int n, ANum *ai, Fn *fn)
503 Ins *i;
504 int nl, nr, t, s;
505 Ref al, ar;
507 if (rtype(r) == RCon) {
508 addcon(&a->offset, &fn->con[r.val]);
509 return 1;
511 assert(rtype(r) == RTmp);
512 i = ai[r.val].i;
513 nl = ai[r.val].l;
514 nr = ai[r.val].r;
515 if (i) {
516 if (nl > nr) {
517 al = i->arg[1];
518 ar = i->arg[0];
519 t = nl, nl = nr, nr = t;
520 } else {
521 al = i->arg[0];
522 ar = i->arg[1];
525 switch (n) {
526 case 3: /* s * i */
527 a->index = al;
528 a->scale = fn->con[ar.val].bits.i;
529 return 0;
530 case 5: /* b + s * i */
531 switch (nr) {
532 case 0:
533 if (fn->tmp[ar.val].slot != -1) {
534 al = i->arg[1];
535 ar = i->arg[0];
537 a->index = ar;
538 a->scale = 1;
539 break;
540 case 3:
541 amatch(a, ar, nr, ai, fn);
542 break;
544 r = al;
545 /* fall through */
546 case 0:
547 s = fn->tmp[r.val].slot;
548 if (s != -1)
549 r = SLOT(s);
550 a->base = r;
551 return n || s != -1;
552 case 2: /* constants */
553 case 4: /* o + b */
554 case 6: /* o + s * i */
555 case 7: /* o + b + s * i */
556 amatch(a, ar, nr, ai, fn);
557 amatch(a, al, nl, ai, fn);
558 return 1;
559 default:
560 die("unreachable");
564 /* instruction selection
565 * requires use counts (as given by parsing)
567 void
568 amd64_isel(Fn *fn)
570 Blk *b, **sb;
571 Ins *i;
572 Phi *p;
573 uint a;
574 int n, al;
575 int64_t sz;
576 ANum *ainfo;
578 /* assign slots to fast allocs */
579 b = fn->start;
580 /* specific to NAlign == 3 */ /* or change n=4 and sz /= 4 below */
581 for (al=Oalloc, n=4; al<=Oalloc1; al++, n*=2)
582 for (i=b->ins; i<&b->ins[b->nins]; i++)
583 if (i->op == al) {
584 if (rtype(i->arg[0]) != RCon)
585 break;
586 sz = fn->con[i->arg[0].val].bits.i;
587 if (sz < 0 || sz >= INT_MAX-15)
588 err("invalid alloc size %"PRId64, sz);
589 sz = (sz + n-1) & -n;
590 sz /= 4;
591 if (sz > INT_MAX - fn->slot)
592 die("alloc too large");
593 fn->tmp[i->to.val].slot = fn->slot;
594 fn->slot += sz;
595 *i = (Ins){.op = Onop};
598 /* process basic blocks */
599 n = fn->ntmp;
600 ainfo = emalloc(n * sizeof ainfo[0]);
601 for (b=fn->start; b; b=b->link) {
602 curi = &insb[NIns];
603 for (sb=(Blk*[3]){b->s1, b->s2, 0}; *sb; sb++)
604 for (p=(*sb)->phi; p; p=p->link) {
605 for (a=0; p->blk[a] != b; a++)
606 assert(a+1 < p->narg);
607 fixarg(&p->arg[a], p->cls, -1, fn);
609 memset(ainfo, 0, n * sizeof ainfo[0]);
610 anumber(ainfo, b, fn->con);
611 seljmp(b, fn);
612 for (i=&b->ins[b->nins]; i!=b->ins;)
613 sel(*--i, ainfo, fn);
614 b->nins = &insb[NIns] - curi;
615 idup(&b->ins, curi, b->nins);
617 free(ainfo);
619 if (debug['I']) {
620 fprintf(stderr, "\n> After instruction selection:\n");
621 printfn(fn, stderr);