OSDN Git Service

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