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. */
28 #include "gensupport.h"
30 static struct obstack obstack
;
31 struct obstack
*rtl_obstack
= &obstack
;
33 #define obstack_chunk_alloc xmalloc
34 #define obstack_chunk_free free
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. */
51 /* Number of operands used in current peephole definition. */
53 static int n_operands
;
55 /* Peephole optimizations get insn codes just like insn patterns.
56 Count them so we know the code of the define_peephole we are handling. */
58 static int insn_code_number
= 0;
60 static void gen_peephole
PARAMS ((rtx
));
61 static void match_rtx
PARAMS ((rtx
, struct link
*, int));
62 static void print_path
PARAMS ((struct link
*));
63 static void print_code
PARAMS ((RTX_CODE
));
69 int ninsns
= XVECLEN (peep
, 0);
74 printf (" insn = ins1;\n");
76 printf (" want_jump = 0;\n");
79 for (i
= 0; i
< ninsns
; i
++)
83 printf (" do { insn = NEXT_INSN (insn);\n");
84 printf (" if (insn == 0) goto L%d; }\n",
86 printf (" while (GET_CODE (insn) == NOTE\n");
87 printf ("\t || (GET_CODE (insn) == INSN\n");
88 printf ("\t && (GET_CODE (PATTERN (insn)) == USE\n");
89 printf ("\t\t || GET_CODE (PATTERN (insn)) == CLOBBER)));\n");
91 printf (" if (GET_CODE (insn) == CODE_LABEL\n\
92 || GET_CODE (insn) == BARRIER)\n goto L%d;\n",
97 printf (" if (GET_CODE (insn) == JUMP_INSN)\n");
98 printf (" want_jump = JUMP_LABEL (insn);\n");
101 printf (" pat = PATTERN (insn);\n");
103 /* Walk the insn's pattern, remembering at all times the path
104 down to the walking point. */
106 match_rtx (XVECEXP (peep
, 0, i
), NULL_PTR
, insn_code_number
);
109 /* We get this far if the pattern matches.
110 Now test the extra condition. */
112 if (XSTR (peep
, 1) && XSTR (peep
, 1)[0])
113 printf (" if (! (%s)) goto L%d;\n",
114 XSTR (peep
, 1), insn_code_number
);
116 /* If that matches, construct new pattern and put it in the first insn.
117 This new pattern will never be matched.
118 It exists only so that insn-extract can get the operands back.
119 So use a simple regular form: a PARALLEL containing a vector
120 of all the operands. */
122 printf (" PATTERN (ins1) = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (%d, operands));\n", n_operands
);
125 printf (" if (want_jump && GET_CODE (ins1) != JUMP_INSN)\n");
127 printf (" rtx insn2 = emit_jump_insn_before (PATTERN (ins1), ins1);\n");
128 printf (" delete_insn (ins1);\n");
129 printf (" ins1 = ins2;\n");
133 /* Record this define_peephole's insn code in the insn,
134 as if it had been recognized to match this. */
135 printf (" INSN_CODE (ins1) = %d;\n",
138 /* Delete the remaining insns. */
140 printf (" delete_for_peephole (NEXT_INSN (ins1), insn);\n");
142 /* See reload1.c for insertion of NOTE which guarantees that this
144 printf (" return NEXT_INSN (insn);\n");
146 printf (" L%d:\n\n", insn_code_number
);
150 match_rtx (x
, path
, fail_label
)
155 register RTX_CODE code
;
158 register const char *fmt
;
170 if (XINT (x
, 0) > max_opno
)
171 max_opno
= XINT (x
, 0);
172 if (XINT (x
, 0) >= n_operands
)
173 n_operands
= 1 + XINT (x
, 0);
179 printf (" operands[%d] = x;\n", XINT (x
, 0));
180 if (XSTR (x
, 1) && XSTR (x
, 1)[0])
181 printf (" if (! %s (x, %smode)) goto L%d;\n",
182 XSTR (x
, 1), GET_MODE_NAME (GET_MODE (x
)), fail_label
);
191 printf (" if (!rtx_equal_p (operands[%d], x)) goto L%d;\n",
192 XINT (x
, 0), fail_label
);
200 printf (" if (GET_CODE (operands[%d]) != GET_CODE (x)\n", XINT (x
, 0));
201 printf (" || GET_MODE (operands[%d]) != GET_MODE (x)) goto L%d;\n",
202 XINT (x
, 0), fail_label
);
203 printf (" operands[%d] = x;\n", XINT (x
, 0));
206 for (i
= 0; i
< XVECLEN (x
, 1); i
++)
209 match_rtx (XVECEXP (x
, 1, i
), &link
, fail_label
);
214 if (XINT (x
, 0) > max_opno
)
215 max_opno
= XINT (x
, 0);
216 if (XINT (x
, 0) >= n_operands
)
217 n_operands
= 1 + XINT (x
, 0);
223 printf (" operands[%d] = x;\n", XINT (x
, 0));
224 if (XSTR (x
, 1) && XSTR (x
, 1)[0])
225 printf (" if (! %s (x, %smode)) goto L%d;\n",
226 XSTR (x
, 1), GET_MODE_NAME (GET_MODE (x
)), fail_label
);
229 for (i
= 0; i
< XVECLEN (x
, 2); i
++)
232 match_rtx (XVECEXP (x
, 2, i
), &link
, fail_label
);
237 if (XINT (x
, 0) > max_opno
)
238 max_opno
= XINT (x
, 0);
239 if (XINT (x
, 0) >= n_operands
)
240 n_operands
= 1 + XINT (x
, 0);
246 printf (" if (GET_CODE (x) != PARALLEL) goto L%d;\n", fail_label
);
247 printf (" operands[%d] = x;\n", XINT (x
, 0));
248 if (XSTR (x
, 1) && XSTR (x
, 1)[0])
249 printf (" if (! %s (x, %smode)) goto L%d;\n",
250 XSTR (x
, 1), GET_MODE_NAME (GET_MODE (x
)), fail_label
);
253 for (i
= 0; i
< XVECLEN (x
, 2); i
++)
256 match_rtx (XVECEXP (x
, 2, i
), &link
, fail_label
);
261 match_rtx (XEXP (x
, 0), path
, fail_label
);
272 printf (" if (GET_CODE (x) != ");
274 printf (") goto L%d;\n", fail_label
);
276 if (GET_MODE (x
) != VOIDmode
)
278 printf (" if (GET_MODE (x) != %smode) goto L%d;\n",
279 GET_MODE_NAME (GET_MODE (x
)), fail_label
);
284 fmt
= GET_RTX_FORMAT (code
);
285 len
= GET_RTX_LENGTH (code
);
286 for (i
= 0; i
< len
; i
++)
289 if (fmt
[i
] == 'e' || fmt
[i
] == 'u')
290 match_rtx (XEXP (x
, i
), &link
, fail_label
);
291 else if (fmt
[i
] == 'E')
294 printf (" if (XVECLEN (x, %d) != %d) goto L%d;\n",
295 i
, XVECLEN (x
, i
), fail_label
);
296 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
299 match_rtx (XVECEXP (x
, i
, j
), &link
, fail_label
);
302 else if (fmt
[i
] == 'i')
304 /* Make sure that at run time `x' is the RTX we want to test. */
312 printf (" if (XINT (x, %d) != %d) goto L%d;\n",
313 i
, XINT (x
, i
), fail_label
);
315 else if (fmt
[i
] == 'w')
317 /* Make sure that at run time `x' is the RTX we want to test. */
325 printf (" if (XWINT (x, %d) != ", i
);
326 printf (HOST_WIDE_INT_PRINT_DEC
, XWINT (x
, i
));
327 printf (") goto L%d;\n", fail_label
);
329 else if (fmt
[i
] == 's')
331 /* Make sure that at run time `x' is the RTX we want to test. */
339 printf (" if (strcmp (XSTR (x, %d), \"%s\")) goto L%d;\n",
340 i
, XSTR (x
, i
), fail_label
);
345 /* Given a PATH, representing a path down the instruction's
346 pattern from the root to a certain point, output code to
347 evaluate to the rtx at that point. */
355 else if (path
->vecelt
>= 0)
357 printf ("XVECEXP (");
358 print_path (path
->next
);
359 printf (", %d, %d)", path
->pos
, path
->vecelt
);
364 print_path (path
->next
);
365 printf (", %d)", path
->pos
);
373 register const char *p1
;
374 for (p1
= GET_RTX_NAME (code
); *p1
; p1
++)
375 putchar (TOUPPER(*p1
));
382 register PTR val
= (PTR
) malloc (size
);
385 fatal ("virtual memory exhausted");
396 ptr
= (PTR
) realloc (old
, size
);
398 ptr
= (PTR
) malloc (size
);
400 fatal ("virtual memory exhausted");
404 extern int main
PARAMS ((int, char **));
415 progname
= "genpeep";
416 obstack_init (rtl_obstack
);
419 fatal ("No input file name.");
421 if (init_md_reader (argv
[1]) != SUCCESS_EXIT_CODE
)
422 return (FATAL_EXIT_CODE
);
424 printf ("/* Generated automatically by the program `genpeep'\n\
425 from the machine description file `md'. */\n\n");
427 printf ("#include \"config.h\"\n");
428 printf ("#include \"system.h\"\n");
429 printf ("#include \"insn-config.h\"\n");
430 printf ("#include \"rtl.h\"\n");
431 printf ("#include \"tm_p.h\"\n");
432 printf ("#include \"regs.h\"\n");
433 printf ("#include \"output.h\"\n");
434 printf ("#include \"real.h\"\n");
435 printf ("#include \"recog.h\"\n");
436 printf ("#include \"except.h\"\n\n");
437 printf ("#include \"function.h\"\n\n");
439 printf ("#ifdef HAVE_peephole\n");
440 printf ("extern rtx peep_operand[];\n\n");
441 printf ("#define operands peep_operand\n\n");
443 printf ("rtx\npeephole (ins1)\n rtx ins1;\n{\n");
444 printf (" rtx insn ATTRIBUTE_UNUSED, x ATTRIBUTE_UNUSED, pat ATTRIBUTE_UNUSED;\n\n");
446 /* Early out: no peepholes for insns followed by barriers. */
447 printf (" if (NEXT_INSN (ins1)\n");
448 printf (" && GET_CODE (NEXT_INSN (ins1)) == BARRIER)\n");
449 printf (" return 0;\n\n");
451 /* Read the machine description. */
455 int line_no
, rtx_number
= 0;
457 desc
= read_md_rtx (&line_no
, &rtx_number
);
461 if (GET_CODE (desc
) == DEFINE_PEEPHOLE
)
466 if (GET_CODE (desc
) == DEFINE_INSN
467 || GET_CODE (desc
) == DEFINE_EXPAND
468 || GET_CODE (desc
) == DEFINE_SPLIT
)
474 printf (" return 0;\n}\n\n");
479 printf ("rtx peep_operand[%d];\n", max_opno
+ 1);
483 return (ferror (stdout
) != 0 ? FATAL_EXIT_CODE
: SUCCESS_EXIT_CODE
);
486 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
489 int code ATTRIBUTE_UNUSED
;