OSDN Git Service

* calls.c (ECF_MALLOC, ECF_MAY_BE_ALLOCA, ECF_RETURNS_TWICE,
[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,
3    1999, 2000 Free Software Foundation, Inc.
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22
23 #include "hconfig.h"
24 #include "system.h"
25 #include "rtl.h"
26 #include "obstack.h"
27 #include "errors.h"
28
29 static struct obstack obstack;
30 struct obstack *rtl_obstack = &obstack;
31
32 #define obstack_chunk_alloc xmalloc
33 #define obstack_chunk_free free
34
35 /* While tree-walking an instruction pattern, we keep a chain
36    of these `struct link's to record how to get down to the
37    current position.  In each one, POS is the operand number,
38    and if the operand is a vector VEC is the element number.
39    VEC is -1 if the operand is not a vector.  */
40
41 struct link
42 {
43   struct link *next;
44   int pos;
45   int vecelt;
46 };
47
48 static int max_opno;
49
50 /* Number of operands used in current peephole definition.  */
51
52 static int n_operands;
53
54 /* Peephole optimizations get insn codes just like insn patterns.
55    Count them so we know the code of the define_peephole we are handling.  */
56
57 static int insn_code_number = 0;
58
59 static void gen_peephole PARAMS ((rtx));
60 static void match_rtx PARAMS ((rtx, struct link *, int));
61 static void print_path PARAMS ((struct link *));
62 static void print_code PARAMS ((RTX_CODE));
63 \f
64 static void
65 gen_peephole (peep)
66      rtx peep;
67 {
68   int ninsns = XVECLEN (peep, 0);
69   int i;
70
71   n_operands = 0;
72
73   printf ("  insn = ins1;\n");
74 #if 0
75   printf ("  want_jump = 0;\n");
76 #endif
77
78   for (i = 0; i < ninsns; i++)
79     {
80       if (i > 0)
81         {
82           printf ("  do { insn = NEXT_INSN (insn);\n");
83           printf ("       if (insn == 0) goto L%d; }\n",
84                   insn_code_number);
85           printf ("  while (GET_CODE (insn) == NOTE\n");
86           printf ("\t || (GET_CODE (insn) == INSN\n");
87           printf ("\t     && (GET_CODE (PATTERN (insn)) == USE\n");
88           printf ("\t\t || GET_CODE (PATTERN (insn)) == CLOBBER)));\n");
89
90           printf ("  if (GET_CODE (insn) == CODE_LABEL\n\
91       || GET_CODE (insn) == BARRIER)\n    goto L%d;\n",
92                   insn_code_number);
93         }
94
95 #if 0
96       printf ("  if (GET_CODE (insn) == JUMP_INSN)\n");
97       printf ("    want_jump = JUMP_LABEL (insn);\n");
98 #endif
99
100       printf ("  pat = PATTERN (insn);\n");
101
102       /* Walk the insn's pattern, remembering at all times the path
103          down to the walking point.  */
104
105       match_rtx (XVECEXP (peep, 0, i), NULL_PTR, insn_code_number);
106     }
107
108   /* We get this far if the pattern matches.
109      Now test the extra condition.  */
110
111   if (XSTR (peep, 1) && XSTR (peep, 1)[0])
112     printf ("  if (! (%s)) goto L%d;\n",
113             XSTR (peep, 1), insn_code_number);
114
115   /* If that matches, construct new pattern and put it in the first insn.
116      This new pattern will never be matched.
117      It exists only so that insn-extract can get the operands back.
118      So use a simple regular form: a PARALLEL containing a vector
119      of all the operands.  */
120
121   printf ("  PATTERN (ins1) = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (%d, operands));\n", n_operands);
122
123 #if 0
124   printf ("  if (want_jump && GET_CODE (ins1) != JUMP_INSN)\n");
125   printf ("    {\n");
126   printf ("      rtx insn2 = emit_jump_insn_before (PATTERN (ins1), ins1);\n");
127   printf ("      delete_insn (ins1);\n");
128   printf ("      ins1 = ins2;\n");
129   printf ("    }\n");
130 #endif
131
132   /* Record this define_peephole's insn code in the insn,
133      as if it had been recognized to match this.  */
134   printf ("  INSN_CODE (ins1) = %d;\n",
135           insn_code_number);
136
137   /* Delete the remaining insns.  */
138   if (ninsns > 1)
139     printf ("  delete_for_peephole (NEXT_INSN (ins1), insn);\n");
140
141   /* See reload1.c for insertion of NOTE which guarantees that this
142      cannot be zero.  */
143   printf ("  return NEXT_INSN (insn);\n");
144
145   printf (" L%d:\n\n", insn_code_number);
146 }
147 \f
148 static void
149 match_rtx (x, path, fail_label)
150      rtx x;
151      struct link *path;
152      int fail_label;
153 {
154   register RTX_CODE code;
155   register int i;
156   register int len;
157   register const char *fmt;
158   struct link link;
159
160   if (x == 0)
161     return;
162
163
164   code = GET_CODE (x);
165
166   switch (code)
167     {
168     case MATCH_OPERAND:
169       if (XINT (x, 0) > max_opno)
170         max_opno = XINT (x, 0);
171       if (XINT (x, 0) >= n_operands)
172         n_operands = 1 + XINT (x, 0);
173
174       printf ("  x = ");
175       print_path (path);
176       printf (";\n");
177
178       printf ("  operands[%d] = x;\n", XINT (x, 0));
179       if (XSTR (x, 1) && XSTR (x, 1)[0])
180         printf ("  if (! %s (x, %smode)) goto L%d;\n",
181                 XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label);
182       return;
183
184     case MATCH_DUP:
185     case MATCH_PAR_DUP:
186       printf ("  x = ");
187       print_path (path);
188       printf (";\n");
189
190       printf ("  if (!rtx_equal_p (operands[%d], x)) goto L%d;\n",
191               XINT (x, 0), fail_label);
192       return;
193
194     case MATCH_OP_DUP:
195       printf ("  x = ");
196       print_path (path);
197       printf (";\n");
198
199       printf ("  if (GET_CODE (operands[%d]) != GET_CODE (x)\n", XINT (x, 0));
200       printf ("      || GET_MODE (operands[%d]) != GET_MODE (x)) goto L%d;\n",
201               XINT (x, 0), fail_label);
202       printf ("  operands[%d] = x;\n", XINT (x, 0));
203       link.next = path;
204       link.vecelt = -1;
205       for (i = 0; i < XVECLEN (x, 1); i++)
206         {
207           link.pos = i;
208           match_rtx (XVECEXP (x, 1, i), &link, fail_label);
209         }
210       return;
211
212     case MATCH_OPERATOR:
213       if (XINT (x, 0) > max_opno)
214         max_opno = XINT (x, 0);
215       if (XINT (x, 0) >= n_operands)
216         n_operands = 1 + XINT (x, 0);
217
218       printf ("  x = ");
219       print_path (path);
220       printf (";\n");
221
222       printf ("  operands[%d] = x;\n", XINT (x, 0));
223       if (XSTR (x, 1) && XSTR (x, 1)[0])
224         printf ("  if (! %s (x, %smode)) goto L%d;\n",
225                 XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label);
226       link.next = path;
227       link.vecelt = -1;
228       for (i = 0; i < XVECLEN (x, 2); i++)
229         {
230           link.pos = i;
231           match_rtx (XVECEXP (x, 2, i), &link, fail_label);
232         }
233       return;
234
235     case MATCH_PARALLEL:
236       if (XINT (x, 0) > max_opno)
237         max_opno = XINT (x, 0);
238       if (XINT (x, 0) >= n_operands)
239         n_operands = 1 + XINT (x, 0);
240
241       printf ("  x = ");
242       print_path (path);
243       printf (";\n");
244
245       printf ("  if (GET_CODE (x) != PARALLEL) goto L%d;\n", fail_label);
246       printf ("  operands[%d] = x;\n", XINT (x, 0));
247       if (XSTR (x, 1) && XSTR (x, 1)[0])
248         printf ("  if (! %s (x, %smode)) goto L%d;\n",
249                 XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label);
250       link.next = path;
251       link.pos = 0;
252       for (i = 0; i < XVECLEN (x, 2); i++)
253         {
254           link.vecelt = i;
255           match_rtx (XVECEXP (x, 2, i), &link, fail_label);
256         }
257       return;
258
259     case ADDRESS:
260       match_rtx (XEXP (x, 0), path, fail_label);
261       return;
262       
263     default:
264       break;
265     }
266
267   printf ("  x = ");
268   print_path (path);
269   printf (";\n");
270
271   printf ("  if (GET_CODE (x) != ");
272   print_code (code);
273   printf (") goto L%d;\n", fail_label);
274
275   if (GET_MODE (x) != VOIDmode)
276     {
277       printf ("  if (GET_MODE (x) != %smode) goto L%d;\n",
278               GET_MODE_NAME (GET_MODE (x)), fail_label);
279     }
280
281   link.next = path;
282   link.vecelt = -1;
283   fmt = GET_RTX_FORMAT (code);
284   len = GET_RTX_LENGTH (code);
285   for (i = 0; i < len; i++)
286     {
287       link.pos = i;
288       if (fmt[i] == 'e' || fmt[i] == 'u')
289         match_rtx (XEXP (x, i), &link, fail_label);
290       else if (fmt[i] == 'E')
291         {
292           int j;
293           printf ("  if (XVECLEN (x, %d) != %d) goto L%d;\n",
294                   i, XVECLEN (x, i), fail_label);
295           for (j = 0; j < XVECLEN (x, i); j++)
296             {
297               link.vecelt = j;
298               match_rtx (XVECEXP (x, i, j), &link, fail_label);
299             }
300         }
301       else if (fmt[i] == 'i')
302         {
303           /* Make sure that at run time `x' is the RTX we want to test.  */
304           if (i != 0)
305             {
306               printf ("  x = ");
307               print_path (path);
308               printf (";\n");
309             }
310
311           printf ("  if (XINT (x, %d) != %d) goto L%d;\n",
312                   i, XINT (x, i), fail_label);
313         }
314       else if (fmt[i] == 'w')
315         {
316           /* Make sure that at run time `x' is the RTX we want to test.  */
317           if (i != 0)
318             {
319               printf ("  x = ");
320               print_path (path);
321               printf (";\n");
322             }
323
324           printf ("  if (XWINT (x, %d) != ", i);
325           printf (HOST_WIDE_INT_PRINT_DEC, XWINT (x, i));
326           printf (") goto L%d;\n", fail_label);
327         }
328       else if (fmt[i] == 's')
329         {
330           /* Make sure that at run time `x' is the RTX we want to test.  */
331           if (i != 0)
332             {
333               printf ("  x = ");
334               print_path (path);
335               printf (";\n");
336             }
337
338           printf ("  if (strcmp (XSTR (x, %d), \"%s\")) goto L%d;\n",
339                   i, XSTR (x, i), fail_label);
340         }
341     }
342 }
343
344 /* Given a PATH, representing a path down the instruction's
345    pattern from the root to a certain point, output code to
346    evaluate to the rtx at that point.  */
347
348 static void
349 print_path (path)
350      struct link *path;
351 {
352   if (path == 0)
353     printf ("pat");
354   else if (path->vecelt >= 0)
355     {
356       printf ("XVECEXP (");
357       print_path (path->next);
358       printf (", %d, %d)", path->pos, path->vecelt);
359     }
360   else
361     {
362       printf ("XEXP (");
363       print_path (path->next);
364       printf (", %d)", path->pos);
365     }
366 }
367 \f
368 static void
369 print_code (code)
370      RTX_CODE code;
371 {
372   register const char *p1;
373   for (p1 = GET_RTX_NAME (code); *p1; p1++)
374     putchar (TOUPPER(*p1));
375 }
376 \f
377 PTR
378 xmalloc (size)
379   size_t size;
380 {
381   register PTR val = (PTR) malloc (size);
382
383   if (val == 0)
384     fatal ("virtual memory exhausted");
385   return val;
386 }
387
388 PTR
389 xrealloc (old, size)
390   PTR old;
391   size_t size;
392 {
393   register PTR ptr;
394   if (old)
395     ptr = (PTR) realloc (old, size);
396   else
397     ptr = (PTR) malloc (size);
398   if (!ptr)
399     fatal ("virtual memory exhausted");
400   return ptr;
401 }
402
403 extern int main PARAMS ((int, char **));
404
405 int
406 main (argc, argv)
407      int argc;
408      char **argv;
409 {
410   rtx desc;
411   FILE *infile;
412   register int c;
413
414   max_opno = -1;
415
416   progname = "genpeep";
417   obstack_init (rtl_obstack);
418
419   if (argc <= 1)
420     fatal ("No input file name.");
421
422   infile = fopen (argv[1], "r");
423   if (infile == 0)
424     {
425       perror (argv[1]);
426       return (FATAL_EXIT_CODE);
427     }
428   read_rtx_filename = argv[1];
429
430   printf ("/* Generated automatically by the program `genpeep'\n\
431 from the machine description file `md'.  */\n\n");
432
433   printf ("#include \"config.h\"\n");
434   printf ("#include \"system.h\"\n");
435   printf ("#include \"insn-config.h\"\n");
436   printf ("#include \"rtl.h\"\n");
437   printf ("#include \"tm_p.h\"\n");
438   printf ("#include \"regs.h\"\n");
439   printf ("#include \"output.h\"\n");
440   printf ("#include \"real.h\"\n");
441   printf ("#include \"recog.h\"\n");
442   printf ("#include \"except.h\"\n\n");
443   printf ("#include \"function.h\"\n\n");
444
445   printf ("#ifdef HAVE_peephole\n");
446   printf ("extern rtx peep_operand[];\n\n");
447   printf ("#define operands peep_operand\n\n");
448
449   printf ("rtx\npeephole (ins1)\n     rtx ins1;\n{\n");
450   printf ("  rtx insn ATTRIBUTE_UNUSED, x ATTRIBUTE_UNUSED, pat ATTRIBUTE_UNUSED;\n\n");
451
452   /* Early out: no peepholes for insns followed by barriers.  */
453   printf ("  if (NEXT_INSN (ins1)\n");
454   printf ("      && GET_CODE (NEXT_INSN (ins1)) == BARRIER)\n");
455   printf ("    return 0;\n\n");
456
457   /* Read the machine description.  */
458
459   while (1)
460     {
461       c = read_skip_spaces (infile);
462       if (c == EOF)
463         break;
464       ungetc (c, infile);
465
466       desc = read_rtx (infile);
467       if (GET_CODE (desc) == DEFINE_PEEPHOLE)
468         {
469           gen_peephole (desc);
470           insn_code_number++;
471         }
472       if (GET_CODE (desc) == DEFINE_INSN
473           || GET_CODE (desc) == DEFINE_EXPAND
474           || GET_CODE (desc) == DEFINE_SPLIT)
475         {
476           insn_code_number++;
477         }
478     }
479
480   printf ("  return 0;\n}\n\n");
481
482   if (max_opno == -1)
483     max_opno = 1;
484
485   printf ("rtx peep_operand[%d];\n", max_opno + 1);
486   printf ("#endif\n");
487
488   fflush (stdout);
489   return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
490 }
491
492 /* Define this so we can link with print-rtl.o to get debug_rtx function.  */
493 const char *
494 get_insn_name (code)
495      int code ATTRIBUTE_UNUSED;
496 {
497   return NULL;
498 }