peep.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "gc.h"
  10. void
  11. peep(void)
  12. {
  13. Reg *r, *r1, *r2;
  14. Prog *p, *p1;
  15. int t;
  16. /*
  17. * complete R structure
  18. */
  19. t = 0;
  20. for(r=firstr; r!=R; r=r1) {
  21. r1 = r->link;
  22. if(r1 == R)
  23. break;
  24. p = r->prog->link;
  25. while(p != r1->prog)
  26. switch(p->as) {
  27. default:
  28. r2 = rega();
  29. r->link = r2;
  30. r2->link = r1;
  31. r2->prog = p;
  32. r2->p1 = r;
  33. r->s1 = r2;
  34. r2->s1 = r1;
  35. r1->p1 = r2;
  36. r = r2;
  37. t++;
  38. case ADATA:
  39. case AGLOBL:
  40. case ANAME:
  41. case ASIGNAME:
  42. p = p->link;
  43. }
  44. }
  45. loop1:
  46. t = 0;
  47. for(r=firstr; r!=R; r=r->link) {
  48. p = r->prog;
  49. if(p->as == AMOVW || p->as == AMOVF || p->as == AMOVD)
  50. if(regtyp(&p->to)) {
  51. if(regtyp(&p->from))
  52. if(p->from.type == p->to.type) {
  53. if(copyprop(r)) {
  54. excise(r);
  55. t++;
  56. } else
  57. if(subprop(r) && copyprop(r)) {
  58. excise(r);
  59. t++;
  60. }
  61. }
  62. if(regzer(&p->from))
  63. if(p->to.type == D_REG) {
  64. p->from.type = D_REG;
  65. p->from.reg = 0;
  66. if(copyprop(r)) {
  67. excise(r);
  68. t++;
  69. } else
  70. if(subprop(r) && copyprop(r)) {
  71. excise(r);
  72. t++;
  73. }
  74. }
  75. }
  76. }
  77. if(t)
  78. goto loop1;
  79. /*
  80. * look for MOVB x,R; MOVB R,R
  81. */
  82. for(r=firstr; r!=R; r=r->link) {
  83. p = r->prog;
  84. switch(p->as) {
  85. default:
  86. continue;
  87. case AMOVH:
  88. case AMOVHU:
  89. case AMOVB:
  90. case AMOVBU:
  91. if(p->to.type != D_REG)
  92. continue;
  93. break;
  94. }
  95. r1 = r->link;
  96. if(r1 == R)
  97. continue;
  98. p1 = r1->prog;
  99. if(p1->as != p->as)
  100. continue;
  101. if(p1->from.type != D_REG || p1->from.reg != p->to.reg)
  102. continue;
  103. if(p1->to.type != D_REG || p1->to.reg != p->to.reg)
  104. continue;
  105. excise(r1);
  106. }
  107. }
  108. void
  109. excise(Reg *r)
  110. {
  111. Prog *p;
  112. p = r->prog;
  113. p->as = ANOP;
  114. p->from = zprog.from;
  115. p->to = zprog.to;
  116. p->reg = zprog.reg; /**/
  117. }
  118. Reg*
  119. uniqp(Reg *r)
  120. {
  121. Reg *r1;
  122. r1 = r->p1;
  123. if(r1 == R) {
  124. r1 = r->p2;
  125. if(r1 == R || r1->p2link != R)
  126. return R;
  127. } else
  128. if(r->p2 != R)
  129. return R;
  130. return r1;
  131. }
  132. Reg*
  133. uniqs(Reg *r)
  134. {
  135. Reg *r1;
  136. r1 = r->s1;
  137. if(r1 == R) {
  138. r1 = r->s2;
  139. if(r1 == R)
  140. return R;
  141. } else
  142. if(r->s2 != R)
  143. return R;
  144. return r1;
  145. }
  146. int
  147. regzer(Adr *a)
  148. {
  149. if(a->type == D_CONST)
  150. if(a->sym == S)
  151. if(a->offset == 0)
  152. return 1;
  153. if(a->type == D_REG)
  154. if(a->reg == 0)
  155. return 1;
  156. return 0;
  157. }
  158. int
  159. regtyp(Adr *a)
  160. {
  161. if(a->type == D_REG) {
  162. if(a->reg != 0)
  163. return 1;
  164. return 0;
  165. }
  166. if(a->type == D_FREG)
  167. return 1;
  168. return 0;
  169. }
  170. /*
  171. * the idea is to substitute
  172. * one register for another
  173. * from one MOV to another
  174. * MOV a, R0
  175. * ADD b, R0 / no use of R1
  176. * MOV R0, R1
  177. * would be converted to
  178. * MOV a, R1
  179. * ADD b, R1
  180. * MOV R1, R0
  181. * hopefully, then the former or latter MOV
  182. * will be eliminated by copy propagation.
  183. */
  184. int
  185. subprop(Reg *r0)
  186. {
  187. Prog *p;
  188. Adr *v1, *v2;
  189. Reg *r;
  190. int t;
  191. p = r0->prog;
  192. v1 = &p->from;
  193. if(!regtyp(v1))
  194. return 0;
  195. v2 = &p->to;
  196. if(!regtyp(v2))
  197. return 0;
  198. for(r=uniqp(r0); r!=R; r=uniqp(r)) {
  199. if(uniqs(r) == R)
  200. break;
  201. p = r->prog;
  202. switch(p->as) {
  203. case AJAL:
  204. return 0;
  205. case ASGT:
  206. case ASGTU:
  207. case AADD:
  208. case AADDU:
  209. case ASUB:
  210. case ASUBU:
  211. case ASLL:
  212. case ASRL:
  213. case ASRA:
  214. case AOR:
  215. case AAND:
  216. case AXOR:
  217. case AMUL:
  218. case AMULU:
  219. case ADIV:
  220. case ADIVU:
  221. case AADDD:
  222. case AADDF:
  223. case ASUBD:
  224. case ASUBF:
  225. case AMULD:
  226. case AMULF:
  227. case ADIVD:
  228. case ADIVF:
  229. if(p->to.type == v1->type)
  230. if(p->to.reg == v1->reg) {
  231. if(p->reg == NREG)
  232. p->reg = p->to.reg;
  233. goto gotit;
  234. }
  235. break;
  236. case AMOVF:
  237. case AMOVD:
  238. case AMOVW:
  239. if(p->to.type == v1->type)
  240. if(p->to.reg == v1->reg)
  241. goto gotit;
  242. break;
  243. }
  244. if(copyau(&p->from, v2) ||
  245. copyau1(p, v2) ||
  246. copyau(&p->to, v2))
  247. break;
  248. if(copysub(&p->from, v1, v2, 0) ||
  249. copysub1(p, v1, v2, 0) ||
  250. copysub(&p->to, v1, v2, 0))
  251. break;
  252. }
  253. return 0;
  254. gotit:
  255. copysub(&p->to, v1, v2, 1);
  256. if(debug['P']) {
  257. print("gotit: %D->%D\n%P", v1, v2, r->prog);
  258. if(p->from.type == v2->type)
  259. print(" excise");
  260. print("\n");
  261. }
  262. for(r=uniqs(r); r!=r0; r=uniqs(r)) {
  263. p = r->prog;
  264. copysub(&p->from, v1, v2, 1);
  265. copysub1(p, v1, v2, 1);
  266. copysub(&p->to, v1, v2, 1);
  267. if(debug['P'])
  268. print("%P\n", r->prog);
  269. }
  270. t = v1->reg;
  271. v1->reg = v2->reg;
  272. v2->reg = t;
  273. if(debug['P'])
  274. print("%P last\n", r->prog);
  275. return 1;
  276. }
  277. /*
  278. * The idea is to remove redundant copies.
  279. * v1->v2 F=0
  280. * (use v2 s/v2/v1/)*
  281. * set v1 F=1
  282. * use v2 return fail
  283. * -----------------
  284. * v1->v2 F=0
  285. * (use v2 s/v2/v1/)*
  286. * set v1 F=1
  287. * set v2 return success
  288. */
  289. int
  290. copyprop(Reg *r0)
  291. {
  292. Prog *p;
  293. Adr *v1, *v2;
  294. Reg *r;
  295. p = r0->prog;
  296. v1 = &p->from;
  297. v2 = &p->to;
  298. if(copyas(v1, v2))
  299. return 1;
  300. for(r=firstr; r!=R; r=r->link)
  301. r->active = 0;
  302. return copy1(v1, v2, r0->s1, 0);
  303. }
  304. int
  305. copy1(Adr *v1, Adr *v2, Reg *r, int f)
  306. {
  307. int t;
  308. Prog *p;
  309. if(r->active) {
  310. if(debug['P'])
  311. print("act set; return 1\n");
  312. return 1;
  313. }
  314. r->active = 1;
  315. if(debug['P'])
  316. print("copy %D->%D f=%d\n", v1, v2, f);
  317. for(; r != R; r = r->s1) {
  318. p = r->prog;
  319. if(debug['P'])
  320. print("%P", p);
  321. if(!f && uniqp(r) == R) {
  322. f = 1;
  323. if(debug['P'])
  324. print("; merge; f=%d", f);
  325. }
  326. t = copyu(p, v2, A);
  327. switch(t) {
  328. case 2: /* rar, cant split */
  329. if(debug['P'])
  330. print("; %Drar; return 0\n", v2);
  331. return 0;
  332. case 3: /* set */
  333. if(debug['P'])
  334. print("; %Dset; return 1\n", v2);
  335. return 1;
  336. case 1: /* used, substitute */
  337. case 4: /* use and set */
  338. if(f) {
  339. if(!debug['P'])
  340. return 0;
  341. if(t == 4)
  342. print("; %Dused+set and f=%d; return 0\n", v2, f);
  343. else
  344. print("; %Dused and f=%d; return 0\n", v2, f);
  345. return 0;
  346. }
  347. if(copyu(p, v2, v1)) {
  348. if(debug['P'])
  349. print("; sub fail; return 0\n");
  350. return 0;
  351. }
  352. if(debug['P'])
  353. print("; sub%D/%D", v2, v1);
  354. if(t == 4) {
  355. if(debug['P'])
  356. print("; %Dused+set; return 1\n", v2);
  357. return 1;
  358. }
  359. break;
  360. }
  361. if(!f) {
  362. t = copyu(p, v1, A);
  363. if(!f && (t == 2 || t == 3 || t == 4)) {
  364. f = 1;
  365. if(debug['P'])
  366. print("; %Dset and !f; f=%d", v1, f);
  367. }
  368. }
  369. if(debug['P'])
  370. print("\n");
  371. if(r->s2)
  372. if(!copy1(v1, v2, r->s2, f))
  373. return 0;
  374. }
  375. return 1;
  376. }
  377. /*
  378. * return
  379. * 1 if v only used (and substitute),
  380. * 2 if read-alter-rewrite
  381. * 3 if set
  382. * 4 if set and used
  383. * 0 otherwise (not touched)
  384. */
  385. copyu(Prog *p, Adr *v, Adr *s)
  386. {
  387. switch(p->as) {
  388. default:
  389. if(debug['P'])
  390. print(" (???)");
  391. return 2;
  392. case ANOP: /* read, write */
  393. case AMOVW:
  394. case AMOVF:
  395. case AMOVD:
  396. case AMOVH:
  397. case AMOVHU:
  398. case AMOVB:
  399. case AMOVBU:
  400. case AMOVDW:
  401. case AMOVWD:
  402. case AMOVFD:
  403. case AMOVDF:
  404. if(s != A) {
  405. if(copysub(&p->from, v, s, 1))
  406. return 1;
  407. if(!copyas(&p->to, v))
  408. if(copysub(&p->to, v, s, 1))
  409. return 1;
  410. return 0;
  411. }
  412. if(copyas(&p->to, v)) {
  413. if(copyau(&p->from, v))
  414. return 4;
  415. return 3;
  416. }
  417. if(copyau(&p->from, v))
  418. return 1;
  419. if(copyau(&p->to, v))
  420. return 1;
  421. return 0;
  422. case ASGT: /* read, read, write */
  423. case ASGTU:
  424. case AADD:
  425. case AADDU:
  426. case ASUB:
  427. case ASUBU:
  428. case ASLL:
  429. case ASRL:
  430. case ASRA:
  431. case AOR:
  432. case ANOR:
  433. case AAND:
  434. case AXOR:
  435. case AMUL:
  436. case AMULU:
  437. case ADIV:
  438. case ADIVU:
  439. case AADDF:
  440. case AADDD:
  441. case ASUBF:
  442. case ASUBD:
  443. case AMULF:
  444. case AMULD:
  445. case ADIVF:
  446. case ADIVD:
  447. if(s != A) {
  448. if(copysub(&p->from, v, s, 1))
  449. return 1;
  450. if(copysub1(p, v, s, 1))
  451. return 1;
  452. if(!copyas(&p->to, v))
  453. if(copysub(&p->to, v, s, 1))
  454. return 1;
  455. return 0;
  456. }
  457. if(copyas(&p->to, v)) {
  458. if(p->reg == NREG)
  459. p->reg = p->to.reg;
  460. if(copyau(&p->from, v))
  461. return 4;
  462. if(copyau1(p, v))
  463. return 4;
  464. return 3;
  465. }
  466. if(copyau(&p->from, v))
  467. return 1;
  468. if(copyau1(p, v))
  469. return 1;
  470. if(copyau(&p->to, v))
  471. return 1;
  472. return 0;
  473. case ABEQ: /* read, read */
  474. case ABNE:
  475. case ABGTZ:
  476. case ABGEZ:
  477. case ABLTZ:
  478. case ABLEZ:
  479. case ACMPEQD:
  480. case ACMPEQF:
  481. case ACMPGED:
  482. case ACMPGEF:
  483. case ACMPGTD:
  484. case ACMPGTF:
  485. case ABFPF:
  486. case ABFPT:
  487. if(s != A) {
  488. if(copysub(&p->from, v, s, 1))
  489. return 1;
  490. return copysub1(p, v, s, 1);
  491. }
  492. if(copyau(&p->from, v))
  493. return 1;
  494. if(copyau1(p, v))
  495. return 1;
  496. return 0;
  497. case AJMP: /* funny */
  498. if(s != A) {
  499. if(copysub(&p->to, v, s, 1))
  500. return 1;
  501. return 0;
  502. }
  503. if(copyau(&p->to, v))
  504. return 1;
  505. return 0;
  506. case ARET: /* funny */
  507. if(v->type == D_REG)
  508. if(v->reg == REGRET)
  509. return 2;
  510. if(v->type == D_FREG)
  511. if(v->reg == FREGRET)
  512. return 2;
  513. case AJAL: /* funny */
  514. if(v->type == D_REG) {
  515. if(v->reg <= REGEXT && v->reg > exregoffset)
  516. return 2;
  517. if(REGARG && v->reg == REGARG)
  518. return 2;
  519. }
  520. if(v->type == D_FREG)
  521. if(v->reg <= FREGEXT && v->reg > exfregoffset)
  522. return 2;
  523. if(s != A) {
  524. if(copysub(&p->to, v, s, 1))
  525. return 1;
  526. return 0;
  527. }
  528. if(copyau(&p->to, v))
  529. return 4;
  530. return 3;
  531. case ATEXT: /* funny */
  532. if(v->type == D_REG)
  533. if(v->reg == REGARG)
  534. return 3;
  535. return 0;
  536. }
  537. }
  538. int
  539. a2type(Prog *p)
  540. {
  541. switch(p->as) {
  542. case ABEQ:
  543. case ABNE:
  544. case ABGTZ:
  545. case ABGEZ:
  546. case ABLTZ:
  547. case ABLEZ:
  548. case ASGT:
  549. case ASGTU:
  550. case AADD:
  551. case AADDU:
  552. case ASUB:
  553. case ASUBU:
  554. case ASLL:
  555. case ASRL:
  556. case ASRA:
  557. case AOR:
  558. case AAND:
  559. case AXOR:
  560. case AMUL:
  561. case AMULU:
  562. case ADIV:
  563. case ADIVU:
  564. return D_REG;
  565. case ACMPEQD:
  566. case ACMPEQF:
  567. case ACMPGED:
  568. case ACMPGEF:
  569. case ACMPGTD:
  570. case ACMPGTF:
  571. case AADDF:
  572. case AADDD:
  573. case ASUBF:
  574. case ASUBD:
  575. case AMULF:
  576. case AMULD:
  577. case ADIVF:
  578. case ADIVD:
  579. return D_FREG;
  580. }
  581. return D_NONE;
  582. }
  583. /*
  584. * direct reference,
  585. * could be set/use depending on
  586. * semantics
  587. */
  588. int
  589. copyas(Adr *a, Adr *v)
  590. {
  591. if(regtyp(v))
  592. if(a->type == v->type)
  593. if(a->reg == v->reg)
  594. return 1;
  595. return 0;
  596. }
  597. /*
  598. * either direct or indirect
  599. */
  600. int
  601. copyau(Adr *a, Adr *v)
  602. {
  603. if(copyas(a, v))
  604. return 1;
  605. if(v->type == D_REG)
  606. if(a->type == D_OREG)
  607. if(v->reg == a->reg)
  608. return 1;
  609. return 0;
  610. }
  611. int
  612. copyau1(Prog *p, Adr *v)
  613. {
  614. if(regtyp(v))
  615. if(p->from.type == v->type || p->to.type == v->type)
  616. if(p->reg == v->reg) {
  617. if(a2type(p) != v->type)
  618. print("botch a2type %P\n", p);
  619. return 1;
  620. }
  621. return 0;
  622. }
  623. /*
  624. * substitute s for v in a
  625. * return failure to substitute
  626. */
  627. int
  628. copysub(Adr *a, Adr *v, Adr *s, int f)
  629. {
  630. if(f)
  631. if(copyau(a, v))
  632. a->reg = s->reg;
  633. return 0;
  634. }
  635. int
  636. copysub1(Prog *p1, Adr *v, Adr *s, int f)
  637. {
  638. if(f)
  639. if(copyau1(p1, v))
  640. p1->reg = s->reg;
  641. return 0;
  642. }