1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
|
#include "all.h"
#include "../endian.h"
enum flag {
ZF = 1 << 0,
SF = 1 << 1,
CF = 1 << 2,
OF = 1 << 3,
CLOBF = 1 << 4,
};
/* flags read by integer cmp ops */
static const uchar intcmpflags[] = {
[Oequ] = ZF, [Oneq] = ZF,
[Olth] = SF|OF, [Olte] = ZF|SF|OF,
[Ogte] = SF|OF, [Ogth] = ZF|SF|OF,
[Oulth] = CF, [Oulte] = ZF|CF,
[Ougte] = CF, [Ougth] = ZF|CF,
};
static const uchar opflags[] = {
[Oneg] = ZF|CLOBF,
[Oadd] = ZF|CLOBF,
[Osub] = ZF|CLOBF,
[Omul] = CLOBF,
[Oumul] = CLOBF,
[Odiv] = CLOBF,
[Oudiv] = CLOBF,
[Orem] = CLOBF,
[Ourem] = CLOBF,
[Oand] = ZF|CLOBF,
[Oior] = ZF|CLOBF,
[Oxor] = ZF|CLOBF,
[Oshl] = ZF|CLOBF,
[Osar] = ZF|CLOBF,
[Oslr] = ZF|CLOBF,
[Oequ] = ZF|CLOBF,
[Oneq] = ZF|CLOBF,
[Olth] = ZF|CLOBF,
[Ogth] = ZF|CLOBF,
[Olte] = ZF|CLOBF,
[Ogte] = ZF|CLOBF,
[Oulth] = ZF|CLOBF,
[Ougth] = ZF|CLOBF,
[Oulte] = ZF|CLOBF,
[Ougte] = ZF|CLOBF,
[Ocall] = CLOBF,
[Oxinc] = ZF|CLOBF,
[Oxdec] = ZF|CLOBF,
};
static int iflagsrc = -1;
static void
fixarg(union ref *r, struct instr *ins, struct block *blk, int *curi)
{
int sh;
enum op op = ins ? ins->op : 0;
if (r->t == RXCON) {
struct xcon *con = &conht[r->i];
if (in_range(op, Oshl, Oslr)) {
sh = con->i;
goto ShiftImm;
} else if (in_range(op, Oadd, Osub) && con->i == 2147483648) {
/* add X, INT32MAX+1 -> sub X, INT32MIN */
ins->op = Oadd + (op == Oadd);
*r = mkintcon(KI4, -2147483648);
} else if (in_range(op, Ocopy, Omove) && kisflt(con->cls) && con->f == 0) {
/* copy of float zero -> regular zero, that emit() will turn into xor x,x */
*r = mkref(RICON, 0);
} else if (kisflt(con->cls) || con->cls == KI8) {
/* float immediates & >32b immediates are loaded from memory */
uchar data[8];
uint siz = cls2siz[con->cls];
if (con->cls == KI4) wr32le(data, con->i);
else if (con->cls != KF4) wr64le(data, con->i);
else {
union { float f; int i; } pun = { con->f };
wr32le(data, pun.i);
}
*r = mkdatref(NULL, siz, /*align*/siz, data, siz, /*deref*/1);
} else if (in_range(op, Odiv, Ourem) && kisint(ins->cls))
goto DivImm;
} else if (r->t == RICON && in_range(op, Odiv, Ourem) && kisint(ins->cls)) {
DivImm: /* there is no division by immediate, must be copied to a register */
*r = insertinstr(blk, (*curi)++, mkinstr(Ocopy, ins->cls, *r));
} else if (r->t == RICON && in_range(op, Oshl, Oslr)) {
sh = r->i;
ShiftImm: /* shift immediate is always 8bit */
*r = mkref(RICON, sh & 255);
}
}
static void
selcall(struct function *fn, struct instr *ins, struct block *blk, int *curi)
{
const struct call *call = &calltab.p[ins->r.i];
int iarg = *curi - 1;
enum irclass cls;
for (int i = call->narg - 1; i >= 0; --i) {
struct instr *arg;
for (;;) {
assert(i >= 0 && "arg?");
if ((arg = &instrtab[blk->ins.p[iarg--]])->op == Oarg)
break;
}
if (call->abiarg[i].reg >= 0) {
assert(!call->abiarg[i].ty.isagg);
*arg = mkinstr(Omove, call->abiarg[i].ty.cls, mkref(RREG, call->abiarg[i].reg), arg->r);
}
}
cls = ins->cls;
ins->cls = 0;
if (cls) {
/* duplicate to reuse same TMP ref */
insertinstr(blk, (*curi)++, *ins);
*ins = mkinstr(Ocopy, cls, mkref(RREG, call->abiret[0].reg));
if (*curi + 1 < blk->ins.n)
if (instrtab[blk->ins.p[*curi + 1]].op == Ocall2r) {
ins = &instrtab[blk->ins.p[++*curi]];
*ins = mkinstr(Ocopy, ins->cls, mkref(RREG, call->abiret[1].reg));
}
}
}
#define isimm32(r) (concls(r) == KI4)
#define rswap(a,b) do { union ref _t = (a); (a) = (b); (b) = _t; } while (0)
static bool
acon(struct addr *addr, union ref r)
{
vlong a = addr->disp;
assert(isintcon(r));
a += intconval(r);
if ((int)a == a) {
addr->disp = a;
return 1;
}
return 0;
}
static bool
ascale(struct addr *addr, union ref a, union ref b)
{
if (b.t != RICON) return 0;
if (addr->index.t) return 0;
if (a.t != RTMP && a.t != RREG) return 0;
if ((unsigned)b.i > 3) return 0;
addr->shift = b.i;
addr->index = a;
return 1;
}
static bool
aadd(struct addr *addr, union ref r)
{
if (r.t == RTMP) {
struct instr *ins = &instrtab[r.i];
if (ins->op == Oadd) {
if (!aadd(addr, ins->l)) goto Ref;
if (!aadd(addr, ins->r)) goto Ref;
ins->skip = 1;
} else if (ins->op == Oshl) {
if (!ascale(addr, ins->l, ins->r)) goto Ref;
ins->skip = 1;
} else if (ins->op == Ocopy && ins->l.t == RMORE) {
struct addr save = *addr, *addr2 = &addrht[ins->l.i];
if ((!addr2->base.t || aadd(addr, addr2->base))
&& acon(addr, mkintcon(KI4, addr2->disp))
&& (!addr2->index.t || ascale(addr, addr2->index, mkref(RICON, addr2->shift))))
{
ins->skip = 1;
} else {
*addr = save;
goto Ref;
}
} else if (ins->op == Ocopy) {
if (!aadd(addr, ins->l)) goto Ref;
ins->skip = 1;
} else goto Ref;
} else if (isnumcon(r)) {
return acon(addr, r);
} else if (r.t == RREG) {
/* temporaries are single assignment, but register aren't, so they can't be *
* safely hoisted into an address value, unless they have global lifetime */
if (!bstest(mctarg->rglob, r.i)) return 0;
Ref:
if (!addr->base.t) addr->base = r;
else if (!addr->index.t) addr->index = r;
else return 0;
} else return 0;
return 1;
}
static bool
fuseaddr(union ref *r)
{
struct addr addr = { 0 };
if (r->t == RMORE) return 1;
if (r->t == RXCON && (!conht[r->i].cls && !conht[r->i].deref)) return 1;
if (r->t != RTMP) return 0;
if (!aadd(&addr, *r)) return 0;
*r = mkaddr(addr);
return 1;
}
/* is add instruction with this arg a candidate to transform into efective addr? */
static bool
addarg4addrp(union ref r)
{
struct instr *ins = &instrtab[r.i];
if (r.t != RTMP) return 0;
return ins->op == Oshl || (ins->op == Ocopy && ins->l.t == RMORE) || ins->op == Oadd;
}
static void
sel(struct function *fn, struct instr *ins, struct block *blk, int *curi)
{
uint siz, alignlog2;
int t;
struct instr temp = {0};
enum op op = ins->op;
switch (op) {
default: assert(0);
case Onop: break;
case Oalloca1: case Oalloca2: case Oalloca4: case Oalloca8: case Oalloca16:
alignlog2 = ins->op - Oalloca1;
siz = ins->l.i << alignlog2;
fn->stksiz += siz;
fn->stksiz = alignup(fn->stksiz, 1 << alignlog2);
if (fn->stksiz > 1<<24) error(NULL, "'%s' stack frame too big", fn->name);
*ins = mkinstr(Oadd, KPTR, mkref(RREG, RBP), mkref(RICON, -fn->stksiz));
break;
case Oparam:
assert(ins->l.t == RICON && ins->l.i < fn->nabiarg);
if (fn->abiarg[ins->l.i].reg >= 0)
*ins = mkinstr(Ocopy, ins->cls, mkref(RREG, fn->abiarg[ins->l.i].reg));
else /* stack */
*ins = mkinstr(Oadd, KPTR, mkref(RREG, RBP), mkref(RICON, -fn->abiarg[ins->l.i].stk));
break;
case Oarg:
fixarg(&ins->r, ins, blk, curi);
break;
case Ocall:
selcall(fn, ins, blk, curi);
break;
case Ocall2r: assert(0);
case Ointrin:
break;
case Oshl: case Osar: case Oslr:
if (!iscon(ins->r)) {
/* shift amount register is always CL */
insertinstr(blk, (*curi)++, mkinstr(Omove, KI4, mkref(RREG, RCX), ins->r));
ins->r = mkref(RREG, RCX);
}
goto ALU;
case Oequ: case Oneq:
case Olth: case Ogth: case Olte: case Ogte:
case Oulth: case Ougth: case Oulte: case Ougte:
if (iscon(ins->l)) {
/* lth imm, x -> gth x, imm */
if (!in_range(ins->op, Oequ, Oneq))
ins->op = ((op - Olth) ^ 1) + Olth;
rswap(ins->l, ins->r);
}
if (ins->l.t != RTMP && ins->l.t != RREG)
ins->l = insertinstr(blk, (*curi)++, mkinstr(Ocopy, ins->cls, ins->l));
fixarg(&ins->r, ins, blk, curi);
break;
case Odiv: case Oudiv: case Orem: case Ourem:
if (kisflt(ins->cls)) goto ALU;
/* TODO fuse div/rem pair */
/* (I)DIV dividend is always in RDX:RAX, output also in those regs */
insertinstr(blk, (*curi)++, mkinstr(Omove, ins->cls, mkref(RREG, RAX), ins->l));
/* mark RDX as clobbered. sign/zero-extending RAX into RDX is handled in emit() */
insertinstr(blk, (*curi)++, mkinstr(Omove, ins->cls, mkref(RREG, RDX), mkref(RREG, RDX)));
fixarg(&ins->r, ins, blk, curi); /* make sure rhs is memory or reg */
ins->l = mkref(RREG, RAX);
if (op == Orem) ins->op = Odiv;
else if (op == Ourem) ins->op = Oudiv;
insertinstr(blk, (*curi)++, *ins); /* duplicate ins to reuse tmp ref */
*ins = mkinstr(Ocopy, ins->cls, mkref(RREG, op < Orem ? RAX : RDX)); /* get output */
temp = mkinstr(Ocopy, ins->cls, mkref(RREG, op < Orem ? RDX : RAX)); /* clobber other reg*/
insertinstr(blk, ++(*curi), temp);
/* swap instrs so that clobber goes first */
t = blk->ins.p[*curi - 1];
blk->ins.p[*curi - 1] = blk->ins.p[*curi - 0];
blk->ins.p[*curi - 0] = t;
break;
case Osub:
if (ins->r.bits == mkref(RICON, 1).bits) {
/* sub x,1 -> dec x */
ins->op = op = Oxdec;
ins->r = NOREF;
} else if (iscon(ins->l)) {
/* sub imm, x -> sub x, imm; neg x */
struct instr sub = *ins;
rswap(sub.l, sub.r);
ins->op = Oneg;
ins->l = insertinstr(blk, (*curi)++, sub);
ins->r = NOREF;
}
goto ALU;
case Oadd:
if (kisint(ins->cls) && (addarg4addrp(ins->l) || addarg4addrp(ins->r))) {
temp.op = Ocopy;
temp.cls = ins->cls;
temp.l = mkref(RTMP, ins - instrtab);
if (fuseaddr(&temp.l)) {
*ins = temp;
break;
}
} else if (ins->l.bits == mkref(RICON, 1).bits) {
/* add 1,x -> inc x */
ins->op = op = Oxinc;
ins->l = ins->r;
ins->r = NOREF;
goto ALU;
} else if (ins->r.bits == mkref(RICON, 1).bits) {
/* add x,1 -> inc x */
ins->op = op = Oxinc;
ins->r = NOREF;
goto ALU;
}
/* fallthru */
case Omul: case Oumul:
case Oand: case Oxor: case Oior:
/* commutative ops */
if (iscon(ins->l))
rswap(ins->l, ins->r);
case Oneg: case Onot:
case Oexts1: case Oextu1: case Oexts2: case Oextu2: case Oexts4: case Oextu4:
ALU:
if (!(op == Oadd && kisint(ins->cls))) /* 3-address add is lea */
if (!(in_range(op, Omul, Oumul) && kisint(ins->cls) && isimm32(ins->r))) /* for (I)MUL r,r/m,imm */
ins->inplace = 1;
if (ins->l.t != RTMP && ins->l.t != RREG)
ins->l = insertinstr(blk, (*curi)++, mkinstr(Ocopy, ins->cls, ins->l));
if (ins->r.t)
case Omove:
fixarg(&ins->r, ins, blk, curi);
break;
case Oloads1: case Oloadu1: case Oloads2: case Oloadu2:
case Oloads4: case Oloadu4: case Oloadi8: case Oloadf4: case Oloadf8:
if (!fuseaddr(&ins->l) && ins->l.t != RTMP && ins->l.t != RREG)
ins->l = insertinstr(blk, (*curi)++, mkinstr(Ocopy, KPTR, ins->l));
break;
case Ostore1: case Ostore2: case Ostore4: case Ostore8:
if (!fuseaddr(&ins->l) && ins->l.t != RTMP && ins->l.t != RREG)
ins->l = insertinstr(blk, (*curi)++, mkinstr(Ocopy, KPTR, ins->l));
fixarg(&ins->r, ins, blk, curi);
break;
case Ocopy:
fixarg(&ins->l, ins, blk, curi);
break;
}
}
static void
seljmp(struct function *fn, struct block *blk)
{
if (blk->jmp.t == Jb && blk->jmp.arg[0].t) {
union ref c = blk->jmp.arg[0];
if (c.t != RTMP) {
enum irclass cls = c.t == RICON ? KI4 : c.t == RXCON && conht[c.i].cls ? conht[c.i].cls : KPTR;
int curi = blk->ins.n;
c = insertinstr(blk, blk->ins.n, mkinstr(Ocopy, cls, c));
sel(fn, &instrtab[c.i], blk, &curi);
}
if (iflagsrc == c.i) {
if (!oiscmp(instrtab[c.i].op))
blk->jmp.arg[0] = NOREF; /* implicit by zero flag */
} else {
if (!(opflags[instrtab[c.i].op] & ZF) || c.i != blk->ins.p[blk->ins.n - 1]) {
blk->jmp.arg[0] = insertinstr(blk, blk->ins.n, mkinstr(Oneq, instrtab[c.i].cls, c, ZEROREF));
}
}
} else if (blk->jmp.t == Jret) {
if (blk->jmp.arg[0].t) {
int curi;
union ref r = mkref(RREG, fn->abiret[0].reg);
struct instr *ins = &instrtab[insertinstr(blk, blk->ins.n, mkinstr(Omove, fn->abiret[0].ty.cls, r , blk->jmp.arg[0])).i];
curi = blk->ins.n;
fixarg(&ins->r, ins, blk, &curi);
blk->jmp.arg[0] = r;
if (blk->jmp.arg[1].t) {
r = mkref(RREG, fn->abiret[1].reg);
ins = &instrtab[insertinstr(blk, blk->ins.n, mkinstr(Omove, fn->abiret[1].ty.cls, r, blk->jmp.arg[1])).i];
curi = blk->ins.n;
fixarg(&ins->r, ins, blk, &curi);
blk->jmp.arg[1] = r;
}
}
}
}
void
amd64_isel(struct function *fn)
{
struct block *blk = fn->entry;
fn->stksiz = 0;
do {
int i;
for (i = 0; i < blk->phi.n; ++i) {
union ref *phi = phitab.p[instrtab[blk->phi.p[i]].l.i];
for (int i = 0; i < blk->npred; ++i) {
int curi = blkpred(blk, i)->ins.n;
fixarg(&phi[i], NULL, blkpred(blk, i), &curi);
}
}
iflagsrc = -1;
for (i = 0; i < blk->ins.n; ++i) {
struct instr *ins = &instrtab[blk->ins.p[i]];
sel(fn, ins, blk, &i);
if (ins->op < arraylength(opflags) && kisint(insrescls(*ins))) {
if (opflags[ins->op] & ZF) iflagsrc = ins - instrtab;
else if (opflags[ins->op] & CLOBF) iflagsrc = -1;
}
}
seljmp(fn, blk);
} while ((blk = blk->lnext) != fn->entry);
if (ccopt.dbg.i) {
efmt("<< After isel >>\n");
irdump(fn);
}
}
/* vim:set ts=3 sw=3 expandtab: */
|