peep.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "gc.h"
  10. void
  11. peep(void)
  12. {
  13. Reg *r, *r1, *r2;
  14. Prog *p, *p1;
  15. int t;
  16. /*
  17. * complete R structure
  18. */
  19. t = 0;
  20. for(r=firstr; r!=R; r=r1) {
  21. r1 = r->link;
  22. if(r1 == R)
  23. break;
  24. p = r->prog->link;
  25. while(p != r1->prog)
  26. switch(p->as) {
  27. default:
  28. r2 = rega();
  29. r->link = r2;
  30. r2->link = r1;
  31. r2->prog = p;
  32. r2->p1 = r;
  33. r->s1 = r2;
  34. r2->s1 = r1;
  35. r1->p1 = r2;
  36. r = r2;
  37. t++;
  38. case ADATA:
  39. case AGLOBL:
  40. case ANAME:
  41. case ASIGNAME:
  42. p = p->link;
  43. }
  44. }
  45. loop1:
  46. t = 0;
  47. for(r=firstr; r!=R; r=r->link) {
  48. p = r->prog;
  49. if(p->as == AMOVW || p->as == AFMOVS || p->as == AFMOVD)
  50. if(regtyp(&p->to)) {
  51. if(regtyp(&p->from))
  52. if(p->from.type == p->to.type) {
  53. if(copyprop(r)) {
  54. excise(r);
  55. t++;
  56. } else
  57. if(subprop(r) && copyprop(r)) {
  58. excise(r);
  59. t++;
  60. }
  61. }
  62. if(regzer(&p->from))
  63. if(p->to.type == D_REG) {
  64. p->from.type = D_REG;
  65. p->from.reg = REGZERO;
  66. if(copyprop(r)) {
  67. excise(r);
  68. t++;
  69. } else
  70. if(subprop(r) && copyprop(r)) {
  71. excise(r);
  72. t++;
  73. }
  74. }
  75. }
  76. }
  77. if(t)
  78. goto loop1;
  79. /*
  80. * look for MOVB x,R; MOVB R,R
  81. */
  82. for(r=firstr; r!=R; r=r->link) {
  83. p = r->prog;
  84. switch(p->as) {
  85. default:
  86. continue;
  87. case AMOVH:
  88. case AMOVHZ:
  89. case AMOVB:
  90. case AMOVBZ:
  91. if(p->to.type != D_REG)
  92. continue;
  93. break;
  94. }
  95. r1 = r->link;
  96. if(r1 == R)
  97. continue;
  98. p1 = r1->prog;
  99. if(p1->as != p->as)
  100. continue;
  101. if(p1->from.type != D_REG || p1->from.reg != p->to.reg)
  102. continue;
  103. if(p1->to.type != D_REG || p1->to.reg != p->to.reg)
  104. continue;
  105. excise(r1);
  106. }
  107. if(debug['Q'] > 1)
  108. return; /* allow following code improvement to be suppressed */
  109. /*
  110. * look for OP x,y,R; CMP R, $0 -> OPCC x,y,R
  111. * when OP can set condition codes correctly
  112. */
  113. for(r=firstr; r!=R; r=r->link) {
  114. p = r->prog;
  115. switch(p->as) {
  116. case ACMP:
  117. if(!regzer(&p->to))
  118. continue;
  119. r1 = r->s1;
  120. if(r1 == R)
  121. continue;
  122. switch(r1->prog->as) {
  123. default:
  124. continue;
  125. case ABCL:
  126. case ABC:
  127. /* the conditions can be complex and these are currently little used */
  128. continue;
  129. case ABEQ:
  130. case ABGE:
  131. case ABGT:
  132. case ABLE:
  133. case ABLT:
  134. case ABNE:
  135. case ABVC:
  136. case ABVS:
  137. break;
  138. }
  139. r1 = r;
  140. do
  141. r1 = uniqp(r1);
  142. while (r1 != R && r1->prog->as == ANOP);
  143. if(r1 == R)
  144. continue;
  145. p1 = r1->prog;
  146. if(p1->to.type != D_REG || p1->to.reg != p->from.reg)
  147. continue;
  148. switch(p1->as) {
  149. case ASUB:
  150. case AADD:
  151. case AXOR:
  152. case AOR:
  153. /* irregular instructions */
  154. if(p1->from.type == D_CONST)
  155. continue;
  156. break;
  157. }
  158. switch(p1->as) {
  159. default:
  160. continue;
  161. case AMOVW:
  162. if(p1->from.type != D_REG)
  163. continue;
  164. continue;
  165. case AANDCC:
  166. case AANDNCC:
  167. case AORCC:
  168. case AORNCC:
  169. case AXORCC:
  170. case ASUBCC:
  171. case ASUBECC:
  172. case ASUBMECC:
  173. case ASUBZECC:
  174. case AADDCC:
  175. case AADDCCC:
  176. case AADDECC:
  177. case AADDMECC:
  178. case AADDZECC:
  179. case ARLWMICC:
  180. case ARLWNMCC:
  181. t = p1->as;
  182. break;
  183. /* don't deal with floating point instructions for now */
  184. /*
  185. case AFABS: t = AFABSCC; break;
  186. case AFADD: t = AFADDCC; break;
  187. case AFADDS: t = AFADDSCC; break;
  188. case AFCTIW: t = AFCTIWCC; break;
  189. case AFCTIWZ: t = AFCTIWZCC; break;
  190. case AFDIV: t = AFDIVCC; break;
  191. case AFDIVS: t = AFDIVSCC; break;
  192. case AFMADD: t = AFMADDCC; break;
  193. case AFMADDS: t = AFMADDSCC; break;
  194. case AFMOVD: t = AFMOVDCC; break;
  195. case AFMSUB: t = AFMSUBCC; break;
  196. case AFMSUBS: t = AFMSUBSCC; break;
  197. case AFMUL: t = AFMULCC; break;
  198. case AFMULS: t = AFMULSCC; break;
  199. case AFNABS: t = AFNABSCC; break;
  200. case AFNEG: t = AFNEGCC; break;
  201. case AFNMADD: t = AFNMADDCC; break;
  202. case AFNMADDS: t = AFNMADDSCC; break;
  203. case AFNMSUB: t = AFNMSUBCC; break;
  204. case AFNMSUBS: t = AFNMSUBSCC; break;
  205. case AFRSP: t = AFRSPCC; break;
  206. case AFSUB: t = AFSUBCC; break;
  207. case AFSUBS: t = AFSUBSCC; break;
  208. case ACNTLZW: t = ACNTLZWCC; break;
  209. case AMTFSB0: t = AMTFSB0CC; break;
  210. case AMTFSB1: t = AMTFSB1CC; break;
  211. */
  212. case AADD: t = AADDCC; break;
  213. case AADDV: t = AADDVCC; break;
  214. case AADDC: t = AADDCCC; break;
  215. case AADDCV: t = AADDCVCC; break;
  216. case AADDME: t = AADDMECC; break;
  217. case AADDMEV: t = AADDMEVCC; break;
  218. case AADDE: t = AADDECC; break;
  219. case AADDEV: t = AADDEVCC; break;
  220. case AADDZE: t = AADDZECC; break;
  221. case AADDZEV: t = AADDZEVCC; break;
  222. case AAND: t = AANDCC; break;
  223. case AANDN: t = AANDNCC; break;
  224. case ADIVW: t = ADIVWCC; break;
  225. case ADIVWV: t = ADIVWVCC; break;
  226. case ADIVWU: t = ADIVWUCC; break;
  227. case ADIVWUV: t = ADIVWUVCC; break;
  228. case AEQV: t = AEQVCC; break;
  229. case AEXTSB: t = AEXTSBCC; break;
  230. case AEXTSH: t = AEXTSHCC; break;
  231. case AMULHW: t = AMULHWCC; break;
  232. case AMULHWU: t = AMULHWUCC; break;
  233. case AMULLW: t = AMULLWCC; break;
  234. case AMULLWV: t = AMULLWVCC; break;
  235. case ANAND: t = ANANDCC; break;
  236. case ANEG: t = ANEGCC; break;
  237. case ANEGV: t = ANEGVCC; break;
  238. case ANOR: t = ANORCC; break;
  239. case AOR: t = AORCC; break;
  240. case AORN: t = AORNCC; break;
  241. case AREM: t = AREMCC; break;
  242. case AREMV: t = AREMVCC; break;
  243. case AREMU: t = AREMUCC; break;
  244. case AREMUV: t = AREMUVCC; break;
  245. case ARLWMI: t = ARLWMICC; break;
  246. case ARLWNM: t = ARLWNMCC; break;
  247. case ASLW: t = ASLWCC; break;
  248. case ASRAW: t = ASRAWCC; break;
  249. case ASRW: t = ASRWCC; break;
  250. case ASUB: t = ASUBCC; break;
  251. case ASUBV: t = ASUBVCC; break;
  252. case ASUBC: t = ASUBCCC; break;
  253. case ASUBCV: t = ASUBCVCC; break;
  254. case ASUBME: t = ASUBMECC; break;
  255. case ASUBMEV: t = ASUBMEVCC; break;
  256. case ASUBE: t = ASUBECC; break;
  257. case ASUBEV: t = ASUBEVCC; break;
  258. case ASUBZE: t = ASUBZECC; break;
  259. case ASUBZEV: t = ASUBZEVCC; break;
  260. case AXOR: t = AXORCC; break;
  261. break;
  262. }
  263. if(debug['Q'])
  264. print("cmp %P; %P -> ", p1, p);
  265. p1->as = t;
  266. if(debug['Q'])
  267. print("%P\n", p1);
  268. excise(r);
  269. continue;
  270. }
  271. }
  272. }
  273. void
  274. excise(Reg *r)
  275. {
  276. Prog *p;
  277. p = r->prog;
  278. p->as = ANOP;
  279. p->from = zprog.from;
  280. p->from3 = zprog.from3;
  281. p->to = zprog.to;
  282. p->reg = zprog.reg; /**/
  283. }
  284. Reg*
  285. uniqp(Reg *r)
  286. {
  287. Reg *r1;
  288. r1 = r->p1;
  289. if(r1 == R) {
  290. r1 = r->p2;
  291. if(r1 == R || r1->p2link != R)
  292. return R;
  293. } else
  294. if(r->p2 != R)
  295. return R;
  296. return r1;
  297. }
  298. Reg*
  299. uniqs(Reg *r)
  300. {
  301. Reg *r1;
  302. r1 = r->s1;
  303. if(r1 == R) {
  304. r1 = r->s2;
  305. if(r1 == R)
  306. return R;
  307. } else
  308. if(r->s2 != R)
  309. return R;
  310. return r1;
  311. }
  312. /*
  313. * if the system forces R0 to be zero,
  314. * convert references to $0 to references to R0.
  315. */
  316. regzer(Adr *a)
  317. {
  318. if(R0ISZERO) {
  319. if(a->type == D_CONST)
  320. if(a->sym == S)
  321. if(a->offset == 0)
  322. return 1;
  323. if(a->type == D_REG)
  324. if(a->reg == REGZERO)
  325. return 1;
  326. }
  327. return 0;
  328. }
  329. regtyp(Adr *a)
  330. {
  331. if(a->type == D_REG) {
  332. if(!R0ISZERO || a->reg != REGZERO)
  333. return 1;
  334. return 0;
  335. }
  336. if(a->type == D_FREG)
  337. return 1;
  338. return 0;
  339. }
  340. /*
  341. * the idea is to substitute
  342. * one register for another
  343. * from one MOV to another
  344. * MOV a, R0
  345. * ADD b, R0 / no use of R1
  346. * MOV R0, R1
  347. * would be converted to
  348. * MOV a, R1
  349. * ADD b, R1
  350. * MOV R1, R0
  351. * hopefully, then the former or latter MOV
  352. * will be eliminated by copy propagation.
  353. */
  354. int
  355. subprop(Reg *r0)
  356. {
  357. Prog *p;
  358. Adr *v1, *v2;
  359. Reg *r;
  360. int t;
  361. p = r0->prog;
  362. v1 = &p->from;
  363. if(!regtyp(v1))
  364. return 0;
  365. v2 = &p->to;
  366. if(!regtyp(v2))
  367. return 0;
  368. for(r=uniqp(r0); r!=R; r=uniqp(r)) {
  369. if(uniqs(r) == R)
  370. break;
  371. p = r->prog;
  372. switch(p->as) {
  373. case ABL:
  374. return 0;
  375. case AADD:
  376. case AADDC:
  377. case AADDCC:
  378. case AADDE:
  379. case AADDECC:
  380. case ASUB:
  381. case ASUBCC:
  382. case ASUBC:
  383. case ASUBCCC:
  384. case ASUBE:
  385. case ASUBECC:
  386. case ASLW:
  387. case ASLWCC:
  388. case ASRW:
  389. case ASRWCC:
  390. case ASRAW:
  391. case ASRAWCC:
  392. case AOR:
  393. case AORCC:
  394. case AORN:
  395. case AORNCC:
  396. case AAND:
  397. case AANDCC:
  398. case AANDN:
  399. case AANDNCC:
  400. case ANAND:
  401. case ANANDCC:
  402. case ANOR:
  403. case ANORCC:
  404. case AXOR:
  405. case AXORCC:
  406. case AMULHW:
  407. case AMULHWU:
  408. case AMULLW:
  409. case ADIVW:
  410. case ADIVWU:
  411. case AREM:
  412. case AREMU:
  413. case ARLWNM:
  414. case ARLWNMCC:
  415. case AFADD:
  416. case AFADDS:
  417. case AFSUB:
  418. case AFSUBS:
  419. case AFMUL:
  420. case AFMULS:
  421. case AFDIV:
  422. case AFDIVS:
  423. if(p->to.type == v1->type)
  424. if(p->to.reg == v1->reg) {
  425. if(p->reg == NREG)
  426. p->reg = p->to.reg;
  427. goto gotit;
  428. }
  429. break;
  430. case AADDME:
  431. case AADDMECC:
  432. case AADDZE:
  433. case AADDZECC:
  434. case ASUBME:
  435. case ASUBMECC:
  436. case ASUBZE:
  437. case ASUBZECC:
  438. case ANEG:
  439. case ANEGCC:
  440. case AFNEG:
  441. case AFNEGCC:
  442. case AFMOVS:
  443. case AFMOVD:
  444. case AMOVW:
  445. if(p->to.type == v1->type)
  446. if(p->to.reg == v1->reg)
  447. goto gotit;
  448. break;
  449. }
  450. if(copyau(&p->from, v2) ||
  451. copyau1(p, v2) ||
  452. copyau(&p->to, v2))
  453. break;
  454. if(copysub(&p->from, v1, v2, 0) ||
  455. copysub1(p, v1, v2, 0) ||
  456. copysub(&p->to, v1, v2, 0))
  457. break;
  458. }
  459. return 0;
  460. gotit:
  461. copysub(&p->to, v1, v2, 1);
  462. if(debug['P']) {
  463. print("gotit: %D->%D\n%P", v1, v2, r->prog);
  464. if(p->from.type == v2->type)
  465. print(" excise");
  466. print("\n");
  467. }
  468. for(r=uniqs(r); r!=r0; r=uniqs(r)) {
  469. p = r->prog;
  470. copysub(&p->from, v1, v2, 1);
  471. copysub1(p, v1, v2, 1);
  472. copysub(&p->to, v1, v2, 1);
  473. if(debug['P'])
  474. print("%P\n", r->prog);
  475. }
  476. t = v1->reg;
  477. v1->reg = v2->reg;
  478. v2->reg = t;
  479. if(debug['P'])
  480. print("%P last\n", r->prog);
  481. return 1;
  482. }
  483. /*
  484. * The idea is to remove redundant copies.
  485. * v1->v2 F=0
  486. * (use v2 s/v2/v1/)*
  487. * set v1 F=1
  488. * use v2 return fail
  489. * -----------------
  490. * v1->v2 F=0
  491. * (use v2 s/v2/v1/)*
  492. * set v1 F=1
  493. * set v2 return success
  494. */
  495. int
  496. copyprop(Reg *r0)
  497. {
  498. Prog *p;
  499. Adr *v1, *v2;
  500. Reg *r;
  501. p = r0->prog;
  502. v1 = &p->from;
  503. v2 = &p->to;
  504. if(copyas(v1, v2))
  505. return 1;
  506. for(r=firstr; r!=R; r=r->link)
  507. r->active = 0;
  508. return copy1(v1, v2, r0->s1, 0);
  509. }
  510. copy1(Adr *v1, Adr *v2, Reg *r, int f)
  511. {
  512. int t;
  513. Prog *p;
  514. if(r->active) {
  515. if(debug['P'])
  516. print("act set; return 1\n");
  517. return 1;
  518. }
  519. r->active = 1;
  520. if(debug['P'])
  521. print("copy %D->%D f=%d\n", v1, v2, f);
  522. for(; r != R; r = r->s1) {
  523. p = r->prog;
  524. if(debug['P'])
  525. print("%P", p);
  526. if(!f && uniqp(r) == R) {
  527. f = 1;
  528. if(debug['P'])
  529. print("; merge; f=%d", f);
  530. }
  531. t = copyu(p, v2, A);
  532. switch(t) {
  533. case 2: /* rar, cant split */
  534. if(debug['P'])
  535. print("; %Drar; return 0\n", v2);
  536. return 0;
  537. case 3: /* set */
  538. if(debug['P'])
  539. print("; %Dset; return 1\n", v2);
  540. return 1;
  541. case 1: /* used, substitute */
  542. case 4: /* use and set */
  543. if(f) {
  544. if(!debug['P'])
  545. return 0;
  546. if(t == 4)
  547. print("; %Dused+set and f=%d; return 0\n", v2, f);
  548. else
  549. print("; %Dused and f=%d; return 0\n", v2, f);
  550. return 0;
  551. }
  552. if(copyu(p, v2, v1)) {
  553. if(debug['P'])
  554. print("; sub fail; return 0\n");
  555. return 0;
  556. }
  557. if(debug['P'])
  558. print("; sub%D/%D", v2, v1);
  559. if(t == 4) {
  560. if(debug['P'])
  561. print("; %Dused+set; return 1\n", v2);
  562. return 1;
  563. }
  564. break;
  565. }
  566. if(!f) {
  567. t = copyu(p, v1, A);
  568. if(!f && (t == 2 || t == 3 || t == 4)) {
  569. f = 1;
  570. if(debug['P'])
  571. print("; %Dset and !f; f=%d", v1, f);
  572. }
  573. }
  574. if(debug['P'])
  575. print("\n");
  576. if(r->s2)
  577. if(!copy1(v1, v2, r->s2, f))
  578. return 0;
  579. }
  580. return 1;
  581. }
  582. /*
  583. * return
  584. * 1 if v only used (and substitute),
  585. * 2 if read-alter-rewrite
  586. * 3 if set
  587. * 4 if set and used
  588. * 0 otherwise (not touched)
  589. */
  590. int
  591. copyu(Prog *p, Adr *v, Adr *s)
  592. {
  593. switch(p->as) {
  594. default:
  595. if(debug['P'])
  596. print(" (???)");
  597. return 2;
  598. case ANOP: /* read, write */
  599. case AMOVW:
  600. case AMOVH:
  601. case AMOVHZ:
  602. case AMOVB:
  603. case AMOVBZ:
  604. case ANEG:
  605. case ANEGCC:
  606. case AADDME:
  607. case AADDMECC:
  608. case AADDZE:
  609. case AADDZECC:
  610. case ASUBME:
  611. case ASUBMECC:
  612. case ASUBZE:
  613. case ASUBZECC:
  614. case AFCTIW:
  615. case AFCTIWZ:
  616. case AFMOVS:
  617. case AFMOVD:
  618. case AFRSP:
  619. case AFNEG:
  620. case AFNEGCC:
  621. if(s != A) {
  622. if(copysub(&p->from, v, s, 1))
  623. return 1;
  624. if(!copyas(&p->to, v))
  625. if(copysub(&p->to, v, s, 1))
  626. return 1;
  627. return 0;
  628. }
  629. if(copyas(&p->to, v)) {
  630. if(copyau(&p->from, v))
  631. return 4;
  632. return 3;
  633. }
  634. if(copyau(&p->from, v))
  635. return 1;
  636. if(copyau(&p->to, v))
  637. return 1;
  638. return 0;
  639. case ARLWMI: /* read read rar */
  640. case ARLWMICC:
  641. if(copyas(&p->to, v))
  642. return 2;
  643. /* fall through */
  644. case AADD: /* read read write */
  645. case AADDC:
  646. case AADDE:
  647. case ASUB:
  648. case ASLW:
  649. case ASRW:
  650. case ASRAW:
  651. case AOR:
  652. case AORCC:
  653. case AORN:
  654. case AORNCC:
  655. case AAND:
  656. case AANDCC:
  657. case AANDN:
  658. case AANDNCC:
  659. case ANAND:
  660. case ANANDCC:
  661. case ANOR:
  662. case ANORCC:
  663. case AXOR:
  664. case AMULHW:
  665. case AMULHWU:
  666. case AMULLW:
  667. case ADIVW:
  668. case ADIVWU:
  669. case AREM:
  670. case AREMU:
  671. case ARLWNM:
  672. case ARLWNMCC:
  673. case AFADDS:
  674. case AFADD:
  675. case AFSUBS:
  676. case AFSUB:
  677. case AFMULS:
  678. case AFMUL:
  679. case AFDIVS:
  680. case AFDIV:
  681. if(s != A) {
  682. if(copysub(&p->from, v, s, 1))
  683. return 1;
  684. if(copysub1(p, v, s, 1))
  685. return 1;
  686. if(!copyas(&p->to, v))
  687. if(copysub(&p->to, v, s, 1))
  688. return 1;
  689. return 0;
  690. }
  691. if(copyas(&p->to, v)) {
  692. if(p->reg == NREG)
  693. p->reg = p->to.reg;
  694. if(copyau(&p->from, v))
  695. return 4;
  696. if(copyau1(p, v))
  697. return 4;
  698. return 3;
  699. }
  700. if(copyau(&p->from, v))
  701. return 1;
  702. if(copyau1(p, v))
  703. return 1;
  704. if(copyau(&p->to, v))
  705. return 1;
  706. return 0;
  707. case ABEQ:
  708. case ABGT:
  709. case ABGE:
  710. case ABLT:
  711. case ABLE:
  712. case ABNE:
  713. case ABVC:
  714. case ABVS:
  715. break;
  716. case ACMP: /* read read */
  717. case ACMPU:
  718. case AFCMPO:
  719. case AFCMPU:
  720. if(s != A) {
  721. if(copysub(&p->from, v, s, 1))
  722. return 1;
  723. return copysub(&p->to, v, s, 1);
  724. }
  725. if(copyau(&p->from, v))
  726. return 1;
  727. if(copyau(&p->to, v))
  728. return 1;
  729. break;
  730. case ABR: /* funny */
  731. if(s != A) {
  732. if(copysub(&p->to, v, s, 1))
  733. return 1;
  734. return 0;
  735. }
  736. if(copyau(&p->to, v))
  737. return 1;
  738. return 0;
  739. case ARETURN: /* funny */
  740. if(v->type == D_REG)
  741. if(v->reg == REGRET)
  742. return 2;
  743. if(v->type == D_FREG)
  744. if(v->reg == FREGRET)
  745. return 2;
  746. case ABL: /* funny */
  747. if(v->type == D_REG) {
  748. if(v->reg <= REGEXT && v->reg > exregoffset)
  749. return 2;
  750. if(v->reg == REGARG)
  751. return 2;
  752. }
  753. if(v->type == D_FREG) {
  754. if(v->reg <= FREGEXT && v->reg > exfregoffset)
  755. return 2;
  756. }
  757. if(s != A) {
  758. if(copysub(&p->to, v, s, 1))
  759. return 1;
  760. return 0;
  761. }
  762. if(copyau(&p->to, v))
  763. return 4;
  764. return 3;
  765. case ATEXT: /* funny */
  766. if(v->type == D_REG)
  767. if(v->reg == REGARG)
  768. return 3;
  769. return 0;
  770. }
  771. return 0;
  772. }
  773. int
  774. a2type(Prog *p)
  775. {
  776. switch(p->as) {
  777. case AADD:
  778. case AADDC:
  779. case AADDCC:
  780. case AADDCCC:
  781. case AADDE:
  782. case AADDECC:
  783. case AADDME:
  784. case AADDMECC:
  785. case AADDZE:
  786. case AADDZECC:
  787. case ASUB:
  788. case ASUBC:
  789. case ASUBCC:
  790. case ASUBCCC:
  791. case ASUBE:
  792. case ASUBECC:
  793. case ASUBME:
  794. case ASUBMECC:
  795. case ASUBZE:
  796. case ASUBZECC:
  797. case ASLW:
  798. case ASLWCC:
  799. case ASRW:
  800. case ASRWCC:
  801. case ASRAW:
  802. case ASRAWCC:
  803. case AOR:
  804. case AORCC:
  805. case AORN:
  806. case AORNCC:
  807. case AAND:
  808. case AANDCC:
  809. case AANDN:
  810. case AANDNCC:
  811. case AXOR:
  812. case AXORCC:
  813. case ANEG:
  814. case ANEGCC:
  815. case AMULHW:
  816. case AMULHWU:
  817. case AMULLW:
  818. case AMULLWCC:
  819. case ADIVW:
  820. case ADIVWCC:
  821. case ADIVWU:
  822. case ADIVWUCC:
  823. case AREM:
  824. case AREMCC:
  825. case AREMU:
  826. case AREMUCC:
  827. case ANAND:
  828. case ANANDCC:
  829. case ANOR:
  830. case ANORCC:
  831. case ARLWMI:
  832. case ARLWMICC:
  833. case ARLWNM:
  834. case ARLWNMCC:
  835. return D_REG;
  836. case AFADDS:
  837. case AFADDSCC:
  838. case AFADD:
  839. case AFADDCC:
  840. case AFSUBS:
  841. case AFSUBSCC:
  842. case AFSUB:
  843. case AFSUBCC:
  844. case AFMULS:
  845. case AFMULSCC:
  846. case AFMUL:
  847. case AFMULCC:
  848. case AFDIVS:
  849. case AFDIVSCC:
  850. case AFDIV:
  851. case AFDIVCC:
  852. case AFNEG:
  853. case AFNEGCC:
  854. return D_FREG;
  855. }
  856. return D_NONE;
  857. }
  858. /*
  859. * direct reference,
  860. * could be set/use depending on
  861. * semantics
  862. */
  863. int
  864. copyas(Adr *a, Adr *v)
  865. {
  866. if(regtyp(v))
  867. if(a->type == v->type)
  868. if(a->reg == v->reg)
  869. return 1;
  870. return 0;
  871. }
  872. /*
  873. * either direct or indirect
  874. */
  875. int
  876. copyau(Adr *a, Adr *v)
  877. {
  878. if(copyas(a, v))
  879. return 1;
  880. if(v->type == D_REG)
  881. if(a->type == D_OREG)
  882. if(v->reg == a->reg)
  883. return 1;
  884. return 0;
  885. }
  886. int
  887. copyau1(Prog *p, Adr *v)
  888. {
  889. if(regtyp(v))
  890. if(p->from.type == v->type || p->to.type == v->type)
  891. if(p->reg == v->reg) {
  892. if(a2type(p) != v->type)
  893. print("botch a2type %P\n", p);
  894. return 1;
  895. }
  896. return 0;
  897. }
  898. /*
  899. * substitute s for v in a
  900. * return failure to substitute
  901. */
  902. int
  903. copysub(Adr *a, Adr *v, Adr *s, int f)
  904. {
  905. if(f)
  906. if(copyau(a, v))
  907. a->reg = s->reg;
  908. return 0;
  909. }
  910. int
  911. copysub1(Prog *p1, Adr *v, Adr *s, int f)
  912. {
  913. if(f)
  914. if(copyau1(p1, v))
  915. p1->reg = s->reg;
  916. return 0;
  917. }