peep.c 24 KB

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