aboutsummaryrefslogtreecommitdiffhomepage
path: root/ir/builder.c
blob: 1d733146b6c4bbd62cd5bd80aee823db232f24bc (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
#include "ir.h"

/* binary arithmetic builder with peephole optimizations */
union ref
irbinop(struct function *fn, enum op op, enum irclass k, union ref l, union ref r)
{
   static const union ref ONE = {.t=RICON, .i=1};
   vlong iv;
   union ref c;

   if (foldbinop(&c, op, k, l, r))
      return c;

   switch (op) {
   case Oadd:
      if (l.bits == ZEROREF.bits) return r; /* 0 + x ==> x */
      /* fallthru */
   case Osub:
      if (r.bits == ZEROREF.bits) return l; /* x +/- 0 ==> x */
      break;
   case Omul: case Oumul:
      if (isnumcon(l)) rswap(l, r); /* put const in rhs */
      if (r.bits == ZEROREF.bits) /* x * 0 ==> 0 */
         return ZEROREF;
      if (r.bits == ONE.bits) /* x * 1 ==> x */
         return l;
      if (isintcon(r) && ispo2(iv = intconval(r))) {
         /* x * 2^y ==> x << y */
         op = Oshl;
         r = mkintcon(k, ilog2(iv));
      }
      break;
   case Odiv:
      break;
   case Oudiv:
      if (isintcon(r) && ispo2(iv = intconval(r))) {
         /* x / 2^y ==> x >> y */
         op = Oslr;
         r = mkintcon(k, ilog2(iv));
      }
      break;
   case Orem:
      break;
   case Ourem:
      if (isintcon(r) && ispo2(iv = intconval(r))) {
         /* x % 2^y ==> x & 2^y-1 */
         op = Oand;
         r = mkintcon(k, iv - 1);
      }
      break;
   case Oand:
      if (isnumcon(l)) rswap(l, r); /* put const in rhs */
      if (r.bits == ZEROREF.bits) /* x & 0 ==> 0 */
         return ZEROREF;
      break;
   case Oior:
      if (isnumcon(l)) rswap(l, r); /* put const in rhs */
      if (r.bits == ZEROREF.bits) /* x | 0 ==> x */
         return l;
   case Oxor:
      if (isnumcon(l)) rswap(l, r); /* put const in rhs */
      if (r.bits == ZEROREF.bits) /* x ^ 0 ==> x */
         return l;
   case Oshl: case Osar: case Oslr:
      if (r.bits == ZEROREF.bits) /* x shift 0 ==> x */
         return l;
      break;
   case Oequ:
      if (r.bits == l.bits && kisint(k))
         return ONE;
      break;
   case Oneq:
      if (r.bits == l.bits && kisint(k))
         return ZEROREF;
      break;
   case Olth:
      break;
   case Ogth:
      break;
   case Olte:
      break;
   case Ogte:
      break;
   case Oulth:
      break;
   case Ougth:
      break;
   case Oulte:
      break;
   case Ougte:
      break;
   default:
      assert(!"binop?");
   }
   return addinstr(fn, mkinstr(op, k, l, r));
}

union ref
irunop(struct function *fn, enum op op, enum irclass k, union ref a)
{
   union ref c;
   struct instr *ins = NULL;
   if (foldunop(&c, op, k, a))
      return c;
   if (a.t == RTMP) ins = &instrtab[a.i];
   switch (op) {
   case Oneg:
      if (ins && ins->op == Oneg) /* -(-x) ==> x */
         return ins->l;
      break;
   case Onot:
      if (ins && ins->op == Onot) /* ~(~x) ==> x */
         return ins->l;
      break;
   case Ocvtf4s: case Ocvtf4u: case Ocvtf4f8: case Ocvtf8s: 
   case Ocvtf8u: case Ocvtf8f4: case Ocvts4f: case Ocvtu4f: 
   case Ocvts8f: case Ocvtu8f: 
   case Oexts1: case Oextu1: case Oexts2: case Oextu2: 
   case Oexts4: case Oextu4:
   case Ocopy:
      break;
   default: assert(!"unop?");
   }
   return addinstr(fn, mkinstr(op, k, a));
}

int allocinstr(void);

union ref
addinstr(struct function *fn, struct instr ins)
{
   int new = allocinstr();
   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);
}

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
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 = allocinstr();
   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)
{
   assert(fn->curblk);
   adduse(fn->curblk, USERJUMP, r0);
   adduse(fn->curblk, USERJUMP, r1);
   putjump(fn, Jret, r0, r1, NULL, NULL);
}

void
puttrap(struct function *fn)
{
   assert(fn->curblk);
   putjump(fn, Jtrap, NOREF, NOREF, NULL, NULL);
}

#undef putjump

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