OSDN Git Service

Sat Aug 15 20:22:33 1998 H.J. Lu (hjl@gnu.org)
[pf3gnuchains/gcc-fork.git] / gcc / stupid.c
1 /* Dummy data flow analysis for GNU compiler in nonoptimizing mode.
2    Copyright (C) 1987, 91, 94, 95, 96, 1997 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 /* This file performs stupid register allocation, which is used
23    when cc1 gets the -noreg switch (which is when cc does not get -O).
24
25    Stupid register allocation goes in place of the flow_analysis,
26    local_alloc and global_alloc passes.  combine_instructions cannot
27    be done with stupid allocation because the data flow info that it needs
28    is not computed here.
29
30    In stupid allocation, the only user-defined variables that can
31    go in registers are those declared "register".  They are assumed
32    to have a life span equal to their scope.  Other user variables
33    are given stack slots in the rtl-generation pass and are not
34    represented as pseudo regs.  A compiler-generated temporary
35    is assumed to live from its first mention to its last mention.
36
37    Since each pseudo-reg's life span is just an interval, it can be
38    represented as a pair of numbers, each of which identifies an insn by
39    its position in the function (number of insns before it).  The first
40    thing done for stupid allocation is to compute such a number for each
41    insn.  It is called the suid.  Then the life-interval of each
42    pseudo reg is computed.  Then the pseudo regs are ordered by priority
43    and assigned hard regs in priority order.  */
44
45 #include "config.h"
46 #include "system.h"
47
48 #include "rtl.h"
49 #include "hard-reg-set.h"
50 #include "regs.h"
51 #include "flags.h"
52 #include "toplev.h"
53 \f
54 /* Vector mapping INSN_UIDs to suids.
55    The suids are like uids but increase monotonically always.
56    We use them to see whether a subroutine call came
57    between a variable's birth and its death.  */
58
59 static int *uid_suid;
60
61 /* Get the suid of an insn.  */
62
63 #define INSN_SUID(INSN) (uid_suid[INSN_UID (INSN)])
64
65 /* Record the suid of the last CALL_INSN
66    so we can tell whether a pseudo reg crosses any calls.  */
67
68 static int last_call_suid;
69
70 /* Record the suid of the last NOTE_INSN_SETJMP
71    so we can tell whether a pseudo reg crosses any setjmp.  */
72
73 static int last_setjmp_suid;
74
75 /* Element N is suid of insn where life span of pseudo reg N ends.
76    Element is  0 if register N has not been seen yet on backward scan.  */
77
78 static int *reg_where_dead;
79
80 /* Element N is suid of insn where life span of pseudo reg N begins.  */
81
82 static int *reg_where_born;
83
84 /* Numbers of pseudo-regs to be allocated, highest priority first.  */
85
86 static int *reg_order;
87
88 /* Indexed by reg number (hard or pseudo), nonzero if register is live
89    at the current point in the instruction stream.  */
90
91 static char *regs_live;
92
93 /* Indexed by reg number, nonzero if reg was used in a SUBREG that changes
94    its size.  */
95
96 static char *regs_change_size;
97
98 /* Indexed by reg number, nonzero if reg crosses a setjmp.  */
99
100 static char *regs_crosses_setjmp;
101
102 /* Indexed by insn's suid, the set of hard regs live after that insn.  */
103
104 static HARD_REG_SET *after_insn_hard_regs;
105
106 /* Record that hard reg REGNO is live after insn INSN.  */
107
108 #define MARK_LIVE_AFTER(INSN,REGNO)  \
109   SET_HARD_REG_BIT (after_insn_hard_regs[INSN_SUID (INSN)], (REGNO))
110
111 static int stupid_reg_compare   PROTO((const GENERIC_PTR,const GENERIC_PTR));
112 static int stupid_find_reg      PROTO((int, enum reg_class, enum machine_mode,
113                                        int, int, int));
114 static void stupid_mark_refs    PROTO((rtx, rtx));
115 \f
116 /* Stupid life analysis is for the case where only variables declared
117    `register' go in registers.  For this case, we mark all
118    pseudo-registers that belong to register variables as
119    dying in the last instruction of the function, and all other
120    pseudo registers as dying in the last place they are referenced.
121    Hard registers are marked as dying in the last reference before
122    the end or before each store into them.  */
123
124 void
125 stupid_life_analysis (f, nregs, file)
126      rtx f;
127      int nregs;
128      FILE *file;
129 {
130   register int i;
131   register rtx last, insn;
132   int max_uid, max_suid;
133
134   current_function_has_computed_jump = 0;
135
136   bzero (regs_ever_live, sizeof regs_ever_live);
137
138   regs_live = (char *) alloca (nregs);
139
140   /* First find the last real insn, and count the number of insns,
141      and assign insns their suids.  */
142
143   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
144     if (INSN_UID (insn) > i)
145       i = INSN_UID (insn);
146
147   max_uid = i + 1;
148   uid_suid = (int *) alloca ((i + 1) * sizeof (int));
149
150   /* Compute the mapping from uids to suids.
151      Suids are numbers assigned to insns, like uids,
152      except that suids increase monotonically through the code.  */
153
154   last = 0;                     /* In case of empty function body */
155   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
156     {
157       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
158         last = insn;
159
160       INSN_SUID (insn) = ++i;
161     }
162
163   last_call_suid = i + 1;
164   last_setjmp_suid = i + 1;
165   max_suid = i + 1;
166
167   max_regno = nregs;
168
169   /* Allocate tables to record info about regs.  */
170
171   reg_where_dead = (int *) alloca (nregs * sizeof (int));
172   bzero ((char *) reg_where_dead, nregs * sizeof (int));
173
174   reg_where_born = (int *) alloca (nregs * sizeof (int));
175   bzero ((char *) reg_where_born, nregs * sizeof (int));
176
177   reg_order = (int *) alloca (nregs * sizeof (int));
178   bzero ((char *) reg_order, nregs * sizeof (int));
179
180   regs_change_size = (char *) alloca (nregs * sizeof (char));
181   bzero ((char *) regs_change_size, nregs * sizeof (char));
182
183   regs_crosses_setjmp = (char *) alloca (nregs * sizeof (char));
184   bzero ((char *) regs_crosses_setjmp, nregs * sizeof (char));
185
186   /* Allocate the reg_renumber array */
187   allocate_reg_info (max_regno, FALSE, TRUE);
188   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
189     reg_renumber[i] = i;
190
191   after_insn_hard_regs
192     = (HARD_REG_SET *) alloca (max_suid * sizeof (HARD_REG_SET));
193
194   bzero ((char *) after_insn_hard_regs, max_suid * sizeof (HARD_REG_SET));
195
196   /* Allocate and zero out many data structures
197      that will record the data from lifetime analysis.  */
198
199   allocate_for_life_analysis ();
200
201   for (i = 0; i < max_regno; i++)
202     REG_N_DEATHS (i) = 1;
203
204   bzero (regs_live, nregs);
205
206   /* Find where each pseudo register is born and dies,
207      by scanning all insns from the end to the start
208      and noting all mentions of the registers.
209
210      Also find where each hard register is live
211      and record that info in after_insn_hard_regs.
212      regs_live[I] is 1 if hard reg I is live
213      at the current point in the scan.  */
214
215   for (insn = last; insn; insn = PREV_INSN (insn))
216     {
217       register HARD_REG_SET *p = after_insn_hard_regs + INSN_SUID (insn);
218
219       /* Copy the info in regs_live into the element of after_insn_hard_regs
220          for the current position in the rtl code.  */
221
222       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
223         if (regs_live[i])
224           SET_HARD_REG_BIT (*p, i);
225
226       /* Update which hard regs are currently live
227          and also the birth and death suids of pseudo regs
228          based on the pattern of this insn.  */
229
230       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
231         stupid_mark_refs (PATTERN (insn), insn);
232
233       if (GET_CODE (insn) == NOTE
234           && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
235         last_setjmp_suid = INSN_SUID (insn);
236
237       /* Mark all call-clobbered regs as dead after each call insn so that
238          a pseudo whose life span includes this insn will not go in one of
239          them.  If the function contains a non-local goto, mark all hard
240          registers dead (except for stack related bits).
241
242          Then mark those regs as all dead for the continuing scan
243          of the insns before the call.  */
244
245       if (GET_CODE (insn) == CALL_INSN)
246         {
247           last_call_suid = INSN_SUID (insn);
248
249           if (current_function_has_nonlocal_label)
250             {
251               IOR_COMPL_HARD_REG_SET (after_insn_hard_regs[last_call_suid],
252                                       fixed_reg_set);
253               for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
254                 if (! fixed_regs[i])
255                   regs_live[i] = 0;
256             }
257           else
258             {
259               IOR_HARD_REG_SET (after_insn_hard_regs[last_call_suid],
260                                 call_used_reg_set);
261               for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
262                 if (call_used_regs[i])
263                   regs_live[i] = 0;
264             }
265
266           /* It is important that this be done after processing the insn's
267              pattern because we want the function result register to still
268              be live if it's also used to pass arguments.  */
269           stupid_mark_refs (CALL_INSN_FUNCTION_USAGE (insn), insn);
270         }
271       if (GET_CODE (insn) == JUMP_INSN && computed_jump_p (insn))
272         current_function_has_computed_jump = 1;
273     }
274
275   /* Now decide the order in which to allocate the pseudo registers.  */
276
277   for (i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
278     reg_order[i] = i;
279
280   qsort (&reg_order[LAST_VIRTUAL_REGISTER + 1],
281          max_regno - LAST_VIRTUAL_REGISTER - 1, sizeof (int),
282          stupid_reg_compare);
283
284   /* Now, in that order, try to find hard registers for those pseudo regs.  */
285
286   for (i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
287     {
288       register int r = reg_order[i];
289
290       /* Some regnos disappear from the rtl.  Ignore them to avoid crash. 
291          Also don't allocate registers that cross a setjmp, or live across
292          a call if this function receives a nonlocal goto.  */
293       if (regno_reg_rtx[r] == 0 || regs_crosses_setjmp[r]
294           || (REG_N_CALLS_CROSSED (r) > 0 
295               && current_function_has_nonlocal_label))
296         continue;
297
298       /* Now find the best hard-register class for this pseudo register */
299       if (N_REG_CLASSES > 1)
300         reg_renumber[r] = stupid_find_reg (REG_N_CALLS_CROSSED (r), 
301                                            reg_preferred_class (r),
302                                            PSEUDO_REGNO_MODE (r),
303                                            reg_where_born[r],
304                                            reg_where_dead[r],
305                                            regs_change_size[r]);
306
307       /* If no reg available in that class, try alternate class.  */
308       if (reg_renumber[r] == -1 && reg_alternate_class (r) != NO_REGS)
309         reg_renumber[r] = stupid_find_reg (REG_N_CALLS_CROSSED (r),
310                                            reg_alternate_class (r),
311                                            PSEUDO_REGNO_MODE (r),
312                                            reg_where_born[r],
313                                            reg_where_dead[r],
314                                            regs_change_size[r]);
315     }
316
317   if (file)
318     dump_flow_info (file);
319 }
320
321 /* Comparison function for qsort.
322    Returns -1 (1) if register *R1P is higher priority than *R2P.  */
323
324 static int
325 stupid_reg_compare (r1p, r2p)
326      const GENERIC_PTR r1p;
327      const GENERIC_PTR r2p;
328 {
329   register int r1 = *(int *)r1p, r2 = *(int *)r2p;
330   register int len1 = reg_where_dead[r1] - reg_where_born[r1];
331   register int len2 = reg_where_dead[r2] - reg_where_born[r2];
332   int tem;
333
334   tem = len2 - len1;
335   if (tem != 0)
336     return tem;
337
338   tem = REG_N_REFS (r1) - REG_N_REFS (r2);
339   if (tem != 0)
340     return tem;
341
342   /* If regs are equally good, sort by regno,
343      so that the results of qsort leave nothing to chance.  */
344   return r1 - r2;
345 }
346 \f
347 /* Find a block of SIZE words of hard registers in reg_class CLASS
348    that can hold a value of machine-mode MODE
349      (but actually we test only the first of the block for holding MODE)
350    currently free from after insn whose suid is BORN_INSN
351    through the insn whose suid is DEAD_INSN,
352    and return the number of the first of them.
353    Return -1 if such a block cannot be found.
354
355    If CALL_PRESERVED is nonzero, insist on registers preserved
356    over subroutine calls, and return -1 if cannot find such.
357
358    If CHANGES_SIZE is nonzero, it means this register was used as the
359    operand of a SUBREG that changes its size.  */
360
361 static int
362 stupid_find_reg (call_preserved, class, mode,
363                  born_insn, dead_insn, changes_size)
364      int call_preserved;
365      enum reg_class class;
366      enum machine_mode mode;
367      int born_insn, dead_insn;
368      int changes_size;
369 {
370   register int i, ins;
371 #ifdef HARD_REG_SET
372   register              /* Declare them register if they are scalars.  */
373 #endif
374     HARD_REG_SET used, this_reg;
375 #ifdef ELIMINABLE_REGS
376   static struct {int from, to; } eliminables[] = ELIMINABLE_REGS;
377 #endif
378
379   /* If this register's life is more than 5,000 insns, we probably
380      can't allocate it, so don't waste the time trying.  This avoids
381      quadratic behavior on programs that have regularly-occurring
382      SAVE_EXPRs.  */
383   if (dead_insn > born_insn + 5000)
384     return -1;
385
386   COPY_HARD_REG_SET (used,
387                      call_preserved ? call_used_reg_set : fixed_reg_set);
388
389 #ifdef ELIMINABLE_REGS
390   for (i = 0; i < sizeof eliminables / sizeof eliminables[0]; i++)
391     SET_HARD_REG_BIT (used, eliminables[i].from);
392 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
393   SET_HARD_REG_BIT (used, HARD_FRAME_POINTER_REGNUM);
394 #endif
395 #else
396   SET_HARD_REG_BIT (used, FRAME_POINTER_REGNUM);
397 #endif
398
399   for (ins = born_insn; ins < dead_insn; ins++)
400     IOR_HARD_REG_SET (used, after_insn_hard_regs[ins]);
401
402 #ifdef STACK_REGS
403   if (current_function_has_computed_jump)
404     for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
405       SET_HARD_REG_BIT (used, i);
406 #endif
407   
408   IOR_COMPL_HARD_REG_SET (used, reg_class_contents[(int) class]);
409
410 #ifdef CLASS_CANNOT_CHANGE_SIZE
411   if (changes_size)
412     IOR_HARD_REG_SET (used,
413                       reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE]);
414 #endif
415
416   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
417     {
418 #ifdef REG_ALLOC_ORDER
419       int regno = reg_alloc_order[i];
420 #else
421       int regno = i;
422 #endif
423
424       /* If a register has screwy overlap problems,
425          don't use it at all if not optimizing.
426          Actually this is only for the 387 stack register,
427          and it's because subsequent code won't work.  */
428 #ifdef OVERLAPPING_REGNO_P
429       if (OVERLAPPING_REGNO_P (regno))
430         continue;
431 #endif
432
433       if (! TEST_HARD_REG_BIT (used, regno)
434           && HARD_REGNO_MODE_OK (regno, mode))
435         {
436           register int j;
437           register int size1 = HARD_REGNO_NREGS (regno, mode);
438           for (j = 1; j < size1 && ! TEST_HARD_REG_BIT (used, regno + j); j++);
439           if (j == size1)
440             {
441               CLEAR_HARD_REG_SET (this_reg);
442               while (--j >= 0)
443                 SET_HARD_REG_BIT (this_reg, regno + j);
444               for (ins = born_insn; ins < dead_insn; ins++)
445                 {
446                   IOR_HARD_REG_SET (after_insn_hard_regs[ins], this_reg);
447                 }
448               return regno;
449             }
450 #ifndef REG_ALLOC_ORDER
451           i += j;               /* Skip starting points we know will lose */
452 #endif
453         }
454     }
455
456   return -1;
457 }
458 \f
459 /* Walk X, noting all assignments and references to registers
460    and recording what they imply about life spans.
461    INSN is the current insn, supplied so we can find its suid.  */
462
463 static void
464 stupid_mark_refs (x, insn)
465      rtx x, insn;
466 {
467   register RTX_CODE code;
468   register char *fmt;
469   register int regno, i;
470
471   if (x == 0)
472     return;
473
474   code = GET_CODE (x);
475
476   if (code == SET || code == CLOBBER)
477     {
478       if (SET_DEST (x) != 0
479           && (GET_CODE (SET_DEST (x)) == REG
480               || (GET_CODE (SET_DEST (x)) == SUBREG
481                   && GET_CODE (SUBREG_REG (SET_DEST (x))) == REG
482                   && (REGNO (SUBREG_REG (SET_DEST (x)))
483                       >= FIRST_PSEUDO_REGISTER))))
484         {
485           /* Register is being assigned.  */
486           /* If setting a SUBREG, we treat the entire reg as being set.  */
487           if (GET_CODE (SET_DEST (x)) == SUBREG)
488             regno = REGNO (SUBREG_REG (SET_DEST (x)));
489           else
490             regno = REGNO (SET_DEST (x));
491
492           /* For hard regs, update the where-live info.  */
493           if (regno < FIRST_PSEUDO_REGISTER)
494             {
495               register int j
496                 = HARD_REGNO_NREGS (regno, GET_MODE (SET_DEST (x)));
497
498               while (--j >= 0)
499                 {
500                   regs_ever_live[regno+j] = 1;
501                   regs_live[regno+j] = 0;
502
503                   /* The following line is for unused outputs;
504                      they do get stored even though never used again.  */
505                   MARK_LIVE_AFTER (insn, regno+j);
506
507                   /* When a hard reg is clobbered, mark it in use
508                      just before this insn, so it is live all through.  */
509                   if (code == CLOBBER && INSN_SUID (insn) > 0)
510                     SET_HARD_REG_BIT (after_insn_hard_regs[INSN_SUID (insn) - 1],
511                                       regno+j);
512                 }
513             }
514           /* For pseudo regs, record where born, where dead, number of
515              times used, and whether live across a call.  */
516           else
517             {
518               /* Update the life-interval bounds of this pseudo reg.  */
519
520               /* When a pseudo-reg is CLOBBERed, it is born just before
521                  the clobbering insn.  When setting, just after.  */
522               int where_born = INSN_SUID (insn) - (code == CLOBBER);
523
524               reg_where_born[regno] = where_born;
525
526               /* The reg must live at least one insn even
527                  in it is never again used--because it has to go
528                  in SOME hard reg.  Mark it as dying after the current
529                  insn so that it will conflict with any other outputs of
530                  this insn.  */
531               if (reg_where_dead[regno] < where_born + 2)
532                 {
533                   reg_where_dead[regno] = where_born + 2;
534                   regs_live[regno] = 1;
535                 }
536
537               /* Count the refs of this reg.  */
538               REG_N_REFS (regno)++;
539
540               if (last_call_suid < reg_where_dead[regno])
541                 REG_N_CALLS_CROSSED (regno) += 1;
542
543               if (last_setjmp_suid < reg_where_dead[regno])
544                 regs_crosses_setjmp[regno] = 1;
545
546               /* If this register is only used in this insn and is only
547                  set, mark it unused.  We have to do this even when not 
548                  optimizing so that MD patterns which count on this
549                  behavior (e.g., it not causing an output reload on
550                  an insn setting CC) will operate correctly.  */
551               if (GET_CODE (SET_DEST (x)) == REG
552                   && REGNO_FIRST_UID (regno) == INSN_UID (insn)
553                   && REGNO_LAST_UID (regno) == INSN_UID (insn)
554                   && (code == CLOBBER || ! reg_mentioned_p (SET_DEST (x),
555                                                             SET_SRC (x))))
556                 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_UNUSED,
557                                                       SET_DEST (x),
558                                                       REG_NOTES (insn));
559             }
560         }
561
562       /* Record references from the value being set,
563          or from addresses in the place being set if that's not a reg.
564          If setting a SUBREG, we treat the entire reg as *used*.  */
565       if (code == SET)
566         {
567           stupid_mark_refs (SET_SRC (x), insn);
568           if (GET_CODE (SET_DEST (x)) != REG)
569             stupid_mark_refs (SET_DEST (x), insn);
570         }
571       return;
572     }
573
574   else if (code == SUBREG
575            && GET_CODE (SUBREG_REG (x)) == REG
576            && REGNO (SUBREG_REG (x)) >= FIRST_PSEUDO_REGISTER
577            && (GET_MODE_SIZE (GET_MODE (x))
578                != GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
579            && (INTEGRAL_MODE_P (GET_MODE (x))
580                || INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (x)))))
581     regs_change_size[REGNO (SUBREG_REG (x))] = 1;
582
583   /* Register value being used, not set.  */
584
585   else if (code == REG)
586     {
587       regno = REGNO (x);
588       if (regno < FIRST_PSEUDO_REGISTER)
589         {
590           /* Hard reg: mark it live for continuing scan of previous insns.  */
591           register int j = HARD_REGNO_NREGS (regno, GET_MODE (x));
592           while (--j >= 0)
593             {
594               regs_ever_live[regno+j] = 1;
595               regs_live[regno+j] = 1;
596             }
597         }
598       else
599         {
600           /* Pseudo reg: record first use, last use and number of uses.  */
601
602           reg_where_born[regno] = INSN_SUID (insn);
603           REG_N_REFS (regno)++;
604           if (regs_live[regno] == 0)
605             {
606               regs_live[regno] = 1;
607               reg_where_dead[regno] = INSN_SUID (insn);
608             }
609         }
610       return;
611     }
612
613   /* Recursive scan of all other rtx's.  */
614
615   fmt = GET_RTX_FORMAT (code);
616   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
617     {
618       if (fmt[i] == 'e')
619         stupid_mark_refs (XEXP (x, i), insn);
620       if (fmt[i] == 'E')
621         {
622           register int j;
623           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
624             stupid_mark_refs (XVECEXP (x, i, j), insn);
625         }
626     }
627 }