source: trunk/third/gcc/flow.c @ 11288

Revision 11288, 91.2 KB checked in by ghudson, 26 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r11287, which included commits to RCS files with non-trunk default branches.
Line 
1/* Data flow analysis for GNU compiler.
2   Copyright (C) 1987, 88, 92-96, 1997 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/* This file contains the data flow analysis pass of the compiler.
23   It computes data flow information
24   which tells combine_instructions which insns to consider combining
25   and controls register allocation.
26
27   Additional data flow information that is too bulky to record
28   is generated during the analysis, and is used at that time to
29   create autoincrement and autodecrement addressing.
30
31   The first step is dividing the function into basic blocks.
32   find_basic_blocks does this.  Then life_analysis determines
33   where each register is live and where it is dead.
34
35   ** find_basic_blocks **
36
37   find_basic_blocks divides the current function's rtl
38   into basic blocks.  It records the beginnings and ends of the
39   basic blocks in the vectors basic_block_head and basic_block_end,
40   and the number of blocks in n_basic_blocks.
41
42   find_basic_blocks also finds any unreachable loops
43   and deletes them.
44
45   ** life_analysis **
46
47   life_analysis is called immediately after find_basic_blocks.
48   It uses the basic block information to determine where each
49   hard or pseudo register is live.
50
51   ** live-register info **
52
53   The information about where each register is live is in two parts:
54   the REG_NOTES of insns, and the vector basic_block_live_at_start.
55
56   basic_block_live_at_start has an element for each basic block,
57   and the element is a bit-vector with a bit for each hard or pseudo
58   register.  The bit is 1 if the register is live at the beginning
59   of the basic block.
60
61   Two types of elements can be added to an insn's REG_NOTES. 
62   A REG_DEAD note is added to an insn's REG_NOTES for any register
63   that meets both of two conditions:  The value in the register is not
64   needed in subsequent insns and the insn does not replace the value in
65   the register (in the case of multi-word hard registers, the value in
66   each register must be replaced by the insn to avoid a REG_DEAD note).
67
68   In the vast majority of cases, an object in a REG_DEAD note will be
69   used somewhere in the insn.  The (rare) exception to this is if an
70   insn uses a multi-word hard register and only some of the registers are
71   needed in subsequent insns.  In that case, REG_DEAD notes will be
72   provided for those hard registers that are not subsequently needed.
73   Partial REG_DEAD notes of this type do not occur when an insn sets
74   only some of the hard registers used in such a multi-word operand;
75   omitting REG_DEAD notes for objects stored in an insn is optional and
76   the desire to do so does not justify the complexity of the partial
77   REG_DEAD notes.
78
79   REG_UNUSED notes are added for each register that is set by the insn
80   but is unused subsequently (if every register set by the insn is unused
81   and the insn does not reference memory or have some other side-effect,
82   the insn is deleted instead).  If only part of a multi-word hard
83   register is used in a subsequent insn, REG_UNUSED notes are made for
84   the parts that will not be used.
85
86   To determine which registers are live after any insn, one can
87   start from the beginning of the basic block and scan insns, noting
88   which registers are set by each insn and which die there.
89
90   ** Other actions of life_analysis **
91
92   life_analysis sets up the LOG_LINKS fields of insns because the
93   information needed to do so is readily available.
94
95   life_analysis deletes insns whose only effect is to store a value
96   that is never used.
97
98   life_analysis notices cases where a reference to a register as
99   a memory address can be combined with a preceding or following
100   incrementation or decrementation of the register.  The separate
101   instruction to increment or decrement is deleted and the address
102   is changed to a POST_INC or similar rtx.
103
104   Each time an incrementing or decrementing address is created,
105   a REG_INC element is added to the insn's REG_NOTES list.
106
107   life_analysis fills in certain vectors containing information about
108   register usage: reg_n_refs, reg_n_deaths, reg_n_sets, reg_live_length,
109   reg_n_calls_crosses and reg_basic_block.  */
110
111#include "config.h"
112#include <stdio.h>
113#include "rtl.h"
114#include "basic-block.h"
115#include "insn-config.h"
116#include "regs.h"
117#include "hard-reg-set.h"
118#include "flags.h"
119#include "output.h"
120#include "except.h"
121
122#include "obstack.h"
123#define obstack_chunk_alloc xmalloc
124#define obstack_chunk_free free
125
126/* The contents of the current function definition are allocated
127   in this obstack, and all are freed at the end of the function.
128   For top-level functions, this is temporary_obstack.
129   Separate obstacks are made for nested functions.  */
130
131extern struct obstack *function_obstack;
132
133/* List of labels that must never be deleted.  */
134extern rtx forced_labels;
135
136/* Get the basic block number of an insn.
137   This info should not be expected to remain available
138   after the end of life_analysis.  */
139
140/* This is the limit of the allocated space in the following two arrays.  */
141
142static int max_uid_for_flow;
143
144#define BLOCK_NUM(INSN)  uid_block_number[INSN_UID (INSN)]
145
146/* This is where the BLOCK_NUM values are really stored.
147   This is set up by find_basic_blocks and used there and in life_analysis,
148   and then freed.  */
149
150static int *uid_block_number;
151
152/* INSN_VOLATILE (insn) is 1 if the insn refers to anything volatile.  */
153
154#define INSN_VOLATILE(INSN) uid_volatile[INSN_UID (INSN)]
155static char *uid_volatile;
156
157/* Number of basic blocks in the current function.  */
158
159int n_basic_blocks;
160
161/* Maximum register number used in this function, plus one.  */
162
163int max_regno;
164
165/* Maximum number of SCRATCH rtx's used in any basic block of this
166   function.  */
167
168int max_scratch;
169
170/* Number of SCRATCH rtx's in the current block.  */
171
172static int num_scratch;
173
174/* Indexed by n, giving various register information */
175
176reg_info *reg_n_info;
177
178/* Element N is the next insn that uses (hard or pseudo) register number N
179   within the current basic block; or zero, if there is no such insn.
180   This is valid only during the final backward scan in propagate_block.  */
181
182static rtx *reg_next_use;
183
184/* Size of a regset for the current function,
185   in (1) bytes and (2) elements.  */
186
187int regset_bytes;
188int regset_size;
189
190/* Element N is first insn in basic block N.
191   This info lasts until we finish compiling the function.  */
192
193rtx *basic_block_head;
194
195/* Element N is last insn in basic block N.
196   This info lasts until we finish compiling the function.  */
197
198rtx *basic_block_end;
199
200/* Element N is a regset describing the registers live
201   at the start of basic block N.
202   This info lasts until we finish compiling the function.  */
203
204regset *basic_block_live_at_start;
205
206/* Regset of regs live when calls to `setjmp'-like functions happen.  */
207
208regset regs_live_at_setjmp;
209
210/* List made of EXPR_LIST rtx's which gives pairs of pseudo registers
211   that have to go in the same hard reg.
212   The first two regs in the list are a pair, and the next two
213   are another pair, etc.  */
214rtx regs_may_share;
215
216/* Element N is nonzero if control can drop into basic block N
217   from the preceding basic block.  Freed after life_analysis.  */
218
219static char *basic_block_drops_in;
220
221/* Element N is depth within loops of the last insn in basic block number N.
222   Freed after life_analysis.  */
223
224static short *basic_block_loop_depth;
225
226/* Element N nonzero if basic block N can actually be reached.
227   Vector exists only during find_basic_blocks.  */
228
229static char *block_live_static;
230
231/* Depth within loops of basic block being scanned for lifetime analysis,
232   plus one.  This is the weight attached to references to registers.  */
233
234static int loop_depth;
235
236/* During propagate_block, this is non-zero if the value of CC0 is live.  */
237
238static int cc0_live;
239
240/* During propagate_block, this contains the last MEM stored into.  It
241   is used to eliminate consecutive stores to the same location.  */
242
243static rtx last_mem_set;
244
245/* Set of registers that may be eliminable.  These are handled specially
246   in updating regs_ever_live.  */
247
248static HARD_REG_SET elim_reg_set;
249
250/* Forward declarations */
251static void find_basic_blocks           PROTO((rtx, rtx));
252static void mark_label_ref              PROTO((rtx, rtx, int));
253static void life_analysis               PROTO((rtx, int));
254void allocate_for_life_analysis         PROTO((void));
255void init_regset_vector                 PROTO((regset *, int, struct obstack *));
256void free_regset_vector                 PROTO((regset *, int));
257static void propagate_block             PROTO((regset, rtx, rtx, int,
258                                               regset, int));
259static rtx flow_delete_insn             PROTO((rtx));
260static int insn_dead_p                  PROTO((rtx, regset, int));
261static int libcall_dead_p               PROTO((rtx, regset, rtx, rtx));
262static void mark_set_regs               PROTO((regset, regset, rtx,
263                                               rtx, regset));
264static void mark_set_1                  PROTO((regset, regset, rtx,
265                                               rtx, regset));
266static void find_auto_inc               PROTO((regset, rtx, rtx));
267static void mark_used_regs              PROTO((regset, regset, rtx, int, rtx));
268static int try_pre_increment_1          PROTO((rtx));
269static int try_pre_increment            PROTO((rtx, rtx, HOST_WIDE_INT));
270static rtx find_use_as_address          PROTO((rtx, rtx, HOST_WIDE_INT));
271void dump_flow_info                     PROTO((FILE *));
272
273/* Find basic blocks of the current function and perform data flow analysis.
274   F is the first insn of the function and NREGS the number of register numbers
275   in use.  */
276
277void
278flow_analysis (f, nregs, file)
279     rtx f;
280     int nregs;
281     FILE *file;
282{
283  register rtx insn;
284  register int i;
285  rtx nonlocal_label_list = nonlocal_label_rtx_list ();
286
287#ifdef ELIMINABLE_REGS
288  static struct {int from, to; } eliminables[] = ELIMINABLE_REGS;
289#endif
290
291  /* Record which registers will be eliminated.  We use this in
292     mark_used_regs.  */
293
294  CLEAR_HARD_REG_SET (elim_reg_set);
295
296#ifdef ELIMINABLE_REGS
297  for (i = 0; i < sizeof eliminables / sizeof eliminables[0]; i++)
298    SET_HARD_REG_BIT (elim_reg_set, eliminables[i].from);
299#else
300  SET_HARD_REG_BIT (elim_reg_set, FRAME_POINTER_REGNUM);
301#endif
302
303  /* Count the basic blocks.  Also find maximum insn uid value used.  */
304
305  {
306    register RTX_CODE prev_code = JUMP_INSN;
307    register RTX_CODE code;
308
309    max_uid_for_flow = 0;
310
311    for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
312      {
313        code = GET_CODE (insn);
314        if (INSN_UID (insn) > max_uid_for_flow)
315          max_uid_for_flow = INSN_UID (insn);
316        if (code == CODE_LABEL
317            || (GET_RTX_CLASS (code) == 'i'
318                && (prev_code == JUMP_INSN
319                    || (prev_code == CALL_INSN
320                        && nonlocal_label_list != 0)
321                    || prev_code == BARRIER)))
322          i++;
323
324        if (code == CALL_INSN && find_reg_note (insn, REG_RETVAL, NULL_RTX))
325          code = INSN;
326
327        if (code != NOTE)
328          prev_code = code;
329      }
330  }
331
332#ifdef AUTO_INC_DEC
333  /* Leave space for insns we make in some cases for auto-inc.  These cases
334     are rare, so we don't need too much space.  */
335  max_uid_for_flow += max_uid_for_flow / 10;
336#endif
337
338  /* Allocate some tables that last till end of compiling this function
339     and some needed only in find_basic_blocks and life_analysis.  */
340
341  n_basic_blocks = i;
342  basic_block_head = (rtx *) oballoc (n_basic_blocks * sizeof (rtx));
343  basic_block_end = (rtx *) oballoc (n_basic_blocks * sizeof (rtx));
344  basic_block_drops_in = (char *) alloca (n_basic_blocks);
345  basic_block_loop_depth = (short *) alloca (n_basic_blocks * sizeof (short));
346  uid_block_number
347    = (int *) alloca ((max_uid_for_flow + 1) * sizeof (int));
348  uid_volatile = (char *) alloca (max_uid_for_flow + 1);
349  bzero (uid_volatile, max_uid_for_flow + 1);
350
351  find_basic_blocks (f, nonlocal_label_list);
352  life_analysis (f, nregs);
353  if (file)
354    dump_flow_info (file);
355
356  basic_block_drops_in = 0;
357  uid_block_number = 0;
358  basic_block_loop_depth = 0;
359}
360
361/* Find all basic blocks of the function whose first insn is F.
362   Store the correct data in the tables that describe the basic blocks,
363   set up the chains of references for each CODE_LABEL, and
364   delete any entire basic blocks that cannot be reached.
365
366   NONLOCAL_LABEL_LIST is the same local variable from flow_analysis.  */
367
368static void
369find_basic_blocks (f, nonlocal_label_list)
370     rtx f, nonlocal_label_list;
371{
372  register rtx insn;
373  register int i;
374  register char *block_live = (char *) alloca (n_basic_blocks);
375  register char *block_marked = (char *) alloca (n_basic_blocks);
376  /* List of label_refs to all labels whose addresses are taken
377     and used as data.  */
378  rtx label_value_list;
379  int label_value_list_marked_live;
380  rtx x, note;
381  enum rtx_code prev_code, code;
382  int depth, pass;
383
384  pass = 1;
385 restart:
386
387  label_value_list = 0;
388  label_value_list_marked_live = 0;
389  block_live_static = block_live;
390  bzero (block_live, n_basic_blocks);
391  bzero (block_marked, n_basic_blocks);
392
393  /* Initialize with just block 0 reachable and no blocks marked.  */
394  if (n_basic_blocks > 0)
395    block_live[0] = 1;
396
397  /* Initialize the ref chain of each label to 0.  Record where all the
398     blocks start and end and their depth in loops.  For each insn, record
399     the block it is in.   Also mark as reachable any blocks headed by labels
400     that must not be deleted.  */
401
402  for (insn = f, i = -1, prev_code = JUMP_INSN, depth = 1;
403       insn; insn = NEXT_INSN (insn))
404    {
405      code = GET_CODE (insn);
406      if (code == NOTE)
407        {
408          if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
409            depth++;
410          else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
411            depth--;
412        }
413
414      /* A basic block starts at label, or after something that can jump.  */
415      else if (code == CODE_LABEL
416               || (GET_RTX_CLASS (code) == 'i'
417                   && (prev_code == JUMP_INSN
418                       || (prev_code == CALL_INSN
419                           && nonlocal_label_list != 0
420                           && ! find_reg_note (insn, REG_RETVAL, NULL_RTX))
421                       || prev_code == BARRIER)))
422        {
423          basic_block_head[++i] = insn;
424          basic_block_end[i] = insn;
425          basic_block_loop_depth[i] = depth;
426
427          if (code == CODE_LABEL)
428            {
429                LABEL_REFS (insn) = insn;
430                /* Any label that cannot be deleted
431                   is considered to start a reachable block.  */
432                if (LABEL_PRESERVE_P (insn))
433                  block_live[i] = 1;
434              }
435        }
436
437      else if (GET_RTX_CLASS (code) == 'i')
438        {
439          basic_block_end[i] = insn;
440          basic_block_loop_depth[i] = depth;
441        }
442
443      if (GET_RTX_CLASS (code) == 'i')
444        {
445          /* Make a list of all labels referred to other than by jumps.  */
446          for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
447            if (REG_NOTE_KIND (note) == REG_LABEL)
448              label_value_list = gen_rtx (EXPR_LIST, VOIDmode, XEXP (note, 0),
449                                          label_value_list);
450        }
451
452      BLOCK_NUM (insn) = i;
453
454      if (code != NOTE)
455        prev_code = code;
456    }
457
458  /* During the second pass, `n_basic_blocks' is only an upper bound.
459     Only perform the sanity check for the first pass, and on the second
460     pass ensure `n_basic_blocks' is set to the correct value.  */
461  if (pass == 1 && i + 1 != n_basic_blocks)
462    abort ();
463  n_basic_blocks = i + 1;
464
465  for (x = forced_labels; x; x = XEXP (x, 1))
466    if (! LABEL_REF_NONLOCAL_P (x))
467      block_live[BLOCK_NUM (XEXP (x, 0))] = 1;
468
469  for (x = exception_handler_labels; x; x = XEXP (x, 1))
470    block_live[BLOCK_NUM (XEXP (x, 0))] = 1;
471
472  /* Record which basic blocks control can drop in to.  */
473
474  for (i = 0; i < n_basic_blocks; i++)
475    {
476      for (insn = PREV_INSN (basic_block_head[i]);
477           insn && GET_CODE (insn) == NOTE; insn = PREV_INSN (insn))
478        ;
479
480      basic_block_drops_in[i] = insn && GET_CODE (insn) != BARRIER;
481    }
482
483  /* Now find which basic blocks can actually be reached
484     and put all jump insns' LABEL_REFS onto the ref-chains
485     of their target labels.  */
486
487  if (n_basic_blocks > 0)
488    {
489      int something_marked = 1;
490      int deleted;
491
492      /* Find all indirect jump insns and mark them as possibly jumping to all
493         the labels whose addresses are explicitly used.  This is because,
494         when there are computed gotos, we can't tell which labels they jump
495         to, of all the possibilities.  */
496
497      for (insn = f; insn; insn = NEXT_INSN (insn))
498        if (computed_jump_p (insn))
499          {
500            if (label_value_list_marked_live == 0)
501              {
502                label_value_list_marked_live = 1;
503
504                /* This could be made smarter by only considering
505                   these live, if the computed goto is live.  */
506
507                /* Don't delete the labels (in this function) that
508                   are referenced by non-jump instructions.  */
509
510                for (x = label_value_list; x; x = XEXP (x, 1))
511                  if (! LABEL_REF_NONLOCAL_P (x))
512                    block_live[BLOCK_NUM (XEXP (x, 0))] = 1;
513              }
514
515            for (x = label_value_list; x; x = XEXP (x, 1))
516              mark_label_ref (gen_rtx (LABEL_REF, VOIDmode, XEXP (x, 0)),
517                              insn, 0);
518
519            for (x = forced_labels; x; x = XEXP (x, 1))
520              mark_label_ref (gen_rtx (LABEL_REF, VOIDmode, XEXP (x, 0)),
521                              insn, 0);
522          }
523
524      /* Find all call insns and mark them as possibly jumping
525         to all the nonlocal goto handler labels.  */
526
527      for (insn = f; insn; insn = NEXT_INSN (insn))
528        if (GET_CODE (insn) == CALL_INSN
529            && ! find_reg_note (insn, REG_RETVAL, NULL_RTX))
530          {
531            for (x = nonlocal_label_list; x; x = XEXP (x, 1))
532              mark_label_ref (gen_rtx (LABEL_REF, VOIDmode, XEXP (x, 0)),
533                              insn, 0);
534
535            /* ??? This could be made smarter:
536               in some cases it's possible to tell that certain
537               calls will not do a nonlocal goto.
538
539               For example, if the nested functions that do the
540               nonlocal gotos do not have their addresses taken, then
541               only calls to those functions or to other nested
542               functions that use them could possibly do nonlocal
543               gotos.  */
544          }
545
546      /* All blocks associated with labels in label_value_list are
547         trivially considered as marked live, if the list is empty.
548         We do this to speed up the below code.  */
549
550      if (label_value_list == 0)
551        label_value_list_marked_live = 1;
552
553      /* Pass over all blocks, marking each block that is reachable
554         and has not yet been marked.
555         Keep doing this until, in one pass, no blocks have been marked.
556         Then blocks_live and blocks_marked are identical and correct.
557         In addition, all jumps actually reachable have been marked.  */
558
559      while (something_marked)
560        {
561          something_marked = 0;
562          for (i = 0; i < n_basic_blocks; i++)
563            if (block_live[i] && !block_marked[i])
564              {
565                block_marked[i] = 1;
566                something_marked = 1;
567                if (i + 1 < n_basic_blocks && basic_block_drops_in[i + 1])
568                  block_live[i + 1] = 1;
569                insn = basic_block_end[i];
570                if (GET_CODE (insn) == JUMP_INSN)
571                  mark_label_ref (PATTERN (insn), insn, 0);
572
573                if (label_value_list_marked_live == 0)
574                  /* Now that we know that this block is live, mark as
575                     live, all the blocks that we might be able to get
576                     to as live.  */
577
578                  for (insn = basic_block_head[i];
579                       insn != NEXT_INSN (basic_block_end[i]);
580                       insn = NEXT_INSN (insn))
581                    {
582                      if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
583                        {
584                          for (note = REG_NOTES (insn);
585                               note;
586                               note = XEXP (note, 1))
587                            if (REG_NOTE_KIND (note) == REG_LABEL)
588                              {
589                                x = XEXP (note, 0);
590                                block_live[BLOCK_NUM (x)] = 1;
591                              }
592                        }
593                    }
594              }
595        }
596
597      /* ??? See if we have a "live" basic block that is not reachable.
598         This can happen if it is headed by a label that is preserved or
599         in one of the label lists, but no call or computed jump is in
600         the loop.  It's not clear if we can delete the block or not,
601         but don't for now.  However, we will mess up register status if
602         it remains unreachable, so add a fake reachability from the
603         previous block.  */
604
605      for (i = 1; i < n_basic_blocks; i++)
606        if (block_live[i] && ! basic_block_drops_in[i]
607            && GET_CODE (basic_block_head[i]) == CODE_LABEL
608            && LABEL_REFS (basic_block_head[i]) == basic_block_head[i])
609          basic_block_drops_in[i] = 1;
610
611      /* Now delete the code for any basic blocks that can't be reached.
612         They can occur because jump_optimize does not recognize
613         unreachable loops as unreachable.  */
614
615      deleted = 0;
616      for (i = 0; i < n_basic_blocks; i++)
617        if (!block_live[i])
618          {
619            deleted++;
620
621            /* Delete the insns in a (non-live) block.  We physically delete
622               every non-note insn except the start and end (so
623               basic_block_head/end needn't be updated), we turn the latter
624               into NOTE_INSN_DELETED notes.
625               We use to "delete" the insns by turning them into notes, but
626               we may be deleting lots of insns that subsequent passes would
627               otherwise have to process.  Secondly, lots of deleted blocks in
628               a row can really slow down propagate_block since it will
629               otherwise process insn-turned-notes multiple times when it
630               looks for loop begin/end notes.  */
631            if (basic_block_head[i] != basic_block_end[i])
632              {
633                /* It would be quicker to delete all of these with a single
634                   unchaining, rather than one at a time, but we need to keep
635                   the NOTE's.  */
636                insn = NEXT_INSN (basic_block_head[i]);
637                while (insn != basic_block_end[i])
638                  {
639                    if (GET_CODE (insn) == BARRIER)
640                      abort ();
641                    else if (GET_CODE (insn) != NOTE)
642                      insn = flow_delete_insn (insn);
643                    else
644                      insn = NEXT_INSN (insn);
645                  }
646              }
647            insn = basic_block_head[i];
648            if (GET_CODE (insn) != NOTE)
649              {
650                /* Turn the head into a deleted insn note.  */
651                if (GET_CODE (insn) == BARRIER)
652                  abort ();
653                PUT_CODE (insn, NOTE);
654                NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
655                NOTE_SOURCE_FILE (insn) = 0;
656              }
657            insn = basic_block_end[i];
658            if (GET_CODE (insn) != NOTE)
659              {
660                /* Turn the tail into a deleted insn note.  */
661                if (GET_CODE (insn) == BARRIER)
662                  abort ();
663                PUT_CODE (insn, NOTE);
664                NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
665                NOTE_SOURCE_FILE (insn) = 0;
666              }
667            /* BARRIERs are between basic blocks, not part of one.
668               Delete a BARRIER if the preceding jump is deleted.
669               We cannot alter a BARRIER into a NOTE
670               because it is too short; but we can really delete
671               it because it is not part of a basic block.  */
672            if (NEXT_INSN (insn) != 0
673                && GET_CODE (NEXT_INSN (insn)) == BARRIER)
674              delete_insn (NEXT_INSN (insn));
675
676            /* Each time we delete some basic blocks,
677               see if there is a jump around them that is
678               being turned into a no-op.  If so, delete it.  */
679
680            if (block_live[i - 1])
681              {
682                register int j;
683                for (j = i + 1; j < n_basic_blocks; j++)
684                  if (block_live[j])
685                    {
686                      rtx label;
687                      insn = basic_block_end[i - 1];
688                      if (GET_CODE (insn) == JUMP_INSN
689                          /* An unconditional jump is the only possibility
690                             we must check for, since a conditional one
691                             would make these blocks live.  */
692                          && simplejump_p (insn)
693                          && (label = XEXP (SET_SRC (PATTERN (insn)), 0), 1)
694                          && INSN_UID (label) != 0
695                          && BLOCK_NUM (label) == j)
696                        {
697                          PUT_CODE (insn, NOTE);
698                          NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
699                          NOTE_SOURCE_FILE (insn) = 0;
700                          if (GET_CODE (NEXT_INSN (insn)) != BARRIER)
701                            abort ();
702                          delete_insn (NEXT_INSN (insn));
703                        }
704                      break;
705                    }
706              }
707          }
708
709      /* There are pathological cases where one function calling hundreds of
710         nested inline functions can generate lots and lots of unreachable
711         blocks that jump can't delete.  Since we don't use sparse matrices
712         a lot of memory will be needed to compile such functions.
713         Implementing sparse matrices is a fair bit of work and it is not
714         clear that they win more than they lose (we don't want to
715         unnecessarily slow down compilation of normal code).  By making
716         another pass for the pathological case, we can greatly speed up
717         their compilation without hurting normal code.  This works because
718         all the insns in the unreachable blocks have either been deleted or
719         turned into notes.
720         Note that we're talking about reducing memory usage by 10's of
721         megabytes and reducing compilation time by several minutes.  */
722      /* ??? The choice of when to make another pass is a bit arbitrary,
723         and was derived from empirical data.  */
724      if (pass == 1
725          && deleted > 200)
726        {
727          pass++;
728          n_basic_blocks -= deleted;
729          /* `n_basic_blocks' may not be correct at this point: two previously
730             separate blocks may now be merged.  That's ok though as we
731             recalculate it during the second pass.  It certainly can't be
732             any larger than the current value.  */
733          goto restart;
734        }
735    }
736}
737
738/* Subroutines of find_basic_blocks.  */
739
740/* Check expression X for label references;
741   if one is found, add INSN to the label's chain of references.
742
743   CHECKDUP means check for and avoid creating duplicate references
744   from the same insn.  Such duplicates do no serious harm but
745   can slow life analysis.  CHECKDUP is set only when duplicates
746   are likely.  */
747
748static void
749mark_label_ref (x, insn, checkdup)
750     rtx x, insn;
751     int checkdup;
752{
753  register RTX_CODE code;
754  register int i;
755  register char *fmt;
756
757  /* We can be called with NULL when scanning label_value_list.  */
758  if (x == 0)
759    return;
760
761  code = GET_CODE (x);
762  if (code == LABEL_REF)
763    {
764      register rtx label = XEXP (x, 0);
765      register rtx y;
766      if (GET_CODE (label) != CODE_LABEL)
767        abort ();
768      /* If the label was never emitted, this insn is junk,
769         but avoid a crash trying to refer to BLOCK_NUM (label).
770         This can happen as a result of a syntax error
771         and a diagnostic has already been printed.  */
772      if (INSN_UID (label) == 0)
773        return;
774      CONTAINING_INSN (x) = insn;
775      /* if CHECKDUP is set, check for duplicate ref from same insn
776         and don't insert.  */
777      if (checkdup)
778        for (y = LABEL_REFS (label); y != label; y = LABEL_NEXTREF (y))
779          if (CONTAINING_INSN (y) == insn)
780            return;
781      LABEL_NEXTREF (x) = LABEL_REFS (label);
782      LABEL_REFS (label) = x;
783      block_live_static[BLOCK_NUM (label)] = 1;
784      return;
785    }
786
787  fmt = GET_RTX_FORMAT (code);
788  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
789    {
790      if (fmt[i] == 'e')
791        mark_label_ref (XEXP (x, i), insn, 0);
792      if (fmt[i] == 'E')
793        {
794          register int j;
795          for (j = 0; j < XVECLEN (x, i); j++)
796            mark_label_ref (XVECEXP (x, i, j), insn, 1);
797        }
798    }
799}
800
801/* Delete INSN by patching it out.
802   Return the next insn.  */
803
804static rtx
805flow_delete_insn (insn)
806     rtx insn;
807{
808  /* ??? For the moment we assume we don't have to watch for NULLs here
809     since the start/end of basic blocks aren't deleted like this.  */
810  NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
811  PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
812  return NEXT_INSN (insn);
813}
814
815/* Determine which registers are live at the start of each
816   basic block of the function whose first insn is F.
817   NREGS is the number of registers used in F.
818   We allocate the vector basic_block_live_at_start
819   and the regsets that it points to, and fill them with the data.
820   regset_size and regset_bytes are also set here.  */
821
822static void
823life_analysis (f, nregs)
824     rtx f;
825     int nregs;
826{
827  int first_pass;
828  int changed;
829  /* For each basic block, a bitmask of regs
830     live on exit from the block.  */
831  regset *basic_block_live_at_end;
832  /* For each basic block, a bitmask of regs
833     live on entry to a successor-block of this block.
834     If this does not match basic_block_live_at_end,
835     that must be updated, and the block must be rescanned.  */
836  regset *basic_block_new_live_at_end;
837  /* For each basic block, a bitmask of regs
838     whose liveness at the end of the basic block
839     can make a difference in which regs are live on entry to the block.
840     These are the regs that are set within the basic block,
841     possibly excluding those that are used after they are set.  */
842  regset *basic_block_significant;
843  register int i;
844  rtx insn;
845
846  struct obstack flow_obstack;
847
848  gcc_obstack_init (&flow_obstack);
849
850  max_regno = nregs;
851
852  bzero (regs_ever_live, sizeof regs_ever_live);
853
854  /* Allocate and zero out many data structures
855     that will record the data from lifetime analysis.  */
856
857  allocate_for_life_analysis ();
858
859  reg_next_use = (rtx *) alloca (nregs * sizeof (rtx));
860  bzero ((char *) reg_next_use, nregs * sizeof (rtx));
861
862  /* Set up several regset-vectors used internally within this function.
863     Their meanings are documented above, with their declarations.  */
864
865  basic_block_live_at_end
866    = (regset *) alloca (n_basic_blocks * sizeof (regset));
867
868  /* Don't use alloca since that leads to a crash rather than an error message
869     if there isn't enough space.
870     Don't use oballoc since we may need to allocate other things during
871     this function on the temporary obstack.  */
872  init_regset_vector (basic_block_live_at_end, n_basic_blocks, &flow_obstack);
873
874  basic_block_new_live_at_end
875    = (regset *) alloca (n_basic_blocks * sizeof (regset));
876  init_regset_vector (basic_block_new_live_at_end, n_basic_blocks,
877                      &flow_obstack);
878
879  basic_block_significant
880    = (regset *) alloca (n_basic_blocks * sizeof (regset));
881  init_regset_vector (basic_block_significant, n_basic_blocks, &flow_obstack);
882
883  /* Record which insns refer to any volatile memory
884     or for any reason can't be deleted just because they are dead stores.
885     Also, delete any insns that copy a register to itself.  */
886
887  for (insn = f; insn; insn = NEXT_INSN (insn))
888    {
889      enum rtx_code code1 = GET_CODE (insn);
890      if (code1 == CALL_INSN)
891        INSN_VOLATILE (insn) = 1;
892      else if (code1 == INSN || code1 == JUMP_INSN)
893        {
894          /* Delete (in effect) any obvious no-op moves.  */
895          if (GET_CODE (PATTERN (insn)) == SET
896              && GET_CODE (SET_DEST (PATTERN (insn))) == REG
897              && GET_CODE (SET_SRC (PATTERN (insn))) == REG
898              && (REGNO (SET_DEST (PATTERN (insn)))
899                  == REGNO (SET_SRC (PATTERN (insn))))
900              /* Insns carrying these notes are useful later on.  */
901              && ! find_reg_note (insn, REG_EQUAL, NULL_RTX))
902            {
903              PUT_CODE (insn, NOTE);
904              NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
905              NOTE_SOURCE_FILE (insn) = 0;
906            }
907          /* Delete (in effect) any obvious no-op moves.  */
908          else if (GET_CODE (PATTERN (insn)) == SET
909              && GET_CODE (SET_DEST (PATTERN (insn))) == SUBREG
910              && GET_CODE (SUBREG_REG (SET_DEST (PATTERN (insn)))) == REG
911              && GET_CODE (SET_SRC (PATTERN (insn))) == SUBREG
912              && GET_CODE (SUBREG_REG (SET_SRC (PATTERN (insn)))) == REG
913              && (REGNO (SUBREG_REG (SET_DEST (PATTERN (insn))))
914                  == REGNO (SUBREG_REG (SET_SRC (PATTERN (insn)))))
915              && SUBREG_WORD (SET_DEST (PATTERN (insn))) ==
916                              SUBREG_WORD (SET_SRC (PATTERN (insn)))
917              /* Insns carrying these notes are useful later on.  */
918              && ! find_reg_note (insn, REG_EQUAL, NULL_RTX))
919            {
920              PUT_CODE (insn, NOTE);
921              NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
922              NOTE_SOURCE_FILE (insn) = 0;
923            }
924          else if (GET_CODE (PATTERN (insn)) == PARALLEL)
925            {
926              /* If nothing but SETs of registers to themselves,
927                 this insn can also be deleted.  */
928              for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
929                {
930                  rtx tem = XVECEXP (PATTERN (insn), 0, i);
931
932                  if (GET_CODE (tem) == USE
933                      || GET_CODE (tem) == CLOBBER)
934                    continue;
935                   
936                  if (GET_CODE (tem) != SET
937                      || GET_CODE (SET_DEST (tem)) != REG
938                      || GET_CODE (SET_SRC (tem)) != REG
939                      || REGNO (SET_DEST (tem)) != REGNO (SET_SRC (tem)))
940                    break;
941                }
942               
943              if (i == XVECLEN (PATTERN (insn), 0)
944                  /* Insns carrying these notes are useful later on.  */
945                  && ! find_reg_note (insn, REG_EQUAL, NULL_RTX))
946                {
947                  PUT_CODE (insn, NOTE);
948                  NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
949                  NOTE_SOURCE_FILE (insn) = 0;
950                }
951              else
952                INSN_VOLATILE (insn) = volatile_refs_p (PATTERN (insn));
953            }
954          else if (GET_CODE (PATTERN (insn)) != USE)
955            INSN_VOLATILE (insn) = volatile_refs_p (PATTERN (insn));
956          /* A SET that makes space on the stack cannot be dead.
957             (Such SETs occur only for allocating variable-size data,
958             so they will always have a PLUS or MINUS according to the
959             direction of stack growth.)
960             Even if this function never uses this stack pointer value,
961             signal handlers do!  */
962          else if (code1 == INSN && GET_CODE (PATTERN (insn)) == SET
963                   && SET_DEST (PATTERN (insn)) == stack_pointer_rtx
964#ifdef STACK_GROWS_DOWNWARD
965                   && GET_CODE (SET_SRC (PATTERN (insn))) == MINUS
966#else
967                   && GET_CODE (SET_SRC (PATTERN (insn))) == PLUS
968#endif
969                   && XEXP (SET_SRC (PATTERN (insn)), 0) == stack_pointer_rtx)
970            INSN_VOLATILE (insn) = 1;
971        }
972    }
973
974  if (n_basic_blocks > 0)
975#ifdef EXIT_IGNORE_STACK
976    if (! EXIT_IGNORE_STACK
977        || (! FRAME_POINTER_REQUIRED && flag_omit_frame_pointer))
978#endif
979      {
980        /* If exiting needs the right stack value,
981           consider the stack pointer live at the end of the function.  */
982        SET_REGNO_REG_SET (basic_block_live_at_end[n_basic_blocks - 1],
983                           STACK_POINTER_REGNUM);
984        SET_REGNO_REG_SET (basic_block_new_live_at_end[n_basic_blocks - 1],
985                           STACK_POINTER_REGNUM);
986      }
987
988  /* Mark the frame pointer is needed at the end of the function.  If
989     we end up eliminating it, it will be removed from the live list
990     of each basic block by reload.  */
991
992  if (n_basic_blocks > 0)
993    {
994      SET_REGNO_REG_SET (basic_block_live_at_end[n_basic_blocks - 1],
995                         FRAME_POINTER_REGNUM);
996      SET_REGNO_REG_SET (basic_block_new_live_at_end[n_basic_blocks - 1],
997                         FRAME_POINTER_REGNUM);
998#if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
999      /* If they are different, also mark the hard frame pointer as live */
1000      SET_REGNO_REG_SET (basic_block_live_at_end[n_basic_blocks - 1],
1001                         HARD_FRAME_POINTER_REGNUM);
1002      SET_REGNO_REG_SET (basic_block_new_live_at_end[n_basic_blocks - 1],
1003                         HARD_FRAME_POINTER_REGNUM);
1004#endif     
1005      }
1006
1007  /* Mark all global registers and all registers used by the epilogue
1008     as being live at the end of the function since they may be
1009     referenced by our caller.  */
1010
1011  if (n_basic_blocks > 0)
1012    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1013      if (global_regs[i]
1014#ifdef EPILOGUE_USES
1015          || EPILOGUE_USES (i)
1016#endif
1017          )
1018        {
1019          SET_REGNO_REG_SET (basic_block_live_at_end[n_basic_blocks - 1], i);
1020          SET_REGNO_REG_SET (basic_block_new_live_at_end[n_basic_blocks - 1], i);
1021        }
1022
1023  /* Propagate life info through the basic blocks
1024     around the graph of basic blocks.
1025
1026     This is a relaxation process: each time a new register
1027     is live at the end of the basic block, we must scan the block
1028     to determine which registers are, as a consequence, live at the beginning
1029     of that block.  These registers must then be marked live at the ends
1030     of all the blocks that can transfer control to that block.
1031     The process continues until it reaches a fixed point.  */
1032
1033  first_pass = 1;
1034  changed = 1;
1035  while (changed)
1036    {
1037      changed = 0;
1038      for (i = n_basic_blocks - 1; i >= 0; i--)
1039        {
1040          int consider = first_pass;
1041          int must_rescan = first_pass;
1042          register int j;
1043
1044          if (!first_pass)
1045            {
1046              /* Set CONSIDER if this block needs thinking about at all
1047                 (that is, if the regs live now at the end of it
1048                 are not the same as were live at the end of it when
1049                 we last thought about it).
1050                 Set must_rescan if it needs to be thought about
1051                 instruction by instruction (that is, if any additional
1052                 reg that is live at the end now but was not live there before
1053                 is one of the significant regs of this basic block).  */
1054
1055              EXECUTE_IF_AND_COMPL_IN_REG_SET
1056                (basic_block_new_live_at_end[i],
1057                 basic_block_live_at_end[i], 0, j,
1058                 {
1059                   consider = 1;
1060                   if (REGNO_REG_SET_P (basic_block_significant[i], j))
1061                     {
1062                       must_rescan = 1;
1063                       goto done;
1064                     }
1065                 });
1066            done:
1067              if (! consider)
1068                continue;
1069            }
1070
1071          /* The live_at_start of this block may be changing,
1072             so another pass will be required after this one.  */
1073          changed = 1;
1074
1075          if (! must_rescan)
1076            {
1077              /* No complete rescan needed;
1078                 just record those variables newly known live at end
1079                 as live at start as well.  */
1080              IOR_AND_COMPL_REG_SET (basic_block_live_at_start[i],
1081                                     basic_block_new_live_at_end[i],
1082                                     basic_block_live_at_end[i]);
1083
1084              IOR_AND_COMPL_REG_SET (basic_block_live_at_end[i],
1085                                     basic_block_new_live_at_end[i],
1086                                     basic_block_live_at_end[i]);
1087            }
1088          else
1089            {
1090              /* Update the basic_block_live_at_start
1091                 by propagation backwards through the block.  */
1092              COPY_REG_SET (basic_block_live_at_end[i],
1093                            basic_block_new_live_at_end[i]);
1094              COPY_REG_SET (basic_block_live_at_start[i],
1095                            basic_block_live_at_end[i]);
1096              propagate_block (basic_block_live_at_start[i],
1097                               basic_block_head[i], basic_block_end[i], 0,
1098                               first_pass ? basic_block_significant[i]
1099                               : (regset) 0,
1100                               i);
1101            }
1102
1103          {
1104            register rtx jump, head;
1105
1106            /* Update the basic_block_new_live_at_end's of the block
1107               that falls through into this one (if any).  */
1108            head = basic_block_head[i];
1109            if (basic_block_drops_in[i])
1110              IOR_REG_SET (basic_block_new_live_at_end[i-1],
1111                           basic_block_live_at_start[i]);
1112
1113            /* Update the basic_block_new_live_at_end's of
1114               all the blocks that jump to this one.  */
1115            if (GET_CODE (head) == CODE_LABEL)
1116              for (jump = LABEL_REFS (head);
1117                   jump != head;
1118                   jump = LABEL_NEXTREF (jump))
1119                {
1120                  register int from_block = BLOCK_NUM (CONTAINING_INSN (jump));
1121                  IOR_REG_SET (basic_block_new_live_at_end[from_block],
1122                               basic_block_live_at_start[i]);
1123                }
1124          }
1125#ifdef USE_C_ALLOCA
1126          alloca (0);
1127#endif
1128        }
1129      first_pass = 0;
1130    }
1131
1132  /* The only pseudos that are live at the beginning of the function are
1133     those that were not set anywhere in the function.  local-alloc doesn't
1134     know how to handle these correctly, so mark them as not local to any
1135     one basic block.  */
1136
1137  if (n_basic_blocks > 0)
1138    EXECUTE_IF_SET_IN_REG_SET (basic_block_live_at_start[0],
1139                               FIRST_PSEUDO_REGISTER, i,
1140                               {
1141                                 REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL;
1142                               });
1143
1144  /* Now the life information is accurate.
1145     Make one more pass over each basic block
1146     to delete dead stores, create autoincrement addressing
1147     and record how many times each register is used, is set, or dies.
1148
1149     To save time, we operate directly in basic_block_live_at_end[i],
1150     thus destroying it (in fact, converting it into a copy of
1151     basic_block_live_at_start[i]).  This is ok now because
1152     basic_block_live_at_end[i] is no longer used past this point.  */
1153
1154  max_scratch = 0;
1155
1156  for (i = 0; i < n_basic_blocks; i++)
1157    {
1158      propagate_block (basic_block_live_at_end[i],
1159                       basic_block_head[i], basic_block_end[i], 1,
1160                       (regset) 0, i);
1161#ifdef USE_C_ALLOCA
1162      alloca (0);
1163#endif
1164    }
1165
1166#if 0
1167  /* Something live during a setjmp should not be put in a register
1168     on certain machines which restore regs from stack frames
1169     rather than from the jmpbuf.
1170     But we don't need to do this for the user's variables, since
1171     ANSI says only volatile variables need this.  */
1172#ifdef LONGJMP_RESTORE_FROM_STACK
1173  EXECUTE_IF_SET_IN_REG_SET (regs_live_at_setjmp,
1174                             FIRST_PSEUDO_REGISTER, i,
1175                             {
1176                               if (regno_reg_rtx[i] != 0
1177                                   && ! REG_USERVAR_P (regno_reg_rtx[i]))
1178                                 {
1179                                   REG_LIVE_LENGTH (i) = -1;
1180                                   REG_BASIC_BLOCK (i) = -1;
1181                                 }
1182                             });
1183#endif
1184#endif
1185
1186  /* We have a problem with any pseudoreg that
1187     lives across the setjmp.  ANSI says that if a
1188     user variable does not change in value
1189     between the setjmp and the longjmp, then the longjmp preserves it.
1190     This includes longjmp from a place where the pseudo appears dead.
1191     (In principle, the value still exists if it is in scope.)
1192     If the pseudo goes in a hard reg, some other value may occupy
1193     that hard reg where this pseudo is dead, thus clobbering the pseudo.
1194     Conclusion: such a pseudo must not go in a hard reg.  */
1195  EXECUTE_IF_SET_IN_REG_SET (regs_live_at_setjmp,
1196                             FIRST_PSEUDO_REGISTER, i,
1197                             {
1198                               if (regno_reg_rtx[i] != 0)
1199                                 {
1200                                   REG_LIVE_LENGTH (i) = -1;
1201                                   REG_BASIC_BLOCK (i) = -1;
1202                                 }
1203                             });
1204
1205
1206  free_regset_vector (basic_block_live_at_end, n_basic_blocks);
1207  free_regset_vector (basic_block_new_live_at_end, n_basic_blocks);
1208  free_regset_vector (basic_block_significant, n_basic_blocks);
1209  basic_block_live_at_end = (regset *)0;
1210  basic_block_new_live_at_end = (regset *)0;
1211  basic_block_significant = (regset *)0;
1212
1213  obstack_free (&flow_obstack, NULL_PTR);
1214}
1215
1216/* Subroutines of life analysis.  */
1217
1218/* Allocate the permanent data structures that represent the results
1219   of life analysis.  Not static since used also for stupid life analysis.  */
1220
1221void
1222allocate_for_life_analysis ()
1223{
1224  register int i;
1225
1226  /* Recalculate the register space, in case it has grown.  Old style
1227     vector oriented regsets would set regset_{size,bytes} here also.  */
1228  allocate_reg_info (max_regno, FALSE, FALSE);
1229
1230  /* Because both reg_scan and flow_analysis want to set up the REG_N_SETS
1231     information, explicitly reset it here.  The allocation should have
1232     already happened on the previous reg_scan pass.  Make sure in case
1233     some more registers were allocated.  */
1234  for (i = 0; i < max_regno; i++)
1235    REG_N_SETS (i) = 0;
1236
1237  basic_block_live_at_start
1238    = (regset *) oballoc (n_basic_blocks * sizeof (regset));
1239  init_regset_vector (basic_block_live_at_start, n_basic_blocks,
1240                      function_obstack);
1241
1242  regs_live_at_setjmp = OBSTACK_ALLOC_REG_SET (function_obstack);
1243  CLEAR_REG_SET (regs_live_at_setjmp);
1244}
1245
1246/* Make each element of VECTOR point at a regset.  The vector has
1247   NELTS elements, and space is allocated from the ALLOC_OBSTACK
1248   obstack.  */
1249
1250void
1251init_regset_vector (vector, nelts, alloc_obstack)
1252     regset *vector;
1253     int nelts;
1254     struct obstack *alloc_obstack;
1255{
1256  register int i;
1257
1258  for (i = 0; i < nelts; i++)
1259    {
1260      vector[i] = OBSTACK_ALLOC_REG_SET (alloc_obstack);
1261      CLEAR_REG_SET (vector[i]);
1262    }
1263}
1264
1265/* Release any additional space allocated for each element of VECTOR point
1266   other than the regset header itself.  The vector has NELTS elements.  */
1267
1268void
1269free_regset_vector (vector, nelts)
1270     regset *vector;
1271     int nelts;
1272{
1273  register int i;
1274
1275  for (i = 0; i < nelts; i++)
1276    FREE_REG_SET (vector[i]);
1277}
1278
1279/* Compute the registers live at the beginning of a basic block
1280   from those live at the end.
1281
1282   When called, OLD contains those live at the end.
1283   On return, it contains those live at the beginning.
1284   FIRST and LAST are the first and last insns of the basic block.
1285
1286   FINAL is nonzero if we are doing the final pass which is not
1287   for computing the life info (since that has already been done)
1288   but for acting on it.  On this pass, we delete dead stores,
1289   set up the logical links and dead-variables lists of instructions,
1290   and merge instructions for autoincrement and autodecrement addresses.
1291
1292   SIGNIFICANT is nonzero only the first time for each basic block.
1293   If it is nonzero, it points to a regset in which we store
1294   a 1 for each register that is set within the block.
1295
1296   BNUM is the number of the basic block.  */
1297
1298static void
1299propagate_block (old, first, last, final, significant, bnum)
1300     register regset old;
1301     rtx first;
1302     rtx last;
1303     int final;
1304     regset significant;
1305     int bnum;
1306{
1307  register rtx insn;
1308  rtx prev;
1309  regset live;
1310  regset dead;
1311
1312  /* The following variables are used only if FINAL is nonzero.  */
1313  /* This vector gets one element for each reg that has been live
1314     at any point in the basic block that has been scanned so far.
1315     SOMETIMES_MAX says how many elements are in use so far.  */
1316  register int *regs_sometimes_live;
1317  int sometimes_max = 0;
1318  /* This regset has 1 for each reg that we have seen live so far.
1319     It and REGS_SOMETIMES_LIVE are updated together.  */
1320  regset maxlive;
1321
1322  /* The loop depth may change in the middle of a basic block.  Since we
1323     scan from end to beginning, we start with the depth at the end of the
1324     current basic block, and adjust as we pass ends and starts of loops.  */
1325  loop_depth = basic_block_loop_depth[bnum];
1326
1327  dead = ALLOCA_REG_SET ();
1328  live = ALLOCA_REG_SET ();
1329
1330  cc0_live = 0;
1331  last_mem_set = 0;
1332
1333  /* Include any notes at the end of the block in the scan.
1334     This is in case the block ends with a call to setjmp.  */
1335
1336  while (NEXT_INSN (last) != 0 && GET_CODE (NEXT_INSN (last)) == NOTE)
1337    {
1338      /* Look for loop boundaries, we are going forward here.  */
1339      last = NEXT_INSN (last);
1340      if (NOTE_LINE_NUMBER (last) == NOTE_INSN_LOOP_BEG)
1341        loop_depth++;
1342      else if (NOTE_LINE_NUMBER (last) == NOTE_INSN_LOOP_END)
1343        loop_depth--;
1344    }
1345
1346  if (final)
1347    {
1348      register int i;
1349
1350      num_scratch = 0;
1351      maxlive = ALLOCA_REG_SET ();
1352      COPY_REG_SET (maxlive, old);
1353      regs_sometimes_live = (int *) alloca (max_regno * sizeof (int));
1354
1355      /* Process the regs live at the end of the block.
1356         Enter them in MAXLIVE and REGS_SOMETIMES_LIVE.
1357         Also mark them as not local to any one basic block. */
1358      EXECUTE_IF_SET_IN_REG_SET (old, 0, i,
1359                                 {
1360                                   REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL;
1361                                   regs_sometimes_live[sometimes_max] = i;
1362                                   sometimes_max++;
1363                                 });
1364    }
1365
1366  /* Scan the block an insn at a time from end to beginning.  */
1367
1368  for (insn = last; ; insn = prev)
1369    {
1370      prev = PREV_INSN (insn);
1371
1372      if (GET_CODE (insn) == NOTE)
1373        {
1374          /* Look for loop boundaries, remembering that we are going
1375             backwards.  */
1376          if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
1377            loop_depth++;
1378          else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
1379            loop_depth--;
1380
1381          /* If we have LOOP_DEPTH == 0, there has been a bookkeeping error.
1382             Abort now rather than setting register status incorrectly.  */
1383          if (loop_depth == 0)
1384            abort ();
1385
1386          /* If this is a call to `setjmp' et al,
1387             warn if any non-volatile datum is live.  */
1388
1389          if (final && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
1390            IOR_REG_SET (regs_live_at_setjmp, old);
1391        }
1392
1393      /* Update the life-status of regs for this insn.
1394         First DEAD gets which regs are set in this insn
1395         then LIVE gets which regs are used in this insn.
1396         Then the regs live before the insn
1397         are those live after, with DEAD regs turned off,
1398         and then LIVE regs turned on.  */
1399
1400      else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1401        {
1402          register int i;
1403          rtx note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
1404          int insn_is_dead
1405            = (insn_dead_p (PATTERN (insn), old, 0)
1406               /* Don't delete something that refers to volatile storage!  */
1407               && ! INSN_VOLATILE (insn));
1408          int libcall_is_dead
1409            = (insn_is_dead && note != 0
1410               && libcall_dead_p (PATTERN (insn), old, note, insn));
1411
1412          /* If an instruction consists of just dead store(s) on final pass,
1413             "delete" it by turning it into a NOTE of type NOTE_INSN_DELETED.
1414             We could really delete it with delete_insn, but that
1415             can cause trouble for first or last insn in a basic block.  */
1416          if (final && insn_is_dead)
1417            {
1418              PUT_CODE (insn, NOTE);
1419              NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1420              NOTE_SOURCE_FILE (insn) = 0;
1421
1422              /* CC0 is now known to be dead.  Either this insn used it,
1423                 in which case it doesn't anymore, or clobbered it,
1424                 so the next insn can't use it.  */
1425              cc0_live = 0;
1426
1427              /* If this insn is copying the return value from a library call,
1428                 delete the entire library call.  */
1429              if (libcall_is_dead)
1430                {
1431                  rtx first = XEXP (note, 0);
1432                  rtx p = insn;
1433                  while (INSN_DELETED_P (first))
1434                    first = NEXT_INSN (first);
1435                  while (p != first)
1436                    {
1437                      p = PREV_INSN (p);
1438                      PUT_CODE (p, NOTE);
1439                      NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
1440                      NOTE_SOURCE_FILE (p) = 0;
1441                    }
1442                }
1443              goto flushed;
1444            }
1445
1446          CLEAR_REG_SET (dead);
1447          CLEAR_REG_SET (live);
1448
1449          /* See if this is an increment or decrement that can be
1450             merged into a following memory address.  */
1451#ifdef AUTO_INC_DEC
1452          {
1453            register rtx x = single_set (insn);
1454
1455            /* Does this instruction increment or decrement a register?  */
1456            if (final && x != 0
1457                && GET_CODE (SET_DEST (x)) == REG
1458                && (GET_CODE (SET_SRC (x)) == PLUS
1459                    || GET_CODE (SET_SRC (x)) == MINUS)
1460                && XEXP (SET_SRC (x), 0) == SET_DEST (x)
1461                && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
1462                /* Ok, look for a following memory ref we can combine with.
1463                   If one is found, change the memory ref to a PRE_INC
1464                   or PRE_DEC, cancel this insn, and return 1.
1465                   Return 0 if nothing has been done.  */
1466                && try_pre_increment_1 (insn))
1467              goto flushed;
1468          }
1469#endif /* AUTO_INC_DEC */
1470
1471          /* If this is not the final pass, and this insn is copying the
1472             value of a library call and it's dead, don't scan the
1473             insns that perform the library call, so that the call's
1474             arguments are not marked live.  */
1475          if (libcall_is_dead)
1476            {
1477              /* Mark the dest reg as `significant'.  */
1478              mark_set_regs (old, dead, PATTERN (insn), NULL_RTX, significant);
1479
1480              insn = XEXP (note, 0);
1481              prev = PREV_INSN (insn);
1482            }
1483          else if (GET_CODE (PATTERN (insn)) == SET
1484                   && SET_DEST (PATTERN (insn)) == stack_pointer_rtx
1485                   && GET_CODE (SET_SRC (PATTERN (insn))) == PLUS
1486                   && XEXP (SET_SRC (PATTERN (insn)), 0) == stack_pointer_rtx
1487                   && GET_CODE (XEXP (SET_SRC (PATTERN (insn)), 1)) == CONST_INT)
1488            /* We have an insn to pop a constant amount off the stack.
1489               (Such insns use PLUS regardless of the direction of the stack,
1490               and any insn to adjust the stack by a constant is always a pop.)
1491               These insns, if not dead stores, have no effect on life.  */
1492            ;
1493          else
1494            {
1495              /* LIVE gets the regs used in INSN;
1496                 DEAD gets those set by it.  Dead insns don't make anything
1497                 live.  */
1498
1499              mark_set_regs (old, dead, PATTERN (insn),
1500                             final ? insn : NULL_RTX, significant);
1501
1502              /* If an insn doesn't use CC0, it becomes dead since we
1503                 assume that every insn clobbers it.  So show it dead here;
1504                 mark_used_regs will set it live if it is referenced.  */
1505              cc0_live = 0;
1506
1507              if (! insn_is_dead)
1508                mark_used_regs (old, live, PATTERN (insn), final, insn);
1509
1510              /* Sometimes we may have inserted something before INSN (such as
1511                 a move) when we make an auto-inc.  So ensure we will scan
1512                 those insns.  */
1513#ifdef AUTO_INC_DEC
1514              prev = PREV_INSN (insn);
1515#endif
1516
1517              if (! insn_is_dead && GET_CODE (insn) == CALL_INSN)
1518                {
1519                  register int i;
1520
1521                  rtx note;
1522
1523                  for (note = CALL_INSN_FUNCTION_USAGE (insn);
1524                       note;
1525                       note = XEXP (note, 1))
1526                    if (GET_CODE (XEXP (note, 0)) == USE)
1527                      mark_used_regs (old, live, SET_DEST (XEXP (note, 0)),
1528                                      final, insn);
1529
1530                  /* Each call clobbers all call-clobbered regs that are not
1531                     global or fixed.  Note that the function-value reg is a
1532                     call-clobbered reg, and mark_set_regs has already had
1533                     a chance to handle it.  */
1534
1535                  for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1536                    if (call_used_regs[i] && ! global_regs[i]
1537                        && ! fixed_regs[i])
1538                      SET_REGNO_REG_SET (dead, i);
1539
1540                  /* The stack ptr is used (honorarily) by a CALL insn.  */
1541                  SET_REGNO_REG_SET (live, STACK_POINTER_REGNUM);
1542
1543                  /* Calls may also reference any of the global registers,
1544                     so they are made live.  */
1545                  for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1546                    if (global_regs[i])
1547                      mark_used_regs (old, live,
1548                                      gen_rtx (REG, reg_raw_mode[i], i),
1549                                      final, insn);
1550
1551                  /* Calls also clobber memory.  */
1552                  last_mem_set = 0;
1553                }
1554
1555              /* Update OLD for the registers used or set.  */
1556              AND_COMPL_REG_SET (old, dead);
1557              IOR_REG_SET (old, live);
1558
1559              if (GET_CODE (insn) == CALL_INSN && final)
1560                {
1561                  /* Any regs live at the time of a call instruction
1562                     must not go in a register clobbered by calls.
1563                     Find all regs now live and record this for them.  */
1564
1565                  register int *p = regs_sometimes_live;
1566
1567                  for (i = 0; i < sometimes_max; i++, p++)
1568                    if (REGNO_REG_SET_P (old, *p))
1569                      REG_N_CALLS_CROSSED (*p)++;
1570                }
1571            }
1572
1573          /* On final pass, add any additional sometimes-live regs
1574             into MAXLIVE and REGS_SOMETIMES_LIVE.
1575             Also update counts of how many insns each reg is live at.  */
1576
1577          if (final)
1578            {
1579              register int regno;
1580              register int *p;
1581
1582              EXECUTE_IF_AND_COMPL_IN_REG_SET
1583                (live, maxlive, 0, regno,
1584                 {
1585                   regs_sometimes_live[sometimes_max++] = regno;
1586                   SET_REGNO_REG_SET (maxlive, regno);
1587                 });
1588
1589              p = regs_sometimes_live;
1590              for (i = 0; i < sometimes_max; i++)
1591                {
1592                  regno = *p++;
1593                  if (REGNO_REG_SET_P (old, regno))
1594                    REG_LIVE_LENGTH (regno)++;
1595                }
1596            }
1597        }
1598    flushed: ;
1599      if (insn == first)
1600        break;
1601    }
1602
1603  FREE_REG_SET (dead);
1604  FREE_REG_SET (live);
1605  if (final)
1606    FREE_REG_SET (maxlive);
1607
1608  if (num_scratch > max_scratch)
1609    max_scratch = num_scratch;
1610}
1611
1612/* Return 1 if X (the body of an insn, or part of it) is just dead stores
1613   (SET expressions whose destinations are registers dead after the insn).
1614   NEEDED is the regset that says which regs are alive after the insn.
1615
1616   Unless CALL_OK is non-zero, an insn is needed if it contains a CALL.  */
1617
1618static int
1619insn_dead_p (x, needed, call_ok)
1620     rtx x;
1621     regset needed;
1622     int call_ok;
1623{
1624  register RTX_CODE code = GET_CODE (x);
1625  /* If setting something that's a reg or part of one,
1626     see if that register's altered value will be live.  */
1627
1628  if (code == SET)
1629    {
1630      register rtx r = SET_DEST (x);
1631      /* A SET that is a subroutine call cannot be dead.  */
1632      if (! call_ok && GET_CODE (SET_SRC (x)) == CALL)
1633        return 0;
1634
1635#ifdef HAVE_cc0
1636      if (GET_CODE (r) == CC0)
1637        return ! cc0_live;
1638#endif
1639     
1640      if (GET_CODE (r) == MEM && last_mem_set && ! MEM_VOLATILE_P (r)
1641          && rtx_equal_p (r, last_mem_set))
1642        return 1;
1643
1644      while (GET_CODE (r) == SUBREG
1645             || GET_CODE (r) == STRICT_LOW_PART
1646             || GET_CODE (r) == ZERO_EXTRACT
1647             || GET_CODE (r) == SIGN_EXTRACT)
1648        r = SUBREG_REG (r);
1649
1650      if (GET_CODE (r) == REG)
1651        {
1652          register int regno = REGNO (r);
1653
1654          /* Don't delete insns to set global regs.  */
1655          if ((regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
1656              /* Make sure insns to set frame pointer aren't deleted.  */
1657              || regno == FRAME_POINTER_REGNUM
1658#if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
1659              || regno == HARD_FRAME_POINTER_REGNUM
1660#endif
1661#if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1662              /* Make sure insns to set arg pointer are never deleted
1663                 (if the arg pointer isn't fixed, there will be a USE for
1664                 it, so we can treat it normally).  */
1665              || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
1666#endif
1667              || REGNO_REG_SET_P (needed, regno))
1668            return 0;
1669
1670          /* If this is a hard register, verify that subsequent words are
1671             not needed.  */
1672          if (regno < FIRST_PSEUDO_REGISTER)
1673            {
1674              int n = HARD_REGNO_NREGS (regno, GET_MODE (r));
1675
1676              while (--n > 0)
1677                if (REGNO_REG_SET_P (needed, regno+n))
1678                  return 0;
1679            }
1680
1681          return 1;
1682        }
1683    }
1684  /* If performing several activities,
1685     insn is dead if each activity is individually dead.
1686     Also, CLOBBERs and USEs can be ignored; a CLOBBER or USE
1687     that's inside a PARALLEL doesn't make the insn worth keeping.  */
1688  else if (code == PARALLEL)
1689    {
1690      register int i = XVECLEN (x, 0);
1691      for (i--; i >= 0; i--)
1692        {
1693          rtx elt = XVECEXP (x, 0, i);
1694          if (!insn_dead_p (elt, needed, call_ok)
1695              && GET_CODE (elt) != CLOBBER
1696              && GET_CODE (elt) != USE)
1697            return 0;
1698        }
1699      return 1;
1700    }
1701  /* We do not check CLOBBER or USE here.
1702     An insn consisting of just a CLOBBER or just a USE
1703     should not be deleted.  */
1704  return 0;
1705}
1706
1707/* If X is the pattern of the last insn in a libcall, and assuming X is dead,
1708   return 1 if the entire library call is dead.
1709   This is true if X copies a register (hard or pseudo)
1710   and if the hard return  reg of the call insn is dead.
1711   (The caller should have tested the destination of X already for death.)
1712
1713   If this insn doesn't just copy a register, then we don't
1714   have an ordinary libcall.  In that case, cse could not have
1715   managed to substitute the source for the dest later on,
1716   so we can assume the libcall is dead.
1717
1718   NEEDED is the bit vector of pseudoregs live before this insn.
1719   NOTE is the REG_RETVAL note of the insn.  INSN is the insn itself.  */
1720
1721static int
1722libcall_dead_p (x, needed, note, insn)
1723     rtx x;
1724     regset needed;
1725     rtx note;
1726     rtx insn;
1727{
1728  register RTX_CODE code = GET_CODE (x);
1729
1730  if (code == SET)
1731    {
1732      register rtx r = SET_SRC (x);
1733      if (GET_CODE (r) == REG)
1734        {
1735          rtx call = XEXP (note, 0);
1736          register int i;
1737
1738          /* Find the call insn.  */
1739          while (call != insn && GET_CODE (call) != CALL_INSN)
1740            call = NEXT_INSN (call);
1741
1742          /* If there is none, do nothing special,
1743             since ordinary death handling can understand these insns.  */
1744          if (call == insn)
1745            return 0;
1746
1747          /* See if the hard reg holding the value is dead.
1748             If this is a PARALLEL, find the call within it.  */
1749          call = PATTERN (call);
1750          if (GET_CODE (call) == PARALLEL)
1751            {
1752              for (i = XVECLEN (call, 0) - 1; i >= 0; i--)
1753                if (GET_CODE (XVECEXP (call, 0, i)) == SET
1754                    && GET_CODE (SET_SRC (XVECEXP (call, 0, i))) == CALL)
1755                  break;
1756
1757              /* This may be a library call that is returning a value
1758                 via invisible pointer.  Do nothing special, since
1759                 ordinary death handling can understand these insns.  */
1760              if (i < 0)
1761                return 0;
1762
1763              call = XVECEXP (call, 0, i);
1764            }
1765
1766          return insn_dead_p (call, needed, 1);
1767        }
1768    }
1769  return 1;
1770}
1771
1772/* Return 1 if register REGNO was used before it was set.
1773   In other words, if it is live at function entry.
1774   Don't count global register variables or variables in registers
1775   that can be used for function arg passing, though.  */
1776
1777int
1778regno_uninitialized (regno)
1779     int regno;
1780{
1781  if (n_basic_blocks == 0
1782      || (regno < FIRST_PSEUDO_REGISTER
1783          && (global_regs[regno] || FUNCTION_ARG_REGNO_P (regno))))
1784    return 0;
1785
1786  return REGNO_REG_SET_P (basic_block_live_at_start[0], regno);
1787}
1788
1789/* 1 if register REGNO was alive at a place where `setjmp' was called
1790   and was set more than once or is an argument.
1791   Such regs may be clobbered by `longjmp'.  */
1792
1793int
1794regno_clobbered_at_setjmp (regno)
1795     int regno;
1796{
1797  if (n_basic_blocks == 0)
1798    return 0;
1799
1800  return ((REG_N_SETS (regno) > 1
1801           || REGNO_REG_SET_P (basic_block_live_at_start[0], regno))
1802          && REGNO_REG_SET_P (regs_live_at_setjmp, regno));
1803}
1804
1805/* Process the registers that are set within X.
1806   Their bits are set to 1 in the regset DEAD,
1807   because they are dead prior to this insn.
1808
1809   If INSN is nonzero, it is the insn being processed
1810   and the fact that it is nonzero implies this is the FINAL pass
1811   in propagate_block.  In this case, various info about register
1812   usage is stored, LOG_LINKS fields of insns are set up.  */
1813
1814static void
1815mark_set_regs (needed, dead, x, insn, significant)
1816     regset needed;
1817     regset dead;
1818     rtx x;
1819     rtx insn;
1820     regset significant;
1821{
1822  register RTX_CODE code = GET_CODE (x);
1823
1824  if (code == SET || code == CLOBBER)
1825    mark_set_1 (needed, dead, x, insn, significant);
1826  else if (code == PARALLEL)
1827    {
1828      register int i;
1829      for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1830        {
1831          code = GET_CODE (XVECEXP (x, 0, i));
1832          if (code == SET || code == CLOBBER)
1833            mark_set_1 (needed, dead, XVECEXP (x, 0, i), insn, significant);
1834        }
1835    }
1836}
1837
1838/* Process a single SET rtx, X.  */
1839
1840static void
1841mark_set_1 (needed, dead, x, insn, significant)
1842     regset needed;
1843     regset dead;
1844     rtx x;
1845     rtx insn;
1846     regset significant;
1847{
1848  register int regno;
1849  register rtx reg = SET_DEST (x);
1850
1851  /* Modifying just one hardware register of a multi-reg value
1852     or just a byte field of a register
1853     does not mean the value from before this insn is now dead.
1854     But it does mean liveness of that register at the end of the block
1855     is significant.
1856
1857     Within mark_set_1, however, we treat it as if the register is
1858     indeed modified.  mark_used_regs will, however, also treat this
1859     register as being used.  Thus, we treat these insns as setting a
1860     new value for the register as a function of its old value.  This
1861     cases LOG_LINKS to be made appropriately and this will help combine.  */
1862
1863  while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
1864         || GET_CODE (reg) == SIGN_EXTRACT
1865         || GET_CODE (reg) == STRICT_LOW_PART)
1866    reg = XEXP (reg, 0);
1867
1868  /* If we are writing into memory or into a register mentioned in the
1869     address of the last thing stored into memory, show we don't know
1870     what the last store was.  If we are writing memory, save the address
1871     unless it is volatile.  */
1872  if (GET_CODE (reg) == MEM
1873      || (GET_CODE (reg) == REG
1874          && last_mem_set != 0 && reg_overlap_mentioned_p (reg, last_mem_set)))
1875    last_mem_set = 0;
1876   
1877  if (GET_CODE (reg) == MEM && ! side_effects_p (reg)
1878      /* There are no REG_INC notes for SP, so we can't assume we'll see
1879         everything that invalidates it.  To be safe, don't eliminate any
1880         stores though SP; none of them should be redundant anyway.  */
1881      && ! reg_mentioned_p (stack_pointer_rtx, reg))
1882    last_mem_set = reg;
1883
1884  if (GET_CODE (reg) == REG
1885      && (regno = REGNO (reg), regno != FRAME_POINTER_REGNUM)
1886#if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
1887      && regno != HARD_FRAME_POINTER_REGNUM
1888#endif
1889#if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1890      && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
1891#endif
1892      && ! (regno < FIRST_PSEUDO_REGISTER && global_regs[regno]))
1893    /* && regno != STACK_POINTER_REGNUM) -- let's try without this.  */
1894    {
1895      int some_needed = REGNO_REG_SET_P (needed, regno);
1896      int some_not_needed = ! some_needed;
1897
1898      /* Mark it as a significant register for this basic block.  */
1899      if (significant)
1900        SET_REGNO_REG_SET (significant, regno);
1901
1902      /* Mark it as as dead before this insn.  */
1903      SET_REGNO_REG_SET (dead, regno);
1904
1905      /* A hard reg in a wide mode may really be multiple registers.
1906         If so, mark all of them just like the first.  */
1907      if (regno < FIRST_PSEUDO_REGISTER)
1908        {
1909          int n;
1910
1911          /* Nothing below is needed for the stack pointer; get out asap.
1912             Eg, log links aren't needed, since combine won't use them.  */
1913          if (regno == STACK_POINTER_REGNUM)
1914            return;
1915
1916          n = HARD_REGNO_NREGS (regno, GET_MODE (reg));
1917          while (--n > 0)
1918            {
1919              int regno_n = regno + n;
1920              int needed_regno = REGNO_REG_SET_P (needed, regno_n);
1921              if (significant)
1922                SET_REGNO_REG_SET (significant, regno_n);
1923
1924              SET_REGNO_REG_SET (dead, regno_n);
1925              some_needed |= needed_regno;
1926              some_not_needed |= ! needed_regno;
1927            }
1928        }
1929      /* Additional data to record if this is the final pass.  */
1930      if (insn)
1931        {
1932          register rtx y = reg_next_use[regno];
1933          register int blocknum = BLOCK_NUM (insn);
1934
1935          /* If this is a hard reg, record this function uses the reg.  */
1936
1937          if (regno < FIRST_PSEUDO_REGISTER)
1938            {
1939              register int i;
1940              int endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
1941
1942              for (i = regno; i < endregno; i++)
1943                {
1944                  /* The next use is no longer "next", since a store
1945                     intervenes.  */
1946                  reg_next_use[i] = 0;
1947
1948                  regs_ever_live[i] = 1;
1949                  REG_N_SETS (i)++;
1950                }
1951            }
1952          else
1953            {
1954              /* The next use is no longer "next", since a store
1955                 intervenes.  */
1956              reg_next_use[regno] = 0;
1957
1958              /* Keep track of which basic blocks each reg appears in.  */
1959
1960              if (REG_BASIC_BLOCK (regno) == REG_BLOCK_UNKNOWN)
1961                REG_BASIC_BLOCK (regno) = blocknum;
1962              else if (REG_BASIC_BLOCK (regno) != blocknum)
1963                REG_BASIC_BLOCK (regno) = REG_BLOCK_GLOBAL;
1964
1965              /* Count (weighted) references, stores, etc.  This counts a
1966                 register twice if it is modified, but that is correct.  */
1967              REG_N_SETS (regno)++;
1968
1969              REG_N_REFS (regno) += loop_depth;
1970                 
1971              /* The insns where a reg is live are normally counted
1972                 elsewhere, but we want the count to include the insn
1973                 where the reg is set, and the normal counting mechanism
1974                 would not count it.  */
1975              REG_LIVE_LENGTH (regno)++;
1976            }
1977
1978          if (! some_not_needed)
1979            {
1980              /* Make a logical link from the next following insn
1981                 that uses this register, back to this insn.
1982                 The following insns have already been processed.
1983
1984                 We don't build a LOG_LINK for hard registers containing
1985                 in ASM_OPERANDs.  If these registers get replaced,
1986                 we might wind up changing the semantics of the insn,
1987                 even if reload can make what appear to be valid assignments
1988                 later.  */
1989              if (y && (BLOCK_NUM (y) == blocknum)
1990                  && (regno >= FIRST_PSEUDO_REGISTER
1991                      || asm_noperands (PATTERN (y)) < 0))
1992                LOG_LINKS (y)
1993                  = gen_rtx (INSN_LIST, VOIDmode, insn, LOG_LINKS (y));
1994            }
1995          else if (! some_needed)
1996            {
1997              /* Note that dead stores have already been deleted when possible
1998                 If we get here, we have found a dead store that cannot
1999                 be eliminated (because the same insn does something useful).
2000                 Indicate this by marking the reg being set as dying here.  */
2001              REG_NOTES (insn)
2002                = gen_rtx (EXPR_LIST, REG_UNUSED, reg, REG_NOTES (insn));
2003              REG_N_DEATHS (REGNO (reg))++;
2004            }
2005          else
2006            {
2007              /* This is a case where we have a multi-word hard register
2008                 and some, but not all, of the words of the register are
2009                 needed in subsequent insns.  Write REG_UNUSED notes
2010                 for those parts that were not needed.  This case should
2011                 be rare.  */
2012
2013              int i;
2014
2015              for (i = HARD_REGNO_NREGS (regno, GET_MODE (reg)) - 1;
2016                   i >= 0; i--)
2017                if (!REGNO_REG_SET_P (needed, regno + i))
2018                  REG_NOTES (insn)
2019                    = gen_rtx (EXPR_LIST, REG_UNUSED,
2020                               gen_rtx (REG, reg_raw_mode[regno + i],
2021                                        regno + i),
2022                               REG_NOTES (insn));
2023            }
2024        }
2025    }
2026  else if (GET_CODE (reg) == REG)
2027    reg_next_use[regno] = 0;
2028
2029  /* If this is the last pass and this is a SCRATCH, show it will be dying
2030     here and count it.  */
2031  else if (GET_CODE (reg) == SCRATCH && insn != 0)
2032    {
2033      REG_NOTES (insn)
2034        = gen_rtx (EXPR_LIST, REG_UNUSED, reg, REG_NOTES (insn));
2035      num_scratch++;
2036    }
2037}
2038
2039#ifdef AUTO_INC_DEC
2040
2041/* X is a MEM found in INSN.  See if we can convert it into an auto-increment
2042   reference.  */
2043
2044static void
2045find_auto_inc (needed, x, insn)
2046     regset needed;
2047     rtx x;
2048     rtx insn;
2049{
2050  rtx addr = XEXP (x, 0);
2051  HOST_WIDE_INT offset = 0;
2052  rtx set;
2053
2054  /* Here we detect use of an index register which might be good for
2055     postincrement, postdecrement, preincrement, or predecrement.  */
2056
2057  if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
2058    offset = INTVAL (XEXP (addr, 1)), addr = XEXP (addr, 0);
2059
2060  if (GET_CODE (addr) == REG)
2061    {
2062      register rtx y;
2063      register int size = GET_MODE_SIZE (GET_MODE (x));
2064      rtx use;
2065      rtx incr;
2066      int regno = REGNO (addr);
2067
2068      /* Is the next use an increment that might make auto-increment? */
2069      if ((incr = reg_next_use[regno]) != 0
2070          && (set = single_set (incr)) != 0
2071          && GET_CODE (set) == SET
2072          && BLOCK_NUM (incr) == BLOCK_NUM (insn)
2073          /* Can't add side effects to jumps; if reg is spilled and
2074             reloaded, there's no way to store back the altered value.  */
2075          && GET_CODE (insn) != JUMP_INSN
2076          && (y = SET_SRC (set), GET_CODE (y) == PLUS)
2077          && XEXP (y, 0) == addr
2078          && GET_CODE (XEXP (y, 1)) == CONST_INT
2079          && (0
2080#ifdef HAVE_POST_INCREMENT
2081              || (INTVAL (XEXP (y, 1)) == size && offset == 0)
2082#endif
2083#ifdef HAVE_POST_DECREMENT
2084              || (INTVAL (XEXP (y, 1)) == - size && offset == 0)
2085#endif
2086#ifdef HAVE_PRE_INCREMENT
2087              || (INTVAL (XEXP (y, 1)) == size && offset == size)
2088#endif
2089#ifdef HAVE_PRE_DECREMENT
2090              || (INTVAL (XEXP (y, 1)) == - size && offset == - size)
2091#endif
2092              )
2093          /* Make sure this reg appears only once in this insn.  */
2094          && (use = find_use_as_address (PATTERN (insn), addr, offset),
2095              use != 0 && use != (rtx) 1))
2096        {
2097          rtx q = SET_DEST (set);
2098          enum rtx_code inc_code = (INTVAL (XEXP (y, 1)) == size
2099                                    ? (offset ? PRE_INC : POST_INC)
2100                                    : (offset ? PRE_DEC : POST_DEC));
2101
2102          if (dead_or_set_p (incr, addr))
2103            {
2104              /* This is the simple case.  Try to make the auto-inc.  If
2105                 we can't, we are done.  Otherwise, we will do any
2106                 needed updates below.  */
2107              if (! validate_change (insn, &XEXP (x, 0),
2108                                     gen_rtx (inc_code, Pmode, addr),
2109                                     0))
2110                return;
2111            }
2112          else if (GET_CODE (q) == REG
2113                   /* PREV_INSN used here to check the semi-open interval
2114                      [insn,incr).  */
2115                   && ! reg_used_between_p (q,  PREV_INSN (insn), incr)
2116                   /* We must also check for sets of q as q may be
2117                      a call clobbered hard register and there may
2118                      be a call between PREV_INSN (insn) and incr.  */
2119                   && ! reg_set_between_p (q,  PREV_INSN (insn), incr))
2120            {
2121              /* We have *p followed sometime later by q = p+size.
2122                 Both p and q must be live afterward,
2123                 and q is not used between INSN and it's assignment.
2124                 Change it to q = p, ...*q..., q = q+size.
2125                 Then fall into the usual case.  */
2126              rtx insns, temp;
2127
2128              start_sequence ();
2129              emit_move_insn (q, addr);
2130              insns = get_insns ();
2131              end_sequence ();
2132
2133              /* If anything in INSNS have UID's that don't fit within the
2134                 extra space we allocate earlier, we can't make this auto-inc.
2135                 This should never happen.  */
2136              for (temp = insns; temp; temp = NEXT_INSN (temp))
2137                {
2138                  if (INSN_UID (temp) > max_uid_for_flow)
2139                    return;
2140                  BLOCK_NUM (temp) = BLOCK_NUM (insn);
2141                }
2142
2143              /* If we can't make the auto-inc, or can't make the
2144                 replacement into Y, exit.  There's no point in making
2145                 the change below if we can't do the auto-inc and doing
2146                 so is not correct in the pre-inc case.  */
2147
2148              validate_change (insn, &XEXP (x, 0),
2149                               gen_rtx (inc_code, Pmode, q),
2150                               1);
2151              validate_change (incr, &XEXP (y, 0), q, 1);
2152              if (! apply_change_group ())
2153                return;
2154
2155              /* We now know we'll be doing this change, so emit the
2156                 new insn(s) and do the updates.  */
2157              emit_insns_before (insns, insn);
2158
2159              if (basic_block_head[BLOCK_NUM (insn)] == insn)
2160                basic_block_head[BLOCK_NUM (insn)] = insns;
2161
2162              /* INCR will become a NOTE and INSN won't contain a
2163                 use of ADDR.  If a use of ADDR was just placed in
2164                 the insn before INSN, make that the next use.
2165                 Otherwise, invalidate it.  */
2166              if (GET_CODE (PREV_INSN (insn)) == INSN
2167                  && GET_CODE (PATTERN (PREV_INSN (insn))) == SET
2168                  && SET_SRC (PATTERN (PREV_INSN (insn))) == addr)
2169                reg_next_use[regno] = PREV_INSN (insn);
2170              else
2171                reg_next_use[regno] = 0;
2172
2173              addr = q;
2174              regno = REGNO (q);
2175
2176              /* REGNO is now used in INCR which is below INSN, but
2177                 it previously wasn't live here.  If we don't mark
2178                 it as needed, we'll put a REG_DEAD note for it
2179                 on this insn, which is incorrect.  */
2180              SET_REGNO_REG_SET (needed, regno);
2181
2182              /* If there are any calls between INSN and INCR, show
2183                 that REGNO now crosses them.  */
2184              for (temp = insn; temp != incr; temp = NEXT_INSN (temp))
2185                if (GET_CODE (temp) == CALL_INSN)
2186                  REG_N_CALLS_CROSSED (regno)++;
2187            }
2188          else
2189            return;
2190
2191          /* If we haven't returned, it means we were able to make the
2192             auto-inc, so update the status.  First, record that this insn
2193             has an implicit side effect.  */
2194
2195          REG_NOTES (insn)
2196            = gen_rtx (EXPR_LIST, REG_INC, addr, REG_NOTES (insn));
2197
2198          /* Modify the old increment-insn to simply copy
2199             the already-incremented value of our register.  */
2200          if (! validate_change (incr, &SET_SRC (set), addr, 0))
2201            abort ();
2202
2203          /* If that makes it a no-op (copying the register into itself) delete
2204             it so it won't appear to be a "use" and a "set" of this
2205             register.  */
2206          if (SET_DEST (set) == addr)
2207            {
2208              PUT_CODE (incr, NOTE);
2209              NOTE_LINE_NUMBER (incr) = NOTE_INSN_DELETED;
2210              NOTE_SOURCE_FILE (incr) = 0;
2211            }
2212
2213          if (regno >= FIRST_PSEUDO_REGISTER)
2214            {
2215              /* Count an extra reference to the reg.  When a reg is
2216                 incremented, spilling it is worse, so we want to make
2217                 that less likely.  */
2218              REG_N_REFS (regno) += loop_depth;
2219
2220              /* Count the increment as a setting of the register,
2221                 even though it isn't a SET in rtl.  */
2222              REG_N_SETS (regno)++;
2223            }
2224        }
2225    }
2226}
2227#endif /* AUTO_INC_DEC */
2228
2229/* Scan expression X and store a 1-bit in LIVE for each reg it uses.
2230   This is done assuming the registers needed from X
2231   are those that have 1-bits in NEEDED.
2232
2233   On the final pass, FINAL is 1.  This means try for autoincrement
2234   and count the uses and deaths of each pseudo-reg.
2235
2236   INSN is the containing instruction.  If INSN is dead, this function is not
2237   called.  */
2238
2239static void
2240mark_used_regs (needed, live, x, final, insn)
2241     regset needed;
2242     regset live;
2243     rtx x;
2244     int final;
2245     rtx insn;
2246{
2247  register RTX_CODE code;
2248  register int regno;
2249  int i;
2250
2251 retry:
2252  code = GET_CODE (x);
2253  switch (code)
2254    {
2255    case LABEL_REF:
2256    case SYMBOL_REF:
2257    case CONST_INT:
2258    case CONST:
2259    case CONST_DOUBLE:
2260    case PC:
2261    case ADDR_VEC:
2262    case ADDR_DIFF_VEC:
2263    case ASM_INPUT:
2264      return;
2265
2266#ifdef HAVE_cc0
2267    case CC0:
2268      cc0_live = 1;
2269      return;
2270#endif
2271
2272    case CLOBBER:
2273      /* If we are clobbering a MEM, mark any registers inside the address
2274         as being used.  */
2275      if (GET_CODE (XEXP (x, 0)) == MEM)
2276        mark_used_regs (needed, live, XEXP (XEXP (x, 0), 0), final, insn);
2277      return;
2278
2279    case MEM:
2280      /* Invalidate the data for the last MEM stored, but only if MEM is
2281         something that can be stored into.  */
2282      if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
2283          && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
2284        ; /* needn't clear last_mem_set */
2285      else
2286        last_mem_set = 0;
2287
2288#ifdef AUTO_INC_DEC
2289      if (final)
2290        find_auto_inc (needed, x, insn);
2291#endif
2292      break;
2293
2294    case SUBREG:
2295      if (GET_CODE (SUBREG_REG (x)) == REG
2296          && REGNO (SUBREG_REG (x)) >= FIRST_PSEUDO_REGISTER
2297          && (GET_MODE_SIZE (GET_MODE (x))
2298              != GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
2299        REG_CHANGES_SIZE (REGNO (SUBREG_REG (x))) = 1;
2300
2301      /* While we're here, optimize this case.  */
2302      x = SUBREG_REG (x);
2303
2304      /* In case the SUBREG is not of a register, don't optimize */
2305      if (GET_CODE (x) != REG)
2306        {
2307          mark_used_regs (needed, live, x, final, insn);
2308          return;
2309        }
2310
2311      /* ... fall through ...  */
2312
2313    case REG:
2314      /* See a register other than being set
2315         => mark it as needed.  */
2316
2317      regno = REGNO (x);
2318      {
2319        int some_needed = REGNO_REG_SET_P (needed, regno);
2320        int some_not_needed = ! some_needed;
2321
2322        SET_REGNO_REG_SET (live, regno);
2323
2324        /* A hard reg in a wide mode may really be multiple registers.
2325           If so, mark all of them just like the first.  */
2326        if (regno < FIRST_PSEUDO_REGISTER)
2327          {
2328            int n;
2329
2330            /* For stack ptr or fixed arg pointer,
2331               nothing below can be necessary, so waste no more time.  */
2332            if (regno == STACK_POINTER_REGNUM
2333#if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2334                || regno == HARD_FRAME_POINTER_REGNUM
2335#endif
2336#if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2337                || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
2338#endif
2339                || regno == FRAME_POINTER_REGNUM)
2340              {
2341                /* If this is a register we are going to try to eliminate,
2342                   don't mark it live here.  If we are successful in
2343                   eliminating it, it need not be live unless it is used for
2344                   pseudos, in which case it will have been set live when
2345                   it was allocated to the pseudos.  If the register will not
2346                   be eliminated, reload will set it live at that point.  */
2347
2348                if (! TEST_HARD_REG_BIT (elim_reg_set, regno))
2349                  regs_ever_live[regno] = 1;
2350                return;
2351              }
2352            /* No death notes for global register variables;
2353               their values are live after this function exits.  */
2354            if (global_regs[regno])
2355              {
2356                if (final)
2357                  reg_next_use[regno] = insn;
2358                return;
2359              }
2360
2361            n = HARD_REGNO_NREGS (regno, GET_MODE (x));
2362            while (--n > 0)
2363              {
2364                int regno_n = regno + n;
2365                int needed_regno = REGNO_REG_SET_P (needed, regno_n);
2366
2367                SET_REGNO_REG_SET (live, regno_n);
2368                some_needed |= needed_regno;
2369                some_not_needed |= ! needed_regno;
2370              }
2371          }
2372        if (final)
2373          {
2374            /* Record where each reg is used, so when the reg
2375               is set we know the next insn that uses it.  */
2376
2377            reg_next_use[regno] = insn;
2378
2379            if (regno < FIRST_PSEUDO_REGISTER)
2380              {
2381                /* If a hard reg is being used,
2382                   record that this function does use it.  */
2383
2384                i = HARD_REGNO_NREGS (regno, GET_MODE (x));
2385                if (i == 0)
2386                  i = 1;
2387                do
2388                  regs_ever_live[regno + --i] = 1;
2389                while (i > 0);
2390              }
2391            else
2392              {
2393                /* Keep track of which basic block each reg appears in.  */
2394
2395                register int blocknum = BLOCK_NUM (insn);
2396
2397                if (REG_BASIC_BLOCK (regno) == REG_BLOCK_UNKNOWN)
2398                  REG_BASIC_BLOCK (regno) = blocknum;
2399                else if (REG_BASIC_BLOCK (regno) != blocknum)
2400                  REG_BASIC_BLOCK (regno) = REG_BLOCK_GLOBAL;
2401
2402                /* Count (weighted) number of uses of each reg.  */
2403
2404                REG_N_REFS (regno) += loop_depth;
2405              }
2406
2407            /* Record and count the insns in which a reg dies.
2408               If it is used in this insn and was dead below the insn
2409               then it dies in this insn.  If it was set in this insn,
2410               we do not make a REG_DEAD note; likewise if we already
2411               made such a note.  */
2412
2413            if (some_not_needed
2414                && ! dead_or_set_p (insn, x)
2415#if 0
2416                && (regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
2417#endif
2418                )
2419              {
2420                /* Check for the case where the register dying partially
2421                   overlaps the register set by this insn.  */
2422                if (regno < FIRST_PSEUDO_REGISTER
2423                    && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
2424                  {
2425                    int n = HARD_REGNO_NREGS (regno, GET_MODE (x));
2426                    while (--n >= 0)
2427                      some_needed |= dead_or_set_regno_p (insn, regno + n);
2428                  }
2429
2430                /* If none of the words in X is needed, make a REG_DEAD
2431                   note.  Otherwise, we must make partial REG_DEAD notes.  */
2432                if (! some_needed)
2433                  {
2434                    REG_NOTES (insn)
2435                      = gen_rtx (EXPR_LIST, REG_DEAD, x, REG_NOTES (insn));
2436                    REG_N_DEATHS (regno)++;
2437                  }
2438                else
2439                  {
2440                    int i;
2441
2442                    /* Don't make a REG_DEAD note for a part of a register
2443                       that is set in the insn.  */
2444
2445                    for (i = HARD_REGNO_NREGS (regno, GET_MODE (x)) - 1;
2446                         i >= 0; i--)
2447                      if (!REGNO_REG_SET_P (needed, regno + i)
2448                          && ! dead_or_set_regno_p (insn, regno + i))
2449                        REG_NOTES (insn)
2450                          = gen_rtx (EXPR_LIST, REG_DEAD,
2451                                     gen_rtx (REG, reg_raw_mode[regno + i],
2452                                              regno + i),
2453                                     REG_NOTES (insn));
2454                  }
2455              }
2456          }
2457      }
2458      return;
2459
2460    case SET:
2461      {
2462        register rtx testreg = SET_DEST (x);
2463        int mark_dest = 0;
2464
2465        /* If storing into MEM, don't show it as being used.  But do
2466           show the address as being used.  */
2467        if (GET_CODE (testreg) == MEM)
2468          {
2469#ifdef AUTO_INC_DEC
2470            if (final)
2471              find_auto_inc (needed, testreg, insn);
2472#endif
2473            mark_used_regs (needed, live, XEXP (testreg, 0), final, insn);
2474            mark_used_regs (needed, live, SET_SRC (x), final, insn);
2475            return;
2476          }
2477           
2478        /* Storing in STRICT_LOW_PART is like storing in a reg
2479           in that this SET might be dead, so ignore it in TESTREG.
2480           but in some other ways it is like using the reg.
2481
2482           Storing in a SUBREG or a bit field is like storing the entire
2483           register in that if the register's value is not used
2484           then this SET is not needed.  */
2485        while (GET_CODE (testreg) == STRICT_LOW_PART
2486               || GET_CODE (testreg) == ZERO_EXTRACT
2487               || GET_CODE (testreg) == SIGN_EXTRACT
2488               || GET_CODE (testreg) == SUBREG)
2489          {
2490            if (GET_CODE (testreg) == SUBREG
2491                && GET_CODE (SUBREG_REG (testreg)) == REG
2492                && REGNO (SUBREG_REG (testreg)) >= FIRST_PSEUDO_REGISTER
2493                && (GET_MODE_SIZE (GET_MODE (testreg))
2494                    != GET_MODE_SIZE (GET_MODE (SUBREG_REG (testreg)))))
2495              REG_CHANGES_SIZE (REGNO (SUBREG_REG (testreg))) = 1;
2496
2497            /* Modifying a single register in an alternate mode
2498               does not use any of the old value.  But these other
2499               ways of storing in a register do use the old value.  */
2500            if (GET_CODE (testreg) == SUBREG
2501                && !(REG_SIZE (SUBREG_REG (testreg)) > REG_SIZE (testreg)))
2502              ;
2503            else
2504              mark_dest = 1;
2505
2506            testreg = XEXP (testreg, 0);
2507          }
2508
2509        /* If this is a store into a register,
2510           recursively scan the value being stored.  */
2511
2512        if (GET_CODE (testreg) == REG
2513            && (regno = REGNO (testreg), regno != FRAME_POINTER_REGNUM)
2514#if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2515            && regno != HARD_FRAME_POINTER_REGNUM
2516#endif
2517#if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2518            && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
2519#endif
2520            )
2521          /* We used to exclude global_regs here, but that seems wrong.
2522             Storing in them is like storing in mem.  */
2523          {
2524            mark_used_regs (needed, live, SET_SRC (x), final, insn);
2525            if (mark_dest)
2526              mark_used_regs (needed, live, SET_DEST (x), final, insn);
2527            return;
2528          }
2529      }
2530      break;
2531
2532    case RETURN:
2533      /* If exiting needs the right stack value, consider this insn as
2534         using the stack pointer.  In any event, consider it as using
2535         all global registers and all registers used by return.  */
2536
2537#ifdef EXIT_IGNORE_STACK
2538      if (! EXIT_IGNORE_STACK
2539          || (! FRAME_POINTER_REQUIRED && flag_omit_frame_pointer))
2540#endif
2541        SET_REGNO_REG_SET (live, STACK_POINTER_REGNUM);
2542
2543      for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2544        if (global_regs[i]
2545#ifdef EPILOGUE_USES
2546            || EPILOGUE_USES (i)
2547#endif
2548            )
2549          SET_REGNO_REG_SET (live, i);
2550      break;
2551
2552    default:
2553      break;
2554    }
2555
2556  /* Recursively scan the operands of this expression.  */
2557
2558  {
2559    register char *fmt = GET_RTX_FORMAT (code);
2560    register int i;
2561   
2562    for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2563      {
2564        if (fmt[i] == 'e')
2565          {
2566            /* Tail recursive case: save a function call level.  */
2567            if (i == 0)
2568              {
2569                x = XEXP (x, 0);
2570                goto retry;
2571              }
2572            mark_used_regs (needed, live, XEXP (x, i), final, insn);
2573          }
2574        else if (fmt[i] == 'E')
2575          {
2576            register int j;
2577            for (j = 0; j < XVECLEN (x, i); j++)
2578              mark_used_regs (needed, live, XVECEXP (x, i, j), final, insn);
2579          }
2580      }
2581  }
2582}
2583
2584#ifdef AUTO_INC_DEC
2585
2586static int
2587try_pre_increment_1 (insn)
2588     rtx insn;
2589{
2590  /* Find the next use of this reg.  If in same basic block,
2591     make it do pre-increment or pre-decrement if appropriate.  */
2592  rtx x = single_set (insn);
2593  HOST_WIDE_INT amount = ((GET_CODE (SET_SRC (x)) == PLUS ? 1 : -1)
2594                * INTVAL (XEXP (SET_SRC (x), 1)));
2595  int regno = REGNO (SET_DEST (x));
2596  rtx y = reg_next_use[regno];
2597  if (y != 0
2598      && BLOCK_NUM (y) == BLOCK_NUM (insn)
2599      /* Don't do this if the reg dies, or gets set in y; a standard addressing
2600         mode would be better.  */
2601      && ! dead_or_set_p (y, SET_DEST (x))
2602      && try_pre_increment (y, SET_DEST (x), amount))
2603    {
2604      /* We have found a suitable auto-increment
2605         and already changed insn Y to do it.
2606         So flush this increment-instruction.  */
2607      PUT_CODE (insn, NOTE);
2608      NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2609      NOTE_SOURCE_FILE (insn) = 0;
2610      /* Count a reference to this reg for the increment
2611         insn we are deleting.  When a reg is incremented.
2612         spilling it is worse, so we want to make that
2613         less likely.  */
2614      if (regno >= FIRST_PSEUDO_REGISTER)
2615        {
2616          REG_N_REFS (regno) += loop_depth;
2617          REG_N_SETS (regno)++;
2618        }
2619      return 1;
2620    }
2621  return 0;
2622}
2623
2624/* Try to change INSN so that it does pre-increment or pre-decrement
2625   addressing on register REG in order to add AMOUNT to REG.
2626   AMOUNT is negative for pre-decrement.
2627   Returns 1 if the change could be made.
2628   This checks all about the validity of the result of modifying INSN.  */
2629
2630static int
2631try_pre_increment (insn, reg, amount)
2632     rtx insn, reg;
2633     HOST_WIDE_INT amount;
2634{
2635  register rtx use;
2636
2637  /* Nonzero if we can try to make a pre-increment or pre-decrement.
2638     For example, addl $4,r1; movl (r1),... can become movl +(r1),...  */
2639  int pre_ok = 0;
2640  /* Nonzero if we can try to make a post-increment or post-decrement.
2641     For example, addl $4,r1; movl -4(r1),... can become movl (r1)+,...
2642     It is possible for both PRE_OK and POST_OK to be nonzero if the machine
2643     supports both pre-inc and post-inc, or both pre-dec and post-dec.  */
2644  int post_ok = 0;
2645
2646  /* Nonzero if the opportunity actually requires post-inc or post-dec.  */
2647  int do_post = 0;
2648
2649  /* From the sign of increment, see which possibilities are conceivable
2650     on this target machine.  */
2651#ifdef HAVE_PRE_INCREMENT
2652  if (amount > 0)
2653    pre_ok = 1;
2654#endif
2655#ifdef HAVE_POST_INCREMENT
2656  if (amount > 0)
2657    post_ok = 1;
2658#endif
2659
2660#ifdef HAVE_PRE_DECREMENT
2661  if (amount < 0)
2662    pre_ok = 1;
2663#endif
2664#ifdef HAVE_POST_DECREMENT
2665  if (amount < 0)
2666    post_ok = 1;
2667#endif
2668
2669  if (! (pre_ok || post_ok))
2670    return 0;
2671
2672  /* It is not safe to add a side effect to a jump insn
2673     because if the incremented register is spilled and must be reloaded
2674     there would be no way to store the incremented value back in memory.  */
2675
2676  if (GET_CODE (insn) == JUMP_INSN)
2677    return 0;
2678
2679  use = 0;
2680  if (pre_ok)
2681    use = find_use_as_address (PATTERN (insn), reg, 0);
2682  if (post_ok && (use == 0 || use == (rtx) 1))
2683    {
2684      use = find_use_as_address (PATTERN (insn), reg, -amount);
2685      do_post = 1;
2686    }
2687
2688  if (use == 0 || use == (rtx) 1)
2689    return 0;
2690
2691  if (GET_MODE_SIZE (GET_MODE (use)) != (amount > 0 ? amount : - amount))
2692    return 0;
2693
2694  /* See if this combination of instruction and addressing mode exists.  */
2695  if (! validate_change (insn, &XEXP (use, 0),
2696                         gen_rtx (amount > 0
2697                                  ? (do_post ? POST_INC : PRE_INC)
2698                                  : (do_post ? POST_DEC : PRE_DEC),
2699                                  Pmode, reg), 0))
2700    return 0;
2701
2702  /* Record that this insn now has an implicit side effect on X.  */
2703  REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_INC, reg, REG_NOTES (insn));
2704  return 1;
2705}
2706
2707#endif /* AUTO_INC_DEC */
2708
2709/* Find the place in the rtx X where REG is used as a memory address.
2710   Return the MEM rtx that so uses it.
2711   If PLUSCONST is nonzero, search instead for a memory address equivalent to
2712   (plus REG (const_int PLUSCONST)).
2713
2714   If such an address does not appear, return 0.
2715   If REG appears more than once, or is used other than in such an address,
2716   return (rtx)1.  */
2717
2718static rtx
2719find_use_as_address (x, reg, plusconst)
2720     register rtx x;
2721     rtx reg;
2722     HOST_WIDE_INT plusconst;
2723{
2724  enum rtx_code code = GET_CODE (x);
2725  char *fmt = GET_RTX_FORMAT (code);
2726  register int i;
2727  register rtx value = 0;
2728  register rtx tem;
2729
2730  if (code == MEM && XEXP (x, 0) == reg && plusconst == 0)
2731    return x;
2732
2733  if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
2734      && XEXP (XEXP (x, 0), 0) == reg
2735      && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2736      && INTVAL (XEXP (XEXP (x, 0), 1)) == plusconst)
2737    return x;
2738
2739  if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
2740    {
2741      /* If REG occurs inside a MEM used in a bit-field reference,
2742         that is unacceptable.  */
2743      if (find_use_as_address (XEXP (x, 0), reg, 0) != 0)
2744        return (rtx) (HOST_WIDE_INT) 1;
2745    }
2746
2747  if (x == reg)
2748    return (rtx) (HOST_WIDE_INT) 1;
2749
2750  for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2751    {
2752      if (fmt[i] == 'e')
2753        {
2754          tem = find_use_as_address (XEXP (x, i), reg, plusconst);
2755          if (value == 0)
2756            value = tem;
2757          else if (tem != 0)
2758            return (rtx) (HOST_WIDE_INT) 1;
2759        }
2760      if (fmt[i] == 'E')
2761        {
2762          register int j;
2763          for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2764            {
2765              tem = find_use_as_address (XVECEXP (x, i, j), reg, plusconst);
2766              if (value == 0)
2767                value = tem;
2768              else if (tem != 0)
2769                return (rtx) (HOST_WIDE_INT) 1;
2770            }
2771        }
2772    }
2773
2774  return value;
2775}
2776
2777/* Write information about registers and basic blocks into FILE.
2778   This is part of making a debugging dump.  */
2779
2780void
2781dump_flow_info (file)
2782     FILE *file;
2783{
2784  register int i;
2785  static char *reg_class_names[] = REG_CLASS_NAMES;
2786
2787  fprintf (file, "%d registers.\n", max_regno);
2788
2789  for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
2790    if (REG_N_REFS (i))
2791      {
2792        enum reg_class class, altclass;
2793        fprintf (file, "\nRegister %d used %d times across %d insns",
2794                 i, REG_N_REFS (i), REG_LIVE_LENGTH (i));
2795        if (REG_BASIC_BLOCK (i) >= 0)
2796          fprintf (file, " in block %d", REG_BASIC_BLOCK (i));
2797        if (REG_N_DEATHS (i) != 1)
2798          fprintf (file, "; dies in %d places", REG_N_DEATHS (i));
2799        if (REG_N_CALLS_CROSSED (i) == 1)
2800          fprintf (file, "; crosses 1 call");
2801        else if (REG_N_CALLS_CROSSED (i))
2802          fprintf (file, "; crosses %d calls", REG_N_CALLS_CROSSED (i));
2803        if (PSEUDO_REGNO_BYTES (i) != UNITS_PER_WORD)
2804          fprintf (file, "; %d bytes", PSEUDO_REGNO_BYTES (i));
2805        class = reg_preferred_class (i);
2806        altclass = reg_alternate_class (i);
2807        if (class != GENERAL_REGS || altclass != ALL_REGS)
2808          {
2809            if (altclass == ALL_REGS || class == ALL_REGS)
2810              fprintf (file, "; pref %s", reg_class_names[(int) class]);
2811            else if (altclass == NO_REGS)
2812              fprintf (file, "; %s or none", reg_class_names[(int) class]);
2813            else
2814              fprintf (file, "; pref %s, else %s",
2815                       reg_class_names[(int) class],
2816                       reg_class_names[(int) altclass]);
2817          }
2818        if (REGNO_POINTER_FLAG (i))
2819          fprintf (file, "; pointer");
2820        fprintf (file, ".\n");
2821      }
2822  fprintf (file, "\n%d basic blocks.\n", n_basic_blocks);
2823  for (i = 0; i < n_basic_blocks; i++)
2824    {
2825      register rtx head, jump;
2826      register int regno;
2827      fprintf (file, "\nBasic block %d: first insn %d, last %d.\n",
2828               i,
2829               INSN_UID (basic_block_head[i]),
2830               INSN_UID (basic_block_end[i]));
2831      /* The control flow graph's storage is freed
2832         now when flow_analysis returns.
2833         Don't try to print it if it is gone.  */
2834      if (basic_block_drops_in)
2835        {
2836          fprintf (file, "Reached from blocks: ");
2837          head = basic_block_head[i];
2838          if (GET_CODE (head) == CODE_LABEL)
2839            for (jump = LABEL_REFS (head);
2840                 jump != head;
2841                 jump = LABEL_NEXTREF (jump))
2842              {
2843                register int from_block = BLOCK_NUM (CONTAINING_INSN (jump));
2844                fprintf (file, " %d", from_block);
2845              }
2846          if (basic_block_drops_in[i])
2847            fprintf (file, " previous");
2848        }
2849      fprintf (file, "\nRegisters live at start:");
2850      for (regno = 0; regno < max_regno; regno++)
2851        if (REGNO_REG_SET_P (basic_block_live_at_start[i], regno))
2852          fprintf (file, " %d", regno);
2853      fprintf (file, "\n");
2854    }
2855  fprintf (file, "\n");
2856}
2857
2858
2859/* Like print_rtl, but also print out live information for the start of each
2860   basic block.  */
2861
2862void
2863print_rtl_with_bb (outf, rtx_first)
2864     FILE *outf;
2865     rtx rtx_first;
2866{
2867  register rtx tmp_rtx;
2868
2869  if (rtx_first == 0)
2870    fprintf (outf, "(nil)\n");
2871
2872  else
2873    {
2874      int i, bb;
2875      enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
2876      int max_uid = get_max_uid ();
2877      int *start = (int *) alloca (max_uid * sizeof (int));
2878      int *end = (int *) alloca (max_uid * sizeof (int));
2879      char *in_bb_p = (char *) alloca (max_uid * sizeof (enum bb_state));
2880
2881      for (i = 0; i < max_uid; i++)
2882        {
2883          start[i] = end[i] = -1;
2884          in_bb_p[i] = NOT_IN_BB;
2885        }
2886
2887      for (i = n_basic_blocks-1; i >= 0; i--)
2888        {
2889          rtx x;
2890          start[INSN_UID (basic_block_head[i])] = i;
2891          end[INSN_UID (basic_block_end[i])] = i;
2892          for (x = basic_block_head[i]; x != NULL_RTX; x = NEXT_INSN (x))
2893            {
2894              in_bb_p[ INSN_UID(x)]
2895                = (in_bb_p[ INSN_UID(x)] == NOT_IN_BB)
2896                 ? IN_ONE_BB : IN_MULTIPLE_BB;
2897              if (x == basic_block_end[i])
2898                break;
2899            }
2900        }
2901
2902      for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx))
2903        {
2904          if ((bb = start[INSN_UID (tmp_rtx)]) >= 0)
2905            {
2906              int pos = 18;
2907
2908              if (PREV_INSN (tmp_rtx) != 0
2909                  && end[INSN_UID (PREV_INSN (tmp_rtx))] >= 0)
2910                fprintf (outf, " start");
2911              else
2912                fprintf (outf, ";; Start");
2913
2914              fprintf (outf, " of basic block %d.\n;; Registers live:", bb);
2915
2916              EXECUTE_IF_SET_IN_REG_SET (basic_block_live_at_start[bb], 0, i,
2917                                         {
2918                                           if (pos > 65)
2919                                             {
2920                                               fprintf (outf, "\n;;\t");
2921                                               pos = 10;
2922                                             }
2923
2924                                           fprintf (outf, " %d", i);
2925                                           pos += (i >= 100 ? 4 : 3);
2926                                           if (i < FIRST_PSEUDO_REGISTER)
2927                                             {
2928                                               fprintf (outf, " [%s]",
2929                                                        reg_names[i]);
2930                                               pos += (strlen (reg_names[i])
2931                                                       + 3);
2932                                             }
2933                                         });
2934              putc ('\n', outf);
2935              putc ('\n', outf);
2936            }
2937
2938          if (in_bb_p[ INSN_UID(tmp_rtx)] == NOT_IN_BB
2939              && GET_CODE (tmp_rtx) != NOTE
2940              && GET_CODE (tmp_rtx) != BARRIER)
2941            fprintf (outf, ";; Insn is not within a basic block\n");
2942          else if (in_bb_p[ INSN_UID(tmp_rtx)] == IN_MULTIPLE_BB)
2943            fprintf (outf, ";; Insn is in multiple basic blocks\n");
2944
2945          print_rtl_single (outf, tmp_rtx);
2946          putc ('\n', outf);
2947
2948          if ((bb = end[INSN_UID (tmp_rtx)]) >= 0)
2949            {
2950              fprintf (outf, "\n;; End of basic block %d", bb);
2951              if (NEXT_INSN (tmp_rtx) != 0
2952                  && start[INSN_UID (NEXT_INSN (tmp_rtx))] >= 0)
2953                fprintf (outf, ";");
2954              else
2955                fprintf (outf, ".\n");
2956            }
2957        }
2958    }
2959}
Note: See TracBrowser for help on using the repository browser.