peep.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  1. #include "gc.h"
  2. int xtramodes(Reg*, Adr*);
  3. void
  4. peep(void)
  5. {
  6. Reg *r, *r1, *r2;
  7. Prog *p, *p1;
  8. int t;
  9. /*
  10. * complete R structure
  11. */
  12. t = 0;
  13. for(r=firstr; r!=R; r=r1) {
  14. r1 = r->link;
  15. if(r1 == R)
  16. break;
  17. p = r->prog->link;
  18. while(p != r1->prog)
  19. switch(p->as) {
  20. default:
  21. r2 = rega();
  22. r->link = r2;
  23. r2->link = r1;
  24. r2->prog = p;
  25. r2->p1 = r;
  26. r->s1 = r2;
  27. r2->s1 = r1;
  28. r1->p1 = r2;
  29. r = r2;
  30. t++;
  31. case ADATA:
  32. case AGLOBL:
  33. case ANAME:
  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 == ASLL || p->as == ASRL || p->as == ASRA) {
  42. /*
  43. * elide shift into D_SHIFT operand of subsequent instruction
  44. */
  45. if(shiftprop(r)) {
  46. excise(r);
  47. t++;
  48. }
  49. }
  50. if(p->as == AMOVW || p->as == AMOVF || p->as == AMOVD)
  51. if(regtyp(&p->to)) {
  52. if(p->from.type == D_CONST)
  53. constprop(&p->from, &p->to, r->s1);
  54. else if(regtyp(&p->from))
  55. if(p->from.type == p->to.type) {
  56. if(copyprop(r)) {
  57. excise(r);
  58. t++;
  59. } else
  60. if(subprop(r) && copyprop(r)) {
  61. excise(r);
  62. t++;
  63. }
  64. }
  65. }
  66. }
  67. if(t)
  68. goto loop1;
  69. /*
  70. * look for MOVB x,R; MOVB R,R
  71. */
  72. for(r=firstr; r!=R; r=r->link) {
  73. p = r->prog;
  74. switch(p->as) {
  75. default:
  76. continue;
  77. case AEOR:
  78. /*
  79. * EOR -1,x,y => MVN x,y
  80. */
  81. if(p->from.type == D_CONST && p->from.offset == -1) {
  82. p->as = AMVN;
  83. p->from.type = D_REG;
  84. if(p->reg != NREG)
  85. p->from.reg = p->reg;
  86. else
  87. p->from.reg = p->to.reg;
  88. p->reg = NREG;
  89. }
  90. continue;
  91. case AMOVH:
  92. case AMOVHU:
  93. case AMOVB:
  94. case AMOVBU:
  95. if(p->to.type != D_REG)
  96. continue;
  97. break;
  98. }
  99. r1 = r->link;
  100. if(r1 == R)
  101. continue;
  102. p1 = r1->prog;
  103. if(p1->as != p->as)
  104. continue;
  105. if(p1->from.type != D_REG || p1->from.reg != p->to.reg)
  106. continue;
  107. if(p1->to.type != D_REG || p1->to.reg != p->to.reg)
  108. continue;
  109. excise(r1);
  110. }
  111. for(r=firstr; r!=R; r=r->link) {
  112. p = r->prog;
  113. switch(p->as) {
  114. case AMOVW:
  115. case AMOVB:
  116. case AMOVBU:
  117. if(p->from.type == D_OREG && p->from.offset == 0)
  118. xtramodes(r, &p->from);
  119. else if(p->to.type == D_OREG && p->to.offset == 0)
  120. xtramodes(r, &p->to);
  121. else
  122. continue;
  123. break;
  124. case ACMP:
  125. /*
  126. * elide CMP $0,x if calculation of x can set condition codes
  127. */
  128. if(p->from.type != D_CONST || p->from.offset != 0)
  129. continue;
  130. r2 = r->s1;
  131. if(r2 == R)
  132. continue;
  133. t = r2->prog->as;
  134. switch(t) {
  135. default:
  136. continue;
  137. case ABEQ:
  138. case ABNE:
  139. case ABMI:
  140. case ABPL:
  141. break;
  142. case ABGE:
  143. t = ABPL;
  144. break;
  145. case ABLT:
  146. t = ABMI;
  147. break;
  148. case ABHI:
  149. t = ABNE;
  150. break;
  151. case ABLS:
  152. t = ABEQ;
  153. break;
  154. }
  155. r1 = r;
  156. do
  157. r1 = uniqp(r1);
  158. while (r1 != R && r1->prog->as == ANOP);
  159. if(r1 == R)
  160. continue;
  161. p1 = r1->prog;
  162. if(p1->to.type != D_REG)
  163. continue;
  164. if(p1->to.reg != p->reg)
  165. if(!(p1->as == AMOVW && p1->from.type == D_REG && p1->from.reg == p->reg))
  166. continue;
  167. switch(p1->as) {
  168. default:
  169. continue;
  170. case AMOVW:
  171. if(p1->from.type != D_REG)
  172. continue;
  173. case AAND:
  174. case AEOR:
  175. case AORR:
  176. case ABIC:
  177. case AMVN:
  178. case ASUB:
  179. case ARSB:
  180. case AADD:
  181. case AADC:
  182. case ASBC:
  183. case ARSC:
  184. break;
  185. }
  186. p1->scond |= C_SBIT;
  187. r2->prog->as = t;
  188. excise(r);
  189. continue;
  190. }
  191. }
  192. predicate();
  193. }
  194. void
  195. excise(Reg *r)
  196. {
  197. Prog *p;
  198. p = r->prog;
  199. p->as = ANOP;
  200. p->scond = zprog.scond;
  201. p->from = zprog.from;
  202. p->to = zprog.to;
  203. p->reg = zprog.reg; /**/
  204. }
  205. Reg*
  206. uniqp(Reg *r)
  207. {
  208. Reg *r1;
  209. r1 = r->p1;
  210. if(r1 == R) {
  211. r1 = r->p2;
  212. if(r1 == R || r1->p2link != R)
  213. return R;
  214. } else
  215. if(r->p2 != R)
  216. return R;
  217. return r1;
  218. }
  219. Reg*
  220. uniqs(Reg *r)
  221. {
  222. Reg *r1;
  223. r1 = r->s1;
  224. if(r1 == R) {
  225. r1 = r->s2;
  226. if(r1 == R)
  227. return R;
  228. } else
  229. if(r->s2 != R)
  230. return R;
  231. return r1;
  232. }
  233. int
  234. regtyp(Adr *a)
  235. {
  236. if(a->type == D_REG)
  237. return 1;
  238. if(a->type == D_FREG)
  239. return 1;
  240. return 0;
  241. }
  242. /*
  243. * the idea is to substitute
  244. * one register for another
  245. * from one MOV to another
  246. * MOV a, R0
  247. * ADD b, R0 / no use of R1
  248. * MOV R0, R1
  249. * would be converted to
  250. * MOV a, R1
  251. * ADD b, R1
  252. * MOV R1, R0
  253. * hopefully, then the former or latter MOV
  254. * will be eliminated by copy propagation.
  255. */
  256. int
  257. subprop(Reg *r0)
  258. {
  259. Prog *p;
  260. Adr *v1, *v2;
  261. Reg *r;
  262. int t;
  263. p = r0->prog;
  264. v1 = &p->from;
  265. if(!regtyp(v1))
  266. return 0;
  267. v2 = &p->to;
  268. if(!regtyp(v2))
  269. return 0;
  270. for(r=uniqp(r0); r!=R; r=uniqp(r)) {
  271. if(uniqs(r) == R)
  272. break;
  273. p = r->prog;
  274. switch(p->as) {
  275. case ABL:
  276. return 0;
  277. case ACMP:
  278. case ACMN:
  279. case AADD:
  280. case ASUB:
  281. case ARSB:
  282. case ASLL:
  283. case ASRL:
  284. case ASRA:
  285. case AORR:
  286. case AAND:
  287. case AEOR:
  288. case AMUL:
  289. case ADIV:
  290. case ADIVU:
  291. case ACMPF:
  292. case ACMPD:
  293. case AADDD:
  294. case AADDF:
  295. case ASUBD:
  296. case ASUBF:
  297. case AMULD:
  298. case AMULF:
  299. case ADIVD:
  300. case ADIVF:
  301. if(p->to.type == v1->type)
  302. if(p->to.reg == v1->reg) {
  303. if(p->reg == NREG)
  304. p->reg = p->to.reg;
  305. goto gotit;
  306. }
  307. break;
  308. case AMOVF:
  309. case AMOVD:
  310. case AMOVW:
  311. if(p->to.type == v1->type)
  312. if(p->to.reg == v1->reg)
  313. goto gotit;
  314. break;
  315. case AMOVM:
  316. t = 1<<v2->reg;
  317. if((p->from.type == D_CONST && (p->from.offset&t)) ||
  318. (p->to.type == D_CONST && (p->to.offset&t)))
  319. return 0;
  320. break;
  321. }
  322. if(copyau(&p->from, v2) ||
  323. copyau1(p, v2) ||
  324. copyau(&p->to, v2))
  325. break;
  326. if(copysub(&p->from, v1, v2, 0) ||
  327. copysub1(p, v1, v2, 0) ||
  328. copysub(&p->to, v1, v2, 0))
  329. break;
  330. }
  331. return 0;
  332. gotit:
  333. copysub(&p->to, v1, v2, 1);
  334. if(debug['P']) {
  335. print("gotit: %D->%D\n%P", v1, v2, r->prog);
  336. if(p->from.type == v2->type)
  337. print(" excise");
  338. print("\n");
  339. }
  340. for(r=uniqs(r); r!=r0; r=uniqs(r)) {
  341. p = r->prog;
  342. copysub(&p->from, v1, v2, 1);
  343. copysub1(p, v1, v2, 1);
  344. copysub(&p->to, v1, v2, 1);
  345. if(debug['P'])
  346. print("%P\n", r->prog);
  347. }
  348. t = v1->reg;
  349. v1->reg = v2->reg;
  350. v2->reg = t;
  351. if(debug['P'])
  352. print("%P last\n", r->prog);
  353. return 1;
  354. }
  355. /*
  356. * The idea is to remove redundant copies.
  357. * v1->v2 F=0
  358. * (use v2 s/v2/v1/)*
  359. * set v1 F=1
  360. * use v2 return fail
  361. * -----------------
  362. * v1->v2 F=0
  363. * (use v2 s/v2/v1/)*
  364. * set v1 F=1
  365. * set v2 return success
  366. */
  367. int
  368. copyprop(Reg *r0)
  369. {
  370. Prog *p;
  371. Adr *v1, *v2;
  372. Reg *r;
  373. p = r0->prog;
  374. v1 = &p->from;
  375. v2 = &p->to;
  376. if(copyas(v1, v2))
  377. return 1;
  378. for(r=firstr; r!=R; r=r->link)
  379. r->active = 0;
  380. return copy1(v1, v2, r0->s1, 0);
  381. }
  382. int
  383. copy1(Adr *v1, Adr *v2, Reg *r, int f)
  384. {
  385. int t;
  386. Prog *p;
  387. if(r->active) {
  388. if(debug['P'])
  389. print("act set; return 1\n");
  390. return 1;
  391. }
  392. r->active = 1;
  393. if(debug['P'])
  394. print("copy %D->%D f=%d\n", v1, v2, f);
  395. for(; r != R; r = r->s1) {
  396. p = r->prog;
  397. if(debug['P'])
  398. print("%P", p);
  399. if(!f && uniqp(r) == R) {
  400. f = 1;
  401. if(debug['P'])
  402. print("; merge; f=%d", f);
  403. }
  404. t = copyu(p, v2, A);
  405. switch(t) {
  406. case 2: /* rar, cant split */
  407. if(debug['P'])
  408. print("; %Drar; return 0\n", v2);
  409. return 0;
  410. case 3: /* set */
  411. if(debug['P'])
  412. print("; %Dset; return 1\n", v2);
  413. return 1;
  414. case 1: /* used, substitute */
  415. case 4: /* use and set */
  416. if(f) {
  417. if(!debug['P'])
  418. return 0;
  419. if(t == 4)
  420. print("; %Dused+set and f=%d; return 0\n", v2, f);
  421. else
  422. print("; %Dused and f=%d; return 0\n", v2, f);
  423. return 0;
  424. }
  425. if(copyu(p, v2, v1)) {
  426. if(debug['P'])
  427. print("; sub fail; return 0\n");
  428. return 0;
  429. }
  430. if(debug['P'])
  431. print("; sub%D/%D", v2, v1);
  432. if(t == 4) {
  433. if(debug['P'])
  434. print("; %Dused+set; return 1\n", v2);
  435. return 1;
  436. }
  437. break;
  438. }
  439. if(!f) {
  440. t = copyu(p, v1, A);
  441. if(!f && (t == 2 || t == 3 || t == 4)) {
  442. f = 1;
  443. if(debug['P'])
  444. print("; %Dset and !f; f=%d", v1, f);
  445. }
  446. }
  447. if(debug['P'])
  448. print("\n");
  449. if(r->s2)
  450. if(!copy1(v1, v2, r->s2, f))
  451. return 0;
  452. }
  453. return 1;
  454. }
  455. /*
  456. * The idea is to remove redundant constants.
  457. * $c1->v1
  458. * ($c1->v2 s/$c1/v1)*
  459. * set v1 return
  460. * The v1->v2 should be eliminated by copy propagation.
  461. */
  462. void
  463. constprop(Adr *c1, Adr *v1, Reg *r)
  464. {
  465. Prog *p;
  466. if(debug['C'])
  467. print("constprop %D->%D\n", c1, v1);
  468. for(; r != R; r = r->s1) {
  469. p = r->prog;
  470. if(debug['C'])
  471. print("%P", p);
  472. if(uniqp(r) == R) {
  473. if(debug['C'])
  474. print("; merge; return\n");
  475. return;
  476. }
  477. if(p->as == AMOVW && copyas(&p->from, c1)) {
  478. if(debug['C'])
  479. print("; sub%D/%D", &p->from, v1);
  480. p->from = *v1;
  481. } else if(copyu(p, v1, A) > 1) {
  482. if(debug['C'])
  483. print("; %Dset; return\n", v1);
  484. return;
  485. }
  486. if(debug['C'])
  487. print("\n");
  488. if(r->s2)
  489. constprop(c1, v1, r->s2);
  490. }
  491. }
  492. /*
  493. * ASLL x,y,w
  494. * .. (not use w, not set x y w)
  495. * AXXX w,a,b (a != w)
  496. * .. (not use w)
  497. * (set w)
  498. * ----------- changed to
  499. * ..
  500. * AXXX (x<<y),a,b
  501. * ..
  502. */
  503. #define FAIL(msg) { if(debug['H']) print("\t%s; FAILURE\n", msg); return 0; }
  504. int
  505. shiftprop(Reg *r)
  506. {
  507. Reg *r1;
  508. Prog *p, *p1, *p2;
  509. int n, o;
  510. Adr a;
  511. p = r->prog;
  512. if(p->to.type != D_REG)
  513. FAIL("BOTCH: result not reg");
  514. n = p->to.reg;
  515. a = zprog.from;
  516. if(p->reg != NREG && p->reg != p->to.reg) {
  517. a.type = D_REG;
  518. a.reg = p->reg;
  519. }
  520. if(debug['H'])
  521. print("shiftprop\n%P", p);
  522. r1 = r;
  523. for(;;) {
  524. /* find first use of shift result; abort if shift operands or result are changed */
  525. r1 = uniqs(r1);
  526. if(r1 == R)
  527. FAIL("branch");
  528. if(uniqp(r1) == R)
  529. FAIL("merge");
  530. p1 = r1->prog;
  531. if(debug['H'])
  532. print("\n%P", p1);
  533. switch(copyu(p1, &p->to, A)) {
  534. case 0: /* not used or set */
  535. if((p->from.type == D_REG && copyu(p1, &p->from, A) > 1) ||
  536. (a.type == D_REG && copyu(p1, &a, A) > 1))
  537. FAIL("args modified");
  538. continue;
  539. case 3: /* set, not used */
  540. FAIL("BOTCH: noref");
  541. }
  542. break;
  543. }
  544. /* check whether substitution can be done */
  545. switch(p1->as) {
  546. default:
  547. FAIL("non-dpi");
  548. case AAND:
  549. case AEOR:
  550. case AADD:
  551. case AADC:
  552. case AORR:
  553. case ASUB:
  554. case ARSB:
  555. case ASBC:
  556. case ARSC:
  557. if(p1->reg == n || (p1->reg == NREG && p1->to.type == D_REG && p1->to.reg == n)) {
  558. if(p1->from.type != D_REG)
  559. FAIL("can't swap");
  560. p1->reg = p1->from.reg;
  561. p1->from.reg = n;
  562. switch(p1->as) {
  563. case ASUB:
  564. p1->as = ARSB;
  565. break;
  566. case ARSB:
  567. p1->as = ASUB;
  568. break;
  569. case ASBC:
  570. p1->as = ARSC;
  571. break;
  572. case ARSC:
  573. p1->as = ASBC;
  574. break;
  575. }
  576. if(debug['H'])
  577. print("\t=>%P", p1);
  578. }
  579. case ABIC:
  580. case ACMP:
  581. case ACMN:
  582. if(p1->reg == n)
  583. FAIL("can't swap");
  584. if(p1->reg == NREG && p1->to.reg == n)
  585. FAIL("shift result used twice");
  586. case AMVN:
  587. if(p1->from.type == D_SHIFT)
  588. FAIL("shift result used in shift");
  589. if(p1->from.type != D_REG || p1->from.reg != n)
  590. FAIL("BOTCH: where is it used?");
  591. break;
  592. }
  593. /* check whether shift result is used subsequently */
  594. p2 = p1;
  595. if(p1->to.reg != n)
  596. for (;;) {
  597. r1 = uniqs(r1);
  598. if(r1 == R)
  599. FAIL("inconclusive");
  600. p1 = r1->prog;
  601. if(debug['H'])
  602. print("\n%P", p1);
  603. switch(copyu(p1, &p->to, A)) {
  604. case 0: /* not used or set */
  605. continue;
  606. case 3: /* set, not used */
  607. break;
  608. default:/* used */
  609. FAIL("reused");
  610. }
  611. break;
  612. }
  613. /* make the substitution */
  614. p2->from.type = D_SHIFT;
  615. p2->from.reg = NREG;
  616. o = p->reg;
  617. if(o == NREG)
  618. o = p->to.reg;
  619. switch(p->from.type){
  620. case D_CONST:
  621. o |= (p->from.offset&0x1f)<<7;
  622. break;
  623. case D_REG:
  624. o |= (1<<4) | (p->from.reg<<8);
  625. break;
  626. }
  627. switch(p->as){
  628. case ASLL:
  629. o |= 0<<5;
  630. break;
  631. case ASRL:
  632. o |= 1<<5;
  633. break;
  634. case ASRA:
  635. o |= 2<<5;
  636. break;
  637. }
  638. p2->from.offset = o;
  639. if(debug['H'])
  640. print("\t=>%P\tSUCCEED\n", p2);
  641. return 1;
  642. }
  643. Reg*
  644. findpre(Reg *r, Adr *v)
  645. {
  646. Reg *r1;
  647. for(r1=uniqp(r); r1!=R; r=r1,r1=uniqp(r)) {
  648. if(uniqs(r1) != r)
  649. return R;
  650. switch(copyu(r1->prog, v, A)) {
  651. case 1: /* used */
  652. case 2: /* read-alter-rewrite */
  653. return R;
  654. case 3: /* set */
  655. case 4: /* set and used */
  656. return r1;
  657. }
  658. }
  659. return R;
  660. }
  661. Reg*
  662. findinc(Reg *r, Reg *r2, Adr *v)
  663. {
  664. Reg *r1;
  665. Prog *p;
  666. for(r1=uniqs(r); r1!=R && r1!=r2; r=r1,r1=uniqs(r)) {
  667. if(uniqp(r1) != r)
  668. return R;
  669. switch(copyu(r1->prog, v, A)) {
  670. case 0: /* not touched */
  671. continue;
  672. case 4: /* set and used */
  673. p = r1->prog;
  674. if(p->as == AADD)
  675. if(p->from.type == D_CONST)
  676. if(p->from.offset > -4096 && p->from.offset < 4096)
  677. return r1;
  678. default:
  679. return R;
  680. }
  681. }
  682. return R;
  683. }
  684. int
  685. nochange(Reg *r, Reg *r2, Prog *p)
  686. {
  687. Adr a[3];
  688. int i, n;
  689. if(r == r2)
  690. return 1;
  691. n = 0;
  692. if(p->reg != NREG && p->reg != p->to.reg) {
  693. a[n].type = D_REG;
  694. a[n++].reg = p->reg;
  695. }
  696. switch(p->from.type) {
  697. case D_SHIFT:
  698. a[n].type = D_REG;
  699. a[n++].reg = p->from.offset&0xf;
  700. case D_REG:
  701. a[n].type = D_REG;
  702. a[n++].reg = p->from.reg;
  703. }
  704. if(n == 0)
  705. return 1;
  706. for(; r!=R && r!=r2; r=uniqs(r)) {
  707. p = r->prog;
  708. for(i=0; i<n; i++)
  709. if(copyu(p, &a[i], A) > 1)
  710. return 0;
  711. }
  712. return 1;
  713. }
  714. int
  715. findu1(Reg *r, Adr *v)
  716. {
  717. for(; r != R; r = r->s1) {
  718. if(r->active)
  719. return 0;
  720. r->active = 1;
  721. switch(copyu(r->prog, v, A)) {
  722. case 1: /* used */
  723. case 2: /* read-alter-rewrite */
  724. case 4: /* set and used */
  725. return 1;
  726. case 3: /* set */
  727. return 0;
  728. }
  729. if(r->s2)
  730. if (findu1(r->s2, v))
  731. return 1;
  732. }
  733. return 0;
  734. }
  735. int
  736. finduse(Reg *r, Adr *v)
  737. {
  738. Reg *r1;
  739. for(r1=firstr; r1!=R; r1=r1->link)
  740. r1->active = 0;
  741. return findu1(r, v);
  742. }
  743. int
  744. xtramodes(Reg *r, Adr *a)
  745. {
  746. Reg *r1, *r2, *r3;
  747. Prog *p, *p1;
  748. Adr v;
  749. p = r->prog;
  750. v = *a;
  751. v.type = D_REG;
  752. r1 = findpre(r, &v);
  753. if(r1 != R) {
  754. p1 = r1->prog;
  755. if(p1->to.type == D_REG && p1->to.reg == v.reg)
  756. switch(p1->as) {
  757. case AADD:
  758. if(p1->from.type == D_REG ||
  759. (p1->from.type == D_SHIFT && (p1->from.offset&(1<<4)) == 0 &&
  760. (p->as != AMOVB || (a == &p->from && (p1->from.offset&~0xf) == 0))) ||
  761. (p1->from.type == D_CONST &&
  762. p1->from.offset > -4096 && p1->from.offset < 4096))
  763. if(nochange(uniqs(r1), r, p1)) {
  764. if(a != &p->from || v.reg != p->to.reg)
  765. if (finduse(r->s1, &v)) {
  766. if(p1->reg == NREG || p1->reg == v.reg)
  767. /* pre-indexing */
  768. p->scond |= C_WBIT;
  769. else return 0;
  770. }
  771. switch (p1->from.type) {
  772. case D_REG:
  773. /* register offset */
  774. a->type = D_SHIFT;
  775. a->offset = p1->from.reg;
  776. break;
  777. case D_SHIFT:
  778. /* scaled register offset */
  779. a->type = D_SHIFT;
  780. case D_CONST:
  781. /* immediate offset */
  782. a->offset = p1->from.offset;
  783. break;
  784. }
  785. if(p1->reg != NREG)
  786. a->reg = p1->reg;
  787. excise(r1);
  788. return 1;
  789. }
  790. break;
  791. case AMOVW:
  792. if(p1->from.type == D_REG)
  793. if((r2 = findinc(r1, r, &p1->from)) != R) {
  794. for(r3=uniqs(r2); r3->prog->as==ANOP; r3=uniqs(r3))
  795. ;
  796. if(r3 == r) {
  797. /* post-indexing */
  798. p1 = r2->prog;
  799. a->reg = p1->to.reg;
  800. a->offset = p1->from.offset;
  801. p->scond |= C_PBIT;
  802. if(!finduse(r, &r1->prog->to))
  803. excise(r1);
  804. excise(r2);
  805. return 1;
  806. }
  807. }
  808. break;
  809. }
  810. }
  811. if(a != &p->from || a->reg != p->to.reg)
  812. if((r1 = findinc(r, R, &v)) != R) {
  813. /* post-indexing */
  814. p1 = r1->prog;
  815. a->offset = p1->from.offset;
  816. p->scond |= C_PBIT;
  817. excise(r1);
  818. return 1;
  819. }
  820. return 0;
  821. }
  822. /*
  823. * return
  824. * 1 if v only used (and substitute),
  825. * 2 if read-alter-rewrite
  826. * 3 if set
  827. * 4 if set and used
  828. * 0 otherwise (not touched)
  829. */
  830. int
  831. copyu(Prog *p, Adr *v, Adr *s)
  832. {
  833. switch(p->as) {
  834. default:
  835. if(debug['P'])
  836. print(" (???)");
  837. return 2;
  838. case AMOVM:
  839. if(v->type != D_REG)
  840. return 0;
  841. if(p->from.type == D_CONST) { /* read reglist, read/rar */
  842. if(s != A) {
  843. if(p->from.offset&(1<<v->reg))
  844. return 1;
  845. if(copysub(&p->to, v, s, 1))
  846. return 1;
  847. return 0;
  848. }
  849. if(copyau(&p->to, v)) {
  850. if(p->scond&C_WBIT)
  851. return 2;
  852. return 1;
  853. }
  854. if(p->from.offset&(1<<v->reg))
  855. return 1;
  856. } else { /* read/rar, write reglist */
  857. if(s != A) {
  858. if(p->to.offset&(1<<v->reg))
  859. return 1;
  860. if(copysub(&p->from, v, s, 1))
  861. return 1;
  862. return 0;
  863. }
  864. if(copyau(&p->from, v)) {
  865. if(p->scond&C_WBIT)
  866. return 2;
  867. if(p->to.offset&(1<<v->reg))
  868. return 4;
  869. return 1;
  870. }
  871. if(p->to.offset&(1<<v->reg))
  872. return 3;
  873. }
  874. return 0;
  875. case ANOP: /* read, write */
  876. case AMOVW:
  877. case AMOVF:
  878. case AMOVD:
  879. case AMOVH:
  880. case AMOVHU:
  881. case AMOVB:
  882. case AMOVBU:
  883. case AMOVDW:
  884. case AMOVWD:
  885. case AMOVFD:
  886. case AMOVDF:
  887. if(p->scond&(C_WBIT|C_PBIT))
  888. if(v->type == D_REG) {
  889. if(p->from.type == D_OREG || p->from.type == D_SHIFT) {
  890. if(p->from.reg == v->reg)
  891. return 2;
  892. } else {
  893. if(p->to.reg == v->reg)
  894. return 2;
  895. }
  896. }
  897. if(s != A) {
  898. if(copysub(&p->from, v, s, 1))
  899. return 1;
  900. if(!copyas(&p->to, v))
  901. if(copysub(&p->to, v, s, 1))
  902. return 1;
  903. return 0;
  904. }
  905. if(copyas(&p->to, v)) {
  906. if(copyau(&p->from, v))
  907. return 4;
  908. return 3;
  909. }
  910. if(copyau(&p->from, v))
  911. return 1;
  912. if(copyau(&p->to, v))
  913. return 1;
  914. return 0;
  915. case AADD: /* read, read, write */
  916. case ASUB:
  917. case ARSB:
  918. case ASLL:
  919. case ASRL:
  920. case ASRA:
  921. case AORR:
  922. case AAND:
  923. case AEOR:
  924. case AMUL:
  925. case ADIV:
  926. case ADIVU:
  927. case AADDF:
  928. case AADDD:
  929. case ASUBF:
  930. case ASUBD:
  931. case AMULF:
  932. case AMULD:
  933. case ADIVF:
  934. case ADIVD:
  935. case ACMPF:
  936. case ACMPD:
  937. case ACMP:
  938. case ACMN:
  939. case ACASE:
  940. if(s != A) {
  941. if(copysub(&p->from, v, s, 1))
  942. return 1;
  943. if(copysub1(p, v, s, 1))
  944. return 1;
  945. if(!copyas(&p->to, v))
  946. if(copysub(&p->to, v, s, 1))
  947. return 1;
  948. return 0;
  949. }
  950. if(copyas(&p->to, v)) {
  951. if(p->reg == NREG)
  952. p->reg = p->to.reg;
  953. if(copyau(&p->from, v))
  954. return 4;
  955. if(copyau1(p, v))
  956. return 4;
  957. return 3;
  958. }
  959. if(copyau(&p->from, v))
  960. return 1;
  961. if(copyau1(p, v))
  962. return 1;
  963. if(copyau(&p->to, v))
  964. return 1;
  965. return 0;
  966. case ABEQ: /* read, read */
  967. case ABNE:
  968. case ABCS:
  969. case ABHS:
  970. case ABCC:
  971. case ABLO:
  972. case ABMI:
  973. case ABPL:
  974. case ABVS:
  975. case ABVC:
  976. case ABHI:
  977. case ABLS:
  978. case ABGE:
  979. case ABLT:
  980. case ABGT:
  981. case ABLE:
  982. if(s != A) {
  983. if(copysub(&p->from, v, s, 1))
  984. return 1;
  985. return copysub1(p, v, s, 1);
  986. }
  987. if(copyau(&p->from, v))
  988. return 1;
  989. if(copyau1(p, v))
  990. return 1;
  991. return 0;
  992. case AB: /* funny */
  993. if(s != A) {
  994. if(copysub(&p->to, v, s, 1))
  995. return 1;
  996. return 0;
  997. }
  998. if(copyau(&p->to, v))
  999. return 1;
  1000. return 0;
  1001. case ARET: /* funny */
  1002. if(v->type == D_REG)
  1003. if(v->reg == REGRET)
  1004. return 2;
  1005. if(v->type == D_FREG)
  1006. if(v->reg == FREGRET)
  1007. return 2;
  1008. case ABL: /* funny */
  1009. if(v->type == D_REG) {
  1010. if(v->reg <= REGEXT && v->reg > exregoffset)
  1011. return 2;
  1012. if(v->reg == REGARG)
  1013. return 2;
  1014. }
  1015. if(v->type == D_FREG)
  1016. if(v->reg <= FREGEXT && v->reg > exfregoffset)
  1017. return 2;
  1018. if(s != A) {
  1019. if(copysub(&p->to, v, s, 1))
  1020. return 1;
  1021. return 0;
  1022. }
  1023. if(copyau(&p->to, v))
  1024. return 4;
  1025. return 3;
  1026. case ATEXT: /* funny */
  1027. if(v->type == D_REG)
  1028. if(v->reg == REGARG)
  1029. return 3;
  1030. return 0;
  1031. }
  1032. return 0;
  1033. }
  1034. int
  1035. a2type(Prog *p)
  1036. {
  1037. switch(p->as) {
  1038. case ACMP:
  1039. case ACMN:
  1040. case AADD:
  1041. case ASUB:
  1042. case ARSB:
  1043. case ASLL:
  1044. case ASRL:
  1045. case ASRA:
  1046. case AORR:
  1047. case AAND:
  1048. case AEOR:
  1049. case AMUL:
  1050. case ADIV:
  1051. case ADIVU:
  1052. return D_REG;
  1053. case ACMPF:
  1054. case ACMPD:
  1055. case AADDF:
  1056. case AADDD:
  1057. case ASUBF:
  1058. case ASUBD:
  1059. case AMULF:
  1060. case AMULD:
  1061. case ADIVF:
  1062. case ADIVD:
  1063. return D_FREG;
  1064. }
  1065. return D_NONE;
  1066. }
  1067. /*
  1068. * direct reference,
  1069. * could be set/use depending on
  1070. * semantics
  1071. */
  1072. int
  1073. copyas(Adr *a, Adr *v)
  1074. {
  1075. if(regtyp(v)) {
  1076. if(a->type == v->type)
  1077. if(a->reg == v->reg)
  1078. return 1;
  1079. } else if(v->type == D_CONST) { /* for constprop */
  1080. if(a->type == v->type)
  1081. if(a->name == v->name)
  1082. if(a->sym == v->sym)
  1083. if(a->reg == v->reg)
  1084. if(a->offset == v->offset)
  1085. return 1;
  1086. }
  1087. return 0;
  1088. }
  1089. /*
  1090. * either direct or indirect
  1091. */
  1092. int
  1093. copyau(Adr *a, Adr *v)
  1094. {
  1095. if(copyas(a, v))
  1096. return 1;
  1097. if(v->type == D_REG) {
  1098. if(a->type == D_OREG) {
  1099. if(v->reg == a->reg)
  1100. return 1;
  1101. } else if(a->type == D_SHIFT) {
  1102. if((a->offset&0xf) == v->reg)
  1103. return 1;
  1104. if((a->offset&(1<<4)) && (a->offset>>8) == v->reg)
  1105. return 1;
  1106. }
  1107. }
  1108. return 0;
  1109. }
  1110. int
  1111. copyau1(Prog *p, Adr *v)
  1112. {
  1113. if(regtyp(v)) {
  1114. if(a2type(p) == v->type)
  1115. if(p->reg == v->reg) {
  1116. if(a2type(p) != v->type)
  1117. print("botch a2type %P\n", p);
  1118. return 1;
  1119. }
  1120. }
  1121. return 0;
  1122. }
  1123. /*
  1124. * substitute s for v in a
  1125. * return failure to substitute
  1126. */
  1127. int
  1128. copysub(Adr *a, Adr *v, Adr *s, int f)
  1129. {
  1130. if(f)
  1131. if(copyau(a, v)) {
  1132. if(a->type == D_SHIFT) {
  1133. if((a->offset&0xf) == v->reg)
  1134. a->offset = (a->offset&~0xf)|s->reg;
  1135. if((a->offset&(1<<4)) && (a->offset>>8) == v->reg)
  1136. a->offset = (a->offset&~(0xf<<8))|(s->reg<<8);
  1137. } else
  1138. a->reg = s->reg;
  1139. }
  1140. return 0;
  1141. }
  1142. int
  1143. copysub1(Prog *p1, Adr *v, Adr *s, int f)
  1144. {
  1145. if(f)
  1146. if(copyau1(p1, v))
  1147. p1->reg = s->reg;
  1148. return 0;
  1149. }
  1150. struct {
  1151. int opcode;
  1152. int notopcode;
  1153. int scond;
  1154. int notscond;
  1155. } predinfo[] = {
  1156. { ABEQ, ABNE, 0x0, 0x1, },
  1157. { ABNE, ABEQ, 0x1, 0x0, },
  1158. { ABCS, ABCC, 0x2, 0x3, },
  1159. { ABHS, ABLO, 0x2, 0x3, },
  1160. { ABCC, ABCS, 0x3, 0x2, },
  1161. { ABLO, ABHS, 0x3, 0x2, },
  1162. { ABMI, ABPL, 0x4, 0x5, },
  1163. { ABPL, ABMI, 0x5, 0x4, },
  1164. { ABVS, ABVC, 0x6, 0x7, },
  1165. { ABVC, ABVS, 0x7, 0x6, },
  1166. { ABHI, ABLS, 0x8, 0x9, },
  1167. { ABLS, ABHI, 0x9, 0x8, },
  1168. { ABGE, ABLT, 0xA, 0xB, },
  1169. { ABLT, ABGE, 0xB, 0xA, },
  1170. { ABGT, ABLE, 0xC, 0xD, },
  1171. { ABLE, ABGT, 0xD, 0xC, },
  1172. };
  1173. typedef struct {
  1174. Reg *start;
  1175. Reg *last;
  1176. Reg *end;
  1177. int len;
  1178. } Joininfo;
  1179. enum {
  1180. Join,
  1181. Split,
  1182. End,
  1183. Branch,
  1184. Setcond,
  1185. Toolong
  1186. };
  1187. enum {
  1188. Falsecond,
  1189. Truecond,
  1190. Delbranch,
  1191. Keepbranch
  1192. };
  1193. int
  1194. isbranch(Prog *p)
  1195. {
  1196. return (ABEQ <= p->as) && (p->as <= ABLE);
  1197. }
  1198. int
  1199. predicable(Prog *p)
  1200. {
  1201. if (isbranch(p)
  1202. || p->as == ANOP
  1203. || p->as == AXXX
  1204. || p->as == ADATA
  1205. || p->as == AGLOBL
  1206. || p->as == AGOK
  1207. || p->as == AHISTORY
  1208. || p->as == ANAME
  1209. || p->as == ATEXT
  1210. || p->as == AWORD
  1211. || p->as == ADYNT
  1212. || p->as == AINIT
  1213. || p->as == ABCASE
  1214. || p->as == ACASE)
  1215. return 0;
  1216. return 1;
  1217. }
  1218. /*
  1219. * Depends on an analysis of the encodings performed by 5l.
  1220. * These seem to be all of the opcodes that lead to the "S" bit
  1221. * being set in the instruction encodings.
  1222. *
  1223. * C_SBIT may also have been set explicitly in p->scond.
  1224. */
  1225. int
  1226. modifiescpsr(Prog *p)
  1227. {
  1228. return (p->scond&C_SBIT)
  1229. || p->as == ATST
  1230. || p->as == ATEQ
  1231. || p->as == ACMN
  1232. || p->as == ACMP
  1233. || p->as == AMULU
  1234. || p->as == ADIVU
  1235. || p->as == AMUL
  1236. || p->as == ADIV
  1237. || p->as == AMOD
  1238. || p->as == AMODU
  1239. || p->as == ABL;
  1240. }
  1241. /*
  1242. * Find the maximal chain of instructions starting with r which could
  1243. * be executed conditionally
  1244. */
  1245. int
  1246. joinsplit(Reg *r, Joininfo *j)
  1247. {
  1248. j->start = r;
  1249. j->last = r;
  1250. j->len = 0;
  1251. do {
  1252. if (r->p2 && (r->p1 || r->p2->p2link)) {
  1253. j->end = r;
  1254. return Join;
  1255. }
  1256. if (r->s1 && r->s2) {
  1257. j->end = r;
  1258. return Split;
  1259. }
  1260. j->last = r;
  1261. if (r->prog->as != ANOP)
  1262. j->len++;
  1263. if (!r->s1 && !r->s2) {
  1264. j->end = r->link;
  1265. return End;
  1266. }
  1267. if (r->s2) {
  1268. j->end = r->s2;
  1269. return Branch;
  1270. }
  1271. if (modifiescpsr(r->prog)) {
  1272. j->end = r->s1;
  1273. return Setcond;
  1274. }
  1275. r = r->s1;
  1276. } while (j->len < 4);
  1277. j->end = r;
  1278. return Toolong;
  1279. }
  1280. Reg *
  1281. successor(Reg *r)
  1282. {
  1283. if (r->s1)
  1284. return r->s1;
  1285. else
  1286. return r->s2;
  1287. }
  1288. void
  1289. applypred(Reg *rstart, Joininfo *j, int cond, int branch)
  1290. {
  1291. int pred;
  1292. Reg *r;
  1293. if(j->len == 0)
  1294. return;
  1295. if (cond == Truecond)
  1296. pred = predinfo[rstart->prog->as - ABEQ].scond;
  1297. else
  1298. pred = predinfo[rstart->prog->as - ABEQ].notscond;
  1299. for (r = j->start; ; r = successor(r)) {
  1300. if (r->prog->as == AB) {
  1301. if (r != j->last || branch == Delbranch)
  1302. excise(r);
  1303. else {
  1304. if (cond == Truecond)
  1305. r->prog->as = predinfo[rstart->prog->as - ABEQ].opcode;
  1306. else
  1307. r->prog->as = predinfo[rstart->prog->as - ABEQ].notopcode;
  1308. }
  1309. }
  1310. else if (predicable(r->prog))
  1311. r->prog->scond = (r->prog->scond&~C_SCOND)|pred;
  1312. if (r->s1 != r->link) {
  1313. r->s1 = r->link;
  1314. r->link->p1 = r;
  1315. }
  1316. if (r == j->last)
  1317. break;
  1318. }
  1319. }
  1320. void
  1321. predicate(void)
  1322. {
  1323. Reg *r;
  1324. int t1, t2;
  1325. Joininfo j1, j2;
  1326. for(r=firstr; r!=R; r=r->link) {
  1327. if (isbranch(r->prog)) {
  1328. t1 = joinsplit(r->s1, &j1);
  1329. t2 = joinsplit(r->s2, &j2);
  1330. if(j1.last->link != j2.start)
  1331. continue;
  1332. if(j1.end == j2.end)
  1333. if((t1 == Branch && (t2 == Join || t2 == Setcond)) ||
  1334. (t2 == Join && (t1 == Join || t1 == Setcond))) {
  1335. applypred(r, &j1, Falsecond, Delbranch);
  1336. applypred(r, &j2, Truecond, Delbranch);
  1337. excise(r);
  1338. continue;
  1339. }
  1340. if(t1 == End || t1 == Branch) {
  1341. applypred(r, &j1, Falsecond, Keepbranch);
  1342. excise(r);
  1343. continue;
  1344. }
  1345. }
  1346. }
  1347. }