OSDN Git Service

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