1 /* Generate code from machine description to perform peephole optimizations.
2 Copyright (C) 1987, 1989, 1992, 1997, 1998,
3 1999, 2000, 2003 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 #include "coretypes.h"
29 #include "gensupport.h"
32 /* While tree-walking an instruction pattern, we keep a chain
33 of these `struct link's to record how to get down to the
34 current position. In each one, POS is the operand number,
35 and if the operand is a vector VEC is the element number.
36 VEC is -1 if the operand is not a vector. */
47 /* Number of operands used in current peephole definition. */
49 static int n_operands
;
51 /* Peephole optimizations get insn codes just like insn patterns.
52 Count them so we know the code of the define_peephole we are handling. */
54 static int insn_code_number
= 0;
56 static void gen_peephole (rtx
);
57 static void match_rtx (rtx
, struct link
*, int);
58 static void print_path (struct link
*);
59 static void print_code (RTX_CODE
);
62 gen_peephole (rtx peep
)
64 int ninsns
= XVECLEN (peep
, 0);
69 printf (" insn = ins1;\n");
71 for (i
= 0; i
< ninsns
; i
++)
75 printf (" do { insn = NEXT_INSN (insn);\n");
76 printf (" if (insn == 0) goto L%d; }\n",
78 printf (" while (NOTE_P (insn)\n");
79 printf ("\t || (NONJUMP_INSN_P (insn)\n");
80 printf ("\t && (GET_CODE (PATTERN (insn)) == USE\n");
81 printf ("\t\t || GET_CODE (PATTERN (insn)) == CLOBBER)));\n");
83 printf (" if (LABEL_P (insn)\n\
84 || BARRIER_P (insn))\n goto L%d;\n",
88 printf (" pat = PATTERN (insn);\n");
90 /* Walk the insn's pattern, remembering at all times the path
91 down to the walking point. */
93 match_rtx (XVECEXP (peep
, 0, i
), NULL
, insn_code_number
);
96 /* We get this far if the pattern matches.
97 Now test the extra condition. */
99 if (XSTR (peep
, 1) && XSTR (peep
, 1)[0])
100 printf (" if (! (%s)) goto L%d;\n",
101 XSTR (peep
, 1), insn_code_number
);
103 /* If that matches, construct new pattern and put it in the first insn.
104 This new pattern will never be matched.
105 It exists only so that insn-extract can get the operands back.
106 So use a simple regular form: a PARALLEL containing a vector
107 of all the operands. */
109 printf (" PATTERN (ins1) = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (%d, operands));\n", n_operands
);
111 /* Record this define_peephole's insn code in the insn,
112 as if it had been recognized to match this. */
113 printf (" INSN_CODE (ins1) = %d;\n",
116 /* Delete the remaining insns. */
118 printf (" delete_for_peephole (NEXT_INSN (ins1), insn);\n");
120 /* See reload1.c for insertion of NOTE which guarantees that this
122 printf (" return NEXT_INSN (insn);\n");
124 printf (" L%d:\n\n", insn_code_number
);
128 match_rtx (rtx x
, struct link
*path
, int fail_label
)
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);
154 printf (" operands[%d] = x;\n", XINT (x
, 0));
155 if (XSTR (x
, 1) && XSTR (x
, 1)[0])
156 printf (" if (! %s (x, %smode)) goto L%d;\n",
157 XSTR (x
, 1), GET_MODE_NAME (GET_MODE (x
)), fail_label
);
166 printf (" if (!rtx_equal_p (operands[%d], x)) goto L%d;\n",
167 XINT (x
, 0), fail_label
);
175 printf (" if (GET_CODE (operands[%d]) != GET_CODE (x)\n", XINT (x
, 0));
176 printf (" || GET_MODE (operands[%d]) != GET_MODE (x)) goto L%d;\n",
177 XINT (x
, 0), fail_label
);
178 printf (" operands[%d] = x;\n", XINT (x
, 0));
181 for (i
= 0; i
< XVECLEN (x
, 1); i
++)
184 match_rtx (XVECEXP (x
, 1, i
), &link
, fail_label
);
189 if (XINT (x
, 0) > max_opno
)
190 max_opno
= XINT (x
, 0);
191 if (XINT (x
, 0) >= n_operands
)
192 n_operands
= 1 + XINT (x
, 0);
198 printf (" operands[%d] = x;\n", XINT (x
, 0));
199 if (XSTR (x
, 1) && XSTR (x
, 1)[0])
200 printf (" if (! %s (x, %smode)) goto L%d;\n",
201 XSTR (x
, 1), GET_MODE_NAME (GET_MODE (x
)), fail_label
);
204 for (i
= 0; i
< XVECLEN (x
, 2); i
++)
207 match_rtx (XVECEXP (x
, 2, i
), &link
, fail_label
);
212 if (XINT (x
, 0) > max_opno
)
213 max_opno
= XINT (x
, 0);
214 if (XINT (x
, 0) >= n_operands
)
215 n_operands
= 1 + XINT (x
, 0);
221 printf (" if (GET_CODE (x) != PARALLEL) goto L%d;\n", fail_label
);
222 printf (" operands[%d] = x;\n", XINT (x
, 0));
223 if (XSTR (x
, 1) && XSTR (x
, 1)[0])
224 printf (" if (! %s (x, %smode)) goto L%d;\n",
225 XSTR (x
, 1), GET_MODE_NAME (GET_MODE (x
)), fail_label
);
228 for (i
= 0; i
< XVECLEN (x
, 2); i
++)
231 match_rtx (XVECEXP (x
, 2, i
), &link
, fail_label
);
236 match_rtx (XEXP (x
, 0), path
, fail_label
);
247 printf (" if (GET_CODE (x) != ");
249 printf (") goto L%d;\n", fail_label
);
251 if (GET_MODE (x
) != VOIDmode
)
253 printf (" if (GET_MODE (x) != %smode) goto L%d;\n",
254 GET_MODE_NAME (GET_MODE (x
)), fail_label
);
259 fmt
= GET_RTX_FORMAT (code
);
260 len
= GET_RTX_LENGTH (code
);
261 for (i
= 0; i
< len
; i
++)
264 if (fmt
[i
] == 'e' || fmt
[i
] == 'u')
265 match_rtx (XEXP (x
, i
), &link
, fail_label
);
266 else if (fmt
[i
] == 'E')
269 printf (" if (XVECLEN (x, %d) != %d) goto L%d;\n",
270 i
, XVECLEN (x
, i
), fail_label
);
271 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
274 match_rtx (XVECEXP (x
, i
, j
), &link
, fail_label
);
277 else if (fmt
[i
] == 'i')
279 /* Make sure that at run time `x' is the RTX we want to test. */
287 printf (" if (XINT (x, %d) != %d) goto L%d;\n",
288 i
, XINT (x
, i
), fail_label
);
290 else if (fmt
[i
] == 'w')
292 /* Make sure that at run time `x' is the RTX we want to test. */
300 printf (" if (XWINT (x, %d) != ", i
);
301 printf (HOST_WIDE_INT_PRINT_DEC
, XWINT (x
, i
));
302 printf (") goto L%d;\n", fail_label
);
304 else if (fmt
[i
] == 's')
306 /* Make sure that at run time `x' is the RTX we want to test. */
314 printf (" if (strcmp (XSTR (x, %d), \"%s\")) goto L%d;\n",
315 i
, XSTR (x
, i
), fail_label
);
320 /* Given a PATH, representing a path down the instruction's
321 pattern from the root to a certain point, output code to
322 evaluate to the rtx at that point. */
325 print_path (struct link
*path
)
329 else if (path
->vecelt
>= 0)
331 printf ("XVECEXP (");
332 print_path (path
->next
);
333 printf (", %d, %d)", path
->pos
, path
->vecelt
);
338 print_path (path
->next
);
339 printf (", %d)", path
->pos
);
344 print_code (RTX_CODE code
)
347 for (p1
= GET_RTX_NAME (code
); *p1
; p1
++)
348 putchar (TOUPPER(*p1
));
351 extern int main (int, char **);
354 main (int argc
, char **argv
)
360 progname
= "genpeep";
362 if (init_md_reader_args (argc
, argv
) != SUCCESS_EXIT_CODE
)
363 return (FATAL_EXIT_CODE
);
365 printf ("/* Generated automatically by the program `genpeep'\n\
366 from the machine description file `md'. */\n\n");
368 printf ("#include \"config.h\"\n");
369 printf ("#include \"system.h\"\n");
370 printf ("#include \"coretypes.h\"\n");
371 printf ("#include \"tm.h\"\n");
372 printf ("#include \"insn-config.h\"\n");
373 printf ("#include \"rtl.h\"\n");
374 printf ("#include \"tm_p.h\"\n");
375 printf ("#include \"regs.h\"\n");
376 printf ("#include \"output.h\"\n");
377 printf ("#include \"real.h\"\n");
378 printf ("#include \"recog.h\"\n");
379 printf ("#include \"except.h\"\n\n");
380 printf ("#include \"function.h\"\n\n");
382 printf ("#ifdef HAVE_peephole\n");
383 printf ("extern rtx peep_operand[];\n\n");
384 printf ("#define operands peep_operand\n\n");
386 printf ("rtx\npeephole (rtx ins1)\n{\n");
387 printf (" rtx insn ATTRIBUTE_UNUSED, x ATTRIBUTE_UNUSED, pat ATTRIBUTE_UNUSED;\n\n");
389 /* Early out: no peepholes for insns followed by barriers. */
390 printf (" if (NEXT_INSN (ins1)\n");
391 printf (" && BARRIER_P (NEXT_INSN (ins1)))\n");
392 printf (" return 0;\n\n");
394 /* Read the machine description. */
398 int line_no
, rtx_number
= 0;
400 desc
= read_md_rtx (&line_no
, &rtx_number
);
404 if (GET_CODE (desc
) == DEFINE_PEEPHOLE
)
409 if (GET_CODE (desc
) == DEFINE_INSN
410 || GET_CODE (desc
) == DEFINE_EXPAND
411 || GET_CODE (desc
) == DEFINE_SPLIT
412 || GET_CODE (desc
) == DEFINE_PEEPHOLE2
)
418 printf (" return 0;\n}\n\n");
423 printf ("rtx peep_operand[%d];\n", max_opno
+ 1);
427 return (ferror (stdout
) != 0 ? FATAL_EXIT_CODE
: SUCCESS_EXIT_CODE
);
430 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
432 get_insn_name (int code ATTRIBUTE_UNUSED
)