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. */
28 static struct obstack obstack
;
29 struct obstack
*rtl_obstack
= &obstack
;
31 #define obstack_chunk_alloc xmalloc
32 #define obstack_chunk_free free
34 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
35 char **insn_name_ptr
= 0;
37 /* While tree-walking an instruction pattern, we keep a chain
38 of these `struct link's to record how to get down to the
39 current position. In each one, POS is the operand number,
40 and if the operand is a vector VEC is the element number.
41 VEC is -1 if the operand is not a vector. */
52 /* Number of operands used in current peephole definition. */
54 static int n_operands
;
56 /* Peephole optimizations get insn codes just like insn patterns.
57 Count them so we know the code of the define_peephole we are handling. */
59 static int insn_code_number
= 0;
61 static void gen_peephole
PROTO((rtx
));
62 static void match_rtx
PROTO((rtx
, struct link
*, int));
63 static void print_path
PROTO((struct link
*));
64 static void print_code
PROTO((RTX_CODE
));
70 int ninsns
= XVECLEN (peep
, 0);
75 printf (" insn = ins1;\n");
77 printf (" want_jump = 0;\n");
80 for (i
= 0; i
< ninsns
; i
++)
84 printf (" do { insn = NEXT_INSN (insn);\n");
85 printf (" if (insn == 0) goto L%d; }\n",
87 printf (" while (GET_CODE (insn) == NOTE\n");
88 printf ("\t || (GET_CODE (insn) == INSN\n");
89 printf ("\t && (GET_CODE (PATTERN (insn)) == USE\n");
90 printf ("\t\t || GET_CODE (PATTERN (insn)) == CLOBBER)));\n");
92 printf (" if (GET_CODE (insn) == CODE_LABEL\n\
93 || GET_CODE (insn) == BARRIER)\n goto L%d;\n",
98 printf (" if (GET_CODE (insn) == JUMP_INSN)\n");
99 printf (" want_jump = JUMP_LABEL (insn);\n");
102 printf (" pat = PATTERN (insn);\n");
104 /* Walk the insn's pattern, remembering at all times the path
105 down to the walking point. */
107 match_rtx (XVECEXP (peep
, 0, i
), NULL_PTR
, insn_code_number
);
110 /* We get this far if the pattern matches.
111 Now test the extra condition. */
113 if (XSTR (peep
, 1) && XSTR (peep
, 1)[0])
114 printf (" if (! (%s)) goto L%d;\n",
115 XSTR (peep
, 1), insn_code_number
);
117 /* If that matches, construct new pattern and put it in the first insn.
118 This new pattern will never be matched.
119 It exists only so that insn-extract can get the operands back.
120 So use a simple regular form: a PARALLEL containing a vector
121 of all the operands. */
123 printf (" PATTERN (ins1) = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (%d, operands));\n", n_operands
);
126 printf (" if (want_jump && GET_CODE (ins1) != JUMP_INSN)\n");
128 printf (" rtx insn2 = emit_jump_insn_before (PATTERN (ins1), ins1);\n");
129 printf (" delete_insn (ins1);\n");
130 printf (" ins1 = ins2;\n");
134 /* Record this define_peephole's insn code in the insn,
135 as if it had been recognized to match this. */
136 printf (" INSN_CODE (ins1) = %d;\n",
139 /* Delete the remaining insns. */
141 printf (" delete_for_peephole (NEXT_INSN (ins1), insn);\n");
143 /* See reload1.c for insertion of NOTE which guarantees that this
145 printf (" return NEXT_INSN (insn);\n");
147 printf (" L%d:\n\n", insn_code_number
);
151 match_rtx (x
, path
, fail_label
)
156 register RTX_CODE code
;
159 register const char *fmt
;
171 if (XINT (x
, 0) > max_opno
)
172 max_opno
= XINT (x
, 0);
173 if (XINT (x
, 0) >= n_operands
)
174 n_operands
= 1 + XINT (x
, 0);
180 printf (" operands[%d] = x;\n", XINT (x
, 0));
181 if (XSTR (x
, 1) && XSTR (x
, 1)[0])
182 printf (" if (! %s (x, %smode)) goto L%d;\n",
183 XSTR (x
, 1), GET_MODE_NAME (GET_MODE (x
)), fail_label
);
192 printf (" if (!rtx_equal_p (operands[%d], x)) goto L%d;\n",
193 XINT (x
, 0), fail_label
);
201 printf (" if (GET_CODE (operands[%d]) != GET_CODE (x)\n", XINT (x
, 0));
202 printf (" || GET_MODE (operands[%d]) != GET_MODE (x)) goto L%d;\n",
203 XINT (x
, 0), fail_label
);
204 printf (" operands[%d] = x;\n", XINT (x
, 0));
207 for (i
= 0; i
< XVECLEN (x
, 1); i
++)
210 match_rtx (XVECEXP (x
, 1, i
), &link
, fail_label
);
215 if (XINT (x
, 0) > max_opno
)
216 max_opno
= XINT (x
, 0);
217 if (XINT (x
, 0) >= n_operands
)
218 n_operands
= 1 + XINT (x
, 0);
224 printf (" operands[%d] = x;\n", XINT (x
, 0));
225 if (XSTR (x
, 1) && XSTR (x
, 1)[0])
226 printf (" if (! %s (x, %smode)) goto L%d;\n",
227 XSTR (x
, 1), GET_MODE_NAME (GET_MODE (x
)), fail_label
);
230 for (i
= 0; i
< XVECLEN (x
, 2); i
++)
233 match_rtx (XVECEXP (x
, 2, i
), &link
, fail_label
);
238 if (XINT (x
, 0) > max_opno
)
239 max_opno
= XINT (x
, 0);
240 if (XINT (x
, 0) >= n_operands
)
241 n_operands
= 1 + XINT (x
, 0);
247 printf (" if (GET_CODE (x) != PARALLEL) goto L%d;\n", fail_label
);
248 printf (" operands[%d] = x;\n", XINT (x
, 0));
249 if (XSTR (x
, 1) && XSTR (x
, 1)[0])
250 printf (" if (! %s (x, %smode)) goto L%d;\n",
251 XSTR (x
, 1), GET_MODE_NAME (GET_MODE (x
)), fail_label
);
254 for (i
= 0; i
< XVECLEN (x
, 2); i
++)
257 match_rtx (XVECEXP (x
, 2, i
), &link
, fail_label
);
262 match_rtx (XEXP (x
, 0), path
, fail_label
);
273 printf (" if (GET_CODE (x) != ");
275 printf (") goto L%d;\n", fail_label
);
277 if (GET_MODE (x
) != VOIDmode
)
279 printf (" if (GET_MODE (x) != %smode) goto L%d;\n",
280 GET_MODE_NAME (GET_MODE (x
)), fail_label
);
285 fmt
= GET_RTX_FORMAT (code
);
286 len
= GET_RTX_LENGTH (code
);
287 for (i
= 0; i
< len
; i
++)
290 if (fmt
[i
] == 'e' || fmt
[i
] == 'u')
291 match_rtx (XEXP (x
, i
), &link
, fail_label
);
292 else if (fmt
[i
] == 'E')
295 printf (" if (XVECLEN (x, %d) != %d) goto L%d;\n",
296 i
, XVECLEN (x
, i
), fail_label
);
297 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
300 match_rtx (XVECEXP (x
, i
, j
), &link
, fail_label
);
303 else if (fmt
[i
] == 'i')
305 /* Make sure that at run time `x' is the RTX we want to test. */
313 printf (" if (XINT (x, %d) != %d) goto L%d;\n",
314 i
, XINT (x
, i
), fail_label
);
316 else if (fmt
[i
] == 'w')
318 /* Make sure that at run time `x' is the RTX we want to test. */
326 printf (" if (XWINT (x, %d) != ", i
);
327 printf (HOST_WIDE_INT_PRINT_DEC
, XWINT (x
, i
));
328 printf (") goto L%d;\n", fail_label
);
330 else if (fmt
[i
] == 's')
332 /* Make sure that at run time `x' is the RTX we want to test. */
340 printf (" if (strcmp (XSTR (x, %d), \"%s\")) goto L%d;\n",
341 i
, XSTR (x
, i
), fail_label
);
346 /* Given a PATH, representing a path down the instruction's
347 pattern from the root to a certain point, output code to
348 evaluate to the rtx at that point. */
356 else if (path
->vecelt
>= 0)
358 printf ("XVECEXP (");
359 print_path (path
->next
);
360 printf (", %d, %d)", path
->pos
, path
->vecelt
);
365 print_path (path
->next
);
366 printf (", %d)", path
->pos
);
374 register const char *p1
;
375 for (p1
= GET_RTX_NAME (code
); *p1
; p1
++)
377 if (*p1
>= 'a' && *p1
<= 'z')
378 putchar (*p1
+ 'A' - 'a');
388 register PTR val
= (PTR
) malloc (size
);
391 fatal ("virtual memory exhausted");
402 ptr
= (PTR
) realloc (old
, size
);
404 ptr
= (PTR
) malloc (size
);
406 fatal ("virtual memory exhausted");
421 progname
= "genpeep";
422 obstack_init (rtl_obstack
);
425 fatal ("No input file name.");
427 infile
= fopen (argv
[1], "r");
431 exit (FATAL_EXIT_CODE
);
436 printf ("/* Generated automatically by the program `genpeep'\n\
437 from the machine description file `md'. */\n\n");
439 printf ("#include \"config.h\"\n");
440 printf ("#include \"system.h\"\n");
441 printf ("#include \"insn-config.h\"\n");
442 printf ("#include \"rtl.h\"\n");
443 printf ("#include \"regs.h\"\n");
444 printf ("#include \"output.h\"\n");
445 printf ("#include \"real.h\"\n");
446 printf ("#include \"recog.h\"\n");
447 printf ("#include \"except.h\"\n\n");
448 printf ("#include \"function.h\"\n\n");
450 printf ("extern rtx peep_operand[];\n\n");
451 printf ("#define operands peep_operand\n\n");
453 printf ("rtx\npeephole (ins1)\n rtx ins1;\n{\n");
454 printf (" rtx insn ATTRIBUTE_UNUSED, x ATTRIBUTE_UNUSED, pat ATTRIBUTE_UNUSED;\n\n");
456 /* Early out: no peepholes for insns followed by barriers. */
457 printf (" if (NEXT_INSN (ins1)\n");
458 printf (" && GET_CODE (NEXT_INSN (ins1)) == BARRIER)\n");
459 printf (" return 0;\n\n");
461 /* Read the machine description. */
465 c
= read_skip_spaces (infile
);
470 desc
= read_rtx (infile
);
471 if (GET_CODE (desc
) == DEFINE_PEEPHOLE
)
476 if (GET_CODE (desc
) == DEFINE_INSN
477 || GET_CODE (desc
) == DEFINE_EXPAND
478 || GET_CODE (desc
) == DEFINE_SPLIT
)
484 printf (" return 0;\n}\n\n");
489 printf ("rtx peep_operand[%d];\n", max_opno
+ 1);
492 exit (ferror (stdout
) != 0 ? FATAL_EXIT_CODE
: SUCCESS_EXIT_CODE
);