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