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