genpeep.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* Generate code from machine description to perform peephole optimizations.
  2. Copyright (C) 1987 Free Software Foundation, Inc.
  3. This file is part of GNU CC.
  4. GNU CC is distributed in the hope that it will be useful,
  5. but WITHOUT ANY WARRANTY. No author or distributor
  6. accepts responsibility to anyone for the consequences of using it
  7. or for whether it serves any particular purpose or works at all,
  8. unless he says so in writing. Refer to the GNU CC General Public
  9. License for full details.
  10. Everyone is granted permission to copy, modify and redistribute
  11. GNU CC, but only under the conditions described in the
  12. GNU CC General Public License. A copy of this license is
  13. supposed to have been given to you along with GNU CC so you
  14. can know your rights and responsibilities. It should be in a
  15. file named COPYING. Among other things, the copyright notice
  16. and this notice must be preserved on all copies. */
  17. #include <stdio.h>
  18. #include "config.h"
  19. #include "rtl.h"
  20. #include "obstack.h"
  21. struct obstack obstack;
  22. struct obstack *rtl_obstack = &obstack;
  23. #define obstack_chunk_alloc xmalloc
  24. #define obstack_chunk_free free
  25. extern int xmalloc ();
  26. extern void free ();
  27. void match_rtx ();
  28. void gen_exp ();
  29. void fatal ();
  30. int max_opno;
  31. /* While tree-walking an instruction pattern, we keep a chain
  32. of these `struct link's to record how to get down to the
  33. current position. In each one, POS is the operand number,
  34. and if the operand is a vector VEC is the element number.
  35. VEC is -1 if the operand is not a vector. */
  36. struct link
  37. {
  38. struct link *next;
  39. int pos;
  40. int vecelt;
  41. };
  42. /* Number of operands used in current peephole definition. */
  43. int n_operands;
  44. /* Peephole optimizations get insn codes just like insn patterns.
  45. Count them so we know the code of the define_peephole we are handling. */
  46. int insn_code_number = 0;
  47. void print_path ();
  48. void print_code ();
  49. void
  50. gen_peephole (peep)
  51. rtx peep;
  52. {
  53. int ninsns = XVECLEN (peep, 0);
  54. int i;
  55. n_operands = 0;
  56. printf (" insn = ins1;\n");
  57. for (i = 0; i < ninsns; i++)
  58. {
  59. if (i > 0)
  60. {
  61. printf (" do { insn = NEXT_INSN (insn);\n");
  62. printf (" if (insn == 0) goto L%d; }\n",
  63. insn_code_number);
  64. printf (" while (GET_CODE (insn) == NOTE);\n");
  65. }
  66. printf (" if (GET_CODE (insn) == CODE_LABEL) goto L%d;\n",
  67. insn_code_number);
  68. printf (" pat = PATTERN (insn);\n");
  69. /* Walk the insn's pattern, remembering at all times the path
  70. down to the walking point. */
  71. match_rtx (XVECEXP (peep, 0, i), 0, insn_code_number);
  72. }
  73. /* We get this far if the pattern matches.
  74. Now test the extra condition. */
  75. if (XSTR (peep, 1) && XSTR (peep, 1)[0])
  76. printf (" if (! (%s)) goto L%d;\n",
  77. XSTR (peep, 1), insn_code_number);
  78. /* If that matches, construct new pattern and put it in the first insn.
  79. This new pattern will never be matched.
  80. It exists only so that insn-extract can get the operands back.
  81. So use a simple regular form: a PARALLEL containing a vector
  82. of all the operands. */
  83. printf (" PATTERN (ins1) = gen_rtx (PARALLEL, VOIDmode, gen_rtvec_v (%d, operands));\n", n_operands);
  84. printf (" insn = ins1;\n");
  85. /* Make sure that labels referred to by the insns
  86. don't get deleted because of their counts' going to zero. */
  87. printf (" for (i = 0; i < %d; i++)\n", n_operands);
  88. printf (" if (GET_CODE (operands[i]) == CODE_LABEL)\n");
  89. printf (" LABEL_NUSES (operands[i])++;\n");
  90. /* Record this define_peephole's insn code in the insn,
  91. as if it had been recognized to match this. */
  92. printf (" INSN_CODE (insn) = %d;\n",
  93. insn_code_number);
  94. /* Delete the remaining insns. */
  95. for (i = 1; i < ninsns; i++)
  96. {
  97. printf (" do insn = NEXT_INSN (insn);\n");
  98. printf (" while (GET_CODE (insn) == NOTE);\n");
  99. printf (" delete_insn (insn);\n");
  100. }
  101. printf (" return 1;\n");
  102. printf (" L%d:\n\n", insn_code_number);
  103. }
  104. void
  105. match_rtx (x, path, fail_label)
  106. rtx x;
  107. struct link *path;
  108. int fail_label;
  109. {
  110. register RTX_CODE code;
  111. register int i;
  112. register int len;
  113. register char *fmt;
  114. struct link link;
  115. if (x == 0)
  116. return;
  117. code = GET_CODE (x);
  118. switch (code)
  119. {
  120. case MATCH_OPERAND:
  121. if (XINT (x, 0) > max_opno)
  122. max_opno = XINT (x, 0);
  123. if (XINT (x, 0) >= n_operands)
  124. n_operands = 1 + XINT (x, 0);
  125. printf (" x = ");
  126. print_path (path);
  127. printf (";\n");
  128. printf (" operands[%d] = x;\n", XINT (x, 0));
  129. if (XSTR (x, 1) && XSTR (x, 1)[0])
  130. printf (" if (! %s (x, %smode)) goto L%d;\n",
  131. XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label);
  132. return;
  133. case MATCH_DUP:
  134. printf (" x = ");
  135. print_path (path);
  136. printf (";\n");
  137. printf (" if (!rtx_equal_p (operands[%d], x)) goto L%d;\n",
  138. XINT (x, 0), fail_label);
  139. return;
  140. case ADDRESS:
  141. match_rtx (XEXP (x, 0), path, fail_label);
  142. return;
  143. }
  144. printf (" x = ");
  145. print_path (path);
  146. printf (";\n");
  147. printf (" if (GET_CODE (x) != ");
  148. print_code (code);
  149. printf (") goto L%d;\n", fail_label);
  150. if (GET_MODE (x) != VOIDmode)
  151. {
  152. printf (" if (GET_MODE (x) != %smode) goto L%d;\n",
  153. GET_MODE_NAME (GET_MODE (x)), fail_label);
  154. }
  155. link.next = path;
  156. link.vecelt = -1;
  157. fmt = GET_RTX_FORMAT (code);
  158. len = GET_RTX_LENGTH (code);
  159. for (i = 0; i < len; i++)
  160. {
  161. link.pos = i;
  162. if (fmt[i] == 'e' || fmt[i] == 'u')
  163. match_rtx (XEXP (x, i), &link, fail_label);
  164. else if (fmt[i] == 'E')
  165. {
  166. int j;
  167. printf (" if (XVECLEN (x, %d) != %d) goto L%d;\n",
  168. i, XVECLEN (x, i), fail_label);
  169. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  170. {
  171. link.vecelt = j;
  172. match_rtx (XVECEXP (x, i, j), &link, fail_label);
  173. }
  174. }
  175. else if (fmt[i] == 'i')
  176. {
  177. printf (" if (XINT (x, %d) != %d) goto L%d;\n",
  178. i, XINT (x, i), fail_label);
  179. }
  180. else if (fmt[i] == 's')
  181. {
  182. printf (" if (strcmp (XSTR (x, %d), \"%s\")) goto L%d;\n",
  183. i, XSTR (x, i), fail_label);
  184. }
  185. }
  186. }
  187. /* Given a PATH, representing a path down the instruction's
  188. pattern from the root to a certain point, output code to
  189. evaluate to the rtx at that point. */
  190. void
  191. print_path (path)
  192. struct link *path;
  193. {
  194. if (path == 0)
  195. printf ("pat");
  196. else if (path->vecelt >= 0)
  197. {
  198. printf ("XVECEXP (");
  199. print_path (path->next);
  200. printf (", %d, %d)", path->pos, path->vecelt);
  201. }
  202. else
  203. {
  204. printf ("XEXP (");
  205. print_path (path->next);
  206. printf (", %d)", path->pos);
  207. }
  208. }
  209. void
  210. print_code (code)
  211. RTX_CODE code;
  212. {
  213. register char *p1;
  214. for (p1 = GET_RTX_NAME (code); *p1; p1++)
  215. {
  216. if (*p1 >= 'a' && *p1 <= 'z')
  217. putchar (*p1 + 'A' - 'a');
  218. else
  219. putchar (*p1);
  220. }
  221. }
  222. int
  223. xmalloc (size)
  224. {
  225. register int val = malloc (size);
  226. if (val == 0)
  227. fatal ("virtual memory exhausted");
  228. return val;
  229. }
  230. int
  231. xrealloc (ptr, size)
  232. char *ptr;
  233. int size;
  234. {
  235. int result = realloc (ptr, size);
  236. if (!result)
  237. fatal ("virtual memory exhausted");
  238. return result;
  239. }
  240. void
  241. fatal (s, a1, a2)
  242. {
  243. fprintf (stderr, "genpeep: ");
  244. fprintf (stderr, s, a1, a2);
  245. fprintf (stderr, "\n");
  246. exit (FATAL_EXIT_CODE);
  247. }
  248. int
  249. main (argc, argv)
  250. int argc;
  251. char **argv;
  252. {
  253. rtx desc;
  254. FILE *infile;
  255. extern rtx read_rtx ();
  256. register int c, i;
  257. max_opno = -1;
  258. obstack_init (rtl_obstack);
  259. if (argc <= 1)
  260. fatal ("No input file name.");
  261. infile = fopen (argv[1], "r");
  262. if (infile == 0)
  263. {
  264. perror (argv[1]);
  265. exit (FATAL_EXIT_CODE);
  266. }
  267. init_rtl ();
  268. printf ("/* Generated automatically by the program `genpeep'\n\
  269. from the machine description file `md'. */\n\n");
  270. printf ("#include \"rtl.h\"\n\n");
  271. printf ("#include \"config.h\"\n\n");
  272. printf ("#include \"regs.h\"\n\n");
  273. printf ("rtx peep_operand[];\n\n");
  274. printf ("#define operands peep_operand\n\n");
  275. printf ("int\npeephole (ins1)\n rtx ins1;\n{\n");
  276. printf (" rtx insn, x, pat;\n");
  277. printf (" int i;\n");
  278. /* Read the machine description. */
  279. while (1)
  280. {
  281. c = read_skip_spaces (infile);
  282. if (c == EOF)
  283. break;
  284. ungetc (c, infile);
  285. desc = read_rtx (infile);
  286. if (GET_CODE (desc) == DEFINE_PEEPHOLE)
  287. {
  288. gen_peephole (desc);
  289. insn_code_number++;
  290. }
  291. if (GET_CODE (desc) == DEFINE_INSN || GET_CODE (desc) == DEFINE_EXPAND)
  292. {
  293. insn_code_number++;
  294. }
  295. }
  296. printf (" return 0;\n}\n\n");
  297. if (max_opno == -1)
  298. max_opno = 1;
  299. printf ("rtx peep_operand[%d];\n", max_opno + 1);
  300. fflush (stdout);
  301. exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
  302. }