home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__src3 / stupid.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-23  |  14.0 KB  |  472 lines

  1. /* Dummy data flow analysis for GNU compiler in nonoptimizing mode.
  2.    Copyright (C) 1987 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 1, 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. /* This file performs stupid register allocation, which is used
  22.    when cc1 gets the -noreg switch (which is when cc does not get -O).
  23.  
  24.    Stupid register allocation goes in place of the the flow_analysis,
  25.    local_alloc and global_alloc passes.  combine_instructions cannot
  26.    be done with stupid allocation because the data flow info that it needs
  27.    is not computed here.
  28.  
  29.    In stupid allocation, the only user-defined variables that can
  30.    go in registers are those declared "register".  They are assumed
  31.    to have a life span equal to their scope.  Other user variables
  32.    are given stack slots in the rtl-generation pass and are not
  33.    represented as pseudo regs.  A compiler-generated temporary
  34.    is assumed to live from its first mention to its last mention.
  35.  
  36.    Since each pseudo-reg's life span is just an interval, it can be
  37.    represented as a pair of numbers, each of which identifies an insn by
  38.    its position in the function (number of insns before it).  The first
  39.    thing done for stupid allocation is to compute such a number for each
  40.    insn.  It is called the suid.  Then the life-interval of each
  41.    pseudo reg is computed.  Then the pseudo regs are ordered by priority
  42.    and assigned hard regs in priority order.  */
  43.  
  44. #include <stdio.h>
  45. #include "config.h"
  46. #include "rtl.h"
  47. #include "hard-reg-set.h"
  48. #include "regs.h"
  49.  
  50. /* Vector mapping INSN_UIDs to suids.
  51.    The suids are like uids but increase monononically always.
  52.    We use them to see whether a subroutine call came
  53.    between a variable's birth and its death.  */
  54.  
  55. static short *uid_suid;
  56.  
  57. /* Get the suid of an insn.  */
  58.  
  59. #define INSN_SUID(INSN) (uid_suid[INSN_UID (INSN)])
  60.  
  61. /* Record the suid of the last CALL_INSN
  62.    so we can tell whether a potential combination crosses any calls.  */
  63.  
  64. static int last_call_suid;
  65.  
  66. /* Element N is suid of insn where life span of pseudo reg N ends.
  67.    Element is  0 if register N has not been seen yet on backward scan.  */
  68.  
  69. static short *reg_where_dead;
  70.  
  71. /* Element N is suid of insn where life span of pseudo reg N begins.  */
  72.  
  73. static short *reg_where_born;
  74.  
  75. /* Numbers of pseudo-regs to be allocated, highest priority first.  */
  76.  
  77. static short *reg_order;
  78.  
  79. /* Indexed by reg number (hard or pseudo), nonzero if register is live
  80.    at the current point in the instruction stream.  */
  81.  
  82. static char *regs_live;
  83.  
  84. /* Indexed by insn's suid, the set of hard regs live after that insn.  */
  85.  
  86. static HARD_REG_SET *after_insn_hard_regs;
  87.  
  88. /* Record that hard reg REGNO is live after insn INSN.  */
  89.  
  90. #define MARK_LIVE_AFTER(INSN,REGNO)  \
  91.   SET_HARD_REG_BIT (after_insn_hard_regs[INSN_SUID (INSN)], (REGNO))
  92.  
  93. static void stupid_mark_refs ();
  94. static int stupid_reg_compare ();
  95. static int stupid_find_reg ();
  96.  
  97. /* Stupid life analysis is for the case where only variables declared
  98.    `register' go in registers.  For this case, we mark all
  99.    pseudo-registers that belong to register variables as
  100.    dying in the last instruction of the function, and all other
  101.    pseudo registers as dying in the last place they are referenced.
  102.    Hard registers are marked as dying in the last reference before
  103.    the end or before each store into them.  */
  104.  
  105. void
  106. stupid_life_analysis (f, nregs, file)
  107.      rtx f;
  108.      int nregs;
  109.      FILE *file;
  110. {
  111.   register int i;
  112.   register rtx last, insn;
  113.   int max_uid;
  114.  
  115.   bzero (regs_ever_live, sizeof regs_ever_live);
  116.  
  117.   regs_live = (char *) alloca (nregs);
  118.  
  119.   /* First find the last real insn, and count the number of insns,
  120.      and assign insns their suids.  */
  121.  
  122.   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  123.     if (INSN_UID (insn) > i)
  124.       i = INSN_UID (insn);
  125.  
  126.   max_uid = i + 1;
  127.   uid_suid = (short *) alloca ((i + 1) * sizeof (short));
  128.  
  129.   /* Compute the mapping from uids to suids.
  130.      Suids are numbers assigned to insns, like uids,
  131.      except that suids increase monotonically through the code.  */
  132.  
  133.   last = 0;            /* In case of empty function body */
  134.   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  135.     {
  136.       if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN
  137.       || GET_CODE (insn) == JUMP_INSN)
  138.     last = insn;
  139.       INSN_SUID (insn) = ++i;
  140.     }
  141.  
  142.   last_call_suid = i + 1;
  143.  
  144.   max_regno = nregs;
  145.  
  146.   /* Allocate tables to record info about regs.  */
  147.  
  148.   reg_where_dead = (short *) alloca (nregs * sizeof (short));
  149.   bzero (reg_where_dead, nregs * sizeof (short));
  150.  
  151.   reg_where_born = (short *) alloca (nregs * sizeof (short));
  152.   bzero (reg_where_born, nregs * sizeof (short));
  153.  
  154.   reg_order = (short *) alloca (nregs * sizeof (short));
  155.   bzero (reg_order, nregs * sizeof (short));
  156.  
  157.   reg_renumber = (short *) oballoc (nregs * sizeof (short));
  158.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  159.     reg_renumber[i] = i;
  160.  
  161.   after_insn_hard_regs = (HARD_REG_SET *) alloca (max_uid * sizeof (HARD_REG_SET));
  162.   bzero (after_insn_hard_regs, max_uid * sizeof (HARD_REG_SET));
  163.  
  164.   /* Allocate and zero out many data structures
  165.      that will record the data from lifetime analysis.  */
  166.  
  167.   allocate_for_life_analysis ();
  168.  
  169.   for (i = 0; i < max_regno; i++)
  170.     {
  171.       reg_n_deaths[i] = 1;
  172.     }
  173.  
  174.   bzero (regs_live, nregs);
  175.  
  176.   /* Find where each pseudo register is born and dies,
  177.      by scanning all insns from the end to the start
  178.      and noting all mentions of the registers.
  179.  
  180.      Also find where each hard register is live
  181.      and record that info in after_insn_hard_regs.
  182.      regs_live[I] is 1 if hard reg I is live
  183.      at the current point in the scan.  */
  184.  
  185.   for (insn = last; insn; insn = PREV_INSN (insn))
  186.     {
  187.       register HARD_REG_SET *p = after_insn_hard_regs + INSN_SUID (insn);
  188.  
  189.       /* Copy the info in regs_live
  190.      into the element of after_insn_hard_regs
  191.      for the current position in the rtl code.  */
  192.  
  193.       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  194.     if (regs_live[i])
  195.       SET_HARD_REG_BIT (*p, i);
  196.  
  197.       /* Mark all call-clobbered regs as live after each call insn
  198.      so that a pseudo whose life span includes this insn
  199.      will not go in one of them.
  200.      Then mark those regs as all dead for the continuing scan
  201.      of the insns before the call.  */
  202.  
  203.       if (GET_CODE (insn) == CALL_INSN)
  204.     {
  205.       last_call_suid = INSN_SUID (insn);
  206.       IOR_HARD_REG_SET (after_insn_hard_regs[last_call_suid],
  207.                 call_used_reg_set);
  208.       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  209.         if (call_used_regs[i])
  210.           regs_live[i] = 0;
  211.     }
  212.  
  213.       /* Update which hard regs are currently live
  214.      and also the birth and death suids of pseudo regs
  215.      based on the pattern of this insn.  */
  216.  
  217.       if (GET_CODE (insn) == INSN
  218.       || GET_CODE (insn) == CALL_INSN
  219.       || GET_CODE (insn) == JUMP_INSN)
  220.     {
  221.       stupid_mark_refs (PATTERN (insn), insn);
  222.     }
  223.     }
  224.  
  225.   /* Now decide the order in which to allocate the pseudo registers.  */
  226.  
  227.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  228.     reg_order[i] = i;
  229.  
  230.   qsort (®_order[FIRST_PSEUDO_REGISTER],
  231.      max_regno - FIRST_PSEUDO_REGISTER, sizeof (short),
  232.      stupid_reg_compare);
  233.  
  234.   /* Now, in that order, try to find hard registers for those pseudo regs.  */
  235.  
  236.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  237.     {
  238.       register int r = reg_order[i];
  239.       enum reg_class class;
  240.  
  241.       /* Some regnos disappear from the rtl.  Ignore them to avoid crash.  */
  242.       if (regno_reg_rtx[r] == 0)
  243.     continue;
  244.  
  245.       /* Now find the best hard-register class for this pseudo register */
  246.       if (N_REG_CLASSES > 1)
  247.     {
  248.       class = reg_preferred_class (r);
  249.  
  250.       reg_renumber[r] = stupid_find_reg (reg_n_calls_crossed[r], class,
  251.                          PSEUDO_REGNO_MODE (r),
  252.                          reg_where_born[r],
  253.                          reg_where_dead[r]);
  254.     }
  255.       else
  256.     reg_renumber[r] = -1;
  257.  
  258.       /* If no reg available in that class,
  259.      try any reg.  */
  260.       if (reg_renumber[r] == -1)
  261.     reg_renumber[r] = stupid_find_reg (reg_n_calls_crossed[r],
  262.                        GENERAL_REGS,
  263.                        PSEUDO_REGNO_MODE (r),
  264.                        reg_where_born[r],
  265.                        reg_where_dead[r]);
  266.     }
  267.  
  268.   if (file)
  269.     dump_flow_info (file);
  270. }
  271.  
  272. /* Comparison function for qsort.
  273.    Returns -1 (1) if register *R1P is higher priority than *R2P.  */
  274.  
  275. static int
  276. stupid_reg_compare (r1p, r2p)
  277.      short *r1p, *r2p;
  278. {
  279.   register int r1 = *r1p, r2 = *r2p;
  280.   register int len1 = reg_where_dead[r1] - reg_where_born[r1];
  281.   register int len2 = reg_where_dead[r2] - reg_where_born[r2];
  282.  
  283.   if (len1 != len2)
  284.     return len2 - len1;
  285.  
  286.   return reg_n_refs[r1] - reg_n_refs[r2];
  287. }
  288.  
  289. /* Find a block of SIZE words of hard registers in reg_class CLASS
  290.    that can hold a value of machine-mode MODE
  291.      (but actually we test only the first of the block for holding MODE)
  292.    currently free from after insn whose suid is BIRTH
  293.    through the insn whose suid is DEATH,
  294.    and return the number of the first of them.
  295.    Return -1 if such a block cannot be found.
  296.    If CALL_PRESERVED is nonzero, insist on registers preserved
  297.    over subroutine calls, and return -1 if cannot find such.  */
  298.  
  299. static int
  300. stupid_find_reg (call_preserved, class, mode,
  301.          born_insn, dead_insn)
  302.      int call_preserved;
  303.      enum reg_class class;
  304.      enum machine_mode mode;
  305.      int born_insn, dead_insn;
  306. {
  307.   register int i, ins;
  308. #ifdef HARD_REG_SET
  309.   register        /* Declare them register if they are scalars.  */
  310. #endif
  311.     HARD_REG_SET used, this_reg;
  312.  
  313.   COPY_HARD_REG_SET (used,
  314.              call_preserved ? call_used_reg_set : fixed_reg_set);
  315.  
  316.   for (ins = born_insn; ins < dead_insn; ins++)
  317.     IOR_HARD_REG_SET (used, after_insn_hard_regs[ins]);
  318.  
  319.   IOR_COMPL_HARD_REG_SET (used, reg_class_contents[(int) class]);
  320.  
  321.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  322.     {
  323. #ifdef REG_ALLOC_ORDER
  324.       int regno = reg_alloc_order[i];
  325. #else
  326.       int regno = i;
  327. #endif
  328.       if (! TEST_HARD_REG_BIT (used, regno)
  329.       && HARD_REGNO_MODE_OK (regno, mode))
  330.     {
  331.       register int j;
  332.       register int size1 = HARD_REGNO_NREGS (regno, mode);
  333.       for (j = 1; j < size1 && ! TEST_HARD_REG_BIT (used, regno + j); j++);
  334.       if (j == size1)
  335.         {
  336.           CLEAR_HARD_REG_SET (this_reg);
  337.           while (--j >= 0)
  338.         SET_HARD_REG_BIT (this_reg, regno + j);
  339.           for (ins = born_insn; ins < dead_insn; ins++)
  340.         {
  341.           IOR_HARD_REG_SET (after_insn_hard_regs[ins], this_reg);
  342.         }
  343.           return regno;
  344.         }
  345. #ifndef REG_ALLOC_ORDER
  346.       i += j;            /* Skip starting points we know will lose */
  347. #endif
  348.     }
  349.     }
  350.   return -1;
  351. }
  352.  
  353. /* Walk X, noting all assignments and references to registers
  354.    and recording what they imply about life spans.
  355.    INSN is the current insn, supplied so we can find its suid.  */
  356.  
  357. static void
  358. stupid_mark_refs (x, insn)
  359.      rtx x, insn;
  360. {
  361.   register RTX_CODE code = GET_CODE (x);
  362.   register char *fmt;
  363.   register int regno, i;
  364.  
  365.   if (code == SET || code == CLOBBER)
  366.     {
  367.       if (SET_DEST (x) != 0 && GET_CODE (SET_DEST (x)) == REG)
  368.     {
  369.       /* Register is being assigned.  */
  370.       regno = REGNO (SET_DEST (x));
  371.  
  372.       /* For hard regs, update the where-live info.  */
  373.       if (regno < FIRST_PSEUDO_REGISTER)
  374.         {
  375.           register int j
  376.         = HARD_REGNO_NREGS (regno, GET_MODE (SET_DEST (x)));
  377.           while (--j >= 0)
  378.         {
  379.           regs_ever_live[regno+j] = 1;
  380.           regs_live[regno+j] = 0;
  381.           /* The following line is for unused outputs;
  382.              they do get stored even though never used again.  */
  383.           MARK_LIVE_AFTER (insn, regno);
  384.           /* When a hard reg is clobbered, mark it in use
  385.              just before this insn, so it is live all through.  */
  386.           if (code == CLOBBER)
  387.             SET_HARD_REG_BIT (after_insn_hard_regs[INSN_SUID (insn)],
  388.                       regno);
  389.         }
  390.         }
  391.       /* For pseudo regs, record where born, where dead, number of
  392.          times used, and whether live across a call.  */
  393.       else
  394.         {
  395.           /* Update the life-interval bounds of this pseudo reg.  */
  396.  
  397.           /* When a pseudo-reg is CLOBBERed, it is born just before
  398.          the clobbering insn.  When setting, just after.  */
  399.           int where_born = INSN_SUID (insn) - (code == CLOBBER);
  400.  
  401.           reg_where_born[regno] = where_born;
  402.           /* The reg must live at least one insn even
  403.          in it is never again used--because it has to go
  404.          in SOME hard reg.  */
  405.           if (reg_where_dead[regno] < where_born + 1)
  406.         reg_where_dead[regno] = where_born + 1;
  407.  
  408.           /* Count the refs of this reg.  */
  409.           reg_n_refs[regno]++;
  410.  
  411.           if (last_call_suid < reg_where_dead[regno])
  412.         reg_n_calls_crossed[regno] += 1;
  413.         }
  414.     }
  415.       /* Record references from the value being set,
  416.      or from addresses in the place being set if that's not a reg.
  417.      If setting a SUBREG, we treat the entire reg as *used*.  */
  418.       if (code == SET)
  419.     {
  420.       stupid_mark_refs (SET_SRC (x), insn);
  421.       if (GET_CODE (SET_DEST (x)) != REG)
  422.         stupid_mark_refs (SET_DEST (x), insn);
  423.     }
  424.       return;
  425.     }
  426.  
  427.   /* Register value being used, not set.  */
  428.  
  429.   if (code == REG)
  430.     {
  431.       regno = REGNO (x);
  432.       if (regno < FIRST_PSEUDO_REGISTER)
  433.     {
  434.       /* Hard reg: mark it live for continuing scan of previous insns.  */
  435.       register int j = HARD_REGNO_NREGS (regno, GET_MODE (x));
  436.       while (--j >= 0)
  437.         {
  438.           regs_ever_live[regno+j] = 1;
  439.           regs_live[regno+j] = 1;
  440.         }
  441.     }
  442.       else
  443.     {
  444.       /* Pseudo reg: record first use, last use and number of uses.  */
  445.  
  446.       reg_where_born[regno] = INSN_SUID (insn);
  447.       reg_n_refs[regno]++;
  448.       if (regs_live[regno] == 0)
  449.         {
  450.           regs_live[regno] = 1;
  451.           reg_where_dead[regno] = INSN_SUID (insn);
  452.         }
  453.     }
  454.       return;
  455.     }
  456.  
  457.   /* Recursive scan of all other rtx's.  */
  458.  
  459.   fmt = GET_RTX_FORMAT (code);
  460.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  461.     {
  462.       if (fmt[i] == 'e')
  463.     stupid_mark_refs (XEXP (x, i), insn);
  464.       if (fmt[i] == 'E')
  465.     {
  466.       register int j;
  467.       for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  468.         stupid_mark_refs (XVECEXP (x, i, j), insn);
  469.     }
  470.     }
  471. }
  472.