peep.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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 == AFMOVS || p->as == AFMOVD)
  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 = REGZERO;
  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 AMOVHZ:
  80. case AMOVB:
  81. case AMOVBZ:
  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. /*
  138. * i could load 0 into R0 in main9.s,
  139. * but suppose a bug smashes it later?
  140. * ``safety first'' ; this code is commented
  141. * out while i decide whether to use it.
  142. */
  143. regzer(Adr *a)
  144. {
  145. USED(a);
  146. /*
  147. if(a->type == D_CONST)
  148. if(a->sym == S)
  149. if(a->offset == 0)
  150. return 1;
  151. if(a->type == D_REG)
  152. if(a->reg == 0)
  153. return 1;
  154. */
  155. return 0;
  156. }
  157. regtyp(Adr *a)
  158. {
  159. if(a->type == D_REG) {
  160. /*
  161. if(a->reg != 0)
  162. */
  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 ABL:
  204. return 0;
  205. case AADD:
  206. case ASUB:
  207. case ASLW:
  208. case ASRW:
  209. case ASRAW:
  210. case AOR:
  211. case AAND:
  212. case AANDCC:
  213. case ANOR:
  214. case ANORCC:
  215. case AXOR:
  216. case AXORCC:
  217. case AMULLW:
  218. case ADIVW:
  219. case ADIVWU:
  220. case AREM:
  221. case AREMU:
  222. case ANEG:
  223. case ANEGCC:
  224. case AFADD:
  225. case AFADDS:
  226. case AFSUB:
  227. case AFSUBS:
  228. case AFMUL:
  229. case AFMULS:
  230. case AFDIV:
  231. case AFDIVS:
  232. case AFNEG:
  233. case AFNEGCC:
  234. if(p->to.type == v1->type)
  235. if(p->to.reg == v1->reg) {
  236. if(p->reg == NREG)
  237. p->reg = p->to.reg;
  238. goto gotit;
  239. }
  240. break;
  241. case AFMOVS:
  242. case AFMOVD:
  243. case AMOVW:
  244. if(p->to.type == v1->type)
  245. if(p->to.reg == v1->reg)
  246. goto gotit;
  247. break;
  248. }
  249. if(copyau(&p->from, v2) ||
  250. copyau1(p, v2) ||
  251. copyau(&p->to, v2))
  252. break;
  253. if(copysub(&p->from, v1, v2, 0) ||
  254. copysub1(p, v1, v2, 0) ||
  255. copysub(&p->to, v1, v2, 0))
  256. break;
  257. }
  258. return 0;
  259. gotit:
  260. copysub(&p->to, v1, v2, 1);
  261. if(debug['P']) {
  262. print("gotit: %D->%D\n%P", v1, v2, r->prog);
  263. if(p->from.type == v2->type)
  264. print(" excise");
  265. print("\n");
  266. }
  267. for(r=uniqs(r); r!=r0; r=uniqs(r)) {
  268. p = r->prog;
  269. copysub(&p->from, v1, v2, 1);
  270. copysub1(p, v1, v2, 1);
  271. copysub(&p->to, v1, v2, 1);
  272. if(debug['P'])
  273. print("%P\n", r->prog);
  274. }
  275. t = v1->reg;
  276. v1->reg = v2->reg;
  277. v2->reg = t;
  278. if(debug['P'])
  279. print("%P last\n", r->prog);
  280. return 1;
  281. }
  282. /*
  283. * The idea is to remove redundant copies.
  284. * v1->v2 F=0
  285. * (use v2 s/v2/v1/)*
  286. * set v1 F=1
  287. * use v2 return fail
  288. * -----------------
  289. * v1->v2 F=0
  290. * (use v2 s/v2/v1/)*
  291. * set v1 F=1
  292. * set v2 return success
  293. */
  294. int
  295. copyprop(Reg *r0)
  296. {
  297. Prog *p;
  298. Adr *v1, *v2;
  299. Reg *r;
  300. p = r0->prog;
  301. v1 = &p->from;
  302. v2 = &p->to;
  303. if(copyas(v1, v2))
  304. return 1;
  305. for(r=firstr; r!=R; r=r->link)
  306. r->active = 0;
  307. return copy1(v1, v2, r0->s1, 0);
  308. }
  309. copy1(Adr *v1, Adr *v2, Reg *r, int f)
  310. {
  311. int t;
  312. Prog *p;
  313. if(r->active) {
  314. if(debug['P'])
  315. print("act set; return 1\n");
  316. return 1;
  317. }
  318. r->active = 1;
  319. if(debug['P'])
  320. print("copy %D->%D f=%d\n", v1, v2, f);
  321. for(; r != R; r = r->s1) {
  322. p = r->prog;
  323. if(debug['P'])
  324. print("%P", p);
  325. if(!f && uniqp(r) == R) {
  326. f = 1;
  327. if(debug['P'])
  328. print("; merge; f=%d", f);
  329. }
  330. t = copyu(p, v2, A);
  331. switch(t) {
  332. case 2: /* rar, cant split */
  333. if(debug['P'])
  334. print("; %Drar; return 0\n", v2);
  335. return 0;
  336. case 3: /* set */
  337. if(debug['P'])
  338. print("; %Dset; return 1\n", v2);
  339. return 1;
  340. case 1: /* used, substitute */
  341. case 4: /* use and set */
  342. if(f) {
  343. if(!debug['P'])
  344. return 0;
  345. if(t == 4)
  346. print("; %Dused+set and f=%d; return 0\n", v2, f);
  347. else
  348. print("; %Dused and f=%d; return 0\n", v2, f);
  349. return 0;
  350. }
  351. if(copyu(p, v2, v1)) {
  352. if(debug['P'])
  353. print("; sub fail; return 0\n");
  354. return 0;
  355. }
  356. if(debug['P'])
  357. print("; sub%D/%D", v2, v1);
  358. if(t == 4) {
  359. if(debug['P'])
  360. print("; %Dused+set; return 1\n", v2);
  361. return 1;
  362. }
  363. break;
  364. }
  365. if(!f) {
  366. t = copyu(p, v1, A);
  367. if(!f && (t == 2 || t == 3 || t == 4)) {
  368. f = 1;
  369. if(debug['P'])
  370. print("; %Dset and !f; f=%d", v1, f);
  371. }
  372. }
  373. if(debug['P'])
  374. print("\n");
  375. if(r->s2)
  376. if(!copy1(v1, v2, r->s2, f))
  377. return 0;
  378. }
  379. return 1;
  380. }
  381. /*
  382. * return
  383. * 1 if v only used (and substitute),
  384. * 2 if read-alter-rewrite
  385. * 3 if set
  386. * 4 if set and used
  387. * 0 otherwise (not touched)
  388. */
  389. int
  390. copyu(Prog *p, Adr *v, Adr *s)
  391. {
  392. switch(p->as) {
  393. default:
  394. if(debug['P'])
  395. print(" (???)");
  396. return 2;
  397. case ANOP: /* read, write */
  398. case AMOVW:
  399. case AMOVH:
  400. case AMOVHZ:
  401. case AMOVB:
  402. case AMOVBZ:
  403. case ANEG:
  404. case ANEGCC:
  405. case AFCTIW:
  406. case AFCTIWZ:
  407. case AFMOVS:
  408. case AFMOVD:
  409. case AFRSP:
  410. case AFNEG:
  411. case AFNEGCC:
  412. if(s != A) {
  413. if(copysub(&p->from, v, s, 1))
  414. return 1;
  415. if(!copyas(&p->to, v))
  416. if(copysub(&p->to, v, s, 1))
  417. return 1;
  418. return 0;
  419. }
  420. if(copyas(&p->to, v)) {
  421. if(copyau(&p->from, v))
  422. return 4;
  423. return 3;
  424. }
  425. if(copyau(&p->from, v))
  426. return 1;
  427. if(copyau(&p->to, v))
  428. return 1;
  429. return 0;
  430. case AADD: /* read read write */
  431. case ASUB:
  432. case ASLW:
  433. case ASRW:
  434. case ASRAW:
  435. case AOR:
  436. case AAND:
  437. case AANDCC:
  438. case ANOR:
  439. case ANORCC:
  440. case AXOR:
  441. case AMULLW:
  442. case ADIVW:
  443. case ADIVWU:
  444. case AREM:
  445. case AREMU:
  446. case AFADDS:
  447. case AFADD:
  448. case AFSUBS:
  449. case AFSUB:
  450. case AFMULS:
  451. case AFMUL:
  452. case AFDIVS:
  453. case AFDIV:
  454. if(s != A) {
  455. if(copysub(&p->from, v, s, 1))
  456. return 1;
  457. if(copysub1(p, v, s, 1))
  458. return 1;
  459. if(!copyas(&p->to, v))
  460. if(copysub(&p->to, v, s, 1))
  461. return 1;
  462. return 0;
  463. }
  464. if(copyas(&p->to, v)) {
  465. if(p->reg == NREG)
  466. p->reg = p->to.reg;
  467. if(copyau(&p->from, v))
  468. return 4;
  469. if(copyau1(p, v))
  470. return 4;
  471. return 3;
  472. }
  473. if(copyau(&p->from, v))
  474. return 1;
  475. if(copyau1(p, v))
  476. return 1;
  477. if(copyau(&p->to, v))
  478. return 1;
  479. return 0;
  480. case ABEQ:
  481. case ABGT:
  482. case ABGE:
  483. case ABLT:
  484. case ABLE:
  485. case ABNE:
  486. case ABVC:
  487. case ABVS:
  488. break;
  489. case ACMP: /* read read */
  490. case ACMPU:
  491. case AFCMPO:
  492. case AFCMPU:
  493. if(s != A) {
  494. if(copysub(&p->from, v, s, 1))
  495. return 1;
  496. return copysub(&p->to, v, s, 1);
  497. }
  498. if(copyau(&p->from, v))
  499. return 1;
  500. if(copyau(&p->to, v))
  501. return 1;
  502. break;
  503. case ABR: /* funny */
  504. if(s != A) {
  505. if(copysub(&p->to, v, s, 1))
  506. return 1;
  507. return 0;
  508. }
  509. if(copyau(&p->to, v))
  510. return 1;
  511. return 0;
  512. case ARETURN: /* funny */
  513. if(v->type == D_REG)
  514. if(v->reg == REGRET)
  515. return 2;
  516. if(v->type == D_FREG)
  517. if(v->reg == FREGRET)
  518. return 2;
  519. case ABL: /* funny */
  520. if(v->type == D_REG) {
  521. if(v->reg <= REGEXT && v->reg > exregoffset)
  522. return 2;
  523. if(v->reg == REGARG)
  524. return 2;
  525. }
  526. if(v->type == D_FREG) {
  527. if(v->reg <= FREGEXT && v->reg > exfregoffset)
  528. return 2;
  529. }
  530. if(s != A) {
  531. if(copysub(&p->to, v, s, 1))
  532. return 1;
  533. return 0;
  534. }
  535. if(copyau(&p->to, v))
  536. return 4;
  537. return 3;
  538. case ATEXT: /* funny */
  539. if(v->type == D_REG)
  540. if(v->reg == REGARG)
  541. return 3;
  542. return 0;
  543. }
  544. return 0;
  545. }
  546. int
  547. a2type(Prog *p)
  548. {
  549. switch(p->as) {
  550. case AADD:
  551. case AADDCC:
  552. case ASUB:
  553. case ASUBCC:
  554. case ASLW:
  555. case ASLWCC:
  556. case ASRW:
  557. case ASRWCC:
  558. case ASRAW:
  559. case ASRAWCC:
  560. case AOR:
  561. case AORCC:
  562. case AAND:
  563. case AANDCC:
  564. case AXOR:
  565. case AXORCC:
  566. case ANEG:
  567. case ANEGCC:
  568. case AMULLW:
  569. case AMULLWCC:
  570. case ADIVW:
  571. case ADIVWCC:
  572. case ADIVWU:
  573. case ADIVWUCC:
  574. case AREM:
  575. case AREMCC:
  576. case AREMU:
  577. case AREMUCC:
  578. case ANOR:
  579. case ANORCC:
  580. return D_REG;
  581. case AFADDS:
  582. case AFADDSCC:
  583. case AFADD:
  584. case AFADDCC:
  585. case AFSUBS:
  586. case AFSUBSCC:
  587. case AFSUB:
  588. case AFSUBCC:
  589. case AFMULS:
  590. case AFMULSCC:
  591. case AFMUL:
  592. case AFMULCC:
  593. case AFDIVS:
  594. case AFDIVSCC:
  595. case AFDIV:
  596. case AFDIVCC:
  597. case AFNEG:
  598. case AFNEGCC:
  599. return D_FREG;
  600. }
  601. return D_NONE;
  602. }
  603. /*
  604. * direct reference,
  605. * could be set/use depending on
  606. * semantics
  607. */
  608. int
  609. copyas(Adr *a, Adr *v)
  610. {
  611. if(regtyp(v))
  612. if(a->type == v->type)
  613. if(a->reg == v->reg)
  614. return 1;
  615. return 0;
  616. }
  617. /*
  618. * either direct or indirect
  619. */
  620. int
  621. copyau(Adr *a, Adr *v)
  622. {
  623. if(copyas(a, v))
  624. return 1;
  625. if(v->type == D_REG)
  626. if(a->type == D_OREG)
  627. if(v->reg == a->reg)
  628. return 1;
  629. return 0;
  630. }
  631. int
  632. copyau1(Prog *p, Adr *v)
  633. {
  634. if(regtyp(v))
  635. if(p->from.type == v->type || p->to.type == v->type)
  636. if(p->reg == v->reg) {
  637. if(a2type(p) != v->type)
  638. print("botch a2type %P\n", p);
  639. return 1;
  640. }
  641. return 0;
  642. }
  643. /*
  644. * substitute s for v in a
  645. * return failure to substitute
  646. */
  647. int
  648. copysub(Adr *a, Adr *v, Adr *s, int f)
  649. {
  650. if(f)
  651. if(copyau(a, v))
  652. a->reg = s->reg;
  653. return 0;
  654. }
  655. int
  656. copysub1(Prog *p1, Adr *v, Adr *s, int f)
  657. {
  658. if(f)
  659. if(copyau1(p1, v))
  660. p1->reg = s->reg;
  661. return 0;
  662. }