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 GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
27 #include "gensupport.h"
30 /* While tree-walking an instruction pattern, we keep a chain
31 of these `struct link's to record how to get down to the
32 current position. In each one, POS is the operand number,
33 and if the operand is a vector VEC is the element number.
34 VEC is -1 if the operand is not a vector. */
45 /* Number of operands used in current peephole definition. */
47 static int n_operands
;
49 /* Peephole optimizations get insn codes just like insn patterns.
50 Count them so we know the code of the define_peephole we are handling. */
52 static int insn_code_number
= 0;
54 static void gen_peephole
PARAMS ((rtx
));
55 static void match_rtx
PARAMS ((rtx
, struct link
*, int));
56 static void print_path
PARAMS ((struct link
*));
57 static void print_code
PARAMS ((RTX_CODE
));
63 int ninsns
= XVECLEN (peep
, 0);
68 printf (" insn = ins1;\n");
70 printf (" want_jump = 0;\n");
73 for (i
= 0; i
< ninsns
; i
++)
77 printf (" do { insn = NEXT_INSN (insn);\n");
78 printf (" if (insn == 0) goto L%d; }\n",
80 printf (" while (GET_CODE (insn) == NOTE\n");
81 printf ("\t || (GET_CODE (insn) == INSN\n");
82 printf ("\t && (GET_CODE (PATTERN (insn)) == USE\n");
83 printf ("\t\t || GET_CODE (PATTERN (insn)) == CLOBBER)));\n");
85 printf (" if (GET_CODE (insn) == CODE_LABEL\n\
86 || GET_CODE (insn) == BARRIER)\n goto L%d;\n",
91 printf (" if (GET_CODE (insn) == JUMP_INSN)\n");
92 printf (" want_jump = JUMP_LABEL (insn);\n");
95 printf (" pat = PATTERN (insn);\n");
97 /* Walk the insn's pattern, remembering at all times the path
98 down to the walking point. */
100 match_rtx (XVECEXP (peep
, 0, i
), NULL
, insn_code_number
);
103 /* We get this far if the pattern matches.
104 Now test the extra condition. */
106 if (XSTR (peep
, 1) && XSTR (peep
, 1)[0])
107 printf (" if (! (%s)) goto L%d;\n",
108 XSTR (peep
, 1), insn_code_number
);
110 /* If that matches, construct new pattern and put it in the first insn.
111 This new pattern will never be matched.
112 It exists only so that insn-extract can get the operands back.
113 So use a simple regular form: a PARALLEL containing a vector
114 of all the operands. */
116 printf (" PATTERN (ins1) = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (%d, operands));\n", n_operands
);
119 printf (" if (want_jump && GET_CODE (ins1) != JUMP_INSN)\n");
121 printf (" rtx insn2 = emit_jump_insn_before (PATTERN (ins1), ins1);\n");
122 printf (" delete_insn (ins1);\n");
123 printf (" ins1 = ins2;\n");
127 /* Record this define_peephole's insn code in the insn,
128 as if it had been recognized to match this. */
129 printf (" INSN_CODE (ins1) = %d;\n",
132 /* Delete the remaining insns. */
134 printf (" delete_for_peephole (NEXT_INSN (ins1), insn);\n");
136 /* See reload1.c for insertion of NOTE which guarantees that this
138 printf (" return NEXT_INSN (insn);\n");
140 printf (" L%d:\n\n", insn_code_number
);
144 match_rtx (x
, path
, fail_label
)
149 register RTX_CODE code
;
152 register const char *fmt
;
164 if (XINT (x
, 0) > max_opno
)
165 max_opno
= XINT (x
, 0);
166 if (XINT (x
, 0) >= n_operands
)
167 n_operands
= 1 + XINT (x
, 0);
173 printf (" operands[%d] = x;\n", XINT (x
, 0));
174 if (XSTR (x
, 1) && XSTR (x
, 1)[0])
175 printf (" if (! %s (x, %smode)) goto L%d;\n",
176 XSTR (x
, 1), GET_MODE_NAME (GET_MODE (x
)), fail_label
);
185 printf (" if (!rtx_equal_p (operands[%d], x)) goto L%d;\n",
186 XINT (x
, 0), fail_label
);
194 printf (" if (GET_CODE (operands[%d]) != GET_CODE (x)\n", XINT (x
, 0));
195 printf (" || GET_MODE (operands[%d]) != GET_MODE (x)) goto L%d;\n",
196 XINT (x
, 0), fail_label
);
197 printf (" operands[%d] = x;\n", XINT (x
, 0));
200 for (i
= 0; i
< XVECLEN (x
, 1); i
++)
203 match_rtx (XVECEXP (x
, 1, i
), &link
, fail_label
);
208 if (XINT (x
, 0) > max_opno
)
209 max_opno
= XINT (x
, 0);
210 if (XINT (x
, 0) >= n_operands
)
211 n_operands
= 1 + XINT (x
, 0);
217 printf (" operands[%d] = x;\n", XINT (x
, 0));
218 if (XSTR (x
, 1) && XSTR (x
, 1)[0])
219 printf (" if (! %s (x, %smode)) goto L%d;\n",
220 XSTR (x
, 1), GET_MODE_NAME (GET_MODE (x
)), fail_label
);
223 for (i
= 0; i
< XVECLEN (x
, 2); i
++)
226 match_rtx (XVECEXP (x
, 2, i
), &link
, fail_label
);
231 if (XINT (x
, 0) > max_opno
)
232 max_opno
= XINT (x
, 0);
233 if (XINT (x
, 0) >= n_operands
)
234 n_operands
= 1 + XINT (x
, 0);
240 printf (" if (GET_CODE (x) != PARALLEL) goto L%d;\n", fail_label
);
241 printf (" operands[%d] = x;\n", XINT (x
, 0));
242 if (XSTR (x
, 1) && XSTR (x
, 1)[0])
243 printf (" if (! %s (x, %smode)) goto L%d;\n",
244 XSTR (x
, 1), GET_MODE_NAME (GET_MODE (x
)), fail_label
);
247 for (i
= 0; i
< XVECLEN (x
, 2); i
++)
250 match_rtx (XVECEXP (x
, 2, i
), &link
, fail_label
);
255 match_rtx (XEXP (x
, 0), path
, fail_label
);
266 printf (" if (GET_CODE (x) != ");
268 printf (") goto L%d;\n", fail_label
);
270 if (GET_MODE (x
) != VOIDmode
)
272 printf (" if (GET_MODE (x) != %smode) goto L%d;\n",
273 GET_MODE_NAME (GET_MODE (x
)), fail_label
);
278 fmt
= GET_RTX_FORMAT (code
);
279 len
= GET_RTX_LENGTH (code
);
280 for (i
= 0; i
< len
; i
++)
283 if (fmt
[i
] == 'e' || fmt
[i
] == 'u')
284 match_rtx (XEXP (x
, i
), &link
, fail_label
);
285 else if (fmt
[i
] == 'E')
288 printf (" if (XVECLEN (x, %d) != %d) goto L%d;\n",
289 i
, XVECLEN (x
, i
), fail_label
);
290 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
293 match_rtx (XVECEXP (x
, i
, j
), &link
, fail_label
);
296 else if (fmt
[i
] == 'i')
298 /* Make sure that at run time `x' is the RTX we want to test. */
306 printf (" if (XINT (x, %d) != %d) goto L%d;\n",
307 i
, XINT (x
, i
), fail_label
);
309 else if (fmt
[i
] == 'w')
311 /* Make sure that at run time `x' is the RTX we want to test. */
319 printf (" if (XWINT (x, %d) != ", i
);
320 printf (HOST_WIDE_INT_PRINT_DEC
, XWINT (x
, i
));
321 printf (") goto L%d;\n", fail_label
);
323 else if (fmt
[i
] == 's')
325 /* Make sure that at run time `x' is the RTX we want to test. */
333 printf (" if (strcmp (XSTR (x, %d), \"%s\")) goto L%d;\n",
334 i
, XSTR (x
, i
), fail_label
);
339 /* Given a PATH, representing a path down the instruction's
340 pattern from the root to a certain point, output code to
341 evaluate to the rtx at that point. */
349 else if (path
->vecelt
>= 0)
351 printf ("XVECEXP (");
352 print_path (path
->next
);
353 printf (", %d, %d)", path
->pos
, path
->vecelt
);
358 print_path (path
->next
);
359 printf (", %d)", path
->pos
);
367 register const char *p1
;
368 for (p1
= GET_RTX_NAME (code
); *p1
; p1
++)
369 putchar (TOUPPER(*p1
));
372 extern int main
PARAMS ((int, char **));
383 progname
= "genpeep";
386 fatal ("No input file name.");
388 if (init_md_reader (argv
[1]) != SUCCESS_EXIT_CODE
)
389 return (FATAL_EXIT_CODE
);
391 printf ("/* Generated automatically by the program `genpeep'\n\
392 from the machine description file `md'. */\n\n");
394 printf ("#include \"config.h\"\n");
395 printf ("#include \"system.h\"\n");
396 printf ("#include \"insn-config.h\"\n");
397 printf ("#include \"rtl.h\"\n");
398 printf ("#include \"tm_p.h\"\n");
399 printf ("#include \"regs.h\"\n");
400 printf ("#include \"output.h\"\n");
401 printf ("#include \"real.h\"\n");
402 printf ("#include \"recog.h\"\n");
403 printf ("#include \"except.h\"\n\n");
404 printf ("#include \"function.h\"\n\n");
406 printf ("#ifdef HAVE_peephole\n");
407 printf ("extern rtx peep_operand[];\n\n");
408 printf ("#define operands peep_operand\n\n");
410 printf ("rtx\npeephole (ins1)\n rtx ins1;\n{\n");
411 printf (" rtx insn ATTRIBUTE_UNUSED, x ATTRIBUTE_UNUSED, pat ATTRIBUTE_UNUSED;\n\n");
413 /* Early out: no peepholes for insns followed by barriers. */
414 printf (" if (NEXT_INSN (ins1)\n");
415 printf (" && GET_CODE (NEXT_INSN (ins1)) == BARRIER)\n");
416 printf (" return 0;\n\n");
418 /* Read the machine description. */
422 int line_no
, rtx_number
= 0;
424 desc
= read_md_rtx (&line_no
, &rtx_number
);
428 if (GET_CODE (desc
) == DEFINE_PEEPHOLE
)
433 if (GET_CODE (desc
) == DEFINE_INSN
434 || GET_CODE (desc
) == DEFINE_EXPAND
435 || GET_CODE (desc
) == DEFINE_SPLIT
436 || GET_CODE (desc
) == DEFINE_PEEPHOLE2
)
442 printf (" return 0;\n}\n\n");
447 printf ("rtx peep_operand[%d];\n", max_opno
+ 1);
451 return (ferror (stdout
) != 0 ? FATAL_EXIT_CODE
: SUCCESS_EXIT_CODE
);
454 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
457 int code ATTRIBUTE_UNUSED
;