OSDN Git Service

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