OSDN Git Service

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