OSDN Git Service

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