OSDN Git Service

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