cse.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * CSE - walk the linearized instruction flow, and
  3. * see if we can simplify it and apply CSE on it.
  4. *
  5. * Copyright (C) 2004 Linus Torvalds
  6. */
  7. #include <string.h>
  8. #include <stdarg.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <stddef.h>
  12. #include <assert.h>
  13. #include "parse.h"
  14. #include "expression.h"
  15. #include "flowgraph.h"
  16. #include "linearize.h"
  17. #include "flow.h"
  18. #include "cse.h"
  19. #define INSN_HASH_SIZE 256
  20. static struct instruction_list *insn_hash_table[INSN_HASH_SIZE];
  21. static int phi_compare(pseudo_t phi1, pseudo_t phi2)
  22. {
  23. const struct instruction *def1 = phi1->def;
  24. const struct instruction *def2 = phi2->def;
  25. if (def1->src1 != def2->src1)
  26. return def1->src1 < def2->src1 ? -1 : 1;
  27. if (def1->bb != def2->bb)
  28. return def1->bb < def2->bb ? -1 : 1;
  29. return 0;
  30. }
  31. void cse_collect(struct instruction *insn)
  32. {
  33. unsigned long hash;
  34. hash = (insn->opcode << 3) + (insn->size >> 3);
  35. switch (insn->opcode) {
  36. case OP_SEL:
  37. hash += hashval(insn->src3);
  38. /* Fall through */
  39. /* Binary arithmetic */
  40. case OP_ADD: case OP_SUB:
  41. case OP_MUL:
  42. case OP_DIVU: case OP_DIVS:
  43. case OP_MODU: case OP_MODS:
  44. case OP_SHL:
  45. case OP_LSR: case OP_ASR:
  46. case OP_AND: case OP_OR:
  47. /* Binary logical */
  48. case OP_XOR:
  49. /* Binary comparison */
  50. case OP_SET_EQ: case OP_SET_NE:
  51. case OP_SET_LE: case OP_SET_GE:
  52. case OP_SET_LT: case OP_SET_GT:
  53. case OP_SET_B: case OP_SET_A:
  54. case OP_SET_BE: case OP_SET_AE:
  55. /* floating-point arithmetic & comparison */
  56. case OP_FPCMP ... OP_FPCMP_END:
  57. case OP_FADD:
  58. case OP_FSUB:
  59. case OP_FMUL:
  60. case OP_FDIV:
  61. hash += hashval(insn->src2);
  62. /* Fall through */
  63. /* Unary */
  64. case OP_NOT: case OP_NEG:
  65. case OP_FNEG:
  66. case OP_SYMADDR:
  67. hash += hashval(insn->src1);
  68. break;
  69. case OP_SETVAL:
  70. hash += hashval(insn->val);
  71. break;
  72. case OP_SETFVAL:
  73. hash += hashval(insn->fvalue);
  74. break;
  75. case OP_SEXT: case OP_ZEXT:
  76. case OP_TRUNC:
  77. case OP_PTRCAST:
  78. case OP_UTPTR: case OP_PTRTU:
  79. if (!insn->orig_type || insn->orig_type->bit_size < 0)
  80. return;
  81. hash += hashval(insn->src);
  82. // Note: see corresponding line in insn_compare()
  83. hash += hashval(insn->orig_type->bit_size);
  84. break;
  85. /* Other */
  86. case OP_PHI: {
  87. pseudo_t phi;
  88. FOR_EACH_PTR(insn->phi_list, phi) {
  89. struct instruction *def;
  90. if (phi == VOID || !phi->def)
  91. continue;
  92. def = phi->def;
  93. hash += hashval(def->src1);
  94. hash += hashval(def->bb);
  95. } END_FOR_EACH_PTR(phi);
  96. break;
  97. }
  98. default:
  99. /*
  100. * Nothing to do, don't even bother hashing them,
  101. * we're not going to try to CSE them
  102. */
  103. return;
  104. }
  105. hash += hash >> 16;
  106. hash &= INSN_HASH_SIZE-1;
  107. add_instruction(insn_hash_table + hash, insn);
  108. }
  109. /* Compare two (sorted) phi-lists */
  110. static int phi_list_compare(struct pseudo_list *l1, struct pseudo_list *l2)
  111. {
  112. pseudo_t phi1, phi2;
  113. PREPARE_PTR_LIST(l1, phi1);
  114. PREPARE_PTR_LIST(l2, phi2);
  115. for (;;) {
  116. int cmp;
  117. while (phi1 && (phi1 == VOID || !phi1->def))
  118. NEXT_PTR_LIST(phi1);
  119. while (phi2 && (phi2 == VOID || !phi2->def))
  120. NEXT_PTR_LIST(phi2);
  121. if (!phi1)
  122. return phi2 ? -1 : 0;
  123. if (!phi2)
  124. return phi1 ? 1 : 0;
  125. cmp = phi_compare(phi1, phi2);
  126. if (cmp)
  127. return cmp;
  128. NEXT_PTR_LIST(phi1);
  129. NEXT_PTR_LIST(phi2);
  130. }
  131. /* Not reached, but we need to make the nesting come out right */
  132. FINISH_PTR_LIST(phi2);
  133. FINISH_PTR_LIST(phi1);
  134. }
  135. static int insn_compare(const void *_i1, const void *_i2)
  136. {
  137. const struct instruction *i1 = _i1;
  138. const struct instruction *i2 = _i2;
  139. int size1, size2;
  140. int diff;
  141. if (i1->opcode != i2->opcode)
  142. return i1->opcode < i2->opcode ? -1 : 1;
  143. switch (i1->opcode) {
  144. /* commutative binop */
  145. case OP_ADD:
  146. case OP_MUL:
  147. case OP_AND: case OP_OR:
  148. case OP_XOR:
  149. case OP_SET_EQ: case OP_SET_NE:
  150. if (i1->src1 == i2->src2 && i1->src2 == i2->src1)
  151. return 0;
  152. goto case_binops;
  153. case OP_SEL:
  154. if (i1->src3 != i2->src3)
  155. return i1->src3 < i2->src3 ? -1 : 1;
  156. /* Fall-through to binops */
  157. /* Binary arithmetic */
  158. case OP_SUB:
  159. case OP_DIVU: case OP_DIVS:
  160. case OP_MODU: case OP_MODS:
  161. case OP_SHL:
  162. case OP_LSR: case OP_ASR:
  163. /* Binary comparison */
  164. case OP_SET_LE: case OP_SET_GE:
  165. case OP_SET_LT: case OP_SET_GT:
  166. case OP_SET_B: case OP_SET_A:
  167. case OP_SET_BE: case OP_SET_AE:
  168. /* floating-point arithmetic */
  169. case OP_FPCMP ... OP_FPCMP_END:
  170. case OP_FADD:
  171. case OP_FSUB:
  172. case OP_FMUL:
  173. case OP_FDIV:
  174. case_binops:
  175. if (i1->src2 != i2->src2)
  176. return i1->src2 < i2->src2 ? -1 : 1;
  177. /* Fall through to unops */
  178. /* Unary */
  179. case OP_NOT: case OP_NEG:
  180. case OP_FNEG:
  181. case OP_SYMADDR:
  182. if (i1->src1 != i2->src1)
  183. return i1->src1 < i2->src1 ? -1 : 1;
  184. break;
  185. case OP_SETVAL:
  186. if (i1->val != i2->val)
  187. return i1->val < i2->val ? -1 : 1;
  188. break;
  189. case OP_SETFVAL:
  190. diff = memcmp(&i1->fvalue, &i2->fvalue, sizeof(i1->fvalue));
  191. if (diff)
  192. return diff;
  193. break;
  194. /* Other */
  195. case OP_PHI:
  196. return phi_list_compare(i1->phi_list, i2->phi_list);
  197. case OP_SEXT: case OP_ZEXT:
  198. case OP_TRUNC:
  199. case OP_PTRCAST:
  200. case OP_UTPTR: case OP_PTRTU:
  201. if (i1->src != i2->src)
  202. return i1->src < i2->src ? -1 : 1;
  203. // Note: if it can be guaranted that identical ->src
  204. // implies identical orig_type->bit_size, then this
  205. // test and the hashing of the original size in
  206. // cse_collect() are not needed.
  207. // It must be generaly true but it isn't guaranted (yet).
  208. size1 = i1->orig_type->bit_size;
  209. size2 = i2->orig_type->bit_size;
  210. if (size1 != size2)
  211. return size1 < size2 ? -1 : 1;
  212. break;
  213. default:
  214. warning(i1->pos, "bad instruction on hash chain");
  215. }
  216. if (i1->size != i2->size)
  217. return i1->size < i2->size ? -1 : 1;
  218. return 0;
  219. }
  220. static void sort_instruction_list(struct instruction_list **list)
  221. {
  222. sort_list((struct ptr_list **)list , insn_compare);
  223. }
  224. static struct instruction * cse_one_instruction(struct instruction *insn, struct instruction *def)
  225. {
  226. convert_instruction_target(insn, def->target);
  227. kill_instruction(insn);
  228. repeat_phase |= REPEAT_CSE;
  229. return def;
  230. }
  231. static struct basic_block *trivial_common_parent(struct basic_block *bb1, struct basic_block *bb2)
  232. {
  233. struct basic_block *parent;
  234. if (bb_list_size(bb1->parents) != 1)
  235. return NULL;
  236. parent = first_basic_block(bb1->parents);
  237. if (bb_list_size(bb2->parents) != 1)
  238. return NULL;
  239. if (first_basic_block(bb2->parents) != parent)
  240. return NULL;
  241. return parent;
  242. }
  243. static inline void remove_instruction(struct instruction_list **list, struct instruction *insn, int count)
  244. {
  245. delete_ptr_list_entry((struct ptr_list **)list, insn, count);
  246. }
  247. static void add_instruction_to_end(struct instruction *insn, struct basic_block *bb)
  248. {
  249. struct instruction *br = delete_last_instruction(&bb->insns);
  250. insn->bb = bb;
  251. add_instruction(&bb->insns, insn);
  252. add_instruction(&bb->insns, br);
  253. }
  254. static struct instruction * try_to_cse(struct entrypoint *ep, struct instruction *i1, struct instruction *i2)
  255. {
  256. struct basic_block *b1, *b2, *common;
  257. /*
  258. * OK, i1 and i2 are the same instruction, modulo "target".
  259. * We should now see if we can combine them.
  260. */
  261. b1 = i1->bb;
  262. b2 = i2->bb;
  263. /*
  264. * Currently we only handle the uninteresting degenerate case where
  265. * the CSE is inside one basic-block.
  266. */
  267. if (b1 == b2) {
  268. struct instruction *insn;
  269. FOR_EACH_PTR(b1->insns, insn) {
  270. if (insn == i1)
  271. return cse_one_instruction(i2, i1);
  272. if (insn == i2)
  273. return cse_one_instruction(i1, i2);
  274. } END_FOR_EACH_PTR(insn);
  275. warning(b1->pos, "Whaa? unable to find CSE instructions");
  276. return i1;
  277. }
  278. if (domtree_dominates(b1, b2))
  279. return cse_one_instruction(i2, i1);
  280. if (domtree_dominates(b2, b1))
  281. return cse_one_instruction(i1, i2);
  282. /* No direct dominance - but we could try to find a common ancestor.. */
  283. common = trivial_common_parent(b1, b2);
  284. if (common) {
  285. i1 = cse_one_instruction(i2, i1);
  286. remove_instruction(&b1->insns, i1, 1);
  287. add_instruction_to_end(i1, common);
  288. } else {
  289. i1 = i2;
  290. }
  291. return i1;
  292. }
  293. void cse_eliminate(struct entrypoint *ep)
  294. {
  295. int i;
  296. for (i = 0; i < INSN_HASH_SIZE; i++) {
  297. struct instruction_list **list = insn_hash_table + i;
  298. if (*list) {
  299. if (instruction_list_size(*list) > 1) {
  300. struct instruction *insn, *last;
  301. sort_instruction_list(list);
  302. last = NULL;
  303. FOR_EACH_PTR(*list, insn) {
  304. if (!insn->bb)
  305. continue;
  306. if (last) {
  307. if (!insn_compare(last, insn))
  308. insn = try_to_cse(ep, last, insn);
  309. }
  310. last = insn;
  311. } END_FOR_EACH_PTR(insn);
  312. }
  313. free_ptr_list(list);
  314. }
  315. }
  316. }