noop.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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, mov, aoffset, 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 ANOR:
  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 ALWAR:
  60. case ASTWCCC:
  61. case AECIWX:
  62. case AECOWX:
  63. case AEIEIO:
  64. case AICBI:
  65. case AISYNC:
  66. case ATLBIE:
  67. case ADCBF:
  68. case ADCBI:
  69. case ADCBST:
  70. case ADCBT:
  71. case ADCBTST:
  72. case ADCBZ:
  73. case ASYNC:
  74. case ATW:
  75. case AWORD:
  76. case ARFI:
  77. case ARFCI:
  78. q = p;
  79. p->mark |= LABEL|SYNC;
  80. continue;
  81. case AMOVW:
  82. q = p;
  83. switch(p->from.type) {
  84. case D_MSR:
  85. case D_SREG:
  86. case D_SPR:
  87. case D_FPSCR:
  88. case D_CREG:
  89. case D_DCR:
  90. p->mark |= LABEL|SYNC;
  91. }
  92. switch(p->to.type) {
  93. case D_MSR:
  94. case D_SREG:
  95. case D_SPR:
  96. case D_FPSCR:
  97. case D_CREG:
  98. case D_DCR:
  99. p->mark |= LABEL|SYNC;
  100. }
  101. continue;
  102. case AFABS:
  103. case AFABSCC:
  104. case AFADD:
  105. case AFADDCC:
  106. case AFCTIW:
  107. case AFCTIWCC:
  108. case AFCTIWZ:
  109. case AFCTIWZCC:
  110. case AFDIV:
  111. case AFDIVCC:
  112. case AFMADD:
  113. case AFMADDCC:
  114. case AFMOVD:
  115. case AFMOVDU:
  116. /* case AFMOVDS: */
  117. case AFMOVS:
  118. case AFMOVSU:
  119. /* case AFMOVSD: */
  120. case AFMSUB:
  121. case AFMSUBCC:
  122. case AFMUL:
  123. case AFMULCC:
  124. case AFNABS:
  125. case AFNABSCC:
  126. case AFNEG:
  127. case AFNEGCC:
  128. case AFNMADD:
  129. case AFNMADDCC:
  130. case AFNMSUB:
  131. case AFNMSUBCC:
  132. case AFRSP:
  133. case AFRSPCC:
  134. case AFSUB:
  135. case AFSUBCC:
  136. q = p;
  137. p->mark |= FLOAT;
  138. continue;
  139. case ABL:
  140. case ABCL:
  141. if(curtext != P)
  142. curtext->mark &= ~LEAF;
  143. case ABC:
  144. case ABEQ:
  145. case ABGE:
  146. case ABGT:
  147. case ABLE:
  148. case ABLT:
  149. case ABNE:
  150. case ABR:
  151. case ABVC:
  152. case ABVS:
  153. p->mark |= BRANCH;
  154. q = p;
  155. q1 = p->cond;
  156. if(q1 != P) {
  157. while(q1->as == ANOP) {
  158. q1 = q1->link;
  159. p->cond = q1;
  160. }
  161. if(!(q1->mark & LEAF))
  162. q1->mark |= LABEL;
  163. } else
  164. p->mark |= LABEL;
  165. q1 = p->link;
  166. if(q1 != P)
  167. q1->mark |= LABEL;
  168. continue;
  169. case AFCMPO:
  170. case AFCMPU:
  171. q = p;
  172. p->mark |= FCMP|FLOAT;
  173. continue;
  174. case ARETURN:
  175. /* special form of RETURN is BECOME */
  176. if(p->from.type == D_CONST)
  177. if(p->from.offset > curbecome)
  178. curbecome = p->from.offset;
  179. q = p;
  180. if(p->link != P)
  181. p->link->mark |= LABEL;
  182. continue;
  183. case ANOP:
  184. q1 = p->link;
  185. q->link = q1; /* q is non-nop */
  186. q1->mark |= p->mark;
  187. continue;
  188. default:
  189. q = p;
  190. continue;
  191. }
  192. }
  193. if(curtext && curtext->from.sym) {
  194. curtext->from.sym->frame = curframe;
  195. curtext->from.sym->become = curbecome;
  196. if(curbecome > maxbecome)
  197. maxbecome = curbecome;
  198. }
  199. if(debug['b'])
  200. print("max become = %d\n", maxbecome);
  201. xdefine("ALEFbecome", STEXT, maxbecome);
  202. curtext = 0;
  203. for(p = firstp; p != P; p = p->link) {
  204. switch(p->as) {
  205. case ATEXT:
  206. curtext = p;
  207. break;
  208. case ABL: /* ABCL? */
  209. if(curtext != P && curtext->from.sym != S && curtext->to.offset >= 0) {
  210. o = maxbecome - curtext->from.sym->frame;
  211. if(o <= 0)
  212. break;
  213. /* calling a become or calling a variable */
  214. if(p->to.sym == S || p->to.sym->become) {
  215. curtext->to.offset += o;
  216. if(debug['b']) {
  217. curp = p;
  218. print("%D calling %D increase %d\n",
  219. &curtext->from, &p->to, o);
  220. }
  221. }
  222. }
  223. break;
  224. }
  225. }
  226. curtext = P;
  227. for(p = firstp; p != P; p = p->link) {
  228. o = p->as;
  229. switch(o) {
  230. case ATEXT:
  231. mov = AMOVW;
  232. aoffset = 0;
  233. curtext = p;
  234. autosize = p->to.offset + 4;
  235. if((p->mark & LEAF) && autosize <= 4)
  236. autosize = 0;
  237. else
  238. if(autosize & 4)
  239. autosize += 4;
  240. p->to.offset = autosize - 4;
  241. q = p;
  242. if(autosize) {
  243. /* use MOVWU to adjust R1 when saving R31, if autosize is small */
  244. if(!(curtext->mark & LEAF) && autosize >= -BIG && autosize <= BIG) {
  245. mov = AMOVWU;
  246. aoffset = -autosize;
  247. } else {
  248. q = prg();
  249. q->as = AADD;
  250. q->line = p->line;
  251. q->from.type = D_CONST;
  252. q->from.offset = -autosize;
  253. q->to.type = D_REG;
  254. q->to.reg = REGSP;
  255. q->link = p->link;
  256. p->link = q;
  257. }
  258. } else
  259. if(!(curtext->mark & LEAF)) {
  260. if(debug['v'])
  261. Bprint(&bso, "save suppressed in: %s\n",
  262. curtext->from.sym->name);
  263. curtext->mark |= LEAF;
  264. }
  265. if(curtext->mark & LEAF) {
  266. if(curtext->from.sym)
  267. curtext->from.sym->type = SLEAF;
  268. break;
  269. }
  270. q1 = prg();
  271. q1->as = mov;
  272. q1->line = p->line;
  273. q1->from.type = D_REG;
  274. q1->from.reg = REGTMP;
  275. q1->to.type = D_OREG;
  276. q1->to.offset = aoffset;
  277. q1->to.reg = REGSP;
  278. q1->link = q->link;
  279. q->link = q1;
  280. q1 = prg();
  281. q1->as = AMOVW;
  282. q1->line = p->line;
  283. q1->from.type = D_SPR;
  284. q1->from.offset = D_LR;
  285. q1->to.type = D_REG;
  286. q1->to.reg = REGTMP;
  287. q1->link = q->link;
  288. q->link = q1;
  289. break;
  290. case ARETURN:
  291. if(p->from.type == D_CONST)
  292. goto become;
  293. if(curtext->mark & LEAF) {
  294. if(!autosize) {
  295. p->as = ABR;
  296. p->from = zprg.from;
  297. p->to.type = D_SPR;
  298. p->to.offset = D_LR;
  299. p->mark |= BRANCH;
  300. break;
  301. }
  302. p->as = AADD;
  303. p->from.type = D_CONST;
  304. p->from.offset = autosize;
  305. p->to.type = D_REG;
  306. p->to.reg = REGSP;
  307. q = prg();
  308. q->as = ABR;
  309. q->line = p->line;
  310. q->to.type = D_SPR;
  311. q->to.offset = D_LR;
  312. q->mark |= BRANCH;
  313. q->link = p->link;
  314. p->link = q;
  315. break;
  316. }
  317. p->as = AMOVW;
  318. p->from.type = D_OREG;
  319. p->from.offset = 0;
  320. p->from.reg = REGSP;
  321. p->to.type = D_REG;
  322. p->to.reg = REGTMP;
  323. q = prg();
  324. q->as = AMOVW;
  325. q->line = p->line;
  326. q->from.type = D_REG;
  327. q->from.reg = REGTMP;
  328. q->to.type = D_SPR;
  329. q->to.offset = D_LR;
  330. q->link = p->link;
  331. p->link = q;
  332. p = q;
  333. if(autosize) {
  334. q = prg();
  335. q->as = AADD;
  336. q->line = p->line;
  337. q->from.type = D_CONST;
  338. q->from.offset = autosize;
  339. q->to.type = D_REG;
  340. q->to.reg = REGSP;
  341. q->link = p->link;
  342. p->link = q;
  343. }
  344. q1 = prg();
  345. q1->as = ABR;
  346. q1->line = p->line;
  347. q1->to.type = D_SPR;
  348. q1->to.offset = D_LR;
  349. q1->mark |= BRANCH;
  350. q1->link = q->link;
  351. q->link = q1;
  352. break;
  353. become:
  354. if(curtext->mark & LEAF) {
  355. q = prg();
  356. q->line = p->line;
  357. q->as = ABR;
  358. q->from = zprg.from;
  359. q->to = p->to;
  360. q->cond = p->cond;
  361. q->link = p->link;
  362. q->mark |= BRANCH;
  363. p->link = q;
  364. p->as = AADD;
  365. p->from = zprg.from;
  366. p->from.type = D_CONST;
  367. p->from.offset = autosize;
  368. p->to = zprg.to;
  369. p->to.type = D_REG;
  370. p->to.reg = REGSP;
  371. break;
  372. }
  373. q = prg();
  374. q->line = p->line;
  375. q->as = ABR;
  376. q->from = zprg.from;
  377. q->to = p->to;
  378. q->cond = p->cond;
  379. q->mark |= BRANCH;
  380. q->link = p->link;
  381. p->link = q;
  382. q = prg();
  383. q->line = p->line;
  384. q->as = AADD;
  385. q->from.type = D_CONST;
  386. q->from.offset = autosize;
  387. q->to.type = D_REG;
  388. q->to.reg = REGSP;
  389. q->link = p->link;
  390. p->link = q;
  391. q = prg();
  392. q->line = p->line;
  393. q->as = AMOVW;
  394. q->line = p->line;
  395. q->from.type = D_REG;
  396. q->from.reg = REGTMP;
  397. q->to.type = D_SPR;
  398. q->to.offset = D_LR;
  399. q->link = p->link;
  400. p->link = q;
  401. p->as = AMOVW;
  402. p->from = zprg.from;
  403. p->from.type = D_OREG;
  404. p->from.offset = 0;
  405. p->from.reg = REGSP;
  406. p->to = zprg.to;
  407. p->to.type = D_REG;
  408. p->to.reg = REGTMP;
  409. break;
  410. }
  411. }
  412. if(debug['Q'] == 0)
  413. return;
  414. curtext = P;
  415. q = P; /* p - 1 */
  416. q1 = firstp; /* top of block */
  417. o = 0; /* count of instructions */
  418. for(p = firstp; p != P; p = p1) {
  419. p1 = p->link;
  420. o++;
  421. if(p->mark & NOSCHED){
  422. if(q1 != p){
  423. sched(q1, q);
  424. }
  425. for(; p != P; p = p->link){
  426. if(!(p->mark & NOSCHED))
  427. break;
  428. q = p;
  429. }
  430. p1 = p;
  431. q1 = p;
  432. o = 0;
  433. continue;
  434. }
  435. if(p->mark & (LABEL|SYNC)) {
  436. if(q1 != p)
  437. sched(q1, q);
  438. q1 = p;
  439. o = 1;
  440. }
  441. if(p->mark & (BRANCH|SYNC)) {
  442. sched(q1, p);
  443. q1 = p1;
  444. o = 0;
  445. }
  446. if(o >= NSCHED) {
  447. sched(q1, p);
  448. q1 = p1;
  449. o = 0;
  450. }
  451. q = p;
  452. }
  453. }
  454. void
  455. addnop(Prog *p)
  456. {
  457. Prog *q;
  458. q = prg();
  459. q->as = ANOR;
  460. q->line = p->line;
  461. q->from.type = D_REG;
  462. q->from.reg = REGZERO;
  463. q->to.type = D_REG;
  464. q->to.reg = REGZERO;
  465. q->link = p->link;
  466. p->link = q;
  467. }