noop.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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 "l.h"
  10. void
  11. noops(void)
  12. {
  13. Prog *p, *p1, *q, *q1;
  14. int o, curframe, curbecome, maxbecome;
  15. /*
  16. * find leaf subroutines
  17. * become sizes
  18. * frame sizes
  19. * strip NOPs
  20. * expand RET
  21. * expand BECOME pseudo
  22. */
  23. if(debug['v'])
  24. Bprint(&bso, "%5.2f noops\n", cputime());
  25. Bflush(&bso);
  26. curframe = 0;
  27. curbecome = 0;
  28. maxbecome = 0;
  29. curtext = 0;
  30. q = P;
  31. for(p = firstp; p != P; p = p->link) {
  32. /* find out how much arg space is used in this TEXT */
  33. if(p->to.type == D_OREG && p->to.reg == REGSP)
  34. if(p->to.offset > curframe)
  35. curframe = p->to.offset;
  36. switch(p->as) {
  37. /* too hard, just leave alone */
  38. case ATEXT:
  39. if(curtext && curtext->from.sym) {
  40. curtext->from.sym->frame = curframe;
  41. curtext->from.sym->become = curbecome;
  42. if(curbecome > maxbecome)
  43. maxbecome = curbecome;
  44. }
  45. curframe = 0;
  46. curbecome = 0;
  47. q = p;
  48. p->mark |= LABEL|LEAF|SYNC;
  49. if(p->link)
  50. p->link->mark |= LABEL;
  51. curtext = p;
  52. break;
  53. case AORN:
  54. q = p;
  55. if(p->to.type == D_REG)
  56. if(p->to.reg == REGZERO)
  57. p->mark |= LABEL|SYNC;
  58. break;
  59. case AUNIMP:
  60. case ATAS:
  61. case ASWAP:
  62. case ATA:
  63. case ATCC:
  64. case ATCS:
  65. case ATE:
  66. case ATG:
  67. case ATGE:
  68. case ATGU:
  69. case ATL:
  70. case ATLE:
  71. case ATLEU:
  72. case ATN:
  73. case ATNE:
  74. case ATNEG:
  75. case ATPOS:
  76. case ATVC:
  77. case ATVS:
  78. case AWORD:
  79. q = p;
  80. p->mark |= LABEL|SYNC;
  81. continue;
  82. case AFABSD:
  83. case AFABSF:
  84. case AFABSX:
  85. case AFADDD:
  86. case AFADDF:
  87. case AFADDX:
  88. case AFDIVD:
  89. case AFDIVF:
  90. case AFDIVX:
  91. case AFMOVD:
  92. case AFMOVDF:
  93. case AFMOVDW:
  94. case AFMOVDX:
  95. case AFMOVF:
  96. case AFMOVFD:
  97. case AFMOVFW:
  98. case AFMOVFX:
  99. case AFMOVWD:
  100. case AFMOVWF:
  101. case AFMOVWX:
  102. case AFMOVX:
  103. case AFMOVXD:
  104. case AFMOVXF:
  105. case AFMOVXW:
  106. case AFMULD:
  107. case AFMULF:
  108. case AFMULX:
  109. case AFNEGD:
  110. case AFNEGF:
  111. case AFNEGX:
  112. case AFSQRTD:
  113. case AFSQRTF:
  114. case AFSQRTX:
  115. case AFSUBD:
  116. case AFSUBF:
  117. case AFSUBX:
  118. q = p;
  119. p->mark |= FLOAT;
  120. continue;
  121. case AMUL:
  122. case ADIV:
  123. case ADIVL:
  124. case AMOD:
  125. case AMODL:
  126. q = p;
  127. if(!debug['M']) {
  128. if(prog_mul == P)
  129. initmuldiv();
  130. if(curtext != P)
  131. curtext->mark &= ~LEAF;
  132. }
  133. continue;
  134. case AJMPL:
  135. if(curtext != P)
  136. curtext->mark &= ~LEAF;
  137. case AJMP:
  138. case ABA:
  139. case ABN:
  140. case ABE:
  141. case ABNE:
  142. case ABLE:
  143. case ABG:
  144. case ABL:
  145. case ABGE:
  146. case ABLEU:
  147. case ABGU:
  148. case ABCS:
  149. case ABCC:
  150. case ABNEG:
  151. case ABPOS:
  152. case ABVC:
  153. case ABVS:
  154. case AFBN:
  155. case AFBO:
  156. case AFBE:
  157. case AFBLG:
  158. case AFBG:
  159. case AFBLE:
  160. case AFBGE:
  161. case AFBL:
  162. case AFBNE:
  163. case AFBUE:
  164. case AFBA:
  165. case AFBU:
  166. case AFBUG:
  167. case AFBULE:
  168. case AFBUGE:
  169. case AFBUL:
  170. p->mark |= BRANCH;
  171. q = p;
  172. q1 = p->cond;
  173. if(q1 != P) {
  174. while(q1->as == ANOP) {
  175. q1 = q1->link;
  176. p->cond = q1;
  177. }
  178. if(!(q1->mark & LEAF))
  179. q1->mark |= LABEL;
  180. } else
  181. p->mark |= LABEL;
  182. q1 = p->link;
  183. if(q1 != P)
  184. q1->mark |= LABEL;
  185. continue;
  186. case AFCMPD:
  187. case AFCMPED:
  188. case AFCMPEF:
  189. case AFCMPEX:
  190. case AFCMPF:
  191. case AFCMPX:
  192. q = p;
  193. p->mark |= FCMP|FLOAT;
  194. continue;
  195. case ARETURN:
  196. /* special form of RETURN is BECOME */
  197. if(p->from.type == D_CONST)
  198. if(p->from.offset > curbecome)
  199. curbecome = p->from.offset;
  200. q = p;
  201. if(p->link != P)
  202. p->link->mark |= LABEL;
  203. continue;
  204. case ANOP:
  205. q1 = p->link;
  206. q->link = q1; /* q is non-nop */
  207. q1->mark |= p->mark;
  208. continue;
  209. default:
  210. q = p;
  211. continue;
  212. }
  213. }
  214. if(curtext && curtext->from.sym) {
  215. curtext->from.sym->frame = curframe;
  216. curtext->from.sym->become = curbecome;
  217. if(curbecome > maxbecome)
  218. maxbecome = curbecome;
  219. }
  220. if(debug['b'])
  221. print("max become = %d\n", maxbecome);
  222. xdefine("ALEFbecome", STEXT, maxbecome);
  223. curtext = 0;
  224. for(p = firstp; p != P; p = p->link) {
  225. switch(p->as) {
  226. case ATEXT:
  227. curtext = p;
  228. break;
  229. case AJMPL:
  230. if(curtext != P && curtext->from.sym != S && curtext->to.offset >= 0) {
  231. o = maxbecome - curtext->from.sym->frame;
  232. if(o <= 0)
  233. break;
  234. /* calling a become or calling a variable */
  235. if(p->to.sym == S || p->to.sym->become) {
  236. curtext->to.offset += o;
  237. if(debug['b']) {
  238. curp = p;
  239. print("%D calling %D increase %d\n",
  240. &curtext->from, &p->to, o);
  241. }
  242. }
  243. }
  244. break;
  245. }
  246. }
  247. curtext = P;
  248. for(p = firstp; p != P; p = p->link) {
  249. o = p->as;
  250. switch(o) {
  251. case ATEXT:
  252. curtext = p;
  253. autosize = p->to.offset + 4;
  254. if((p->mark & LEAF) && autosize <= 4)
  255. autosize = 0;
  256. else
  257. if(autosize & 4)
  258. autosize += 4;
  259. p->to.offset = autosize - 4;
  260. q = p;
  261. if(autosize) {
  262. q = prg();
  263. q->as = ASUB;
  264. q->line = p->line;
  265. q->from.type = D_CONST;
  266. q->from.offset = autosize;
  267. q->to.type = D_REG;
  268. q->to.reg = REGSP;
  269. q->link = p->link;
  270. p->link = q;
  271. } else
  272. if(!(curtext->mark & LEAF)) {
  273. if(debug['v'])
  274. Bprint(&bso, "save suppressed in: %s\n",
  275. curtext->from.sym->name);
  276. curtext->mark |= LEAF;
  277. }
  278. if(curtext->mark & LEAF) {
  279. if(curtext->from.sym)
  280. curtext->from.sym->type = SLEAF;
  281. break;
  282. }
  283. q1 = prg();
  284. q1->as = AMOVW;
  285. q1->line = p->line;
  286. q1->from.type = D_REG;
  287. q1->from.reg = REGLINK;
  288. q1->to.type = D_OREG;
  289. q1->from.offset = 0;
  290. q1->to.reg = REGSP;
  291. q1->link = q->link;
  292. q->link = q1;
  293. break;
  294. case AMUL:
  295. case ADIV:
  296. case ADIVL:
  297. case AMOD:
  298. case AMODL:
  299. if(debug['M'])
  300. break;
  301. if(p->from.type != D_REG)
  302. break;
  303. if(p->to.type != D_REG)
  304. break;
  305. q1 = p;
  306. /* MOV a,4(SP) */
  307. q = prg();
  308. q->link = p->link;
  309. p->link = q;
  310. p = q;
  311. p->as = AMOVW;
  312. p->line = q1->line;
  313. p->from.type = D_REG;
  314. p->from.reg = q1->from.reg;
  315. p->to.type = D_OREG;
  316. p->to.reg = REGSP;
  317. p->to.offset = 4;
  318. /* MOV b,REGTMP */
  319. q = prg();
  320. q->link = p->link;
  321. p->link = q;
  322. p = q;
  323. p->as = AMOVW;
  324. p->line = q1->line;
  325. p->from.type = D_REG;
  326. p->from.reg = q1->reg;
  327. if(q1->reg == NREG)
  328. p->from.reg = q1->to.reg;
  329. p->to.type = D_REG;
  330. p->to.reg = REGTMP;
  331. p->to.offset = 0;
  332. /* CALL appropriate */
  333. q = prg();
  334. q->link = p->link;
  335. p->link = q;
  336. p = q;
  337. p->as = AJMPL;
  338. p->line = q1->line;
  339. p->to.type = D_BRANCH;
  340. p->cond = p;
  341. p->mark |= BRANCH;
  342. switch(o) {
  343. case AMUL:
  344. p->cond = prog_mul;
  345. break;
  346. case ADIV:
  347. p->cond = prog_div;
  348. break;
  349. case ADIVL:
  350. p->cond = prog_divl;
  351. break;
  352. case AMOD:
  353. p->cond = prog_mod;
  354. break;
  355. case AMODL:
  356. p->cond = prog_modl;
  357. break;
  358. }
  359. /* MOV REGTMP, b */
  360. q = prg();
  361. q->link = p->link;
  362. p->link = q;
  363. p = q;
  364. p->as = AMOVW;
  365. p->line = q1->line;
  366. p->from.type = D_REG;
  367. p->from.reg = REGTMP;
  368. p->from.offset = 0;
  369. p->to.type = D_REG;
  370. p->to.reg = q1->to.reg;
  371. /* ADD $8,SP */
  372. q = prg();
  373. q->link = p->link;
  374. p->link = q;
  375. p = q;
  376. p->as = AADD;
  377. p->from.type = D_CONST;
  378. p->from.reg = NREG;
  379. p->from.offset = 8;
  380. p->reg = NREG;
  381. p->to.type = D_REG;
  382. p->to.reg = REGSP;
  383. /* SUB $8,SP */
  384. q1->as = ASUB;
  385. q1->from.type = D_CONST;
  386. q1->from.offset = 8;
  387. q1->from.reg = NREG;
  388. q1->reg = NREG;
  389. q1->to.type = D_REG;
  390. q1->to.reg = REGSP;
  391. break;
  392. case ARETURN:
  393. if(p->from.type == D_CONST)
  394. goto become;
  395. if(curtext->mark & LEAF) {
  396. if(!autosize) {
  397. p->as = AJMP;
  398. p->from = zprg.from;
  399. p->to.type = D_OREG;
  400. p->to.offset = 8;
  401. p->to.reg = REGLINK;
  402. p->mark |= BRANCH;
  403. break;
  404. }
  405. p->as = AADD;
  406. p->from.type = D_CONST;
  407. p->from.offset = autosize;
  408. p->to.type = D_REG;
  409. p->to.reg = REGSP;
  410. q = prg();
  411. q->as = AJMP;
  412. q->line = p->line;
  413. q->to.type = D_OREG;
  414. q->to.offset = 8;
  415. q->to.reg = REGLINK;
  416. q->mark |= BRANCH;
  417. q->link = p->link;
  418. p->link = q;
  419. break;
  420. }
  421. p->as = AMOVW;
  422. p->from.type = D_OREG;
  423. p->from.offset = 0;
  424. p->from.reg = REGSP;
  425. p->to.type = D_REG;
  426. p->to.reg = REGRET+1;
  427. q = p;
  428. if(autosize) {
  429. q = prg();
  430. q->as = AADD;
  431. q->line = p->line;
  432. q->from.type = D_CONST;
  433. q->from.offset = autosize;
  434. q->to.type = D_REG;
  435. q->to.reg = REGSP;
  436. q->link = p->link;
  437. p->link = q;
  438. }
  439. q1 = prg();
  440. q1->as = AJMP;
  441. q1->line = p->line;
  442. q1->to.type = D_OREG;
  443. q1->to.offset = 8;
  444. q1->to.reg = REGRET+1;
  445. q1->mark |= BRANCH;
  446. q1->link = q->link;
  447. q->link = q1;
  448. break;
  449. become:
  450. if(curtext->mark & LEAF) {
  451. q = prg();
  452. q->line = p->line;
  453. q->as = AJMP;
  454. q->from = zprg.from;
  455. q->to = p->to;
  456. q->cond = p->cond;
  457. q->link = p->link;
  458. q->mark |= BRANCH;
  459. p->link = q;
  460. p->as = AADD;
  461. p->from = zprg.from;
  462. p->from.type = D_CONST;
  463. p->from.offset = autosize;
  464. p->to = zprg.to;
  465. p->to.type = D_REG;
  466. p->to.reg = REGSP;
  467. break;
  468. }
  469. q = prg();
  470. q->line = p->line;
  471. q->as = AJMP;
  472. q->from = zprg.from;
  473. q->to = p->to;
  474. q->cond = p->cond;
  475. q->mark |= BRANCH;
  476. q->link = p->link;
  477. p->link = q;
  478. q = prg();
  479. q->line = p->line;
  480. q->as = AADD;
  481. q->from.type = D_CONST;
  482. q->from.offset = autosize;
  483. q->to.type = D_REG;
  484. q->to.reg = REGSP;
  485. q->link = p->link;
  486. p->link = q;
  487. p->as = AMOVW;
  488. p->from = zprg.from;
  489. p->from.type = D_OREG;
  490. p->from.offset = 0;
  491. p->from.reg = REGSP;
  492. p->to = zprg.to;
  493. p->to.type = D_REG;
  494. p->to.reg = REGLINK;
  495. break;
  496. }
  497. }
  498. curtext = P;
  499. q = P; /* p - 1 */
  500. q1 = firstp; /* top of block */
  501. o = 0; /* count of instructions */
  502. for(p = firstp; p != P; p = p1) {
  503. p1 = p->link;
  504. o++;
  505. if(p->mark & NOSCHED){
  506. if(q1 != p){
  507. sched(q1, q);
  508. }
  509. for(; p != P; p = p->link){
  510. if(!(p->mark & NOSCHED))
  511. break;
  512. q = p;
  513. }
  514. p1 = p;
  515. q1 = p;
  516. o = 0;
  517. continue;
  518. }
  519. if(p->mark & (LABEL|SYNC)) {
  520. if(q1 != p)
  521. sched(q1, q);
  522. q1 = p;
  523. o = 1;
  524. }
  525. if(p->mark & (BRANCH|SYNC)) {
  526. sched(q1, p);
  527. q1 = p1;
  528. o = 0;
  529. }
  530. if(o >= NSCHED) {
  531. sched(q1, p);
  532. q1 = p1;
  533. o = 0;
  534. }
  535. q = p;
  536. }
  537. }
  538. void
  539. addnop(Prog *p)
  540. {
  541. Prog *q;
  542. q = prg();
  543. q->as = AORN;
  544. q->line = p->line;
  545. q->from.type = D_REG;
  546. q->from.reg = REGZERO;
  547. q->to.type = D_REG;
  548. q->to.reg = REGZERO;
  549. q->link = p->link;
  550. p->link = q;
  551. }
  552. void
  553. initmuldiv(void)
  554. {
  555. Sym *s1, *s2, *s3, *s4, *s5;
  556. Prog *p;
  557. s1 = lookup("_mul", 0);
  558. s2 = lookup("_div", 0);
  559. s3 = lookup("_divl", 0);
  560. s4 = lookup("_mod", 0);
  561. s5 = lookup("_modl", 0);
  562. for(p = firstp; p != P; p = p->link)
  563. if(p->as == ATEXT) {
  564. if(p->from.sym == s1)
  565. prog_mul = p;
  566. if(p->from.sym == s2)
  567. prog_div = p;
  568. if(p->from.sym == s3)
  569. prog_divl = p;
  570. if(p->from.sym == s4)
  571. prog_mod = p;
  572. if(p->from.sym == s5)
  573. prog_modl = p;
  574. }
  575. if(prog_mul == P) {
  576. diag("undefined: %s", s1->name);
  577. prog_mul = curtext;
  578. }
  579. if(prog_div == P) {
  580. diag("undefined: %s", s2->name);
  581. prog_div = curtext;
  582. }
  583. if(prog_divl == P) {
  584. diag("undefined: %s", s3->name);
  585. prog_divl = curtext;
  586. }
  587. if(prog_mod == P) {
  588. diag("undefined: %s", s4->name);
  589. prog_mod = curtext;
  590. }
  591. if(prog_modl == P) {
  592. diag("undefined: %s", s5->name);
  593. prog_modl = curtext;
  594. }
  595. }