sgen.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. noretval(int n)
  12. {
  13. if(n & 1) {
  14. gins(ANOP, Z, Z);
  15. p->to.type = REGRET;
  16. }
  17. if(n & 2) {
  18. gins(ANOP, Z, Z);
  19. p->to.type = FREGRET;
  20. }
  21. if((n&3) == 3)
  22. if(thisfn && thisfn->link && typefd[thisfn->link->etype])
  23. gins(AFLDZ, Z, Z);
  24. }
  25. /* welcome to commute */
  26. static void
  27. commute(Node *n)
  28. {
  29. Node *l, *r;
  30. l = n->left;
  31. r = n->right;
  32. if(r->complex > l->complex) {
  33. n->left = r;
  34. n->right = l;
  35. }
  36. }
  37. void
  38. indexshift(Node *n)
  39. {
  40. int g;
  41. if(!typechlp[n->type->etype])
  42. return;
  43. simplifyshift(n);
  44. if(n->op == OASHL && n->right->op == OCONST){
  45. g = vconst(n->right);
  46. if(g >= 0 && g < 4)
  47. n->addable = 7;
  48. }
  49. }
  50. /*
  51. * calculate addressability as follows
  52. * NAME ==> 10/11 name+value(SB/SP)
  53. * REGISTER ==> 12 register
  54. * CONST ==> 20 $value
  55. * *(20) ==> 21 value
  56. * &(10) ==> 13 $name+value(SB)
  57. * &(11) ==> 1 $name+value(SP)
  58. * (13) + (20) ==> 13 fold constants
  59. * (1) + (20) ==> 1 fold constants
  60. * *(13) ==> 10 back to name
  61. * *(1) ==> 11 back to name
  62. *
  63. * (20) * (X) ==> 7 multiplier in indexing
  64. * (X,7) + (13,1) ==> 8 adder in indexing (addresses)
  65. * (8) ==> &9(OINDEX) index, almost addressable
  66. *
  67. * calculate complexity (number of registers)
  68. */
  69. void
  70. xcom(Node *n)
  71. {
  72. Node *l, *r;
  73. int g;
  74. if(n == Z)
  75. return;
  76. l = n->left;
  77. r = n->right;
  78. n->complex = 0;
  79. n->addable = 0;
  80. switch(n->op) {
  81. case OCONST:
  82. n->addable = 20;
  83. break;
  84. case ONAME:
  85. n->addable = 10;
  86. if(n->class == CPARAM || n->class == CAUTO)
  87. n->addable = 11;
  88. break;
  89. case OEXREG:
  90. n->addable = 12;
  91. break;
  92. case OREGISTER:
  93. n->addable = 12;
  94. break;
  95. case OINDREG:
  96. n->addable = 12;
  97. break;
  98. case OADDR:
  99. xcom(l);
  100. if(l->addable == 10)
  101. n->addable = 13;
  102. else
  103. if(l->addable == 11)
  104. n->addable = 1;
  105. break;
  106. case OADD:
  107. xcom(l);
  108. xcom(r);
  109. if(n->type->etype != TIND)
  110. break;
  111. switch(r->addable) {
  112. case 20:
  113. switch(l->addable) {
  114. case 1:
  115. case 13:
  116. commadd:
  117. l->type = n->type;
  118. *n = *l;
  119. l = new(0, Z, Z);
  120. *l = *(n->left);
  121. l->xoffset += r->vconst;
  122. n->left = l;
  123. r = n->right;
  124. goto brk;
  125. }
  126. break;
  127. case 1:
  128. case 13:
  129. case 10:
  130. case 11:
  131. /* l is the base, r is the index */
  132. if(l->addable != 20)
  133. n->addable = 8;
  134. break;
  135. }
  136. switch(l->addable) {
  137. case 20:
  138. switch(r->addable) {
  139. case 13:
  140. case 1:
  141. r = n->left;
  142. l = n->right;
  143. n->left = l;
  144. n->right = r;
  145. goto commadd;
  146. }
  147. break;
  148. case 13:
  149. case 1:
  150. case 10:
  151. case 11:
  152. /* r is the base, l is the index */
  153. if(r->addable != 20)
  154. n->addable = 8;
  155. break;
  156. }
  157. if(n->addable == 8 && !side(n)) {
  158. indx(n);
  159. l = new1(OINDEX, idx.basetree, idx.regtree);
  160. l->scale = idx.scale;
  161. l->addable = 9;
  162. l->complex = l->right->complex;
  163. l->type = l->left->type;
  164. n->op = OADDR;
  165. n->left = l;
  166. n->right = Z;
  167. n->addable = 8;
  168. break;
  169. }
  170. break;
  171. case OINDEX:
  172. xcom(l);
  173. xcom(r);
  174. n->addable = 9;
  175. break;
  176. case OIND:
  177. xcom(l);
  178. if(l->op == OADDR) {
  179. l = l->left;
  180. l->type = n->type;
  181. *n = *l;
  182. return;
  183. }
  184. switch(l->addable) {
  185. case 20:
  186. n->addable = 21;
  187. break;
  188. case 1:
  189. n->addable = 11;
  190. break;
  191. case 13:
  192. n->addable = 10;
  193. break;
  194. }
  195. break;
  196. case OASHL:
  197. xcom(l);
  198. xcom(r);
  199. indexshift(n);
  200. break;
  201. case OMUL:
  202. case OLMUL:
  203. xcom(l);
  204. xcom(r);
  205. g = vlog(l);
  206. if(g >= 0) {
  207. n->left = r;
  208. n->right = l;
  209. l = r;
  210. r = n->right;
  211. }
  212. g = vlog(r);
  213. if(g >= 0) {
  214. n->op = OASHL;
  215. r->vconst = g;
  216. r->type = types[TINT];
  217. indexshift(n);
  218. break;
  219. }
  220. commute(n);
  221. break;
  222. case OASLDIV:
  223. xcom(l);
  224. xcom(r);
  225. g = vlog(r);
  226. if(g >= 0) {
  227. n->op = OASLSHR;
  228. r->vconst = g;
  229. r->type = types[TINT];
  230. }
  231. break;
  232. case OLDIV:
  233. xcom(l);
  234. xcom(r);
  235. g = vlog(r);
  236. if(g >= 0) {
  237. n->op = OLSHR;
  238. r->vconst = g;
  239. r->type = types[TINT];
  240. indexshift(n);
  241. break;
  242. }
  243. break;
  244. case OASLMOD:
  245. xcom(l);
  246. xcom(r);
  247. g = vlog(r);
  248. if(g >= 0) {
  249. n->op = OASAND;
  250. r->vconst--;
  251. }
  252. break;
  253. case OLMOD:
  254. xcom(l);
  255. xcom(r);
  256. g = vlog(r);
  257. if(g >= 0) {
  258. n->op = OAND;
  259. r->vconst--;
  260. }
  261. break;
  262. case OASMUL:
  263. case OASLMUL:
  264. xcom(l);
  265. xcom(r);
  266. g = vlog(r);
  267. if(g >= 0) {
  268. n->op = OASASHL;
  269. r->vconst = g;
  270. }
  271. break;
  272. case OLSHR:
  273. case OASHR:
  274. xcom(l);
  275. xcom(r);
  276. indexshift(n);
  277. break;
  278. default:
  279. if(l != Z)
  280. xcom(l);
  281. if(r != Z)
  282. xcom(r);
  283. break;
  284. }
  285. brk:
  286. if(n->addable >= 10)
  287. return;
  288. if(l != Z)
  289. n->complex = l->complex;
  290. if(r != Z) {
  291. if(r->complex == n->complex)
  292. n->complex = r->complex+1;
  293. else
  294. if(r->complex > n->complex)
  295. n->complex = r->complex;
  296. }
  297. if(n->complex == 0)
  298. n->complex++;
  299. if(com64(n))
  300. return;
  301. switch(n->op) {
  302. case OFUNC:
  303. n->complex = FNX;
  304. break;
  305. case OLMOD:
  306. case OMOD:
  307. case OLMUL:
  308. case OLDIV:
  309. case OMUL:
  310. case ODIV:
  311. case OASLMUL:
  312. case OASLDIV:
  313. case OASLMOD:
  314. case OASMUL:
  315. case OASDIV:
  316. case OASMOD:
  317. if(r->complex >= l->complex) {
  318. n->complex = l->complex + 3;
  319. if(r->complex > n->complex)
  320. n->complex = r->complex;
  321. } else {
  322. n->complex = r->complex + 3;
  323. if(l->complex > n->complex)
  324. n->complex = l->complex;
  325. }
  326. break;
  327. case OLSHR:
  328. case OASHL:
  329. case OASHR:
  330. case OASLSHR:
  331. case OASASHL:
  332. case OASASHR:
  333. if(r->complex >= l->complex) {
  334. n->complex = l->complex + 2;
  335. if(r->complex > n->complex)
  336. n->complex = r->complex;
  337. } else {
  338. n->complex = r->complex + 2;
  339. if(l->complex > n->complex)
  340. n->complex = l->complex;
  341. }
  342. break;
  343. case OADD:
  344. case OXOR:
  345. case OAND:
  346. case OOR:
  347. /*
  348. * immediate operators, make const on right
  349. */
  350. if(l->op == OCONST) {
  351. n->left = r;
  352. n->right = l;
  353. }
  354. break;
  355. case OEQ:
  356. case ONE:
  357. case OLE:
  358. case OLT:
  359. case OGE:
  360. case OGT:
  361. case OHI:
  362. case OHS:
  363. case OLO:
  364. case OLS:
  365. /*
  366. * compare operators, make const on left
  367. */
  368. if(r->op == OCONST) {
  369. n->left = r;
  370. n->right = l;
  371. n->op = invrel[relindex(n->op)];
  372. }
  373. break;
  374. }
  375. }
  376. void
  377. indx(Node *n)
  378. {
  379. Node *l, *r;
  380. if(debug['x'])
  381. prtree(n, "indx");
  382. l = n->left;
  383. r = n->right;
  384. if(l->addable == 1 || l->addable == 13 || r->complex > l->complex) {
  385. n->right = l;
  386. n->left = r;
  387. l = r;
  388. r = n->right;
  389. }
  390. if(l->addable != 7) {
  391. idx.regtree = l;
  392. idx.scale = 1;
  393. } else
  394. if(l->right->addable == 20) {
  395. idx.regtree = l->left;
  396. idx.scale = 1 << l->right->vconst;
  397. } else
  398. if(l->left->addable == 20) {
  399. idx.regtree = l->right;
  400. idx.scale = 1 << l->left->vconst;
  401. } else
  402. diag(n, "bad index");
  403. idx.basetree = r;
  404. if(debug['x']) {
  405. print("scale = %d\n", idx.scale);
  406. prtree(idx.regtree, "index");
  407. prtree(idx.basetree, "base");
  408. }
  409. }