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