* java/util/Properties.java (load): Only skip line if the first
[official-gcc.git] / gcc / genpeep.c
blobf5fd84e1aabfd4a2d95f0338de709f1d20b9550d
1 /* Generate code from machine description to perform peephole optimizations.
2 Copyright (C) 1987, 1989, 1992, 1997, 1998,
3 1999, 2000 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
10 version.
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
15 for more details.
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
20 02111-1307, USA. */
23 #include "bconfig.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "errors.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. */
38 struct link
40 struct link *next;
41 int pos;
42 int vecelt;
45 static int max_opno;
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 PARAMS ((rtx));
57 static void match_rtx PARAMS ((rtx, struct link *, int));
58 static void print_path PARAMS ((struct link *));
59 static void print_code PARAMS ((RTX_CODE));
61 static void
62 gen_peephole (peep)
63 rtx peep;
65 int ninsns = XVECLEN (peep, 0);
66 int i;
68 n_operands = 0;
70 printf (" insn = ins1;\n");
71 #if 0
72 printf (" want_jump = 0;\n");
73 #endif
75 for (i = 0; i < ninsns; i++)
77 if (i > 0)
79 printf (" do { insn = NEXT_INSN (insn);\n");
80 printf (" if (insn == 0) goto L%d; }\n",
81 insn_code_number);
82 printf (" while (GET_CODE (insn) == NOTE\n");
83 printf ("\t || (GET_CODE (insn) == INSN\n");
84 printf ("\t && (GET_CODE (PATTERN (insn)) == USE\n");
85 printf ("\t\t || GET_CODE (PATTERN (insn)) == CLOBBER)));\n");
87 printf (" if (GET_CODE (insn) == CODE_LABEL\n\
88 || GET_CODE (insn) == BARRIER)\n goto L%d;\n",
89 insn_code_number);
92 #if 0
93 printf (" if (GET_CODE (insn) == JUMP_INSN)\n");
94 printf (" want_jump = JUMP_LABEL (insn);\n");
95 #endif
97 printf (" pat = PATTERN (insn);\n");
99 /* Walk the insn's pattern, remembering at all times the path
100 down to the walking point. */
102 match_rtx (XVECEXP (peep, 0, i), NULL, insn_code_number);
105 /* We get this far if the pattern matches.
106 Now test the extra condition. */
108 if (XSTR (peep, 1) && XSTR (peep, 1)[0])
109 printf (" if (! (%s)) goto L%d;\n",
110 XSTR (peep, 1), insn_code_number);
112 /* If that matches, construct new pattern and put it in the first insn.
113 This new pattern will never be matched.
114 It exists only so that insn-extract can get the operands back.
115 So use a simple regular form: a PARALLEL containing a vector
116 of all the operands. */
118 printf (" PATTERN (ins1) = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (%d, operands));\n", n_operands);
120 #if 0
121 printf (" if (want_jump && GET_CODE (ins1) != JUMP_INSN)\n");
122 printf (" {\n");
123 printf (" rtx insn2 = emit_jump_insn_before (PATTERN (ins1), ins1);\n");
124 printf (" delete_related_insns (ins1);\n");
125 printf (" ins1 = ins2;\n");
126 printf (" }\n");
127 #endif
129 /* Record this define_peephole's insn code in the insn,
130 as if it had been recognized to match this. */
131 printf (" INSN_CODE (ins1) = %d;\n",
132 insn_code_number);
134 /* Delete the remaining insns. */
135 if (ninsns > 1)
136 printf (" delete_for_peephole (NEXT_INSN (ins1), insn);\n");
138 /* See reload1.c for insertion of NOTE which guarantees that this
139 cannot be zero. */
140 printf (" return NEXT_INSN (insn);\n");
142 printf (" L%d:\n\n", insn_code_number);
145 static void
146 match_rtx (x, path, fail_label)
147 rtx x;
148 struct link *path;
149 int fail_label;
151 RTX_CODE code;
152 int i;
153 int len;
154 const char *fmt;
155 struct link link;
157 if (x == 0)
158 return;
161 code = GET_CODE (x);
163 switch (code)
165 case MATCH_OPERAND:
166 if (XINT (x, 0) > max_opno)
167 max_opno = XINT (x, 0);
168 if (XINT (x, 0) >= n_operands)
169 n_operands = 1 + XINT (x, 0);
171 printf (" x = ");
172 print_path (path);
173 printf (";\n");
175 printf (" operands[%d] = x;\n", XINT (x, 0));
176 if (XSTR (x, 1) && XSTR (x, 1)[0])
177 printf (" if (! %s (x, %smode)) goto L%d;\n",
178 XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label);
179 return;
181 case MATCH_DUP:
182 case MATCH_PAR_DUP:
183 printf (" x = ");
184 print_path (path);
185 printf (";\n");
187 printf (" if (!rtx_equal_p (operands[%d], x)) goto L%d;\n",
188 XINT (x, 0), fail_label);
189 return;
191 case MATCH_OP_DUP:
192 printf (" x = ");
193 print_path (path);
194 printf (";\n");
196 printf (" if (GET_CODE (operands[%d]) != GET_CODE (x)\n", XINT (x, 0));
197 printf (" || GET_MODE (operands[%d]) != GET_MODE (x)) goto L%d;\n",
198 XINT (x, 0), fail_label);
199 printf (" operands[%d] = x;\n", XINT (x, 0));
200 link.next = path;
201 link.vecelt = -1;
202 for (i = 0; i < XVECLEN (x, 1); i++)
204 link.pos = i;
205 match_rtx (XVECEXP (x, 1, i), &link, fail_label);
207 return;
209 case MATCH_OPERATOR:
210 if (XINT (x, 0) > max_opno)
211 max_opno = XINT (x, 0);
212 if (XINT (x, 0) >= n_operands)
213 n_operands = 1 + XINT (x, 0);
215 printf (" x = ");
216 print_path (path);
217 printf (";\n");
219 printf (" operands[%d] = x;\n", XINT (x, 0));
220 if (XSTR (x, 1) && XSTR (x, 1)[0])
221 printf (" if (! %s (x, %smode)) goto L%d;\n",
222 XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label);
223 link.next = path;
224 link.vecelt = -1;
225 for (i = 0; i < XVECLEN (x, 2); i++)
227 link.pos = i;
228 match_rtx (XVECEXP (x, 2, i), &link, fail_label);
230 return;
232 case MATCH_PARALLEL:
233 if (XINT (x, 0) > max_opno)
234 max_opno = XINT (x, 0);
235 if (XINT (x, 0) >= n_operands)
236 n_operands = 1 + XINT (x, 0);
238 printf (" x = ");
239 print_path (path);
240 printf (";\n");
242 printf (" if (GET_CODE (x) != PARALLEL) goto L%d;\n", fail_label);
243 printf (" operands[%d] = x;\n", XINT (x, 0));
244 if (XSTR (x, 1) && XSTR (x, 1)[0])
245 printf (" if (! %s (x, %smode)) goto L%d;\n",
246 XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label);
247 link.next = path;
248 link.pos = 0;
249 for (i = 0; i < XVECLEN (x, 2); i++)
251 link.vecelt = i;
252 match_rtx (XVECEXP (x, 2, i), &link, fail_label);
254 return;
256 case ADDRESS:
257 match_rtx (XEXP (x, 0), path, fail_label);
258 return;
260 default:
261 break;
264 printf (" x = ");
265 print_path (path);
266 printf (";\n");
268 printf (" if (GET_CODE (x) != ");
269 print_code (code);
270 printf (") goto L%d;\n", fail_label);
272 if (GET_MODE (x) != VOIDmode)
274 printf (" if (GET_MODE (x) != %smode) goto L%d;\n",
275 GET_MODE_NAME (GET_MODE (x)), fail_label);
278 link.next = path;
279 link.vecelt = -1;
280 fmt = GET_RTX_FORMAT (code);
281 len = GET_RTX_LENGTH (code);
282 for (i = 0; i < len; i++)
284 link.pos = i;
285 if (fmt[i] == 'e' || fmt[i] == 'u')
286 match_rtx (XEXP (x, i), &link, fail_label);
287 else if (fmt[i] == 'E')
289 int j;
290 printf (" if (XVECLEN (x, %d) != %d) goto L%d;\n",
291 i, XVECLEN (x, i), fail_label);
292 for (j = 0; j < XVECLEN (x, i); j++)
294 link.vecelt = j;
295 match_rtx (XVECEXP (x, i, j), &link, fail_label);
298 else if (fmt[i] == 'i')
300 /* Make sure that at run time `x' is the RTX we want to test. */
301 if (i != 0)
303 printf (" x = ");
304 print_path (path);
305 printf (";\n");
308 printf (" if (XINT (x, %d) != %d) goto L%d;\n",
309 i, XINT (x, i), fail_label);
311 else if (fmt[i] == 'w')
313 /* Make sure that at run time `x' is the RTX we want to test. */
314 if (i != 0)
316 printf (" x = ");
317 print_path (path);
318 printf (";\n");
321 printf (" if (XWINT (x, %d) != ", i);
322 printf (HOST_WIDE_INT_PRINT_DEC, XWINT (x, i));
323 printf (") goto L%d;\n", fail_label);
325 else if (fmt[i] == 's')
327 /* Make sure that at run time `x' is the RTX we want to test. */
328 if (i != 0)
330 printf (" x = ");
331 print_path (path);
332 printf (";\n");
335 printf (" if (strcmp (XSTR (x, %d), \"%s\")) goto L%d;\n",
336 i, XSTR (x, i), fail_label);
341 /* Given a PATH, representing a path down the instruction's
342 pattern from the root to a certain point, output code to
343 evaluate to the rtx at that point. */
345 static void
346 print_path (path)
347 struct link *path;
349 if (path == 0)
350 printf ("pat");
351 else if (path->vecelt >= 0)
353 printf ("XVECEXP (");
354 print_path (path->next);
355 printf (", %d, %d)", path->pos, path->vecelt);
357 else
359 printf ("XEXP (");
360 print_path (path->next);
361 printf (", %d)", path->pos);
365 static void
366 print_code (code)
367 RTX_CODE code;
369 const char *p1;
370 for (p1 = GET_RTX_NAME (code); *p1; p1++)
371 putchar (TOUPPER(*p1));
374 extern int main PARAMS ((int, char **));
377 main (argc, argv)
378 int argc;
379 char **argv;
381 rtx desc;
383 max_opno = -1;
385 progname = "genpeep";
387 if (argc <= 1)
388 fatal ("no input file name");
390 if (init_md_reader_args (argc, argv) != SUCCESS_EXIT_CODE)
391 return (FATAL_EXIT_CODE);
393 printf ("/* Generated automatically by the program `genpeep'\n\
394 from the machine description file `md'. */\n\n");
396 printf ("#include \"config.h\"\n");
397 printf ("#include \"system.h\"\n");
398 printf ("#include \"coretypes.h\"\n");
399 printf ("#include \"tm.h\"\n");
400 printf ("#include \"insn-config.h\"\n");
401 printf ("#include \"rtl.h\"\n");
402 printf ("#include \"tm_p.h\"\n");
403 printf ("#include \"regs.h\"\n");
404 printf ("#include \"output.h\"\n");
405 printf ("#include \"real.h\"\n");
406 printf ("#include \"recog.h\"\n");
407 printf ("#include \"except.h\"\n\n");
408 printf ("#include \"function.h\"\n\n");
410 printf ("#ifdef HAVE_peephole\n");
411 printf ("extern rtx peep_operand[];\n\n");
412 printf ("#define operands peep_operand\n\n");
414 printf ("rtx\npeephole (ins1)\n rtx ins1;\n{\n");
415 printf (" rtx insn ATTRIBUTE_UNUSED, x ATTRIBUTE_UNUSED, pat ATTRIBUTE_UNUSED;\n\n");
417 /* Early out: no peepholes for insns followed by barriers. */
418 printf (" if (NEXT_INSN (ins1)\n");
419 printf (" && GET_CODE (NEXT_INSN (ins1)) == BARRIER)\n");
420 printf (" return 0;\n\n");
422 /* Read the machine description. */
424 while (1)
426 int line_no, rtx_number = 0;
428 desc = read_md_rtx (&line_no, &rtx_number);
429 if (desc == NULL)
430 break;
432 if (GET_CODE (desc) == DEFINE_PEEPHOLE)
434 gen_peephole (desc);
435 insn_code_number++;
437 if (GET_CODE (desc) == DEFINE_INSN
438 || GET_CODE (desc) == DEFINE_EXPAND
439 || GET_CODE (desc) == DEFINE_SPLIT
440 || GET_CODE (desc) == DEFINE_PEEPHOLE2)
442 insn_code_number++;
446 printf (" return 0;\n}\n\n");
448 if (max_opno == -1)
449 max_opno = 1;
451 printf ("rtx peep_operand[%d];\n", max_opno + 1);
452 printf ("#endif\n");
454 fflush (stdout);
455 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
458 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
459 const char *
460 get_insn_name (code)
461 int code ATTRIBUTE_UNUSED;
463 return NULL;