peep.c 10 KB

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