ssa.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. #include "all.h"
  2. #include <stdarg.h>
  3. void
  4. adduse(Tmp *tmp, int ty, Blk *b, ...)
  5. {
  6. Use *u;
  7. int n;
  8. va_list ap;
  9. if (!tmp->use)
  10. return;
  11. va_start(ap, b);
  12. n = tmp->nuse;
  13. vgrow(&tmp->use, ++tmp->nuse);
  14. u = &tmp->use[n];
  15. u->type = ty;
  16. u->bid = b->id;
  17. switch (ty) {
  18. case UPhi:
  19. u->u.phi = va_arg(ap, Phi *);
  20. break;
  21. case UIns:
  22. u->u.ins = va_arg(ap, Ins *);
  23. break;
  24. case UJmp:
  25. break;
  26. default:
  27. die("unreachable");
  28. }
  29. va_end(ap);
  30. }
  31. /* fill usage, width, phi, and class information
  32. * must not change .visit fields
  33. */
  34. void
  35. filluse(Fn *fn)
  36. {
  37. Blk *b;
  38. Phi *p;
  39. Ins *i;
  40. int m, t, tp, w, x;
  41. uint a;
  42. Tmp *tmp;
  43. tmp = fn->tmp;
  44. for (t=Tmp0; t<fn->ntmp; t++) {
  45. tmp[t].def = 0;
  46. tmp[t].bid = -1u;
  47. tmp[t].ndef = 0;
  48. tmp[t].nuse = 0;
  49. tmp[t].cls = 0;
  50. tmp[t].phi = 0;
  51. tmp[t].width = WFull;
  52. if (tmp[t].use == 0)
  53. tmp[t].use = vnew(0, sizeof(Use), PFn);
  54. }
  55. for (b=fn->start; b; b=b->link) {
  56. for (p=b->phi; p; p=p->link) {
  57. assert(rtype(p->to) == RTmp);
  58. tp = p->to.val;
  59. tmp[tp].bid = b->id;
  60. tmp[tp].ndef++;
  61. tmp[tp].cls = p->cls;
  62. tp = phicls(tp, fn->tmp);
  63. for (a=0; a<p->narg; a++)
  64. if (rtype(p->arg[a]) == RTmp) {
  65. t = p->arg[a].val;
  66. adduse(&tmp[t], UPhi, b, p);
  67. t = phicls(t, fn->tmp);
  68. if (t != tp)
  69. tmp[t].phi = tp;
  70. }
  71. }
  72. for (i=b->ins; i<&b->ins[b->nins]; i++) {
  73. if (!req(i->to, R)) {
  74. assert(rtype(i->to) == RTmp);
  75. w = WFull;
  76. if (isparbh(i->op))
  77. w = Wsb + (i->op - Oparsb);
  78. if (isload(i->op) && i->op != Oload)
  79. w = Wsb + (i->op - Oloadsb);
  80. if (isext(i->op))
  81. w = Wsb + (i->op - Oextsb);
  82. if (iscmp(i->op, &x, &x))
  83. w = Wub;
  84. if (w == Wsw || w == Wuw)
  85. if (i->cls == Kw)
  86. w = WFull;
  87. t = i->to.val;
  88. tmp[t].width = w;
  89. tmp[t].def = i;
  90. tmp[t].bid = b->id;
  91. tmp[t].ndef++;
  92. tmp[t].cls = i->cls;
  93. }
  94. for (m=0; m<2; m++)
  95. if (rtype(i->arg[m]) == RTmp) {
  96. t = i->arg[m].val;
  97. adduse(&tmp[t], UIns, b, i);
  98. }
  99. }
  100. if (rtype(b->jmp.arg) == RTmp)
  101. adduse(&tmp[b->jmp.arg.val], UJmp, b);
  102. }
  103. }
  104. static Ref
  105. refindex(int t, Fn *fn)
  106. {
  107. return newtmp(fn->tmp[t].name, fn->tmp[t].cls, fn);
  108. }
  109. static void
  110. phiins(Fn *fn)
  111. {
  112. BSet u[1], defs[1];
  113. Blk *a, *b, **blist, **be, **bp;
  114. Ins *i;
  115. Phi *p;
  116. Use *use;
  117. Ref r;
  118. int t, nt, ok;
  119. uint n, defb;
  120. short k;
  121. bsinit(u, fn->nblk);
  122. bsinit(defs, fn->nblk);
  123. blist = emalloc(fn->nblk * sizeof blist[0]);
  124. be = &blist[fn->nblk];
  125. nt = fn->ntmp;
  126. for (t=Tmp0; t<nt; t++) {
  127. fn->tmp[t].visit = 0;
  128. if (fn->tmp[t].phi != 0)
  129. continue;
  130. if (fn->tmp[t].ndef == 1) {
  131. ok = 1;
  132. defb = fn->tmp[t].bid;
  133. use = fn->tmp[t].use;
  134. for (n=fn->tmp[t].nuse; n--; use++)
  135. ok &= use->bid == defb;
  136. if (ok || defb == fn->start->id)
  137. continue;
  138. }
  139. bszero(u);
  140. k = Kx;
  141. bp = be;
  142. for (b=fn->start; b; b=b->link) {
  143. b->visit = 0;
  144. r = R;
  145. for (i=b->ins; i<&b->ins[b->nins]; i++) {
  146. if (!req(r, R)) {
  147. if (req(i->arg[0], TMP(t)))
  148. i->arg[0] = r;
  149. if (req(i->arg[1], TMP(t)))
  150. i->arg[1] = r;
  151. }
  152. if (req(i->to, TMP(t))) {
  153. if (!bshas(b->out, t)) {
  154. r = refindex(t, fn);
  155. i->to = r;
  156. } else {
  157. if (!bshas(u, b->id)) {
  158. bsset(u, b->id);
  159. *--bp = b;
  160. }
  161. if (clsmerge(&k, i->cls))
  162. die("invalid input");
  163. }
  164. }
  165. }
  166. if (!req(r, R) && req(b->jmp.arg, TMP(t)))
  167. b->jmp.arg = r;
  168. }
  169. bscopy(defs, u);
  170. while (bp != be) {
  171. fn->tmp[t].visit = t;
  172. b = *bp++;
  173. bsclr(u, b->id);
  174. for (n=0; n<b->nfron; n++) {
  175. a = b->fron[n];
  176. if (a->visit++ == 0)
  177. if (bshas(a->in, t)) {
  178. p = alloc(sizeof *p);
  179. p->cls = k;
  180. p->to = TMP(t);
  181. p->link = a->phi;
  182. p->arg = vnew(0, sizeof p->arg[0], PFn);
  183. p->blk = vnew(0, sizeof p->blk[0], PFn);
  184. a->phi = p;
  185. if (!bshas(defs, a->id))
  186. if (!bshas(u, a->id)) {
  187. bsset(u, a->id);
  188. *--bp = a;
  189. }
  190. }
  191. }
  192. }
  193. }
  194. free(blist);
  195. }
  196. typedef struct Name Name;
  197. struct Name {
  198. Ref r;
  199. Blk *b;
  200. Name *up;
  201. };
  202. static Name *namel;
  203. static Name *
  204. nnew(Ref r, Blk *b, Name *up)
  205. {
  206. Name *n;
  207. if (namel) {
  208. n = namel;
  209. namel = n->up;
  210. } else
  211. /* could use alloc, here
  212. * but namel should be reset
  213. */
  214. n = emalloc(sizeof *n);
  215. n->r = r;
  216. n->b = b;
  217. n->up = up;
  218. return n;
  219. }
  220. static void
  221. nfree(Name *n)
  222. {
  223. n->up = namel;
  224. namel = n;
  225. }
  226. static void
  227. rendef(Ref *r, Blk *b, Name **stk, Fn *fn)
  228. {
  229. Ref r1;
  230. int t;
  231. t = r->val;
  232. if (req(*r, R) || !fn->tmp[t].visit)
  233. return;
  234. r1 = refindex(t, fn);
  235. fn->tmp[r1.val].visit = t;
  236. stk[t] = nnew(r1, b, stk[t]);
  237. *r = r1;
  238. }
  239. static Ref
  240. getstk(int t, Blk *b, Name **stk)
  241. {
  242. Name *n, *n1;
  243. n = stk[t];
  244. while (n && !dom(n->b, b)) {
  245. n1 = n;
  246. n = n->up;
  247. nfree(n1);
  248. }
  249. stk[t] = n;
  250. if (!n) {
  251. /* uh, oh, warn */
  252. return UNDEF;
  253. } else
  254. return n->r;
  255. }
  256. static void
  257. renblk(Blk *b, Name **stk, Fn *fn)
  258. {
  259. Phi *p;
  260. Ins *i;
  261. Blk *s, **ps, *succ[3];
  262. int t, m;
  263. for (p=b->phi; p; p=p->link)
  264. rendef(&p->to, b, stk, fn);
  265. for (i=b->ins; i<&b->ins[b->nins]; i++) {
  266. for (m=0; m<2; m++) {
  267. t = i->arg[m].val;
  268. if (rtype(i->arg[m]) == RTmp)
  269. if (fn->tmp[t].visit)
  270. i->arg[m] = getstk(t, b, stk);
  271. }
  272. rendef(&i->to, b, stk, fn);
  273. }
  274. t = b->jmp.arg.val;
  275. if (rtype(b->jmp.arg) == RTmp)
  276. if (fn->tmp[t].visit)
  277. b->jmp.arg = getstk(t, b, stk);
  278. succ[0] = b->s1;
  279. succ[1] = b->s2 == b->s1 ? 0 : b->s2;
  280. succ[2] = 0;
  281. for (ps=succ; (s=*ps); ps++)
  282. for (p=s->phi; p; p=p->link) {
  283. t = p->to.val;
  284. if ((t=fn->tmp[t].visit)) {
  285. m = p->narg++;
  286. vgrow(&p->arg, p->narg);
  287. vgrow(&p->blk, p->narg);
  288. p->arg[m] = getstk(t, b, stk);
  289. p->blk[m] = b;
  290. }
  291. }
  292. for (s=b->dom; s; s=s->dlink)
  293. renblk(s, stk, fn);
  294. }
  295. /* require rpo and use */
  296. void
  297. ssa(Fn *fn)
  298. {
  299. Name **stk, *n;
  300. int d, nt;
  301. Blk *b, *b1;
  302. nt = fn->ntmp;
  303. stk = emalloc(nt * sizeof stk[0]);
  304. d = debug['L'];
  305. debug['L'] = 0;
  306. filldom(fn);
  307. if (debug['N']) {
  308. fprintf(stderr, "\n> Dominators:\n");
  309. for (b1=fn->start; b1; b1=b1->link) {
  310. if (!b1->dom)
  311. continue;
  312. fprintf(stderr, "%10s:", b1->name);
  313. for (b=b1->dom; b; b=b->dlink)
  314. fprintf(stderr, " %s", b->name);
  315. fprintf(stderr, "\n");
  316. }
  317. }
  318. fillfron(fn);
  319. filllive(fn);
  320. phiins(fn);
  321. renblk(fn->start, stk, fn);
  322. while (nt--)
  323. while ((n=stk[nt])) {
  324. stk[nt] = n->up;
  325. nfree(n);
  326. }
  327. debug['L'] = d;
  328. free(stk);
  329. if (debug['N']) {
  330. fprintf(stderr, "\n> After SSA construction:\n");
  331. printfn(fn, stderr);
  332. }
  333. }
  334. static int
  335. phicheck(Phi *p, Blk *b, Ref t)
  336. {
  337. Blk *b1;
  338. uint n;
  339. for (n=0; n<p->narg; n++)
  340. if (req(p->arg[n], t)) {
  341. b1 = p->blk[n];
  342. if (b1 != b && !sdom(b, b1))
  343. return 1;
  344. }
  345. return 0;
  346. }
  347. /* require use and ssa */
  348. void
  349. ssacheck(Fn *fn)
  350. {
  351. Tmp *t;
  352. Ins *i;
  353. Phi *p;
  354. Use *u;
  355. Blk *b, *bu;
  356. Ref r;
  357. for (t=&fn->tmp[Tmp0]; t-fn->tmp < fn->ntmp; t++) {
  358. if (t->ndef > 1)
  359. err("ssa temporary %%%s defined more than once",
  360. t->name);
  361. if (t->nuse > 0 && t->ndef == 0) {
  362. bu = fn->rpo[t->use[0].bid];
  363. goto Err;
  364. }
  365. }
  366. for (b=fn->start; b; b=b->link) {
  367. for (p=b->phi; p; p=p->link) {
  368. r = p->to;
  369. t = &fn->tmp[r.val];
  370. for (u=t->use; u<&t->use[t->nuse]; u++) {
  371. bu = fn->rpo[u->bid];
  372. if (u->type == UPhi) {
  373. if (phicheck(u->u.phi, b, r))
  374. goto Err;
  375. } else
  376. if (bu != b && !sdom(b, bu))
  377. goto Err;
  378. }
  379. }
  380. for (i=b->ins; i<&b->ins[b->nins]; i++) {
  381. if (rtype(i->to) != RTmp)
  382. continue;
  383. r = i->to;
  384. t = &fn->tmp[r.val];
  385. for (u=t->use; u<&t->use[t->nuse]; u++) {
  386. bu = fn->rpo[u->bid];
  387. if (u->type == UPhi) {
  388. if (phicheck(u->u.phi, b, r))
  389. goto Err;
  390. } else {
  391. if (bu == b) {
  392. if (u->type == UIns)
  393. if (u->u.ins <= i)
  394. goto Err;
  395. } else
  396. if (!sdom(b, bu))
  397. goto Err;
  398. }
  399. }
  400. }
  401. }
  402. return;
  403. Err:
  404. if (t->visit)
  405. die("%%%s violates ssa invariant", t->name);
  406. else
  407. err("ssa temporary %%%s is used undefined in @%s",
  408. t->name, bu->name);
  409. }