OSDN Git Service

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