peep.c 10 KB

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