OSDN Git Service

a64235ace233e5e6cdb37b5911c06275eb23e6e2
[pf3gnuchains/gcc-fork.git] / gcc / genpeep.c
1 /* Generate code from machine description to perform peephole optimizations.
2    Copyright (C) 1987, 1989, 1992, 1997, 1998 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 /* While tree-walking an instruction pattern, we keep a chain
34    of these `struct link's to record how to get down to the
35    current position.  In each one, POS is the operand number,
36    and if the operand is a vector VEC is the element number.
37    VEC is -1 if the operand is not a vector.  */
38
39 struct link
40 {
41   struct link *next;
42   int pos;
43   int vecelt;
44 };
45
46 char *xmalloc PROTO((unsigned));
47 static void match_rtx PROTO((rtx, struct link *, int));
48 static void fatal ();
49 void fancy_abort PROTO((void));
50
51 static int max_opno;
52
53 /* Number of operands used in current peephole definition.  */
54
55 static int n_operands;
56
57 /* Peephole optimizations get insn codes just like insn patterns.
58    Count them so we know the code of the define_peephole we are handling.  */
59
60 static int insn_code_number = 0;
61
62 static void print_path PROTO((struct link *));
63 static void print_code PROTO((RTX_CODE));
64 \f
65 static void
66 gen_peephole (peep)
67      rtx peep;
68 {
69   int ninsns = XVECLEN (peep, 0);
70   int i;
71
72   n_operands = 0;
73
74   printf ("  insn = ins1;\n");
75 #if 0
76   printf ("  want_jump = 0;\n");
77 #endif
78
79   for (i = 0; i < ninsns; i++)
80     {
81       if (i > 0)
82         {
83           printf ("  do { insn = NEXT_INSN (insn);\n");
84           printf ("       if (insn == 0) goto L%d; }\n",
85                   insn_code_number);
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");
90
91           printf ("  if (GET_CODE (insn) == CODE_LABEL\n\
92       || GET_CODE (insn) == BARRIER)\n    goto L%d;\n",
93                   insn_code_number);
94         }
95
96 #if 0
97       printf ("  if (GET_CODE (insn) == JUMP_INSN)\n");
98       printf ("    want_jump = JUMP_LABEL (insn);\n");
99 #endif
100
101       printf ("  pat = PATTERN (insn);\n");
102
103       /* Walk the insn's pattern, remembering at all times the path
104          down to the walking point.  */
105
106       match_rtx (XVECEXP (peep, 0, i), NULL_PTR, insn_code_number);
107     }
108
109   /* We get this far if the pattern matches.
110      Now test the extra condition.  */
111
112   if (XSTR (peep, 1) && XSTR (peep, 1)[0])
113     printf ("  if (! (%s)) goto L%d;\n",
114             XSTR (peep, 1), insn_code_number);
115
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.  */
121
122   printf ("  PATTERN (ins1) = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (%d, operands));\n", n_operands);
123
124 #if 0
125   printf ("  if (want_jump && GET_CODE (ins1) != JUMP_INSN)\n");
126   printf ("    {\n");
127   printf ("      rtx insn2 = emit_jump_insn_before (PATTERN (ins1), ins1);\n");
128   printf ("      delete_insn (ins1);\n");
129   printf ("      ins1 = ins2;\n");
130   printf ("    }\n");
131 #endif
132
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",
136           insn_code_number);
137
138   /* Delete the remaining insns.  */
139   if (ninsns > 1)
140     printf ("  delete_for_peephole (NEXT_INSN (ins1), insn);\n");
141
142   /* See reload1.c for insertion of NOTE which guarantees that this
143      cannot be zero.  */
144   printf ("  return NEXT_INSN (insn);\n");
145
146   printf (" L%d:\n\n", insn_code_number);
147 }
148 \f
149 static void
150 match_rtx (x, path, fail_label)
151      rtx x;
152      struct link *path;
153      int fail_label;
154 {
155   register RTX_CODE code;
156   register int i;
157   register int len;
158   register char *fmt;
159   struct link link;
160
161   if (x == 0)
162     return;
163
164
165   code = GET_CODE (x);
166
167   switch (code)
168     {
169     case MATCH_OPERAND:
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);
174
175       printf ("  x = ");
176       print_path (path);
177       printf (";\n");
178
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);
183       return;
184
185     case MATCH_DUP:
186     case MATCH_PAR_DUP:
187       printf ("  x = ");
188       print_path (path);
189       printf (";\n");
190
191       printf ("  if (!rtx_equal_p (operands[%d], x)) goto L%d;\n",
192               XINT (x, 0), fail_label);
193       return;
194
195     case MATCH_OP_DUP:
196       printf ("  x = ");
197       print_path (path);
198       printf (";\n");
199
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));
204       link.next = path;
205       link.vecelt = -1;
206       for (i = 0; i < XVECLEN (x, 1); i++)
207         {
208           link.pos = i;
209           match_rtx (XVECEXP (x, 1, i), &link, fail_label);
210         }
211       return;
212
213     case MATCH_OPERATOR:
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);
218
219       printf ("  x = ");
220       print_path (path);
221       printf (";\n");
222
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);
227       link.next = path;
228       link.vecelt = -1;
229       for (i = 0; i < XVECLEN (x, 2); i++)
230         {
231           link.pos = i;
232           match_rtx (XVECEXP (x, 2, i), &link, fail_label);
233         }
234       return;
235
236     case MATCH_PARALLEL:
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);
241
242       printf ("  x = ");
243       print_path (path);
244       printf (";\n");
245
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);
251       link.next = path;
252       link.pos = 0;
253       for (i = 0; i < XVECLEN (x, 2); i++)
254         {
255           link.vecelt = i;
256           match_rtx (XVECEXP (x, 2, i), &link, fail_label);
257         }
258       return;
259
260     case ADDRESS:
261       match_rtx (XEXP (x, 0), path, fail_label);
262       return;
263       
264     default:
265       break;
266     }
267
268   printf ("  x = ");
269   print_path (path);
270   printf (";\n");
271
272   printf ("  if (GET_CODE (x) != ");
273   print_code (code);
274   printf (") goto L%d;\n", fail_label);
275
276   if (GET_MODE (x) != VOIDmode)
277     {
278       printf ("  if (GET_MODE (x) != %smode) goto L%d;\n",
279               GET_MODE_NAME (GET_MODE (x)), fail_label);
280     }
281
282   link.next = path;
283   link.vecelt = -1;
284   fmt = GET_RTX_FORMAT (code);
285   len = GET_RTX_LENGTH (code);
286   for (i = 0; i < len; i++)
287     {
288       link.pos = i;
289       if (fmt[i] == 'e' || fmt[i] == 'u')
290         match_rtx (XEXP (x, i), &link, fail_label);
291       else if (fmt[i] == 'E')
292         {
293           int j;
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++)
297             {
298               link.vecelt = j;
299               match_rtx (XVECEXP (x, i, j), &link, fail_label);
300             }
301         }
302       else if (fmt[i] == 'i')
303         {
304           /* Make sure that at run time `x' is the RTX we want to test.  */
305           if (i != 0)
306             {
307               printf ("  x = ");
308               print_path (path);
309               printf (";\n");
310             }
311
312           printf ("  if (XINT (x, %d) != %d) goto L%d;\n",
313                   i, XINT (x, i), fail_label);
314         }
315       else if (fmt[i] == 'w')
316         {
317           /* Make sure that at run time `x' is the RTX we want to test.  */
318           if (i != 0)
319             {
320               printf ("  x = ");
321               print_path (path);
322               printf (";\n");
323             }
324
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);
328         }
329       else if (fmt[i] == 's')
330         {
331           /* Make sure that at run time `x' is the RTX we want to test.  */
332           if (i != 0)
333             {
334               printf ("  x = ");
335               print_path (path);
336               printf (";\n");
337             }
338
339           printf ("  if (strcmp (XSTR (x, %d), \"%s\")) goto L%d;\n",
340                   i, XSTR (x, i), fail_label);
341         }
342     }
343 }
344
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.  */
348
349 static void
350 print_path (path)
351      struct link *path;
352 {
353   if (path == 0)
354     printf ("pat");
355   else if (path->vecelt >= 0)
356     {
357       printf ("XVECEXP (");
358       print_path (path->next);
359       printf (", %d, %d)", path->pos, path->vecelt);
360     }
361   else
362     {
363       printf ("XEXP (");
364       print_path (path->next);
365       printf (", %d)", path->pos);
366     }
367 }
368 \f
369 static void
370 print_code (code)
371      RTX_CODE code;
372 {
373   register char *p1;
374   for (p1 = GET_RTX_NAME (code); *p1; p1++)
375     {
376       if (*p1 >= 'a' && *p1 <= 'z')
377         putchar (*p1 + 'A' - 'a');
378       else
379         putchar (*p1);
380     }
381 }
382 \f
383 char *
384 xmalloc (size)
385      unsigned size;
386 {
387   register char *val = (char *) malloc (size);
388
389   if (val == 0)
390     fatal ("virtual memory exhausted");
391   return val;
392 }
393
394 char *
395 xrealloc (ptr, size)
396      char *ptr;
397      unsigned size;
398 {
399   char *result = (char *) realloc (ptr, size);
400   if (!result)
401     fatal ("virtual memory exhausted");
402   return result;
403 }
404
405 static void
406 fatal (s, a1, a2)
407      char *s;
408 {
409   fprintf (stderr, "genpeep: ");
410   fprintf (stderr, s, a1, a2);
411   fprintf (stderr, "\n");
412   exit (FATAL_EXIT_CODE);
413 }
414
415 /* More 'friendly' abort that prints the line and file.
416    config.h can #define abort fancy_abort if you like that sort of thing.  */
417
418 void
419 fancy_abort ()
420 {
421   fatal ("Internal gcc abort.");
422 }
423 \f
424 int
425 main (argc, argv)
426      int argc;
427      char **argv;
428 {
429   rtx desc;
430   FILE *infile;
431   register int c;
432
433   max_opno = -1;
434
435   obstack_init (rtl_obstack);
436
437   if (argc <= 1)
438     fatal ("No input file name.");
439
440   infile = fopen (argv[1], "r");
441   if (infile == 0)
442     {
443       perror (argv[1]);
444       exit (FATAL_EXIT_CODE);
445     }
446
447   init_rtl ();
448
449   printf ("/* Generated automatically by the program `genpeep'\n\
450 from the machine description file `md'.  */\n\n");
451
452   printf ("#include \"config.h\"\n");
453   printf ("#include <stdio.h>\n");
454   printf ("#include \"rtl.h\"\n");
455   printf ("#include \"regs.h\"\n");
456   printf ("#include \"output.h\"\n");
457   printf ("#include \"real.h\"\n");
458   printf ("#include \"except.h\"\n\n");
459
460   printf ("extern rtx peep_operand[];\n\n");
461   printf ("#define operands peep_operand\n\n");
462
463   printf ("rtx\npeephole (ins1)\n     rtx ins1;\n{\n");
464   printf ("  rtx insn, x, pat;\n\n");
465
466   /* Early out: no peepholes for insns followed by barriers.  */
467   printf ("  if (NEXT_INSN (ins1)\n");
468   printf ("      && GET_CODE (NEXT_INSN (ins1)) == BARRIER)\n");
469   printf ("    return 0;\n\n");
470
471   /* Read the machine description.  */
472
473   while (1)
474     {
475       c = read_skip_spaces (infile);
476       if (c == EOF)
477         break;
478       ungetc (c, infile);
479
480       desc = read_rtx (infile);
481       if (GET_CODE (desc) == DEFINE_PEEPHOLE)
482         {
483           gen_peephole (desc);
484           insn_code_number++;
485         }
486       if (GET_CODE (desc) == DEFINE_INSN
487           || GET_CODE (desc) == DEFINE_EXPAND
488           || GET_CODE (desc) == DEFINE_SPLIT)
489         {
490           insn_code_number++;
491         }
492     }
493
494   printf ("  return 0;\n}\n\n");
495
496   if (max_opno == -1)
497     max_opno = 1;
498
499   printf ("rtx peep_operand[%d];\n", max_opno + 1);
500
501   fflush (stdout);
502   exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
503   /* NOTREACHED */
504   return 0;
505 }