noop.c 8.8 KB

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