]>
gcc.gnu.org Git - gcc.git/blob - gcc/genpeep.c
1 /* Generate code from machine description to perform peephole optimizations.
2 Copyright (C) 1987, 1989, 1992 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
26 static struct obstack obstack
;
27 struct obstack
*rtl_obstack
= &obstack
;
29 #define obstack_chunk_alloc xmalloc
30 #define obstack_chunk_free free
33 extern rtx
read_rtx ();
35 /* While tree-walking an instruction pattern, we keep a chain
36 of these `struct link's to record how to get down to the
37 current position. In each one, POS is the operand number,
38 and if the operand is a vector VEC is the element number.
39 VEC is -1 if the operand is not a vector. */
49 static void match_rtx ();
50 static void gen_exp ();
56 /* Number of operands used in current peephole definition. */
58 static int n_operands
;
60 /* Peephole optimizations get insn codes just like insn patterns.
61 Count them so we know the code of the define_peephole we are handling. */
63 static int insn_code_number
= 0;
65 static void print_path ();
66 static void print_code ();
72 int ninsns
= XVECLEN (peep
, 0);
77 printf (" insn = ins1;\n");
79 printf (" want_jump = 0;\n");
82 for (i
= 0; i
< ninsns
; i
++)
86 printf (" do { insn = NEXT_INSN (insn);\n");
87 printf (" if (insn == 0) goto L%d; }\n",
89 printf (" while (GET_CODE (insn) == NOTE\n");
90 printf ("\t || (GET_CODE (insn) == INSN\n");
91 printf ("\t && (GET_CODE (PATTERN (insn)) == USE\n");
92 printf ("\t\t || GET_CODE (PATTERN (insn)) == CLOBBER)));\n");
94 printf (" if (GET_CODE (insn) == CODE_LABEL\n\
95 || GET_CODE (insn) == BARRIER)\n goto L%d;\n",
100 printf (" if (GET_CODE (insn) == JUMP_INSN)\n");
101 printf (" want_jump = JUMP_LABEL (insn);\n");
104 printf (" pat = PATTERN (insn);\n");
106 /* Walk the insn's pattern, remembering at all times the path
107 down to the walking point. */
109 match_rtx (XVECEXP (peep
, 0, i
), 0, insn_code_number
);
112 /* We get this far if the pattern matches.
113 Now test the extra condition. */
115 if (XSTR (peep
, 1) && XSTR (peep
, 1)[0])
116 printf (" if (! (%s)) goto L%d;\n",
117 XSTR (peep
, 1), insn_code_number
);
119 /* If that matches, construct new pattern and put it in the first insn.
120 This new pattern will never be matched.
121 It exists only so that insn-extract can get the operands back.
122 So use a simple regular form: a PARALLEL containing a vector
123 of all the operands. */
125 printf (" PATTERN (ins1) = gen_rtx (PARALLEL, VOIDmode, gen_rtvec_v (%d, operands));\n", n_operands
);
128 printf (" if (want_jump && GET_CODE (ins1) != JUMP_INSN)\n");
130 printf (" rtx insn2 = emit_jump_insn_before (PATTERN (ins1), ins1);\n");
131 printf (" delete_insn (ins1);\n");
132 printf (" ins1 = ins2;\n");
136 /* Record this define_peephole's insn code in the insn,
137 as if it had been recognized to match this. */
138 printf (" INSN_CODE (ins1) = %d;\n",
141 /* Delete the remaining insns. */
143 printf (" delete_for_peephole (NEXT_INSN (ins1), insn);\n");
145 /* See reload1.c for insertion of NOTE which guarantees that this
147 printf (" return NEXT_INSN (insn);\n");
149 printf (" L%d:\n\n", insn_code_number
);
153 match_rtx (x
, path
, fail_label
)
158 register RTX_CODE code
;
173 if (XINT (x
, 0) > max_opno
)
174 max_opno
= XINT (x
, 0);
175 if (XINT (x
, 0) >= n_operands
)
176 n_operands
= 1 + XINT (x
, 0);
182 printf (" operands[%d] = x;\n", XINT (x
, 0));
183 if (XSTR (x
, 1) && XSTR (x
, 1)[0])
184 printf (" if (! %s (x, %smode)) goto L%d;\n",
185 XSTR (x
, 1), GET_MODE_NAME (GET_MODE (x
)), fail_label
);
193 printf (" if (!rtx_equal_p (operands[%d], x)) goto L%d;\n",
194 XINT (x
, 0), fail_label
);
202 printf (" if (GET_CODE (operands[%d]) != GET_CODE (x)\n", XINT (x
, 0));
203 printf (" || GET_MODE (operands[%d]) != GET_MODE (x)) goto L%d;\n",
204 XINT (x
, 0), fail_label
);
205 printf (" operands[%d] = x;\n", XINT (x
, 0));
208 for (i
= 0; i
< XVECLEN (x
, 1); i
++)
211 match_rtx (XVECEXP (x
, 1, i
), &link
, fail_label
);
216 if (XINT (x
, 0) > max_opno
)
217 max_opno
= XINT (x
, 0);
218 if (XINT (x
, 0) >= n_operands
)
219 n_operands
= 1 + XINT (x
, 0);
225 printf (" operands[%d] = x;\n", XINT (x
, 0));
226 if (XSTR (x
, 1) && XSTR (x
, 1)[0])
227 printf (" if (! %s (x, %smode)) goto L%d;\n",
228 XSTR (x
, 1), GET_MODE_NAME (GET_MODE (x
)), fail_label
);
231 for (i
= 0; i
< XVECLEN (x
, 2); i
++)
234 match_rtx (XVECEXP (x
, 2, i
), &link
, fail_label
);
239 if (XINT (x
, 0) > max_opno
)
240 max_opno
= XINT (x
, 0);
241 if (XINT (x
, 0) >= n_operands
)
242 n_operands
= 1 + XINT (x
, 0);
248 printf (" if (GET_CODE (x) != PARALLEL) goto L%d;\n", fail_label
);
249 printf (" operands[%d] = x;\n", XINT (x
, 0));
250 if (XSTR (x
, 1) && XSTR (x
, 1)[0])
251 printf (" if (! %s (x, %smode)) goto L%d;\n",
252 XSTR (x
, 1), GET_MODE_NAME (GET_MODE (x
)), fail_label
);
255 for (i
= 0; i
< XVECLEN (x
, 2); i
++)
258 match_rtx (XVECEXP (x
, 2, i
), &link
, fail_label
);
263 match_rtx (XEXP (x
, 0), path
, fail_label
);
271 printf (" if (GET_CODE (x) != ");
273 printf (") goto L%d;\n", fail_label
);
275 if (GET_MODE (x
) != VOIDmode
)
277 printf (" if (GET_MODE (x) != %smode) goto L%d;\n",
278 GET_MODE_NAME (GET_MODE (x
)), fail_label
);
283 fmt
= GET_RTX_FORMAT (code
);
284 len
= GET_RTX_LENGTH (code
);
285 for (i
= 0; i
< len
; i
++)
288 if (fmt
[i
] == 'e' || fmt
[i
] == 'u')
289 match_rtx (XEXP (x
, i
), &link
, fail_label
);
290 else if (fmt
[i
] == 'E')
293 printf (" if (XVECLEN (x, %d) != %d) goto L%d;\n",
294 i
, XVECLEN (x
, i
), fail_label
);
295 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
298 match_rtx (XVECEXP (x
, i
, j
), &link
, fail_label
);
301 else if (fmt
[i
] == 'i')
303 /* Make sure that at run time `x' is the RTX we want to test. */
311 printf (" if (XINT (x, %d) != %d) goto L%d;\n",
312 i
, XINT (x
, i
), fail_label
);
314 else if (fmt
[i
] == 's')
316 /* Make sure that at run time `x' is the RTX we want to test. */
324 printf (" if (strcmp (XSTR (x, %d), \"%s\")) goto L%d;\n",
325 i
, XSTR (x
, i
), fail_label
);
330 /* Given a PATH, representing a path down the instruction's
331 pattern from the root to a certain point, output code to
332 evaluate to the rtx at that point. */
340 else if (path
->vecelt
>= 0)
342 printf ("XVECEXP (");
343 print_path (path
->next
);
344 printf (", %d, %d)", path
->pos
, path
->vecelt
);
349 print_path (path
->next
);
350 printf (", %d)", path
->pos
);
359 for (p1
= GET_RTX_NAME (code
); *p1
; p1
++)
361 if (*p1
>= 'a' && *p1
<= 'z')
362 putchar (*p1
+ 'A' - 'a');
372 register char *val
= (char *) malloc (size
);
375 fatal ("virtual memory exhausted");
384 char *result
= (char *) realloc (ptr
, size
);
386 fatal ("virtual memory exhausted");
394 fprintf (stderr
, "genpeep: ");
395 fprintf (stderr
, s
, a1
, a2
);
396 fprintf (stderr
, "\n");
397 exit (FATAL_EXIT_CODE
);
400 /* More 'friendly' abort that prints the line and file.
401 config.h can #define abort fancy_abort if you like that sort of thing. */
406 fatal ("Internal gcc abort.");
420 obstack_init (rtl_obstack
);
423 fatal ("No input file name.");
425 infile
= fopen (argv
[1], "r");
429 exit (FATAL_EXIT_CODE
);
434 printf ("/* Generated automatically by the program `genpeep'\n\
435 from the machine description file `md'. */\n\n");
437 printf ("#include \"config.h\"\n");
438 printf ("#include \"rtl.h\"\n");
439 printf ("#include \"regs.h\"\n");
440 printf ("#include \"output.h\"\n");
441 printf ("#include \"real.h\"\n\n");
443 printf ("extern rtx peep_operand[];\n\n");
444 printf ("#define operands peep_operand\n\n");
446 printf ("rtx\npeephole (ins1)\n rtx ins1;\n{\n");
447 printf (" rtx insn, x, pat;\n");
448 printf (" int i;\n\n");
450 /* Early out: no peepholes for insns followed by barriers. */
451 printf (" if (NEXT_INSN (ins1)\n");
452 printf (" && GET_CODE (NEXT_INSN (ins1)) == BARRIER)\n");
453 printf (" return 0;\n\n");
455 /* Read the machine description. */
459 c
= read_skip_spaces (infile
);
464 desc
= read_rtx (infile
);
465 if (GET_CODE (desc
) == DEFINE_PEEPHOLE
)
470 if (GET_CODE (desc
) == DEFINE_INSN
471 || GET_CODE (desc
) == DEFINE_EXPAND
472 || GET_CODE (desc
) == DEFINE_SPLIT
)
478 printf (" return 0;\n}\n\n");
483 printf ("rtx peep_operand[%d];\n", max_opno
+ 1);
486 exit (ferror (stdout
) != 0 ? FATAL_EXIT_CODE
: SUCCESS_EXIT_CODE
);
This page took 0.051848 seconds and 5 git commands to generate.