1 /* Generate code from machine description to perform peephole optimizations.
2 Copyright (C) 1987, 89, 92, 97, 98, 1999 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. */
27 static struct obstack obstack
;
28 struct obstack
*rtl_obstack
= &obstack
;
30 #define obstack_chunk_alloc xmalloc
31 #define obstack_chunk_free free
33 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
34 char **insn_name_ptr
= 0;
36 /* While tree-walking an instruction pattern, we keep a chain
37 of these `struct link's to record how to get down to the
38 current position. In each one, POS is the operand number,
39 and if the operand is a vector VEC is the element number.
40 VEC is -1 if the operand is not a vector. */
49 void fatal
PVPROTO ((const char *, ...))
50 ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN
;
54 /* Number of operands used in current peephole definition. */
56 static int n_operands
;
58 /* Peephole optimizations get insn codes just like insn patterns.
59 Count them so we know the code of the define_peephole we are handling. */
61 static int insn_code_number
= 0;
63 static void gen_peephole
PROTO((rtx
));
64 static void match_rtx
PROTO((rtx
, struct link
*, int));
65 static void print_path
PROTO((struct link
*));
66 static void print_code
PROTO((RTX_CODE
));
72 int ninsns
= XVECLEN (peep
, 0);
77 printf (" insn = ins1;\n");
79 printf (" want_jump = 0;\n");
82 for (i
= 0; i
< ninsns
; i
++)
86 printf (" do { insn = NEXT_INSN (insn);\n");
87 printf (" if (insn == 0) goto L%d; }\n",
89 printf (" while (GET_CODE (insn) == NOTE\n");
90 printf ("\t || (GET_CODE (insn) == INSN\n");
91 printf ("\t && (GET_CODE (PATTERN (insn)) == USE\n");
92 printf ("\t\t || GET_CODE (PATTERN (insn)) == CLOBBER)));\n");
94 printf (" if (GET_CODE (insn) == CODE_LABEL\n\
95 || GET_CODE (insn) == BARRIER)\n goto L%d;\n",
100 printf (" if (GET_CODE (insn) == JUMP_INSN)\n");
101 printf (" want_jump = JUMP_LABEL (insn);\n");
104 printf (" pat = PATTERN (insn);\n");
106 /* Walk the insn's pattern, remembering at all times the path
107 down to the walking point. */
109 match_rtx (XVECEXP (peep
, 0, i
), NULL_PTR
, insn_code_number
);
112 /* We get this far if the pattern matches.
113 Now test the extra condition. */
115 if (XSTR (peep
, 1) && XSTR (peep
, 1)[0])
116 printf (" if (! (%s)) goto L%d;\n",
117 XSTR (peep
, 1), insn_code_number
);
119 /* If that matches, construct new pattern and put it in the first insn.
120 This new pattern will never be matched.
121 It exists only so that insn-extract can get the operands back.
122 So use a simple regular form: a PARALLEL containing a vector
123 of all the operands. */
125 printf (" PATTERN (ins1) = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (%d, operands));\n", n_operands
);
128 printf (" if (want_jump && GET_CODE (ins1) != JUMP_INSN)\n");
130 printf (" rtx insn2 = emit_jump_insn_before (PATTERN (ins1), ins1);\n");
131 printf (" delete_insn (ins1);\n");
132 printf (" ins1 = ins2;\n");
136 /* Record this define_peephole's insn code in the insn,
137 as if it had been recognized to match this. */
138 printf (" INSN_CODE (ins1) = %d;\n",
141 /* Delete the remaining insns. */
143 printf (" delete_for_peephole (NEXT_INSN (ins1), insn);\n");
145 /* See reload1.c for insertion of NOTE which guarantees that this
147 printf (" return NEXT_INSN (insn);\n");
149 printf (" L%d:\n\n", insn_code_number
);
153 match_rtx (x
, path
, fail_label
)
158 register RTX_CODE code
;
161 register const char *fmt
;
173 if (XINT (x
, 0) > max_opno
)
174 max_opno
= XINT (x
, 0);
175 if (XINT (x
, 0) >= n_operands
)
176 n_operands
= 1 + XINT (x
, 0);
182 printf (" operands[%d] = x;\n", XINT (x
, 0));
183 if (XSTR (x
, 1) && XSTR (x
, 1)[0])
184 printf (" if (! %s (x, %smode)) goto L%d;\n",
185 XSTR (x
, 1), GET_MODE_NAME (GET_MODE (x
)), fail_label
);
194 printf (" if (!rtx_equal_p (operands[%d], x)) goto L%d;\n",
195 XINT (x
, 0), fail_label
);
203 printf (" if (GET_CODE (operands[%d]) != GET_CODE (x)\n", XINT (x
, 0));
204 printf (" || GET_MODE (operands[%d]) != GET_MODE (x)) goto L%d;\n",
205 XINT (x
, 0), fail_label
);
206 printf (" operands[%d] = x;\n", XINT (x
, 0));
209 for (i
= 0; i
< XVECLEN (x
, 1); i
++)
212 match_rtx (XVECEXP (x
, 1, i
), &link
, fail_label
);
217 if (XINT (x
, 0) > max_opno
)
218 max_opno
= XINT (x
, 0);
219 if (XINT (x
, 0) >= n_operands
)
220 n_operands
= 1 + XINT (x
, 0);
226 printf (" operands[%d] = x;\n", XINT (x
, 0));
227 if (XSTR (x
, 1) && XSTR (x
, 1)[0])
228 printf (" if (! %s (x, %smode)) goto L%d;\n",
229 XSTR (x
, 1), GET_MODE_NAME (GET_MODE (x
)), fail_label
);
232 for (i
= 0; i
< XVECLEN (x
, 2); i
++)
235 match_rtx (XVECEXP (x
, 2, i
), &link
, fail_label
);
240 if (XINT (x
, 0) > max_opno
)
241 max_opno
= XINT (x
, 0);
242 if (XINT (x
, 0) >= n_operands
)
243 n_operands
= 1 + XINT (x
, 0);
249 printf (" if (GET_CODE (x) != PARALLEL) goto L%d;\n", fail_label
);
250 printf (" operands[%d] = x;\n", XINT (x
, 0));
251 if (XSTR (x
, 1) && XSTR (x
, 1)[0])
252 printf (" if (! %s (x, %smode)) goto L%d;\n",
253 XSTR (x
, 1), GET_MODE_NAME (GET_MODE (x
)), fail_label
);
256 for (i
= 0; i
< XVECLEN (x
, 2); i
++)
259 match_rtx (XVECEXP (x
, 2, i
), &link
, fail_label
);
264 match_rtx (XEXP (x
, 0), path
, fail_label
);
275 printf (" if (GET_CODE (x) != ");
277 printf (") goto L%d;\n", fail_label
);
279 if (GET_MODE (x
) != VOIDmode
)
281 printf (" if (GET_MODE (x) != %smode) goto L%d;\n",
282 GET_MODE_NAME (GET_MODE (x
)), fail_label
);
287 fmt
= GET_RTX_FORMAT (code
);
288 len
= GET_RTX_LENGTH (code
);
289 for (i
= 0; i
< len
; i
++)
292 if (fmt
[i
] == 'e' || fmt
[i
] == 'u')
293 match_rtx (XEXP (x
, i
), &link
, fail_label
);
294 else if (fmt
[i
] == 'E')
297 printf (" if (XVECLEN (x, %d) != %d) goto L%d;\n",
298 i
, XVECLEN (x
, i
), fail_label
);
299 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
302 match_rtx (XVECEXP (x
, i
, j
), &link
, fail_label
);
305 else if (fmt
[i
] == 'i')
307 /* Make sure that at run time `x' is the RTX we want to test. */
315 printf (" if (XINT (x, %d) != %d) goto L%d;\n",
316 i
, XINT (x
, i
), fail_label
);
318 else if (fmt
[i
] == 'w')
320 /* Make sure that at run time `x' is the RTX we want to test. */
328 printf (" if (XWINT (x, %d) != ", i
);
329 printf (HOST_WIDE_INT_PRINT_DEC
, XWINT (x
, i
));
330 printf (") goto L%d;\n", fail_label
);
332 else if (fmt
[i
] == 's')
334 /* Make sure that at run time `x' is the RTX we want to test. */
342 printf (" if (strcmp (XSTR (x, %d), \"%s\")) goto L%d;\n",
343 i
, XSTR (x
, i
), fail_label
);
348 /* Given a PATH, representing a path down the instruction's
349 pattern from the root to a certain point, output code to
350 evaluate to the rtx at that point. */
358 else if (path
->vecelt
>= 0)
360 printf ("XVECEXP (");
361 print_path (path
->next
);
362 printf (", %d, %d)", path
->pos
, path
->vecelt
);
367 print_path (path
->next
);
368 printf (", %d)", path
->pos
);
376 register const char *p1
;
377 for (p1
= GET_RTX_NAME (code
); *p1
; p1
++)
379 if (*p1
>= 'a' && *p1
<= 'z')
380 putchar (*p1
+ 'A' - 'a');
390 register PTR val
= (PTR
) malloc (size
);
393 fatal ("virtual memory exhausted");
404 ptr
= (PTR
) realloc (old
, size
);
406 ptr
= (PTR
) malloc (size
);
408 fatal ("virtual memory exhausted");
413 fatal
VPROTO ((const char *format
, ...))
415 #ifndef ANSI_PROTOTYPES
420 VA_START (ap
, format
);
422 #ifndef ANSI_PROTOTYPES
423 format
= va_arg (ap
, const char *);
426 fprintf (stderr
, "genpeep: ");
427 vfprintf (stderr
, format
, ap
);
429 fprintf (stderr
, "\n");
430 exit (FATAL_EXIT_CODE
);
444 obstack_init (rtl_obstack
);
447 fatal ("No input file name.");
449 infile
= fopen (argv
[1], "r");
453 exit (FATAL_EXIT_CODE
);
458 printf ("/* Generated automatically by the program `genpeep'\n\
459 from the machine description file `md'. */\n\n");
461 printf ("#include \"config.h\"\n");
462 printf ("#include \"system.h\"\n");
463 printf ("#include \"insn-config.h\"\n");
464 printf ("#include \"rtl.h\"\n");
465 printf ("#include \"regs.h\"\n");
466 printf ("#include \"output.h\"\n");
467 printf ("#include \"real.h\"\n");
468 printf ("#include \"recog.h\"\n");
469 printf ("#include \"except.h\"\n\n");
470 printf ("#include \"function.h\"\n\n");
472 printf ("extern rtx peep_operand[];\n\n");
473 printf ("#define operands peep_operand\n\n");
475 printf ("rtx\npeephole (ins1)\n rtx ins1;\n{\n");
476 printf (" rtx insn ATTRIBUTE_UNUSED, x ATTRIBUTE_UNUSED, pat ATTRIBUTE_UNUSED;\n\n");
478 /* Early out: no peepholes for insns followed by barriers. */
479 printf (" if (NEXT_INSN (ins1)\n");
480 printf (" && GET_CODE (NEXT_INSN (ins1)) == BARRIER)\n");
481 printf (" return 0;\n\n");
483 /* Read the machine description. */
487 c
= read_skip_spaces (infile
);
492 desc
= read_rtx (infile
);
493 if (GET_CODE (desc
) == DEFINE_PEEPHOLE
)
498 if (GET_CODE (desc
) == DEFINE_INSN
499 || GET_CODE (desc
) == DEFINE_EXPAND
500 || GET_CODE (desc
) == DEFINE_SPLIT
)
506 printf (" return 0;\n}\n\n");
511 printf ("rtx peep_operand[%d];\n", max_opno
+ 1);
514 exit (ferror (stdout
) != 0 ? FATAL_EXIT_CODE
: SUCCESS_EXIT_CODE
);