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 GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 #include "coretypes.h"
29 #include "gensupport.h"
32 /* While tree-walking an instruction pattern, we keep a chain
33 of these `struct link's to record how to get down to the
34 current position. In each one, POS is the operand number,
35 and if the operand is a vector VEC is the element number.
36 VEC is -1 if the operand is not a vector. */
47 /* Number of operands used in current peephole definition. */
49 static int n_operands
;
51 /* Peephole optimizations get insn codes just like insn patterns.
52 Count them so we know the code of the define_peephole we are handling. */
54 static int insn_code_number
= 0;
56 static void gen_peephole
PARAMS ((rtx
));
57 static void match_rtx
PARAMS ((rtx
, struct link
*, int));
58 static void print_path
PARAMS ((struct link
*));
59 static void print_code
PARAMS ((RTX_CODE
));
65 int ninsns
= XVECLEN (peep
, 0);
70 printf (" insn = ins1;\n");
72 printf (" want_jump = 0;\n");
75 for (i
= 0; i
< ninsns
; i
++)
79 printf (" do { insn = NEXT_INSN (insn);\n");
80 printf (" if (insn == 0) goto L%d; }\n",
82 printf (" while (GET_CODE (insn) == NOTE\n");
83 printf ("\t || (GET_CODE (insn) == INSN\n");
84 printf ("\t && (GET_CODE (PATTERN (insn)) == USE\n");
85 printf ("\t\t || GET_CODE (PATTERN (insn)) == CLOBBER)));\n");
87 printf (" if (GET_CODE (insn) == CODE_LABEL\n\
88 || GET_CODE (insn) == BARRIER)\n goto L%d;\n",
93 printf (" if (GET_CODE (insn) == JUMP_INSN)\n");
94 printf (" want_jump = JUMP_LABEL (insn);\n");
97 printf (" pat = PATTERN (insn);\n");
99 /* Walk the insn's pattern, remembering at all times the path
100 down to the walking point. */
102 match_rtx (XVECEXP (peep
, 0, i
), NULL
, insn_code_number
);
105 /* We get this far if the pattern matches.
106 Now test the extra condition. */
108 if (XSTR (peep
, 1) && XSTR (peep
, 1)[0])
109 printf (" if (! (%s)) goto L%d;\n",
110 XSTR (peep
, 1), insn_code_number
);
112 /* If that matches, construct new pattern and put it in the first insn.
113 This new pattern will never be matched.
114 It exists only so that insn-extract can get the operands back.
115 So use a simple regular form: a PARALLEL containing a vector
116 of all the operands. */
118 printf (" PATTERN (ins1) = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (%d, operands));\n", n_operands
);
121 printf (" if (want_jump && GET_CODE (ins1) != JUMP_INSN)\n");
123 printf (" rtx insn2 = emit_jump_insn_before (PATTERN (ins1), ins1);\n");
124 printf (" delete_related_insns (ins1);\n");
125 printf (" ins1 = ins2;\n");
129 /* Record this define_peephole's insn code in the insn,
130 as if it had been recognized to match this. */
131 printf (" INSN_CODE (ins1) = %d;\n",
134 /* Delete the remaining insns. */
136 printf (" delete_for_peephole (NEXT_INSN (ins1), insn);\n");
138 /* See reload1.c for insertion of NOTE which guarantees that this
140 printf (" return NEXT_INSN (insn);\n");
142 printf (" L%d:\n\n", insn_code_number
);
146 match_rtx (x
, path
, fail_label
)
166 if (XINT (x
, 0) > max_opno
)
167 max_opno
= XINT (x
, 0);
168 if (XINT (x
, 0) >= n_operands
)
169 n_operands
= 1 + XINT (x
, 0);
175 printf (" operands[%d] = x;\n", XINT (x
, 0));
176 if (XSTR (x
, 1) && XSTR (x
, 1)[0])
177 printf (" if (! %s (x, %smode)) goto L%d;\n",
178 XSTR (x
, 1), GET_MODE_NAME (GET_MODE (x
)), fail_label
);
187 printf (" if (!rtx_equal_p (operands[%d], x)) goto L%d;\n",
188 XINT (x
, 0), fail_label
);
196 printf (" if (GET_CODE (operands[%d]) != GET_CODE (x)\n", XINT (x
, 0));
197 printf (" || GET_MODE (operands[%d]) != GET_MODE (x)) goto L%d;\n",
198 XINT (x
, 0), fail_label
);
199 printf (" operands[%d] = x;\n", XINT (x
, 0));
202 for (i
= 0; i
< XVECLEN (x
, 1); i
++)
205 match_rtx (XVECEXP (x
, 1, i
), &link
, fail_label
);
210 if (XINT (x
, 0) > max_opno
)
211 max_opno
= XINT (x
, 0);
212 if (XINT (x
, 0) >= n_operands
)
213 n_operands
= 1 + XINT (x
, 0);
219 printf (" operands[%d] = x;\n", XINT (x
, 0));
220 if (XSTR (x
, 1) && XSTR (x
, 1)[0])
221 printf (" if (! %s (x, %smode)) goto L%d;\n",
222 XSTR (x
, 1), GET_MODE_NAME (GET_MODE (x
)), fail_label
);
225 for (i
= 0; i
< XVECLEN (x
, 2); i
++)
228 match_rtx (XVECEXP (x
, 2, i
), &link
, fail_label
);
233 if (XINT (x
, 0) > max_opno
)
234 max_opno
= XINT (x
, 0);
235 if (XINT (x
, 0) >= n_operands
)
236 n_operands
= 1 + XINT (x
, 0);
242 printf (" if (GET_CODE (x) != PARALLEL) goto L%d;\n", fail_label
);
243 printf (" operands[%d] = x;\n", XINT (x
, 0));
244 if (XSTR (x
, 1) && XSTR (x
, 1)[0])
245 printf (" if (! %s (x, %smode)) goto L%d;\n",
246 XSTR (x
, 1), GET_MODE_NAME (GET_MODE (x
)), fail_label
);
249 for (i
= 0; i
< XVECLEN (x
, 2); i
++)
252 match_rtx (XVECEXP (x
, 2, i
), &link
, fail_label
);
257 match_rtx (XEXP (x
, 0), path
, fail_label
);
268 printf (" if (GET_CODE (x) != ");
270 printf (") goto L%d;\n", fail_label
);
272 if (GET_MODE (x
) != VOIDmode
)
274 printf (" if (GET_MODE (x) != %smode) goto L%d;\n",
275 GET_MODE_NAME (GET_MODE (x
)), fail_label
);
280 fmt
= GET_RTX_FORMAT (code
);
281 len
= GET_RTX_LENGTH (code
);
282 for (i
= 0; i
< len
; i
++)
285 if (fmt
[i
] == 'e' || fmt
[i
] == 'u')
286 match_rtx (XEXP (x
, i
), &link
, fail_label
);
287 else if (fmt
[i
] == 'E')
290 printf (" if (XVECLEN (x, %d) != %d) goto L%d;\n",
291 i
, XVECLEN (x
, i
), fail_label
);
292 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
295 match_rtx (XVECEXP (x
, i
, j
), &link
, fail_label
);
298 else if (fmt
[i
] == 'i')
300 /* Make sure that at run time `x' is the RTX we want to test. */
308 printf (" if (XINT (x, %d) != %d) goto L%d;\n",
309 i
, XINT (x
, i
), fail_label
);
311 else if (fmt
[i
] == 'w')
313 /* Make sure that at run time `x' is the RTX we want to test. */
321 printf (" if (XWINT (x, %d) != ", i
);
322 printf (HOST_WIDE_INT_PRINT_DEC
, XWINT (x
, i
));
323 printf (") goto L%d;\n", fail_label
);
325 else if (fmt
[i
] == 's')
327 /* Make sure that at run time `x' is the RTX we want to test. */
335 printf (" if (strcmp (XSTR (x, %d), \"%s\")) goto L%d;\n",
336 i
, XSTR (x
, i
), fail_label
);
341 /* Given a PATH, representing a path down the instruction's
342 pattern from the root to a certain point, output code to
343 evaluate to the rtx at that point. */
351 else if (path
->vecelt
>= 0)
353 printf ("XVECEXP (");
354 print_path (path
->next
);
355 printf (", %d, %d)", path
->pos
, path
->vecelt
);
360 print_path (path
->next
);
361 printf (", %d)", path
->pos
);
370 for (p1
= GET_RTX_NAME (code
); *p1
; p1
++)
371 putchar (TOUPPER(*p1
));
374 extern int main
PARAMS ((int, char **));
385 progname
= "genpeep";
388 fatal ("no input file name");
390 if (init_md_reader_args (argc
, argv
) != SUCCESS_EXIT_CODE
)
391 return (FATAL_EXIT_CODE
);
393 printf ("/* Generated automatically by the program `genpeep'\n\
394 from the machine description file `md'. */\n\n");
396 printf ("#include \"config.h\"\n");
397 printf ("#include \"system.h\"\n");
398 printf ("#include \"coretypes.h\"\n");
399 printf ("#include \"tm.h\"\n");
400 printf ("#include \"insn-config.h\"\n");
401 printf ("#include \"rtl.h\"\n");
402 printf ("#include \"tm_p.h\"\n");
403 printf ("#include \"regs.h\"\n");
404 printf ("#include \"output.h\"\n");
405 printf ("#include \"real.h\"\n");
406 printf ("#include \"recog.h\"\n");
407 printf ("#include \"except.h\"\n\n");
408 printf ("#include \"function.h\"\n\n");
410 printf ("#ifdef HAVE_peephole\n");
411 printf ("extern rtx peep_operand[];\n\n");
412 printf ("#define operands peep_operand\n\n");
414 printf ("rtx\npeephole (ins1)\n rtx ins1;\n{\n");
415 printf (" rtx insn ATTRIBUTE_UNUSED, x ATTRIBUTE_UNUSED, pat ATTRIBUTE_UNUSED;\n\n");
417 /* Early out: no peepholes for insns followed by barriers. */
418 printf (" if (NEXT_INSN (ins1)\n");
419 printf (" && GET_CODE (NEXT_INSN (ins1)) == BARRIER)\n");
420 printf (" return 0;\n\n");
422 /* Read the machine description. */
426 int line_no
, rtx_number
= 0;
428 desc
= read_md_rtx (&line_no
, &rtx_number
);
432 if (GET_CODE (desc
) == DEFINE_PEEPHOLE
)
437 if (GET_CODE (desc
) == DEFINE_INSN
438 || GET_CODE (desc
) == DEFINE_EXPAND
439 || GET_CODE (desc
) == DEFINE_SPLIT
440 || GET_CODE (desc
) == DEFINE_PEEPHOLE2
)
446 printf (" return 0;\n}\n\n");
451 printf ("rtx peep_operand[%d];\n", max_opno
+ 1);
455 return (ferror (stdout
) != 0 ? FATAL_EXIT_CODE
: SUCCESS_EXIT_CODE
);
458 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
461 int code ATTRIBUTE_UNUSED
;