OSDN Git Service

* rtl.h (addr_diff_vec_flags): New typedef.
[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 <stdio.h>
23 #include "hconfig.h"
24 #include "rtl.h"
25 #include "obstack.h"
26
27 #ifdef HAVE_STDLIB_H
28 #include <stdlib.h>
29 #endif
30
31 static struct obstack obstack;
32 struct obstack *rtl_obstack = &obstack;
33
34 #define obstack_chunk_alloc xmalloc
35 #define obstack_chunk_free free
36
37 #ifdef NEED_DECLARATION_FREE
38 extern void free ();
39 #endif
40 extern rtx read_rtx ();
41
42 /* While tree-walking an instruction pattern, we keep a chain
43    of these `struct link's to record how to get down to the
44    current position.  In each one, POS is the operand number,
45    and if the operand is a vector VEC is the element number.
46    VEC is -1 if the operand is not a vector.  */
47
48 struct link
49 {
50   struct link *next;
51   int pos;
52   int vecelt;
53 };
54
55 char *xmalloc ();
56 static void match_rtx ();
57 static void fatal ();
58 void fancy_abort ();
59
60 static int max_opno;
61
62 /* Number of operands used in current peephole definition.  */
63
64 static int n_operands;
65
66 /* Peephole optimizations get insn codes just like insn patterns.
67    Count them so we know the code of the define_peephole we are handling.  */
68
69 static int insn_code_number = 0;
70
71 static void print_path ();
72 static void print_code ();
73 \f
74 static void
75 gen_peephole (peep)
76      rtx peep;
77 {
78   int ninsns = XVECLEN (peep, 0);
79   int i;
80
81   n_operands = 0;
82
83   printf ("  insn = ins1;\n");
84 #if 0
85   printf ("  want_jump = 0;\n");
86 #endif
87
88   for (i = 0; i < ninsns; i++)
89     {
90       if (i > 0)
91         {
92           printf ("  do { insn = NEXT_INSN (insn);\n");
93           printf ("       if (insn == 0) goto L%d; }\n",
94                   insn_code_number);
95           printf ("  while (GET_CODE (insn) == NOTE\n");
96           printf ("\t || (GET_CODE (insn) == INSN\n");
97           printf ("\t     && (GET_CODE (PATTERN (insn)) == USE\n");
98           printf ("\t\t || GET_CODE (PATTERN (insn)) == CLOBBER)));\n");
99
100           printf ("  if (GET_CODE (insn) == CODE_LABEL\n\
101       || GET_CODE (insn) == BARRIER)\n    goto L%d;\n",
102                   insn_code_number);
103         }
104
105 #if 0
106       printf ("  if (GET_CODE (insn) == JUMP_INSN)\n");
107       printf ("    want_jump = JUMP_LABEL (insn);\n");
108 #endif
109
110       printf ("  pat = PATTERN (insn);\n");
111
112       /* Walk the insn's pattern, remembering at all times the path
113          down to the walking point.  */
114
115       match_rtx (XVECEXP (peep, 0, i), NULL_PTR, insn_code_number);
116     }
117
118   /* We get this far if the pattern matches.
119      Now test the extra condition.  */
120
121   if (XSTR (peep, 1) && XSTR (peep, 1)[0])
122     printf ("  if (! (%s)) goto L%d;\n",
123             XSTR (peep, 1), insn_code_number);
124
125   /* If that matches, construct new pattern and put it in the first insn.
126      This new pattern will never be matched.
127      It exists only so that insn-extract can get the operands back.
128      So use a simple regular form: a PARALLEL containing a vector
129      of all the operands.  */
130
131   printf ("  PATTERN (ins1) = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (%d, operands));\n", n_operands);
132
133 #if 0
134   printf ("  if (want_jump && GET_CODE (ins1) != JUMP_INSN)\n");
135   printf ("    {\n");
136   printf ("      rtx insn2 = emit_jump_insn_before (PATTERN (ins1), ins1);\n");
137   printf ("      delete_insn (ins1);\n");
138   printf ("      ins1 = ins2;\n");
139   printf ("    }\n");
140 #endif
141
142   /* Record this define_peephole's insn code in the insn,
143      as if it had been recognized to match this.  */
144   printf ("  INSN_CODE (ins1) = %d;\n",
145           insn_code_number);
146
147   /* Delete the remaining insns.  */
148   if (ninsns > 1)
149     printf ("  delete_for_peephole (NEXT_INSN (ins1), insn);\n");
150
151   /* See reload1.c for insertion of NOTE which guarantees that this
152      cannot be zero.  */
153   printf ("  return NEXT_INSN (insn);\n");
154
155   printf (" L%d:\n\n", insn_code_number);
156 }
157 \f
158 static void
159 match_rtx (x, path, fail_label)
160      rtx x;
161      struct link *path;
162      int fail_label;
163 {
164   register RTX_CODE code;
165   register int i;
166   register int len;
167   register char *fmt;
168   struct link link;
169
170   if (x == 0)
171     return;
172
173
174   code = GET_CODE (x);
175
176   switch (code)
177     {
178     case MATCH_OPERAND:
179       if (XINT (x, 0) > max_opno)
180         max_opno = XINT (x, 0);
181       if (XINT (x, 0) >= n_operands)
182         n_operands = 1 + XINT (x, 0);
183
184       printf ("  x = ");
185       print_path (path);
186       printf (";\n");
187
188       printf ("  operands[%d] = x;\n", XINT (x, 0));
189       if (XSTR (x, 1) && XSTR (x, 1)[0])
190         printf ("  if (! %s (x, %smode)) goto L%d;\n",
191                 XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label);
192       return;
193
194     case MATCH_DUP:
195     case MATCH_PAR_DUP:
196       printf ("  x = ");
197       print_path (path);
198       printf (";\n");
199
200       printf ("  if (!rtx_equal_p (operands[%d], x)) goto L%d;\n",
201               XINT (x, 0), fail_label);
202       return;
203
204     case MATCH_OP_DUP:
205       printf ("  x = ");
206       print_path (path);
207       printf (";\n");
208
209       printf ("  if (GET_CODE (operands[%d]) != GET_CODE (x)\n", XINT (x, 0));
210       printf ("      || GET_MODE (operands[%d]) != GET_MODE (x)) goto L%d;\n",
211               XINT (x, 0), fail_label);
212       printf ("  operands[%d] = x;\n", XINT (x, 0));
213       link.next = path;
214       link.vecelt = -1;
215       for (i = 0; i < XVECLEN (x, 1); i++)
216         {
217           link.pos = i;
218           match_rtx (XVECEXP (x, 1, i), &link, fail_label);
219         }
220       return;
221
222     case MATCH_OPERATOR:
223       if (XINT (x, 0) > max_opno)
224         max_opno = XINT (x, 0);
225       if (XINT (x, 0) >= n_operands)
226         n_operands = 1 + XINT (x, 0);
227
228       printf ("  x = ");
229       print_path (path);
230       printf (";\n");
231
232       printf ("  operands[%d] = x;\n", XINT (x, 0));
233       if (XSTR (x, 1) && XSTR (x, 1)[0])
234         printf ("  if (! %s (x, %smode)) goto L%d;\n",
235                 XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label);
236       link.next = path;
237       link.vecelt = -1;
238       for (i = 0; i < XVECLEN (x, 2); i++)
239         {
240           link.pos = i;
241           match_rtx (XVECEXP (x, 2, i), &link, fail_label);
242         }
243       return;
244
245     case MATCH_PARALLEL:
246       if (XINT (x, 0) > max_opno)
247         max_opno = XINT (x, 0);
248       if (XINT (x, 0) >= n_operands)
249         n_operands = 1 + XINT (x, 0);
250
251       printf ("  x = ");
252       print_path (path);
253       printf (";\n");
254
255       printf ("  if (GET_CODE (x) != PARALLEL) goto L%d;\n", fail_label);
256       printf ("  operands[%d] = x;\n", XINT (x, 0));
257       if (XSTR (x, 1) && XSTR (x, 1)[0])
258         printf ("  if (! %s (x, %smode)) goto L%d;\n",
259                 XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label);
260       link.next = path;
261       link.pos = 0;
262       for (i = 0; i < XVECLEN (x, 2); i++)
263         {
264           link.vecelt = i;
265           match_rtx (XVECEXP (x, 2, i), &link, fail_label);
266         }
267       return;
268
269     case ADDRESS:
270       match_rtx (XEXP (x, 0), path, fail_label);
271       return;
272       
273     default:
274       break;
275     }
276
277   printf ("  x = ");
278   print_path (path);
279   printf (";\n");
280
281   printf ("  if (GET_CODE (x) != ");
282   print_code (code);
283   printf (") goto L%d;\n", fail_label);
284
285   if (GET_MODE (x) != VOIDmode)
286     {
287       printf ("  if (GET_MODE (x) != %smode) goto L%d;\n",
288               GET_MODE_NAME (GET_MODE (x)), fail_label);
289     }
290
291   link.next = path;
292   link.vecelt = -1;
293   fmt = GET_RTX_FORMAT (code);
294   len = GET_RTX_LENGTH (code);
295   for (i = 0; i < len; i++)
296     {
297       link.pos = i;
298       if (fmt[i] == 'e' || fmt[i] == 'u')
299         match_rtx (XEXP (x, i), &link, fail_label);
300       else if (fmt[i] == 'E')
301         {
302           int j;
303           printf ("  if (XVECLEN (x, %d) != %d) goto L%d;\n",
304                   i, XVECLEN (x, i), fail_label);
305           for (j = 0; j < XVECLEN (x, i); j++)
306             {
307               link.vecelt = j;
308               match_rtx (XVECEXP (x, i, j), &link, fail_label);
309             }
310         }
311       else if (fmt[i] == 'i')
312         {
313           /* Make sure that at run time `x' is the RTX we want to test.  */
314           if (i != 0)
315             {
316               printf ("  x = ");
317               print_path (path);
318               printf (";\n");
319             }
320
321           printf ("  if (XINT (x, %d) != %d) goto L%d;\n",
322                   i, XINT (x, i), fail_label);
323         }
324       else if (fmt[i] == 'w')
325         {
326           /* Make sure that at run time `x' is the RTX we want to test.  */
327           if (i != 0)
328             {
329               printf ("  x = ");
330               print_path (path);
331               printf (";\n");
332             }
333
334           printf ("  if (XWINT (x, %d) != ", i);
335           printf (HOST_WIDE_INT_PRINT_DEC, XWINT (x, i));
336           printf (") goto L%d;\n", fail_label);
337         }
338       else if (fmt[i] == 's')
339         {
340           /* Make sure that at run time `x' is the RTX we want to test.  */
341           if (i != 0)
342             {
343               printf ("  x = ");
344               print_path (path);
345               printf (";\n");
346             }
347
348           printf ("  if (strcmp (XSTR (x, %d), \"%s\")) goto L%d;\n",
349                   i, XSTR (x, i), fail_label);
350         }
351     }
352 }
353
354 /* Given a PATH, representing a path down the instruction's
355    pattern from the root to a certain point, output code to
356    evaluate to the rtx at that point.  */
357
358 static void
359 print_path (path)
360      struct link *path;
361 {
362   if (path == 0)
363     printf ("pat");
364   else if (path->vecelt >= 0)
365     {
366       printf ("XVECEXP (");
367       print_path (path->next);
368       printf (", %d, %d)", path->pos, path->vecelt);
369     }
370   else
371     {
372       printf ("XEXP (");
373       print_path (path->next);
374       printf (", %d)", path->pos);
375     }
376 }
377 \f
378 static void
379 print_code (code)
380      RTX_CODE code;
381 {
382   register char *p1;
383   for (p1 = GET_RTX_NAME (code); *p1; p1++)
384     {
385       if (*p1 >= 'a' && *p1 <= 'z')
386         putchar (*p1 + 'A' - 'a');
387       else
388         putchar (*p1);
389     }
390 }
391 \f
392 char *
393 xmalloc (size)
394      unsigned size;
395 {
396   register char *val = (char *) malloc (size);
397
398   if (val == 0)
399     fatal ("virtual memory exhausted");
400   return val;
401 }
402
403 char *
404 xrealloc (ptr, size)
405      char *ptr;
406      unsigned size;
407 {
408   char *result = (char *) realloc (ptr, size);
409   if (!result)
410     fatal ("virtual memory exhausted");
411   return result;
412 }
413
414 static void
415 fatal (s, a1, a2)
416      char *s;
417 {
418   fprintf (stderr, "genpeep: ");
419   fprintf (stderr, s, a1, a2);
420   fprintf (stderr, "\n");
421   exit (FATAL_EXIT_CODE);
422 }
423
424 /* More 'friendly' abort that prints the line and file.
425    config.h can #define abort fancy_abort if you like that sort of thing.  */
426
427 void
428 fancy_abort ()
429 {
430   fatal ("Internal gcc abort.");
431 }
432 \f
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 <stdio.h>\n");
463   printf ("#include \"rtl.h\"\n");
464   printf ("#include \"regs.h\"\n");
465   printf ("#include \"output.h\"\n");
466   printf ("#include \"real.h\"\n\n");
467
468   printf ("extern rtx peep_operand[];\n\n");
469   printf ("#define operands peep_operand\n\n");
470
471   printf ("rtx\npeephole (ins1)\n     rtx ins1;\n{\n");
472   printf ("  rtx insn, x, pat;\n");
473   printf ("  int i;\n\n");
474
475   /* Early out: no peepholes for insns followed by barriers.  */
476   printf ("  if (NEXT_INSN (ins1)\n");
477   printf ("      && GET_CODE (NEXT_INSN (ins1)) == BARRIER)\n");
478   printf ("    return 0;\n\n");
479
480   /* Read the machine description.  */
481
482   while (1)
483     {
484       c = read_skip_spaces (infile);
485       if (c == EOF)
486         break;
487       ungetc (c, infile);
488
489       desc = read_rtx (infile);
490       if (GET_CODE (desc) == DEFINE_PEEPHOLE)
491         {
492           gen_peephole (desc);
493           insn_code_number++;
494         }
495       if (GET_CODE (desc) == DEFINE_INSN
496           || GET_CODE (desc) == DEFINE_EXPAND
497           || GET_CODE (desc) == DEFINE_SPLIT)
498         {
499           insn_code_number++;
500         }
501     }
502
503   printf ("  return 0;\n}\n\n");
504
505   if (max_opno == -1)
506     max_opno = 1;
507
508   printf ("rtx peep_operand[%d];\n", max_opno + 1);
509
510   fflush (stdout);
511   exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
512   /* NOTREACHED */
513   return 0;
514 }