OSDN Git Service

* flow.c (print_rtl_and_abort_fcn): Renamed from print_rtl_and_abort.
[pf3gnuchains/gcc-fork.git] / gcc / flow.c
1 /* Data flow analysis for GNU compiler.
2    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001 Free Software Foundation, Inc.
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
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_CROSSED 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 "tree.h"
124 #include "rtl.h"
125 #include "tm_p.h"
126 #include "hard-reg-set.h"
127 #include "basic-block.h"
128 #include "insn-config.h"
129 #include "regs.h"
130 #include "flags.h"
131 #include "output.h"
132 #include "function.h"
133 #include "except.h"
134 #include "toplev.h"
135 #include "recog.h"
136 #include "insn-flags.h"
137 #include "expr.h"
138 #include "ssa.h"
139
140 #include "obstack.h"
141 #include "splay-tree.h"
142
143 #define obstack_chunk_alloc xmalloc
144 #define obstack_chunk_free free
145
146 /* EXIT_IGNORE_STACK should be nonzero if, when returning from a function,
147    the stack pointer does not matter.  The value is tested only in
148    functions that have frame pointers.
149    No definition is equivalent to always zero.  */
150 #ifndef EXIT_IGNORE_STACK
151 #define EXIT_IGNORE_STACK 0
152 #endif
153
154 #ifndef HAVE_epilogue
155 #define HAVE_epilogue 0
156 #endif
157 #ifndef HAVE_prologue
158 #define HAVE_prologue 0
159 #endif
160 #ifndef HAVE_sibcall_epilogue
161 #define HAVE_sibcall_epilogue 0
162 #endif
163
164 #ifndef LOCAL_REGNO
165 #define LOCAL_REGNO(REGNO)  0
166 #endif
167 #ifndef EPILOGUE_USES
168 #define EPILOGUE_USES(REGNO)  0
169 #endif
170
171 /* The obstack on which the flow graph components are allocated.  */
172
173 struct obstack flow_obstack;
174 static char *flow_firstobj;
175
176 /* Number of basic blocks in the current function.  */
177
178 int n_basic_blocks;
179
180 /* Number of edges in the current function.  */
181
182 int n_edges;
183
184 /* The basic block array.  */
185
186 varray_type basic_block_info;
187
188 /* The special entry and exit blocks.  */
189
190 struct basic_block_def entry_exit_blocks[2]
191 = {{NULL,                       /* head */
192     NULL,                       /* end */
193     NULL,                       /* pred */
194     NULL,                       /* succ */
195     NULL,                       /* local_set */
196     NULL,                       /* cond_local_set */
197     NULL,                       /* global_live_at_start */
198     NULL,                       /* global_live_at_end */
199     NULL,                       /* aux */
200     ENTRY_BLOCK,                /* index */
201     0,                          /* loop_depth */
202     -1, -1,                     /* eh_beg, eh_end */
203     0                           /* count */
204   },
205   {
206     NULL,                       /* head */
207     NULL,                       /* end */
208     NULL,                       /* pred */
209     NULL,                       /* succ */
210     NULL,                       /* local_set */
211     NULL,                       /* cond_local_set */
212     NULL,                       /* global_live_at_start */
213     NULL,                       /* global_live_at_end */
214     NULL,                       /* aux */
215     EXIT_BLOCK,                 /* index */
216     0,                          /* loop_depth */
217     -1, -1,                     /* eh_beg, eh_end */
218     0                           /* count */
219   }
220 };
221
222 /* Nonzero if the second flow pass has completed.  */
223 int flow2_completed;
224
225 /* Maximum register number used in this function, plus one.  */
226
227 int max_regno;
228
229 /* Indexed by n, giving various register information */
230
231 varray_type reg_n_info;
232
233 /* Size of a regset for the current function,
234    in (1) bytes and (2) elements.  */
235
236 int regset_bytes;
237 int regset_size;
238
239 /* Regset of regs live when calls to `setjmp'-like functions happen.  */
240 /* ??? Does this exist only for the setjmp-clobbered warning message?  */
241
242 regset regs_live_at_setjmp;
243
244 /* List made of EXPR_LIST rtx's which gives pairs of pseudo registers
245    that have to go in the same hard reg.
246    The first two regs in the list are a pair, and the next two
247    are another pair, etc.  */
248 rtx regs_may_share;
249
250 /* Callback that determines if it's ok for a function to have no
251    noreturn attribute.  */
252 int (*lang_missing_noreturn_ok_p) PARAMS ((tree));
253
254 /* Set of registers that may be eliminable.  These are handled specially
255    in updating regs_ever_live.  */
256
257 static HARD_REG_SET elim_reg_set;
258
259 /* The basic block structure for every insn, indexed by uid.  */
260
261 varray_type basic_block_for_insn;
262
263 /* The labels mentioned in non-jump rtl.  Valid during find_basic_blocks.  */
264 /* ??? Should probably be using LABEL_NUSES instead.  It would take a
265    bit of surgery to be able to use or co-opt the routines in jump.  */
266
267 static rtx label_value_list;
268 static rtx tail_recursion_label_list;
269
270 /* Holds information for tracking conditional register life information.  */
271 struct reg_cond_life_info
272 {
273   /* An EXPR_LIST of conditions under which a register is dead.  */
274   rtx condition;
275
276   /* ??? Could store mask of bytes that are dead, so that we could finally
277      track lifetimes of multi-word registers accessed via subregs.  */
278 };
279
280 /* For use in communicating between propagate_block and its subroutines.
281    Holds all information needed to compute life and def-use information.  */
282
283 struct propagate_block_info
284 {
285   /* The basic block we're considering.  */
286   basic_block bb;
287
288   /* Bit N is set if register N is conditionally or unconditionally live.  */
289   regset reg_live;
290
291   /* Bit N is set if register N is set this insn.  */
292   regset new_set;
293
294   /* Element N is the next insn that uses (hard or pseudo) register N
295      within the current basic block; or zero, if there is no such insn.  */
296   rtx *reg_next_use;
297
298   /* Contains a list of all the MEMs we are tracking for dead store
299      elimination.  */
300   rtx mem_set_list;
301
302   /* If non-null, record the set of registers set unconditionally in the
303      basic block.  */
304   regset local_set;
305
306   /* If non-null, record the set of registers set conditionally in the
307      basic block.  */
308   regset cond_local_set;
309
310 #ifdef HAVE_conditional_execution
311   /* Indexed by register number, holds a reg_cond_life_info for each
312      register that is not unconditionally live or dead.  */
313   splay_tree reg_cond_dead;
314
315   /* Bit N is set if register N is in an expression in reg_cond_dead.  */
316   regset reg_cond_reg;
317 #endif
318
319   /* The length of mem_set_list.  */
320   int mem_set_list_len;
321
322   /* Non-zero if the value of CC0 is live.  */
323   int cc0_live;
324
325   /* Flags controling the set of information propagate_block collects.  */
326   int flags;
327 };
328
329 /* Maximum length of pbi->mem_set_list before we start dropping
330    new elements on the floor.  */
331 #define MAX_MEM_SET_LIST_LEN    100
332
333 /* Store the data structures necessary for depth-first search.  */
334 struct depth_first_search_dsS {
335   /* stack for backtracking during the algorithm */
336   basic_block *stack;
337
338   /* number of edges in the stack.  That is, positions 0, ..., sp-1
339      have edges.  */
340   unsigned int sp;
341
342   /* record of basic blocks already seen by depth-first search */
343   sbitmap visited_blocks;
344 };
345 typedef struct depth_first_search_dsS *depth_first_search_ds;
346
347 /* Have print_rtl_and_abort give the same information that fancy_abort
348    does.  */
349 #define print_rtl_and_abort() \
350   print_rtl_and_abort_fcn (__FILE__, __LINE__, __FUNCTION__)
351
352 /* Forward declarations */
353 static int count_basic_blocks           PARAMS ((rtx));
354 static void find_basic_blocks_1         PARAMS ((rtx));
355 static rtx find_label_refs              PARAMS ((rtx, rtx));
356 static void clear_edges                 PARAMS ((void));
357 static void make_edges                  PARAMS ((rtx));
358 static void make_label_edge             PARAMS ((sbitmap *, basic_block,
359                                                  rtx, int));
360 static void make_eh_edge                PARAMS ((sbitmap *, eh_nesting_info *,
361                                                  basic_block, rtx, int));
362 static void mark_critical_edges         PARAMS ((void));
363 static void move_stray_eh_region_notes  PARAMS ((void));
364 static void record_active_eh_regions    PARAMS ((rtx));
365
366 static void commit_one_edge_insertion   PARAMS ((edge));
367
368 static void delete_unreachable_blocks   PARAMS ((void));
369 static void delete_eh_regions           PARAMS ((void));
370 static int can_delete_note_p            PARAMS ((rtx));
371 static void expunge_block               PARAMS ((basic_block));
372 static int can_delete_label_p           PARAMS ((rtx));
373 static int tail_recursion_label_p       PARAMS ((rtx));
374 static int merge_blocks_move_predecessor_nojumps PARAMS ((basic_block,
375                                                           basic_block));
376 static int merge_blocks_move_successor_nojumps PARAMS ((basic_block,
377                                                         basic_block));
378 static int merge_blocks                 PARAMS ((edge,basic_block,basic_block));
379 static void try_merge_blocks            PARAMS ((void));
380 static void tidy_fallthru_edges         PARAMS ((void));
381 static int verify_wide_reg_1            PARAMS ((rtx *, void *));
382 static void verify_wide_reg             PARAMS ((int, rtx, rtx));
383 static void verify_local_live_at_start  PARAMS ((regset, basic_block));
384 static int set_noop_p                   PARAMS ((rtx));
385 static int noop_move_p                  PARAMS ((rtx));
386 static void delete_noop_moves           PARAMS ((rtx));
387 static void notice_stack_pointer_modification_1 PARAMS ((rtx, rtx, void *));
388 static void notice_stack_pointer_modification PARAMS ((rtx));
389 static void mark_reg                    PARAMS ((rtx, void *));
390 static void mark_regs_live_at_end       PARAMS ((regset));
391 static int set_phi_alternative_reg      PARAMS ((rtx, int, int, void *));
392 static void calculate_global_regs_live  PARAMS ((sbitmap, sbitmap, int));
393 static void propagate_block_delete_insn PARAMS ((basic_block, rtx));
394 static rtx propagate_block_delete_libcall PARAMS ((basic_block, rtx, rtx));
395 static int insn_dead_p                  PARAMS ((struct propagate_block_info *,
396                                                  rtx, int, rtx));
397 static int libcall_dead_p               PARAMS ((struct propagate_block_info *,
398                                                  rtx, rtx));
399 static void mark_set_regs               PARAMS ((struct propagate_block_info *,
400                                                  rtx, rtx));
401 static void mark_set_1                  PARAMS ((struct propagate_block_info *,
402                                                  enum rtx_code, rtx, rtx,
403                                                  rtx, int));
404 #ifdef HAVE_conditional_execution
405 static int mark_regno_cond_dead         PARAMS ((struct propagate_block_info *,
406                                                  int, rtx));
407 static void free_reg_cond_life_info     PARAMS ((splay_tree_value));
408 static int flush_reg_cond_reg_1         PARAMS ((splay_tree_node, void *));
409 static void flush_reg_cond_reg          PARAMS ((struct propagate_block_info *,
410                                                  int));
411 static rtx elim_reg_cond                PARAMS ((rtx, unsigned int));
412 static rtx ior_reg_cond                 PARAMS ((rtx, rtx, int));
413 static rtx not_reg_cond                 PARAMS ((rtx));
414 static rtx and_reg_cond                 PARAMS ((rtx, rtx, int));
415 #endif
416 #ifdef AUTO_INC_DEC
417 static void attempt_auto_inc            PARAMS ((struct propagate_block_info *,
418                                                  rtx, rtx, rtx, rtx, rtx));
419 static void find_auto_inc               PARAMS ((struct propagate_block_info *,
420                                                  rtx, rtx));
421 static int try_pre_increment_1          PARAMS ((struct propagate_block_info *,
422                                                  rtx));
423 static int try_pre_increment            PARAMS ((rtx, rtx, HOST_WIDE_INT));
424 #endif
425 static void mark_used_reg               PARAMS ((struct propagate_block_info *,
426                                                  rtx, rtx, rtx));
427 static void mark_used_regs              PARAMS ((struct propagate_block_info *,
428                                                  rtx, rtx, rtx));
429 void dump_flow_info                     PARAMS ((FILE *));
430 void debug_flow_info                    PARAMS ((void));
431 static void dump_edge_info              PARAMS ((FILE *, edge, int));
432 static void print_rtl_and_abort_fcn     PARAMS ((const char *, int,
433                                                  const char *))
434                                         ATTRIBUTE_NORETURN;
435
436 static void invalidate_mems_from_autoinc PARAMS ((struct propagate_block_info *,
437                                                   rtx));
438 static void invalidate_mems_from_set    PARAMS ((struct propagate_block_info *,
439                                                  rtx));
440 static void remove_fake_successors      PARAMS ((basic_block));
441 static void flow_nodes_print            PARAMS ((const char *, const sbitmap,
442                                                  FILE *));
443 static void flow_edge_list_print        PARAMS ((const char *, const edge *,
444                                                  int, FILE *));
445 static void flow_loops_cfg_dump         PARAMS ((const struct loops *,
446                                                  FILE *));
447 static int flow_loop_nested_p           PARAMS ((struct loop *,
448                                                  struct loop *));
449 static int flow_loop_entry_edges_find   PARAMS ((basic_block, const sbitmap,
450                                                  edge **));
451 static int flow_loop_exit_edges_find    PARAMS ((const sbitmap, edge **));
452 static int flow_loop_nodes_find PARAMS ((basic_block, basic_block, sbitmap));
453 static int flow_depth_first_order_compute PARAMS ((int *, int *));
454 static void flow_dfs_compute_reverse_init
455   PARAMS ((depth_first_search_ds));
456 static void flow_dfs_compute_reverse_add_bb
457   PARAMS ((depth_first_search_ds, basic_block));
458 static basic_block flow_dfs_compute_reverse_execute
459   PARAMS ((depth_first_search_ds));
460 static void flow_dfs_compute_reverse_finish
461   PARAMS ((depth_first_search_ds));
462 static void flow_loop_pre_header_scan PARAMS ((struct loop *));
463 static basic_block flow_loop_pre_header_find PARAMS ((basic_block,
464                                                       const sbitmap *));
465 static void flow_loop_tree_node_add     PARAMS ((struct loop *, struct loop *));
466 static void flow_loops_tree_build       PARAMS ((struct loops *));
467 static int flow_loop_level_compute      PARAMS ((struct loop *, int));
468 static int flow_loops_level_compute     PARAMS ((struct loops *));
469 static void allocate_bb_life_data       PARAMS ((void));
470 \f
471 /* Find basic blocks of the current function.
472    F is the first insn of the function and NREGS the number of register
473    numbers in use.  */
474
475 void
476 find_basic_blocks (f, nregs, file)
477      rtx f;
478      int nregs ATTRIBUTE_UNUSED;
479      FILE *file ATTRIBUTE_UNUSED;
480 {
481   int max_uid;
482
483   /* Flush out existing data.  */
484   if (basic_block_info != NULL)
485     {
486       int i;
487
488       clear_edges ();
489
490       /* Clear bb->aux on all extant basic blocks.  We'll use this as a
491          tag for reuse during create_basic_block, just in case some pass
492          copies around basic block notes improperly.  */
493       for (i = 0; i < n_basic_blocks; ++i)
494         BASIC_BLOCK (i)->aux = NULL;
495
496       VARRAY_FREE (basic_block_info);
497     }
498
499   n_basic_blocks = count_basic_blocks (f);
500
501   /* Size the basic block table.  The actual structures will be allocated
502      by find_basic_blocks_1, since we want to keep the structure pointers
503      stable across calls to find_basic_blocks.  */
504   /* ??? This whole issue would be much simpler if we called find_basic_blocks
505      exactly once, and thereafter we don't have a single long chain of
506      instructions at all until close to the end of compilation when we
507      actually lay them out.  */
508
509   VARRAY_BB_INIT (basic_block_info, n_basic_blocks, "basic_block_info");
510
511   find_basic_blocks_1 (f);
512
513   /* Record the block to which an insn belongs.  */
514   /* ??? This should be done another way, by which (perhaps) a label is
515      tagged directly with the basic block that it starts.  It is used for
516      more than that currently, but IMO that is the only valid use.  */
517
518   max_uid = get_max_uid ();
519 #ifdef AUTO_INC_DEC
520   /* Leave space for insns life_analysis makes in some cases for auto-inc.
521      These cases are rare, so we don't need too much space.  */
522   max_uid += max_uid / 10;
523 #endif
524
525   compute_bb_for_insn (max_uid);
526
527   /* Discover the edges of our cfg.  */
528   record_active_eh_regions (f);
529   make_edges (label_value_list);
530
531   /* Do very simple cleanup now, for the benefit of code that runs between
532      here and cleanup_cfg, e.g. thread_prologue_and_epilogue_insns.  */
533   tidy_fallthru_edges ();
534
535   mark_critical_edges ();
536
537 #ifdef ENABLE_CHECKING
538   verify_flow_info ();
539 #endif
540 }
541
542 void
543 check_function_return_warnings ()
544 {
545   if (warn_missing_noreturn
546       && !TREE_THIS_VOLATILE (cfun->decl)
547       && EXIT_BLOCK_PTR->pred == NULL
548       && (lang_missing_noreturn_ok_p
549           && !lang_missing_noreturn_ok_p (cfun->decl)))
550     warning ("function might be possible candidate for attribute `noreturn'");
551
552   /* If we have a path to EXIT, then we do return.  */
553   if (TREE_THIS_VOLATILE (cfun->decl)
554       && EXIT_BLOCK_PTR->pred != NULL)
555     warning ("`noreturn' function does return");
556
557   /* If the clobber_return_insn appears in some basic block, then we
558      do reach the end without returning a value.  */
559   else if (warn_return_type
560            && cfun->x_clobber_return_insn != NULL
561            && EXIT_BLOCK_PTR->pred != NULL)
562     {
563       int max_uid = get_max_uid ();
564
565       /* If clobber_return_insn was excised by jump1, then renumber_insns
566          can make max_uid smaller than the number still recorded in our rtx.
567          That's fine, since this is a quick way of verifying that the insn
568          is no longer in the chain.  */
569       if (INSN_UID (cfun->x_clobber_return_insn) < max_uid)
570         {
571           /* Recompute insn->block mapping, since the initial mapping is
572              set before we delete unreachable blocks.  */
573           compute_bb_for_insn (max_uid);
574
575           if (BLOCK_FOR_INSN (cfun->x_clobber_return_insn) != NULL)
576             warning ("control reaches end of non-void function");
577         }
578     }
579 }
580
581 /* Count the basic blocks of the function.  */
582
583 static int
584 count_basic_blocks (f)
585      rtx f;
586 {
587   register rtx insn;
588   register RTX_CODE prev_code;
589   register int count = 0;
590   int eh_region = 0;
591   int call_had_abnormal_edge = 0;
592
593   prev_code = JUMP_INSN;
594   for (insn = f; insn; insn = NEXT_INSN (insn))
595     {
596       register RTX_CODE code = GET_CODE (insn);
597
598       if (code == CODE_LABEL
599           || (GET_RTX_CLASS (code) == 'i'
600               && (prev_code == JUMP_INSN
601                   || prev_code == BARRIER
602                   || (prev_code == CALL_INSN && call_had_abnormal_edge))))
603         count++;
604
605       /* Record whether this call created an edge.  */
606       if (code == CALL_INSN)
607         {
608           rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
609           int region = (note ? INTVAL (XEXP (note, 0)) : 1);
610
611           call_had_abnormal_edge = 0;
612
613           /* If there is an EH region or rethrow, we have an edge.  */
614           if ((eh_region && region > 0)
615               || find_reg_note (insn, REG_EH_RETHROW, NULL_RTX))
616             call_had_abnormal_edge = 1;
617           else if (nonlocal_goto_handler_labels && region >= 0)
618             /* If there is a nonlocal goto label and the specified
619                region number isn't -1, we have an edge. (0 means
620                no throw, but might have a nonlocal goto).  */
621             call_had_abnormal_edge = 1;
622         }
623
624       if (code != NOTE)
625         prev_code = code;
626       else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG)
627         ++eh_region;
628       else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END)
629         --eh_region;
630     }
631
632   /* The rest of the compiler works a bit smoother when we don't have to
633      check for the edge case of do-nothing functions with no basic blocks.  */
634   if (count == 0)
635     {
636       emit_insn (gen_rtx_USE (VOIDmode, const0_rtx));
637       count = 1;
638     }
639
640   return count;
641 }
642
643 /* Scan a list of insns for labels referred to other than by jumps.
644    This is used to scan the alternatives of a call placeholder.  */
645 static rtx
646 find_label_refs (f, lvl)
647      rtx f;
648      rtx lvl;
649 {
650   rtx insn;
651
652   for (insn = f; insn; insn = NEXT_INSN (insn))
653     if (INSN_P (insn) && GET_CODE (insn) != JUMP_INSN)
654       {
655         rtx note;
656
657         /* Make a list of all labels referred to other than by jumps
658            (which just don't have the REG_LABEL notes).
659
660            Make a special exception for labels followed by an ADDR*VEC,
661            as this would be a part of the tablejump setup code.
662
663            Make a special exception for the eh_return_stub_label, which
664            we know isn't part of any otherwise visible control flow.
665
666            Make a special exception to registers loaded with label
667            values just before jump insns that use them.  */
668
669         for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
670           if (REG_NOTE_KIND (note) == REG_LABEL)
671             {
672               rtx lab = XEXP (note, 0), next;
673
674               if (lab == eh_return_stub_label)
675                 ;
676               else if ((next = next_nonnote_insn (lab)) != NULL
677                        && GET_CODE (next) == JUMP_INSN
678                        && (GET_CODE (PATTERN (next)) == ADDR_VEC
679                            || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC))
680                 ;
681               else if (GET_CODE (lab) == NOTE)
682                 ;
683               else if (GET_CODE (NEXT_INSN (insn)) == JUMP_INSN
684                        && find_reg_note (NEXT_INSN (insn), REG_LABEL, lab))
685                 ;
686               else
687                 lvl = alloc_EXPR_LIST (0, XEXP (note, 0), lvl);
688             }
689       }
690
691   return lvl;
692 }
693
694 /* Find all basic blocks of the function whose first insn is F.
695
696    Collect and return a list of labels whose addresses are taken.  This
697    will be used in make_edges for use with computed gotos.  */
698
699 static void
700 find_basic_blocks_1 (f)
701      rtx f;
702 {
703   register rtx insn, next;
704   int i = 0;
705   rtx bb_note = NULL_RTX;
706   rtx eh_list = NULL_RTX;
707   rtx lvl = NULL_RTX;
708   rtx trll = NULL_RTX;
709   rtx head = NULL_RTX;
710   rtx end = NULL_RTX;
711
712   /* We process the instructions in a slightly different way than we did
713      previously.  This is so that we see a NOTE_BASIC_BLOCK after we have
714      closed out the previous block, so that it gets attached at the proper
715      place.  Since this form should be equivalent to the previous,
716      count_basic_blocks continues to use the old form as a check.  */
717
718   for (insn = f; insn; insn = next)
719     {
720       enum rtx_code code = GET_CODE (insn);
721
722       next = NEXT_INSN (insn);
723
724       switch (code)
725         {
726         case NOTE:
727           {
728             int kind = NOTE_LINE_NUMBER (insn);
729
730             /* Keep a LIFO list of the currently active exception notes.  */
731             if (kind == NOTE_INSN_EH_REGION_BEG)
732               eh_list = alloc_INSN_LIST (insn, eh_list);
733             else if (kind == NOTE_INSN_EH_REGION_END)
734               {
735                 rtx t = eh_list;
736
737                 eh_list = XEXP (eh_list, 1);
738                 free_INSN_LIST_node (t);
739               }
740
741             /* Look for basic block notes with which to keep the
742                basic_block_info pointers stable.  Unthread the note now;
743                we'll put it back at the right place in create_basic_block.
744                Or not at all if we've already found a note in this block.  */
745             else if (kind == NOTE_INSN_BASIC_BLOCK)
746               {
747                 if (bb_note == NULL_RTX)
748                   bb_note = insn;
749                 else
750                   next = flow_delete_insn (insn);
751               }
752             break;
753           }
754
755         case CODE_LABEL:
756           /* A basic block starts at a label.  If we've closed one off due
757              to a barrier or some such, no need to do it again.  */
758           if (head != NULL_RTX)
759             {
760               /* While we now have edge lists with which other portions of
761                  the compiler might determine a call ending a basic block
762                  does not imply an abnormal edge, it will be a bit before
763                  everything can be updated.  So continue to emit a noop at
764                  the end of such a block.  */
765               if (GET_CODE (end) == CALL_INSN && ! SIBLING_CALL_P (end))
766                 {
767                   rtx nop = gen_rtx_USE (VOIDmode, const0_rtx);
768                   end = emit_insn_after (nop, end);
769                 }
770
771               create_basic_block (i++, head, end, bb_note);
772               bb_note = NULL_RTX;
773             }
774
775           head = end = insn;
776           break;
777
778         case JUMP_INSN:
779           /* A basic block ends at a jump.  */
780           if (head == NULL_RTX)
781             head = insn;
782           else
783             {
784               /* ??? Make a special check for table jumps.  The way this
785                  happens is truly and amazingly gross.  We are about to
786                  create a basic block that contains just a code label and
787                  an addr*vec jump insn.  Worse, an addr_diff_vec creates
788                  its own natural loop.
789
790                  Prevent this bit of brain damage, pasting things together
791                  correctly in make_edges.
792
793                  The correct solution involves emitting the table directly
794                  on the tablejump instruction as a note, or JUMP_LABEL.  */
795
796               if (GET_CODE (PATTERN (insn)) == ADDR_VEC
797                   || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
798                 {
799                   head = end = NULL;
800                   n_basic_blocks--;
801                   break;
802                 }
803             }
804           end = insn;
805           goto new_bb_inclusive;
806
807         case BARRIER:
808           /* A basic block ends at a barrier.  It may be that an unconditional
809              jump already closed the basic block -- no need to do it again.  */
810           if (head == NULL_RTX)
811             break;
812
813           /* While we now have edge lists with which other portions of the
814              compiler might determine a call ending a basic block does not
815              imply an abnormal edge, it will be a bit before everything can
816              be updated.  So continue to emit a noop at the end of such a
817              block.  */
818           if (GET_CODE (end) == CALL_INSN && ! SIBLING_CALL_P (end))
819             {
820               rtx nop = gen_rtx_USE (VOIDmode, const0_rtx);
821               end = emit_insn_after (nop, end);
822             }
823           goto new_bb_exclusive;
824
825         case CALL_INSN:
826           {
827             /* Record whether this call created an edge.  */
828             rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
829             int region = (note ? INTVAL (XEXP (note, 0)) : 1);
830             int call_has_abnormal_edge = 0;
831
832             if (GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
833               {
834                 /* Scan each of the alternatives for label refs.  */
835                 lvl = find_label_refs (XEXP (PATTERN (insn), 0), lvl);
836                 lvl = find_label_refs (XEXP (PATTERN (insn), 1), lvl);
837                 lvl = find_label_refs (XEXP (PATTERN (insn), 2), lvl);
838                 /* Record its tail recursion label, if any.  */
839                 if (XEXP (PATTERN (insn), 3) != NULL_RTX)
840                   trll = alloc_EXPR_LIST (0, XEXP (PATTERN (insn), 3), trll);
841               }
842
843             /* If there is an EH region or rethrow, we have an edge.  */
844             if ((eh_list && region > 0)
845                 || find_reg_note (insn, REG_EH_RETHROW, NULL_RTX))
846               call_has_abnormal_edge = 1;
847             else if (nonlocal_goto_handler_labels && region >= 0)
848               /* If there is a nonlocal goto label and the specified
849                  region number isn't -1, we have an edge. (0 means
850                  no throw, but might have a nonlocal goto).  */
851               call_has_abnormal_edge = 1;
852
853             /* A basic block ends at a call that can either throw or
854                do a non-local goto.  */
855             if (call_has_abnormal_edge)
856               {
857               new_bb_inclusive:
858                 if (head == NULL_RTX)
859                   head = insn;
860                 end = insn;
861
862               new_bb_exclusive:
863                 create_basic_block (i++, head, end, bb_note);
864                 head = end = NULL_RTX;
865                 bb_note = NULL_RTX;
866                 break;
867               }
868           }
869           /* Fall through.  */
870
871         default:
872           if (GET_RTX_CLASS (code) == 'i')
873             {
874               if (head == NULL_RTX)
875                 head = insn;
876               end = insn;
877             }
878           break;
879         }
880
881       if (GET_RTX_CLASS (code) == 'i'
882           && GET_CODE (insn) != JUMP_INSN)
883         {
884           rtx note;
885
886           /* Make a list of all labels referred to other than by jumps.
887
888              Make a special exception for labels followed by an ADDR*VEC,
889              as this would be a part of the tablejump setup code.
890
891              Make a special exception for the eh_return_stub_label, which
892              we know isn't part of any otherwise visible control flow.
893
894              Make a special exception to registers loaded with label
895              values just before jump insns that use them.  */
896
897           for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
898             if (REG_NOTE_KIND (note) == REG_LABEL)
899               {
900                 rtx lab = XEXP (note, 0), next;
901
902                 if (lab == eh_return_stub_label)
903                   ;
904                 else if ((next = next_nonnote_insn (lab)) != NULL
905                          && GET_CODE (next) == JUMP_INSN
906                          && (GET_CODE (PATTERN (next)) == ADDR_VEC
907                              || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC))
908                   ;
909                 else if (GET_CODE (lab) == NOTE)
910                   ;
911                 else if (GET_CODE (NEXT_INSN (insn)) == JUMP_INSN
912                          && find_reg_note (NEXT_INSN (insn), REG_LABEL, lab))
913                   ;
914                 else
915                   lvl = alloc_EXPR_LIST (0, XEXP (note, 0), lvl);
916               }
917         }
918     }
919
920   if (head != NULL_RTX)
921     create_basic_block (i++, head, end, bb_note);
922   else if (bb_note)
923     flow_delete_insn (bb_note);
924
925   if (i != n_basic_blocks)
926     abort ();
927
928   label_value_list = lvl;
929   tail_recursion_label_list = trll;
930 }
931
932 /* Tidy the CFG by deleting unreachable code and whatnot.  */
933
934 void
935 cleanup_cfg (f)
936      rtx f;
937 {
938   delete_unreachable_blocks ();
939   move_stray_eh_region_notes ();
940   record_active_eh_regions (f);
941   try_merge_blocks ();
942   mark_critical_edges ();
943
944   /* Kill the data we won't maintain.  */
945   free_EXPR_LIST_list (&label_value_list);
946   free_EXPR_LIST_list (&tail_recursion_label_list);
947 }
948
949 /* Create a new basic block consisting of the instructions between
950    HEAD and END inclusive.  Reuses the note and basic block struct
951    in BB_NOTE, if any.  */
952
953 void
954 create_basic_block (index, head, end, bb_note)
955      int index;
956      rtx head, end, bb_note;
957 {
958   basic_block bb;
959
960   if (bb_note
961       && ! RTX_INTEGRATED_P (bb_note)
962       && (bb = NOTE_BASIC_BLOCK (bb_note)) != NULL
963       && bb->aux == NULL)
964     {
965       /* If we found an existing note, thread it back onto the chain.  */
966
967       rtx after;
968
969       if (GET_CODE (head) == CODE_LABEL)
970         after = head;
971       else
972         {
973           after = PREV_INSN (head);
974           head = bb_note;
975         }
976
977       if (after != bb_note && NEXT_INSN (after) != bb_note)
978         reorder_insns (bb_note, bb_note, after);
979     }
980   else
981     {
982       /* Otherwise we must create a note and a basic block structure.
983          Since we allow basic block structs in rtl, give the struct
984          the same lifetime by allocating it off the function obstack
985          rather than using malloc.  */
986
987       bb = (basic_block) obstack_alloc (&flow_obstack, sizeof (*bb));
988       memset (bb, 0, sizeof (*bb));
989
990       if (GET_CODE (head) == CODE_LABEL)
991         bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK, head);
992       else
993         {
994           bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, head);
995           head = bb_note;
996         }
997       NOTE_BASIC_BLOCK (bb_note) = bb;
998     }
999
1000   /* Always include the bb note in the block.  */
1001   if (NEXT_INSN (end) == bb_note)
1002     end = bb_note;
1003
1004   bb->head = head;
1005   bb->end = end;
1006   bb->index = index;
1007   BASIC_BLOCK (index) = bb;
1008
1009   /* Tag the block so that we know it has been used when considering
1010      other basic block notes.  */
1011   bb->aux = bb;
1012 }
1013 \f
1014 /* Records the basic block struct in BB_FOR_INSN, for every instruction
1015    indexed by INSN_UID.  MAX is the size of the array.  */
1016
1017 void
1018 compute_bb_for_insn (max)
1019      int max;
1020 {
1021   int i;
1022
1023   if (basic_block_for_insn)
1024     VARRAY_FREE (basic_block_for_insn);
1025   VARRAY_BB_INIT (basic_block_for_insn, max, "basic_block_for_insn");
1026
1027   for (i = 0; i < n_basic_blocks; ++i)
1028     {
1029       basic_block bb = BASIC_BLOCK (i);
1030       rtx insn, end;
1031
1032       end = bb->end;
1033       insn = bb->head;
1034       while (1)
1035         {
1036           int uid = INSN_UID (insn);
1037           if (uid < max)
1038             VARRAY_BB (basic_block_for_insn, uid) = bb;
1039           if (insn == end)
1040             break;
1041           insn = NEXT_INSN (insn);
1042         }
1043     }
1044 }
1045
1046 /* Free the memory associated with the edge structures.  */
1047
1048 static void
1049 clear_edges ()
1050 {
1051   int i;
1052   edge n, e;
1053
1054   for (i = 0; i < n_basic_blocks; ++i)
1055     {
1056       basic_block bb = BASIC_BLOCK (i);
1057
1058       for (e = bb->succ; e; e = n)
1059         {
1060           n = e->succ_next;
1061           free (e);
1062         }
1063
1064       bb->succ = 0;
1065       bb->pred = 0;
1066     }
1067
1068   for (e = ENTRY_BLOCK_PTR->succ; e; e = n)
1069     {
1070       n = e->succ_next;
1071       free (e);
1072     }
1073
1074   ENTRY_BLOCK_PTR->succ = 0;
1075   EXIT_BLOCK_PTR->pred = 0;
1076
1077   n_edges = 0;
1078 }
1079
1080 /* Identify the edges between basic blocks.
1081
1082    NONLOCAL_LABEL_LIST is a list of non-local labels in the function.  Blocks
1083    that are otherwise unreachable may be reachable with a non-local goto.
1084
1085    BB_EH_END is an array indexed by basic block number in which we record
1086    the list of exception regions active at the end of the basic block.  */
1087
1088 static void
1089 make_edges (label_value_list)
1090      rtx label_value_list;
1091 {
1092   int i;
1093   eh_nesting_info *eh_nest_info = init_eh_nesting_info ();
1094   sbitmap *edge_cache = NULL;
1095
1096   /* Assume no computed jump; revise as we create edges.  */
1097   current_function_has_computed_jump = 0;
1098
1099   /* Heavy use of computed goto in machine-generated code can lead to
1100      nearly fully-connected CFGs.  In that case we spend a significant
1101      amount of time searching the edge lists for duplicates.  */
1102   if (forced_labels || label_value_list)
1103     {
1104       edge_cache = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
1105       sbitmap_vector_zero (edge_cache, n_basic_blocks);
1106     }
1107
1108   /* By nature of the way these get numbered, block 0 is always the entry.  */
1109   make_edge (edge_cache, ENTRY_BLOCK_PTR, BASIC_BLOCK (0), EDGE_FALLTHRU);
1110
1111   for (i = 0; i < n_basic_blocks; ++i)
1112     {
1113       basic_block bb = BASIC_BLOCK (i);
1114       rtx insn, x;
1115       enum rtx_code code;
1116       int force_fallthru = 0;
1117
1118       /* Examine the last instruction of the block, and discover the
1119          ways we can leave the block.  */
1120
1121       insn = bb->end;
1122       code = GET_CODE (insn);
1123
1124       /* A branch.  */
1125       if (code == JUMP_INSN)
1126         {
1127           rtx tmp;
1128
1129           /* Recognize a non-local goto as a branch outside the
1130              current function.  */
1131           if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
1132             ;
1133
1134           /* ??? Recognize a tablejump and do the right thing.  */
1135           else if ((tmp = JUMP_LABEL (insn)) != NULL_RTX
1136                    && (tmp = NEXT_INSN (tmp)) != NULL_RTX
1137                    && GET_CODE (tmp) == JUMP_INSN
1138                    && (GET_CODE (PATTERN (tmp)) == ADDR_VEC
1139                        || GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC))
1140             {
1141               rtvec vec;
1142               int j;
1143
1144               if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
1145                 vec = XVEC (PATTERN (tmp), 0);
1146               else
1147                 vec = XVEC (PATTERN (tmp), 1);
1148
1149               for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
1150                 make_label_edge (edge_cache, bb,
1151                                  XEXP (RTVEC_ELT (vec, j), 0), 0);
1152
1153               /* Some targets (eg, ARM) emit a conditional jump that also
1154                  contains the out-of-range target.  Scan for these and
1155                  add an edge if necessary.  */
1156               if ((tmp = single_set (insn)) != NULL
1157                   && SET_DEST (tmp) == pc_rtx
1158                   && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
1159                   && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
1160                 make_label_edge (edge_cache, bb,
1161                                  XEXP (XEXP (SET_SRC (tmp), 2), 0), 0);
1162
1163 #ifdef CASE_DROPS_THROUGH
1164               /* Silly VAXen.  The ADDR_VEC is going to be in the way of
1165                  us naturally detecting fallthru into the next block.  */
1166               force_fallthru = 1;
1167 #endif
1168             }
1169
1170           /* If this is a computed jump, then mark it as reaching
1171              everything on the label_value_list and forced_labels list.  */
1172           else if (computed_jump_p (insn))
1173             {
1174               current_function_has_computed_jump = 1;
1175
1176               for (x = label_value_list; x; x = XEXP (x, 1))
1177                 make_label_edge (edge_cache, bb, XEXP (x, 0), EDGE_ABNORMAL);
1178
1179               for (x = forced_labels; x; x = XEXP (x, 1))
1180                 make_label_edge (edge_cache, bb, XEXP (x, 0), EDGE_ABNORMAL);
1181             }
1182
1183           /* Returns create an exit out.  */
1184           else if (returnjump_p (insn))
1185             make_edge (edge_cache, bb, EXIT_BLOCK_PTR, 0);
1186
1187           /* Otherwise, we have a plain conditional or unconditional jump.  */
1188           else
1189             {
1190               if (! JUMP_LABEL (insn))
1191                 abort ();
1192               make_label_edge (edge_cache, bb, JUMP_LABEL (insn), 0);
1193             }
1194         }
1195
1196       /* If this is a sibling call insn, then this is in effect a
1197          combined call and return, and so we need an edge to the
1198          exit block.  No need to worry about EH edges, since we
1199          wouldn't have created the sibling call in the first place.  */
1200
1201       if (code == CALL_INSN && SIBLING_CALL_P (insn))
1202         make_edge (edge_cache, bb, EXIT_BLOCK_PTR,
1203                    EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
1204
1205       /* If this is a CALL_INSN, then mark it as reaching the active EH
1206          handler for this CALL_INSN.  If we're handling asynchronous
1207          exceptions then any insn can reach any of the active handlers.
1208
1209          Also mark the CALL_INSN as reaching any nonlocal goto handler.  */
1210
1211       else if (code == CALL_INSN || asynchronous_exceptions)
1212         {
1213           /* Add any appropriate EH edges.  We do this unconditionally
1214              since there may be a REG_EH_REGION or REG_EH_RETHROW note
1215              on the call, and this needn't be within an EH region.  */
1216           make_eh_edge (edge_cache, eh_nest_info, bb, insn, bb->eh_end);
1217
1218           /* If we have asynchronous exceptions, do the same for *all*
1219              exception regions active in the block.  */
1220           if (asynchronous_exceptions
1221               && bb->eh_beg != bb->eh_end)
1222             {
1223               if (bb->eh_beg >= 0)
1224                 make_eh_edge (edge_cache, eh_nest_info, bb,
1225                               NULL_RTX, bb->eh_beg);
1226
1227               for (x = bb->head; x != bb->end; x = NEXT_INSN (x))
1228                 if (GET_CODE (x) == NOTE
1229                     && (NOTE_LINE_NUMBER (x) == NOTE_INSN_EH_REGION_BEG
1230                         || NOTE_LINE_NUMBER (x) == NOTE_INSN_EH_REGION_END))
1231                   {
1232                     int region = NOTE_EH_HANDLER (x);
1233                     make_eh_edge (edge_cache, eh_nest_info, bb,
1234                                   NULL_RTX, region);
1235                   }
1236             }
1237
1238           if (code == CALL_INSN && nonlocal_goto_handler_labels)
1239             {
1240               /* ??? This could be made smarter: in some cases it's possible
1241                  to tell that certain calls will not do a nonlocal goto.
1242
1243                  For example, if the nested functions that do the nonlocal
1244                  gotos do not have their addresses taken, then only calls to
1245                  those functions or to other nested functions that use them
1246                  could possibly do nonlocal gotos.  */
1247               /* We do know that a REG_EH_REGION note with a value less
1248                  than 0 is guaranteed not to perform a non-local goto.  */
1249               rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1250               if (!note || INTVAL (XEXP (note, 0)) >=  0)
1251                 for (x = nonlocal_goto_handler_labels; x; x = XEXP (x, 1))
1252                   make_label_edge (edge_cache, bb, XEXP (x, 0),
1253                                    EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
1254             }
1255         }
1256
1257       /* We know something about the structure of the function __throw in
1258          libgcc2.c.  It is the only function that ever contains eh_stub
1259          labels.  It modifies its return address so that the last block
1260          returns to one of the eh_stub labels within it.  So we have to
1261          make additional edges in the flow graph.  */
1262       if (i + 1 == n_basic_blocks && eh_return_stub_label != 0)
1263         make_label_edge (edge_cache, bb, eh_return_stub_label, EDGE_EH);
1264
1265       /* Find out if we can drop through to the next block.  */
1266       insn = next_nonnote_insn (insn);
1267       if (!insn || (i + 1 == n_basic_blocks && force_fallthru))
1268         make_edge (edge_cache, bb, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
1269       else if (i + 1 < n_basic_blocks)
1270         {
1271           rtx tmp = BLOCK_HEAD (i + 1);
1272           if (GET_CODE (tmp) == NOTE)
1273             tmp = next_nonnote_insn (tmp);
1274           if (force_fallthru || insn == tmp)
1275             make_edge (edge_cache, bb, BASIC_BLOCK (i + 1), EDGE_FALLTHRU);
1276         }
1277     }
1278
1279   free_eh_nesting_info (eh_nest_info);
1280   if (edge_cache)
1281     sbitmap_vector_free (edge_cache);
1282 }
1283
1284 /* Create an edge between two basic blocks.  FLAGS are auxiliary information
1285    about the edge that is accumulated between calls.  */
1286
1287 void
1288 make_edge (edge_cache, src, dst, flags)
1289      sbitmap *edge_cache;
1290      basic_block src, dst;
1291      int flags;
1292 {
1293   int use_edge_cache;
1294   edge e;
1295
1296   /* Don't bother with edge cache for ENTRY or EXIT; there aren't that
1297      many edges to them, and we didn't allocate memory for it.  */
1298   use_edge_cache = (edge_cache
1299                     && src != ENTRY_BLOCK_PTR
1300                     && dst != EXIT_BLOCK_PTR);
1301
1302   /* Make sure we don't add duplicate edges.  */
1303   switch (use_edge_cache)
1304     {
1305     default:
1306       /* Quick test for non-existance of the edge.  */
1307       if (! TEST_BIT (edge_cache[src->index], dst->index))
1308         break;
1309
1310       /* The edge exists; early exit if no work to do.  */
1311       if (flags == 0)
1312         return;
1313
1314       /* FALLTHRU */
1315     case 0:
1316       for (e = src->succ; e; e = e->succ_next)
1317         if (e->dest == dst)
1318           {
1319             e->flags |= flags;
1320             return;
1321           }
1322       break;
1323     }
1324
1325   e = (edge) xcalloc (1, sizeof (*e));
1326   n_edges++;
1327
1328   e->succ_next = src->succ;
1329   e->pred_next = dst->pred;
1330   e->src = src;
1331   e->dest = dst;
1332   e->flags = flags;
1333
1334   src->succ = e;
1335   dst->pred = e;
1336
1337   if (use_edge_cache)
1338     SET_BIT (edge_cache[src->index], dst->index);
1339 }
1340
1341 /* Create an edge from a basic block to a label.  */
1342
1343 static void
1344 make_label_edge (edge_cache, src, label, flags)
1345      sbitmap *edge_cache;
1346      basic_block src;
1347      rtx label;
1348      int flags;
1349 {
1350   if (GET_CODE (label) != CODE_LABEL)
1351     abort ();
1352
1353   /* If the label was never emitted, this insn is junk, but avoid a
1354      crash trying to refer to BLOCK_FOR_INSN (label).  This can happen
1355      as a result of a syntax error and a diagnostic has already been
1356      printed.  */
1357
1358   if (INSN_UID (label) == 0)
1359     return;
1360
1361   make_edge (edge_cache, src, BLOCK_FOR_INSN (label), flags);
1362 }
1363
1364 /* Create the edges generated by INSN in REGION.  */
1365
1366 static void
1367 make_eh_edge (edge_cache, eh_nest_info, src, insn, region)
1368      sbitmap *edge_cache;
1369      eh_nesting_info *eh_nest_info;
1370      basic_block src;
1371      rtx insn;
1372      int region;
1373 {
1374   handler_info **handler_list;
1375   int num, is_call;
1376
1377   is_call = (insn && GET_CODE (insn) == CALL_INSN ? EDGE_ABNORMAL_CALL : 0);
1378   num = reachable_handlers (region, eh_nest_info, insn, &handler_list);
1379   while (--num >= 0)
1380     {
1381       make_label_edge (edge_cache, src, handler_list[num]->handler_label,
1382                        EDGE_ABNORMAL | EDGE_EH | is_call);
1383     }
1384 }
1385
1386 /* EH_REGION notes appearing between basic blocks is ambiguous, and even
1387    dangerous if we intend to move basic blocks around.  Move such notes
1388    into the following block.  */
1389
1390 static void
1391 move_stray_eh_region_notes ()
1392 {
1393   int i;
1394   basic_block b1, b2;
1395
1396   if (n_basic_blocks < 2)
1397     return;
1398
1399   b2 = BASIC_BLOCK (n_basic_blocks - 1);
1400   for (i = n_basic_blocks - 2; i >= 0; --i, b2 = b1)
1401     {
1402       rtx insn, next, list = NULL_RTX;
1403
1404       b1 = BASIC_BLOCK (i);
1405       for (insn = NEXT_INSN (b1->end); insn != b2->head; insn = next)
1406         {
1407           next = NEXT_INSN (insn);
1408           if (GET_CODE (insn) == NOTE
1409               && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG
1410                   || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END))
1411             {
1412               /* Unlink from the insn chain.  */
1413               NEXT_INSN (PREV_INSN (insn)) = next;
1414               PREV_INSN (next) = PREV_INSN (insn);
1415
1416               /* Queue it.  */
1417               NEXT_INSN (insn) = list;
1418               list = insn;
1419             }
1420         }
1421
1422       if (list == NULL_RTX)
1423         continue;
1424
1425       /* Find where to insert these things.  */
1426       insn = b2->head;
1427       if (GET_CODE (insn) == CODE_LABEL)
1428         insn = NEXT_INSN (insn);
1429
1430       while (list)
1431         {
1432           next = NEXT_INSN (list);
1433           add_insn_after (list, insn);
1434           list = next;
1435         }
1436     }
1437 }
1438
1439 /* Recompute eh_beg/eh_end for each basic block.  */
1440
1441 static void
1442 record_active_eh_regions (f)
1443      rtx f;
1444 {
1445   rtx insn, eh_list = NULL_RTX;
1446   int i = 0;
1447   basic_block bb = BASIC_BLOCK (0);
1448
1449   for (insn = f; insn; insn = NEXT_INSN (insn))
1450     {
1451       if (bb->head == insn)
1452         bb->eh_beg = (eh_list ? NOTE_EH_HANDLER (XEXP (eh_list, 0)) : -1);
1453
1454       if (GET_CODE (insn) == NOTE)
1455         {
1456           int kind = NOTE_LINE_NUMBER (insn);
1457           if (kind == NOTE_INSN_EH_REGION_BEG)
1458             eh_list = alloc_INSN_LIST (insn, eh_list);
1459           else if (kind == NOTE_INSN_EH_REGION_END)
1460             {
1461               rtx t = XEXP (eh_list, 1);
1462               free_INSN_LIST_node (eh_list);
1463               eh_list = t;
1464             }
1465         }
1466
1467       if (bb->end == insn)
1468         {
1469           bb->eh_end = (eh_list ? NOTE_EH_HANDLER (XEXP (eh_list, 0)) : -1);
1470           i += 1;
1471           if (i == n_basic_blocks)
1472             break;
1473           bb = BASIC_BLOCK (i);
1474         }
1475     }
1476 }
1477
1478 /* Identify critical edges and set the bits appropriately.  */
1479
1480 static void
1481 mark_critical_edges ()
1482 {
1483   int i, n = n_basic_blocks;
1484   basic_block bb;
1485
1486   /* We begin with the entry block.  This is not terribly important now,
1487      but could be if a front end (Fortran) implemented alternate entry
1488      points.  */
1489   bb = ENTRY_BLOCK_PTR;
1490   i = -1;
1491
1492   while (1)
1493     {
1494       edge e;
1495
1496       /* (1) Critical edges must have a source with multiple successors.  */
1497       if (bb->succ && bb->succ->succ_next)
1498         {
1499           for (e = bb->succ; e; e = e->succ_next)
1500             {
1501               /* (2) Critical edges must have a destination with multiple
1502                  predecessors.  Note that we know there is at least one
1503                  predecessor -- the edge we followed to get here.  */
1504               if (e->dest->pred->pred_next)
1505                 e->flags |= EDGE_CRITICAL;
1506               else
1507                 e->flags &= ~EDGE_CRITICAL;
1508             }
1509         }
1510       else
1511         {
1512           for (e = bb->succ; e; e = e->succ_next)
1513             e->flags &= ~EDGE_CRITICAL;
1514         }
1515
1516       if (++i >= n)
1517         break;
1518       bb = BASIC_BLOCK (i);
1519     }
1520 }
1521 \f
1522 /* Split a block BB after insn INSN creating a new fallthru edge.
1523    Return the new edge.  Note that to keep other parts of the compiler happy,
1524    this function renumbers all the basic blocks so that the new
1525    one has a number one greater than the block split.  */
1526
1527 edge
1528 split_block (bb, insn)
1529      basic_block bb;
1530      rtx insn;
1531 {
1532   basic_block new_bb;
1533   edge new_edge;
1534   edge e;
1535   rtx bb_note;
1536   int i, j;
1537
1538   /* There is no point splitting the block after its end.  */
1539   if (bb->end == insn)
1540     return 0;
1541
1542   /* Create the new structures.  */
1543   new_bb = (basic_block) obstack_alloc (&flow_obstack, sizeof (*new_bb));
1544   new_edge = (edge) xcalloc (1, sizeof (*new_edge));
1545   n_edges++;
1546
1547   memset (new_bb, 0, sizeof (*new_bb));
1548
1549   new_bb->head = NEXT_INSN (insn);
1550   new_bb->end = bb->end;
1551   bb->end = insn;
1552
1553   new_bb->succ = bb->succ;
1554   bb->succ = new_edge;
1555   new_bb->pred = new_edge;
1556   new_bb->count = bb->count;
1557   new_bb->loop_depth = bb->loop_depth;
1558
1559   new_edge->src = bb;
1560   new_edge->dest = new_bb;
1561   new_edge->flags = EDGE_FALLTHRU;
1562   new_edge->probability = REG_BR_PROB_BASE;
1563   new_edge->count = bb->count;
1564
1565   /* Redirect the src of the successor edges of bb to point to new_bb.  */
1566   for (e = new_bb->succ; e; e = e->succ_next)
1567     e->src = new_bb;
1568
1569   /* Place the new block just after the block being split.  */
1570   VARRAY_GROW (basic_block_info, ++n_basic_blocks);
1571
1572   /* Some parts of the compiler expect blocks to be number in
1573      sequential order so insert the new block immediately after the
1574      block being split..  */
1575   j = bb->index;
1576   for (i = n_basic_blocks - 1; i > j + 1; --i)
1577     {
1578       basic_block tmp = BASIC_BLOCK (i - 1);
1579       BASIC_BLOCK (i) = tmp;
1580       tmp->index = i;
1581     }
1582
1583   BASIC_BLOCK (i) = new_bb;
1584   new_bb->index = i;
1585
1586   /* Create the basic block note.  */
1587   bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK,
1588                               new_bb->head);
1589   NOTE_BASIC_BLOCK (bb_note) = new_bb;
1590   new_bb->head = bb_note;
1591
1592   update_bb_for_insn (new_bb);
1593
1594   if (bb->global_live_at_start)
1595     {
1596       new_bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1597       new_bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1598       COPY_REG_SET (new_bb->global_live_at_end, bb->global_live_at_end);
1599
1600       /* We now have to calculate which registers are live at the end
1601          of the split basic block and at the start of the new basic
1602          block.  Start with those registers that are known to be live
1603          at the end of the original basic block and get
1604          propagate_block to determine which registers are live.  */
1605       COPY_REG_SET (new_bb->global_live_at_start, bb->global_live_at_end);
1606       propagate_block (new_bb, new_bb->global_live_at_start, NULL, NULL, 0);
1607       COPY_REG_SET (bb->global_live_at_end,
1608                     new_bb->global_live_at_start);
1609     }
1610
1611   return new_edge;
1612 }
1613
1614
1615 /* Split a (typically critical) edge.  Return the new block.
1616    Abort on abnormal edges.
1617
1618    ??? The code generally expects to be called on critical edges.
1619    The case of a block ending in an unconditional jump to a
1620    block with multiple predecessors is not handled optimally.  */
1621
1622 basic_block
1623 split_edge (edge_in)
1624      edge edge_in;
1625 {
1626   basic_block old_pred, bb, old_succ;
1627   edge edge_out;
1628   rtx bb_note;
1629   int i, j;
1630
1631   /* Abnormal edges cannot be split.  */
1632   if ((edge_in->flags & EDGE_ABNORMAL) != 0)
1633     abort ();
1634
1635   old_pred = edge_in->src;
1636   old_succ = edge_in->dest;
1637
1638   /* Remove the existing edge from the destination's pred list.  */
1639   {
1640     edge *pp;
1641     for (pp = &old_succ->pred; *pp != edge_in; pp = &(*pp)->pred_next)
1642       continue;
1643     *pp = edge_in->pred_next;
1644     edge_in->pred_next = NULL;
1645   }
1646
1647   /* Create the new structures.  */
1648   bb = (basic_block) obstack_alloc (&flow_obstack, sizeof (*bb));
1649   edge_out = (edge) xcalloc (1, sizeof (*edge_out));
1650   n_edges++;
1651
1652   memset (bb, 0, sizeof (*bb));
1653
1654   /* ??? This info is likely going to be out of date very soon.  */
1655   if (old_succ->global_live_at_start)
1656     {
1657       bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1658       bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1659       COPY_REG_SET (bb->global_live_at_start, old_succ->global_live_at_start);
1660       COPY_REG_SET (bb->global_live_at_end, old_succ->global_live_at_start);
1661     }
1662
1663   /* Wire them up.  */
1664   bb->pred = edge_in;
1665   bb->succ = edge_out;
1666   bb->count = edge_in->count;
1667
1668   edge_in->dest = bb;
1669   edge_in->flags &= ~EDGE_CRITICAL;
1670
1671   edge_out->pred_next = old_succ->pred;
1672   edge_out->succ_next = NULL;
1673   edge_out->src = bb;
1674   edge_out->dest = old_succ;
1675   edge_out->flags = EDGE_FALLTHRU;
1676   edge_out->probability = REG_BR_PROB_BASE;
1677   edge_out->count = edge_in->count;
1678
1679   old_succ->pred = edge_out;
1680
1681   /* Tricky case -- if there existed a fallthru into the successor
1682      (and we're not it) we must add a new unconditional jump around
1683      the new block we're actually interested in.
1684
1685      Further, if that edge is critical, this means a second new basic
1686      block must be created to hold it.  In order to simplify correct
1687      insn placement, do this before we touch the existing basic block
1688      ordering for the block we were really wanting.  */
1689   if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1690     {
1691       edge e;
1692       for (e = edge_out->pred_next; e; e = e->pred_next)
1693         if (e->flags & EDGE_FALLTHRU)
1694           break;
1695
1696       if (e)
1697         {
1698           basic_block jump_block;
1699           rtx pos;
1700
1701           if ((e->flags & EDGE_CRITICAL) == 0
1702               && e->src != ENTRY_BLOCK_PTR)
1703             {
1704               /* Non critical -- we can simply add a jump to the end
1705                  of the existing predecessor.  */
1706               jump_block = e->src;
1707             }
1708           else
1709             {
1710               /* We need a new block to hold the jump.  The simplest
1711                  way to do the bulk of the work here is to recursively
1712                  call ourselves.  */
1713               jump_block = split_edge (e);
1714               e = jump_block->succ;
1715             }
1716
1717           /* Now add the jump insn ...  */
1718           pos = emit_jump_insn_after (gen_jump (old_succ->head),
1719                                       jump_block->end);
1720           jump_block->end = pos;
1721           if (basic_block_for_insn)
1722             set_block_for_insn (pos, jump_block);
1723           emit_barrier_after (pos);
1724
1725           /* ... let jump know that label is in use, ...  */
1726           JUMP_LABEL (pos) = old_succ->head;
1727           ++LABEL_NUSES (old_succ->head);
1728
1729           /* ... and clear fallthru on the outgoing edge.  */
1730           e->flags &= ~EDGE_FALLTHRU;
1731
1732           /* Continue splitting the interesting edge.  */
1733         }
1734     }
1735
1736   /* Place the new block just in front of the successor.  */
1737   VARRAY_GROW (basic_block_info, ++n_basic_blocks);
1738   if (old_succ == EXIT_BLOCK_PTR)
1739     j = n_basic_blocks - 1;
1740   else
1741     j = old_succ->index;
1742   for (i = n_basic_blocks - 1; i > j; --i)
1743     {
1744       basic_block tmp = BASIC_BLOCK (i - 1);
1745       BASIC_BLOCK (i) = tmp;
1746       tmp->index = i;
1747     }
1748   BASIC_BLOCK (i) = bb;
1749   bb->index = i;
1750
1751   /* Create the basic block note.
1752
1753      Where we place the note can have a noticable impact on the generated
1754      code.  Consider this cfg:
1755
1756                         E
1757                         |
1758                         0
1759                        / \
1760                    +->1-->2--->E
1761                    |  |
1762                    +--+
1763
1764       If we need to insert an insn on the edge from block 0 to block 1,
1765       we want to ensure the instructions we insert are outside of any
1766       loop notes that physically sit between block 0 and block 1.  Otherwise
1767       we confuse the loop optimizer into thinking the loop is a phony.  */
1768   if (old_succ != EXIT_BLOCK_PTR
1769       && PREV_INSN (old_succ->head)
1770       && GET_CODE (PREV_INSN (old_succ->head)) == NOTE
1771       && NOTE_LINE_NUMBER (PREV_INSN (old_succ->head)) == NOTE_INSN_LOOP_BEG)
1772     bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK,
1773                                 PREV_INSN (old_succ->head));
1774   else if (old_succ != EXIT_BLOCK_PTR)
1775     bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, old_succ->head);
1776   else
1777     bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK, get_last_insn ());
1778   NOTE_BASIC_BLOCK (bb_note) = bb;
1779   bb->head = bb->end = bb_note;
1780
1781   /* Not quite simple -- for non-fallthru edges, we must adjust the
1782      predecessor's jump instruction to target our new block.  */
1783   if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1784     {
1785       rtx tmp, insn = old_pred->end;
1786       rtx old_label = old_succ->head;
1787       rtx new_label = gen_label_rtx ();
1788
1789       if (GET_CODE (insn) != JUMP_INSN)
1790         abort ();
1791
1792       /* ??? Recognize a tablejump and adjust all matching cases.  */
1793       if ((tmp = JUMP_LABEL (insn)) != NULL_RTX
1794           && (tmp = NEXT_INSN (tmp)) != NULL_RTX
1795           && GET_CODE (tmp) == JUMP_INSN
1796           && (GET_CODE (PATTERN (tmp)) == ADDR_VEC
1797               || GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC))
1798         {
1799           rtvec vec;
1800           int j;
1801
1802           if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
1803             vec = XVEC (PATTERN (tmp), 0);
1804           else
1805             vec = XVEC (PATTERN (tmp), 1);
1806
1807           for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
1808             if (XEXP (RTVEC_ELT (vec, j), 0) == old_label)
1809               {
1810                 RTVEC_ELT (vec, j) = gen_rtx_LABEL_REF (VOIDmode, new_label);
1811                 --LABEL_NUSES (old_label);
1812                 ++LABEL_NUSES (new_label);
1813               }
1814
1815           /* Handle casesi dispatch insns */
1816           if ((tmp = single_set (insn)) != NULL
1817               && SET_DEST (tmp) == pc_rtx
1818               && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
1819               && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF
1820               && XEXP (XEXP (SET_SRC (tmp), 2), 0) == old_label)
1821             {
1822               XEXP (SET_SRC (tmp), 2) = gen_rtx_LABEL_REF (VOIDmode,
1823                                                            new_label);
1824               --LABEL_NUSES (old_label);
1825               ++LABEL_NUSES (new_label);
1826             }
1827         }
1828       else
1829         {
1830           /* This would have indicated an abnormal edge.  */
1831           if (computed_jump_p (insn))
1832             abort ();
1833
1834           /* A return instruction can't be redirected.  */
1835           if (returnjump_p (insn))
1836             abort ();
1837
1838           /* If the insn doesn't go where we think, we're confused.  */
1839           if (JUMP_LABEL (insn) != old_label)
1840             abort ();
1841
1842           redirect_jump (insn, new_label, 0);
1843         }
1844
1845       emit_label_before (new_label, bb_note);
1846       bb->head = new_label;
1847     }
1848
1849   return bb;
1850 }
1851
1852 /* Queue instructions for insertion on an edge between two basic blocks.
1853    The new instructions and basic blocks (if any) will not appear in the
1854    CFG until commit_edge_insertions is called.  */
1855
1856 void
1857 insert_insn_on_edge (pattern, e)
1858      rtx pattern;
1859      edge e;
1860 {
1861   /* We cannot insert instructions on an abnormal critical edge.
1862      It will be easier to find the culprit if we die now.  */
1863   if ((e->flags & (EDGE_ABNORMAL|EDGE_CRITICAL))
1864       == (EDGE_ABNORMAL|EDGE_CRITICAL))
1865     abort ();
1866
1867   if (e->insns == NULL_RTX)
1868     start_sequence ();
1869   else
1870     push_to_sequence (e->insns);
1871
1872   emit_insn (pattern);
1873
1874   e->insns = get_insns ();
1875   end_sequence ();
1876 }
1877
1878 /* Update the CFG for the instructions queued on edge E.  */
1879
1880 static void
1881 commit_one_edge_insertion (e)
1882      edge e;
1883 {
1884   rtx before = NULL_RTX, after = NULL_RTX, insns, tmp, last;
1885   basic_block bb;
1886
1887   /* Pull the insns off the edge now since the edge might go away.  */
1888   insns = e->insns;
1889   e->insns = NULL_RTX;
1890
1891   /* Figure out where to put these things.  If the destination has
1892      one predecessor, insert there.  Except for the exit block.  */
1893   if (e->dest->pred->pred_next == NULL
1894       && e->dest != EXIT_BLOCK_PTR)
1895     {
1896       bb = e->dest;
1897
1898       /* Get the location correct wrt a code label, and "nice" wrt
1899          a basic block note, and before everything else.  */
1900       tmp = bb->head;
1901       if (GET_CODE (tmp) == CODE_LABEL)
1902         tmp = NEXT_INSN (tmp);
1903       if (NOTE_INSN_BASIC_BLOCK_P (tmp))
1904         tmp = NEXT_INSN (tmp);
1905       if (tmp == bb->head)
1906         before = tmp;
1907       else
1908         after = PREV_INSN (tmp);
1909     }
1910
1911   /* If the source has one successor and the edge is not abnormal,
1912      insert there.  Except for the entry block.  */
1913   else if ((e->flags & EDGE_ABNORMAL) == 0
1914            && e->src->succ->succ_next == NULL
1915            && e->src != ENTRY_BLOCK_PTR)
1916     {
1917       bb = e->src;
1918       /* It is possible to have a non-simple jump here.  Consider a target
1919          where some forms of unconditional jumps clobber a register.  This
1920          happens on the fr30 for example.
1921
1922          We know this block has a single successor, so we can just emit
1923          the queued insns before the jump.  */
1924       if (GET_CODE (bb->end) == JUMP_INSN)
1925         {
1926           before = bb->end;
1927         }
1928       else
1929         {
1930           /* We'd better be fallthru, or we've lost track of what's what.  */
1931           if ((e->flags & EDGE_FALLTHRU) == 0)
1932             abort ();
1933
1934           after = bb->end;
1935         }
1936     }
1937
1938   /* Otherwise we must split the edge.  */
1939   else
1940     {
1941       bb = split_edge (e);
1942       after = bb->end;
1943     }
1944
1945   /* Now that we've found the spot, do the insertion.  */
1946
1947   /* Set the new block number for these insns, if structure is allocated.  */
1948   if (basic_block_for_insn)
1949     {
1950       rtx i;
1951       for (i = insns; i != NULL_RTX; i = NEXT_INSN (i))
1952         set_block_for_insn (i, bb);
1953     }
1954
1955   if (before)
1956     {
1957       emit_insns_before (insns, before);
1958       if (before == bb->head)
1959         bb->head = insns;
1960
1961       last = prev_nonnote_insn (before);
1962     }
1963   else
1964     {
1965       last = emit_insns_after (insns, after);
1966       if (after == bb->end)
1967         bb->end = last;
1968     }
1969
1970   if (returnjump_p (last))
1971     {
1972       /* ??? Remove all outgoing edges from BB and add one for EXIT.
1973          This is not currently a problem because this only happens
1974          for the (single) epilogue, which already has a fallthru edge
1975          to EXIT.  */
1976
1977       e = bb->succ;
1978       if (e->dest != EXIT_BLOCK_PTR
1979           || e->succ_next != NULL
1980           || (e->flags & EDGE_FALLTHRU) == 0)
1981         abort ();
1982       e->flags &= ~EDGE_FALLTHRU;
1983
1984       emit_barrier_after (last);
1985       bb->end = last;
1986
1987       if (before)
1988         flow_delete_insn (before);
1989     }
1990   else if (GET_CODE (last) == JUMP_INSN)
1991     abort ();
1992 }
1993
1994 /* Update the CFG for all queued instructions.  */
1995
1996 void
1997 commit_edge_insertions ()
1998 {
1999   int i;
2000   basic_block bb;
2001
2002 #ifdef ENABLE_CHECKING
2003   verify_flow_info ();
2004 #endif
2005
2006   i = -1;
2007   bb = ENTRY_BLOCK_PTR;
2008   while (1)
2009     {
2010       edge e, next;
2011
2012       for (e = bb->succ; e; e = next)
2013         {
2014           next = e->succ_next;
2015           if (e->insns)
2016             commit_one_edge_insertion (e);
2017         }
2018
2019       if (++i >= n_basic_blocks)
2020         break;
2021       bb = BASIC_BLOCK (i);
2022     }
2023 }
2024
2025 /* Add fake edges to the function exit for any non constant calls in
2026    the bitmap of blocks specified by BLOCKS or to the whole CFG if
2027    BLOCKS is zero.  Return the nuber of blocks that were split.  */
2028
2029 int
2030 flow_call_edges_add (blocks)
2031      sbitmap blocks;
2032 {
2033   int i;
2034   int blocks_split = 0;
2035   int bb_num = 0;
2036   basic_block *bbs;
2037
2038   /* Map bb indicies into basic block pointers since split_block
2039      will renumber the basic blocks.  */
2040
2041   bbs = xmalloc (n_basic_blocks * sizeof (*bbs));
2042
2043   if (! blocks)
2044     {
2045       for (i = 0; i < n_basic_blocks; i++)
2046         bbs[bb_num++] = BASIC_BLOCK (i);
2047     }
2048   else
2049     {
2050       EXECUTE_IF_SET_IN_SBITMAP (blocks, 0, i, 
2051       {
2052         bbs[bb_num++] = BASIC_BLOCK (i);
2053       });
2054     }
2055
2056
2057   /* Now add fake edges to the function exit for any non constant
2058      calls since there is no way that we can determine if they will
2059      return or not...  */
2060
2061   for (i = 0; i < bb_num; i++)
2062     {
2063       basic_block bb = bbs[i];
2064       rtx insn;
2065       rtx prev_insn;
2066
2067       for (insn = bb->end; ; insn = prev_insn)
2068         {
2069           prev_insn = PREV_INSN (insn);
2070           if (GET_CODE (insn) == CALL_INSN && ! CONST_CALL_P (insn))
2071             {
2072               edge e;
2073
2074               /* Note that the following may create a new basic block
2075                  and renumber the existing basic blocks.  */
2076               e = split_block (bb, insn);
2077               if (e)
2078                 blocks_split++;
2079
2080               make_edge (NULL, bb, EXIT_BLOCK_PTR, EDGE_FAKE);
2081             }
2082           if (insn == bb->head)
2083             break;
2084         }
2085     }
2086
2087   if (blocks_split)
2088     verify_flow_info ();
2089
2090   free (bbs);
2091   return blocks_split;
2092 }
2093 \f
2094 /* Delete all unreachable basic blocks.   */
2095
2096 static void
2097 delete_unreachable_blocks ()
2098 {
2099   basic_block *worklist, *tos;
2100   int deleted_handler;
2101   edge e;
2102   int i, n;
2103
2104   n = n_basic_blocks;
2105   tos = worklist = (basic_block *) xmalloc (sizeof (basic_block) * n);
2106
2107   /* Use basic_block->aux as a marker.  Clear them all.  */
2108
2109   for (i = 0; i < n; ++i)
2110     BASIC_BLOCK (i)->aux = NULL;
2111
2112   /* Add our starting points to the worklist.  Almost always there will
2113      be only one.  It isn't inconcievable that we might one day directly
2114      support Fortran alternate entry points.  */
2115
2116   for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
2117     {
2118       *tos++ = e->dest;
2119
2120       /* Mark the block with a handy non-null value.  */
2121       e->dest->aux = e;
2122     }
2123
2124   /* Iterate: find everything reachable from what we've already seen.  */
2125
2126   while (tos != worklist)
2127     {
2128       basic_block b = *--tos;
2129
2130       for (e = b->succ; e; e = e->succ_next)
2131         if (!e->dest->aux)
2132           {
2133             *tos++ = e->dest;
2134             e->dest->aux = e;
2135           }
2136     }
2137
2138   /* Delete all unreachable basic blocks.  Count down so that we don't
2139      interfere with the block renumbering that happens in flow_delete_block.  */
2140
2141   deleted_handler = 0;
2142
2143   for (i = n - 1; i >= 0; --i)
2144     {
2145       basic_block b = BASIC_BLOCK (i);
2146
2147       if (b->aux != NULL)
2148         /* This block was found.  Tidy up the mark.  */
2149         b->aux = NULL;
2150       else
2151         deleted_handler |= flow_delete_block (b);
2152     }
2153
2154   tidy_fallthru_edges ();
2155
2156   /* If we deleted an exception handler, we may have EH region begin/end
2157      blocks to remove as well.  */
2158   if (deleted_handler)
2159     delete_eh_regions ();
2160
2161   free (worklist);
2162 }
2163
2164 /* Find EH regions for which there is no longer a handler, and delete them.  */
2165
2166 static void
2167 delete_eh_regions ()
2168 {
2169   rtx insn;
2170
2171   update_rethrow_references ();
2172
2173   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2174     if (GET_CODE (insn) == NOTE)
2175       {
2176         if ((NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG)
2177             || (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END))
2178           {
2179             int num = NOTE_EH_HANDLER (insn);
2180             /* A NULL handler indicates a region is no longer needed,
2181                as long as its rethrow label isn't used.  */
2182             if (get_first_handler (num) == NULL && ! rethrow_used (num))
2183               {
2184                 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2185                 NOTE_SOURCE_FILE (insn) = 0;
2186               }
2187           }
2188       }
2189 }
2190
2191 /* Return true if NOTE is not one of the ones that must be kept paired,
2192    so that we may simply delete them.  */
2193
2194 static int
2195 can_delete_note_p (note)
2196      rtx note;
2197 {
2198   return (NOTE_LINE_NUMBER (note) == NOTE_INSN_DELETED
2199           || NOTE_LINE_NUMBER (note) == NOTE_INSN_BASIC_BLOCK);
2200 }
2201
2202 /* Unlink a chain of insns between START and FINISH, leaving notes
2203    that must be paired.  */
2204
2205 void
2206 flow_delete_insn_chain (start, finish)
2207      rtx start, finish;
2208 {
2209   /* Unchain the insns one by one.  It would be quicker to delete all
2210      of these with a single unchaining, rather than one at a time, but
2211      we need to keep the NOTE's.  */
2212
2213   rtx next;
2214
2215   while (1)
2216     {
2217       next = NEXT_INSN (start);
2218       if (GET_CODE (start) == NOTE && !can_delete_note_p (start))
2219         ;
2220       else if (GET_CODE (start) == CODE_LABEL
2221                && ! can_delete_label_p (start))
2222         {
2223           const char *name = LABEL_NAME (start);
2224           PUT_CODE (start, NOTE);
2225           NOTE_LINE_NUMBER (start) = NOTE_INSN_DELETED_LABEL;
2226           NOTE_SOURCE_FILE (start) = name;
2227         }
2228       else
2229         next = flow_delete_insn (start);
2230
2231       if (start == finish)
2232         break;
2233       start = next;
2234     }
2235 }
2236
2237 /* Delete the insns in a (non-live) block.  We physically delete every
2238    non-deleted-note insn, and update the flow graph appropriately.
2239
2240    Return nonzero if we deleted an exception handler.  */
2241
2242 /* ??? Preserving all such notes strikes me as wrong.  It would be nice
2243    to post-process the stream to remove empty blocks, loops, ranges, etc.  */
2244
2245 int
2246 flow_delete_block (b)
2247      basic_block b;
2248 {
2249   int deleted_handler = 0;
2250   rtx insn, end, tmp;
2251
2252   /* If the head of this block is a CODE_LABEL, then it might be the
2253      label for an exception handler which can't be reached.
2254
2255      We need to remove the label from the exception_handler_label list
2256      and remove the associated NOTE_INSN_EH_REGION_BEG and
2257      NOTE_INSN_EH_REGION_END notes.  */
2258
2259   insn = b->head;
2260
2261   never_reached_warning (insn);
2262
2263   if (GET_CODE (insn) == CODE_LABEL)
2264     {
2265       rtx x, *prev = &exception_handler_labels;
2266
2267       for (x = exception_handler_labels; x; x = XEXP (x, 1))
2268         {
2269           if (XEXP (x, 0) == insn)
2270             {
2271               /* Found a match, splice this label out of the EH label list.  */
2272               *prev = XEXP (x, 1);
2273               XEXP (x, 1) = NULL_RTX;
2274               XEXP (x, 0) = NULL_RTX;
2275
2276               /* Remove the handler from all regions */
2277               remove_handler (insn);
2278               deleted_handler = 1;
2279               break;
2280             }
2281           prev = &XEXP (x, 1);
2282         }
2283     }
2284
2285   /* Include any jump table following the basic block.  */
2286   end = b->end;
2287   if (GET_CODE (end) == JUMP_INSN
2288       && (tmp = JUMP_LABEL (end)) != NULL_RTX
2289       && (tmp = NEXT_INSN (tmp)) != NULL_RTX
2290       && GET_CODE (tmp) == JUMP_INSN
2291       && (GET_CODE (PATTERN (tmp)) == ADDR_VEC
2292           || GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC))
2293     end = tmp;
2294
2295   /* Include any barrier that may follow the basic block.  */
2296   tmp = next_nonnote_insn (end);
2297   if (tmp && GET_CODE (tmp) == BARRIER)
2298     end = tmp;
2299
2300   /* Selectively delete the entire chain.  */
2301   flow_delete_insn_chain (insn, end);
2302
2303   /* Remove the edges into and out of this block.  Note that there may
2304      indeed be edges in, if we are removing an unreachable loop.  */
2305   {
2306     edge e, next, *q;
2307
2308     for (e = b->pred; e; e = next)
2309       {
2310         for (q = &e->src->succ; *q != e; q = &(*q)->succ_next)
2311           continue;
2312         *q = e->succ_next;
2313         next = e->pred_next;
2314         n_edges--;
2315         free (e);
2316       }
2317     for (e = b->succ; e; e = next)
2318       {
2319         for (q = &e->dest->pred; *q != e; q = &(*q)->pred_next)
2320           continue;
2321         *q = e->pred_next;
2322         next = e->succ_next;
2323         n_edges--;
2324         free (e);
2325       }
2326
2327     b->pred = NULL;
2328     b->succ = NULL;
2329   }
2330
2331   /* Remove the basic block from the array, and compact behind it.  */
2332   expunge_block (b);
2333
2334   return deleted_handler;
2335 }
2336
2337 /* Remove block B from the basic block array and compact behind it.  */
2338
2339 static void
2340 expunge_block (b)
2341      basic_block b;
2342 {
2343   int i, n = n_basic_blocks;
2344
2345   for (i = b->index; i + 1 < n; ++i)
2346     {
2347       basic_block x = BASIC_BLOCK (i + 1);
2348       BASIC_BLOCK (i) = x;
2349       x->index = i;
2350     }
2351
2352   basic_block_info->num_elements--;
2353   n_basic_blocks--;
2354 }
2355
2356 /* Delete INSN by patching it out.  Return the next insn.  */
2357
2358 rtx
2359 flow_delete_insn (insn)
2360      rtx insn;
2361 {
2362   rtx prev = PREV_INSN (insn);
2363   rtx next = NEXT_INSN (insn);
2364   rtx note;
2365
2366   PREV_INSN (insn) = NULL_RTX;
2367   NEXT_INSN (insn) = NULL_RTX;
2368   INSN_DELETED_P (insn) = 1;
2369
2370   if (prev)
2371     NEXT_INSN (prev) = next;
2372   if (next)
2373     PREV_INSN (next) = prev;
2374   else
2375     set_last_insn (prev);
2376
2377   if (GET_CODE (insn) == CODE_LABEL)
2378     remove_node_from_expr_list (insn, &nonlocal_goto_handler_labels);
2379
2380   /* If deleting a jump, decrement the use count of the label.  Deleting
2381      the label itself should happen in the normal course of block merging.  */
2382   if (GET_CODE (insn) == JUMP_INSN
2383       && JUMP_LABEL (insn)
2384       && GET_CODE (JUMP_LABEL (insn)) == CODE_LABEL)
2385     LABEL_NUSES (JUMP_LABEL (insn))--;
2386
2387   /* Also if deleting an insn that references a label.  */
2388   else if ((note = find_reg_note (insn, REG_LABEL, NULL_RTX)) != NULL_RTX
2389            && GET_CODE (XEXP (note, 0)) == CODE_LABEL)
2390     LABEL_NUSES (XEXP (note, 0))--;
2391
2392   return next;
2393 }
2394
2395 /* True if a given label can be deleted.  */
2396
2397 static int
2398 can_delete_label_p (label)
2399      rtx label;
2400 {
2401   rtx x;
2402
2403   if (LABEL_PRESERVE_P (label))
2404     return 0;
2405
2406   for (x = forced_labels; x; x = XEXP (x, 1))
2407     if (label == XEXP (x, 0))
2408       return 0;
2409   for (x = label_value_list; x; x = XEXP (x, 1))
2410     if (label == XEXP (x, 0))
2411       return 0;
2412   for (x = exception_handler_labels; x; x = XEXP (x, 1))
2413     if (label == XEXP (x, 0))
2414       return 0;
2415
2416   /* User declared labels must be preserved.  */
2417   if (LABEL_NAME (label) != 0)
2418     return 0;
2419
2420   return 1;
2421 }
2422
2423 static int
2424 tail_recursion_label_p (label)
2425      rtx label;
2426 {
2427   rtx x;
2428
2429   for (x = tail_recursion_label_list; x; x = XEXP (x, 1))
2430     if (label == XEXP (x, 0))
2431       return 1;
2432
2433   return 0;
2434 }
2435
2436 /* Blocks A and B are to be merged into a single block A.  The insns
2437    are already contiguous, hence `nomove'.  */
2438
2439 void
2440 merge_blocks_nomove (a, b)
2441      basic_block a, b;
2442 {
2443   edge e;
2444   rtx b_head, b_end, a_end;
2445   rtx del_first = NULL_RTX, del_last = NULL_RTX;
2446   int b_empty = 0;
2447
2448   /* If there was a CODE_LABEL beginning B, delete it.  */
2449   b_head = b->head;
2450   b_end = b->end;
2451   if (GET_CODE (b_head) == CODE_LABEL)
2452     {
2453       /* Detect basic blocks with nothing but a label.  This can happen
2454          in particular at the end of a function.  */
2455       if (b_head == b_end)
2456         b_empty = 1;
2457       del_first = del_last = b_head;
2458       b_head = NEXT_INSN (b_head);
2459     }
2460
2461   /* Delete the basic block note.  */
2462   if (NOTE_INSN_BASIC_BLOCK_P (b_head))
2463     {
2464       if (b_head == b_end)
2465         b_empty = 1;
2466       if (! del_last)
2467         del_first = b_head;
2468       del_last = b_head;
2469       b_head = NEXT_INSN (b_head);
2470     }
2471
2472   /* If there was a jump out of A, delete it.  */
2473   a_end = a->end;
2474   if (GET_CODE (a_end) == JUMP_INSN)
2475     {
2476       rtx prev;
2477
2478       for (prev = PREV_INSN (a_end); ; prev = PREV_INSN (prev))
2479         if (GET_CODE (prev) != NOTE
2480             || NOTE_LINE_NUMBER (prev) == NOTE_INSN_BASIC_BLOCK
2481             || prev == a->head)
2482           break;
2483
2484       del_first = a_end;
2485
2486 #ifdef HAVE_cc0
2487       /* If this was a conditional jump, we need to also delete
2488          the insn that set cc0.  */
2489       if (prev && sets_cc0_p (prev))
2490         {
2491           rtx tmp = prev;
2492           prev = prev_nonnote_insn (prev);
2493           if (!prev)
2494             prev = a->head;
2495           del_first = tmp;
2496         }
2497 #endif
2498
2499       a_end = prev;
2500     }
2501   else if (GET_CODE (NEXT_INSN (a_end)) == BARRIER)
2502     del_first = NEXT_INSN (a_end);
2503
2504   /* Delete everything marked above as well as crap that might be
2505      hanging out between the two blocks.  */
2506   flow_delete_insn_chain (del_first, del_last);
2507
2508   /* Normally there should only be one successor of A and that is B, but
2509      partway though the merge of blocks for conditional_execution we'll
2510      be merging a TEST block with THEN and ELSE successors.  Free the
2511      whole lot of them and hope the caller knows what they're doing.  */
2512   while (a->succ)
2513     remove_edge (a->succ);
2514
2515   /* Adjust the edges out of B for the new owner.  */
2516   for (e = b->succ; e; e = e->succ_next)
2517     e->src = a;
2518   a->succ = b->succ;
2519
2520   /* B hasn't quite yet ceased to exist.  Attempt to prevent mishap.  */
2521   b->pred = b->succ = NULL;
2522
2523   /* Reassociate the insns of B with A.  */
2524   if (!b_empty)
2525     {
2526       if (basic_block_for_insn)
2527         {
2528           BLOCK_FOR_INSN (b_head) = a;
2529           while (b_head != b_end)
2530             {
2531               b_head = NEXT_INSN (b_head);
2532               BLOCK_FOR_INSN (b_head) = a;
2533             }
2534         }
2535       a_end = b_end;
2536     }
2537   a->end = a_end;
2538
2539   expunge_block (b);
2540 }
2541
2542 /* Blocks A and B are to be merged into a single block.  A has no incoming
2543    fallthru edge, so it can be moved before B without adding or modifying
2544    any jumps (aside from the jump from A to B).  */
2545
2546 static int
2547 merge_blocks_move_predecessor_nojumps (a, b)
2548      basic_block a, b;
2549 {
2550   rtx start, end, barrier;
2551   int index;
2552
2553   start = a->head;
2554   end = a->end;
2555
2556   barrier = next_nonnote_insn (end);
2557   if (GET_CODE (barrier) != BARRIER)
2558     abort ();
2559   flow_delete_insn (barrier);
2560
2561   /* Move block and loop notes out of the chain so that we do not
2562      disturb their order.
2563
2564      ??? A better solution would be to squeeze out all the non-nested notes
2565      and adjust the block trees appropriately.   Even better would be to have
2566      a tighter connection between block trees and rtl so that this is not
2567      necessary.  */
2568   start = squeeze_notes (start, end);
2569
2570   /* Scramble the insn chain.  */
2571   if (end != PREV_INSN (b->head))
2572     reorder_insns (start, end, PREV_INSN (b->head));
2573
2574   if (rtl_dump_file)
2575     {
2576       fprintf (rtl_dump_file, "Moved block %d before %d and merged.\n",
2577                a->index, b->index);
2578     }
2579
2580   /* Swap the records for the two blocks around.  Although we are deleting B,
2581      A is now where B was and we want to compact the BB array from where
2582      A used to be.  */
2583   BASIC_BLOCK (a->index) = b;
2584   BASIC_BLOCK (b->index) = a;
2585   index = a->index;
2586   a->index = b->index;
2587   b->index = index;
2588
2589   /* Now blocks A and B are contiguous.  Merge them.  */
2590   merge_blocks_nomove (a, b);
2591
2592   return 1;
2593 }
2594
2595 /* Blocks A and B are to be merged into a single block.  B has no outgoing
2596    fallthru edge, so it can be moved after A without adding or modifying
2597    any jumps (aside from the jump from A to B).  */
2598
2599 static int
2600 merge_blocks_move_successor_nojumps (a, b)
2601      basic_block a, b;
2602 {
2603   rtx start, end, barrier;
2604
2605   start = b->head;
2606   end = b->end;
2607   barrier = NEXT_INSN (end);
2608
2609   /* Recognize a jump table following block B.  */
2610   if (GET_CODE (barrier) == CODE_LABEL
2611       && NEXT_INSN (barrier)
2612       && GET_CODE (NEXT_INSN (barrier)) == JUMP_INSN
2613       && (GET_CODE (PATTERN (NEXT_INSN (barrier))) == ADDR_VEC
2614           || GET_CODE (PATTERN (NEXT_INSN (barrier))) == ADDR_DIFF_VEC))
2615     {
2616       end = NEXT_INSN (barrier);
2617       barrier = NEXT_INSN (end);
2618     }
2619
2620   /* There had better have been a barrier there.  Delete it.  */
2621   if (GET_CODE (barrier) != BARRIER)
2622     abort ();
2623   flow_delete_insn (barrier);
2624
2625   /* Move block and loop notes out of the chain so that we do not
2626      disturb their order.
2627
2628      ??? A better solution would be to squeeze out all the non-nested notes
2629      and adjust the block trees appropriately.   Even better would be to have
2630      a tighter connection between block trees and rtl so that this is not
2631      necessary.  */
2632   start = squeeze_notes (start, end);
2633
2634   /* Scramble the insn chain.  */
2635   reorder_insns (start, end, a->end);
2636
2637   /* Now blocks A and B are contiguous.  Merge them.  */
2638   merge_blocks_nomove (a, b);
2639
2640   if (rtl_dump_file)
2641     {
2642       fprintf (rtl_dump_file, "Moved block %d after %d and merged.\n",
2643                b->index, a->index);
2644     }
2645
2646   return 1;
2647 }
2648
2649 /* Attempt to merge basic blocks that are potentially non-adjacent.
2650    Return true iff the attempt succeeded.  */
2651
2652 static int
2653 merge_blocks (e, b, c)
2654      edge e;
2655      basic_block b, c;
2656 {
2657   /* If C has a tail recursion label, do not merge.  There is no
2658      edge recorded from the call_placeholder back to this label, as
2659      that would make optimize_sibling_and_tail_recursive_calls more
2660      complex for no gain.  */
2661   if (GET_CODE (c->head) == CODE_LABEL
2662       && tail_recursion_label_p (c->head))
2663     return 0;
2664
2665   /* If B has a fallthru edge to C, no need to move anything.  */
2666   if (e->flags & EDGE_FALLTHRU)
2667     {
2668       merge_blocks_nomove (b, c);
2669
2670       if (rtl_dump_file)
2671         {
2672           fprintf (rtl_dump_file, "Merged %d and %d without moving.\n",
2673                    b->index, c->index);
2674         }
2675
2676       return 1;
2677     }
2678   else
2679     {
2680       edge tmp_edge;
2681       basic_block d;
2682       int c_has_outgoing_fallthru;
2683       int b_has_incoming_fallthru;
2684
2685       /* We must make sure to not munge nesting of exception regions,
2686          lexical blocks, and loop notes.
2687
2688          The first is taken care of by requiring that the active eh
2689          region at the end of one block always matches the active eh
2690          region at the beginning of the next block.
2691
2692          The later two are taken care of by squeezing out all the notes.  */
2693
2694       /* ???  A throw/catch edge (or any abnormal edge) should be rarely
2695          executed and we may want to treat blocks which have two out
2696          edges, one normal, one abnormal as only having one edge for
2697          block merging purposes.  */
2698
2699       for (tmp_edge = c->succ; tmp_edge; tmp_edge = tmp_edge->succ_next)
2700         if (tmp_edge->flags & EDGE_FALLTHRU)
2701           break;
2702       c_has_outgoing_fallthru = (tmp_edge != NULL);
2703
2704       for (tmp_edge = b->pred; tmp_edge; tmp_edge = tmp_edge->pred_next)
2705         if (tmp_edge->flags & EDGE_FALLTHRU)
2706           break;
2707       b_has_incoming_fallthru = (tmp_edge != NULL);
2708
2709       /* If B does not have an incoming fallthru, and the exception regions
2710          match, then it can be moved immediately before C without introducing
2711          or modifying jumps.
2712
2713          C can not be the first block, so we do not have to worry about
2714          accessing a non-existent block.  */
2715       d = BASIC_BLOCK (c->index - 1);
2716       if (! b_has_incoming_fallthru
2717           && d->eh_end == b->eh_beg
2718           && b->eh_end == c->eh_beg)
2719         return merge_blocks_move_predecessor_nojumps (b, c);
2720
2721       /* Otherwise, we're going to try to move C after B.  Make sure the
2722          exception regions match.
2723
2724          If B is the last basic block, then we must not try to access the
2725          block structure for block B + 1.  Luckily in that case we do not
2726          need to worry about matching exception regions.  */
2727       d = (b->index + 1 < n_basic_blocks ? BASIC_BLOCK (b->index + 1) : NULL);
2728       if (b->eh_end == c->eh_beg
2729           && (d == NULL || c->eh_end == d->eh_beg))
2730         {
2731           /* If C does not have an outgoing fallthru, then it can be moved
2732              immediately after B without introducing or modifying jumps.  */
2733           if (! c_has_outgoing_fallthru)
2734             return merge_blocks_move_successor_nojumps (b, c);
2735
2736           /* Otherwise, we'll need to insert an extra jump, and possibly
2737              a new block to contain it.  */
2738           /* ??? Not implemented yet.  */
2739         }
2740
2741       return 0;
2742     }
2743 }
2744
2745 /* Top level driver for merge_blocks.  */
2746
2747 static void
2748 try_merge_blocks ()
2749 {
2750   int i;
2751
2752   /* Attempt to merge blocks as made possible by edge removal.  If a block
2753      has only one successor, and the successor has only one predecessor,
2754      they may be combined.  */
2755
2756   for (i = 0; i < n_basic_blocks;)
2757     {
2758       basic_block c, b = BASIC_BLOCK (i);
2759       edge s;
2760
2761       /* A loop because chains of blocks might be combineable.  */
2762       while ((s = b->succ) != NULL
2763              && s->succ_next == NULL
2764              && (s->flags & EDGE_EH) == 0
2765              && (c = s->dest) != EXIT_BLOCK_PTR
2766              && c->pred->pred_next == NULL
2767              /* If the jump insn has side effects, we can't kill the edge.  */
2768              && (GET_CODE (b->end) != JUMP_INSN
2769                  || onlyjump_p (b->end))
2770              && merge_blocks (s, b, c))
2771         continue;
2772
2773       /* Don't get confused by the index shift caused by deleting blocks.  */
2774       i = b->index + 1;
2775     }
2776 }
2777
2778 /* The given edge should potentially be a fallthru edge.  If that is in
2779    fact true, delete the jump and barriers that are in the way.  */
2780
2781 void
2782 tidy_fallthru_edge (e, b, c)
2783      edge e;
2784      basic_block b, c;
2785 {
2786   rtx q;
2787
2788   /* ??? In a late-running flow pass, other folks may have deleted basic
2789      blocks by nopping out blocks, leaving multiple BARRIERs between here
2790      and the target label. They ought to be chastized and fixed.
2791
2792      We can also wind up with a sequence of undeletable labels between
2793      one block and the next.
2794
2795      So search through a sequence of barriers, labels, and notes for
2796      the head of block C and assert that we really do fall through.  */
2797
2798   if (next_real_insn (b->end) != next_real_insn (PREV_INSN (c->head)))
2799     return;
2800
2801   /* Remove what will soon cease being the jump insn from the source block.
2802      If block B consisted only of this single jump, turn it into a deleted
2803      note.  */
2804   q = b->end;
2805   if (GET_CODE (q) == JUMP_INSN
2806       && onlyjump_p (q)
2807       && (any_uncondjump_p (q)
2808           || (b->succ == e && e->succ_next == NULL)))
2809     {
2810 #ifdef HAVE_cc0
2811       /* If this was a conditional jump, we need to also delete
2812          the insn that set cc0.  */
2813       if (any_condjump_p (q) && sets_cc0_p (PREV_INSN (q)))
2814         q = PREV_INSN (q);
2815 #endif
2816
2817       if (b->head == q)
2818         {
2819           PUT_CODE (q, NOTE);
2820           NOTE_LINE_NUMBER (q) = NOTE_INSN_DELETED;
2821           NOTE_SOURCE_FILE (q) = 0;
2822         }
2823       else
2824         {
2825           q = PREV_INSN (q);
2826
2827           /* We don't want a block to end on a line-number note since that has
2828              the potential of changing the code between -g and not -g.  */
2829           while (GET_CODE (q) == NOTE && NOTE_LINE_NUMBER (q) >= 0)
2830             q = PREV_INSN (q);
2831         }
2832
2833       b->end = q;
2834     }
2835
2836   /* Selectively unlink the sequence.  */
2837   if (q != PREV_INSN (c->head))
2838     flow_delete_insn_chain (NEXT_INSN (q), PREV_INSN (c->head));
2839
2840   e->flags |= EDGE_FALLTHRU;
2841 }
2842
2843 /* Fix up edges that now fall through, or rather should now fall through
2844    but previously required a jump around now deleted blocks.  Simplify
2845    the search by only examining blocks numerically adjacent, since this
2846    is how find_basic_blocks created them.  */
2847
2848 static void
2849 tidy_fallthru_edges ()
2850 {
2851   int i;
2852
2853   for (i = 1; i < n_basic_blocks; ++i)
2854     {
2855       basic_block b = BASIC_BLOCK (i - 1);
2856       basic_block c = BASIC_BLOCK (i);
2857       edge s;
2858
2859       /* We care about simple conditional or unconditional jumps with
2860          a single successor.
2861
2862          If we had a conditional branch to the next instruction when
2863          find_basic_blocks was called, then there will only be one
2864          out edge for the block which ended with the conditional
2865          branch (since we do not create duplicate edges).
2866
2867          Furthermore, the edge will be marked as a fallthru because we
2868          merge the flags for the duplicate edges.  So we do not want to
2869          check that the edge is not a FALLTHRU edge.  */
2870       if ((s = b->succ) != NULL
2871           && s->succ_next == NULL
2872           && s->dest == c
2873           /* If the jump insn has side effects, we can't tidy the edge.  */
2874           && (GET_CODE (b->end) != JUMP_INSN
2875               || onlyjump_p (b->end)))
2876         tidy_fallthru_edge (s, b, c);
2877     }
2878 }
2879 \f
2880 /* Perform data flow analysis.
2881    F is the first insn of the function; FLAGS is a set of PROP_* flags
2882    to be used in accumulating flow info.  */
2883
2884 void
2885 life_analysis (f, file, flags)
2886      rtx f;
2887      FILE *file;
2888      int flags;
2889 {
2890 #ifdef ELIMINABLE_REGS
2891   register int i;
2892   static struct {int from, to; } eliminables[] = ELIMINABLE_REGS;
2893 #endif
2894
2895   /* Record which registers will be eliminated.  We use this in
2896      mark_used_regs.  */
2897
2898   CLEAR_HARD_REG_SET (elim_reg_set);
2899
2900 #ifdef ELIMINABLE_REGS
2901   for (i = 0; i < (int) ARRAY_SIZE (eliminables); i++)
2902     SET_HARD_REG_BIT (elim_reg_set, eliminables[i].from);
2903 #else
2904   SET_HARD_REG_BIT (elim_reg_set, FRAME_POINTER_REGNUM);
2905 #endif
2906
2907   if (! optimize)
2908     flags &= ~(PROP_LOG_LINKS | PROP_AUTOINC);
2909
2910   /* The post-reload life analysis have (on a global basis) the same
2911      registers live as was computed by reload itself.  elimination
2912      Otherwise offsets and such may be incorrect.
2913
2914      Reload will make some registers as live even though they do not
2915      appear in the rtl.
2916
2917      We don't want to create new auto-incs after reload, since they
2918      are unlikely to be useful and can cause problems with shared
2919      stack slots.  */
2920   if (reload_completed)
2921     flags &= ~(PROP_REG_INFO | PROP_AUTOINC);
2922
2923   /* We want alias analysis information for local dead store elimination.  */
2924   if (optimize && (flags & PROP_SCAN_DEAD_CODE))
2925     init_alias_analysis ();
2926
2927   /* Always remove no-op moves.  Do this before other processing so
2928      that we don't have to keep re-scanning them.  */
2929   delete_noop_moves (f);
2930
2931   /* Some targets can emit simpler epilogues if they know that sp was
2932      not ever modified during the function.  After reload, of course,
2933      we've already emitted the epilogue so there's no sense searching.  */
2934   if (! reload_completed)
2935     notice_stack_pointer_modification (f);
2936
2937   /* Allocate and zero out data structures that will record the
2938      data from lifetime analysis.  */
2939   allocate_reg_life_data ();
2940   allocate_bb_life_data ();
2941
2942   /* Find the set of registers live on function exit.  */
2943   mark_regs_live_at_end (EXIT_BLOCK_PTR->global_live_at_start);
2944
2945   /* "Update" life info from zero.  It'd be nice to begin the
2946      relaxation with just the exit and noreturn blocks, but that set
2947      is not immediately handy.  */
2948
2949   if (flags & PROP_REG_INFO)
2950     memset (regs_ever_live, 0, sizeof (regs_ever_live));
2951   update_life_info (NULL, UPDATE_LIFE_GLOBAL, flags);
2952
2953   /* Clean up.  */
2954   if (optimize && (flags & PROP_SCAN_DEAD_CODE))
2955     end_alias_analysis ();
2956
2957   if (file)
2958     dump_flow_info (file);
2959
2960   free_basic_block_vars (1);
2961 }
2962
2963 /* A subroutine of verify_wide_reg, called through for_each_rtx.
2964    Search for REGNO.  If found, abort if it is not wider than word_mode.  */
2965
2966 static int
2967 verify_wide_reg_1 (px, pregno)
2968      rtx *px;
2969      void *pregno;
2970 {
2971   rtx x = *px;
2972   unsigned int regno = *(int *) pregno;
2973
2974   if (GET_CODE (x) == REG && REGNO (x) == regno)
2975     {
2976       if (GET_MODE_BITSIZE (GET_MODE (x)) <= BITS_PER_WORD)
2977         abort ();
2978       return 1;
2979     }
2980   return 0;
2981 }
2982
2983 /* A subroutine of verify_local_live_at_start.  Search through insns
2984    between HEAD and END looking for register REGNO.  */
2985
2986 static void
2987 verify_wide_reg (regno, head, end)
2988      int regno;
2989      rtx head, end;
2990 {
2991   while (1)
2992     {
2993       if (INSN_P (head)
2994           && for_each_rtx (&PATTERN (head), verify_wide_reg_1, &regno))
2995         return;
2996       if (head == end)
2997         break;
2998       head = NEXT_INSN (head);
2999     }
3000
3001   /* We didn't find the register at all.  Something's way screwy.  */
3002   if (rtl_dump_file)
3003     fprintf (rtl_dump_file, "Aborting in verify_wide_reg; reg %d\n", regno);
3004   print_rtl_and_abort ();
3005 }
3006
3007 /* A subroutine of update_life_info.  Verify that there are no untoward
3008    changes in live_at_start during a local update.  */
3009
3010 static void
3011 verify_local_live_at_start (new_live_at_start, bb)
3012      regset new_live_at_start;
3013      basic_block bb;
3014 {
3015   if (reload_completed)
3016     {
3017       /* After reload, there are no pseudos, nor subregs of multi-word
3018          registers.  The regsets should exactly match.  */
3019       if (! REG_SET_EQUAL_P (new_live_at_start, bb->global_live_at_start))
3020         {
3021           if (rtl_dump_file)
3022             {
3023               fprintf (rtl_dump_file,
3024                        "live_at_start mismatch in bb %d, aborting\n",
3025                        bb->index);
3026               debug_bitmap_file (rtl_dump_file, bb->global_live_at_start);
3027               debug_bitmap_file (rtl_dump_file, new_live_at_start);
3028             }
3029           print_rtl_and_abort ();
3030         }
3031     }
3032   else
3033     {
3034       int i;
3035
3036       /* Find the set of changed registers.  */
3037       XOR_REG_SET (new_live_at_start, bb->global_live_at_start);
3038
3039       EXECUTE_IF_SET_IN_REG_SET (new_live_at_start, 0, i,
3040         {
3041           /* No registers should die.  */
3042           if (REGNO_REG_SET_P (bb->global_live_at_start, i))
3043             {
3044               if (rtl_dump_file)
3045                 fprintf (rtl_dump_file,
3046                          "Register %d died unexpectedly in block %d\n", i,
3047                          bb->index);
3048               print_rtl_and_abort ();
3049             }
3050
3051           /* Verify that the now-live register is wider than word_mode.  */
3052           verify_wide_reg (i, bb->head, bb->end);
3053         });
3054     }
3055 }
3056
3057 /* Updates life information starting with the basic blocks set in BLOCKS.
3058    If BLOCKS is null, consider it to be the universal set.
3059
3060    If EXTENT is UPDATE_LIFE_LOCAL, such as after splitting or peepholeing,
3061    we are only expecting local modifications to basic blocks.  If we find
3062    extra registers live at the beginning of a block, then we either killed
3063    useful data, or we have a broken split that wants data not provided.
3064    If we find registers removed from live_at_start, that means we have
3065    a broken peephole that is killing a register it shouldn't.
3066
3067    ??? This is not true in one situation -- when a pre-reload splitter
3068    generates subregs of a multi-word pseudo, current life analysis will
3069    lose the kill.  So we _can_ have a pseudo go live.  How irritating.
3070
3071    Including PROP_REG_INFO does not properly refresh regs_ever_live
3072    unless the caller resets it to zero.  */
3073
3074 void
3075 update_life_info (blocks, extent, prop_flags)
3076      sbitmap blocks;
3077      enum update_life_extent extent;
3078      int prop_flags;
3079 {
3080   regset tmp;
3081   regset_head tmp_head;
3082   int i;
3083
3084   tmp = INITIALIZE_REG_SET (tmp_head);
3085
3086   /* For a global update, we go through the relaxation process again.  */
3087   if (extent != UPDATE_LIFE_LOCAL)
3088     {
3089       calculate_global_regs_live (blocks, blocks,
3090                                   prop_flags & PROP_SCAN_DEAD_CODE);
3091
3092       /* If asked, remove notes from the blocks we'll update.  */
3093       if (extent == UPDATE_LIFE_GLOBAL_RM_NOTES)
3094         count_or_remove_death_notes (blocks, 1);
3095     }
3096
3097   if (blocks)
3098     {
3099       EXECUTE_IF_SET_IN_SBITMAP (blocks, 0, i,
3100         {
3101           basic_block bb = BASIC_BLOCK (i);
3102
3103           COPY_REG_SET (tmp, bb->global_live_at_end);
3104           propagate_block (bb, tmp, NULL, NULL, prop_flags);
3105
3106           if (extent == UPDATE_LIFE_LOCAL)
3107             verify_local_live_at_start (tmp, bb);
3108         });
3109     }
3110   else
3111     {
3112       for (i = n_basic_blocks - 1; i >= 0; --i)
3113         {
3114           basic_block bb = BASIC_BLOCK (i);
3115
3116           COPY_REG_SET (tmp, bb->global_live_at_end);
3117           propagate_block (bb, tmp, NULL, NULL, prop_flags);
3118
3119           if (extent == UPDATE_LIFE_LOCAL)
3120             verify_local_live_at_start (tmp, bb);
3121         }
3122     }
3123
3124   FREE_REG_SET (tmp);
3125
3126   if (prop_flags & PROP_REG_INFO)
3127     {
3128       /* The only pseudos that are live at the beginning of the function
3129          are those that were not set anywhere in the function.  local-alloc
3130          doesn't know how to handle these correctly, so mark them as not
3131          local to any one basic block.  */
3132       EXECUTE_IF_SET_IN_REG_SET (ENTRY_BLOCK_PTR->global_live_at_end,
3133                                  FIRST_PSEUDO_REGISTER, i,
3134                                  { REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL; });
3135
3136       /* We have a problem with any pseudoreg that lives across the setjmp.
3137          ANSI says that if a user variable does not change in value between
3138          the setjmp and the longjmp, then the longjmp preserves it.  This
3139          includes longjmp from a place where the pseudo appears dead.
3140          (In principle, the value still exists if it is in scope.)
3141          If the pseudo goes in a hard reg, some other value may occupy
3142          that hard reg where this pseudo is dead, thus clobbering the pseudo.
3143          Conclusion: such a pseudo must not go in a hard reg.  */
3144       EXECUTE_IF_SET_IN_REG_SET (regs_live_at_setjmp,
3145                                  FIRST_PSEUDO_REGISTER, i,
3146                                  {
3147                                    if (regno_reg_rtx[i] != 0)
3148                                      {
3149                                        REG_LIVE_LENGTH (i) = -1;
3150                                        REG_BASIC_BLOCK (i) = REG_BLOCK_UNKNOWN;
3151                                      }
3152                                  });
3153     }
3154 }
3155
3156 /* Free the variables allocated by find_basic_blocks.
3157
3158    KEEP_HEAD_END_P is non-zero if basic_block_info is not to be freed.  */
3159
3160 void
3161 free_basic_block_vars (keep_head_end_p)
3162      int keep_head_end_p;
3163 {
3164   if (basic_block_for_insn)
3165     {
3166       VARRAY_FREE (basic_block_for_insn);
3167       basic_block_for_insn = NULL;
3168     }
3169
3170   if (! keep_head_end_p)
3171     {
3172       clear_edges ();
3173       VARRAY_FREE (basic_block_info);
3174       n_basic_blocks = 0;
3175
3176       ENTRY_BLOCK_PTR->aux = NULL;
3177       ENTRY_BLOCK_PTR->global_live_at_end = NULL;
3178       EXIT_BLOCK_PTR->aux = NULL;
3179       EXIT_BLOCK_PTR->global_live_at_start = NULL;
3180     }
3181 }
3182
3183 /* Return nonzero if the destination of SET equals the source.  */
3184
3185 static int
3186 set_noop_p (set)
3187      rtx set;
3188 {
3189   rtx src = SET_SRC (set);
3190   rtx dst = SET_DEST (set);
3191
3192   if (GET_CODE (src) == SUBREG && GET_CODE (dst) == SUBREG)
3193     {
3194       if (SUBREG_WORD (src) != SUBREG_WORD (dst))
3195         return 0;
3196       src = SUBREG_REG (src);
3197       dst = SUBREG_REG (dst);
3198     }
3199
3200   return (GET_CODE (src) == REG && GET_CODE (dst) == REG
3201           && REGNO (src) == REGNO (dst));
3202 }
3203
3204 /* Return nonzero if an insn consists only of SETs, each of which only sets a
3205    value to itself.  */
3206
3207 static int
3208 noop_move_p (insn)
3209      rtx insn;
3210 {
3211   rtx pat = PATTERN (insn);
3212
3213   /* Insns carrying these notes are useful later on.  */
3214   if (find_reg_note (insn, REG_EQUAL, NULL_RTX))
3215     return 0;
3216
3217   if (GET_CODE (pat) == SET && set_noop_p (pat))
3218     return 1;
3219
3220   if (GET_CODE (pat) == PARALLEL)
3221     {
3222       int i;
3223       /* If nothing but SETs of registers to themselves,
3224          this insn can also be deleted.  */
3225       for (i = 0; i < XVECLEN (pat, 0); i++)
3226         {
3227           rtx tem = XVECEXP (pat, 0, i);
3228
3229           if (GET_CODE (tem) == USE
3230               || GET_CODE (tem) == CLOBBER)
3231             continue;
3232
3233           if (GET_CODE (tem) != SET || ! set_noop_p (tem))
3234             return 0;
3235         }
3236
3237       return 1;
3238     }
3239   return 0;
3240 }
3241
3242 /* Delete any insns that copy a register to itself.  */
3243
3244 static void
3245 delete_noop_moves (f)
3246      rtx f;
3247 {
3248   rtx insn;
3249   for (insn = f; insn; insn = NEXT_INSN (insn))
3250     {
3251       if (GET_CODE (insn) == INSN && noop_move_p (insn))
3252         {
3253           PUT_CODE (insn, NOTE);
3254           NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
3255           NOTE_SOURCE_FILE (insn) = 0;
3256         }
3257     }
3258 }
3259
3260 /* Determine if the stack pointer is constant over the life of the function.
3261    Only useful before prologues have been emitted.  */
3262
3263 static void
3264 notice_stack_pointer_modification_1 (x, pat, data)
3265      rtx x;
3266      rtx pat ATTRIBUTE_UNUSED;
3267      void *data ATTRIBUTE_UNUSED;
3268 {
3269   if (x == stack_pointer_rtx
3270       /* The stack pointer is only modified indirectly as the result
3271          of a push until later in flow.  See the comments in rtl.texi
3272          regarding Embedded Side-Effects on Addresses.  */
3273       || (GET_CODE (x) == MEM
3274           && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == 'a'
3275           && XEXP (XEXP (x, 0), 0) == stack_pointer_rtx))
3276     current_function_sp_is_unchanging = 0;
3277 }
3278
3279 static void
3280 notice_stack_pointer_modification (f)
3281      rtx f;
3282 {
3283   rtx insn;
3284
3285   /* Assume that the stack pointer is unchanging if alloca hasn't
3286      been used.  */
3287   current_function_sp_is_unchanging = !current_function_calls_alloca;
3288   if (! current_function_sp_is_unchanging)
3289     return;
3290
3291   for (insn = f; insn; insn = NEXT_INSN (insn))
3292     {
3293       if (INSN_P (insn))
3294         {
3295           /* Check if insn modifies the stack pointer.  */
3296           note_stores (PATTERN (insn), notice_stack_pointer_modification_1,
3297                        NULL);
3298           if (! current_function_sp_is_unchanging)
3299             return;
3300         }
3301     }
3302 }
3303
3304 /* Mark a register in SET.  Hard registers in large modes get all
3305    of their component registers set as well.  */
3306
3307 static void
3308 mark_reg (reg, xset)
3309      rtx reg;
3310      void *xset;
3311 {
3312   regset set = (regset) xset;
3313   int regno = REGNO (reg);
3314
3315   if (GET_MODE (reg) == BLKmode)
3316     abort ();
3317
3318   SET_REGNO_REG_SET (set, regno);
3319   if (regno < FIRST_PSEUDO_REGISTER)
3320     {
3321       int n = HARD_REGNO_NREGS (regno, GET_MODE (reg));
3322       while (--n > 0)
3323         SET_REGNO_REG_SET (set, regno + n);
3324     }
3325 }
3326
3327 /* Mark those regs which are needed at the end of the function as live
3328    at the end of the last basic block.  */
3329
3330 static void
3331 mark_regs_live_at_end (set)
3332      regset set;
3333 {
3334   int i;
3335
3336   /* If exiting needs the right stack value, consider the stack pointer
3337      live at the end of the function.  */
3338   if ((HAVE_epilogue && reload_completed)
3339       || ! EXIT_IGNORE_STACK
3340       || (! FRAME_POINTER_REQUIRED
3341           && ! current_function_calls_alloca
3342           && flag_omit_frame_pointer)
3343       || current_function_sp_is_unchanging)
3344     {
3345       SET_REGNO_REG_SET (set, STACK_POINTER_REGNUM);
3346     }
3347
3348   /* Mark the frame pointer if needed at the end of the function.  If
3349      we end up eliminating it, it will be removed from the live list
3350      of each basic block by reload.  */
3351
3352   if (! reload_completed || frame_pointer_needed)
3353     {
3354       SET_REGNO_REG_SET (set, FRAME_POINTER_REGNUM);
3355 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3356       /* If they are different, also mark the hard frame pointer as live.  */
3357       if (! LOCAL_REGNO (HARD_FRAME_POINTER_REGNUM))
3358         SET_REGNO_REG_SET (set, HARD_FRAME_POINTER_REGNUM);
3359 #endif
3360     }
3361
3362 #ifndef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
3363   /* Many architectures have a GP register even without flag_pic.
3364      Assume the pic register is not in use, or will be handled by
3365      other means, if it is not fixed.  */
3366   if (PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
3367       && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
3368     SET_REGNO_REG_SET (set, PIC_OFFSET_TABLE_REGNUM);
3369 #endif
3370
3371   /* Mark all global registers, and all registers used by the epilogue
3372      as being live at the end of the function since they may be
3373      referenced by our caller.  */
3374   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3375     if (global_regs[i] || EPILOGUE_USES (i))
3376       SET_REGNO_REG_SET (set, i);
3377
3378   /* Mark all call-saved registers that we actaully used.  */
3379   if (HAVE_epilogue && reload_completed)
3380     {
3381       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3382         if (regs_ever_live[i] && ! call_used_regs[i] && ! LOCAL_REGNO (i))
3383           SET_REGNO_REG_SET (set, i);
3384     }
3385
3386   /* Mark function return value.  */
3387   diddle_return_value (mark_reg, set);
3388 }
3389
3390 /* Callback function for for_each_successor_phi.  DATA is a regset.
3391    Sets the SRC_REGNO, the regno of the phi alternative for phi node
3392    INSN, in the regset.  */
3393
3394 static int
3395 set_phi_alternative_reg (insn, dest_regno, src_regno, data)
3396      rtx insn ATTRIBUTE_UNUSED;
3397      int dest_regno ATTRIBUTE_UNUSED;
3398      int src_regno;
3399      void *data;
3400 {
3401   regset live = (regset) data;
3402   SET_REGNO_REG_SET (live, src_regno);
3403   return 0;
3404 }
3405
3406 /* Propagate global life info around the graph of basic blocks.  Begin
3407    considering blocks with their corresponding bit set in BLOCKS_IN.
3408    If BLOCKS_IN is null, consider it the universal set.
3409
3410    BLOCKS_OUT is set for every block that was changed.  */
3411
3412 static void
3413 calculate_global_regs_live (blocks_in, blocks_out, flags)
3414      sbitmap blocks_in, blocks_out;
3415      int flags;
3416 {
3417   basic_block *queue, *qhead, *qtail, *qend;
3418   regset tmp, new_live_at_end;
3419   regset_head tmp_head;
3420   regset_head new_live_at_end_head;
3421   int i;
3422
3423   tmp = INITIALIZE_REG_SET (tmp_head);
3424   new_live_at_end = INITIALIZE_REG_SET (new_live_at_end_head);
3425
3426   /* Create a worklist.  Allocate an extra slot for ENTRY_BLOCK, and one
3427      because the `head == tail' style test for an empty queue doesn't
3428      work with a full queue.  */
3429   queue = (basic_block *) xmalloc ((n_basic_blocks + 2) * sizeof (*queue));
3430   qtail = queue;
3431   qhead = qend = queue + n_basic_blocks + 2;
3432
3433   /* Queue the blocks set in the initial mask.  Do this in reverse block
3434      number order so that we are more likely for the first round to do
3435      useful work.  We use AUX non-null to flag that the block is queued.  */
3436   if (blocks_in)
3437     {
3438       /* Clear out the garbage that might be hanging out in bb->aux.  */
3439       for (i = n_basic_blocks - 1; i >= 0; --i)
3440         BASIC_BLOCK (i)->aux = NULL;
3441
3442       EXECUTE_IF_SET_IN_SBITMAP (blocks_in, 0, i,
3443         {
3444           basic_block bb = BASIC_BLOCK (i);
3445           *--qhead = bb;
3446           bb->aux = bb;
3447         });
3448     }
3449   else
3450     {
3451       for (i = 0; i < n_basic_blocks; ++i)
3452         {
3453           basic_block bb = BASIC_BLOCK (i);
3454           *--qhead = bb;
3455           bb->aux = bb;
3456         }
3457     }
3458
3459   if (blocks_out)
3460     sbitmap_zero (blocks_out);
3461
3462   while (qhead != qtail)
3463     {
3464       int rescan, changed;
3465       basic_block bb;
3466       edge e;
3467
3468       bb = *qhead++;
3469       if (qhead == qend)
3470         qhead = queue;
3471       bb->aux = NULL;
3472
3473       /* Begin by propogating live_at_start from the successor blocks.  */
3474       CLEAR_REG_SET (new_live_at_end);
3475       for (e = bb->succ; e; e = e->succ_next)
3476         {
3477           basic_block sb = e->dest;
3478           IOR_REG_SET (new_live_at_end, sb->global_live_at_start);
3479         }
3480
3481       /* The all-important stack pointer must always be live.  */
3482       SET_REGNO_REG_SET (new_live_at_end, STACK_POINTER_REGNUM);
3483
3484       /* Before reload, there are a few registers that must be forced
3485          live everywhere -- which might not already be the case for
3486          blocks within infinite loops.  */
3487       if (! reload_completed)
3488         {
3489           /* Any reference to any pseudo before reload is a potential
3490              reference of the frame pointer.  */
3491           SET_REGNO_REG_SET (new_live_at_end, FRAME_POINTER_REGNUM);
3492
3493 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3494           /* Pseudos with argument area equivalences may require
3495              reloading via the argument pointer.  */
3496           if (fixed_regs[ARG_POINTER_REGNUM])
3497             SET_REGNO_REG_SET (new_live_at_end, ARG_POINTER_REGNUM);
3498 #endif
3499
3500           /* Any constant, or pseudo with constant equivalences, may
3501              require reloading from memory using the pic register.  */
3502           if (PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
3503               && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
3504             SET_REGNO_REG_SET (new_live_at_end, PIC_OFFSET_TABLE_REGNUM);
3505         }
3506
3507       /* Regs used in phi nodes are not included in
3508          global_live_at_start, since they are live only along a
3509          particular edge.  Set those regs that are live because of a
3510          phi node alternative corresponding to this particular block.  */
3511       if (in_ssa_form)
3512         for_each_successor_phi (bb, &set_phi_alternative_reg,
3513                                 new_live_at_end);
3514
3515       if (bb == ENTRY_BLOCK_PTR)
3516         {
3517           COPY_REG_SET (bb->global_live_at_end, new_live_at_end);
3518           continue;
3519         }
3520
3521       /* On our first pass through this block, we'll go ahead and continue.
3522          Recognize first pass by local_set NULL.  On subsequent passes, we
3523          get to skip out early if live_at_end wouldn't have changed.  */
3524
3525       if (bb->local_set == NULL)
3526         {
3527           bb->local_set = OBSTACK_ALLOC_REG_SET (&flow_obstack);
3528           bb->cond_local_set = OBSTACK_ALLOC_REG_SET (&flow_obstack);
3529           rescan = 1;
3530         }
3531       else
3532         {
3533           /* If any bits were removed from live_at_end, we'll have to
3534              rescan the block.  This wouldn't be necessary if we had
3535              precalculated local_live, however with PROP_SCAN_DEAD_CODE
3536              local_live is really dependent on live_at_end.  */
3537           CLEAR_REG_SET (tmp);
3538           rescan = bitmap_operation (tmp, bb->global_live_at_end,
3539                                      new_live_at_end, BITMAP_AND_COMPL);
3540
3541           if (! rescan)
3542             {
3543               /* If any of the registers in the new live_at_end set are
3544                  conditionally set in this basic block, we must rescan.
3545                  This is because conditional lifetimes at the end of the
3546                  block do not just take the live_at_end set into account,
3547                  but also the liveness at the start of each successor
3548                  block.  We can miss changes in those sets if we only
3549                  compare the new live_at_end against the previous one.  */
3550               CLEAR_REG_SET (tmp);
3551               rescan = bitmap_operation (tmp, new_live_at_end,
3552                                          bb->cond_local_set, BITMAP_AND);
3553             }
3554
3555           if (! rescan)
3556             {
3557               /* Find the set of changed bits.  Take this opportunity
3558                  to notice that this set is empty and early out.  */
3559               CLEAR_REG_SET (tmp);
3560               changed = bitmap_operation (tmp, bb->global_live_at_end,
3561                                           new_live_at_end, BITMAP_XOR);
3562               if (! changed)
3563                 continue;
3564
3565               /* If any of the changed bits overlap with local_set,
3566                  we'll have to rescan the block.  Detect overlap by
3567                  the AND with ~local_set turning off bits.  */
3568               rescan = bitmap_operation (tmp, tmp, bb->local_set,
3569                                          BITMAP_AND_COMPL);
3570             }
3571         }
3572
3573       /* Let our caller know that BB changed enough to require its
3574          death notes updated.  */
3575       if (blocks_out)
3576         SET_BIT (blocks_out, bb->index);
3577
3578       if (! rescan)
3579         {
3580           /* Add to live_at_start the set of all registers in
3581              new_live_at_end that aren't in the old live_at_end.  */
3582
3583           bitmap_operation (tmp, new_live_at_end, bb->global_live_at_end,
3584                             BITMAP_AND_COMPL);
3585           COPY_REG_SET (bb->global_live_at_end, new_live_at_end);
3586
3587           changed = bitmap_operation (bb->global_live_at_start,
3588                                       bb->global_live_at_start,
3589                                       tmp, BITMAP_IOR);
3590           if (! changed)
3591             continue;
3592         }
3593       else
3594         {
3595           COPY_REG_SET (bb->global_live_at_end, new_live_at_end);
3596
3597           /* Rescan the block insn by insn to turn (a copy of) live_at_end
3598              into live_at_start.  */
3599           propagate_block (bb, new_live_at_end, bb->local_set,
3600                            bb->cond_local_set, flags);
3601
3602           /* If live_at start didn't change, no need to go farther.  */
3603           if (REG_SET_EQUAL_P (bb->global_live_at_start, new_live_at_end))
3604             continue;
3605
3606           COPY_REG_SET (bb->global_live_at_start, new_live_at_end);
3607         }
3608
3609       /* Queue all predecessors of BB so that we may re-examine
3610          their live_at_end.  */
3611       for (e = bb->pred; e; e = e->pred_next)
3612         {
3613           basic_block pb = e->src;
3614           if (pb->aux == NULL)
3615             {
3616               *qtail++ = pb;
3617               if (qtail == qend)
3618                 qtail = queue;
3619               pb->aux = pb;
3620             }
3621         }
3622     }
3623
3624   FREE_REG_SET (tmp);
3625   FREE_REG_SET (new_live_at_end);
3626
3627   if (blocks_out)
3628     {
3629       EXECUTE_IF_SET_IN_SBITMAP (blocks_out, 0, i,
3630         {
3631           basic_block bb = BASIC_BLOCK (i);
3632           FREE_REG_SET (bb->local_set);
3633           FREE_REG_SET (bb->cond_local_set);
3634         });
3635     }
3636   else
3637     {
3638       for (i = n_basic_blocks - 1; i >= 0; --i)
3639         {
3640           basic_block bb = BASIC_BLOCK (i);
3641           FREE_REG_SET (bb->local_set);
3642           FREE_REG_SET (bb->cond_local_set);
3643         }
3644     }
3645
3646   free (queue);
3647 }
3648 \f
3649 /* Subroutines of life analysis.  */
3650
3651 /* Allocate the permanent data structures that represent the results
3652    of life analysis.  Not static since used also for stupid life analysis.  */
3653
3654 static void
3655 allocate_bb_life_data ()
3656 {
3657   register int i;
3658
3659   for (i = 0; i < n_basic_blocks; i++)
3660     {
3661       basic_block bb = BASIC_BLOCK (i);
3662
3663       bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (&flow_obstack);
3664       bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (&flow_obstack);
3665     }
3666
3667   ENTRY_BLOCK_PTR->global_live_at_end
3668     = OBSTACK_ALLOC_REG_SET (&flow_obstack);
3669   EXIT_BLOCK_PTR->global_live_at_start
3670     = OBSTACK_ALLOC_REG_SET (&flow_obstack);
3671
3672   regs_live_at_setjmp = OBSTACK_ALLOC_REG_SET (&flow_obstack);
3673 }
3674
3675 void
3676 allocate_reg_life_data ()
3677 {
3678   int i;
3679
3680   max_regno = max_reg_num ();
3681
3682   /* Recalculate the register space, in case it has grown.  Old style
3683      vector oriented regsets would set regset_{size,bytes} here also.  */
3684   allocate_reg_info (max_regno, FALSE, FALSE);
3685
3686   /* Reset all the data we'll collect in propagate_block and its
3687      subroutines.  */
3688   for (i = 0; i < max_regno; i++)
3689     {
3690       REG_N_SETS (i) = 0;
3691       REG_N_REFS (i) = 0;
3692       REG_N_DEATHS (i) = 0;
3693       REG_N_CALLS_CROSSED (i) = 0;
3694       REG_LIVE_LENGTH (i) = 0;
3695       REG_BASIC_BLOCK (i) = REG_BLOCK_UNKNOWN;
3696     }
3697 }
3698
3699 /* Delete dead instructions for propagate_block.  */
3700
3701 static void
3702 propagate_block_delete_insn (bb, insn)
3703      basic_block bb;
3704      rtx insn;
3705 {
3706   rtx inote = find_reg_note (insn, REG_LABEL, NULL_RTX);
3707
3708   /* If the insn referred to a label, and that label was attached to
3709      an ADDR_VEC, it's safe to delete the ADDR_VEC.  In fact, it's
3710      pretty much mandatory to delete it, because the ADDR_VEC may be
3711      referencing labels that no longer exist.  */
3712
3713   if (inote)
3714     {
3715       rtx label = XEXP (inote, 0);
3716       rtx next;
3717
3718       if (LABEL_NUSES (label) == 1
3719           && (next = next_nonnote_insn (label)) != NULL
3720           && GET_CODE (next) == JUMP_INSN
3721           && (GET_CODE (PATTERN (next)) == ADDR_VEC
3722               || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC))
3723         {
3724           rtx pat = PATTERN (next);
3725           int diff_vec_p = GET_CODE (pat) == ADDR_DIFF_VEC;
3726           int len = XVECLEN (pat, diff_vec_p);
3727           int i;
3728
3729           for (i = 0; i < len; i++)
3730             LABEL_NUSES (XEXP (XVECEXP (pat, diff_vec_p, i), 0))--;
3731
3732           flow_delete_insn (next);
3733         }
3734     }
3735
3736   if (bb->end == insn)
3737     bb->end = PREV_INSN (insn);
3738   flow_delete_insn (insn);
3739 }
3740
3741 /* Delete dead libcalls for propagate_block.  Return the insn
3742    before the libcall.  */
3743
3744 static rtx
3745 propagate_block_delete_libcall (bb, insn, note)
3746      basic_block bb;
3747      rtx insn, note;
3748 {
3749   rtx first = XEXP (note, 0);
3750   rtx before = PREV_INSN (first);
3751
3752   if (insn == bb->end)
3753     bb->end = before;
3754
3755   flow_delete_insn_chain (first, insn);
3756   return before;
3757 }
3758
3759 /* Update the life-status of regs for one insn.  Return the previous insn.  */
3760
3761 rtx
3762 propagate_one_insn (pbi, insn)
3763      struct propagate_block_info *pbi;
3764      rtx insn;
3765 {
3766   rtx prev = PREV_INSN (insn);
3767   int flags = pbi->flags;
3768   int insn_is_dead = 0;
3769   int libcall_is_dead = 0;
3770   rtx note;
3771   int i;
3772
3773   if (! INSN_P (insn))
3774     return prev;
3775
3776   note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
3777   if (flags & PROP_SCAN_DEAD_CODE)
3778     {
3779       insn_is_dead = insn_dead_p (pbi, PATTERN (insn), 0, REG_NOTES (insn));
3780       libcall_is_dead = (insn_is_dead && note != 0
3781                          && libcall_dead_p (pbi, note, insn));
3782     }
3783
3784   /* If an instruction consists of just dead store(s) on final pass,
3785      delete it.  */
3786   if ((flags & PROP_KILL_DEAD_CODE) && insn_is_dead)
3787     {
3788       /* If we're trying to delete a prologue or epilogue instruction
3789          that isn't flagged as possibly being dead, something is wrong.
3790          But if we are keeping the stack pointer depressed, we might well
3791          be deleting insns that are used to compute the amount to update
3792          it by, so they are fine.  */
3793       if (reload_completed
3794           && !(TREE_CODE (TREE_TYPE (current_function_decl)) == FUNCTION_TYPE
3795                 && (TYPE_RETURNS_STACK_DEPRESSED
3796                     (TREE_TYPE (current_function_decl))))
3797           && (((HAVE_epilogue || HAVE_prologue)
3798                && prologue_epilogue_contains (insn))
3799               || (HAVE_sibcall_epilogue
3800                   && sibcall_epilogue_contains (insn)))
3801           && find_reg_note (insn, REG_MAYBE_DEAD, NULL_RTX) == 0)
3802         abort ();
3803
3804       /* Record sets.  Do this even for dead instructions, since they
3805          would have killed the values if they hadn't been deleted.  */
3806       mark_set_regs (pbi, PATTERN (insn), insn);
3807
3808       /* CC0 is now known to be dead.  Either this insn used it,
3809          in which case it doesn't anymore, or clobbered it,
3810          so the next insn can't use it.  */
3811       pbi->cc0_live = 0;
3812
3813       if (libcall_is_dead)
3814         {
3815           prev = propagate_block_delete_libcall (pbi->bb, insn, note);
3816           insn = NEXT_INSN (prev);
3817         }
3818       else
3819         propagate_block_delete_insn (pbi->bb, insn);
3820
3821       return prev;
3822     }
3823
3824   /* See if this is an increment or decrement that can be merged into
3825      a following memory address.  */
3826 #ifdef AUTO_INC_DEC
3827   {
3828     register rtx x = single_set (insn);
3829
3830     /* Does this instruction increment or decrement a register?  */
3831     if ((flags & PROP_AUTOINC)
3832         && x != 0
3833         && GET_CODE (SET_DEST (x)) == REG
3834         && (GET_CODE (SET_SRC (x)) == PLUS
3835             || GET_CODE (SET_SRC (x)) == MINUS)
3836         && XEXP (SET_SRC (x), 0) == SET_DEST (x)
3837         && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3838         /* Ok, look for a following memory ref we can combine with.
3839            If one is found, change the memory ref to a PRE_INC
3840            or PRE_DEC, cancel this insn, and return 1.
3841            Return 0 if nothing has been done.  */
3842         && try_pre_increment_1 (pbi, insn))
3843       return prev;
3844   }
3845 #endif /* AUTO_INC_DEC */
3846
3847   CLEAR_REG_SET (pbi->new_set);
3848
3849   /* If this is not the final pass, and this insn is copying the value of
3850      a library call and it's dead, don't scan the insns that perform the
3851      library call, so that the call's arguments are not marked live.  */
3852   if (libcall_is_dead)
3853     {
3854       /* Record the death of the dest reg.  */
3855       mark_set_regs (pbi, PATTERN (insn), insn);
3856
3857       insn = XEXP (note, 0);
3858       return PREV_INSN (insn);
3859     }
3860   else if (GET_CODE (PATTERN (insn)) == SET
3861            && SET_DEST (PATTERN (insn)) == stack_pointer_rtx
3862            && GET_CODE (SET_SRC (PATTERN (insn))) == PLUS
3863            && XEXP (SET_SRC (PATTERN (insn)), 0) == stack_pointer_rtx
3864            && GET_CODE (XEXP (SET_SRC (PATTERN (insn)), 1)) == CONST_INT)
3865     /* We have an insn to pop a constant amount off the stack.
3866        (Such insns use PLUS regardless of the direction of the stack,
3867        and any insn to adjust the stack by a constant is always a pop.)
3868        These insns, if not dead stores, have no effect on life.  */
3869     ;
3870   else
3871     {
3872       /* Any regs live at the time of a call instruction must not go
3873          in a register clobbered by calls.  Find all regs now live and
3874          record this for them.  */
3875
3876       if (GET_CODE (insn) == CALL_INSN && (flags & PROP_REG_INFO))
3877         EXECUTE_IF_SET_IN_REG_SET (pbi->reg_live, 0, i,
3878                                    { REG_N_CALLS_CROSSED (i)++; });
3879
3880       /* Record sets.  Do this even for dead instructions, since they
3881          would have killed the values if they hadn't been deleted.  */
3882       mark_set_regs (pbi, PATTERN (insn), insn);
3883
3884       if (GET_CODE (insn) == CALL_INSN)
3885         {
3886           register int i;
3887           rtx note, cond;
3888
3889           cond = NULL_RTX;
3890           if (GET_CODE (PATTERN (insn)) == COND_EXEC)
3891             cond = COND_EXEC_TEST (PATTERN (insn));
3892
3893           /* Non-constant calls clobber memory.  */
3894           if (! CONST_CALL_P (insn))
3895             {
3896               free_EXPR_LIST_list (&pbi->mem_set_list);
3897               pbi->mem_set_list_len = 0;
3898             }
3899
3900           /* There may be extra registers to be clobbered.  */
3901           for (note = CALL_INSN_FUNCTION_USAGE (insn);
3902                note;
3903                note = XEXP (note, 1))
3904             if (GET_CODE (XEXP (note, 0)) == CLOBBER)
3905               mark_set_1 (pbi, CLOBBER, XEXP (XEXP (note, 0), 0),
3906                           cond, insn, pbi->flags);
3907
3908           /* Calls change all call-used and global registers.  */
3909           for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3910             if (call_used_regs[i] && ! global_regs[i]
3911                 && ! fixed_regs[i])
3912               {
3913                 /* We do not want REG_UNUSED notes for these registers.  */
3914                 mark_set_1 (pbi, CLOBBER, gen_rtx_REG (reg_raw_mode[i], i),
3915                             cond, insn,
3916                             pbi->flags & ~(PROP_DEATH_NOTES | PROP_REG_INFO));
3917               }
3918         }
3919
3920       /* If an insn doesn't use CC0, it becomes dead since we assume
3921          that every insn clobbers it.  So show it dead here;
3922          mark_used_regs will set it live if it is referenced.  */
3923       pbi->cc0_live = 0;
3924
3925       /* Record uses.  */
3926       if (! insn_is_dead)
3927         mark_used_regs (pbi, PATTERN (insn), NULL_RTX, insn);
3928
3929       /* Sometimes we may have inserted something before INSN (such as a move)
3930          when we make an auto-inc.  So ensure we will scan those insns.  */
3931 #ifdef AUTO_INC_DEC
3932       prev = PREV_INSN (insn);
3933 #endif
3934
3935       if (! insn_is_dead && GET_CODE (insn) == CALL_INSN)
3936         {
3937           register int i;
3938           rtx note, cond;
3939
3940           cond = NULL_RTX;
3941           if (GET_CODE (PATTERN (insn)) == COND_EXEC)
3942             cond = COND_EXEC_TEST (PATTERN (insn));
3943
3944           /* Calls use their arguments.  */
3945           for (note = CALL_INSN_FUNCTION_USAGE (insn);
3946                note;
3947                note = XEXP (note, 1))
3948             if (GET_CODE (XEXP (note, 0)) == USE)
3949               mark_used_regs (pbi, XEXP (XEXP (note, 0), 0),
3950                               cond, insn);
3951
3952           /* The stack ptr is used (honorarily) by a CALL insn.  */
3953           SET_REGNO_REG_SET (pbi->reg_live, STACK_POINTER_REGNUM);
3954
3955           /* Calls may also reference any of the global registers,
3956              so they are made live.  */
3957           for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3958             if (global_regs[i])
3959               mark_used_reg (pbi, gen_rtx_REG (reg_raw_mode[i], i),
3960                              cond, insn);
3961         }
3962     }
3963
3964   /* On final pass, update counts of how many insns in which each reg
3965      is live.  */
3966   if (flags & PROP_REG_INFO)
3967     EXECUTE_IF_SET_IN_REG_SET (pbi->reg_live, 0, i,
3968                                { REG_LIVE_LENGTH (i)++; });
3969
3970   return prev;
3971 }
3972
3973 /* Initialize a propagate_block_info struct for public consumption.
3974    Note that the structure itself is opaque to this file, but that
3975    the user can use the regsets provided here.  */
3976
3977 struct propagate_block_info *
3978 init_propagate_block_info (bb, live, local_set, cond_local_set, flags)
3979      basic_block bb;
3980      regset live, local_set, cond_local_set;
3981      int flags;
3982 {
3983   struct propagate_block_info *pbi = xmalloc (sizeof (*pbi));
3984
3985   pbi->bb = bb;
3986   pbi->reg_live = live;
3987   pbi->mem_set_list = NULL_RTX;
3988   pbi->mem_set_list_len = 0;
3989   pbi->local_set = local_set;
3990   pbi->cond_local_set = cond_local_set;
3991   pbi->cc0_live = 0;
3992   pbi->flags = flags;
3993
3994   if (flags & (PROP_LOG_LINKS | PROP_AUTOINC))
3995     pbi->reg_next_use = (rtx *) xcalloc (max_reg_num (), sizeof (rtx));
3996   else
3997     pbi->reg_next_use = NULL;
3998
3999   pbi->new_set = BITMAP_XMALLOC ();
4000
4001 #ifdef HAVE_conditional_execution
4002   pbi->reg_cond_dead = splay_tree_new (splay_tree_compare_ints, NULL,
4003                                        free_reg_cond_life_info);
4004   pbi->reg_cond_reg = BITMAP_XMALLOC ();
4005
4006   /* If this block ends in a conditional branch, for each register live
4007      from one side of the branch and not the other, record the register
4008      as conditionally dead.  */
4009   if (GET_CODE (bb->end) == JUMP_INSN
4010       && any_condjump_p (bb->end))
4011     {
4012       regset_head diff_head;
4013       regset diff = INITIALIZE_REG_SET (diff_head);
4014       basic_block bb_true, bb_false;
4015       rtx cond_true, cond_false, set_src;
4016       int i;
4017
4018       /* Identify the successor blocks.  */
4019       bb_true = bb->succ->dest;
4020       if (bb->succ->succ_next != NULL)
4021         {
4022           bb_false = bb->succ->succ_next->dest;
4023
4024           if (bb->succ->flags & EDGE_FALLTHRU)
4025             {
4026               basic_block t = bb_false;
4027               bb_false = bb_true;
4028               bb_true = t;
4029             }
4030           else if (! (bb->succ->succ_next->flags & EDGE_FALLTHRU))
4031             abort ();
4032         }
4033       else
4034         {
4035           /* This can happen with a conditional jump to the next insn.  */
4036           if (JUMP_LABEL (bb->end) != bb_true->head)
4037             abort ();
4038
4039           /* Simplest way to do nothing.  */
4040           bb_false = bb_true;
4041         }
4042
4043       /* Extract the condition from the branch.  */
4044       set_src = SET_SRC (pc_set (bb->end));
4045       cond_true = XEXP (set_src, 0);
4046       cond_false = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond_true)),
4047                                    GET_MODE (cond_true), XEXP (cond_true, 0),
4048                                    XEXP (cond_true, 1));
4049       if (GET_CODE (XEXP (set_src, 1)) == PC)
4050         {
4051           rtx t = cond_false;
4052           cond_false = cond_true;
4053           cond_true = t;
4054         }
4055
4056       /* Compute which register lead different lives in the successors.  */
4057       if (bitmap_operation (diff, bb_true->global_live_at_start,
4058                             bb_false->global_live_at_start, BITMAP_XOR))
4059         {
4060           rtx reg = XEXP (cond_true, 0);
4061
4062           if (GET_CODE (reg) == SUBREG)
4063             reg = SUBREG_REG (reg);
4064
4065           if (GET_CODE (reg) != REG)
4066             abort ();
4067
4068           SET_REGNO_REG_SET (pbi->reg_cond_reg, REGNO (reg));
4069
4070           /* For each such register, mark it conditionally dead.  */
4071           EXECUTE_IF_SET_IN_REG_SET
4072             (diff, 0, i,
4073              {
4074                struct reg_cond_life_info *rcli;
4075                rtx cond;
4076
4077                rcli = (struct reg_cond_life_info *) xmalloc (sizeof (*rcli));
4078
4079                if (REGNO_REG_SET_P (bb_true->global_live_at_start, i))
4080                  cond = cond_false;
4081                else
4082                  cond = cond_true;
4083                rcli->condition = cond;
4084
4085                splay_tree_insert (pbi->reg_cond_dead, i,
4086                                   (splay_tree_value) rcli);
4087              });
4088         }
4089
4090       FREE_REG_SET (diff);
4091     }
4092 #endif
4093
4094   /* If this block has no successors, any stores to the frame that aren't
4095      used later in the block are dead.  So make a pass over the block
4096      recording any such that are made and show them dead at the end.  We do
4097      a very conservative and simple job here.  */
4098   if (optimize
4099       && ! (TREE_CODE (TREE_TYPE (current_function_decl)) == FUNCTION_TYPE
4100             && (TYPE_RETURNS_STACK_DEPRESSED
4101                 (TREE_TYPE (current_function_decl))))
4102       && (flags & PROP_SCAN_DEAD_CODE)
4103       && (bb->succ == NULL
4104           || (bb->succ->succ_next == NULL
4105               && bb->succ->dest == EXIT_BLOCK_PTR)))
4106     {
4107       rtx insn;
4108       for (insn = bb->end; insn != bb->head; insn = PREV_INSN (insn))
4109         if (GET_CODE (insn) == INSN
4110             && GET_CODE (PATTERN (insn)) == SET
4111             && GET_CODE (SET_DEST (PATTERN (insn))) == MEM)
4112           {
4113             rtx mem = SET_DEST (PATTERN (insn));
4114
4115             /* This optimization is performed by faking a store to the
4116                memory at the end of the block.  This doesn't work for
4117                unchanging memories because multiple stores to unchanging
4118                memory is illegal and alias analysis doesn't consider it.  */
4119             if (RTX_UNCHANGING_P (mem))
4120               continue;
4121
4122             if (XEXP (mem, 0) == frame_pointer_rtx
4123                 || (GET_CODE (XEXP (mem, 0)) == PLUS
4124                     && XEXP (XEXP (mem, 0), 0) == frame_pointer_rtx
4125                     && GET_CODE (XEXP (XEXP (mem, 0), 1)) == CONST_INT))
4126               {
4127 #ifdef AUTO_INC_DEC
4128                 /* Store a copy of mem, otherwise the address may be scrogged
4129                    by find_auto_inc.  This matters because insn_dead_p uses
4130                    an rtx_equal_p check to determine if two addresses are
4131                    the same.  This works before find_auto_inc, but fails
4132                    after find_auto_inc, causing discrepencies between the
4133                    set of live registers calculated during the
4134                    calculate_global_regs_live phase and what actually exists
4135                    after flow completes, leading to aborts.  */
4136                 if (flags & PROP_AUTOINC)
4137                   mem = shallow_copy_rtx (mem);
4138 #endif
4139                 pbi->mem_set_list = alloc_EXPR_LIST (0, mem, pbi->mem_set_list);
4140                 if (++pbi->mem_set_list_len >= MAX_MEM_SET_LIST_LEN)
4141                   break;
4142               }
4143           }
4144     }
4145
4146   return pbi;
4147 }
4148
4149 /* Release a propagate_block_info struct.  */
4150
4151 void
4152 free_propagate_block_info (pbi)
4153      struct propagate_block_info *pbi;
4154 {
4155   free_EXPR_LIST_list (&pbi->mem_set_list);
4156
4157   BITMAP_XFREE (pbi->new_set);
4158
4159 #ifdef HAVE_conditional_execution
4160   splay_tree_delete (pbi->reg_cond_dead);
4161   BITMAP_XFREE (pbi->reg_cond_reg);
4162 #endif
4163
4164   if (pbi->reg_next_use)
4165     free (pbi->reg_next_use);
4166
4167   free (pbi);
4168 }
4169
4170 /* Compute the registers live at the beginning of a basic block BB from
4171    those live at the end.
4172
4173    When called, REG_LIVE contains those live at the end.  On return, it
4174    contains those live at the beginning.
4175
4176    LOCAL_SET, if non-null, will be set with all registers killed
4177    unconditionally by this basic block.
4178    Likewise, COND_LOCAL_SET, if non-null, will be set with all registers
4179    killed conditionally by this basic block.  If there is any unconditional
4180    set of a register, then the corresponding bit will be set in LOCAL_SET
4181    and cleared in COND_LOCAL_SET.
4182    It is valid for LOCAL_SET and COND_LOCAL_SET to be the same set.  In this
4183    case, the resulting set will be equal to the union of the two sets that
4184    would otherwise be computed.  */
4185
4186 void
4187 propagate_block (bb, live, local_set, cond_local_set, flags)
4188      basic_block bb;
4189      regset live;
4190      regset local_set;
4191      regset cond_local_set;
4192      int flags;
4193 {
4194   struct propagate_block_info *pbi;
4195   rtx insn, prev;
4196
4197   pbi = init_propagate_block_info (bb, live, local_set, cond_local_set, flags);
4198
4199   if (flags & PROP_REG_INFO)
4200     {
4201       register int i;
4202
4203       /* Process the regs live at the end of the block.
4204          Mark them as not local to any one basic block.  */
4205       EXECUTE_IF_SET_IN_REG_SET (live, 0, i,
4206                                  { REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL; });
4207     }
4208
4209   /* Scan the block an insn at a time from end to beginning.  */
4210
4211   for (insn = bb->end;; insn = prev)
4212     {
4213       /* If this is a call to `setjmp' et al, warn if any
4214          non-volatile datum is live.  */
4215       if ((flags & PROP_REG_INFO)
4216           && GET_CODE (insn) == NOTE
4217           && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
4218         IOR_REG_SET (regs_live_at_setjmp, pbi->reg_live);
4219
4220       prev = propagate_one_insn (pbi, insn);
4221
4222       if (insn == bb->head)
4223         break;
4224     }
4225
4226   free_propagate_block_info (pbi);
4227 }
4228 \f
4229 /* Return 1 if X (the body of an insn, or part of it) is just dead stores
4230    (SET expressions whose destinations are registers dead after the insn).
4231    NEEDED is the regset that says which regs are alive after the insn.
4232
4233    Unless CALL_OK is non-zero, an insn is needed if it contains a CALL.
4234
4235    If X is the entire body of an insn, NOTES contains the reg notes
4236    pertaining to the insn.  */
4237
4238 static int
4239 insn_dead_p (pbi, x, call_ok, notes)
4240      struct propagate_block_info *pbi;
4241      rtx x;
4242      int call_ok;
4243      rtx notes ATTRIBUTE_UNUSED;
4244 {
4245   enum rtx_code code = GET_CODE (x);
4246
4247 #ifdef AUTO_INC_DEC
4248   /* If flow is invoked after reload, we must take existing AUTO_INC
4249      expresions into account.  */
4250   if (reload_completed)
4251     {
4252       for (; notes; notes = XEXP (notes, 1))
4253         {
4254           if (REG_NOTE_KIND (notes) == REG_INC)
4255             {
4256               int regno = REGNO (XEXP (notes, 0));
4257
4258               /* Don't delete insns to set global regs.  */
4259               if ((regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
4260                   || REGNO_REG_SET_P (pbi->reg_live, regno))
4261                 return 0;
4262             }
4263         }
4264     }
4265 #endif
4266
4267   /* If setting something that's a reg or part of one,
4268      see if that register's altered value will be live.  */
4269
4270   if (code == SET)
4271     {
4272       rtx r = SET_DEST (x);
4273
4274 #ifdef HAVE_cc0
4275       if (GET_CODE (r) == CC0)
4276         return ! pbi->cc0_live;
4277 #endif
4278
4279       /* A SET that is a subroutine call cannot be dead.  */
4280       if (GET_CODE (SET_SRC (x)) == CALL)
4281         {
4282           if (! call_ok)
4283             return 0;
4284         }
4285
4286       /* Don't eliminate loads from volatile memory or volatile asms.  */
4287       else if (volatile_refs_p (SET_SRC (x)))
4288         return 0;
4289
4290       if (GET_CODE (r) == MEM)
4291         {
4292           rtx temp;
4293
4294           if (MEM_VOLATILE_P (r))
4295             return 0;
4296
4297           /* Walk the set of memory locations we are currently tracking
4298              and see if one is an identical match to this memory location.
4299              If so, this memory write is dead (remember, we're walking
4300              backwards from the end of the block to the start).  */
4301           temp = pbi->mem_set_list;
4302           while (temp)
4303             {
4304               rtx mem = XEXP (temp, 0);
4305
4306               if (rtx_equal_p (mem, r))
4307                 return 1;
4308 #ifdef AUTO_INC_DEC
4309               /* Check if memory reference matches an auto increment. Only
4310                  post increment/decrement or modify are valid.  */
4311               if (GET_MODE (mem) == GET_MODE (r)
4312                   && (GET_CODE (XEXP (mem, 0)) == POST_DEC
4313                       || GET_CODE (XEXP (mem, 0)) == POST_INC
4314                       || GET_CODE (XEXP (mem, 0)) == POST_MODIFY)
4315                   && GET_MODE (XEXP (mem, 0)) == GET_MODE (r)
4316                   && rtx_equal_p (XEXP (XEXP (mem, 0), 0), XEXP (r, 0)))
4317                 return 1;
4318 #endif
4319               temp = XEXP (temp, 1);
4320             }
4321         }
4322       else
4323         {
4324           while (GET_CODE (r) == SUBREG
4325                  || GET_CODE (r) == STRICT_LOW_PART
4326                  || GET_CODE (r) == ZERO_EXTRACT)
4327             r = XEXP (r, 0);
4328
4329           if (GET_CODE (r) == REG)
4330             {
4331               int regno = REGNO (r);
4332
4333               /* Obvious.  */
4334               if (REGNO_REG_SET_P (pbi->reg_live, regno))
4335                 return 0;
4336
4337               /* If this is a hard register, verify that subsequent
4338                  words are not needed.  */
4339               if (regno < FIRST_PSEUDO_REGISTER)
4340                 {
4341                   int n = HARD_REGNO_NREGS (regno, GET_MODE (r));
4342
4343                   while (--n > 0)
4344                     if (REGNO_REG_SET_P (pbi->reg_live, regno+n))
4345                       return 0;
4346                 }
4347
4348               /* Don't delete insns to set global regs.  */
4349               if (regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
4350                 return 0;
4351
4352               /* Make sure insns to set the stack pointer aren't deleted.  */
4353               if (regno == STACK_POINTER_REGNUM)
4354                 return 0;
4355
4356               /* ??? These bits might be redundant with the force live bits
4357                  in calculate_global_regs_live.  We would delete from
4358                  sequential sets; whether this actually affects real code
4359                  for anything but the stack pointer I don't know.  */
4360               /* Make sure insns to set the frame pointer aren't deleted.  */
4361               if (regno == FRAME_POINTER_REGNUM
4362                   && (! reload_completed || frame_pointer_needed))
4363                 return 0;
4364 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
4365               if (regno == HARD_FRAME_POINTER_REGNUM
4366                   && (! reload_completed || frame_pointer_needed))
4367                 return 0;
4368 #endif
4369
4370 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
4371               /* Make sure insns to set arg pointer are never deleted
4372                  (if the arg pointer isn't fixed, there will be a USE
4373                  for it, so we can treat it normally).  */
4374               if (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
4375                 return 0;
4376 #endif
4377
4378               /* Otherwise, the set is dead.  */
4379               return 1;
4380             }
4381         }
4382     }
4383
4384   /* If performing several activities, insn is dead if each activity
4385      is individually dead.  Also, CLOBBERs and USEs can be ignored; a
4386      CLOBBER or USE that's inside a PARALLEL doesn't make the insn
4387      worth keeping.  */
4388   else if (code == PARALLEL)
4389     {
4390       int i = XVECLEN (x, 0);
4391
4392       for (i--; i >= 0; i--)
4393         if (GET_CODE (XVECEXP (x, 0, i)) != CLOBBER
4394             && GET_CODE (XVECEXP (x, 0, i)) != USE
4395             && ! insn_dead_p (pbi, XVECEXP (x, 0, i), call_ok, NULL_RTX))
4396           return 0;
4397
4398       return 1;
4399     }
4400
4401   /* A CLOBBER of a pseudo-register that is dead serves no purpose.  That
4402      is not necessarily true for hard registers.  */
4403   else if (code == CLOBBER && GET_CODE (XEXP (x, 0)) == REG
4404            && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER
4405            && ! REGNO_REG_SET_P (pbi->reg_live, REGNO (XEXP (x, 0))))
4406     return 1;
4407
4408   /* We do not check other CLOBBER or USE here.  An insn consisting of just
4409      a CLOBBER or just a USE should not be deleted.  */
4410   return 0;
4411 }
4412
4413 /* If INSN is the last insn in a libcall, and assuming INSN is dead,
4414    return 1 if the entire library call is dead.
4415    This is true if INSN copies a register (hard or pseudo)
4416    and if the hard return reg of the call insn is dead.
4417    (The caller should have tested the destination of the SET inside
4418    INSN already for death.)
4419
4420    If this insn doesn't just copy a register, then we don't
4421    have an ordinary libcall.  In that case, cse could not have
4422    managed to substitute the source for the dest later on,
4423    so we can assume the libcall is dead.
4424
4425    PBI is the block info giving pseudoregs live before this insn.
4426    NOTE is the REG_RETVAL note of the insn.  */
4427
4428 static int
4429 libcall_dead_p (pbi, note, insn)
4430      struct propagate_block_info *pbi;
4431      rtx note;
4432      rtx insn;
4433 {
4434   rtx x = single_set (insn);
4435
4436   if (x)
4437     {
4438       register rtx r = SET_SRC (x);
4439       if (GET_CODE (r) == REG)
4440         {
4441           rtx call = XEXP (note, 0);
4442           rtx call_pat;
4443           register int i;
4444
4445           /* Find the call insn.  */
4446           while (call != insn && GET_CODE (call) != CALL_INSN)
4447             call = NEXT_INSN (call);
4448
4449           /* If there is none, do nothing special,
4450              since ordinary death handling can understand these insns.  */
4451           if (call == insn)
4452             return 0;
4453
4454           /* See if the hard reg holding the value is dead.
4455              If this is a PARALLEL, find the call within it.  */
4456           call_pat = PATTERN (call);
4457           if (GET_CODE (call_pat) == PARALLEL)
4458             {
4459               for (i = XVECLEN (call_pat, 0) - 1; i >= 0; i--)
4460                 if (GET_CODE (XVECEXP (call_pat, 0, i)) == SET
4461                     && GET_CODE (SET_SRC (XVECEXP (call_pat, 0, i))) == CALL)
4462                   break;
4463
4464               /* This may be a library call that is returning a value
4465                  via invisible pointer.  Do nothing special, since
4466                  ordinary death handling can understand these insns.  */
4467               if (i < 0)
4468                 return 0;
4469
4470               call_pat = XVECEXP (call_pat, 0, i);
4471             }
4472
4473           return insn_dead_p (pbi, call_pat, 1, REG_NOTES (call));
4474         }
4475     }
4476   return 1;
4477 }
4478
4479 /* Return 1 if register REGNO was used before it was set, i.e. if it is
4480    live at function entry.  Don't count global register variables, variables
4481    in registers that can be used for function arg passing, or variables in
4482    fixed hard registers.  */
4483
4484 int
4485 regno_uninitialized (regno)
4486      int regno;
4487 {
4488   if (n_basic_blocks == 0
4489       || (regno < FIRST_PSEUDO_REGISTER
4490           && (global_regs[regno]
4491               || fixed_regs[regno]
4492               || FUNCTION_ARG_REGNO_P (regno))))
4493     return 0;
4494
4495   return REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, regno);
4496 }
4497
4498 /* 1 if register REGNO was alive at a place where `setjmp' was called
4499    and was set more than once or is an argument.
4500    Such regs may be clobbered by `longjmp'.  */
4501
4502 int
4503 regno_clobbered_at_setjmp (regno)
4504      int regno;
4505 {
4506   if (n_basic_blocks == 0)
4507     return 0;
4508
4509   return ((REG_N_SETS (regno) > 1
4510            || REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, regno))
4511           && REGNO_REG_SET_P (regs_live_at_setjmp, regno));
4512 }
4513 \f
4514 /* INSN references memory, possibly using autoincrement addressing modes.
4515    Find any entries on the mem_set_list that need to be invalidated due
4516    to an address change.  */
4517
4518 static void
4519 invalidate_mems_from_autoinc (pbi, insn)
4520      struct propagate_block_info *pbi;
4521      rtx insn;
4522 {
4523   rtx note = REG_NOTES (insn);
4524   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
4525     {
4526       if (REG_NOTE_KIND (note) == REG_INC)
4527         {
4528           rtx temp = pbi->mem_set_list;
4529           rtx prev = NULL_RTX;
4530           rtx next;
4531
4532           while (temp)
4533             {
4534               next = XEXP (temp, 1);
4535               if (reg_overlap_mentioned_p (XEXP (note, 0), XEXP (temp, 0)))
4536                 {
4537                   /* Splice temp out of list.  */
4538                   if (prev)
4539                     XEXP (prev, 1) = next;
4540                   else
4541                     pbi->mem_set_list = next;
4542                   free_EXPR_LIST_node (temp);
4543                   pbi->mem_set_list_len--;
4544                 }
4545               else
4546                 prev = temp;
4547               temp = next;
4548             }
4549         }
4550     }
4551 }
4552
4553 /* EXP is either a MEM or a REG.  Remove any dependant entries
4554    from pbi->mem_set_list.  */
4555
4556 static void
4557 invalidate_mems_from_set (pbi, exp)
4558      struct propagate_block_info *pbi;
4559      rtx exp;
4560 {
4561   rtx temp = pbi->mem_set_list;
4562   rtx prev = NULL_RTX;
4563   rtx next;
4564
4565   while (temp)
4566     {
4567       next = XEXP (temp, 1);
4568       if ((GET_CODE (exp) == MEM
4569            && output_dependence (XEXP (temp, 0), exp))
4570           || (GET_CODE (exp) == REG
4571               && reg_overlap_mentioned_p (exp, XEXP (temp, 0))))
4572         {
4573           /* Splice this entry out of the list.  */
4574           if (prev)
4575             XEXP (prev, 1) = next;
4576           else
4577             pbi->mem_set_list = next;
4578           free_EXPR_LIST_node (temp);
4579           pbi->mem_set_list_len--;
4580         }
4581       else
4582         prev = temp;
4583       temp = next;
4584     }
4585 }
4586
4587 /* Process the registers that are set within X.  Their bits are set to
4588    1 in the regset DEAD, because they are dead prior to this insn.
4589
4590    If INSN is nonzero, it is the insn being processed.
4591
4592    FLAGS is the set of operations to perform.  */
4593
4594 static void
4595 mark_set_regs (pbi, x, insn)
4596      struct propagate_block_info *pbi;
4597      rtx x, insn;
4598 {
4599   rtx cond = NULL_RTX;
4600   rtx link;
4601   enum rtx_code code;
4602
4603   if (insn)
4604     for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
4605       {
4606         if (REG_NOTE_KIND (link) == REG_INC)
4607           mark_set_1 (pbi, SET, XEXP (link, 0),
4608                       (GET_CODE (x) == COND_EXEC
4609                        ? COND_EXEC_TEST (x) : NULL_RTX),
4610                       insn, pbi->flags);
4611       }
4612  retry:
4613   switch (code = GET_CODE (x))
4614     {
4615     case SET:
4616     case CLOBBER:
4617       mark_set_1 (pbi, code, SET_DEST (x), cond, insn, pbi->flags);
4618       return;
4619
4620     case COND_EXEC:
4621       cond = COND_EXEC_TEST (x);
4622       x = COND_EXEC_CODE (x);
4623       goto retry;
4624
4625     case PARALLEL:
4626       {
4627         register int i;
4628         for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
4629           {
4630             rtx sub = XVECEXP (x, 0, i);
4631             switch (code = GET_CODE (sub))
4632               {
4633               case COND_EXEC:
4634                 if (cond != NULL_RTX)
4635                   abort ();
4636
4637                 cond = COND_EXEC_TEST (sub);
4638                 sub = COND_EXEC_CODE (sub);
4639                 if (GET_CODE (sub) != SET && GET_CODE (sub) != CLOBBER)
4640                   break;
4641                 /* Fall through.  */
4642
4643               case SET:
4644               case CLOBBER:
4645                 mark_set_1 (pbi, code, SET_DEST (sub), cond, insn, pbi->flags);
4646                 break;
4647
4648               default:
4649                 break;
4650               }
4651           }
4652         break;
4653       }
4654
4655     default:
4656       break;
4657     }
4658 }
4659
4660 /* Process a single SET rtx, X.  */
4661
4662 static void
4663 mark_set_1 (pbi, code, reg, cond, insn, flags)
4664      struct propagate_block_info *pbi;
4665      enum rtx_code code;
4666      rtx reg, cond, insn;
4667      int flags;
4668 {
4669   int regno_first = -1, regno_last = -1;
4670   int not_dead = 0;
4671   int i;
4672
4673   /* Modifying just one hardware register of a multi-reg value or just a
4674      byte field of a register does not mean the value from before this insn
4675      is now dead.  Of course, if it was dead after it's unused now.  */
4676
4677   switch (GET_CODE (reg))
4678     {
4679     case PARALLEL:
4680       /* Some targets place small structures in registers for return values of
4681          functions.  We have to detect this case specially here to get correct
4682          flow information.  */
4683       for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
4684         if (XEXP (XVECEXP (reg, 0, i), 0) != 0)
4685           mark_set_1 (pbi, code, XEXP (XVECEXP (reg, 0, i), 0), cond, insn,
4686                       flags);
4687       return;
4688
4689     case ZERO_EXTRACT:
4690     case SIGN_EXTRACT:
4691     case STRICT_LOW_PART:
4692       /* ??? Assumes STRICT_LOW_PART not used on multi-word registers.  */
4693       do
4694         reg = XEXP (reg, 0);
4695       while (GET_CODE (reg) == SUBREG
4696              || GET_CODE (reg) == ZERO_EXTRACT
4697              || GET_CODE (reg) == SIGN_EXTRACT
4698              || GET_CODE (reg) == STRICT_LOW_PART);
4699       if (GET_CODE (reg) == MEM)
4700         break;
4701       not_dead = REGNO_REG_SET_P (pbi->reg_live, REGNO (reg));
4702       /* Fall through.  */
4703
4704     case REG:
4705       regno_last = regno_first = REGNO (reg);
4706       if (regno_first < FIRST_PSEUDO_REGISTER)
4707         regno_last += HARD_REGNO_NREGS (regno_first, GET_MODE (reg)) - 1;
4708       break;
4709
4710     case SUBREG:
4711       if (GET_CODE (SUBREG_REG (reg)) == REG)
4712         {
4713           enum machine_mode outer_mode = GET_MODE (reg);
4714           enum machine_mode inner_mode = GET_MODE (SUBREG_REG (reg));
4715
4716           /* Identify the range of registers affected.  This is moderately
4717              tricky for hard registers.  See alter_subreg.  */
4718
4719           regno_last = regno_first = REGNO (SUBREG_REG (reg));
4720           if (regno_first < FIRST_PSEUDO_REGISTER)
4721             {
4722 #ifdef ALTER_HARD_SUBREG
4723               regno_first = ALTER_HARD_SUBREG (outer_mode, SUBREG_WORD (reg),
4724                                                inner_mode, regno_first);
4725 #else
4726               regno_first += SUBREG_WORD (reg);
4727 #endif
4728               regno_last = (regno_first
4729                             + HARD_REGNO_NREGS (regno_first, outer_mode) - 1);
4730
4731               /* Since we've just adjusted the register number ranges, make
4732                  sure REG matches.  Otherwise some_was_live will be clear
4733                  when it shouldn't have been, and we'll create incorrect
4734                  REG_UNUSED notes.  */
4735               reg = gen_rtx_REG (outer_mode, regno_first);
4736             }
4737           else
4738             {
4739               /* If the number of words in the subreg is less than the number
4740                  of words in the full register, we have a well-defined partial
4741                  set.  Otherwise the high bits are undefined.
4742
4743                  This is only really applicable to pseudos, since we just took
4744                  care of multi-word hard registers.  */
4745               if (((GET_MODE_SIZE (outer_mode)
4746                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
4747                   < ((GET_MODE_SIZE (inner_mode)
4748                       + UNITS_PER_WORD - 1) / UNITS_PER_WORD))
4749                 not_dead = REGNO_REG_SET_P (pbi->reg_live, regno_first);
4750
4751               reg = SUBREG_REG (reg);
4752             }
4753         }
4754       else
4755         reg = SUBREG_REG (reg);
4756       break;
4757
4758     default:
4759       break;
4760     }
4761
4762   /* If this set is a MEM, then it kills any aliased writes.
4763      If this set is a REG, then it kills any MEMs which use the reg.  */
4764   if (optimize && (flags & PROP_SCAN_DEAD_CODE))
4765     {
4766       if (GET_CODE (reg) == MEM || GET_CODE (reg) == REG)
4767         invalidate_mems_from_set (pbi, reg);
4768
4769       /* If the memory reference had embedded side effects (autoincrement
4770          address modes.  Then we may need to kill some entries on the
4771          memory set list.  */
4772       if (insn && GET_CODE (reg) == MEM)
4773         invalidate_mems_from_autoinc (pbi, insn);
4774
4775       if (pbi->mem_set_list_len < MAX_MEM_SET_LIST_LEN
4776           && GET_CODE (reg) == MEM && ! side_effects_p (reg)
4777           /* ??? With more effort we could track conditional memory life.  */
4778           && ! cond
4779           /* We do not know the size of a BLKmode store, so we do not track
4780              them for redundant store elimination.  */
4781           && GET_MODE (reg) != BLKmode
4782           /* There are no REG_INC notes for SP, so we can't assume we'll see
4783              everything that invalidates it.  To be safe, don't eliminate any
4784              stores though SP; none of them should be redundant anyway.  */
4785           && ! reg_mentioned_p (stack_pointer_rtx, reg))
4786         {
4787 #ifdef AUTO_INC_DEC
4788           /* Store a copy of mem, otherwise the address may be
4789              scrogged by find_auto_inc.  */
4790           if (flags & PROP_AUTOINC)
4791             reg = shallow_copy_rtx (reg);
4792 #endif
4793           pbi->mem_set_list = alloc_EXPR_LIST (0, reg, pbi->mem_set_list);
4794           pbi->mem_set_list_len++;
4795         }
4796     }
4797
4798   if (GET_CODE (reg) == REG
4799       && ! (regno_first == FRAME_POINTER_REGNUM
4800             && (! reload_completed || frame_pointer_needed))
4801 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
4802       && ! (regno_first == HARD_FRAME_POINTER_REGNUM
4803             && (! reload_completed || frame_pointer_needed))
4804 #endif
4805 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
4806       && ! (regno_first == ARG_POINTER_REGNUM && fixed_regs[regno_first])
4807 #endif
4808       )
4809     {
4810       int some_was_live = 0, some_was_dead = 0;
4811
4812       for (i = regno_first; i <= regno_last; ++i)
4813         {
4814           int needed_regno = REGNO_REG_SET_P (pbi->reg_live, i);
4815           if (pbi->local_set)
4816             {
4817               /* Order of the set operation matters here since both
4818                  sets may be the same.  */
4819               CLEAR_REGNO_REG_SET (pbi->cond_local_set, i);
4820               if (cond != NULL_RTX
4821                   && ! REGNO_REG_SET_P (pbi->local_set, i))
4822                 SET_REGNO_REG_SET (pbi->cond_local_set, i);
4823               else
4824                 SET_REGNO_REG_SET (pbi->local_set, i);
4825             }
4826           if (code != CLOBBER)
4827             SET_REGNO_REG_SET (pbi->new_set, i);
4828
4829           some_was_live |= needed_regno;
4830           some_was_dead |= ! needed_regno;
4831         }
4832
4833 #ifdef HAVE_conditional_execution
4834       /* Consider conditional death in deciding that the register needs
4835          a death note.  */
4836       if (some_was_live && ! not_dead
4837           /* The stack pointer is never dead.  Well, not strictly true,
4838              but it's very difficult to tell from here.  Hopefully
4839              combine_stack_adjustments will fix up the most egregious
4840              errors.  */
4841           && regno_first != STACK_POINTER_REGNUM)
4842         {
4843           for (i = regno_first; i <= regno_last; ++i)
4844             if (! mark_regno_cond_dead (pbi, i, cond))
4845               not_dead = 1;
4846         }
4847 #endif
4848
4849       /* Additional data to record if this is the final pass.  */
4850       if (flags & (PROP_LOG_LINKS | PROP_REG_INFO
4851                    | PROP_DEATH_NOTES | PROP_AUTOINC))
4852         {
4853           register rtx y;
4854           register int blocknum = pbi->bb->index;
4855
4856           y = NULL_RTX;
4857           if (flags & (PROP_LOG_LINKS | PROP_AUTOINC))
4858             {
4859               y = pbi->reg_next_use[regno_first];
4860
4861               /* The next use is no longer next, since a store intervenes.  */
4862               for (i = regno_first; i <= regno_last; ++i)
4863                 pbi->reg_next_use[i] = 0;
4864             }
4865
4866           if (flags & PROP_REG_INFO)
4867             {
4868               for (i = regno_first; i <= regno_last; ++i)
4869                 {
4870                   /* Count (weighted) references, stores, etc.  This counts a
4871                      register twice if it is modified, but that is correct.  */
4872                   REG_N_SETS (i) += 1;
4873                   REG_N_REFS (i) += (optimize_size ? 1
4874                                      : pbi->bb->loop_depth + 1);
4875
4876                   /* The insns where a reg is live are normally counted
4877                      elsewhere, but we want the count to include the insn
4878                      where the reg is set, and the normal counting mechanism
4879                      would not count it.  */
4880                   REG_LIVE_LENGTH (i) += 1;
4881                 }
4882
4883               /* If this is a hard reg, record this function uses the reg.  */
4884               if (regno_first < FIRST_PSEUDO_REGISTER)
4885                 {
4886                   for (i = regno_first; i <= regno_last; i++)
4887                     regs_ever_live[i] = 1;
4888                 }
4889               else
4890                 {
4891                   /* Keep track of which basic blocks each reg appears in.  */
4892                   if (REG_BASIC_BLOCK (regno_first) == REG_BLOCK_UNKNOWN)
4893                     REG_BASIC_BLOCK (regno_first) = blocknum;
4894                   else if (REG_BASIC_BLOCK (regno_first) != blocknum)
4895                     REG_BASIC_BLOCK (regno_first) = REG_BLOCK_GLOBAL;
4896                 }
4897             }
4898
4899           if (! some_was_dead)
4900             {
4901               if (flags & PROP_LOG_LINKS)
4902                 {
4903                   /* Make a logical link from the next following insn
4904                      that uses this register, back to this insn.
4905                      The following insns have already been processed.
4906
4907                      We don't build a LOG_LINK for hard registers containing
4908                      in ASM_OPERANDs.  If these registers get replaced,
4909                      we might wind up changing the semantics of the insn,
4910                      even if reload can make what appear to be valid
4911                      assignments later.  */
4912                   if (y && (BLOCK_NUM (y) == blocknum)
4913                       && (regno_first >= FIRST_PSEUDO_REGISTER
4914                           || asm_noperands (PATTERN (y)) < 0))
4915                     LOG_LINKS (y) = alloc_INSN_LIST (insn, LOG_LINKS (y));
4916                 }
4917             }
4918           else if (not_dead)
4919             ;
4920           else if (! some_was_live)
4921             {
4922               if (flags & PROP_REG_INFO)
4923                 REG_N_DEATHS (regno_first) += 1;
4924
4925               if (flags & PROP_DEATH_NOTES)
4926                 {
4927                   /* Note that dead stores have already been deleted
4928                      when possible.  If we get here, we have found a
4929                      dead store that cannot be eliminated (because the
4930                      same insn does something useful).  Indicate this
4931                      by marking the reg being set as dying here.  */
4932                   REG_NOTES (insn)
4933                     = alloc_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn));
4934                 }
4935             }
4936           else
4937             {
4938               if (flags & PROP_DEATH_NOTES)
4939                 {
4940                   /* This is a case where we have a multi-word hard register
4941                      and some, but not all, of the words of the register are
4942                      needed in subsequent insns.  Write REG_UNUSED notes
4943                      for those parts that were not needed.  This case should
4944                      be rare.  */
4945
4946                   for (i = regno_first; i <= regno_last; ++i)
4947                     if (! REGNO_REG_SET_P (pbi->reg_live, i))
4948                       REG_NOTES (insn)
4949                         = alloc_EXPR_LIST (REG_UNUSED,
4950                                            gen_rtx_REG (reg_raw_mode[i], i),
4951                                            REG_NOTES (insn));
4952                 }
4953             }
4954         }
4955
4956       /* Mark the register as being dead.  */
4957       if (some_was_live
4958           && ! not_dead
4959           /* The stack pointer is never dead.  Well, not strictly true,
4960              but it's very difficult to tell from here.  Hopefully
4961              combine_stack_adjustments will fix up the most egregious
4962              errors.  */
4963           && regno_first != STACK_POINTER_REGNUM)
4964         {
4965           for (i = regno_first; i <= regno_last; ++i)
4966             CLEAR_REGNO_REG_SET (pbi->reg_live, i);
4967         }
4968     }
4969   else if (GET_CODE (reg) == REG)
4970     {
4971       if (flags & (PROP_LOG_LINKS | PROP_AUTOINC))
4972         pbi->reg_next_use[regno_first] = 0;
4973     }
4974
4975   /* If this is the last pass and this is a SCRATCH, show it will be dying
4976      here and count it.  */
4977   else if (GET_CODE (reg) == SCRATCH)
4978     {
4979       if (flags & PROP_DEATH_NOTES)
4980         REG_NOTES (insn)
4981           = alloc_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn));
4982     }
4983 }
4984 \f
4985 #ifdef HAVE_conditional_execution
4986 /* Mark REGNO conditionally dead.
4987    Return true if the register is now unconditionally dead.  */
4988
4989 static int
4990 mark_regno_cond_dead (pbi, regno, cond)
4991      struct propagate_block_info *pbi;
4992      int regno;
4993      rtx cond;
4994 {
4995   /* If this is a store to a predicate register, the value of the
4996      predicate is changing, we don't know that the predicate as seen
4997      before is the same as that seen after.  Flush all dependent
4998      conditions from reg_cond_dead.  This will make all such
4999      conditionally live registers unconditionally live.  */
5000   if (REGNO_REG_SET_P (pbi->reg_cond_reg, regno))
5001     flush_reg_cond_reg (pbi, regno);
5002
5003   /* If this is an unconditional store, remove any conditional
5004      life that may have existed.  */
5005   if (cond == NULL_RTX)
5006     splay_tree_remove (pbi->reg_cond_dead, regno);
5007   else
5008     {
5009       splay_tree_node node;
5010       struct reg_cond_life_info *rcli;
5011       rtx ncond;
5012
5013       /* Otherwise this is a conditional set.  Record that fact.
5014          It may have been conditionally used, or there may be a
5015          subsequent set with a complimentary condition.  */
5016
5017       node = splay_tree_lookup (pbi->reg_cond_dead, regno);
5018       if (node == NULL)
5019         {
5020           /* The register was unconditionally live previously.
5021              Record the current condition as the condition under
5022              which it is dead.  */
5023           rcli = (struct reg_cond_life_info *) xmalloc (sizeof (*rcli));
5024           rcli->condition = cond;
5025           splay_tree_insert (pbi->reg_cond_dead, regno,
5026                              (splay_tree_value) rcli);
5027
5028           SET_REGNO_REG_SET (pbi->reg_cond_reg, REGNO (XEXP (cond, 0)));
5029
5030           /* Not unconditionaly dead.  */
5031           return 0;
5032         }
5033       else
5034         {
5035           /* The register was conditionally live previously.
5036              Add the new condition to the old.  */
5037           rcli = (struct reg_cond_life_info *) node->value;
5038           ncond = rcli->condition;
5039           ncond = ior_reg_cond (ncond, cond, 1);
5040
5041           /* If the register is now unconditionally dead,
5042              remove the entry in the splay_tree.  */
5043           if (ncond == const1_rtx)
5044             splay_tree_remove (pbi->reg_cond_dead, regno);
5045           else
5046             {
5047               rcli->condition = ncond;
5048
5049               SET_REGNO_REG_SET (pbi->reg_cond_reg, REGNO (XEXP (cond, 0)));
5050
5051               /* Not unconditionaly dead.  */
5052               return 0;
5053             }
5054         }
5055     }
5056
5057   return 1;
5058 }
5059
5060 /* Called from splay_tree_delete for pbi->reg_cond_life.  */
5061
5062 static void
5063 free_reg_cond_life_info (value)
5064      splay_tree_value value;
5065 {
5066   struct reg_cond_life_info *rcli = (struct reg_cond_life_info *) value;
5067   free (rcli);
5068 }
5069
5070 /* Helper function for flush_reg_cond_reg.  */
5071
5072 static int
5073 flush_reg_cond_reg_1 (node, data)
5074      splay_tree_node node;
5075      void *data;
5076 {
5077   struct reg_cond_life_info *rcli;
5078   int *xdata = (int *) data;
5079   unsigned int regno = xdata[0];
5080
5081   /* Don't need to search if last flushed value was farther on in
5082      the in-order traversal.  */
5083   if (xdata[1] >= (int) node->key)
5084     return 0;
5085
5086   /* Splice out portions of the expression that refer to regno.  */
5087   rcli = (struct reg_cond_life_info *) node->value;
5088   rcli->condition = elim_reg_cond (rcli->condition, regno);
5089
5090   /* If the entire condition is now false, signal the node to be removed.  */
5091   if (rcli->condition == const0_rtx)
5092     {
5093       xdata[1] = node->key;
5094       return -1;
5095     }
5096   else if (rcli->condition == const1_rtx)
5097     abort ();
5098
5099   return 0;
5100 }
5101
5102 /* Flush all (sub) expressions referring to REGNO from REG_COND_LIVE.  */
5103
5104 static void
5105 flush_reg_cond_reg (pbi, regno)
5106      struct propagate_block_info *pbi;
5107      int regno;
5108 {
5109   int pair[2];
5110
5111   pair[0] = regno;
5112   pair[1] = -1;
5113   while (splay_tree_foreach (pbi->reg_cond_dead,
5114                              flush_reg_cond_reg_1, pair) == -1)
5115     splay_tree_remove (pbi->reg_cond_dead, pair[1]);
5116
5117   CLEAR_REGNO_REG_SET (pbi->reg_cond_reg, regno);
5118 }
5119
5120 /* Logical arithmetic on predicate conditions.  IOR, NOT and AND.
5121    For ior/and, the ADD flag determines whether we want to add the new
5122    condition X to the old one unconditionally.  If it is zero, we will
5123    only return a new expression if X allows us to simplify part of
5124    OLD, otherwise we return OLD unchanged to the caller.
5125    If ADD is nonzero, we will return a new condition in all cases.  The
5126    toplevel caller of one of these functions should always pass 1 for
5127    ADD.  */
5128
5129 static rtx
5130 ior_reg_cond (old, x, add)
5131      rtx old, x;
5132      int add;
5133 {
5134   rtx op0, op1;
5135
5136   if (GET_RTX_CLASS (GET_CODE (old)) == '<')
5137     {
5138       if (GET_RTX_CLASS (GET_CODE (x)) == '<'
5139           && GET_CODE (x) == reverse_condition (GET_CODE (old))
5140           && REGNO (XEXP (x, 0)) == REGNO (XEXP (old, 0)))
5141         return const1_rtx;
5142       if (GET_CODE (x) == GET_CODE (old)
5143           && REGNO (XEXP (x, 0)) == REGNO (XEXP (old, 0)))
5144         return old;
5145       if (! add)
5146         return old;
5147       return gen_rtx_IOR (0, old, x);
5148     }
5149
5150   switch (GET_CODE (old))
5151     {
5152     case IOR:
5153       op0 = ior_reg_cond (XEXP (old, 0), x, 0);
5154       op1 = ior_reg_cond (XEXP (old, 1), x, 0);
5155       if (op0 != XEXP (old, 0) || op1 != XEXP (old, 1))
5156         {
5157           if (op0 == const0_rtx)
5158             return op1;
5159           if (op1 == const0_rtx)
5160             return op0;
5161           if (op0 == const1_rtx || op1 == const1_rtx)
5162             return const1_rtx;
5163           if (op0 == XEXP (old, 0))
5164             op0 = gen_rtx_IOR (0, op0, x);
5165           else
5166             op1 = gen_rtx_IOR (0, op1, x);
5167           return gen_rtx_IOR (0, op0, op1);
5168         }
5169       if (! add)
5170         return old;
5171       return gen_rtx_IOR (0, old, x);
5172
5173     case AND:
5174       op0 = ior_reg_cond (XEXP (old, 0), x, 0);
5175       op1 = ior_reg_cond (XEXP (old, 1), x, 0);
5176       if (op0 != XEXP (old, 0) || op1 != XEXP (old, 1))
5177         {
5178           if (op0 == const1_rtx)
5179             return op1;
5180           if (op1 == const1_rtx)
5181             return op0;
5182           if (op0 == const0_rtx || op1 == const0_rtx)
5183             return const0_rtx;
5184           if (op0 == XEXP (old, 0))
5185             op0 = gen_rtx_IOR (0, op0, x);
5186           else
5187             op1 = gen_rtx_IOR (0, op1, x);
5188           return gen_rtx_AND (0, op0, op1);
5189         }
5190       if (! add)
5191         return old;
5192       return gen_rtx_IOR (0, old, x);
5193
5194     case NOT:
5195       op0 = and_reg_cond (XEXP (old, 0), not_reg_cond (x), 0);
5196       if (op0 != XEXP (old, 0))
5197         return not_reg_cond (op0);
5198       if (! add)
5199         return old;
5200       return gen_rtx_IOR (0, old, x);
5201
5202     default:
5203       abort ();
5204     }
5205 }
5206
5207 static rtx
5208 not_reg_cond (x)
5209      rtx x;
5210 {
5211   enum rtx_code x_code;
5212
5213   if (x == const0_rtx)
5214     return const1_rtx;
5215   else if (x == const1_rtx)
5216     return const0_rtx;
5217   x_code = GET_CODE (x);
5218   if (x_code == NOT)
5219     return XEXP (x, 0);
5220   if (GET_RTX_CLASS (x_code) == '<'
5221       && GET_CODE (XEXP (x, 0)) == REG)
5222     {
5223       if (XEXP (x, 1) != const0_rtx)
5224         abort ();
5225
5226       return gen_rtx_fmt_ee (reverse_condition (x_code),
5227                              VOIDmode, XEXP (x, 0), const0_rtx);
5228     }
5229   return gen_rtx_NOT (0, x);
5230 }
5231
5232 static rtx
5233 and_reg_cond (old, x, add)
5234      rtx old, x;
5235      int add;
5236 {
5237   rtx op0, op1;
5238
5239   if (GET_RTX_CLASS (GET_CODE (old)) == '<')
5240     {
5241       if (GET_RTX_CLASS (GET_CODE (x)) == '<'
5242           && GET_CODE (x) == reverse_condition (GET_CODE (old))
5243           && REGNO (XEXP (x, 0)) == REGNO (XEXP (old, 0)))
5244         return const0_rtx;
5245       if (GET_CODE (x) == GET_CODE (old)
5246           && REGNO (XEXP (x, 0)) == REGNO (XEXP (old, 0)))
5247         return old;
5248       if (! add)
5249         return old;
5250       return gen_rtx_AND (0, old, x);
5251     }
5252
5253   switch (GET_CODE (old))
5254     {
5255     case IOR:
5256       op0 = and_reg_cond (XEXP (old, 0), x, 0);
5257       op1 = and_reg_cond (XEXP (old, 1), x, 0);
5258       if (op0 != XEXP (old, 0) || op1 != XEXP (old, 1))
5259         {
5260           if (op0 == const0_rtx)
5261             return op1;
5262           if (op1 == const0_rtx)
5263             return op0;
5264           if (op0 == const1_rtx || op1 == const1_rtx)
5265             return const1_rtx;
5266           if (op0 == XEXP (old, 0))
5267             op0 = gen_rtx_AND (0, op0, x);
5268           else
5269             op1 = gen_rtx_AND (0, op1, x);
5270           return gen_rtx_IOR (0, op0, op1);
5271         }
5272       if (! add)
5273         return old;
5274       return gen_rtx_AND (0, old, x);
5275
5276     case AND:
5277       op0 = and_reg_cond (XEXP (old, 0), x, 0);
5278       op1 = and_reg_cond (XEXP (old, 1), x, 0);
5279       if (op0 != XEXP (old, 0) || op1 != XEXP (old, 1))
5280         {
5281           if (op0 == const1_rtx)
5282             return op1;
5283           if (op1 == const1_rtx)
5284             return op0;
5285           if (op0 == const0_rtx || op1 == const0_rtx)
5286             return const0_rtx;
5287           if (op0 == XEXP (old, 0))
5288             op0 = gen_rtx_AND (0, op0, x);
5289           else
5290             op1 = gen_rtx_AND (0, op1, x);
5291           return gen_rtx_AND (0, op0, op1);
5292         }
5293       if (! add)
5294         return old;
5295       return gen_rtx_AND (0, old, x);
5296
5297     case NOT:
5298       op0 = ior_reg_cond (XEXP (old, 0), not_reg_cond (x), 0);
5299       if (op0 != XEXP (old, 0))
5300         return not_reg_cond (op0);
5301       if (! add)
5302         return old;
5303       return gen_rtx_AND (0, old, x);
5304
5305     default:
5306       abort ();
5307     }
5308 }
5309
5310 /* Given a condition X, remove references to reg REGNO and return the
5311    new condition.  The removal will be done so that all conditions
5312    involving REGNO are considered to evaluate to false.  This function
5313    is used when the value of REGNO changes.  */
5314
5315 static rtx
5316 elim_reg_cond (x, regno)
5317      rtx x;
5318      unsigned int regno;
5319 {
5320   rtx op0, op1;
5321
5322   if (GET_RTX_CLASS (GET_CODE (x)) == '<')
5323     {
5324       if (REGNO (XEXP (x, 0)) == regno)
5325         return const0_rtx;
5326       return x;
5327     }
5328
5329   switch (GET_CODE (x))
5330     {
5331     case AND:
5332       op0 = elim_reg_cond (XEXP (x, 0), regno);
5333       op1 = elim_reg_cond (XEXP (x, 1), regno);
5334       if (op0 == const0_rtx || op1 == const0_rtx)
5335         return const0_rtx;
5336       if (op0 == const1_rtx)
5337         return op1;
5338       if (op1 == const1_rtx)
5339         return op0;
5340       if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
5341         return x;
5342       return gen_rtx_AND (0, op0, op1);
5343
5344     case IOR:
5345       op0 = elim_reg_cond (XEXP (x, 0), regno);
5346       op1 = elim_reg_cond (XEXP (x, 1), regno);
5347       if (op0 == const1_rtx || op1 == const1_rtx)
5348         return const1_rtx;
5349       if (op0 == const0_rtx)
5350         return op1;
5351       if (op1 == const0_rtx)
5352         return op0;
5353       if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
5354         return x;
5355       return gen_rtx_IOR (0, op0, op1);
5356
5357     case NOT:
5358       op0 = elim_reg_cond (XEXP (x, 0), regno);
5359       if (op0 == const0_rtx)
5360         return const1_rtx;
5361       if (op0 == const1_rtx)
5362         return const0_rtx;
5363       if (op0 != XEXP (x, 0))
5364         return not_reg_cond (op0);
5365       return x;
5366
5367     default:
5368       abort ();
5369     }
5370 }
5371 #endif /* HAVE_conditional_execution */
5372 \f
5373 #ifdef AUTO_INC_DEC
5374
5375 /* Try to substitute the auto-inc expression INC as the address inside
5376    MEM which occurs in INSN.  Currently, the address of MEM is an expression
5377    involving INCR_REG, and INCR is the next use of INCR_REG; it is an insn
5378    that has a single set whose source is a PLUS of INCR_REG and something
5379    else.  */
5380
5381 static void
5382 attempt_auto_inc (pbi, inc, insn, mem, incr, incr_reg)
5383      struct propagate_block_info *pbi;
5384      rtx inc, insn, mem, incr, incr_reg;
5385 {
5386   int regno = REGNO (incr_reg);
5387   rtx set = single_set (incr);
5388   rtx q = SET_DEST (set);
5389   rtx y = SET_SRC (set);
5390   int opnum = XEXP (y, 0) == incr_reg ? 0 : 1;
5391
5392   /* Make sure this reg appears only once in this insn.  */
5393   if (count_occurrences (PATTERN (insn), incr_reg, 1) != 1)
5394     return;
5395
5396   if (dead_or_set_p (incr, incr_reg)
5397       /* Mustn't autoinc an eliminable register.  */
5398       && (regno >= FIRST_PSEUDO_REGISTER
5399           || ! TEST_HARD_REG_BIT (elim_reg_set, regno)))
5400     {
5401       /* This is the simple case.  Try to make the auto-inc.  If
5402          we can't, we are done.  Otherwise, we will do any
5403          needed updates below.  */
5404       if (! validate_change (insn, &XEXP (mem, 0), inc, 0))
5405         return;
5406     }
5407   else if (GET_CODE (q) == REG
5408            /* PREV_INSN used here to check the semi-open interval
5409               [insn,incr).  */
5410            && ! reg_used_between_p (q,  PREV_INSN (insn), incr)
5411            /* We must also check for sets of q as q may be
5412               a call clobbered hard register and there may
5413               be a call between PREV_INSN (insn) and incr.  */
5414            && ! reg_set_between_p (q,  PREV_INSN (insn), incr))
5415     {
5416       /* We have *p followed sometime later by q = p+size.
5417          Both p and q must be live afterward,
5418          and q is not used between INSN and its assignment.
5419          Change it to q = p, ...*q..., q = q+size.
5420          Then fall into the usual case.  */
5421       rtx insns, temp;
5422
5423       start_sequence ();
5424       emit_move_insn (q, incr_reg);
5425       insns = get_insns ();
5426       end_sequence ();
5427
5428       if (basic_block_for_insn)
5429         for (temp = insns; temp; temp = NEXT_INSN (temp))
5430           set_block_for_insn (temp, pbi->bb);
5431
5432       /* If we can't make the auto-inc, or can't make the
5433          replacement into Y, exit.  There's no point in making
5434          the change below if we can't do the auto-inc and doing
5435          so is not correct in the pre-inc case.  */
5436
5437       XEXP (inc, 0) = q;
5438       validate_change (insn, &XEXP (mem, 0), inc, 1);
5439       validate_change (incr, &XEXP (y, opnum), q, 1);
5440       if (! apply_change_group ())
5441         return;
5442
5443       /* We now know we'll be doing this change, so emit the
5444          new insn(s) and do the updates.  */
5445       emit_insns_before (insns, insn);
5446
5447       if (pbi->bb->head == insn)
5448         pbi->bb->head = insns;
5449
5450       /* INCR will become a NOTE and INSN won't contain a
5451          use of INCR_REG.  If a use of INCR_REG was just placed in
5452          the insn before INSN, make that the next use.
5453          Otherwise, invalidate it.  */
5454       if (GET_CODE (PREV_INSN (insn)) == INSN
5455           && GET_CODE (PATTERN (PREV_INSN (insn))) == SET
5456           && SET_SRC (PATTERN (PREV_INSN (insn))) == incr_reg)
5457         pbi->reg_next_use[regno] = PREV_INSN (insn);
5458       else
5459         pbi->reg_next_use[regno] = 0;
5460
5461       incr_reg = q;
5462       regno = REGNO (q);
5463
5464       /* REGNO is now used in INCR which is below INSN, but
5465          it previously wasn't live here.  If we don't mark
5466          it as live, we'll put a REG_DEAD note for it
5467          on this insn, which is incorrect.  */
5468       SET_REGNO_REG_SET (pbi->reg_live, regno);
5469
5470       /* If there are any calls between INSN and INCR, show
5471          that REGNO now crosses them.  */
5472       for (temp = insn; temp != incr; temp = NEXT_INSN (temp))
5473         if (GET_CODE (temp) == CALL_INSN)
5474           REG_N_CALLS_CROSSED (regno)++;
5475     }
5476   else
5477     return;
5478
5479   /* If we haven't returned, it means we were able to make the
5480      auto-inc, so update the status.  First, record that this insn
5481      has an implicit side effect.  */
5482
5483   REG_NOTES (insn) = alloc_EXPR_LIST (REG_INC, incr_reg, REG_NOTES (insn));
5484
5485   /* Modify the old increment-insn to simply copy
5486      the already-incremented value of our register.  */
5487   if (! validate_change (incr, &SET_SRC (set), incr_reg, 0))
5488     abort ();
5489
5490   /* If that makes it a no-op (copying the register into itself) delete
5491      it so it won't appear to be a "use" and a "set" of this
5492      register.  */
5493   if (REGNO (SET_DEST (set)) == REGNO (incr_reg))
5494     {
5495       /* If the original source was dead, it's dead now.  */
5496       rtx note;
5497
5498       while ((note = find_reg_note (incr, REG_DEAD, NULL_RTX)) != NULL_RTX)
5499         {
5500           remove_note (incr, note);
5501           if (XEXP (note, 0) != incr_reg)
5502             CLEAR_REGNO_REG_SET (pbi->reg_live, REGNO (XEXP (note, 0)));
5503         }
5504
5505       PUT_CODE (incr, NOTE);
5506       NOTE_LINE_NUMBER (incr) = NOTE_INSN_DELETED;
5507       NOTE_SOURCE_FILE (incr) = 0;
5508     }
5509
5510   if (regno >= FIRST_PSEUDO_REGISTER)
5511     {
5512       /* Count an extra reference to the reg.  When a reg is
5513          incremented, spilling it is worse, so we want to make
5514          that less likely.  */
5515       REG_N_REFS (regno) += (optimize_size ? 1 : pbi->bb->loop_depth + 1);
5516
5517       /* Count the increment as a setting of the register,
5518          even though it isn't a SET in rtl.  */
5519       REG_N_SETS (regno)++;
5520     }
5521 }
5522
5523 /* X is a MEM found in INSN.  See if we can convert it into an auto-increment
5524    reference.  */
5525
5526 static void
5527 find_auto_inc (pbi, x, insn)
5528      struct propagate_block_info *pbi;
5529      rtx x;
5530      rtx insn;
5531 {
5532   rtx addr = XEXP (x, 0);
5533   HOST_WIDE_INT offset = 0;
5534   rtx set, y, incr, inc_val;
5535   int regno;
5536   int size = GET_MODE_SIZE (GET_MODE (x));
5537
5538   if (GET_CODE (insn) == JUMP_INSN)
5539     return;
5540
5541   /* Here we detect use of an index register which might be good for
5542      postincrement, postdecrement, preincrement, or predecrement.  */
5543
5544   if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
5545     offset = INTVAL (XEXP (addr, 1)), addr = XEXP (addr, 0);
5546
5547   if (GET_CODE (addr) != REG)
5548     return;
5549
5550   regno = REGNO (addr);
5551
5552   /* Is the next use an increment that might make auto-increment? */
5553   incr = pbi->reg_next_use[regno];
5554   if (incr == 0 || BLOCK_NUM (incr) != BLOCK_NUM (insn))
5555     return;
5556   set = single_set (incr);
5557   if (set == 0 || GET_CODE (set) != SET)
5558     return;
5559   y = SET_SRC (set);
5560
5561   if (GET_CODE (y) != PLUS)
5562     return;
5563
5564   if (REG_P (XEXP (y, 0)) && REGNO (XEXP (y, 0)) == REGNO (addr))
5565     inc_val = XEXP (y, 1);
5566   else if (REG_P (XEXP (y, 1)) && REGNO (XEXP (y, 1)) == REGNO (addr))
5567     inc_val = XEXP (y, 0);
5568   else
5569     return;
5570
5571   if (GET_CODE (inc_val) == CONST_INT)
5572     {
5573       if (HAVE_POST_INCREMENT
5574           && (INTVAL (inc_val) == size && offset == 0))
5575         attempt_auto_inc (pbi, gen_rtx_POST_INC (Pmode, addr), insn, x,
5576                           incr, addr);
5577       else if (HAVE_POST_DECREMENT
5578                && (INTVAL (inc_val) == -size && offset == 0))
5579         attempt_auto_inc (pbi, gen_rtx_POST_DEC (Pmode, addr), insn, x,
5580                           incr, addr);
5581       else if (HAVE_PRE_INCREMENT
5582                && (INTVAL (inc_val) == size && offset == size))
5583         attempt_auto_inc (pbi, gen_rtx_PRE_INC (Pmode, addr), insn, x,
5584                           incr, addr);
5585       else if (HAVE_PRE_DECREMENT
5586                && (INTVAL (inc_val) == -size && offset == -size))
5587         attempt_auto_inc (pbi, gen_rtx_PRE_DEC (Pmode, addr), insn, x,
5588                           incr, addr);
5589       else if (HAVE_POST_MODIFY_DISP && offset == 0)
5590         attempt_auto_inc (pbi, gen_rtx_POST_MODIFY (Pmode, addr,
5591                                                     gen_rtx_PLUS (Pmode,
5592                                                                   addr,
5593                                                                   inc_val)),
5594                           insn, x, incr, addr);
5595     }
5596   else if (GET_CODE (inc_val) == REG
5597            && ! reg_set_between_p (inc_val, PREV_INSN (insn),
5598                                    NEXT_INSN (incr)))
5599
5600     {
5601       if (HAVE_POST_MODIFY_REG && offset == 0)
5602         attempt_auto_inc (pbi, gen_rtx_POST_MODIFY (Pmode, addr,
5603                                                     gen_rtx_PLUS (Pmode,
5604                                                                   addr,
5605                                                                   inc_val)),
5606                           insn, x, incr, addr);
5607     }
5608 }
5609
5610 #endif /* AUTO_INC_DEC */
5611 \f
5612 static void
5613 mark_used_reg (pbi, reg, cond, insn)
5614      struct propagate_block_info *pbi;
5615      rtx reg;
5616      rtx cond ATTRIBUTE_UNUSED;
5617      rtx insn;
5618 {
5619   int regno = REGNO (reg);
5620   int some_was_live = REGNO_REG_SET_P (pbi->reg_live, regno);
5621   int some_was_dead = ! some_was_live;
5622   int some_not_set;
5623   int n;
5624
5625   /* A hard reg in a wide mode may really be multiple registers.
5626      If so, mark all of them just like the first.  */
5627   if (regno < FIRST_PSEUDO_REGISTER)
5628     {
5629       n = HARD_REGNO_NREGS (regno, GET_MODE (reg));
5630       while (--n > 0)
5631         {
5632           int needed_regno = REGNO_REG_SET_P (pbi->reg_live, regno + n);
5633           some_was_live |= needed_regno;
5634           some_was_dead |= ! needed_regno;
5635         }
5636     }
5637
5638   if (pbi->flags & (PROP_LOG_LINKS | PROP_AUTOINC))
5639     {
5640       /* Record where each reg is used, so when the reg is set we know
5641          the next insn that uses it.  */
5642       pbi->reg_next_use[regno] = insn;
5643     }
5644
5645   if (pbi->flags & PROP_REG_INFO)
5646     {
5647       if (regno < FIRST_PSEUDO_REGISTER)
5648         {
5649           /* If this is a register we are going to try to eliminate,
5650              don't mark it live here.  If we are successful in
5651              eliminating it, it need not be live unless it is used for
5652              pseudos, in which case it will have been set live when it
5653              was allocated to the pseudos.  If the register will not
5654              be eliminated, reload will set it live at that point.
5655
5656              Otherwise, record that this function uses this register.  */
5657           /* ??? The PPC backend tries to "eliminate" on the pic
5658              register to itself.  This should be fixed.  In the mean
5659              time, hack around it.  */
5660
5661           if (! (TEST_HARD_REG_BIT (elim_reg_set, regno)
5662                  && (regno == FRAME_POINTER_REGNUM
5663                      || regno == ARG_POINTER_REGNUM)))
5664             {
5665               int n = HARD_REGNO_NREGS (regno, GET_MODE (reg));
5666               do
5667                 regs_ever_live[regno + --n] = 1;
5668               while (n > 0);
5669             }
5670         }
5671       else
5672         {
5673           /* Keep track of which basic block each reg appears in.  */
5674
5675           register int blocknum = pbi->bb->index;
5676           if (REG_BASIC_BLOCK (regno) == REG_BLOCK_UNKNOWN)
5677             REG_BASIC_BLOCK (regno) = blocknum;
5678           else if (REG_BASIC_BLOCK (regno) != blocknum)
5679             REG_BASIC_BLOCK (regno) = REG_BLOCK_GLOBAL;
5680
5681           /* Count (weighted) number of uses of each reg.  */
5682           REG_N_REFS (regno) += (optimize_size ? 1
5683                                  : pbi->bb->loop_depth + 1);
5684         }
5685     }
5686
5687   /* Find out if any of the register was set this insn.  */
5688   some_not_set = ! REGNO_REG_SET_P (pbi->new_set, regno);
5689   if (regno < FIRST_PSEUDO_REGISTER)
5690     {
5691       n = HARD_REGNO_NREGS (regno, GET_MODE (reg));
5692       while (--n > 0)
5693         some_not_set |= ! REGNO_REG_SET_P (pbi->new_set, regno + n);
5694     }
5695
5696   /* Record and count the insns in which a reg dies.  If it is used in
5697      this insn and was dead below the insn then it dies in this insn.
5698      If it was set in this insn, we do not make a REG_DEAD note;
5699      likewise if we already made such a note.  */
5700   if ((pbi->flags & (PROP_DEATH_NOTES | PROP_REG_INFO))
5701       && some_was_dead
5702       && some_not_set)
5703     {
5704       /* Check for the case where the register dying partially
5705          overlaps the register set by this insn.  */
5706       if (regno < FIRST_PSEUDO_REGISTER
5707           && HARD_REGNO_NREGS (regno, GET_MODE (reg)) > 1)
5708         {
5709           n = HARD_REGNO_NREGS (regno, GET_MODE (reg));
5710           while (--n >= 0)
5711             some_was_live |= REGNO_REG_SET_P (pbi->new_set, regno + n);
5712         }
5713
5714       /* If none of the words in X is needed, make a REG_DEAD note.
5715          Otherwise, we must make partial REG_DEAD notes.  */
5716       if (! some_was_live)
5717         {
5718           if ((pbi->flags & PROP_DEATH_NOTES)
5719               && ! find_regno_note (insn, REG_DEAD, regno))
5720             REG_NOTES (insn)
5721               = alloc_EXPR_LIST (REG_DEAD, reg, REG_NOTES (insn));
5722
5723           if (pbi->flags & PROP_REG_INFO)
5724             REG_N_DEATHS (regno)++;
5725         }
5726       else
5727         {
5728           /* Don't make a REG_DEAD note for a part of a register
5729              that is set in the insn.  */
5730
5731           n = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg)) - 1;
5732           for (; n >= regno; n--)
5733             if (! REGNO_REG_SET_P (pbi->reg_live, n)
5734                 && ! dead_or_set_regno_p (insn, n))
5735               REG_NOTES (insn)
5736                 = alloc_EXPR_LIST (REG_DEAD,
5737                                    gen_rtx_REG (reg_raw_mode[n], n),
5738                                    REG_NOTES (insn));
5739         }
5740     }
5741
5742   SET_REGNO_REG_SET (pbi->reg_live, regno);
5743   if (regno < FIRST_PSEUDO_REGISTER)
5744     {
5745       n = HARD_REGNO_NREGS (regno, GET_MODE (reg));
5746       while (--n > 0)
5747         SET_REGNO_REG_SET (pbi->reg_live, regno + n);
5748     }
5749
5750 #ifdef HAVE_conditional_execution
5751   /* If this is a conditional use, record that fact.  If it is later
5752      conditionally set, we'll know to kill the register.  */
5753   if (cond != NULL_RTX)
5754     {
5755       splay_tree_node node;
5756       struct reg_cond_life_info *rcli;
5757       rtx ncond;
5758
5759       if (some_was_live)
5760         {
5761           node = splay_tree_lookup (pbi->reg_cond_dead, regno);
5762           if (node == NULL)
5763             {
5764               /* The register was unconditionally live previously.
5765                  No need to do anything.  */
5766             }
5767           else
5768             {
5769               /* The register was conditionally live previously.
5770                  Subtract the new life cond from the old death cond.  */
5771               rcli = (struct reg_cond_life_info *) node->value;
5772               ncond = rcli->condition;
5773               ncond = and_reg_cond (ncond, not_reg_cond (cond), 1);
5774
5775               /* If the register is now unconditionally live, remove the
5776                  entry in the splay_tree.  */
5777               if (ncond == const0_rtx)
5778                 {
5779                   rcli->condition = NULL_RTX;
5780                   splay_tree_remove (pbi->reg_cond_dead, regno);
5781                 }
5782               else
5783                 {
5784                   rcli->condition = ncond;
5785                   SET_REGNO_REG_SET (pbi->reg_cond_reg, REGNO (XEXP (cond, 0)));
5786                 }
5787             }
5788         }
5789       else
5790         {
5791           /* The register was not previously live at all.  Record
5792              the condition under which it is still dead.  */
5793           rcli = (struct reg_cond_life_info *) xmalloc (sizeof (*rcli));
5794           rcli->condition = not_reg_cond (cond);
5795           splay_tree_insert (pbi->reg_cond_dead, regno,
5796                              (splay_tree_value) rcli);
5797
5798           SET_REGNO_REG_SET (pbi->reg_cond_reg, REGNO (XEXP (cond, 0)));
5799         }
5800     }
5801   else if (some_was_live)
5802     {
5803       splay_tree_node node;
5804       struct reg_cond_life_info *rcli;
5805
5806       node = splay_tree_lookup (pbi->reg_cond_dead, regno);
5807       if (node != NULL)
5808         {
5809           /* The register was conditionally live previously, but is now
5810              unconditionally so.  Remove it from the conditionally dead
5811              list, so that a conditional set won't cause us to think
5812              it dead.  */
5813           rcli = (struct reg_cond_life_info *) node->value;
5814           rcli->condition = NULL_RTX;
5815           splay_tree_remove (pbi->reg_cond_dead, regno);
5816         }
5817     }
5818
5819 #endif
5820 }
5821
5822 /* Scan expression X and store a 1-bit in NEW_LIVE for each reg it uses.
5823    This is done assuming the registers needed from X are those that
5824    have 1-bits in PBI->REG_LIVE.
5825
5826    INSN is the containing instruction.  If INSN is dead, this function
5827    is not called.  */
5828
5829 static void
5830 mark_used_regs (pbi, x, cond, insn)
5831      struct propagate_block_info *pbi;
5832      rtx x, cond, insn;
5833 {
5834   register RTX_CODE code;
5835   register int regno;
5836   int flags = pbi->flags;
5837
5838  retry:
5839   code = GET_CODE (x);
5840   switch (code)
5841     {
5842     case LABEL_REF:
5843     case SYMBOL_REF:
5844     case CONST_INT:
5845     case CONST:
5846     case CONST_DOUBLE:
5847     case PC:
5848     case ADDR_VEC:
5849     case ADDR_DIFF_VEC:
5850       return;
5851
5852 #ifdef HAVE_cc0
5853     case CC0:
5854       pbi->cc0_live = 1;
5855       return;
5856 #endif
5857
5858     case CLOBBER:
5859       /* If we are clobbering a MEM, mark any registers inside the address
5860          as being used.  */
5861       if (GET_CODE (XEXP (x, 0)) == MEM)
5862         mark_used_regs (pbi, XEXP (XEXP (x, 0), 0), cond, insn);
5863       return;
5864
5865     case MEM:
5866       /* Don't bother watching stores to mems if this is not the
5867          final pass.  We'll not be deleting dead stores this round.  */
5868       if (optimize && (flags & PROP_SCAN_DEAD_CODE))
5869         {
5870           /* Invalidate the data for the last MEM stored, but only if MEM is
5871              something that can be stored into.  */
5872           if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
5873               && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
5874             /* Needn't clear the memory set list.  */
5875             ;
5876           else
5877             {
5878               rtx temp = pbi->mem_set_list;
5879               rtx prev = NULL_RTX;
5880               rtx next;
5881
5882               while (temp)
5883                 {
5884                   next = XEXP (temp, 1);
5885                   if (anti_dependence (XEXP (temp, 0), x))
5886                     {
5887                       /* Splice temp out of the list.  */
5888                       if (prev)
5889                         XEXP (prev, 1) = next;
5890                       else
5891                         pbi->mem_set_list = next;
5892                       free_EXPR_LIST_node (temp);
5893                       pbi->mem_set_list_len--;
5894                     }
5895                   else
5896                     prev = temp;
5897                   temp = next;
5898                 }
5899             }
5900
5901           /* If the memory reference had embedded side effects (autoincrement
5902              address modes.  Then we may need to kill some entries on the
5903              memory set list.  */
5904           if (insn)
5905             invalidate_mems_from_autoinc (pbi, insn);
5906         }
5907
5908 #ifdef AUTO_INC_DEC
5909       if (flags & PROP_AUTOINC)
5910         find_auto_inc (pbi, x, insn);
5911 #endif
5912       break;
5913
5914     case SUBREG:
5915 #ifdef CLASS_CANNOT_CHANGE_MODE
5916       if (GET_CODE (SUBREG_REG (x)) == REG
5917           && REGNO (SUBREG_REG (x)) >= FIRST_PSEUDO_REGISTER
5918           && CLASS_CANNOT_CHANGE_MODE_P (GET_MODE (x),
5919                                          GET_MODE (SUBREG_REG (x))))
5920         REG_CHANGES_MODE (REGNO (SUBREG_REG (x))) = 1;
5921 #endif
5922
5923       /* While we're here, optimize this case.  */
5924       x = SUBREG_REG (x);
5925       if (GET_CODE (x) != REG)
5926         goto retry;
5927       /* Fall through.  */
5928
5929     case REG:
5930       /* See a register other than being set => mark it as needed.  */
5931       mark_used_reg (pbi, x, cond, insn);
5932       return;
5933
5934     case SET:
5935       {
5936         register rtx testreg = SET_DEST (x);
5937         int mark_dest = 0;
5938
5939         /* If storing into MEM, don't show it as being used.  But do
5940            show the address as being used.  */
5941         if (GET_CODE (testreg) == MEM)
5942           {
5943 #ifdef AUTO_INC_DEC
5944             if (flags & PROP_AUTOINC)
5945               find_auto_inc (pbi, testreg, insn);
5946 #endif
5947             mark_used_regs (pbi, XEXP (testreg, 0), cond, insn);
5948             mark_used_regs (pbi, SET_SRC (x), cond, insn);
5949             return;
5950           }
5951
5952         /* Storing in STRICT_LOW_PART is like storing in a reg
5953            in that this SET might be dead, so ignore it in TESTREG.
5954            but in some other ways it is like using the reg.
5955
5956            Storing in a SUBREG or a bit field is like storing the entire
5957            register in that if the register's value is not used
5958            then this SET is not needed.  */
5959         while (GET_CODE (testreg) == STRICT_LOW_PART
5960                || GET_CODE (testreg) == ZERO_EXTRACT
5961                || GET_CODE (testreg) == SIGN_EXTRACT
5962                || GET_CODE (testreg) == SUBREG)
5963           {
5964 #ifdef CLASS_CANNOT_CHANGE_MODE
5965             if (GET_CODE (testreg) == SUBREG
5966                 && GET_CODE (SUBREG_REG (testreg)) == REG
5967                 && REGNO (SUBREG_REG (testreg)) >= FIRST_PSEUDO_REGISTER
5968                 && CLASS_CANNOT_CHANGE_MODE_P (GET_MODE (SUBREG_REG (testreg)),
5969                                                GET_MODE (testreg)))
5970               REG_CHANGES_MODE (REGNO (SUBREG_REG (testreg))) = 1;
5971 #endif
5972
5973             /* Modifying a single register in an alternate mode
5974                does not use any of the old value.  But these other
5975                ways of storing in a register do use the old value.  */
5976             if (GET_CODE (testreg) == SUBREG
5977                 && !(REG_SIZE (SUBREG_REG (testreg)) > REG_SIZE (testreg)))
5978               ;
5979             else
5980               mark_dest = 1;
5981
5982             testreg = XEXP (testreg, 0);
5983           }
5984
5985         /* If this is a store into a register or group of registers,
5986            recursively scan the value being stored.  */
5987
5988         if ((GET_CODE (testreg) == PARALLEL
5989              && GET_MODE (testreg) == BLKmode)
5990             || (GET_CODE (testreg) == REG
5991                 && (regno = REGNO (testreg),
5992                     ! (regno == FRAME_POINTER_REGNUM
5993                        && (! reload_completed || frame_pointer_needed)))
5994 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
5995                 && ! (regno == HARD_FRAME_POINTER_REGNUM
5996                       && (! reload_completed || frame_pointer_needed))
5997 #endif
5998 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
5999                 && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
6000 #endif
6001                 ))
6002           {
6003             if (mark_dest)
6004               mark_used_regs (pbi, SET_DEST (x), cond, insn);
6005             mark_used_regs (pbi, SET_SRC (x), cond, insn);
6006             return;
6007           }
6008       }
6009       break;
6010
6011     case ASM_OPERANDS:
6012     case UNSPEC_VOLATILE:
6013     case TRAP_IF:
6014     case ASM_INPUT:
6015       {
6016         /* Traditional and volatile asm instructions must be considered to use
6017            and clobber all hard registers, all pseudo-registers and all of
6018            memory.  So must TRAP_IF and UNSPEC_VOLATILE operations.
6019
6020            Consider for instance a volatile asm that changes the fpu rounding
6021            mode.  An insn should not be moved across this even if it only uses
6022            pseudo-regs because it might give an incorrectly rounded result.
6023
6024            ?!? Unfortunately, marking all hard registers as live causes massive
6025            problems for the register allocator and marking all pseudos as live
6026            creates mountains of uninitialized variable warnings.
6027
6028            So for now, just clear the memory set list and mark any regs
6029            we can find in ASM_OPERANDS as used.  */
6030         if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
6031           {
6032             free_EXPR_LIST_list (&pbi->mem_set_list);
6033             pbi->mem_set_list_len = 0;
6034           }
6035
6036         /* For all ASM_OPERANDS, we must traverse the vector of input operands.
6037            We can not just fall through here since then we would be confused
6038            by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
6039            traditional asms unlike their normal usage.  */
6040         if (code == ASM_OPERANDS)
6041           {
6042             int j;
6043
6044             for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
6045               mark_used_regs (pbi, ASM_OPERANDS_INPUT (x, j), cond, insn);
6046           }
6047         break;
6048       }
6049
6050     case COND_EXEC:
6051       if (cond != NULL_RTX)
6052         abort ();
6053
6054       mark_used_regs (pbi, COND_EXEC_TEST (x), NULL_RTX, insn);
6055
6056       cond = COND_EXEC_TEST (x);
6057       x = COND_EXEC_CODE (x);
6058       goto retry;
6059
6060     case PHI:
6061       /* We _do_not_ want to scan operands of phi nodes.  Operands of
6062          a phi function are evaluated only when control reaches this
6063          block along a particular edge.  Therefore, regs that appear
6064          as arguments to phi should not be added to the global live at
6065          start.  */
6066       return;
6067
6068     default:
6069       break;
6070     }
6071
6072   /* Recursively scan the operands of this expression.  */
6073
6074   {
6075     register const char *fmt = GET_RTX_FORMAT (code);
6076     register int i;
6077
6078     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6079       {
6080         if (fmt[i] == 'e')
6081           {
6082             /* Tail recursive case: save a function call level.  */
6083             if (i == 0)
6084               {
6085                 x = XEXP (x, 0);
6086                 goto retry;
6087               }
6088             mark_used_regs (pbi, XEXP (x, i), cond, insn);
6089           }
6090         else if (fmt[i] == 'E')
6091           {
6092             register int j;
6093             for (j = 0; j < XVECLEN (x, i); j++)
6094               mark_used_regs (pbi, XVECEXP (x, i, j), cond, insn);
6095           }
6096       }
6097   }
6098 }
6099 \f
6100 #ifdef AUTO_INC_DEC
6101
6102 static int
6103 try_pre_increment_1 (pbi, insn)
6104      struct propagate_block_info *pbi;
6105      rtx insn;
6106 {
6107   /* Find the next use of this reg.  If in same basic block,
6108      make it do pre-increment or pre-decrement if appropriate.  */
6109   rtx x = single_set (insn);
6110   HOST_WIDE_INT amount = ((GET_CODE (SET_SRC (x)) == PLUS ? 1 : -1)
6111                           * INTVAL (XEXP (SET_SRC (x), 1)));
6112   int regno = REGNO (SET_DEST (x));
6113   rtx y = pbi->reg_next_use[regno];
6114   if (y != 0
6115       && SET_DEST (x) != stack_pointer_rtx
6116       && BLOCK_NUM (y) == BLOCK_NUM (insn)
6117       /* Don't do this if the reg dies, or gets set in y; a standard addressing
6118          mode would be better.  */
6119       && ! dead_or_set_p (y, SET_DEST (x))
6120       && try_pre_increment (y, SET_DEST (x), amount))
6121     {
6122       /* We have found a suitable auto-increment and already changed
6123          insn Y to do it.  So flush this increment instruction.  */
6124       propagate_block_delete_insn (pbi->bb, insn);
6125
6126       /* Count a reference to this reg for the increment insn we are
6127          deleting.  When a reg is incremented, spilling it is worse,
6128          so we want to make that less likely.  */
6129       if (regno >= FIRST_PSEUDO_REGISTER)
6130         {
6131           REG_N_REFS (regno) += (optimize_size ? 1
6132                                  : pbi->bb->loop_depth + 1);
6133           REG_N_SETS (regno)++;
6134         }
6135
6136       /* Flush any remembered memories depending on the value of
6137          the incremented register.  */
6138       invalidate_mems_from_set (pbi, SET_DEST (x));
6139
6140       return 1;
6141     }
6142   return 0;
6143 }
6144
6145 /* Try to change INSN so that it does pre-increment or pre-decrement
6146    addressing on register REG in order to add AMOUNT to REG.
6147    AMOUNT is negative for pre-decrement.
6148    Returns 1 if the change could be made.
6149    This checks all about the validity of the result of modifying INSN.  */
6150
6151 static int
6152 try_pre_increment (insn, reg, amount)
6153      rtx insn, reg;
6154      HOST_WIDE_INT amount;
6155 {
6156   register rtx use;
6157
6158   /* Nonzero if we can try to make a pre-increment or pre-decrement.
6159      For example, addl $4,r1; movl (r1),... can become movl +(r1),...  */
6160   int pre_ok = 0;
6161   /* Nonzero if we can try to make a post-increment or post-decrement.
6162      For example, addl $4,r1; movl -4(r1),... can become movl (r1)+,...
6163      It is possible for both PRE_OK and POST_OK to be nonzero if the machine
6164      supports both pre-inc and post-inc, or both pre-dec and post-dec.  */
6165   int post_ok = 0;
6166
6167   /* Nonzero if the opportunity actually requires post-inc or post-dec.  */
6168   int do_post = 0;
6169
6170   /* From the sign of increment, see which possibilities are conceivable
6171      on this target machine.  */
6172   if (HAVE_PRE_INCREMENT && amount > 0)
6173     pre_ok = 1;
6174   if (HAVE_POST_INCREMENT && amount > 0)
6175     post_ok = 1;
6176
6177   if (HAVE_PRE_DECREMENT && amount < 0)
6178     pre_ok = 1;
6179   if (HAVE_POST_DECREMENT && amount < 0)
6180     post_ok = 1;
6181
6182   if (! (pre_ok || post_ok))
6183     return 0;
6184
6185   /* It is not safe to add a side effect to a jump insn
6186      because if the incremented register is spilled and must be reloaded
6187      there would be no way to store the incremented value back in memory.  */
6188
6189   if (GET_CODE (insn) == JUMP_INSN)
6190     return 0;
6191
6192   use = 0;
6193   if (pre_ok)
6194     use = find_use_as_address (PATTERN (insn), reg, 0);
6195   if (post_ok && (use == 0 || use == (rtx) 1))
6196     {
6197       use = find_use_as_address (PATTERN (insn), reg, -amount);
6198       do_post = 1;
6199     }
6200
6201   if (use == 0 || use == (rtx) 1)
6202     return 0;
6203
6204   if (GET_MODE_SIZE (GET_MODE (use)) != (amount > 0 ? amount : - amount))
6205     return 0;
6206
6207   /* See if this combination of instruction and addressing mode exists.  */
6208   if (! validate_change (insn, &XEXP (use, 0),
6209                          gen_rtx_fmt_e (amount > 0
6210                                         ? (do_post ? POST_INC : PRE_INC)
6211                                         : (do_post ? POST_DEC : PRE_DEC),
6212                                         Pmode, reg), 0))
6213     return 0;
6214
6215   /* Record that this insn now has an implicit side effect on X.  */
6216   REG_NOTES (insn) = alloc_EXPR_LIST (REG_INC, reg, REG_NOTES (insn));
6217   return 1;
6218 }
6219
6220 #endif /* AUTO_INC_DEC */
6221 \f
6222 /* Find the place in the rtx X where REG is used as a memory address.
6223    Return the MEM rtx that so uses it.
6224    If PLUSCONST is nonzero, search instead for a memory address equivalent to
6225    (plus REG (const_int PLUSCONST)).
6226
6227    If such an address does not appear, return 0.
6228    If REG appears more than once, or is used other than in such an address,
6229    return (rtx)1.  */
6230
6231 rtx
6232 find_use_as_address (x, reg, plusconst)
6233      register rtx x;
6234      rtx reg;
6235      HOST_WIDE_INT plusconst;
6236 {
6237   enum rtx_code code = GET_CODE (x);
6238   const char *fmt = GET_RTX_FORMAT (code);
6239   register int i;
6240   register rtx value = 0;
6241   register rtx tem;
6242
6243   if (code == MEM && XEXP (x, 0) == reg && plusconst == 0)
6244     return x;
6245
6246   if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
6247       && XEXP (XEXP (x, 0), 0) == reg
6248       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6249       && INTVAL (XEXP (XEXP (x, 0), 1)) == plusconst)
6250     return x;
6251
6252   if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
6253     {
6254       /* If REG occurs inside a MEM used in a bit-field reference,
6255          that is unacceptable.  */
6256       if (find_use_as_address (XEXP (x, 0), reg, 0) != 0)
6257         return (rtx) (HOST_WIDE_INT) 1;
6258     }
6259
6260   if (x == reg)
6261     return (rtx) (HOST_WIDE_INT) 1;
6262
6263   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6264     {
6265       if (fmt[i] == 'e')
6266         {
6267           tem = find_use_as_address (XEXP (x, i), reg, plusconst);
6268           if (value == 0)
6269             value = tem;
6270           else if (tem != 0)
6271             return (rtx) (HOST_WIDE_INT) 1;
6272         }
6273       else if (fmt[i] == 'E')
6274         {
6275           register int j;
6276           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6277             {
6278               tem = find_use_as_address (XVECEXP (x, i, j), reg, plusconst);
6279               if (value == 0)
6280                 value = tem;
6281               else if (tem != 0)
6282                 return (rtx) (HOST_WIDE_INT) 1;
6283             }
6284         }
6285     }
6286
6287   return value;
6288 }
6289 \f
6290 /* Write information about registers and basic blocks into FILE.
6291    This is part of making a debugging dump.  */
6292
6293 void
6294 dump_regset (r, outf)
6295      regset r;
6296      FILE *outf;
6297 {
6298   int i;
6299   if (r == NULL)
6300     {
6301       fputs (" (nil)", outf);
6302       return;
6303     }
6304
6305   EXECUTE_IF_SET_IN_REG_SET (r, 0, i,
6306     {
6307       fprintf (outf, " %d", i);
6308       if (i < FIRST_PSEUDO_REGISTER)
6309         fprintf (outf, " [%s]",
6310                  reg_names[i]);
6311     });
6312 }
6313
6314 void
6315 debug_regset (r)
6316      regset r;
6317 {
6318   dump_regset (r, stderr);
6319   putc ('\n', stderr);
6320 }
6321
6322 void
6323 dump_flow_info (file)
6324      FILE *file;
6325 {
6326   register int i;
6327   static const char * const reg_class_names[] = REG_CLASS_NAMES;
6328
6329   fprintf (file, "%d registers.\n", max_regno);
6330   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
6331     if (REG_N_REFS (i))
6332       {
6333         enum reg_class class, altclass;
6334         fprintf (file, "\nRegister %d used %d times across %d insns",
6335                  i, REG_N_REFS (i), REG_LIVE_LENGTH (i));
6336         if (REG_BASIC_BLOCK (i) >= 0)
6337           fprintf (file, " in block %d", REG_BASIC_BLOCK (i));
6338         if (REG_N_SETS (i))
6339           fprintf (file, "; set %d time%s", REG_N_SETS (i),
6340                    (REG_N_SETS (i) == 1) ? "" : "s");
6341         if (REG_USERVAR_P (regno_reg_rtx[i]))
6342           fprintf (file, "; user var");
6343         if (REG_N_DEATHS (i) != 1)
6344           fprintf (file, "; dies in %d places", REG_N_DEATHS (i));
6345         if (REG_N_CALLS_CROSSED (i) == 1)
6346           fprintf (file, "; crosses 1 call");
6347         else if (REG_N_CALLS_CROSSED (i))
6348           fprintf (file, "; crosses %d calls", REG_N_CALLS_CROSSED (i));
6349         if (PSEUDO_REGNO_BYTES (i) != UNITS_PER_WORD)
6350           fprintf (file, "; %d bytes", PSEUDO_REGNO_BYTES (i));
6351         class = reg_preferred_class (i);
6352         altclass = reg_alternate_class (i);
6353         if (class != GENERAL_REGS || altclass != ALL_REGS)
6354           {
6355             if (altclass == ALL_REGS || class == ALL_REGS)
6356               fprintf (file, "; pref %s", reg_class_names[(int) class]);
6357             else if (altclass == NO_REGS)
6358               fprintf (file, "; %s or none", reg_class_names[(int) class]);
6359             else
6360               fprintf (file, "; pref %s, else %s",
6361                        reg_class_names[(int) class],
6362                        reg_class_names[(int) altclass]);
6363           }
6364         if (REG_POINTER (regno_reg_rtx[i]))
6365           fprintf (file, "; pointer");
6366         fprintf (file, ".\n");
6367       }
6368
6369   fprintf (file, "\n%d basic blocks, %d edges.\n", n_basic_blocks, n_edges);
6370   for (i = 0; i < n_basic_blocks; i++)
6371     {
6372       register basic_block bb = BASIC_BLOCK (i);
6373       register edge e;
6374
6375       fprintf (file, "\nBasic block %d: first insn %d, last %d, loop_depth %d, count %d.\n",
6376                i, INSN_UID (bb->head), INSN_UID (bb->end), bb->loop_depth, bb->count);
6377
6378       fprintf (file, "Predecessors: ");
6379       for (e = bb->pred; e; e = e->pred_next)
6380         dump_edge_info (file, e, 0);
6381
6382       fprintf (file, "\nSuccessors: ");
6383       for (e = bb->succ; e; e = e->succ_next)
6384         dump_edge_info (file, e, 1);
6385
6386       fprintf (file, "\nRegisters live at start:");
6387       dump_regset (bb->global_live_at_start, file);
6388
6389       fprintf (file, "\nRegisters live at end:");
6390       dump_regset (bb->global_live_at_end, file);
6391
6392       putc ('\n', file);
6393     }
6394
6395   putc ('\n', file);
6396 }
6397
6398 void
6399 debug_flow_info ()
6400 {
6401   dump_flow_info (stderr);
6402 }
6403
6404 static void
6405 dump_edge_info (file, e, do_succ)
6406      FILE *file;
6407      edge e;
6408      int do_succ;
6409 {
6410   basic_block side = (do_succ ? e->dest : e->src);
6411
6412   if (side == ENTRY_BLOCK_PTR)
6413     fputs (" ENTRY", file);
6414   else if (side == EXIT_BLOCK_PTR)
6415     fputs (" EXIT", file);
6416   else
6417     fprintf (file, " %d", side->index);
6418
6419   if (e->count)
6420     fprintf (file, " count:%d", e->count);
6421
6422   if (e->flags)
6423     {
6424       static const char * const bitnames[] = {
6425         "fallthru", "crit", "ab", "abcall", "eh", "fake"
6426       };
6427       int comma = 0;
6428       int i, flags = e->flags;
6429
6430       fputc (' ', file);
6431       fputc ('(', file);
6432       for (i = 0; flags; i++)
6433         if (flags & (1 << i))
6434           {
6435             flags &= ~(1 << i);
6436
6437             if (comma)
6438               fputc (',', file);
6439             if (i < (int) ARRAY_SIZE (bitnames))
6440               fputs (bitnames[i], file);
6441             else
6442               fprintf (file, "%d", i);
6443             comma = 1;
6444           }
6445       fputc (')', file);
6446     }
6447 }
6448 \f
6449 /* Print out one basic block with live information at start and end.  */
6450
6451 void
6452 dump_bb (bb, outf)
6453      basic_block bb;
6454      FILE *outf;
6455 {
6456   rtx insn;
6457   rtx last;
6458   edge e;
6459
6460   fprintf (outf, ";; Basic block %d, loop depth %d, count %d",
6461            bb->index, bb->loop_depth, bb->count);
6462   if (bb->eh_beg != -1 || bb->eh_end != -1)
6463     fprintf (outf, ", eh regions %d/%d", bb->eh_beg, bb->eh_end);
6464   putc ('\n', outf);
6465
6466   fputs (";; Predecessors: ", outf);
6467   for (e = bb->pred; e; e = e->pred_next)
6468     dump_edge_info (outf, e, 0);
6469   putc ('\n', outf);
6470
6471   fputs (";; Registers live at start:", outf);
6472   dump_regset (bb->global_live_at_start, outf);
6473   putc ('\n', outf);
6474
6475   for (insn = bb->head, last = NEXT_INSN (bb->end);
6476        insn != last;
6477        insn = NEXT_INSN (insn))
6478     print_rtl_single (outf, insn);
6479
6480   fputs (";; Registers live at end:", outf);
6481   dump_regset (bb->global_live_at_end, outf);
6482   putc ('\n', outf);
6483
6484   fputs (";; Successors: ", outf);
6485   for (e = bb->succ; e; e = e->succ_next)
6486     dump_edge_info (outf, e, 1);
6487   putc ('\n', outf);
6488 }
6489
6490 void
6491 debug_bb (bb)
6492      basic_block bb;
6493 {
6494   dump_bb (bb, stderr);
6495 }
6496
6497 void
6498 debug_bb_n (n)
6499      int n;
6500 {
6501   dump_bb (BASIC_BLOCK (n), stderr);
6502 }
6503
6504 /* Like print_rtl, but also print out live information for the start of each
6505    basic block.  */
6506
6507 void
6508 print_rtl_with_bb (outf, rtx_first)
6509      FILE *outf;
6510      rtx rtx_first;
6511 {
6512   register rtx tmp_rtx;
6513
6514   if (rtx_first == 0)
6515     fprintf (outf, "(nil)\n");
6516   else
6517     {
6518       int i;
6519       enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
6520       int max_uid = get_max_uid ();
6521       basic_block *start = (basic_block *)
6522         xcalloc (max_uid, sizeof (basic_block));
6523       basic_block *end = (basic_block *)
6524         xcalloc (max_uid, sizeof (basic_block));
6525       enum bb_state *in_bb_p = (enum bb_state *)
6526         xcalloc (max_uid, sizeof (enum bb_state));
6527
6528       for (i = n_basic_blocks - 1; i >= 0; i--)
6529         {
6530           basic_block bb = BASIC_BLOCK (i);
6531           rtx x;
6532
6533           start[INSN_UID (bb->head)] = bb;
6534           end[INSN_UID (bb->end)] = bb;
6535           for (x = bb->head; x != NULL_RTX; x = NEXT_INSN (x))
6536             {
6537               enum bb_state state = IN_MULTIPLE_BB;
6538               if (in_bb_p[INSN_UID (x)] == NOT_IN_BB)
6539                 state = IN_ONE_BB;
6540               in_bb_p[INSN_UID (x)] = state;
6541
6542               if (x == bb->end)
6543                 break;
6544             }
6545         }
6546
6547       for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx))
6548         {
6549           int did_output;
6550           basic_block bb;
6551
6552           if ((bb = start[INSN_UID (tmp_rtx)]) != NULL)
6553             {
6554               fprintf (outf, ";; Start of basic block %d, registers live:",
6555                        bb->index);
6556               dump_regset (bb->global_live_at_start, outf);
6557               putc ('\n', outf);
6558             }
6559
6560           if (in_bb_p[INSN_UID (tmp_rtx)] == NOT_IN_BB
6561               && GET_CODE (tmp_rtx) != NOTE
6562               && GET_CODE (tmp_rtx) != BARRIER)
6563             fprintf (outf, ";; Insn is not within a basic block\n");
6564           else if (in_bb_p[INSN_UID (tmp_rtx)] == IN_MULTIPLE_BB)
6565             fprintf (outf, ";; Insn is in multiple basic blocks\n");
6566
6567           did_output = print_rtl_single (outf, tmp_rtx);
6568
6569           if ((bb = end[INSN_UID (tmp_rtx)]) != NULL)
6570             {
6571               fprintf (outf, ";; End of basic block %d, registers live:\n",
6572                        bb->index);
6573               dump_regset (bb->global_live_at_end, outf);
6574               putc ('\n', outf);
6575             }
6576
6577           if (did_output)
6578             putc ('\n', outf);
6579         }
6580
6581       free (start);
6582       free (end);
6583       free (in_bb_p);
6584     }
6585
6586   if (current_function_epilogue_delay_list != 0)
6587     {
6588       fprintf (outf, "\n;; Insns in epilogue delay list:\n\n");
6589       for (tmp_rtx = current_function_epilogue_delay_list; tmp_rtx != 0;
6590            tmp_rtx = XEXP (tmp_rtx, 1))
6591         print_rtl_single (outf, XEXP (tmp_rtx, 0));
6592     }
6593 }
6594
6595 /* Dump the rtl into the current debugging dump file, then abort.  */
6596
6597 static void
6598 print_rtl_and_abort_fcn (file, line, function)
6599      const char *file;
6600      int line;
6601      const char *function;
6602 {
6603   if (rtl_dump_file)
6604     {
6605       print_rtl_with_bb (rtl_dump_file, get_insns ());
6606       fclose (rtl_dump_file);
6607     }
6608
6609   fancy_abort (file, line, function);
6610 }
6611
6612 /* Recompute register set/reference counts immediately prior to register
6613    allocation.
6614
6615    This avoids problems with set/reference counts changing to/from values
6616    which have special meanings to the register allocators.
6617
6618    Additionally, the reference counts are the primary component used by the
6619    register allocators to prioritize pseudos for allocation to hard regs.
6620    More accurate reference counts generally lead to better register allocation.
6621
6622    F is the first insn to be scanned.
6623
6624    LOOP_STEP denotes how much loop_depth should be incremented per
6625    loop nesting level in order to increase the ref count more for
6626    references in a loop.
6627
6628    It might be worthwhile to update REG_LIVE_LENGTH, REG_BASIC_BLOCK and
6629    possibly other information which is used by the register allocators.  */
6630
6631 void
6632 recompute_reg_usage (f, loop_step)
6633      rtx f ATTRIBUTE_UNUSED;
6634      int loop_step ATTRIBUTE_UNUSED;
6635 {
6636   allocate_reg_life_data ();
6637   update_life_info (NULL, UPDATE_LIFE_LOCAL, PROP_REG_INFO);
6638 }
6639
6640 /* Optionally removes all the REG_DEAD and REG_UNUSED notes from a set of
6641    blocks.  If BLOCKS is NULL, assume the universal set.  Returns a count
6642    of the number of registers that died.  */
6643
6644 int
6645 count_or_remove_death_notes (blocks, kill)
6646      sbitmap blocks;
6647      int kill;
6648 {
6649   int i, count = 0;
6650
6651   for (i = n_basic_blocks - 1; i >= 0; --i)
6652     {
6653       basic_block bb;
6654       rtx insn;
6655
6656       if (blocks && ! TEST_BIT (blocks, i))
6657         continue;
6658
6659       bb = BASIC_BLOCK (i);
6660
6661       for (insn = bb->head;; insn = NEXT_INSN (insn))
6662         {
6663           if (INSN_P (insn))
6664             {
6665               rtx *pprev = &REG_NOTES (insn);
6666               rtx link = *pprev;
6667
6668               while (link)
6669                 {
6670                   switch (REG_NOTE_KIND (link))
6671                     {
6672                     case REG_DEAD:
6673                       if (GET_CODE (XEXP (link, 0)) == REG)
6674                         {
6675                           rtx reg = XEXP (link, 0);
6676                           int n;
6677
6678                           if (REGNO (reg) >= FIRST_PSEUDO_REGISTER)
6679                             n = 1;
6680                           else
6681                             n = HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg));
6682                           count += n;
6683                         }
6684                       /* Fall through.  */
6685
6686                     case REG_UNUSED:
6687                       if (kill)
6688                         {
6689                           rtx next = XEXP (link, 1);
6690                           free_EXPR_LIST_node (link);
6691                           *pprev = link = next;
6692                           break;
6693                         }
6694                       /* Fall through.  */
6695
6696                     default:
6697                       pprev = &XEXP (link, 1);
6698                       link = *pprev;
6699                       break;
6700                     }
6701                 }
6702             }
6703
6704           if (insn == bb->end)
6705             break;
6706         }
6707     }
6708
6709   return count;
6710 }
6711
6712
6713 /* Update insns block within BB.  */
6714
6715 void
6716 update_bb_for_insn (bb)
6717      basic_block bb;
6718 {
6719   rtx insn;
6720
6721   if (! basic_block_for_insn)
6722     return;
6723
6724   for (insn = bb->head; ; insn = NEXT_INSN (insn))
6725     {
6726       set_block_for_insn (insn, bb);
6727
6728       if (insn == bb->end)
6729         break;
6730     }
6731 }
6732
6733
6734 /* Record INSN's block as BB.  */
6735
6736 void
6737 set_block_for_insn (insn, bb)
6738      rtx insn;
6739      basic_block bb;
6740 {
6741   size_t uid = INSN_UID (insn);
6742   if (uid >= basic_block_for_insn->num_elements)
6743     {
6744       int new_size;
6745
6746       /* Add one-eighth the size so we don't keep calling xrealloc.  */
6747       new_size = uid + (uid + 7) / 8;
6748
6749       VARRAY_GROW (basic_block_for_insn, new_size);
6750     }
6751   VARRAY_BB (basic_block_for_insn, uid) = bb;
6752 }
6753
6754 /* Record INSN's block number as BB.  */
6755 /* ??? This has got to go.  */
6756
6757 void
6758 set_block_num (insn, bb)
6759      rtx insn;
6760      int bb;
6761 {
6762   set_block_for_insn (insn, BASIC_BLOCK (bb));
6763 }
6764 \f
6765 /* Verify the CFG consistency.  This function check some CFG invariants and
6766    aborts when something is wrong.  Hope that this function will help to
6767    convert many optimization passes to preserve CFG consistent.
6768
6769    Currently it does following checks:
6770
6771    - test head/end pointers
6772    - overlapping of basic blocks
6773    - edge list corectness
6774    - headers of basic blocks (the NOTE_INSN_BASIC_BLOCK note)
6775    - tails of basic blocks (ensure that boundary is necesary)
6776    - scans body of the basic block for JUMP_INSN, CODE_LABEL
6777      and NOTE_INSN_BASIC_BLOCK
6778    - check that all insns are in the basic blocks
6779    (except the switch handling code, barriers and notes)
6780    - check that all returns are followed by barriers
6781
6782    In future it can be extended check a lot of other stuff as well
6783    (reachability of basic blocks, life information, etc. etc.).  */
6784
6785 void
6786 verify_flow_info ()
6787 {
6788   const int max_uid = get_max_uid ();
6789   const rtx rtx_first = get_insns ();
6790   rtx last_head = get_last_insn ();
6791   basic_block *bb_info;
6792   rtx x;
6793   int i, last_bb_num_seen, num_bb_notes, err = 0;
6794
6795   bb_info = (basic_block *) xcalloc (max_uid, sizeof (basic_block));
6796
6797   for (i = n_basic_blocks - 1; i >= 0; i--)
6798     {
6799       basic_block bb = BASIC_BLOCK (i);
6800       rtx head = bb->head;
6801       rtx end = bb->end;
6802
6803       /* Verify the end of the basic block is in the INSN chain.  */
6804       for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
6805         if (x == end)
6806           break;
6807       if (!x)
6808         {
6809           error ("End insn %d for block %d not found in the insn stream.",
6810                  INSN_UID (end), bb->index);
6811           err = 1;
6812         }
6813
6814       /* Work backwards from the end to the head of the basic block
6815          to verify the head is in the RTL chain.  */
6816       for (; x != NULL_RTX; x = PREV_INSN (x))
6817         {
6818           /* While walking over the insn chain, verify insns appear
6819              in only one basic block and initialize the BB_INFO array
6820              used by other passes.  */
6821           if (bb_info[INSN_UID (x)] != NULL)
6822             {
6823               error ("Insn %d is in multiple basic blocks (%d and %d)",
6824                      INSN_UID (x), bb->index, bb_info[INSN_UID (x)]->index);
6825               err = 1;
6826             }
6827           bb_info[INSN_UID (x)] = bb;
6828
6829           if (x == head)
6830             break;
6831         }
6832       if (!x)
6833         {
6834           error ("Head insn %d for block %d not found in the insn stream.",
6835                  INSN_UID (head), bb->index);
6836           err = 1;
6837         }
6838
6839       last_head = x;
6840     }
6841
6842   /* Now check the basic blocks (boundaries etc.) */
6843   for (i = n_basic_blocks - 1; i >= 0; i--)
6844     {
6845       basic_block bb = BASIC_BLOCK (i);
6846       /* Check corectness of edge lists */
6847       edge e;
6848
6849       e = bb->succ;
6850       while (e)
6851         {
6852           if (e->src != bb)
6853             {
6854               fprintf (stderr,
6855                        "verify_flow_info: Basic block %d succ edge is corrupted\n",
6856                        bb->index);
6857               fprintf (stderr, "Predecessor: ");
6858               dump_edge_info (stderr, e, 0);
6859               fprintf (stderr, "\nSuccessor: ");
6860               dump_edge_info (stderr, e, 1);
6861               fflush (stderr);
6862               err = 1;
6863             }
6864           if (e->dest != EXIT_BLOCK_PTR)
6865             {
6866               edge e2 = e->dest->pred;
6867               while (e2 && e2 != e)
6868                 e2 = e2->pred_next;
6869               if (!e2)
6870                 {
6871                   error ("Basic block %i edge lists are corrupted", bb->index);
6872                   err = 1;
6873                 }
6874             }
6875           e = e->succ_next;
6876         }
6877
6878       e = bb->pred;
6879       while (e)
6880         {
6881           if (e->dest != bb)
6882             {
6883               error ("Basic block %d pred edge is corrupted", bb->index);
6884               fputs ("Predecessor: ", stderr);
6885               dump_edge_info (stderr, e, 0);
6886               fputs ("\nSuccessor: ", stderr);
6887               dump_edge_info (stderr, e, 1);
6888               fputc ('\n', stderr);
6889               err = 1;
6890             }
6891           if (e->src != ENTRY_BLOCK_PTR)
6892             {
6893               edge e2 = e->src->succ;
6894               while (e2 && e2 != e)
6895                 e2 = e2->succ_next;
6896               if (!e2)
6897                 {
6898                   error ("Basic block %i edge lists are corrupted", bb->index);
6899                   err = 1;
6900                 }
6901             }
6902           e = e->pred_next;
6903         }
6904
6905       /* OK pointers are correct.  Now check the header of basic
6906          block.  It ought to contain optional CODE_LABEL followed
6907          by NOTE_BASIC_BLOCK.  */
6908       x = bb->head;
6909       if (GET_CODE (x) == CODE_LABEL)
6910         {
6911           if (bb->end == x)
6912             {
6913               error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
6914                      bb->index);
6915               err = 1;
6916             }
6917           x = NEXT_INSN (x);
6918         }
6919       if (!NOTE_INSN_BASIC_BLOCK_P (x) || NOTE_BASIC_BLOCK (x) != bb)
6920         {
6921           error ("NOTE_INSN_BASIC_BLOCK is missing for block %d\n",
6922                  bb->index);
6923           err = 1;
6924         }
6925
6926       if (bb->end == x)
6927         {
6928           /* Do checks for empty blocks here */
6929         }
6930       else
6931         {
6932           x = NEXT_INSN (x);
6933           while (x)
6934             {
6935               if (NOTE_INSN_BASIC_BLOCK_P (x))
6936                 {
6937                   error ("NOTE_INSN_BASIC_BLOCK %d in the middle of basic block %d",
6938                          INSN_UID (x), bb->index);
6939                   err = 1;
6940                 }
6941
6942               if (x == bb->end)
6943                 break;
6944
6945               if (GET_CODE (x) == JUMP_INSN
6946                   || GET_CODE (x) == CODE_LABEL
6947                   || GET_CODE (x) == BARRIER)
6948                 {
6949                   error ("In basic block %d:", bb->index);
6950                   fatal_insn ("Flow control insn inside a basic block", x);
6951                 }
6952
6953               x = NEXT_INSN (x);
6954             }
6955         }
6956     }
6957
6958   last_bb_num_seen = -1;
6959   num_bb_notes = 0;
6960   x = rtx_first;
6961   while (x)
6962     {
6963       if (NOTE_INSN_BASIC_BLOCK_P (x))
6964         {
6965           basic_block bb = NOTE_BASIC_BLOCK (x);
6966           num_bb_notes++;
6967           if (bb->index != last_bb_num_seen + 1)
6968             /* Basic blocks not numbered consecutively.  */
6969             abort ();
6970                
6971           last_bb_num_seen = bb->index;
6972         }
6973
6974       if (!bb_info[INSN_UID (x)])
6975         {
6976           switch (GET_CODE (x))
6977             {
6978             case BARRIER:
6979             case NOTE:
6980               break;
6981
6982             case CODE_LABEL:
6983               /* An addr_vec is placed outside any block block.  */
6984               if (NEXT_INSN (x)
6985                   && GET_CODE (NEXT_INSN (x)) == JUMP_INSN
6986                   && (GET_CODE (PATTERN (NEXT_INSN (x))) == ADDR_DIFF_VEC
6987                       || GET_CODE (PATTERN (NEXT_INSN (x))) == ADDR_VEC))
6988                 {
6989                   x = NEXT_INSN (x);
6990                 }
6991
6992               /* But in any case, non-deletable labels can appear anywhere.  */
6993               break;
6994
6995             default:
6996               fatal_insn ("Insn outside basic block", x);
6997             }
6998         }
6999
7000       if (INSN_P (x)
7001           && GET_CODE (x) == JUMP_INSN
7002           && returnjump_p (x) && ! condjump_p (x)
7003           && ! (NEXT_INSN (x) && GET_CODE (NEXT_INSN (x)) == BARRIER))
7004             fatal_insn ("Return not followed by barrier", x);
7005
7006       x = NEXT_INSN (x);
7007     }
7008
7009   if (num_bb_notes != n_basic_blocks)
7010     internal_error
7011       ("number of bb notes in insn chain (%d) != n_basic_blocks (%d)",
7012        num_bb_notes, n_basic_blocks);
7013
7014   if (err)
7015     abort ();
7016
7017   /* Clean up.  */
7018   free (bb_info);
7019 }
7020 \f
7021 /* Functions to access an edge list with a vector representation.
7022    Enough data is kept such that given an index number, the
7023    pred and succ that edge represents can be determined, or
7024    given a pred and a succ, its index number can be returned.
7025    This allows algorithms which consume a lot of memory to
7026    represent the normally full matrix of edge (pred,succ) with a
7027    single indexed vector,  edge (EDGE_INDEX (pred, succ)), with no
7028    wasted space in the client code due to sparse flow graphs.  */
7029
7030 /* This functions initializes the edge list. Basically the entire
7031    flowgraph is processed, and all edges are assigned a number,
7032    and the data structure is filled in.  */
7033
7034 struct edge_list *
7035 create_edge_list ()
7036 {
7037   struct edge_list *elist;
7038   edge e;
7039   int num_edges;
7040   int x;
7041   int block_count;
7042
7043   block_count = n_basic_blocks + 2;   /* Include the entry and exit blocks.  */
7044
7045   num_edges = 0;
7046
7047   /* Determine the number of edges in the flow graph by counting successor
7048      edges on each basic block.  */
7049   for (x = 0; x < n_basic_blocks; x++)
7050     {
7051       basic_block bb = BASIC_BLOCK (x);
7052
7053       for (e = bb->succ; e; e = e->succ_next)
7054         num_edges++;
7055     }
7056   /* Don't forget successors of the entry block.  */
7057   for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
7058     num_edges++;
7059
7060   elist = (struct edge_list *) xmalloc (sizeof (struct edge_list));
7061   elist->num_blocks = block_count;
7062   elist->num_edges = num_edges;
7063   elist->index_to_edge = (edge *) xmalloc (sizeof (edge) * num_edges);
7064
7065   num_edges = 0;
7066
7067   /* Follow successors of the entry block, and register these edges.  */
7068   for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
7069     {
7070       elist->index_to_edge[num_edges] = e;
7071       num_edges++;
7072     }
7073
7074   for (x = 0; x < n_basic_blocks; x++)
7075     {
7076       basic_block bb = BASIC_BLOCK (x);
7077
7078       /* Follow all successors of blocks, and register these edges.  */
7079       for (e = bb->succ; e; e = e->succ_next)
7080         {
7081           elist->index_to_edge[num_edges] = e;
7082           num_edges++;
7083         }
7084     }
7085   return elist;
7086 }
7087
7088 /* This function free's memory associated with an edge list.  */
7089
7090 void
7091 free_edge_list (elist)
7092      struct edge_list *elist;
7093 {
7094   if (elist)
7095     {
7096       free (elist->index_to_edge);
7097       free (elist);
7098     }
7099 }
7100
7101 /* This function provides debug output showing an edge list.  */
7102
7103 void
7104 print_edge_list (f, elist)
7105      FILE *f;
7106      struct edge_list *elist;
7107 {
7108   int x;
7109   fprintf (f, "Compressed edge list, %d BBs + entry & exit, and %d edges\n",
7110            elist->num_blocks - 2, elist->num_edges);
7111
7112   for (x = 0; x < elist->num_edges; x++)
7113     {
7114       fprintf (f, " %-4d - edge(", x);
7115       if (INDEX_EDGE_PRED_BB (elist, x) == ENTRY_BLOCK_PTR)
7116         fprintf (f, "entry,");
7117       else
7118         fprintf (f, "%d,", INDEX_EDGE_PRED_BB (elist, x)->index);
7119
7120       if (INDEX_EDGE_SUCC_BB (elist, x) == EXIT_BLOCK_PTR)
7121         fprintf (f, "exit)\n");
7122       else
7123         fprintf (f, "%d)\n", INDEX_EDGE_SUCC_BB (elist, x)->index);
7124     }
7125 }
7126
7127 /* This function provides an internal consistency check of an edge list,
7128    verifying that all edges are present, and that there are no
7129    extra edges.  */
7130
7131 void
7132 verify_edge_list (f, elist)
7133      FILE *f;
7134      struct edge_list *elist;
7135 {
7136   int x, pred, succ, index;
7137   edge e;
7138
7139   for (x = 0; x < n_basic_blocks; x++)
7140     {
7141       basic_block bb = BASIC_BLOCK (x);
7142
7143       for (e = bb->succ; e; e = e->succ_next)
7144         {
7145           pred = e->src->index;
7146           succ = e->dest->index;
7147           index = EDGE_INDEX (elist, e->src, e->dest);
7148           if (index == EDGE_INDEX_NO_EDGE)
7149             {
7150               fprintf (f, "*p* No index for edge from %d to %d\n", pred, succ);
7151               continue;
7152             }
7153           if (INDEX_EDGE_PRED_BB (elist, index)->index != pred)
7154             fprintf (f, "*p* Pred for index %d should be %d not %d\n",
7155                      index, pred, INDEX_EDGE_PRED_BB (elist, index)->index);
7156           if (INDEX_EDGE_SUCC_BB (elist, index)->index != succ)
7157             fprintf (f, "*p* Succ for index %d should be %d not %d\n",
7158                      index, succ, INDEX_EDGE_SUCC_BB (elist, index)->index);
7159         }
7160     }
7161   for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
7162     {
7163       pred = e->src->index;
7164       succ = e->dest->index;
7165       index = EDGE_INDEX (elist, e->src, e->dest);
7166       if (index == EDGE_INDEX_NO_EDGE)
7167         {
7168           fprintf (f, "*p* No index for edge from %d to %d\n", pred, succ);
7169           continue;
7170         }
7171       if (INDEX_EDGE_PRED_BB (elist, index)->index != pred)
7172         fprintf (f, "*p* Pred for index %d should be %d not %d\n",
7173                  index, pred, INDEX_EDGE_PRED_BB (elist, index)->index);
7174       if (INDEX_EDGE_SUCC_BB (elist, index)->index != succ)
7175         fprintf (f, "*p* Succ for index %d should be %d not %d\n",
7176                  index, succ, INDEX_EDGE_SUCC_BB (elist, index)->index);
7177     }
7178   /* We've verified that all the edges are in the list, no lets make sure
7179      there are no spurious edges in the list.  */
7180
7181   for (pred = 0; pred < n_basic_blocks; pred++)
7182     for (succ = 0; succ < n_basic_blocks; succ++)
7183       {
7184         basic_block p = BASIC_BLOCK (pred);
7185         basic_block s = BASIC_BLOCK (succ);
7186
7187         int found_edge = 0;
7188
7189         for (e = p->succ; e; e = e->succ_next)
7190           if (e->dest == s)
7191             {
7192               found_edge = 1;
7193               break;
7194             }
7195         for (e = s->pred; e; e = e->pred_next)
7196           if (e->src == p)
7197             {
7198               found_edge = 1;
7199               break;
7200             }
7201         if (EDGE_INDEX (elist, BASIC_BLOCK (pred), BASIC_BLOCK (succ))
7202             == EDGE_INDEX_NO_EDGE && found_edge != 0)
7203           fprintf (f, "*** Edge (%d, %d) appears to not have an index\n",
7204                    pred, succ);
7205         if (EDGE_INDEX (elist, BASIC_BLOCK (pred), BASIC_BLOCK (succ))
7206             != EDGE_INDEX_NO_EDGE && found_edge == 0)
7207           fprintf (f, "*** Edge (%d, %d) has index %d, but there is no edge\n",
7208                    pred, succ, EDGE_INDEX (elist, BASIC_BLOCK (pred),
7209                                            BASIC_BLOCK (succ)));
7210       }
7211   for (succ = 0; succ < n_basic_blocks; succ++)
7212     {
7213       basic_block p = ENTRY_BLOCK_PTR;
7214       basic_block s = BASIC_BLOCK (succ);
7215
7216       int found_edge = 0;
7217
7218       for (e = p->succ; e; e = e->succ_next)
7219         if (e->dest == s)
7220           {
7221             found_edge = 1;
7222             break;
7223           }
7224       for (e = s->pred; e; e = e->pred_next)
7225         if (e->src == p)
7226           {
7227             found_edge = 1;
7228             break;
7229           }
7230       if (EDGE_INDEX (elist, ENTRY_BLOCK_PTR, BASIC_BLOCK (succ))
7231           == EDGE_INDEX_NO_EDGE && found_edge != 0)
7232         fprintf (f, "*** Edge (entry, %d) appears to not have an index\n",
7233                  succ);
7234       if (EDGE_INDEX (elist, ENTRY_BLOCK_PTR, BASIC_BLOCK (succ))
7235           != EDGE_INDEX_NO_EDGE && found_edge == 0)
7236         fprintf (f, "*** Edge (entry, %d) has index %d, but no edge exists\n",
7237                  succ, EDGE_INDEX (elist, ENTRY_BLOCK_PTR,
7238                                    BASIC_BLOCK (succ)));
7239     }
7240   for (pred = 0; pred < n_basic_blocks; pred++)
7241     {
7242       basic_block p = BASIC_BLOCK (pred);
7243       basic_block s = EXIT_BLOCK_PTR;
7244
7245       int found_edge = 0;
7246
7247       for (e = p->succ; e; e = e->succ_next)
7248         if (e->dest == s)
7249           {
7250             found_edge = 1;
7251             break;
7252           }
7253       for (e = s->pred; e; e = e->pred_next)
7254         if (e->src == p)
7255           {
7256             found_edge = 1;
7257             break;
7258           }
7259       if (EDGE_INDEX (elist, BASIC_BLOCK (pred), EXIT_BLOCK_PTR)
7260           == EDGE_INDEX_NO_EDGE && found_edge != 0)
7261         fprintf (f, "*** Edge (%d, exit) appears to not have an index\n",
7262                  pred);
7263       if (EDGE_INDEX (elist, BASIC_BLOCK (pred), EXIT_BLOCK_PTR)
7264           != EDGE_INDEX_NO_EDGE && found_edge == 0)
7265         fprintf (f, "*** Edge (%d, exit) has index %d, but no edge exists\n",
7266                  pred, EDGE_INDEX (elist, BASIC_BLOCK (pred),
7267                                    EXIT_BLOCK_PTR));
7268     }
7269 }
7270
7271 /* This routine will determine what, if any, edge there is between
7272    a specified predecessor and successor.  */
7273
7274 int
7275 find_edge_index (edge_list, pred, succ)
7276      struct edge_list *edge_list;
7277      basic_block pred, succ;
7278 {
7279   int x;
7280   for (x = 0; x < NUM_EDGES (edge_list); x++)
7281     {
7282       if (INDEX_EDGE_PRED_BB (edge_list, x) == pred
7283           && INDEX_EDGE_SUCC_BB (edge_list, x) == succ)
7284         return x;
7285     }
7286   return (EDGE_INDEX_NO_EDGE);
7287 }
7288
7289 /* This function will remove an edge from the flow graph.  */
7290
7291 void
7292 remove_edge (e)
7293      edge e;
7294 {
7295   edge last_pred = NULL;
7296   edge last_succ = NULL;
7297   edge tmp;
7298   basic_block src, dest;
7299   src = e->src;
7300   dest = e->dest;
7301   for (tmp = src->succ; tmp && tmp != e; tmp = tmp->succ_next)
7302     last_succ = tmp;
7303
7304   if (!tmp)
7305     abort ();
7306   if (last_succ)
7307     last_succ->succ_next = e->succ_next;
7308   else
7309     src->succ = e->succ_next;
7310
7311   for (tmp = dest->pred; tmp && tmp != e; tmp = tmp->pred_next)
7312     last_pred = tmp;
7313
7314   if (!tmp)
7315     abort ();
7316   if (last_pred)
7317     last_pred->pred_next = e->pred_next;
7318   else
7319     dest->pred = e->pred_next;
7320
7321   n_edges--;
7322   free (e);
7323 }
7324
7325 /* This routine will remove any fake successor edges for a basic block.
7326    When the edge is removed, it is also removed from whatever predecessor
7327    list it is in.  */
7328
7329 static void
7330 remove_fake_successors (bb)
7331      basic_block bb;
7332 {
7333   edge e;
7334   for (e = bb->succ; e;)
7335     {
7336       edge tmp = e;
7337       e = e->succ_next;
7338       if ((tmp->flags & EDGE_FAKE) == EDGE_FAKE)
7339         remove_edge (tmp);
7340     }
7341 }
7342
7343 /* This routine will remove all fake edges from the flow graph.  If
7344    we remove all fake successors, it will automatically remove all
7345    fake predecessors.  */
7346
7347 void
7348 remove_fake_edges ()
7349 {
7350   int x;
7351
7352   for (x = 0; x < n_basic_blocks; x++)
7353     remove_fake_successors (BASIC_BLOCK (x));
7354
7355   /* We've handled all successors except the entry block's.  */
7356   remove_fake_successors (ENTRY_BLOCK_PTR);
7357 }
7358
7359 /* This function will add a fake edge between any block which has no
7360    successors, and the exit block. Some data flow equations require these
7361    edges to exist.  */
7362
7363 void
7364 add_noreturn_fake_exit_edges ()
7365 {
7366   int x;
7367
7368   for (x = 0; x < n_basic_blocks; x++)
7369     if (BASIC_BLOCK (x)->succ == NULL)
7370       make_edge (NULL, BASIC_BLOCK (x), EXIT_BLOCK_PTR, EDGE_FAKE);
7371 }
7372
7373 /* This function adds a fake edge between any infinite loops to the
7374    exit block.  Some optimizations require a path from each node to
7375    the exit node.
7376
7377    See also Morgan, Figure 3.10, pp. 82-83.
7378
7379    The current implementation is ugly, not attempting to minimize the
7380    number of inserted fake edges.  To reduce the number of fake edges
7381    to insert, add fake edges from _innermost_ loops containing only
7382    nodes not reachable from the exit block.  */
7383
7384 void
7385 connect_infinite_loops_to_exit ()
7386 {
7387   basic_block unvisited_block;
7388
7389   /* Perform depth-first search in the reverse graph to find nodes
7390      reachable from the exit block.  */
7391   struct depth_first_search_dsS dfs_ds;
7392
7393   flow_dfs_compute_reverse_init (&dfs_ds);
7394   flow_dfs_compute_reverse_add_bb (&dfs_ds, EXIT_BLOCK_PTR);
7395
7396   /* Repeatedly add fake edges, updating the unreachable nodes.  */
7397   while (1)
7398     {
7399       unvisited_block = flow_dfs_compute_reverse_execute (&dfs_ds);
7400       if (!unvisited_block)
7401         break;
7402       make_edge (NULL, unvisited_block, EXIT_BLOCK_PTR, EDGE_FAKE);
7403       flow_dfs_compute_reverse_add_bb (&dfs_ds, unvisited_block);
7404     }
7405
7406   flow_dfs_compute_reverse_finish (&dfs_ds);
7407
7408   return;
7409 }
7410
7411 /* Redirect an edge's successor from one block to another.  */
7412
7413 void
7414 redirect_edge_succ (e, new_succ)
7415      edge e;
7416      basic_block new_succ;
7417 {
7418   edge *pe;
7419
7420   /* Disconnect the edge from the old successor block.  */
7421   for (pe = &e->dest->pred; *pe != e; pe = &(*pe)->pred_next)
7422     continue;
7423   *pe = (*pe)->pred_next;
7424
7425   /* Reconnect the edge to the new successor block.  */
7426   e->pred_next = new_succ->pred;
7427   new_succ->pred = e;
7428   e->dest = new_succ;
7429 }
7430
7431 /* Redirect an edge's predecessor from one block to another.  */
7432
7433 void
7434 redirect_edge_pred (e, new_pred)
7435      edge e;
7436      basic_block new_pred;
7437 {
7438   edge *pe;
7439
7440   /* Disconnect the edge from the old predecessor block.  */
7441   for (pe = &e->src->succ; *pe != e; pe = &(*pe)->succ_next)
7442     continue;
7443   *pe = (*pe)->succ_next;
7444
7445   /* Reconnect the edge to the new predecessor block.  */
7446   e->succ_next = new_pred->succ;
7447   new_pred->succ = e;
7448   e->src = new_pred;
7449 }
7450 \f
7451 /* Dump the list of basic blocks in the bitmap NODES.  */
7452
7453 static void
7454 flow_nodes_print (str, nodes, file)
7455      const char *str;
7456      const sbitmap nodes;
7457      FILE *file;
7458 {
7459   int node;
7460
7461   if (! nodes)
7462     return;
7463
7464   fprintf (file, "%s { ", str);
7465   EXECUTE_IF_SET_IN_SBITMAP (nodes, 0, node, {fprintf (file, "%d ", node);});
7466   fputs ("}\n", file);
7467 }
7468
7469
7470 /* Dump the list of edges in the array EDGE_LIST.  */
7471
7472 static void
7473 flow_edge_list_print (str, edge_list, num_edges, file)
7474      const char *str;
7475      const edge *edge_list;
7476      int num_edges;
7477      FILE *file;
7478 {
7479   int i;
7480
7481   if (! edge_list)
7482     return;
7483
7484   fprintf (file, "%s { ", str);
7485   for (i = 0; i < num_edges; i++)
7486     fprintf (file, "%d->%d ", edge_list[i]->src->index,
7487              edge_list[i]->dest->index);
7488   fputs ("}\n", file);
7489 }
7490
7491
7492 /* Dump loop related CFG information.  */
7493
7494 static void
7495 flow_loops_cfg_dump (loops, file)
7496      const struct loops *loops;
7497      FILE *file;
7498 {
7499   int i;
7500
7501   if (! loops->num || ! file || ! loops->cfg.dom)
7502     return;
7503
7504   for (i = 0; i < n_basic_blocks; i++)
7505     {
7506       edge succ;
7507
7508       fprintf (file, ";; %d succs { ", i);
7509       for (succ = BASIC_BLOCK (i)->succ; succ; succ = succ->succ_next)
7510         fprintf (file, "%d ", succ->dest->index);
7511       flow_nodes_print ("} dom", loops->cfg.dom[i], file);
7512     }
7513
7514   /* Dump the DFS node order.  */
7515   if (loops->cfg.dfs_order)
7516     {
7517       fputs (";; DFS order: ", file);
7518       for (i = 0; i < n_basic_blocks; i++)
7519         fprintf (file, "%d ", loops->cfg.dfs_order[i]);
7520       fputs ("\n", file);
7521     }
7522   /* Dump the reverse completion node order.  */
7523   if (loops->cfg.rc_order)
7524     {
7525       fputs (";; RC order: ", file);
7526       for (i = 0; i < n_basic_blocks; i++)
7527         fprintf (file, "%d ", loops->cfg.rc_order[i]);
7528       fputs ("\n", file);
7529     }
7530 }
7531
7532 /* Return non-zero if the nodes of LOOP are a subset of OUTER.  */
7533
7534 static int
7535 flow_loop_nested_p (outer, loop)
7536      struct loop *outer;
7537      struct loop *loop;
7538 {
7539   return sbitmap_a_subset_b_p (loop->nodes, outer->nodes);
7540 }
7541
7542
7543 /* Dump the loop information specified by LOOP to the stream FILE
7544    using auxiliary dump callback function LOOP_DUMP_AUX if non null.  */
7545 void
7546 flow_loop_dump (loop, file, loop_dump_aux, verbose)
7547      const struct loop *loop;
7548      FILE *file;
7549      void (*loop_dump_aux) PARAMS((const struct loop *, FILE *, int));
7550      int verbose;
7551 {
7552   if (! loop || ! loop->header)
7553     return;
7554
7555   fprintf (file, ";;\n;; Loop %d (%d to %d):%s%s\n",
7556            loop->num, INSN_UID (loop->first->head),
7557            INSN_UID (loop->last->end),
7558            loop->shared ? " shared" : "",
7559            loop->invalid ? " invalid" : "");
7560   fprintf (file, ";;  header %d, latch %d, pre-header %d, first %d, last %d\n",
7561            loop->header->index, loop->latch->index,
7562            loop->pre_header ? loop->pre_header->index : -1,
7563            loop->first->index, loop->last->index);
7564   fprintf (file, ";;  depth %d, level %d, outer %ld\n",
7565            loop->depth, loop->level,
7566            (long) (loop->outer ? loop->outer->num : -1));
7567
7568   if (loop->pre_header_edges)
7569     flow_edge_list_print (";;  pre-header edges", loop->pre_header_edges,
7570                           loop->num_pre_header_edges, file);
7571   flow_edge_list_print (";;  entry edges", loop->entry_edges,
7572                         loop->num_entries, file);
7573   fprintf (file, ";;  %d", loop->num_nodes);
7574   flow_nodes_print (" nodes", loop->nodes, file);
7575   flow_edge_list_print (";;  exit edges", loop->exit_edges,
7576                         loop->num_exits, file);
7577   if (loop->exits_doms)
7578     flow_nodes_print (";;  exit doms", loop->exits_doms, file);
7579   if (loop_dump_aux)
7580     loop_dump_aux (loop, file, verbose);
7581 }
7582
7583
7584 /* Dump the loop information specified by LOOPS to the stream FILE,
7585    using auxiliary dump callback function LOOP_DUMP_AUX if non null.  */
7586 void
7587 flow_loops_dump (loops, file, loop_dump_aux, verbose)
7588      const struct loops *loops;
7589      FILE *file;
7590      void (*loop_dump_aux) PARAMS((const struct loop *, FILE *, int));
7591      int verbose;
7592 {
7593   int i;
7594   int num_loops;
7595
7596   num_loops = loops->num;
7597   if (! num_loops || ! file)
7598     return;
7599
7600   fprintf (file, ";; %d loops found, %d levels\n",
7601            num_loops, loops->levels);
7602
7603   for (i = 0; i < num_loops; i++)
7604     {
7605       struct loop *loop = &loops->array[i];
7606
7607       flow_loop_dump (loop, file, loop_dump_aux, verbose);
7608
7609       if (loop->shared)
7610         {
7611           int j;
7612
7613           for (j = 0; j < i; j++)
7614             {
7615               struct loop *oloop = &loops->array[j];
7616
7617               if (loop->header == oloop->header)
7618                 {
7619                   int disjoint;
7620                   int smaller;
7621
7622                   smaller = loop->num_nodes < oloop->num_nodes;
7623
7624                   /* If the union of LOOP and OLOOP is different than
7625                      the larger of LOOP and OLOOP then LOOP and OLOOP
7626                      must be disjoint.  */
7627                   disjoint = ! flow_loop_nested_p (smaller ? loop : oloop,
7628                                                    smaller ? oloop : loop);
7629                   fprintf (file,
7630                            ";; loop header %d shared by loops %d, %d %s\n",
7631                            loop->header->index, i, j,
7632                            disjoint ? "disjoint" : "nested");
7633                 }
7634             }
7635         }
7636     }
7637
7638   if (verbose)
7639     flow_loops_cfg_dump (loops, file);
7640 }
7641
7642
7643 /* Free all the memory allocated for LOOPS.  */
7644
7645 void
7646 flow_loops_free (loops)
7647      struct loops *loops;
7648 {
7649   if (loops->array)
7650     {
7651       int i;
7652
7653       if (! loops->num)
7654         abort ();
7655
7656       /* Free the loop descriptors.  */
7657       for (i = 0; i < loops->num; i++)
7658         {
7659           struct loop *loop = &loops->array[i];
7660
7661           if (loop->pre_header_edges)
7662             free (loop->pre_header_edges);
7663           if (loop->nodes)
7664             sbitmap_free (loop->nodes);
7665           if (loop->entry_edges)
7666             free (loop->entry_edges);
7667           if (loop->exit_edges)
7668             free (loop->exit_edges);
7669           if (loop->exits_doms)
7670             sbitmap_free (loop->exits_doms);
7671         }
7672       free (loops->array);
7673       loops->array = NULL;
7674
7675       if (loops->cfg.dom)
7676         sbitmap_vector_free (loops->cfg.dom);
7677       if (loops->cfg.dfs_order)
7678         free (loops->cfg.dfs_order);
7679
7680       if (loops->shared_headers)
7681         sbitmap_free (loops->shared_headers);
7682     }
7683 }
7684
7685
7686 /* Find the entry edges into the loop with header HEADER and nodes
7687    NODES and store in ENTRY_EDGES array.  Return the number of entry
7688    edges from the loop.  */
7689
7690 static int
7691 flow_loop_entry_edges_find (header, nodes, entry_edges)
7692      basic_block header;
7693      const sbitmap nodes;
7694      edge **entry_edges;
7695 {
7696   edge e;
7697   int num_entries;
7698
7699   *entry_edges = NULL;
7700
7701   num_entries = 0;
7702   for (e = header->pred; e; e = e->pred_next)
7703     {
7704       basic_block src = e->src;
7705
7706       if (src == ENTRY_BLOCK_PTR || ! TEST_BIT (nodes, src->index))
7707         num_entries++;
7708     }
7709
7710   if (! num_entries)
7711     abort ();
7712
7713   *entry_edges = (edge *) xmalloc (num_entries * sizeof (edge *));
7714
7715   num_entries = 0;
7716   for (e = header->pred; e; e = e->pred_next)
7717     {
7718       basic_block src = e->src;
7719
7720       if (src == ENTRY_BLOCK_PTR || ! TEST_BIT (nodes, src->index))
7721         (*entry_edges)[num_entries++] = e;
7722     }
7723
7724   return num_entries;
7725 }
7726
7727
7728 /* Find the exit edges from the loop using the bitmap of loop nodes
7729    NODES and store in EXIT_EDGES array.  Return the number of
7730    exit edges from the loop.  */
7731
7732 static int
7733 flow_loop_exit_edges_find (nodes, exit_edges)
7734      const sbitmap nodes;
7735      edge **exit_edges;
7736 {
7737   edge e;
7738   int node;
7739   int num_exits;
7740
7741   *exit_edges = NULL;
7742
7743   /* Check all nodes within the loop to see if there are any
7744      successors not in the loop.  Note that a node may have multiple
7745      exiting edges ?????  A node can have one jumping edge and one fallthru
7746      edge so only one of these can exit the loop.  */
7747   num_exits = 0;
7748   EXECUTE_IF_SET_IN_SBITMAP (nodes, 0, node, {
7749     for (e = BASIC_BLOCK (node)->succ; e; e = e->succ_next)
7750       {
7751         basic_block dest = e->dest;
7752
7753         if (dest == EXIT_BLOCK_PTR || ! TEST_BIT (nodes, dest->index))
7754             num_exits++;
7755       }
7756   });
7757
7758   if (! num_exits)
7759     return 0;
7760
7761   *exit_edges = (edge *) xmalloc (num_exits * sizeof (edge *));
7762
7763   /* Store all exiting edges into an array.  */
7764   num_exits = 0;
7765   EXECUTE_IF_SET_IN_SBITMAP (nodes, 0, node, {
7766     for (e = BASIC_BLOCK (node)->succ; e; e = e->succ_next)
7767       {
7768         basic_block dest = e->dest;
7769
7770         if (dest == EXIT_BLOCK_PTR || ! TEST_BIT (nodes, dest->index))
7771           (*exit_edges)[num_exits++] = e;
7772       }
7773   });
7774
7775   return num_exits;
7776 }
7777
7778
7779 /* Find the nodes contained within the loop with header HEADER and
7780    latch LATCH and store in NODES.  Return the number of nodes within
7781    the loop.  */
7782
7783 static int
7784 flow_loop_nodes_find (header, latch, nodes)
7785      basic_block header;
7786      basic_block latch;
7787      sbitmap nodes;
7788 {
7789   basic_block *stack;
7790   int sp;
7791   int num_nodes = 0;
7792
7793   stack = (basic_block *) xmalloc (n_basic_blocks * sizeof (basic_block));
7794   sp = 0;
7795
7796   /* Start with only the loop header in the set of loop nodes.  */
7797   sbitmap_zero (nodes);
7798   SET_BIT (nodes, header->index);
7799   num_nodes++;
7800   header->loop_depth++;
7801
7802   /* Push the loop latch on to the stack.  */
7803   if (! TEST_BIT (nodes, latch->index))
7804     {
7805       SET_BIT (nodes, latch->index);
7806       latch->loop_depth++;
7807       num_nodes++;
7808       stack[sp++] = latch;
7809     }
7810
7811   while (sp)
7812     {
7813       basic_block node;
7814       edge e;
7815
7816       node = stack[--sp];
7817       for (e = node->pred; e; e = e->pred_next)
7818         {
7819           basic_block ancestor = e->src;
7820
7821           /* If each ancestor not marked as part of loop, add to set of
7822              loop nodes and push on to stack.  */
7823           if (ancestor != ENTRY_BLOCK_PTR
7824               && ! TEST_BIT (nodes, ancestor->index))
7825             {
7826               SET_BIT (nodes, ancestor->index);
7827               ancestor->loop_depth++;
7828               num_nodes++;
7829               stack[sp++] = ancestor;
7830             }
7831         }
7832     }
7833   free (stack);
7834   return num_nodes;
7835 }
7836
7837 /* Compute the depth first search order and store in the array
7838   DFS_ORDER if non-zero, marking the nodes visited in VISITED.  If
7839   RC_ORDER is non-zero, return the reverse completion number for each
7840   node.  Returns the number of nodes visited.  A depth first search
7841   tries to get as far away from the starting point as quickly as
7842   possible.  */
7843
7844 static int
7845 flow_depth_first_order_compute (dfs_order, rc_order)
7846      int *dfs_order;
7847      int *rc_order;
7848 {
7849   edge *stack;
7850   int sp;
7851   int dfsnum = 0;
7852   int rcnum = n_basic_blocks - 1;
7853   sbitmap visited;
7854
7855   /* Allocate stack for back-tracking up CFG.  */
7856   stack = (edge *) xmalloc ((n_basic_blocks + 1) * sizeof (edge));
7857   sp = 0;
7858
7859   /* Allocate bitmap to track nodes that have been visited.  */
7860   visited = sbitmap_alloc (n_basic_blocks);
7861
7862   /* None of the nodes in the CFG have been visited yet.  */
7863   sbitmap_zero (visited);
7864
7865   /* Push the first edge on to the stack.  */
7866   stack[sp++] = ENTRY_BLOCK_PTR->succ;
7867
7868   while (sp)
7869     {
7870       edge e;
7871       basic_block src;
7872       basic_block dest;
7873
7874       /* Look at the edge on the top of the stack.  */
7875       e = stack[sp - 1];
7876       src = e->src;
7877       dest = e->dest;
7878
7879       /* Check if the edge destination has been visited yet.  */
7880       if (dest != EXIT_BLOCK_PTR && ! TEST_BIT (visited, dest->index))
7881         {
7882           /* Mark that we have visited the destination.  */
7883           SET_BIT (visited, dest->index);
7884
7885           if (dfs_order)
7886             dfs_order[dfsnum++] = dest->index;
7887
7888           if (dest->succ)
7889             {
7890               /* Since the DEST node has been visited for the first
7891                  time, check its successors.  */
7892               stack[sp++] = dest->succ;
7893             }
7894           else
7895             {
7896               /* There are no successors for the DEST node so assign
7897                  its reverse completion number.  */
7898               if (rc_order)
7899                 rc_order[rcnum--] = dest->index;
7900             }
7901         }
7902       else
7903         {
7904           if (! e->succ_next && src != ENTRY_BLOCK_PTR)
7905             {
7906               /* There are no more successors for the SRC node
7907                  so assign its reverse completion number.  */
7908               if (rc_order)
7909                 rc_order[rcnum--] = src->index;
7910             }
7911
7912           if (e->succ_next)
7913             stack[sp - 1] = e->succ_next;
7914           else
7915             sp--;
7916         }
7917     }
7918
7919   free (stack);
7920   sbitmap_free (visited);
7921
7922   /* The number of nodes visited should not be greater than
7923      n_basic_blocks.  */
7924   if (dfsnum > n_basic_blocks)
7925     abort ();
7926
7927   /* There are some nodes left in the CFG that are unreachable.  */
7928   if (dfsnum < n_basic_blocks)
7929     abort ();
7930   return dfsnum;
7931 }
7932
7933 /* Compute the depth first search order on the _reverse_ graph and
7934    store in the array DFS_ORDER, marking the nodes visited in VISITED.
7935    Returns the number of nodes visited.
7936
7937    The computation is split into three pieces:
7938
7939    flow_dfs_compute_reverse_init () creates the necessary data
7940    structures.
7941
7942    flow_dfs_compute_reverse_add_bb () adds a basic block to the data
7943    structures.  The block will start the search.
7944
7945    flow_dfs_compute_reverse_execute () continues (or starts) the
7946    search using the block on the top of the stack, stopping when the
7947    stack is empty.
7948
7949    flow_dfs_compute_reverse_finish () destroys the necessary data
7950    structures.
7951
7952    Thus, the user will probably call ..._init(), call ..._add_bb() to
7953    add a beginning basic block to the stack, call ..._execute(),
7954    possibly add another bb to the stack and again call ..._execute(),
7955    ..., and finally call _finish().  */
7956
7957 /* Initialize the data structures used for depth-first search on the
7958    reverse graph.  If INITIALIZE_STACK is nonzero, the exit block is
7959    added to the basic block stack.  DATA is the current depth-first
7960    search context.  If INITIALIZE_STACK is non-zero, there is an
7961    element on the stack.  */
7962
7963 static void
7964 flow_dfs_compute_reverse_init (data)
7965      depth_first_search_ds data;
7966 {
7967   /* Allocate stack for back-tracking up CFG.  */
7968   data->stack =
7969     (basic_block *) xmalloc ((n_basic_blocks - (INVALID_BLOCK + 1))
7970                              * sizeof (basic_block));
7971   data->sp = 0;
7972
7973   /* Allocate bitmap to track nodes that have been visited.  */
7974   data->visited_blocks = sbitmap_alloc (n_basic_blocks - (INVALID_BLOCK + 1));
7975
7976   /* None of the nodes in the CFG have been visited yet.  */
7977   sbitmap_zero (data->visited_blocks);
7978
7979   return;
7980 }
7981
7982 /* Add the specified basic block to the top of the dfs data
7983    structures.  When the search continues, it will start at the
7984    block.  */
7985
7986 static void
7987 flow_dfs_compute_reverse_add_bb (data, bb)
7988      depth_first_search_ds data;
7989      basic_block bb;
7990 {
7991   data->stack[data->sp++] = bb;
7992   return;
7993 }
7994
7995 /* Continue the depth-first search through the reverse graph starting
7996    with the block at the stack's top and ending when the stack is
7997    empty.  Visited nodes are marked.  Returns an unvisited basic
7998    block, or NULL if there is none available.  */
7999
8000 static basic_block
8001 flow_dfs_compute_reverse_execute (data)
8002      depth_first_search_ds data;
8003 {
8004   basic_block bb;
8005   edge e;
8006   int i;
8007
8008   while (data->sp > 0)
8009     {
8010       bb = data->stack[--data->sp];
8011
8012       /* Mark that we have visited this node.  */
8013       if (!TEST_BIT (data->visited_blocks, bb->index - (INVALID_BLOCK + 1)))
8014         {
8015           SET_BIT (data->visited_blocks, bb->index - (INVALID_BLOCK + 1));
8016
8017           /* Perform depth-first search on adjacent vertices.  */
8018           for (e = bb->pred; e; e = e->pred_next)
8019             flow_dfs_compute_reverse_add_bb (data, e->src);
8020         }
8021     }
8022
8023   /* Determine if there are unvisited basic blocks.  */
8024   for (i = n_basic_blocks - (INVALID_BLOCK + 1); --i >= 0;)
8025     if (!TEST_BIT (data->visited_blocks, i))
8026       return BASIC_BLOCK (i + (INVALID_BLOCK + 1));
8027   return NULL;
8028 }
8029
8030 /* Destroy the data structures needed for depth-first search on the
8031    reverse graph.  */
8032
8033 static void
8034 flow_dfs_compute_reverse_finish (data)
8035      depth_first_search_ds data;
8036 {
8037   free (data->stack);
8038   sbitmap_free (data->visited_blocks);
8039   return;
8040 }
8041
8042
8043 /* Find the root node of the loop pre-header extended basic block and
8044    the edges along the trace from the root node to the loop header.  */
8045
8046 static void
8047 flow_loop_pre_header_scan (loop)
8048      struct loop *loop;
8049 {
8050   int num = 0;
8051   basic_block ebb;
8052
8053   loop->num_pre_header_edges = 0;
8054
8055   if (loop->num_entries != 1)
8056      return;
8057
8058   ebb = loop->entry_edges[0]->src;
8059
8060   if (ebb != ENTRY_BLOCK_PTR)
8061     {
8062       edge e;
8063
8064       /* Count number of edges along trace from loop header to
8065          root of pre-header extended basic block.  Usually this is
8066          only one or two edges. */
8067       num++;
8068       while (ebb->pred->src != ENTRY_BLOCK_PTR && ! ebb->pred->pred_next)
8069         {
8070           ebb = ebb->pred->src;
8071           num++;
8072         }
8073
8074       loop->pre_header_edges = (edge *) xmalloc (num * sizeof (edge *));
8075       loop->num_pre_header_edges = num;
8076
8077       /* Store edges in order that they are followed.   The source
8078          of the first edge is the root node of the pre-header extended
8079          basic block and the destination of the last last edge is
8080          the loop header.  */
8081       for (e = loop->entry_edges[0]; num; e = e->src->pred)
8082         {
8083           loop->pre_header_edges[--num] = e;
8084         }
8085     }
8086 }
8087
8088
8089 /* Return the block for the pre-header of the loop with header
8090    HEADER where DOM specifies the dominator information.  Return NULL if
8091    there is no pre-header.  */
8092
8093 static basic_block
8094 flow_loop_pre_header_find (header, dom)
8095      basic_block header;
8096      const sbitmap *dom;
8097 {
8098   basic_block pre_header;
8099   edge e;
8100
8101   /* If block p is a predecessor of the header and is the only block
8102      that the header does not dominate, then it is the pre-header.  */
8103   pre_header = NULL;
8104   for (e = header->pred; e; e = e->pred_next)
8105     {
8106       basic_block node = e->src;
8107
8108       if (node != ENTRY_BLOCK_PTR
8109           && ! TEST_BIT (dom[node->index], header->index))
8110         {
8111           if (pre_header == NULL)
8112             pre_header = node;
8113           else
8114             {
8115               /* There are multiple edges into the header from outside
8116                  the loop so there is no pre-header block.  */
8117               pre_header = NULL;
8118               break;
8119             }
8120         }
8121     }
8122   return pre_header;
8123 }
8124
8125 /* Add LOOP to the loop hierarchy tree where PREVLOOP was the loop
8126    previously added.  The insertion algorithm assumes that the loops
8127    are added in the order found by a depth first search of the CFG.  */
8128
8129 static void
8130 flow_loop_tree_node_add (prevloop, loop)
8131      struct loop *prevloop;
8132      struct loop *loop;
8133 {
8134
8135   if (flow_loop_nested_p (prevloop, loop))
8136     {
8137       prevloop->inner = loop;
8138       loop->outer = prevloop;
8139       return;
8140     }
8141
8142   while (prevloop->outer)
8143     {
8144       if (flow_loop_nested_p (prevloop->outer, loop))
8145         {
8146           prevloop->next = loop;
8147           loop->outer = prevloop->outer;
8148           return;
8149         }
8150       prevloop = prevloop->outer;
8151     }
8152
8153   prevloop->next = loop;
8154   loop->outer = NULL;
8155 }
8156
8157 /* Build the loop hierarchy tree for LOOPS.  */
8158
8159 static void
8160 flow_loops_tree_build (loops)
8161      struct loops *loops;
8162 {
8163   int i;
8164   int num_loops;
8165
8166   num_loops = loops->num;
8167   if (! num_loops)
8168     return;
8169
8170   /* Root the loop hierarchy tree with the first loop found.
8171      Since we used a depth first search this should be the
8172      outermost loop.  */
8173   loops->tree = &loops->array[0];
8174   loops->tree->outer = loops->tree->inner = loops->tree->next = NULL;
8175
8176   /* Add the remaining loops to the tree.  */
8177   for (i = 1; i < num_loops; i++)
8178     flow_loop_tree_node_add (&loops->array[i - 1], &loops->array[i]);
8179 }
8180
8181 /* Helper function to compute loop nesting depth and enclosed loop level
8182    for the natural loop specified by LOOP at the loop depth DEPTH.
8183    Returns the loop level.  */
8184
8185 static int
8186 flow_loop_level_compute (loop, depth)
8187      struct loop *loop;
8188      int depth;
8189 {
8190   struct loop *inner;
8191   int level = 1;
8192
8193   if (! loop)
8194     return 0;
8195
8196   /* Traverse loop tree assigning depth and computing level as the
8197      maximum level of all the inner loops of this loop.  The loop
8198      level is equivalent to the height of the loop in the loop tree
8199      and corresponds to the number of enclosed loop levels (including
8200      itself).  */
8201   for (inner = loop->inner; inner; inner = inner->next)
8202     {
8203       int ilevel;
8204
8205       ilevel = flow_loop_level_compute (inner, depth + 1) + 1;
8206
8207       if (ilevel > level)
8208         level = ilevel;
8209     }
8210   loop->level = level;
8211   loop->depth = depth;
8212   return level;
8213 }
8214
8215 /* Compute the loop nesting depth and enclosed loop level for the loop
8216    hierarchy tree specfied by LOOPS.  Return the maximum enclosed loop
8217    level.  */
8218
8219 static int
8220 flow_loops_level_compute (loops)
8221      struct loops *loops;
8222 {
8223   struct loop *loop;
8224   int level;
8225   int levels = 0;
8226
8227   /* Traverse all the outer level loops.  */
8228   for (loop = loops->tree; loop; loop = loop->next)
8229     {
8230       level = flow_loop_level_compute (loop, 1);
8231       if (level > levels)
8232         levels = level;
8233     }
8234   return levels;
8235 }
8236
8237
8238 /* Scan a single natural loop specified by LOOP collecting information
8239    about it specified by FLAGS.  */
8240
8241 int
8242 flow_loop_scan (loops, loop, flags)
8243      struct loops *loops;
8244      struct loop *loop;
8245      int flags;
8246 {
8247   /* Determine prerequisites.  */
8248   if ((flags & LOOP_EXITS_DOMS) && ! loop->exit_edges)
8249     flags |= LOOP_EXIT_EDGES;
8250
8251   if (flags & LOOP_ENTRY_EDGES)
8252     {
8253       /* Find edges which enter the loop header.
8254          Note that the entry edges should only
8255          enter the header of a natural loop.  */
8256       loop->num_entries
8257         = flow_loop_entry_edges_find (loop->header,
8258                                       loop->nodes,
8259                                       &loop->entry_edges);
8260     }
8261
8262   if (flags & LOOP_EXIT_EDGES)
8263     {
8264       /* Find edges which exit the loop.  */
8265       loop->num_exits
8266         = flow_loop_exit_edges_find (loop->nodes,
8267                                      &loop->exit_edges);
8268     }
8269
8270   if (flags & LOOP_EXITS_DOMS)
8271     {
8272       int j;
8273
8274       /* Determine which loop nodes dominate all the exits
8275          of the loop.  */
8276       loop->exits_doms = sbitmap_alloc (n_basic_blocks);
8277       sbitmap_copy (loop->exits_doms, loop->nodes);
8278       for (j = 0; j < loop->num_exits; j++)
8279         sbitmap_a_and_b (loop->exits_doms, loop->exits_doms,
8280                          loops->cfg.dom[loop->exit_edges[j]->src->index]);
8281       
8282       /* The header of a natural loop must dominate
8283          all exits.  */
8284       if (! TEST_BIT (loop->exits_doms, loop->header->index))
8285         abort ();
8286     }
8287   
8288   if (flags & LOOP_PRE_HEADER)
8289     {
8290       /* Look to see if the loop has a pre-header node.  */
8291       loop->pre_header
8292         = flow_loop_pre_header_find (loop->header, loops->cfg.dom);
8293
8294       /* Find the blocks within the extended basic block of
8295          the loop pre-header.  */
8296       flow_loop_pre_header_scan (loop);
8297     }
8298   return 1;
8299 }
8300
8301
8302 /* Find all the natural loops in the function and save in LOOPS structure
8303    and recalculate loop_depth information in basic block structures.
8304    FLAGS controls which loop information is collected.
8305    Return the number of natural loops found.  */
8306
8307 int
8308 flow_loops_find (loops, flags)
8309      struct loops *loops;
8310      int flags;
8311 {
8312   int i;
8313   int b;
8314   int num_loops;
8315   edge e;
8316   sbitmap headers;
8317   sbitmap *dom;
8318   int *dfs_order;
8319   int *rc_order;
8320
8321   /* This function cannot be repeatedly called with different
8322      flags to build up the loop information.  The loop tree
8323      must always be built if this function is called.  */
8324   if (! (flags & LOOP_TREE))
8325     abort ();
8326
8327   memset (loops, 0, sizeof (*loops));
8328
8329   /* Taking care of this degenerate case makes the rest of
8330      this code simpler.  */
8331   if (n_basic_blocks == 0)
8332     return 0;
8333
8334   dfs_order = NULL;
8335   rc_order = NULL;
8336
8337   /* Compute the dominators.  */
8338   dom = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
8339   calculate_dominance_info (NULL, dom, CDI_DOMINATORS);
8340
8341   /* Count the number of loop edges (back edges).  This should be the
8342      same as the number of natural loops.  */
8343
8344   num_loops = 0;
8345   for (b = 0; b < n_basic_blocks; b++)
8346     {
8347       basic_block header;
8348
8349       header = BASIC_BLOCK (b);
8350       header->loop_depth = 0;
8351
8352       for (e = header->pred; e; e = e->pred_next)
8353         {
8354           basic_block latch = e->src;
8355
8356           /* Look for back edges where a predecessor is dominated
8357              by this block.  A natural loop has a single entry
8358              node (header) that dominates all the nodes in the
8359              loop.  It also has single back edge to the header
8360              from a latch node.  Note that multiple natural loops
8361              may share the same header.  */
8362           if (b != header->index)
8363             abort ();
8364
8365           if (latch != ENTRY_BLOCK_PTR && TEST_BIT (dom[latch->index], b))
8366             num_loops++;
8367         }
8368     }
8369
8370   if (num_loops)
8371     {
8372       /* Compute depth first search order of the CFG so that outer
8373          natural loops will be found before inner natural loops.  */
8374       dfs_order = (int *) xmalloc (n_basic_blocks * sizeof (int));
8375       rc_order = (int *) xmalloc (n_basic_blocks * sizeof (int));
8376       flow_depth_first_order_compute (dfs_order, rc_order);
8377
8378       /* Save CFG derived information to avoid recomputing it.  */
8379       loops->cfg.dom = dom;
8380       loops->cfg.dfs_order = dfs_order;
8381       loops->cfg.rc_order = rc_order;
8382
8383       /* Allocate loop structures.  */
8384       loops->array
8385         = (struct loop *) xcalloc (num_loops, sizeof (struct loop));
8386
8387       headers = sbitmap_alloc (n_basic_blocks);
8388       sbitmap_zero (headers);
8389
8390       loops->shared_headers = sbitmap_alloc (n_basic_blocks);
8391       sbitmap_zero (loops->shared_headers);
8392
8393       /* Find and record information about all the natural loops
8394          in the CFG.  */
8395       num_loops = 0;
8396       for (b = 0; b < n_basic_blocks; b++)
8397         {
8398           basic_block header;
8399
8400           /* Search the nodes of the CFG in reverse completion order
8401              so that we can find outer loops first.  */
8402           header = BASIC_BLOCK (rc_order[b]);
8403
8404           /* Look for all the possible latch blocks for this header.  */
8405           for (e = header->pred; e; e = e->pred_next)
8406             {
8407               basic_block latch = e->src;
8408
8409               /* Look for back edges where a predecessor is dominated
8410                  by this block.  A natural loop has a single entry
8411                  node (header) that dominates all the nodes in the
8412                  loop.  It also has single back edge to the header
8413                  from a latch node.  Note that multiple natural loops
8414                  may share the same header.  */
8415               if (latch != ENTRY_BLOCK_PTR
8416                   && TEST_BIT (dom[latch->index], header->index))
8417                 {
8418                   struct loop *loop;
8419
8420                   loop = loops->array + num_loops;
8421
8422                   loop->header = header;
8423                   loop->latch = latch;
8424                   loop->num = num_loops;
8425
8426                   num_loops++;
8427                 }
8428             }
8429         }
8430
8431       for (i = 0; i < num_loops; i++)
8432         {
8433           struct loop *loop = &loops->array[i];
8434
8435           /* Keep track of blocks that are loop headers so
8436              that we can tell which loops should be merged.  */
8437           if (TEST_BIT (headers, loop->header->index))
8438             SET_BIT (loops->shared_headers, loop->header->index);
8439           SET_BIT (headers, loop->header->index);
8440
8441           /* Find nodes contained within the loop.  */
8442           loop->nodes = sbitmap_alloc (n_basic_blocks);
8443           loop->num_nodes
8444             = flow_loop_nodes_find (loop->header, loop->latch, loop->nodes);
8445
8446           /* Compute first and last blocks within the loop.
8447              These are often the same as the loop header and
8448              loop latch respectively, but this is not always
8449              the case.  */
8450           loop->first
8451             = BASIC_BLOCK (sbitmap_first_set_bit (loop->nodes));
8452           loop->last
8453             = BASIC_BLOCK (sbitmap_last_set_bit (loop->nodes));
8454
8455           flow_loop_scan (loops, loop, flags);
8456         }
8457
8458       /* Natural loops with shared headers may either be disjoint or
8459          nested.  Disjoint loops with shared headers cannot be inner
8460          loops and should be merged.  For now just mark loops that share
8461          headers.  */
8462       for (i = 0; i < num_loops; i++)
8463         if (TEST_BIT (loops->shared_headers, loops->array[i].header->index))
8464           loops->array[i].shared = 1;
8465
8466       sbitmap_free (headers);
8467     }
8468
8469   loops->num = num_loops;
8470
8471   /* Build the loop hierarchy tree.  */
8472   flow_loops_tree_build (loops);
8473
8474   /* Assign the loop nesting depth and enclosed loop level for each
8475      loop.  */
8476   loops->levels = flow_loops_level_compute (loops);
8477
8478   return num_loops;
8479 }
8480
8481
8482 /* Update the information regarding the loops in the CFG
8483    specified by LOOPS.  */
8484 int
8485 flow_loops_update (loops, flags)
8486      struct loops *loops;
8487      int flags;
8488 {
8489   /* One day we may want to update the current loop data.  For now
8490      throw away the old stuff and rebuild what we need.  */
8491   if (loops->array)
8492     flow_loops_free (loops);
8493
8494   return flow_loops_find (loops, flags);
8495 }
8496
8497
8498 /* Return non-zero if edge E enters header of LOOP from outside of LOOP.  */
8499
8500 int
8501 flow_loop_outside_edge_p (loop, e)
8502      const struct loop *loop;
8503      edge e;
8504 {
8505   if (e->dest != loop->header)
8506     abort ();
8507   return (e->src == ENTRY_BLOCK_PTR)
8508     || ! TEST_BIT (loop->nodes, e->src->index);
8509 }
8510
8511 /* Clear LOG_LINKS fields of insns in a chain.
8512    Also clear the global_live_at_{start,end} fields of the basic block
8513    structures.  */
8514
8515 void
8516 clear_log_links (insns)
8517      rtx insns;
8518 {
8519   rtx i;
8520   int b;
8521
8522   for (i = insns; i; i = NEXT_INSN (i))
8523     if (INSN_P (i))
8524       LOG_LINKS (i) = 0;
8525
8526   for (b = 0; b < n_basic_blocks; b++)
8527     {
8528       basic_block bb = BASIC_BLOCK (b);
8529
8530       bb->global_live_at_start = NULL;
8531       bb->global_live_at_end = NULL;
8532     }
8533
8534   ENTRY_BLOCK_PTR->global_live_at_end = NULL;
8535   EXIT_BLOCK_PTR->global_live_at_start = NULL;
8536 }
8537
8538 /* Given a register bitmap, turn on the bits in a HARD_REG_SET that
8539    correspond to the hard registers, if any, set in that map.  This
8540    could be done far more efficiently by having all sorts of special-cases
8541    with moving single words, but probably isn't worth the trouble.  */
8542
8543 void
8544 reg_set_to_hard_reg_set (to, from)
8545      HARD_REG_SET *to;
8546      bitmap from;
8547 {
8548   int i;
8549
8550   EXECUTE_IF_SET_IN_BITMAP
8551     (from, 0, i,
8552      {
8553        if (i >= FIRST_PSEUDO_REGISTER)
8554          return;
8555        SET_HARD_REG_BIT (*to, i);
8556      });
8557 }
8558
8559 /* Called once at intialization time.  */
8560
8561 void
8562 init_flow ()
8563 {
8564   static int initialized;
8565
8566   if (!initialized)
8567     {
8568       gcc_obstack_init (&flow_obstack);
8569       flow_firstobj = (char *) obstack_alloc (&flow_obstack, 0);
8570       initialized = 1;
8571     }
8572   else
8573     {
8574       obstack_free (&flow_obstack, flow_firstobj);
8575       flow_firstobj = (char *) obstack_alloc (&flow_obstack, 0);
8576     }
8577 }