memops.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * memops - try to combine memory ops.
  3. *
  4. * Copyright (C) 2004 Linus Torvalds
  5. */
  6. #include <string.h>
  7. #include <stdarg.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <stddef.h>
  11. #include <assert.h>
  12. #include "parse.h"
  13. #include "expression.h"
  14. #include "linearize.h"
  15. #include "flow.h"
  16. static int find_dominating_parents(pseudo_t pseudo, struct instruction *insn,
  17. struct basic_block *bb, unsigned long generation, struct pseudo_list **dominators,
  18. int local)
  19. {
  20. struct basic_block *parent;
  21. FOR_EACH_PTR(bb->parents, parent) {
  22. struct instruction *one;
  23. struct instruction *br;
  24. pseudo_t phi;
  25. FOR_EACH_PTR_REVERSE(parent->insns, one) {
  26. int dominance;
  27. if (!one->bb)
  28. continue;
  29. if (one == insn)
  30. goto no_dominance;
  31. dominance = dominates(pseudo, insn, one, local);
  32. if (dominance < 0) {
  33. if (one->opcode == OP_LOAD)
  34. continue;
  35. return 0;
  36. }
  37. if (!dominance)
  38. continue;
  39. goto found_dominator;
  40. } END_FOR_EACH_PTR_REVERSE(one);
  41. no_dominance:
  42. if (parent->generation == generation)
  43. continue;
  44. parent->generation = generation;
  45. if (!find_dominating_parents(pseudo, insn, parent, generation, dominators, local))
  46. return 0;
  47. continue;
  48. found_dominator:
  49. br = delete_last_instruction(&parent->insns);
  50. phi = alloc_phi(parent, one->target, one->type);
  51. phi->ident = phi->ident ? : one->target->ident;
  52. add_instruction(&parent->insns, br);
  53. use_pseudo(insn, phi, add_pseudo(dominators, phi));
  54. } END_FOR_EACH_PTR(parent);
  55. return 1;
  56. }
  57. static int address_taken(pseudo_t pseudo)
  58. {
  59. struct pseudo_user *pu;
  60. FOR_EACH_PTR(pseudo->users, pu) {
  61. struct instruction *insn = pu->insn;
  62. if (insn->bb && (insn->opcode != OP_LOAD && insn->opcode != OP_STORE))
  63. return 1;
  64. if (pu->userp != &insn->src)
  65. return 1;
  66. } END_FOR_EACH_PTR(pu);
  67. return 0;
  68. }
  69. static int local_pseudo(pseudo_t pseudo)
  70. {
  71. return pseudo->type == PSEUDO_SYM
  72. && !(pseudo->sym->ctype.modifiers & (MOD_STATIC | MOD_NONLOCAL))
  73. && !address_taken(pseudo);
  74. }
  75. static bool compatible_loads(struct instruction *a, struct instruction *b)
  76. {
  77. if (is_integral_type(a->type) && is_float_type(b->type))
  78. return false;
  79. if (is_float_type(a->type) && is_integral_type(b->type))
  80. return false;
  81. return true;
  82. }
  83. static void simplify_loads(struct basic_block *bb)
  84. {
  85. struct instruction *insn;
  86. FOR_EACH_PTR_REVERSE(bb->insns, insn) {
  87. if (!insn->bb)
  88. continue;
  89. if (insn->opcode == OP_LOAD) {
  90. struct instruction *dom;
  91. pseudo_t pseudo = insn->src;
  92. int local = local_pseudo(pseudo);
  93. struct pseudo_list *dominators;
  94. unsigned long generation;
  95. /* Check for illegal offsets.. */
  96. check_access(insn);
  97. if (insn->is_volatile)
  98. continue;
  99. RECURSE_PTR_REVERSE(insn, dom) {
  100. int dominance;
  101. if (!dom->bb)
  102. continue;
  103. dominance = dominates(pseudo, insn, dom, local);
  104. if (dominance) {
  105. /* possible partial dominance? */
  106. if (dominance < 0) {
  107. if (dom->opcode == OP_LOAD)
  108. continue;
  109. goto next_load;
  110. }
  111. if (!compatible_loads(insn, dom))
  112. goto next_load;
  113. /* Yeehaa! Found one! */
  114. convert_load_instruction(insn, dom->target);
  115. goto next_load;
  116. }
  117. } END_FOR_EACH_PTR_REVERSE(dom);
  118. /* OK, go find the parents */
  119. generation = ++bb_generation;
  120. bb->generation = generation;
  121. dominators = NULL;
  122. if (find_dominating_parents(pseudo, insn, bb, generation, &dominators, local)) {
  123. /* This happens with initial assignments to structures etc.. */
  124. if (!dominators) {
  125. if (local) {
  126. assert(pseudo->type != PSEUDO_ARG);
  127. convert_load_instruction(insn, value_pseudo(0));
  128. }
  129. goto next_load;
  130. }
  131. rewrite_load_instruction(insn, dominators);
  132. } else { // cleanup pending phi-sources
  133. pseudo_t phi;
  134. FOR_EACH_PTR(dominators, phi) {
  135. kill_instruction(phi->def);
  136. } END_FOR_EACH_PTR(phi);
  137. }
  138. }
  139. next_load:
  140. /* Do the next one */;
  141. } END_FOR_EACH_PTR_REVERSE(insn);
  142. }
  143. static void kill_dominated_stores(struct basic_block *bb)
  144. {
  145. struct instruction *insn;
  146. FOR_EACH_PTR_REVERSE(bb->insns, insn) {
  147. if (!insn->bb)
  148. continue;
  149. if (insn->opcode == OP_STORE) {
  150. struct instruction *dom;
  151. pseudo_t pseudo = insn->src;
  152. int local;
  153. if (!insn->type)
  154. continue;
  155. if (insn->is_volatile)
  156. continue;
  157. local = local_pseudo(pseudo);
  158. RECURSE_PTR_REVERSE(insn, dom) {
  159. int dominance;
  160. if (!dom->bb)
  161. continue;
  162. dominance = dominates(pseudo, insn, dom, local);
  163. if (dominance) {
  164. /* possible partial dominance? */
  165. if (dominance < 0)
  166. goto next_store;
  167. if (dom->opcode == OP_LOAD)
  168. goto next_store;
  169. /* Yeehaa! Found one! */
  170. kill_instruction_force(dom);
  171. }
  172. } END_FOR_EACH_PTR_REVERSE(dom);
  173. /* OK, we should check the parents now */
  174. }
  175. next_store:
  176. /* Do the next one */;
  177. } END_FOR_EACH_PTR_REVERSE(insn);
  178. }
  179. void simplify_memops(struct entrypoint *ep)
  180. {
  181. struct basic_block *bb;
  182. pseudo_t pseudo;
  183. FOR_EACH_PTR_REVERSE(ep->bbs, bb) {
  184. simplify_loads(bb);
  185. } END_FOR_EACH_PTR_REVERSE(bb);
  186. FOR_EACH_PTR_REVERSE(ep->bbs, bb) {
  187. kill_dominated_stores(bb);
  188. } END_FOR_EACH_PTR_REVERSE(bb);
  189. FOR_EACH_PTR(ep->accesses, pseudo) {
  190. struct symbol *var = pseudo->sym;
  191. unsigned long mod;
  192. if (!var)
  193. continue;
  194. mod = var->ctype.modifiers;
  195. if (mod & (MOD_VOLATILE | MOD_NONLOCAL | MOD_STATIC))
  196. continue;
  197. kill_dead_stores(ep, pseudo, local_pseudo(pseudo));
  198. } END_FOR_EACH_PTR(pseudo);
  199. }