OSDN Git Service

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