peep.c 10 KB

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