system.h: Don't redefine abort or trim_filename.
[gcc.git] / gcc / genpeep.c
1 /* Generate code from machine description to perform peephole optimizations.
2 Copyright (C) 1987, 89, 92, 97, 98, 1999 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
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)
9 any later version.
10
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.
15
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. */
20
21
22 #include "hconfig.h"
23 #include "system.h"
24 #include "rtl.h"
25 #include "obstack.h"
26
27 static struct obstack obstack;
28 struct obstack *rtl_obstack = &obstack;
29
30 #define obstack_chunk_alloc xmalloc
31 #define obstack_chunk_free free
32
33 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
34 char **insn_name_ptr = 0;
35
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. */
41
42 struct link
43 {
44 struct link *next;
45 int pos;
46 int vecelt;
47 };
48
49 void fatal PVPROTO ((const char *, ...))
50 ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN;
51
52 static int max_opno;
53
54 /* Number of operands used in current peephole definition. */
55
56 static int n_operands;
57
58 /* Peephole optimizations get insn codes just like insn patterns.
59 Count them so we know the code of the define_peephole we are handling. */
60
61 static int insn_code_number = 0;
62
63 static void gen_peephole PROTO((rtx));
64 static void match_rtx PROTO((rtx, struct link *, int));
65 static void print_path PROTO((struct link *));
66 static void print_code PROTO((RTX_CODE));
67 \f
68 static void
69 gen_peephole (peep)
70 rtx peep;
71 {
72 int ninsns = XVECLEN (peep, 0);
73 int i;
74
75 n_operands = 0;
76
77 printf (" insn = ins1;\n");
78 #if 0
79 printf (" want_jump = 0;\n");
80 #endif
81
82 for (i = 0; i < ninsns; i++)
83 {
84 if (i > 0)
85 {
86 printf (" do { insn = NEXT_INSN (insn);\n");
87 printf (" if (insn == 0) goto L%d; }\n",
88 insn_code_number);
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");
93
94 printf (" if (GET_CODE (insn) == CODE_LABEL\n\
95 || GET_CODE (insn) == BARRIER)\n goto L%d;\n",
96 insn_code_number);
97 }
98
99 #if 0
100 printf (" if (GET_CODE (insn) == JUMP_INSN)\n");
101 printf (" want_jump = JUMP_LABEL (insn);\n");
102 #endif
103
104 printf (" pat = PATTERN (insn);\n");
105
106 /* Walk the insn's pattern, remembering at all times the path
107 down to the walking point. */
108
109 match_rtx (XVECEXP (peep, 0, i), NULL_PTR, insn_code_number);
110 }
111
112 /* We get this far if the pattern matches.
113 Now test the extra condition. */
114
115 if (XSTR (peep, 1) && XSTR (peep, 1)[0])
116 printf (" if (! (%s)) goto L%d;\n",
117 XSTR (peep, 1), insn_code_number);
118
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. */
124
125 printf (" PATTERN (ins1) = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (%d, operands));\n", n_operands);
126
127 #if 0
128 printf (" if (want_jump && GET_CODE (ins1) != JUMP_INSN)\n");
129 printf (" {\n");
130 printf (" rtx insn2 = emit_jump_insn_before (PATTERN (ins1), ins1);\n");
131 printf (" delete_insn (ins1);\n");
132 printf (" ins1 = ins2;\n");
133 printf (" }\n");
134 #endif
135
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",
139 insn_code_number);
140
141 /* Delete the remaining insns. */
142 if (ninsns > 1)
143 printf (" delete_for_peephole (NEXT_INSN (ins1), insn);\n");
144
145 /* See reload1.c for insertion of NOTE which guarantees that this
146 cannot be zero. */
147 printf (" return NEXT_INSN (insn);\n");
148
149 printf (" L%d:\n\n", insn_code_number);
150 }
151 \f
152 static void
153 match_rtx (x, path, fail_label)
154 rtx x;
155 struct link *path;
156 int fail_label;
157 {
158 register RTX_CODE code;
159 register int i;
160 register int len;
161 register const char *fmt;
162 struct link link;
163
164 if (x == 0)
165 return;
166
167
168 code = GET_CODE (x);
169
170 switch (code)
171 {
172 case MATCH_OPERAND:
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);
177
178 printf (" x = ");
179 print_path (path);
180 printf (";\n");
181
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);
186 return;
187
188 case MATCH_DUP:
189 case MATCH_PAR_DUP:
190 printf (" x = ");
191 print_path (path);
192 printf (";\n");
193
194 printf (" if (!rtx_equal_p (operands[%d], x)) goto L%d;\n",
195 XINT (x, 0), fail_label);
196 return;
197
198 case MATCH_OP_DUP:
199 printf (" x = ");
200 print_path (path);
201 printf (";\n");
202
203 printf (" if (GET_CODE (operands[%d]) != GET_CODE (x)\n", XINT (x, 0));
204 printf (" || GET_MODE (operands[%d]) != GET_MODE (x)) goto L%d;\n",
205 XINT (x, 0), fail_label);
206 printf (" operands[%d] = x;\n", XINT (x, 0));
207 link.next = path;
208 link.vecelt = -1;
209 for (i = 0; i < XVECLEN (x, 1); i++)
210 {
211 link.pos = i;
212 match_rtx (XVECEXP (x, 1, i), &link, fail_label);
213 }
214 return;
215
216 case MATCH_OPERATOR:
217 if (XINT (x, 0) > max_opno)
218 max_opno = XINT (x, 0);
219 if (XINT (x, 0) >= n_operands)
220 n_operands = 1 + XINT (x, 0);
221
222 printf (" x = ");
223 print_path (path);
224 printf (";\n");
225
226 printf (" operands[%d] = x;\n", XINT (x, 0));
227 if (XSTR (x, 1) && XSTR (x, 1)[0])
228 printf (" if (! %s (x, %smode)) goto L%d;\n",
229 XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label);
230 link.next = path;
231 link.vecelt = -1;
232 for (i = 0; i < XVECLEN (x, 2); i++)
233 {
234 link.pos = i;
235 match_rtx (XVECEXP (x, 2, i), &link, fail_label);
236 }
237 return;
238
239 case MATCH_PARALLEL:
240 if (XINT (x, 0) > max_opno)
241 max_opno = XINT (x, 0);
242 if (XINT (x, 0) >= n_operands)
243 n_operands = 1 + XINT (x, 0);
244
245 printf (" x = ");
246 print_path (path);
247 printf (";\n");
248
249 printf (" if (GET_CODE (x) != PARALLEL) goto L%d;\n", fail_label);
250 printf (" operands[%d] = x;\n", XINT (x, 0));
251 if (XSTR (x, 1) && XSTR (x, 1)[0])
252 printf (" if (! %s (x, %smode)) goto L%d;\n",
253 XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label);
254 link.next = path;
255 link.pos = 0;
256 for (i = 0; i < XVECLEN (x, 2); i++)
257 {
258 link.vecelt = i;
259 match_rtx (XVECEXP (x, 2, i), &link, fail_label);
260 }
261 return;
262
263 case ADDRESS:
264 match_rtx (XEXP (x, 0), path, fail_label);
265 return;
266
267 default:
268 break;
269 }
270
271 printf (" x = ");
272 print_path (path);
273 printf (";\n");
274
275 printf (" if (GET_CODE (x) != ");
276 print_code (code);
277 printf (") goto L%d;\n", fail_label);
278
279 if (GET_MODE (x) != VOIDmode)
280 {
281 printf (" if (GET_MODE (x) != %smode) goto L%d;\n",
282 GET_MODE_NAME (GET_MODE (x)), fail_label);
283 }
284
285 link.next = path;
286 link.vecelt = -1;
287 fmt = GET_RTX_FORMAT (code);
288 len = GET_RTX_LENGTH (code);
289 for (i = 0; i < len; i++)
290 {
291 link.pos = i;
292 if (fmt[i] == 'e' || fmt[i] == 'u')
293 match_rtx (XEXP (x, i), &link, fail_label);
294 else if (fmt[i] == 'E')
295 {
296 int j;
297 printf (" if (XVECLEN (x, %d) != %d) goto L%d;\n",
298 i, XVECLEN (x, i), fail_label);
299 for (j = 0; j < XVECLEN (x, i); j++)
300 {
301 link.vecelt = j;
302 match_rtx (XVECEXP (x, i, j), &link, fail_label);
303 }
304 }
305 else if (fmt[i] == 'i')
306 {
307 /* Make sure that at run time `x' is the RTX we want to test. */
308 if (i != 0)
309 {
310 printf (" x = ");
311 print_path (path);
312 printf (";\n");
313 }
314
315 printf (" if (XINT (x, %d) != %d) goto L%d;\n",
316 i, XINT (x, i), fail_label);
317 }
318 else if (fmt[i] == 'w')
319 {
320 /* Make sure that at run time `x' is the RTX we want to test. */
321 if (i != 0)
322 {
323 printf (" x = ");
324 print_path (path);
325 printf (";\n");
326 }
327
328 printf (" if (XWINT (x, %d) != ", i);
329 printf (HOST_WIDE_INT_PRINT_DEC, XWINT (x, i));
330 printf (") goto L%d;\n", fail_label);
331 }
332 else if (fmt[i] == 's')
333 {
334 /* Make sure that at run time `x' is the RTX we want to test. */
335 if (i != 0)
336 {
337 printf (" x = ");
338 print_path (path);
339 printf (";\n");
340 }
341
342 printf (" if (strcmp (XSTR (x, %d), \"%s\")) goto L%d;\n",
343 i, XSTR (x, i), fail_label);
344 }
345 }
346 }
347
348 /* Given a PATH, representing a path down the instruction's
349 pattern from the root to a certain point, output code to
350 evaluate to the rtx at that point. */
351
352 static void
353 print_path (path)
354 struct link *path;
355 {
356 if (path == 0)
357 printf ("pat");
358 else if (path->vecelt >= 0)
359 {
360 printf ("XVECEXP (");
361 print_path (path->next);
362 printf (", %d, %d)", path->pos, path->vecelt);
363 }
364 else
365 {
366 printf ("XEXP (");
367 print_path (path->next);
368 printf (", %d)", path->pos);
369 }
370 }
371 \f
372 static void
373 print_code (code)
374 RTX_CODE code;
375 {
376 register const char *p1;
377 for (p1 = GET_RTX_NAME (code); *p1; p1++)
378 {
379 if (*p1 >= 'a' && *p1 <= 'z')
380 putchar (*p1 + 'A' - 'a');
381 else
382 putchar (*p1);
383 }
384 }
385 \f
386 PTR
387 xmalloc (size)
388 size_t size;
389 {
390 register PTR val = (PTR) malloc (size);
391
392 if (val == 0)
393 fatal ("virtual memory exhausted");
394 return val;
395 }
396
397 PTR
398 xrealloc (old, size)
399 PTR old;
400 size_t size;
401 {
402 register PTR ptr;
403 if (old)
404 ptr = (PTR) realloc (old, size);
405 else
406 ptr = (PTR) malloc (size);
407 if (!ptr)
408 fatal ("virtual memory exhausted");
409 return ptr;
410 }
411
412 void
413 fatal VPROTO ((const char *format, ...))
414 {
415 #ifndef ANSI_PROTOTYPES
416 const char *format;
417 #endif
418 va_list ap;
419
420 VA_START (ap, format);
421
422 #ifndef ANSI_PROTOTYPES
423 format = va_arg (ap, const char *);
424 #endif
425
426 fprintf (stderr, "genpeep: ");
427 vfprintf (stderr, format, ap);
428 va_end (ap);
429 fprintf (stderr, "\n");
430 exit (FATAL_EXIT_CODE);
431 }
432
433 int
434 main (argc, argv)
435 int argc;
436 char **argv;
437 {
438 rtx desc;
439 FILE *infile;
440 register int c;
441
442 max_opno = -1;
443
444 obstack_init (rtl_obstack);
445
446 if (argc <= 1)
447 fatal ("No input file name.");
448
449 infile = fopen (argv[1], "r");
450 if (infile == 0)
451 {
452 perror (argv[1]);
453 exit (FATAL_EXIT_CODE);
454 }
455
456 init_rtl ();
457
458 printf ("/* Generated automatically by the program `genpeep'\n\
459 from the machine description file `md'. */\n\n");
460
461 printf ("#include \"config.h\"\n");
462 printf ("#include \"system.h\"\n");
463 printf ("#include \"insn-config.h\"\n");
464 printf ("#include \"rtl.h\"\n");
465 printf ("#include \"regs.h\"\n");
466 printf ("#include \"output.h\"\n");
467 printf ("#include \"real.h\"\n");
468 printf ("#include \"recog.h\"\n");
469 printf ("#include \"except.h\"\n\n");
470 printf ("#include \"function.h\"\n\n");
471
472 printf ("extern rtx peep_operand[];\n\n");
473 printf ("#define operands peep_operand\n\n");
474
475 printf ("rtx\npeephole (ins1)\n rtx ins1;\n{\n");
476 printf (" rtx insn ATTRIBUTE_UNUSED, x ATTRIBUTE_UNUSED, pat ATTRIBUTE_UNUSED;\n\n");
477
478 /* Early out: no peepholes for insns followed by barriers. */
479 printf (" if (NEXT_INSN (ins1)\n");
480 printf (" && GET_CODE (NEXT_INSN (ins1)) == BARRIER)\n");
481 printf (" return 0;\n\n");
482
483 /* Read the machine description. */
484
485 while (1)
486 {
487 c = read_skip_spaces (infile);
488 if (c == EOF)
489 break;
490 ungetc (c, infile);
491
492 desc = read_rtx (infile);
493 if (GET_CODE (desc) == DEFINE_PEEPHOLE)
494 {
495 gen_peephole (desc);
496 insn_code_number++;
497 }
498 if (GET_CODE (desc) == DEFINE_INSN
499 || GET_CODE (desc) == DEFINE_EXPAND
500 || GET_CODE (desc) == DEFINE_SPLIT)
501 {
502 insn_code_number++;
503 }
504 }
505
506 printf (" return 0;\n}\n\n");
507
508 if (max_opno == -1)
509 max_opno = 1;
510
511 printf ("rtx peep_operand[%d];\n", max_opno + 1);
512
513 fflush (stdout);
514 exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
515 /* NOTREACHED */
516 return 0;
517 }