1 /* Data flow analysis for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* This file contains the data flow analysis pass of the compiler. It
24 computes data flow information which tells combine_instructions
25 which insns to consider combining and controls register allocation.
27 Additional data flow information that is too bulky to record is
28 generated during the analysis, and is used at that time to create
29 autoincrement and autodecrement addressing.
31 The first step is dividing the function into basic blocks.
32 find_basic_blocks does this. Then life_analysis determines
33 where each register is live and where it is dead.
35 ** find_basic_blocks **
37 find_basic_blocks divides the current function's rtl into basic
38 blocks and constructs the CFG. The blocks are recorded in the
39 basic_block_info array; the CFG exists in the edge structures
40 referenced by the blocks.
42 find_basic_blocks also finds any unreachable loops and deletes them.
46 life_analysis is called immediately after find_basic_blocks.
47 It uses the basic block information to determine where each
48 hard or pseudo register is live.
50 ** live-register info **
52 The information about where each register is live is in two parts:
53 the REG_NOTES of insns, and the vector basic_block->global_live_at_start.
55 basic_block->global_live_at_start has an element for each basic
56 block, and the element is a bit-vector with a bit for each hard or
57 pseudo register. The bit is 1 if the register is live at the
58 beginning of the basic block.
60 Two types of elements can be added to an insn's REG_NOTES.
61 A REG_DEAD note is added to an insn's REG_NOTES for any register
62 that meets both of two conditions: The value in the register is not
63 needed in subsequent insns and the insn does not replace the value in
64 the register (in the case of multi-word hard registers, the value in
65 each register must be replaced by the insn to avoid a REG_DEAD note).
67 In the vast majority of cases, an object in a REG_DEAD note will be
68 used somewhere in the insn. The (rare) exception to this is if an
69 insn uses a multi-word hard register and only some of the registers are
70 needed in subsequent insns. In that case, REG_DEAD notes will be
71 provided for those hard registers that are not subsequently needed.
72 Partial REG_DEAD notes of this type do not occur when an insn sets
73 only some of the hard registers used in such a multi-word operand;
74 omitting REG_DEAD notes for objects stored in an insn is optional and
75 the desire to do so does not justify the complexity of the partial
78 REG_UNUSED notes are added for each register that is set by the insn
79 but is unused subsequently (if every register set by the insn is unused
80 and the insn does not reference memory or have some other side-effect,
81 the insn is deleted instead). If only part of a multi-word hard
82 register is used in a subsequent insn, REG_UNUSED notes are made for
83 the parts that will not be used.
85 To determine which registers are live after any insn, one can
86 start from the beginning of the basic block and scan insns, noting
87 which registers are set by each insn and which die there.
89 ** Other actions of life_analysis **
91 life_analysis sets up the LOG_LINKS fields of insns because the
92 information needed to do so is readily available.
94 life_analysis deletes insns whose only effect is to store a value
97 life_analysis notices cases where a reference to a register as
98 a memory address can be combined with a preceding or following
99 incrementation or decrementation of the register. The separate
100 instruction to increment or decrement is deleted and the address
101 is changed to a POST_INC or similar rtx.
103 Each time an incrementing or decrementing address is created,
104 a REG_INC element is added to the insn's REG_NOTES list.
106 life_analysis fills in certain vectors containing information about
107 register usage: REG_N_REFS, REG_N_DEATHS, REG_N_SETS, REG_LIVE_LENGTH,
108 REG_N_CALLS_CROSSED and REG_BASIC_BLOCK.
110 life_analysis sets current_function_sp_is_unchanging if the function
111 doesn't modify the stack pointer. */
115 Split out from life_analysis:
116 - local property discovery (bb->local_live, bb->local_set)
117 - global property computation
119 - pre/post modify transformation
127 #include "basic-block.h"
128 #include "insn-config.h"
130 #include "hard-reg-set.h"
133 #include "function.h"
137 #include "insn-flags.h"
141 #include "splay-tree.h"
143 #define obstack_chunk_alloc xmalloc
144 #define obstack_chunk_free free
147 /* EXIT_IGNORE_STACK should be nonzero if, when returning from a function,
148 the stack pointer does not matter. The value is tested only in
149 functions that have frame pointers.
150 No definition is equivalent to always zero. */
151 #ifndef EXIT_IGNORE_STACK
152 #define EXIT_IGNORE_STACK 0
155 #ifndef HAVE_epilogue
156 #define HAVE_epilogue 0
158 #ifndef HAVE_prologue
159 #define HAVE_prologue 0
161 #ifndef HAVE_sibcall_epilogue
162 #define HAVE_sibcall_epilogue 0
165 /* The contents of the current function definition are allocated
166 in this obstack, and all are freed at the end of the function.
167 For top-level functions, this is temporary_obstack.
168 Separate obstacks are made for nested functions. */
170 extern struct obstack *function_obstack;
172 /* Number of basic blocks in the current function. */
176 /* Number of edges in the current function. */
180 /* The basic block array. */
182 varray_type basic_block_info;
184 /* The special entry and exit blocks. */
186 struct basic_block_def entry_exit_blocks[2]
191 NULL, /* local_set */
192 NULL, /* global_live_at_start */
193 NULL, /* global_live_at_end */
195 ENTRY_BLOCK, /* index */
197 -1, -1 /* eh_beg, eh_end */
204 NULL, /* local_set */
205 NULL, /* global_live_at_start */
206 NULL, /* global_live_at_end */
208 EXIT_BLOCK, /* index */
210 -1, -1 /* eh_beg, eh_end */
214 /* Nonzero if the second flow pass has completed. */
217 /* Maximum register number used in this function, plus one. */
221 /* Indexed by n, giving various register information */
223 varray_type reg_n_info;
225 /* Size of a regset for the current function,
226 in (1) bytes and (2) elements. */
231 /* Regset of regs live when calls to `setjmp'-like functions happen. */
232 /* ??? Does this exist only for the setjmp-clobbered warning message? */
234 regset regs_live_at_setjmp;
236 /* List made of EXPR_LIST rtx's which gives pairs of pseudo registers
237 that have to go in the same hard reg.
238 The first two regs in the list are a pair, and the next two
239 are another pair, etc. */
242 /* Set of registers that may be eliminable. These are handled specially
243 in updating regs_ever_live. */
245 static HARD_REG_SET elim_reg_set;
247 /* The basic block structure for every insn, indexed by uid. */
249 varray_type basic_block_for_insn;
251 /* The labels mentioned in non-jump rtl. Valid during find_basic_blocks. */
252 /* ??? Should probably be using LABEL_NUSES instead. It would take a
253 bit of surgery to be able to use or co-opt the routines in jump. */
255 static rtx label_value_list;
256 static rtx tail_recursion_label_list;
258 /* Holds information for tracking conditional register life information. */
259 struct reg_cond_life_info
261 /* An EXPR_LIST of conditions under which a register is dead. */
264 /* ??? Could store mask of bytes that are dead, so that we could finally
265 track lifetimes of multi-word registers accessed via subregs. */
268 /* For use in communicating between propagate_block and its subroutines.
269 Holds all information needed to compute life and def-use information. */
271 struct propagate_block_info
273 /* The basic block we're considering. */
276 /* Bit N is set if register N is conditionally or unconditionally live. */
279 /* Bit N is set if register N is set this insn. */
282 /* Element N is the next insn that uses (hard or pseudo) register N
283 within the current basic block; or zero, if there is no such insn. */
286 /* Contains a list of all the MEMs we are tracking for dead store
290 /* If non-null, record the set of registers set in the basic block. */
293 #ifdef HAVE_conditional_execution
294 /* Indexed by register number, holds a reg_cond_life_info for each
295 register that is not unconditionally live or dead. */
296 splay_tree reg_cond_dead;
298 /* Bit N is set if register N is in an expression in reg_cond_dead. */
302 /* Non-zero if the value of CC0 is live. */
305 /* Flags controling the set of information propagate_block collects. */
309 /* Forward declarations */
310 static int count_basic_blocks PARAMS ((rtx));
311 static void find_basic_blocks_1 PARAMS ((rtx));
312 static void clear_edges PARAMS ((void));
313 static void make_edges PARAMS ((rtx));
314 static void make_label_edge PARAMS ((sbitmap *, basic_block,
316 static void make_eh_edge PARAMS ((sbitmap *, eh_nesting_info *,
317 basic_block, rtx, int));
318 static void mark_critical_edges PARAMS ((void));
319 static void move_stray_eh_region_notes PARAMS ((void));
320 static void record_active_eh_regions PARAMS ((rtx));
322 static void commit_one_edge_insertion PARAMS ((edge));
324 static void delete_unreachable_blocks PARAMS ((void));
325 static void delete_eh_regions PARAMS ((void));
326 static int can_delete_note_p PARAMS ((rtx));
327 static void expunge_block PARAMS ((basic_block));
328 static int can_delete_label_p PARAMS ((rtx));
329 static int tail_recursion_label_p PARAMS ((rtx));
330 static int merge_blocks_move_predecessor_nojumps PARAMS ((basic_block,
332 static int merge_blocks_move_successor_nojumps PARAMS ((basic_block,
334 static int merge_blocks PARAMS ((edge,basic_block,basic_block));
335 static void try_merge_blocks PARAMS ((void));
336 static void tidy_fallthru_edges PARAMS ((void));
337 static int verify_wide_reg_1 PARAMS ((rtx *, void *));
338 static void verify_wide_reg PARAMS ((int, rtx, rtx));
339 static void verify_local_live_at_start PARAMS ((regset, basic_block));
340 static int set_noop_p PARAMS ((rtx));
341 static int noop_move_p PARAMS ((rtx));
342 static void delete_noop_moves PARAMS ((rtx));
343 static void notice_stack_pointer_modification_1 PARAMS ((rtx, rtx, void *));
344 static void notice_stack_pointer_modification PARAMS ((rtx));
345 static void mark_reg PARAMS ((rtx, void *));
346 static void mark_regs_live_at_end PARAMS ((regset));
347 static int set_phi_alternative_reg PARAMS ((rtx, int, int, void *));
348 static void calculate_global_regs_live PARAMS ((sbitmap, sbitmap, int));
349 static void propagate_block_delete_insn PARAMS ((basic_block, rtx));
350 static rtx propagate_block_delete_libcall PARAMS ((basic_block, rtx, rtx));
351 static int insn_dead_p PARAMS ((struct propagate_block_info *,
353 static int libcall_dead_p PARAMS ((struct propagate_block_info *,
355 static void mark_set_regs PARAMS ((struct propagate_block_info *,
357 static void mark_set_1 PARAMS ((struct propagate_block_info *,
358 enum rtx_code, rtx, rtx,
360 #ifdef HAVE_conditional_execution
361 static int mark_regno_cond_dead PARAMS ((struct propagate_block_info *,
363 static void free_reg_cond_life_info PARAMS ((splay_tree_value));
364 static int flush_reg_cond_reg_1 PARAMS ((splay_tree_node, void *));
365 static void flush_reg_cond_reg PARAMS ((struct propagate_block_info *,
367 static rtx ior_reg_cond PARAMS ((rtx, rtx));
368 static rtx not_reg_cond PARAMS ((rtx));
369 static rtx nand_reg_cond PARAMS ((rtx, rtx));
372 static void find_auto_inc PARAMS ((struct propagate_block_info *,
374 static int try_pre_increment_1 PARAMS ((struct propagate_block_info *,
376 static int try_pre_increment PARAMS ((rtx, rtx, HOST_WIDE_INT));
378 static void mark_used_reg PARAMS ((struct propagate_block_info *,
380 static void mark_used_regs PARAMS ((struct propagate_block_info *,
382 void dump_flow_info PARAMS ((FILE *));
383 void debug_flow_info PARAMS ((void));
384 static void dump_edge_info PARAMS ((FILE *, edge, int));
386 static void invalidate_mems_from_autoinc PARAMS ((struct propagate_block_info *,
388 static void remove_fake_successors PARAMS ((basic_block));
389 static void flow_nodes_print PARAMS ((const char *, const sbitmap, FILE *));
390 static void flow_exits_print PARAMS ((const char *, const edge *, int, FILE *));
391 static void flow_loops_cfg_dump PARAMS ((const struct loops *, FILE *));
392 static int flow_loop_nested_p PARAMS ((struct loop *, struct loop *));
393 static int flow_loop_exits_find PARAMS ((const sbitmap, edge **));
394 static int flow_loop_nodes_find PARAMS ((basic_block, basic_block, sbitmap));
395 static int flow_depth_first_order_compute PARAMS ((int *));
396 static basic_block flow_loop_pre_header_find PARAMS ((basic_block, const sbitmap *));
397 static void flow_loop_tree_node_add PARAMS ((struct loop *, struct loop *));
398 static void flow_loops_tree_build PARAMS ((struct loops *));
399 static int flow_loop_level_compute PARAMS ((struct loop *, int));
400 static int flow_loops_level_compute PARAMS ((struct loops *));
402 /* Find basic blocks of the current function.
403 F is the first insn of the function and NREGS the number of register
407 find_basic_blocks (f, nregs, file)
409 int nregs ATTRIBUTE_UNUSED;
410 FILE *file ATTRIBUTE_UNUSED;
414 /* Flush out existing data. */
415 if (basic_block_info != NULL)
421 /* Clear bb->aux on all extant basic blocks. We'll use this as a
422 tag for reuse during create_basic_block, just in case some pass
423 copies around basic block notes improperly. */
424 for (i = 0; i < n_basic_blocks; ++i)
425 BASIC_BLOCK (i)->aux = NULL;
427 VARRAY_FREE (basic_block_info);
430 n_basic_blocks = count_basic_blocks (f);
432 /* Size the basic block table. The actual structures will be allocated
433 by find_basic_blocks_1, since we want to keep the structure pointers
434 stable across calls to find_basic_blocks. */
435 /* ??? This whole issue would be much simpler if we called find_basic_blocks
436 exactly once, and thereafter we don't have a single long chain of
437 instructions at all until close to the end of compilation when we
438 actually lay them out. */
440 VARRAY_BB_INIT (basic_block_info, n_basic_blocks, "basic_block_info");
442 find_basic_blocks_1 (f);
444 /* Record the block to which an insn belongs. */
445 /* ??? This should be done another way, by which (perhaps) a label is
446 tagged directly with the basic block that it starts. It is used for
447 more than that currently, but IMO that is the only valid use. */
449 max_uid = get_max_uid ();
451 /* Leave space for insns life_analysis makes in some cases for auto-inc.
452 These cases are rare, so we don't need too much space. */
453 max_uid += max_uid / 10;
456 compute_bb_for_insn (max_uid);
458 /* Discover the edges of our cfg. */
459 record_active_eh_regions (f);
460 make_edges (label_value_list);
462 /* Do very simple cleanup now, for the benefit of code that runs between
463 here and cleanup_cfg, e.g. thread_prologue_and_epilogue_insns. */
464 tidy_fallthru_edges ();
466 mark_critical_edges ();
468 #ifdef ENABLE_CHECKING
473 /* Count the basic blocks of the function. */
476 count_basic_blocks (f)
480 register RTX_CODE prev_code;
481 register int count = 0;
483 int call_had_abnormal_edge = 0;
485 prev_code = JUMP_INSN;
486 for (insn = f; insn; insn = NEXT_INSN (insn))
488 register RTX_CODE code = GET_CODE (insn);
490 if (code == CODE_LABEL
491 || (GET_RTX_CLASS (code) == 'i'
492 && (prev_code == JUMP_INSN
493 || prev_code == BARRIER
494 || (prev_code == CALL_INSN && call_had_abnormal_edge))))
497 /* Record whether this call created an edge. */
498 if (code == CALL_INSN)
500 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
501 int region = (note ? INTVAL (XEXP (note, 0)) : 1);
503 call_had_abnormal_edge = 0;
505 /* If there is an EH region or rethrow, we have an edge. */
506 if ((eh_region && region > 0)
507 || find_reg_note (insn, REG_EH_RETHROW, NULL_RTX))
508 call_had_abnormal_edge = 1;
509 else if (nonlocal_goto_handler_labels && region >= 0)
510 /* If there is a nonlocal goto label and the specified
511 region number isn't -1, we have an edge. (0 means
512 no throw, but might have a nonlocal goto). */
513 call_had_abnormal_edge = 1;
518 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG)
520 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END)
524 /* The rest of the compiler works a bit smoother when we don't have to
525 check for the edge case of do-nothing functions with no basic blocks. */
528 emit_insn (gen_rtx_USE (VOIDmode, const0_rtx));
535 /* Find all basic blocks of the function whose first insn is F.
537 Collect and return a list of labels whose addresses are taken. This
538 will be used in make_edges for use with computed gotos. */
541 find_basic_blocks_1 (f)
544 register rtx insn, next;
546 rtx bb_note = NULL_RTX;
547 rtx eh_list = NULL_RTX;
553 /* We process the instructions in a slightly different way than we did
554 previously. This is so that we see a NOTE_BASIC_BLOCK after we have
555 closed out the previous block, so that it gets attached at the proper
556 place. Since this form should be equivalent to the previous,
557 count_basic_blocks continues to use the old form as a check. */
559 for (insn = f; insn; insn = next)
561 enum rtx_code code = GET_CODE (insn);
563 next = NEXT_INSN (insn);
569 int kind = NOTE_LINE_NUMBER (insn);
571 /* Keep a LIFO list of the currently active exception notes. */
572 if (kind == NOTE_INSN_EH_REGION_BEG)
573 eh_list = alloc_INSN_LIST (insn, eh_list);
574 else if (kind == NOTE_INSN_EH_REGION_END)
578 eh_list = XEXP (eh_list, 1);
579 free_INSN_LIST_node (t);
582 /* Look for basic block notes with which to keep the
583 basic_block_info pointers stable. Unthread the note now;
584 we'll put it back at the right place in create_basic_block.
585 Or not at all if we've already found a note in this block. */
586 else if (kind == NOTE_INSN_BASIC_BLOCK)
588 if (bb_note == NULL_RTX)
591 next = flow_delete_insn (insn);
597 /* A basic block starts at a label. If we've closed one off due
598 to a barrier or some such, no need to do it again. */
599 if (head != NULL_RTX)
601 /* While we now have edge lists with which other portions of
602 the compiler might determine a call ending a basic block
603 does not imply an abnormal edge, it will be a bit before
604 everything can be updated. So continue to emit a noop at
605 the end of such a block. */
606 if (GET_CODE (end) == CALL_INSN && ! SIBLING_CALL_P (end))
608 rtx nop = gen_rtx_USE (VOIDmode, const0_rtx);
609 end = emit_insn_after (nop, end);
612 create_basic_block (i++, head, end, bb_note);
620 /* A basic block ends at a jump. */
621 if (head == NULL_RTX)
625 /* ??? Make a special check for table jumps. The way this
626 happens is truly and amazingly gross. We are about to
627 create a basic block that contains just a code label and
628 an addr*vec jump insn. Worse, an addr_diff_vec creates
629 its own natural loop.
631 Prevent this bit of brain damage, pasting things together
632 correctly in make_edges.
634 The correct solution involves emitting the table directly
635 on the tablejump instruction as a note, or JUMP_LABEL. */
637 if (GET_CODE (PATTERN (insn)) == ADDR_VEC
638 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
646 goto new_bb_inclusive;
649 /* A basic block ends at a barrier. It may be that an unconditional
650 jump already closed the basic block -- no need to do it again. */
651 if (head == NULL_RTX)
654 /* While we now have edge lists with which other portions of the
655 compiler might determine a call ending a basic block does not
656 imply an abnormal edge, it will be a bit before everything can
657 be updated. So continue to emit a noop at the end of such a
659 if (GET_CODE (end) == CALL_INSN && ! SIBLING_CALL_P (end))
661 rtx nop = gen_rtx_USE (VOIDmode, const0_rtx);
662 end = emit_insn_after (nop, end);
664 goto new_bb_exclusive;
668 /* Record whether this call created an edge. */
669 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
670 int region = (note ? INTVAL (XEXP (note, 0)) : 1);
671 int call_has_abnormal_edge = 0;
673 /* If this is a call placeholder, record its tail recursion
675 if (GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER
676 && XEXP (PATTERN (insn), 3) != NULL_RTX)
677 trll = alloc_EXPR_LIST (0, XEXP (PATTERN (insn), 3), trll);
679 /* If there is an EH region or rethrow, we have an edge. */
680 if ((eh_list && region > 0)
681 || find_reg_note (insn, REG_EH_RETHROW, NULL_RTX))
682 call_has_abnormal_edge = 1;
683 else if (nonlocal_goto_handler_labels && region >= 0)
684 /* If there is a nonlocal goto label and the specified
685 region number isn't -1, we have an edge. (0 means
686 no throw, but might have a nonlocal goto). */
687 call_has_abnormal_edge = 1;
689 /* A basic block ends at a call that can either throw or
690 do a non-local goto. */
691 if (call_has_abnormal_edge)
694 if (head == NULL_RTX)
699 create_basic_block (i++, head, end, bb_note);
700 head = end = NULL_RTX;
708 if (GET_RTX_CLASS (code) == 'i')
710 if (head == NULL_RTX)
717 if (GET_RTX_CLASS (code) == 'i')
721 /* Make a list of all labels referred to other than by jumps
722 (which just don't have the REG_LABEL notes).
724 Make a special exception for labels followed by an ADDR*VEC,
725 as this would be a part of the tablejump setup code.
727 Make a special exception for the eh_return_stub_label, which
728 we know isn't part of any otherwise visible control flow. */
730 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
731 if (REG_NOTE_KIND (note) == REG_LABEL)
733 rtx lab = XEXP (note, 0), next;
735 if (lab == eh_return_stub_label)
737 else if ((next = next_nonnote_insn (lab)) != NULL
738 && GET_CODE (next) == JUMP_INSN
739 && (GET_CODE (PATTERN (next)) == ADDR_VEC
740 || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC))
742 else if (GET_CODE (lab) == NOTE)
745 lvl = alloc_EXPR_LIST (0, XEXP (note, 0), lvl);
750 if (head != NULL_RTX)
751 create_basic_block (i++, head, end, bb_note);
753 flow_delete_insn (bb_note);
755 if (i != n_basic_blocks)
758 label_value_list = lvl;
759 tail_recursion_label_list = trll;
762 /* Tidy the CFG by deleting unreachable code and whatnot. */
768 delete_unreachable_blocks ();
769 move_stray_eh_region_notes ();
770 record_active_eh_regions (f);
772 mark_critical_edges ();
774 /* Kill the data we won't maintain. */
775 free_EXPR_LIST_list (&label_value_list);
776 free_EXPR_LIST_list (&tail_recursion_label_list);
779 /* Create a new basic block consisting of the instructions between
780 HEAD and END inclusive. Reuses the note and basic block struct
781 in BB_NOTE, if any. */
784 create_basic_block (index, head, end, bb_note)
786 rtx head, end, bb_note;
791 && ! RTX_INTEGRATED_P (bb_note)
792 && (bb = NOTE_BASIC_BLOCK (bb_note)) != NULL
795 /* If we found an existing note, thread it back onto the chain. */
799 if (GET_CODE (head) == CODE_LABEL)
803 after = PREV_INSN (head);
807 if (after != bb_note && NEXT_INSN (after) != bb_note)
808 reorder_insns (bb_note, bb_note, after);
812 /* Otherwise we must create a note and a basic block structure.
813 Since we allow basic block structs in rtl, give the struct
814 the same lifetime by allocating it off the function obstack
815 rather than using malloc. */
817 bb = (basic_block) obstack_alloc (function_obstack, sizeof (*bb));
818 memset (bb, 0, sizeof (*bb));
820 if (GET_CODE (head) == CODE_LABEL)
821 bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK, head);
824 bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, head);
827 NOTE_BASIC_BLOCK (bb_note) = bb;
830 /* Always include the bb note in the block. */
831 if (NEXT_INSN (end) == bb_note)
837 BASIC_BLOCK (index) = bb;
839 /* Tag the block so that we know it has been used when considering
840 other basic block notes. */
844 /* Records the basic block struct in BB_FOR_INSN, for every instruction
845 indexed by INSN_UID. MAX is the size of the array. */
848 compute_bb_for_insn (max)
853 if (basic_block_for_insn)
854 VARRAY_FREE (basic_block_for_insn);
855 VARRAY_BB_INIT (basic_block_for_insn, max, "basic_block_for_insn");
857 for (i = 0; i < n_basic_blocks; ++i)
859 basic_block bb = BASIC_BLOCK (i);
866 int uid = INSN_UID (insn);
868 VARRAY_BB (basic_block_for_insn, uid) = bb;
871 insn = NEXT_INSN (insn);
876 /* Free the memory associated with the edge structures. */
884 for (i = 0; i < n_basic_blocks; ++i)
886 basic_block bb = BASIC_BLOCK (i);
888 for (e = bb->succ; e ; e = n)
898 for (e = ENTRY_BLOCK_PTR->succ; e ; e = n)
904 ENTRY_BLOCK_PTR->succ = 0;
905 EXIT_BLOCK_PTR->pred = 0;
910 /* Identify the edges between basic blocks.
912 NONLOCAL_LABEL_LIST is a list of non-local labels in the function. Blocks
913 that are otherwise unreachable may be reachable with a non-local goto.
915 BB_EH_END is an array indexed by basic block number in which we record
916 the list of exception regions active at the end of the basic block. */
919 make_edges (label_value_list)
920 rtx label_value_list;
923 eh_nesting_info *eh_nest_info = init_eh_nesting_info ();
924 sbitmap *edge_cache = NULL;
926 /* Assume no computed jump; revise as we create edges. */
927 current_function_has_computed_jump = 0;
929 /* Heavy use of computed goto in machine-generated code can lead to
930 nearly fully-connected CFGs. In that case we spend a significant
931 amount of time searching the edge lists for duplicates. */
932 if (forced_labels || label_value_list)
934 edge_cache = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
935 sbitmap_vector_zero (edge_cache, n_basic_blocks);
938 /* By nature of the way these get numbered, block 0 is always the entry. */
939 make_edge (edge_cache, ENTRY_BLOCK_PTR, BASIC_BLOCK (0), EDGE_FALLTHRU);
941 for (i = 0; i < n_basic_blocks; ++i)
943 basic_block bb = BASIC_BLOCK (i);
946 int force_fallthru = 0;
948 /* Examine the last instruction of the block, and discover the
949 ways we can leave the block. */
952 code = GET_CODE (insn);
955 if (code == JUMP_INSN)
959 /* ??? Recognize a tablejump and do the right thing. */
960 if ((tmp = JUMP_LABEL (insn)) != NULL_RTX
961 && (tmp = NEXT_INSN (tmp)) != NULL_RTX
962 && GET_CODE (tmp) == JUMP_INSN
963 && (GET_CODE (PATTERN (tmp)) == ADDR_VEC
964 || GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC))
969 if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
970 vec = XVEC (PATTERN (tmp), 0);
972 vec = XVEC (PATTERN (tmp), 1);
974 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
975 make_label_edge (edge_cache, bb,
976 XEXP (RTVEC_ELT (vec, j), 0), 0);
978 /* Some targets (eg, ARM) emit a conditional jump that also
979 contains the out-of-range target. Scan for these and
980 add an edge if necessary. */
981 if ((tmp = single_set (insn)) != NULL
982 && SET_DEST (tmp) == pc_rtx
983 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
984 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
985 make_label_edge (edge_cache, bb,
986 XEXP (XEXP (SET_SRC (tmp), 2), 0), 0);
988 #ifdef CASE_DROPS_THROUGH
989 /* Silly VAXen. The ADDR_VEC is going to be in the way of
990 us naturally detecting fallthru into the next block. */
995 /* If this is a computed jump, then mark it as reaching
996 everything on the label_value_list and forced_labels list. */
997 else if (computed_jump_p (insn))
999 current_function_has_computed_jump = 1;
1001 for (x = label_value_list; x; x = XEXP (x, 1))
1002 make_label_edge (edge_cache, bb, XEXP (x, 0), EDGE_ABNORMAL);
1004 for (x = forced_labels; x; x = XEXP (x, 1))
1005 make_label_edge (edge_cache, bb, XEXP (x, 0), EDGE_ABNORMAL);
1008 /* Returns create an exit out. */
1009 else if (returnjump_p (insn))
1010 make_edge (edge_cache, bb, EXIT_BLOCK_PTR, 0);
1012 /* Otherwise, we have a plain conditional or unconditional jump. */
1015 if (! JUMP_LABEL (insn))
1017 make_label_edge (edge_cache, bb, JUMP_LABEL (insn), 0);
1021 /* If this is a sibling call insn, then this is in effect a
1022 combined call and return, and so we need an edge to the
1023 exit block. No need to worry about EH edges, since we
1024 wouldn't have created the sibling call in the first place. */
1026 if (code == CALL_INSN && SIBLING_CALL_P (insn))
1027 make_edge (edge_cache, bb, EXIT_BLOCK_PTR, 0);
1030 /* If this is a CALL_INSN, then mark it as reaching the active EH
1031 handler for this CALL_INSN. If we're handling asynchronous
1032 exceptions then any insn can reach any of the active handlers.
1034 Also mark the CALL_INSN as reaching any nonlocal goto handler. */
1036 if (code == CALL_INSN || asynchronous_exceptions)
1038 /* Add any appropriate EH edges. We do this unconditionally
1039 since there may be a REG_EH_REGION or REG_EH_RETHROW note
1040 on the call, and this needn't be within an EH region. */
1041 make_eh_edge (edge_cache, eh_nest_info, bb, insn, bb->eh_end);
1043 /* If we have asynchronous exceptions, do the same for *all*
1044 exception regions active in the block. */
1045 if (asynchronous_exceptions
1046 && bb->eh_beg != bb->eh_end)
1048 if (bb->eh_beg >= 0)
1049 make_eh_edge (edge_cache, eh_nest_info, bb,
1050 NULL_RTX, bb->eh_beg);
1052 for (x = bb->head; x != bb->end; x = NEXT_INSN (x))
1053 if (GET_CODE (x) == NOTE
1054 && (NOTE_LINE_NUMBER (x) == NOTE_INSN_EH_REGION_BEG
1055 || NOTE_LINE_NUMBER (x) == NOTE_INSN_EH_REGION_END))
1057 int region = NOTE_EH_HANDLER (x);
1058 make_eh_edge (edge_cache, eh_nest_info, bb,
1063 if (code == CALL_INSN && nonlocal_goto_handler_labels)
1065 /* ??? This could be made smarter: in some cases it's possible
1066 to tell that certain calls will not do a nonlocal goto.
1068 For example, if the nested functions that do the nonlocal
1069 gotos do not have their addresses taken, then only calls to
1070 those functions or to other nested functions that use them
1071 could possibly do nonlocal gotos. */
1072 /* We do know that a REG_EH_REGION note with a value less
1073 than 0 is guaranteed not to perform a non-local goto. */
1074 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1075 if (!note || INTVAL (XEXP (note, 0)) >= 0)
1076 for (x = nonlocal_goto_handler_labels; x ; x = XEXP (x, 1))
1077 make_label_edge (edge_cache, bb, XEXP (x, 0),
1078 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
1082 /* We know something about the structure of the function __throw in
1083 libgcc2.c. It is the only function that ever contains eh_stub
1084 labels. It modifies its return address so that the last block
1085 returns to one of the eh_stub labels within it. So we have to
1086 make additional edges in the flow graph. */
1087 if (i + 1 == n_basic_blocks && eh_return_stub_label != 0)
1088 make_label_edge (edge_cache, bb, eh_return_stub_label, EDGE_EH);
1090 /* Find out if we can drop through to the next block. */
1091 insn = next_nonnote_insn (insn);
1092 if (!insn || (i + 1 == n_basic_blocks && force_fallthru))
1093 make_edge (edge_cache, bb, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
1094 else if (i + 1 < n_basic_blocks)
1096 rtx tmp = BLOCK_HEAD (i + 1);
1097 if (GET_CODE (tmp) == NOTE)
1098 tmp = next_nonnote_insn (tmp);
1099 if (force_fallthru || insn == tmp)
1100 make_edge (edge_cache, bb, BASIC_BLOCK (i + 1), EDGE_FALLTHRU);
1104 free_eh_nesting_info (eh_nest_info);
1106 sbitmap_vector_free (edge_cache);
1109 /* Create an edge between two basic blocks. FLAGS are auxiliary information
1110 about the edge that is accumulated between calls. */
1113 make_edge (edge_cache, src, dst, flags)
1114 sbitmap *edge_cache;
1115 basic_block src, dst;
1121 /* Don't bother with edge cache for ENTRY or EXIT; there aren't that
1122 many edges to them, and we didn't allocate memory for it. */
1123 use_edge_cache = (edge_cache
1124 && src != ENTRY_BLOCK_PTR
1125 && dst != EXIT_BLOCK_PTR);
1127 /* Make sure we don't add duplicate edges. */
1128 if (! use_edge_cache || TEST_BIT (edge_cache[src->index], dst->index))
1129 for (e = src->succ; e ; e = e->succ_next)
1136 e = (edge) xcalloc (1, sizeof (*e));
1139 e->succ_next = src->succ;
1140 e->pred_next = dst->pred;
1149 SET_BIT (edge_cache[src->index], dst->index);
1152 /* Create an edge from a basic block to a label. */
1155 make_label_edge (edge_cache, src, label, flags)
1156 sbitmap *edge_cache;
1161 if (GET_CODE (label) != CODE_LABEL)
1164 /* If the label was never emitted, this insn is junk, but avoid a
1165 crash trying to refer to BLOCK_FOR_INSN (label). This can happen
1166 as a result of a syntax error and a diagnostic has already been
1169 if (INSN_UID (label) == 0)
1172 make_edge (edge_cache, src, BLOCK_FOR_INSN (label), flags);
1175 /* Create the edges generated by INSN in REGION. */
1178 make_eh_edge (edge_cache, eh_nest_info, src, insn, region)
1179 sbitmap *edge_cache;
1180 eh_nesting_info *eh_nest_info;
1185 handler_info **handler_list;
1188 is_call = (insn && GET_CODE (insn) == CALL_INSN ? EDGE_ABNORMAL_CALL : 0);
1189 num = reachable_handlers (region, eh_nest_info, insn, &handler_list);
1192 make_label_edge (edge_cache, src, handler_list[num]->handler_label,
1193 EDGE_ABNORMAL | EDGE_EH | is_call);
1197 /* EH_REGION notes appearing between basic blocks is ambiguous, and even
1198 dangerous if we intend to move basic blocks around. Move such notes
1199 into the following block. */
1202 move_stray_eh_region_notes ()
1207 if (n_basic_blocks < 2)
1210 b2 = BASIC_BLOCK (n_basic_blocks - 1);
1211 for (i = n_basic_blocks - 2; i >= 0; --i, b2 = b1)
1213 rtx insn, next, list = NULL_RTX;
1215 b1 = BASIC_BLOCK (i);
1216 for (insn = NEXT_INSN (b1->end); insn != b2->head; insn = next)
1218 next = NEXT_INSN (insn);
1219 if (GET_CODE (insn) == NOTE
1220 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG
1221 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END))
1223 /* Unlink from the insn chain. */
1224 NEXT_INSN (PREV_INSN (insn)) = next;
1225 PREV_INSN (next) = PREV_INSN (insn);
1228 NEXT_INSN (insn) = list;
1233 if (list == NULL_RTX)
1236 /* Find where to insert these things. */
1238 if (GET_CODE (insn) == CODE_LABEL)
1239 insn = NEXT_INSN (insn);
1243 next = NEXT_INSN (list);
1244 add_insn_after (list, insn);
1250 /* Recompute eh_beg/eh_end for each basic block. */
1253 record_active_eh_regions (f)
1256 rtx insn, eh_list = NULL_RTX;
1258 basic_block bb = BASIC_BLOCK (0);
1260 for (insn = f; insn ; insn = NEXT_INSN (insn))
1262 if (bb->head == insn)
1263 bb->eh_beg = (eh_list ? NOTE_EH_HANDLER (XEXP (eh_list, 0)) : -1);
1265 if (GET_CODE (insn) == NOTE)
1267 int kind = NOTE_LINE_NUMBER (insn);
1268 if (kind == NOTE_INSN_EH_REGION_BEG)
1269 eh_list = alloc_INSN_LIST (insn, eh_list);
1270 else if (kind == NOTE_INSN_EH_REGION_END)
1272 rtx t = XEXP (eh_list, 1);
1273 free_INSN_LIST_node (eh_list);
1278 if (bb->end == insn)
1280 bb->eh_end = (eh_list ? NOTE_EH_HANDLER (XEXP (eh_list, 0)) : -1);
1282 if (i == n_basic_blocks)
1284 bb = BASIC_BLOCK (i);
1289 /* Identify critical edges and set the bits appropriately. */
1292 mark_critical_edges ()
1294 int i, n = n_basic_blocks;
1297 /* We begin with the entry block. This is not terribly important now,
1298 but could be if a front end (Fortran) implemented alternate entry
1300 bb = ENTRY_BLOCK_PTR;
1307 /* (1) Critical edges must have a source with multiple successors. */
1308 if (bb->succ && bb->succ->succ_next)
1310 for (e = bb->succ; e ; e = e->succ_next)
1312 /* (2) Critical edges must have a destination with multiple
1313 predecessors. Note that we know there is at least one
1314 predecessor -- the edge we followed to get here. */
1315 if (e->dest->pred->pred_next)
1316 e->flags |= EDGE_CRITICAL;
1318 e->flags &= ~EDGE_CRITICAL;
1323 for (e = bb->succ; e ; e = e->succ_next)
1324 e->flags &= ~EDGE_CRITICAL;
1329 bb = BASIC_BLOCK (i);
1333 /* Split a (typically critical) edge. Return the new block.
1334 Abort on abnormal edges.
1336 ??? The code generally expects to be called on critical edges.
1337 The case of a block ending in an unconditional jump to a
1338 block with multiple predecessors is not handled optimally. */
1341 split_edge (edge_in)
1344 basic_block old_pred, bb, old_succ;
1349 /* Abnormal edges cannot be split. */
1350 if ((edge_in->flags & EDGE_ABNORMAL) != 0)
1353 old_pred = edge_in->src;
1354 old_succ = edge_in->dest;
1356 /* Remove the existing edge from the destination's pred list. */
1359 for (pp = &old_succ->pred; *pp != edge_in; pp = &(*pp)->pred_next)
1361 *pp = edge_in->pred_next;
1362 edge_in->pred_next = NULL;
1365 /* Create the new structures. */
1366 bb = (basic_block) obstack_alloc (function_obstack, sizeof (*bb));
1367 edge_out = (edge) xcalloc (1, sizeof (*edge_out));
1370 memset (bb, 0, sizeof (*bb));
1372 /* ??? This info is likely going to be out of date very soon. */
1373 if (old_succ->global_live_at_start)
1375 bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (function_obstack);
1376 bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (function_obstack);
1377 COPY_REG_SET (bb->global_live_at_start, old_succ->global_live_at_start);
1378 COPY_REG_SET (bb->global_live_at_end, old_succ->global_live_at_start);
1383 bb->succ = edge_out;
1386 edge_in->flags &= ~EDGE_CRITICAL;
1388 edge_out->pred_next = old_succ->pred;
1389 edge_out->succ_next = NULL;
1391 edge_out->dest = old_succ;
1392 edge_out->flags = EDGE_FALLTHRU;
1393 edge_out->probability = REG_BR_PROB_BASE;
1395 old_succ->pred = edge_out;
1397 /* Tricky case -- if there existed a fallthru into the successor
1398 (and we're not it) we must add a new unconditional jump around
1399 the new block we're actually interested in.
1401 Further, if that edge is critical, this means a second new basic
1402 block must be created to hold it. In order to simplify correct
1403 insn placement, do this before we touch the existing basic block
1404 ordering for the block we were really wanting. */
1405 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1408 for (e = edge_out->pred_next; e ; e = e->pred_next)
1409 if (e->flags & EDGE_FALLTHRU)
1414 basic_block jump_block;
1417 if ((e->flags & EDGE_CRITICAL) == 0
1418 && e->src != ENTRY_BLOCK_PTR)
1420 /* Non critical -- we can simply add a jump to the end
1421 of the existing predecessor. */
1422 jump_block = e->src;
1426 /* We need a new block to hold the jump. The simplest
1427 way to do the bulk of the work here is to recursively
1429 jump_block = split_edge (e);
1430 e = jump_block->succ;
1433 /* Now add the jump insn ... */
1434 pos = emit_jump_insn_after (gen_jump (old_succ->head),
1436 jump_block->end = pos;
1437 if (basic_block_for_insn)
1438 set_block_for_insn (pos, jump_block);
1439 emit_barrier_after (pos);
1441 /* ... let jump know that label is in use, ... */
1442 JUMP_LABEL (pos) = old_succ->head;
1443 ++LABEL_NUSES (old_succ->head);
1445 /* ... and clear fallthru on the outgoing edge. */
1446 e->flags &= ~EDGE_FALLTHRU;
1448 /* Continue splitting the interesting edge. */
1452 /* Place the new block just in front of the successor. */
1453 VARRAY_GROW (basic_block_info, ++n_basic_blocks);
1454 if (old_succ == EXIT_BLOCK_PTR)
1455 j = n_basic_blocks - 1;
1457 j = old_succ->index;
1458 for (i = n_basic_blocks - 1; i > j; --i)
1460 basic_block tmp = BASIC_BLOCK (i - 1);
1461 BASIC_BLOCK (i) = tmp;
1464 BASIC_BLOCK (i) = bb;
1467 /* Create the basic block note.
1469 Where we place the note can have a noticable impact on the generated
1470 code. Consider this cfg:
1481 If we need to insert an insn on the edge from block 0 to block 1,
1482 we want to ensure the instructions we insert are outside of any
1483 loop notes that physically sit between block 0 and block 1. Otherwise
1484 we confuse the loop optimizer into thinking the loop is a phony. */
1485 if (old_succ != EXIT_BLOCK_PTR
1486 && PREV_INSN (old_succ->head)
1487 && GET_CODE (PREV_INSN (old_succ->head)) == NOTE
1488 && NOTE_LINE_NUMBER (PREV_INSN (old_succ->head)) == NOTE_INSN_LOOP_BEG)
1489 bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK,
1490 PREV_INSN (old_succ->head));
1491 else if (old_succ != EXIT_BLOCK_PTR)
1492 bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, old_succ->head);
1494 bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK, get_last_insn ());
1495 NOTE_BASIC_BLOCK (bb_note) = bb;
1496 bb->head = bb->end = bb_note;
1498 /* Not quite simple -- for non-fallthru edges, we must adjust the
1499 predecessor's jump instruction to target our new block. */
1500 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1502 rtx tmp, insn = old_pred->end;
1503 rtx old_label = old_succ->head;
1504 rtx new_label = gen_label_rtx ();
1506 if (GET_CODE (insn) != JUMP_INSN)
1509 /* ??? Recognize a tablejump and adjust all matching cases. */
1510 if ((tmp = JUMP_LABEL (insn)) != NULL_RTX
1511 && (tmp = NEXT_INSN (tmp)) != NULL_RTX
1512 && GET_CODE (tmp) == JUMP_INSN
1513 && (GET_CODE (PATTERN (tmp)) == ADDR_VEC
1514 || GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC))
1519 if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
1520 vec = XVEC (PATTERN (tmp), 0);
1522 vec = XVEC (PATTERN (tmp), 1);
1524 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
1525 if (XEXP (RTVEC_ELT (vec, j), 0) == old_label)
1527 RTVEC_ELT (vec, j) = gen_rtx_LABEL_REF (VOIDmode, new_label);
1528 --LABEL_NUSES (old_label);
1529 ++LABEL_NUSES (new_label);
1532 /* Handle casesi dispatch insns */
1533 if ((tmp = single_set (insn)) != NULL
1534 && SET_DEST (tmp) == pc_rtx
1535 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
1536 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF
1537 && XEXP (XEXP (SET_SRC (tmp), 2), 0) == old_label)
1539 XEXP (SET_SRC (tmp), 2) = gen_rtx_LABEL_REF (VOIDmode,
1541 --LABEL_NUSES (old_label);
1542 ++LABEL_NUSES (new_label);
1547 /* This would have indicated an abnormal edge. */
1548 if (computed_jump_p (insn))
1551 /* A return instruction can't be redirected. */
1552 if (returnjump_p (insn))
1555 /* If the insn doesn't go where we think, we're confused. */
1556 if (JUMP_LABEL (insn) != old_label)
1559 redirect_jump (insn, new_label, 0);
1562 emit_label_before (new_label, bb_note);
1563 bb->head = new_label;
1569 /* Queue instructions for insertion on an edge between two basic blocks.
1570 The new instructions and basic blocks (if any) will not appear in the
1571 CFG until commit_edge_insertions is called. */
1574 insert_insn_on_edge (pattern, e)
1578 /* We cannot insert instructions on an abnormal critical edge.
1579 It will be easier to find the culprit if we die now. */
1580 if ((e->flags & (EDGE_ABNORMAL|EDGE_CRITICAL))
1581 == (EDGE_ABNORMAL|EDGE_CRITICAL))
1584 if (e->insns == NULL_RTX)
1587 push_to_sequence (e->insns);
1589 emit_insn (pattern);
1591 e->insns = get_insns ();
1595 /* Update the CFG for the instructions queued on edge E. */
1598 commit_one_edge_insertion (e)
1601 rtx before = NULL_RTX, after = NULL_RTX, insns, tmp, last;
1604 /* Pull the insns off the edge now since the edge might go away. */
1606 e->insns = NULL_RTX;
1608 /* Figure out where to put these things. If the destination has
1609 one predecessor, insert there. Except for the exit block. */
1610 if (e->dest->pred->pred_next == NULL
1611 && e->dest != EXIT_BLOCK_PTR)
1615 /* Get the location correct wrt a code label, and "nice" wrt
1616 a basic block note, and before everything else. */
1618 if (GET_CODE (tmp) == CODE_LABEL)
1619 tmp = NEXT_INSN (tmp);
1620 if (GET_CODE (tmp) == NOTE
1621 && NOTE_LINE_NUMBER (tmp) == NOTE_INSN_BASIC_BLOCK)
1622 tmp = NEXT_INSN (tmp);
1623 if (tmp == bb->head)
1626 after = PREV_INSN (tmp);
1629 /* If the source has one successor and the edge is not abnormal,
1630 insert there. Except for the entry block. */
1631 else if ((e->flags & EDGE_ABNORMAL) == 0
1632 && e->src->succ->succ_next == NULL
1633 && e->src != ENTRY_BLOCK_PTR)
1636 /* It is possible to have a non-simple jump here. Consider a target
1637 where some forms of unconditional jumps clobber a register. This
1638 happens on the fr30 for example.
1640 We know this block has a single successor, so we can just emit
1641 the queued insns before the jump. */
1642 if (GET_CODE (bb->end) == JUMP_INSN)
1648 /* We'd better be fallthru, or we've lost track of what's what. */
1649 if ((e->flags & EDGE_FALLTHRU) == 0)
1656 /* Otherwise we must split the edge. */
1659 bb = split_edge (e);
1663 /* Now that we've found the spot, do the insertion. */
1665 /* Set the new block number for these insns, if structure is allocated. */
1666 if (basic_block_for_insn)
1669 for (i = insns; i != NULL_RTX; i = NEXT_INSN (i))
1670 set_block_for_insn (i, bb);
1675 emit_insns_before (insns, before);
1676 if (before == bb->head)
1679 last = prev_nonnote_insn (before);
1683 last = emit_insns_after (insns, after);
1684 if (after == bb->end)
1688 if (returnjump_p (last))
1690 /* ??? Remove all outgoing edges from BB and add one for EXIT.
1691 This is not currently a problem because this only happens
1692 for the (single) epilogue, which already has a fallthru edge
1696 if (e->dest != EXIT_BLOCK_PTR
1697 || e->succ_next != NULL
1698 || (e->flags & EDGE_FALLTHRU) == 0)
1700 e->flags &= ~EDGE_FALLTHRU;
1702 emit_barrier_after (last);
1706 flow_delete_insn (before);
1708 else if (GET_CODE (last) == JUMP_INSN)
1712 /* Update the CFG for all queued instructions. */
1715 commit_edge_insertions ()
1720 #ifdef ENABLE_CHECKING
1721 verify_flow_info ();
1725 bb = ENTRY_BLOCK_PTR;
1730 for (e = bb->succ; e ; e = next)
1732 next = e->succ_next;
1734 commit_one_edge_insertion (e);
1737 if (++i >= n_basic_blocks)
1739 bb = BASIC_BLOCK (i);
1743 /* Delete all unreachable basic blocks. */
1746 delete_unreachable_blocks ()
1748 basic_block *worklist, *tos;
1749 int deleted_handler;
1754 tos = worklist = (basic_block *) xmalloc (sizeof (basic_block) * n);
1756 /* Use basic_block->aux as a marker. Clear them all. */
1758 for (i = 0; i < n; ++i)
1759 BASIC_BLOCK (i)->aux = NULL;
1761 /* Add our starting points to the worklist. Almost always there will
1762 be only one. It isn't inconcievable that we might one day directly
1763 support Fortran alternate entry points. */
1765 for (e = ENTRY_BLOCK_PTR->succ; e ; e = e->succ_next)
1769 /* Mark the block with a handy non-null value. */
1773 /* Iterate: find everything reachable from what we've already seen. */
1775 while (tos != worklist)
1777 basic_block b = *--tos;
1779 for (e = b->succ; e ; e = e->succ_next)
1787 /* Delete all unreachable basic blocks. Count down so that we don't
1788 interfere with the block renumbering that happens in flow_delete_block. */
1790 deleted_handler = 0;
1792 for (i = n - 1; i >= 0; --i)
1794 basic_block b = BASIC_BLOCK (i);
1797 /* This block was found. Tidy up the mark. */
1800 deleted_handler |= flow_delete_block (b);
1803 tidy_fallthru_edges ();
1805 /* If we deleted an exception handler, we may have EH region begin/end
1806 blocks to remove as well. */
1807 if (deleted_handler)
1808 delete_eh_regions ();
1813 /* Find EH regions for which there is no longer a handler, and delete them. */
1816 delete_eh_regions ()
1820 update_rethrow_references ();
1822 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1823 if (GET_CODE (insn) == NOTE)
1825 if ((NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG) ||
1826 (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END))
1828 int num = NOTE_EH_HANDLER (insn);
1829 /* A NULL handler indicates a region is no longer needed,
1830 as long as its rethrow label isn't used. */
1831 if (get_first_handler (num) == NULL && ! rethrow_used (num))
1833 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1834 NOTE_SOURCE_FILE (insn) = 0;
1840 /* Return true if NOTE is not one of the ones that must be kept paired,
1841 so that we may simply delete them. */
1844 can_delete_note_p (note)
1847 return (NOTE_LINE_NUMBER (note) == NOTE_INSN_DELETED
1848 || NOTE_LINE_NUMBER (note) == NOTE_INSN_BASIC_BLOCK);
1851 /* Unlink a chain of insns between START and FINISH, leaving notes
1852 that must be paired. */
1855 flow_delete_insn_chain (start, finish)
1858 /* Unchain the insns one by one. It would be quicker to delete all
1859 of these with a single unchaining, rather than one at a time, but
1860 we need to keep the NOTE's. */
1866 next = NEXT_INSN (start);
1867 if (GET_CODE (start) == NOTE && !can_delete_note_p (start))
1869 else if (GET_CODE (start) == CODE_LABEL
1870 && ! can_delete_label_p (start))
1872 const char *name = LABEL_NAME (start);
1873 PUT_CODE (start, NOTE);
1874 NOTE_LINE_NUMBER (start) = NOTE_INSN_DELETED_LABEL;
1875 NOTE_SOURCE_FILE (start) = name;
1878 next = flow_delete_insn (start);
1880 if (start == finish)
1886 /* Delete the insns in a (non-live) block. We physically delete every
1887 non-deleted-note insn, and update the flow graph appropriately.
1889 Return nonzero if we deleted an exception handler. */
1891 /* ??? Preserving all such notes strikes me as wrong. It would be nice
1892 to post-process the stream to remove empty blocks, loops, ranges, etc. */
1895 flow_delete_block (b)
1898 int deleted_handler = 0;
1901 /* If the head of this block is a CODE_LABEL, then it might be the
1902 label for an exception handler which can't be reached.
1904 We need to remove the label from the exception_handler_label list
1905 and remove the associated NOTE_INSN_EH_REGION_BEG and
1906 NOTE_INSN_EH_REGION_END notes. */
1910 never_reached_warning (insn);
1912 if (GET_CODE (insn) == CODE_LABEL)
1914 rtx x, *prev = &exception_handler_labels;
1916 for (x = exception_handler_labels; x; x = XEXP (x, 1))
1918 if (XEXP (x, 0) == insn)
1920 /* Found a match, splice this label out of the EH label list. */
1921 *prev = XEXP (x, 1);
1922 XEXP (x, 1) = NULL_RTX;
1923 XEXP (x, 0) = NULL_RTX;
1925 /* Remove the handler from all regions */
1926 remove_handler (insn);
1927 deleted_handler = 1;
1930 prev = &XEXP (x, 1);
1934 /* Include any jump table following the basic block. */
1936 if (GET_CODE (end) == JUMP_INSN
1937 && (tmp = JUMP_LABEL (end)) != NULL_RTX
1938 && (tmp = NEXT_INSN (tmp)) != NULL_RTX
1939 && GET_CODE (tmp) == JUMP_INSN
1940 && (GET_CODE (PATTERN (tmp)) == ADDR_VEC
1941 || GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC))
1944 /* Include any barrier that may follow the basic block. */
1945 tmp = next_nonnote_insn (end);
1946 if (tmp && GET_CODE (tmp) == BARRIER)
1949 /* Selectively delete the entire chain. */
1950 flow_delete_insn_chain (insn, end);
1952 /* Remove the edges into and out of this block. Note that there may
1953 indeed be edges in, if we are removing an unreachable loop. */
1957 for (e = b->pred; e ; e = next)
1959 for (q = &e->src->succ; *q != e; q = &(*q)->succ_next)
1962 next = e->pred_next;
1966 for (e = b->succ; e ; e = next)
1968 for (q = &e->dest->pred; *q != e; q = &(*q)->pred_next)
1971 next = e->succ_next;
1980 /* Remove the basic block from the array, and compact behind it. */
1983 return deleted_handler;
1986 /* Remove block B from the basic block array and compact behind it. */
1992 int i, n = n_basic_blocks;
1994 for (i = b->index; i + 1 < n; ++i)
1996 basic_block x = BASIC_BLOCK (i + 1);
1997 BASIC_BLOCK (i) = x;
2001 basic_block_info->num_elements--;
2005 /* Delete INSN by patching it out. Return the next insn. */
2008 flow_delete_insn (insn)
2011 rtx prev = PREV_INSN (insn);
2012 rtx next = NEXT_INSN (insn);
2015 PREV_INSN (insn) = NULL_RTX;
2016 NEXT_INSN (insn) = NULL_RTX;
2017 INSN_DELETED_P (insn) = 1;
2020 NEXT_INSN (prev) = next;
2022 PREV_INSN (next) = prev;
2024 set_last_insn (prev);
2026 if (GET_CODE (insn) == CODE_LABEL)
2027 remove_node_from_expr_list (insn, &nonlocal_goto_handler_labels);
2029 /* If deleting a jump, decrement the use count of the label. Deleting
2030 the label itself should happen in the normal course of block merging. */
2031 if (GET_CODE (insn) == JUMP_INSN
2032 && JUMP_LABEL (insn)
2033 && GET_CODE (JUMP_LABEL (insn)) == CODE_LABEL)
2034 LABEL_NUSES (JUMP_LABEL (insn))--;
2036 /* Also if deleting an insn that references a label. */
2037 else if ((note = find_reg_note (insn, REG_LABEL, NULL_RTX)) != NULL_RTX
2038 && GET_CODE (XEXP (note, 0)) == CODE_LABEL)
2039 LABEL_NUSES (XEXP (note, 0))--;
2044 /* True if a given label can be deleted. */
2047 can_delete_label_p (label)
2052 if (LABEL_PRESERVE_P (label))
2055 for (x = forced_labels; x ; x = XEXP (x, 1))
2056 if (label == XEXP (x, 0))
2058 for (x = label_value_list; x ; x = XEXP (x, 1))
2059 if (label == XEXP (x, 0))
2061 for (x = exception_handler_labels; x ; x = XEXP (x, 1))
2062 if (label == XEXP (x, 0))
2065 /* User declared labels must be preserved. */
2066 if (LABEL_NAME (label) != 0)
2073 tail_recursion_label_p (label)
2078 for (x = tail_recursion_label_list; x ; x = XEXP (x, 1))
2079 if (label == XEXP (x, 0))
2085 /* Blocks A and B are to be merged into a single block A. The insns
2086 are already contiguous, hence `nomove'. */
2089 merge_blocks_nomove (a, b)
2093 rtx b_head, b_end, a_end;
2094 rtx del_first = NULL_RTX, del_last = NULL_RTX;
2097 /* If there was a CODE_LABEL beginning B, delete it. */
2100 if (GET_CODE (b_head) == CODE_LABEL)
2102 /* Detect basic blocks with nothing but a label. This can happen
2103 in particular at the end of a function. */
2104 if (b_head == b_end)
2106 del_first = del_last = b_head;
2107 b_head = NEXT_INSN (b_head);
2110 /* Delete the basic block note. */
2111 if (GET_CODE (b_head) == NOTE
2112 && NOTE_LINE_NUMBER (b_head) == NOTE_INSN_BASIC_BLOCK)
2114 if (b_head == b_end)
2119 b_head = NEXT_INSN (b_head);
2122 /* If there was a jump out of A, delete it. */
2124 if (GET_CODE (a_end) == JUMP_INSN)
2128 prev = prev_nonnote_insn (a_end);
2135 /* If this was a conditional jump, we need to also delete
2136 the insn that set cc0. */
2137 if (prev && sets_cc0_p (prev))
2140 prev = prev_nonnote_insn (prev);
2150 /* Delete everything marked above as well as crap that might be
2151 hanging out between the two blocks. */
2152 flow_delete_insn_chain (del_first, del_last);
2154 /* Normally there should only be one successor of A and that is B, but
2155 partway though the merge of blocks for conditional_execution we'll
2156 be merging a TEST block with THEN and ELSE successors. Free the
2157 whole lot of them and hope the caller knows what they're doing. */
2159 remove_edge (a->succ);
2161 /* Adjust the edges out of B for the new owner. */
2162 for (e = b->succ; e ; e = e->succ_next)
2166 /* B hasn't quite yet ceased to exist. Attempt to prevent mishap. */
2167 b->pred = b->succ = NULL;
2169 /* Reassociate the insns of B with A. */
2172 if (basic_block_for_insn)
2174 BLOCK_FOR_INSN (b_head) = a;
2175 while (b_head != b_end)
2177 b_head = NEXT_INSN (b_head);
2178 BLOCK_FOR_INSN (b_head) = a;
2188 /* Blocks A and B are to be merged into a single block. A has no incoming
2189 fallthru edge, so it can be moved before B without adding or modifying
2190 any jumps (aside from the jump from A to B). */
2193 merge_blocks_move_predecessor_nojumps (a, b)
2196 rtx start, end, barrier;
2202 barrier = next_nonnote_insn (end);
2203 if (GET_CODE (barrier) != BARRIER)
2205 flow_delete_insn (barrier);
2207 /* Move block and loop notes out of the chain so that we do not
2208 disturb their order.
2210 ??? A better solution would be to squeeze out all the non-nested notes
2211 and adjust the block trees appropriately. Even better would be to have
2212 a tighter connection between block trees and rtl so that this is not
2214 start = squeeze_notes (start, end);
2216 /* Scramble the insn chain. */
2217 if (end != PREV_INSN (b->head))
2218 reorder_insns (start, end, PREV_INSN (b->head));
2222 fprintf (rtl_dump_file, "Moved block %d before %d and merged.\n",
2223 a->index, b->index);
2226 /* Swap the records for the two blocks around. Although we are deleting B,
2227 A is now where B was and we want to compact the BB array from where
2229 BASIC_BLOCK(a->index) = b;
2230 BASIC_BLOCK(b->index) = a;
2232 a->index = b->index;
2235 /* Now blocks A and B are contiguous. Merge them. */
2236 merge_blocks_nomove (a, b);
2241 /* Blocks A and B are to be merged into a single block. B has no outgoing
2242 fallthru edge, so it can be moved after A without adding or modifying
2243 any jumps (aside from the jump from A to B). */
2246 merge_blocks_move_successor_nojumps (a, b)
2249 rtx start, end, barrier;
2253 barrier = NEXT_INSN (end);
2255 /* Recognize a jump table following block B. */
2256 if (GET_CODE (barrier) == CODE_LABEL
2257 && NEXT_INSN (barrier)
2258 && GET_CODE (NEXT_INSN (barrier)) == JUMP_INSN
2259 && (GET_CODE (PATTERN (NEXT_INSN (barrier))) == ADDR_VEC
2260 || GET_CODE (PATTERN (NEXT_INSN (barrier))) == ADDR_DIFF_VEC))
2262 end = NEXT_INSN (barrier);
2263 barrier = NEXT_INSN (end);
2266 /* There had better have been a barrier there. Delete it. */
2267 if (GET_CODE (barrier) != BARRIER)
2269 flow_delete_insn (barrier);
2271 /* Move block and loop notes out of the chain so that we do not
2272 disturb their order.
2274 ??? A better solution would be to squeeze out all the non-nested notes
2275 and adjust the block trees appropriately. Even better would be to have
2276 a tighter connection between block trees and rtl so that this is not
2278 start = squeeze_notes (start, end);
2280 /* Scramble the insn chain. */
2281 reorder_insns (start, end, a->end);
2283 /* Now blocks A and B are contiguous. Merge them. */
2284 merge_blocks_nomove (a, b);
2288 fprintf (rtl_dump_file, "Moved block %d after %d and merged.\n",
2289 b->index, a->index);
2295 /* Attempt to merge basic blocks that are potentially non-adjacent.
2296 Return true iff the attempt succeeded. */
2299 merge_blocks (e, b, c)
2303 /* If C has a tail recursion label, do not merge. There is no
2304 edge recorded from the call_placeholder back to this label, as
2305 that would make optimize_sibling_and_tail_recursive_calls more
2306 complex for no gain. */
2307 if (GET_CODE (c->head) == CODE_LABEL
2308 && tail_recursion_label_p (c->head))
2311 /* If B has a fallthru edge to C, no need to move anything. */
2312 if (e->flags & EDGE_FALLTHRU)
2314 merge_blocks_nomove (b, c);
2318 fprintf (rtl_dump_file, "Merged %d and %d without moving.\n",
2319 b->index, c->index);
2328 int c_has_outgoing_fallthru;
2329 int b_has_incoming_fallthru;
2331 /* We must make sure to not munge nesting of exception regions,
2332 lexical blocks, and loop notes.
2334 The first is taken care of by requiring that the active eh
2335 region at the end of one block always matches the active eh
2336 region at the beginning of the next block.
2338 The later two are taken care of by squeezing out all the notes. */
2340 /* ??? A throw/catch edge (or any abnormal edge) should be rarely
2341 executed and we may want to treat blocks which have two out
2342 edges, one normal, one abnormal as only having one edge for
2343 block merging purposes. */
2345 for (tmp_edge = c->succ; tmp_edge ; tmp_edge = tmp_edge->succ_next)
2346 if (tmp_edge->flags & EDGE_FALLTHRU)
2348 c_has_outgoing_fallthru = (tmp_edge != NULL);
2350 for (tmp_edge = b->pred; tmp_edge ; tmp_edge = tmp_edge->pred_next)
2351 if (tmp_edge->flags & EDGE_FALLTHRU)
2353 b_has_incoming_fallthru = (tmp_edge != NULL);
2355 /* If B does not have an incoming fallthru, and the exception regions
2356 match, then it can be moved immediately before C without introducing
2359 C can not be the first block, so we do not have to worry about
2360 accessing a non-existent block. */
2361 d = BASIC_BLOCK (c->index - 1);
2362 if (! b_has_incoming_fallthru
2363 && d->eh_end == b->eh_beg
2364 && b->eh_end == c->eh_beg)
2365 return merge_blocks_move_predecessor_nojumps (b, c);
2367 /* Otherwise, we're going to try to move C after B. Make sure the
2368 exception regions match.
2370 If B is the last basic block, then we must not try to access the
2371 block structure for block B + 1. Luckily in that case we do not
2372 need to worry about matching exception regions. */
2373 d = (b->index + 1 < n_basic_blocks ? BASIC_BLOCK (b->index + 1) : NULL);
2374 if (b->eh_end == c->eh_beg
2375 && (d == NULL || c->eh_end == d->eh_beg))
2377 /* If C does not have an outgoing fallthru, then it can be moved
2378 immediately after B without introducing or modifying jumps. */
2379 if (! c_has_outgoing_fallthru)
2380 return merge_blocks_move_successor_nojumps (b, c);
2382 /* Otherwise, we'll need to insert an extra jump, and possibly
2383 a new block to contain it. */
2384 /* ??? Not implemented yet. */
2391 /* Top level driver for merge_blocks. */
2398 /* Attempt to merge blocks as made possible by edge removal. If a block
2399 has only one successor, and the successor has only one predecessor,
2400 they may be combined. */
2402 for (i = 0; i < n_basic_blocks; )
2404 basic_block c, b = BASIC_BLOCK (i);
2407 /* A loop because chains of blocks might be combineable. */
2408 while ((s = b->succ) != NULL
2409 && s->succ_next == NULL
2410 && (s->flags & EDGE_EH) == 0
2411 && (c = s->dest) != EXIT_BLOCK_PTR
2412 && c->pred->pred_next == NULL
2413 /* If the jump insn has side effects, we can't kill the edge. */
2414 && (GET_CODE (b->end) != JUMP_INSN
2415 || onlyjump_p (b->end))
2416 && merge_blocks (s, b, c))
2419 /* Don't get confused by the index shift caused by deleting blocks. */
2424 /* The given edge should potentially be a fallthru edge. If that is in
2425 fact true, delete the jump and barriers that are in the way. */
2428 tidy_fallthru_edge (e, b, c)
2434 /* ??? In a late-running flow pass, other folks may have deleted basic
2435 blocks by nopping out blocks, leaving multiple BARRIERs between here
2436 and the target label. They ought to be chastized and fixed.
2438 We can also wind up with a sequence of undeletable labels between
2439 one block and the next.
2441 So search through a sequence of barriers, labels, and notes for
2442 the head of block C and assert that we really do fall through. */
2444 if (next_real_insn (b->end) != next_real_insn (PREV_INSN (c->head)))
2447 /* Remove what will soon cease being the jump insn from the source block.
2448 If block B consisted only of this single jump, turn it into a deleted
2451 if (GET_CODE (q) == JUMP_INSN
2452 && (simplejump_p (q)
2453 || (b->succ == e && e->succ_next == NULL)))
2456 /* If this was a conditional jump, we need to also delete
2457 the insn that set cc0. */
2458 if (! simplejump_p (q) && condjump_p (q) && sets_cc0_p (PREV_INSN (q)))
2465 NOTE_LINE_NUMBER (q) = NOTE_INSN_DELETED;
2466 NOTE_SOURCE_FILE (q) = 0;
2469 b->end = q = PREV_INSN (q);
2472 /* Selectively unlink the sequence. */
2473 if (q != PREV_INSN (c->head))
2474 flow_delete_insn_chain (NEXT_INSN (q), PREV_INSN (c->head));
2476 e->flags |= EDGE_FALLTHRU;
2479 /* Fix up edges that now fall through, or rather should now fall through
2480 but previously required a jump around now deleted blocks. Simplify
2481 the search by only examining blocks numerically adjacent, since this
2482 is how find_basic_blocks created them. */
2485 tidy_fallthru_edges ()
2489 for (i = 1; i < n_basic_blocks; ++i)
2491 basic_block b = BASIC_BLOCK (i - 1);
2492 basic_block c = BASIC_BLOCK (i);
2495 /* We care about simple conditional or unconditional jumps with
2498 If we had a conditional branch to the next instruction when
2499 find_basic_blocks was called, then there will only be one
2500 out edge for the block which ended with the conditional
2501 branch (since we do not create duplicate edges).
2503 Furthermore, the edge will be marked as a fallthru because we
2504 merge the flags for the duplicate edges. So we do not want to
2505 check that the edge is not a FALLTHRU edge. */
2506 if ((s = b->succ) != NULL
2507 && s->succ_next == NULL
2509 /* If the jump insn has side effects, we can't tidy the edge. */
2510 && (GET_CODE (b->end) != JUMP_INSN
2511 || onlyjump_p (b->end)))
2512 tidy_fallthru_edge (s, b, c);
2516 /* Perform data flow analysis.
2517 F is the first insn of the function; FLAGS is a set of PROP_* flags
2518 to be used in accumulating flow info. */
2521 life_analysis (f, file, flags)
2526 #ifdef ELIMINABLE_REGS
2528 static struct {int from, to; } eliminables[] = ELIMINABLE_REGS;
2531 /* Record which registers will be eliminated. We use this in
2534 CLEAR_HARD_REG_SET (elim_reg_set);
2536 #ifdef ELIMINABLE_REGS
2537 for (i = 0; i < (int) (sizeof eliminables / sizeof eliminables[0]); i++)
2538 SET_HARD_REG_BIT (elim_reg_set, eliminables[i].from);
2540 SET_HARD_REG_BIT (elim_reg_set, FRAME_POINTER_REGNUM);
2544 flags &= PROP_DEATH_NOTES | PROP_REG_INFO;
2546 /* The post-reload life analysis have (on a global basis) the same
2547 registers live as was computed by reload itself. elimination
2548 Otherwise offsets and such may be incorrect.
2550 Reload will make some registers as live even though they do not
2551 appear in the rtl. */
2552 if (reload_completed)
2553 flags &= ~PROP_REG_INFO;
2555 /* We want alias analysis information for local dead store elimination. */
2556 if (flags & PROP_SCAN_DEAD_CODE)
2557 init_alias_analysis ();
2559 /* Always remove no-op moves. Do this before other processing so
2560 that we don't have to keep re-scanning them. */
2561 delete_noop_moves (f);
2563 /* Some targets can emit simpler epilogues if they know that sp was
2564 not ever modified during the function. After reload, of course,
2565 we've already emitted the epilogue so there's no sense searching. */
2566 if (! reload_completed)
2567 notice_stack_pointer_modification (f);
2569 /* Allocate and zero out data structures that will record the
2570 data from lifetime analysis. */
2571 allocate_reg_life_data ();
2572 allocate_bb_life_data ();
2574 /* Find the set of registers live on function exit. */
2575 mark_regs_live_at_end (EXIT_BLOCK_PTR->global_live_at_start);
2577 /* "Update" life info from zero. It'd be nice to begin the
2578 relaxation with just the exit and noreturn blocks, but that set
2579 is not immediately handy. */
2581 if (flags & PROP_REG_INFO)
2582 memset (regs_ever_live, 0, sizeof(regs_ever_live));
2583 update_life_info (NULL, UPDATE_LIFE_GLOBAL, flags);
2586 if (flags & PROP_SCAN_DEAD_CODE)
2587 end_alias_analysis ();
2590 dump_flow_info (file);
2592 free_basic_block_vars (1);
2595 /* A subroutine of verify_wide_reg, called through for_each_rtx.
2596 Search for REGNO. If found, abort if it is not wider than word_mode. */
2599 verify_wide_reg_1 (px, pregno)
2604 unsigned int regno = *(int *) pregno;
2606 if (GET_CODE (x) == REG && REGNO (x) == regno)
2608 if (GET_MODE_BITSIZE (GET_MODE (x)) <= BITS_PER_WORD)
2615 /* A subroutine of verify_local_live_at_start. Search through insns
2616 between HEAD and END looking for register REGNO. */
2619 verify_wide_reg (regno, head, end)
2625 if (GET_RTX_CLASS (GET_CODE (head)) == 'i'
2626 && for_each_rtx (&PATTERN (head), verify_wide_reg_1, ®no))
2630 head = NEXT_INSN (head);
2633 /* We didn't find the register at all. Something's way screwy. */
2637 /* A subroutine of update_life_info. Verify that there are no untoward
2638 changes in live_at_start during a local update. */
2641 verify_local_live_at_start (new_live_at_start, bb)
2642 regset new_live_at_start;
2645 if (reload_completed)
2647 /* After reload, there are no pseudos, nor subregs of multi-word
2648 registers. The regsets should exactly match. */
2649 if (! REG_SET_EQUAL_P (new_live_at_start, bb->global_live_at_start))
2656 /* Find the set of changed registers. */
2657 XOR_REG_SET (new_live_at_start, bb->global_live_at_start);
2659 EXECUTE_IF_SET_IN_REG_SET (new_live_at_start, 0, i,
2661 /* No registers should die. */
2662 if (REGNO_REG_SET_P (bb->global_live_at_start, i))
2664 /* Verify that the now-live register is wider than word_mode. */
2665 verify_wide_reg (i, bb->head, bb->end);
2670 /* Updates life information starting with the basic blocks set in BLOCKS.
2671 If BLOCKS is null, consider it to be the universal set.
2673 If EXTENT is UPDATE_LIFE_LOCAL, such as after splitting or peepholeing,
2674 we are only expecting local modifications to basic blocks. If we find
2675 extra registers live at the beginning of a block, then we either killed
2676 useful data, or we have a broken split that wants data not provided.
2677 If we find registers removed from live_at_start, that means we have
2678 a broken peephole that is killing a register it shouldn't.
2680 ??? This is not true in one situation -- when a pre-reload splitter
2681 generates subregs of a multi-word pseudo, current life analysis will
2682 lose the kill. So we _can_ have a pseudo go live. How irritating.
2684 Including PROP_REG_INFO does not properly refresh regs_ever_live
2685 unless the caller resets it to zero. */
2688 update_life_info (blocks, extent, prop_flags)
2690 enum update_life_extent extent;
2694 regset_head tmp_head;
2697 tmp = INITIALIZE_REG_SET (tmp_head);
2699 /* For a global update, we go through the relaxation process again. */
2700 if (extent != UPDATE_LIFE_LOCAL)
2702 calculate_global_regs_live (blocks, blocks,
2703 prop_flags & PROP_SCAN_DEAD_CODE);
2705 /* If asked, remove notes from the blocks we'll update. */
2706 if (extent == UPDATE_LIFE_GLOBAL_RM_NOTES)
2707 count_or_remove_death_notes (blocks, 1);
2712 EXECUTE_IF_SET_IN_SBITMAP (blocks, 0, i,
2714 basic_block bb = BASIC_BLOCK (i);
2716 COPY_REG_SET (tmp, bb->global_live_at_end);
2717 propagate_block (bb, tmp, (regset) NULL, prop_flags);
2719 if (extent == UPDATE_LIFE_LOCAL)
2720 verify_local_live_at_start (tmp, bb);
2725 for (i = n_basic_blocks - 1; i >= 0; --i)
2727 basic_block bb = BASIC_BLOCK (i);
2729 COPY_REG_SET (tmp, bb->global_live_at_end);
2730 propagate_block (bb, tmp, (regset) NULL, prop_flags);
2732 if (extent == UPDATE_LIFE_LOCAL)
2733 verify_local_live_at_start (tmp, bb);
2739 if (prop_flags & PROP_REG_INFO)
2741 /* The only pseudos that are live at the beginning of the function
2742 are those that were not set anywhere in the function. local-alloc
2743 doesn't know how to handle these correctly, so mark them as not
2744 local to any one basic block. */
2745 EXECUTE_IF_SET_IN_REG_SET (ENTRY_BLOCK_PTR->global_live_at_end,
2746 FIRST_PSEUDO_REGISTER, i,
2747 { REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL; });
2749 /* We have a problem with any pseudoreg that lives across the setjmp.
2750 ANSI says that if a user variable does not change in value between
2751 the setjmp and the longjmp, then the longjmp preserves it. This
2752 includes longjmp from a place where the pseudo appears dead.
2753 (In principle, the value still exists if it is in scope.)
2754 If the pseudo goes in a hard reg, some other value may occupy
2755 that hard reg where this pseudo is dead, thus clobbering the pseudo.
2756 Conclusion: such a pseudo must not go in a hard reg. */
2757 EXECUTE_IF_SET_IN_REG_SET (regs_live_at_setjmp,
2758 FIRST_PSEUDO_REGISTER, i,
2760 if (regno_reg_rtx[i] != 0)
2762 REG_LIVE_LENGTH (i) = -1;
2763 REG_BASIC_BLOCK (i) = REG_BLOCK_UNKNOWN;
2769 /* Free the variables allocated by find_basic_blocks.
2771 KEEP_HEAD_END_P is non-zero if basic_block_info is not to be freed. */
2774 free_basic_block_vars (keep_head_end_p)
2775 int keep_head_end_p;
2777 if (basic_block_for_insn)
2779 VARRAY_FREE (basic_block_for_insn);
2780 basic_block_for_insn = NULL;
2783 if (! keep_head_end_p)
2786 VARRAY_FREE (basic_block_info);
2789 ENTRY_BLOCK_PTR->aux = NULL;
2790 ENTRY_BLOCK_PTR->global_live_at_end = NULL;
2791 EXIT_BLOCK_PTR->aux = NULL;
2792 EXIT_BLOCK_PTR->global_live_at_start = NULL;
2796 /* Return nonzero if the destination of SET equals the source. */
2801 rtx src = SET_SRC (set);
2802 rtx dst = SET_DEST (set);
2804 if (GET_CODE (src) == SUBREG && GET_CODE (dst) == SUBREG)
2806 if (SUBREG_WORD (src) != SUBREG_WORD (dst))
2808 src = SUBREG_REG (src);
2809 dst = SUBREG_REG (dst);
2812 return (GET_CODE (src) == REG && GET_CODE (dst) == REG
2813 && REGNO (src) == REGNO (dst));
2816 /* Return nonzero if an insn consists only of SETs, each of which only sets a
2822 rtx pat = PATTERN (insn);
2824 /* Insns carrying these notes are useful later on. */
2825 if (find_reg_note (insn, REG_EQUAL, NULL_RTX))
2828 if (GET_CODE (pat) == SET && set_noop_p (pat))
2831 if (GET_CODE (pat) == PARALLEL)
2834 /* If nothing but SETs of registers to themselves,
2835 this insn can also be deleted. */
2836 for (i = 0; i < XVECLEN (pat, 0); i++)
2838 rtx tem = XVECEXP (pat, 0, i);
2840 if (GET_CODE (tem) == USE
2841 || GET_CODE (tem) == CLOBBER)
2844 if (GET_CODE (tem) != SET || ! set_noop_p (tem))
2853 /* Delete any insns that copy a register to itself. */
2856 delete_noop_moves (f)
2860 for (insn = f; insn; insn = NEXT_INSN (insn))
2862 if (GET_CODE (insn) == INSN && noop_move_p (insn))
2864 PUT_CODE (insn, NOTE);
2865 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2866 NOTE_SOURCE_FILE (insn) = 0;
2871 /* Determine if the stack pointer is constant over the life of the function.
2872 Only useful before prologues have been emitted. */
2875 notice_stack_pointer_modification_1 (x, pat, data)
2877 rtx pat ATTRIBUTE_UNUSED;
2878 void *data ATTRIBUTE_UNUSED;
2880 if (x == stack_pointer_rtx
2881 /* The stack pointer is only modified indirectly as the result
2882 of a push until later in flow. See the comments in rtl.texi
2883 regarding Embedded Side-Effects on Addresses. */
2884 || (GET_CODE (x) == MEM
2885 && (GET_CODE (XEXP (x, 0)) == PRE_DEC
2886 || GET_CODE (XEXP (x, 0)) == PRE_INC
2887 || GET_CODE (XEXP (x, 0)) == POST_DEC
2888 || GET_CODE (XEXP (x, 0)) == POST_INC)
2889 && XEXP (XEXP (x, 0), 0) == stack_pointer_rtx))
2890 current_function_sp_is_unchanging = 0;
2894 notice_stack_pointer_modification (f)
2899 /* Assume that the stack pointer is unchanging if alloca hasn't
2901 current_function_sp_is_unchanging = !current_function_calls_alloca;
2902 if (! current_function_sp_is_unchanging)
2905 for (insn = f; insn; insn = NEXT_INSN (insn))
2907 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2909 /* Check if insn modifies the stack pointer. */
2910 note_stores (PATTERN (insn), notice_stack_pointer_modification_1,
2912 if (! current_function_sp_is_unchanging)
2918 /* Mark a register in SET. Hard registers in large modes get all
2919 of their component registers set as well. */
2921 mark_reg (reg, xset)
2925 regset set = (regset) xset;
2926 int regno = REGNO (reg);
2928 if (GET_MODE (reg) == BLKmode)
2931 SET_REGNO_REG_SET (set, regno);
2932 if (regno < FIRST_PSEUDO_REGISTER)
2934 int n = HARD_REGNO_NREGS (regno, GET_MODE (reg));
2936 SET_REGNO_REG_SET (set, regno + n);
2940 /* Mark those regs which are needed at the end of the function as live
2941 at the end of the last basic block. */
2943 mark_regs_live_at_end (set)
2948 /* If exiting needs the right stack value, consider the stack pointer
2949 live at the end of the function. */
2950 if ((HAVE_epilogue && reload_completed)
2951 || ! EXIT_IGNORE_STACK
2952 || (! FRAME_POINTER_REQUIRED
2953 && ! current_function_calls_alloca
2954 && flag_omit_frame_pointer)
2955 || current_function_sp_is_unchanging)
2957 SET_REGNO_REG_SET (set, STACK_POINTER_REGNUM);
2960 /* Mark the frame pointer if needed at the end of the function. If
2961 we end up eliminating it, it will be removed from the live list
2962 of each basic block by reload. */
2964 if (! reload_completed || frame_pointer_needed)
2966 SET_REGNO_REG_SET (set, FRAME_POINTER_REGNUM);
2967 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2968 /* If they are different, also mark the hard frame pointer as live */
2969 SET_REGNO_REG_SET (set, HARD_FRAME_POINTER_REGNUM);
2973 #ifdef PIC_OFFSET_TABLE_REGNUM
2974 #ifndef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
2975 /* Many architectures have a GP register even without flag_pic.
2976 Assume the pic register is not in use, or will be handled by
2977 other means, if it is not fixed. */
2978 if (fixed_regs[PIC_OFFSET_TABLE_REGNUM])
2979 SET_REGNO_REG_SET (set, PIC_OFFSET_TABLE_REGNUM);
2983 /* Mark all global registers, and all registers used by the epilogue
2984 as being live at the end of the function since they may be
2985 referenced by our caller. */
2986 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2988 #ifdef EPILOGUE_USES
2989 || EPILOGUE_USES (i)
2992 SET_REGNO_REG_SET (set, i);
2994 /* Mark all call-saved registers that we actaully used. */
2995 if (HAVE_epilogue && reload_completed)
2997 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2998 if (! call_used_regs[i] && regs_ever_live[i])
2999 SET_REGNO_REG_SET (set, i);
3002 /* Mark function return value. */
3003 diddle_return_value (mark_reg, set);
3006 /* Callback function for for_each_successor_phi. DATA is a regset.
3007 Sets the SRC_REGNO, the regno of the phi alternative for phi node
3008 INSN, in the regset. */
3011 set_phi_alternative_reg (insn, dest_regno, src_regno, data)
3012 rtx insn ATTRIBUTE_UNUSED;
3013 int dest_regno ATTRIBUTE_UNUSED;
3017 regset live = (regset) data;
3018 SET_REGNO_REG_SET (live, src_regno);
3022 /* Propagate global life info around the graph of basic blocks. Begin
3023 considering blocks with their corresponding bit set in BLOCKS_IN.
3024 If BLOCKS_IN is null, consider it the universal set.
3026 BLOCKS_OUT is set for every block that was changed. */
3029 calculate_global_regs_live (blocks_in, blocks_out, flags)
3030 sbitmap blocks_in, blocks_out;
3033 basic_block *queue, *qhead, *qtail, *qend;
3034 regset tmp, new_live_at_end;
3035 regset_head tmp_head;
3036 regset_head new_live_at_end_head;
3039 tmp = INITIALIZE_REG_SET (tmp_head);
3040 new_live_at_end = INITIALIZE_REG_SET (new_live_at_end_head);
3042 /* Create a worklist. Allocate an extra slot for ENTRY_BLOCK, and one
3043 because the `head == tail' style test for an empty queue doesn't
3044 work with a full queue. */
3045 queue = (basic_block *) xmalloc ((n_basic_blocks + 2) * sizeof (*queue));
3047 qhead = qend = queue + n_basic_blocks + 2;
3049 /* Clear out the garbage that might be hanging out in bb->aux. */
3050 for (i = n_basic_blocks - 1; i >= 0; --i)
3051 BASIC_BLOCK (i)->aux = NULL;
3053 /* Queue the blocks set in the initial mask. Do this in reverse block
3054 number order so that we are more likely for the first round to do
3055 useful work. We use AUX non-null to flag that the block is queued. */
3058 EXECUTE_IF_SET_IN_SBITMAP (blocks_in, 0, i,
3060 basic_block bb = BASIC_BLOCK (i);
3067 for (i = 0; i < n_basic_blocks; ++i)
3069 basic_block bb = BASIC_BLOCK (i);
3076 sbitmap_zero (blocks_out);
3078 while (qhead != qtail)
3080 int rescan, changed;
3089 /* Begin by propogating live_at_start from the successor blocks. */
3090 CLEAR_REG_SET (new_live_at_end);
3091 for (e = bb->succ; e ; e = e->succ_next)
3093 basic_block sb = e->dest;
3094 IOR_REG_SET (new_live_at_end, sb->global_live_at_start);
3097 /* Force the stack pointer to be live -- which might not already be
3098 the case for blocks within infinite loops. */
3099 SET_REGNO_REG_SET (new_live_at_end, STACK_POINTER_REGNUM);
3101 /* Regs used in phi nodes are not included in
3102 global_live_at_start, since they are live only along a
3103 particular edge. Set those regs that are live because of a
3104 phi node alternative corresponding to this particular block. */
3106 for_each_successor_phi (bb, &set_phi_alternative_reg,
3109 if (bb == ENTRY_BLOCK_PTR)
3111 COPY_REG_SET (bb->global_live_at_end, new_live_at_end);
3115 /* On our first pass through this block, we'll go ahead and continue.
3116 Recognize first pass by local_set NULL. On subsequent passes, we
3117 get to skip out early if live_at_end wouldn't have changed. */
3119 if (bb->local_set == NULL)
3121 bb->local_set = OBSTACK_ALLOC_REG_SET (function_obstack);
3126 /* If any bits were removed from live_at_end, we'll have to
3127 rescan the block. This wouldn't be necessary if we had
3128 precalculated local_live, however with PROP_SCAN_DEAD_CODE
3129 local_live is really dependant on live_at_end. */
3130 CLEAR_REG_SET (tmp);
3131 rescan = bitmap_operation (tmp, bb->global_live_at_end,
3132 new_live_at_end, BITMAP_AND_COMPL);
3136 /* Find the set of changed bits. Take this opportunity
3137 to notice that this set is empty and early out. */
3138 CLEAR_REG_SET (tmp);
3139 changed = bitmap_operation (tmp, bb->global_live_at_end,
3140 new_live_at_end, BITMAP_XOR);
3144 /* If any of the changed bits overlap with local_set,
3145 we'll have to rescan the block. Detect overlap by
3146 the AND with ~local_set turning off bits. */
3147 rescan = bitmap_operation (tmp, tmp, bb->local_set,
3152 /* Let our caller know that BB changed enough to require its
3153 death notes updated. */
3155 SET_BIT (blocks_out, bb->index);
3159 /* Add to live_at_start the set of all registers in
3160 new_live_at_end that aren't in the old live_at_end. */
3162 bitmap_operation (tmp, new_live_at_end, bb->global_live_at_end,
3164 COPY_REG_SET (bb->global_live_at_end, new_live_at_end);
3166 changed = bitmap_operation (bb->global_live_at_start,
3167 bb->global_live_at_start,
3174 COPY_REG_SET (bb->global_live_at_end, new_live_at_end);
3176 /* Rescan the block insn by insn to turn (a copy of) live_at_end
3177 into live_at_start. */
3178 propagate_block (bb, new_live_at_end, bb->local_set, flags);
3180 /* If live_at start didn't change, no need to go farther. */
3181 if (REG_SET_EQUAL_P (bb->global_live_at_start, new_live_at_end))
3184 COPY_REG_SET (bb->global_live_at_start, new_live_at_end);
3187 /* Queue all predecessors of BB so that we may re-examine
3188 their live_at_end. */
3189 for (e = bb->pred; e ; e = e->pred_next)
3191 basic_block pb = e->src;
3192 if (pb->aux == NULL)
3203 FREE_REG_SET (new_live_at_end);
3207 EXECUTE_IF_SET_IN_SBITMAP (blocks_out, 0, i,
3209 basic_block bb = BASIC_BLOCK (i);
3210 FREE_REG_SET (bb->local_set);
3215 for (i = n_basic_blocks - 1; i >= 0; --i)
3217 basic_block bb = BASIC_BLOCK (i);
3218 FREE_REG_SET (bb->local_set);
3225 /* Subroutines of life analysis. */
3227 /* Allocate the permanent data structures that represent the results
3228 of life analysis. Not static since used also for stupid life analysis. */
3231 allocate_bb_life_data ()
3235 for (i = 0; i < n_basic_blocks; i++)
3237 basic_block bb = BASIC_BLOCK (i);
3239 bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (function_obstack);
3240 bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (function_obstack);
3243 ENTRY_BLOCK_PTR->global_live_at_end
3244 = OBSTACK_ALLOC_REG_SET (function_obstack);
3245 EXIT_BLOCK_PTR->global_live_at_start
3246 = OBSTACK_ALLOC_REG_SET (function_obstack);
3248 regs_live_at_setjmp = OBSTACK_ALLOC_REG_SET (function_obstack);
3252 allocate_reg_life_data ()
3256 max_regno = max_reg_num ();
3258 /* Recalculate the register space, in case it has grown. Old style
3259 vector oriented regsets would set regset_{size,bytes} here also. */
3260 allocate_reg_info (max_regno, FALSE, FALSE);
3262 /* Reset all the data we'll collect in propagate_block and its
3264 for (i = 0; i < max_regno; i++)
3268 REG_N_DEATHS (i) = 0;
3269 REG_N_CALLS_CROSSED (i) = 0;
3270 REG_LIVE_LENGTH (i) = 0;
3271 REG_BASIC_BLOCK (i) = REG_BLOCK_UNKNOWN;
3275 /* Delete dead instructions for propagate_block. */
3278 propagate_block_delete_insn (bb, insn)
3282 rtx inote = find_reg_note (insn, REG_LABEL, NULL_RTX);
3284 /* If the insn referred to a label, and that label was attached to
3285 an ADDR_VEC, it's safe to delete the ADDR_VEC. In fact, it's
3286 pretty much mandatory to delete it, because the ADDR_VEC may be
3287 referencing labels that no longer exist. */
3291 rtx label = XEXP (inote, 0);
3294 if (LABEL_NUSES (label) == 1
3295 && (next = next_nonnote_insn (label)) != NULL
3296 && GET_CODE (next) == JUMP_INSN
3297 && (GET_CODE (PATTERN (next)) == ADDR_VEC
3298 || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC))
3300 rtx pat = PATTERN (next);
3301 int diff_vec_p = GET_CODE (pat) == ADDR_DIFF_VEC;
3302 int len = XVECLEN (pat, diff_vec_p);
3305 for (i = 0; i < len; i++)
3306 LABEL_NUSES (XEXP (XVECEXP (pat, diff_vec_p, i), 0))--;
3308 flow_delete_insn (next);
3312 if (bb->end == insn)
3313 bb->end = PREV_INSN (insn);
3314 flow_delete_insn (insn);
3317 /* Delete dead libcalls for propagate_block. Return the insn
3318 before the libcall. */
3321 propagate_block_delete_libcall (bb, insn, note)
3325 rtx first = XEXP (note, 0);
3326 rtx before = PREV_INSN (first);
3328 if (insn == bb->end)
3331 flow_delete_insn_chain (first, insn);
3335 /* Update the life-status of regs for one insn. Return the previous insn. */
3338 propagate_one_insn (pbi, insn)
3339 struct propagate_block_info *pbi;
3342 rtx prev = PREV_INSN (insn);
3343 int flags = pbi->flags;
3344 int insn_is_dead = 0;
3345 int libcall_is_dead = 0;
3349 if (! INSN_P (insn))
3352 note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
3353 if (flags & PROP_SCAN_DEAD_CODE)
3355 insn_is_dead = insn_dead_p (pbi, PATTERN (insn), 0,
3357 libcall_is_dead = (insn_is_dead && note != 0
3358 && libcall_dead_p (pbi, PATTERN (insn),
3362 /* We almost certainly don't want to delete prologue or epilogue
3363 instructions. Warn about probable compiler losage. */
3366 && (((HAVE_epilogue || HAVE_prologue)
3367 && prologue_epilogue_contains (insn))
3368 || (HAVE_sibcall_epilogue
3369 && sibcall_epilogue_contains (insn))))
3371 if (flags & PROP_KILL_DEAD_CODE)
3373 warning ("ICE: would have deleted prologue/epilogue insn");
3374 if (!inhibit_warnings)
3377 libcall_is_dead = insn_is_dead = 0;
3380 /* If an instruction consists of just dead store(s) on final pass,
3382 if ((flags & PROP_KILL_DEAD_CODE) && insn_is_dead)
3384 /* Record sets. Do this even for dead instructions, since they
3385 would have killed the values if they hadn't been deleted. */
3386 mark_set_regs (pbi, PATTERN (insn), insn);
3388 /* CC0 is now known to be dead. Either this insn used it,
3389 in which case it doesn't anymore, or clobbered it,
3390 so the next insn can't use it. */
3393 if (libcall_is_dead)
3395 prev = propagate_block_delete_libcall (pbi->bb, insn, note);
3396 insn = NEXT_INSN (prev);
3399 propagate_block_delete_insn (pbi->bb, insn);
3404 /* See if this is an increment or decrement that can be merged into
3405 a following memory address. */
3408 register rtx x = single_set (insn);
3410 /* Does this instruction increment or decrement a register? */
3411 if (!reload_completed
3412 && (flags & PROP_AUTOINC)
3414 && GET_CODE (SET_DEST (x)) == REG
3415 && (GET_CODE (SET_SRC (x)) == PLUS
3416 || GET_CODE (SET_SRC (x)) == MINUS)
3417 && XEXP (SET_SRC (x), 0) == SET_DEST (x)
3418 && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3419 /* Ok, look for a following memory ref we can combine with.
3420 If one is found, change the memory ref to a PRE_INC
3421 or PRE_DEC, cancel this insn, and return 1.
3422 Return 0 if nothing has been done. */
3423 && try_pre_increment_1 (pbi, insn))
3426 #endif /* AUTO_INC_DEC */
3428 CLEAR_REG_SET (pbi->new_set);
3430 /* If this is not the final pass, and this insn is copying the value of
3431 a library call and it's dead, don't scan the insns that perform the
3432 library call, so that the call's arguments are not marked live. */
3433 if (libcall_is_dead)
3435 /* Record the death of the dest reg. */
3436 mark_set_regs (pbi, PATTERN (insn), insn);
3438 insn = XEXP (note, 0);
3439 return PREV_INSN (insn);
3441 else if (GET_CODE (PATTERN (insn)) == SET
3442 && SET_DEST (PATTERN (insn)) == stack_pointer_rtx
3443 && GET_CODE (SET_SRC (PATTERN (insn))) == PLUS
3444 && XEXP (SET_SRC (PATTERN (insn)), 0) == stack_pointer_rtx
3445 && GET_CODE (XEXP (SET_SRC (PATTERN (insn)), 1)) == CONST_INT)
3446 /* We have an insn to pop a constant amount off the stack.
3447 (Such insns use PLUS regardless of the direction of the stack,
3448 and any insn to adjust the stack by a constant is always a pop.)
3449 These insns, if not dead stores, have no effect on life. */
3453 /* Any regs live at the time of a call instruction must not go
3454 in a register clobbered by calls. Find all regs now live and
3455 record this for them. */
3457 if (GET_CODE (insn) == CALL_INSN && (flags & PROP_REG_INFO))
3458 EXECUTE_IF_SET_IN_REG_SET (pbi->reg_live, 0, i,
3459 { REG_N_CALLS_CROSSED (i)++; });
3461 /* Record sets. Do this even for dead instructions, since they
3462 would have killed the values if they hadn't been deleted. */
3463 mark_set_regs (pbi, PATTERN (insn), insn);
3465 if (GET_CODE (insn) == CALL_INSN)
3471 if (GET_CODE (PATTERN (insn)) == COND_EXEC)
3472 cond = COND_EXEC_TEST (PATTERN (insn));
3474 /* Non-constant calls clobber memory. */
3475 if (! CONST_CALL_P (insn))
3476 free_EXPR_LIST_list (&pbi->mem_set_list);
3478 /* There may be extra registers to be clobbered. */
3479 for (note = CALL_INSN_FUNCTION_USAGE (insn);
3481 note = XEXP (note, 1))
3482 if (GET_CODE (XEXP (note, 0)) == CLOBBER)
3483 mark_set_1 (pbi, CLOBBER, XEXP (XEXP (note, 0), 0),
3484 cond, insn, pbi->flags);
3486 /* Calls change all call-used and global registers. */
3487 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3488 if (call_used_regs[i] && ! global_regs[i]
3491 /* We do not want REG_UNUSED notes for these registers. */
3492 mark_set_1 (pbi, CLOBBER, gen_rtx_REG (reg_raw_mode[i], i),
3494 pbi->flags & ~(PROP_DEATH_NOTES | PROP_REG_INFO));
3498 /* If an insn doesn't use CC0, it becomes dead since we assume
3499 that every insn clobbers it. So show it dead here;
3500 mark_used_regs will set it live if it is referenced. */
3505 mark_used_regs (pbi, PATTERN (insn), NULL_RTX, insn);
3507 /* Sometimes we may have inserted something before INSN (such as a move)
3508 when we make an auto-inc. So ensure we will scan those insns. */
3510 prev = PREV_INSN (insn);
3513 if (! insn_is_dead && GET_CODE (insn) == CALL_INSN)
3519 if (GET_CODE (PATTERN (insn)) == COND_EXEC)
3520 cond = COND_EXEC_TEST (PATTERN (insn));
3522 /* Calls use their arguments. */
3523 for (note = CALL_INSN_FUNCTION_USAGE (insn);
3525 note = XEXP (note, 1))
3526 if (GET_CODE (XEXP (note, 0)) == USE)
3527 mark_used_regs (pbi, XEXP (XEXP (note, 0), 0),
3530 /* The stack ptr is used (honorarily) by a CALL insn. */
3531 SET_REGNO_REG_SET (pbi->reg_live, STACK_POINTER_REGNUM);
3533 /* Calls may also reference any of the global registers,
3534 so they are made live. */
3535 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3537 mark_used_reg (pbi, gen_rtx_REG (reg_raw_mode[i], i),
3542 /* On final pass, update counts of how many insns in which each reg
3544 if (flags & PROP_REG_INFO)
3545 EXECUTE_IF_SET_IN_REG_SET (pbi->reg_live, 0, i,
3546 { REG_LIVE_LENGTH (i)++; });
3551 /* Initialize a propagate_block_info struct for public consumption.
3552 Note that the structure itself is opaque to this file, but that
3553 the user can use the regsets provided here. */
3555 struct propagate_block_info *
3556 init_propagate_block_info (bb, live, local_set, flags)
3562 struct propagate_block_info *pbi = xmalloc (sizeof(*pbi));
3565 pbi->reg_live = live;
3566 pbi->mem_set_list = NULL_RTX;
3567 pbi->local_set = local_set;
3571 if (flags & (PROP_LOG_LINKS | PROP_AUTOINC))
3572 pbi->reg_next_use = (rtx *) xcalloc (max_reg_num (), sizeof (rtx));
3574 pbi->reg_next_use = NULL;
3576 pbi->new_set = BITMAP_XMALLOC ();
3578 #ifdef HAVE_conditional_execution
3579 pbi->reg_cond_dead = splay_tree_new (splay_tree_compare_ints, NULL,
3580 free_reg_cond_life_info);
3581 pbi->reg_cond_reg = BITMAP_XMALLOC ();
3583 /* If this block ends in a conditional branch, for each register live
3584 from one side of the branch and not the other, record the register
3585 as conditionally dead. */
3586 if (GET_CODE (bb->end) == JUMP_INSN
3587 && condjump_p (bb->end)
3588 && ! simplejump_p (bb->end))
3590 regset_head diff_head;
3591 regset diff = INITIALIZE_REG_SET (diff_head);
3592 basic_block bb_true, bb_false;
3593 rtx cond_true, cond_false;
3596 /* Identify the successor blocks. */
3597 bb_true = bb->succ->dest;
3598 if (bb->succ->succ_next != NULL)
3600 bb_false = bb->succ->succ_next->dest;
3602 if (bb->succ->flags & EDGE_FALLTHRU)
3604 basic_block t = bb_false;
3608 else if (! (bb->succ->succ_next->flags & EDGE_FALLTHRU))
3613 /* This can happen with a conditional jump to the next insn. */
3614 if (JUMP_LABEL (bb->end) != bb_true->head)
3617 /* Simplest way to do nothing. */
3621 /* Extract the condition from the branch. */
3622 cond_true = XEXP (SET_SRC (PATTERN (bb->end)), 0);
3623 cond_false = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond_true)),
3624 GET_MODE (cond_true), XEXP (cond_true, 0),
3625 XEXP (cond_true, 1));
3626 if (GET_CODE (XEXP (SET_SRC (PATTERN (bb->end)), 1)) == PC)
3629 cond_false = cond_true;
3633 /* Compute which register lead different lives in the successors. */
3634 if (bitmap_operation (diff, bb_true->global_live_at_start,
3635 bb_false->global_live_at_start, BITMAP_XOR))
3637 if (GET_CODE (XEXP (cond_true, 0)) != REG)
3639 SET_REGNO_REG_SET (pbi->reg_cond_reg, REGNO (XEXP (cond_true, 0)));
3641 /* For each such register, mark it conditionally dead. */
3642 EXECUTE_IF_SET_IN_REG_SET
3645 struct reg_cond_life_info *rcli;
3648 rcli = (struct reg_cond_life_info *) xmalloc (sizeof (*rcli));
3650 if (REGNO_REG_SET_P (bb_true->global_live_at_start, i))
3654 rcli->condition = alloc_EXPR_LIST (0, cond, NULL_RTX);
3656 splay_tree_insert (pbi->reg_cond_dead, i,
3657 (splay_tree_value) rcli);
3661 FREE_REG_SET (diff);
3668 /* Release a propagate_block_info struct. */
3671 free_propagate_block_info (pbi)
3672 struct propagate_block_info *pbi;
3674 free_EXPR_LIST_list (&pbi->mem_set_list);
3676 BITMAP_XFREE (pbi->new_set);
3678 #ifdef HAVE_conditional_execution
3679 splay_tree_delete (pbi->reg_cond_dead);
3680 BITMAP_XFREE (pbi->reg_cond_reg);
3683 if (pbi->reg_next_use)
3684 free (pbi->reg_next_use);
3689 /* Compute the registers live at the beginning of a basic block BB from
3690 those live at the end.
3692 When called, REG_LIVE contains those live at the end. On return, it
3693 contains those live at the beginning.
3695 LOCAL_SET, if non-null, will be set with all registers killed by
3696 this basic block. */
3699 propagate_block (bb, live, local_set, flags)
3705 struct propagate_block_info *pbi;
3708 pbi = init_propagate_block_info (bb, live, local_set, flags);
3710 if (flags & PROP_REG_INFO)
3714 /* Process the regs live at the end of the block.
3715 Mark them as not local to any one basic block. */
3716 EXECUTE_IF_SET_IN_REG_SET (live, 0, i,
3717 { REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL; });
3720 /* Scan the block an insn at a time from end to beginning. */
3722 for (insn = bb->end; ; insn = prev)
3724 /* If this is a call to `setjmp' et al, warn if any
3725 non-volatile datum is live. */
3726 if ((flags & PROP_REG_INFO)
3727 && GET_CODE (insn) == NOTE
3728 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
3729 IOR_REG_SET (regs_live_at_setjmp, pbi->reg_live);
3731 prev = propagate_one_insn (pbi, insn);
3733 if (insn == bb->head)
3737 free_propagate_block_info (pbi);
3740 /* Return 1 if X (the body of an insn, or part of it) is just dead stores
3741 (SET expressions whose destinations are registers dead after the insn).
3742 NEEDED is the regset that says which regs are alive after the insn.
3744 Unless CALL_OK is non-zero, an insn is needed if it contains a CALL.
3746 If X is the entire body of an insn, NOTES contains the reg notes
3747 pertaining to the insn. */
3750 insn_dead_p (pbi, x, call_ok, notes)
3751 struct propagate_block_info *pbi;
3754 rtx notes ATTRIBUTE_UNUSED;
3756 enum rtx_code code = GET_CODE (x);
3759 /* If flow is invoked after reload, we must take existing AUTO_INC
3760 expresions into account. */
3761 if (reload_completed)
3763 for ( ; notes; notes = XEXP (notes, 1))
3765 if (REG_NOTE_KIND (notes) == REG_INC)
3767 int regno = REGNO (XEXP (notes, 0));
3769 /* Don't delete insns to set global regs. */
3770 if ((regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
3771 || REGNO_REG_SET_P (pbi->reg_live, regno))
3778 /* If setting something that's a reg or part of one,
3779 see if that register's altered value will be live. */
3783 rtx r = SET_DEST (x);
3786 if (GET_CODE (r) == CC0)
3787 return ! pbi->cc0_live;
3790 /* A SET that is a subroutine call cannot be dead. */
3791 if (GET_CODE (SET_SRC (x)) == CALL)
3797 /* Don't eliminate loads from volatile memory or volatile asms. */
3798 else if (volatile_refs_p (SET_SRC (x)))
3801 if (GET_CODE (r) == MEM)
3805 if (MEM_VOLATILE_P (r))
3808 /* Walk the set of memory locations we are currently tracking
3809 and see if one is an identical match to this memory location.
3810 If so, this memory write is dead (remember, we're walking
3811 backwards from the end of the block to the start. */
3812 temp = pbi->mem_set_list;
3815 if (rtx_equal_p (XEXP (temp, 0), r))
3817 temp = XEXP (temp, 1);
3822 while (GET_CODE (r) == SUBREG
3823 || GET_CODE (r) == STRICT_LOW_PART
3824 || GET_CODE (r) == ZERO_EXTRACT)
3827 if (GET_CODE (r) == REG)
3829 int regno = REGNO (r);
3832 if (REGNO_REG_SET_P (pbi->reg_live, regno))
3835 /* If this is a hard register, verify that subsequent
3836 words are not needed. */
3837 if (regno < FIRST_PSEUDO_REGISTER)
3839 int n = HARD_REGNO_NREGS (regno, GET_MODE (r));
3842 if (REGNO_REG_SET_P (pbi->reg_live, regno+n))
3846 /* Don't delete insns to set global regs. */
3847 if (regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
3850 /* Make sure insns to set the stack pointer aren't deleted. */
3851 if (regno == STACK_POINTER_REGNUM)
3854 /* Make sure insns to set the frame pointer aren't deleted. */
3855 if (regno == FRAME_POINTER_REGNUM
3856 && (! reload_completed || frame_pointer_needed))
3858 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3859 if (regno == HARD_FRAME_POINTER_REGNUM
3860 && (! reload_completed || frame_pointer_needed))
3864 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3865 /* Make sure insns to set arg pointer are never deleted
3866 (if the arg pointer isn't fixed, there will be a USE
3867 for it, so we can treat it normally). */
3868 if (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
3872 /* Otherwise, the set is dead. */
3878 /* If performing several activities, insn is dead if each activity
3879 is individually dead. Also, CLOBBERs and USEs can be ignored; a
3880 CLOBBER or USE that's inside a PARALLEL doesn't make the insn
3882 else if (code == PARALLEL)
3884 int i = XVECLEN (x, 0);
3886 for (i--; i >= 0; i--)
3887 if (GET_CODE (XVECEXP (x, 0, i)) != CLOBBER
3888 && GET_CODE (XVECEXP (x, 0, i)) != USE
3889 && ! insn_dead_p (pbi, XVECEXP (x, 0, i), call_ok, NULL_RTX))
3895 /* A CLOBBER of a pseudo-register that is dead serves no purpose. That
3896 is not necessarily true for hard registers. */
3897 else if (code == CLOBBER && GET_CODE (XEXP (x, 0)) == REG
3898 && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER
3899 && ! REGNO_REG_SET_P (pbi->reg_live, REGNO (XEXP (x, 0))))
3902 /* We do not check other CLOBBER or USE here. An insn consisting of just
3903 a CLOBBER or just a USE should not be deleted. */
3907 /* If X is the pattern of the last insn in a libcall, and assuming X is dead,
3908 return 1 if the entire library call is dead.
3909 This is true if X copies a register (hard or pseudo)
3910 and if the hard return reg of the call insn is dead.
3911 (The caller should have tested the destination of X already for death.)
3913 If this insn doesn't just copy a register, then we don't
3914 have an ordinary libcall. In that case, cse could not have
3915 managed to substitute the source for the dest later on,
3916 so we can assume the libcall is dead.
3918 NEEDED is the bit vector of pseudoregs live before this insn.
3919 NOTE is the REG_RETVAL note of the insn. INSN is the insn itself. */
3922 libcall_dead_p (pbi, x, note, insn)
3923 struct propagate_block_info *pbi;
3928 register RTX_CODE code = GET_CODE (x);
3932 register rtx r = SET_SRC (x);
3933 if (GET_CODE (r) == REG)
3935 rtx call = XEXP (note, 0);
3939 /* Find the call insn. */
3940 while (call != insn && GET_CODE (call) != CALL_INSN)
3941 call = NEXT_INSN (call);
3943 /* If there is none, do nothing special,
3944 since ordinary death handling can understand these insns. */
3948 /* See if the hard reg holding the value is dead.
3949 If this is a PARALLEL, find the call within it. */
3950 call_pat = PATTERN (call);
3951 if (GET_CODE (call_pat) == PARALLEL)
3953 for (i = XVECLEN (call_pat, 0) - 1; i >= 0; i--)
3954 if (GET_CODE (XVECEXP (call_pat, 0, i)) == SET
3955 && GET_CODE (SET_SRC (XVECEXP (call_pat, 0, i))) == CALL)
3958 /* This may be a library call that is returning a value
3959 via invisible pointer. Do nothing special, since
3960 ordinary death handling can understand these insns. */
3964 call_pat = XVECEXP (call_pat, 0, i);
3967 return insn_dead_p (pbi, call_pat, 1, REG_NOTES (call));
3973 /* Return 1 if register REGNO was used before it was set, i.e. if it is
3974 live at function entry. Don't count global register variables, variables
3975 in registers that can be used for function arg passing, or variables in
3976 fixed hard registers. */
3979 regno_uninitialized (regno)
3982 if (n_basic_blocks == 0
3983 || (regno < FIRST_PSEUDO_REGISTER
3984 && (global_regs[regno]
3985 || fixed_regs[regno]
3986 || FUNCTION_ARG_REGNO_P (regno))))
3989 return REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, regno);
3992 /* 1 if register REGNO was alive at a place where `setjmp' was called
3993 and was set more than once or is an argument.
3994 Such regs may be clobbered by `longjmp'. */
3997 regno_clobbered_at_setjmp (regno)
4000 if (n_basic_blocks == 0)
4003 return ((REG_N_SETS (regno) > 1
4004 || REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, regno))
4005 && REGNO_REG_SET_P (regs_live_at_setjmp, regno));
4008 /* INSN references memory, possibly using autoincrement addressing modes.
4009 Find any entries on the mem_set_list that need to be invalidated due
4010 to an address change. */
4013 invalidate_mems_from_autoinc (pbi, insn)
4014 struct propagate_block_info *pbi;
4017 rtx note = REG_NOTES (insn);
4018 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
4020 if (REG_NOTE_KIND (note) == REG_INC)
4022 rtx temp = pbi->mem_set_list;
4023 rtx prev = NULL_RTX;
4028 next = XEXP (temp, 1);
4029 if (reg_overlap_mentioned_p (XEXP (note, 0), XEXP (temp, 0)))
4031 /* Splice temp out of list. */
4033 XEXP (prev, 1) = next;
4035 pbi->mem_set_list = next;
4036 free_EXPR_LIST_node (temp);
4046 /* Process the registers that are set within X. Their bits are set to
4047 1 in the regset DEAD, because they are dead prior to this insn.
4049 If INSN is nonzero, it is the insn being processed.
4051 FLAGS is the set of operations to perform. */
4054 mark_set_regs (pbi, x, insn)
4055 struct propagate_block_info *pbi;
4058 rtx cond = NULL_RTX;
4062 switch (code = GET_CODE (x))
4066 mark_set_1 (pbi, code, SET_DEST (x), cond, insn, pbi->flags);
4070 cond = COND_EXEC_TEST (x);
4071 x = COND_EXEC_CODE (x);
4077 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
4079 rtx sub = XVECEXP (x, 0, i);
4080 switch (code = GET_CODE (sub))
4083 if (cond != NULL_RTX)
4086 cond = COND_EXEC_TEST (sub);
4087 sub = COND_EXEC_CODE (sub);
4088 if (GET_CODE (sub) != SET && GET_CODE (sub) != CLOBBER)
4094 mark_set_1 (pbi, code, SET_DEST (sub), cond, insn, pbi->flags);
4109 /* Process a single SET rtx, X. */
4112 mark_set_1 (pbi, code, reg, cond, insn, flags)
4113 struct propagate_block_info *pbi;
4115 rtx reg, cond, insn;
4118 int regno_first = -1, regno_last = -1;
4122 /* Some targets place small structures in registers for
4123 return values of functions. We have to detect this
4124 case specially here to get correct flow information. */
4125 if (GET_CODE (reg) == PARALLEL
4126 && GET_MODE (reg) == BLKmode)
4128 for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
4129 mark_set_1 (pbi, code, XVECEXP (reg, 0, i), cond, insn, flags);
4133 /* Modifying just one hardware register of a multi-reg value or just a
4134 byte field of a register does not mean the value from before this insn
4135 is now dead. Of course, if it was dead after it's unused now. */
4137 switch (GET_CODE (reg))
4141 case STRICT_LOW_PART:
4142 /* ??? Assumes STRICT_LOW_PART not used on multi-word registers. */
4144 reg = XEXP (reg, 0);
4145 while (GET_CODE (reg) == SUBREG
4146 || GET_CODE (reg) == ZERO_EXTRACT
4147 || GET_CODE (reg) == SIGN_EXTRACT
4148 || GET_CODE (reg) == STRICT_LOW_PART);
4149 if (GET_CODE (reg) == MEM)
4151 not_dead = REGNO_REG_SET_P (pbi->reg_live, REGNO (reg));
4155 regno_last = regno_first = REGNO (reg);
4156 if (regno_first < FIRST_PSEUDO_REGISTER)
4157 regno_last += HARD_REGNO_NREGS (regno_first, GET_MODE (reg)) - 1;
4161 if (GET_CODE (SUBREG_REG (reg)) == REG)
4163 enum machine_mode outer_mode = GET_MODE (reg);
4164 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (reg));
4166 /* Identify the range of registers affected. This is moderately
4167 tricky for hard registers. See alter_subreg. */
4169 regno_last = regno_first = REGNO (SUBREG_REG (reg));
4170 if (regno_first < FIRST_PSEUDO_REGISTER)
4172 #ifdef ALTER_HARD_SUBREG
4173 regno_first = ALTER_HARD_SUBREG (outer_mode, SUBREG_WORD (reg),
4174 inner_mode, regno_first);
4176 regno_first += SUBREG_WORD (reg);
4178 regno_last = (regno_first
4179 + HARD_REGNO_NREGS (regno_first, outer_mode) - 1);
4181 /* Since we've just adjusted the register number ranges, make
4182 sure REG matches. Otherwise some_was_live will be clear
4183 when it shouldn't have been, and we'll create incorrect
4184 REG_UNUSED notes. */
4185 reg = gen_rtx_REG (outer_mode, regno_first);
4189 /* If the number of words in the subreg is less than the number
4190 of words in the full register, we have a well-defined partial
4191 set. Otherwise the high bits are undefined.
4193 This is only really applicable to pseudos, since we just took
4194 care of multi-word hard registers. */
4195 if (((GET_MODE_SIZE (outer_mode)
4196 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
4197 < ((GET_MODE_SIZE (inner_mode)
4198 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))
4199 not_dead = REGNO_REG_SET_P (pbi->reg_live, regno_first);
4201 reg = SUBREG_REG (reg);
4205 reg = SUBREG_REG (reg);
4212 /* If this set is a MEM, then it kills any aliased writes.
4213 If this set is a REG, then it kills any MEMs which use the reg. */
4214 if (flags & PROP_SCAN_DEAD_CODE)
4216 if (GET_CODE (reg) == MEM || GET_CODE (reg) == REG)
4218 rtx temp = pbi->mem_set_list;
4219 rtx prev = NULL_RTX;
4224 next = XEXP (temp, 1);
4225 if ((GET_CODE (reg) == MEM
4226 && output_dependence (XEXP (temp, 0), reg))
4227 || (GET_CODE (reg) == REG
4228 && reg_overlap_mentioned_p (reg, XEXP (temp, 0))))
4230 /* Splice this entry out of the list. */
4232 XEXP (prev, 1) = next;
4234 pbi->mem_set_list = next;
4235 free_EXPR_LIST_node (temp);
4243 /* If the memory reference had embedded side effects (autoincrement
4244 address modes. Then we may need to kill some entries on the
4246 if (insn && GET_CODE (reg) == MEM)
4247 invalidate_mems_from_autoinc (pbi, insn);
4249 if (GET_CODE (reg) == MEM && ! side_effects_p (reg)
4250 /* We do not know the size of a BLKmode store, so we do not track
4251 them for redundant store elimination. */
4252 && GET_MODE (reg) != BLKmode
4253 /* There are no REG_INC notes for SP, so we can't assume we'll see
4254 everything that invalidates it. To be safe, don't eliminate any
4255 stores though SP; none of them should be redundant anyway. */
4256 && ! reg_mentioned_p (stack_pointer_rtx, reg))
4257 pbi->mem_set_list = alloc_EXPR_LIST (0, reg, pbi->mem_set_list);
4260 if (GET_CODE (reg) == REG
4261 && ! (regno_first == FRAME_POINTER_REGNUM
4262 && (! reload_completed || frame_pointer_needed))
4263 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
4264 && ! (regno_first == HARD_FRAME_POINTER_REGNUM
4265 && (! reload_completed || frame_pointer_needed))
4267 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
4268 && ! (regno_first == ARG_POINTER_REGNUM && fixed_regs[regno_first])
4272 int some_was_live = 0, some_was_dead = 0;
4274 for (i = regno_first; i <= regno_last; ++i)
4276 int needed_regno = REGNO_REG_SET_P (pbi->reg_live, i);
4278 SET_REGNO_REG_SET (pbi->local_set, i);
4279 if (code != CLOBBER)
4280 SET_REGNO_REG_SET (pbi->new_set, i);
4282 some_was_live |= needed_regno;
4283 some_was_dead |= ! needed_regno;
4286 #ifdef HAVE_conditional_execution
4287 /* Consider conditional death in deciding that the register needs
4289 if (some_was_live && ! not_dead
4290 /* The stack pointer is never dead. Well, not strictly true,
4291 but it's very difficult to tell from here. Hopefully
4292 combine_stack_adjustments will fix up the most egregious
4294 && regno_first != STACK_POINTER_REGNUM)
4296 for (i = regno_first; i <= regno_last; ++i)
4297 if (! mark_regno_cond_dead (pbi, i, cond))
4302 /* Additional data to record if this is the final pass. */
4303 if (flags & (PROP_LOG_LINKS | PROP_REG_INFO
4304 | PROP_DEATH_NOTES | PROP_AUTOINC))
4307 register int blocknum = pbi->bb->index;
4310 if (flags & (PROP_LOG_LINKS | PROP_AUTOINC))
4312 y = pbi->reg_next_use[regno_first];
4314 /* The next use is no longer next, since a store intervenes. */
4315 for (i = regno_first; i <= regno_last; ++i)
4316 pbi->reg_next_use[i] = 0;
4319 if (flags & PROP_REG_INFO)
4321 for (i = regno_first; i <= regno_last; ++i)
4323 /* Count (weighted) references, stores, etc. This counts a
4324 register twice if it is modified, but that is correct. */
4325 REG_N_SETS (i) += 1;
4326 REG_N_REFS (i) += (optimize_size ? 1
4327 : pbi->bb->loop_depth + 1);
4329 /* The insns where a reg is live are normally counted
4330 elsewhere, but we want the count to include the insn
4331 where the reg is set, and the normal counting mechanism
4332 would not count it. */
4333 REG_LIVE_LENGTH (i) += 1;
4336 /* If this is a hard reg, record this function uses the reg. */
4337 if (regno_first < FIRST_PSEUDO_REGISTER)
4339 for (i = regno_first; i <= regno_last; i++)
4340 regs_ever_live[i] = 1;
4344 /* Keep track of which basic blocks each reg appears in. */
4345 if (REG_BASIC_BLOCK (regno_first) == REG_BLOCK_UNKNOWN)
4346 REG_BASIC_BLOCK (regno_first) = blocknum;
4347 else if (REG_BASIC_BLOCK (regno_first) != blocknum)
4348 REG_BASIC_BLOCK (regno_first) = REG_BLOCK_GLOBAL;
4352 if (! some_was_dead)
4354 if (flags & PROP_LOG_LINKS)
4356 /* Make a logical link from the next following insn
4357 that uses this register, back to this insn.
4358 The following insns have already been processed.
4360 We don't build a LOG_LINK for hard registers containing
4361 in ASM_OPERANDs. If these registers get replaced,
4362 we might wind up changing the semantics of the insn,
4363 even if reload can make what appear to be valid
4364 assignments later. */
4365 if (y && (BLOCK_NUM (y) == blocknum)
4366 && (regno_first >= FIRST_PSEUDO_REGISTER
4367 || asm_noperands (PATTERN (y)) < 0))
4368 LOG_LINKS (y) = alloc_INSN_LIST (insn, LOG_LINKS (y));
4373 else if (! some_was_live)
4375 if (flags & PROP_REG_INFO)
4376 REG_N_DEATHS (regno_first) += 1;
4378 if (flags & PROP_DEATH_NOTES)
4380 /* Note that dead stores have already been deleted
4381 when possible. If we get here, we have found a
4382 dead store that cannot be eliminated (because the
4383 same insn does something useful). Indicate this
4384 by marking the reg being set as dying here. */
4386 = alloc_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn));
4391 if (flags & PROP_DEATH_NOTES)
4393 /* This is a case where we have a multi-word hard register
4394 and some, but not all, of the words of the register are
4395 needed in subsequent insns. Write REG_UNUSED notes
4396 for those parts that were not needed. This case should
4399 for (i = regno_first; i <= regno_last; ++i)
4400 if (! REGNO_REG_SET_P (pbi->reg_live, i))
4402 = alloc_EXPR_LIST (REG_UNUSED,
4403 gen_rtx_REG (reg_raw_mode[i], i),
4409 /* Mark the register as being dead. */
4412 /* The stack pointer is never dead. Well, not strictly true,
4413 but it's very difficult to tell from here. Hopefully
4414 combine_stack_adjustments will fix up the most egregious
4416 && regno_first != STACK_POINTER_REGNUM)
4418 for (i = regno_first; i <= regno_last; ++i)
4419 CLEAR_REGNO_REG_SET (pbi->reg_live, i);
4422 else if (GET_CODE (reg) == REG)
4424 if (flags & (PROP_LOG_LINKS | PROP_AUTOINC))
4425 pbi->reg_next_use[regno_first] = 0;
4428 /* If this is the last pass and this is a SCRATCH, show it will be dying
4429 here and count it. */
4430 else if (GET_CODE (reg) == SCRATCH)
4432 if (flags & PROP_DEATH_NOTES)
4434 = alloc_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn));
4438 #ifdef HAVE_conditional_execution
4439 /* Mark REGNO conditionally dead. Return true if the register is
4440 now unconditionally dead. */
4443 mark_regno_cond_dead (pbi, regno, cond)
4444 struct propagate_block_info *pbi;
4448 /* If this is a store to a predicate register, the value of the
4449 predicate is changing, we don't know that the predicate as seen
4450 before is the same as that seen after. Flush all dependant
4451 conditions from reg_cond_dead. This will make all such
4452 conditionally live registers unconditionally live. */
4453 if (REGNO_REG_SET_P (pbi->reg_cond_reg, regno))
4454 flush_reg_cond_reg (pbi, regno);
4456 /* If this is an unconditional store, remove any conditional
4457 life that may have existed. */
4458 if (cond == NULL_RTX)
4459 splay_tree_remove (pbi->reg_cond_dead, regno);
4462 splay_tree_node node;
4463 struct reg_cond_life_info *rcli;
4466 /* Otherwise this is a conditional set. Record that fact.
4467 It may have been conditionally used, or there may be a
4468 subsequent set with a complimentary condition. */
4470 node = splay_tree_lookup (pbi->reg_cond_dead, regno);
4473 /* The register was unconditionally live previously.
4474 Record the current condition as the condition under
4475 which it is dead. */
4476 rcli = (struct reg_cond_life_info *)
4477 xmalloc (sizeof (*rcli));
4478 rcli->condition = alloc_EXPR_LIST (0, cond, NULL_RTX);
4479 splay_tree_insert (pbi->reg_cond_dead, regno,
4480 (splay_tree_value) rcli);
4482 SET_REGNO_REG_SET (pbi->reg_cond_reg,
4483 REGNO (XEXP (cond, 0)));
4485 /* Not unconditionaly dead. */
4490 /* The register was conditionally live previously.
4491 Add the new condition to the old. */
4492 rcli = (struct reg_cond_life_info *) node->value;
4493 ncond = rcli->condition;
4494 ncond = ior_reg_cond (ncond, cond);
4496 /* If the register is now unconditionally dead,
4497 remove the entry in the splay_tree. */
4498 if (ncond == const1_rtx)
4499 splay_tree_remove (pbi->reg_cond_dead, regno);
4502 rcli->condition = ncond;
4504 SET_REGNO_REG_SET (pbi->reg_cond_reg,
4505 REGNO (XEXP (cond, 0)));
4507 /* Not unconditionaly dead. */
4516 /* Called from splay_tree_delete for pbi->reg_cond_life. */
4519 free_reg_cond_life_info (value)
4520 splay_tree_value value;
4522 struct reg_cond_life_info *rcli = (struct reg_cond_life_info *) value;
4523 free_EXPR_LIST_list (&rcli->condition);
4527 /* Helper function for flush_reg_cond_reg. */
4530 flush_reg_cond_reg_1 (node, data)
4531 splay_tree_node node;
4534 struct reg_cond_life_info *rcli;
4535 int *xdata = (int *) data;
4536 unsigned int regno = xdata[0];
4539 /* Don't need to search if last flushed value was farther on in
4540 the in-order traversal. */
4541 if (xdata[1] >= (int) node->key)
4544 /* Splice out portions of the expression that refer to regno. */
4545 rcli = (struct reg_cond_life_info *) node->value;
4546 c = *(prev = &rcli->condition);
4549 if (regno == REGNO (XEXP (XEXP (c, 0), 0)))
4551 rtx next = XEXP (c, 1);
4552 free_EXPR_LIST_node (c);
4556 c = *(prev = &XEXP (c, 1));
4559 /* If the entire condition is now NULL, signal the node to be removed. */
4560 if (! rcli->condition)
4562 xdata[1] = node->key;
4569 /* Flush all (sub) expressions referring to REGNO from REG_COND_LIVE. */
4572 flush_reg_cond_reg (pbi, regno)
4573 struct propagate_block_info *pbi;
4580 while (splay_tree_foreach (pbi->reg_cond_dead,
4581 flush_reg_cond_reg_1, pair) == -1)
4582 splay_tree_remove (pbi->reg_cond_dead, pair[1]);
4584 CLEAR_REGNO_REG_SET (pbi->reg_cond_reg, regno);
4587 /* Logical arithmetic on predicate conditions. IOR, NOT and NAND.
4588 We actually use EXPR_LIST to chain the sub-expressions together
4589 instead of IOR because it's easier to manipulate and we have
4590 the lists.c functions to reuse nodes.
4592 Return a new rtl expression as appropriate. */
4595 ior_reg_cond (old, x)
4598 enum rtx_code x_code;
4602 /* We expect these conditions to be of the form (eq reg 0). */
4603 x_code = GET_CODE (x);
4604 if (GET_RTX_CLASS (x_code) != '<'
4605 || GET_CODE (x_reg = XEXP (x, 0)) != REG
4606 || XEXP (x, 1) != const0_rtx)
4609 /* Search the expression for an existing sub-expression of X_REG. */
4610 for (c = old; c ; c = XEXP (c, 1))
4612 rtx y = XEXP (c, 0);
4613 if (REGNO (XEXP (y, 0)) == REGNO (x_reg))
4615 /* If we find X already present in OLD, we need do nothing. */
4616 if (GET_CODE (y) == x_code)
4619 /* If we find X being a compliment of a condition in OLD,
4620 then the entire condition is true. */
4621 if (GET_CODE (y) == reverse_condition (x_code))
4626 /* Otherwise just add to the chain. */
4627 return alloc_EXPR_LIST (0, x, old);
4634 enum rtx_code x_code;
4637 /* We expect these conditions to be of the form (eq reg 0). */
4638 x_code = GET_CODE (x);
4639 if (GET_RTX_CLASS (x_code) != '<'
4640 || GET_CODE (x_reg = XEXP (x, 0)) != REG
4641 || XEXP (x, 1) != const0_rtx)
4644 return alloc_EXPR_LIST (0, gen_rtx_fmt_ee (reverse_condition (x_code),
4645 VOIDmode, x_reg, const0_rtx),
4650 nand_reg_cond (old, x)
4653 enum rtx_code x_code;
4657 /* We expect these conditions to be of the form (eq reg 0). */
4658 x_code = GET_CODE (x);
4659 if (GET_RTX_CLASS (x_code) != '<'
4660 || GET_CODE (x_reg = XEXP (x, 0)) != REG
4661 || XEXP (x, 1) != const0_rtx)
4664 /* Search the expression for an existing sub-expression of X_REG. */
4666 for (c = *(prev = &old); c ; c = *(prev = &XEXP (c, 1)))
4668 rtx y = XEXP (c, 0);
4669 if (REGNO (XEXP (y, 0)) == REGNO (x_reg))
4671 /* If we find X already present in OLD, then we need to
4673 if (GET_CODE (y) == x_code)
4675 *prev = XEXP (c, 1);
4676 free_EXPR_LIST_node (c);
4677 return old ? old : const0_rtx;
4680 /* If we find X being a compliment of a condition in OLD,
4681 then we need do nothing. */
4682 if (GET_CODE (y) == reverse_condition (x_code))
4687 /* Otherwise, by implication, the register in question is now live for
4688 the inverse of the condition X. */
4689 return alloc_EXPR_LIST (0, gen_rtx_fmt_ee (reverse_condition (x_code),
4690 VOIDmode, x_reg, const0_rtx),
4693 #endif /* HAVE_conditional_execution */
4697 /* X is a MEM found in INSN. See if we can convert it into an auto-increment
4701 find_auto_inc (pbi, x, insn)
4702 struct propagate_block_info *pbi;
4706 rtx addr = XEXP (x, 0);
4707 HOST_WIDE_INT offset = 0;
4710 /* Here we detect use of an index register which might be good for
4711 postincrement, postdecrement, preincrement, or predecrement. */
4713 if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
4714 offset = INTVAL (XEXP (addr, 1)), addr = XEXP (addr, 0);
4716 if (GET_CODE (addr) == REG)
4719 register int size = GET_MODE_SIZE (GET_MODE (x));
4722 int regno = REGNO (addr);
4724 /* Is the next use an increment that might make auto-increment? */
4725 if ((incr = pbi->reg_next_use[regno]) != 0
4726 && (set = single_set (incr)) != 0
4727 && GET_CODE (set) == SET
4728 && BLOCK_NUM (incr) == BLOCK_NUM (insn)
4729 /* Can't add side effects to jumps; if reg is spilled and
4730 reloaded, there's no way to store back the altered value. */
4731 && GET_CODE (insn) != JUMP_INSN
4732 && (y = SET_SRC (set), GET_CODE (y) == PLUS)
4733 && XEXP (y, 0) == addr
4734 && GET_CODE (XEXP (y, 1)) == CONST_INT
4735 && ((HAVE_POST_INCREMENT
4736 && (INTVAL (XEXP (y, 1)) == size && offset == 0))
4737 || (HAVE_POST_DECREMENT
4738 && (INTVAL (XEXP (y, 1)) == - size && offset == 0))
4739 || (HAVE_PRE_INCREMENT
4740 && (INTVAL (XEXP (y, 1)) == size && offset == size))
4741 || (HAVE_PRE_DECREMENT
4742 && (INTVAL (XEXP (y, 1)) == - size && offset == - size)))
4743 /* Make sure this reg appears only once in this insn. */
4744 && (use = find_use_as_address (PATTERN (insn), addr, offset),
4745 use != 0 && use != (rtx) 1))
4747 rtx q = SET_DEST (set);
4748 enum rtx_code inc_code = (INTVAL (XEXP (y, 1)) == size
4749 ? (offset ? PRE_INC : POST_INC)
4750 : (offset ? PRE_DEC : POST_DEC));
4752 if (dead_or_set_p (incr, addr)
4753 /* Mustn't autoinc an eliminable register. */
4754 && (regno >= FIRST_PSEUDO_REGISTER
4755 || ! TEST_HARD_REG_BIT (elim_reg_set, regno)))
4757 /* This is the simple case. Try to make the auto-inc. If
4758 we can't, we are done. Otherwise, we will do any
4759 needed updates below. */
4760 if (! validate_change (insn, &XEXP (x, 0),
4761 gen_rtx_fmt_e (inc_code, Pmode, addr),
4765 else if (GET_CODE (q) == REG
4766 /* PREV_INSN used here to check the semi-open interval
4768 && ! reg_used_between_p (q, PREV_INSN (insn), incr)
4769 /* We must also check for sets of q as q may be
4770 a call clobbered hard register and there may
4771 be a call between PREV_INSN (insn) and incr. */
4772 && ! reg_set_between_p (q, PREV_INSN (insn), incr))
4774 /* We have *p followed sometime later by q = p+size.
4775 Both p and q must be live afterward,
4776 and q is not used between INSN and its assignment.
4777 Change it to q = p, ...*q..., q = q+size.
4778 Then fall into the usual case. */
4782 emit_move_insn (q, addr);
4783 insns = get_insns ();
4786 if (basic_block_for_insn)
4787 for (temp = insns; temp; temp = NEXT_INSN (temp))
4788 set_block_for_insn (temp, pbi->bb);
4790 /* If we can't make the auto-inc, or can't make the
4791 replacement into Y, exit. There's no point in making
4792 the change below if we can't do the auto-inc and doing
4793 so is not correct in the pre-inc case. */
4795 validate_change (insn, &XEXP (x, 0),
4796 gen_rtx_fmt_e (inc_code, Pmode, q),
4798 validate_change (incr, &XEXP (y, 0), q, 1);
4799 if (! apply_change_group ())
4802 /* We now know we'll be doing this change, so emit the
4803 new insn(s) and do the updates. */
4804 emit_insns_before (insns, insn);
4806 if (pbi->bb->head == insn)
4807 pbi->bb->head = insns;
4809 /* INCR will become a NOTE and INSN won't contain a
4810 use of ADDR. If a use of ADDR was just placed in
4811 the insn before INSN, make that the next use.
4812 Otherwise, invalidate it. */
4813 if (GET_CODE (PREV_INSN (insn)) == INSN
4814 && GET_CODE (PATTERN (PREV_INSN (insn))) == SET
4815 && SET_SRC (PATTERN (PREV_INSN (insn))) == addr)
4816 pbi->reg_next_use[regno] = PREV_INSN (insn);
4818 pbi->reg_next_use[regno] = 0;
4823 /* REGNO is now used in INCR which is below INSN, but it
4824 previously wasn't live here. If we don't mark it as
4825 live, we'll put a REG_DEAD note for it on this insn,
4826 which is incorrect. */
4827 SET_REGNO_REG_SET (pbi->reg_live, regno);
4829 /* If there are any calls between INSN and INCR, show
4830 that REGNO now crosses them. */
4831 for (temp = insn; temp != incr; temp = NEXT_INSN (temp))
4832 if (GET_CODE (temp) == CALL_INSN)
4833 REG_N_CALLS_CROSSED (regno)++;
4838 /* If we haven't returned, it means we were able to make the
4839 auto-inc, so update the status. First, record that this insn
4840 has an implicit side effect. */
4843 = alloc_EXPR_LIST (REG_INC, addr, REG_NOTES (insn));
4845 /* Modify the old increment-insn to simply copy
4846 the already-incremented value of our register. */
4847 if (! validate_change (incr, &SET_SRC (set), addr, 0))
4850 /* If that makes it a no-op (copying the register into itself) delete
4851 it so it won't appear to be a "use" and a "set" of this
4853 if (SET_DEST (set) == addr)
4855 /* If the original source was dead, it's dead now. */
4856 rtx note = find_reg_note (incr, REG_DEAD, NULL_RTX);
4857 if (note && XEXP (note, 0) != addr)
4858 CLEAR_REGNO_REG_SET (pbi->reg_live, REGNO (XEXP (note, 0)));
4860 PUT_CODE (incr, NOTE);
4861 NOTE_LINE_NUMBER (incr) = NOTE_INSN_DELETED;
4862 NOTE_SOURCE_FILE (incr) = 0;
4865 if (regno >= FIRST_PSEUDO_REGISTER)
4867 /* Count an extra reference to the reg. When a reg is
4868 incremented, spilling it is worse, so we want to make
4869 that less likely. */
4870 REG_N_REFS (regno) += (optimize_size ? 1
4871 : pbi->bb->loop_depth + 1);
4873 /* Count the increment as a setting of the register,
4874 even though it isn't a SET in rtl. */
4875 REG_N_SETS (regno)++;
4880 #endif /* AUTO_INC_DEC */
4883 mark_used_reg (pbi, reg, cond, insn)
4884 struct propagate_block_info *pbi;
4886 rtx cond ATTRIBUTE_UNUSED;
4889 int regno = REGNO (reg);
4890 int some_was_live = REGNO_REG_SET_P (pbi->reg_live, regno);
4891 int some_was_dead = ! some_was_live;
4895 /* A hard reg in a wide mode may really be multiple registers.
4896 If so, mark all of them just like the first. */
4897 if (regno < FIRST_PSEUDO_REGISTER)
4899 n = HARD_REGNO_NREGS (regno, GET_MODE (reg));
4902 int needed_regno = REGNO_REG_SET_P (pbi->reg_live, regno + n);
4903 some_was_live |= needed_regno;
4904 some_was_dead |= ! needed_regno;
4908 if (pbi->flags & (PROP_LOG_LINKS | PROP_AUTOINC))
4910 /* Record where each reg is used, so when the reg is set we know
4911 the next insn that uses it. */
4912 pbi->reg_next_use[regno] = insn;
4915 if (pbi->flags & PROP_REG_INFO)
4917 if (regno < FIRST_PSEUDO_REGISTER)
4919 /* If this is a register we are going to try to eliminate,
4920 don't mark it live here. If we are successful in
4921 eliminating it, it need not be live unless it is used for
4922 pseudos, in which case it will have been set live when it
4923 was allocated to the pseudos. If the register will not
4924 be eliminated, reload will set it live at that point.
4926 Otherwise, record that this function uses this register. */
4927 /* ??? The PPC backend tries to "eliminate" on the pic
4928 register to itself. This should be fixed. In the mean
4929 time, hack around it. */
4931 if (! (TEST_HARD_REG_BIT (elim_reg_set, regno)
4932 && (regno == FRAME_POINTER_REGNUM
4933 || regno == ARG_POINTER_REGNUM)))
4935 int n = HARD_REGNO_NREGS (regno, GET_MODE (reg));
4937 regs_ever_live[regno + --n] = 1;
4943 /* Keep track of which basic block each reg appears in. */
4945 register int blocknum = pbi->bb->index;
4946 if (REG_BASIC_BLOCK (regno) == REG_BLOCK_UNKNOWN)
4947 REG_BASIC_BLOCK (regno) = blocknum;
4948 else if (REG_BASIC_BLOCK (regno) != blocknum)
4949 REG_BASIC_BLOCK (regno) = REG_BLOCK_GLOBAL;
4951 /* Count (weighted) number of uses of each reg. */
4952 REG_N_REFS (regno) += (optimize_size ? 1
4953 : pbi->bb->loop_depth + 1);
4957 /* Find out if any of the register was set this insn. */
4958 some_not_set = ! REGNO_REG_SET_P (pbi->new_set, regno);
4959 if (regno < FIRST_PSEUDO_REGISTER)
4961 n = HARD_REGNO_NREGS (regno, GET_MODE (reg));
4963 some_not_set |= ! REGNO_REG_SET_P (pbi->new_set, regno + n);
4966 /* Record and count the insns in which a reg dies. If it is used in
4967 this insn and was dead below the insn then it dies in this insn.
4968 If it was set in this insn, we do not make a REG_DEAD note;
4969 likewise if we already made such a note. */
4970 if ((pbi->flags & (PROP_DEATH_NOTES | PROP_REG_INFO))
4974 /* Check for the case where the register dying partially
4975 overlaps the register set by this insn. */
4976 if (regno < FIRST_PSEUDO_REGISTER
4977 && HARD_REGNO_NREGS (regno, GET_MODE (reg)) > 1)
4979 n = HARD_REGNO_NREGS (regno, GET_MODE (reg));
4981 some_was_live |= REGNO_REG_SET_P (pbi->new_set, regno + n);
4984 /* If none of the words in X is needed, make a REG_DEAD note.
4985 Otherwise, we must make partial REG_DEAD notes. */
4986 if (! some_was_live)
4988 if ((pbi->flags & PROP_DEATH_NOTES)
4989 && ! find_regno_note (insn, REG_DEAD, regno))
4991 = alloc_EXPR_LIST (REG_DEAD, reg, REG_NOTES (insn));
4993 if (pbi->flags & PROP_REG_INFO)
4994 REG_N_DEATHS (regno)++;
4998 /* Don't make a REG_DEAD note for a part of a register
4999 that is set in the insn. */
5001 n = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg)) - 1;
5002 for (; n >= regno; n--)
5003 if (! REGNO_REG_SET_P (pbi->reg_live, n)
5004 && ! dead_or_set_regno_p (insn, n))
5006 = alloc_EXPR_LIST (REG_DEAD,
5007 gen_rtx_REG (reg_raw_mode[n], n),
5012 SET_REGNO_REG_SET (pbi->reg_live, regno);
5013 if (regno < FIRST_PSEUDO_REGISTER)
5015 n = HARD_REGNO_NREGS (regno, GET_MODE (reg));
5017 SET_REGNO_REG_SET (pbi->reg_live, regno + n);
5020 #ifdef HAVE_conditional_execution
5021 /* If this is a conditional use, record that fact. If it is later
5022 conditionally set, we'll know to kill the register. */
5023 if (cond != NULL_RTX)
5025 splay_tree_node node;
5026 struct reg_cond_life_info *rcli;
5031 node = splay_tree_lookup (pbi->reg_cond_dead, regno);
5034 /* The register was unconditionally live previously.
5035 No need to do anything. */
5039 /* The register was conditionally live previously.
5040 Subtract the new life cond from the old death cond. */
5041 rcli = (struct reg_cond_life_info *) node->value;
5042 ncond = rcli->condition;
5043 ncond = nand_reg_cond (ncond, cond);
5045 /* If the register is now unconditionally live, remove the
5046 entry in the splay_tree. */
5047 if (ncond == const0_rtx)
5049 rcli->condition = NULL_RTX;
5050 splay_tree_remove (pbi->reg_cond_dead, regno);
5053 rcli->condition = ncond;
5058 /* The register was not previously live at all. Record
5059 the condition under which it is still dead. */
5060 rcli = (struct reg_cond_life_info *) xmalloc (sizeof (*rcli));
5061 rcli->condition = not_reg_cond (cond);
5062 splay_tree_insert (pbi->reg_cond_dead, regno,
5063 (splay_tree_value) rcli);
5069 /* Scan expression X and store a 1-bit in NEW_LIVE for each reg it uses.
5070 This is done assuming the registers needed from X are those that
5071 have 1-bits in PBI->REG_LIVE.
5073 INSN is the containing instruction. If INSN is dead, this function
5077 mark_used_regs (pbi, x, cond, insn)
5078 struct propagate_block_info *pbi;
5081 register RTX_CODE code;
5083 int flags = pbi->flags;
5086 code = GET_CODE (x);
5106 /* If we are clobbering a MEM, mark any registers inside the address
5108 if (GET_CODE (XEXP (x, 0)) == MEM)
5109 mark_used_regs (pbi, XEXP (XEXP (x, 0), 0), cond, insn);
5113 /* Don't bother watching stores to mems if this is not the
5114 final pass. We'll not be deleting dead stores this round. */
5115 if (flags & PROP_SCAN_DEAD_CODE)
5117 /* Invalidate the data for the last MEM stored, but only if MEM is
5118 something that can be stored into. */
5119 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
5120 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
5121 ; /* needn't clear the memory set list */
5124 rtx temp = pbi->mem_set_list;
5125 rtx prev = NULL_RTX;
5130 next = XEXP (temp, 1);
5131 if (anti_dependence (XEXP (temp, 0), x))
5133 /* Splice temp out of the list. */
5135 XEXP (prev, 1) = next;
5137 pbi->mem_set_list = next;
5138 free_EXPR_LIST_node (temp);
5146 /* If the memory reference had embedded side effects (autoincrement
5147 address modes. Then we may need to kill some entries on the
5150 invalidate_mems_from_autoinc (pbi, insn);
5154 if (flags & PROP_AUTOINC)
5155 find_auto_inc (pbi, x, insn);
5160 if (GET_CODE (SUBREG_REG (x)) == REG
5161 && REGNO (SUBREG_REG (x)) >= FIRST_PSEUDO_REGISTER
5162 && (GET_MODE_SIZE (GET_MODE (x))
5163 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
5164 REG_CHANGES_SIZE (REGNO (SUBREG_REG (x))) = 1;
5166 /* While we're here, optimize this case. */
5168 if (GET_CODE (x) != REG)
5173 /* See a register other than being set => mark it as needed. */
5174 mark_used_reg (pbi, x, cond, insn);
5179 register rtx testreg = SET_DEST (x);
5182 /* If storing into MEM, don't show it as being used. But do
5183 show the address as being used. */
5184 if (GET_CODE (testreg) == MEM)
5187 if (flags & PROP_AUTOINC)
5188 find_auto_inc (pbi, testreg, insn);
5190 mark_used_regs (pbi, XEXP (testreg, 0), cond, insn);
5191 mark_used_regs (pbi, SET_SRC (x), cond, insn);
5195 /* Storing in STRICT_LOW_PART is like storing in a reg
5196 in that this SET might be dead, so ignore it in TESTREG.
5197 but in some other ways it is like using the reg.
5199 Storing in a SUBREG or a bit field is like storing the entire
5200 register in that if the register's value is not used
5201 then this SET is not needed. */
5202 while (GET_CODE (testreg) == STRICT_LOW_PART
5203 || GET_CODE (testreg) == ZERO_EXTRACT
5204 || GET_CODE (testreg) == SIGN_EXTRACT
5205 || GET_CODE (testreg) == SUBREG)
5207 if (GET_CODE (testreg) == SUBREG
5208 && GET_CODE (SUBREG_REG (testreg)) == REG
5209 && REGNO (SUBREG_REG (testreg)) >= FIRST_PSEUDO_REGISTER
5210 && (GET_MODE_SIZE (GET_MODE (testreg))
5211 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (testreg)))))
5212 REG_CHANGES_SIZE (REGNO (SUBREG_REG (testreg))) = 1;
5214 /* Modifying a single register in an alternate mode
5215 does not use any of the old value. But these other
5216 ways of storing in a register do use the old value. */
5217 if (GET_CODE (testreg) == SUBREG
5218 && !(REG_SIZE (SUBREG_REG (testreg)) > REG_SIZE (testreg)))
5223 testreg = XEXP (testreg, 0);
5226 /* If this is a store into a register, recursively scan the
5227 value being stored. */
5229 if ((GET_CODE (testreg) == PARALLEL
5230 && GET_MODE (testreg) == BLKmode)
5231 || (GET_CODE (testreg) == REG
5232 && (regno = REGNO (testreg),
5233 ! (regno == FRAME_POINTER_REGNUM
5234 && (! reload_completed || frame_pointer_needed)))
5235 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
5236 && ! (regno == HARD_FRAME_POINTER_REGNUM
5237 && (! reload_completed || frame_pointer_needed))
5239 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
5240 && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
5245 mark_used_regs (pbi, SET_DEST (x), cond, insn);
5246 mark_used_regs (pbi, SET_SRC (x), cond, insn);
5253 case UNSPEC_VOLATILE:
5257 /* Traditional and volatile asm instructions must be considered to use
5258 and clobber all hard registers, all pseudo-registers and all of
5259 memory. So must TRAP_IF and UNSPEC_VOLATILE operations.
5261 Consider for instance a volatile asm that changes the fpu rounding
5262 mode. An insn should not be moved across this even if it only uses
5263 pseudo-regs because it might give an incorrectly rounded result.
5265 ?!? Unfortunately, marking all hard registers as live causes massive
5266 problems for the register allocator and marking all pseudos as live
5267 creates mountains of uninitialized variable warnings.
5269 So for now, just clear the memory set list and mark any regs
5270 we can find in ASM_OPERANDS as used. */
5271 if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
5272 free_EXPR_LIST_list (&pbi->mem_set_list);
5274 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
5275 We can not just fall through here since then we would be confused
5276 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
5277 traditional asms unlike their normal usage. */
5278 if (code == ASM_OPERANDS)
5282 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
5283 mark_used_regs (pbi, ASM_OPERANDS_INPUT (x, j), cond, insn);
5289 if (cond != NULL_RTX)
5292 mark_used_regs (pbi, COND_EXEC_TEST (x), NULL_RTX, insn);
5294 cond = COND_EXEC_TEST (x);
5295 x = COND_EXEC_CODE (x);
5299 /* We _do_not_ want to scan operands of phi nodes. Operands of
5300 a phi function are evaluated only when control reaches this
5301 block along a particular edge. Therefore, regs that appear
5302 as arguments to phi should not be added to the global live at
5310 /* Recursively scan the operands of this expression. */
5313 register const char *fmt = GET_RTX_FORMAT (code);
5316 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5320 /* Tail recursive case: save a function call level. */
5326 mark_used_regs (pbi, XEXP (x, i), cond, insn);
5328 else if (fmt[i] == 'E')
5331 for (j = 0; j < XVECLEN (x, i); j++)
5332 mark_used_regs (pbi, XVECEXP (x, i, j), cond, insn);
5341 try_pre_increment_1 (pbi, insn)
5342 struct propagate_block_info *pbi;
5345 /* Find the next use of this reg. If in same basic block,
5346 make it do pre-increment or pre-decrement if appropriate. */
5347 rtx x = single_set (insn);
5348 HOST_WIDE_INT amount = ((GET_CODE (SET_SRC (x)) == PLUS ? 1 : -1)
5349 * INTVAL (XEXP (SET_SRC (x), 1)));
5350 int regno = REGNO (SET_DEST (x));
5351 rtx y = pbi->reg_next_use[regno];
5353 && BLOCK_NUM (y) == BLOCK_NUM (insn)
5354 /* Don't do this if the reg dies, or gets set in y; a standard addressing
5355 mode would be better. */
5356 && ! dead_or_set_p (y, SET_DEST (x))
5357 && try_pre_increment (y, SET_DEST (x), amount))
5359 /* We have found a suitable auto-increment
5360 and already changed insn Y to do it.
5361 So flush this increment-instruction. */
5362 PUT_CODE (insn, NOTE);
5363 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
5364 NOTE_SOURCE_FILE (insn) = 0;
5365 /* Count a reference to this reg for the increment
5366 insn we are deleting. When a reg is incremented.
5367 spilling it is worse, so we want to make that
5369 if (regno >= FIRST_PSEUDO_REGISTER)
5371 REG_N_REFS (regno) += (optimize_size ? 1
5372 : pbi->bb->loop_depth + 1);
5373 REG_N_SETS (regno)++;
5380 /* Try to change INSN so that it does pre-increment or pre-decrement
5381 addressing on register REG in order to add AMOUNT to REG.
5382 AMOUNT is negative for pre-decrement.
5383 Returns 1 if the change could be made.
5384 This checks all about the validity of the result of modifying INSN. */
5387 try_pre_increment (insn, reg, amount)
5389 HOST_WIDE_INT amount;
5393 /* Nonzero if we can try to make a pre-increment or pre-decrement.
5394 For example, addl $4,r1; movl (r1),... can become movl +(r1),... */
5396 /* Nonzero if we can try to make a post-increment or post-decrement.
5397 For example, addl $4,r1; movl -4(r1),... can become movl (r1)+,...
5398 It is possible for both PRE_OK and POST_OK to be nonzero if the machine
5399 supports both pre-inc and post-inc, or both pre-dec and post-dec. */
5402 /* Nonzero if the opportunity actually requires post-inc or post-dec. */
5405 /* From the sign of increment, see which possibilities are conceivable
5406 on this target machine. */
5407 if (HAVE_PRE_INCREMENT && amount > 0)
5409 if (HAVE_POST_INCREMENT && amount > 0)
5412 if (HAVE_PRE_DECREMENT && amount < 0)
5414 if (HAVE_POST_DECREMENT && amount < 0)
5417 if (! (pre_ok || post_ok))
5420 /* It is not safe to add a side effect to a jump insn
5421 because if the incremented register is spilled and must be reloaded
5422 there would be no way to store the incremented value back in memory. */
5424 if (GET_CODE (insn) == JUMP_INSN)
5429 use = find_use_as_address (PATTERN (insn), reg, 0);
5430 if (post_ok && (use == 0 || use == (rtx) 1))
5432 use = find_use_as_address (PATTERN (insn), reg, -amount);
5436 if (use == 0 || use == (rtx) 1)
5439 if (GET_MODE_SIZE (GET_MODE (use)) != (amount > 0 ? amount : - amount))
5442 /* See if this combination of instruction and addressing mode exists. */
5443 if (! validate_change (insn, &XEXP (use, 0),
5444 gen_rtx_fmt_e (amount > 0
5445 ? (do_post ? POST_INC : PRE_INC)
5446 : (do_post ? POST_DEC : PRE_DEC),
5450 /* Record that this insn now has an implicit side effect on X. */
5451 REG_NOTES (insn) = alloc_EXPR_LIST (REG_INC, reg, REG_NOTES (insn));
5455 #endif /* AUTO_INC_DEC */
5457 /* Find the place in the rtx X where REG is used as a memory address.
5458 Return the MEM rtx that so uses it.
5459 If PLUSCONST is nonzero, search instead for a memory address equivalent to
5460 (plus REG (const_int PLUSCONST)).
5462 If such an address does not appear, return 0.
5463 If REG appears more than once, or is used other than in such an address,
5467 find_use_as_address (x, reg, plusconst)
5470 HOST_WIDE_INT plusconst;
5472 enum rtx_code code = GET_CODE (x);
5473 const char *fmt = GET_RTX_FORMAT (code);
5475 register rtx value = 0;
5478 if (code == MEM && XEXP (x, 0) == reg && plusconst == 0)
5481 if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
5482 && XEXP (XEXP (x, 0), 0) == reg
5483 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5484 && INTVAL (XEXP (XEXP (x, 0), 1)) == plusconst)
5487 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
5489 /* If REG occurs inside a MEM used in a bit-field reference,
5490 that is unacceptable. */
5491 if (find_use_as_address (XEXP (x, 0), reg, 0) != 0)
5492 return (rtx) (HOST_WIDE_INT) 1;
5496 return (rtx) (HOST_WIDE_INT) 1;
5498 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5502 tem = find_use_as_address (XEXP (x, i), reg, plusconst);
5506 return (rtx) (HOST_WIDE_INT) 1;
5508 else if (fmt[i] == 'E')
5511 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5513 tem = find_use_as_address (XVECEXP (x, i, j), reg, plusconst);
5517 return (rtx) (HOST_WIDE_INT) 1;
5525 /* Write information about registers and basic blocks into FILE.
5526 This is part of making a debugging dump. */
5529 dump_regset (r, outf)
5536 fputs (" (nil)", outf);
5540 EXECUTE_IF_SET_IN_REG_SET (r, 0, i,
5542 fprintf (outf, " %d", i);
5543 if (i < FIRST_PSEUDO_REGISTER)
5544 fprintf (outf, " [%s]",
5553 dump_regset (r, stderr);
5554 putc ('\n', stderr);
5558 dump_flow_info (file)
5562 static const char * const reg_class_names[] = REG_CLASS_NAMES;
5564 fprintf (file, "%d registers.\n", max_regno);
5565 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
5568 enum reg_class class, altclass;
5569 fprintf (file, "\nRegister %d used %d times across %d insns",
5570 i, REG_N_REFS (i), REG_LIVE_LENGTH (i));
5571 if (REG_BASIC_BLOCK (i) >= 0)
5572 fprintf (file, " in block %d", REG_BASIC_BLOCK (i));
5574 fprintf (file, "; set %d time%s", REG_N_SETS (i),
5575 (REG_N_SETS (i) == 1) ? "" : "s");
5576 if (REG_USERVAR_P (regno_reg_rtx[i]))
5577 fprintf (file, "; user var");
5578 if (REG_N_DEATHS (i) != 1)
5579 fprintf (file, "; dies in %d places", REG_N_DEATHS (i));
5580 if (REG_N_CALLS_CROSSED (i) == 1)
5581 fprintf (file, "; crosses 1 call");
5582 else if (REG_N_CALLS_CROSSED (i))
5583 fprintf (file, "; crosses %d calls", REG_N_CALLS_CROSSED (i));
5584 if (PSEUDO_REGNO_BYTES (i) != UNITS_PER_WORD)
5585 fprintf (file, "; %d bytes", PSEUDO_REGNO_BYTES (i));
5586 class = reg_preferred_class (i);
5587 altclass = reg_alternate_class (i);
5588 if (class != GENERAL_REGS || altclass != ALL_REGS)
5590 if (altclass == ALL_REGS || class == ALL_REGS)
5591 fprintf (file, "; pref %s", reg_class_names[(int) class]);
5592 else if (altclass == NO_REGS)
5593 fprintf (file, "; %s or none", reg_class_names[(int) class]);
5595 fprintf (file, "; pref %s, else %s",
5596 reg_class_names[(int) class],
5597 reg_class_names[(int) altclass]);
5599 if (REGNO_POINTER_FLAG (i))
5600 fprintf (file, "; pointer");
5601 fprintf (file, ".\n");
5604 fprintf (file, "\n%d basic blocks, %d edges.\n", n_basic_blocks, n_edges);
5605 for (i = 0; i < n_basic_blocks; i++)
5607 register basic_block bb = BASIC_BLOCK (i);
5610 fprintf (file, "\nBasic block %d: first insn %d, last %d, loop_depth %d.\n",
5611 i, INSN_UID (bb->head), INSN_UID (bb->end), bb->loop_depth);
5613 fprintf (file, "Predecessors: ");
5614 for (e = bb->pred; e ; e = e->pred_next)
5615 dump_edge_info (file, e, 0);
5617 fprintf (file, "\nSuccessors: ");
5618 for (e = bb->succ; e ; e = e->succ_next)
5619 dump_edge_info (file, e, 1);
5621 fprintf (file, "\nRegisters live at start:");
5622 dump_regset (bb->global_live_at_start, file);
5624 fprintf (file, "\nRegisters live at end:");
5625 dump_regset (bb->global_live_at_end, file);
5636 dump_flow_info (stderr);
5640 dump_edge_info (file, e, do_succ)
5645 basic_block side = (do_succ ? e->dest : e->src);
5647 if (side == ENTRY_BLOCK_PTR)
5648 fputs (" ENTRY", file);
5649 else if (side == EXIT_BLOCK_PTR)
5650 fputs (" EXIT", file);
5652 fprintf (file, " %d", side->index);
5656 static const char * const bitnames[] = {
5657 "fallthru", "crit", "ab", "abcall", "eh", "fake"
5660 int i, flags = e->flags;
5664 for (i = 0; flags; i++)
5665 if (flags & (1 << i))
5671 if (i < (int)(sizeof (bitnames) / sizeof (*bitnames)))
5672 fputs (bitnames[i], file);
5674 fprintf (file, "%d", i);
5682 /* Print out one basic block with live information at start and end. */
5692 fprintf (outf, ";; Basic block %d, loop depth %d",
5693 bb->index, bb->loop_depth);
5694 if (bb->eh_beg != -1 || bb->eh_end != -1)
5695 fprintf (outf, ", eh regions %d/%d", bb->eh_beg, bb->eh_end);
5698 fputs (";; Predecessors: ", outf);
5699 for (e = bb->pred; e ; e = e->pred_next)
5700 dump_edge_info (outf, e, 0);
5703 fputs (";; Registers live at start:", outf);
5704 dump_regset (bb->global_live_at_start, outf);
5707 for (insn = bb->head, last = NEXT_INSN (bb->end);
5709 insn = NEXT_INSN (insn))
5710 print_rtl_single (outf, insn);
5712 fputs (";; Registers live at end:", outf);
5713 dump_regset (bb->global_live_at_end, outf);
5716 fputs (";; Successors: ", outf);
5717 for (e = bb->succ; e; e = e->succ_next)
5718 dump_edge_info (outf, e, 1);
5726 dump_bb (bb, stderr);
5733 dump_bb (BASIC_BLOCK(n), stderr);
5736 /* Like print_rtl, but also print out live information for the start of each
5740 print_rtl_with_bb (outf, rtx_first)
5744 register rtx tmp_rtx;
5747 fprintf (outf, "(nil)\n");
5751 enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
5752 int max_uid = get_max_uid ();
5753 basic_block *start = (basic_block *)
5754 xcalloc (max_uid, sizeof (basic_block));
5755 basic_block *end = (basic_block *)
5756 xcalloc (max_uid, sizeof (basic_block));
5757 enum bb_state *in_bb_p = (enum bb_state *)
5758 xcalloc (max_uid, sizeof (enum bb_state));
5760 for (i = n_basic_blocks - 1; i >= 0; i--)
5762 basic_block bb = BASIC_BLOCK (i);
5765 start[INSN_UID (bb->head)] = bb;
5766 end[INSN_UID (bb->end)] = bb;
5767 for (x = bb->head; x != NULL_RTX; x = NEXT_INSN (x))
5769 enum bb_state state = IN_MULTIPLE_BB;
5770 if (in_bb_p[INSN_UID(x)] == NOT_IN_BB)
5772 in_bb_p[INSN_UID(x)] = state;
5779 for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx))
5784 if ((bb = start[INSN_UID (tmp_rtx)]) != NULL)
5786 fprintf (outf, ";; Start of basic block %d, registers live:",
5788 dump_regset (bb->global_live_at_start, outf);
5792 if (in_bb_p[INSN_UID(tmp_rtx)] == NOT_IN_BB
5793 && GET_CODE (tmp_rtx) != NOTE
5794 && GET_CODE (tmp_rtx) != BARRIER)
5795 fprintf (outf, ";; Insn is not within a basic block\n");
5796 else if (in_bb_p[INSN_UID(tmp_rtx)] == IN_MULTIPLE_BB)
5797 fprintf (outf, ";; Insn is in multiple basic blocks\n");
5799 did_output = print_rtl_single (outf, tmp_rtx);
5801 if ((bb = end[INSN_UID (tmp_rtx)]) != NULL)
5803 fprintf (outf, ";; End of basic block %d, registers live:\n",
5805 dump_regset (bb->global_live_at_end, outf);
5818 if (current_function_epilogue_delay_list != 0)
5820 fprintf (outf, "\n;; Insns in epilogue delay list:\n\n");
5821 for (tmp_rtx = current_function_epilogue_delay_list; tmp_rtx != 0;
5822 tmp_rtx = XEXP (tmp_rtx, 1))
5823 print_rtl_single (outf, XEXP (tmp_rtx, 0));
5827 /* Compute dominator relationships using new flow graph structures. */
5829 compute_flow_dominators (dominators, post_dominators)
5830 sbitmap *dominators;
5831 sbitmap *post_dominators;
5834 sbitmap *temp_bitmap;
5836 basic_block *worklist, *workend, *qin, *qout;
5839 /* Allocate a worklist array/queue. Entries are only added to the
5840 list if they were not already on the list. So the size is
5841 bounded by the number of basic blocks. */
5842 worklist = (basic_block *) xmalloc (sizeof (basic_block) * n_basic_blocks);
5843 workend = &worklist[n_basic_blocks];
5845 temp_bitmap = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
5846 sbitmap_vector_zero (temp_bitmap, n_basic_blocks);
5850 /* The optimistic setting of dominators requires us to put every
5851 block on the work list initially. */
5852 qin = qout = worklist;
5853 for (bb = 0; bb < n_basic_blocks; bb++)
5855 *qin++ = BASIC_BLOCK (bb);
5856 BASIC_BLOCK (bb)->aux = BASIC_BLOCK (bb);
5858 qlen = n_basic_blocks;
5861 /* We want a maximal solution, so initially assume everything dominates
5863 sbitmap_vector_ones (dominators, n_basic_blocks);
5865 /* Mark successors of the entry block so we can identify them below. */
5866 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
5867 e->dest->aux = ENTRY_BLOCK_PTR;
5869 /* Iterate until the worklist is empty. */
5872 /* Take the first entry off the worklist. */
5873 basic_block b = *qout++;
5874 if (qout >= workend)
5880 /* Compute the intersection of the dominators of all the
5883 If one of the predecessor blocks is the ENTRY block, then the
5884 intersection of the dominators of the predecessor blocks is
5885 defined as the null set. We can identify such blocks by the
5886 special value in the AUX field in the block structure. */
5887 if (b->aux == ENTRY_BLOCK_PTR)
5889 /* Do not clear the aux field for blocks which are
5890 successors of the ENTRY block. That way we we never
5891 add them to the worklist again.
5893 The intersect of dominators of the preds of this block is
5894 defined as the null set. */
5895 sbitmap_zero (temp_bitmap[bb]);
5899 /* Clear the aux field of this block so it can be added to
5900 the worklist again if necessary. */
5902 sbitmap_intersection_of_preds (temp_bitmap[bb], dominators, bb);
5905 /* Make sure each block always dominates itself. */
5906 SET_BIT (temp_bitmap[bb], bb);
5908 /* If the out state of this block changed, then we need to
5909 add the successors of this block to the worklist if they
5910 are not already on the worklist. */
5911 if (sbitmap_a_and_b (dominators[bb], dominators[bb], temp_bitmap[bb]))
5913 for (e = b->succ; e; e = e->succ_next)
5915 if (!e->dest->aux && e->dest != EXIT_BLOCK_PTR)
5929 if (post_dominators)
5931 /* The optimistic setting of dominators requires us to put every
5932 block on the work list initially. */
5933 qin = qout = worklist;
5934 for (bb = 0; bb < n_basic_blocks; bb++)
5936 *qin++ = BASIC_BLOCK (bb);
5937 BASIC_BLOCK (bb)->aux = BASIC_BLOCK (bb);
5939 qlen = n_basic_blocks;
5942 /* We want a maximal solution, so initially assume everything post
5943 dominates everything else. */
5944 sbitmap_vector_ones (post_dominators, n_basic_blocks);
5946 /* Mark predecessors of the exit block so we can identify them below. */
5947 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
5948 e->src->aux = EXIT_BLOCK_PTR;
5950 /* Iterate until the worklist is empty. */
5953 /* Take the first entry off the worklist. */
5954 basic_block b = *qout++;
5955 if (qout >= workend)
5961 /* Compute the intersection of the post dominators of all the
5964 If one of the successor blocks is the EXIT block, then the
5965 intersection of the dominators of the successor blocks is
5966 defined as the null set. We can identify such blocks by the
5967 special value in the AUX field in the block structure. */
5968 if (b->aux == EXIT_BLOCK_PTR)
5970 /* Do not clear the aux field for blocks which are
5971 predecessors of the EXIT block. That way we we never
5972 add them to the worklist again.
5974 The intersect of dominators of the succs of this block is
5975 defined as the null set. */
5976 sbitmap_zero (temp_bitmap[bb]);
5980 /* Clear the aux field of this block so it can be added to
5981 the worklist again if necessary. */
5983 sbitmap_intersection_of_succs (temp_bitmap[bb],
5984 post_dominators, bb);
5987 /* Make sure each block always post dominates itself. */
5988 SET_BIT (temp_bitmap[bb], bb);
5990 /* If the out state of this block changed, then we need to
5991 add the successors of this block to the worklist if they
5992 are not already on the worklist. */
5993 if (sbitmap_a_and_b (post_dominators[bb],
5994 post_dominators[bb],
5997 for (e = b->pred; e; e = e->pred_next)
5999 if (!e->src->aux && e->src != ENTRY_BLOCK_PTR)
6017 /* Given DOMINATORS, compute the immediate dominators into IDOM. */
6020 compute_immediate_dominators (idom, dominators)
6022 sbitmap *dominators;
6027 tmp = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
6029 /* Begin with tmp(n) = dom(n) - { n }. */
6030 for (b = n_basic_blocks; --b >= 0; )
6032 sbitmap_copy (tmp[b], dominators[b]);
6033 RESET_BIT (tmp[b], b);
6036 /* Subtract out all of our dominator's dominators. */
6037 for (b = n_basic_blocks; --b >= 0; )
6039 sbitmap tmp_b = tmp[b];
6042 for (s = n_basic_blocks; --s >= 0; )
6043 if (TEST_BIT (tmp_b, s))
6044 sbitmap_difference (tmp_b, tmp_b, tmp[s]);
6047 /* Find the one bit set in the bitmap and put it in the output array. */
6048 for (b = n_basic_blocks; --b >= 0; )
6051 EXECUTE_IF_SET_IN_SBITMAP (tmp[b], 0, t, { idom[b] = t; });
6054 sbitmap_vector_free (tmp);
6057 /* Recompute register set/reference counts immediately prior to register
6060 This avoids problems with set/reference counts changing to/from values
6061 which have special meanings to the register allocators.
6063 Additionally, the reference counts are the primary component used by the
6064 register allocators to prioritize pseudos for allocation to hard regs.
6065 More accurate reference counts generally lead to better register allocation.
6067 F is the first insn to be scanned.
6069 LOOP_STEP denotes how much loop_depth should be incremented per
6070 loop nesting level in order to increase the ref count more for
6071 references in a loop.
6073 It might be worthwhile to update REG_LIVE_LENGTH, REG_BASIC_BLOCK and
6074 possibly other information which is used by the register allocators. */
6077 recompute_reg_usage (f, loop_step)
6078 rtx f ATTRIBUTE_UNUSED;
6079 int loop_step ATTRIBUTE_UNUSED;
6081 allocate_reg_life_data ();
6082 update_life_info (NULL, UPDATE_LIFE_LOCAL, PROP_REG_INFO);
6085 /* Optionally removes all the REG_DEAD and REG_UNUSED notes from a set of
6086 blocks. If BLOCKS is NULL, assume the universal set. Returns a count
6087 of the number of registers that died. */
6090 count_or_remove_death_notes (blocks, kill)
6096 for (i = n_basic_blocks - 1; i >= 0; --i)
6101 if (blocks && ! TEST_BIT (blocks, i))
6104 bb = BASIC_BLOCK (i);
6106 for (insn = bb->head; ; insn = NEXT_INSN (insn))
6108 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
6110 rtx *pprev = ®_NOTES (insn);
6115 switch (REG_NOTE_KIND (link))
6118 if (GET_CODE (XEXP (link, 0)) == REG)
6120 rtx reg = XEXP (link, 0);
6123 if (REGNO (reg) >= FIRST_PSEUDO_REGISTER)
6126 n = HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg));
6134 rtx next = XEXP (link, 1);
6135 free_EXPR_LIST_node (link);
6136 *pprev = link = next;
6142 pprev = &XEXP (link, 1);
6149 if (insn == bb->end)
6157 /* Record INSN's block as BB. */
6160 set_block_for_insn (insn, bb)
6164 size_t uid = INSN_UID (insn);
6165 if (uid >= basic_block_for_insn->num_elements)
6169 /* Add one-eighth the size so we don't keep calling xrealloc. */
6170 new_size = uid + (uid + 7) / 8;
6172 VARRAY_GROW (basic_block_for_insn, new_size);
6174 VARRAY_BB (basic_block_for_insn, uid) = bb;
6177 /* Record INSN's block number as BB. */
6178 /* ??? This has got to go. */
6181 set_block_num (insn, bb)
6185 set_block_for_insn (insn, BASIC_BLOCK (bb));
6188 /* Verify the CFG consistency. This function check some CFG invariants and
6189 aborts when something is wrong. Hope that this function will help to
6190 convert many optimization passes to preserve CFG consistent.
6192 Currently it does following checks:
6194 - test head/end pointers
6195 - overlapping of basic blocks
6196 - edge list corectness
6197 - headers of basic blocks (the NOTE_INSN_BASIC_BLOCK note)
6198 - tails of basic blocks (ensure that boundary is necesary)
6199 - scans body of the basic block for JUMP_INSN, CODE_LABEL
6200 and NOTE_INSN_BASIC_BLOCK
6201 - check that all insns are in the basic blocks
6202 (except the switch handling code, barriers and notes)
6203 - check that all returns are followed by barriers
6205 In future it can be extended check a lot of other stuff as well
6206 (reachability of basic blocks, life information, etc. etc.). */
6211 const int max_uid = get_max_uid ();
6212 const rtx rtx_first = get_insns ();
6213 basic_block *bb_info;
6215 int i, last_bb_num_seen, num_bb_notes, err = 0;
6217 bb_info = (basic_block *) xcalloc (max_uid, sizeof (basic_block));
6219 /* First pass check head/end pointers and set bb_info array used by
6221 for (i = n_basic_blocks - 1; i >= 0; i--)
6223 basic_block bb = BASIC_BLOCK (i);
6225 /* Check the head pointer and make sure that it is pointing into
6227 for (x = rtx_first; x != NULL_RTX; x = NEXT_INSN (x))
6232 error ("Head insn %d for block %d not found in the insn stream.",
6233 INSN_UID (bb->head), bb->index);
6237 /* Check the end pointer and make sure that it is pointing into
6239 for (x = bb->head; x != NULL_RTX; x = NEXT_INSN (x))
6241 if (bb_info[INSN_UID (x)] != NULL)
6243 error ("Insn %d is in multiple basic blocks (%d and %d)",
6244 INSN_UID (x), bb->index, bb_info[INSN_UID (x)]->index);
6247 bb_info[INSN_UID (x)] = bb;
6254 error ("End insn %d for block %d not found in the insn stream.",
6255 INSN_UID (bb->end), bb->index);
6260 /* Now check the basic blocks (boundaries etc.) */
6261 for (i = n_basic_blocks - 1; i >= 0; i--)
6263 basic_block bb = BASIC_BLOCK (i);
6264 /* Check corectness of edge lists */
6272 fprintf (stderr, "verify_flow_info: Basic block %d succ edge is corrupted\n",
6274 fprintf (stderr, "Predecessor: ");
6275 dump_edge_info (stderr, e, 0);
6276 fprintf (stderr, "\nSuccessor: ");
6277 dump_edge_info (stderr, e, 1);
6281 if (e->dest != EXIT_BLOCK_PTR)
6283 edge e2 = e->dest->pred;
6284 while (e2 && e2 != e)
6288 error ("Basic block %i edge lists are corrupted", bb->index);
6300 error ("Basic block %d pred edge is corrupted", bb->index);
6301 fputs ("Predecessor: ", stderr);
6302 dump_edge_info (stderr, e, 0);
6303 fputs ("\nSuccessor: ", stderr);
6304 dump_edge_info (stderr, e, 1);
6305 fputc ('\n', stderr);
6308 if (e->src != ENTRY_BLOCK_PTR)
6310 edge e2 = e->src->succ;
6311 while (e2 && e2 != e)
6315 error ("Basic block %i edge lists are corrupted", bb->index);
6322 /* OK pointers are correct. Now check the header of basic
6323 block. It ought to contain optional CODE_LABEL followed
6324 by NOTE_BASIC_BLOCK. */
6326 if (GET_CODE (x) == CODE_LABEL)
6330 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
6336 if (GET_CODE (x) != NOTE
6337 || NOTE_LINE_NUMBER (x) != NOTE_INSN_BASIC_BLOCK
6338 || NOTE_BASIC_BLOCK (x) != bb)
6340 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d\n",
6347 /* Do checks for empty blocks here */
6354 if (GET_CODE (x) == NOTE
6355 && NOTE_LINE_NUMBER (x) == NOTE_INSN_BASIC_BLOCK)
6357 error ("NOTE_INSN_BASIC_BLOCK %d in the middle of basic block %d",
6358 INSN_UID (x), bb->index);
6365 if (GET_CODE (x) == JUMP_INSN
6366 || GET_CODE (x) == CODE_LABEL
6367 || GET_CODE (x) == BARRIER)
6369 error ("In basic block %d:", bb->index);
6370 fatal_insn ("Flow control insn inside a basic block", x);
6378 last_bb_num_seen = -1;
6383 if (GET_CODE (x) == NOTE
6384 && NOTE_LINE_NUMBER (x) == NOTE_INSN_BASIC_BLOCK)
6386 basic_block bb = NOTE_BASIC_BLOCK (x);
6388 if (bb->index != last_bb_num_seen + 1)
6389 fatal ("Basic blocks not numbered consecutively");
6390 last_bb_num_seen = bb->index;
6393 if (!bb_info[INSN_UID (x)])
6395 switch (GET_CODE (x))
6402 /* An addr_vec is placed outside any block block. */
6404 && GET_CODE (NEXT_INSN (x)) == JUMP_INSN
6405 && (GET_CODE (PATTERN (NEXT_INSN (x))) == ADDR_DIFF_VEC
6406 || GET_CODE (PATTERN (NEXT_INSN (x))) == ADDR_VEC))
6411 /* But in any case, non-deletable labels can appear anywhere. */
6415 fatal_insn ("Insn outside basic block", x);
6419 if (GET_RTX_CLASS (GET_CODE (x)) == 'i'
6420 && GET_CODE (x) == JUMP_INSN
6421 && returnjump_p (x) && ! condjump_p (x)
6422 && ! (NEXT_INSN (x) && GET_CODE (NEXT_INSN (x)) == BARRIER))
6423 fatal_insn ("Return not followed by barrier", x);
6428 if (num_bb_notes != n_basic_blocks)
6429 fatal ("number of bb notes in insn chain (%d) != n_basic_blocks (%d)",
6430 num_bb_notes, n_basic_blocks);
6439 /* Functions to access an edge list with a vector representation.
6440 Enough data is kept such that given an index number, the
6441 pred and succ that edge reprsents can be determined, or
6442 given a pred and a succ, it's index number can be returned.
6443 This allows algorithms which comsume a lot of memory to
6444 represent the normally full matrix of edge (pred,succ) with a
6445 single indexed vector, edge (EDGE_INDEX (pred, succ)), with no
6446 wasted space in the client code due to sparse flow graphs. */
6448 /* This functions initializes the edge list. Basically the entire
6449 flowgraph is processed, and all edges are assigned a number,
6450 and the data structure is filed in. */
6454 struct edge_list *elist;
6460 block_count = n_basic_blocks + 2; /* Include the entry and exit blocks. */
6464 /* Determine the number of edges in the flow graph by counting successor
6465 edges on each basic block. */
6466 for (x = 0; x < n_basic_blocks; x++)
6468 basic_block bb = BASIC_BLOCK (x);
6470 for (e = bb->succ; e; e = e->succ_next)
6473 /* Don't forget successors of the entry block. */
6474 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
6477 elist = (struct edge_list *) xmalloc (sizeof (struct edge_list));
6478 elist->num_blocks = block_count;
6479 elist->num_edges = num_edges;
6480 elist->index_to_edge = (edge *) xmalloc (sizeof (edge) * num_edges);
6484 /* Follow successors of the entry block, and register these edges. */
6485 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
6487 elist->index_to_edge[num_edges] = e;
6491 for (x = 0; x < n_basic_blocks; x++)
6493 basic_block bb = BASIC_BLOCK (x);
6495 /* Follow all successors of blocks, and register these edges. */
6496 for (e = bb->succ; e; e = e->succ_next)
6498 elist->index_to_edge[num_edges] = e;
6505 /* This function free's memory associated with an edge list. */
6507 free_edge_list (elist)
6508 struct edge_list *elist;
6512 free (elist->index_to_edge);
6517 /* This function provides debug output showing an edge list. */
6519 print_edge_list (f, elist)
6521 struct edge_list *elist;
6524 fprintf(f, "Compressed edge list, %d BBs + entry & exit, and %d edges\n",
6525 elist->num_blocks - 2, elist->num_edges);
6527 for (x = 0; x < elist->num_edges; x++)
6529 fprintf (f, " %-4d - edge(", x);
6530 if (INDEX_EDGE_PRED_BB (elist, x) == ENTRY_BLOCK_PTR)
6531 fprintf (f,"entry,");
6533 fprintf (f,"%d,", INDEX_EDGE_PRED_BB (elist, x)->index);
6535 if (INDEX_EDGE_SUCC_BB (elist, x) == EXIT_BLOCK_PTR)
6536 fprintf (f,"exit)\n");
6538 fprintf (f,"%d)\n", INDEX_EDGE_SUCC_BB (elist, x)->index);
6542 /* This function provides an internal consistancy check of an edge list,
6543 verifying that all edges are present, and that there are no
6546 verify_edge_list (f, elist)
6548 struct edge_list *elist;
6550 int x, pred, succ, index;
6553 for (x = 0; x < n_basic_blocks; x++)
6555 basic_block bb = BASIC_BLOCK (x);
6557 for (e = bb->succ; e; e = e->succ_next)
6559 pred = e->src->index;
6560 succ = e->dest->index;
6561 index = EDGE_INDEX (elist, e->src, e->dest);
6562 if (index == EDGE_INDEX_NO_EDGE)
6564 fprintf (f, "*p* No index for edge from %d to %d\n",pred, succ);
6567 if (INDEX_EDGE_PRED_BB (elist, index)->index != pred)
6568 fprintf (f, "*p* Pred for index %d should be %d not %d\n",
6569 index, pred, INDEX_EDGE_PRED_BB (elist, index)->index);
6570 if (INDEX_EDGE_SUCC_BB (elist, index)->index != succ)
6571 fprintf (f, "*p* Succ for index %d should be %d not %d\n",
6572 index, succ, INDEX_EDGE_SUCC_BB (elist, index)->index);
6575 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
6577 pred = e->src->index;
6578 succ = e->dest->index;
6579 index = EDGE_INDEX (elist, e->src, e->dest);
6580 if (index == EDGE_INDEX_NO_EDGE)
6582 fprintf (f, "*p* No index for edge from %d to %d\n",pred, succ);
6585 if (INDEX_EDGE_PRED_BB (elist, index)->index != pred)
6586 fprintf (f, "*p* Pred for index %d should be %d not %d\n",
6587 index, pred, INDEX_EDGE_PRED_BB (elist, index)->index);
6588 if (INDEX_EDGE_SUCC_BB (elist, index)->index != succ)
6589 fprintf (f, "*p* Succ for index %d should be %d not %d\n",
6590 index, succ, INDEX_EDGE_SUCC_BB (elist, index)->index);
6592 /* We've verified that all the edges are in the list, no lets make sure
6593 there are no spurious edges in the list. */
6595 for (pred = 0 ; pred < n_basic_blocks; pred++)
6596 for (succ = 0 ; succ < n_basic_blocks; succ++)
6598 basic_block p = BASIC_BLOCK (pred);
6599 basic_block s = BASIC_BLOCK (succ);
6603 for (e = p->succ; e; e = e->succ_next)
6609 for (e = s->pred; e; e = e->pred_next)
6615 if (EDGE_INDEX (elist, BASIC_BLOCK (pred), BASIC_BLOCK (succ))
6616 == EDGE_INDEX_NO_EDGE && found_edge != 0)
6617 fprintf (f, "*** Edge (%d, %d) appears to not have an index\n",
6619 if (EDGE_INDEX (elist, BASIC_BLOCK (pred), BASIC_BLOCK (succ))
6620 != EDGE_INDEX_NO_EDGE && found_edge == 0)
6621 fprintf (f, "*** Edge (%d, %d) has index %d, but there is no edge\n",
6622 pred, succ, EDGE_INDEX (elist, BASIC_BLOCK (pred),
6623 BASIC_BLOCK (succ)));
6625 for (succ = 0 ; succ < n_basic_blocks; succ++)
6627 basic_block p = ENTRY_BLOCK_PTR;
6628 basic_block s = BASIC_BLOCK (succ);
6632 for (e = p->succ; e; e = e->succ_next)
6638 for (e = s->pred; e; e = e->pred_next)
6644 if (EDGE_INDEX (elist, ENTRY_BLOCK_PTR, BASIC_BLOCK (succ))
6645 == EDGE_INDEX_NO_EDGE && found_edge != 0)
6646 fprintf (f, "*** Edge (entry, %d) appears to not have an index\n",
6648 if (EDGE_INDEX (elist, ENTRY_BLOCK_PTR, BASIC_BLOCK (succ))
6649 != EDGE_INDEX_NO_EDGE && found_edge == 0)
6650 fprintf (f, "*** Edge (entry, %d) has index %d, but no edge exists\n",
6651 succ, EDGE_INDEX (elist, ENTRY_BLOCK_PTR,
6652 BASIC_BLOCK (succ)));
6654 for (pred = 0 ; pred < n_basic_blocks; pred++)
6656 basic_block p = BASIC_BLOCK (pred);
6657 basic_block s = EXIT_BLOCK_PTR;
6661 for (e = p->succ; e; e = e->succ_next)
6667 for (e = s->pred; e; e = e->pred_next)
6673 if (EDGE_INDEX (elist, BASIC_BLOCK (pred), EXIT_BLOCK_PTR)
6674 == EDGE_INDEX_NO_EDGE && found_edge != 0)
6675 fprintf (f, "*** Edge (%d, exit) appears to not have an index\n",
6677 if (EDGE_INDEX (elist, BASIC_BLOCK (pred), EXIT_BLOCK_PTR)
6678 != EDGE_INDEX_NO_EDGE && found_edge == 0)
6679 fprintf (f, "*** Edge (%d, exit) has index %d, but no edge exists\n",
6680 pred, EDGE_INDEX (elist, BASIC_BLOCK (pred),
6685 /* This routine will determine what, if any, edge there is between
6686 a specified predecessor and successor. */
6689 find_edge_index (edge_list, pred, succ)
6690 struct edge_list *edge_list;
6691 basic_block pred, succ;
6694 for (x = 0; x < NUM_EDGES (edge_list); x++)
6696 if (INDEX_EDGE_PRED_BB (edge_list, x) == pred
6697 && INDEX_EDGE_SUCC_BB (edge_list, x) == succ)
6700 return (EDGE_INDEX_NO_EDGE);
6703 /* This function will remove an edge from the flow graph. */
6708 edge last_pred = NULL;
6709 edge last_succ = NULL;
6711 basic_block src, dest;
6714 for (tmp = src->succ; tmp && tmp != e; tmp = tmp->succ_next)
6720 last_succ->succ_next = e->succ_next;
6722 src->succ = e->succ_next;
6724 for (tmp = dest->pred; tmp && tmp != e; tmp = tmp->pred_next)
6730 last_pred->pred_next = e->pred_next;
6732 dest->pred = e->pred_next;
6738 /* This routine will remove any fake successor edges for a basic block.
6739 When the edge is removed, it is also removed from whatever predecessor
6742 remove_fake_successors (bb)
6746 for (e = bb->succ; e ; )
6750 if ((tmp->flags & EDGE_FAKE) == EDGE_FAKE)
6755 /* This routine will remove all fake edges from the flow graph. If
6756 we remove all fake successors, it will automatically remove all
6757 fake predecessors. */
6759 remove_fake_edges ()
6763 for (x = 0; x < n_basic_blocks; x++)
6764 remove_fake_successors (BASIC_BLOCK (x));
6766 /* We've handled all successors except the entry block's. */
6767 remove_fake_successors (ENTRY_BLOCK_PTR);
6770 /* This functions will add a fake edge between any block which has no
6771 successors, and the exit block. Some data flow equations require these
6774 add_noreturn_fake_exit_edges ()
6778 for (x = 0; x < n_basic_blocks; x++)
6779 if (BASIC_BLOCK (x)->succ == NULL)
6780 make_edge (NULL, BASIC_BLOCK (x), EXIT_BLOCK_PTR, EDGE_FAKE);
6783 /* Dump the list of basic blocks in the bitmap NODES. */
6785 flow_nodes_print (str, nodes, file)
6787 const sbitmap nodes;
6792 fprintf (file, "%s { ", str);
6793 EXECUTE_IF_SET_IN_SBITMAP (nodes, 0, node, {fprintf (file, "%d ", node);});
6794 fputs ("}\n", file);
6798 /* Dump the list of exiting edges in the array EDGES. */
6800 flow_exits_print (str, edges, num_edges, file)
6808 fprintf (file, "%s { ", str);
6809 for (i = 0; i < num_edges; i++)
6810 fprintf (file, "%d->%d ", edges[i]->src->index, edges[i]->dest->index);
6811 fputs ("}\n", file);
6815 /* Dump loop related CFG information. */
6817 flow_loops_cfg_dump (loops, file)
6818 const struct loops *loops;
6823 if (! loops->num || ! file || ! loops->cfg.dom)
6826 for (i = 0; i < n_basic_blocks; i++)
6830 fprintf (file, ";; %d succs { ", i);
6831 for (succ = BASIC_BLOCK (i)->succ; succ; succ = succ->succ_next)
6832 fprintf (file, "%d ", succ->dest->index);
6833 flow_nodes_print ("} dom", loops->cfg.dom[i], file);
6837 /* Dump the DFS node order. */
6838 if (loops->cfg.dfs_order)
6840 fputs (";; DFS order: ", file);
6841 for (i = 0; i < n_basic_blocks; i++)
6842 fprintf (file, "%d ", loops->cfg.dfs_order[i]);
6848 /* Return non-zero if the nodes of LOOP are a subset of OUTER. */
6850 flow_loop_nested_p (outer, loop)
6854 return sbitmap_a_subset_b_p (loop->nodes, outer->nodes);
6858 /* Dump the loop information specified by LOOPS to the stream FILE. */
6860 flow_loops_dump (loops, file, verbose)
6861 const struct loops *loops;
6868 num_loops = loops->num;
6869 if (! num_loops || ! file)
6872 fprintf (file, ";; %d loops found, %d levels\n",
6873 num_loops, loops->levels);
6875 for (i = 0; i < num_loops; i++)
6877 struct loop *loop = &loops->array[i];
6879 fprintf (file, ";; loop %d (%d to %d):\n;; header %d, latch %d, pre-header %d, depth %d, level %d, outer %ld\n",
6880 i, INSN_UID (loop->header->head), INSN_UID (loop->latch->end),
6881 loop->header->index, loop->latch->index,
6882 loop->pre_header ? loop->pre_header->index : -1,
6883 loop->depth, loop->level,
6884 (long) (loop->outer ? (loop->outer - loops->array) : -1));
6885 fprintf (file, ";; %d", loop->num_nodes);
6886 flow_nodes_print (" nodes", loop->nodes, file);
6887 fprintf (file, ";; %d", loop->num_exits);
6888 flow_exits_print (" exits", loop->exits, loop->num_exits, file);
6894 for (j = 0; j < i; j++)
6896 struct loop *oloop = &loops->array[j];
6898 if (loop->header == oloop->header)
6903 smaller = loop->num_nodes < oloop->num_nodes;
6905 /* If the union of LOOP and OLOOP is different than
6906 the larger of LOOP and OLOOP then LOOP and OLOOP
6907 must be disjoint. */
6908 disjoint = ! flow_loop_nested_p (smaller ? loop : oloop,
6909 smaller ? oloop : loop);
6910 fprintf (file, ";; loop header %d shared by loops %d, %d %s\n",
6911 loop->header->index, i, j,
6912 disjoint ? "disjoint" : "nested");
6919 /* Print diagnostics to compare our concept of a loop with
6920 what the loop notes say. */
6921 if (GET_CODE (PREV_INSN (loop->first->head)) != NOTE
6922 || NOTE_LINE_NUMBER (PREV_INSN (loop->first->head))
6923 != NOTE_INSN_LOOP_BEG)
6924 fprintf (file, ";; No NOTE_INSN_LOOP_BEG at %d\n",
6925 INSN_UID (PREV_INSN (loop->first->head)));
6926 if (GET_CODE (NEXT_INSN (loop->last->end)) != NOTE
6927 || NOTE_LINE_NUMBER (NEXT_INSN (loop->last->end))
6928 != NOTE_INSN_LOOP_END)
6929 fprintf (file, ";; No NOTE_INSN_LOOP_END at %d\n",
6930 INSN_UID (NEXT_INSN (loop->last->end)));
6935 flow_loops_cfg_dump (loops, file);
6939 /* Free all the memory allocated for LOOPS. */
6941 flow_loops_free (loops)
6942 struct loops *loops;
6951 /* Free the loop descriptors. */
6952 for (i = 0; i < loops->num; i++)
6954 struct loop *loop = &loops->array[i];
6957 sbitmap_free (loop->nodes);
6961 free (loops->array);
6962 loops->array = NULL;
6965 sbitmap_vector_free (loops->cfg.dom);
6966 if (loops->cfg.dfs_order)
6967 free (loops->cfg.dfs_order);
6969 sbitmap_free (loops->shared_headers);
6974 /* Find the exits from the loop using the bitmap of loop nodes NODES
6975 and store in EXITS array. Return the number of exits from the
6978 flow_loop_exits_find (nodes, exits)
6979 const sbitmap nodes;
6988 /* Check all nodes within the loop to see if there are any
6989 successors not in the loop. Note that a node may have multiple
6992 EXECUTE_IF_SET_IN_SBITMAP (nodes, 0, node, {
6993 for (e = BASIC_BLOCK (node)->succ; e; e = e->succ_next)
6995 basic_block dest = e->dest;
6997 if (dest == EXIT_BLOCK_PTR || ! TEST_BIT (nodes, dest->index))
7005 *exits = (edge *) xmalloc (num_exits * sizeof (edge *));
7007 /* Store all exiting edges into an array. */
7009 EXECUTE_IF_SET_IN_SBITMAP (nodes, 0, node, {
7010 for (e = BASIC_BLOCK (node)->succ; e; e = e->succ_next)
7012 basic_block dest = e->dest;
7014 if (dest == EXIT_BLOCK_PTR || ! TEST_BIT (nodes, dest->index))
7015 (*exits)[num_exits++] = e;
7023 /* Find the nodes contained within the loop with header HEADER and
7024 latch LATCH and store in NODES. Return the number of nodes within
7027 flow_loop_nodes_find (header, latch, nodes)
7036 stack = (basic_block *) xmalloc (n_basic_blocks * sizeof (basic_block));
7039 /* Start with only the loop header in the set of loop nodes. */
7040 sbitmap_zero (nodes);
7041 SET_BIT (nodes, header->index);
7043 header->loop_depth++;
7045 /* Push the loop latch on to the stack. */
7046 if (! TEST_BIT (nodes, latch->index))
7048 SET_BIT (nodes, latch->index);
7049 latch->loop_depth++;
7051 stack[sp++] = latch;
7060 for (e = node->pred; e; e = e->pred_next)
7062 basic_block ancestor = e->src;
7064 /* If each ancestor not marked as part of loop, add to set of
7065 loop nodes and push on to stack. */
7066 if (ancestor != ENTRY_BLOCK_PTR
7067 && ! TEST_BIT (nodes, ancestor->index))
7069 SET_BIT (nodes, ancestor->index);
7070 ancestor->loop_depth++;
7072 stack[sp++] = ancestor;
7081 /* Compute the depth first search order and store in the array
7082 DFS_ORDER, marking the nodes visited in VISITED. Returns the
7083 number of nodes visited. */
7085 flow_depth_first_order_compute (dfs_order)
7094 /* Allocate stack for back-tracking up CFG. */
7095 stack = (edge *) xmalloc (n_basic_blocks * sizeof (edge));
7098 /* Allocate bitmap to track nodes that have been visited. */
7099 visited = sbitmap_alloc (n_basic_blocks);
7101 /* None of the nodes in the CFG have been visited yet. */
7102 sbitmap_zero (visited);
7104 /* Start with the first successor edge from the entry block. */
7105 e = ENTRY_BLOCK_PTR->succ;
7108 basic_block src = e->src;
7109 basic_block dest = e->dest;
7111 /* Mark that we have visited this node. */
7112 if (src != ENTRY_BLOCK_PTR)
7113 SET_BIT (visited, src->index);
7115 /* If this node has not been visited before, push the current
7116 edge on to the stack and proceed with the first successor
7117 edge of this node. */
7118 if (dest != EXIT_BLOCK_PTR && ! TEST_BIT (visited, dest->index)
7126 if (dest != EXIT_BLOCK_PTR && ! TEST_BIT (visited, dest->index)
7129 /* DEST has no successors (for example, a non-returning
7130 function is called) so do not push the current edge
7131 but carry on with its next successor. */
7132 dfs_order[dest->index] = n_basic_blocks - ++dfsnum;
7133 SET_BIT (visited, dest->index);
7136 while (! e->succ_next && src != ENTRY_BLOCK_PTR)
7138 dfs_order[src->index] = n_basic_blocks - ++dfsnum;
7140 /* Pop edge off stack. */
7148 sbitmap_free (visited);
7150 /* The number of nodes visited should not be greater than
7152 if (dfsnum > n_basic_blocks)
7155 /* There are some nodes left in the CFG that are unreachable. */
7156 if (dfsnum < n_basic_blocks)
7162 /* Return the block for the pre-header of the loop with header
7163 HEADER where DOM specifies the dominator information. Return NULL if
7164 there is no pre-header. */
7166 flow_loop_pre_header_find (header, dom)
7170 basic_block pre_header;
7173 /* If block p is a predecessor of the header and is the only block
7174 that the header does not dominate, then it is the pre-header. */
7176 for (e = header->pred; e; e = e->pred_next)
7178 basic_block node = e->src;
7180 if (node != ENTRY_BLOCK_PTR
7181 && ! TEST_BIT (dom[node->index], header->index))
7183 if (pre_header == NULL)
7187 /* There are multiple edges into the header from outside
7188 the loop so there is no pre-header block. */
7198 /* Add LOOP to the loop hierarchy tree where PREVLOOP was the loop
7199 previously added. The insertion algorithm assumes that the loops
7200 are added in the order found by a depth first search of the CFG. */
7202 flow_loop_tree_node_add (prevloop, loop)
7203 struct loop *prevloop;
7207 if (flow_loop_nested_p (prevloop, loop))
7209 prevloop->inner = loop;
7210 loop->outer = prevloop;
7214 while (prevloop->outer)
7216 if (flow_loop_nested_p (prevloop->outer, loop))
7218 prevloop->next = loop;
7219 loop->outer = prevloop->outer;
7222 prevloop = prevloop->outer;
7225 prevloop->next = loop;
7230 /* Build the loop hierarchy tree for LOOPS. */
7232 flow_loops_tree_build (loops)
7233 struct loops *loops;
7238 num_loops = loops->num;
7242 /* Root the loop hierarchy tree with the first loop found.
7243 Since we used a depth first search this should be the
7245 loops->tree = &loops->array[0];
7246 loops->tree->outer = loops->tree->inner = loops->tree->next = NULL;
7248 /* Add the remaining loops to the tree. */
7249 for (i = 1; i < num_loops; i++)
7250 flow_loop_tree_node_add (&loops->array[i - 1], &loops->array[i]);
7254 /* Helper function to compute loop nesting depth and enclosed loop level
7255 for the natural loop specified by LOOP at the loop depth DEPTH.
7256 Returns the loop level. */
7258 flow_loop_level_compute (loop, depth)
7268 /* Traverse loop tree assigning depth and computing level as the
7269 maximum level of all the inner loops of this loop. The loop
7270 level is equivalent to the height of the loop in the loop tree
7271 and corresponds to the number of enclosed loop levels (including
7273 for (inner = loop->inner; inner; inner = inner->next)
7277 ilevel = flow_loop_level_compute (inner, depth + 1) + 1;
7282 loop->level = level;
7283 loop->depth = depth;
7288 /* Compute the loop nesting depth and enclosed loop level for the loop
7289 hierarchy tree specfied by LOOPS. Return the maximum enclosed loop
7293 flow_loops_level_compute (loops)
7294 struct loops *loops;
7300 /* Traverse all the outer level loops. */
7301 for (loop = loops->tree; loop; loop = loop->next)
7303 level = flow_loop_level_compute (loop, 1);
7311 /* Find all the natural loops in the function and save in LOOPS structure
7312 and recalculate loop_depth information in basic block structures.
7313 Return the number of natural loops found. */
7316 flow_loops_find (loops)
7317 struct loops *loops;
7328 loops->array = NULL;
7332 /* Taking care of this degenerate case makes the rest of
7333 this code simpler. */
7334 if (n_basic_blocks == 0)
7337 /* Compute the dominators. */
7338 dom = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
7339 compute_flow_dominators (dom, NULL);
7341 /* Count the number of loop edges (back edges). This should be the
7342 same as the number of natural loops. Also clear the loop_depth
7343 and as we work from inner->outer in a loop nest we call
7344 find_loop_nodes_find which will increment loop_depth for nodes
7345 within the current loop, which happens to enclose inner loops. */
7348 for (b = 0; b < n_basic_blocks; b++)
7350 BASIC_BLOCK (b)->loop_depth = 0;
7351 for (e = BASIC_BLOCK (b)->pred; e; e = e->pred_next)
7353 basic_block latch = e->src;
7355 /* Look for back edges where a predecessor is dominated
7356 by this block. A natural loop has a single entry
7357 node (header) that dominates all the nodes in the
7358 loop. It also has single back edge to the header
7359 from a latch node. Note that multiple natural loops
7360 may share the same header. */
7361 if (latch != ENTRY_BLOCK_PTR && TEST_BIT (dom[latch->index], b))
7368 /* Compute depth first search order of the CFG so that outer
7369 natural loops will be found before inner natural loops. */
7370 dfs_order = (int *) xmalloc (n_basic_blocks * sizeof (int));
7371 flow_depth_first_order_compute (dfs_order);
7373 /* Allocate loop structures. */
7375 = (struct loop *) xcalloc (num_loops, sizeof (struct loop));
7377 headers = sbitmap_alloc (n_basic_blocks);
7378 sbitmap_zero (headers);
7380 loops->shared_headers = sbitmap_alloc (n_basic_blocks);
7381 sbitmap_zero (loops->shared_headers);
7383 /* Find and record information about all the natural loops
7386 for (b = 0; b < n_basic_blocks; b++)
7390 /* Search the nodes of the CFG in DFS order that we can find
7391 outer loops first. */
7392 header = BASIC_BLOCK (dfs_order[b]);
7394 /* Look for all the possible latch blocks for this header. */
7395 for (e = header->pred; e; e = e->pred_next)
7397 basic_block latch = e->src;
7399 /* Look for back edges where a predecessor is dominated
7400 by this block. A natural loop has a single entry
7401 node (header) that dominates all the nodes in the
7402 loop. It also has single back edge to the header
7403 from a latch node. Note that multiple natural loops
7404 may share the same header. */
7405 if (latch != ENTRY_BLOCK_PTR
7406 && TEST_BIT (dom[latch->index], header->index))
7410 loop = loops->array + num_loops;
7412 loop->header = header;
7413 loop->latch = latch;
7415 /* Keep track of blocks that are loop headers so
7416 that we can tell which loops should be merged. */
7417 if (TEST_BIT (headers, header->index))
7418 SET_BIT (loops->shared_headers, header->index);
7419 SET_BIT (headers, header->index);
7421 /* Find nodes contained within the loop. */
7422 loop->nodes = sbitmap_alloc (n_basic_blocks);
7424 = flow_loop_nodes_find (header, latch, loop->nodes);
7426 /* Compute first and last blocks within the loop.
7427 These are often the same as the loop header and
7428 loop latch respectively, but this is not always
7431 = BASIC_BLOCK (sbitmap_first_set_bit (loop->nodes));
7433 = BASIC_BLOCK (sbitmap_last_set_bit (loop->nodes));
7435 /* Find edges which exit the loop. Note that a node
7436 may have several exit edges. */
7438 = flow_loop_exits_find (loop->nodes, &loop->exits);
7440 /* Look to see if the loop has a pre-header node. */
7442 = flow_loop_pre_header_find (header, dom);
7449 /* Natural loops with shared headers may either be disjoint or
7450 nested. Disjoint loops with shared headers cannot be inner
7451 loops and should be merged. For now just mark loops that share
7453 for (i = 0; i < num_loops; i++)
7454 if (TEST_BIT (loops->shared_headers, loops->array[i].header->index))
7455 loops->array[i].shared = 1;
7457 sbitmap_free (headers);
7460 loops->num = num_loops;
7462 /* Save CFG derived information to avoid recomputing it. */
7463 loops->cfg.dom = dom;
7464 loops->cfg.dfs_order = dfs_order;
7466 /* Build the loop hierarchy tree. */
7467 flow_loops_tree_build (loops);
7469 /* Assign the loop nesting depth and enclosed loop level for each
7471 loops->levels = flow_loops_level_compute (loops);
7477 /* Return non-zero if edge E enters header of LOOP from outside of LOOP. */
7479 flow_loop_outside_edge_p (loop, e)
7480 const struct loop *loop;
7483 if (e->dest != loop->header)
7485 return (e->src == ENTRY_BLOCK_PTR)
7486 || ! TEST_BIT (loop->nodes, e->src->index);
7490 /* Clear LOG_LINKS fields of insns in a chain. */
7492 clear_log_links (insns)
7496 for (i = insns; i; i = NEXT_INSN (i))
7497 if (GET_RTX_CLASS (GET_CODE (i)) == 'i')