Do not do src->dest copy if register would not be allocated a normal register
[official-gcc.git] / gcc / genpeep.c
blobfd3939843bb77740f1f548331d6602d93a028469
1 /* Generate code from machine description to perform peephole optimizations.
2 Copyright (C) 1987, 1989, 1992, 1997, 1998 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 #include "hconfig.h"
23 #ifdef __STDC__
24 #include <stdarg.h>
25 #else
26 #include <varargs.h>
27 #endif
28 #include "system.h"
29 #include "rtl.h"
30 #include "obstack.h"
32 static struct obstack obstack;
33 struct obstack *rtl_obstack = &obstack;
35 #define obstack_chunk_alloc xmalloc
36 #define obstack_chunk_free free
38 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
39 char **insn_name_ptr = 0;
41 /* While tree-walking an instruction pattern, we keep a chain
42 of these `struct link's to record how to get down to the
43 current position. In each one, POS is the operand number,
44 and if the operand is a vector VEC is the element number.
45 VEC is -1 if the operand is not a vector. */
47 struct link
49 struct link *next;
50 int pos;
51 int vecelt;
54 char *xmalloc PROTO((unsigned));
55 static void fatal PVPROTO ((char *, ...)) ATTRIBUTE_PRINTF_1;
56 void fancy_abort PROTO((void));
58 static int max_opno;
60 /* Number of operands used in current peephole definition. */
62 static int n_operands;
64 /* Peephole optimizations get insn codes just like insn patterns.
65 Count them so we know the code of the define_peephole we are handling. */
67 static int insn_code_number = 0;
69 static void gen_peephole PROTO((rtx));
70 static void match_rtx PROTO((rtx, struct link *, int));
71 static void print_path PROTO((struct link *));
72 static void print_code PROTO((RTX_CODE));
74 static void
75 gen_peephole (peep)
76 rtx peep;
78 int ninsns = XVECLEN (peep, 0);
79 int i;
81 n_operands = 0;
83 printf (" insn = ins1;\n");
84 #if 0
85 printf (" want_jump = 0;\n");
86 #endif
88 for (i = 0; i < ninsns; i++)
90 if (i > 0)
92 printf (" do { insn = NEXT_INSN (insn);\n");
93 printf (" if (insn == 0) goto L%d; }\n",
94 insn_code_number);
95 printf (" while (GET_CODE (insn) == NOTE\n");
96 printf ("\t || (GET_CODE (insn) == INSN\n");
97 printf ("\t && (GET_CODE (PATTERN (insn)) == USE\n");
98 printf ("\t\t || GET_CODE (PATTERN (insn)) == CLOBBER)));\n");
100 printf (" if (GET_CODE (insn) == CODE_LABEL\n\
101 || GET_CODE (insn) == BARRIER)\n goto L%d;\n",
102 insn_code_number);
105 #if 0
106 printf (" if (GET_CODE (insn) == JUMP_INSN)\n");
107 printf (" want_jump = JUMP_LABEL (insn);\n");
108 #endif
110 printf (" pat = PATTERN (insn);\n");
112 /* Walk the insn's pattern, remembering at all times the path
113 down to the walking point. */
115 match_rtx (XVECEXP (peep, 0, i), NULL_PTR, insn_code_number);
118 /* We get this far if the pattern matches.
119 Now test the extra condition. */
121 if (XSTR (peep, 1) && XSTR (peep, 1)[0])
122 printf (" if (! (%s)) goto L%d;\n",
123 XSTR (peep, 1), insn_code_number);
125 /* If that matches, construct new pattern and put it in the first insn.
126 This new pattern will never be matched.
127 It exists only so that insn-extract can get the operands back.
128 So use a simple regular form: a PARALLEL containing a vector
129 of all the operands. */
131 printf (" PATTERN (ins1) = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (%d, operands));\n", n_operands);
133 #if 0
134 printf (" if (want_jump && GET_CODE (ins1) != JUMP_INSN)\n");
135 printf (" {\n");
136 printf (" rtx insn2 = emit_jump_insn_before (PATTERN (ins1), ins1);\n");
137 printf (" delete_insn (ins1);\n");
138 printf (" ins1 = ins2;\n");
139 printf (" }\n");
140 #endif
142 /* Record this define_peephole's insn code in the insn,
143 as if it had been recognized to match this. */
144 printf (" INSN_CODE (ins1) = %d;\n",
145 insn_code_number);
147 /* Delete the remaining insns. */
148 if (ninsns > 1)
149 printf (" delete_for_peephole (NEXT_INSN (ins1), insn);\n");
151 /* See reload1.c for insertion of NOTE which guarantees that this
152 cannot be zero. */
153 printf (" return NEXT_INSN (insn);\n");
155 printf (" L%d:\n\n", insn_code_number);
158 static void
159 match_rtx (x, path, fail_label)
160 rtx x;
161 struct link *path;
162 int fail_label;
164 register RTX_CODE code;
165 register int i;
166 register int len;
167 register char *fmt;
168 struct link link;
170 if (x == 0)
171 return;
174 code = GET_CODE (x);
176 switch (code)
178 case MATCH_OPERAND:
179 if (XINT (x, 0) > max_opno)
180 max_opno = XINT (x, 0);
181 if (XINT (x, 0) >= n_operands)
182 n_operands = 1 + XINT (x, 0);
184 printf (" x = ");
185 print_path (path);
186 printf (";\n");
188 printf (" operands[%d] = x;\n", XINT (x, 0));
189 if (XSTR (x, 1) && XSTR (x, 1)[0])
190 printf (" if (! %s (x, %smode)) goto L%d;\n",
191 XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label);
192 return;
194 case MATCH_DUP:
195 case MATCH_PAR_DUP:
196 printf (" x = ");
197 print_path (path);
198 printf (";\n");
200 printf (" if (!rtx_equal_p (operands[%d], x)) goto L%d;\n",
201 XINT (x, 0), fail_label);
202 return;
204 case MATCH_OP_DUP:
205 printf (" x = ");
206 print_path (path);
207 printf (";\n");
209 printf (" if (GET_CODE (operands[%d]) != GET_CODE (x)\n", XINT (x, 0));
210 printf (" || GET_MODE (operands[%d]) != GET_MODE (x)) goto L%d;\n",
211 XINT (x, 0), fail_label);
212 printf (" operands[%d] = x;\n", XINT (x, 0));
213 link.next = path;
214 link.vecelt = -1;
215 for (i = 0; i < XVECLEN (x, 1); i++)
217 link.pos = i;
218 match_rtx (XVECEXP (x, 1, i), &link, fail_label);
220 return;
222 case MATCH_OPERATOR:
223 if (XINT (x, 0) > max_opno)
224 max_opno = XINT (x, 0);
225 if (XINT (x, 0) >= n_operands)
226 n_operands = 1 + XINT (x, 0);
228 printf (" x = ");
229 print_path (path);
230 printf (";\n");
232 printf (" operands[%d] = x;\n", XINT (x, 0));
233 if (XSTR (x, 1) && XSTR (x, 1)[0])
234 printf (" if (! %s (x, %smode)) goto L%d;\n",
235 XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label);
236 link.next = path;
237 link.vecelt = -1;
238 for (i = 0; i < XVECLEN (x, 2); i++)
240 link.pos = i;
241 match_rtx (XVECEXP (x, 2, i), &link, fail_label);
243 return;
245 case MATCH_PARALLEL:
246 if (XINT (x, 0) > max_opno)
247 max_opno = XINT (x, 0);
248 if (XINT (x, 0) >= n_operands)
249 n_operands = 1 + XINT (x, 0);
251 printf (" x = ");
252 print_path (path);
253 printf (";\n");
255 printf (" if (GET_CODE (x) != PARALLEL) goto L%d;\n", fail_label);
256 printf (" operands[%d] = x;\n", XINT (x, 0));
257 if (XSTR (x, 1) && XSTR (x, 1)[0])
258 printf (" if (! %s (x, %smode)) goto L%d;\n",
259 XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label);
260 link.next = path;
261 link.pos = 0;
262 for (i = 0; i < XVECLEN (x, 2); i++)
264 link.vecelt = i;
265 match_rtx (XVECEXP (x, 2, i), &link, fail_label);
267 return;
269 case ADDRESS:
270 match_rtx (XEXP (x, 0), path, fail_label);
271 return;
273 default:
274 break;
277 printf (" x = ");
278 print_path (path);
279 printf (";\n");
281 printf (" if (GET_CODE (x) != ");
282 print_code (code);
283 printf (") goto L%d;\n", fail_label);
285 if (GET_MODE (x) != VOIDmode)
287 printf (" if (GET_MODE (x) != %smode) goto L%d;\n",
288 GET_MODE_NAME (GET_MODE (x)), fail_label);
291 link.next = path;
292 link.vecelt = -1;
293 fmt = GET_RTX_FORMAT (code);
294 len = GET_RTX_LENGTH (code);
295 for (i = 0; i < len; i++)
297 link.pos = i;
298 if (fmt[i] == 'e' || fmt[i] == 'u')
299 match_rtx (XEXP (x, i), &link, fail_label);
300 else if (fmt[i] == 'E')
302 int j;
303 printf (" if (XVECLEN (x, %d) != %d) goto L%d;\n",
304 i, XVECLEN (x, i), fail_label);
305 for (j = 0; j < XVECLEN (x, i); j++)
307 link.vecelt = j;
308 match_rtx (XVECEXP (x, i, j), &link, fail_label);
311 else if (fmt[i] == 'i')
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 (XINT (x, %d) != %d) goto L%d;\n",
322 i, XINT (x, i), fail_label);
324 else if (fmt[i] == 'w')
326 /* Make sure that at run time `x' is the RTX we want to test. */
327 if (i != 0)
329 printf (" x = ");
330 print_path (path);
331 printf (";\n");
334 printf (" if (XWINT (x, %d) != ", i);
335 printf (HOST_WIDE_INT_PRINT_DEC, XWINT (x, i));
336 printf (") goto L%d;\n", fail_label);
338 else if (fmt[i] == 's')
340 /* Make sure that at run time `x' is the RTX we want to test. */
341 if (i != 0)
343 printf (" x = ");
344 print_path (path);
345 printf (";\n");
348 printf (" if (strcmp (XSTR (x, %d), \"%s\")) goto L%d;\n",
349 i, XSTR (x, i), fail_label);
354 /* Given a PATH, representing a path down the instruction's
355 pattern from the root to a certain point, output code to
356 evaluate to the rtx at that point. */
358 static void
359 print_path (path)
360 struct link *path;
362 if (path == 0)
363 printf ("pat");
364 else if (path->vecelt >= 0)
366 printf ("XVECEXP (");
367 print_path (path->next);
368 printf (", %d, %d)", path->pos, path->vecelt);
370 else
372 printf ("XEXP (");
373 print_path (path->next);
374 printf (", %d)", path->pos);
378 static void
379 print_code (code)
380 RTX_CODE code;
382 register char *p1;
383 for (p1 = GET_RTX_NAME (code); *p1; p1++)
385 if (*p1 >= 'a' && *p1 <= 'z')
386 putchar (*p1 + 'A' - 'a');
387 else
388 putchar (*p1);
392 char *
393 xmalloc (size)
394 unsigned size;
396 register char *val = (char *) malloc (size);
398 if (val == 0)
399 fatal ("virtual memory exhausted");
400 return val;
403 char *
404 xrealloc (ptr, size)
405 char *ptr;
406 unsigned size;
408 char *result = (char *) realloc (ptr, size);
409 if (!result)
410 fatal ("virtual memory exhausted");
411 return result;
414 static void
415 fatal VPROTO ((char *format, ...))
417 #ifndef __STDC__
418 char *format;
419 #endif
420 va_list ap;
422 VA_START (ap, format);
424 #ifndef __STDC__
425 format = va_arg (ap, char *);
426 #endif
428 fprintf (stderr, "genpeep: ");
429 vfprintf (stderr, format, ap);
430 va_end (ap);
431 fprintf (stderr, "\n");
432 exit (FATAL_EXIT_CODE);
435 /* More 'friendly' abort that prints the line and file.
436 config.h can #define abort fancy_abort if you like that sort of thing. */
438 void
439 fancy_abort ()
441 fatal ("Internal gcc abort.");
445 main (argc, argv)
446 int argc;
447 char **argv;
449 rtx desc;
450 FILE *infile;
451 register int c;
453 max_opno = -1;
455 obstack_init (rtl_obstack);
457 if (argc <= 1)
458 fatal ("No input file name.");
460 infile = fopen (argv[1], "r");
461 if (infile == 0)
463 perror (argv[1]);
464 exit (FATAL_EXIT_CODE);
467 init_rtl ();
469 printf ("/* Generated automatically by the program `genpeep'\n\
470 from the machine description file `md'. */\n\n");
472 printf ("#include \"config.h\"\n");
473 printf ("#include \"system.h\"\n");
474 printf ("#include \"rtl.h\"\n");
475 printf ("#include \"regs.h\"\n");
476 printf ("#include \"output.h\"\n");
477 printf ("#include \"real.h\"\n");
478 printf ("#include \"except.h\"\n\n");
480 printf ("extern rtx peep_operand[];\n\n");
481 printf ("#define operands peep_operand\n\n");
483 printf ("rtx\npeephole (ins1)\n rtx ins1;\n{\n");
484 printf (" rtx insn, x, pat;\n\n");
486 /* Early out: no peepholes for insns followed by barriers. */
487 printf (" if (NEXT_INSN (ins1)\n");
488 printf (" && GET_CODE (NEXT_INSN (ins1)) == BARRIER)\n");
489 printf (" return 0;\n\n");
491 /* Read the machine description. */
493 while (1)
495 c = read_skip_spaces (infile);
496 if (c == EOF)
497 break;
498 ungetc (c, infile);
500 desc = read_rtx (infile);
501 if (GET_CODE (desc) == DEFINE_PEEPHOLE)
503 gen_peephole (desc);
504 insn_code_number++;
506 if (GET_CODE (desc) == DEFINE_INSN
507 || GET_CODE (desc) == DEFINE_EXPAND
508 || GET_CODE (desc) == DEFINE_SPLIT)
510 insn_code_number++;
514 printf (" return 0;\n}\n\n");
516 if (max_opno == -1)
517 max_opno = 1;
519 printf ("rtx peep_operand[%d];\n", max_opno + 1);
521 fflush (stdout);
522 exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
523 /* NOTREACHED */
524 return 0;