noop.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. #include "l.h"
  2. void
  3. noops(void)
  4. {
  5. Prog *p, *p1, *q, *q1;
  6. int o, curframe, curbecome, maxbecome;
  7. /*
  8. * find leaf subroutines
  9. * become sizes
  10. * frame sizes
  11. * strip NOPs
  12. * expand RET
  13. * expand BECOME pseudo
  14. */
  15. if(debug['v'])
  16. Bprint(&bso, "%5.2f noops\n", cputime());
  17. Bflush(&bso);
  18. curframe = 0;
  19. curbecome = 0;
  20. maxbecome = 0;
  21. curtext = 0;
  22. q = P;
  23. for(p = firstp; p != P; p = p->link) {
  24. /* find out how much arg space is used in this TEXT */
  25. if(p->to.type == D_OREG && p->to.reg == REGSP)
  26. if(p->to.offset > curframe)
  27. curframe = p->to.offset;
  28. switch(p->as) {
  29. /* too hard, just leave alone */
  30. case ATEXT:
  31. if(curtext && curtext->from.sym) {
  32. curtext->from.sym->frame = curframe;
  33. curtext->from.sym->become = curbecome;
  34. if(curbecome > maxbecome)
  35. maxbecome = curbecome;
  36. }
  37. curframe = 0;
  38. curbecome = 0;
  39. q = p;
  40. p->mark |= LABEL|LEAF|SYNC;
  41. if(p->link)
  42. p->link->mark |= LABEL;
  43. curtext = p;
  44. break;
  45. case ANOR:
  46. q = p;
  47. if(p->to.type == D_REG)
  48. if(p->to.reg == REGZERO)
  49. p->mark |= LABEL|SYNC;
  50. break;
  51. case ALWAR:
  52. case ASTWCCC:
  53. case AECIWX:
  54. case AECOWX:
  55. case AEIEIO:
  56. case AICBI:
  57. case AISYNC:
  58. case ATLBIE:
  59. case ADCBF:
  60. case ADCBI:
  61. case ADCBST:
  62. case ADCBT:
  63. case ADCBTST:
  64. case ADCBZ:
  65. case ASYNC:
  66. case ATW:
  67. case AWORD:
  68. q = p;
  69. p->mark |= LABEL|SYNC;
  70. continue;
  71. case AFABS:
  72. case AFABSCC:
  73. case AFADD:
  74. case AFADDCC:
  75. case AFCTIW:
  76. case AFCTIWCC:
  77. case AFCTIWZ:
  78. case AFCTIWZCC:
  79. case AFDIV:
  80. case AFDIVCC:
  81. case AFMADD:
  82. case AFMADDCC:
  83. case AFMOVD:
  84. case AFMOVDU:
  85. /* case AFMOVDS: */
  86. case AFMOVS:
  87. case AFMOVSU:
  88. /* case AFMOVSD: */
  89. case AFMSUB:
  90. case AFMSUBCC:
  91. case AFMUL:
  92. case AFMULCC:
  93. case AFNABS:
  94. case AFNABSCC:
  95. case AFNEG:
  96. case AFNEGCC:
  97. case AFNMADD:
  98. case AFNMADDCC:
  99. case AFNMSUB:
  100. case AFNMSUBCC:
  101. case AFRSP:
  102. case AFRSPCC:
  103. case AFSUB:
  104. case AFSUBCC:
  105. q = p;
  106. p->mark |= FLOAT;
  107. continue;
  108. case ABL:
  109. case ABCL:
  110. if(curtext != P)
  111. curtext->mark &= ~LEAF;
  112. case ABC:
  113. case ABEQ:
  114. case ABGE:
  115. case ABGT:
  116. case ABLE:
  117. case ABLT:
  118. case ABNE:
  119. case ABR:
  120. case ABVC:
  121. case ABVS:
  122. p->mark |= BRANCH;
  123. q = p;
  124. q1 = p->cond;
  125. if(q1 != P) {
  126. while(q1->as == ANOP) {
  127. q1 = q1->link;
  128. p->cond = q1;
  129. }
  130. if(!(q1->mark & LEAF))
  131. q1->mark |= LABEL;
  132. } else
  133. p->mark |= LABEL;
  134. q1 = p->link;
  135. if(q1 != P)
  136. q1->mark |= LABEL;
  137. continue;
  138. case AFCMPO:
  139. case AFCMPU:
  140. q = p;
  141. p->mark |= FCMP|FLOAT;
  142. continue;
  143. case ARETURN:
  144. /* special form of RETURN is BECOME */
  145. if(p->from.type == D_CONST)
  146. if(p->from.offset > curbecome)
  147. curbecome = p->from.offset;
  148. q = p;
  149. if(p->link != P)
  150. p->link->mark |= LABEL;
  151. continue;
  152. case ANOP:
  153. q1 = p->link;
  154. q->link = q1; /* q is non-nop */
  155. q1->mark |= p->mark;
  156. continue;
  157. default:
  158. q = p;
  159. continue;
  160. }
  161. }
  162. if(curtext && curtext->from.sym) {
  163. curtext->from.sym->frame = curframe;
  164. curtext->from.sym->become = curbecome;
  165. if(curbecome > maxbecome)
  166. maxbecome = curbecome;
  167. }
  168. if(debug['b'])
  169. print("max become = %d\n", maxbecome);
  170. xdefine("ALEFbecome", STEXT, maxbecome);
  171. curtext = 0;
  172. for(p = firstp; p != P; p = p->link) {
  173. switch(p->as) {
  174. case ATEXT:
  175. curtext = p;
  176. break;
  177. case ABL: /* ABCL? */
  178. if(curtext != P && curtext->from.sym != S && curtext->to.offset >= 0) {
  179. o = maxbecome - curtext->from.sym->frame;
  180. if(o <= 0)
  181. break;
  182. /* calling a become or calling a variable */
  183. if(p->to.sym == S || p->to.sym->become) {
  184. curtext->to.offset += o;
  185. if(debug['b']) {
  186. curp = p;
  187. print("%D calling %D increase %d\n",
  188. &curtext->from, &p->to, o);
  189. }
  190. }
  191. }
  192. break;
  193. }
  194. }
  195. curtext = P;
  196. for(p = firstp; p != P; p = p->link) {
  197. o = p->as;
  198. switch(o) {
  199. case ATEXT:
  200. /* could use MOVWU to save R31, if autosize is not too big */
  201. curtext = p;
  202. autosize = p->to.offset + 4;
  203. if((p->mark & LEAF) && autosize <= 4)
  204. autosize = 0;
  205. else
  206. if(autosize & 4)
  207. autosize += 4;
  208. p->to.offset = autosize - 4;
  209. q = p;
  210. if(autosize) {
  211. q = prg();
  212. q->as = AADD;
  213. q->line = p->line;
  214. q->from.type = D_CONST;
  215. q->from.offset = -autosize;
  216. q->to.type = D_REG;
  217. q->to.reg = REGSP;
  218. q->link = p->link;
  219. p->link = q;
  220. } else
  221. if(!(curtext->mark & LEAF)) {
  222. if(debug['v'])
  223. Bprint(&bso, "save suppressed in: %s\n",
  224. curtext->from.sym->name);
  225. curtext->mark |= LEAF;
  226. }
  227. if(curtext->mark & LEAF) {
  228. if(curtext->from.sym)
  229. curtext->from.sym->type = SLEAF;
  230. break;
  231. }
  232. q1 = prg();
  233. q1->as = AMOVW;
  234. q1->line = p->line;
  235. q1->from.type = D_REG;
  236. q1->from.reg = REGTMP;
  237. q1->to.type = D_OREG;
  238. q1->from.offset = 0;
  239. q1->to.reg = REGSP;
  240. q1->link = q->link;
  241. q->link = q1;
  242. q1 = prg();
  243. q1->as = AMOVW;
  244. q1->line = p->line;
  245. q1->from.type = D_SPR;
  246. q1->from.offset = D_LR;
  247. q1->to.type = D_REG;
  248. q1->to.reg = REGTMP;
  249. q1->link = q->link;
  250. q->link = q1;
  251. break;
  252. case ARETURN:
  253. if(p->from.type == D_CONST)
  254. goto become;
  255. if(curtext->mark & LEAF) {
  256. if(!autosize) {
  257. p->as = ABR;
  258. p->from = zprg.from;
  259. p->to.type = D_SPR;
  260. p->to.offset = D_LR;
  261. p->mark |= BRANCH;
  262. break;
  263. }
  264. p->as = AADD;
  265. p->from.type = D_CONST;
  266. p->from.offset = autosize;
  267. p->to.type = D_REG;
  268. p->to.reg = REGSP;
  269. q = prg();
  270. q->as = ABR;
  271. q->line = p->line;
  272. q->to.type = D_SPR;
  273. q->to.offset = D_LR;
  274. q->mark |= BRANCH;
  275. q->link = p->link;
  276. p->link = q;
  277. break;
  278. }
  279. p->as = AMOVW;
  280. p->from.type = D_OREG;
  281. p->from.offset = 0;
  282. p->from.reg = REGSP;
  283. p->to.type = D_REG;
  284. p->to.reg = REGTMP;
  285. q = prg();
  286. q->as = AMOVW;
  287. q->line = p->line;
  288. q->from.type = D_REG;
  289. q->from.reg = REGTMP;
  290. q->to.type = D_SPR;
  291. q->to.offset = D_LR;
  292. q->link = p->link;
  293. p->link = q;
  294. p = q;
  295. if(autosize) {
  296. q = prg();
  297. q->as = AADD;
  298. q->line = p->line;
  299. q->from.type = D_CONST;
  300. q->from.offset = autosize;
  301. q->to.type = D_REG;
  302. q->to.reg = REGSP;
  303. q->link = p->link;
  304. p->link = q;
  305. p = q;
  306. }
  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. p = q;
  316. break;
  317. become:
  318. if(curtext->mark & LEAF) {
  319. q = prg();
  320. q->line = p->line;
  321. q->as = ABR;
  322. q->from = zprg.from;
  323. q->to = p->to;
  324. q->cond = p->cond;
  325. q->link = p->link;
  326. q->mark |= BRANCH;
  327. p->link = q;
  328. p->as = AADD;
  329. p->from = zprg.from;
  330. p->from.type = D_CONST;
  331. p->from.offset = autosize;
  332. p->to = zprg.to;
  333. p->to.type = D_REG;
  334. p->to.reg = REGSP;
  335. break;
  336. }
  337. /* BUG: need one more MOVW from REGTMP to LR */
  338. q = prg();
  339. q->line = p->line;
  340. q->as = ABR;
  341. q->from = zprg.from;
  342. q->to = p->to;
  343. q->cond = p->cond;
  344. q->mark |= BRANCH;
  345. q->link = p->link;
  346. p->link = q;
  347. q = prg();
  348. q->line = p->line;
  349. q->as = AADD;
  350. q->from.type = D_CONST;
  351. q->from.offset = autosize;
  352. q->to.type = D_REG;
  353. q->to.reg = REGSP;
  354. q->link = p->link;
  355. p->link = q;
  356. p->as = AMOVW;
  357. p->from = zprg.from;
  358. p->from.type = D_OREG;
  359. p->from.offset = 0;
  360. p->from.reg = REGSP;
  361. p->to = zprg.to;
  362. p->to.type = D_REG;
  363. p->to.reg = REGTMP;
  364. break;
  365. }
  366. }
  367. SET(p1); USED(p1);
  368. #ifdef INSSCHED
  369. curtext = P;
  370. q = P; /* p - 1 */
  371. q1 = firstp; /* top of block */
  372. o = 0; /* count of instructions */
  373. for(p = firstp; p != P; p = p1) {
  374. p1 = p->link;
  375. o++;
  376. if(p->mark & NOSCHED){
  377. if(q1 != p){
  378. sched(q1, q);
  379. }
  380. for(; p != P; p = p->link){
  381. if(!(p->mark & NOSCHED))
  382. break;
  383. q = p;
  384. }
  385. p1 = p;
  386. q1 = p;
  387. o = 0;
  388. continue;
  389. }
  390. if(p->mark & (LABEL|SYNC)) {
  391. if(q1 != p)
  392. sched(q1, q);
  393. q1 = p;
  394. o = 1;
  395. }
  396. if(p->mark & (BRANCH|SYNC)) {
  397. sched(q1, p);
  398. q1 = p1;
  399. o = 0;
  400. }
  401. if(o >= NSCHED) {
  402. sched(q1, p);
  403. q1 = p1;
  404. o = 0;
  405. }
  406. q = p;
  407. }
  408. #endif
  409. }
  410. void
  411. addnop(Prog *p)
  412. {
  413. Prog *q;
  414. q = prg();
  415. q->as = ANOR;
  416. q->line = p->line;
  417. q->from.type = D_REG;
  418. q->from.reg = REGZERO;
  419. q->to.type = D_REG;
  420. q->to.reg = REGZERO;
  421. q->link = p->link;
  422. p->link = q;
  423. }