]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/genpeep.c
1 /* Generate code from machine description to perform peephole optimizations.
2 Copyright (C) 1987, 1989, 1992, 1997 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. */
31 static struct obstack obstack
;
32 struct obstack
*rtl_obstack
= &obstack
;
34 #define obstack_chunk_alloc xmalloc
35 #define obstack_chunk_free free
38 extern rtx
read_rtx ();
40 /* While tree-walking an instruction pattern, we keep a chain
41 of these `struct link's to record how to get down to the
42 current position. In each one, POS is the operand number,
43 and if the operand is a vector VEC is the element number.
44 VEC is -1 if the operand is not a vector. */
54 static void match_rtx ();
60 /* Number of operands used in current peephole definition. */
62 static int n_operands
;
64 /* Peephole optimizations get insn codes just like insn patterns.
65 Count them so we know the code of the define_peephole we are handling. */
67 static int insn_code_number
= 0;
69 static void print_path ();
70 static void print_code ();
76 int ninsns
= XVECLEN (peep
, 0);
81 printf (" insn = ins1;\n");
83 printf (" want_jump = 0;\n");
86 for (i
= 0; i
< ninsns
; i
++)
90 printf (" do { insn = NEXT_INSN (insn);\n");
91 printf (" if (insn == 0) goto L%d; }\n",
93 printf (" while (GET_CODE (insn) == NOTE\n");
94 printf ("\t || (GET_CODE (insn) == INSN\n");
95 printf ("\t && (GET_CODE (PATTERN (insn)) == USE\n");
96 printf ("\t\t || GET_CODE (PATTERN (insn)) == CLOBBER)));\n");
98 printf (" if (GET_CODE (insn) == CODE_LABEL\n\
99 || GET_CODE (insn) == BARRIER)\n goto L%d;\n",
104 printf (" if (GET_CODE (insn) == JUMP_INSN)\n");
105 printf (" want_jump = JUMP_LABEL (insn);\n");
108 printf (" pat = PATTERN (insn);\n");
110 /* Walk the insn's pattern, remembering at all times the path
111 down to the walking point. */
113 match_rtx (XVECEXP (peep
, 0, i
), NULL_PTR
, insn_code_number
);
116 /* We get this far if the pattern matches.
117 Now test the extra condition. */
119 if (XSTR (peep
, 1) && XSTR (peep
, 1)[0])
120 printf (" if (! (%s)) goto L%d;\n",
121 XSTR (peep
, 1), insn_code_number
);
123 /* If that matches, construct new pattern and put it in the first insn.
124 This new pattern will never be matched.
125 It exists only so that insn-extract can get the operands back.
126 So use a simple regular form: a PARALLEL containing a vector
127 of all the operands. */
129 printf (" PATTERN (ins1) = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (%d, operands));\n", n_operands
);
132 printf (" if (want_jump && GET_CODE (ins1) != JUMP_INSN)\n");
134 printf (" rtx insn2 = emit_jump_insn_before (PATTERN (ins1), ins1);\n");
135 printf (" delete_insn (ins1);\n");
136 printf (" ins1 = ins2;\n");
140 /* Record this define_peephole's insn code in the insn,
141 as if it had been recognized to match this. */
142 printf (" INSN_CODE (ins1) = %d;\n",
145 /* Delete the remaining insns. */
147 printf (" delete_for_peephole (NEXT_INSN (ins1), insn);\n");
149 /* See reload1.c for insertion of NOTE which guarantees that this
151 printf (" return NEXT_INSN (insn);\n");
153 printf (" L%d:\n\n", insn_code_number
);
157 match_rtx (x
, path
, fail_label
)
162 register RTX_CODE code
;
177 if (XINT (x
, 0) > max_opno
)
178 max_opno
= XINT (x
, 0);
179 if (XINT (x
, 0) >= n_operands
)
180 n_operands
= 1 + XINT (x
, 0);
186 printf (" operands[%d] = x;\n", XINT (x
, 0));
187 if (XSTR (x
, 1) && XSTR (x
, 1)[0])
188 printf (" if (! %s (x, %smode)) goto L%d;\n",
189 XSTR (x
, 1), GET_MODE_NAME (GET_MODE (x
)), fail_label
);
198 printf (" if (!rtx_equal_p (operands[%d], x)) goto L%d;\n",
199 XINT (x
, 0), fail_label
);
207 printf (" if (GET_CODE (operands[%d]) != GET_CODE (x)\n", XINT (x
, 0));
208 printf (" || GET_MODE (operands[%d]) != GET_MODE (x)) goto L%d;\n",
209 XINT (x
, 0), fail_label
);
210 printf (" operands[%d] = x;\n", XINT (x
, 0));
213 for (i
= 0; i
< XVECLEN (x
, 1); i
++)
216 match_rtx (XVECEXP (x
, 1, i
), &link
, fail_label
);
221 if (XINT (x
, 0) > max_opno
)
222 max_opno
= XINT (x
, 0);
223 if (XINT (x
, 0) >= n_operands
)
224 n_operands
= 1 + XINT (x
, 0);
230 printf (" operands[%d] = x;\n", XINT (x
, 0));
231 if (XSTR (x
, 1) && XSTR (x
, 1)[0])
232 printf (" if (! %s (x, %smode)) goto L%d;\n",
233 XSTR (x
, 1), GET_MODE_NAME (GET_MODE (x
)), fail_label
);
236 for (i
= 0; i
< XVECLEN (x
, 2); i
++)
239 match_rtx (XVECEXP (x
, 2, i
), &link
, fail_label
);
244 if (XINT (x
, 0) > max_opno
)
245 max_opno
= XINT (x
, 0);
246 if (XINT (x
, 0) >= n_operands
)
247 n_operands
= 1 + XINT (x
, 0);
253 printf (" if (GET_CODE (x) != PARALLEL) goto L%d;\n", fail_label
);
254 printf (" operands[%d] = x;\n", XINT (x
, 0));
255 if (XSTR (x
, 1) && XSTR (x
, 1)[0])
256 printf (" if (! %s (x, %smode)) goto L%d;\n",
257 XSTR (x
, 1), GET_MODE_NAME (GET_MODE (x
)), fail_label
);
260 for (i
= 0; i
< XVECLEN (x
, 2); i
++)
263 match_rtx (XVECEXP (x
, 2, i
), &link
, fail_label
);
268 match_rtx (XEXP (x
, 0), path
, fail_label
);
279 printf (" if (GET_CODE (x) != ");
281 printf (") goto L%d;\n", fail_label
);
283 if (GET_MODE (x
) != VOIDmode
)
285 printf (" if (GET_MODE (x) != %smode) goto L%d;\n",
286 GET_MODE_NAME (GET_MODE (x
)), fail_label
);
291 fmt
= GET_RTX_FORMAT (code
);
292 len
= GET_RTX_LENGTH (code
);
293 for (i
= 0; i
< len
; i
++)
296 if (fmt
[i
] == 'e' || fmt
[i
] == 'u')
297 match_rtx (XEXP (x
, i
), &link
, fail_label
);
298 else if (fmt
[i
] == 'E')
301 printf (" if (XVECLEN (x, %d) != %d) goto L%d;\n",
302 i
, XVECLEN (x
, i
), fail_label
);
303 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
306 match_rtx (XVECEXP (x
, i
, j
), &link
, fail_label
);
309 else if (fmt
[i
] == 'i')
311 /* Make sure that at run time `x' is the RTX we want to test. */
319 printf (" if (XINT (x, %d) != %d) goto L%d;\n",
320 i
, XINT (x
, i
), fail_label
);
322 else if (fmt
[i
] == 'w')
324 /* Make sure that at run time `x' is the RTX we want to test. */
332 printf (" if (XWINT (x, %d) != ", i
);
333 printf (HOST_WIDE_INT_PRINT_DEC
, XWINT (x
, i
));
334 printf (") goto L%d;\n", fail_label
);
336 else if (fmt
[i
] == 's')
338 /* Make sure that at run time `x' is the RTX we want to test. */
346 printf (" if (strcmp (XSTR (x, %d), \"%s\")) goto L%d;\n",
347 i
, XSTR (x
, i
), fail_label
);
352 /* Given a PATH, representing a path down the instruction's
353 pattern from the root to a certain point, output code to
354 evaluate to the rtx at that point. */
362 else if (path
->vecelt
>= 0)
364 printf ("XVECEXP (");
365 print_path (path
->next
);
366 printf (", %d, %d)", path
->pos
, path
->vecelt
);
371 print_path (path
->next
);
372 printf (", %d)", path
->pos
);
381 for (p1
= GET_RTX_NAME (code
); *p1
; p1
++)
383 if (*p1
>= 'a' && *p1
<= 'z')
384 putchar (*p1
+ 'A' - 'a');
394 register char *val
= (char *) malloc (size
);
397 fatal ("virtual memory exhausted");
406 char *result
= (char *) realloc (ptr
, size
);
408 fatal ("virtual memory exhausted");
416 fprintf (stderr
, "genpeep: ");
417 fprintf (stderr
, s
, a1
, a2
);
418 fprintf (stderr
, "\n");
419 exit (FATAL_EXIT_CODE
);
422 /* More 'friendly' abort that prints the line and file.
423 config.h can #define abort fancy_abort if you like that sort of thing. */
428 fatal ("Internal gcc abort.");
442 obstack_init (rtl_obstack
);
445 fatal ("No input file name.");
447 infile
= fopen (argv
[1], "r");
451 exit (FATAL_EXIT_CODE
);
456 printf ("/* Generated automatically by the program `genpeep'\n\
457 from the machine description file `md'. */\n\n");
459 printf ("#include \"config.h\"\n");
460 printf ("#include <stdio.h>\n");
461 printf ("#include \"rtl.h\"\n");
462 printf ("#include \"regs.h\"\n");
463 printf ("#include \"output.h\"\n");
464 printf ("#include \"real.h\"\n\n");
466 printf ("extern rtx peep_operand[];\n\n");
467 printf ("#define operands peep_operand\n\n");
469 printf ("rtx\npeephole (ins1)\n rtx ins1;\n{\n");
470 printf (" rtx insn, x, pat;\n");
471 printf (" int i;\n\n");
473 /* Early out: no peepholes for insns followed by barriers. */
474 printf (" if (NEXT_INSN (ins1)\n");
475 printf (" && GET_CODE (NEXT_INSN (ins1)) == BARRIER)\n");
476 printf (" return 0;\n\n");
478 /* Read the machine description. */
482 c
= read_skip_spaces (infile
);
487 desc
= read_rtx (infile
);
488 if (GET_CODE (desc
) == DEFINE_PEEPHOLE
)
493 if (GET_CODE (desc
) == DEFINE_INSN
494 || GET_CODE (desc
) == DEFINE_EXPAND
495 || GET_CODE (desc
) == DEFINE_SPLIT
)
501 printf (" return 0;\n}\n\n");
506 printf ("rtx peep_operand[%d];\n", max_opno
+ 1);
509 exit (ferror (stdout
) != 0 ? FATAL_EXIT_CODE
: SUCCESS_EXIT_CODE
);