PageRenderTime 56ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/gcc-4.3/gcc/genpeep.c

https://bitbucket.org/pizzafactory/blackfin-toolchain
C | 430 lines | 310 code | 71 blank | 49 comment | 72 complexity | 96be4aaf1ec0ca22a46ff7d357d98d1b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, GPL-3.0, AGPL-1.0, LGPL-2.1
  1. /* Generate code from machine description to perform peephole optimizations.
  2. Copyright (C) 1987, 1989, 1992, 1997, 1998, 1999, 2000, 2003, 2004,
  3. 2007 Free Software Foundation, Inc.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "bconfig.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "tm.h"
  20. #include "rtl.h"
  21. #include "errors.h"
  22. #include "gensupport.h"
  23. /* While tree-walking an instruction pattern, we keep a chain
  24. of these `struct link's to record how to get down to the
  25. current position. In each one, POS is the operand number,
  26. and if the operand is a vector VEC is the element number.
  27. VEC is -1 if the operand is not a vector. */
  28. struct link
  29. {
  30. struct link *next;
  31. int pos;
  32. int vecelt;
  33. };
  34. static int max_opno;
  35. /* Number of operands used in current peephole definition. */
  36. static int n_operands;
  37. /* Peephole optimizations get insn codes just like insn patterns.
  38. Count them so we know the code of the define_peephole we are handling. */
  39. static int insn_code_number = 0;
  40. static void gen_peephole (rtx);
  41. static void match_rtx (rtx, struct link *, int);
  42. static void print_path (struct link *);
  43. static void print_code (RTX_CODE);
  44. static void
  45. gen_peephole (rtx peep)
  46. {
  47. int ninsns = XVECLEN (peep, 0);
  48. int i;
  49. n_operands = 0;
  50. printf (" insn = ins1;\n");
  51. for (i = 0; i < ninsns; i++)
  52. {
  53. if (i > 0)
  54. {
  55. printf (" do { insn = NEXT_INSN (insn);\n");
  56. printf (" if (insn == 0) goto L%d; }\n",
  57. insn_code_number);
  58. printf (" while (NOTE_P (insn)\n");
  59. printf ("\t || (NONJUMP_INSN_P (insn)\n");
  60. printf ("\t && (GET_CODE (PATTERN (insn)) == USE\n");
  61. printf ("\t\t || GET_CODE (PATTERN (insn)) == CLOBBER)));\n");
  62. printf (" if (LABEL_P (insn)\n\
  63. || BARRIER_P (insn))\n goto L%d;\n",
  64. insn_code_number);
  65. }
  66. printf (" pat = PATTERN (insn);\n");
  67. /* Walk the insn's pattern, remembering at all times the path
  68. down to the walking point. */
  69. match_rtx (XVECEXP (peep, 0, i), NULL, insn_code_number);
  70. }
  71. /* We get this far if the pattern matches.
  72. Now test the extra condition. */
  73. if (XSTR (peep, 1) && XSTR (peep, 1)[0])
  74. printf (" if (! (%s)) goto L%d;\n",
  75. XSTR (peep, 1), insn_code_number);
  76. /* If that matches, construct new pattern and put it in the first insn.
  77. This new pattern will never be matched.
  78. It exists only so that insn-extract can get the operands back.
  79. So use a simple regular form: a PARALLEL containing a vector
  80. of all the operands. */
  81. printf (" PATTERN (ins1) = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (%d, operands));\n", n_operands);
  82. /* Record this define_peephole's insn code in the insn,
  83. as if it had been recognized to match this. */
  84. printf (" INSN_CODE (ins1) = %d;\n",
  85. insn_code_number);
  86. /* Delete the remaining insns. */
  87. if (ninsns > 1)
  88. printf (" delete_for_peephole (NEXT_INSN (ins1), insn);\n");
  89. /* See reload1.c for insertion of NOTE which guarantees that this
  90. cannot be zero. */
  91. printf (" return NEXT_INSN (insn);\n");
  92. printf (" L%d:\n\n", insn_code_number);
  93. }
  94. static void
  95. match_rtx (rtx x, struct link *path, int fail_label)
  96. {
  97. RTX_CODE code;
  98. int i;
  99. int len;
  100. const char *fmt;
  101. struct link link;
  102. if (x == 0)
  103. return;
  104. code = GET_CODE (x);
  105. switch (code)
  106. {
  107. case MATCH_OPERAND:
  108. if (XINT (x, 0) > max_opno)
  109. max_opno = XINT (x, 0);
  110. if (XINT (x, 0) >= n_operands)
  111. n_operands = 1 + XINT (x, 0);
  112. printf (" x = ");
  113. print_path (path);
  114. printf (";\n");
  115. printf (" operands[%d] = x;\n", XINT (x, 0));
  116. if (XSTR (x, 1) && XSTR (x, 1)[0])
  117. printf (" if (! %s (x, %smode)) goto L%d;\n",
  118. XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label);
  119. return;
  120. case MATCH_DUP:
  121. case MATCH_PAR_DUP:
  122. printf (" x = ");
  123. print_path (path);
  124. printf (";\n");
  125. printf (" if (!rtx_equal_p (operands[%d], x)) goto L%d;\n",
  126. XINT (x, 0), fail_label);
  127. return;
  128. case MATCH_OP_DUP:
  129. printf (" x = ");
  130. print_path (path);
  131. printf (";\n");
  132. printf (" if (GET_CODE (operands[%d]) != GET_CODE (x)\n", XINT (x, 0));
  133. printf (" || GET_MODE (operands[%d]) != GET_MODE (x)) goto L%d;\n",
  134. XINT (x, 0), fail_label);
  135. printf (" operands[%d] = x;\n", XINT (x, 0));
  136. link.next = path;
  137. link.vecelt = -1;
  138. for (i = 0; i < XVECLEN (x, 1); i++)
  139. {
  140. link.pos = i;
  141. match_rtx (XVECEXP (x, 1, i), &link, fail_label);
  142. }
  143. return;
  144. case MATCH_OPERATOR:
  145. if (XINT (x, 0) > max_opno)
  146. max_opno = XINT (x, 0);
  147. if (XINT (x, 0) >= n_operands)
  148. n_operands = 1 + XINT (x, 0);
  149. printf (" x = ");
  150. print_path (path);
  151. printf (";\n");
  152. printf (" operands[%d] = x;\n", XINT (x, 0));
  153. if (XSTR (x, 1) && XSTR (x, 1)[0])
  154. printf (" if (! %s (x, %smode)) goto L%d;\n",
  155. XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label);
  156. link.next = path;
  157. link.vecelt = -1;
  158. for (i = 0; i < XVECLEN (x, 2); i++)
  159. {
  160. link.pos = i;
  161. match_rtx (XVECEXP (x, 2, i), &link, fail_label);
  162. }
  163. return;
  164. case MATCH_PARALLEL:
  165. if (XINT (x, 0) > max_opno)
  166. max_opno = XINT (x, 0);
  167. if (XINT (x, 0) >= n_operands)
  168. n_operands = 1 + XINT (x, 0);
  169. printf (" x = ");
  170. print_path (path);
  171. printf (";\n");
  172. printf (" if (GET_CODE (x) != PARALLEL) goto L%d;\n", fail_label);
  173. printf (" operands[%d] = x;\n", XINT (x, 0));
  174. if (XSTR (x, 1) && XSTR (x, 1)[0])
  175. printf (" if (! %s (x, %smode)) goto L%d;\n",
  176. XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label);
  177. link.next = path;
  178. link.pos = 0;
  179. for (i = 0; i < XVECLEN (x, 2); i++)
  180. {
  181. link.vecelt = i;
  182. match_rtx (XVECEXP (x, 2, i), &link, fail_label);
  183. }
  184. return;
  185. case ADDRESS:
  186. match_rtx (XEXP (x, 0), path, fail_label);
  187. return;
  188. default:
  189. break;
  190. }
  191. printf (" x = ");
  192. print_path (path);
  193. printf (";\n");
  194. printf (" if (GET_CODE (x) != ");
  195. print_code (code);
  196. printf (") goto L%d;\n", fail_label);
  197. if (GET_MODE (x) != VOIDmode)
  198. {
  199. printf (" if (GET_MODE (x) != %smode) goto L%d;\n",
  200. GET_MODE_NAME (GET_MODE (x)), fail_label);
  201. }
  202. link.next = path;
  203. link.vecelt = -1;
  204. fmt = GET_RTX_FORMAT (code);
  205. len = GET_RTX_LENGTH (code);
  206. for (i = 0; i < len; i++)
  207. {
  208. link.pos = i;
  209. if (fmt[i] == 'e' || fmt[i] == 'u')
  210. match_rtx (XEXP (x, i), &link, fail_label);
  211. else if (fmt[i] == 'E')
  212. {
  213. int j;
  214. printf (" if (XVECLEN (x, %d) != %d) goto L%d;\n",
  215. i, XVECLEN (x, i), fail_label);
  216. for (j = 0; j < XVECLEN (x, i); j++)
  217. {
  218. link.vecelt = j;
  219. match_rtx (XVECEXP (x, i, j), &link, fail_label);
  220. }
  221. }
  222. else if (fmt[i] == 'i')
  223. {
  224. /* Make sure that at run time `x' is the RTX we want to test. */
  225. if (i != 0)
  226. {
  227. printf (" x = ");
  228. print_path (path);
  229. printf (";\n");
  230. }
  231. printf (" if (XINT (x, %d) != %d) goto L%d;\n",
  232. i, XINT (x, i), fail_label);
  233. }
  234. else if (fmt[i] == 'w')
  235. {
  236. /* Make sure that at run time `x' is the RTX we want to test. */
  237. if (i != 0)
  238. {
  239. printf (" x = ");
  240. print_path (path);
  241. printf (";\n");
  242. }
  243. printf (" if (XWINT (x, %d) != ", i);
  244. printf (HOST_WIDE_INT_PRINT_DEC, XWINT (x, i));
  245. printf (") goto L%d;\n", fail_label);
  246. }
  247. else if (fmt[i] == 's')
  248. {
  249. /* Make sure that at run time `x' is the RTX we want to test. */
  250. if (i != 0)
  251. {
  252. printf (" x = ");
  253. print_path (path);
  254. printf (";\n");
  255. }
  256. printf (" if (strcmp (XSTR (x, %d), \"%s\")) goto L%d;\n",
  257. i, XSTR (x, i), fail_label);
  258. }
  259. }
  260. }
  261. /* Given a PATH, representing a path down the instruction's
  262. pattern from the root to a certain point, output code to
  263. evaluate to the rtx at that point. */
  264. static void
  265. print_path (struct link *path)
  266. {
  267. if (path == 0)
  268. printf ("pat");
  269. else if (path->vecelt >= 0)
  270. {
  271. printf ("XVECEXP (");
  272. print_path (path->next);
  273. printf (", %d, %d)", path->pos, path->vecelt);
  274. }
  275. else
  276. {
  277. printf ("XEXP (");
  278. print_path (path->next);
  279. printf (", %d)", path->pos);
  280. }
  281. }
  282. static void
  283. print_code (RTX_CODE code)
  284. {
  285. const char *p1;
  286. for (p1 = GET_RTX_NAME (code); *p1; p1++)
  287. putchar (TOUPPER(*p1));
  288. }
  289. extern int main (int, char **);
  290. int
  291. main (int argc, char **argv)
  292. {
  293. rtx desc;
  294. max_opno = -1;
  295. progname = "genpeep";
  296. if (init_md_reader_args (argc, argv) != SUCCESS_EXIT_CODE)
  297. return (FATAL_EXIT_CODE);
  298. printf ("/* Generated automatically by the program `genpeep'\n\
  299. from the machine description file `md'. */\n\n");
  300. printf ("#include \"config.h\"\n");
  301. printf ("#include \"system.h\"\n");
  302. printf ("#include \"coretypes.h\"\n");
  303. printf ("#include \"tm.h\"\n");
  304. printf ("#include \"insn-config.h\"\n");
  305. printf ("#include \"rtl.h\"\n");
  306. printf ("#include \"tm_p.h\"\n");
  307. printf ("#include \"regs.h\"\n");
  308. printf ("#include \"output.h\"\n");
  309. printf ("#include \"real.h\"\n");
  310. printf ("#include \"recog.h\"\n");
  311. printf ("#include \"except.h\"\n");
  312. printf ("#include \"function.h\"\n");
  313. printf ("#include \"toplev.h\"\n");
  314. printf ("#include \"flags.h\"\n");
  315. printf ("#include \"tm-constrs.h\"\n\n");
  316. printf ("#ifdef HAVE_peephole\n");
  317. printf ("extern rtx peep_operand[];\n\n");
  318. printf ("#define operands peep_operand\n\n");
  319. printf ("rtx\npeephole (rtx ins1)\n{\n");
  320. printf (" rtx insn ATTRIBUTE_UNUSED, x ATTRIBUTE_UNUSED, pat ATTRIBUTE_UNUSED;\n\n");
  321. /* Early out: no peepholes for insns followed by barriers. */
  322. printf (" if (NEXT_INSN (ins1)\n");
  323. printf (" && BARRIER_P (NEXT_INSN (ins1)))\n");
  324. printf (" return 0;\n\n");
  325. /* Read the machine description. */
  326. while (1)
  327. {
  328. int line_no, rtx_number = 0;
  329. desc = read_md_rtx (&line_no, &rtx_number);
  330. if (desc == NULL)
  331. break;
  332. if (GET_CODE (desc) == DEFINE_PEEPHOLE)
  333. {
  334. gen_peephole (desc);
  335. insn_code_number++;
  336. }
  337. if (GET_CODE (desc) == DEFINE_INSN
  338. || GET_CODE (desc) == DEFINE_EXPAND
  339. || GET_CODE (desc) == DEFINE_SPLIT
  340. || GET_CODE (desc) == DEFINE_PEEPHOLE2)
  341. {
  342. insn_code_number++;
  343. }
  344. }
  345. printf (" return 0;\n}\n\n");
  346. if (max_opno == -1)
  347. max_opno = 1;
  348. printf ("rtx peep_operand[%d];\n", max_opno + 1);
  349. printf ("#endif\n");
  350. fflush (stdout);
  351. return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
  352. }