home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / NeXT / GnuSource / cc-61.0.1 / cc / genpeep.c < prev    next >
C/C++ Source or Header  |  1991-06-03  |  12KB  |  486 lines

  1. /* Generate code from machine description to perform peephole optimizations.
  2.    Copyright (C) 1987, 1989 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, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <stdio.h>
  22. #include "config.h"
  23. #include "rtl.h"
  24. #include "obstack.h"
  25.  
  26. struct obstack obstack;
  27. struct obstack *rtl_obstack = &obstack;
  28.  
  29. #define obstack_chunk_alloc xmalloc
  30. #define obstack_chunk_free free
  31. extern int xmalloc ();
  32. extern void free ();
  33.  
  34. /* While tree-walking an instruction pattern, we keep a chain
  35.    of these `struct link's to record how to get down to the
  36.    current position.  In each one, POS is the operand number,
  37.    and if the operand is a vector VEC is the element number.
  38.    VEC is -1 if the operand is not a vector.  */
  39.  
  40. struct link
  41. {
  42.   struct link *next;
  43.   int pos;
  44.   int vecelt;
  45. };
  46.  
  47. void match_rtx ();
  48. void gen_exp ();
  49. void fatal ();
  50. void fancy_abort ();
  51.  
  52. int max_opno;
  53.  
  54. /* Number of operands used in current peephole definition.  */
  55.  
  56. int n_operands;
  57.  
  58. /* Peephole optimizations get insn codes just like insn patterns.
  59.    Count them so we know the code of the define_peephole we are handling.  */
  60.  
  61. int insn_code_number = 0;
  62.  
  63. void print_path ();
  64. void print_code ();
  65.  
  66. void
  67. gen_peephole (peep)
  68.      rtx peep;
  69. {
  70.   int ninsns = XVECLEN (peep, 0);
  71.   int i;
  72.  
  73.   n_operands = 0;
  74.  
  75.   printf ("  insn = ins1;\n");
  76. #if 0
  77.   printf ("  want_jump = 0;\n");
  78. #endif
  79.  
  80.   for (i = 0; i < ninsns; i++)
  81.     {
  82.       if (i > 0)
  83.     {
  84.       printf ("  do { insn = NEXT_INSN (insn);\n");
  85.       printf ("       if (insn == 0) goto L%d; }\n",
  86.           insn_code_number);
  87.       printf ("  while (GET_CODE (insn) == NOTE\n");
  88.       printf ("\t || (GET_CODE (insn) == INSN\n");
  89.       printf ("\t     && (GET_CODE (PATTERN (insn)) == USE\n");
  90.       printf ("\t\t || GET_CODE (PATTERN (insn)) == CLOBBER)));\n");
  91.  
  92.       printf ("  if (GET_CODE (insn) == CODE_LABEL\n\
  93.       || GET_CODE (insn) == BARRIER)\n    goto L%d;\n",
  94.             insn_code_number);
  95.     }
  96.  
  97. #if 0
  98.       printf ("  if (GET_CODE (insn) == JUMP_INSN)\n");
  99.       printf ("    want_jump = JUMP_LABEL (insn);\n");
  100. #endif
  101.  
  102.       printf ("  pat = PATTERN (insn);\n");
  103.  
  104.       /* Walk the insn's pattern, remembering at all times the path
  105.      down to the walking point.  */
  106.  
  107.       match_rtx (XVECEXP (peep, 0, i), 0, insn_code_number);
  108.     }
  109.  
  110.   /* We get this far if the pattern matches.
  111.      Now test the extra condition.  */
  112.  
  113.   if (XSTR (peep, 1) && XSTR (peep, 1)[0])
  114.     printf ("  if (! (%s)) goto L%d;\n",
  115.         XSTR (peep, 1), insn_code_number);
  116.  
  117.   /* If that matches, construct new pattern and put it in the first insn.
  118.      This new pattern will never be matched.
  119.      It exists only so that insn-extract can get the operands back.
  120.      So use a simple regular form: a PARALLEL containing a vector
  121.      of all the operands.  */
  122.  
  123.   printf ("  PATTERN (ins1) = gen_rtx (PARALLEL, VOIDmode, gen_rtvec_v (%d, operands));\n", n_operands);
  124.  
  125. #if 0
  126.   printf ("  if (want_jump && GET_CODE (ins1) != JUMP_INSN)\n");
  127.   printf ("    {\n");
  128.   printf ("      rtx insn2 = emit_jump_insn_before (PATTERN (ins1), ins1);\n");
  129.   printf ("      delete_insn (ins1);\n");
  130.   printf ("      ins1 = ins2;\n");
  131.   printf ("    }\n");
  132. #endif
  133.  
  134.   /* Record this define_peephole's insn code in the insn,
  135.      as if it had been recognized to match this.  */
  136.   printf ("  INSN_CODE (ins1) = %d;\n",
  137.       insn_code_number);
  138.  
  139.   /* Delete the remaining insns.  */
  140.   if (ninsns > 1)
  141.     printf ("  delete_for_peephole (NEXT_INSN (ins1), insn);\n");
  142.  
  143.   /* See reload1.c for insertion of NOTE which guarantees that this
  144.      cannot be zero.  */
  145.   printf ("  return NEXT_INSN (insn);\n");
  146.  
  147.   printf (" L%d:\n\n", insn_code_number);
  148. }
  149.  
  150. void
  151. match_rtx (x, path, fail_label)
  152.      rtx x;
  153.      struct link *path;
  154.      int fail_label;
  155. {
  156.   register RTX_CODE code;
  157.   register int i;
  158.   register int len;
  159.   register char *fmt;
  160.   struct link link;
  161.  
  162.   if (x == 0)
  163.     return;
  164.  
  165.  
  166.   code = GET_CODE (x);
  167.  
  168.   switch (code)
  169.     {
  170.     case MATCH_OPERAND:
  171.       if (XINT (x, 0) > max_opno)
  172.     max_opno = XINT (x, 0);
  173.       if (XINT (x, 0) >= n_operands)
  174.     n_operands = 1 + XINT (x, 0);
  175.  
  176.       printf ("  x = ");
  177.       print_path (path);
  178.       printf (";\n");
  179.  
  180.       printf ("  operands[%d] = x;\n", XINT (x, 0));
  181.       if (XSTR (x, 1) && XSTR (x, 1)[0])
  182.     printf ("  if (! %s (x, %smode)) goto L%d;\n",
  183.         XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label);
  184.       return;
  185.  
  186.     case MATCH_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.  
  265.   printf ("  x = ");
  266.   print_path (path);
  267.   printf (";\n");
  268.  
  269.   printf ("  if (GET_CODE (x) != ");
  270.   print_code (code);
  271.   printf (") goto L%d;\n", fail_label);
  272.  
  273.   if (GET_MODE (x) != VOIDmode)
  274.     {
  275.       printf ("  if (GET_MODE (x) != %smode) goto L%d;\n",
  276.           GET_MODE_NAME (GET_MODE (x)), fail_label);
  277.     }
  278.  
  279.   link.next = path;
  280.   link.vecelt = -1;
  281.   fmt = GET_RTX_FORMAT (code);
  282.   len = GET_RTX_LENGTH (code);
  283.   for (i = 0; i < len; i++)
  284.     {
  285.       link.pos = i;
  286.       if (fmt[i] == 'e' || fmt[i] == 'u')
  287.     match_rtx (XEXP (x, i), &link, fail_label);
  288.       else if (fmt[i] == 'E')
  289.     {
  290.       int j;
  291.       printf ("  if (XVECLEN (x, %d) != %d) goto L%d;\n",
  292.           i, XVECLEN (x, i), fail_label);
  293.       for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  294.         {
  295.           link.vecelt = j;
  296.           match_rtx (XVECEXP (x, i, j), &link, fail_label);
  297.         }
  298.     }
  299.       else if (fmt[i] == 'i')
  300.     {
  301.       /* Make sure that at run time `x' is the RTX we want to test.  */
  302.       if (i != 0)
  303.         {
  304.           printf ("  x = ");
  305.           print_path (path);
  306.           printf (";\n");
  307.         }
  308.  
  309.       printf ("  if (XINT (x, %d) != %d) goto L%d;\n",
  310.           i, XINT (x, i), fail_label);
  311.     }
  312.       else if (fmt[i] == 's')
  313.     {
  314.       /* Make sure that at run time `x' is the RTX we want to test.  */
  315.       if (i != 0)
  316.         {
  317.           printf ("  x = ");
  318.           print_path (path);
  319.           printf (";\n");
  320.         }
  321.  
  322.       printf ("  if (strcmp (XSTR (x, %d), \"%s\")) goto L%d;\n",
  323.           i, XSTR (x, i), fail_label);
  324.     }
  325.     }
  326. }
  327.  
  328. /* Given a PATH, representing a path down the instruction's
  329.    pattern from the root to a certain point, output code to
  330.    evaluate to the rtx at that point.  */
  331.  
  332. void
  333. print_path (path)
  334.      struct link *path;
  335. {
  336.   if (path == 0)
  337.     printf ("pat");
  338.   else if (path->vecelt >= 0)
  339.     {
  340.       printf ("XVECEXP (");
  341.       print_path (path->next);
  342.       printf (", %d, %d)", path->pos, path->vecelt);
  343.     }
  344.   else
  345.     {
  346.       printf ("XEXP (");
  347.       print_path (path->next);
  348.       printf (", %d)", path->pos);
  349.     }
  350. }
  351.  
  352. void
  353. print_code (code)
  354.      RTX_CODE code;
  355. {
  356.   register char *p1;
  357.   for (p1 = GET_RTX_NAME (code); *p1; p1++)
  358.     {
  359.       if (*p1 >= 'a' && *p1 <= 'z')
  360.     putchar (*p1 + 'A' - 'a');
  361.       else
  362.     putchar (*p1);
  363.     }
  364. }
  365.  
  366. int
  367. xmalloc (size)
  368. {
  369.   register int val = malloc (size);
  370.  
  371.   if (val == 0)
  372.     fatal ("virtual memory exhausted");
  373.   return val;
  374. }
  375.  
  376. int
  377. xrealloc (ptr, size)
  378.      char *ptr;
  379.      int size;
  380. {
  381.   int result = realloc (ptr, size);
  382.   if (!result)
  383.     fatal ("virtual memory exhausted");
  384.   return result;
  385. }
  386.  
  387. void
  388. fatal (s, a1, a2)
  389.      char *s;
  390. {
  391.   fprintf (stderr, "genpeep: ");
  392.   fprintf (stderr, s, a1, a2);
  393.   fprintf (stderr, "\n");
  394.   exit (FATAL_EXIT_CODE);
  395. }
  396.  
  397. /* More 'friendly' abort that prints the line and file.
  398.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  399.  
  400. void
  401. fancy_abort ()
  402. {
  403.   fatal ("Internal gcc abort.");
  404. }
  405.  
  406. int
  407. main (argc, argv)
  408.      int argc;
  409.      char **argv;
  410. {
  411.   rtx desc;
  412.   FILE *infile;
  413.   extern rtx read_rtx ();
  414.   register int c;
  415.  
  416.   max_opno = -1;
  417.  
  418.   obstack_init (rtl_obstack);
  419.  
  420.   if (argc <= 1)
  421.     fatal ("No input file name.");
  422.  
  423.   infile = fopen (argv[1], "r");
  424.   if (infile == 0)
  425.     {
  426.       perror (argv[1]);
  427.       exit (FATAL_EXIT_CODE);
  428.     }
  429.  
  430.   init_rtl ();
  431.  
  432.   printf ("/* Generated automatically by the program `genpeep'\n\
  433. from the machine description file `md'.  */\n\n");
  434.  
  435.   printf ("#include \"config.h\"\n");
  436.   printf ("#include \"rtl.h\"\n");
  437.   printf ("#include \"regs.h\"\n");
  438.   printf ("#include \"output.h\"\n");
  439.   printf ("#include \"real.h\"\n\n");
  440.  
  441.   printf ("extern rtx peep_operand[];\n\n");
  442.   printf ("#define operands peep_operand\n\n");
  443.  
  444.   printf ("rtx\npeephole (ins1)\n     rtx ins1;\n{\n");
  445.   printf ("  rtx insn, x, pat;\n");
  446.   printf ("  int i;\n\n");
  447.  
  448.   /* Early out: no peepholes for insns followed by barriers.  */
  449.   printf ("  if (NEXT_INSN (ins1)\n");
  450.   printf ("      && GET_CODE (NEXT_INSN (ins1)) == BARRIER)\n");
  451.   printf ("    return 0;\n\n");
  452.  
  453.   /* Read the machine description.  */
  454.  
  455.   while (1)
  456.     {
  457.       c = read_skip_spaces (infile);
  458.       if (c == EOF)
  459.     break;
  460.       ungetc (c, infile);
  461.  
  462.       desc = read_rtx (infile);
  463.       if (GET_CODE (desc) == DEFINE_PEEPHOLE)
  464.     {
  465.       gen_peephole (desc);
  466.       insn_code_number++;
  467.     }
  468.       if (GET_CODE (desc) == DEFINE_INSN
  469.       || GET_CODE (desc) == DEFINE_EXPAND
  470.       || GET_CODE (desc) == DEFINE_SPLIT)
  471.     {
  472.       insn_code_number++;
  473.     }
  474.     }
  475.  
  476.   printf ("  return 0;\n}\n\n");
  477.  
  478.   if (max_opno == -1)
  479.     max_opno = 1;
  480.  
  481.   printf ("rtx peep_operand[%d];\n", max_opno + 1);
  482.  
  483.   fflush (stdout);
  484.   exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
  485. }
  486.