ssa.c 7.7 KB

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