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 /* While tree-walking an instruction pattern, we keep a chain
35 of these `struct link's to record how to get down to the
36 current position. In each one, POS is the operand number,
37 and if the operand is a vector VEC is the element number.
38 VEC is -1 if the operand is not a vector. */
49 /* Number of operands used in current peephole definition. */
51 static int n_operands
;
53 /* Peephole optimizations get insn codes just like insn patterns.
54 Count them so we know the code of the define_peephole we are handling. */
56 static int insn_code_number
= 0;
58 static void gen_peephole
PROTO((rtx
));
59 static void match_rtx
PROTO((rtx
, struct link
*, int));
60 static void print_path
PROTO((struct link
*));
61 static void print_code
PROTO((RTX_CODE
));
67 int ninsns
= XVECLEN (peep
, 0);
72 printf (" insn = ins1;\n");
74 printf (" want_jump = 0;\n");
77 for (i
= 0; i
< ninsns
; i
++)
81 printf (" do { insn = NEXT_INSN (insn);\n");
82 printf (" if (insn == 0) goto L%d; }\n",
84 printf (" while (GET_CODE (insn) == NOTE\n");
85 printf ("\t || (GET_CODE (insn) == INSN\n");
86 printf ("\t && (GET_CODE (PATTERN (insn)) == USE\n");
87 printf ("\t\t || GET_CODE (PATTERN (insn)) == CLOBBER)));\n");
89 printf (" if (GET_CODE (insn) == CODE_LABEL\n\
90 || GET_CODE (insn) == BARRIER)\n goto L%d;\n",
95 printf (" if (GET_CODE (insn) == JUMP_INSN)\n");
96 printf (" want_jump = JUMP_LABEL (insn);\n");
99 printf (" pat = PATTERN (insn);\n");
101 /* Walk the insn's pattern, remembering at all times the path
102 down to the walking point. */
104 match_rtx (XVECEXP (peep
, 0, i
), NULL_PTR
, insn_code_number
);
107 /* We get this far if the pattern matches.
108 Now test the extra condition. */
110 if (XSTR (peep
, 1) && XSTR (peep
, 1)[0])
111 printf (" if (! (%s)) goto L%d;\n",
112 XSTR (peep
, 1), insn_code_number
);
114 /* If that matches, construct new pattern and put it in the first insn.
115 This new pattern will never be matched.
116 It exists only so that insn-extract can get the operands back.
117 So use a simple regular form: a PARALLEL containing a vector
118 of all the operands. */
120 printf (" PATTERN (ins1) = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (%d, operands));\n", n_operands
);
123 printf (" if (want_jump && GET_CODE (ins1) != JUMP_INSN)\n");
125 printf (" rtx insn2 = emit_jump_insn_before (PATTERN (ins1), ins1);\n");
126 printf (" delete_insn (ins1);\n");
127 printf (" ins1 = ins2;\n");
131 /* Record this define_peephole's insn code in the insn,
132 as if it had been recognized to match this. */
133 printf (" INSN_CODE (ins1) = %d;\n",
136 /* Delete the remaining insns. */
138 printf (" delete_for_peephole (NEXT_INSN (ins1), insn);\n");
140 /* See reload1.c for insertion of NOTE which guarantees that this
142 printf (" return NEXT_INSN (insn);\n");
144 printf (" L%d:\n\n", insn_code_number
);
148 match_rtx (x
, path
, fail_label
)
153 register RTX_CODE code
;
156 register const char *fmt
;
168 if (XINT (x
, 0) > max_opno
)
169 max_opno
= XINT (x
, 0);
170 if (XINT (x
, 0) >= n_operands
)
171 n_operands
= 1 + XINT (x
, 0);
177 printf (" operands[%d] = x;\n", XINT (x
, 0));
178 if (XSTR (x
, 1) && XSTR (x
, 1)[0])
179 printf (" if (! %s (x, %smode)) goto L%d;\n",
180 XSTR (x
, 1), GET_MODE_NAME (GET_MODE (x
)), fail_label
);
189 printf (" if (!rtx_equal_p (operands[%d], x)) goto L%d;\n",
190 XINT (x
, 0), fail_label
);
198 printf (" if (GET_CODE (operands[%d]) != GET_CODE (x)\n", XINT (x
, 0));
199 printf (" || GET_MODE (operands[%d]) != GET_MODE (x)) goto L%d;\n",
200 XINT (x
, 0), fail_label
);
201 printf (" operands[%d] = x;\n", XINT (x
, 0));
204 for (i
= 0; i
< XVECLEN (x
, 1); i
++)
207 match_rtx (XVECEXP (x
, 1, i
), &link
, fail_label
);
212 if (XINT (x
, 0) > max_opno
)
213 max_opno
= XINT (x
, 0);
214 if (XINT (x
, 0) >= n_operands
)
215 n_operands
= 1 + XINT (x
, 0);
221 printf (" operands[%d] = x;\n", XINT (x
, 0));
222 if (XSTR (x
, 1) && XSTR (x
, 1)[0])
223 printf (" if (! %s (x, %smode)) goto L%d;\n",
224 XSTR (x
, 1), GET_MODE_NAME (GET_MODE (x
)), fail_label
);
227 for (i
= 0; i
< XVECLEN (x
, 2); i
++)
230 match_rtx (XVECEXP (x
, 2, i
), &link
, fail_label
);
235 if (XINT (x
, 0) > max_opno
)
236 max_opno
= XINT (x
, 0);
237 if (XINT (x
, 0) >= n_operands
)
238 n_operands
= 1 + XINT (x
, 0);
244 printf (" if (GET_CODE (x) != PARALLEL) goto L%d;\n", fail_label
);
245 printf (" operands[%d] = x;\n", XINT (x
, 0));
246 if (XSTR (x
, 1) && XSTR (x
, 1)[0])
247 printf (" if (! %s (x, %smode)) goto L%d;\n",
248 XSTR (x
, 1), GET_MODE_NAME (GET_MODE (x
)), fail_label
);
251 for (i
= 0; i
< XVECLEN (x
, 2); i
++)
254 match_rtx (XVECEXP (x
, 2, i
), &link
, fail_label
);
259 match_rtx (XEXP (x
, 0), path
, fail_label
);
270 printf (" if (GET_CODE (x) != ");
272 printf (") goto L%d;\n", fail_label
);
274 if (GET_MODE (x
) != VOIDmode
)
276 printf (" if (GET_MODE (x) != %smode) goto L%d;\n",
277 GET_MODE_NAME (GET_MODE (x
)), fail_label
);
282 fmt
= GET_RTX_FORMAT (code
);
283 len
= GET_RTX_LENGTH (code
);
284 for (i
= 0; i
< len
; i
++)
287 if (fmt
[i
] == 'e' || fmt
[i
] == 'u')
288 match_rtx (XEXP (x
, i
), &link
, fail_label
);
289 else if (fmt
[i
] == 'E')
292 printf (" if (XVECLEN (x, %d) != %d) goto L%d;\n",
293 i
, XVECLEN (x
, i
), fail_label
);
294 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
297 match_rtx (XVECEXP (x
, i
, j
), &link
, fail_label
);
300 else if (fmt
[i
] == 'i')
302 /* Make sure that at run time `x' is the RTX we want to test. */
310 printf (" if (XINT (x, %d) != %d) goto L%d;\n",
311 i
, XINT (x
, i
), fail_label
);
313 else if (fmt
[i
] == 'w')
315 /* Make sure that at run time `x' is the RTX we want to test. */
323 printf (" if (XWINT (x, %d) != ", i
);
324 printf (HOST_WIDE_INT_PRINT_DEC
, XWINT (x
, i
));
325 printf (") goto L%d;\n", fail_label
);
327 else if (fmt
[i
] == 's')
329 /* Make sure that at run time `x' is the RTX we want to test. */
337 printf (" if (strcmp (XSTR (x, %d), \"%s\")) goto L%d;\n",
338 i
, XSTR (x
, i
), fail_label
);
343 /* Given a PATH, representing a path down the instruction's
344 pattern from the root to a certain point, output code to
345 evaluate to the rtx at that point. */
353 else if (path
->vecelt
>= 0)
355 printf ("XVECEXP (");
356 print_path (path
->next
);
357 printf (", %d, %d)", path
->pos
, path
->vecelt
);
362 print_path (path
->next
);
363 printf (", %d)", path
->pos
);
371 register const char *p1
;
372 for (p1
= GET_RTX_NAME (code
); *p1
; p1
++)
375 putchar (toupper(*p1
));
385 register PTR val
= (PTR
) malloc (size
);
388 fatal ("virtual memory exhausted");
399 ptr
= (PTR
) realloc (old
, size
);
401 ptr
= (PTR
) malloc (size
);
403 fatal ("virtual memory exhausted");
407 extern int main
PROTO ((int, char **));
420 progname
= "genpeep";
421 obstack_init (rtl_obstack
);
424 fatal ("No input file name.");
426 infile
= fopen (argv
[1], "r");
430 return (FATAL_EXIT_CODE
);
433 printf ("/* Generated automatically by the program `genpeep'\n\
434 from the machine description file `md'. */\n\n");
436 printf ("#include \"config.h\"\n");
437 printf ("#include \"system.h\"\n");
438 printf ("#include \"insn-config.h\"\n");
439 printf ("#include \"rtl.h\"\n");
440 printf ("#include \"regs.h\"\n");
441 printf ("#include \"output.h\"\n");
442 printf ("#include \"real.h\"\n");
443 printf ("#include \"recog.h\"\n");
444 printf ("#include \"except.h\"\n\n");
445 printf ("#include \"function.h\"\n\n");
447 printf ("#ifdef HAVE_peephole\n");
448 printf ("extern rtx peep_operand[];\n\n");
449 printf ("#define operands peep_operand\n\n");
451 printf ("rtx\npeephole (ins1)\n rtx ins1;\n{\n");
452 printf (" rtx insn ATTRIBUTE_UNUSED, x ATTRIBUTE_UNUSED, pat ATTRIBUTE_UNUSED;\n\n");
454 /* Early out: no peepholes for insns followed by barriers. */
455 printf (" if (NEXT_INSN (ins1)\n");
456 printf (" && GET_CODE (NEXT_INSN (ins1)) == BARRIER)\n");
457 printf (" return 0;\n\n");
459 /* Read the machine description. */
463 c
= read_skip_spaces (infile
);
468 desc
= read_rtx (infile
);
469 if (GET_CODE (desc
) == DEFINE_PEEPHOLE
)
474 if (GET_CODE (desc
) == DEFINE_INSN
475 || GET_CODE (desc
) == DEFINE_EXPAND
476 || GET_CODE (desc
) == DEFINE_SPLIT
)
482 printf (" return 0;\n}\n\n");
487 printf ("rtx peep_operand[%d];\n", max_opno
+ 1);
491 return (ferror (stdout
) != 0 ? FATAL_EXIT_CODE
: SUCCESS_EXIT_CODE
);
494 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
497 int code ATTRIBUTE_UNUSED
;