OSDN Git Service

ccec7929d359bf640a0d3b63d253e20901786577
[pf3gnuchains/gcc-fork.git] / gcc / flow.c
1 /* Data flow analysis for GNU compiler.
2    Copyright (C) 1987, 88, 92-98, 1999 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.  It
23    computes data flow information which tells combine_instructions
24    which insns to consider combining and controls register allocation.
25
26    Additional data flow information that is too bulky to record is
27    generated during the analysis, and is used at that time to create
28    autoincrement and autodecrement addressing.
29
30    The first step is dividing the function into basic blocks.
31    find_basic_blocks does this.  Then life_analysis determines
32    where each register is live and where it is dead.
33
34    ** find_basic_blocks **
35
36    find_basic_blocks divides the current function's rtl into basic
37    blocks and constructs the CFG.  The blocks are recorded in the
38    basic_block_info array; the CFG exists in the edge structures
39    referenced by the blocks.
40
41    find_basic_blocks also finds any unreachable loops and deletes them.
42
43    ** life_analysis **
44
45    life_analysis is called immediately after find_basic_blocks.
46    It uses the basic block information to determine where each
47    hard or pseudo register is live.
48
49    ** live-register info **
50
51    The information about where each register is live is in two parts:
52    the REG_NOTES of insns, and the vector basic_block->global_live_at_start.
53
54    basic_block->global_live_at_start has an element for each basic
55    block, and the element is a bit-vector with a bit for each hard or
56    pseudo register.  The bit is 1 if the register is live at the
57    beginning of the basic block.
58
59    Two types of elements can be added to an insn's REG_NOTES.  
60    A REG_DEAD note is added to an insn's REG_NOTES for any register
61    that meets both of two conditions:  The value in the register is not
62    needed in subsequent insns and the insn does not replace the value in
63    the register (in the case of multi-word hard registers, the value in
64    each register must be replaced by the insn to avoid a REG_DEAD note).
65
66    In the vast majority of cases, an object in a REG_DEAD note will be
67    used somewhere in the insn.  The (rare) exception to this is if an
68    insn uses a multi-word hard register and only some of the registers are
69    needed in subsequent insns.  In that case, REG_DEAD notes will be
70    provided for those hard registers that are not subsequently needed.
71    Partial REG_DEAD notes of this type do not occur when an insn sets
72    only some of the hard registers used in such a multi-word operand;
73    omitting REG_DEAD notes for objects stored in an insn is optional and
74    the desire to do so does not justify the complexity of the partial
75    REG_DEAD notes.
76
77    REG_UNUSED notes are added for each register that is set by the insn
78    but is unused subsequently (if every register set by the insn is unused
79    and the insn does not reference memory or have some other side-effect,
80    the insn is deleted instead).  If only part of a multi-word hard
81    register is used in a subsequent insn, REG_UNUSED notes are made for
82    the parts that will not be used.
83
84    To determine which registers are live after any insn, one can
85    start from the beginning of the basic block and scan insns, noting
86    which registers are set by each insn and which die there.
87
88    ** Other actions of life_analysis **
89
90    life_analysis sets up the LOG_LINKS fields of insns because the
91    information needed to do so is readily available.
92
93    life_analysis deletes insns whose only effect is to store a value
94    that is never used.
95
96    life_analysis notices cases where a reference to a register as
97    a memory address can be combined with a preceding or following
98    incrementation or decrementation of the register.  The separate
99    instruction to increment or decrement is deleted and the address
100    is changed to a POST_INC or similar rtx.
101
102    Each time an incrementing or decrementing address is created,
103    a REG_INC element is added to the insn's REG_NOTES list.
104
105    life_analysis fills in certain vectors containing information about
106    register usage: reg_n_refs, reg_n_deaths, reg_n_sets, reg_live_length,
107    reg_n_calls_crosses and reg_basic_block.
108
109    life_analysis sets current_function_sp_is_unchanging if the function
110    doesn't modify the stack pointer.  */
111
112 /* TODO: 
113
114    Split out from life_analysis:
115         - local property discovery (bb->local_live, bb->local_set)
116         - global property computation
117         - log links creation
118         - pre/post modify transformation
119 */
120 \f
121 #include "config.h"
122 #include "system.h"
123 #include "rtl.h"
124 #include "basic-block.h"
125 #include "insn-config.h"
126 #include "regs.h"
127 #include "hard-reg-set.h"
128 #include "flags.h"
129 #include "output.h"
130 #include "function.h"
131 #include "except.h"
132 #include "toplev.h"
133 #include "recog.h"
134 #include "insn-flags.h"
135
136 #include "obstack.h"
137 #define obstack_chunk_alloc xmalloc
138 #define obstack_chunk_free free
139
140
141 /* EXIT_IGNORE_STACK should be nonzero if, when returning from a function,
142    the stack pointer does not matter.  The value is tested only in
143    functions that have frame pointers.
144    No definition is equivalent to always zero.  */
145 #ifndef EXIT_IGNORE_STACK
146 #define EXIT_IGNORE_STACK 0
147 #endif
148
149
150 /* The contents of the current function definition are allocated
151    in this obstack, and all are freed at the end of the function.
152    For top-level functions, this is temporary_obstack.
153    Separate obstacks are made for nested functions.  */
154
155 extern struct obstack *function_obstack;
156
157 /* Number of basic blocks in the current function.  */
158
159 int n_basic_blocks;
160
161 /* The basic block array.  */
162
163 varray_type basic_block_info;
164
165 /* The special entry and exit blocks.  */
166
167 struct basic_block_def entry_exit_blocks[2] = 
168 {
169   {
170     NULL,                       /* head */
171     NULL,                       /* end */
172     NULL,                       /* pred */
173     NULL,                       /* succ */
174     NULL,                       /* local_set */
175     NULL,                       /* global_live_at_start */
176     NULL,                       /* global_live_at_end */
177     NULL,                       /* aux */
178     ENTRY_BLOCK,                /* index */
179     0                           /* loop_depth */
180   },
181   {
182     NULL,                       /* head */
183     NULL,                       /* end */
184     NULL,                       /* pred */
185     NULL,                       /* succ */
186     NULL,                       /* local_set */
187     NULL,                       /* global_live_at_start */
188     NULL,                       /* global_live_at_end */
189     NULL,                       /* aux */
190     EXIT_BLOCK,                 /* index */
191     0                           /* loop_depth */
192   }
193 };
194
195 /* Nonzero if the second flow pass has completed.  */
196 int flow2_completed;
197
198 /* Maximum register number used in this function, plus one.  */
199
200 int max_regno;
201
202 /* Indexed by n, giving various register information */
203
204 varray_type reg_n_info;
205
206 /* Size of the reg_n_info table.  */
207
208 unsigned int reg_n_max;
209
210 /* Element N is the next insn that uses (hard or pseudo) register number N
211    within the current basic block; or zero, if there is no such insn.
212    This is valid only during the final backward scan in propagate_block.  */
213
214 static rtx *reg_next_use;
215
216 /* Size of a regset for the current function,
217    in (1) bytes and (2) elements.  */
218
219 int regset_bytes;
220 int regset_size;
221
222 /* Regset of regs live when calls to `setjmp'-like functions happen.  */
223 /* ??? Does this exist only for the setjmp-clobbered warning message?  */
224
225 regset regs_live_at_setjmp;
226
227 /* List made of EXPR_LIST rtx's which gives pairs of pseudo registers
228    that have to go in the same hard reg.
229    The first two regs in the list are a pair, and the next two
230    are another pair, etc.  */
231 rtx regs_may_share;
232
233 /* Depth within loops of basic block being scanned for lifetime analysis,
234    plus one.  This is the weight attached to references to registers.  */
235
236 static int loop_depth;
237
238 /* During propagate_block, this is non-zero if the value of CC0 is live.  */
239
240 static int cc0_live;
241
242 /* During propagate_block, this contains a list of all the MEMs we are
243    tracking for dead store elimination. 
244
245    ?!? Note we leak memory by not free-ing items on this list.  We need to
246    write some generic routines to operate on memory lists since cse, gcse,
247    loop, sched, flow and possibly other passes all need to do basically the
248    same operations on these lists.  */
249
250 static rtx mem_set_list;
251
252 /* Set of registers that may be eliminable.  These are handled specially
253    in updating regs_ever_live.  */
254
255 static HARD_REG_SET elim_reg_set;
256
257 /* The basic block structure for every insn, indexed by uid.  */
258
259 varray_type basic_block_for_insn;
260
261 /* The labels mentioned in non-jump rtl.  Valid during find_basic_blocks.  */
262 /* ??? Should probably be using LABEL_NUSES instead.  It would take a 
263    bit of surgery to be able to use or co-opt the routines in jump.  */
264
265 static rtx label_value_list;
266
267 /* INSN_VOLATILE (insn) is 1 if the insn refers to anything volatile.  */
268
269 #define INSN_VOLATILE(INSN) bitmap_bit_p (uid_volatile, INSN_UID (INSN))
270 #define SET_INSN_VOLATILE(INSN) bitmap_set_bit (uid_volatile, INSN_UID (INSN))
271 static bitmap uid_volatile;
272
273 /* Forward declarations */
274 static int count_basic_blocks           PROTO((rtx));
275 static rtx find_basic_blocks_1          PROTO((rtx, rtx*));
276 static void create_basic_block          PROTO((int, rtx, rtx, rtx));
277 static void compute_bb_for_insn         PROTO((varray_type, int));
278 static void clear_edges                 PROTO((void));
279 static void make_edges                  PROTO((rtx, rtx*));
280 static void make_edge                   PROTO((basic_block, basic_block, int));
281 static void make_label_edge             PROTO((basic_block, rtx, int));
282 static void mark_critical_edges         PROTO((void));
283
284 static void commit_one_edge_insertion   PROTO((edge));
285
286 static void delete_unreachable_blocks   PROTO((void));
287 static void delete_eh_regions           PROTO((void));
288 static int can_delete_note_p            PROTO((rtx));
289 static void delete_insn_chain           PROTO((rtx, rtx));
290 static int delete_block                 PROTO((basic_block));
291 static void expunge_block               PROTO((basic_block));
292 static rtx flow_delete_insn             PROTO((rtx));
293 static int can_delete_label_p           PROTO((rtx));
294 static void merge_blocks_nomove         PROTO((basic_block, basic_block));
295 static int merge_blocks                 PROTO((edge,basic_block,basic_block));
296 static void tidy_fallthru_edge          PROTO((edge,basic_block,basic_block));
297 static void calculate_loop_depth        PROTO((rtx));
298
299 static int set_noop_p                   PROTO((rtx));
300 static int noop_move_p                  PROTO((rtx));
301 static void notice_stack_pointer_modification PROTO ((rtx, rtx));
302 static void record_volatile_insns       PROTO((rtx));
303 static void mark_regs_live_at_end       PROTO((regset));
304 static void life_analysis_1             PROTO((rtx, int, int));
305 static void init_regset_vector          PROTO ((regset *, int,
306                                                 struct obstack *));
307 static void propagate_block             PROTO((regset, rtx, rtx, int, 
308                                                regset, int, int));
309 static int insn_dead_p                  PROTO((rtx, regset, int, rtx));
310 static int libcall_dead_p               PROTO((rtx, regset, rtx, rtx));
311 static void mark_set_regs               PROTO((regset, regset, rtx,
312                                                rtx, regset));
313 static void mark_set_1                  PROTO((regset, regset, rtx,
314                                                rtx, regset));
315 #ifdef AUTO_INC_DEC
316 static void find_auto_inc               PROTO((regset, rtx, rtx));
317 static int try_pre_increment_1          PROTO((rtx));
318 static int try_pre_increment            PROTO((rtx, rtx, HOST_WIDE_INT));
319 #endif
320 static void mark_used_regs              PROTO((regset, regset, rtx, int, rtx));
321 void dump_flow_info                     PROTO((FILE *));
322 static void dump_edge_info              PROTO((FILE *, edge, int));
323
324 static int_list_ptr alloc_int_list_node PROTO ((int_list_block **));
325 static int_list_ptr add_int_list_node   PROTO ((int_list_block **,
326                                                 int_list **, int));
327
328 static void add_pred_succ               PROTO ((int, int, int_list_ptr *,
329                                                 int_list_ptr *, int *, int *));
330
331 static void count_reg_sets_1            PROTO ((rtx));
332 static void count_reg_sets              PROTO ((rtx));
333 static void count_reg_references        PROTO ((rtx));
334 static void notice_stack_pointer_modification PROTO ((rtx, rtx));
335 static void invalidate_mems_from_autoinc        PROTO ((rtx));
336 void verify_flow_info                   PROTO ((void));
337 \f
338 /* Find basic blocks of the current function.
339    F is the first insn of the function and NREGS the number of register
340    numbers in use.  */
341
342 void
343 find_basic_blocks (f, nregs, file, do_cleanup)
344      rtx f;
345      int nregs ATTRIBUTE_UNUSED;
346      FILE *file ATTRIBUTE_UNUSED;
347      int do_cleanup;
348 {
349   rtx *bb_eh_end;
350   int max_uid;
351
352   /* Flush out existing data.  */
353   if (basic_block_info != NULL)
354     {
355       int i;
356
357       clear_edges ();
358
359       /* Clear bb->aux on all extant basic blocks.  We'll use this as a 
360          tag for reuse during create_basic_block, just in case some pass
361          copies around basic block notes improperly.  */
362       for (i = 0; i < n_basic_blocks; ++i)
363         BASIC_BLOCK (i)->aux = NULL;
364
365       VARRAY_FREE (basic_block_info);
366     }
367
368   n_basic_blocks = count_basic_blocks (f);
369
370   /* Size the basic block table.  The actual structures will be allocated
371      by find_basic_blocks_1, since we want to keep the structure pointers
372      stable across calls to find_basic_blocks.  */
373   /* ??? This whole issue would be much simpler if we called find_basic_blocks
374      exactly once, and thereafter we don't have a single long chain of 
375      instructions at all until close to the end of compilation when we
376      actually lay them out.  */
377
378   VARRAY_BB_INIT (basic_block_info, n_basic_blocks, "basic_block_info");
379
380   /* An array to record the active exception region at the end of each
381      basic block.  It is filled in by find_basic_blocks_1 for make_edges.  */
382   bb_eh_end = (rtx *) alloca (n_basic_blocks * sizeof (rtx));
383
384   label_value_list = find_basic_blocks_1 (f, bb_eh_end);
385   
386   /* Record the block to which an insn belongs.  */
387   /* ??? This should be done another way, by which (perhaps) a label is
388      tagged directly with the basic block that it starts.  It is used for
389      more than that currently, but IMO that is the only valid use.  */
390
391   max_uid = get_max_uid ();
392 #ifdef AUTO_INC_DEC
393   /* Leave space for insns life_analysis makes in some cases for auto-inc.
394      These cases are rare, so we don't need too much space.  */
395   max_uid += max_uid / 10;
396 #endif
397
398   VARRAY_BB_INIT (basic_block_for_insn, max_uid, "basic_block_for_insn");
399   compute_bb_for_insn (basic_block_for_insn, max_uid);
400
401   /* Discover the edges of our cfg.  */
402
403   make_edges (label_value_list, bb_eh_end);
404
405   /* Delete unreachable blocks.  */
406
407   if (do_cleanup)
408     delete_unreachable_blocks ();
409
410   /* Mark critical edges.  */
411
412   mark_critical_edges ();
413
414   /* Discover the loop depth at the start of each basic block to aid
415      register allocation.  */
416   calculate_loop_depth (f);
417
418   /* Kill the data we won't maintain.  */
419   label_value_list = 0;
420
421 #ifdef ENABLE_CHECKING
422   verify_flow_info ();
423 #endif
424 }
425
426 /* Count the basic blocks of the function.  */
427
428 static int 
429 count_basic_blocks (f)
430      rtx f;
431 {
432   register rtx insn;
433   register RTX_CODE prev_code;
434   register int count = 0;
435   int eh_region = 0;
436   int call_had_abnormal_edge = 0;
437   rtx prev_call = NULL_RTX;
438
439   prev_code = JUMP_INSN;
440   for (insn = f; insn; insn = NEXT_INSN (insn))
441     {
442       register RTX_CODE code = GET_CODE (insn);
443
444       if (code == CODE_LABEL
445           || (GET_RTX_CLASS (code) == 'i'
446               && (prev_code == JUMP_INSN
447                   || prev_code == BARRIER
448                   || (prev_code == CALL_INSN && call_had_abnormal_edge))))
449         {
450           count++;
451
452           /* If the previous insn was a call that did not create an
453              abnormal edge, we want to add a nop so that the CALL_INSN
454              itself is not at basic_block_end.  This allows us to
455              easily distinguish between normal calls and those which
456              create abnormal edges in the flow graph.  */
457
458           if (count > 0 && prev_call != 0 && !call_had_abnormal_edge)
459             {
460               rtx nop = gen_rtx_USE (VOIDmode, const0_rtx);
461               emit_insn_after (nop, prev_call);
462             }
463         }
464
465       /* Record whether this call created an edge.  */
466       if (code == CALL_INSN)
467         {
468           rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
469           int region = (note ? XINT (XEXP (note, 0), 0) : 1);
470           prev_call = insn;
471           call_had_abnormal_edge = 0;
472
473           /* If there is a specified EH region, we have an edge.  */
474           if (eh_region && region > 0)
475             call_had_abnormal_edge = 1;
476           else
477             {
478               /* If there is a nonlocal goto label and the specified
479                  region number isn't -1, we have an edge. (0 means
480                  no throw, but might have a nonlocal goto).  */
481               if (nonlocal_goto_handler_labels && region >= 0)
482                 call_had_abnormal_edge = 1;
483             }
484         }
485       else if (code != NOTE)
486         prev_call = NULL_RTX;
487
488       if (code != NOTE)
489         prev_code = code;
490       else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG)
491         ++eh_region;
492       else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END)
493         --eh_region;
494
495     }
496
497   /* The rest of the compiler works a bit smoother when we don't have to
498      check for the edge case of do-nothing functions with no basic blocks.  */
499   if (count == 0)
500     {
501       emit_insn (gen_rtx_USE (VOIDmode, const0_rtx));
502       count = 1;
503     }
504
505   return count;
506 }
507
508 /* Find all basic blocks of the function whose first insn is F.
509    Store the correct data in the tables that describe the basic blocks,
510    set up the chains of references for each CODE_LABEL, and
511    delete any entire basic blocks that cannot be reached.
512
513    NONLOCAL_LABEL_LIST is a list of non-local labels in the function.  Blocks
514    that are otherwise unreachable may be reachable with a non-local goto.
515
516    BB_EH_END is an array in which we record the list of exception regions
517    active at the end of every basic block.  */
518
519 static rtx
520 find_basic_blocks_1 (f, bb_eh_end)
521      rtx f;
522      rtx *bb_eh_end;
523 {
524   register rtx insn, next;
525   int call_has_abnormal_edge = 0;
526   int i = 0;
527   rtx bb_note = NULL_RTX;
528   rtx eh_list = NULL_RTX;
529   rtx label_value_list = NULL_RTX;
530   rtx head = NULL_RTX;
531   rtx end = NULL_RTX;
532   
533   /* We process the instructions in a slightly different way than we did
534      previously.  This is so that we see a NOTE_BASIC_BLOCK after we have
535      closed out the previous block, so that it gets attached at the proper
536      place.  Since this form should be equivalent to the previous,
537      find_basic_blocks_0 continues to use the old form as a check.  */
538
539   for (insn = f; insn; insn = next)
540     {
541       enum rtx_code code = GET_CODE (insn);
542
543       next = NEXT_INSN (insn);
544
545       if (code == CALL_INSN)
546         {
547           /* Record whether this call created an edge.  */
548           rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
549           int region = (note ? XINT (XEXP (note, 0), 0) : 1);
550           call_has_abnormal_edge = 0;
551
552           /* If there is an EH region, we have an edge.  */
553           if (eh_list && region > 0)
554             call_has_abnormal_edge = 1;
555           else
556             {
557               /* If there is a nonlocal goto label and the specified
558                  region number isn't -1, we have an edge. (0 means
559                  no throw, but might have a nonlocal goto).  */
560               if (nonlocal_goto_handler_labels && region >= 0)
561                 call_has_abnormal_edge = 1;
562             }
563         }
564
565       switch (code)
566         {
567         case NOTE:
568           {
569             int kind = NOTE_LINE_NUMBER (insn);
570
571             /* Keep a LIFO list of the currently active exception notes.  */
572             if (kind == NOTE_INSN_EH_REGION_BEG)
573               eh_list = gen_rtx_INSN_LIST (VOIDmode, insn, eh_list);
574             else if (kind == NOTE_INSN_EH_REGION_END)
575               eh_list = XEXP (eh_list, 1);
576
577             /* Look for basic block notes with which to keep the 
578                basic_block_info pointers stable.  Unthread the note now;
579                we'll put it back at the right place in create_basic_block.
580                Or not at all if we've already found a note in this block.  */
581             else if (kind == NOTE_INSN_BASIC_BLOCK)
582               {
583                 if (bb_note == NULL_RTX)
584                   bb_note = insn;
585                 next = flow_delete_insn (insn);
586               }
587
588             break;
589           }
590
591         case CODE_LABEL:
592           /* A basic block starts at a label.  If we've closed one off due 
593              to a barrier or some such, no need to do it again.  */
594           if (head != NULL_RTX)
595             {
596               /* While we now have edge lists with which other portions of
597                  the compiler might determine a call ending a basic block
598                  does not imply an abnormal edge, it will be a bit before
599                  everything can be updated.  So continue to emit a noop at
600                  the end of such a block.  */
601               if (GET_CODE (end) == CALL_INSN)
602                 {
603                   rtx nop = gen_rtx_USE (VOIDmode, const0_rtx);
604                   end = emit_insn_after (nop, end);
605                 }
606
607               bb_eh_end[i] = eh_list;
608               create_basic_block (i++, head, end, bb_note);
609               bb_note = NULL_RTX;
610             }
611           head = end = insn;
612           break;
613
614         case JUMP_INSN:
615           /* A basic block ends at a jump.  */
616           if (head == NULL_RTX)
617             head = insn;
618           else
619             {
620               /* ??? Make a special check for table jumps.  The way this 
621                  happens is truely and amazingly gross.  We are about to
622                  create a basic block that contains just a code label and
623                  an addr*vec jump insn.  Worse, an addr_diff_vec creates
624                  its own natural loop.
625
626                  Prevent this bit of brain damage, pasting things together
627                  correctly in make_edges.  
628
629                  The correct solution involves emitting the table directly
630                  on the tablejump instruction as a note, or JUMP_LABEL.  */
631
632               if (GET_CODE (PATTERN (insn)) == ADDR_VEC
633                   || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
634                 {
635                   head = end = NULL;
636                   n_basic_blocks--;
637                   break;
638                 }
639             }
640           end = insn;
641           goto new_bb_inclusive;
642
643         case BARRIER:
644           /* A basic block ends at a barrier.  It may be that an unconditional
645              jump already closed the basic block -- no need to do it again.  */
646           if (head == NULL_RTX)
647             break;
648
649           /* While we now have edge lists with which other portions of the
650              compiler might determine a call ending a basic block does not
651              imply an abnormal edge, it will be a bit before everything can
652              be updated.  So continue to emit a noop at the end of such a
653              block.  */
654           if (GET_CODE (end) == CALL_INSN)
655             {
656               rtx nop = gen_rtx_USE (VOIDmode, const0_rtx);
657               end = emit_insn_after (nop, end);
658             }
659           goto new_bb_exclusive;
660
661         case CALL_INSN:
662           /* A basic block ends at a call that can either throw or
663              do a non-local goto.  */
664           if (call_has_abnormal_edge)
665             {
666             new_bb_inclusive:
667               if (head == NULL_RTX)
668                 head = insn;
669               end = insn;
670
671             new_bb_exclusive:
672               bb_eh_end[i] = eh_list;
673               create_basic_block (i++, head, end, bb_note);
674               head = end = NULL_RTX;
675               bb_note = NULL_RTX;
676               break;
677             }
678           /* FALLTHRU */
679
680         default:
681           if (GET_RTX_CLASS (code) == 'i')
682             {
683               if (head == NULL_RTX)
684                 head = insn;
685               end = insn;
686             }
687           break;
688         }
689
690       if (GET_RTX_CLASS (code) == 'i')
691         {
692           rtx note;
693
694           /* Make a list of all labels referred to other than by jumps
695              (which just don't have the REG_LABEL notes). 
696
697              Make a special exception for labels followed by an ADDR*VEC,
698              as this would be a part of the tablejump setup code. 
699
700              Make a special exception for the eh_return_stub_label, which
701              we know isn't part of any otherwise visible control flow.  */
702              
703           for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
704             if (REG_NOTE_KIND (note) == REG_LABEL)
705               {
706                 rtx lab = XEXP (note, 0), next;
707
708                 if (lab == eh_return_stub_label)
709                   ;
710                 else if ((next = next_nonnote_insn (lab)) != NULL
711                          && GET_CODE (next) == JUMP_INSN
712                          && (GET_CODE (PATTERN (next)) == ADDR_VEC
713                              || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC))
714                   ;
715                 else
716                   label_value_list
717                     = gen_rtx_EXPR_LIST (VOIDmode, XEXP (note, 0),
718                                          label_value_list);
719               }
720         }
721     }
722
723   if (head != NULL_RTX)
724     {
725       bb_eh_end[i] = eh_list;
726       create_basic_block (i++, head, end, bb_note);
727     }
728
729   if (i != n_basic_blocks)
730     abort ();
731
732   return label_value_list;
733 }
734
735 /* Create a new basic block consisting of the instructions between
736    HEAD and END inclusive.  Reuses the note and basic block struct
737    in BB_NOTE, if any.  */
738
739 static void
740 create_basic_block (index, head, end, bb_note)
741      int index;
742      rtx head, end, bb_note;
743 {
744   basic_block bb;
745
746   if (bb_note
747       && ! RTX_INTEGRATED_P (bb_note)
748       && (bb = NOTE_BASIC_BLOCK (bb_note)) != NULL
749       && bb->aux == NULL)
750     {
751       /* If we found an existing note, thread it back onto the chain.  */
752
753       if (GET_CODE (head) == CODE_LABEL)
754         add_insn_after (bb_note, head);
755       else
756         {
757           add_insn_before (bb_note, head);
758           head = bb_note;
759         }
760     }
761   else
762     {
763       /* Otherwise we must create a note and a basic block structure.
764          Since we allow basic block structs in rtl, give the struct
765          the same lifetime by allocating it off the function obstack
766          rather than using malloc.  */
767
768       bb = (basic_block) obstack_alloc (function_obstack, sizeof (*bb));
769       memset (bb, 0, sizeof (*bb));
770
771       if (GET_CODE (head) == CODE_LABEL)
772         bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK, head);
773       else
774         {
775           bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, head);
776           head = bb_note;
777         }
778       NOTE_BASIC_BLOCK (bb_note) = bb;
779     }
780
781   /* Always include the bb note in the block.  */
782   if (NEXT_INSN (end) == bb_note)
783     end = bb_note;
784
785   bb->head = head;
786   bb->end = end;
787   bb->index = index;
788   BASIC_BLOCK (index) = bb;
789
790   /* Tag the block so that we know it has been used when considering
791      other basic block notes.  */
792   bb->aux = bb;
793 }
794 \f
795 /* Records the basic block struct in BB_FOR_INSN, for every instruction
796    indexed by INSN_UID.  MAX is the size of the array.  */
797
798 static void
799 compute_bb_for_insn (bb_for_insn, max)
800      varray_type bb_for_insn;
801      int max;
802 {
803   int i;
804
805   for (i = 0; i < n_basic_blocks; ++i)
806     {
807       basic_block bb = BASIC_BLOCK (i);
808       rtx insn, end;
809
810       end = bb->end;
811       insn = bb->head;
812       while (1)
813         {
814           int uid = INSN_UID (insn);
815           if (uid < max)
816             VARRAY_BB (bb_for_insn, uid) = bb;
817           if (insn == end)
818             break;
819           insn = NEXT_INSN (insn);
820         }
821     }
822 }
823
824 /* Free the memory associated with the edge structures.  */
825
826 static void
827 clear_edges ()
828 {
829   int i;
830   edge n, e;
831
832   for (i = 0; i < n_basic_blocks; ++i)
833     {
834       basic_block bb = BASIC_BLOCK (i);
835
836       for (e = bb->succ; e ; e = n)
837         {
838           n = e->succ_next;
839           free (e);
840         }
841
842       bb->succ = 0;
843       bb->pred = 0;
844     }
845
846   for (e = ENTRY_BLOCK_PTR->succ; e ; e = n)
847     {
848       n = e->succ_next;
849       free (e);
850     }
851
852   ENTRY_BLOCK_PTR->succ = 0;
853   EXIT_BLOCK_PTR->pred = 0;
854 }
855
856 /* Identify the edges between basic blocks.
857
858    NONLOCAL_LABEL_LIST is a list of non-local labels in the function.  Blocks
859    that are otherwise unreachable may be reachable with a non-local goto.
860
861    BB_EH_END is an array indexed by basic block number in which we record 
862    the list of exception regions active at the end of the basic block.  */
863
864 static void
865 make_edges (label_value_list, bb_eh_end)
866      rtx label_value_list;
867      rtx *bb_eh_end;
868 {
869   int i;
870   eh_nesting_info *eh_nest_info = init_eh_nesting_info ();
871
872   /* Assume no computed jump; revise as we create edges.  */
873   current_function_has_computed_jump = 0;
874
875   /* By nature of the way these get numbered, block 0 is always the entry.  */
876   make_edge (ENTRY_BLOCK_PTR, BASIC_BLOCK (0), EDGE_FALLTHRU);
877
878   for (i = 0; i < n_basic_blocks; ++i)
879     {
880       basic_block bb = BASIC_BLOCK (i);
881       rtx insn, x, eh_list;
882       enum rtx_code code;
883       int force_fallthru = 0;
884
885       /* If we have asynchronous exceptions, scan the notes for all exception
886          regions active in the block.  In the normal case, we only need the
887          one active at the end of the block, which is bb_eh_end[i].  */
888
889       eh_list = bb_eh_end[i];
890       if (asynchronous_exceptions)
891         {
892           for (insn = bb->end; insn != bb->head; insn = PREV_INSN (insn))
893             {
894               if (GET_CODE (insn) == NOTE
895                   && NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END)
896                 eh_list = gen_rtx_INSN_LIST (VOIDmode, insn, eh_list);
897             }
898         }
899
900       /* Now examine the last instruction of the block, and discover the
901          ways we can leave the block.  */
902
903       insn = bb->end;
904       code = GET_CODE (insn);
905
906       /* A branch.  */
907       if (code == JUMP_INSN)
908         {
909           rtx tmp;
910
911           /* ??? Recognize a tablejump and do the right thing.  */
912           if ((tmp = JUMP_LABEL (insn)) != NULL_RTX
913               && (tmp = NEXT_INSN (tmp)) != NULL_RTX
914               && GET_CODE (tmp) == JUMP_INSN
915               && (GET_CODE (PATTERN (tmp)) == ADDR_VEC
916                   || GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC))
917             {
918               rtvec vec;
919               int j;
920
921               if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
922                 vec = XVEC (PATTERN (tmp), 0);
923               else
924                 vec = XVEC (PATTERN (tmp), 1);
925
926               for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
927                 make_label_edge (bb, XEXP (RTVEC_ELT (vec, j), 0), 0);
928
929               /* Some targets (eg, ARM) emit a conditional jump that also
930                  contains the out-of-range target.  Scan for these and
931                  add an edge if necessary.  */
932               if ((tmp = single_set (insn)) != NULL
933                   && SET_DEST (tmp) == pc_rtx
934                   && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
935                   && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
936                 make_label_edge (bb, XEXP (XEXP (SET_SRC (tmp), 2), 0), 0);
937
938 #ifdef CASE_DROPS_THROUGH
939               /* Silly VAXen.  The ADDR_VEC is going to be in the way of
940                  us naturally detecting fallthru into the next block.  */
941               force_fallthru = 1;
942 #endif
943             }
944
945           /* If this is a computed jump, then mark it as reaching
946              everything on the label_value_list and forced_labels list.  */
947           else if (computed_jump_p (insn))
948             {
949               current_function_has_computed_jump = 1;
950
951               for (x = label_value_list; x; x = XEXP (x, 1))
952                 make_label_edge (bb, XEXP (x, 0), EDGE_ABNORMAL);
953               
954               for (x = forced_labels; x; x = XEXP (x, 1))
955                 make_label_edge (bb, XEXP (x, 0), EDGE_ABNORMAL);
956             }
957
958           /* Returns create an exit out.  */
959           else if (returnjump_p (insn))
960             make_edge (bb, EXIT_BLOCK_PTR, 0);
961
962           /* Otherwise, we have a plain conditional or unconditional jump.  */
963           else
964             {
965               if (! JUMP_LABEL (insn))
966                 abort ();
967               make_label_edge (bb, JUMP_LABEL (insn), 0);
968             }
969         }
970
971       /* If this is a CALL_INSN, then mark it as reaching the active EH
972          handler for this CALL_INSN.  If we're handling asynchronous
973          exceptions then any insn can reach any of the active handlers.
974
975          Also mark the CALL_INSN as reaching any nonlocal goto handler.  */
976
977       if (code == CALL_INSN || asynchronous_exceptions)
978         {
979           int is_call = (code == CALL_INSN ? EDGE_ABNORMAL_CALL : 0);
980           handler_info **handler_list;
981           int eh_region = -1;
982           int num;
983
984           if (eh_list)
985             eh_region = NOTE_BLOCK_NUMBER (XEXP (eh_list, 0));
986
987           num = reachable_handlers (eh_region, eh_nest_info,
988                                     insn, &handler_list);
989           for ( ; num > 0; num--)
990             {
991               make_label_edge (bb, handler_list[num - 1]->handler_label,
992                                EDGE_ABNORMAL | EDGE_EH | is_call);
993             }
994
995           if (code == CALL_INSN && nonlocal_goto_handler_labels)
996             {
997               /* ??? This could be made smarter: in some cases it's possible
998                  to tell that certain calls will not do a nonlocal goto.
999
1000                  For example, if the nested functions that do the nonlocal
1001                  gotos do not have their addresses taken, then only calls to
1002                  those functions or to other nested functions that use them
1003                  could possibly do nonlocal gotos.  */
1004               /* We do know that a REG_EH_REGION note with a value less
1005                  than 0 is guaranteed not to perform a non-local goto.  */
1006               rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1007               if (!note || XINT (XEXP (note, 0), 0) >=  0)
1008                 for (x = nonlocal_goto_handler_labels; x ; x = XEXP (x, 1))
1009                   make_label_edge (bb, XEXP (x, 0),
1010                                    EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
1011             }
1012         }
1013
1014       /* We know something about the structure of the function __throw in
1015          libgcc2.c.  It is the only function that ever contains eh_stub
1016          labels.  It modifies its return address so that the last block
1017          returns to one of the eh_stub labels within it.  So we have to
1018          make additional edges in the flow graph.  */
1019       if (i + 1 == n_basic_blocks && eh_return_stub_label != 0)
1020         make_label_edge (bb, eh_return_stub_label, EDGE_EH);
1021
1022       /* Find out if we can drop through to the next block.  */
1023       insn = next_nonnote_insn (insn);
1024       if (!insn || (i + 1 == n_basic_blocks && force_fallthru))
1025         make_edge (bb, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
1026       else if (i + 1 < n_basic_blocks)
1027         {
1028           rtx tmp = BLOCK_HEAD (i + 1);
1029           if (GET_CODE (tmp) == NOTE)
1030             tmp = next_nonnote_insn (tmp);
1031           if (force_fallthru || insn == tmp)
1032             make_edge (bb, BASIC_BLOCK (i + 1), EDGE_FALLTHRU);
1033         }
1034     }
1035   free_eh_nesting_info (eh_nest_info);
1036 }
1037
1038 /* Create an edge between two basic blocks.  FLAGS are auxiliary information
1039    about the edge that is accumulated between calls.  */
1040
1041 static void
1042 make_edge (src, dst, flags)
1043      basic_block src, dst;
1044      int flags;
1045 {
1046   edge e;
1047
1048   /* Make sure we don't add duplicate edges.  */
1049
1050   for (e = src->succ; e ; e = e->succ_next)
1051     if (e->dest == dst)
1052       {
1053         e->flags |= flags;
1054         return;
1055       }
1056
1057   e = (edge) xcalloc (1, sizeof (*e));
1058
1059   e->succ_next = src->succ;
1060   e->pred_next = dst->pred;
1061   e->src = src;
1062   e->dest = dst;
1063   e->flags = flags;
1064
1065   src->succ = e;
1066   dst->pred = e;
1067 }
1068
1069 /* Create an edge from a basic block to a label.  */
1070
1071 static void
1072 make_label_edge (src, label, flags)
1073      basic_block src;
1074      rtx label;
1075      int flags;
1076 {
1077   if (GET_CODE (label) != CODE_LABEL)
1078     abort ();
1079
1080   /* If the label was never emitted, this insn is junk, but avoid a
1081      crash trying to refer to BLOCK_FOR_INSN (label).  This can happen
1082      as a result of a syntax error and a diagnostic has already been
1083      printed.  */
1084
1085   if (INSN_UID (label) == 0)
1086     return;
1087
1088   make_edge (src, BLOCK_FOR_INSN (label), flags);
1089 }
1090
1091 /* Identify critical edges and set the bits appropriately.  */
1092 static void
1093 mark_critical_edges ()
1094 {
1095   int i, n = n_basic_blocks;
1096   basic_block bb;
1097
1098   /* We begin with the entry block.  This is not terribly important now,
1099      but could be if a front end (Fortran) implemented alternate entry
1100      points.  */
1101   bb = ENTRY_BLOCK_PTR;
1102   i = -1;
1103
1104   while (1)
1105     {
1106       edge e;
1107
1108       /* (1) Critical edges must have a source with multiple successors.  */
1109       if (bb->succ && bb->succ->succ_next)
1110         {
1111           for (e = bb->succ; e ; e = e->succ_next)
1112             {
1113               /* (2) Critical edges must have a destination with multiple
1114                  predecessors.  Note that we know there is at least one
1115                  predecessor -- the edge we followed to get here.  */
1116               if (e->dest->pred->pred_next)
1117                 e->flags |= EDGE_CRITICAL;
1118               else
1119                 e->flags &= ~EDGE_CRITICAL;
1120             }
1121         }
1122       else
1123         {
1124           for (e = bb->succ; e ; e = e->succ_next)
1125             e->flags &= ~EDGE_CRITICAL;
1126         }
1127
1128       if (++i >= n)
1129         break;
1130       bb = BASIC_BLOCK (i);
1131     }
1132 }
1133 \f
1134 /* Split a (typically critical) edge.  Return the new block.
1135    Abort on abnormal edges. 
1136
1137    ??? The code generally expects to be called on critical edges.
1138    The case of a block ending in an unconditional jump to a 
1139    block with multiple predecessors is not handled optimally.  */
1140
1141 basic_block
1142 split_edge (edge_in)
1143      edge edge_in;
1144 {
1145   basic_block old_pred, bb, old_succ;
1146   edge edge_out;
1147   rtx bb_note;
1148   int i;
1149  
1150   /* Abnormal edges cannot be split.  */
1151   if ((edge_in->flags & EDGE_ABNORMAL) != 0)
1152     abort ();
1153
1154   old_pred = edge_in->src;
1155   old_succ = edge_in->dest;
1156
1157   /* Remove the existing edge from the destination's pred list.  */
1158   {
1159     edge *pp;
1160     for (pp = &old_succ->pred; *pp != edge_in; pp = &(*pp)->pred_next)
1161       continue;
1162     *pp = edge_in->pred_next;
1163     edge_in->pred_next = NULL;
1164   }
1165
1166   /* Create the new structures.  */
1167   bb = (basic_block) obstack_alloc (function_obstack, sizeof (*bb));
1168   edge_out = (edge) xcalloc (1, sizeof (*edge_out));
1169
1170   memset (bb, 0, sizeof (*bb));
1171   bb->local_set = OBSTACK_ALLOC_REG_SET (function_obstack);
1172   bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (function_obstack);
1173   bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (function_obstack);
1174
1175   /* ??? This info is likely going to be out of date very soon.  */
1176   CLEAR_REG_SET (bb->local_set);
1177   if (old_succ->global_live_at_start)
1178     {
1179       COPY_REG_SET (bb->global_live_at_start, old_succ->global_live_at_start);
1180       COPY_REG_SET (bb->global_live_at_end, old_succ->global_live_at_start);
1181     }
1182   else
1183     {
1184       CLEAR_REG_SET (bb->global_live_at_start);
1185       CLEAR_REG_SET (bb->global_live_at_end);
1186     }
1187
1188   /* Wire them up.  */
1189   bb->pred = edge_in;
1190   bb->succ = edge_out;
1191
1192   edge_in->dest = bb;
1193   edge_in->flags &= ~EDGE_CRITICAL;
1194
1195   edge_out->pred_next = old_succ->pred;
1196   edge_out->succ_next = NULL;
1197   edge_out->src = bb;
1198   edge_out->dest = old_succ;
1199   edge_out->flags = EDGE_FALLTHRU;
1200   edge_out->probability = REG_BR_PROB_BASE;
1201
1202   old_succ->pred = edge_out;
1203
1204   /* Tricky case -- if there existed a fallthru into the successor
1205      (and we're not it) we must add a new unconditional jump around
1206      the new block we're actually interested in. 
1207
1208      Further, if that edge is critical, this means a second new basic
1209      block must be created to hold it.  In order to simplify correct
1210      insn placement, do this before we touch the existing basic block
1211      ordering for the block we were really wanting.  */
1212   if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1213     {
1214       edge e;
1215       for (e = edge_out->pred_next; e ; e = e->pred_next)
1216         if (e->flags & EDGE_FALLTHRU)
1217           break;
1218
1219       if (e)
1220         {
1221           basic_block jump_block;
1222           rtx pos;
1223
1224           if ((e->flags & EDGE_CRITICAL) == 0)
1225             {
1226               /* Non critical -- we can simply add a jump to the end
1227                  of the existing predecessor.  */
1228               jump_block = e->src;
1229             }
1230           else
1231             {
1232               /* We need a new block to hold the jump.  The simplest
1233                  way to do the bulk of the work here is to recursively
1234                  call ourselves.  */
1235               jump_block = split_edge (e);
1236               e = jump_block->succ;
1237             }
1238
1239           /* Now add the jump insn ...  */
1240           pos = emit_jump_insn_after (gen_jump (old_succ->head),
1241                                       jump_block->end);
1242           jump_block->end = pos;
1243           emit_barrier_after (pos);
1244
1245           /* ... let jump know that label is in use, ...  */
1246           JUMP_LABEL (pos) = old_succ->head;
1247           ++LABEL_NUSES (old_succ->head);
1248           
1249           /* ... and clear fallthru on the outgoing edge.  */
1250           e->flags &= ~EDGE_FALLTHRU;
1251
1252           /* Continue splitting the interesting edge.  */
1253         }
1254     }
1255
1256   /* Place the new block just in front of the successor.  */
1257   VARRAY_GROW (basic_block_info, ++n_basic_blocks);
1258   for (i = n_basic_blocks - 1; i > old_succ->index; --i)
1259     {
1260       basic_block tmp = BASIC_BLOCK (i - 1);
1261       BASIC_BLOCK (i) = tmp;
1262       tmp->index = i;
1263     }
1264   BASIC_BLOCK (i) = bb;
1265   bb->index = i;
1266
1267   /* Create the basic block note.  */
1268   bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, old_succ->head);
1269   NOTE_BASIC_BLOCK (bb_note) = bb;
1270   bb->head = bb->end = bb_note;
1271
1272   /* Not quite simple -- for non-fallthru edges, we must adjust the
1273      predecessor's jump instruction to target our new block.  */
1274   if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1275     {
1276       rtx tmp, insn = old_pred->end;
1277       rtx old_label = old_succ->head;
1278       rtx new_label = gen_label_rtx ();
1279
1280       if (GET_CODE (insn) != JUMP_INSN)
1281         abort ();
1282
1283       /* ??? Recognize a tablejump and adjust all matching cases.  */
1284       if ((tmp = JUMP_LABEL (insn)) != NULL_RTX
1285           && (tmp = NEXT_INSN (tmp)) != NULL_RTX
1286           && GET_CODE (tmp) == JUMP_INSN
1287           && (GET_CODE (PATTERN (tmp)) == ADDR_VEC
1288               || GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC))
1289         {
1290           rtvec vec;
1291           int j;
1292
1293           if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
1294             vec = XVEC (PATTERN (tmp), 0);
1295           else
1296             vec = XVEC (PATTERN (tmp), 1);
1297
1298           for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
1299             if (XEXP (RTVEC_ELT (vec, j), 0) == old_label)
1300               {
1301                 RTVEC_ELT (vec, j) = gen_rtx_LABEL_REF (VOIDmode, new_label);
1302                 --LABEL_NUSES (old_label);
1303                 ++LABEL_NUSES (new_label);
1304               }
1305         }
1306       else
1307         {
1308           /* This would have indicated an abnormal edge.  */
1309           if (computed_jump_p (insn))
1310             abort ();
1311
1312           /* A return instruction can't be redirected.  */
1313           if (returnjump_p (insn))
1314             abort ();
1315
1316           /* If the insn doesn't go where we think, we're confused.  */
1317           if (JUMP_LABEL (insn) != old_label)
1318             abort ();
1319
1320           redirect_jump (insn, new_label);
1321         }
1322
1323       emit_label_before (new_label, bb_note);
1324       bb->head = new_label;
1325     }
1326
1327   return bb;
1328 }
1329
1330 /* Queue instructions for insertion on an edge between two basic blocks.
1331    The new instructions and basic blocks (if any) will not appear in the
1332    CFG until commit_edge_insertions is called.  */
1333
1334 void
1335 insert_insn_on_edge (pattern, e)
1336      rtx pattern;
1337      edge e;
1338 {
1339   /* We cannot insert instructions on an abnormal critical edge.
1340      It will be easier to find the culprit if we die now.  */
1341   if ((e->flags & (EDGE_ABNORMAL|EDGE_CRITICAL))
1342       == (EDGE_ABNORMAL|EDGE_CRITICAL))
1343     abort ();
1344
1345   if (e->insns == NULL_RTX)
1346     start_sequence ();
1347   else
1348     push_to_sequence (e->insns);
1349
1350   emit_insn (pattern);
1351
1352   e->insns = get_insns ();
1353   end_sequence();
1354 }
1355
1356 /* Update the CFG for the instructions queued on edge E.  */
1357
1358 static void
1359 commit_one_edge_insertion (e)
1360      edge e;
1361 {
1362   rtx before = NULL_RTX, after = NULL_RTX, tmp;
1363   basic_block bb;
1364
1365   /* Figure out where to put these things.  If the destination has
1366      one predecessor, insert there.  Except for the exit block.  */
1367   if (e->dest->pred->pred_next == NULL
1368       && e->dest != EXIT_BLOCK_PTR)
1369     {
1370       bb = e->dest;
1371
1372       /* Get the location correct wrt a code label, and "nice" wrt
1373          a basic block note, and before everything else.  */
1374       tmp = bb->head;
1375       if (GET_CODE (tmp) == CODE_LABEL)
1376         tmp = NEXT_INSN (tmp);
1377       if (GET_CODE (tmp) == NOTE
1378           && NOTE_LINE_NUMBER (tmp) == NOTE_INSN_BASIC_BLOCK)
1379         tmp = NEXT_INSN (tmp);
1380       if (tmp == bb->head)
1381         before = tmp;
1382       else
1383         after = PREV_INSN (tmp);
1384     }
1385   
1386   /* If the source has one successor and the edge is not abnormal,
1387      insert there.  Except for the entry block.  */
1388   else if ((e->flags & EDGE_ABNORMAL) == 0
1389            && e->src->succ->succ_next == NULL
1390            && e->src != ENTRY_BLOCK_PTR)
1391     {
1392       bb = e->src;
1393       if (GET_CODE (bb->end) == JUMP_INSN)
1394         {
1395           /* ??? Is it possible to wind up with non-simple jumps?  Perhaps
1396              a jump with delay slots already filled?  */
1397           if (! simplejump_p (bb->end))
1398             abort ();
1399
1400           before = bb->end;
1401         }
1402       else
1403         {
1404           /* We'd better be fallthru, or we've lost track of what's what.  */
1405           if ((e->flags & EDGE_FALLTHRU) == 0)
1406             abort ();
1407
1408           after = bb->end;
1409         }
1410     }
1411
1412   /* Otherwise we must split the edge.  */
1413   else
1414     {
1415       bb = split_edge (e);
1416       after = bb->end;
1417     }
1418
1419   /* Now that we've found the spot, do the insertion.  */
1420   tmp = e->insns;
1421   e->insns = NULL_RTX;
1422
1423   /* Set the new block number for these insns, if structure is allocated.  */
1424   if (basic_block_for_insn)
1425     {
1426       rtx i;
1427       for (i = tmp; i != NULL_RTX; i = NEXT_INSN (i))
1428         set_block_for_insn (i, bb);
1429     }
1430
1431   if (before)
1432     {
1433       emit_insns_before (tmp, before);
1434       if (before == bb->head)
1435         bb->head = tmp;
1436     }
1437   else
1438     {
1439       tmp = emit_insns_after (tmp, after);
1440       if (after == bb->end)
1441         bb->end = tmp;
1442     }
1443 }
1444
1445 /* Update the CFG for all queued instructions.  */
1446
1447 void
1448 commit_edge_insertions ()
1449 {
1450   int i;
1451   basic_block bb;
1452
1453   i = -1;
1454   bb = ENTRY_BLOCK_PTR;
1455   while (1)
1456     {
1457       edge e, next;
1458
1459       for (e = bb->succ; e ; e = next)
1460         {
1461           next = e->succ_next;
1462           if (e->insns)
1463             commit_one_edge_insertion (e);
1464         }
1465
1466       if (++i >= n_basic_blocks)
1467         break;
1468       bb = BASIC_BLOCK (i);
1469     }
1470 }
1471 \f
1472 /* Delete all unreachable basic blocks.   */
1473
1474 static void
1475 delete_unreachable_blocks ()
1476 {
1477   basic_block *worklist, *tos;
1478   int deleted_handler;
1479   edge e;
1480   int i, n;
1481
1482   n = n_basic_blocks;
1483   tos = worklist = (basic_block *) alloca (sizeof (basic_block) * n);
1484
1485   /* Use basic_block->aux as a marker.  Clear them all.  */
1486
1487   for (i = 0; i < n; ++i)
1488     BASIC_BLOCK (i)->aux = NULL;
1489
1490   /* Add our starting points to the worklist.  Almost always there will
1491      be only one.  It isn't inconcievable that we might one day directly
1492      support Fortran alternate entry points.  */
1493
1494   for (e = ENTRY_BLOCK_PTR->succ; e ; e = e->succ_next)
1495     {
1496       *tos++ = e->dest;
1497
1498       /* Mark the block with a handy non-null value.  */
1499       e->dest->aux = e;
1500     }
1501       
1502   /* Iterate: find everything reachable from what we've already seen.  */
1503
1504   while (tos != worklist)
1505     {
1506       basic_block b = *--tos;
1507
1508       for (e = b->succ; e ; e = e->succ_next)
1509         if (!e->dest->aux)
1510           {
1511             *tos++ = e->dest;
1512             e->dest->aux = e;
1513           }
1514     }
1515
1516   /* Delete all unreachable basic blocks.  Count down so that we don't
1517      interfere with the block renumbering that happens in delete_block.  */
1518
1519   deleted_handler = 0;
1520
1521   for (i = n - 1; i >= 0; --i)
1522     {
1523       basic_block b = BASIC_BLOCK (i);
1524
1525       if (b->aux != NULL)
1526         /* This block was found.  Tidy up the mark.  */
1527         b->aux = NULL;
1528       else
1529         deleted_handler |= delete_block (b);
1530     }
1531
1532   /* Fix up edges that now fall through, or rather should now fall through
1533      but previously required a jump around now deleted blocks.  Simplify
1534      the search by only examining blocks numerically adjacent, since this
1535      is how find_basic_blocks created them.  */
1536
1537   for (i = 1; i < n_basic_blocks; ++i)
1538     {
1539       basic_block b = BASIC_BLOCK (i - 1);
1540       basic_block c = BASIC_BLOCK (i);
1541       edge s;
1542
1543       /* We care about simple conditional or unconditional jumps with
1544          a single successor.
1545
1546          If we had a conditional branch to the next instruction when
1547          find_basic_blocks was called, then there will only be one
1548          out edge for the block which ended with the conditional
1549          branch (since we do not create duplicate edges).
1550
1551          Furthermore, the edge will be marked as a fallthru because we
1552          merge the flags for the duplicate edges.  So we do not want to
1553          check that the edge is not a FALLTHRU edge.  */
1554       if ((s = b->succ) != NULL
1555           && s->succ_next == NULL
1556           && s->dest == c
1557           /* If the jump insn has side effects, we can't tidy the edge.  */
1558           && (GET_CODE (b->end) != JUMP_INSN
1559               || onlyjump_p (b->end)))
1560         tidy_fallthru_edge (s, b, c);
1561     }
1562
1563   /* Attempt to merge blocks as made possible by edge removal.  If a block
1564      has only one successor, and the successor has only one predecessor, 
1565      they may be combined.  */
1566
1567   for (i = 0; i < n_basic_blocks; )
1568     {
1569       basic_block c, b = BASIC_BLOCK (i);
1570       edge s;
1571
1572       /* A loop because chains of blocks might be combineable.  */
1573       while ((s = b->succ) != NULL
1574              && s->succ_next == NULL
1575              && (s->flags & EDGE_EH) == 0
1576              && (c = s->dest) != EXIT_BLOCK_PTR
1577              && c->pred->pred_next == NULL
1578              /* If the jump insn has side effects, we can't kill the edge.  */
1579              && (GET_CODE (b->end) != JUMP_INSN
1580                  || onlyjump_p (b->end))
1581              && merge_blocks (s, b, c))
1582         continue;
1583
1584       /* Don't get confused by the index shift caused by deleting blocks.  */
1585       i = b->index + 1;
1586     }
1587
1588   /* If we deleted an exception handler, we may have EH region begin/end
1589      blocks to remove as well. */
1590   if (deleted_handler)
1591     delete_eh_regions ();
1592 }
1593
1594 /* Find EH regions for which there is no longer a handler, and delete them.  */
1595
1596 static void
1597 delete_eh_regions ()
1598 {
1599   rtx insn;
1600
1601   update_rethrow_references ();
1602
1603   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1604     if (GET_CODE (insn) == NOTE)
1605       {
1606         if ((NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG) ||
1607             (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END)) 
1608           {
1609             int num = CODE_LABEL_NUMBER (insn);
1610             /* A NULL handler indicates a region is no longer needed,
1611                as long as it isn't the target of a rethrow.  */
1612             if (get_first_handler (num) == NULL && ! rethrow_used (num))
1613               {
1614                 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1615                 NOTE_SOURCE_FILE (insn) = 0;
1616               }
1617           }
1618       }
1619 }
1620
1621 /* Return true if NOTE is not one of the ones that must be kept paired,
1622    so that we may simply delete them.  */
1623
1624 static int
1625 can_delete_note_p (note)
1626      rtx note;
1627 {
1628   return (NOTE_LINE_NUMBER (note) == NOTE_INSN_DELETED
1629           || NOTE_LINE_NUMBER (note) == NOTE_INSN_BASIC_BLOCK);
1630 }
1631
1632 /* Unlink a chain of insns between START and FINISH, leaving notes
1633    that must be paired.  */
1634
1635 static void
1636 delete_insn_chain (start, finish)
1637      rtx start, finish;
1638 {
1639   /* Unchain the insns one by one.  It would be quicker to delete all
1640      of these with a single unchaining, rather than one at a time, but
1641      we need to keep the NOTE's.  */
1642
1643   rtx next;
1644
1645   while (1)
1646     {
1647       next = NEXT_INSN (start);
1648       if (GET_CODE (start) == NOTE && !can_delete_note_p (start))
1649         ;
1650       else if (GET_CODE (start) == CODE_LABEL && !can_delete_label_p (start))
1651         ;
1652       else
1653         next = flow_delete_insn (start);
1654
1655       if (start == finish)
1656         break;
1657       start = next;
1658     }
1659 }
1660
1661 /* Delete the insns in a (non-live) block.  We physically delete every
1662    non-deleted-note insn, and update the flow graph appropriately.
1663
1664    Return nonzero if we deleted an exception handler.  */
1665
1666 /* ??? Preserving all such notes strikes me as wrong.  It would be nice
1667    to post-process the stream to remove empty blocks, loops, ranges, etc.  */
1668
1669 static int
1670 delete_block (b)
1671      basic_block b;
1672 {
1673   int deleted_handler = 0;
1674   rtx insn, end;
1675
1676   /* If the head of this block is a CODE_LABEL, then it might be the
1677      label for an exception handler which can't be reached.
1678
1679      We need to remove the label from the exception_handler_label list
1680      and remove the associated NOTE_EH_REGION_BEG and NOTE_EH_REGION_END
1681      notes.  */
1682
1683   insn = b->head;
1684   
1685   if (GET_CODE (insn) == CODE_LABEL)
1686     {
1687       rtx x, *prev = &exception_handler_labels;
1688
1689       for (x = exception_handler_labels; x; x = XEXP (x, 1))
1690         {
1691           if (XEXP (x, 0) == insn)
1692             {
1693               /* Found a match, splice this label out of the EH label list.  */
1694               *prev = XEXP (x, 1);
1695               XEXP (x, 1) = NULL_RTX;
1696               XEXP (x, 0) = NULL_RTX;
1697
1698               /* Remove the handler from all regions */
1699               remove_handler (insn);
1700               deleted_handler = 1;
1701               break;
1702             }
1703           prev = &XEXP (x, 1);
1704         }
1705
1706       /* This label may be referenced by code solely for its value, or
1707          referenced by static data, or something.  We have determined
1708          that it is not reachable, but cannot delete the label itself.
1709          Save code space and continue to delete the balance of the block,
1710          along with properly updating the cfg.  */
1711       if (!can_delete_label_p (insn))
1712         {
1713           /* If we've only got one of these, skip the whole deleting
1714              insns thing.  */
1715           if (insn == b->end)
1716             goto no_delete_insns;
1717           insn = NEXT_INSN (insn);
1718         }
1719     }
1720
1721   /* Selectively unlink the insn chain.  Include any BARRIER that may
1722      follow the basic block.  */
1723   end = next_nonnote_insn (b->end);
1724   if (!end || GET_CODE (end) != BARRIER)
1725     end = b->end;
1726   delete_insn_chain (insn, end);
1727
1728 no_delete_insns:
1729
1730   /* Remove the edges into and out of this block.  Note that there may 
1731      indeed be edges in, if we are removing an unreachable loop.  */
1732   {
1733     edge e, next, *q;
1734
1735     for (e = b->pred; e ; e = next)
1736       {
1737         for (q = &e->src->succ; *q != e; q = &(*q)->succ_next)
1738           continue;
1739         *q = e->succ_next;
1740         next = e->pred_next;
1741         free (e);
1742       }
1743     for (e = b->succ; e ; e = next)
1744       {
1745         for (q = &e->dest->pred; *q != e; q = &(*q)->pred_next)
1746           continue;
1747         *q = e->pred_next;
1748         next = e->succ_next;
1749         free (e);
1750       }
1751
1752     b->pred = NULL;
1753     b->succ = NULL;
1754   }
1755
1756   /* Remove the basic block from the array, and compact behind it.  */
1757   expunge_block (b);
1758
1759   return deleted_handler;
1760 }
1761
1762 /* Remove block B from the basic block array and compact behind it.  */
1763
1764 static void
1765 expunge_block (b)
1766      basic_block b;
1767 {
1768   int i, n = n_basic_blocks;
1769
1770   for (i = b->index; i + 1 < n; ++i)
1771     {
1772       basic_block x = BASIC_BLOCK (i + 1);
1773       BASIC_BLOCK (i) = x;
1774       x->index = i;
1775     }
1776
1777   basic_block_info->num_elements--;
1778   n_basic_blocks--;
1779 }
1780
1781 /* Delete INSN by patching it out.  Return the next insn.  */
1782
1783 static rtx
1784 flow_delete_insn (insn)
1785      rtx insn;
1786 {
1787   rtx prev = PREV_INSN (insn);
1788   rtx next = NEXT_INSN (insn);
1789
1790   PREV_INSN (insn) = NULL_RTX;
1791   NEXT_INSN (insn) = NULL_RTX;
1792
1793   if (prev)
1794     NEXT_INSN (prev) = next;
1795   if (next)
1796     PREV_INSN (next) = prev;
1797   else
1798     set_last_insn (prev);
1799
1800   if (GET_CODE (insn) == CODE_LABEL)
1801     remove_node_from_expr_list (insn, &nonlocal_goto_handler_labels);
1802
1803   /* If deleting a jump, decrement the use count of the label.  Deleting
1804      the label itself should happen in the normal course of block merging.  */
1805   if (GET_CODE (insn) == JUMP_INSN && JUMP_LABEL (insn))
1806     LABEL_NUSES (JUMP_LABEL (insn))--;
1807
1808   return next;
1809 }
1810
1811 /* True if a given label can be deleted.  */
1812
1813 static int 
1814 can_delete_label_p (label)
1815      rtx label;
1816 {
1817   rtx x;
1818
1819   if (LABEL_PRESERVE_P (label))
1820     return 0;
1821
1822   for (x = forced_labels; x ; x = XEXP (x, 1))
1823     if (label == XEXP (x, 0))
1824       return 0;
1825   for (x = label_value_list; x ; x = XEXP (x, 1))
1826     if (label == XEXP (x, 0))
1827       return 0;
1828   for (x = exception_handler_labels; x ; x = XEXP (x, 1))
1829     if (label == XEXP (x, 0))
1830       return 0;
1831
1832   /* User declared labels must be preserved.  */
1833   if (LABEL_NAME (label) != 0)
1834     return 0;
1835   
1836   return 1;
1837 }
1838
1839 /* Blocks A and B are to be merged into a single block.  The insns
1840    are already contiguous, hence `nomove'.  */
1841
1842 static void
1843 merge_blocks_nomove (a, b)
1844      basic_block a, b;
1845 {
1846   edge e;
1847   rtx b_head, b_end, a_end;
1848   int b_empty = 0;
1849
1850   /* If there was a CODE_LABEL beginning B, delete it.  */
1851   b_head = b->head;
1852   b_end = b->end;
1853   if (GET_CODE (b_head) == CODE_LABEL)
1854     {
1855       /* Detect basic blocks with nothing but a label.  This can happen
1856          in particular at the end of a function.  */
1857       if (b_head == b_end)
1858         b_empty = 1;
1859       b_head = flow_delete_insn (b_head);
1860     }
1861
1862   /* Delete the basic block note.  */
1863   if (GET_CODE (b_head) == NOTE 
1864       && NOTE_LINE_NUMBER (b_head) == NOTE_INSN_BASIC_BLOCK)
1865     {
1866       if (b_head == b_end)
1867         b_empty = 1;
1868       b_head = flow_delete_insn (b_head);
1869     }
1870
1871   /* If there was a jump out of A, delete it.  */
1872   a_end = a->end;
1873   if (GET_CODE (a_end) == JUMP_INSN)
1874     {
1875       rtx prev;
1876
1877       prev = prev_nonnote_insn (a_end);
1878       if (!prev) 
1879         prev = a->head;
1880
1881 #ifdef HAVE_cc0
1882       /* If this was a conditional jump, we need to also delete
1883          the insn that set cc0.  */
1884
1885       if (prev && sets_cc0_p (prev))
1886         {
1887           rtx tmp = prev;
1888           prev = prev_nonnote_insn (prev);
1889           if (!prev)
1890             prev = a->head;
1891           flow_delete_insn (tmp);
1892         }
1893 #endif
1894
1895       /* Note that a->head != a->end, since we should have at least a
1896          bb note plus the jump, so prev != insn.  */
1897       flow_delete_insn (a_end);
1898       a_end = prev;
1899     }
1900
1901   /* By definition, there should only be one successor of A, and that is
1902      B.  Free that edge struct.  */
1903   free (a->succ);
1904
1905   /* Adjust the edges out of B for the new owner.  */
1906   for (e = b->succ; e ; e = e->succ_next)
1907     e->src = a;
1908   a->succ = b->succ;
1909
1910   /* Reassociate the insns of B with A.  */
1911   if (!b_empty)
1912     {
1913       BLOCK_FOR_INSN (b_head) = a;
1914       while (b_head != b_end)
1915         {
1916           b_head = NEXT_INSN (b_head);
1917           BLOCK_FOR_INSN (b_head) = a;
1918         }
1919       a_end = b_head;
1920     }
1921   a->end = a_end;
1922   
1923   /* Compact the basic block array.  */
1924   expunge_block (b);
1925 }
1926
1927 /* Attempt to merge basic blocks that are potentially non-adjacent.  
1928    Return true iff the attempt succeeded.  */
1929
1930 static int
1931 merge_blocks (e, b, c)
1932      edge e;
1933      basic_block b, c;
1934 {
1935   /* If B has a fallthru edge to C, no need to move anything.  */
1936   if (!(e->flags & EDGE_FALLTHRU))
1937     {
1938       /* ??? From here on out we must make sure to not munge nesting
1939          of exception regions and lexical blocks.  Need to think about
1940          these cases before this gets implemented.  */
1941       return 0;
1942
1943       /* If C has an outgoing fallthru, and B does not have an incoming
1944          fallthru, move B before C.  The later clause is somewhat arbitrary,
1945          but avoids modifying blocks other than the two we've been given.  */
1946
1947       /* Otherwise, move C after B.  If C had a fallthru, which doesn't
1948          happen to be the physical successor to B, insert an unconditional
1949          branch.  If C already ended with a conditional branch, the new
1950          jump must go in a new basic block D.  */
1951     }
1952
1953   /* If a label still appears somewhere and we cannot delete the label,
1954      then we cannot merge the blocks.  The edge was tidied already.  */
1955   {
1956     rtx insn, stop = NEXT_INSN (c->head);
1957     for (insn = NEXT_INSN (b->end); insn != stop; insn = NEXT_INSN (insn))
1958       if (GET_CODE (insn) == CODE_LABEL && !can_delete_label_p (insn))
1959         return 0;
1960   }
1961
1962   merge_blocks_nomove (b, c);
1963   return 1;
1964 }
1965
1966 /* The given edge should potentially a fallthru edge.  If that is in
1967    fact true, delete the unconditional jump and barriers that are in
1968    the way.  */
1969
1970 static void
1971 tidy_fallthru_edge (e, b, c)
1972      edge e;
1973      basic_block b, c;
1974 {
1975   rtx q;
1976
1977   /* ??? In a late-running flow pass, other folks may have deleted basic
1978      blocks by nopping out blocks, leaving multiple BARRIERs between here
1979      and the target label. They ought to be chastized and fixed.
1980
1981      We can also wind up with a sequence of undeletable labels between
1982      one block and the next.
1983
1984      So search through a sequence of barriers, labels, and notes for
1985      the head of block C and assert that we really do fall through.  */
1986
1987   if (next_real_insn (b->end) != next_real_insn (PREV_INSN (c->head)))
1988     return;
1989
1990   /* Remove what will soon cease being the jump insn from the source block.
1991      If block B consisted only of this single jump, turn it into a deleted
1992      note.  */
1993   q = b->end;
1994   if (GET_CODE (q) == JUMP_INSN)
1995     {
1996 #ifdef HAVE_cc0
1997       /* If this was a conditional jump, we need to also delete
1998          the insn that set cc0.  */
1999       if (! simplejump_p (q) && condjump_p (q))
2000         q = PREV_INSN (q);
2001 #endif
2002
2003       if (b->head == q)
2004         {
2005           PUT_CODE (q, NOTE);
2006           NOTE_LINE_NUMBER (q) = NOTE_INSN_DELETED;
2007           NOTE_SOURCE_FILE (q) = 0;
2008         }
2009       else
2010         b->end = q = PREV_INSN (q);
2011     }
2012
2013   /* Selectively unlink the sequence.  */
2014   if (q != PREV_INSN (c->head))
2015     delete_insn_chain (NEXT_INSN (q), PREV_INSN (c->head));
2016
2017   e->flags |= EDGE_FALLTHRU;
2018 }
2019
2020 /* Discover and record the loop depth at the head of each basic block.  */
2021
2022 static void
2023 calculate_loop_depth (insns)
2024      rtx insns;
2025 {
2026   basic_block bb;
2027   rtx insn;
2028   int i = 0, depth = 1;
2029
2030   bb = BASIC_BLOCK (i);
2031   for (insn = insns; insn ; insn = NEXT_INSN (insn))
2032     {
2033       if (insn == bb->head)
2034         {
2035           bb->loop_depth = depth;
2036           if (++i >= n_basic_blocks)
2037             break;
2038           bb = BASIC_BLOCK (i);
2039         }
2040
2041       if (GET_CODE (insn) == NOTE)
2042         {
2043           if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
2044             depth++;
2045           else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
2046             depth--;
2047
2048           /* If we have LOOP_DEPTH == 0, there has been a bookkeeping error. */
2049           if (depth == 0)
2050             abort ();
2051         }
2052     }
2053 }
2054 \f
2055 /* Perform data flow analysis.
2056    F is the first insn of the function and NREGS the number of register numbers
2057    in use.  */
2058
2059 void
2060 life_analysis (f, nregs, file, remove_dead_code)
2061      rtx f;
2062      int nregs;
2063      FILE *file;
2064      int remove_dead_code;
2065 {
2066 #ifdef ELIMINABLE_REGS
2067   register size_t i;
2068   static struct {int from, to; } eliminables[] = ELIMINABLE_REGS;
2069 #endif
2070
2071   /* Record which registers will be eliminated.  We use this in
2072      mark_used_regs.  */
2073
2074   CLEAR_HARD_REG_SET (elim_reg_set);
2075
2076 #ifdef ELIMINABLE_REGS
2077   for (i = 0; i < sizeof eliminables / sizeof eliminables[0]; i++)
2078     SET_HARD_REG_BIT (elim_reg_set, eliminables[i].from);
2079 #else
2080   SET_HARD_REG_BIT (elim_reg_set, FRAME_POINTER_REGNUM);
2081 #endif
2082
2083   /* Allocate a bitmap to be filled in by record_volatile_insns.  */
2084   uid_volatile = BITMAP_ALLOCA ();
2085
2086   /* We want alias analysis information for local dead store elimination.  */
2087   init_alias_analysis ();
2088   life_analysis_1 (f, nregs, remove_dead_code);
2089   end_alias_analysis ();
2090
2091   if (file)
2092     dump_flow_info (file);
2093
2094   BITMAP_FREE (uid_volatile);
2095   free_basic_block_vars (1);
2096 }
2097
2098 /* Free the variables allocated by find_basic_blocks.
2099
2100    KEEP_HEAD_END_P is non-zero if basic_block_info is not to be freed.  */
2101
2102 void
2103 free_basic_block_vars (keep_head_end_p)
2104      int keep_head_end_p;
2105 {
2106   if (basic_block_for_insn)
2107     {
2108       VARRAY_FREE (basic_block_for_insn);
2109       basic_block_for_insn = NULL;
2110     }
2111
2112   if (! keep_head_end_p)
2113     {
2114       clear_edges ();
2115       VARRAY_FREE (basic_block_info);
2116       n_basic_blocks = 0;
2117
2118       ENTRY_BLOCK_PTR->aux = NULL;
2119       ENTRY_BLOCK_PTR->global_live_at_end = NULL;
2120       EXIT_BLOCK_PTR->aux = NULL;
2121       EXIT_BLOCK_PTR->global_live_at_start = NULL;
2122     }
2123 }
2124
2125 /* Return nonzero if the destination of SET equals the source.  */
2126 static int
2127 set_noop_p (set)
2128      rtx set;
2129 {
2130   rtx src = SET_SRC (set);
2131   rtx dst = SET_DEST (set);
2132   if (GET_CODE (src) == REG && GET_CODE (dst) == REG
2133       && REGNO (src) == REGNO (dst))
2134     return 1;
2135   if (GET_CODE (src) != SUBREG || GET_CODE (dst) != SUBREG
2136       || SUBREG_WORD (src) != SUBREG_WORD (dst))
2137     return 0;
2138   src = SUBREG_REG (src);
2139   dst = SUBREG_REG (dst);
2140   if (GET_CODE (src) == REG && GET_CODE (dst) == REG
2141       && REGNO (src) == REGNO (dst))
2142     return 1;
2143   return 0;
2144 }
2145
2146 /* Return nonzero if an insn consists only of SETs, each of which only sets a
2147    value to itself.  */
2148 static int
2149 noop_move_p (insn)
2150      rtx insn;
2151 {
2152   rtx pat = PATTERN (insn);
2153
2154   /* Insns carrying these notes are useful later on.  */
2155   if (find_reg_note (insn, REG_EQUAL, NULL_RTX))
2156     return 0;
2157
2158   if (GET_CODE (pat) == SET && set_noop_p (pat))
2159     return 1;
2160
2161   if (GET_CODE (pat) == PARALLEL)
2162     {
2163       int i;
2164       /* If nothing but SETs of registers to themselves,
2165          this insn can also be deleted.  */
2166       for (i = 0; i < XVECLEN (pat, 0); i++)
2167         {
2168           rtx tem = XVECEXP (pat, 0, i);
2169
2170           if (GET_CODE (tem) == USE
2171               || GET_CODE (tem) == CLOBBER)
2172             continue;
2173
2174           if (GET_CODE (tem) != SET || ! set_noop_p (tem))
2175             return 0;
2176         }
2177
2178       return 1;
2179     }
2180   return 0;
2181 }
2182
2183 static void
2184 notice_stack_pointer_modification (x, pat)
2185      rtx x;
2186      rtx pat ATTRIBUTE_UNUSED;
2187 {
2188   if (x == stack_pointer_rtx
2189       /* The stack pointer is only modified indirectly as the result
2190          of a push until later in flow.  See the comments in rtl.texi
2191          regarding Embedded Side-Effects on Addresses.  */
2192       || (GET_CODE (x) == MEM
2193           && (GET_CODE (XEXP (x, 0)) == PRE_DEC
2194               || GET_CODE (XEXP (x, 0)) == PRE_INC
2195               || GET_CODE (XEXP (x, 0)) == POST_DEC
2196               || GET_CODE (XEXP (x, 0)) == POST_INC)
2197           && XEXP (XEXP (x, 0), 0) == stack_pointer_rtx))
2198     current_function_sp_is_unchanging = 0;
2199 }
2200
2201 /* Record which insns refer to any volatile memory
2202    or for any reason can't be deleted just because they are dead stores.
2203    Also, delete any insns that copy a register to itself.
2204    And see if the stack pointer is modified.  */
2205 static void
2206 record_volatile_insns (f)
2207      rtx f;
2208 {
2209   rtx insn;
2210   for (insn = f; insn; insn = NEXT_INSN (insn))
2211     {
2212       enum rtx_code code1 = GET_CODE (insn);
2213       if (code1 == CALL_INSN)
2214         SET_INSN_VOLATILE (insn);
2215       else if (code1 == INSN || code1 == JUMP_INSN)
2216         {
2217           if (GET_CODE (PATTERN (insn)) != USE
2218               && volatile_refs_p (PATTERN (insn)))
2219             SET_INSN_VOLATILE (insn);
2220
2221           /* A SET that makes space on the stack cannot be dead.
2222              (Such SETs occur only for allocating variable-size data,
2223              so they will always have a PLUS or MINUS according to the
2224              direction of stack growth.)
2225              Even if this function never uses this stack pointer value,
2226              signal handlers do!  */
2227           else if (code1 == INSN && GET_CODE (PATTERN (insn)) == SET
2228                    && SET_DEST (PATTERN (insn)) == stack_pointer_rtx
2229 #ifdef STACK_GROWS_DOWNWARD
2230                    && GET_CODE (SET_SRC (PATTERN (insn))) == MINUS
2231 #else
2232                    && GET_CODE (SET_SRC (PATTERN (insn))) == PLUS
2233 #endif
2234                    && XEXP (SET_SRC (PATTERN (insn)), 0) == stack_pointer_rtx)
2235             SET_INSN_VOLATILE (insn);
2236
2237           /* Delete (in effect) any obvious no-op moves.  */
2238           else if (noop_move_p (insn))
2239             {
2240               PUT_CODE (insn, NOTE);
2241               NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2242               NOTE_SOURCE_FILE (insn) = 0;
2243             }
2244         }
2245
2246       /* Check if insn modifies the stack pointer.  */
2247       if ( current_function_sp_is_unchanging
2248            && GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2249         note_stores (PATTERN (insn), notice_stack_pointer_modification);
2250     }
2251 }
2252
2253 /* Mark those regs which are needed at the end of the function as live
2254    at the end of the last basic block.  */
2255 static void
2256 mark_regs_live_at_end (set)
2257      regset set;
2258 {
2259   int i;
2260   
2261   /* If exiting needs the right stack value, consider the stack pointer
2262      live at the end of the function.  */
2263   if (! EXIT_IGNORE_STACK
2264       || (! FRAME_POINTER_REQUIRED
2265           && ! current_function_calls_alloca
2266           && flag_omit_frame_pointer)
2267       || current_function_sp_is_unchanging)
2268     {
2269       SET_REGNO_REG_SET (set, STACK_POINTER_REGNUM);
2270     }
2271
2272   /* Mark the frame pointer if needed at the end of the function.  If
2273      we end up eliminating it, it will be removed from the live list
2274      of each basic block by reload.  */
2275
2276   if (! reload_completed || frame_pointer_needed)
2277     {
2278       SET_REGNO_REG_SET (set, FRAME_POINTER_REGNUM);
2279 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2280       /* If they are different, also mark the hard frame pointer as live */
2281       SET_REGNO_REG_SET (set, HARD_FRAME_POINTER_REGNUM);
2282 #endif      
2283     }
2284
2285   /* Mark all global registers, and all registers used by the epilogue
2286      as being live at the end of the function since they may be
2287      referenced by our caller.  */
2288   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2289     if (global_regs[i]
2290 #ifdef EPILOGUE_USES
2291         || EPILOGUE_USES (i)
2292 #endif
2293         )
2294       SET_REGNO_REG_SET (set, i);
2295
2296   /* ??? Mark function return value here rather than as uses.  */
2297 }
2298
2299 /* Determine which registers are live at the start of each
2300    basic block of the function whose first insn is F.
2301    NREGS is the number of registers used in F.
2302    We allocate the vector basic_block_live_at_start
2303    and the regsets that it points to, and fill them with the data.
2304    regset_size and regset_bytes are also set here.  */
2305
2306 static void
2307 life_analysis_1 (f, nregs, remove_dead_code)
2308      rtx f;
2309      int nregs;
2310      int remove_dead_code;
2311 {
2312   int first_pass;
2313   int changed;
2314   register int i;
2315   char save_regs_ever_live[FIRST_PSEUDO_REGISTER];
2316   regset *new_live_at_end;
2317
2318   struct obstack flow_obstack;
2319
2320   gcc_obstack_init (&flow_obstack);
2321
2322   max_regno = nregs;
2323
2324   /* Allocate and zero out many data structures
2325      that will record the data from lifetime analysis.  */
2326
2327   allocate_reg_life_data ();
2328   allocate_bb_life_data ();
2329
2330   reg_next_use = (rtx *) alloca (nregs * sizeof (rtx));
2331   memset (reg_next_use, 0, nregs * sizeof (rtx));
2332
2333   /* Set up regset-vectors used internally within this function.
2334      Their meanings are documented above, with their declarations.  */
2335
2336   new_live_at_end = (regset *) alloca ((n_basic_blocks + 1) * sizeof (regset));
2337   init_regset_vector (new_live_at_end, n_basic_blocks + 1, &flow_obstack);
2338
2339   /* Stick these vectors into the AUX field of the basic block, so that
2340      we don't have to keep going through the index.  */
2341
2342   for (i = 0; i < n_basic_blocks; ++i)
2343     BASIC_BLOCK (i)->aux = new_live_at_end[i];
2344   ENTRY_BLOCK_PTR->aux = new_live_at_end[i];
2345
2346   /* Assume that the stack pointer is unchanging if alloca hasn't been used.
2347      This will be cleared by record_volatile_insns if it encounters an insn
2348      which modifies the stack pointer.  */
2349   current_function_sp_is_unchanging = !current_function_calls_alloca;
2350
2351   record_volatile_insns (f);
2352
2353   if (n_basic_blocks > 0)
2354     {
2355       regset theend;
2356       register edge e;
2357
2358       theend = EXIT_BLOCK_PTR->global_live_at_start;
2359       mark_regs_live_at_end (theend);
2360
2361       /* Propogate this exit data to each of EXIT's predecessors.  */
2362       for (e = EXIT_BLOCK_PTR->pred; e ; e = e->pred_next)
2363         {
2364           COPY_REG_SET (e->src->global_live_at_end, theend);
2365           COPY_REG_SET ((regset) e->src->aux, theend);
2366         }
2367     }
2368
2369   /* The post-reload life analysis have (on a global basis) the same registers
2370      live as was computed by reload itself.
2371
2372      Otherwise elimination offsets and such may be incorrect.
2373
2374      Reload will make some registers as live even though they do not appear
2375      in the rtl.  */
2376   if (reload_completed)
2377     memcpy (save_regs_ever_live, regs_ever_live, sizeof (regs_ever_live));
2378   memset (regs_ever_live, 0, sizeof regs_ever_live);
2379
2380   /* Propagate life info through the basic blocks
2381      around the graph of basic blocks.
2382
2383      This is a relaxation process: each time a new register
2384      is live at the end of the basic block, we must scan the block
2385      to determine which registers are, as a consequence, live at the beginning
2386      of that block.  These registers must then be marked live at the ends
2387      of all the blocks that can transfer control to that block.
2388      The process continues until it reaches a fixed point.  */
2389
2390   first_pass = 1;
2391   changed = 1;
2392   while (changed)
2393     {
2394       changed = 0;
2395       for (i = n_basic_blocks - 1; i >= 0; i--)
2396         {
2397           basic_block bb = BASIC_BLOCK (i);
2398           int consider = first_pass;
2399           int must_rescan = first_pass;
2400           register int j;
2401
2402           if (!first_pass)
2403             {
2404               /* Set CONSIDER if this block needs thinking about at all
2405                  (that is, if the regs live now at the end of it
2406                  are not the same as were live at the end of it when
2407                  we last thought about it).
2408                  Set must_rescan if it needs to be thought about
2409                  instruction by instruction (that is, if any additional
2410                  reg that is live at the end now but was not live there before
2411                  is one of the significant regs of this basic block).  */
2412
2413               EXECUTE_IF_AND_COMPL_IN_REG_SET
2414                 ((regset) bb->aux, bb->global_live_at_end, 0, j,
2415                  {
2416                    consider = 1;
2417                    if (REGNO_REG_SET_P (bb->local_set, j))
2418                      {
2419                        must_rescan = 1;
2420                        goto done;
2421                      }
2422                  });
2423             done:
2424               if (! consider)
2425                 continue;
2426             }
2427
2428           /* The live_at_start of this block may be changing,
2429              so another pass will be required after this one.  */
2430           changed = 1;
2431
2432           if (! must_rescan)
2433             {
2434               /* No complete rescan needed;
2435                  just record those variables newly known live at end
2436                  as live at start as well.  */
2437               IOR_AND_COMPL_REG_SET (bb->global_live_at_start,
2438                                      (regset) bb->aux,
2439                                      bb->global_live_at_end);
2440
2441               IOR_AND_COMPL_REG_SET (bb->global_live_at_end,
2442                                      (regset) bb->aux,
2443                                      bb->global_live_at_end);
2444             }
2445           else
2446             {
2447               /* Update the basic_block_live_at_start
2448                  by propagation backwards through the block.  */
2449               COPY_REG_SET (bb->global_live_at_end, (regset) bb->aux);
2450               COPY_REG_SET (bb->global_live_at_start,
2451                             bb->global_live_at_end);
2452               propagate_block (bb->global_live_at_start,
2453                                bb->head, bb->end, 0,
2454                                first_pass ? bb->local_set : (regset) 0,
2455                                i, remove_dead_code);
2456             }
2457
2458           /* Update the new_live_at_end's of the block's predecessors.  */
2459           {
2460             register edge e;
2461
2462             for (e = bb->pred; e ; e = e->pred_next)
2463               IOR_REG_SET ((regset) e->src->aux, bb->global_live_at_start);
2464           }
2465
2466 #ifdef USE_C_ALLOCA
2467           alloca (0);
2468 #endif
2469         }
2470       first_pass = 0;
2471     }
2472
2473   /* The only pseudos that are live at the beginning of the function are
2474      those that were not set anywhere in the function.  local-alloc doesn't
2475      know how to handle these correctly, so mark them as not local to any
2476      one basic block.  */
2477
2478   if (n_basic_blocks > 0)
2479     EXECUTE_IF_SET_IN_REG_SET (BASIC_BLOCK (0)->global_live_at_start,
2480                                FIRST_PSEUDO_REGISTER, i,
2481                                {
2482                                  REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL;
2483                                });
2484
2485   /* Now the life information is accurate.  Make one more pass over each
2486      basic block to delete dead stores, create autoincrement addressing
2487      and record how many times each register is used, is set, or dies.  */
2488
2489   for (i = 0; i < n_basic_blocks; i++)
2490     {
2491       basic_block bb = BASIC_BLOCK (i);
2492
2493       /* We start with global_live_at_end to determine which stores are
2494          dead.  This process is destructive, and we wish to preserve the
2495          contents of global_live_at_end for posterity.  Fortunately,
2496          new_live_at_end, due to the way we converged on a solution,
2497          contains a duplicate of global_live_at_end that we can kill.  */
2498       propagate_block ((regset) bb->aux, bb->head, bb->end, 1, (regset) 0, i, remove_dead_code);
2499
2500 #ifdef USE_C_ALLOCA
2501       alloca (0);
2502 #endif
2503     }
2504
2505   /* We have a problem with any pseudoreg that lives across the setjmp. 
2506      ANSI says that if a user variable does not change in value between
2507      the setjmp and the longjmp, then the longjmp preserves it.  This
2508      includes longjmp from a place where the pseudo appears dead.
2509      (In principle, the value still exists if it is in scope.)
2510      If the pseudo goes in a hard reg, some other value may occupy
2511      that hard reg where this pseudo is dead, thus clobbering the pseudo.
2512      Conclusion: such a pseudo must not go in a hard reg.  */
2513   EXECUTE_IF_SET_IN_REG_SET (regs_live_at_setjmp,
2514                              FIRST_PSEUDO_REGISTER, i,
2515                              {
2516                                if (regno_reg_rtx[i] != 0)
2517                                  {
2518                                    REG_LIVE_LENGTH (i) = -1;
2519                                    REG_BASIC_BLOCK (i) = -1;
2520                                  }
2521                              });
2522
2523   /* Restore regs_ever_live that was provided by reload.  */
2524   if (reload_completed)
2525     memcpy (regs_ever_live, save_regs_ever_live, sizeof (regs_ever_live));
2526
2527   free_regset_vector (new_live_at_end, n_basic_blocks);
2528   obstack_free (&flow_obstack, NULL_PTR);
2529
2530   for (i = 0; i < n_basic_blocks; ++i)
2531     BASIC_BLOCK (i)->aux = NULL;
2532   ENTRY_BLOCK_PTR->aux = NULL;
2533 }
2534 \f
2535 /* Subroutines of life analysis.  */
2536
2537 /* Allocate the permanent data structures that represent the results
2538    of life analysis.  Not static since used also for stupid life analysis.  */
2539
2540 void
2541 allocate_bb_life_data ()
2542 {
2543   register int i;
2544
2545   for (i = 0; i < n_basic_blocks; i++)
2546     {
2547       basic_block bb = BASIC_BLOCK (i);
2548
2549       bb->local_set = OBSTACK_ALLOC_REG_SET (function_obstack);
2550       bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (function_obstack);
2551       bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (function_obstack);
2552     }
2553
2554   ENTRY_BLOCK_PTR->global_live_at_end
2555     = OBSTACK_ALLOC_REG_SET (function_obstack);
2556   EXIT_BLOCK_PTR->global_live_at_start
2557     = OBSTACK_ALLOC_REG_SET (function_obstack);
2558
2559   regs_live_at_setjmp = OBSTACK_ALLOC_REG_SET (function_obstack);
2560 }
2561
2562 void
2563 allocate_reg_life_data ()
2564 {
2565   int i;
2566
2567   /* Recalculate the register space, in case it has grown.  Old style
2568      vector oriented regsets would set regset_{size,bytes} here also.  */
2569   allocate_reg_info (max_regno, FALSE, FALSE);
2570
2571   /* Because both reg_scan and flow_analysis want to set up the REG_N_SETS
2572      information, explicitly reset it here.  The allocation should have
2573      already happened on the previous reg_scan pass.  Make sure in case
2574      some more registers were allocated.  */
2575   for (i = 0; i < max_regno; i++)
2576     REG_N_SETS (i) = 0;
2577 }
2578
2579 /* Make each element of VECTOR point at a regset.  The vector has
2580    NELTS elements, and space is allocated from the ALLOC_OBSTACK
2581    obstack.  */
2582
2583 static void
2584 init_regset_vector (vector, nelts, alloc_obstack)
2585      regset *vector;
2586      int nelts;
2587      struct obstack *alloc_obstack;
2588 {
2589   register int i;
2590
2591   for (i = 0; i < nelts; i++)
2592     {
2593       vector[i] = OBSTACK_ALLOC_REG_SET (alloc_obstack);
2594       CLEAR_REG_SET (vector[i]);
2595     }
2596 }
2597
2598 /* Release any additional space allocated for each element of VECTOR point
2599    other than the regset header itself.  The vector has NELTS elements.  */
2600
2601 void
2602 free_regset_vector (vector, nelts)
2603      regset *vector;
2604      int nelts;
2605 {
2606   register int i;
2607
2608   for (i = 0; i < nelts; i++)
2609     FREE_REG_SET (vector[i]);
2610 }
2611
2612 /* Compute the registers live at the beginning of a basic block
2613    from those live at the end.
2614
2615    When called, OLD contains those live at the end.
2616    On return, it contains those live at the beginning.
2617    FIRST and LAST are the first and last insns of the basic block.
2618
2619    FINAL is nonzero if we are doing the final pass which is not
2620    for computing the life info (since that has already been done)
2621    but for acting on it.  On this pass, we delete dead stores,
2622    set up the logical links and dead-variables lists of instructions,
2623    and merge instructions for autoincrement and autodecrement addresses.
2624
2625    SIGNIFICANT is nonzero only the first time for each basic block.
2626    If it is nonzero, it points to a regset in which we store
2627    a 1 for each register that is set within the block.
2628
2629    BNUM is the number of the basic block.  */
2630
2631 static void
2632 propagate_block (old, first, last, final, significant, bnum, remove_dead_code)
2633      register regset old;
2634      rtx first;
2635      rtx last;
2636      int final;
2637      regset significant;
2638      int bnum;
2639      int remove_dead_code;
2640 {
2641   register rtx insn;
2642   rtx prev;
2643   regset live;
2644   regset dead;
2645
2646   /* Find the loop depth for this block.  Ignore loop level changes in the
2647      middle of the basic block -- for register allocation purposes, the 
2648      important uses will be in the blocks wholely contained within the loop
2649      not in the loop pre-header or post-trailer.  */
2650   loop_depth = BASIC_BLOCK (bnum)->loop_depth;
2651
2652   dead = ALLOCA_REG_SET ();
2653   live = ALLOCA_REG_SET ();
2654
2655   cc0_live = 0;
2656   mem_set_list = NULL_RTX;
2657
2658   if (final)
2659     {
2660       register int i;
2661
2662       /* Process the regs live at the end of the block.
2663          Mark them as not local to any one basic block. */
2664       EXECUTE_IF_SET_IN_REG_SET (old, 0, i,
2665                                  {
2666                                    REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL;
2667                                  });
2668     }
2669
2670   /* Scan the block an insn at a time from end to beginning.  */
2671
2672   for (insn = last; ; insn = prev)
2673     {
2674       prev = PREV_INSN (insn);
2675
2676       if (GET_CODE (insn) == NOTE)
2677         {
2678           /* If this is a call to `setjmp' et al,
2679              warn if any non-volatile datum is live.  */
2680
2681           if (final && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
2682             IOR_REG_SET (regs_live_at_setjmp, old);
2683         }
2684
2685       /* Update the life-status of regs for this insn.
2686          First DEAD gets which regs are set in this insn
2687          then LIVE gets which regs are used in this insn.
2688          Then the regs live before the insn
2689          are those live after, with DEAD regs turned off,
2690          and then LIVE regs turned on.  */
2691
2692       else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2693         {
2694           register int i;
2695           rtx note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
2696           int insn_is_dead = 0;
2697           int libcall_is_dead = 0;
2698
2699           if (remove_dead_code)
2700             {
2701               insn_is_dead = (insn_dead_p (PATTERN (insn), old, 0, REG_NOTES (insn))
2702                               /* Don't delete something that refers to volatile storage!  */
2703                               && ! INSN_VOLATILE (insn));
2704               libcall_is_dead = (insn_is_dead && note != 0
2705                                  && libcall_dead_p (PATTERN (insn), old, note, insn));
2706             }
2707
2708           /* If an instruction consists of just dead store(s) on final pass,
2709              "delete" it by turning it into a NOTE of type NOTE_INSN_DELETED.
2710              We could really delete it with delete_insn, but that
2711              can cause trouble for first or last insn in a basic block.  */
2712           if (final && insn_is_dead)
2713             {
2714               PUT_CODE (insn, NOTE);
2715               NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2716               NOTE_SOURCE_FILE (insn) = 0;
2717
2718               /* CC0 is now known to be dead.  Either this insn used it,
2719                  in which case it doesn't anymore, or clobbered it,
2720                  so the next insn can't use it.  */
2721               cc0_live = 0;
2722
2723               /* If this insn is copying the return value from a library call,
2724                  delete the entire library call.  */
2725               if (libcall_is_dead)
2726                 {
2727                   rtx first = XEXP (note, 0);
2728                   rtx p = insn;
2729                   while (INSN_DELETED_P (first))
2730                     first = NEXT_INSN (first);
2731                   while (p != first)
2732                     {
2733                       p = PREV_INSN (p);
2734                       PUT_CODE (p, NOTE);
2735                       NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
2736                       NOTE_SOURCE_FILE (p) = 0;
2737                     }
2738                 }
2739               goto flushed;
2740             }
2741
2742           CLEAR_REG_SET (dead);
2743           CLEAR_REG_SET (live);
2744
2745           /* See if this is an increment or decrement that can be
2746              merged into a following memory address.  */
2747 #ifdef AUTO_INC_DEC
2748           {
2749             register rtx x = single_set (insn);
2750
2751             /* Does this instruction increment or decrement a register?  */
2752             if (!reload_completed
2753                 && final && x != 0
2754                 && GET_CODE (SET_DEST (x)) == REG
2755                 && (GET_CODE (SET_SRC (x)) == PLUS
2756                     || GET_CODE (SET_SRC (x)) == MINUS)
2757                 && XEXP (SET_SRC (x), 0) == SET_DEST (x)
2758                 && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2759                 /* Ok, look for a following memory ref we can combine with.
2760                    If one is found, change the memory ref to a PRE_INC
2761                    or PRE_DEC, cancel this insn, and return 1.
2762                    Return 0 if nothing has been done.  */
2763                 && try_pre_increment_1 (insn))
2764               goto flushed;
2765           }
2766 #endif /* AUTO_INC_DEC */
2767
2768           /* If this is not the final pass, and this insn is copying the
2769              value of a library call and it's dead, don't scan the
2770              insns that perform the library call, so that the call's
2771              arguments are not marked live.  */
2772           if (libcall_is_dead)
2773             {
2774               /* Mark the dest reg as `significant'.  */
2775               mark_set_regs (old, dead, PATTERN (insn), NULL_RTX, significant);
2776
2777               insn = XEXP (note, 0);
2778               prev = PREV_INSN (insn);
2779             }
2780           else if (GET_CODE (PATTERN (insn)) == SET
2781                    && SET_DEST (PATTERN (insn)) == stack_pointer_rtx
2782                    && GET_CODE (SET_SRC (PATTERN (insn))) == PLUS
2783                    && XEXP (SET_SRC (PATTERN (insn)), 0) == stack_pointer_rtx
2784                    && GET_CODE (XEXP (SET_SRC (PATTERN (insn)), 1)) == CONST_INT)
2785             /* We have an insn to pop a constant amount off the stack.
2786                (Such insns use PLUS regardless of the direction of the stack,
2787                and any insn to adjust the stack by a constant is always a pop.)
2788                These insns, if not dead stores, have no effect on life.  */
2789             ;
2790           else
2791             {
2792               /* Any regs live at the time of a call instruction
2793                  must not go in a register clobbered by calls.
2794                  Find all regs now live and record this for them.  */
2795
2796               if (GET_CODE (insn) == CALL_INSN && final)
2797                 EXECUTE_IF_SET_IN_REG_SET (old, 0, i,
2798                                            {
2799                                              REG_N_CALLS_CROSSED (i)++;
2800                                            });
2801
2802               /* LIVE gets the regs used in INSN;
2803                  DEAD gets those set by it.  Dead insns don't make anything
2804                  live.  */
2805
2806               mark_set_regs (old, dead, PATTERN (insn),
2807                              final ? insn : NULL_RTX, significant);
2808
2809               /* If an insn doesn't use CC0, it becomes dead since we 
2810                  assume that every insn clobbers it.  So show it dead here;
2811                  mark_used_regs will set it live if it is referenced.  */
2812               cc0_live = 0;
2813
2814               if (! insn_is_dead)
2815                 mark_used_regs (old, live, PATTERN (insn), final, insn);
2816
2817               /* Sometimes we may have inserted something before INSN (such as
2818                  a move) when we make an auto-inc.  So ensure we will scan
2819                  those insns.  */
2820 #ifdef AUTO_INC_DEC
2821               prev = PREV_INSN (insn);
2822 #endif
2823
2824               if (! insn_is_dead && GET_CODE (insn) == CALL_INSN)
2825                 {
2826                   register int i;
2827
2828                   rtx note;
2829
2830                   for (note = CALL_INSN_FUNCTION_USAGE (insn);
2831                        note;
2832                        note = XEXP (note, 1))
2833                     if (GET_CODE (XEXP (note, 0)) == USE)
2834                       mark_used_regs (old, live, SET_DEST (XEXP (note, 0)),
2835                                       final, insn);
2836
2837                   /* Each call clobbers all call-clobbered regs that are not
2838                      global or fixed.  Note that the function-value reg is a
2839                      call-clobbered reg, and mark_set_regs has already had
2840                      a chance to handle it.  */
2841
2842                   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2843                     if (call_used_regs[i] && ! global_regs[i]
2844                         && ! fixed_regs[i])
2845                       SET_REGNO_REG_SET (dead, i);
2846
2847                   /* The stack ptr is used (honorarily) by a CALL insn.  */
2848                   SET_REGNO_REG_SET (live, STACK_POINTER_REGNUM);
2849
2850                   /* Calls may also reference any of the global registers,
2851                      so they are made live.  */
2852                   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2853                     if (global_regs[i])
2854                       mark_used_regs (old, live,
2855                                       gen_rtx_REG (reg_raw_mode[i], i),
2856                                       final, insn);
2857
2858                   /* Calls also clobber memory.  */
2859                   mem_set_list = NULL_RTX;
2860                 }
2861
2862               /* Update OLD for the registers used or set.  */
2863               AND_COMPL_REG_SET (old, dead);
2864               IOR_REG_SET (old, live);
2865
2866             }
2867
2868           /* On final pass, update counts of how many insns each reg is live
2869              at.  */
2870           if (final)
2871             EXECUTE_IF_SET_IN_REG_SET (old, 0, i,
2872                                        { REG_LIVE_LENGTH (i)++; });
2873         }
2874     flushed: ;
2875       if (insn == first)
2876         break;
2877     }
2878
2879   FREE_REG_SET (dead);
2880   FREE_REG_SET (live);
2881 }
2882 \f
2883 /* Return 1 if X (the body of an insn, or part of it) is just dead stores
2884    (SET expressions whose destinations are registers dead after the insn).
2885    NEEDED is the regset that says which regs are alive after the insn.
2886
2887    Unless CALL_OK is non-zero, an insn is needed if it contains a CALL.
2888
2889    If X is the entire body of an insn, NOTES contains the reg notes
2890    pertaining to the insn.  */
2891
2892 static int
2893 insn_dead_p (x, needed, call_ok, notes)
2894      rtx x;
2895      regset needed;
2896      int call_ok;
2897      rtx notes ATTRIBUTE_UNUSED;
2898 {
2899   enum rtx_code code = GET_CODE (x);
2900
2901 #ifdef AUTO_INC_DEC
2902   /* If flow is invoked after reload, we must take existing AUTO_INC
2903      expresions into account.  */
2904   if (reload_completed)
2905     {
2906       for ( ; notes; notes = XEXP (notes, 1))
2907         {
2908           if (REG_NOTE_KIND (notes) == REG_INC)
2909             {
2910               int regno = REGNO (XEXP (notes, 0));
2911
2912               /* Don't delete insns to set global regs.  */
2913               if ((regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
2914                   || REGNO_REG_SET_P (needed, regno))
2915                 return 0;
2916             }
2917         }
2918     }
2919 #endif
2920
2921   /* If setting something that's a reg or part of one,
2922      see if that register's altered value will be live.  */
2923
2924   if (code == SET)
2925     {
2926       rtx r = SET_DEST (x);
2927
2928       /* A SET that is a subroutine call cannot be dead.  */
2929       if (! call_ok && GET_CODE (SET_SRC (x)) == CALL)
2930         return 0;
2931
2932 #ifdef HAVE_cc0
2933       if (GET_CODE (r) == CC0)
2934         return ! cc0_live;
2935 #endif
2936       
2937       if (GET_CODE (r) == MEM && ! MEM_VOLATILE_P (r))
2938         {
2939           rtx temp;
2940           /* Walk the set of memory locations we are currently tracking
2941              and see if one is an identical match to this memory location.
2942              If so, this memory write is dead (remember, we're walking
2943              backwards from the end of the block to the start.  */
2944           temp = mem_set_list;
2945           while (temp)
2946             {
2947               if (rtx_equal_p (XEXP (temp, 0), r))
2948                 return 1;
2949               temp = XEXP (temp, 1);
2950             }
2951         }
2952
2953       while (GET_CODE (r) == SUBREG || GET_CODE (r) == STRICT_LOW_PART
2954              || GET_CODE (r) == ZERO_EXTRACT)
2955         r = SUBREG_REG (r);
2956
2957       if (GET_CODE (r) == REG)
2958         {
2959           int regno = REGNO (r);
2960
2961           /* Don't delete insns to set global regs.  */
2962           if ((regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
2963               /* Make sure insns to set frame pointer aren't deleted.  */
2964               || (regno == FRAME_POINTER_REGNUM
2965                   && (! reload_completed || frame_pointer_needed))
2966 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2967               || (regno == HARD_FRAME_POINTER_REGNUM
2968                   && (! reload_completed || frame_pointer_needed))
2969 #endif
2970 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2971               /* Make sure insns to set arg pointer are never deleted
2972                  (if the arg pointer isn't fixed, there will be a USE for
2973                  it, so we can treat it normally).  */
2974               || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
2975 #endif
2976               || REGNO_REG_SET_P (needed, regno))
2977             return 0;
2978
2979           /* If this is a hard register, verify that subsequent words are
2980              not needed.  */
2981           if (regno < FIRST_PSEUDO_REGISTER)
2982             {
2983               int n = HARD_REGNO_NREGS (regno, GET_MODE (r));
2984
2985               while (--n > 0)
2986                 if (REGNO_REG_SET_P (needed, regno+n))
2987                   return 0;
2988             }
2989
2990           return 1;
2991         }
2992     }
2993
2994   /* If performing several activities,
2995      insn is dead if each activity is individually dead.
2996      Also, CLOBBERs and USEs can be ignored; a CLOBBER or USE
2997      that's inside a PARALLEL doesn't make the insn worth keeping.  */
2998   else if (code == PARALLEL)
2999     {
3000       int i = XVECLEN (x, 0);
3001
3002       for (i--; i >= 0; i--)
3003         if (GET_CODE (XVECEXP (x, 0, i)) != CLOBBER
3004             && GET_CODE (XVECEXP (x, 0, i)) != USE
3005             && ! insn_dead_p (XVECEXP (x, 0, i), needed, call_ok, NULL_RTX))
3006           return 0;
3007
3008       return 1;
3009     }
3010
3011   /* A CLOBBER of a pseudo-register that is dead serves no purpose.  That
3012      is not necessarily true for hard registers.  */
3013   else if (code == CLOBBER && GET_CODE (XEXP (x, 0)) == REG
3014            && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER
3015            && ! REGNO_REG_SET_P (needed, REGNO (XEXP (x, 0))))
3016     return 1;
3017
3018   /* We do not check other CLOBBER or USE here.  An insn consisting of just
3019      a CLOBBER or just a USE should not be deleted.  */
3020   return 0;
3021 }
3022
3023 /* If X is the pattern of the last insn in a libcall, and assuming X is dead,
3024    return 1 if the entire library call is dead.
3025    This is true if X copies a register (hard or pseudo)
3026    and if the hard return  reg of the call insn is dead.
3027    (The caller should have tested the destination of X already for death.)
3028
3029    If this insn doesn't just copy a register, then we don't
3030    have an ordinary libcall.  In that case, cse could not have
3031    managed to substitute the source for the dest later on,
3032    so we can assume the libcall is dead.
3033
3034    NEEDED is the bit vector of pseudoregs live before this insn.
3035    NOTE is the REG_RETVAL note of the insn.  INSN is the insn itself.  */
3036
3037 static int
3038 libcall_dead_p (x, needed, note, insn)
3039      rtx x;
3040      regset needed;
3041      rtx note;
3042      rtx insn;
3043 {
3044   register RTX_CODE code = GET_CODE (x);
3045
3046   if (code == SET)
3047     {
3048       register rtx r = SET_SRC (x);
3049       if (GET_CODE (r) == REG)
3050         {
3051           rtx call = XEXP (note, 0);
3052           rtx call_pat;
3053           register int i;
3054
3055           /* Find the call insn.  */
3056           while (call != insn && GET_CODE (call) != CALL_INSN)
3057             call = NEXT_INSN (call);
3058
3059           /* If there is none, do nothing special,
3060              since ordinary death handling can understand these insns.  */
3061           if (call == insn)
3062             return 0;
3063
3064           /* See if the hard reg holding the value is dead.
3065              If this is a PARALLEL, find the call within it.  */
3066           call_pat = PATTERN (call);
3067           if (GET_CODE (call_pat) == PARALLEL)
3068             {
3069               for (i = XVECLEN (call_pat, 0) - 1; i >= 0; i--)
3070                 if (GET_CODE (XVECEXP (call_pat, 0, i)) == SET
3071                     && GET_CODE (SET_SRC (XVECEXP (call_pat, 0, i))) == CALL)
3072                   break;
3073
3074               /* This may be a library call that is returning a value
3075                  via invisible pointer.  Do nothing special, since
3076                  ordinary death handling can understand these insns.  */
3077               if (i < 0)
3078                 return 0;
3079
3080               call_pat = XVECEXP (call_pat, 0, i);
3081             }
3082
3083           return insn_dead_p (call_pat, needed, 1, REG_NOTES (call));
3084         }
3085     }
3086   return 1;
3087 }
3088
3089 /* Return 1 if register REGNO was used before it was set, i.e. if it is
3090    live at function entry.  Don't count global register variables, variables
3091    in registers that can be used for function arg passing, or variables in
3092    fixed hard registers.  */
3093
3094 int
3095 regno_uninitialized (regno)
3096      int regno;
3097 {
3098   if (n_basic_blocks == 0
3099       || (regno < FIRST_PSEUDO_REGISTER
3100           && (global_regs[regno]
3101               || fixed_regs[regno]
3102               || FUNCTION_ARG_REGNO_P (regno))))
3103     return 0;
3104
3105   return REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, regno);
3106 }
3107
3108 /* 1 if register REGNO was alive at a place where `setjmp' was called
3109    and was set more than once or is an argument.
3110    Such regs may be clobbered by `longjmp'.  */
3111
3112 int
3113 regno_clobbered_at_setjmp (regno)
3114      int regno;
3115 {
3116   if (n_basic_blocks == 0)
3117     return 0;
3118
3119   return ((REG_N_SETS (regno) > 1
3120            || REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, regno))
3121           && REGNO_REG_SET_P (regs_live_at_setjmp, regno));
3122 }
3123 \f
3124 /* INSN references memory, possibly using autoincrement addressing modes.
3125    Find any entries on the mem_set_list that need to be invalidated due
3126    to an address change.  */
3127 static void
3128 invalidate_mems_from_autoinc (insn)
3129      rtx insn;
3130 {
3131   rtx note = REG_NOTES (insn);
3132   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
3133     {
3134       if (REG_NOTE_KIND (note) == REG_INC)
3135         {
3136           rtx temp = mem_set_list;
3137           rtx prev = NULL_RTX;
3138
3139           while (temp)
3140             {
3141               if (reg_overlap_mentioned_p (XEXP (note, 0), XEXP (temp, 0)))
3142                 {
3143                   /* Splice temp out of list.  */
3144                   if (prev)
3145                     XEXP (prev, 1) = XEXP (temp, 1);
3146                   else
3147                     mem_set_list = XEXP (temp, 1);
3148                 }
3149               else
3150                 prev = temp;
3151               temp = XEXP (temp, 1);
3152             }
3153         }
3154     }
3155 }
3156
3157 /* Process the registers that are set within X.
3158    Their bits are set to 1 in the regset DEAD,
3159    because they are dead prior to this insn.
3160
3161    If INSN is nonzero, it is the insn being processed
3162    and the fact that it is nonzero implies this is the FINAL pass
3163    in propagate_block.  In this case, various info about register
3164    usage is stored, LOG_LINKS fields of insns are set up.  */
3165
3166 static void
3167 mark_set_regs (needed, dead, x, insn, significant)
3168      regset needed;
3169      regset dead;
3170      rtx x;
3171      rtx insn;
3172      regset significant;
3173 {
3174   register RTX_CODE code = GET_CODE (x);
3175
3176   if (code == SET || code == CLOBBER)
3177     mark_set_1 (needed, dead, x, insn, significant);
3178   else if (code == PARALLEL)
3179     {
3180       register int i;
3181       for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
3182         {
3183           code = GET_CODE (XVECEXP (x, 0, i));
3184           if (code == SET || code == CLOBBER)
3185             mark_set_1 (needed, dead, XVECEXP (x, 0, i), insn, significant);
3186         }
3187     }
3188 }
3189
3190 /* Process a single SET rtx, X.  */
3191
3192 static void
3193 mark_set_1 (needed, dead, x, insn, significant)
3194      regset needed;
3195      regset dead;
3196      rtx x;
3197      rtx insn;
3198      regset significant;
3199 {
3200   register int regno;
3201   register rtx reg = SET_DEST (x);
3202
3203   /* Some targets place small structures in registers for
3204      return values of functions.  We have to detect this
3205      case specially here to get correct flow information.  */
3206   if (GET_CODE (reg) == PARALLEL
3207       && GET_MODE (reg) == BLKmode)
3208     {
3209       register int i;
3210
3211       for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
3212           mark_set_1 (needed, dead, XVECEXP (reg, 0, i), insn, significant);
3213       return;
3214     }
3215
3216   /* Modifying just one hardware register of a multi-reg value
3217      or just a byte field of a register
3218      does not mean the value from before this insn is now dead.
3219      But it does mean liveness of that register at the end of the block
3220      is significant.
3221
3222      Within mark_set_1, however, we treat it as if the register is
3223      indeed modified.  mark_used_regs will, however, also treat this
3224      register as being used.  Thus, we treat these insns as setting a
3225      new value for the register as a function of its old value.  This
3226      cases LOG_LINKS to be made appropriately and this will help combine.  */
3227
3228   while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
3229          || GET_CODE (reg) == SIGN_EXTRACT
3230          || GET_CODE (reg) == STRICT_LOW_PART)
3231     reg = XEXP (reg, 0);
3232
3233   /* If this set is a MEM, then it kills any aliased writes. 
3234      If this set is a REG, then it kills any MEMs which use the reg.  */
3235   if (GET_CODE (reg) == MEM
3236       || GET_CODE (reg) == REG)
3237     {
3238       rtx temp = mem_set_list;
3239       rtx prev = NULL_RTX;
3240
3241       while (temp)
3242         {
3243           if ((GET_CODE (reg) == MEM
3244                && output_dependence (XEXP (temp, 0), reg))
3245               || (GET_CODE (reg) == REG
3246                   && reg_overlap_mentioned_p (reg, XEXP (temp, 0))))
3247             {
3248               /* Splice this entry out of the list.  */
3249               if (prev)
3250                 XEXP (prev, 1) = XEXP (temp, 1);
3251               else
3252                 mem_set_list = XEXP (temp, 1);
3253             }
3254           else
3255             prev = temp;
3256           temp = XEXP (temp, 1);
3257         }
3258     }
3259
3260   /* If the memory reference had embedded side effects (autoincrement
3261      address modes.  Then we may need to kill some entries on the
3262      memory set list.  */
3263   if (insn && GET_CODE (reg) == MEM)
3264     invalidate_mems_from_autoinc (insn);
3265
3266   if (GET_CODE (reg) == MEM && ! side_effects_p (reg)
3267       /* We do not know the size of a BLKmode store, so we do not track
3268          them for redundant store elimination.  */
3269       && GET_MODE (reg) != BLKmode
3270       /* There are no REG_INC notes for SP, so we can't assume we'll see 
3271          everything that invalidates it.  To be safe, don't eliminate any
3272          stores though SP; none of them should be redundant anyway.  */
3273       && ! reg_mentioned_p (stack_pointer_rtx, reg))
3274     mem_set_list = gen_rtx_EXPR_LIST (VOIDmode, reg, mem_set_list);
3275
3276   if (GET_CODE (reg) == REG
3277       && (regno = REGNO (reg), ! (regno == FRAME_POINTER_REGNUM
3278                                   && (! reload_completed || frame_pointer_needed)))
3279 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3280       && ! (regno == HARD_FRAME_POINTER_REGNUM
3281             && (! reload_completed || frame_pointer_needed))
3282 #endif
3283 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3284       && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
3285 #endif
3286       && ! (regno < FIRST_PSEUDO_REGISTER && global_regs[regno]))
3287     /* && regno != STACK_POINTER_REGNUM) -- let's try without this.  */
3288     {
3289       int some_needed = REGNO_REG_SET_P (needed, regno);
3290       int some_not_needed = ! some_needed;
3291
3292       /* Mark it as a significant register for this basic block.  */
3293       if (significant)
3294         SET_REGNO_REG_SET (significant, regno);
3295
3296       /* Mark it as dead before this insn.  */
3297       SET_REGNO_REG_SET (dead, regno);
3298
3299       /* A hard reg in a wide mode may really be multiple registers.
3300          If so, mark all of them just like the first.  */
3301       if (regno < FIRST_PSEUDO_REGISTER)
3302         {
3303           int n;
3304
3305           /* Nothing below is needed for the stack pointer; get out asap.
3306              Eg, log links aren't needed, since combine won't use them.  */
3307           if (regno == STACK_POINTER_REGNUM)
3308             return;
3309
3310           n = HARD_REGNO_NREGS (regno, GET_MODE (reg));
3311           while (--n > 0)
3312             {
3313               int regno_n = regno + n;
3314               int needed_regno = REGNO_REG_SET_P (needed, regno_n);
3315               if (significant)
3316                 SET_REGNO_REG_SET (significant, regno_n);
3317
3318               SET_REGNO_REG_SET (dead, regno_n);
3319               some_needed |= needed_regno;
3320               some_not_needed |= ! needed_regno;
3321             }
3322         }
3323       /* Additional data to record if this is the final pass.  */
3324       if (insn)
3325         {
3326           register rtx y = reg_next_use[regno];
3327           register int blocknum = BLOCK_NUM (insn);
3328
3329           /* If this is a hard reg, record this function uses the reg.  */
3330
3331           if (regno < FIRST_PSEUDO_REGISTER)
3332             {
3333               register int i;
3334               int endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
3335
3336               for (i = regno; i < endregno; i++)
3337                 {
3338                   /* The next use is no longer "next", since a store
3339                      intervenes.  */
3340                   reg_next_use[i] = 0;
3341
3342                   regs_ever_live[i] = 1;
3343                   REG_N_SETS (i)++;
3344                 }
3345             }
3346           else
3347             {
3348               /* The next use is no longer "next", since a store
3349                  intervenes.  */
3350               reg_next_use[regno] = 0;
3351
3352               /* Keep track of which basic blocks each reg appears in.  */
3353
3354               if (REG_BASIC_BLOCK (regno) == REG_BLOCK_UNKNOWN)
3355                 REG_BASIC_BLOCK (regno) = blocknum;
3356               else if (REG_BASIC_BLOCK (regno) != blocknum)
3357                 REG_BASIC_BLOCK (regno) = REG_BLOCK_GLOBAL;
3358
3359               /* Count (weighted) references, stores, etc.  This counts a
3360                  register twice if it is modified, but that is correct.  */
3361               REG_N_SETS (regno)++;
3362
3363               REG_N_REFS (regno) += loop_depth;
3364                   
3365               /* The insns where a reg is live are normally counted
3366                  elsewhere, but we want the count to include the insn
3367                  where the reg is set, and the normal counting mechanism
3368                  would not count it.  */
3369               REG_LIVE_LENGTH (regno)++;
3370             }
3371
3372           if (! some_not_needed)
3373             {
3374               /* Make a logical link from the next following insn
3375                  that uses this register, back to this insn.
3376                  The following insns have already been processed.
3377
3378                  We don't build a LOG_LINK for hard registers containing
3379                  in ASM_OPERANDs.  If these registers get replaced,
3380                  we might wind up changing the semantics of the insn,
3381                  even if reload can make what appear to be valid assignments
3382                  later.  */
3383               if (y && (BLOCK_NUM (y) == blocknum)
3384                   && (regno >= FIRST_PSEUDO_REGISTER
3385                       || asm_noperands (PATTERN (y)) < 0))
3386                 LOG_LINKS (y)
3387                   = gen_rtx_INSN_LIST (VOIDmode, insn, LOG_LINKS (y));
3388             }
3389           else if (! some_needed)
3390             {
3391               /* Note that dead stores have already been deleted when possible
3392                  If we get here, we have found a dead store that cannot
3393                  be eliminated (because the same insn does something useful).
3394                  Indicate this by marking the reg being set as dying here.  */
3395               REG_NOTES (insn)
3396                 = gen_rtx_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn));
3397               REG_N_DEATHS (REGNO (reg))++;
3398             }
3399           else
3400             {
3401               /* This is a case where we have a multi-word hard register
3402                  and some, but not all, of the words of the register are
3403                  needed in subsequent insns.  Write REG_UNUSED notes
3404                  for those parts that were not needed.  This case should
3405                  be rare.  */
3406
3407               int i;
3408
3409               for (i = HARD_REGNO_NREGS (regno, GET_MODE (reg)) - 1;
3410                    i >= 0; i--)
3411                 if (!REGNO_REG_SET_P (needed, regno + i))
3412                   REG_NOTES (insn)
3413                     = gen_rtx_EXPR_LIST (REG_UNUSED,
3414                                          gen_rtx_REG (reg_raw_mode[regno + i],
3415                                                       regno + i),
3416                                          REG_NOTES (insn));
3417             }
3418         }
3419     }
3420   else if (GET_CODE (reg) == REG)
3421     reg_next_use[regno] = 0;
3422
3423   /* If this is the last pass and this is a SCRATCH, show it will be dying
3424      here and count it.  */
3425   else if (GET_CODE (reg) == SCRATCH && insn != 0)
3426     {
3427       REG_NOTES (insn)
3428         = gen_rtx_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn));
3429     }
3430 }
3431 \f
3432 #ifdef AUTO_INC_DEC
3433
3434 /* X is a MEM found in INSN.  See if we can convert it into an auto-increment
3435    reference.  */
3436
3437 static void
3438 find_auto_inc (needed, x, insn)
3439      regset needed;
3440      rtx x;
3441      rtx insn;
3442 {
3443   rtx addr = XEXP (x, 0);
3444   HOST_WIDE_INT offset = 0;
3445   rtx set;
3446
3447   /* Here we detect use of an index register which might be good for
3448      postincrement, postdecrement, preincrement, or predecrement.  */
3449
3450   if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
3451     offset = INTVAL (XEXP (addr, 1)), addr = XEXP (addr, 0);
3452
3453   if (GET_CODE (addr) == REG)
3454     {
3455       register rtx y;
3456       register int size = GET_MODE_SIZE (GET_MODE (x));
3457       rtx use;
3458       rtx incr;
3459       int regno = REGNO (addr);
3460
3461       /* Is the next use an increment that might make auto-increment? */
3462       if ((incr = reg_next_use[regno]) != 0
3463           && (set = single_set (incr)) != 0
3464           && GET_CODE (set) == SET
3465           && BLOCK_NUM (incr) == BLOCK_NUM (insn)
3466           /* Can't add side effects to jumps; if reg is spilled and
3467              reloaded, there's no way to store back the altered value.  */
3468           && GET_CODE (insn) != JUMP_INSN
3469           && (y = SET_SRC (set), GET_CODE (y) == PLUS)
3470           && XEXP (y, 0) == addr
3471           && GET_CODE (XEXP (y, 1)) == CONST_INT
3472           && ((HAVE_POST_INCREMENT
3473                && (INTVAL (XEXP (y, 1)) == size && offset == 0))
3474               || (HAVE_POST_DECREMENT
3475                   && (INTVAL (XEXP (y, 1)) == - size && offset == 0))
3476               || (HAVE_PRE_INCREMENT
3477                   && (INTVAL (XEXP (y, 1)) == size && offset == size))
3478               || (HAVE_PRE_DECREMENT
3479                   && (INTVAL (XEXP (y, 1)) == - size && offset == - size)))
3480           /* Make sure this reg appears only once in this insn.  */
3481           && (use = find_use_as_address (PATTERN (insn), addr, offset),
3482               use != 0 && use != (rtx) 1))
3483         {
3484           rtx q = SET_DEST (set);
3485           enum rtx_code inc_code = (INTVAL (XEXP (y, 1)) == size
3486                                     ? (offset ? PRE_INC : POST_INC)
3487                                     : (offset ? PRE_DEC : POST_DEC));
3488
3489           if (dead_or_set_p (incr, addr))
3490             {
3491               /* This is the simple case.  Try to make the auto-inc.  If
3492                  we can't, we are done.  Otherwise, we will do any
3493                  needed updates below.  */
3494               if (! validate_change (insn, &XEXP (x, 0),
3495                                      gen_rtx_fmt_e (inc_code, Pmode, addr),
3496                                      0))
3497                 return;
3498             }
3499           else if (GET_CODE (q) == REG
3500                    /* PREV_INSN used here to check the semi-open interval
3501                       [insn,incr).  */
3502                    && ! reg_used_between_p (q,  PREV_INSN (insn), incr)
3503                    /* We must also check for sets of q as q may be
3504                       a call clobbered hard register and there may
3505                       be a call between PREV_INSN (insn) and incr.  */
3506                    && ! reg_set_between_p (q,  PREV_INSN (insn), incr))
3507             {
3508               /* We have *p followed sometime later by q = p+size.
3509                  Both p and q must be live afterward,
3510                  and q is not used between INSN and its assignment.
3511                  Change it to q = p, ...*q..., q = q+size.
3512                  Then fall into the usual case.  */
3513               rtx insns, temp;
3514               basic_block bb;
3515
3516               start_sequence ();
3517               emit_move_insn (q, addr);
3518               insns = get_insns ();
3519               end_sequence ();
3520
3521               bb = BLOCK_FOR_INSN (insn);
3522               for (temp = insns; temp; temp = NEXT_INSN (temp))
3523                 set_block_for_insn (temp, bb);
3524
3525               /* If we can't make the auto-inc, or can't make the
3526                  replacement into Y, exit.  There's no point in making
3527                  the change below if we can't do the auto-inc and doing
3528                  so is not correct in the pre-inc case.  */
3529
3530               validate_change (insn, &XEXP (x, 0),
3531                                gen_rtx_fmt_e (inc_code, Pmode, q),
3532                                1);
3533               validate_change (incr, &XEXP (y, 0), q, 1);
3534               if (! apply_change_group ())
3535                 return;
3536
3537               /* We now know we'll be doing this change, so emit the
3538                  new insn(s) and do the updates.  */
3539               emit_insns_before (insns, insn);
3540
3541               if (BLOCK_FOR_INSN (insn)->head == insn)
3542                 BLOCK_FOR_INSN (insn)->head = insns;
3543
3544               /* INCR will become a NOTE and INSN won't contain a
3545                  use of ADDR.  If a use of ADDR was just placed in
3546                  the insn before INSN, make that the next use. 
3547                  Otherwise, invalidate it.  */
3548               if (GET_CODE (PREV_INSN (insn)) == INSN
3549                   && GET_CODE (PATTERN (PREV_INSN (insn))) == SET
3550                   && SET_SRC (PATTERN (PREV_INSN (insn))) == addr)
3551                 reg_next_use[regno] = PREV_INSN (insn);
3552               else
3553                 reg_next_use[regno] = 0;
3554
3555               addr = q;
3556               regno = REGNO (q);
3557
3558               /* REGNO is now used in INCR which is below INSN, but
3559                  it previously wasn't live here.  If we don't mark
3560                  it as needed, we'll put a REG_DEAD note for it
3561                  on this insn, which is incorrect.  */
3562               SET_REGNO_REG_SET (needed, regno);
3563
3564               /* If there are any calls between INSN and INCR, show
3565                  that REGNO now crosses them.  */
3566               for (temp = insn; temp != incr; temp = NEXT_INSN (temp))
3567                 if (GET_CODE (temp) == CALL_INSN)
3568                   REG_N_CALLS_CROSSED (regno)++;
3569             }
3570           else
3571             return;
3572
3573           /* If we haven't returned, it means we were able to make the
3574              auto-inc, so update the status.  First, record that this insn
3575              has an implicit side effect.  */
3576
3577           REG_NOTES (insn)
3578             = gen_rtx_EXPR_LIST (REG_INC, addr, REG_NOTES (insn));
3579
3580           /* Modify the old increment-insn to simply copy
3581              the already-incremented value of our register.  */
3582           if (! validate_change (incr, &SET_SRC (set), addr, 0))
3583             abort ();
3584
3585           /* If that makes it a no-op (copying the register into itself) delete
3586              it so it won't appear to be a "use" and a "set" of this
3587              register.  */
3588           if (SET_DEST (set) == addr)
3589             {
3590               PUT_CODE (incr, NOTE);
3591               NOTE_LINE_NUMBER (incr) = NOTE_INSN_DELETED;
3592               NOTE_SOURCE_FILE (incr) = 0;
3593             }
3594
3595           if (regno >= FIRST_PSEUDO_REGISTER)
3596             {
3597               /* Count an extra reference to the reg.  When a reg is
3598                  incremented, spilling it is worse, so we want to make
3599                  that less likely.  */
3600               REG_N_REFS (regno) += loop_depth;
3601
3602               /* Count the increment as a setting of the register,
3603                  even though it isn't a SET in rtl.  */
3604               REG_N_SETS (regno)++;
3605             }
3606         }
3607     }
3608 }
3609 #endif /* AUTO_INC_DEC */
3610 \f
3611 /* Scan expression X and store a 1-bit in LIVE for each reg it uses.
3612    This is done assuming the registers needed from X
3613    are those that have 1-bits in NEEDED.
3614
3615    On the final pass, FINAL is 1.  This means try for autoincrement
3616    and count the uses and deaths of each pseudo-reg.
3617
3618    INSN is the containing instruction.  If INSN is dead, this function is not
3619    called.  */
3620
3621 static void
3622 mark_used_regs (needed, live, x, final, insn)
3623      regset needed;
3624      regset live;
3625      rtx x;
3626      int final;
3627      rtx insn;
3628 {
3629   register RTX_CODE code;
3630   register int regno;
3631   int i;
3632
3633  retry:
3634   code = GET_CODE (x);
3635   switch (code)
3636     {
3637     case LABEL_REF:
3638     case SYMBOL_REF:
3639     case CONST_INT:
3640     case CONST:
3641     case CONST_DOUBLE:
3642     case PC:
3643     case ADDR_VEC:
3644     case ADDR_DIFF_VEC:
3645       return;
3646
3647 #ifdef HAVE_cc0
3648     case CC0:
3649       cc0_live = 1;
3650       return;
3651 #endif
3652
3653     case CLOBBER:
3654       /* If we are clobbering a MEM, mark any registers inside the address
3655          as being used.  */
3656       if (GET_CODE (XEXP (x, 0)) == MEM)
3657         mark_used_regs (needed, live, XEXP (XEXP (x, 0), 0), final, insn);
3658       return;
3659
3660     case MEM:
3661       /* Invalidate the data for the last MEM stored, but only if MEM is
3662          something that can be stored into.  */
3663       if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
3664           && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
3665         ; /* needn't clear the memory set list */
3666       else
3667         {
3668           rtx temp = mem_set_list;
3669           rtx prev = NULL_RTX;
3670
3671           while (temp)
3672             {
3673               if (anti_dependence (XEXP (temp, 0), x))
3674                 {
3675                   /* Splice temp out of the list.  */
3676                   if (prev)
3677                     XEXP (prev, 1) = XEXP (temp, 1);
3678                   else
3679                     mem_set_list = XEXP (temp, 1);
3680                 }
3681               else
3682                 prev = temp;
3683               temp = XEXP (temp, 1);
3684             }
3685         }
3686
3687       /* If the memory reference had embedded side effects (autoincrement
3688          address modes.  Then we may need to kill some entries on the
3689          memory set list.  */
3690       if (insn)
3691         invalidate_mems_from_autoinc (insn);
3692
3693 #ifdef AUTO_INC_DEC
3694       if (final)
3695         find_auto_inc (needed, x, insn);
3696 #endif
3697       break;
3698
3699     case SUBREG:
3700       if (GET_CODE (SUBREG_REG (x)) == REG
3701           && REGNO (SUBREG_REG (x)) >= FIRST_PSEUDO_REGISTER
3702           && (GET_MODE_SIZE (GET_MODE (x))
3703               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
3704         REG_CHANGES_SIZE (REGNO (SUBREG_REG (x))) = 1;
3705
3706       /* While we're here, optimize this case.  */
3707       x = SUBREG_REG (x);
3708
3709       /* In case the SUBREG is not of a register, don't optimize */
3710       if (GET_CODE (x) != REG)
3711         {
3712           mark_used_regs (needed, live, x, final, insn);
3713           return;
3714         }
3715
3716       /* ... fall through ...  */
3717
3718     case REG:
3719       /* See a register other than being set
3720          => mark it as needed.  */
3721
3722       regno = REGNO (x);
3723       {
3724         int some_needed = REGNO_REG_SET_P (needed, regno);
3725         int some_not_needed = ! some_needed;
3726
3727         SET_REGNO_REG_SET (live, regno);
3728
3729         /* A hard reg in a wide mode may really be multiple registers.
3730            If so, mark all of them just like the first.  */
3731         if (regno < FIRST_PSEUDO_REGISTER)
3732           {
3733             int n;
3734
3735             /* For stack ptr or fixed arg pointer,
3736                nothing below can be necessary, so waste no more time.  */
3737             if (regno == STACK_POINTER_REGNUM
3738 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3739                 || (regno == HARD_FRAME_POINTER_REGNUM
3740                     && (! reload_completed || frame_pointer_needed))
3741 #endif
3742 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3743                 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
3744 #endif
3745                 || (regno == FRAME_POINTER_REGNUM
3746                     && (! reload_completed || frame_pointer_needed)))
3747               {
3748                 /* If this is a register we are going to try to eliminate,
3749                    don't mark it live here.  If we are successful in
3750                    eliminating it, it need not be live unless it is used for
3751                    pseudos, in which case it will have been set live when
3752                    it was allocated to the pseudos.  If the register will not
3753                    be eliminated, reload will set it live at that point.  */
3754
3755                 if (! TEST_HARD_REG_BIT (elim_reg_set, regno))
3756                   regs_ever_live[regno] = 1;
3757                 return;
3758               }
3759             /* No death notes for global register variables;
3760                their values are live after this function exits.  */
3761             if (global_regs[regno])
3762               {
3763                 if (final)
3764                   reg_next_use[regno] = insn;
3765                 return;
3766               }
3767
3768             n = HARD_REGNO_NREGS (regno, GET_MODE (x));
3769             while (--n > 0)
3770               {
3771                 int regno_n = regno + n;
3772                 int needed_regno = REGNO_REG_SET_P (needed, regno_n);
3773
3774                 SET_REGNO_REG_SET (live, regno_n);
3775                 some_needed |= needed_regno;
3776                 some_not_needed |= ! needed_regno;
3777               }
3778           }
3779         if (final)
3780           {
3781             /* Record where each reg is used, so when the reg
3782                is set we know the next insn that uses it.  */
3783
3784             reg_next_use[regno] = insn;
3785
3786             if (regno < FIRST_PSEUDO_REGISTER)
3787               {
3788                 /* If a hard reg is being used,
3789                    record that this function does use it.  */
3790
3791                 i = HARD_REGNO_NREGS (regno, GET_MODE (x));
3792                 if (i == 0)
3793                   i = 1;
3794                 do
3795                   regs_ever_live[regno + --i] = 1;
3796                 while (i > 0);
3797               }
3798             else
3799               {
3800                 /* Keep track of which basic block each reg appears in.  */
3801
3802                 register int blocknum = BLOCK_NUM (insn);
3803
3804                 if (REG_BASIC_BLOCK (regno) == REG_BLOCK_UNKNOWN)
3805                   REG_BASIC_BLOCK (regno) = blocknum;
3806                 else if (REG_BASIC_BLOCK (regno) != blocknum)
3807                   REG_BASIC_BLOCK (regno) = REG_BLOCK_GLOBAL;
3808
3809                 /* Count (weighted) number of uses of each reg.  */
3810
3811                 REG_N_REFS (regno) += loop_depth;
3812               }
3813
3814             /* Record and count the insns in which a reg dies.
3815                If it is used in this insn and was dead below the insn
3816                then it dies in this insn.  If it was set in this insn,
3817                we do not make a REG_DEAD note; likewise if we already
3818                made such a note.  */
3819
3820             if (some_not_needed
3821                 && ! dead_or_set_p (insn, x)
3822 #if 0
3823                 && (regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
3824 #endif
3825                 )
3826               {
3827                 /* Check for the case where the register dying partially
3828                    overlaps the register set by this insn.  */
3829                 if (regno < FIRST_PSEUDO_REGISTER
3830                     && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
3831                   {
3832                     int n = HARD_REGNO_NREGS (regno, GET_MODE (x));
3833                     while (--n >= 0)
3834                       some_needed |= dead_or_set_regno_p (insn, regno + n);
3835                   }
3836
3837                 /* If none of the words in X is needed, make a REG_DEAD
3838                    note.  Otherwise, we must make partial REG_DEAD notes.  */
3839                 if (! some_needed)
3840                   {
3841                     REG_NOTES (insn)
3842                       = gen_rtx_EXPR_LIST (REG_DEAD, x, REG_NOTES (insn));
3843                     REG_N_DEATHS (regno)++;
3844                   }
3845                 else
3846                   {
3847                     int i;
3848
3849                     /* Don't make a REG_DEAD note for a part of a register
3850                        that is set in the insn.  */
3851
3852                     for (i = HARD_REGNO_NREGS (regno, GET_MODE (x)) - 1;
3853                          i >= 0; i--)
3854                       if (!REGNO_REG_SET_P (needed, regno + i)
3855                           && ! dead_or_set_regno_p (insn, regno + i))
3856                         REG_NOTES (insn)
3857                           = gen_rtx_EXPR_LIST (REG_DEAD,
3858                                                gen_rtx_REG (reg_raw_mode[regno + i],
3859                                                             regno + i),
3860                                                REG_NOTES (insn));
3861                   }
3862               }
3863           }
3864       }
3865       return;
3866
3867     case SET:
3868       {
3869         register rtx testreg = SET_DEST (x);
3870         int mark_dest = 0;
3871
3872         /* If storing into MEM, don't show it as being used.  But do
3873            show the address as being used.  */
3874         if (GET_CODE (testreg) == MEM)
3875           {
3876 #ifdef AUTO_INC_DEC
3877             if (final)
3878               find_auto_inc (needed, testreg, insn);
3879 #endif
3880             mark_used_regs (needed, live, XEXP (testreg, 0), final, insn);
3881             mark_used_regs (needed, live, SET_SRC (x), final, insn);
3882             return;
3883           }
3884             
3885         /* Storing in STRICT_LOW_PART is like storing in a reg
3886            in that this SET might be dead, so ignore it in TESTREG.
3887            but in some other ways it is like using the reg.
3888
3889            Storing in a SUBREG or a bit field is like storing the entire
3890            register in that if the register's value is not used
3891            then this SET is not needed.  */
3892         while (GET_CODE (testreg) == STRICT_LOW_PART
3893                || GET_CODE (testreg) == ZERO_EXTRACT
3894                || GET_CODE (testreg) == SIGN_EXTRACT
3895                || GET_CODE (testreg) == SUBREG)
3896           {
3897             if (GET_CODE (testreg) == SUBREG
3898                 && GET_CODE (SUBREG_REG (testreg)) == REG
3899                 && REGNO (SUBREG_REG (testreg)) >= FIRST_PSEUDO_REGISTER
3900                 && (GET_MODE_SIZE (GET_MODE (testreg))
3901                     != GET_MODE_SIZE (GET_MODE (SUBREG_REG (testreg)))))
3902               REG_CHANGES_SIZE (REGNO (SUBREG_REG (testreg))) = 1;
3903
3904             /* Modifying a single register in an alternate mode
3905                does not use any of the old value.  But these other
3906                ways of storing in a register do use the old value.  */
3907             if (GET_CODE (testreg) == SUBREG
3908                 && !(REG_SIZE (SUBREG_REG (testreg)) > REG_SIZE (testreg)))
3909               ;
3910             else
3911               mark_dest = 1;
3912
3913             testreg = XEXP (testreg, 0);
3914           }
3915
3916         /* If this is a store into a register,
3917            recursively scan the value being stored.  */
3918
3919         if ((GET_CODE (testreg) == PARALLEL
3920              && GET_MODE (testreg) == BLKmode)
3921             || (GET_CODE (testreg) == REG
3922                 && (regno = REGNO (testreg), ! (regno == FRAME_POINTER_REGNUM
3923                                                 && (! reload_completed || frame_pointer_needed)))
3924 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3925                 && ! (regno == HARD_FRAME_POINTER_REGNUM
3926                       && (! reload_completed || frame_pointer_needed))
3927 #endif
3928 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3929                 && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
3930 #endif
3931                 ))
3932           /* We used to exclude global_regs here, but that seems wrong.
3933              Storing in them is like storing in mem.  */
3934           {
3935             mark_used_regs (needed, live, SET_SRC (x), final, insn);
3936             if (mark_dest)
3937               mark_used_regs (needed, live, SET_DEST (x), final, insn);
3938             return;
3939           }
3940       }
3941       break;
3942
3943     case RETURN:
3944       /* If exiting needs the right stack value, consider this insn as
3945          using the stack pointer.  In any event, consider it as using
3946          all global registers and all registers used by return.  */
3947       if (! EXIT_IGNORE_STACK
3948           || (! FRAME_POINTER_REQUIRED
3949               && ! current_function_calls_alloca
3950               && flag_omit_frame_pointer)
3951           || current_function_sp_is_unchanging)
3952         SET_REGNO_REG_SET (live, STACK_POINTER_REGNUM);
3953
3954       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3955         if (global_regs[i]
3956 #ifdef EPILOGUE_USES
3957             || EPILOGUE_USES (i)
3958 #endif
3959             )
3960           SET_REGNO_REG_SET (live, i);
3961       break;
3962
3963     case ASM_OPERANDS:
3964     case UNSPEC_VOLATILE:
3965     case TRAP_IF:
3966     case ASM_INPUT:
3967       {
3968         /* Traditional and volatile asm instructions must be considered to use
3969            and clobber all hard registers, all pseudo-registers and all of
3970            memory.  So must TRAP_IF and UNSPEC_VOLATILE operations.
3971
3972            Consider for instance a volatile asm that changes the fpu rounding
3973            mode.  An insn should not be moved across this even if it only uses
3974            pseudo-regs because it might give an incorrectly rounded result. 
3975
3976            ?!? Unfortunately, marking all hard registers as live causes massive
3977            problems for the register allocator and marking all pseudos as live
3978            creates mountains of uninitialized variable warnings.
3979
3980            So for now, just clear the memory set list and mark any regs
3981            we can find in ASM_OPERANDS as used.  */
3982         if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
3983           mem_set_list = NULL_RTX;
3984
3985         /* For all ASM_OPERANDS, we must traverse the vector of input operands.
3986            We can not just fall through here since then we would be confused
3987            by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
3988            traditional asms unlike their normal usage.  */
3989         if (code == ASM_OPERANDS)
3990           {
3991             int j;
3992
3993             for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
3994               mark_used_regs (needed, live, ASM_OPERANDS_INPUT (x, j),
3995                               final, insn);
3996           }
3997         break;
3998       }
3999
4000
4001     default:
4002       break;
4003     }
4004
4005   /* Recursively scan the operands of this expression.  */
4006
4007   {
4008     register char *fmt = GET_RTX_FORMAT (code);
4009     register int i;
4010     
4011     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4012       {
4013         if (fmt[i] == 'e')
4014           {
4015             /* Tail recursive case: save a function call level.  */
4016             if (i == 0)
4017               {
4018                 x = XEXP (x, 0);
4019                 goto retry;
4020               }
4021             mark_used_regs (needed, live, XEXP (x, i), final, insn);
4022           }
4023         else if (fmt[i] == 'E')
4024           {
4025             register int j;
4026             for (j = 0; j < XVECLEN (x, i); j++)
4027               mark_used_regs (needed, live, XVECEXP (x, i, j), final, insn);
4028           }
4029       }
4030   }
4031 }
4032 \f
4033 #ifdef AUTO_INC_DEC
4034
4035 static int
4036 try_pre_increment_1 (insn)
4037      rtx insn;
4038 {
4039   /* Find the next use of this reg.  If in same basic block,
4040      make it do pre-increment or pre-decrement if appropriate.  */
4041   rtx x = single_set (insn);
4042   HOST_WIDE_INT amount = ((GET_CODE (SET_SRC (x)) == PLUS ? 1 : -1)
4043                 * INTVAL (XEXP (SET_SRC (x), 1)));
4044   int regno = REGNO (SET_DEST (x));
4045   rtx y = reg_next_use[regno];
4046   if (y != 0
4047       && BLOCK_NUM (y) == BLOCK_NUM (insn)
4048       /* Don't do this if the reg dies, or gets set in y; a standard addressing
4049          mode would be better.  */
4050       && ! dead_or_set_p (y, SET_DEST (x))
4051       && try_pre_increment (y, SET_DEST (x), amount))
4052     {
4053       /* We have found a suitable auto-increment
4054          and already changed insn Y to do it.
4055          So flush this increment-instruction.  */
4056       PUT_CODE (insn, NOTE);
4057       NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
4058       NOTE_SOURCE_FILE (insn) = 0;
4059       /* Count a reference to this reg for the increment
4060          insn we are deleting.  When a reg is incremented.
4061          spilling it is worse, so we want to make that
4062          less likely.  */
4063       if (regno >= FIRST_PSEUDO_REGISTER)
4064         {
4065           REG_N_REFS (regno) += loop_depth;
4066           REG_N_SETS (regno)++;
4067         }
4068       return 1;
4069     }
4070   return 0;
4071 }
4072
4073 /* Try to change INSN so that it does pre-increment or pre-decrement
4074    addressing on register REG in order to add AMOUNT to REG.
4075    AMOUNT is negative for pre-decrement.
4076    Returns 1 if the change could be made.
4077    This checks all about the validity of the result of modifying INSN.  */
4078
4079 static int
4080 try_pre_increment (insn, reg, amount)
4081      rtx insn, reg;
4082      HOST_WIDE_INT amount;
4083 {
4084   register rtx use;
4085
4086   /* Nonzero if we can try to make a pre-increment or pre-decrement.
4087      For example, addl $4,r1; movl (r1),... can become movl +(r1),...  */
4088   int pre_ok = 0;
4089   /* Nonzero if we can try to make a post-increment or post-decrement.
4090      For example, addl $4,r1; movl -4(r1),... can become movl (r1)+,...
4091      It is possible for both PRE_OK and POST_OK to be nonzero if the machine
4092      supports both pre-inc and post-inc, or both pre-dec and post-dec.  */
4093   int post_ok = 0;
4094
4095   /* Nonzero if the opportunity actually requires post-inc or post-dec.  */
4096   int do_post = 0;
4097
4098   /* From the sign of increment, see which possibilities are conceivable
4099      on this target machine.  */
4100   if (HAVE_PRE_INCREMENT && amount > 0)
4101     pre_ok = 1;
4102   if (HAVE_POST_INCREMENT && amount > 0)
4103     post_ok = 1;
4104
4105   if (HAVE_PRE_DECREMENT && amount < 0)
4106     pre_ok = 1;
4107   if (HAVE_POST_DECREMENT && amount < 0)
4108     post_ok = 1;
4109
4110   if (! (pre_ok || post_ok))
4111     return 0;
4112
4113   /* It is not safe to add a side effect to a jump insn
4114      because if the incremented register is spilled and must be reloaded
4115      there would be no way to store the incremented value back in memory.  */
4116
4117   if (GET_CODE (insn) == JUMP_INSN)
4118     return 0;
4119
4120   use = 0;
4121   if (pre_ok)
4122     use = find_use_as_address (PATTERN (insn), reg, 0);
4123   if (post_ok && (use == 0 || use == (rtx) 1))
4124     {
4125       use = find_use_as_address (PATTERN (insn), reg, -amount);
4126       do_post = 1;
4127     }
4128
4129   if (use == 0 || use == (rtx) 1)
4130     return 0;
4131
4132   if (GET_MODE_SIZE (GET_MODE (use)) != (amount > 0 ? amount : - amount))
4133     return 0;
4134
4135   /* See if this combination of instruction and addressing mode exists.  */
4136   if (! validate_change (insn, &XEXP (use, 0),
4137                          gen_rtx_fmt_e (amount > 0
4138                                         ? (do_post ? POST_INC : PRE_INC)
4139                                         : (do_post ? POST_DEC : PRE_DEC),
4140                                         Pmode, reg), 0))
4141     return 0;
4142
4143   /* Record that this insn now has an implicit side effect on X.  */
4144   REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_INC, reg, REG_NOTES (insn));
4145   return 1;
4146 }
4147
4148 #endif /* AUTO_INC_DEC */
4149 \f
4150 /* Find the place in the rtx X where REG is used as a memory address.
4151    Return the MEM rtx that so uses it.
4152    If PLUSCONST is nonzero, search instead for a memory address equivalent to
4153    (plus REG (const_int PLUSCONST)).
4154
4155    If such an address does not appear, return 0.
4156    If REG appears more than once, or is used other than in such an address,
4157    return (rtx)1.  */
4158
4159 rtx
4160 find_use_as_address (x, reg, plusconst)
4161      register rtx x;
4162      rtx reg;
4163      HOST_WIDE_INT plusconst;
4164 {
4165   enum rtx_code code = GET_CODE (x);
4166   char *fmt = GET_RTX_FORMAT (code);
4167   register int i;
4168   register rtx value = 0;
4169   register rtx tem;
4170
4171   if (code == MEM && XEXP (x, 0) == reg && plusconst == 0)
4172     return x;
4173
4174   if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
4175       && XEXP (XEXP (x, 0), 0) == reg
4176       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4177       && INTVAL (XEXP (XEXP (x, 0), 1)) == plusconst)
4178     return x;
4179
4180   if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
4181     {
4182       /* If REG occurs inside a MEM used in a bit-field reference,
4183          that is unacceptable.  */
4184       if (find_use_as_address (XEXP (x, 0), reg, 0) != 0)
4185         return (rtx) (HOST_WIDE_INT) 1;
4186     }
4187
4188   if (x == reg)
4189     return (rtx) (HOST_WIDE_INT) 1;
4190
4191   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4192     {
4193       if (fmt[i] == 'e')
4194         {
4195           tem = find_use_as_address (XEXP (x, i), reg, plusconst);
4196           if (value == 0)
4197             value = tem;
4198           else if (tem != 0)
4199             return (rtx) (HOST_WIDE_INT) 1;
4200         }
4201       if (fmt[i] == 'E')
4202         {
4203           register int j;
4204           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4205             {
4206               tem = find_use_as_address (XVECEXP (x, i, j), reg, plusconst);
4207               if (value == 0)
4208                 value = tem;
4209               else if (tem != 0)
4210                 return (rtx) (HOST_WIDE_INT) 1;
4211             }
4212         }
4213     }
4214
4215   return value;
4216 }
4217 \f
4218 /* Write information about registers and basic blocks into FILE.
4219    This is part of making a debugging dump.  */
4220
4221 void
4222 dump_flow_info (file)
4223      FILE *file;
4224 {
4225   register int i;
4226   static char *reg_class_names[] = REG_CLASS_NAMES;
4227
4228   fprintf (file, "%d registers.\n", max_regno);
4229   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
4230     if (REG_N_REFS (i))
4231       {
4232         enum reg_class class, altclass;
4233         fprintf (file, "\nRegister %d used %d times across %d insns",
4234                  i, REG_N_REFS (i), REG_LIVE_LENGTH (i));
4235         if (REG_BASIC_BLOCK (i) >= 0)
4236           fprintf (file, " in block %d", REG_BASIC_BLOCK (i));
4237         if (REG_N_SETS (i))
4238           fprintf (file, "; set %d time%s", REG_N_SETS (i),
4239                    (REG_N_SETS (i) == 1) ? "" : "s");
4240         if (REG_USERVAR_P (regno_reg_rtx[i]))
4241           fprintf (file, "; user var");
4242         if (REG_N_DEATHS (i) != 1)
4243           fprintf (file, "; dies in %d places", REG_N_DEATHS (i));
4244         if (REG_N_CALLS_CROSSED (i) == 1)
4245           fprintf (file, "; crosses 1 call");
4246         else if (REG_N_CALLS_CROSSED (i))
4247           fprintf (file, "; crosses %d calls", REG_N_CALLS_CROSSED (i));
4248         if (PSEUDO_REGNO_BYTES (i) != UNITS_PER_WORD)
4249           fprintf (file, "; %d bytes", PSEUDO_REGNO_BYTES (i));
4250         class = reg_preferred_class (i);
4251         altclass = reg_alternate_class (i);
4252         if (class != GENERAL_REGS || altclass != ALL_REGS)
4253           {
4254             if (altclass == ALL_REGS || class == ALL_REGS)
4255               fprintf (file, "; pref %s", reg_class_names[(int) class]);
4256             else if (altclass == NO_REGS)
4257               fprintf (file, "; %s or none", reg_class_names[(int) class]);
4258             else
4259               fprintf (file, "; pref %s, else %s",
4260                        reg_class_names[(int) class],
4261                        reg_class_names[(int) altclass]);
4262           }
4263         if (REGNO_POINTER_FLAG (i))
4264           fprintf (file, "; pointer");
4265         fprintf (file, ".\n");
4266       }
4267
4268   fprintf (file, "\n%d basic blocks.\n", n_basic_blocks);
4269   for (i = 0; i < n_basic_blocks; i++)
4270     {
4271       register basic_block bb = BASIC_BLOCK (i);
4272       register int regno;
4273       register edge e;
4274
4275       fprintf (file, "\nBasic block %d: first insn %d, last %d.\n",
4276                i, INSN_UID (bb->head), INSN_UID (bb->end));
4277
4278       fprintf (file, "Predecessors: ");
4279       for (e = bb->pred; e ; e = e->pred_next)
4280         dump_edge_info (file, e, 0);
4281
4282       fprintf (file, "\nSuccessors: ");
4283       for (e = bb->succ; e ; e = e->succ_next)
4284         dump_edge_info (file, e, 1);
4285
4286       fprintf (file, "\nRegisters live at start:");
4287       if (bb->global_live_at_start)
4288         {
4289           for (regno = 0; regno < max_regno; regno++)
4290             if (REGNO_REG_SET_P (bb->global_live_at_start, regno))
4291               fprintf (file, " %d", regno);
4292         }
4293       else
4294         fprintf (file, " n/a");
4295
4296       fprintf (file, "\nRegisters live at end:");
4297       if (bb->global_live_at_end)
4298         {
4299           for (regno = 0; regno < max_regno; regno++)
4300             if (REGNO_REG_SET_P (bb->global_live_at_end, regno))
4301               fprintf (file, " %d", regno);
4302         }
4303       else
4304         fprintf (file, " n/a");
4305
4306       putc('\n', file);
4307     }
4308
4309   putc('\n', file);
4310 }
4311
4312 static void
4313 dump_edge_info (file, e, do_succ)
4314      FILE *file;
4315      edge e;
4316      int do_succ;
4317 {
4318   basic_block side = (do_succ ? e->dest : e->src);
4319
4320   if (side == ENTRY_BLOCK_PTR)
4321     fputs (" ENTRY", file);
4322   else if (side == EXIT_BLOCK_PTR)
4323     fputs (" EXIT", file);
4324   else
4325     fprintf (file, " %d", side->index);
4326
4327   if (e->flags)
4328     {
4329       static char * bitnames[] = {
4330         "fallthru", "crit", "ab", "abcall", "eh", "fake"
4331       };
4332       int comma = 0;
4333       int i, flags = e->flags;
4334
4335       fputc (' ', file);
4336       fputc ('(', file);
4337       for (i = 0; flags; i++)
4338         if (flags & (1 << i))
4339           {
4340             flags &= ~(1 << i);
4341
4342             if (comma)
4343               fputc (',', file);
4344             if (i < (int)(sizeof (bitnames) / sizeof (*bitnames)))
4345               fputs (bitnames[i], file);
4346             else
4347               fprintf (file, "%d", i);
4348             comma = 1;
4349           }
4350       fputc (')', file);
4351     }
4352 }
4353
4354 \f
4355 /* Like print_rtl, but also print out live information for the start of each
4356    basic block.  */
4357
4358 void
4359 print_rtl_with_bb (outf, rtx_first)
4360      FILE *outf;
4361      rtx rtx_first;
4362 {
4363   register rtx tmp_rtx;
4364
4365   if (rtx_first == 0)
4366     fprintf (outf, "(nil)\n");
4367   else
4368     {
4369       int i;
4370       enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
4371       int max_uid = get_max_uid ();
4372       basic_block *start = (basic_block *)
4373         alloca (max_uid * sizeof (basic_block));
4374       basic_block *end = (basic_block *)
4375         alloca (max_uid * sizeof (basic_block));
4376       enum bb_state *in_bb_p = (enum bb_state *)
4377         alloca (max_uid * sizeof (enum bb_state));
4378
4379       memset (start, 0, max_uid * sizeof (basic_block));
4380       memset (end, 0, max_uid * sizeof (basic_block));
4381       memset (in_bb_p, 0, max_uid * sizeof (enum bb_state));
4382
4383       for (i = n_basic_blocks - 1; i >= 0; i--)
4384         {
4385           basic_block bb = BASIC_BLOCK (i);
4386           rtx x;
4387
4388           start[INSN_UID (bb->head)] = bb;
4389           end[INSN_UID (bb->end)] = bb;
4390           for (x = bb->head; x != NULL_RTX; x = NEXT_INSN (x))
4391             {
4392               enum bb_state state = IN_MULTIPLE_BB;
4393               if (in_bb_p[INSN_UID(x)] == NOT_IN_BB)
4394                 state = IN_ONE_BB;
4395               in_bb_p[INSN_UID(x)] = state;
4396
4397               if (x == bb->end)
4398                 break;
4399             }
4400         }
4401
4402       for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx))
4403         {
4404           int did_output;
4405           basic_block bb;
4406
4407           if ((bb = start[INSN_UID (tmp_rtx)]) != NULL)
4408             {
4409               fprintf (outf, ";; Start of basic block %d, registers live:",
4410                        bb->index);
4411
4412               EXECUTE_IF_SET_IN_REG_SET (bb->global_live_at_start, 0, i,
4413                                          {
4414                                            fprintf (outf, " %d", i);
4415                                            if (i < FIRST_PSEUDO_REGISTER)
4416                                              fprintf (outf, " [%s]",
4417                                                       reg_names[i]);
4418                                          });
4419               putc ('\n', outf);
4420             }
4421
4422           if (in_bb_p[INSN_UID(tmp_rtx)] == NOT_IN_BB
4423               && GET_CODE (tmp_rtx) != NOTE
4424               && GET_CODE (tmp_rtx) != BARRIER
4425               && ! obey_regdecls)
4426             fprintf (outf, ";; Insn is not within a basic block\n");
4427           else if (in_bb_p[INSN_UID(tmp_rtx)] == IN_MULTIPLE_BB)
4428             fprintf (outf, ";; Insn is in multiple basic blocks\n");
4429
4430           did_output = print_rtl_single (outf, tmp_rtx);
4431
4432           if ((bb = end[INSN_UID (tmp_rtx)]) != NULL)
4433             fprintf (outf, ";; End of basic block %d\n", bb->index);
4434
4435           if (did_output)
4436             putc ('\n', outf);
4437         }
4438     }
4439 }
4440
4441 \f
4442 /* Integer list support.  */
4443
4444 /* Allocate a node from list *HEAD_PTR.  */
4445
4446 static int_list_ptr
4447 alloc_int_list_node (head_ptr)
4448      int_list_block **head_ptr;
4449 {
4450   struct int_list_block *first_blk = *head_ptr;
4451
4452   if (first_blk == NULL || first_blk->nodes_left <= 0)
4453     {
4454       first_blk = (struct int_list_block *) xmalloc (sizeof (struct int_list_block));
4455       first_blk->nodes_left = INT_LIST_NODES_IN_BLK;
4456       first_blk->next = *head_ptr;
4457       *head_ptr = first_blk;
4458     }
4459
4460   first_blk->nodes_left--;
4461   return &first_blk->nodes[first_blk->nodes_left];
4462 }
4463
4464 /* Pointer to head of predecessor/successor block list.  */
4465 static int_list_block *pred_int_list_blocks;
4466
4467 /* Add a new node to integer list LIST with value VAL.
4468    LIST is a pointer to a list object to allow for different implementations.
4469    If *LIST is initially NULL, the list is empty.
4470    The caller must not care whether the element is added to the front or
4471    to the end of the list (to allow for different implementations).  */
4472
4473 static int_list_ptr
4474 add_int_list_node (blk_list, list, val)
4475      int_list_block **blk_list;
4476      int_list **list;
4477      int val;
4478 {
4479   int_list_ptr p = alloc_int_list_node (blk_list);
4480
4481   p->val = val;
4482   p->next = *list;
4483   *list = p;
4484   return p;
4485 }
4486
4487 /* Free the blocks of lists at BLK_LIST.  */
4488
4489 void
4490 free_int_list (blk_list)
4491      int_list_block **blk_list;
4492 {
4493   int_list_block *p, *next;
4494
4495   for (p = *blk_list; p != NULL; p = next)
4496     {
4497       next = p->next;
4498       free (p);
4499     }
4500
4501   /* Mark list as empty for the next function we compile.  */
4502   *blk_list = NULL;
4503 }
4504 \f
4505 /* Predecessor/successor computation.  */
4506
4507 /* Mark PRED_BB a precessor of SUCC_BB,
4508    and conversely SUCC_BB a successor of PRED_BB.  */
4509
4510 static void
4511 add_pred_succ (pred_bb, succ_bb, s_preds, s_succs, num_preds, num_succs)
4512      int pred_bb;
4513      int succ_bb;
4514      int_list_ptr *s_preds;
4515      int_list_ptr *s_succs;
4516      int *num_preds;
4517      int *num_succs;
4518 {
4519   if (succ_bb != EXIT_BLOCK)
4520     {
4521       add_int_list_node (&pred_int_list_blocks, &s_preds[succ_bb], pred_bb);
4522       num_preds[succ_bb]++;
4523     }
4524   if (pred_bb != ENTRY_BLOCK)
4525     {
4526       add_int_list_node (&pred_int_list_blocks, &s_succs[pred_bb], succ_bb);
4527       num_succs[pred_bb]++;
4528     }
4529 }
4530
4531 /* Convert edge lists into pred/succ lists for backward compatibility.  */
4532
4533 void
4534 compute_preds_succs (s_preds, s_succs, num_preds, num_succs)
4535      int_list_ptr *s_preds;
4536      int_list_ptr *s_succs;
4537      int *num_preds;
4538      int *num_succs;
4539 {
4540   int i, n = n_basic_blocks;
4541   edge e;
4542
4543   memset (s_preds, 0, n_basic_blocks * sizeof (int_list_ptr));
4544   memset (s_succs, 0, n_basic_blocks * sizeof (int_list_ptr));
4545   memset (num_preds, 0, n_basic_blocks * sizeof (int));
4546   memset (num_succs, 0, n_basic_blocks * sizeof (int));
4547
4548   for (i = 0; i < n; ++i)
4549     {
4550       basic_block bb = BASIC_BLOCK (i);
4551       
4552       for (e = bb->succ; e ; e = e->succ_next)
4553         add_pred_succ (i, e->dest->index, s_preds, s_succs,
4554                        num_preds, num_succs);
4555     }
4556
4557   for (e = ENTRY_BLOCK_PTR->succ; e ; e = e->succ_next)
4558     add_pred_succ (ENTRY_BLOCK, e->dest->index, s_preds, s_succs,
4559                    num_preds, num_succs);
4560 }
4561
4562 void
4563 dump_bb_data (file, preds, succs, live_info)
4564      FILE *file;
4565      int_list_ptr *preds;
4566      int_list_ptr *succs;
4567      int live_info;
4568 {
4569   int bb;
4570   int_list_ptr p;
4571
4572   fprintf (file, "BB data\n\n");
4573   for (bb = 0; bb < n_basic_blocks; bb++)
4574     {
4575       fprintf (file, "BB %d, start %d, end %d\n", bb,
4576                INSN_UID (BLOCK_HEAD (bb)), INSN_UID (BLOCK_END (bb)));
4577       fprintf (file, "  preds:");
4578       for (p = preds[bb]; p != NULL; p = p->next)
4579         {
4580           int pred_bb = INT_LIST_VAL (p);
4581           if (pred_bb == ENTRY_BLOCK)
4582             fprintf (file, " entry");
4583           else
4584             fprintf (file, " %d", pred_bb);
4585         }
4586       fprintf (file, "\n");
4587       fprintf (file, "  succs:");
4588       for (p = succs[bb]; p != NULL; p = p->next)
4589         {
4590           int succ_bb = INT_LIST_VAL (p);
4591           if (succ_bb == EXIT_BLOCK)
4592             fprintf (file, " exit");
4593           else
4594             fprintf (file, " %d", succ_bb);
4595         }
4596       if (live_info)
4597         {
4598           int regno;
4599           fprintf (file, "\nRegisters live at start:");
4600           for (regno = 0; regno < max_regno; regno++)
4601             if (REGNO_REG_SET_P (BASIC_BLOCK (bb)->global_live_at_start, regno))
4602               fprintf (file, " %d", regno);
4603           fprintf (file, "\n");
4604         }
4605       fprintf (file, "\n");
4606     }
4607   fprintf (file, "\n");
4608 }
4609
4610 /* Free basic block data storage.  */
4611
4612 void
4613 free_bb_mem ()
4614 {
4615   free_int_list (&pred_int_list_blocks);
4616 }
4617
4618 /* Compute dominator relationships.  */
4619 void
4620 compute_dominators (dominators, post_dominators, s_preds, s_succs)
4621      sbitmap *dominators;
4622      sbitmap *post_dominators;
4623      int_list_ptr *s_preds;
4624      int_list_ptr *s_succs;
4625 {
4626   int bb, changed, passes;
4627   sbitmap *temp_bitmap;
4628
4629   temp_bitmap = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
4630   sbitmap_vector_ones (dominators, n_basic_blocks);
4631   sbitmap_vector_ones (post_dominators, n_basic_blocks);
4632   sbitmap_vector_zero (temp_bitmap, n_basic_blocks);
4633
4634   sbitmap_zero (dominators[0]);
4635   SET_BIT (dominators[0], 0);
4636
4637   sbitmap_zero (post_dominators[n_basic_blocks - 1]);
4638   SET_BIT (post_dominators[n_basic_blocks - 1], 0);
4639
4640   passes = 0;
4641   changed = 1;
4642   while (changed)
4643     {
4644       changed = 0;
4645       for (bb = 1; bb < n_basic_blocks; bb++)
4646         {
4647           sbitmap_intersect_of_predecessors (temp_bitmap[bb], dominators,
4648                                              bb, s_preds);
4649           SET_BIT (temp_bitmap[bb], bb);
4650           changed |= sbitmap_a_and_b (dominators[bb],
4651                                       dominators[bb],
4652                                       temp_bitmap[bb]);
4653           sbitmap_intersect_of_successors (temp_bitmap[bb], post_dominators,
4654                                            bb, s_succs);
4655           SET_BIT (temp_bitmap[bb], bb);
4656           changed |= sbitmap_a_and_b (post_dominators[bb],
4657                                       post_dominators[bb],
4658                                       temp_bitmap[bb]);
4659         }
4660       passes++;
4661     }
4662
4663   free (temp_bitmap);
4664 }
4665
4666 /* Given DOMINATORS, compute the immediate dominators into IDOM.  */
4667
4668 void
4669 compute_immediate_dominators (idom, dominators)
4670      int *idom;
4671      sbitmap *dominators;
4672 {
4673   sbitmap *tmp;
4674   int b;
4675
4676   tmp = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
4677
4678   /* Begin with tmp(n) = dom(n) - { n }.  */
4679   for (b = n_basic_blocks; --b >= 0; )
4680     {
4681       sbitmap_copy (tmp[b], dominators[b]);
4682       RESET_BIT (tmp[b], b);
4683     }
4684
4685   /* Subtract out all of our dominator's dominators.  */
4686   for (b = n_basic_blocks; --b >= 0; )
4687     {
4688       sbitmap tmp_b = tmp[b];
4689       int s;
4690
4691       for (s = n_basic_blocks; --s >= 0; )
4692         if (TEST_BIT (tmp_b, s))
4693           sbitmap_difference (tmp_b, tmp_b, tmp[s]);
4694     }
4695
4696   /* Find the one bit set in the bitmap and put it in the output array.  */
4697   for (b = n_basic_blocks; --b >= 0; )
4698     {
4699       int t;
4700       EXECUTE_IF_SET_IN_SBITMAP (tmp[b], 0, t, { idom[b] = t; });
4701     }
4702
4703   sbitmap_vector_free (tmp);
4704 }
4705
4706 /* Count for a single SET rtx, X.  */
4707
4708 static void
4709 count_reg_sets_1 (x)
4710      rtx x;
4711 {
4712   register int regno;
4713   register rtx reg = SET_DEST (x);
4714
4715   /* Find the register that's set/clobbered.  */
4716   while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
4717          || GET_CODE (reg) == SIGN_EXTRACT
4718          || GET_CODE (reg) == STRICT_LOW_PART)
4719     reg = XEXP (reg, 0);
4720
4721   if (GET_CODE (reg) == PARALLEL
4722       && GET_MODE (reg) == BLKmode)
4723     {
4724       register int i;
4725       for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
4726         count_reg_sets_1 (XVECEXP (reg, 0, i));
4727       return;
4728     }
4729
4730   if (GET_CODE (reg) == REG)
4731     {
4732       regno = REGNO (reg);
4733       if (regno >= FIRST_PSEUDO_REGISTER)
4734         {
4735           /* Count (weighted) references, stores, etc.  This counts a
4736              register twice if it is modified, but that is correct.  */
4737           REG_N_SETS (regno)++;
4738
4739           REG_N_REFS (regno) += loop_depth;
4740         }
4741     }
4742 }
4743
4744 /* Increment REG_N_SETS for each SET or CLOBBER found in X; also increment
4745    REG_N_REFS by the current loop depth for each SET or CLOBBER found.  */
4746
4747 static void
4748 count_reg_sets  (x)
4749      rtx x;
4750 {
4751   register RTX_CODE code = GET_CODE (x);
4752
4753   if (code == SET || code == CLOBBER)
4754     count_reg_sets_1 (x);
4755   else if (code == PARALLEL)
4756     {
4757       register int i;
4758       for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
4759         {
4760           code = GET_CODE (XVECEXP (x, 0, i));
4761           if (code == SET || code == CLOBBER)
4762             count_reg_sets_1 (XVECEXP (x, 0, i));
4763         }
4764     }
4765 }
4766
4767 /* Increment REG_N_REFS by the current loop depth each register reference
4768    found in X.  */
4769
4770 static void
4771 count_reg_references (x)
4772      rtx x;
4773 {
4774   register RTX_CODE code;
4775
4776  retry:
4777   code = GET_CODE (x);
4778   switch (code)
4779     {
4780     case LABEL_REF:
4781     case SYMBOL_REF:
4782     case CONST_INT:
4783     case CONST:
4784     case CONST_DOUBLE:
4785     case PC:
4786     case ADDR_VEC:
4787     case ADDR_DIFF_VEC:
4788     case ASM_INPUT:
4789       return;
4790
4791 #ifdef HAVE_cc0
4792     case CC0:
4793       return;
4794 #endif
4795
4796     case CLOBBER:
4797       /* If we are clobbering a MEM, mark any registers inside the address
4798          as being used.  */
4799       if (GET_CODE (XEXP (x, 0)) == MEM)
4800         count_reg_references (XEXP (XEXP (x, 0), 0));
4801       return;
4802
4803     case SUBREG:
4804       /* While we're here, optimize this case.  */
4805       x = SUBREG_REG (x);
4806
4807       /* In case the SUBREG is not of a register, don't optimize */
4808       if (GET_CODE (x) != REG)
4809         {
4810           count_reg_references (x);
4811           return;
4812         }
4813
4814       /* ... fall through ...  */
4815
4816     case REG:
4817       if (REGNO (x) >= FIRST_PSEUDO_REGISTER)
4818         REG_N_REFS (REGNO (x)) += loop_depth;
4819       return;
4820
4821     case SET:
4822       {
4823         register rtx testreg = SET_DEST (x);
4824         int mark_dest = 0;
4825
4826         /* If storing into MEM, don't show it as being used.  But do
4827            show the address as being used.  */
4828         if (GET_CODE (testreg) == MEM)
4829           {
4830             count_reg_references (XEXP (testreg, 0));
4831             count_reg_references (SET_SRC (x));
4832             return;
4833           }
4834             
4835         /* Storing in STRICT_LOW_PART is like storing in a reg
4836            in that this SET might be dead, so ignore it in TESTREG.
4837            but in some other ways it is like using the reg.
4838
4839            Storing in a SUBREG or a bit field is like storing the entire
4840            register in that if the register's value is not used
4841            then this SET is not needed.  */
4842         while (GET_CODE (testreg) == STRICT_LOW_PART
4843                || GET_CODE (testreg) == ZERO_EXTRACT
4844                || GET_CODE (testreg) == SIGN_EXTRACT
4845                || GET_CODE (testreg) == SUBREG)
4846           {
4847             /* Modifying a single register in an alternate mode
4848                does not use any of the old value.  But these other
4849                ways of storing in a register do use the old value.  */
4850             if (GET_CODE (testreg) == SUBREG
4851                 && !(REG_SIZE (SUBREG_REG (testreg)) > REG_SIZE (testreg)))
4852               ;
4853             else
4854               mark_dest = 1;
4855
4856             testreg = XEXP (testreg, 0);
4857           }
4858
4859         /* If this is a store into a register,
4860            recursively scan the value being stored.  */
4861
4862         if ((GET_CODE (testreg) == PARALLEL
4863              && GET_MODE (testreg) == BLKmode)
4864             || GET_CODE (testreg) == REG)
4865           {
4866             count_reg_references (SET_SRC (x));
4867             if (mark_dest)
4868               count_reg_references (SET_DEST (x));
4869             return;
4870           }
4871       }
4872       break;
4873
4874     default:
4875       break;
4876     }
4877
4878   /* Recursively scan the operands of this expression.  */
4879
4880   {
4881     register char *fmt = GET_RTX_FORMAT (code);
4882     register int i;
4883     
4884     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4885       {
4886         if (fmt[i] == 'e')
4887           {
4888             /* Tail recursive case: save a function call level.  */
4889             if (i == 0)
4890               {
4891                 x = XEXP (x, 0);
4892                 goto retry;
4893               }
4894             count_reg_references (XEXP (x, i));
4895           }
4896         else if (fmt[i] == 'E')
4897           {
4898             register int j;
4899             for (j = 0; j < XVECLEN (x, i); j++)
4900               count_reg_references (XVECEXP (x, i, j));
4901           }
4902       }
4903   }
4904 }
4905
4906 /* Recompute register set/reference counts immediately prior to register
4907    allocation.
4908
4909    This avoids problems with set/reference counts changing to/from values
4910    which have special meanings to the register allocators.
4911
4912    Additionally, the reference counts are the primary component used by the
4913    register allocators to prioritize pseudos for allocation to hard regs.
4914    More accurate reference counts generally lead to better register allocation.
4915
4916    F is the first insn to be scanned.
4917    LOOP_STEP denotes how much loop_depth should be incremented per
4918    loop nesting level in order to increase the ref count more for references
4919    in a loop.
4920
4921    It might be worthwhile to update REG_LIVE_LENGTH, REG_BASIC_BLOCK and
4922    possibly other information which is used by the register allocators.  */
4923
4924 void
4925 recompute_reg_usage (f, loop_step)
4926      rtx f;
4927      int loop_step;
4928 {
4929   rtx insn;
4930   int i, max_reg;
4931
4932   /* Clear out the old data.  */
4933   max_reg = max_reg_num ();
4934   for (i = FIRST_PSEUDO_REGISTER; i < max_reg; i++)
4935     {
4936       REG_N_SETS (i) = 0;
4937       REG_N_REFS (i) = 0;
4938     }
4939
4940   /* Scan each insn in the chain and count how many times each register is
4941      set/used.  */
4942   loop_depth = 1;
4943   for (insn = f; insn; insn = NEXT_INSN (insn))
4944     {
4945       /* Keep track of loop depth.  */
4946       if (GET_CODE (insn) == NOTE)
4947         {
4948           /* Look for loop boundaries.  */
4949           if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
4950             loop_depth -= loop_step;
4951           else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
4952             loop_depth += loop_step;
4953
4954           /* If we have LOOP_DEPTH == 0, there has been a bookkeeping error. 
4955              Abort now rather than setting register status incorrectly.  */
4956           if (loop_depth == 0)
4957             abort ();
4958         }
4959       else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
4960         {
4961           rtx links;
4962
4963           /* This call will increment REG_N_SETS for each SET or CLOBBER
4964              of a register in INSN.  It will also increment REG_N_REFS
4965              by the loop depth for each set of a register in INSN.  */
4966           count_reg_sets (PATTERN (insn));
4967
4968           /* count_reg_sets does not detect autoincrement address modes, so
4969              detect them here by looking at the notes attached to INSN.  */
4970           for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
4971             {
4972               if (REG_NOTE_KIND (links) == REG_INC)
4973                 /* Count (weighted) references, stores, etc.  This counts a
4974                    register twice if it is modified, but that is correct.  */
4975                 REG_N_SETS (REGNO (XEXP (links, 0)))++;
4976             }
4977
4978           /* This call will increment REG_N_REFS by the current loop depth for
4979              each reference to a register in INSN.  */
4980           count_reg_references (PATTERN (insn));
4981
4982           /* count_reg_references will not include counts for arguments to
4983              function calls, so detect them here by examining the
4984              CALL_INSN_FUNCTION_USAGE data.  */
4985           if (GET_CODE (insn) == CALL_INSN)
4986             {
4987               rtx note;
4988
4989               for (note = CALL_INSN_FUNCTION_USAGE (insn);
4990                    note;
4991                    note = XEXP (note, 1))
4992                 if (GET_CODE (XEXP (note, 0)) == USE)
4993                   count_reg_references (SET_DEST (XEXP (note, 0)));
4994             }
4995         }
4996     }
4997 }
4998
4999 /* Record INSN's block as BB.  */
5000
5001 void
5002 set_block_for_insn (insn, bb)
5003      rtx insn;
5004      basic_block bb;
5005 {
5006   size_t uid = INSN_UID (insn);
5007   if (uid >= basic_block_for_insn->num_elements)
5008     {
5009       int new_size;
5010       
5011       /* Add one-eighth the size so we don't keep calling xrealloc.  */
5012       new_size = uid + (uid + 7) / 8;
5013
5014       VARRAY_GROW (basic_block_for_insn, new_size);
5015     }
5016   VARRAY_BB (basic_block_for_insn, uid) = bb;
5017 }
5018
5019 /* Record INSN's block number as BB.  */
5020 /* ??? This has got to go.  */
5021
5022 void
5023 set_block_num (insn, bb)
5024      rtx insn;
5025      int bb;
5026 {
5027   set_block_for_insn (insn, BASIC_BLOCK (bb));
5028 }
5029 \f
5030 /* Verify the CFG consistency.  This function check some CFG invariants and
5031    aborts when something is wrong.  Hope that this function will help to
5032    convert many optimization passes to preserve CFG consistent.
5033
5034    Currently it does following checks: 
5035
5036    - test head/end pointers
5037    - overlapping of basic blocks
5038    - edge list corectness
5039    - headers of basic blocks (the NOTE_INSN_BASIC_BLOCK note)
5040    - tails of basic blocks (ensure that boundary is necesary)
5041    - scans body of the basic block for JUMP_INSN, CODE_LABEL
5042      and NOTE_INSN_BASIC_BLOCK
5043    - check that all insns are in the basic blocks 
5044    (except the switch handling code, barriers and notes)
5045
5046    In future it can be extended check a lot of other stuff as well
5047    (reachability of basic blocks, life information, etc. etc.).  */
5048
5049 void
5050 verify_flow_info ()
5051 {
5052   const int max_uid = get_max_uid ();
5053   const rtx rtx_first = get_insns ();
5054   basic_block *bb_info;
5055   rtx x;
5056   int i;
5057
5058   bb_info = (basic_block *) alloca (max_uid * sizeof (basic_block));
5059   memset (bb_info, 0, max_uid * sizeof (basic_block));
5060
5061   /* First pass check head/end pointers and set bb_info array used by
5062      later passes.  */
5063   for (i = n_basic_blocks - 1; i >= 0; i--)
5064     {
5065       basic_block bb = BASIC_BLOCK (i);
5066
5067       /* Check the head pointer and make sure that it is pointing into
5068          insn list.  */
5069       for (x = rtx_first; x != NULL_RTX; x = NEXT_INSN (x))
5070         if (x == bb->head)
5071           break;
5072       if (!x)
5073         {
5074           fatal ("verify_flow_info: Head insn %d for block %d not found in the insn stream.\n",
5075                  INSN_UID (bb->head), bb->index);
5076         }
5077
5078       /* Check the end pointer and make sure that it is pointing into
5079          insn list.  */
5080       for (x = bb->head; x != NULL_RTX; x = NEXT_INSN (x))
5081         {
5082           if (bb_info[INSN_UID (x)] != NULL)
5083             {
5084               fatal ("verify_flow_info: Insn %d is in multiple basic blocks (%d and %d)",
5085                      INSN_UID (x), bb->index, bb_info[INSN_UID (x)]->index);
5086             }
5087           bb_info[INSN_UID (x)] = bb;
5088
5089           if (x == bb->end)
5090             break;
5091         }
5092       if (!x)
5093         {
5094           fatal ("verify_flow_info: End insn %d for block %d not found in the insn stream.\n",
5095                  INSN_UID (bb->end), bb->index);
5096         }
5097     }
5098
5099   /* Now check the basic blocks (boundaries etc.) */
5100   for (i = n_basic_blocks - 1; i >= 0; i--)
5101     {
5102       basic_block bb = BASIC_BLOCK (i);
5103       /* Check corectness of edge lists */
5104       edge e;
5105
5106       e = bb->succ;
5107       while (e)
5108         {
5109           if (e->src != bb)
5110             {
5111               fprintf (stderr, "verify_flow_info: Basic block %d succ edge is corrupted\n",
5112                        bb->index);
5113               fprintf (stderr, "Predecessor: ");
5114               dump_edge_info (stderr, e, 0);
5115               fprintf (stderr, "\nSuccessor: ");
5116               dump_edge_info (stderr, e, 1);
5117               fflush (stderr);
5118               abort ();
5119             }
5120           if (e->dest != EXIT_BLOCK_PTR)
5121             {
5122               edge e2 = e->dest->pred;
5123               while (e2 && e2 != e)
5124                 e2 = e2->pred_next;
5125               if (!e2)
5126                 {
5127                   fatal ("verify_flow_info: Basic block %i edge lists are corrupted\n",
5128                          bb->index);
5129                 }
5130             }
5131           e = e->succ_next;
5132         }
5133
5134       e = bb->pred;
5135       while (e)
5136         {
5137           if (e->dest != bb)
5138             {
5139               fprintf (stderr, "verify_flow_info: Basic block %d pred edge is corrupted\n",
5140                        bb->index);
5141               fprintf (stderr, "Predecessor: ");
5142               dump_edge_info (stderr, e, 0);
5143               fprintf (stderr, "\nSuccessor: ");
5144               dump_edge_info (stderr, e, 1);
5145               fflush (stderr);
5146               abort ();
5147             }
5148           if (e->src != ENTRY_BLOCK_PTR)
5149             {
5150               edge e2 = e->src->succ;
5151               while (e2 && e2 != e)
5152                 e2 = e2->succ_next;
5153               if (!e2)
5154                 {
5155                   fatal ("verify_flow_info: Basic block %i edge lists are corrupted\n",
5156                          bb->index);
5157                 }
5158             }
5159           e = e->pred_next;
5160         }
5161
5162       /* OK pointers are correct.  Now check the header of basic
5163          block.  It ought to contain optional CODE_LABEL followed
5164          by NOTE_BASIC_BLOCK.  */
5165       x = bb->head;
5166       if (GET_CODE (x) == CODE_LABEL)
5167         {
5168           if (bb->end == x)
5169             {
5170               fatal ("verify_flow_info: Basic block contains only CODE_LABEL and no NOTE_INSN_BASIC_BLOCK note\n");
5171             }
5172           x = NEXT_INSN (x);
5173         }
5174       if (GET_CODE (x) != NOTE
5175           || NOTE_LINE_NUMBER (x) != NOTE_INSN_BASIC_BLOCK
5176           || NOTE_BASIC_BLOCK (x) != bb)
5177         {
5178           fatal ("verify_flow_info: NOTE_INSN_BASIC_BLOCK is missing for block %d\n",
5179                  bb->index);
5180         }
5181
5182       if (bb->end == x)
5183         {
5184           /* Do checks for empty blocks here */
5185         }
5186       else
5187         {
5188           x = NEXT_INSN (x);
5189           while (x)
5190             {
5191               if (GET_CODE (x) == NOTE
5192                   && NOTE_LINE_NUMBER (x) == NOTE_INSN_BASIC_BLOCK)
5193                 {
5194                   fatal ("verify_flow_info: NOTE_INSN_BASIC_BLOCK %d in the middle of basic block %d\n",
5195                          INSN_UID (x), bb->index);
5196                 }
5197
5198               if (x == bb->end)
5199                 break;
5200
5201               if (GET_CODE (x) == JUMP_INSN
5202                   || GET_CODE (x) == CODE_LABEL
5203                   || GET_CODE (x) == BARRIER)
5204                 {
5205                   fatal_insn ("verify_flow_info: Incorrect insn in the middle of basic block %d\n",
5206                               x, bb->index);
5207                 }
5208
5209               x = NEXT_INSN (x);
5210             }
5211         }
5212     }
5213
5214   x = rtx_first;
5215   while (x)
5216     {
5217       if (!bb_info[INSN_UID (x)])
5218         {
5219           switch (GET_CODE (x))
5220             {
5221             case BARRIER:
5222             case NOTE:
5223               break;
5224
5225             case CODE_LABEL:
5226               /* An addr_vec is placed outside any block block.  */
5227               if (NEXT_INSN (x)
5228                   && GET_CODE (NEXT_INSN (x)) == JUMP_INSN
5229                   && (GET_CODE (PATTERN (NEXT_INSN (x))) == ADDR_DIFF_VEC
5230                       || GET_CODE (PATTERN (NEXT_INSN (x))) == ADDR_VEC))
5231                 {
5232                   x = NEXT_INSN (x);
5233                 }
5234
5235               /* But in any case, non-deletable labels can appear anywhere.  */
5236               break;
5237
5238             default:
5239               fatal_insn ("verify_flow_info: Insn outside basic block\n", x);
5240             }
5241         }
5242
5243       x = NEXT_INSN (x);
5244     }
5245 }