peep.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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 == AFMOVF || p->as == AFMOVD)
  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 AJMPL:
  196. return 0;
  197. case AADD:
  198. case ASUB:
  199. case ASLL:
  200. case ASRL:
  201. case ASRA:
  202. case AOR:
  203. case AAND:
  204. case AXOR:
  205. case AMUL:
  206. case ADIV:
  207. case ADIVL:
  208. case AMOD:
  209. case AMODL:
  210. case AFADDD:
  211. case AFADDF:
  212. case AFSUBD:
  213. case AFSUBF:
  214. case AFMULD:
  215. case AFMULF:
  216. case AFDIVD:
  217. case AFDIVF:
  218. if(p->to.type == v1->type)
  219. if(p->to.reg == v1->reg) {
  220. if(p->reg == NREG)
  221. p->reg = p->to.reg;
  222. goto gotit;
  223. }
  224. break;
  225. case AFMOVF:
  226. case AFMOVD:
  227. case AMOVW:
  228. if(p->to.type == v1->type)
  229. if(p->to.reg == v1->reg)
  230. goto gotit;
  231. break;
  232. }
  233. if(copyau(&p->from, v2) ||
  234. copyau1(p, v2) ||
  235. copyau(&p->to, v2))
  236. break;
  237. if(copysub(&p->from, v1, v2, 0) ||
  238. copysub1(p, v1, v2, 0) ||
  239. copysub(&p->to, v1, v2, 0))
  240. break;
  241. }
  242. return 0;
  243. gotit:
  244. copysub(&p->to, v1, v2, 1);
  245. if(debug['P']) {
  246. print("gotit: %D->%D\n%P", v1, v2, r->prog);
  247. if(p->from.type == v2->type)
  248. print(" excise");
  249. print("\n");
  250. }
  251. for(r=uniqs(r); r!=r0; r=uniqs(r)) {
  252. p = r->prog;
  253. copysub(&p->from, v1, v2, 1);
  254. copysub1(p, v1, v2, 1);
  255. copysub(&p->to, v1, v2, 1);
  256. if(debug['P'])
  257. print("%P\n", r->prog);
  258. }
  259. t = v1->reg;
  260. v1->reg = v2->reg;
  261. v2->reg = t;
  262. if(debug['P'])
  263. print("%P last\n", r->prog);
  264. return 1;
  265. }
  266. /*
  267. * The idea is to remove redundant copies.
  268. * v1->v2 F=0
  269. * (use v2 s/v2/v1/)*
  270. * set v1 F=1
  271. * use v2 return fail
  272. * -----------------
  273. * v1->v2 F=0
  274. * (use v2 s/v2/v1/)*
  275. * set v1 F=1
  276. * set v2 return success
  277. */
  278. int
  279. copyprop(Reg *r0)
  280. {
  281. Prog *p;
  282. Adr *v1, *v2;
  283. Reg *r;
  284. p = r0->prog;
  285. v1 = &p->from;
  286. v2 = &p->to;
  287. if(copyas(v1, v2))
  288. return 1;
  289. for(r=firstr; r!=R; r=r->link)
  290. r->active = 0;
  291. return copy1(v1, v2, r0->s1, 0);
  292. }
  293. int
  294. copy1(Adr *v1, Adr *v2, Reg *r, int f)
  295. {
  296. int t;
  297. Prog *p;
  298. if(r->active) {
  299. if(debug['P'])
  300. print("act set; return 1\n");
  301. return 1;
  302. }
  303. r->active = 1;
  304. if(debug['P'])
  305. print("copy %D->%D f=%d\n", v1, v2, f);
  306. for(; r != R; r = r->s1) {
  307. p = r->prog;
  308. if(debug['P'])
  309. print("%P", p);
  310. if(!f && uniqp(r) == R) {
  311. f = 1;
  312. if(debug['P'])
  313. print("; merge; f=%d", f);
  314. }
  315. t = copyu(p, v2, A);
  316. switch(t) {
  317. case 2: /* rar, cant split */
  318. if(debug['P'])
  319. print("; %Drar; return 0\n", v2);
  320. return 0;
  321. case 3: /* set */
  322. if(debug['P'])
  323. print("; %Dset; return 1\n", v2);
  324. return 1;
  325. case 1: /* used, substitute */
  326. case 4: /* use and set */
  327. if(f) {
  328. if(!debug['P'])
  329. return 0;
  330. if(t == 4)
  331. print("; %Dused+set and f=%d; return 0\n", v2, f);
  332. else
  333. print("; %Dused and f=%d; return 0\n", v2, f);
  334. return 0;
  335. }
  336. if(copyu(p, v2, v1)) {
  337. if(debug['P'])
  338. print("; sub fail; return 0\n");
  339. return 0;
  340. }
  341. if(debug['P'])
  342. print("; sub%D/%D", v2, v1);
  343. if(t == 4) {
  344. if(debug['P'])
  345. print("; %Dused+set; return 1\n", v2);
  346. return 1;
  347. }
  348. break;
  349. }
  350. if(!f) {
  351. t = copyu(p, v1, A);
  352. if(!f && (t == 2 || t == 3 || t == 4)) {
  353. f = 1;
  354. if(debug['P'])
  355. print("; %Dset and !f; f=%d", v1, f);
  356. }
  357. }
  358. if(debug['P'])
  359. print("\n");
  360. if(r->s2)
  361. if(!copy1(v1, v2, r->s2, f))
  362. return 0;
  363. }
  364. return 1;
  365. }
  366. /*
  367. * return
  368. * 1 if v only used (and substitute),
  369. * 2 if read-alter-rewrite
  370. * 3 if set
  371. * 4 if set and used
  372. * 0 otherwise (not touched)
  373. */
  374. int
  375. copyu(Prog *p, Adr *v, Adr *s)
  376. {
  377. switch(p->as) {
  378. default:
  379. if(debug['P'])
  380. print(" (?)");
  381. return 2;
  382. case ANOP: /* read, write */
  383. case AMOVW:
  384. case AMOVH:
  385. case AMOVHU:
  386. case AMOVB:
  387. case AMOVBU:
  388. case AFMOVF:
  389. case AFMOVD:
  390. case AFMOVDW:
  391. case AFMOVWD:
  392. case AFMOVFW:
  393. case AFMOVWF:
  394. case AFMOVFD:
  395. case AFMOVDF:
  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 AADD: /* read read write */
  415. case ASUB:
  416. case ASLL:
  417. case ASRL:
  418. case ASRA:
  419. case AOR:
  420. case AAND:
  421. case AXOR:
  422. case AMUL:
  423. case ADIV:
  424. case ADIVL:
  425. case AMOD:
  426. case AMODL:
  427. case AFADDF:
  428. case AFADDD:
  429. case AFSUBF:
  430. case AFSUBD:
  431. case AFMULF:
  432. case AFMULD:
  433. case AFDIVF:
  434. case AFDIVD:
  435. if(s != A) {
  436. if(copysub(&p->from, v, s, 1))
  437. return 1;
  438. if(copysub1(p, v, s, 1))
  439. return 1;
  440. if(!copyas(&p->to, v))
  441. if(copysub(&p->to, v, s, 1))
  442. return 1;
  443. return 0;
  444. }
  445. if(copyas(&p->to, v)) {
  446. if(p->reg == NREG)
  447. p->reg = p->to.reg;
  448. if(copyau(&p->from, v))
  449. return 4;
  450. if(copyau1(p, v))
  451. return 4;
  452. return 3;
  453. }
  454. if(copyau(&p->from, v))
  455. return 1;
  456. if(copyau1(p, v))
  457. return 1;
  458. if(copyau(&p->to, v))
  459. return 1;
  460. return 0;
  461. case ABA: /* no reference */
  462. case ABCC:
  463. case ABCS:
  464. case ABE:
  465. case ABG:
  466. case ABGE:
  467. case ABGU:
  468. case ABL:
  469. case ABLE:
  470. case ABLEU:
  471. case ABN:
  472. case ABNE:
  473. case ABNEG:
  474. case ABPOS:
  475. case ABVC:
  476. case ABVS:
  477. case AFBA:
  478. case AFBE:
  479. case AFBG:
  480. case AFBGE:
  481. case AFBL:
  482. case AFBLE:
  483. case AFBNE:
  484. case AFBN:
  485. case AFBLG:
  486. case AFBO:
  487. case AFBU:
  488. case AFBUE:
  489. case AFBUG:
  490. case AFBUGE:
  491. case AFBUL:
  492. case AFBULE:
  493. break;
  494. case ACMP: /* read read */
  495. case AFCMPD:
  496. case AFCMPF:
  497. if(s != A) {
  498. if(copysub(&p->from, v, s, 1))
  499. return 1;
  500. return copysub(&p->to, v, s, 1);
  501. }
  502. if(copyau(&p->from, v))
  503. return 1;
  504. if(copyau(&p->to, v))
  505. return 1;
  506. break;
  507. case AJMP: /* funny */
  508. if(s != A) {
  509. if(copysub(&p->to, v, s, 1))
  510. return 1;
  511. return 0;
  512. }
  513. if(copyau(&p->to, v))
  514. return 1;
  515. return 0;
  516. case ARETURN: /* funny */
  517. if(v->type == D_REG)
  518. if(v->reg == REGRET)
  519. return 2;
  520. if(v->type == D_FREG)
  521. if(v->reg == FREGRET)
  522. return 2;
  523. case AJMPL: /* funny */
  524. if(v->type == D_REG) {
  525. if(v->reg <= REGEXT && v->reg > exregoffset)
  526. return 2;
  527. if(v->reg == REGARG)
  528. return 2;
  529. }
  530. if(v->type == D_FREG) {
  531. if(v->reg <= FREGEXT && v->reg > exfregoffset)
  532. return 2;
  533. }
  534. if(s != A) {
  535. if(copysub(&p->to, v, s, 1))
  536. return 1;
  537. return 0;
  538. }
  539. if(copyau(&p->to, v))
  540. return 4;
  541. return 3;
  542. case ATEXT: /* funny */
  543. if(v->type == D_REG)
  544. if(v->reg == REGARG)
  545. return 3;
  546. return 0;
  547. }
  548. return 0;
  549. }
  550. int
  551. a2type(Prog *p)
  552. {
  553. switch(p->as) {
  554. case AADD:
  555. case ASUB:
  556. case ASLL:
  557. case ASRL:
  558. case ASRA:
  559. case AOR:
  560. case AAND:
  561. case AXOR:
  562. case AMUL:
  563. case ADIV:
  564. case ADIVL:
  565. case AMOD:
  566. case AMODL:
  567. return D_REG;
  568. case AFADDF:
  569. case AFADDD:
  570. case AFSUBF:
  571. case AFSUBD:
  572. case AFMULF:
  573. case AFMULD:
  574. case AFDIVF:
  575. case AFDIVD:
  576. return D_FREG;
  577. }
  578. return D_NONE;
  579. }
  580. /*
  581. * direct reference,
  582. * could be set/use depending on
  583. * semantics
  584. */
  585. int
  586. copyas(Adr *a, Adr *v)
  587. {
  588. if(regtyp(v))
  589. if(a->type == v->type)
  590. if(a->reg == v->reg)
  591. return 1;
  592. return 0;
  593. }
  594. /*
  595. * either direct or indirect
  596. */
  597. int
  598. copyau(Adr *a, Adr *v)
  599. {
  600. if(copyas(a, v))
  601. return 1;
  602. if(v->type == D_REG)
  603. if(a->type == D_OREG)
  604. if(v->reg == a->reg)
  605. return 1;
  606. return 0;
  607. }
  608. int
  609. copyau1(Prog *p, Adr *v)
  610. {
  611. if(regtyp(v))
  612. if(p->from.type == v->type || p->to.type == v->type)
  613. if(p->reg == v->reg) {
  614. if(a2type(p) != v->type)
  615. print("botch a2type %P\n", p);
  616. return 1;
  617. }
  618. return 0;
  619. }
  620. /*
  621. * substitute s for v in a
  622. * return failure to substitute
  623. */
  624. int
  625. copysub(Adr *a, Adr *v, Adr *s, int f)
  626. {
  627. if(f)
  628. if(copyau(a, v))
  629. a->reg = s->reg;
  630. return 0;
  631. }
  632. int
  633. copysub1(Prog *p1, Adr *v, Adr *s, int f)
  634. {
  635. if(f)
  636. if(copyau1(p1, v))
  637. p1->reg = s->reg;
  638. return 0;
  639. }