aboutsummaryrefslogtreecommitdiffhomepage
path: root/ir/ir.c
blob: 0132a97759c11910414ea3515e3a9650cd44bdb4 (plain) (blame)
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
#include "ir.h"
#include "../obj/obj.h"

uchar type2cls[NTYPETAG];
uchar cls2siz[KF8+1];
const uchar siz2intcls[] = { [1] = KI4, [2] = KI4, [4] = KI4, [8] = KI8 };

const char *opnames[] = {
   "?\??",
#define _(o,...) #o,
#include "op.def"
#undef _
};

const uchar opnarg[] = {
   0,
#define _(o,n) n,
#include "op.def"
#undef _
};

struct instr instrtab[MAXINSTR];
struct use *instruse[MAXINSTR];
short instrnuse[MAXINSTR];
static struct use instrusebuf[MAXINSTR][2];
int ninstr;
static int instrfreelist;
struct calltab calltab;
struct phitab phitab;
struct dattab dattab;
struct addr addrht[1 << 12];
static int naddrht;
struct xcon conht[1 << 12];
static int nconht;
int visitmark;

void
irinit(struct function *fn)
{
   static struct call callsbuf[64];
   static union ref *phisbuf[64];
   static struct irdat datsbuf[64];

   ninstr = 0;
   instrfreelist = -1;
   vinit(&calltab, callsbuf, arraylength(callsbuf));
   for (int i = 0; i < phitab.n; ++i) xbfree(phitab.p[i]);
   vinit(&phitab, phisbuf, arraylength(phisbuf));
   if (!dattab.p) vinit(&dattab, datsbuf, arraylength(datsbuf));
   if (naddrht >= arraylength(addrht)/2)
      memset(addrht, naddrht = 0, sizeof addrht);
   if (nconht >= arraylength(conht)/2)
      memset(conht, nconht = 0, sizeof conht);
   if (!type2cls[TYINT]) {
      for (int i = TYBOOL; i <= TYUVLONG; ++i) {
         int siz = targ_primsizes[i];
         type2cls[i] = siz < 8 ? KI4 : KI8;
      }
      type2cls[TYFLOAT] = KF4;
      type2cls[TYDOUBLE] = KF8;
      type2cls[TYLDOUBLE] = KF8;
      type2cls[TYPTR] = KPTR;
      type2cls[TYARRAY] = KPTR;
      cls2siz[KI4] = cls2siz[KF4] = 4;
      cls2siz[KI8] = cls2siz[KF8] = 8;
      cls2siz[KPTR] = targ_primsizes[TYPTR];
   }
   fn->entry = fn->curblk = allocz(fn->arena, sizeof(struct block), 0);
   fn->nblk = 1;
   fn->entry->lprev = fn->entry->lnext = fn->entry;
}

static int
addaddr(const struct addr *addr)
{
   uint h = hashb(0, addr, sizeof *addr);
   uint i = h, n = arraylength(addrht);
   for (;; ++i) {
      i &= arraylength(addrht) - 1;
      if (!addrht[i].base.bits && !addrht[i].index.bits) {
         addrht[i] = *addr;
         ++naddrht;
         return i;
      } else if (!memcmp(&addrht[i], addr, sizeof *addr)) {
         return i;
      }
      assert(--n > 0 && "addrht full");
   }
}

static int
addcon(const struct xcon *con)
{
   uint h = hashb(0, con, sizeof *con);
   uint i = h, n = arraylength(conht);
   assert(con->issym || con->isdat || con->cls);
   for (;; ++i) {
      i &= arraylength(conht) - 1;
      if (!conht[i].issym && !conht[i].cls) {
         conht[i] = *con;
         ++nconht;
         return i;
      } else if (!memcmp(&conht[i], con, sizeof *con)) {
         return i;
      }
      assert(--n > 0 && "conht full");
   }
}

union irtype
mkirtype(union type t)
{
   if (t.t == TYVOID || isscalar(t))
      return (union irtype) { .cls = type2cls[scalartypet(t)] };
   assert(isagg(t));
   return (union irtype) { .isagg = 1, .dat = t.dat };
}

union ref
mkintcon(enum irclass k, vlong i)
{
   if (i < 1l << 28 && i >= -(1l << 28)) {
      return mkref(RICON, i);
   } else {
      struct xcon con = { .cls = k, .i = i };
      if (cls2siz[k] == 4) /* check upper half is zero or -1 */
         assert(in_range((i >> 32) + 1, 0, 1));
      return mkref(RXCON, addcon(&con));
   }
}

union ref
mkfltcon(enum irclass k, double f)
{
   struct xcon con = { .cls = k, .f = k == KF4 ? (float) f : f };
   return mkref(RXCON, addcon(&con));
}

union ref
mksymref(const char *s)
{
   struct xcon con = { .issym = 1, .sym = s };
   return mkref(RXCON, addcon(&con));
}

union ref
mkdatref(const char *name, uint siz, uint align, const void *bytes, uint n, bool deref)
{
   struct irdat dat = { .align = align, .siz = siz, .name = name, .section = Srodata };

   assert(n <= siz && siz && align);
   if (!name) {
      extern const char *intern(const char *);
      char buf[32];
      struct wbuf wbuf = MEMBUF(buf, sizeof buf);

      bfmt(&wbuf, ".L.%d", dattab.n);
      ioputc(&wbuf, 0);
      assert(!wbuf.err);
      dat.name = name = intern(buf);
   }
   dat.off = objnewdat(name, dat.section, 0, siz, align);
   memcpy(objout.rodata.p+dat.off, bytes, n);
   memset(objout.rodata.p+dat.off+n, 0, siz - n);
   vpush(&dattab, dat);
   return mkref(RXCON, addcon(&(struct xcon){.isdat = 1, .deref = deref, .dat = dattab.n - 1}));
}

const char *
xcon2sym(int ref)
{
   struct xcon con = conht[ref];
   assert(con.issym ^ con.isdat);
   return con.issym ? con.sym : dattab.p[con.dat].name;
}

struct instr
mkalloca(uint siz, uint align)
{
   struct instr ins = { .cls = KPTR };
   assert(ispo2(align) && align <= 16);
   ins.op = Oalloca1 + ilog2(align);
   ins.l = mkref(RICON, siz/align + (siz%align != 0));
   return ins;
}

union ref
mkcallarg(union irtype ret, uint narg, int vararg)
{
   struct call call = { .ret=ret, .narg=narg, .vararg=vararg };
   assert((long) vararg <= narg);
   vpush(&calltab, call);
   return mkref(RXXX, calltab.n-1);
}

union ref
mkaddr(struct addr addr)
{
   return mkref(RADDR, addaddr(&addr));
}

void
addpred(struct block *blk, struct block *p)
{
   if (blk->npred == 0) {
      blk->_pred0 = p;
      ++blk->npred;
      return;
   }
   if (blk->npred == 1) {
      struct block *p0 = blk->_pred0;
      blk->_pred = 0;
      xbgrow(&blk->_pred, 4);
      *blk->_pred = p0;
   }
   xbpush(&blk->_pred, &blk->npred, p);
}

static void
delpred(struct block *blk, struct block *p)
{
   for (int i = 0; i < blk->npred; ++i) {
      if (blkpred(blk, i) == p) {
         for (int k = i; k < blk->npred - 1; ++k) {
            blkpred(blk, k) = blkpred(blk, k + 1);
         }
         --blk->npred;
         return;
      }
   }
   assert(0&&"blk not in p");
}

struct block *
newblk(struct function *fn)
{
   struct block *blk = alloc(fn->arena, sizeof(struct block), 0);
   memset(blk, 0, sizeof *blk);
   blk->id = -1;
   return blk;
}

void
freeblk(struct function *fn, struct block *blk)
{
   if (blk->npred > 1)
      xbfree(blk->_pred);
   vfree(&blk->phi);
   vfree(&blk->ins);
   if (blk->lnext) blk->lnext->lprev = blk->lprev;
   if (blk->lprev) blk->lprev->lnext = blk->lnext;
   blk->id = 1u<<31;
   --fn->nblk;
}

struct block *
insertblk(struct function *fn, struct block *pred, struct block *subst)
{
   struct block *new = newblk(fn);
   struct block **s = pred->s1 == subst ? &pred->s1 : &pred->s2;
   assert(*s == subst);
   new->lnext = pred->lnext;
   new->lprev = pred;
   pred->lnext->lprev = new;
   pred->lnext = new;
   *s = new;
   new->jmp.t = Jb;
   new->s1 = subst;
   addpred(new, pred);
   for (int i = 0; i < subst->npred; ++i) {
      if (blkpred(subst, i) == pred) {
         blkpred(subst, i) = new;
         ++fn->nblk;
         return new;
      }
   }
   assert(0);
}


static int
newinstr(void)
{
   int t;
   if (instrfreelist != -1) {
      t = instrfreelist;
      memcpy(&instrfreelist, &instrtab[t], sizeof(int));
   } else {
      assert(ninstr < arraylength(instrtab));
      t = ninstr++;
   }
   if (instruse[t] != instrusebuf[t])
      xbfree(instruse[t]);
   instruse[t] = instrusebuf[t];
   instrnuse[t] = 0;
   return t;
}

void
adduse(struct block *ublk, int ui, union ref r) {
   struct use user = { ublk, ui };

   if (r.t != RTMP) return;
   if (instrnuse[r.i] < arraylength(instrusebuf[r.i])) {
      instruse[r.i][instrnuse[r.i]++] = user;
   } else if (instrnuse[r.i] == arraylength(*instrusebuf)) {
      struct use *use = NULL;
      xbgrow(&use, arraylength(*instrusebuf) + 2);
      memcpy(use, instruse[r.i], sizeof(*instrusebuf));
      use[instrnuse[r.i]++] = user;
      instruse[r.i] = use;
   } else {
      xbpush(&instruse[r.i], &instrnuse[r.i], user);
   }
}

bool
deluse(struct block *ublk, int ui, union ref r) {
   if (r.t != RTMP) return 0;

   for (int i = 0; i < instrnuse[r.i]; ++i) {
      if (instruse[r.i][i].blk == ublk && instruse[r.i][i].u == ui) {
         for (int k = i; k < instrnuse[r.i] - 1; ++k) {
            instruse[r.i][k] = instruse[r.i][k+1];
         }
         goto Shrink;
      }
   }
   return 0;

Shrink:
   if (instrnuse[r.i] == arraylength(*instrusebuf) + 1) {
      struct use *use = instruse[r.i];
      instruse[r.i] = instrusebuf[r.i];
      memcpy(instruse[r.i], use, sizeof *instrusebuf);
   }
   --instrnuse[r.i];
   return 1;
}

union ref
insertinstr(struct block *blk, int idx, struct instr ins)
{
   int new = newinstr();

   instrtab[new] = ins;
   if (idx == blk->ins.n) vpush(&blk->ins, new);
   else {
      assert(idx >= 0 && idx < blk->ins.n);
      vpush_((void **)&blk->ins.p, &blk->ins._cap, &blk->ins.n, sizeof *blk->ins.p);
      vresize(&blk->ins, blk->ins.n);
      for (int i = blk->ins.n++; i > idx; --i)
         blk->ins.p[i] = blk->ins.p[i - 1];
      blk->ins.p[idx] = new;
   }
   adduse(blk, new, ins.l);
   adduse(blk, new, ins.r);
   return mkref(RTMP, new);
}

union ref
insertphi(struct block *blk, enum irclass cls)
{
   int new = newinstr();
   union ref *refs = NULL;
   assert(blk->npred > 0);
   xbgrow(&refs, blk->npred);
   memset(refs, 0, blk->npred * sizeof *refs);
   vpush(&phitab, refs);
   instrtab[new] = mkinstr(Ophi, cls, mkref(RXXX, phitab.n - 1));
   vpush(&blk->phi, new);
   return mkref(RTMP, new);
}

void
numberinstrs(struct function *fn)
{
   struct block *blk = fn->entry;
   int start = 0;
   do {
      blk->inumstart = start;
      start += blk->ins.n+2;
   } while ((blk = blk->lnext) != fn->entry);
}

/* require use */
void
replcuses(union ref from, union ref to)
{
   assert(from.t == RTMP);
   for (int i = 0; i < instrnuse[from.i]; ++i) {
      struct use use = instruse[from.i][i];
      union ref *u;
      int n, j;
      if (use.u == from.i) continue;
      if (use.u == USERJUMP) {
         u = &use.blk->jmp.arg[0];
         n = 2;
      } else if (instrtab[use.u].op == Ophi) {
         u = phitab.p[instrtab[use.u].l.i];
         n = use.blk->npred;
      } else {
         u = &instrtab[use.u].l;
         n = 2;
      }
      
      for (j = 0; j < n; ++j) {
         if (u[j].bits == from.bits) {
            u[j].bits = to.bits;
            adduse(use.blk, use.u, to);
            --i;
            break;
         }
      }
   }
}

void
deluses(int ins)
{
   if (instruse[ins] != instrusebuf[ins]) {
      xbfree(instruse[ins]);
      instruse[ins] = instrusebuf[ins];
   }
   instrnuse[ins] = 0;
}

void
delinstr(struct block *blk, int idx)
{
   int t = blk->ins.p[idx];
   assert(idx >= 0 && idx < blk->ins.n);
   memcpy(&instrtab[t], &instrfreelist, sizeof(int));
   instrfreelist = t;
   deluses(t);
   for (int i = idx; i < blk->ins.n - 1; ++i)
      blk->ins.p[i] = blk->ins.p[i + 1];
   --blk->ins.n;
}

void
delphi(struct block *blk, int idx)
{
   int t = blk->phi.p[idx];
   assert(idx >= 0 && idx < blk->phi.n);
   memcpy(&instrtab[t], &instrfreelist, sizeof(int));
   instrfreelist = t;
   deluses(t);
   for (int i = idx; i < blk->phi.n - 1; ++i)
      blk->phi.p[i] = blk->phi.p[i + 1];
   --blk->phi.n;
}

void
delnops(struct block *blk)
{
   int i, n, t;
   /* delete trailing nops */
   while (blk->ins.n > 0 && instrtab[t = blk->ins.p[blk->ins.n - 1]].op == Onop) {
      --blk->ins.n;
      deluses(t);
      memcpy(&instrtab[t], &instrfreelist, sizeof(int));
      instrfreelist = t;
   }
   /* delete rest of nops */
   for (i = blk->ins.n - 2, n = 0; i >= 0; --i) {
      if (instrtab[t = blk->ins.p[i]].op == Onop) {
         deluses(t);
         memcpy(&instrtab[t], &instrfreelist, sizeof(int));
         instrfreelist = t;
         ++n;
      } else if (n) {
         memmove(blk->ins.p+i+1, blk->ins.p+i+1+n, (blk->ins.n - n - i - 1)*sizeof *blk->ins.p);
         blk->ins.n -= n;
         n = 0;
      }
   }
   if (n)
      memmove(blk->ins.p, blk->ins.p + n, (blk->ins.n -= n)*sizeof *blk->ins.p);
}

void
fillblkids(struct function *fn)
{
   int i = 0;
   struct block *blk = fn->entry;
   do blk->id = i++; while ((blk = blk->lnext) != fn->entry);
}

/** IR builders **/

void
useblk(struct function *fn, struct block *blk)
{
   extern int nerror;
   if (fn->curblk && nerror == 0) assert(fn->curblk->jmp.t && "never finished block");
   if (blk) assert(!blk->jmp.t && "reusing built block");
   if (!blk->lprev) { /* initialize */
      blk->lnext = fn->entry;
      blk->lprev = fn->entry->lprev;
      blk->lprev->lnext = blk;
      blk->id = blk->lprev->id + 1;
      ++fn->nblk;
      fn->entry->lprev = blk;
   }
   fn->curblk = blk;
}

union ref
addinstr(struct function *fn, struct instr ins)
{
   int new = newinstr();
   assert(fn->curblk != NULL);
   instrtab[new] = ins;
   adduse(fn->curblk, new, ins.l);
   adduse(fn->curblk, new, ins.r);
   vpush(&fn->curblk->ins, new);
   return mkref(RTMP, new);
}

union ref
addphi(struct function *fn, enum irclass cls, union ref *r)
{
   int new;
   struct instr ins = { Ophi, cls };
   union ref *refs = NULL;

   xbgrow(&refs, fn->curblk->npred);
   memcpy(refs, r, fn->curblk->npred * sizeof *r);
   vpush(&phitab, refs);
   ins.l = mkref(RXXX, phitab.n-1);

   assert(fn->curblk != NULL);
   assert(fn->curblk->ins.n == 0);
   new = newinstr();
   instrtab[new] = ins;
   for (int i = 0; i < fn->curblk->npred; ++i) {
      adduse(fn->curblk, new, r[i]);
   }
   vpush(&fn->curblk->phi, new);
   return mkref(RTMP, new);
}

#define putjump(fn, j, arg0, arg1, T, F) \
   fn->curblk->jmp.t = j;                \
   fn->curblk->jmp.arg[0] = arg0;        \
   fn->curblk->jmp.arg[1] = arg1;        \
   fn->curblk->s1 = T;                   \
   fn->curblk->s2 = F;                   \
   fn->curblk = NULL;

void
putbranch(struct function *fn, struct block *blk)
{
   assert(fn->curblk && blk);
   addpred(blk, fn->curblk);
   putjump(fn, Jb, NOREF, NOREF, blk, NULL);
}

void
putcondbranch(struct function *fn, union ref arg, struct block *t, struct block *f)
{
   assert(fn->curblk && t && f);
   adduse(fn->curblk, USERJUMP, arg);
   addpred(t, fn->curblk);
   addpred(f, fn->curblk);
   putjump(fn, Jb, arg, NOREF, t, f);
}

void
putreturn(struct function *fn, union ref r0, union ref r1)
{
   adduse(fn->curblk, USERJUMP, r0);
   adduse(fn->curblk, USERJUMP, r1);
   putjump(fn, Jret, r0, r1, NULL, NULL);
}

#undef putjump

/** Misc **/

static void
freefn(struct function *fn)
{
   struct block *blk = fn->entry;
   do {
      if (blk->npred > 1) xbfree(blk->_pred);
      vfree(&blk->phi);
      vfree(&blk->ins);
   } while ((blk = blk->lnext) != fn->entry);
}

void
irfini(struct function *fn)
{
   extern int nerror;
   if (nerror) {
      freefn(fn);
      return;
   }

   abi0(fn);
   lowerintrin(fn);
   mem2reg(fn);
   copyopt(fn);
   if (ccopt.dbg.o) {
      efmt("<< Before isel >>\n");
      irdump(fn);
   }
   mctarg->isel(fn);
   regalloc(fn);
   if (!ccopt.dbg.any)
      mctarg->emit(fn);

   freefn(fn);
}

/* vim:set ts=3 sw=3 expandtab: */