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)
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. */
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. */
54 char *xmalloc
PROTO((unsigned));
55 static void fatal
PVPROTO ((char *, ...)) ATTRIBUTE_PRINTF_1
;
56 void fancy_abort
PROTO((void));
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
));
78 int ninsns
= XVECLEN (peep
, 0);
83 printf (" insn = ins1;\n");
85 printf (" want_jump = 0;\n");
88 for (i
= 0; i
< ninsns
; i
++)
92 printf (" do { insn = NEXT_INSN (insn);\n");
93 printf (" if (insn == 0) goto L%d; }\n",
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",
106 printf (" if (GET_CODE (insn) == JUMP_INSN)\n");
107 printf (" want_jump = JUMP_LABEL (insn);\n");
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
);
134 printf (" if (want_jump && GET_CODE (ins1) != JUMP_INSN)\n");
136 printf (" rtx insn2 = emit_jump_insn_before (PATTERN (ins1), ins1);\n");
137 printf (" delete_insn (ins1);\n");
138 printf (" ins1 = ins2;\n");
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",
147 /* Delete the remaining insns. */
149 printf (" delete_for_peephole (NEXT_INSN (ins1), insn);\n");
151 /* See reload1.c for insertion of NOTE which guarantees that this
153 printf (" return NEXT_INSN (insn);\n");
155 printf (" L%d:\n\n", insn_code_number
);
159 match_rtx (x
, path
, fail_label
)
164 register RTX_CODE code
;
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);
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
);
200 printf (" if (!rtx_equal_p (operands[%d], x)) goto L%d;\n",
201 XINT (x
, 0), fail_label
);
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));
215 for (i
= 0; i
< XVECLEN (x
, 1); i
++)
218 match_rtx (XVECEXP (x
, 1, i
), &link
, fail_label
);
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);
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
);
238 for (i
= 0; i
< XVECLEN (x
, 2); i
++)
241 match_rtx (XVECEXP (x
, 2, i
), &link
, fail_label
);
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);
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
);
262 for (i
= 0; i
< XVECLEN (x
, 2); i
++)
265 match_rtx (XVECEXP (x
, 2, i
), &link
, fail_label
);
270 match_rtx (XEXP (x
, 0), path
, fail_label
);
281 printf (" if (GET_CODE (x) != ");
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
);
293 fmt
= GET_RTX_FORMAT (code
);
294 len
= GET_RTX_LENGTH (code
);
295 for (i
= 0; i
< len
; i
++)
298 if (fmt
[i
] == 'e' || fmt
[i
] == 'u')
299 match_rtx (XEXP (x
, i
), &link
, fail_label
);
300 else if (fmt
[i
] == 'E')
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
++)
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. */
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. */
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. */
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. */
364 else if (path
->vecelt
>= 0)
366 printf ("XVECEXP (");
367 print_path (path
->next
);
368 printf (", %d, %d)", path
->pos
, path
->vecelt
);
373 print_path (path
->next
);
374 printf (", %d)", path
->pos
);
383 for (p1
= GET_RTX_NAME (code
); *p1
; p1
++)
385 if (*p1
>= 'a' && *p1
<= 'z')
386 putchar (*p1
+ 'A' - 'a');
396 register char *val
= (char *) malloc (size
);
399 fatal ("virtual memory exhausted");
408 char *result
= (char *) realloc (ptr
, size
);
410 fatal ("virtual memory exhausted");
415 fatal
VPROTO ((char *format
, ...))
422 VA_START (ap
, format
);
425 format
= va_arg (ap
, char *);
428 fprintf (stderr
, "genpeep: ");
429 vfprintf (stderr
, format
, 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. */
441 fatal ("Internal gcc abort.");
455 obstack_init (rtl_obstack
);
458 fatal ("No input file name.");
460 infile
= fopen (argv
[1], "r");
464 exit (FATAL_EXIT_CODE
);
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. */
495 c
= read_skip_spaces (infile
);
500 desc
= read_rtx (infile
);
501 if (GET_CODE (desc
) == DEFINE_PEEPHOLE
)
506 if (GET_CODE (desc
) == DEFINE_INSN
507 || GET_CODE (desc
) == DEFINE_EXPAND
508 || GET_CODE (desc
) == DEFINE_SPLIT
)
514 printf (" return 0;\n}\n\n");
519 printf ("rtx peep_operand[%d];\n", max_opno
+ 1);
522 exit (ferror (stdout
) != 0 ? FATAL_EXIT_CODE
: SUCCESS_EXIT_CODE
);