* genattrtab.c (simplify_cond): Make TESTS an array of rtxs, instead
[official-gcc.git] / gcc / flow.c
blobe23be42c89f541ec96332f1ddf862e800ff8b21e
1 /* Data flow analysis for GNU compiler.
2 Copyright (C) 1987, 88, 92-98, 1999 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* This file contains the data flow analysis pass of the compiler. It
23 computes data flow information which tells combine_instructions
24 which insns to consider combining and controls register allocation.
26 Additional data flow information that is too bulky to record is
27 generated during the analysis, and is used at that time to create
28 autoincrement and autodecrement addressing.
30 The first step is dividing the function into basic blocks.
31 find_basic_blocks does this. Then life_analysis determines
32 where each register is live and where it is dead.
34 ** find_basic_blocks **
36 find_basic_blocks divides the current function's rtl into basic
37 blocks and constructs the CFG. The blocks are recorded in the
38 basic_block_info array; the CFG exists in the edge structures
39 referenced by the blocks.
41 find_basic_blocks also finds any unreachable loops and deletes them.
43 ** life_analysis **
45 life_analysis is called immediately after find_basic_blocks.
46 It uses the basic block information to determine where each
47 hard or pseudo register is live.
49 ** live-register info **
51 The information about where each register is live is in two parts:
52 the REG_NOTES of insns, and the vector basic_block->global_live_at_start.
54 basic_block->global_live_at_start has an element for each basic
55 block, and the element is a bit-vector with a bit for each hard or
56 pseudo register. The bit is 1 if the register is live at the
57 beginning of the basic block.
59 Two types of elements can be added to an insn's REG_NOTES.
60 A REG_DEAD note is added to an insn's REG_NOTES for any register
61 that meets both of two conditions: The value in the register is not
62 needed in subsequent insns and the insn does not replace the value in
63 the register (in the case of multi-word hard registers, the value in
64 each register must be replaced by the insn to avoid a REG_DEAD note).
66 In the vast majority of cases, an object in a REG_DEAD note will be
67 used somewhere in the insn. The (rare) exception to this is if an
68 insn uses a multi-word hard register and only some of the registers are
69 needed in subsequent insns. In that case, REG_DEAD notes will be
70 provided for those hard registers that are not subsequently needed.
71 Partial REG_DEAD notes of this type do not occur when an insn sets
72 only some of the hard registers used in such a multi-word operand;
73 omitting REG_DEAD notes for objects stored in an insn is optional and
74 the desire to do so does not justify the complexity of the partial
75 REG_DEAD notes.
77 REG_UNUSED notes are added for each register that is set by the insn
78 but is unused subsequently (if every register set by the insn is unused
79 and the insn does not reference memory or have some other side-effect,
80 the insn is deleted instead). If only part of a multi-word hard
81 register is used in a subsequent insn, REG_UNUSED notes are made for
82 the parts that will not be used.
84 To determine which registers are live after any insn, one can
85 start from the beginning of the basic block and scan insns, noting
86 which registers are set by each insn and which die there.
88 ** Other actions of life_analysis **
90 life_analysis sets up the LOG_LINKS fields of insns because the
91 information needed to do so is readily available.
93 life_analysis deletes insns whose only effect is to store a value
94 that is never used.
96 life_analysis notices cases where a reference to a register as
97 a memory address can be combined with a preceding or following
98 incrementation or decrementation of the register. The separate
99 instruction to increment or decrement is deleted and the address
100 is changed to a POST_INC or similar rtx.
102 Each time an incrementing or decrementing address is created,
103 a REG_INC element is added to the insn's REG_NOTES list.
105 life_analysis fills in certain vectors containing information about
106 register usage: reg_n_refs, reg_n_deaths, reg_n_sets, reg_live_length,
107 reg_n_calls_crosses and reg_basic_block.
109 life_analysis sets current_function_sp_is_unchanging if the function
110 doesn't modify the stack pointer. */
112 /* TODO:
114 Split out from life_analysis:
115 - local property discovery (bb->local_live, bb->local_set)
116 - global property computation
117 - log links creation
118 - pre/post modify transformation
121 #include "config.h"
122 #include "system.h"
123 #include "rtl.h"
124 #include "tm_p.h"
125 #include "basic-block.h"
126 #include "insn-config.h"
127 #include "regs.h"
128 #include "hard-reg-set.h"
129 #include "flags.h"
130 #include "output.h"
131 #include "function.h"
132 #include "except.h"
133 #include "toplev.h"
134 #include "recog.h"
135 #include "insn-flags.h"
137 #include "obstack.h"
139 #define obstack_chunk_alloc xmalloc
140 #define obstack_chunk_free free
143 /* EXIT_IGNORE_STACK should be nonzero if, when returning from a function,
144 the stack pointer does not matter. The value is tested only in
145 functions that have frame pointers.
146 No definition is equivalent to always zero. */
147 #ifndef EXIT_IGNORE_STACK
148 #define EXIT_IGNORE_STACK 0
149 #endif
152 /* The contents of the current function definition are allocated
153 in this obstack, and all are freed at the end of the function.
154 For top-level functions, this is temporary_obstack.
155 Separate obstacks are made for nested functions. */
157 extern struct obstack *function_obstack;
159 /* Number of basic blocks in the current function. */
161 int n_basic_blocks;
163 /* The basic block array. */
165 varray_type basic_block_info;
167 /* The special entry and exit blocks. */
169 struct basic_block_def entry_exit_blocks[2] =
172 NULL, /* head */
173 NULL, /* end */
174 NULL, /* pred */
175 NULL, /* succ */
176 NULL, /* local_set */
177 NULL, /* global_live_at_start */
178 NULL, /* global_live_at_end */
179 NULL, /* aux */
180 ENTRY_BLOCK, /* index */
181 0, /* loop_depth */
182 -1, -1 /* eh_beg, eh_end */
185 NULL, /* head */
186 NULL, /* end */
187 NULL, /* pred */
188 NULL, /* succ */
189 NULL, /* local_set */
190 NULL, /* global_live_at_start */
191 NULL, /* global_live_at_end */
192 NULL, /* aux */
193 EXIT_BLOCK, /* index */
194 0, /* loop_depth */
195 -1, -1 /* eh_beg, eh_end */
199 /* Nonzero if the second flow pass has completed. */
200 int flow2_completed;
202 /* Maximum register number used in this function, plus one. */
204 int max_regno;
206 /* Indexed by n, giving various register information */
208 varray_type reg_n_info;
210 /* Size of the reg_n_info table. */
212 unsigned int reg_n_max;
214 /* Element N is the next insn that uses (hard or pseudo) register number N
215 within the current basic block; or zero, if there is no such insn.
216 This is valid only during the final backward scan in propagate_block. */
218 static rtx *reg_next_use;
220 /* Size of a regset for the current function,
221 in (1) bytes and (2) elements. */
223 int regset_bytes;
224 int regset_size;
226 /* Regset of regs live when calls to `setjmp'-like functions happen. */
227 /* ??? Does this exist only for the setjmp-clobbered warning message? */
229 regset regs_live_at_setjmp;
231 /* List made of EXPR_LIST rtx's which gives pairs of pseudo registers
232 that have to go in the same hard reg.
233 The first two regs in the list are a pair, and the next two
234 are another pair, etc. */
235 rtx regs_may_share;
237 /* Depth within loops of basic block being scanned for lifetime analysis,
238 plus one. This is the weight attached to references to registers. */
240 static int loop_depth;
242 /* During propagate_block, this is non-zero if the value of CC0 is live. */
244 static int cc0_live;
246 /* During propagate_block, this contains a list of all the MEMs we are
247 tracking for dead store elimination.
249 ?!? Note we leak memory by not free-ing items on this list. We need to
250 write some generic routines to operate on memory lists since cse, gcse,
251 loop, sched, flow and possibly other passes all need to do basically the
252 same operations on these lists. */
254 static rtx mem_set_list;
256 /* Set of registers that may be eliminable. These are handled specially
257 in updating regs_ever_live. */
259 static HARD_REG_SET elim_reg_set;
261 /* The basic block structure for every insn, indexed by uid. */
263 varray_type basic_block_for_insn;
265 /* The labels mentioned in non-jump rtl. Valid during find_basic_blocks. */
266 /* ??? Should probably be using LABEL_NUSES instead. It would take a
267 bit of surgery to be able to use or co-opt the routines in jump. */
269 static rtx label_value_list;
271 /* INSN_VOLATILE (insn) is 1 if the insn refers to anything volatile. */
273 #define INSN_VOLATILE(INSN) bitmap_bit_p (uid_volatile, INSN_UID (INSN))
274 #define SET_INSN_VOLATILE(INSN) bitmap_set_bit (uid_volatile, INSN_UID (INSN))
275 static bitmap uid_volatile;
277 /* Forward declarations */
278 static int count_basic_blocks PROTO((rtx));
279 static rtx find_basic_blocks_1 PROTO((rtx));
280 static void create_basic_block PROTO((int, rtx, rtx, rtx));
281 static void clear_edges PROTO((void));
282 static void make_edges PROTO((rtx));
283 static void make_edge PROTO((basic_block, basic_block, int));
284 static void make_label_edge PROTO((basic_block, rtx, int));
285 static void make_eh_edge PROTO((eh_nesting_info *, basic_block,
286 rtx, int));
287 static void mark_critical_edges PROTO((void));
288 static void move_stray_eh_region_notes PROTO((void));
289 static void record_active_eh_regions PROTO((rtx));
291 static void commit_one_edge_insertion PROTO((edge));
293 static void delete_unreachable_blocks PROTO((void));
294 static void delete_eh_regions PROTO((void));
295 static int can_delete_note_p PROTO((rtx));
296 static void flow_delete_insn_chain PROTO((rtx, rtx));
297 static int delete_block PROTO((basic_block));
298 static void expunge_block PROTO((basic_block));
299 static rtx flow_delete_insn PROTO((rtx));
300 static int can_delete_label_p PROTO((rtx));
301 static int merge_blocks_move_predecessor_nojumps PROTO((basic_block,
302 basic_block));
303 static int merge_blocks_move_successor_nojumps PROTO((basic_block,
304 basic_block));
305 static void merge_blocks_nomove PROTO((basic_block, basic_block));
306 static int merge_blocks PROTO((edge,basic_block,basic_block));
307 static void try_merge_blocks PROTO((void));
308 static void tidy_fallthru_edge PROTO((edge,basic_block,basic_block));
309 static void calculate_loop_depth PROTO((rtx));
311 static int set_noop_p PROTO((rtx));
312 static int noop_move_p PROTO((rtx));
313 static void notice_stack_pointer_modification PROTO ((rtx, rtx));
314 static void record_volatile_insns PROTO((rtx));
315 static void mark_regs_live_at_end PROTO((regset));
316 static void life_analysis_1 PROTO((rtx, int, int));
317 static void init_regset_vector PROTO ((regset *, int,
318 struct obstack *));
319 static void propagate_block PROTO((regset, rtx, rtx, int,
320 regset, int, int));
321 static int insn_dead_p PROTO((rtx, regset, int, rtx));
322 static int libcall_dead_p PROTO((rtx, regset, rtx, rtx));
323 static void mark_set_regs PROTO((regset, regset, rtx,
324 rtx, regset));
325 static void mark_set_1 PROTO((regset, regset, rtx,
326 rtx, regset));
327 #ifdef AUTO_INC_DEC
328 static void find_auto_inc PROTO((regset, rtx, rtx));
329 static int try_pre_increment_1 PROTO((rtx));
330 static int try_pre_increment PROTO((rtx, rtx, HOST_WIDE_INT));
331 #endif
332 static void mark_used_regs PROTO((regset, regset, rtx, int, rtx));
333 void dump_flow_info PROTO((FILE *));
334 static void dump_edge_info PROTO((FILE *, edge, int));
336 static int_list_ptr alloc_int_list_node PROTO ((int_list_block **));
337 static int_list_ptr add_int_list_node PROTO ((int_list_block **,
338 int_list **, int));
340 static void add_pred_succ PROTO ((int, int, int_list_ptr *,
341 int_list_ptr *, int *, int *));
343 static void count_reg_sets_1 PROTO ((rtx));
344 static void count_reg_sets PROTO ((rtx));
345 static void count_reg_references PROTO ((rtx));
346 static void notice_stack_pointer_modification PROTO ((rtx, rtx));
347 static void invalidate_mems_from_autoinc PROTO ((rtx));
348 static void maybe_remove_dead_notes PROTO ((rtx, rtx, rtx, rtx,
349 rtx, rtx));
350 static int maybe_add_dead_note_use PROTO ((rtx, rtx));
351 static int maybe_add_dead_note PROTO ((rtx, rtx, rtx));
352 static int sets_reg_or_subreg PROTO ((rtx, rtx));
353 static void update_n_sets PROTO ((rtx, int));
354 static void new_insn_dead_notes PROTO ((rtx, rtx, rtx, rtx, rtx, rtx));
355 void verify_flow_info PROTO ((void));
357 /* Find basic blocks of the current function.
358 F is the first insn of the function and NREGS the number of register
359 numbers in use. */
361 void
362 find_basic_blocks (f, nregs, file, do_cleanup)
363 rtx f;
364 int nregs ATTRIBUTE_UNUSED;
365 FILE *file ATTRIBUTE_UNUSED;
366 int do_cleanup;
368 int max_uid;
370 /* Flush out existing data. */
371 if (basic_block_info != NULL)
373 int i;
375 clear_edges ();
377 /* Clear bb->aux on all extant basic blocks. We'll use this as a
378 tag for reuse during create_basic_block, just in case some pass
379 copies around basic block notes improperly. */
380 for (i = 0; i < n_basic_blocks; ++i)
381 BASIC_BLOCK (i)->aux = NULL;
383 VARRAY_FREE (basic_block_info);
386 n_basic_blocks = count_basic_blocks (f);
388 /* Size the basic block table. The actual structures will be allocated
389 by find_basic_blocks_1, since we want to keep the structure pointers
390 stable across calls to find_basic_blocks. */
391 /* ??? This whole issue would be much simpler if we called find_basic_blocks
392 exactly once, and thereafter we don't have a single long chain of
393 instructions at all until close to the end of compilation when we
394 actually lay them out. */
396 VARRAY_BB_INIT (basic_block_info, n_basic_blocks, "basic_block_info");
398 label_value_list = find_basic_blocks_1 (f);
400 /* Record the block to which an insn belongs. */
401 /* ??? This should be done another way, by which (perhaps) a label is
402 tagged directly with the basic block that it starts. It is used for
403 more than that currently, but IMO that is the only valid use. */
405 max_uid = get_max_uid ();
406 #ifdef AUTO_INC_DEC
407 /* Leave space for insns life_analysis makes in some cases for auto-inc.
408 These cases are rare, so we don't need too much space. */
409 max_uid += max_uid / 10;
410 #endif
412 compute_bb_for_insn (max_uid);
414 /* Discover the edges of our cfg. */
416 record_active_eh_regions (f);
417 make_edges (label_value_list);
419 /* Delete unreachable blocks, then merge blocks when possible. */
421 if (do_cleanup)
423 delete_unreachable_blocks ();
424 move_stray_eh_region_notes ();
425 record_active_eh_regions (f);
426 try_merge_blocks ();
429 /* Mark critical edges. */
431 mark_critical_edges ();
433 /* Discover the loop depth at the start of each basic block to aid
434 register allocation. */
435 calculate_loop_depth (f);
437 /* Kill the data we won't maintain. */
438 label_value_list = 0;
440 #ifdef ENABLE_CHECKING
441 verify_flow_info ();
442 #endif
445 /* Count the basic blocks of the function. */
447 static int
448 count_basic_blocks (f)
449 rtx f;
451 register rtx insn;
452 register RTX_CODE prev_code;
453 register int count = 0;
454 int eh_region = 0;
455 int call_had_abnormal_edge = 0;
456 rtx prev_call = NULL_RTX;
458 prev_code = JUMP_INSN;
459 for (insn = f; insn; insn = NEXT_INSN (insn))
461 register RTX_CODE code = GET_CODE (insn);
463 if (code == CODE_LABEL
464 || (GET_RTX_CLASS (code) == 'i'
465 && (prev_code == JUMP_INSN
466 || prev_code == BARRIER
467 || (prev_code == CALL_INSN && call_had_abnormal_edge))))
469 count++;
471 /* If the previous insn was a call that did not create an
472 abnormal edge, we want to add a nop so that the CALL_INSN
473 itself is not at basic_block_end. This allows us to
474 easily distinguish between normal calls and those which
475 create abnormal edges in the flow graph. */
477 if (count > 0 && prev_call != 0 && !call_had_abnormal_edge)
479 rtx nop = gen_rtx_USE (VOIDmode, const0_rtx);
480 emit_insn_after (nop, prev_call);
484 /* Record whether this call created an edge. */
485 if (code == CALL_INSN)
487 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
488 int region = (note ? XWINT (XEXP (note, 0), 0) : 1);
489 prev_call = insn;
490 call_had_abnormal_edge = 0;
492 /* If there is a specified EH region, we have an edge. */
493 if (eh_region && region > 0)
494 call_had_abnormal_edge = 1;
495 else
497 /* If there is a nonlocal goto label and the specified
498 region number isn't -1, we have an edge. (0 means
499 no throw, but might have a nonlocal goto). */
500 if (nonlocal_goto_handler_labels && region >= 0)
501 call_had_abnormal_edge = 1;
504 else if (code != NOTE)
505 prev_call = NULL_RTX;
507 if (code != NOTE)
508 prev_code = code;
509 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG)
510 ++eh_region;
511 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END)
512 --eh_region;
516 /* The rest of the compiler works a bit smoother when we don't have to
517 check for the edge case of do-nothing functions with no basic blocks. */
518 if (count == 0)
520 emit_insn (gen_rtx_USE (VOIDmode, const0_rtx));
521 count = 1;
524 return count;
527 /* Find all basic blocks of the function whose first insn is F.
529 Collect and return a list of labels whose addresses are taken. This
530 will be used in make_edges for use with computed gotos. */
532 static rtx
533 find_basic_blocks_1 (f)
534 rtx f;
536 register rtx insn, next;
537 int call_has_abnormal_edge = 0;
538 int i = 0;
539 rtx bb_note = NULL_RTX;
540 rtx eh_list = NULL_RTX;
541 rtx label_value_list = NULL_RTX;
542 rtx head = NULL_RTX;
543 rtx end = NULL_RTX;
545 /* We process the instructions in a slightly different way than we did
546 previously. This is so that we see a NOTE_BASIC_BLOCK after we have
547 closed out the previous block, so that it gets attached at the proper
548 place. Since this form should be equivalent to the previous,
549 count_basic_blocks continues to use the old form as a check. */
551 for (insn = f; insn; insn = next)
553 enum rtx_code code = GET_CODE (insn);
555 next = NEXT_INSN (insn);
557 if (code == CALL_INSN)
559 /* Record whether this call created an edge. */
560 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
561 int region = (note ? XWINT (XEXP (note, 0), 0) : 1);
562 call_has_abnormal_edge = 0;
564 /* If there is an EH region, we have an edge. */
565 if (eh_list && region > 0)
566 call_has_abnormal_edge = 1;
567 else
569 /* If there is a nonlocal goto label and the specified
570 region number isn't -1, we have an edge. (0 means
571 no throw, but might have a nonlocal goto). */
572 if (nonlocal_goto_handler_labels && region >= 0)
573 call_has_abnormal_edge = 1;
577 switch (code)
579 case NOTE:
581 int kind = NOTE_LINE_NUMBER (insn);
583 /* Keep a LIFO list of the currently active exception notes. */
584 if (kind == NOTE_INSN_EH_REGION_BEG)
585 eh_list = alloc_INSN_LIST (insn, eh_list);
586 else if (kind == NOTE_INSN_EH_REGION_END)
588 rtx t = eh_list;
589 eh_list = XEXP (eh_list, 1);
590 free_INSN_LIST_node (t);
593 /* Look for basic block notes with which to keep the
594 basic_block_info pointers stable. Unthread the note now;
595 we'll put it back at the right place in create_basic_block.
596 Or not at all if we've already found a note in this block. */
597 else if (kind == NOTE_INSN_BASIC_BLOCK)
599 if (bb_note == NULL_RTX)
600 bb_note = insn;
601 next = flow_delete_insn (insn);
604 break;
607 case CODE_LABEL:
608 /* A basic block starts at a label. If we've closed one off due
609 to a barrier or some such, no need to do it again. */
610 if (head != NULL_RTX)
612 /* While we now have edge lists with which other portions of
613 the compiler might determine a call ending a basic block
614 does not imply an abnormal edge, it will be a bit before
615 everything can be updated. So continue to emit a noop at
616 the end of such a block. */
617 if (GET_CODE (end) == CALL_INSN)
619 rtx nop = gen_rtx_USE (VOIDmode, const0_rtx);
620 end = emit_insn_after (nop, end);
623 create_basic_block (i++, head, end, bb_note);
624 bb_note = NULL_RTX;
626 head = end = insn;
627 break;
629 case JUMP_INSN:
630 /* A basic block ends at a jump. */
631 if (head == NULL_RTX)
632 head = insn;
633 else
635 /* ??? Make a special check for table jumps. The way this
636 happens is truely and amazingly gross. We are about to
637 create a basic block that contains just a code label and
638 an addr*vec jump insn. Worse, an addr_diff_vec creates
639 its own natural loop.
641 Prevent this bit of brain damage, pasting things together
642 correctly in make_edges.
644 The correct solution involves emitting the table directly
645 on the tablejump instruction as a note, or JUMP_LABEL. */
647 if (GET_CODE (PATTERN (insn)) == ADDR_VEC
648 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
650 head = end = NULL;
651 n_basic_blocks--;
652 break;
655 end = insn;
656 goto new_bb_inclusive;
658 case BARRIER:
659 /* A basic block ends at a barrier. It may be that an unconditional
660 jump already closed the basic block -- no need to do it again. */
661 if (head == NULL_RTX)
662 break;
664 /* While we now have edge lists with which other portions of the
665 compiler might determine a call ending a basic block does not
666 imply an abnormal edge, it will be a bit before everything can
667 be updated. So continue to emit a noop at the end of such a
668 block. */
669 if (GET_CODE (end) == CALL_INSN)
671 rtx nop = gen_rtx_USE (VOIDmode, const0_rtx);
672 end = emit_insn_after (nop, end);
674 goto new_bb_exclusive;
676 case CALL_INSN:
677 /* A basic block ends at a call that can either throw or
678 do a non-local goto. */
679 if (call_has_abnormal_edge)
681 new_bb_inclusive:
682 if (head == NULL_RTX)
683 head = insn;
684 end = insn;
686 new_bb_exclusive:
687 create_basic_block (i++, head, end, bb_note);
688 head = end = NULL_RTX;
689 bb_note = NULL_RTX;
690 break;
692 /* FALLTHRU */
694 default:
695 if (GET_RTX_CLASS (code) == 'i')
697 if (head == NULL_RTX)
698 head = insn;
699 end = insn;
701 break;
704 if (GET_RTX_CLASS (code) == 'i')
706 rtx note;
708 /* Make a list of all labels referred to other than by jumps
709 (which just don't have the REG_LABEL notes).
711 Make a special exception for labels followed by an ADDR*VEC,
712 as this would be a part of the tablejump setup code.
714 Make a special exception for the eh_return_stub_label, which
715 we know isn't part of any otherwise visible control flow. */
717 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
718 if (REG_NOTE_KIND (note) == REG_LABEL)
720 rtx lab = XEXP (note, 0), next;
722 if (lab == eh_return_stub_label)
724 else if ((next = next_nonnote_insn (lab)) != NULL
725 && GET_CODE (next) == JUMP_INSN
726 && (GET_CODE (PATTERN (next)) == ADDR_VEC
727 || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC))
729 else
730 label_value_list
731 = gen_rtx_EXPR_LIST (VOIDmode, XEXP (note, 0),
732 label_value_list);
737 if (head != NULL_RTX)
738 create_basic_block (i++, head, end, bb_note);
740 if (i != n_basic_blocks)
741 abort ();
743 return label_value_list;
746 /* Create a new basic block consisting of the instructions between
747 HEAD and END inclusive. Reuses the note and basic block struct
748 in BB_NOTE, if any. */
750 static void
751 create_basic_block (index, head, end, bb_note)
752 int index;
753 rtx head, end, bb_note;
755 basic_block bb;
757 if (bb_note
758 && ! RTX_INTEGRATED_P (bb_note)
759 && (bb = NOTE_BASIC_BLOCK (bb_note)) != NULL
760 && bb->aux == NULL)
762 /* If we found an existing note, thread it back onto the chain. */
764 if (GET_CODE (head) == CODE_LABEL)
765 add_insn_after (bb_note, head);
766 else
768 add_insn_before (bb_note, head);
769 head = bb_note;
772 else
774 /* Otherwise we must create a note and a basic block structure.
775 Since we allow basic block structs in rtl, give the struct
776 the same lifetime by allocating it off the function obstack
777 rather than using malloc. */
779 bb = (basic_block) obstack_alloc (function_obstack, sizeof (*bb));
780 memset (bb, 0, sizeof (*bb));
782 if (GET_CODE (head) == CODE_LABEL)
783 bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK, head);
784 else
786 bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, head);
787 head = bb_note;
789 NOTE_BASIC_BLOCK (bb_note) = bb;
792 /* Always include the bb note in the block. */
793 if (NEXT_INSN (end) == bb_note)
794 end = bb_note;
796 bb->head = head;
797 bb->end = end;
798 bb->index = index;
799 BASIC_BLOCK (index) = bb;
801 /* Tag the block so that we know it has been used when considering
802 other basic block notes. */
803 bb->aux = bb;
806 /* Records the basic block struct in BB_FOR_INSN, for every instruction
807 indexed by INSN_UID. MAX is the size of the array. */
809 void
810 compute_bb_for_insn (max)
811 int max;
813 int i;
815 VARRAY_BB_INIT (basic_block_for_insn, max, "basic_block_for_insn");
817 for (i = 0; i < n_basic_blocks; ++i)
819 basic_block bb = BASIC_BLOCK (i);
820 rtx insn, end;
822 end = bb->end;
823 insn = bb->head;
824 while (1)
826 int uid = INSN_UID (insn);
827 if (uid < max)
828 VARRAY_BB (basic_block_for_insn, uid) = bb;
829 if (insn == end)
830 break;
831 insn = NEXT_INSN (insn);
836 /* Free the memory associated with the edge structures. */
838 static void
839 clear_edges ()
841 int i;
842 edge n, e;
844 for (i = 0; i < n_basic_blocks; ++i)
846 basic_block bb = BASIC_BLOCK (i);
848 for (e = bb->succ; e ; e = n)
850 n = e->succ_next;
851 free (e);
854 bb->succ = 0;
855 bb->pred = 0;
858 for (e = ENTRY_BLOCK_PTR->succ; e ; e = n)
860 n = e->succ_next;
861 free (e);
864 ENTRY_BLOCK_PTR->succ = 0;
865 EXIT_BLOCK_PTR->pred = 0;
868 /* Identify the edges between basic blocks.
870 NONLOCAL_LABEL_LIST is a list of non-local labels in the function. Blocks
871 that are otherwise unreachable may be reachable with a non-local goto.
873 BB_EH_END is an array indexed by basic block number in which we record
874 the list of exception regions active at the end of the basic block. */
876 static void
877 make_edges (label_value_list)
878 rtx label_value_list;
880 int i;
881 eh_nesting_info *eh_nest_info = init_eh_nesting_info ();
883 /* Assume no computed jump; revise as we create edges. */
884 current_function_has_computed_jump = 0;
886 /* By nature of the way these get numbered, block 0 is always the entry. */
887 make_edge (ENTRY_BLOCK_PTR, BASIC_BLOCK (0), EDGE_FALLTHRU);
889 for (i = 0; i < n_basic_blocks; ++i)
891 basic_block bb = BASIC_BLOCK (i);
892 rtx insn, x;
893 enum rtx_code code;
894 int force_fallthru = 0;
896 /* Examine the last instruction of the block, and discover the
897 ways we can leave the block. */
899 insn = bb->end;
900 code = GET_CODE (insn);
902 /* A branch. */
903 if (code == JUMP_INSN)
905 rtx tmp;
907 /* ??? Recognize a tablejump and do the right thing. */
908 if ((tmp = JUMP_LABEL (insn)) != NULL_RTX
909 && (tmp = NEXT_INSN (tmp)) != NULL_RTX
910 && GET_CODE (tmp) == JUMP_INSN
911 && (GET_CODE (PATTERN (tmp)) == ADDR_VEC
912 || GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC))
914 rtvec vec;
915 int j;
917 if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
918 vec = XVEC (PATTERN (tmp), 0);
919 else
920 vec = XVEC (PATTERN (tmp), 1);
922 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
923 make_label_edge (bb, XEXP (RTVEC_ELT (vec, j), 0), 0);
925 /* Some targets (eg, ARM) emit a conditional jump that also
926 contains the out-of-range target. Scan for these and
927 add an edge if necessary. */
928 if ((tmp = single_set (insn)) != NULL
929 && SET_DEST (tmp) == pc_rtx
930 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
931 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
932 make_label_edge (bb, XEXP (XEXP (SET_SRC (tmp), 2), 0), 0);
934 #ifdef CASE_DROPS_THROUGH
935 /* Silly VAXen. The ADDR_VEC is going to be in the way of
936 us naturally detecting fallthru into the next block. */
937 force_fallthru = 1;
938 #endif
941 /* If this is a computed jump, then mark it as reaching
942 everything on the label_value_list and forced_labels list. */
943 else if (computed_jump_p (insn))
945 current_function_has_computed_jump = 1;
947 for (x = label_value_list; x; x = XEXP (x, 1))
948 make_label_edge (bb, XEXP (x, 0), EDGE_ABNORMAL);
950 for (x = forced_labels; x; x = XEXP (x, 1))
951 make_label_edge (bb, XEXP (x, 0), EDGE_ABNORMAL);
954 /* Returns create an exit out. */
955 else if (returnjump_p (insn))
956 make_edge (bb, EXIT_BLOCK_PTR, 0);
958 /* Otherwise, we have a plain conditional or unconditional jump. */
959 else
961 if (! JUMP_LABEL (insn))
962 abort ();
963 make_label_edge (bb, JUMP_LABEL (insn), 0);
967 /* If this is a CALL_INSN, then mark it as reaching the active EH
968 handler for this CALL_INSN. If we're handling asynchronous
969 exceptions then any insn can reach any of the active handlers.
971 Also mark the CALL_INSN as reaching any nonlocal goto handler. */
973 if (code == CALL_INSN || asynchronous_exceptions)
975 /* If there's an EH region active at the end of a block,
976 add the appropriate edges. */
977 if (bb->eh_end >= 0)
978 make_eh_edge (eh_nest_info, bb, insn, bb->eh_end);
980 /* If we have asynchronous exceptions, do the same for *all*
981 exception regions active in the block. */
982 if (asynchronous_exceptions
983 && bb->eh_beg != bb->eh_end)
985 if (bb->eh_beg >= 0)
986 make_eh_edge (eh_nest_info, bb, NULL_RTX, bb->eh_beg);
988 for (x = bb->head; x != bb->end; x = PREV_INSN (x))
989 if (GET_CODE (x) == NOTE
990 && (NOTE_LINE_NUMBER (x) == NOTE_INSN_EH_REGION_BEG
991 || NOTE_LINE_NUMBER (x) == NOTE_INSN_EH_REGION_END))
993 int region = NOTE_EH_HANDLER (x);
994 make_eh_edge (eh_nest_info, bb, NULL_RTX, region);
998 if (code == CALL_INSN && nonlocal_goto_handler_labels)
1000 /* ??? This could be made smarter: in some cases it's possible
1001 to tell that certain calls will not do a nonlocal goto.
1003 For example, if the nested functions that do the nonlocal
1004 gotos do not have their addresses taken, then only calls to
1005 those functions or to other nested functions that use them
1006 could possibly do nonlocal gotos. */
1007 /* We do know that a REG_EH_REGION note with a value less
1008 than 0 is guaranteed not to perform a non-local goto. */
1009 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1010 if (!note || XINT (XEXP (note, 0), 0) >= 0)
1011 for (x = nonlocal_goto_handler_labels; x ; x = XEXP (x, 1))
1012 make_label_edge (bb, XEXP (x, 0),
1013 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
1017 /* We know something about the structure of the function __throw in
1018 libgcc2.c. It is the only function that ever contains eh_stub
1019 labels. It modifies its return address so that the last block
1020 returns to one of the eh_stub labels within it. So we have to
1021 make additional edges in the flow graph. */
1022 if (i + 1 == n_basic_blocks && eh_return_stub_label != 0)
1023 make_label_edge (bb, eh_return_stub_label, EDGE_EH);
1025 /* Find out if we can drop through to the next block. */
1026 insn = next_nonnote_insn (insn);
1027 if (!insn || (i + 1 == n_basic_blocks && force_fallthru))
1028 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
1029 else if (i + 1 < n_basic_blocks)
1031 rtx tmp = BLOCK_HEAD (i + 1);
1032 if (GET_CODE (tmp) == NOTE)
1033 tmp = next_nonnote_insn (tmp);
1034 if (force_fallthru || insn == tmp)
1035 make_edge (bb, BASIC_BLOCK (i + 1), EDGE_FALLTHRU);
1038 free_eh_nesting_info (eh_nest_info);
1041 /* Create an edge between two basic blocks. FLAGS are auxiliary information
1042 about the edge that is accumulated between calls. */
1044 static void
1045 make_edge (src, dst, flags)
1046 basic_block src, dst;
1047 int flags;
1049 edge e;
1051 /* Make sure we don't add duplicate edges. */
1053 for (e = src->succ; e ; e = e->succ_next)
1054 if (e->dest == dst)
1056 e->flags |= flags;
1057 return;
1060 e = (edge) xcalloc (1, sizeof (*e));
1062 e->succ_next = src->succ;
1063 e->pred_next = dst->pred;
1064 e->src = src;
1065 e->dest = dst;
1066 e->flags = flags;
1068 src->succ = e;
1069 dst->pred = e;
1072 /* Create an edge from a basic block to a label. */
1074 static void
1075 make_label_edge (src, label, flags)
1076 basic_block src;
1077 rtx label;
1078 int flags;
1080 if (GET_CODE (label) != CODE_LABEL)
1081 abort ();
1083 /* If the label was never emitted, this insn is junk, but avoid a
1084 crash trying to refer to BLOCK_FOR_INSN (label). This can happen
1085 as a result of a syntax error and a diagnostic has already been
1086 printed. */
1088 if (INSN_UID (label) == 0)
1089 return;
1091 make_edge (src, BLOCK_FOR_INSN (label), flags);
1094 /* Create the edges generated by INSN in REGION. */
1096 static void
1097 make_eh_edge (eh_nest_info, src, insn, region)
1098 eh_nesting_info *eh_nest_info;
1099 basic_block src;
1100 rtx insn;
1101 int region;
1103 handler_info **handler_list;
1104 int num, is_call;
1106 is_call = (insn && GET_CODE (insn) == CALL_INSN ? EDGE_ABNORMAL_CALL : 0);
1107 num = reachable_handlers (region, eh_nest_info, insn, &handler_list);
1108 while (--num >= 0)
1110 make_label_edge (src, handler_list[num]->handler_label,
1111 EDGE_ABNORMAL | EDGE_EH | is_call);
1115 /* EH_REGION notes appearing between basic blocks is ambiguous, and even
1116 dangerous if we intend to move basic blocks around. Move such notes
1117 into the following block. */
1119 static void
1120 move_stray_eh_region_notes ()
1122 int i;
1123 basic_block b1, b2;
1125 if (n_basic_blocks < 2)
1126 return;
1128 b2 = BASIC_BLOCK (n_basic_blocks - 1);
1129 for (i = n_basic_blocks - 2; i >= 0; --i, b2 = b1)
1131 rtx insn, next, list = NULL_RTX;
1133 b1 = BASIC_BLOCK (i);
1134 for (insn = NEXT_INSN (b1->end); insn != b2->head; insn = next)
1136 next = NEXT_INSN (insn);
1137 if (GET_CODE (insn) == NOTE
1138 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG
1139 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END))
1141 /* Unlink from the insn chain. */
1142 NEXT_INSN (PREV_INSN (insn)) = next;
1143 PREV_INSN (next) = PREV_INSN (insn);
1145 /* Queue it. */
1146 NEXT_INSN (insn) = list;
1147 list = insn;
1151 if (list == NULL_RTX)
1152 continue;
1154 /* Find where to insert these things. */
1155 insn = b2->head;
1156 if (GET_CODE (insn) == CODE_LABEL)
1157 insn = NEXT_INSN (insn);
1159 while (list)
1161 next = NEXT_INSN (list);
1162 add_insn_after (list, insn);
1163 list = next;
1168 /* Recompute eh_beg/eh_end for each basic block. */
1170 static void
1171 record_active_eh_regions (f)
1172 rtx f;
1174 rtx insn, eh_list = NULL_RTX;
1175 int i = 0;
1176 basic_block bb = BASIC_BLOCK (0);
1178 for (insn = f; insn ; insn = NEXT_INSN (insn))
1180 if (bb->head == insn)
1181 bb->eh_beg = (eh_list ? NOTE_EH_HANDLER (XEXP (eh_list, 0)) : -1);
1183 if (GET_CODE (insn) == NOTE)
1185 int kind = NOTE_LINE_NUMBER (insn);
1186 if (kind == NOTE_INSN_EH_REGION_BEG)
1187 eh_list = alloc_INSN_LIST (insn, eh_list);
1188 else if (kind == NOTE_INSN_EH_REGION_END)
1190 rtx t = XEXP (eh_list, 1);
1191 free_INSN_LIST_node (eh_list);
1192 eh_list = t;
1196 if (bb->end == insn)
1198 bb->eh_end = (eh_list ? NOTE_EH_HANDLER (XEXP (eh_list, 0)) : -1);
1199 i += 1;
1200 if (i == n_basic_blocks)
1201 break;
1202 bb = BASIC_BLOCK (i);
1207 /* Identify critical edges and set the bits appropriately. */
1209 static void
1210 mark_critical_edges ()
1212 int i, n = n_basic_blocks;
1213 basic_block bb;
1215 /* We begin with the entry block. This is not terribly important now,
1216 but could be if a front end (Fortran) implemented alternate entry
1217 points. */
1218 bb = ENTRY_BLOCK_PTR;
1219 i = -1;
1221 while (1)
1223 edge e;
1225 /* (1) Critical edges must have a source with multiple successors. */
1226 if (bb->succ && bb->succ->succ_next)
1228 for (e = bb->succ; e ; e = e->succ_next)
1230 /* (2) Critical edges must have a destination with multiple
1231 predecessors. Note that we know there is at least one
1232 predecessor -- the edge we followed to get here. */
1233 if (e->dest->pred->pred_next)
1234 e->flags |= EDGE_CRITICAL;
1235 else
1236 e->flags &= ~EDGE_CRITICAL;
1239 else
1241 for (e = bb->succ; e ; e = e->succ_next)
1242 e->flags &= ~EDGE_CRITICAL;
1245 if (++i >= n)
1246 break;
1247 bb = BASIC_BLOCK (i);
1251 /* Split a (typically critical) edge. Return the new block.
1252 Abort on abnormal edges.
1254 ??? The code generally expects to be called on critical edges.
1255 The case of a block ending in an unconditional jump to a
1256 block with multiple predecessors is not handled optimally. */
1258 basic_block
1259 split_edge (edge_in)
1260 edge edge_in;
1262 basic_block old_pred, bb, old_succ;
1263 edge edge_out;
1264 rtx bb_note;
1265 int i, j;
1267 /* Abnormal edges cannot be split. */
1268 if ((edge_in->flags & EDGE_ABNORMAL) != 0)
1269 abort ();
1271 old_pred = edge_in->src;
1272 old_succ = edge_in->dest;
1274 /* Remove the existing edge from the destination's pred list. */
1276 edge *pp;
1277 for (pp = &old_succ->pred; *pp != edge_in; pp = &(*pp)->pred_next)
1278 continue;
1279 *pp = edge_in->pred_next;
1280 edge_in->pred_next = NULL;
1283 /* Create the new structures. */
1284 bb = (basic_block) obstack_alloc (function_obstack, sizeof (*bb));
1285 edge_out = (edge) xcalloc (1, sizeof (*edge_out));
1287 memset (bb, 0, sizeof (*bb));
1288 bb->local_set = OBSTACK_ALLOC_REG_SET (function_obstack);
1289 bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (function_obstack);
1290 bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (function_obstack);
1292 /* ??? This info is likely going to be out of date very soon. */
1293 CLEAR_REG_SET (bb->local_set);
1294 if (old_succ->global_live_at_start)
1296 COPY_REG_SET (bb->global_live_at_start, old_succ->global_live_at_start);
1297 COPY_REG_SET (bb->global_live_at_end, old_succ->global_live_at_start);
1299 else
1301 CLEAR_REG_SET (bb->global_live_at_start);
1302 CLEAR_REG_SET (bb->global_live_at_end);
1305 /* Wire them up. */
1306 bb->pred = edge_in;
1307 bb->succ = edge_out;
1309 edge_in->dest = bb;
1310 edge_in->flags &= ~EDGE_CRITICAL;
1312 edge_out->pred_next = old_succ->pred;
1313 edge_out->succ_next = NULL;
1314 edge_out->src = bb;
1315 edge_out->dest = old_succ;
1316 edge_out->flags = EDGE_FALLTHRU;
1317 edge_out->probability = REG_BR_PROB_BASE;
1319 old_succ->pred = edge_out;
1321 /* Tricky case -- if there existed a fallthru into the successor
1322 (and we're not it) we must add a new unconditional jump around
1323 the new block we're actually interested in.
1325 Further, if that edge is critical, this means a second new basic
1326 block must be created to hold it. In order to simplify correct
1327 insn placement, do this before we touch the existing basic block
1328 ordering for the block we were really wanting. */
1329 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1331 edge e;
1332 for (e = edge_out->pred_next; e ; e = e->pred_next)
1333 if (e->flags & EDGE_FALLTHRU)
1334 break;
1336 if (e)
1338 basic_block jump_block;
1339 rtx pos;
1341 if ((e->flags & EDGE_CRITICAL) == 0)
1343 /* Non critical -- we can simply add a jump to the end
1344 of the existing predecessor. */
1345 jump_block = e->src;
1347 else
1349 /* We need a new block to hold the jump. The simplest
1350 way to do the bulk of the work here is to recursively
1351 call ourselves. */
1352 jump_block = split_edge (e);
1353 e = jump_block->succ;
1356 /* Now add the jump insn ... */
1357 pos = emit_jump_insn_after (gen_jump (old_succ->head),
1358 jump_block->end);
1359 jump_block->end = pos;
1360 emit_barrier_after (pos);
1362 /* ... let jump know that label is in use, ... */
1363 JUMP_LABEL (pos) = old_succ->head;
1364 ++LABEL_NUSES (old_succ->head);
1366 /* ... and clear fallthru on the outgoing edge. */
1367 e->flags &= ~EDGE_FALLTHRU;
1369 /* Continue splitting the interesting edge. */
1373 /* Place the new block just in front of the successor. */
1374 VARRAY_GROW (basic_block_info, ++n_basic_blocks);
1375 if (old_succ == EXIT_BLOCK_PTR)
1376 j = n_basic_blocks - 1;
1377 else
1378 j = old_succ->index;
1379 for (i = n_basic_blocks - 1; i > j; --i)
1381 basic_block tmp = BASIC_BLOCK (i - 1);
1382 BASIC_BLOCK (i) = tmp;
1383 tmp->index = i;
1385 BASIC_BLOCK (i) = bb;
1386 bb->index = i;
1388 /* Create the basic block note. */
1389 if (old_succ != EXIT_BLOCK_PTR)
1390 bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, old_succ->head);
1391 else
1392 bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK, get_last_insn ());
1393 NOTE_BASIC_BLOCK (bb_note) = bb;
1394 bb->head = bb->end = bb_note;
1396 /* Not quite simple -- for non-fallthru edges, we must adjust the
1397 predecessor's jump instruction to target our new block. */
1398 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1400 rtx tmp, insn = old_pred->end;
1401 rtx old_label = old_succ->head;
1402 rtx new_label = gen_label_rtx ();
1404 if (GET_CODE (insn) != JUMP_INSN)
1405 abort ();
1407 /* ??? Recognize a tablejump and adjust all matching cases. */
1408 if ((tmp = JUMP_LABEL (insn)) != NULL_RTX
1409 && (tmp = NEXT_INSN (tmp)) != NULL_RTX
1410 && GET_CODE (tmp) == JUMP_INSN
1411 && (GET_CODE (PATTERN (tmp)) == ADDR_VEC
1412 || GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC))
1414 rtvec vec;
1415 int j;
1417 if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
1418 vec = XVEC (PATTERN (tmp), 0);
1419 else
1420 vec = XVEC (PATTERN (tmp), 1);
1422 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
1423 if (XEXP (RTVEC_ELT (vec, j), 0) == old_label)
1425 RTVEC_ELT (vec, j) = gen_rtx_LABEL_REF (VOIDmode, new_label);
1426 --LABEL_NUSES (old_label);
1427 ++LABEL_NUSES (new_label);
1430 else
1432 /* This would have indicated an abnormal edge. */
1433 if (computed_jump_p (insn))
1434 abort ();
1436 /* A return instruction can't be redirected. */
1437 if (returnjump_p (insn))
1438 abort ();
1440 /* If the insn doesn't go where we think, we're confused. */
1441 if (JUMP_LABEL (insn) != old_label)
1442 abort ();
1444 redirect_jump (insn, new_label);
1447 emit_label_before (new_label, bb_note);
1448 bb->head = new_label;
1451 return bb;
1454 /* Queue instructions for insertion on an edge between two basic blocks.
1455 The new instructions and basic blocks (if any) will not appear in the
1456 CFG until commit_edge_insertions is called. */
1458 void
1459 insert_insn_on_edge (pattern, e)
1460 rtx pattern;
1461 edge e;
1463 /* We cannot insert instructions on an abnormal critical edge.
1464 It will be easier to find the culprit if we die now. */
1465 if ((e->flags & (EDGE_ABNORMAL|EDGE_CRITICAL))
1466 == (EDGE_ABNORMAL|EDGE_CRITICAL))
1467 abort ();
1469 if (e->insns == NULL_RTX)
1470 start_sequence ();
1471 else
1472 push_to_sequence (e->insns);
1474 emit_insn (pattern);
1476 e->insns = get_insns ();
1477 end_sequence();
1480 /* Update the CFG for the instructions queued on edge E. */
1482 static void
1483 commit_one_edge_insertion (e)
1484 edge e;
1486 rtx before = NULL_RTX, after = NULL_RTX, tmp;
1487 basic_block bb;
1489 /* Figure out where to put these things. If the destination has
1490 one predecessor, insert there. Except for the exit block. */
1491 if (e->dest->pred->pred_next == NULL
1492 && e->dest != EXIT_BLOCK_PTR)
1494 bb = e->dest;
1496 /* Get the location correct wrt a code label, and "nice" wrt
1497 a basic block note, and before everything else. */
1498 tmp = bb->head;
1499 if (GET_CODE (tmp) == CODE_LABEL)
1500 tmp = NEXT_INSN (tmp);
1501 if (GET_CODE (tmp) == NOTE
1502 && NOTE_LINE_NUMBER (tmp) == NOTE_INSN_BASIC_BLOCK)
1503 tmp = NEXT_INSN (tmp);
1504 if (tmp == bb->head)
1505 before = tmp;
1506 else
1507 after = PREV_INSN (tmp);
1510 /* If the source has one successor and the edge is not abnormal,
1511 insert there. Except for the entry block. */
1512 else if ((e->flags & EDGE_ABNORMAL) == 0
1513 && e->src->succ->succ_next == NULL
1514 && e->src != ENTRY_BLOCK_PTR)
1516 bb = e->src;
1517 if (GET_CODE (bb->end) == JUMP_INSN)
1519 /* ??? Is it possible to wind up with non-simple jumps? Perhaps
1520 a jump with delay slots already filled? */
1521 if (! simplejump_p (bb->end))
1522 abort ();
1524 before = bb->end;
1526 else
1528 /* We'd better be fallthru, or we've lost track of what's what. */
1529 if ((e->flags & EDGE_FALLTHRU) == 0)
1530 abort ();
1532 after = bb->end;
1536 /* Otherwise we must split the edge. */
1537 else
1539 bb = split_edge (e);
1540 after = bb->end;
1543 /* Now that we've found the spot, do the insertion. */
1544 tmp = e->insns;
1545 e->insns = NULL_RTX;
1547 /* Set the new block number for these insns, if structure is allocated. */
1548 if (basic_block_for_insn)
1550 rtx i;
1551 for (i = tmp; i != NULL_RTX; i = NEXT_INSN (i))
1552 set_block_for_insn (i, bb);
1555 if (before)
1557 emit_insns_before (tmp, before);
1558 if (before == bb->head)
1559 bb->head = tmp;
1561 else
1563 tmp = emit_insns_after (tmp, after);
1564 if (after == bb->end)
1565 bb->end = tmp;
1569 /* Update the CFG for all queued instructions. */
1571 void
1572 commit_edge_insertions ()
1574 int i;
1575 basic_block bb;
1577 i = -1;
1578 bb = ENTRY_BLOCK_PTR;
1579 while (1)
1581 edge e, next;
1583 for (e = bb->succ; e ; e = next)
1585 next = e->succ_next;
1586 if (e->insns)
1587 commit_one_edge_insertion (e);
1590 if (++i >= n_basic_blocks)
1591 break;
1592 bb = BASIC_BLOCK (i);
1596 /* Delete all unreachable basic blocks. */
1598 static void
1599 delete_unreachable_blocks ()
1601 basic_block *worklist, *tos;
1602 int deleted_handler;
1603 edge e;
1604 int i, n;
1606 n = n_basic_blocks;
1607 tos = worklist = (basic_block *) alloca (sizeof (basic_block) * n);
1609 /* Use basic_block->aux as a marker. Clear them all. */
1611 for (i = 0; i < n; ++i)
1612 BASIC_BLOCK (i)->aux = NULL;
1614 /* Add our starting points to the worklist. Almost always there will
1615 be only one. It isn't inconcievable that we might one day directly
1616 support Fortran alternate entry points. */
1618 for (e = ENTRY_BLOCK_PTR->succ; e ; e = e->succ_next)
1620 *tos++ = e->dest;
1622 /* Mark the block with a handy non-null value. */
1623 e->dest->aux = e;
1626 /* Iterate: find everything reachable from what we've already seen. */
1628 while (tos != worklist)
1630 basic_block b = *--tos;
1632 for (e = b->succ; e ; e = e->succ_next)
1633 if (!e->dest->aux)
1635 *tos++ = e->dest;
1636 e->dest->aux = e;
1640 /* Delete all unreachable basic blocks. Count down so that we don't
1641 interfere with the block renumbering that happens in delete_block. */
1643 deleted_handler = 0;
1645 for (i = n - 1; i >= 0; --i)
1647 basic_block b = BASIC_BLOCK (i);
1649 if (b->aux != NULL)
1650 /* This block was found. Tidy up the mark. */
1651 b->aux = NULL;
1652 else
1653 deleted_handler |= delete_block (b);
1656 /* Fix up edges that now fall through, or rather should now fall through
1657 but previously required a jump around now deleted blocks. Simplify
1658 the search by only examining blocks numerically adjacent, since this
1659 is how find_basic_blocks created them. */
1661 for (i = 1; i < n_basic_blocks; ++i)
1663 basic_block b = BASIC_BLOCK (i - 1);
1664 basic_block c = BASIC_BLOCK (i);
1665 edge s;
1667 /* We care about simple conditional or unconditional jumps with
1668 a single successor.
1670 If we had a conditional branch to the next instruction when
1671 find_basic_blocks was called, then there will only be one
1672 out edge for the block which ended with the conditional
1673 branch (since we do not create duplicate edges).
1675 Furthermore, the edge will be marked as a fallthru because we
1676 merge the flags for the duplicate edges. So we do not want to
1677 check that the edge is not a FALLTHRU edge. */
1678 if ((s = b->succ) != NULL
1679 && s->succ_next == NULL
1680 && s->dest == c
1681 /* If the jump insn has side effects, we can't tidy the edge. */
1682 && (GET_CODE (b->end) != JUMP_INSN
1683 || onlyjump_p (b->end)))
1684 tidy_fallthru_edge (s, b, c);
1687 /* If we deleted an exception handler, we may have EH region begin/end
1688 blocks to remove as well. */
1689 if (deleted_handler)
1690 delete_eh_regions ();
1693 /* Find EH regions for which there is no longer a handler, and delete them. */
1695 static void
1696 delete_eh_regions ()
1698 rtx insn;
1700 update_rethrow_references ();
1702 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1703 if (GET_CODE (insn) == NOTE)
1705 if ((NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG) ||
1706 (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END))
1708 int num = NOTE_EH_HANDLER (insn);
1709 /* A NULL handler indicates a region is no longer needed,
1710 as long as it isn't the target of a rethrow. */
1711 if (get_first_handler (num) == NULL && ! rethrow_used (num))
1713 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1714 NOTE_SOURCE_FILE (insn) = 0;
1720 /* Return true if NOTE is not one of the ones that must be kept paired,
1721 so that we may simply delete them. */
1723 static int
1724 can_delete_note_p (note)
1725 rtx note;
1727 return (NOTE_LINE_NUMBER (note) == NOTE_INSN_DELETED
1728 || NOTE_LINE_NUMBER (note) == NOTE_INSN_BASIC_BLOCK);
1731 /* Unlink a chain of insns between START and FINISH, leaving notes
1732 that must be paired. */
1734 static void
1735 flow_delete_insn_chain (start, finish)
1736 rtx start, finish;
1738 /* Unchain the insns one by one. It would be quicker to delete all
1739 of these with a single unchaining, rather than one at a time, but
1740 we need to keep the NOTE's. */
1742 rtx next;
1744 while (1)
1746 next = NEXT_INSN (start);
1747 if (GET_CODE (start) == NOTE && !can_delete_note_p (start))
1749 else if (GET_CODE (start) == CODE_LABEL && !can_delete_label_p (start))
1751 else
1752 next = flow_delete_insn (start);
1754 if (start == finish)
1755 break;
1756 start = next;
1760 /* Delete the insns in a (non-live) block. We physically delete every
1761 non-deleted-note insn, and update the flow graph appropriately.
1763 Return nonzero if we deleted an exception handler. */
1765 /* ??? Preserving all such notes strikes me as wrong. It would be nice
1766 to post-process the stream to remove empty blocks, loops, ranges, etc. */
1768 static int
1769 delete_block (b)
1770 basic_block b;
1772 int deleted_handler = 0;
1773 rtx insn, end;
1775 /* If the head of this block is a CODE_LABEL, then it might be the
1776 label for an exception handler which can't be reached.
1778 We need to remove the label from the exception_handler_label list
1779 and remove the associated NOTE_INSN_EH_REGION_BEG and
1780 NOTE_INSN_EH_REGION_END notes. */
1782 insn = b->head;
1784 never_reached_warning (insn);
1786 if (GET_CODE (insn) == CODE_LABEL)
1788 rtx x, *prev = &exception_handler_labels;
1790 for (x = exception_handler_labels; x; x = XEXP (x, 1))
1792 if (XEXP (x, 0) == insn)
1794 /* Found a match, splice this label out of the EH label list. */
1795 *prev = XEXP (x, 1);
1796 XEXP (x, 1) = NULL_RTX;
1797 XEXP (x, 0) = NULL_RTX;
1799 /* Remove the handler from all regions */
1800 remove_handler (insn);
1801 deleted_handler = 1;
1802 break;
1804 prev = &XEXP (x, 1);
1807 /* This label may be referenced by code solely for its value, or
1808 referenced by static data, or something. We have determined
1809 that it is not reachable, but cannot delete the label itself.
1810 Save code space and continue to delete the balance of the block,
1811 along with properly updating the cfg. */
1812 if (!can_delete_label_p (insn))
1814 /* If we've only got one of these, skip the whole deleting
1815 insns thing. */
1816 if (insn == b->end)
1817 goto no_delete_insns;
1818 insn = NEXT_INSN (insn);
1822 /* Selectively unlink the insn chain. Include any BARRIER that may
1823 follow the basic block. */
1824 end = next_nonnote_insn (b->end);
1825 if (!end || GET_CODE (end) != BARRIER)
1826 end = b->end;
1827 flow_delete_insn_chain (insn, end);
1829 no_delete_insns:
1831 /* Remove the edges into and out of this block. Note that there may
1832 indeed be edges in, if we are removing an unreachable loop. */
1834 edge e, next, *q;
1836 for (e = b->pred; e ; e = next)
1838 for (q = &e->src->succ; *q != e; q = &(*q)->succ_next)
1839 continue;
1840 *q = e->succ_next;
1841 next = e->pred_next;
1842 free (e);
1844 for (e = b->succ; e ; e = next)
1846 for (q = &e->dest->pred; *q != e; q = &(*q)->pred_next)
1847 continue;
1848 *q = e->pred_next;
1849 next = e->succ_next;
1850 free (e);
1853 b->pred = NULL;
1854 b->succ = NULL;
1857 /* Remove the basic block from the array, and compact behind it. */
1858 expunge_block (b);
1860 return deleted_handler;
1863 /* Remove block B from the basic block array and compact behind it. */
1865 static void
1866 expunge_block (b)
1867 basic_block b;
1869 int i, n = n_basic_blocks;
1871 for (i = b->index; i + 1 < n; ++i)
1873 basic_block x = BASIC_BLOCK (i + 1);
1874 BASIC_BLOCK (i) = x;
1875 x->index = i;
1878 basic_block_info->num_elements--;
1879 n_basic_blocks--;
1882 /* Delete INSN by patching it out. Return the next insn. */
1884 static rtx
1885 flow_delete_insn (insn)
1886 rtx insn;
1888 rtx prev = PREV_INSN (insn);
1889 rtx next = NEXT_INSN (insn);
1891 PREV_INSN (insn) = NULL_RTX;
1892 NEXT_INSN (insn) = NULL_RTX;
1894 if (prev)
1895 NEXT_INSN (prev) = next;
1896 if (next)
1897 PREV_INSN (next) = prev;
1898 else
1899 set_last_insn (prev);
1901 if (GET_CODE (insn) == CODE_LABEL)
1902 remove_node_from_expr_list (insn, &nonlocal_goto_handler_labels);
1904 /* If deleting a jump, decrement the use count of the label. Deleting
1905 the label itself should happen in the normal course of block merging. */
1906 if (GET_CODE (insn) == JUMP_INSN && JUMP_LABEL (insn))
1907 LABEL_NUSES (JUMP_LABEL (insn))--;
1909 return next;
1912 /* True if a given label can be deleted. */
1914 static int
1915 can_delete_label_p (label)
1916 rtx label;
1918 rtx x;
1920 if (LABEL_PRESERVE_P (label))
1921 return 0;
1923 for (x = forced_labels; x ; x = XEXP (x, 1))
1924 if (label == XEXP (x, 0))
1925 return 0;
1926 for (x = label_value_list; x ; x = XEXP (x, 1))
1927 if (label == XEXP (x, 0))
1928 return 0;
1929 for (x = exception_handler_labels; x ; x = XEXP (x, 1))
1930 if (label == XEXP (x, 0))
1931 return 0;
1933 /* User declared labels must be preserved. */
1934 if (LABEL_NAME (label) != 0)
1935 return 0;
1937 return 1;
1940 /* Blocks A and B are to be merged into a single block. A has no incoming
1941 fallthru edge, so it can be moved before B without adding or modifying
1942 any jumps (aside from the jump from A to B). */
1944 static int
1945 merge_blocks_move_predecessor_nojumps (a, b)
1946 basic_block a, b;
1948 rtx start, end, insertpoint, barrier;
1950 start = a->head;
1951 end = a->end;
1952 insertpoint = PREV_INSN (b->head);
1954 /* We want to delete the BARRIER after the end of the insns we are
1955 going to move. If we don't find a BARRIER, then do nothing. This
1956 can happen in some cases if we have labels we can not delete.
1958 Similarly, do nothing if we can not delete the label at the start
1959 of the target block. */
1960 barrier = next_nonnote_insn (end);
1961 if (GET_CODE (barrier) != BARRIER
1962 || (GET_CODE (b->head) == CODE_LABEL
1963 && ! can_delete_label_p (b->head)))
1964 return 0;
1965 else
1966 flow_delete_insn (barrier);
1968 /* Move block and loop notes out of the chain so that we do not
1969 disturb their order.
1971 ??? A better solution would be to squeeze out all the non-nested notes
1972 and adjust the block trees appropriately. Even better would be to have
1973 a tighter connection between block trees and rtl so that this is not
1974 necessary. */
1975 start = squeeze_notes (start, end);
1977 /* Scramble the insn chain. */
1978 reorder_insns (start, end, insertpoint);
1980 /* Now blocks A and B are contiguous. Merge them. */
1981 merge_blocks_nomove (a, b);
1983 if (rtl_dump_file)
1985 fprintf (rtl_dump_file, "Moved block %d before %d and merged.\n",
1986 a->index, b->index);
1989 return 1;
1992 /* Blocks A and B are to be merged into a single block. B has no outgoing
1993 fallthru edge, so it can be moved after A without adding or modifying
1994 any jumps (aside from the jump from A to B). */
1996 static int
1997 merge_blocks_move_successor_nojumps (a, b)
1998 basic_block a, b;
2000 rtx start, end, insertpoint, barrier;
2002 start = b->head;
2003 end = b->end;
2004 insertpoint = a->end;
2006 /* We want to delete the BARRIER after the end of the insns we are
2007 going to move. If we don't find a BARRIER, then do nothing. This
2008 can happen in some cases if we have labels we can not delete.
2010 Similarly, do nothing if we can not delete the label at the start
2011 of the target block. */
2012 barrier = next_nonnote_insn (end);
2013 if (GET_CODE (barrier) != BARRIER
2014 || (GET_CODE (b->head) == CODE_LABEL
2015 && ! can_delete_label_p (b->head)))
2016 return 0;
2017 else
2018 flow_delete_insn (barrier);
2020 /* Move block and loop notes out of the chain so that we do not
2021 disturb their order.
2023 ??? A better solution would be to squeeze out all the non-nested notes
2024 and adjust the block trees appropriately. Even better would be to have
2025 a tighter connection between block trees and rtl so that this is not
2026 necessary. */
2027 start = squeeze_notes (start, end);
2029 /* Scramble the insn chain. */
2030 reorder_insns (start, end, insertpoint);
2032 /* Now blocks A and B are contiguous. Merge them. */
2033 merge_blocks_nomove (a, b);
2035 if (rtl_dump_file)
2037 fprintf (rtl_dump_file, "Moved block %d after %d and merged.\n",
2038 b->index, a->index);
2041 return 1;
2044 /* Blocks A and B are to be merged into a single block. The insns
2045 are already contiguous, hence `nomove'. */
2047 static void
2048 merge_blocks_nomove (a, b)
2049 basic_block a, b;
2051 edge e;
2052 rtx b_head, b_end, a_end;
2053 int b_empty = 0;
2055 /* If there was a CODE_LABEL beginning B, delete it. */
2056 b_head = b->head;
2057 b_end = b->end;
2058 if (GET_CODE (b_head) == CODE_LABEL)
2060 /* Detect basic blocks with nothing but a label. This can happen
2061 in particular at the end of a function. */
2062 if (b_head == b_end)
2063 b_empty = 1;
2064 b_head = flow_delete_insn (b_head);
2067 /* Delete the basic block note. */
2068 if (GET_CODE (b_head) == NOTE
2069 && NOTE_LINE_NUMBER (b_head) == NOTE_INSN_BASIC_BLOCK)
2071 if (b_head == b_end)
2072 b_empty = 1;
2073 b_head = flow_delete_insn (b_head);
2076 /* If there was a jump out of A, delete it. */
2077 a_end = a->end;
2078 if (GET_CODE (a_end) == JUMP_INSN)
2080 rtx prev;
2082 prev = prev_nonnote_insn (a_end);
2083 if (!prev)
2084 prev = a->head;
2086 #ifdef HAVE_cc0
2087 /* If this was a conditional jump, we need to also delete
2088 the insn that set cc0. */
2090 if (prev && sets_cc0_p (prev))
2092 rtx tmp = prev;
2093 prev = prev_nonnote_insn (prev);
2094 if (!prev)
2095 prev = a->head;
2096 flow_delete_insn (tmp);
2098 #endif
2100 /* Note that a->head != a->end, since we should have at least a
2101 bb note plus the jump, so prev != insn. */
2102 flow_delete_insn (a_end);
2103 a_end = prev;
2106 /* By definition, there should only be one successor of A, and that is
2107 B. Free that edge struct. */
2108 free (a->succ);
2110 /* Adjust the edges out of B for the new owner. */
2111 for (e = b->succ; e ; e = e->succ_next)
2112 e->src = a;
2113 a->succ = b->succ;
2115 /* Reassociate the insns of B with A. */
2116 if (!b_empty)
2118 BLOCK_FOR_INSN (b_head) = a;
2119 while (b_head != b_end)
2121 b_head = NEXT_INSN (b_head);
2122 BLOCK_FOR_INSN (b_head) = a;
2124 a_end = b_head;
2126 a->end = a_end;
2128 /* Compact the basic block array. */
2129 expunge_block (b);
2132 /* Attempt to merge basic blocks that are potentially non-adjacent.
2133 Return true iff the attempt succeeded. */
2135 static int
2136 merge_blocks (e, b, c)
2137 edge e;
2138 basic_block b, c;
2140 /* If B has a fallthru edge to C, no need to move anything. */
2141 if (e->flags & EDGE_FALLTHRU)
2143 /* If a label still appears somewhere and we cannot delete the label,
2144 then we cannot merge the blocks. The edge was tidied already. */
2146 rtx insn, stop = NEXT_INSN (c->head);
2147 for (insn = NEXT_INSN (b->end); insn != stop; insn = NEXT_INSN (insn))
2148 if (GET_CODE (insn) == CODE_LABEL && !can_delete_label_p (insn))
2149 return 0;
2151 merge_blocks_nomove (b, c);
2153 if (rtl_dump_file)
2155 fprintf (rtl_dump_file, "Merged %d and %d without moving.\n",
2156 b->index, c->index);
2159 return 1;
2161 else
2163 edge tmp_edge;
2164 basic_block d;
2165 int c_has_outgoing_fallthru;
2166 int b_has_incoming_fallthru;
2168 /* We must make sure to not munge nesting of exception regions,
2169 lexical blocks, and loop notes.
2171 The first is taken care of by requiring that the active eh
2172 region at the end of one block always matches the active eh
2173 region at the beginning of the next block.
2175 The later two are taken care of by squeezing out all the notes. */
2177 /* ??? A throw/catch edge (or any abnormal edge) should be rarely
2178 executed and we may want to treat blocks which have two out
2179 edges, one normal, one abnormal as only having one edge for
2180 block merging purposes. */
2182 for (tmp_edge = c->succ; tmp_edge ; tmp_edge = tmp_edge->succ_next)
2183 if (tmp_edge->flags & EDGE_FALLTHRU)
2184 break;
2185 c_has_outgoing_fallthru = (tmp_edge != NULL);
2187 for (tmp_edge = b->pred; tmp_edge ; tmp_edge = tmp_edge->pred_next)
2188 if (tmp_edge->flags & EDGE_FALLTHRU)
2189 break;
2190 b_has_incoming_fallthru = (tmp_edge != NULL);
2192 /* If B does not have an incoming fallthru, and the exception regions
2193 match, then it can be moved immediately before C without introducing
2194 or modifying jumps. */
2195 d = BASIC_BLOCK (c->index - 1);
2196 if (! b_has_incoming_fallthru
2197 && d->eh_end == b->eh_beg
2198 && b->eh_end == c->eh_beg)
2199 return merge_blocks_move_predecessor_nojumps (b, c);
2201 /* Otherwise, we're going to try to move C after B. Make sure the
2202 exception regions match. */
2203 d = BASIC_BLOCK (b->index + 1);
2204 if (b->eh_end == c->eh_beg
2205 && c->eh_end == d->eh_beg)
2207 /* If C does not have an outgoing fallthru, then it can be moved
2208 immediately after B without introducing or modifying jumps. */
2209 if (! c_has_outgoing_fallthru)
2210 return merge_blocks_move_successor_nojumps (b, c);
2212 /* Otherwise, we'll need to insert an extra jump, and possibly
2213 a new block to contain it. */
2214 /* ??? Not implemented yet. */
2217 return 0;
2221 /* Top level driver for merge_blocks. */
2223 static void
2224 try_merge_blocks ()
2226 int i;
2228 /* Attempt to merge blocks as made possible by edge removal. If a block
2229 has only one successor, and the successor has only one predecessor,
2230 they may be combined. */
2232 for (i = 0; i < n_basic_blocks; )
2234 basic_block c, b = BASIC_BLOCK (i);
2235 edge s;
2237 /* A loop because chains of blocks might be combineable. */
2238 while ((s = b->succ) != NULL
2239 && s->succ_next == NULL
2240 && (s->flags & EDGE_EH) == 0
2241 && (c = s->dest) != EXIT_BLOCK_PTR
2242 && c->pred->pred_next == NULL
2243 /* If the jump insn has side effects, we can't kill the edge. */
2244 && (GET_CODE (b->end) != JUMP_INSN
2245 || onlyjump_p (b->end))
2246 && merge_blocks (s, b, c))
2247 continue;
2249 /* Don't get confused by the index shift caused by deleting blocks. */
2250 i = b->index + 1;
2254 /* The given edge should potentially a fallthru edge. If that is in
2255 fact true, delete the unconditional jump and barriers that are in
2256 the way. */
2258 static void
2259 tidy_fallthru_edge (e, b, c)
2260 edge e;
2261 basic_block b, c;
2263 rtx q;
2265 /* ??? In a late-running flow pass, other folks may have deleted basic
2266 blocks by nopping out blocks, leaving multiple BARRIERs between here
2267 and the target label. They ought to be chastized and fixed.
2269 We can also wind up with a sequence of undeletable labels between
2270 one block and the next.
2272 So search through a sequence of barriers, labels, and notes for
2273 the head of block C and assert that we really do fall through. */
2275 if (next_real_insn (b->end) != next_real_insn (PREV_INSN (c->head)))
2276 return;
2278 /* Remove what will soon cease being the jump insn from the source block.
2279 If block B consisted only of this single jump, turn it into a deleted
2280 note. */
2281 q = b->end;
2282 if (GET_CODE (q) == JUMP_INSN)
2284 #ifdef HAVE_cc0
2285 /* If this was a conditional jump, we need to also delete
2286 the insn that set cc0. */
2287 if (! simplejump_p (q) && condjump_p (q) && sets_cc0_p (PREV_INSN (q)))
2288 q = PREV_INSN (q);
2289 #endif
2291 if (b->head == q)
2293 PUT_CODE (q, NOTE);
2294 NOTE_LINE_NUMBER (q) = NOTE_INSN_DELETED;
2295 NOTE_SOURCE_FILE (q) = 0;
2297 else
2298 b->end = q = PREV_INSN (q);
2301 /* Selectively unlink the sequence. */
2302 if (q != PREV_INSN (c->head))
2303 flow_delete_insn_chain (NEXT_INSN (q), PREV_INSN (c->head));
2305 e->flags |= EDGE_FALLTHRU;
2308 /* Discover and record the loop depth at the head of each basic block. */
2310 static void
2311 calculate_loop_depth (insns)
2312 rtx insns;
2314 basic_block bb;
2315 rtx insn;
2316 int i = 0, depth = 1;
2318 bb = BASIC_BLOCK (i);
2319 for (insn = insns; insn ; insn = NEXT_INSN (insn))
2321 if (insn == bb->head)
2323 bb->loop_depth = depth;
2324 if (++i >= n_basic_blocks)
2325 break;
2326 bb = BASIC_BLOCK (i);
2329 if (GET_CODE (insn) == NOTE)
2331 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
2332 depth++;
2333 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
2334 depth--;
2336 /* If we have LOOP_DEPTH == 0, there has been a bookkeeping error. */
2337 if (depth == 0)
2338 abort ();
2343 /* Perform data flow analysis.
2344 F is the first insn of the function and NREGS the number of register numbers
2345 in use. */
2347 void
2348 life_analysis (f, nregs, file, remove_dead_code)
2349 rtx f;
2350 int nregs;
2351 FILE *file;
2352 int remove_dead_code;
2354 #ifdef ELIMINABLE_REGS
2355 register size_t i;
2356 static struct {int from, to; } eliminables[] = ELIMINABLE_REGS;
2357 #endif
2359 /* Record which registers will be eliminated. We use this in
2360 mark_used_regs. */
2362 CLEAR_HARD_REG_SET (elim_reg_set);
2364 #ifdef ELIMINABLE_REGS
2365 for (i = 0; i < sizeof eliminables / sizeof eliminables[0]; i++)
2366 SET_HARD_REG_BIT (elim_reg_set, eliminables[i].from);
2367 #else
2368 SET_HARD_REG_BIT (elim_reg_set, FRAME_POINTER_REGNUM);
2369 #endif
2371 /* Allocate a bitmap to be filled in by record_volatile_insns. */
2372 uid_volatile = BITMAP_ALLOCA ();
2374 /* We want alias analysis information for local dead store elimination. */
2375 init_alias_analysis ();
2377 life_analysis_1 (f, nregs, remove_dead_code);
2379 if (! reload_completed)
2380 mark_constant_function ();
2382 end_alias_analysis ();
2384 if (file)
2385 dump_flow_info (file);
2387 BITMAP_FREE (uid_volatile);
2388 free_basic_block_vars (1);
2391 /* Free the variables allocated by find_basic_blocks.
2393 KEEP_HEAD_END_P is non-zero if basic_block_info is not to be freed. */
2395 void
2396 free_basic_block_vars (keep_head_end_p)
2397 int keep_head_end_p;
2399 if (basic_block_for_insn)
2401 VARRAY_FREE (basic_block_for_insn);
2402 basic_block_for_insn = NULL;
2405 if (! keep_head_end_p)
2407 clear_edges ();
2408 VARRAY_FREE (basic_block_info);
2409 n_basic_blocks = 0;
2411 ENTRY_BLOCK_PTR->aux = NULL;
2412 ENTRY_BLOCK_PTR->global_live_at_end = NULL;
2413 EXIT_BLOCK_PTR->aux = NULL;
2414 EXIT_BLOCK_PTR->global_live_at_start = NULL;
2418 /* Return nonzero if the destination of SET equals the source. */
2419 static int
2420 set_noop_p (set)
2421 rtx set;
2423 rtx src = SET_SRC (set);
2424 rtx dst = SET_DEST (set);
2425 if (GET_CODE (src) == REG && GET_CODE (dst) == REG
2426 && REGNO (src) == REGNO (dst))
2427 return 1;
2428 if (GET_CODE (src) != SUBREG || GET_CODE (dst) != SUBREG
2429 || SUBREG_WORD (src) != SUBREG_WORD (dst))
2430 return 0;
2431 src = SUBREG_REG (src);
2432 dst = SUBREG_REG (dst);
2433 if (GET_CODE (src) == REG && GET_CODE (dst) == REG
2434 && REGNO (src) == REGNO (dst))
2435 return 1;
2436 return 0;
2439 /* Return nonzero if an insn consists only of SETs, each of which only sets a
2440 value to itself. */
2441 static int
2442 noop_move_p (insn)
2443 rtx insn;
2445 rtx pat = PATTERN (insn);
2447 /* Insns carrying these notes are useful later on. */
2448 if (find_reg_note (insn, REG_EQUAL, NULL_RTX))
2449 return 0;
2451 if (GET_CODE (pat) == SET && set_noop_p (pat))
2452 return 1;
2454 if (GET_CODE (pat) == PARALLEL)
2456 int i;
2457 /* If nothing but SETs of registers to themselves,
2458 this insn can also be deleted. */
2459 for (i = 0; i < XVECLEN (pat, 0); i++)
2461 rtx tem = XVECEXP (pat, 0, i);
2463 if (GET_CODE (tem) == USE
2464 || GET_CODE (tem) == CLOBBER)
2465 continue;
2467 if (GET_CODE (tem) != SET || ! set_noop_p (tem))
2468 return 0;
2471 return 1;
2473 return 0;
2476 static void
2477 notice_stack_pointer_modification (x, pat)
2478 rtx x;
2479 rtx pat ATTRIBUTE_UNUSED;
2481 if (x == stack_pointer_rtx
2482 /* The stack pointer is only modified indirectly as the result
2483 of a push until later in flow. See the comments in rtl.texi
2484 regarding Embedded Side-Effects on Addresses. */
2485 || (GET_CODE (x) == MEM
2486 && (GET_CODE (XEXP (x, 0)) == PRE_DEC
2487 || GET_CODE (XEXP (x, 0)) == PRE_INC
2488 || GET_CODE (XEXP (x, 0)) == POST_DEC
2489 || GET_CODE (XEXP (x, 0)) == POST_INC)
2490 && XEXP (XEXP (x, 0), 0) == stack_pointer_rtx))
2491 current_function_sp_is_unchanging = 0;
2494 /* Record which insns refer to any volatile memory
2495 or for any reason can't be deleted just because they are dead stores.
2496 Also, delete any insns that copy a register to itself.
2497 And see if the stack pointer is modified. */
2498 static void
2499 record_volatile_insns (f)
2500 rtx f;
2502 rtx insn;
2503 for (insn = f; insn; insn = NEXT_INSN (insn))
2505 enum rtx_code code1 = GET_CODE (insn);
2506 if (code1 == CALL_INSN)
2507 SET_INSN_VOLATILE (insn);
2508 else if (code1 == INSN || code1 == JUMP_INSN)
2510 if (GET_CODE (PATTERN (insn)) != USE
2511 && volatile_refs_p (PATTERN (insn)))
2512 SET_INSN_VOLATILE (insn);
2514 /* A SET that makes space on the stack cannot be dead.
2515 (Such SETs occur only for allocating variable-size data,
2516 so they will always have a PLUS or MINUS according to the
2517 direction of stack growth.)
2518 Even if this function never uses this stack pointer value,
2519 signal handlers do! */
2520 else if (code1 == INSN && GET_CODE (PATTERN (insn)) == SET
2521 && SET_DEST (PATTERN (insn)) == stack_pointer_rtx
2522 #ifdef STACK_GROWS_DOWNWARD
2523 && GET_CODE (SET_SRC (PATTERN (insn))) == MINUS
2524 #else
2525 && GET_CODE (SET_SRC (PATTERN (insn))) == PLUS
2526 #endif
2527 && XEXP (SET_SRC (PATTERN (insn)), 0) == stack_pointer_rtx)
2528 SET_INSN_VOLATILE (insn);
2530 /* Delete (in effect) any obvious no-op moves. */
2531 else if (noop_move_p (insn))
2533 PUT_CODE (insn, NOTE);
2534 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2535 NOTE_SOURCE_FILE (insn) = 0;
2539 /* Check if insn modifies the stack pointer. */
2540 if ( current_function_sp_is_unchanging
2541 && GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2542 note_stores (PATTERN (insn), notice_stack_pointer_modification);
2546 /* Mark those regs which are needed at the end of the function as live
2547 at the end of the last basic block. */
2548 static void
2549 mark_regs_live_at_end (set)
2550 regset set;
2552 int i;
2554 /* If exiting needs the right stack value, consider the stack pointer
2555 live at the end of the function. */
2556 if (! EXIT_IGNORE_STACK
2557 || (! FRAME_POINTER_REQUIRED
2558 && ! current_function_calls_alloca
2559 && flag_omit_frame_pointer)
2560 || current_function_sp_is_unchanging)
2562 SET_REGNO_REG_SET (set, STACK_POINTER_REGNUM);
2565 /* Mark the frame pointer if needed at the end of the function. If
2566 we end up eliminating it, it will be removed from the live list
2567 of each basic block by reload. */
2569 if (! reload_completed || frame_pointer_needed)
2571 SET_REGNO_REG_SET (set, FRAME_POINTER_REGNUM);
2572 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2573 /* If they are different, also mark the hard frame pointer as live */
2574 SET_REGNO_REG_SET (set, HARD_FRAME_POINTER_REGNUM);
2575 #endif
2578 /* Mark all global registers, and all registers used by the epilogue
2579 as being live at the end of the function since they may be
2580 referenced by our caller. */
2581 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2582 if (global_regs[i]
2583 #ifdef EPILOGUE_USES
2584 || EPILOGUE_USES (i)
2585 #endif
2587 SET_REGNO_REG_SET (set, i);
2589 /* ??? Mark function return value here rather than as uses. */
2592 /* Determine which registers are live at the start of each
2593 basic block of the function whose first insn is F.
2594 NREGS is the number of registers used in F.
2595 We allocate the vector basic_block_live_at_start
2596 and the regsets that it points to, and fill them with the data.
2597 regset_size and regset_bytes are also set here. */
2599 static void
2600 life_analysis_1 (f, nregs, remove_dead_code)
2601 rtx f;
2602 int nregs;
2603 int remove_dead_code;
2605 int first_pass;
2606 int changed;
2607 register int i;
2608 char save_regs_ever_live[FIRST_PSEUDO_REGISTER];
2609 regset *new_live_at_end;
2611 struct obstack flow_obstack;
2613 gcc_obstack_init (&flow_obstack);
2615 max_regno = nregs;
2617 /* Allocate and zero out many data structures
2618 that will record the data from lifetime analysis. */
2620 allocate_reg_life_data ();
2621 allocate_bb_life_data ();
2623 reg_next_use = (rtx *) alloca (nregs * sizeof (rtx));
2624 memset (reg_next_use, 0, nregs * sizeof (rtx));
2626 /* Set up regset-vectors used internally within this function.
2627 Their meanings are documented above, with their declarations. */
2629 new_live_at_end = (regset *) alloca ((n_basic_blocks + 1) * sizeof (regset));
2630 init_regset_vector (new_live_at_end, n_basic_blocks + 1, &flow_obstack);
2632 /* Stick these vectors into the AUX field of the basic block, so that
2633 we don't have to keep going through the index. */
2635 for (i = 0; i < n_basic_blocks; ++i)
2636 BASIC_BLOCK (i)->aux = new_live_at_end[i];
2637 ENTRY_BLOCK_PTR->aux = new_live_at_end[i];
2639 /* Assume that the stack pointer is unchanging if alloca hasn't been used.
2640 This will be cleared by record_volatile_insns if it encounters an insn
2641 which modifies the stack pointer. */
2642 current_function_sp_is_unchanging = !current_function_calls_alloca;
2644 record_volatile_insns (f);
2646 if (n_basic_blocks > 0)
2648 regset theend;
2649 register edge e;
2651 theend = EXIT_BLOCK_PTR->global_live_at_start;
2652 mark_regs_live_at_end (theend);
2654 /* Propogate this exit data to each of EXIT's predecessors. */
2655 for (e = EXIT_BLOCK_PTR->pred; e ; e = e->pred_next)
2657 COPY_REG_SET (e->src->global_live_at_end, theend);
2658 COPY_REG_SET ((regset) e->src->aux, theend);
2662 /* The post-reload life analysis have (on a global basis) the same registers
2663 live as was computed by reload itself.
2665 Otherwise elimination offsets and such may be incorrect.
2667 Reload will make some registers as live even though they do not appear
2668 in the rtl. */
2669 if (reload_completed)
2670 memcpy (save_regs_ever_live, regs_ever_live, sizeof (regs_ever_live));
2671 memset (regs_ever_live, 0, sizeof regs_ever_live);
2673 /* Propagate life info through the basic blocks
2674 around the graph of basic blocks.
2676 This is a relaxation process: each time a new register
2677 is live at the end of the basic block, we must scan the block
2678 to determine which registers are, as a consequence, live at the beginning
2679 of that block. These registers must then be marked live at the ends
2680 of all the blocks that can transfer control to that block.
2681 The process continues until it reaches a fixed point. */
2683 first_pass = 1;
2684 changed = 1;
2685 while (changed)
2687 changed = 0;
2688 for (i = n_basic_blocks - 1; i >= 0; i--)
2690 basic_block bb = BASIC_BLOCK (i);
2691 int consider = first_pass;
2692 int must_rescan = first_pass;
2693 register int j;
2695 if (!first_pass)
2697 /* Set CONSIDER if this block needs thinking about at all
2698 (that is, if the regs live now at the end of it
2699 are not the same as were live at the end of it when
2700 we last thought about it).
2701 Set must_rescan if it needs to be thought about
2702 instruction by instruction (that is, if any additional
2703 reg that is live at the end now but was not live there before
2704 is one of the significant regs of this basic block). */
2706 EXECUTE_IF_AND_COMPL_IN_REG_SET
2707 ((regset) bb->aux, bb->global_live_at_end, 0, j,
2709 consider = 1;
2710 if (REGNO_REG_SET_P (bb->local_set, j))
2712 must_rescan = 1;
2713 goto done;
2716 done:
2717 if (! consider)
2718 continue;
2721 /* The live_at_start of this block may be changing,
2722 so another pass will be required after this one. */
2723 changed = 1;
2725 if (! must_rescan)
2727 /* No complete rescan needed;
2728 just record those variables newly known live at end
2729 as live at start as well. */
2730 IOR_AND_COMPL_REG_SET (bb->global_live_at_start,
2731 (regset) bb->aux,
2732 bb->global_live_at_end);
2734 IOR_AND_COMPL_REG_SET (bb->global_live_at_end,
2735 (regset) bb->aux,
2736 bb->global_live_at_end);
2738 else
2740 /* Update the basic_block_live_at_start
2741 by propagation backwards through the block. */
2742 COPY_REG_SET (bb->global_live_at_end, (regset) bb->aux);
2743 COPY_REG_SET (bb->global_live_at_start,
2744 bb->global_live_at_end);
2745 propagate_block (bb->global_live_at_start,
2746 bb->head, bb->end, 0,
2747 first_pass ? bb->local_set : (regset) 0,
2748 i, remove_dead_code);
2751 /* Update the new_live_at_end's of the block's predecessors. */
2753 register edge e;
2755 for (e = bb->pred; e ; e = e->pred_next)
2756 IOR_REG_SET ((regset) e->src->aux, bb->global_live_at_start);
2759 #ifdef USE_C_ALLOCA
2760 alloca (0);
2761 #endif
2763 first_pass = 0;
2766 /* The only pseudos that are live at the beginning of the function are
2767 those that were not set anywhere in the function. local-alloc doesn't
2768 know how to handle these correctly, so mark them as not local to any
2769 one basic block. */
2771 if (n_basic_blocks > 0)
2772 EXECUTE_IF_SET_IN_REG_SET (BASIC_BLOCK (0)->global_live_at_start,
2773 FIRST_PSEUDO_REGISTER, i,
2775 REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL;
2778 /* Now the life information is accurate. Make one more pass over each
2779 basic block to delete dead stores, create autoincrement addressing
2780 and record how many times each register is used, is set, or dies. */
2782 for (i = 0; i < n_basic_blocks; i++)
2784 basic_block bb = BASIC_BLOCK (i);
2786 /* We start with global_live_at_end to determine which stores are
2787 dead. This process is destructive, and we wish to preserve the
2788 contents of global_live_at_end for posterity. Fortunately,
2789 new_live_at_end, due to the way we converged on a solution,
2790 contains a duplicate of global_live_at_end that we can kill. */
2791 propagate_block ((regset) bb->aux, bb->head, bb->end, 1, (regset) 0, i, remove_dead_code);
2793 #ifdef USE_C_ALLOCA
2794 alloca (0);
2795 #endif
2798 /* We have a problem with any pseudoreg that lives across the setjmp.
2799 ANSI says that if a user variable does not change in value between
2800 the setjmp and the longjmp, then the longjmp preserves it. This
2801 includes longjmp from a place where the pseudo appears dead.
2802 (In principle, the value still exists if it is in scope.)
2803 If the pseudo goes in a hard reg, some other value may occupy
2804 that hard reg where this pseudo is dead, thus clobbering the pseudo.
2805 Conclusion: such a pseudo must not go in a hard reg. */
2806 EXECUTE_IF_SET_IN_REG_SET (regs_live_at_setjmp,
2807 FIRST_PSEUDO_REGISTER, i,
2809 if (regno_reg_rtx[i] != 0)
2811 REG_LIVE_LENGTH (i) = -1;
2812 REG_BASIC_BLOCK (i) = -1;
2816 /* Restore regs_ever_live that was provided by reload. */
2817 if (reload_completed)
2818 memcpy (regs_ever_live, save_regs_ever_live, sizeof (regs_ever_live));
2820 free_regset_vector (new_live_at_end, n_basic_blocks);
2821 obstack_free (&flow_obstack, NULL_PTR);
2823 for (i = 0; i < n_basic_blocks; ++i)
2824 BASIC_BLOCK (i)->aux = NULL;
2825 ENTRY_BLOCK_PTR->aux = NULL;
2828 /* Subroutines of life analysis. */
2830 /* Allocate the permanent data structures that represent the results
2831 of life analysis. Not static since used also for stupid life analysis. */
2833 void
2834 allocate_bb_life_data ()
2836 register int i;
2838 for (i = 0; i < n_basic_blocks; i++)
2840 basic_block bb = BASIC_BLOCK (i);
2842 bb->local_set = OBSTACK_ALLOC_REG_SET (function_obstack);
2843 bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (function_obstack);
2844 bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (function_obstack);
2847 ENTRY_BLOCK_PTR->global_live_at_end
2848 = OBSTACK_ALLOC_REG_SET (function_obstack);
2849 EXIT_BLOCK_PTR->global_live_at_start
2850 = OBSTACK_ALLOC_REG_SET (function_obstack);
2852 regs_live_at_setjmp = OBSTACK_ALLOC_REG_SET (function_obstack);
2855 void
2856 allocate_reg_life_data ()
2858 int i;
2860 /* Recalculate the register space, in case it has grown. Old style
2861 vector oriented regsets would set regset_{size,bytes} here also. */
2862 allocate_reg_info (max_regno, FALSE, FALSE);
2864 /* Because both reg_scan and flow_analysis want to set up the REG_N_SETS
2865 information, explicitly reset it here. The allocation should have
2866 already happened on the previous reg_scan pass. Make sure in case
2867 some more registers were allocated. */
2868 for (i = 0; i < max_regno; i++)
2869 REG_N_SETS (i) = 0;
2872 /* Make each element of VECTOR point at a regset. The vector has
2873 NELTS elements, and space is allocated from the ALLOC_OBSTACK
2874 obstack. */
2876 static void
2877 init_regset_vector (vector, nelts, alloc_obstack)
2878 regset *vector;
2879 int nelts;
2880 struct obstack *alloc_obstack;
2882 register int i;
2884 for (i = 0; i < nelts; i++)
2886 vector[i] = OBSTACK_ALLOC_REG_SET (alloc_obstack);
2887 CLEAR_REG_SET (vector[i]);
2891 /* Release any additional space allocated for each element of VECTOR point
2892 other than the regset header itself. The vector has NELTS elements. */
2894 void
2895 free_regset_vector (vector, nelts)
2896 regset *vector;
2897 int nelts;
2899 register int i;
2901 for (i = 0; i < nelts; i++)
2902 FREE_REG_SET (vector[i]);
2905 /* Compute the registers live at the beginning of a basic block
2906 from those live at the end.
2908 When called, OLD contains those live at the end.
2909 On return, it contains those live at the beginning.
2910 FIRST and LAST are the first and last insns of the basic block.
2912 FINAL is nonzero if we are doing the final pass which is not
2913 for computing the life info (since that has already been done)
2914 but for acting on it. On this pass, we delete dead stores,
2915 set up the logical links and dead-variables lists of instructions,
2916 and merge instructions for autoincrement and autodecrement addresses.
2918 SIGNIFICANT is nonzero only the first time for each basic block.
2919 If it is nonzero, it points to a regset in which we store
2920 a 1 for each register that is set within the block.
2922 BNUM is the number of the basic block. */
2924 static void
2925 propagate_block (old, first, last, final, significant, bnum, remove_dead_code)
2926 register regset old;
2927 rtx first;
2928 rtx last;
2929 int final;
2930 regset significant;
2931 int bnum;
2932 int remove_dead_code;
2934 register rtx insn;
2935 rtx prev;
2936 regset live;
2937 regset dead;
2939 /* Find the loop depth for this block. Ignore loop level changes in the
2940 middle of the basic block -- for register allocation purposes, the
2941 important uses will be in the blocks wholely contained within the loop
2942 not in the loop pre-header or post-trailer. */
2943 loop_depth = BASIC_BLOCK (bnum)->loop_depth;
2945 dead = ALLOCA_REG_SET ();
2946 live = ALLOCA_REG_SET ();
2948 cc0_live = 0;
2949 mem_set_list = NULL_RTX;
2951 if (final)
2953 register int i;
2955 /* Process the regs live at the end of the block.
2956 Mark them as not local to any one basic block. */
2957 EXECUTE_IF_SET_IN_REG_SET (old, 0, i,
2959 REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL;
2963 /* Scan the block an insn at a time from end to beginning. */
2965 for (insn = last; ; insn = prev)
2967 prev = PREV_INSN (insn);
2969 if (GET_CODE (insn) == NOTE)
2971 /* If this is a call to `setjmp' et al,
2972 warn if any non-volatile datum is live. */
2974 if (final && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
2975 IOR_REG_SET (regs_live_at_setjmp, old);
2978 /* Update the life-status of regs for this insn.
2979 First DEAD gets which regs are set in this insn
2980 then LIVE gets which regs are used in this insn.
2981 Then the regs live before the insn
2982 are those live after, with DEAD regs turned off,
2983 and then LIVE regs turned on. */
2985 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2987 register int i;
2988 rtx note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
2989 int insn_is_dead = 0;
2990 int libcall_is_dead = 0;
2992 if (remove_dead_code)
2994 insn_is_dead = (insn_dead_p (PATTERN (insn), old, 0, REG_NOTES (insn))
2995 /* Don't delete something that refers to volatile storage! */
2996 && ! INSN_VOLATILE (insn));
2997 libcall_is_dead = (insn_is_dead && note != 0
2998 && libcall_dead_p (PATTERN (insn), old, note, insn));
3001 /* If an instruction consists of just dead store(s) on final pass,
3002 "delete" it by turning it into a NOTE of type NOTE_INSN_DELETED.
3003 We could really delete it with delete_insn, but that
3004 can cause trouble for first or last insn in a basic block. */
3005 if (final && insn_is_dead)
3007 PUT_CODE (insn, NOTE);
3008 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
3009 NOTE_SOURCE_FILE (insn) = 0;
3011 /* CC0 is now known to be dead. Either this insn used it,
3012 in which case it doesn't anymore, or clobbered it,
3013 so the next insn can't use it. */
3014 cc0_live = 0;
3016 /* If this insn is copying the return value from a library call,
3017 delete the entire library call. */
3018 if (libcall_is_dead)
3020 rtx first = XEXP (note, 0);
3021 rtx p = insn;
3022 while (INSN_DELETED_P (first))
3023 first = NEXT_INSN (first);
3024 while (p != first)
3026 p = PREV_INSN (p);
3027 PUT_CODE (p, NOTE);
3028 NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
3029 NOTE_SOURCE_FILE (p) = 0;
3032 goto flushed;
3035 CLEAR_REG_SET (dead);
3036 CLEAR_REG_SET (live);
3038 /* See if this is an increment or decrement that can be
3039 merged into a following memory address. */
3040 #ifdef AUTO_INC_DEC
3042 register rtx x = single_set (insn);
3044 /* Does this instruction increment or decrement a register? */
3045 if (!reload_completed
3046 && final && x != 0
3047 && GET_CODE (SET_DEST (x)) == REG
3048 && (GET_CODE (SET_SRC (x)) == PLUS
3049 || GET_CODE (SET_SRC (x)) == MINUS)
3050 && XEXP (SET_SRC (x), 0) == SET_DEST (x)
3051 && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3052 /* Ok, look for a following memory ref we can combine with.
3053 If one is found, change the memory ref to a PRE_INC
3054 or PRE_DEC, cancel this insn, and return 1.
3055 Return 0 if nothing has been done. */
3056 && try_pre_increment_1 (insn))
3057 goto flushed;
3059 #endif /* AUTO_INC_DEC */
3061 /* If this is not the final pass, and this insn is copying the
3062 value of a library call and it's dead, don't scan the
3063 insns that perform the library call, so that the call's
3064 arguments are not marked live. */
3065 if (libcall_is_dead)
3067 /* Mark the dest reg as `significant'. */
3068 mark_set_regs (old, dead, PATTERN (insn), NULL_RTX, significant);
3070 insn = XEXP (note, 0);
3071 prev = PREV_INSN (insn);
3073 else if (GET_CODE (PATTERN (insn)) == SET
3074 && SET_DEST (PATTERN (insn)) == stack_pointer_rtx
3075 && GET_CODE (SET_SRC (PATTERN (insn))) == PLUS
3076 && XEXP (SET_SRC (PATTERN (insn)), 0) == stack_pointer_rtx
3077 && GET_CODE (XEXP (SET_SRC (PATTERN (insn)), 1)) == CONST_INT)
3078 /* We have an insn to pop a constant amount off the stack.
3079 (Such insns use PLUS regardless of the direction of the stack,
3080 and any insn to adjust the stack by a constant is always a pop.)
3081 These insns, if not dead stores, have no effect on life. */
3083 else
3085 /* Any regs live at the time of a call instruction
3086 must not go in a register clobbered by calls.
3087 Find all regs now live and record this for them. */
3089 if (GET_CODE (insn) == CALL_INSN && final)
3090 EXECUTE_IF_SET_IN_REG_SET (old, 0, i,
3092 REG_N_CALLS_CROSSED (i)++;
3095 /* LIVE gets the regs used in INSN;
3096 DEAD gets those set by it. Dead insns don't make anything
3097 live. */
3099 mark_set_regs (old, dead, PATTERN (insn),
3100 final ? insn : NULL_RTX, significant);
3102 /* If an insn doesn't use CC0, it becomes dead since we
3103 assume that every insn clobbers it. So show it dead here;
3104 mark_used_regs will set it live if it is referenced. */
3105 cc0_live = 0;
3107 if (! insn_is_dead)
3108 mark_used_regs (old, live, PATTERN (insn), final, insn);
3110 /* Sometimes we may have inserted something before INSN (such as
3111 a move) when we make an auto-inc. So ensure we will scan
3112 those insns. */
3113 #ifdef AUTO_INC_DEC
3114 prev = PREV_INSN (insn);
3115 #endif
3117 if (! insn_is_dead && GET_CODE (insn) == CALL_INSN)
3119 register int i;
3121 rtx note;
3123 for (note = CALL_INSN_FUNCTION_USAGE (insn);
3124 note;
3125 note = XEXP (note, 1))
3126 if (GET_CODE (XEXP (note, 0)) == USE)
3127 mark_used_regs (old, live, XEXP (XEXP (note, 0), 0),
3128 final, insn);
3130 /* Each call clobbers all call-clobbered regs that are not
3131 global or fixed. Note that the function-value reg is a
3132 call-clobbered reg, and mark_set_regs has already had
3133 a chance to handle it. */
3135 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3136 if (call_used_regs[i] && ! global_regs[i]
3137 && ! fixed_regs[i])
3138 SET_REGNO_REG_SET (dead, i);
3140 /* The stack ptr is used (honorarily) by a CALL insn. */
3141 SET_REGNO_REG_SET (live, STACK_POINTER_REGNUM);
3143 /* Calls may also reference any of the global registers,
3144 so they are made live. */
3145 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3146 if (global_regs[i])
3147 mark_used_regs (old, live,
3148 gen_rtx_REG (reg_raw_mode[i], i),
3149 final, insn);
3151 /* Calls also clobber memory. */
3152 mem_set_list = NULL_RTX;
3155 /* Update OLD for the registers used or set. */
3156 AND_COMPL_REG_SET (old, dead);
3157 IOR_REG_SET (old, live);
3161 /* On final pass, update counts of how many insns each reg is live
3162 at. */
3163 if (final)
3164 EXECUTE_IF_SET_IN_REG_SET (old, 0, i,
3165 { REG_LIVE_LENGTH (i)++; });
3167 flushed: ;
3168 if (insn == first)
3169 break;
3172 FREE_REG_SET (dead);
3173 FREE_REG_SET (live);
3176 /* Return 1 if X (the body of an insn, or part of it) is just dead stores
3177 (SET expressions whose destinations are registers dead after the insn).
3178 NEEDED is the regset that says which regs are alive after the insn.
3180 Unless CALL_OK is non-zero, an insn is needed if it contains a CALL.
3182 If X is the entire body of an insn, NOTES contains the reg notes
3183 pertaining to the insn. */
3185 static int
3186 insn_dead_p (x, needed, call_ok, notes)
3187 rtx x;
3188 regset needed;
3189 int call_ok;
3190 rtx notes ATTRIBUTE_UNUSED;
3192 enum rtx_code code = GET_CODE (x);
3194 #ifdef AUTO_INC_DEC
3195 /* If flow is invoked after reload, we must take existing AUTO_INC
3196 expresions into account. */
3197 if (reload_completed)
3199 for ( ; notes; notes = XEXP (notes, 1))
3201 if (REG_NOTE_KIND (notes) == REG_INC)
3203 int regno = REGNO (XEXP (notes, 0));
3205 /* Don't delete insns to set global regs. */
3206 if ((regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
3207 || REGNO_REG_SET_P (needed, regno))
3208 return 0;
3212 #endif
3214 /* If setting something that's a reg or part of one,
3215 see if that register's altered value will be live. */
3217 if (code == SET)
3219 rtx r = SET_DEST (x);
3221 /* A SET that is a subroutine call cannot be dead. */
3222 if (! call_ok && GET_CODE (SET_SRC (x)) == CALL)
3223 return 0;
3225 #ifdef HAVE_cc0
3226 if (GET_CODE (r) == CC0)
3227 return ! cc0_live;
3228 #endif
3230 if (GET_CODE (r) == MEM && ! MEM_VOLATILE_P (r))
3232 rtx temp;
3233 /* Walk the set of memory locations we are currently tracking
3234 and see if one is an identical match to this memory location.
3235 If so, this memory write is dead (remember, we're walking
3236 backwards from the end of the block to the start. */
3237 temp = mem_set_list;
3238 while (temp)
3240 if (rtx_equal_p (XEXP (temp, 0), r))
3241 return 1;
3242 temp = XEXP (temp, 1);
3246 while (GET_CODE (r) == SUBREG || GET_CODE (r) == STRICT_LOW_PART
3247 || GET_CODE (r) == ZERO_EXTRACT)
3248 r = XEXP (r, 0);
3250 if (GET_CODE (r) == REG)
3252 int regno = REGNO (r);
3254 /* Don't delete insns to set global regs. */
3255 if ((regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
3256 /* Make sure insns to set frame pointer aren't deleted. */
3257 || (regno == FRAME_POINTER_REGNUM
3258 && (! reload_completed || frame_pointer_needed))
3259 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3260 || (regno == HARD_FRAME_POINTER_REGNUM
3261 && (! reload_completed || frame_pointer_needed))
3262 #endif
3263 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3264 /* Make sure insns to set arg pointer are never deleted
3265 (if the arg pointer isn't fixed, there will be a USE for
3266 it, so we can treat it normally). */
3267 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
3268 #endif
3269 || REGNO_REG_SET_P (needed, regno))
3270 return 0;
3272 /* If this is a hard register, verify that subsequent words are
3273 not needed. */
3274 if (regno < FIRST_PSEUDO_REGISTER)
3276 int n = HARD_REGNO_NREGS (regno, GET_MODE (r));
3278 while (--n > 0)
3279 if (REGNO_REG_SET_P (needed, regno+n))
3280 return 0;
3283 return 1;
3287 /* If performing several activities,
3288 insn is dead if each activity is individually dead.
3289 Also, CLOBBERs and USEs can be ignored; a CLOBBER or USE
3290 that's inside a PARALLEL doesn't make the insn worth keeping. */
3291 else if (code == PARALLEL)
3293 int i = XVECLEN (x, 0);
3295 for (i--; i >= 0; i--)
3296 if (GET_CODE (XVECEXP (x, 0, i)) != CLOBBER
3297 && GET_CODE (XVECEXP (x, 0, i)) != USE
3298 && ! insn_dead_p (XVECEXP (x, 0, i), needed, call_ok, NULL_RTX))
3299 return 0;
3301 return 1;
3304 /* A CLOBBER of a pseudo-register that is dead serves no purpose. That
3305 is not necessarily true for hard registers. */
3306 else if (code == CLOBBER && GET_CODE (XEXP (x, 0)) == REG
3307 && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER
3308 && ! REGNO_REG_SET_P (needed, REGNO (XEXP (x, 0))))
3309 return 1;
3311 /* We do not check other CLOBBER or USE here. An insn consisting of just
3312 a CLOBBER or just a USE should not be deleted. */
3313 return 0;
3316 /* If X is the pattern of the last insn in a libcall, and assuming X is dead,
3317 return 1 if the entire library call is dead.
3318 This is true if X copies a register (hard or pseudo)
3319 and if the hard return reg of the call insn is dead.
3320 (The caller should have tested the destination of X already for death.)
3322 If this insn doesn't just copy a register, then we don't
3323 have an ordinary libcall. In that case, cse could not have
3324 managed to substitute the source for the dest later on,
3325 so we can assume the libcall is dead.
3327 NEEDED is the bit vector of pseudoregs live before this insn.
3328 NOTE is the REG_RETVAL note of the insn. INSN is the insn itself. */
3330 static int
3331 libcall_dead_p (x, needed, note, insn)
3332 rtx x;
3333 regset needed;
3334 rtx note;
3335 rtx insn;
3337 register RTX_CODE code = GET_CODE (x);
3339 if (code == SET)
3341 register rtx r = SET_SRC (x);
3342 if (GET_CODE (r) == REG)
3344 rtx call = XEXP (note, 0);
3345 rtx call_pat;
3346 register int i;
3348 /* Find the call insn. */
3349 while (call != insn && GET_CODE (call) != CALL_INSN)
3350 call = NEXT_INSN (call);
3352 /* If there is none, do nothing special,
3353 since ordinary death handling can understand these insns. */
3354 if (call == insn)
3355 return 0;
3357 /* See if the hard reg holding the value is dead.
3358 If this is a PARALLEL, find the call within it. */
3359 call_pat = PATTERN (call);
3360 if (GET_CODE (call_pat) == PARALLEL)
3362 for (i = XVECLEN (call_pat, 0) - 1; i >= 0; i--)
3363 if (GET_CODE (XVECEXP (call_pat, 0, i)) == SET
3364 && GET_CODE (SET_SRC (XVECEXP (call_pat, 0, i))) == CALL)
3365 break;
3367 /* This may be a library call that is returning a value
3368 via invisible pointer. Do nothing special, since
3369 ordinary death handling can understand these insns. */
3370 if (i < 0)
3371 return 0;
3373 call_pat = XVECEXP (call_pat, 0, i);
3376 return insn_dead_p (call_pat, needed, 1, REG_NOTES (call));
3379 return 1;
3382 /* Return 1 if register REGNO was used before it was set, i.e. if it is
3383 live at function entry. Don't count global register variables, variables
3384 in registers that can be used for function arg passing, or variables in
3385 fixed hard registers. */
3388 regno_uninitialized (regno)
3389 int regno;
3391 if (n_basic_blocks == 0
3392 || (regno < FIRST_PSEUDO_REGISTER
3393 && (global_regs[regno]
3394 || fixed_regs[regno]
3395 || FUNCTION_ARG_REGNO_P (regno))))
3396 return 0;
3398 return REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, regno);
3401 /* 1 if register REGNO was alive at a place where `setjmp' was called
3402 and was set more than once or is an argument.
3403 Such regs may be clobbered by `longjmp'. */
3406 regno_clobbered_at_setjmp (regno)
3407 int regno;
3409 if (n_basic_blocks == 0)
3410 return 0;
3412 return ((REG_N_SETS (regno) > 1
3413 || REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, regno))
3414 && REGNO_REG_SET_P (regs_live_at_setjmp, regno));
3417 /* INSN references memory, possibly using autoincrement addressing modes.
3418 Find any entries on the mem_set_list that need to be invalidated due
3419 to an address change. */
3420 static void
3421 invalidate_mems_from_autoinc (insn)
3422 rtx insn;
3424 rtx note = REG_NOTES (insn);
3425 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
3427 if (REG_NOTE_KIND (note) == REG_INC)
3429 rtx temp = mem_set_list;
3430 rtx prev = NULL_RTX;
3432 while (temp)
3434 if (reg_overlap_mentioned_p (XEXP (note, 0), XEXP (temp, 0)))
3436 /* Splice temp out of list. */
3437 if (prev)
3438 XEXP (prev, 1) = XEXP (temp, 1);
3439 else
3440 mem_set_list = XEXP (temp, 1);
3442 else
3443 prev = temp;
3444 temp = XEXP (temp, 1);
3450 /* Process the registers that are set within X.
3451 Their bits are set to 1 in the regset DEAD,
3452 because they are dead prior to this insn.
3454 If INSN is nonzero, it is the insn being processed
3455 and the fact that it is nonzero implies this is the FINAL pass
3456 in propagate_block. In this case, various info about register
3457 usage is stored, LOG_LINKS fields of insns are set up. */
3459 static void
3460 mark_set_regs (needed, dead, x, insn, significant)
3461 regset needed;
3462 regset dead;
3463 rtx x;
3464 rtx insn;
3465 regset significant;
3467 register RTX_CODE code = GET_CODE (x);
3469 if (code == SET || code == CLOBBER)
3470 mark_set_1 (needed, dead, x, insn, significant);
3471 else if (code == PARALLEL)
3473 register int i;
3474 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
3476 code = GET_CODE (XVECEXP (x, 0, i));
3477 if (code == SET || code == CLOBBER)
3478 mark_set_1 (needed, dead, XVECEXP (x, 0, i), insn, significant);
3483 /* Process a single SET rtx, X. */
3485 static void
3486 mark_set_1 (needed, dead, x, insn, significant)
3487 regset needed;
3488 regset dead;
3489 rtx x;
3490 rtx insn;
3491 regset significant;
3493 register int regno;
3494 register rtx reg = SET_DEST (x);
3496 /* Some targets place small structures in registers for
3497 return values of functions. We have to detect this
3498 case specially here to get correct flow information. */
3499 if (GET_CODE (reg) == PARALLEL
3500 && GET_MODE (reg) == BLKmode)
3502 register int i;
3504 for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
3505 mark_set_1 (needed, dead, XVECEXP (reg, 0, i), insn, significant);
3506 return;
3509 /* Modifying just one hardware register of a multi-reg value
3510 or just a byte field of a register
3511 does not mean the value from before this insn is now dead.
3512 But it does mean liveness of that register at the end of the block
3513 is significant.
3515 Within mark_set_1, however, we treat it as if the register is
3516 indeed modified. mark_used_regs will, however, also treat this
3517 register as being used. Thus, we treat these insns as setting a
3518 new value for the register as a function of its old value. This
3519 cases LOG_LINKS to be made appropriately and this will help combine. */
3521 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
3522 || GET_CODE (reg) == SIGN_EXTRACT
3523 || GET_CODE (reg) == STRICT_LOW_PART)
3524 reg = XEXP (reg, 0);
3526 /* If this set is a MEM, then it kills any aliased writes.
3527 If this set is a REG, then it kills any MEMs which use the reg. */
3528 if (GET_CODE (reg) == MEM
3529 || GET_CODE (reg) == REG)
3531 rtx temp = mem_set_list;
3532 rtx prev = NULL_RTX;
3534 while (temp)
3536 if ((GET_CODE (reg) == MEM
3537 && output_dependence (XEXP (temp, 0), reg))
3538 || (GET_CODE (reg) == REG
3539 && reg_overlap_mentioned_p (reg, XEXP (temp, 0))))
3541 /* Splice this entry out of the list. */
3542 if (prev)
3543 XEXP (prev, 1) = XEXP (temp, 1);
3544 else
3545 mem_set_list = XEXP (temp, 1);
3547 else
3548 prev = temp;
3549 temp = XEXP (temp, 1);
3553 /* If the memory reference had embedded side effects (autoincrement
3554 address modes. Then we may need to kill some entries on the
3555 memory set list. */
3556 if (insn && GET_CODE (reg) == MEM)
3557 invalidate_mems_from_autoinc (insn);
3559 if (GET_CODE (reg) == MEM && ! side_effects_p (reg)
3560 /* We do not know the size of a BLKmode store, so we do not track
3561 them for redundant store elimination. */
3562 && GET_MODE (reg) != BLKmode
3563 /* There are no REG_INC notes for SP, so we can't assume we'll see
3564 everything that invalidates it. To be safe, don't eliminate any
3565 stores though SP; none of them should be redundant anyway. */
3566 && ! reg_mentioned_p (stack_pointer_rtx, reg))
3567 mem_set_list = gen_rtx_EXPR_LIST (VOIDmode, reg, mem_set_list);
3569 if (GET_CODE (reg) == REG
3570 && (regno = REGNO (reg), ! (regno == FRAME_POINTER_REGNUM
3571 && (! reload_completed || frame_pointer_needed)))
3572 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3573 && ! (regno == HARD_FRAME_POINTER_REGNUM
3574 && (! reload_completed || frame_pointer_needed))
3575 #endif
3576 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3577 && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
3578 #endif
3579 && ! (regno < FIRST_PSEUDO_REGISTER && global_regs[regno]))
3580 /* && regno != STACK_POINTER_REGNUM) -- let's try without this. */
3582 int some_needed = REGNO_REG_SET_P (needed, regno);
3583 int some_not_needed = ! some_needed;
3585 /* Mark it as a significant register for this basic block. */
3586 if (significant)
3587 SET_REGNO_REG_SET (significant, regno);
3589 /* Mark it as dead before this insn. */
3590 SET_REGNO_REG_SET (dead, regno);
3592 /* A hard reg in a wide mode may really be multiple registers.
3593 If so, mark all of them just like the first. */
3594 if (regno < FIRST_PSEUDO_REGISTER)
3596 int n;
3598 /* Nothing below is needed for the stack pointer; get out asap.
3599 Eg, log links aren't needed, since combine won't use them. */
3600 if (regno == STACK_POINTER_REGNUM)
3601 return;
3603 n = HARD_REGNO_NREGS (regno, GET_MODE (reg));
3604 while (--n > 0)
3606 int regno_n = regno + n;
3607 int needed_regno = REGNO_REG_SET_P (needed, regno_n);
3608 if (significant)
3609 SET_REGNO_REG_SET (significant, regno_n);
3611 SET_REGNO_REG_SET (dead, regno_n);
3612 some_needed |= needed_regno;
3613 some_not_needed |= ! needed_regno;
3616 /* Additional data to record if this is the final pass. */
3617 if (insn)
3619 register rtx y = reg_next_use[regno];
3620 register int blocknum = BLOCK_NUM (insn);
3622 /* If this is a hard reg, record this function uses the reg. */
3624 if (regno < FIRST_PSEUDO_REGISTER)
3626 register int i;
3627 int endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
3629 for (i = regno; i < endregno; i++)
3631 /* The next use is no longer "next", since a store
3632 intervenes. */
3633 reg_next_use[i] = 0;
3635 regs_ever_live[i] = 1;
3636 REG_N_SETS (i)++;
3639 else
3641 /* The next use is no longer "next", since a store
3642 intervenes. */
3643 reg_next_use[regno] = 0;
3645 /* Keep track of which basic blocks each reg appears in. */
3647 if (REG_BASIC_BLOCK (regno) == REG_BLOCK_UNKNOWN)
3648 REG_BASIC_BLOCK (regno) = blocknum;
3649 else if (REG_BASIC_BLOCK (regno) != blocknum)
3650 REG_BASIC_BLOCK (regno) = REG_BLOCK_GLOBAL;
3652 /* Count (weighted) references, stores, etc. This counts a
3653 register twice if it is modified, but that is correct. */
3654 REG_N_SETS (regno)++;
3656 REG_N_REFS (regno) += loop_depth;
3658 /* The insns where a reg is live are normally counted
3659 elsewhere, but we want the count to include the insn
3660 where the reg is set, and the normal counting mechanism
3661 would not count it. */
3662 REG_LIVE_LENGTH (regno)++;
3665 if (! some_not_needed)
3667 /* Make a logical link from the next following insn
3668 that uses this register, back to this insn.
3669 The following insns have already been processed.
3671 We don't build a LOG_LINK for hard registers containing
3672 in ASM_OPERANDs. If these registers get replaced,
3673 we might wind up changing the semantics of the insn,
3674 even if reload can make what appear to be valid assignments
3675 later. */
3676 if (y && (BLOCK_NUM (y) == blocknum)
3677 && (regno >= FIRST_PSEUDO_REGISTER
3678 || asm_noperands (PATTERN (y)) < 0))
3679 LOG_LINKS (y)
3680 = gen_rtx_INSN_LIST (VOIDmode, insn, LOG_LINKS (y));
3682 else if (! some_needed)
3684 /* Note that dead stores have already been deleted when possible
3685 If we get here, we have found a dead store that cannot
3686 be eliminated (because the same insn does something useful).
3687 Indicate this by marking the reg being set as dying here. */
3688 REG_NOTES (insn)
3689 = gen_rtx_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn));
3690 REG_N_DEATHS (REGNO (reg))++;
3692 else
3694 /* This is a case where we have a multi-word hard register
3695 and some, but not all, of the words of the register are
3696 needed in subsequent insns. Write REG_UNUSED notes
3697 for those parts that were not needed. This case should
3698 be rare. */
3700 int i;
3702 for (i = HARD_REGNO_NREGS (regno, GET_MODE (reg)) - 1;
3703 i >= 0; i--)
3704 if (!REGNO_REG_SET_P (needed, regno + i))
3705 REG_NOTES (insn)
3706 = gen_rtx_EXPR_LIST (REG_UNUSED,
3707 gen_rtx_REG (reg_raw_mode[regno + i],
3708 regno + i),
3709 REG_NOTES (insn));
3713 else if (GET_CODE (reg) == REG)
3714 reg_next_use[regno] = 0;
3716 /* If this is the last pass and this is a SCRATCH, show it will be dying
3717 here and count it. */
3718 else if (GET_CODE (reg) == SCRATCH && insn != 0)
3720 REG_NOTES (insn)
3721 = gen_rtx_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn));
3725 #ifdef AUTO_INC_DEC
3727 /* X is a MEM found in INSN. See if we can convert it into an auto-increment
3728 reference. */
3730 static void
3731 find_auto_inc (needed, x, insn)
3732 regset needed;
3733 rtx x;
3734 rtx insn;
3736 rtx addr = XEXP (x, 0);
3737 HOST_WIDE_INT offset = 0;
3738 rtx set;
3740 /* Here we detect use of an index register which might be good for
3741 postincrement, postdecrement, preincrement, or predecrement. */
3743 if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
3744 offset = INTVAL (XEXP (addr, 1)), addr = XEXP (addr, 0);
3746 if (GET_CODE (addr) == REG)
3748 register rtx y;
3749 register int size = GET_MODE_SIZE (GET_MODE (x));
3750 rtx use;
3751 rtx incr;
3752 int regno = REGNO (addr);
3754 /* Is the next use an increment that might make auto-increment? */
3755 if ((incr = reg_next_use[regno]) != 0
3756 && (set = single_set (incr)) != 0
3757 && GET_CODE (set) == SET
3758 && BLOCK_NUM (incr) == BLOCK_NUM (insn)
3759 /* Can't add side effects to jumps; if reg is spilled and
3760 reloaded, there's no way to store back the altered value. */
3761 && GET_CODE (insn) != JUMP_INSN
3762 && (y = SET_SRC (set), GET_CODE (y) == PLUS)
3763 && XEXP (y, 0) == addr
3764 && GET_CODE (XEXP (y, 1)) == CONST_INT
3765 && ((HAVE_POST_INCREMENT
3766 && (INTVAL (XEXP (y, 1)) == size && offset == 0))
3767 || (HAVE_POST_DECREMENT
3768 && (INTVAL (XEXP (y, 1)) == - size && offset == 0))
3769 || (HAVE_PRE_INCREMENT
3770 && (INTVAL (XEXP (y, 1)) == size && offset == size))
3771 || (HAVE_PRE_DECREMENT
3772 && (INTVAL (XEXP (y, 1)) == - size && offset == - size)))
3773 /* Make sure this reg appears only once in this insn. */
3774 && (use = find_use_as_address (PATTERN (insn), addr, offset),
3775 use != 0 && use != (rtx) 1))
3777 rtx q = SET_DEST (set);
3778 enum rtx_code inc_code = (INTVAL (XEXP (y, 1)) == size
3779 ? (offset ? PRE_INC : POST_INC)
3780 : (offset ? PRE_DEC : POST_DEC));
3782 if (dead_or_set_p (incr, addr))
3784 /* This is the simple case. Try to make the auto-inc. If
3785 we can't, we are done. Otherwise, we will do any
3786 needed updates below. */
3787 if (! validate_change (insn, &XEXP (x, 0),
3788 gen_rtx_fmt_e (inc_code, Pmode, addr),
3790 return;
3792 else if (GET_CODE (q) == REG
3793 /* PREV_INSN used here to check the semi-open interval
3794 [insn,incr). */
3795 && ! reg_used_between_p (q, PREV_INSN (insn), incr)
3796 /* We must also check for sets of q as q may be
3797 a call clobbered hard register and there may
3798 be a call between PREV_INSN (insn) and incr. */
3799 && ! reg_set_between_p (q, PREV_INSN (insn), incr))
3801 /* We have *p followed sometime later by q = p+size.
3802 Both p and q must be live afterward,
3803 and q is not used between INSN and its assignment.
3804 Change it to q = p, ...*q..., q = q+size.
3805 Then fall into the usual case. */
3806 rtx insns, temp;
3807 basic_block bb;
3809 start_sequence ();
3810 emit_move_insn (q, addr);
3811 insns = get_insns ();
3812 end_sequence ();
3814 bb = BLOCK_FOR_INSN (insn);
3815 for (temp = insns; temp; temp = NEXT_INSN (temp))
3816 set_block_for_insn (temp, bb);
3818 /* If we can't make the auto-inc, or can't make the
3819 replacement into Y, exit. There's no point in making
3820 the change below if we can't do the auto-inc and doing
3821 so is not correct in the pre-inc case. */
3823 validate_change (insn, &XEXP (x, 0),
3824 gen_rtx_fmt_e (inc_code, Pmode, q),
3826 validate_change (incr, &XEXP (y, 0), q, 1);
3827 if (! apply_change_group ())
3828 return;
3830 /* We now know we'll be doing this change, so emit the
3831 new insn(s) and do the updates. */
3832 emit_insns_before (insns, insn);
3834 if (BLOCK_FOR_INSN (insn)->head == insn)
3835 BLOCK_FOR_INSN (insn)->head = insns;
3837 /* INCR will become a NOTE and INSN won't contain a
3838 use of ADDR. If a use of ADDR was just placed in
3839 the insn before INSN, make that the next use.
3840 Otherwise, invalidate it. */
3841 if (GET_CODE (PREV_INSN (insn)) == INSN
3842 && GET_CODE (PATTERN (PREV_INSN (insn))) == SET
3843 && SET_SRC (PATTERN (PREV_INSN (insn))) == addr)
3844 reg_next_use[regno] = PREV_INSN (insn);
3845 else
3846 reg_next_use[regno] = 0;
3848 addr = q;
3849 regno = REGNO (q);
3851 /* REGNO is now used in INCR which is below INSN, but
3852 it previously wasn't live here. If we don't mark
3853 it as needed, we'll put a REG_DEAD note for it
3854 on this insn, which is incorrect. */
3855 SET_REGNO_REG_SET (needed, regno);
3857 /* If there are any calls between INSN and INCR, show
3858 that REGNO now crosses them. */
3859 for (temp = insn; temp != incr; temp = NEXT_INSN (temp))
3860 if (GET_CODE (temp) == CALL_INSN)
3861 REG_N_CALLS_CROSSED (regno)++;
3863 else
3864 return;
3866 /* If we haven't returned, it means we were able to make the
3867 auto-inc, so update the status. First, record that this insn
3868 has an implicit side effect. */
3870 REG_NOTES (insn)
3871 = gen_rtx_EXPR_LIST (REG_INC, addr, REG_NOTES (insn));
3873 /* Modify the old increment-insn to simply copy
3874 the already-incremented value of our register. */
3875 if (! validate_change (incr, &SET_SRC (set), addr, 0))
3876 abort ();
3878 /* If that makes it a no-op (copying the register into itself) delete
3879 it so it won't appear to be a "use" and a "set" of this
3880 register. */
3881 if (SET_DEST (set) == addr)
3883 PUT_CODE (incr, NOTE);
3884 NOTE_LINE_NUMBER (incr) = NOTE_INSN_DELETED;
3885 NOTE_SOURCE_FILE (incr) = 0;
3888 if (regno >= FIRST_PSEUDO_REGISTER)
3890 /* Count an extra reference to the reg. When a reg is
3891 incremented, spilling it is worse, so we want to make
3892 that less likely. */
3893 REG_N_REFS (regno) += loop_depth;
3895 /* Count the increment as a setting of the register,
3896 even though it isn't a SET in rtl. */
3897 REG_N_SETS (regno)++;
3902 #endif /* AUTO_INC_DEC */
3904 /* Scan expression X and store a 1-bit in LIVE for each reg it uses.
3905 This is done assuming the registers needed from X
3906 are those that have 1-bits in NEEDED.
3908 On the final pass, FINAL is 1. This means try for autoincrement
3909 and count the uses and deaths of each pseudo-reg.
3911 INSN is the containing instruction. If INSN is dead, this function is not
3912 called. */
3914 static void
3915 mark_used_regs (needed, live, x, final, insn)
3916 regset needed;
3917 regset live;
3918 rtx x;
3919 int final;
3920 rtx insn;
3922 register RTX_CODE code;
3923 register int regno;
3924 int i;
3926 retry:
3927 code = GET_CODE (x);
3928 switch (code)
3930 case LABEL_REF:
3931 case SYMBOL_REF:
3932 case CONST_INT:
3933 case CONST:
3934 case CONST_DOUBLE:
3935 case PC:
3936 case ADDR_VEC:
3937 case ADDR_DIFF_VEC:
3938 return;
3940 #ifdef HAVE_cc0
3941 case CC0:
3942 cc0_live = 1;
3943 return;
3944 #endif
3946 case CLOBBER:
3947 /* If we are clobbering a MEM, mark any registers inside the address
3948 as being used. */
3949 if (GET_CODE (XEXP (x, 0)) == MEM)
3950 mark_used_regs (needed, live, XEXP (XEXP (x, 0), 0), final, insn);
3951 return;
3953 case MEM:
3954 /* Invalidate the data for the last MEM stored, but only if MEM is
3955 something that can be stored into. */
3956 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
3957 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
3958 ; /* needn't clear the memory set list */
3959 else
3961 rtx temp = mem_set_list;
3962 rtx prev = NULL_RTX;
3964 while (temp)
3966 if (anti_dependence (XEXP (temp, 0), x))
3968 /* Splice temp out of the list. */
3969 if (prev)
3970 XEXP (prev, 1) = XEXP (temp, 1);
3971 else
3972 mem_set_list = XEXP (temp, 1);
3974 else
3975 prev = temp;
3976 temp = XEXP (temp, 1);
3980 /* If the memory reference had embedded side effects (autoincrement
3981 address modes. Then we may need to kill some entries on the
3982 memory set list. */
3983 if (insn)
3984 invalidate_mems_from_autoinc (insn);
3986 #ifdef AUTO_INC_DEC
3987 if (final)
3988 find_auto_inc (needed, x, insn);
3989 #endif
3990 break;
3992 case SUBREG:
3993 if (GET_CODE (SUBREG_REG (x)) == REG
3994 && REGNO (SUBREG_REG (x)) >= FIRST_PSEUDO_REGISTER
3995 && (GET_MODE_SIZE (GET_MODE (x))
3996 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
3997 REG_CHANGES_SIZE (REGNO (SUBREG_REG (x))) = 1;
3999 /* While we're here, optimize this case. */
4000 x = SUBREG_REG (x);
4002 /* In case the SUBREG is not of a register, don't optimize */
4003 if (GET_CODE (x) != REG)
4005 mark_used_regs (needed, live, x, final, insn);
4006 return;
4009 /* ... fall through ... */
4011 case REG:
4012 /* See a register other than being set
4013 => mark it as needed. */
4015 regno = REGNO (x);
4017 int some_needed = REGNO_REG_SET_P (needed, regno);
4018 int some_not_needed = ! some_needed;
4020 SET_REGNO_REG_SET (live, regno);
4022 /* A hard reg in a wide mode may really be multiple registers.
4023 If so, mark all of them just like the first. */
4024 if (regno < FIRST_PSEUDO_REGISTER)
4026 int n;
4028 /* For stack ptr or fixed arg pointer,
4029 nothing below can be necessary, so waste no more time. */
4030 if (regno == STACK_POINTER_REGNUM
4031 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
4032 || (regno == HARD_FRAME_POINTER_REGNUM
4033 && (! reload_completed || frame_pointer_needed))
4034 #endif
4035 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
4036 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
4037 #endif
4038 || (regno == FRAME_POINTER_REGNUM
4039 && (! reload_completed || frame_pointer_needed)))
4041 /* If this is a register we are going to try to eliminate,
4042 don't mark it live here. If we are successful in
4043 eliminating it, it need not be live unless it is used for
4044 pseudos, in which case it will have been set live when
4045 it was allocated to the pseudos. If the register will not
4046 be eliminated, reload will set it live at that point. */
4048 if (! TEST_HARD_REG_BIT (elim_reg_set, regno))
4049 regs_ever_live[regno] = 1;
4050 return;
4052 /* No death notes for global register variables;
4053 their values are live after this function exits. */
4054 if (global_regs[regno])
4056 if (final)
4057 reg_next_use[regno] = insn;
4058 return;
4061 n = HARD_REGNO_NREGS (regno, GET_MODE (x));
4062 while (--n > 0)
4064 int regno_n = regno + n;
4065 int needed_regno = REGNO_REG_SET_P (needed, regno_n);
4067 SET_REGNO_REG_SET (live, regno_n);
4068 some_needed |= needed_regno;
4069 some_not_needed |= ! needed_regno;
4072 if (final)
4074 /* Record where each reg is used, so when the reg
4075 is set we know the next insn that uses it. */
4077 reg_next_use[regno] = insn;
4079 if (regno < FIRST_PSEUDO_REGISTER)
4081 /* If a hard reg is being used,
4082 record that this function does use it. */
4084 i = HARD_REGNO_NREGS (regno, GET_MODE (x));
4085 if (i == 0)
4086 i = 1;
4088 regs_ever_live[regno + --i] = 1;
4089 while (i > 0);
4091 else
4093 /* Keep track of which basic block each reg appears in. */
4095 register int blocknum = BLOCK_NUM (insn);
4097 if (REG_BASIC_BLOCK (regno) == REG_BLOCK_UNKNOWN)
4098 REG_BASIC_BLOCK (regno) = blocknum;
4099 else if (REG_BASIC_BLOCK (regno) != blocknum)
4100 REG_BASIC_BLOCK (regno) = REG_BLOCK_GLOBAL;
4102 /* Count (weighted) number of uses of each reg. */
4104 REG_N_REFS (regno) += loop_depth;
4107 /* Record and count the insns in which a reg dies.
4108 If it is used in this insn and was dead below the insn
4109 then it dies in this insn. If it was set in this insn,
4110 we do not make a REG_DEAD note; likewise if we already
4111 made such a note. */
4113 if (some_not_needed
4114 && ! dead_or_set_p (insn, x)
4115 #if 0
4116 && (regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
4117 #endif
4120 /* Check for the case where the register dying partially
4121 overlaps the register set by this insn. */
4122 if (regno < FIRST_PSEUDO_REGISTER
4123 && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
4125 int n = HARD_REGNO_NREGS (regno, GET_MODE (x));
4126 while (--n >= 0)
4127 some_needed |= dead_or_set_regno_p (insn, regno + n);
4130 /* If none of the words in X is needed, make a REG_DEAD
4131 note. Otherwise, we must make partial REG_DEAD notes. */
4132 if (! some_needed)
4134 REG_NOTES (insn)
4135 = gen_rtx_EXPR_LIST (REG_DEAD, x, REG_NOTES (insn));
4136 REG_N_DEATHS (regno)++;
4138 else
4140 int i;
4142 /* Don't make a REG_DEAD note for a part of a register
4143 that is set in the insn. */
4145 for (i = HARD_REGNO_NREGS (regno, GET_MODE (x)) - 1;
4146 i >= 0; i--)
4147 if (!REGNO_REG_SET_P (needed, regno + i)
4148 && ! dead_or_set_regno_p (insn, regno + i))
4149 REG_NOTES (insn)
4150 = gen_rtx_EXPR_LIST (REG_DEAD,
4151 gen_rtx_REG (reg_raw_mode[regno + i],
4152 regno + i),
4153 REG_NOTES (insn));
4158 return;
4160 case SET:
4162 register rtx testreg = SET_DEST (x);
4163 int mark_dest = 0;
4165 /* If storing into MEM, don't show it as being used. But do
4166 show the address as being used. */
4167 if (GET_CODE (testreg) == MEM)
4169 #ifdef AUTO_INC_DEC
4170 if (final)
4171 find_auto_inc (needed, testreg, insn);
4172 #endif
4173 mark_used_regs (needed, live, XEXP (testreg, 0), final, insn);
4174 mark_used_regs (needed, live, SET_SRC (x), final, insn);
4175 return;
4178 /* Storing in STRICT_LOW_PART is like storing in a reg
4179 in that this SET might be dead, so ignore it in TESTREG.
4180 but in some other ways it is like using the reg.
4182 Storing in a SUBREG or a bit field is like storing the entire
4183 register in that if the register's value is not used
4184 then this SET is not needed. */
4185 while (GET_CODE (testreg) == STRICT_LOW_PART
4186 || GET_CODE (testreg) == ZERO_EXTRACT
4187 || GET_CODE (testreg) == SIGN_EXTRACT
4188 || GET_CODE (testreg) == SUBREG)
4190 if (GET_CODE (testreg) == SUBREG
4191 && GET_CODE (SUBREG_REG (testreg)) == REG
4192 && REGNO (SUBREG_REG (testreg)) >= FIRST_PSEUDO_REGISTER
4193 && (GET_MODE_SIZE (GET_MODE (testreg))
4194 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (testreg)))))
4195 REG_CHANGES_SIZE (REGNO (SUBREG_REG (testreg))) = 1;
4197 /* Modifying a single register in an alternate mode
4198 does not use any of the old value. But these other
4199 ways of storing in a register do use the old value. */
4200 if (GET_CODE (testreg) == SUBREG
4201 && !(REG_SIZE (SUBREG_REG (testreg)) > REG_SIZE (testreg)))
4203 else
4204 mark_dest = 1;
4206 testreg = XEXP (testreg, 0);
4209 /* If this is a store into a register,
4210 recursively scan the value being stored. */
4212 if ((GET_CODE (testreg) == PARALLEL
4213 && GET_MODE (testreg) == BLKmode)
4214 || (GET_CODE (testreg) == REG
4215 && (regno = REGNO (testreg), ! (regno == FRAME_POINTER_REGNUM
4216 && (! reload_completed || frame_pointer_needed)))
4217 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
4218 && ! (regno == HARD_FRAME_POINTER_REGNUM
4219 && (! reload_completed || frame_pointer_needed))
4220 #endif
4221 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
4222 && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
4223 #endif
4225 /* We used to exclude global_regs here, but that seems wrong.
4226 Storing in them is like storing in mem. */
4228 mark_used_regs (needed, live, SET_SRC (x), final, insn);
4229 if (mark_dest)
4230 mark_used_regs (needed, live, SET_DEST (x), final, insn);
4231 return;
4234 break;
4236 case RETURN:
4237 /* If exiting needs the right stack value, consider this insn as
4238 using the stack pointer. In any event, consider it as using
4239 all global registers and all registers used by return. */
4240 if (! EXIT_IGNORE_STACK
4241 || (! FRAME_POINTER_REQUIRED
4242 && ! current_function_calls_alloca
4243 && flag_omit_frame_pointer)
4244 || current_function_sp_is_unchanging)
4245 SET_REGNO_REG_SET (live, STACK_POINTER_REGNUM);
4247 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4248 if (global_regs[i]
4249 #ifdef EPILOGUE_USES
4250 || EPILOGUE_USES (i)
4251 #endif
4253 SET_REGNO_REG_SET (live, i);
4254 break;
4256 case ASM_OPERANDS:
4257 case UNSPEC_VOLATILE:
4258 case TRAP_IF:
4259 case ASM_INPUT:
4261 /* Traditional and volatile asm instructions must be considered to use
4262 and clobber all hard registers, all pseudo-registers and all of
4263 memory. So must TRAP_IF and UNSPEC_VOLATILE operations.
4265 Consider for instance a volatile asm that changes the fpu rounding
4266 mode. An insn should not be moved across this even if it only uses
4267 pseudo-regs because it might give an incorrectly rounded result.
4269 ?!? Unfortunately, marking all hard registers as live causes massive
4270 problems for the register allocator and marking all pseudos as live
4271 creates mountains of uninitialized variable warnings.
4273 So for now, just clear the memory set list and mark any regs
4274 we can find in ASM_OPERANDS as used. */
4275 if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
4276 mem_set_list = NULL_RTX;
4278 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
4279 We can not just fall through here since then we would be confused
4280 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
4281 traditional asms unlike their normal usage. */
4282 if (code == ASM_OPERANDS)
4284 int j;
4286 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
4287 mark_used_regs (needed, live, ASM_OPERANDS_INPUT (x, j),
4288 final, insn);
4290 break;
4294 default:
4295 break;
4298 /* Recursively scan the operands of this expression. */
4301 register const char *fmt = GET_RTX_FORMAT (code);
4302 register int i;
4304 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4306 if (fmt[i] == 'e')
4308 /* Tail recursive case: save a function call level. */
4309 if (i == 0)
4311 x = XEXP (x, 0);
4312 goto retry;
4314 mark_used_regs (needed, live, XEXP (x, i), final, insn);
4316 else if (fmt[i] == 'E')
4318 register int j;
4319 for (j = 0; j < XVECLEN (x, i); j++)
4320 mark_used_regs (needed, live, XVECEXP (x, i, j), final, insn);
4326 #ifdef AUTO_INC_DEC
4328 static int
4329 try_pre_increment_1 (insn)
4330 rtx insn;
4332 /* Find the next use of this reg. If in same basic block,
4333 make it do pre-increment or pre-decrement if appropriate. */
4334 rtx x = single_set (insn);
4335 HOST_WIDE_INT amount = ((GET_CODE (SET_SRC (x)) == PLUS ? 1 : -1)
4336 * INTVAL (XEXP (SET_SRC (x), 1)));
4337 int regno = REGNO (SET_DEST (x));
4338 rtx y = reg_next_use[regno];
4339 if (y != 0
4340 && BLOCK_NUM (y) == BLOCK_NUM (insn)
4341 /* Don't do this if the reg dies, or gets set in y; a standard addressing
4342 mode would be better. */
4343 && ! dead_or_set_p (y, SET_DEST (x))
4344 && try_pre_increment (y, SET_DEST (x), amount))
4346 /* We have found a suitable auto-increment
4347 and already changed insn Y to do it.
4348 So flush this increment-instruction. */
4349 PUT_CODE (insn, NOTE);
4350 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
4351 NOTE_SOURCE_FILE (insn) = 0;
4352 /* Count a reference to this reg for the increment
4353 insn we are deleting. When a reg is incremented.
4354 spilling it is worse, so we want to make that
4355 less likely. */
4356 if (regno >= FIRST_PSEUDO_REGISTER)
4358 REG_N_REFS (regno) += loop_depth;
4359 REG_N_SETS (regno)++;
4361 return 1;
4363 return 0;
4366 /* Try to change INSN so that it does pre-increment or pre-decrement
4367 addressing on register REG in order to add AMOUNT to REG.
4368 AMOUNT is negative for pre-decrement.
4369 Returns 1 if the change could be made.
4370 This checks all about the validity of the result of modifying INSN. */
4372 static int
4373 try_pre_increment (insn, reg, amount)
4374 rtx insn, reg;
4375 HOST_WIDE_INT amount;
4377 register rtx use;
4379 /* Nonzero if we can try to make a pre-increment or pre-decrement.
4380 For example, addl $4,r1; movl (r1),... can become movl +(r1),... */
4381 int pre_ok = 0;
4382 /* Nonzero if we can try to make a post-increment or post-decrement.
4383 For example, addl $4,r1; movl -4(r1),... can become movl (r1)+,...
4384 It is possible for both PRE_OK and POST_OK to be nonzero if the machine
4385 supports both pre-inc and post-inc, or both pre-dec and post-dec. */
4386 int post_ok = 0;
4388 /* Nonzero if the opportunity actually requires post-inc or post-dec. */
4389 int do_post = 0;
4391 /* From the sign of increment, see which possibilities are conceivable
4392 on this target machine. */
4393 if (HAVE_PRE_INCREMENT && amount > 0)
4394 pre_ok = 1;
4395 if (HAVE_POST_INCREMENT && amount > 0)
4396 post_ok = 1;
4398 if (HAVE_PRE_DECREMENT && amount < 0)
4399 pre_ok = 1;
4400 if (HAVE_POST_DECREMENT && amount < 0)
4401 post_ok = 1;
4403 if (! (pre_ok || post_ok))
4404 return 0;
4406 /* It is not safe to add a side effect to a jump insn
4407 because if the incremented register is spilled and must be reloaded
4408 there would be no way to store the incremented value back in memory. */
4410 if (GET_CODE (insn) == JUMP_INSN)
4411 return 0;
4413 use = 0;
4414 if (pre_ok)
4415 use = find_use_as_address (PATTERN (insn), reg, 0);
4416 if (post_ok && (use == 0 || use == (rtx) 1))
4418 use = find_use_as_address (PATTERN (insn), reg, -amount);
4419 do_post = 1;
4422 if (use == 0 || use == (rtx) 1)
4423 return 0;
4425 if (GET_MODE_SIZE (GET_MODE (use)) != (amount > 0 ? amount : - amount))
4426 return 0;
4428 /* See if this combination of instruction and addressing mode exists. */
4429 if (! validate_change (insn, &XEXP (use, 0),
4430 gen_rtx_fmt_e (amount > 0
4431 ? (do_post ? POST_INC : PRE_INC)
4432 : (do_post ? POST_DEC : PRE_DEC),
4433 Pmode, reg), 0))
4434 return 0;
4436 /* Record that this insn now has an implicit side effect on X. */
4437 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_INC, reg, REG_NOTES (insn));
4438 return 1;
4441 #endif /* AUTO_INC_DEC */
4443 /* Find the place in the rtx X where REG is used as a memory address.
4444 Return the MEM rtx that so uses it.
4445 If PLUSCONST is nonzero, search instead for a memory address equivalent to
4446 (plus REG (const_int PLUSCONST)).
4448 If such an address does not appear, return 0.
4449 If REG appears more than once, or is used other than in such an address,
4450 return (rtx)1. */
4453 find_use_as_address (x, reg, plusconst)
4454 register rtx x;
4455 rtx reg;
4456 HOST_WIDE_INT plusconst;
4458 enum rtx_code code = GET_CODE (x);
4459 const char *fmt = GET_RTX_FORMAT (code);
4460 register int i;
4461 register rtx value = 0;
4462 register rtx tem;
4464 if (code == MEM && XEXP (x, 0) == reg && plusconst == 0)
4465 return x;
4467 if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
4468 && XEXP (XEXP (x, 0), 0) == reg
4469 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4470 && INTVAL (XEXP (XEXP (x, 0), 1)) == plusconst)
4471 return x;
4473 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
4475 /* If REG occurs inside a MEM used in a bit-field reference,
4476 that is unacceptable. */
4477 if (find_use_as_address (XEXP (x, 0), reg, 0) != 0)
4478 return (rtx) (HOST_WIDE_INT) 1;
4481 if (x == reg)
4482 return (rtx) (HOST_WIDE_INT) 1;
4484 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4486 if (fmt[i] == 'e')
4488 tem = find_use_as_address (XEXP (x, i), reg, plusconst);
4489 if (value == 0)
4490 value = tem;
4491 else if (tem != 0)
4492 return (rtx) (HOST_WIDE_INT) 1;
4494 if (fmt[i] == 'E')
4496 register int j;
4497 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4499 tem = find_use_as_address (XVECEXP (x, i, j), reg, plusconst);
4500 if (value == 0)
4501 value = tem;
4502 else if (tem != 0)
4503 return (rtx) (HOST_WIDE_INT) 1;
4508 return value;
4511 /* Write information about registers and basic blocks into FILE.
4512 This is part of making a debugging dump. */
4514 void
4515 dump_flow_info (file)
4516 FILE *file;
4518 register int i;
4519 static const char * const reg_class_names[] = REG_CLASS_NAMES;
4521 fprintf (file, "%d registers.\n", max_regno);
4522 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
4523 if (REG_N_REFS (i))
4525 enum reg_class class, altclass;
4526 fprintf (file, "\nRegister %d used %d times across %d insns",
4527 i, REG_N_REFS (i), REG_LIVE_LENGTH (i));
4528 if (REG_BASIC_BLOCK (i) >= 0)
4529 fprintf (file, " in block %d", REG_BASIC_BLOCK (i));
4530 if (REG_N_SETS (i))
4531 fprintf (file, "; set %d time%s", REG_N_SETS (i),
4532 (REG_N_SETS (i) == 1) ? "" : "s");
4533 if (REG_USERVAR_P (regno_reg_rtx[i]))
4534 fprintf (file, "; user var");
4535 if (REG_N_DEATHS (i) != 1)
4536 fprintf (file, "; dies in %d places", REG_N_DEATHS (i));
4537 if (REG_N_CALLS_CROSSED (i) == 1)
4538 fprintf (file, "; crosses 1 call");
4539 else if (REG_N_CALLS_CROSSED (i))
4540 fprintf (file, "; crosses %d calls", REG_N_CALLS_CROSSED (i));
4541 if (PSEUDO_REGNO_BYTES (i) != UNITS_PER_WORD)
4542 fprintf (file, "; %d bytes", PSEUDO_REGNO_BYTES (i));
4543 class = reg_preferred_class (i);
4544 altclass = reg_alternate_class (i);
4545 if (class != GENERAL_REGS || altclass != ALL_REGS)
4547 if (altclass == ALL_REGS || class == ALL_REGS)
4548 fprintf (file, "; pref %s", reg_class_names[(int) class]);
4549 else if (altclass == NO_REGS)
4550 fprintf (file, "; %s or none", reg_class_names[(int) class]);
4551 else
4552 fprintf (file, "; pref %s, else %s",
4553 reg_class_names[(int) class],
4554 reg_class_names[(int) altclass]);
4556 if (REGNO_POINTER_FLAG (i))
4557 fprintf (file, "; pointer");
4558 fprintf (file, ".\n");
4561 fprintf (file, "\n%d basic blocks.\n", n_basic_blocks);
4562 for (i = 0; i < n_basic_blocks; i++)
4564 register basic_block bb = BASIC_BLOCK (i);
4565 register int regno;
4566 register edge e;
4568 fprintf (file, "\nBasic block %d: first insn %d, last %d.\n",
4569 i, INSN_UID (bb->head), INSN_UID (bb->end));
4571 fprintf (file, "Predecessors: ");
4572 for (e = bb->pred; e ; e = e->pred_next)
4573 dump_edge_info (file, e, 0);
4575 fprintf (file, "\nSuccessors: ");
4576 for (e = bb->succ; e ; e = e->succ_next)
4577 dump_edge_info (file, e, 1);
4579 fprintf (file, "\nRegisters live at start:");
4580 if (bb->global_live_at_start)
4582 for (regno = 0; regno < max_regno; regno++)
4583 if (REGNO_REG_SET_P (bb->global_live_at_start, regno))
4584 fprintf (file, " %d", regno);
4586 else
4587 fprintf (file, " n/a");
4589 fprintf (file, "\nRegisters live at end:");
4590 if (bb->global_live_at_end)
4592 for (regno = 0; regno < max_regno; regno++)
4593 if (REGNO_REG_SET_P (bb->global_live_at_end, regno))
4594 fprintf (file, " %d", regno);
4596 else
4597 fprintf (file, " n/a");
4599 putc('\n', file);
4602 putc('\n', file);
4605 static void
4606 dump_edge_info (file, e, do_succ)
4607 FILE *file;
4608 edge e;
4609 int do_succ;
4611 basic_block side = (do_succ ? e->dest : e->src);
4613 if (side == ENTRY_BLOCK_PTR)
4614 fputs (" ENTRY", file);
4615 else if (side == EXIT_BLOCK_PTR)
4616 fputs (" EXIT", file);
4617 else
4618 fprintf (file, " %d", side->index);
4620 if (e->flags)
4622 static const char * const bitnames[] = {
4623 "fallthru", "crit", "ab", "abcall", "eh", "fake"
4625 int comma = 0;
4626 int i, flags = e->flags;
4628 fputc (' ', file);
4629 fputc ('(', file);
4630 for (i = 0; flags; i++)
4631 if (flags & (1 << i))
4633 flags &= ~(1 << i);
4635 if (comma)
4636 fputc (',', file);
4637 if (i < (int)(sizeof (bitnames) / sizeof (*bitnames)))
4638 fputs (bitnames[i], file);
4639 else
4640 fprintf (file, "%d", i);
4641 comma = 1;
4643 fputc (')', file);
4648 /* Like print_rtl, but also print out live information for the start of each
4649 basic block. */
4651 void
4652 print_rtl_with_bb (outf, rtx_first)
4653 FILE *outf;
4654 rtx rtx_first;
4656 register rtx tmp_rtx;
4658 if (rtx_first == 0)
4659 fprintf (outf, "(nil)\n");
4660 else
4662 int i;
4663 enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
4664 int max_uid = get_max_uid ();
4665 basic_block *start = (basic_block *)
4666 alloca (max_uid * sizeof (basic_block));
4667 basic_block *end = (basic_block *)
4668 alloca (max_uid * sizeof (basic_block));
4669 enum bb_state *in_bb_p = (enum bb_state *)
4670 alloca (max_uid * sizeof (enum bb_state));
4672 memset (start, 0, max_uid * sizeof (basic_block));
4673 memset (end, 0, max_uid * sizeof (basic_block));
4674 memset (in_bb_p, 0, max_uid * sizeof (enum bb_state));
4676 for (i = n_basic_blocks - 1; i >= 0; i--)
4678 basic_block bb = BASIC_BLOCK (i);
4679 rtx x;
4681 start[INSN_UID (bb->head)] = bb;
4682 end[INSN_UID (bb->end)] = bb;
4683 for (x = bb->head; x != NULL_RTX; x = NEXT_INSN (x))
4685 enum bb_state state = IN_MULTIPLE_BB;
4686 if (in_bb_p[INSN_UID(x)] == NOT_IN_BB)
4687 state = IN_ONE_BB;
4688 in_bb_p[INSN_UID(x)] = state;
4690 if (x == bb->end)
4691 break;
4695 for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx))
4697 int did_output;
4698 basic_block bb;
4700 if ((bb = start[INSN_UID (tmp_rtx)]) != NULL)
4702 fprintf (outf, ";; Start of basic block %d, registers live:",
4703 bb->index);
4705 EXECUTE_IF_SET_IN_REG_SET (bb->global_live_at_start, 0, i,
4707 fprintf (outf, " %d", i);
4708 if (i < FIRST_PSEUDO_REGISTER)
4709 fprintf (outf, " [%s]",
4710 reg_names[i]);
4712 putc ('\n', outf);
4715 if (in_bb_p[INSN_UID(tmp_rtx)] == NOT_IN_BB
4716 && GET_CODE (tmp_rtx) != NOTE
4717 && GET_CODE (tmp_rtx) != BARRIER
4718 && ! obey_regdecls)
4719 fprintf (outf, ";; Insn is not within a basic block\n");
4720 else if (in_bb_p[INSN_UID(tmp_rtx)] == IN_MULTIPLE_BB)
4721 fprintf (outf, ";; Insn is in multiple basic blocks\n");
4723 did_output = print_rtl_single (outf, tmp_rtx);
4725 if ((bb = end[INSN_UID (tmp_rtx)]) != NULL)
4726 fprintf (outf, ";; End of basic block %d\n", bb->index);
4728 if (did_output)
4729 putc ('\n', outf);
4733 if (current_function_epilogue_delay_list != 0)
4735 fprintf (outf, "\n;; Insns in epilogue delay list:\n\n");
4736 for (tmp_rtx = current_function_epilogue_delay_list; tmp_rtx != 0;
4737 tmp_rtx = XEXP (tmp_rtx, 1))
4738 print_rtl_single (outf, XEXP (tmp_rtx, 0));
4743 /* Integer list support. */
4745 /* Allocate a node from list *HEAD_PTR. */
4747 static int_list_ptr
4748 alloc_int_list_node (head_ptr)
4749 int_list_block **head_ptr;
4751 struct int_list_block *first_blk = *head_ptr;
4753 if (first_blk == NULL || first_blk->nodes_left <= 0)
4755 first_blk = (struct int_list_block *) xmalloc (sizeof (struct int_list_block));
4756 first_blk->nodes_left = INT_LIST_NODES_IN_BLK;
4757 first_blk->next = *head_ptr;
4758 *head_ptr = first_blk;
4761 first_blk->nodes_left--;
4762 return &first_blk->nodes[first_blk->nodes_left];
4765 /* Pointer to head of predecessor/successor block list. */
4766 static int_list_block *pred_int_list_blocks;
4768 /* Add a new node to integer list LIST with value VAL.
4769 LIST is a pointer to a list object to allow for different implementations.
4770 If *LIST is initially NULL, the list is empty.
4771 The caller must not care whether the element is added to the front or
4772 to the end of the list (to allow for different implementations). */
4774 static int_list_ptr
4775 add_int_list_node (blk_list, list, val)
4776 int_list_block **blk_list;
4777 int_list **list;
4778 int val;
4780 int_list_ptr p = alloc_int_list_node (blk_list);
4782 p->val = val;
4783 p->next = *list;
4784 *list = p;
4785 return p;
4788 /* Free the blocks of lists at BLK_LIST. */
4790 void
4791 free_int_list (blk_list)
4792 int_list_block **blk_list;
4794 int_list_block *p, *next;
4796 for (p = *blk_list; p != NULL; p = next)
4798 next = p->next;
4799 free (p);
4802 /* Mark list as empty for the next function we compile. */
4803 *blk_list = NULL;
4806 /* Predecessor/successor computation. */
4808 /* Mark PRED_BB a precessor of SUCC_BB,
4809 and conversely SUCC_BB a successor of PRED_BB. */
4811 static void
4812 add_pred_succ (pred_bb, succ_bb, s_preds, s_succs, num_preds, num_succs)
4813 int pred_bb;
4814 int succ_bb;
4815 int_list_ptr *s_preds;
4816 int_list_ptr *s_succs;
4817 int *num_preds;
4818 int *num_succs;
4820 if (succ_bb != EXIT_BLOCK)
4822 add_int_list_node (&pred_int_list_blocks, &s_preds[succ_bb], pred_bb);
4823 num_preds[succ_bb]++;
4825 if (pred_bb != ENTRY_BLOCK)
4827 add_int_list_node (&pred_int_list_blocks, &s_succs[pred_bb], succ_bb);
4828 num_succs[pred_bb]++;
4832 /* Convert edge lists into pred/succ lists for backward compatibility. */
4834 void
4835 compute_preds_succs (s_preds, s_succs, num_preds, num_succs)
4836 int_list_ptr *s_preds;
4837 int_list_ptr *s_succs;
4838 int *num_preds;
4839 int *num_succs;
4841 int i, n = n_basic_blocks;
4842 edge e;
4844 memset (s_preds, 0, n_basic_blocks * sizeof (int_list_ptr));
4845 memset (s_succs, 0, n_basic_blocks * sizeof (int_list_ptr));
4846 memset (num_preds, 0, n_basic_blocks * sizeof (int));
4847 memset (num_succs, 0, n_basic_blocks * sizeof (int));
4849 for (i = 0; i < n; ++i)
4851 basic_block bb = BASIC_BLOCK (i);
4853 for (e = bb->succ; e ; e = e->succ_next)
4854 add_pred_succ (i, e->dest->index, s_preds, s_succs,
4855 num_preds, num_succs);
4858 for (e = ENTRY_BLOCK_PTR->succ; e ; e = e->succ_next)
4859 add_pred_succ (ENTRY_BLOCK, e->dest->index, s_preds, s_succs,
4860 num_preds, num_succs);
4863 void
4864 dump_bb_data (file, preds, succs, live_info)
4865 FILE *file;
4866 int_list_ptr *preds;
4867 int_list_ptr *succs;
4868 int live_info;
4870 int bb;
4871 int_list_ptr p;
4873 fprintf (file, "BB data\n\n");
4874 for (bb = 0; bb < n_basic_blocks; bb++)
4876 fprintf (file, "BB %d, start %d, end %d\n", bb,
4877 INSN_UID (BLOCK_HEAD (bb)), INSN_UID (BLOCK_END (bb)));
4878 fprintf (file, " preds:");
4879 for (p = preds[bb]; p != NULL; p = p->next)
4881 int pred_bb = INT_LIST_VAL (p);
4882 if (pred_bb == ENTRY_BLOCK)
4883 fprintf (file, " entry");
4884 else
4885 fprintf (file, " %d", pred_bb);
4887 fprintf (file, "\n");
4888 fprintf (file, " succs:");
4889 for (p = succs[bb]; p != NULL; p = p->next)
4891 int succ_bb = INT_LIST_VAL (p);
4892 if (succ_bb == EXIT_BLOCK)
4893 fprintf (file, " exit");
4894 else
4895 fprintf (file, " %d", succ_bb);
4897 if (live_info)
4899 int regno;
4900 fprintf (file, "\nRegisters live at start:");
4901 for (regno = 0; regno < max_regno; regno++)
4902 if (REGNO_REG_SET_P (BASIC_BLOCK (bb)->global_live_at_start, regno))
4903 fprintf (file, " %d", regno);
4904 fprintf (file, "\n");
4906 fprintf (file, "\n");
4908 fprintf (file, "\n");
4911 /* Free basic block data storage. */
4913 void
4914 free_bb_mem ()
4916 free_int_list (&pred_int_list_blocks);
4919 /* Compute dominator relationships. */
4920 void
4921 compute_dominators (dominators, post_dominators, s_preds, s_succs)
4922 sbitmap *dominators;
4923 sbitmap *post_dominators;
4924 int_list_ptr *s_preds;
4925 int_list_ptr *s_succs;
4927 int bb, changed, passes;
4928 sbitmap *temp_bitmap;
4930 temp_bitmap = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
4931 sbitmap_vector_ones (dominators, n_basic_blocks);
4932 sbitmap_vector_ones (post_dominators, n_basic_blocks);
4933 sbitmap_vector_zero (temp_bitmap, n_basic_blocks);
4935 sbitmap_zero (dominators[0]);
4936 SET_BIT (dominators[0], 0);
4938 sbitmap_zero (post_dominators[n_basic_blocks - 1]);
4939 SET_BIT (post_dominators[n_basic_blocks - 1], 0);
4941 passes = 0;
4942 changed = 1;
4943 while (changed)
4945 changed = 0;
4946 for (bb = 1; bb < n_basic_blocks; bb++)
4948 sbitmap_intersect_of_predecessors (temp_bitmap[bb], dominators,
4949 bb, s_preds);
4950 SET_BIT (temp_bitmap[bb], bb);
4951 changed |= sbitmap_a_and_b (dominators[bb],
4952 dominators[bb],
4953 temp_bitmap[bb]);
4954 sbitmap_intersect_of_successors (temp_bitmap[bb], post_dominators,
4955 bb, s_succs);
4956 SET_BIT (temp_bitmap[bb], bb);
4957 changed |= sbitmap_a_and_b (post_dominators[bb],
4958 post_dominators[bb],
4959 temp_bitmap[bb]);
4961 passes++;
4964 free (temp_bitmap);
4967 /* Compute dominator relationships using new flow graph structures. */
4968 void
4969 compute_flow_dominators (dominators, post_dominators)
4970 sbitmap *dominators;
4971 sbitmap *post_dominators;
4973 int bb, changed, passes;
4974 sbitmap *temp_bitmap;
4976 temp_bitmap = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
4977 sbitmap_vector_ones (dominators, n_basic_blocks);
4978 sbitmap_vector_ones (post_dominators, n_basic_blocks);
4979 sbitmap_vector_zero (temp_bitmap, n_basic_blocks);
4981 sbitmap_zero (dominators[0]);
4982 SET_BIT (dominators[0], 0);
4984 sbitmap_zero (post_dominators[n_basic_blocks - 1]);
4985 SET_BIT (post_dominators[n_basic_blocks - 1], 0);
4987 passes = 0;
4988 changed = 1;
4989 while (changed)
4991 changed = 0;
4992 for (bb = 1; bb < n_basic_blocks; bb++)
4994 sbitmap_intersection_of_preds (temp_bitmap[bb], dominators, bb);
4995 SET_BIT (temp_bitmap[bb], bb);
4996 changed |= sbitmap_a_and_b (dominators[bb],
4997 dominators[bb],
4998 temp_bitmap[bb]);
4999 sbitmap_intersection_of_succs (temp_bitmap[bb], post_dominators, bb);
5000 SET_BIT (temp_bitmap[bb], bb);
5001 changed |= sbitmap_a_and_b (post_dominators[bb],
5002 post_dominators[bb],
5003 temp_bitmap[bb]);
5005 passes++;
5008 free (temp_bitmap);
5011 /* Given DOMINATORS, compute the immediate dominators into IDOM. */
5013 void
5014 compute_immediate_dominators (idom, dominators)
5015 int *idom;
5016 sbitmap *dominators;
5018 sbitmap *tmp;
5019 int b;
5021 tmp = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
5023 /* Begin with tmp(n) = dom(n) - { n }. */
5024 for (b = n_basic_blocks; --b >= 0; )
5026 sbitmap_copy (tmp[b], dominators[b]);
5027 RESET_BIT (tmp[b], b);
5030 /* Subtract out all of our dominator's dominators. */
5031 for (b = n_basic_blocks; --b >= 0; )
5033 sbitmap tmp_b = tmp[b];
5034 int s;
5036 for (s = n_basic_blocks; --s >= 0; )
5037 if (TEST_BIT (tmp_b, s))
5038 sbitmap_difference (tmp_b, tmp_b, tmp[s]);
5041 /* Find the one bit set in the bitmap and put it in the output array. */
5042 for (b = n_basic_blocks; --b >= 0; )
5044 int t;
5045 EXECUTE_IF_SET_IN_SBITMAP (tmp[b], 0, t, { idom[b] = t; });
5048 sbitmap_vector_free (tmp);
5051 /* Count for a single SET rtx, X. */
5053 static void
5054 count_reg_sets_1 (x)
5055 rtx x;
5057 register int regno;
5058 register rtx reg = SET_DEST (x);
5060 /* Find the register that's set/clobbered. */
5061 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
5062 || GET_CODE (reg) == SIGN_EXTRACT
5063 || GET_CODE (reg) == STRICT_LOW_PART)
5064 reg = XEXP (reg, 0);
5066 if (GET_CODE (reg) == PARALLEL
5067 && GET_MODE (reg) == BLKmode)
5069 register int i;
5070 for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
5071 count_reg_sets_1 (XVECEXP (reg, 0, i));
5072 return;
5075 if (GET_CODE (reg) == REG)
5077 regno = REGNO (reg);
5078 if (regno >= FIRST_PSEUDO_REGISTER)
5080 /* Count (weighted) references, stores, etc. This counts a
5081 register twice if it is modified, but that is correct. */
5082 REG_N_SETS (regno)++;
5084 REG_N_REFS (regno) += loop_depth;
5089 /* Increment REG_N_SETS for each SET or CLOBBER found in X; also increment
5090 REG_N_REFS by the current loop depth for each SET or CLOBBER found. */
5092 static void
5093 count_reg_sets (x)
5094 rtx x;
5096 register RTX_CODE code = GET_CODE (x);
5098 if (code == SET || code == CLOBBER)
5099 count_reg_sets_1 (x);
5100 else if (code == PARALLEL)
5102 register int i;
5103 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
5105 code = GET_CODE (XVECEXP (x, 0, i));
5106 if (code == SET || code == CLOBBER)
5107 count_reg_sets_1 (XVECEXP (x, 0, i));
5112 /* Increment REG_N_REFS by the current loop depth each register reference
5113 found in X. */
5115 static void
5116 count_reg_references (x)
5117 rtx x;
5119 register RTX_CODE code;
5121 retry:
5122 code = GET_CODE (x);
5123 switch (code)
5125 case LABEL_REF:
5126 case SYMBOL_REF:
5127 case CONST_INT:
5128 case CONST:
5129 case CONST_DOUBLE:
5130 case PC:
5131 case ADDR_VEC:
5132 case ADDR_DIFF_VEC:
5133 case ASM_INPUT:
5134 return;
5136 #ifdef HAVE_cc0
5137 case CC0:
5138 return;
5139 #endif
5141 case CLOBBER:
5142 /* If we are clobbering a MEM, mark any registers inside the address
5143 as being used. */
5144 if (GET_CODE (XEXP (x, 0)) == MEM)
5145 count_reg_references (XEXP (XEXP (x, 0), 0));
5146 return;
5148 case SUBREG:
5149 /* While we're here, optimize this case. */
5150 x = SUBREG_REG (x);
5152 /* In case the SUBREG is not of a register, don't optimize */
5153 if (GET_CODE (x) != REG)
5155 count_reg_references (x);
5156 return;
5159 /* ... fall through ... */
5161 case REG:
5162 if (REGNO (x) >= FIRST_PSEUDO_REGISTER)
5163 REG_N_REFS (REGNO (x)) += loop_depth;
5164 return;
5166 case SET:
5168 register rtx testreg = SET_DEST (x);
5169 int mark_dest = 0;
5171 /* If storing into MEM, don't show it as being used. But do
5172 show the address as being used. */
5173 if (GET_CODE (testreg) == MEM)
5175 count_reg_references (XEXP (testreg, 0));
5176 count_reg_references (SET_SRC (x));
5177 return;
5180 /* Storing in STRICT_LOW_PART is like storing in a reg
5181 in that this SET might be dead, so ignore it in TESTREG.
5182 but in some other ways it is like using the reg.
5184 Storing in a SUBREG or a bit field is like storing the entire
5185 register in that if the register's value is not used
5186 then this SET is not needed. */
5187 while (GET_CODE (testreg) == STRICT_LOW_PART
5188 || GET_CODE (testreg) == ZERO_EXTRACT
5189 || GET_CODE (testreg) == SIGN_EXTRACT
5190 || GET_CODE (testreg) == SUBREG)
5192 /* Modifying a single register in an alternate mode
5193 does not use any of the old value. But these other
5194 ways of storing in a register do use the old value. */
5195 if (GET_CODE (testreg) == SUBREG
5196 && !(REG_SIZE (SUBREG_REG (testreg)) > REG_SIZE (testreg)))
5198 else
5199 mark_dest = 1;
5201 testreg = XEXP (testreg, 0);
5204 /* If this is a store into a register,
5205 recursively scan the value being stored. */
5207 if ((GET_CODE (testreg) == PARALLEL
5208 && GET_MODE (testreg) == BLKmode)
5209 || GET_CODE (testreg) == REG)
5211 count_reg_references (SET_SRC (x));
5212 if (mark_dest)
5213 count_reg_references (SET_DEST (x));
5214 return;
5217 break;
5219 default:
5220 break;
5223 /* Recursively scan the operands of this expression. */
5226 register const char *fmt = GET_RTX_FORMAT (code);
5227 register int i;
5229 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5231 if (fmt[i] == 'e')
5233 /* Tail recursive case: save a function call level. */
5234 if (i == 0)
5236 x = XEXP (x, 0);
5237 goto retry;
5239 count_reg_references (XEXP (x, i));
5241 else if (fmt[i] == 'E')
5243 register int j;
5244 for (j = 0; j < XVECLEN (x, i); j++)
5245 count_reg_references (XVECEXP (x, i, j));
5251 /* Recompute register set/reference counts immediately prior to register
5252 allocation.
5254 This avoids problems with set/reference counts changing to/from values
5255 which have special meanings to the register allocators.
5257 Additionally, the reference counts are the primary component used by the
5258 register allocators to prioritize pseudos for allocation to hard regs.
5259 More accurate reference counts generally lead to better register allocation.
5261 F is the first insn to be scanned.
5262 LOOP_STEP denotes how much loop_depth should be incremented per
5263 loop nesting level in order to increase the ref count more for references
5264 in a loop.
5266 It might be worthwhile to update REG_LIVE_LENGTH, REG_BASIC_BLOCK and
5267 possibly other information which is used by the register allocators. */
5269 void
5270 recompute_reg_usage (f, loop_step)
5271 rtx f;
5272 int loop_step;
5274 rtx insn;
5275 int i, max_reg;
5277 /* Clear out the old data. */
5278 max_reg = max_reg_num ();
5279 for (i = FIRST_PSEUDO_REGISTER; i < max_reg; i++)
5281 REG_N_SETS (i) = 0;
5282 REG_N_REFS (i) = 0;
5285 /* Scan each insn in the chain and count how many times each register is
5286 set/used. */
5287 loop_depth = 1;
5288 for (insn = f; insn; insn = NEXT_INSN (insn))
5290 /* Keep track of loop depth. */
5291 if (GET_CODE (insn) == NOTE)
5293 /* Look for loop boundaries. */
5294 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
5295 loop_depth -= loop_step;
5296 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
5297 loop_depth += loop_step;
5299 /* If we have LOOP_DEPTH == 0, there has been a bookkeeping error.
5300 Abort now rather than setting register status incorrectly. */
5301 if (loop_depth == 0)
5302 abort ();
5304 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
5306 rtx links;
5308 /* This call will increment REG_N_SETS for each SET or CLOBBER
5309 of a register in INSN. It will also increment REG_N_REFS
5310 by the loop depth for each set of a register in INSN. */
5311 count_reg_sets (PATTERN (insn));
5313 /* count_reg_sets does not detect autoincrement address modes, so
5314 detect them here by looking at the notes attached to INSN. */
5315 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
5317 if (REG_NOTE_KIND (links) == REG_INC)
5318 /* Count (weighted) references, stores, etc. This counts a
5319 register twice if it is modified, but that is correct. */
5320 REG_N_SETS (REGNO (XEXP (links, 0)))++;
5323 /* This call will increment REG_N_REFS by the current loop depth for
5324 each reference to a register in INSN. */
5325 count_reg_references (PATTERN (insn));
5327 /* count_reg_references will not include counts for arguments to
5328 function calls, so detect them here by examining the
5329 CALL_INSN_FUNCTION_USAGE data. */
5330 if (GET_CODE (insn) == CALL_INSN)
5332 rtx note;
5334 for (note = CALL_INSN_FUNCTION_USAGE (insn);
5335 note;
5336 note = XEXP (note, 1))
5337 if (GET_CODE (XEXP (note, 0)) == USE)
5338 count_reg_references (XEXP (XEXP (note, 0), 0));
5344 /* Record INSN's block as BB. */
5346 void
5347 set_block_for_insn (insn, bb)
5348 rtx insn;
5349 basic_block bb;
5351 size_t uid = INSN_UID (insn);
5352 if (uid >= basic_block_for_insn->num_elements)
5354 int new_size;
5356 /* Add one-eighth the size so we don't keep calling xrealloc. */
5357 new_size = uid + (uid + 7) / 8;
5359 VARRAY_GROW (basic_block_for_insn, new_size);
5361 VARRAY_BB (basic_block_for_insn, uid) = bb;
5364 /* Record INSN's block number as BB. */
5365 /* ??? This has got to go. */
5367 void
5368 set_block_num (insn, bb)
5369 rtx insn;
5370 int bb;
5372 set_block_for_insn (insn, BASIC_BLOCK (bb));
5375 /* Unlink a chain of insns between START and FINISH inclusive, leaving notes
5376 that must be paired, and return the new chain. */
5379 unlink_insn_chain (start, finish)
5380 rtx start, finish;
5382 rtx insert_point = PREV_INSN (start);
5383 rtx chain = NULL_RTX, curr;
5385 /* Unchain the insns one by one. It would be quicker to delete all
5386 of these with a single unchaining, rather than one at a time, but
5387 we need to keep the NOTE's. */
5389 while (1)
5391 rtx next = NEXT_INSN (start);
5393 remove_insn (start);
5395 /* ??? Despite the fact that we're patching out the insn, it's
5396 still referenced in LOG_LINKS. Rather than try and track
5397 them all down and remove them, just mark the insn deleted. */
5398 INSN_DELETED_P (start) = 1;
5400 if (GET_CODE (start) == NOTE && ! can_delete_note_p (start))
5402 add_insn_after (start, insert_point);
5403 insert_point = start;
5405 else
5407 if (chain != NULL)
5409 NEXT_INSN (curr) = start;
5410 PREV_INSN (start) = curr;
5411 curr = start;
5413 else
5415 chain = start;
5416 curr = start;
5417 PREV_INSN (chain) = NULL_RTX;
5421 if (start == finish)
5422 break;
5423 start = next;
5426 if (chain != NULL_RTX)
5427 NEXT_INSN (curr) = NULL_RTX;
5429 return chain;
5432 /* Subroutine of update_life_info. Determines whether multiple
5433 REG_NOTEs need to be distributed for the hard register mentioned in
5434 NOTE. This can happen if a reference to a hard register in the
5435 original insns was split into several smaller hard register
5436 references in the new insns. */
5438 static void
5439 split_hard_reg_notes (curr_insn, note, first, last)
5440 rtx curr_insn, note, first, last;
5442 rtx reg, temp, link;
5443 rtx insn;
5444 int n_regs, i, new_reg;
5446 reg = XEXP (note, 0);
5448 if (REG_NOTE_KIND (note) != REG_DEAD
5449 || GET_CODE (reg) != REG
5450 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
5451 || HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg)) == 1)
5453 XEXP (note, 1) = REG_NOTES (curr_insn);
5454 REG_NOTES (curr_insn) = note;
5455 return;
5458 n_regs = HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg));
5460 for (i = 0; i < n_regs; i++)
5462 new_reg = REGNO (reg) + i;
5464 /* Check for references to new_reg in the split insns. */
5465 for (insn = last; ; insn = PREV_INSN (insn))
5467 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
5468 && (temp = regno_use_in (new_reg, PATTERN (insn))))
5470 /* Create a new reg dead note here. */
5471 link = rtx_alloc (EXPR_LIST);
5472 PUT_REG_NOTE_KIND (link, REG_DEAD);
5473 XEXP (link, 0) = temp;
5474 XEXP (link, 1) = REG_NOTES (insn);
5475 REG_NOTES (insn) = link;
5477 /* If killed multiple registers here, then add in the excess. */
5478 i += HARD_REGNO_NREGS (REGNO (temp), GET_MODE (temp)) - 1;
5480 break;
5482 /* It isn't mentioned anywhere, so no new reg note is needed for
5483 this register. */
5484 if (insn == first)
5485 break;
5490 /* SET_INSN kills REG; add a REG_DEAD note mentioning REG to the last
5491 use of REG in the insns after SET_INSN and before or including
5492 LAST, if necessary.
5494 A non-zero value is returned if we added a REG_DEAD note, or if we
5495 determined that a REG_DEAD note because of this particular SET
5496 wasn't necessary. */
5498 static int
5499 maybe_add_dead_note (reg, set_insn, last)
5500 rtx reg, set_insn, last;
5502 rtx insn;
5504 for (insn = last; insn != set_insn; insn = PREV_INSN (insn))
5506 rtx set;
5508 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
5509 && reg_overlap_mentioned_p (reg, PATTERN (insn))
5510 && (set = single_set (insn)))
5512 rtx insn_dest = SET_DEST (set);
5514 while (GET_CODE (insn_dest) == ZERO_EXTRACT
5515 || GET_CODE (insn_dest) == SUBREG
5516 || GET_CODE (insn_dest) == STRICT_LOW_PART
5517 || GET_CODE (insn_dest) == SIGN_EXTRACT)
5518 insn_dest = XEXP (insn_dest, 0);
5520 if (! rtx_equal_p (insn_dest, reg))
5522 /* Use the same scheme as combine.c, don't put both REG_DEAD
5523 and REG_UNUSED notes on the same insn. */
5524 if (! find_regno_note (insn, REG_UNUSED, REGNO (reg))
5525 && ! find_regno_note (insn, REG_DEAD, REGNO (reg)))
5527 rtx note = rtx_alloc (EXPR_LIST);
5528 PUT_REG_NOTE_KIND (note, REG_DEAD);
5529 XEXP (note, 0) = reg;
5530 XEXP (note, 1) = REG_NOTES (insn);
5531 REG_NOTES (insn) = note;
5533 return 1;
5535 else if (reg_overlap_mentioned_p (reg, SET_SRC (set)))
5537 /* We found an instruction that both uses the register and
5538 sets it, so no new REG_NOTE is needed for the previous
5539 set. */
5540 return 0;
5544 return 0;
5547 static int
5548 maybe_add_dead_note_use (insn, dest)
5549 rtx insn, dest;
5551 rtx set;
5553 /* We need to add a REG_DEAD note to the last place DEST is
5554 referenced. */
5556 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
5557 && reg_mentioned_p (dest, PATTERN (insn))
5558 && (set = single_set (insn)))
5560 rtx insn_dest = SET_DEST (set);
5562 while (GET_CODE (insn_dest) == ZERO_EXTRACT
5563 || GET_CODE (insn_dest) == SUBREG
5564 || GET_CODE (insn_dest) == STRICT_LOW_PART
5565 || GET_CODE (insn_dest) == SIGN_EXTRACT)
5566 insn_dest = XEXP (insn_dest, 0);
5568 if (! rtx_equal_p (insn_dest, dest))
5570 /* Use the same scheme as combine.c, don't put both REG_DEAD
5571 and REG_UNUSED notes on the same insn. */
5572 if (! find_regno_note (insn, REG_UNUSED, REGNO (dest))
5573 && ! find_regno_note (insn, REG_DEAD, REGNO (dest)))
5575 rtx note = rtx_alloc (EXPR_LIST);
5576 PUT_REG_NOTE_KIND (note, REG_DEAD);
5577 XEXP (note, 0) = dest;
5578 XEXP (note, 1) = REG_NOTES (insn);
5579 REG_NOTES (insn) = note;
5581 return 1;
5584 return 0;
5587 /* Find the first insn in the set of insns from FIRST to LAST inclusive
5588 that contains the note NOTE. */
5590 find_insn_with_note (note, first, last)
5591 rtx note, first, last;
5593 rtx insn;
5595 for (insn = first; insn != NULL_RTX; insn = NEXT_INSN (insn))
5597 rtx temp = find_reg_note (insn, REG_NOTE_KIND (note), XEXP (note, 0));
5598 if (temp == note)
5600 return insn;
5602 if (insn == last)
5604 break;
5607 return NULL_RTX;
5610 /* Subroutine of update_life_info. Determines whether a SET or
5611 CLOBBER in an insn created by splitting needs a REG_DEAD or
5612 REG_UNUSED note added. */
5614 static void
5615 new_insn_dead_notes (pat, insn, first, last, orig_first_insn, orig_last_insn)
5616 rtx pat, insn, first, last, orig_first_insn, orig_last_insn;
5618 rtx dest, tem;
5620 if (GET_CODE (pat) != CLOBBER && GET_CODE (pat) != SET)
5621 abort ();
5623 dest = XEXP (pat, 0);
5625 while (GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SUBREG
5626 || GET_CODE (dest) == STRICT_LOW_PART
5627 || GET_CODE (dest) == SIGN_EXTRACT)
5628 dest = XEXP (dest, 0);
5630 if (GET_CODE (dest) == REG)
5632 #if 0
5633 /* If the original insns already used this register, we may not
5634 add new notes for it. One example for a replacement that
5635 needs this test is when a multi-word memory access with
5636 register-indirect addressing is changed into multiple memory
5637 accesses with auto-increment and one adjusting add
5638 instruction for the address register.
5640 However, there is a problem with this code. We're assuming
5641 that any registers that are set in the new insns are either
5642 set/referenced in the old insns (and thus "inherit" the
5643 liveness of the old insns), or are registers that are dead
5644 before we enter this part of the stream (and thus should be
5645 dead when we leave).
5647 To do this absolutely correctly, we must determine the actual
5648 liveness of the registers before we go randomly adding
5649 REG_DEAD notes. This can probably be accurately done by
5650 calling mark_referenced_resources() on the old stream before
5651 replacing the old insns. */
5652 /* ??? The conclusion reached here -- that we can't add DEAD notes
5653 when the register is preexisting -- is false. I can't envision
5654 a sequence postulated above that wouldn't be properly handled
5655 by the code below. In the meantime, consider the 1->2 split
5657 (set (reg:SI 100) (ne:SI (reg:SI 100) (const_int 0)))
5659 (set (reg:CC icc) (compare:CC (reg:SI 100) (const_int 0)))
5660 (set (reg:SI 100) (ne:SI (reg:CC icc) (const_int 0)))
5662 We do in fact need a new DEAD note on the first insn for reg 100. */
5664 for (tem = orig_first_insn; tem != NULL_RTX; tem = NEXT_INSN (tem))
5666 if (GET_RTX_CLASS (GET_CODE (tem)) == 'i'
5667 && reg_referenced_p (dest, PATTERN (tem)))
5668 return;
5669 if (tem == orig_last_insn)
5670 break;
5672 #endif
5674 /* So it's a new register, presumably only used within this
5675 group of insns. Find the last insn in the set of new insns
5676 that DEST is referenced in, and add a dead note to it. */
5677 if (! maybe_add_dead_note (dest, insn, last))
5679 /* If this is a set, it must die somewhere, unless it is the
5680 dest of the original insn, and thus is live after the
5681 original insn. Abort if it isn't supposed to be live after
5682 the original insn.
5684 If this is a clobber, then just add a REG_UNUSED note. */
5685 if (GET_CODE (pat) == CLOBBER)
5687 rtx note = rtx_alloc (EXPR_LIST);
5688 PUT_REG_NOTE_KIND (note, REG_UNUSED);
5689 XEXP (note, 0) = dest;
5690 XEXP (note, 1) = REG_NOTES (insn);
5691 REG_NOTES (insn) = note;
5692 return;
5694 else
5696 rtx curr;
5697 int got_set = 0;
5699 for (curr = orig_first_insn; curr; curr = NEXT_INSN (curr))
5701 got_set = sets_reg_or_subreg (curr, dest);
5702 if (got_set)
5703 break;
5704 if (curr == orig_last_insn)
5705 break;
5708 /* In case reg was not used later, it is dead store.
5709 add REG_UNUSED note. */
5710 if (! got_set)
5712 rtx note = rtx_alloc (EXPR_LIST);
5713 PUT_REG_NOTE_KIND (note, REG_UNUSED);
5714 XEXP (note, 0) = dest;
5715 XEXP (note, 1) = REG_NOTES (insn);
5716 REG_NOTES (insn) = note;
5717 return;
5722 if (insn != first)
5724 rtx set = single_set (insn);
5726 /* If this is a set, scan backwards for a previous
5727 reference, and attach a REG_DEAD note to it. But we don't
5728 want to do it if the insn is both using and setting the
5729 register.
5731 Global registers are always live. */
5732 if (set && ! reg_overlap_mentioned_p (dest, SET_SRC (pat))
5733 && (REGNO (dest) >= FIRST_PSEUDO_REGISTER
5734 || ! global_regs[REGNO (dest)]))
5736 for (tem = PREV_INSN (insn);
5737 tem != NULL_RTX; tem = PREV_INSN (tem))
5739 if (maybe_add_dead_note_use (tem, dest))
5740 break;
5741 if (tem == first)
5742 break;
5749 /* Subroutine of update_life_info. Update the value of reg_n_sets for all
5750 registers modified by X. INC is -1 if the containing insn is being deleted,
5751 and is 1 if the containing insn is a newly generated insn. */
5753 static void
5754 update_n_sets (x, inc)
5755 rtx x;
5756 int inc;
5758 rtx dest = SET_DEST (x);
5760 while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SUBREG
5761 || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SIGN_EXTRACT)
5762 dest = SUBREG_REG (dest);
5764 if (GET_CODE (dest) == REG)
5766 int regno = REGNO (dest);
5768 if (regno < FIRST_PSEUDO_REGISTER)
5770 register int i;
5771 int endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (dest));
5773 for (i = regno; i < endregno; i++)
5774 REG_N_SETS (i) += inc;
5776 else
5777 REG_N_SETS (regno) += inc;
5781 /* Scan INSN for a SET that sets REG. If it sets REG via a SUBREG,
5782 then return 2. If it sets REG directly, return 1. Otherwise, return
5783 0. */
5785 static int sets_reg_or_subreg_ret;
5786 static rtx sets_reg_or_subreg_rtx;
5788 static void
5789 sets_reg_or_subreg_1 (x, set)
5790 rtx x, set;
5792 if (rtx_equal_p (x, sets_reg_or_subreg_rtx))
5794 if (x == XEXP (set, 0))
5795 sets_reg_or_subreg_ret = 1;
5796 else if (GET_CODE (XEXP (set, 0)) == SUBREG)
5797 sets_reg_or_subreg_ret = 2;
5801 static int
5802 sets_reg_or_subreg (insn, reg)
5803 rtx insn;
5804 rtx reg;
5806 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
5807 return 0;
5809 sets_reg_or_subreg_ret = 0;
5810 sets_reg_or_subreg_rtx = reg;
5811 note_stores (PATTERN (insn), sets_reg_or_subreg_1);
5812 return sets_reg_or_subreg_ret;
5815 /* If a replaced SET_INSN (which is part of the insns between
5816 OLD_FIRST_INSN and OLD_LAST_INSN inclusive) is modifying a multiple
5817 register target, and the original dest is now set in the new insns
5818 (between FIRST_INSN and LAST_INSN inclusive) by one or more subreg
5819 sets, then the new insns no longer kill the destination of the
5820 original insn.
5822 We may also be directly using the register in the new insns before
5823 setting it.
5825 In either case, if there exists an instruction in the same basic
5826 block before the replaced insns which uses the original dest (and
5827 contains a corresponding REG_DEAD note), then we must remove this
5828 REG_DEAD note.
5830 SET_INSN is the insn that contains the SET; it may be a PARALLEL
5831 containing the SET insn.
5833 SET is the actual SET insn proper. */
5835 static void
5836 maybe_remove_dead_notes (set_insn, set, first_insn, last_insn,
5837 old_first_insn, old_last_insn)
5838 rtx set_insn, set;
5839 rtx first_insn, last_insn;
5840 rtx old_first_insn, old_last_insn;
5842 rtx insn;
5843 rtx stop_insn = NEXT_INSN (last_insn);
5844 int set_type = 0;
5845 rtx set_dest;
5846 rtx set_pattern;
5848 if (GET_RTX_CLASS (GET_CODE (set)) != 'i')
5849 return;
5851 set_pattern = PATTERN (set);
5853 if (GET_CODE (set_pattern) == PARALLEL)
5855 int i;
5857 for (i = 0; i < XVECLEN (set_pattern, 0); i++)
5859 maybe_remove_dead_notes (set_insn, XVECEXP (set_pattern, 0, i),
5860 first_insn, last_insn,
5861 old_first_insn, old_last_insn);
5863 return;
5866 if (GET_CODE (set_pattern) != SET)
5868 return;
5871 set_dest = SET_DEST (set_pattern);
5873 if (GET_CODE (set_dest) != REG)
5875 return;
5878 /* We have a set of a REG. First we need to determine if this set is
5879 both using and setting the register. (FIXME: if this is in a
5880 PARALLEL, we will have to check the other exprs as well.) */
5881 if (reg_overlap_mentioned_p (set_dest, SET_SRC (set_pattern)))
5883 return;
5886 /* Now determine if we used or set the register in the old insns
5887 previous to this one. */
5889 for (insn = old_first_insn; insn != set_insn; insn = NEXT_INSN (insn))
5891 if (reg_overlap_mentioned_p (set_dest, insn))
5893 return;
5897 /* Now determine if we're setting it in the new insns, or using
5898 it. */
5899 for (insn = first_insn; insn != stop_insn; insn = NEXT_INSN (insn))
5901 set_type = sets_reg_or_subreg (insn, set_dest);
5902 if (set_type != 0)
5904 break;
5906 else if (reg_overlap_mentioned_p (set_dest, insn))
5908 /* Is the reg now used in this new insn? -- This is probably an
5909 error. */
5910 set_type = 2;
5911 break;
5914 if (set_type == 2)
5916 /* The register is being set via a SUBREG or is being used in
5917 some other way, so it's no longer dead.
5919 Search backwards from first_insn, looking for the first insn
5920 that uses the original dest. Stop if we pass a CODE_LABEL or
5921 a JUMP_INSN.
5923 If we find such an insn and it has a REG_DEAD note referring
5924 to the original dest, then delete the note. */
5926 for (insn = first_insn; insn != NULL_RTX; insn = PREV_INSN (insn))
5928 if (GET_CODE (insn) == CODE_LABEL
5929 || GET_CODE (insn) == JUMP_INSN)
5930 break;
5931 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
5932 && reg_mentioned_p (set_dest, insn))
5934 rtx note = find_regno_note (insn, REG_DEAD, REGNO (set_dest));
5935 if (note != NULL_RTX)
5937 remove_note (insn, note);
5939 /* ??? -- Is this right? */
5940 break;
5944 else if (set_type == 0)
5946 /* The reg is not being set or used in the new insns at all. */
5947 int i, regno;
5949 /* Should never reach here for a pseudo reg. */
5950 if (REGNO (set_dest) >= FIRST_PSEUDO_REGISTER)
5951 abort ();
5953 /* This can happen for a hard register, if the new insns do not
5954 contain instructions which would be no-ops referring to the
5955 old registers.
5957 We try to verify that this is the case by checking to see if
5958 the original instruction uses all of the registers that it
5959 set. This case is OK, because deleting a no-op can not affect
5960 REG_DEAD notes on other insns. If this is not the case, then
5961 abort. */
5963 regno = REGNO (set_dest);
5964 for (i = HARD_REGNO_NREGS (regno, GET_MODE (set_dest)) - 1;
5965 i >= 0; i--)
5967 if (! refers_to_regno_p (regno + i, regno + i + 1, set,
5968 NULL_PTR))
5969 break;
5971 if (i >= 0)
5972 abort ();
5976 /* Updates all flow-analysis related quantities (including REG_NOTES) for
5977 the insns from FIRST to LAST inclusive that were created by replacing
5978 the insns from ORIG_INSN_FIRST to ORIG_INSN_LAST inclusive. NOTES
5979 are the original REG_NOTES. */
5981 void
5982 update_life_info (notes, first, last, orig_first_insn, orig_last_insn)
5983 rtx notes;
5984 rtx first, last;
5985 rtx orig_first_insn, orig_last_insn;
5987 rtx insn, note;
5988 rtx next;
5989 rtx orig_dest, temp;
5990 rtx orig_insn;
5991 rtx tem;
5993 /* Get and save the destination set by the original insn, if there
5994 was only one insn replaced. */
5996 if (orig_first_insn == orig_last_insn)
5998 orig_insn = orig_first_insn;
5999 orig_dest = single_set (orig_insn);
6000 if (orig_dest)
6001 orig_dest = SET_DEST (orig_dest);
6003 else
6005 orig_insn = NULL_RTX;
6006 orig_dest = NULL_RTX;
6009 /* Move REG_NOTES from the original insns to where they now belong. */
6011 for (note = notes; note; note = next)
6013 next = XEXP (note, 1);
6014 switch (REG_NOTE_KIND (note))
6016 case REG_DEAD:
6017 case REG_UNUSED:
6018 /* Move these notes from the original insn to the last new
6019 insn where the register is mentioned. */
6021 for (insn = last; ; insn = PREV_INSN (insn))
6023 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
6024 && reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
6026 /* Sometimes need to convert REG_UNUSED notes to
6027 REG_DEAD notes. */
6028 if (REG_NOTE_KIND (note) == REG_UNUSED
6029 && GET_CODE (XEXP (note, 0)) == REG
6030 && ! dead_or_set_p (insn, XEXP (note, 0)))
6032 PUT_REG_NOTE_KIND (note, REG_DEAD);
6034 split_hard_reg_notes (insn, note, first, last);
6035 /* The reg only dies in one insn, the last one that uses
6036 it. */
6037 break;
6039 /* It must die somewhere, fail if we couldn't find where it died.
6041 We abort because otherwise the register will be live
6042 longer than it should, and we'll probably take an
6043 abort later. What we should do instead is search back
6044 and find the appropriate places to insert the note. */
6045 if (insn == first)
6047 if (REG_NOTE_KIND (note) == REG_DEAD)
6049 abort ();
6051 break;
6054 break;
6056 case REG_WAS_0:
6058 rtx note_dest;
6060 /* If the insn that set the register to 0 was deleted, this
6061 note cannot be relied on any longer. The destination might
6062 even have been moved to memory.
6063 This was observed for SH4 with execute/920501-6.c compilation,
6064 -O2 -fomit-frame-pointer -finline-functions . */
6066 if (GET_CODE (XEXP (note, 0)) == NOTE
6067 || INSN_DELETED_P (XEXP (note, 0)))
6068 break;
6069 if (orig_insn != NULL_RTX)
6071 note_dest = orig_dest;
6073 else
6075 note_dest = find_insn_with_note (note, orig_first_insn,
6076 orig_last_insn);
6077 if (note_dest != NULL_RTX)
6079 note_dest = single_set (note_dest);
6080 if (note_dest != NULL_RTX)
6081 note_dest = SET_DEST (note_dest);
6084 /* This note applies to the dest of the original insn. Find the
6085 first new insn that now has the same dest, and move the note
6086 there. */
6088 if (! note_dest)
6089 break;
6091 for (insn = first; ; insn = NEXT_INSN (insn))
6093 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
6094 && (temp = single_set (insn))
6095 && rtx_equal_p (SET_DEST (temp), note_dest))
6097 XEXP (note, 1) = REG_NOTES (insn);
6098 REG_NOTES (insn) = note;
6099 /* The reg is only zero before one insn, the first that
6100 uses it. */
6101 break;
6103 /* If this note refers to a multiple word hard
6104 register, it may have been split into several smaller
6105 hard register references. We could split the notes,
6106 but simply dropping them is good enough. */
6107 if (GET_CODE (note_dest) == REG
6108 && REGNO (note_dest) < FIRST_PSEUDO_REGISTER
6109 && HARD_REGNO_NREGS (REGNO (note_dest),
6110 GET_MODE (note_dest)) > 1)
6111 break;
6113 /* It must be set somewhere; bail if we couldn't find
6114 where it was set. */
6117 break;
6119 case REG_EQUAL:
6120 case REG_EQUIV:
6121 /* A REG_EQUIV or REG_EQUAL note on an insn with more than one
6122 set is meaningless. Just drop the note. */
6123 if (! orig_dest)
6124 break;
6126 case REG_NO_CONFLICT:
6127 case REG_NOALIAS:
6128 /* These notes apply to the dest of the original insn. Find the last
6129 new insn that now has the same dest, and move the note there.
6131 If we are replacing multiple insns, just drop the note. */
6133 if (! orig_insn)
6134 break;
6136 if (! orig_dest)
6137 abort ();
6139 for (insn = last; ; insn = PREV_INSN (insn))
6141 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
6142 && (temp = single_set (insn))
6143 && rtx_equal_p (SET_DEST (temp), orig_dest))
6145 XEXP (note, 1) = REG_NOTES (insn);
6146 REG_NOTES (insn) = note;
6147 /* Only put this note on one of the new insns. */
6148 break;
6151 /* The original dest must still be set someplace. Abort if we
6152 couldn't find it. */
6153 if (insn == first)
6155 /* However, if this note refers to a multiple word hard
6156 register, it may have been split into several smaller
6157 hard register references. We could split the notes,
6158 but simply dropping them is good enough. */
6159 if (GET_CODE (orig_dest) == REG
6160 && REGNO (orig_dest) < FIRST_PSEUDO_REGISTER
6161 && HARD_REGNO_NREGS (REGNO (orig_dest),
6162 GET_MODE (orig_dest)) > 1)
6163 break;
6164 /* Likewise for multi-word memory references. */
6165 if (GET_CODE (orig_dest) == MEM
6166 && GET_MODE_SIZE (GET_MODE (orig_dest)) > MOVE_MAX)
6167 break;
6168 abort ();
6171 break;
6173 case REG_LIBCALL:
6174 /* Move a REG_LIBCALL note to the first insn created, and update
6175 the corresponding REG_RETVAL note. */
6176 XEXP (note, 1) = REG_NOTES (first);
6177 REG_NOTES (first) = note;
6179 insn = XEXP (note, 0);
6180 note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
6181 if (note)
6182 XEXP (note, 0) = first;
6183 break;
6185 case REG_EXEC_COUNT:
6186 /* Move a REG_EXEC_COUNT note to the first insn created. */
6187 XEXP (note, 1) = REG_NOTES (first);
6188 REG_NOTES (first) = note;
6189 break;
6191 case REG_RETVAL:
6192 /* Move a REG_RETVAL note to the last insn created, and update
6193 the corresponding REG_LIBCALL note. */
6194 XEXP (note, 1) = REG_NOTES (last);
6195 REG_NOTES (last) = note;
6197 insn = XEXP (note, 0);
6198 note = find_reg_note (insn, REG_LIBCALL, NULL_RTX);
6199 if (note)
6200 XEXP (note, 0) = last;
6201 break;
6203 case REG_NONNEG:
6204 case REG_BR_PROB:
6205 /* This should be moved to whichever instruction is a JUMP_INSN. */
6207 for (insn = last; ; insn = PREV_INSN (insn))
6209 if (GET_CODE (insn) == JUMP_INSN)
6211 XEXP (note, 1) = REG_NOTES (insn);
6212 REG_NOTES (insn) = note;
6213 /* Only put this note on one of the new insns. */
6214 break;
6216 /* Fail if we couldn't find a JUMP_INSN. */
6217 if (insn == first)
6218 abort ();
6220 break;
6222 case REG_INC:
6223 /* reload sometimes leaves obsolete REG_INC notes around. */
6224 if (reload_completed)
6225 break;
6226 /* This should be moved to whichever instruction now has the
6227 increment operation. */
6228 abort ();
6230 case REG_LABEL:
6231 /* Should be moved to the new insn(s) which use the label. */
6232 for (insn = first; insn != NEXT_INSN (last); insn = NEXT_INSN (insn))
6233 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
6234 && reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
6235 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_LABEL,
6236 XEXP (note, 0),
6237 REG_NOTES (insn));
6238 break;
6240 case REG_CC_SETTER:
6241 case REG_CC_USER:
6242 /* These two notes will never appear until after reorg, so we don't
6243 have to handle them here. */
6244 default:
6245 abort ();
6249 /* Each new insn created has a new set. If the destination is a
6250 register, then this reg is now live across several insns, whereas
6251 previously the dest reg was born and died within the same insn.
6252 To reflect this, we now need a REG_DEAD note on the insn where
6253 this dest reg dies.
6255 Similarly, the new insns may have clobbers that need REG_UNUSED
6256 notes. */
6258 for (insn = first; ;insn = NEXT_INSN (insn))
6260 rtx pat;
6261 int i;
6263 pat = PATTERN (insn);
6264 if (GET_CODE (pat) == SET || GET_CODE (pat) == CLOBBER)
6265 new_insn_dead_notes (pat, insn, first, last,
6266 orig_first_insn, orig_last_insn);
6267 else if (GET_CODE (pat) == PARALLEL)
6269 for (i = 0; i < XVECLEN (pat, 0); i++)
6271 if (GET_CODE (XVECEXP (pat, 0, i)) == SET
6272 || GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER)
6274 rtx parpat = XVECEXP (pat, 0, i);
6276 new_insn_dead_notes (parpat, insn, first, last,
6277 orig_first_insn, orig_last_insn);
6281 if (insn == last)
6283 break;
6287 /* Check to see if we have any REG_DEAD notes on insns previous to
6288 the new ones that are now incorrect and need to be removed. */
6290 for (insn = orig_first_insn; ; insn = NEXT_INSN (insn))
6292 maybe_remove_dead_notes (insn, insn, first, last,
6293 orig_first_insn, orig_last_insn);
6295 if (insn == orig_last_insn)
6296 break;
6299 /* Update reg_n_sets. This is necessary to prevent local alloc from
6300 converting REG_EQUAL notes to REG_EQUIV when the new insns are setting
6301 a reg multiple times instead of once. */
6303 for (tem = orig_first_insn; tem != NULL_RTX; tem = NEXT_INSN (tem))
6305 rtx x;
6306 RTX_CODE code;
6308 if (GET_RTX_CLASS (GET_CODE (tem)) != 'i')
6309 continue;
6311 x = PATTERN (tem);
6312 code = GET_CODE (x);
6313 if (code == SET || code == CLOBBER)
6314 update_n_sets (x, -1);
6315 else if (code == PARALLEL)
6317 int i;
6318 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
6320 code = GET_CODE (XVECEXP (x, 0, i));
6321 if (code == SET || code == CLOBBER)
6322 update_n_sets (XVECEXP (x, 0, i), -1);
6325 if (tem == orig_last_insn)
6326 break;
6329 for (insn = first; ; insn = NEXT_INSN (insn))
6331 rtx x = PATTERN (insn);
6332 RTX_CODE code = GET_CODE (x);
6334 if (code == SET || code == CLOBBER)
6335 update_n_sets (x, 1);
6336 else if (code == PARALLEL)
6338 int i;
6339 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
6341 code = GET_CODE (XVECEXP (x, 0, i));
6342 if (code == SET || code == CLOBBER)
6343 update_n_sets (XVECEXP (x, 0, i), 1);
6347 if (insn == last)
6348 break;
6352 /* Prepends the set of REG_NOTES in NEW to NOTES, and returns NEW. */
6353 static rtx
6354 prepend_reg_notes (notes, new)
6355 rtx notes, new;
6357 rtx end;
6359 if (new == NULL_RTX)
6361 return notes;
6363 if (notes == NULL_RTX)
6365 return new;
6367 end = new;
6368 while (XEXP (end, 1) != NULL_RTX)
6370 end = XEXP (end, 1);
6372 XEXP (end, 1) = notes;
6373 return new;
6376 /* Replace the insns from FIRST to LAST inclusive with the set of insns in
6377 NEW, and update the life analysis info accordingly. */
6378 void
6379 replace_insns (first, last, first_new, notes)
6380 rtx first, last, first_new, notes;
6382 rtx stop = NEXT_INSN (last);
6383 rtx prev = PREV_INSN (first);
6384 rtx last_new, curr;
6385 int i;
6387 if (notes == NULL_RTX)
6389 for (curr = first; curr != stop; curr = NEXT_INSN (curr))
6390 if (GET_RTX_CLASS (GET_CODE (curr)) == 'i')
6391 notes = prepend_reg_notes (notes, REG_NOTES (curr));
6394 last_new = emit_insn_after (first_new, prev);
6395 first_new = NEXT_INSN (prev);
6397 for (i = 0; i < n_basic_blocks; i++)
6399 if (BLOCK_HEAD (i) == first)
6400 BLOCK_HEAD (i) = first_new;
6401 if (BLOCK_END (i) == last)
6402 BLOCK_END (i) = last_new;
6404 /* This is probably bogus. */
6405 if (first_new == last_new)
6407 if (GET_CODE (first_new) == SEQUENCE)
6409 first_new = XVECEXP (first_new, 0, 0);
6410 last_new = XVECEXP (last_new, 0, XVECLEN (last_new, 0) - 1);
6413 update_life_info (notes, first_new, last_new, first, last);
6414 flow_delete_insn_chain (first, last);
6417 /* Verify the CFG consistency. This function check some CFG invariants and
6418 aborts when something is wrong. Hope that this function will help to
6419 convert many optimization passes to preserve CFG consistent.
6421 Currently it does following checks:
6423 - test head/end pointers
6424 - overlapping of basic blocks
6425 - edge list corectness
6426 - headers of basic blocks (the NOTE_INSN_BASIC_BLOCK note)
6427 - tails of basic blocks (ensure that boundary is necesary)
6428 - scans body of the basic block for JUMP_INSN, CODE_LABEL
6429 and NOTE_INSN_BASIC_BLOCK
6430 - check that all insns are in the basic blocks
6431 (except the switch handling code, barriers and notes)
6433 In future it can be extended check a lot of other stuff as well
6434 (reachability of basic blocks, life information, etc. etc.). */
6436 void
6437 verify_flow_info ()
6439 const int max_uid = get_max_uid ();
6440 const rtx rtx_first = get_insns ();
6441 basic_block *bb_info;
6442 rtx x;
6443 int i;
6445 bb_info = (basic_block *) alloca (max_uid * sizeof (basic_block));
6446 memset (bb_info, 0, max_uid * sizeof (basic_block));
6448 /* First pass check head/end pointers and set bb_info array used by
6449 later passes. */
6450 for (i = n_basic_blocks - 1; i >= 0; i--)
6452 basic_block bb = BASIC_BLOCK (i);
6454 /* Check the head pointer and make sure that it is pointing into
6455 insn list. */
6456 for (x = rtx_first; x != NULL_RTX; x = NEXT_INSN (x))
6457 if (x == bb->head)
6458 break;
6459 if (!x)
6461 error ("Head insn %d for block %d not found in the insn stream.",
6462 INSN_UID (bb->head), bb->index);
6463 abort ();
6466 /* Check the end pointer and make sure that it is pointing into
6467 insn list. */
6468 for (x = bb->head; x != NULL_RTX; x = NEXT_INSN (x))
6470 if (bb_info[INSN_UID (x)] != NULL)
6472 error ("Insn %d is in multiple basic blocks (%d and %d)",
6473 INSN_UID (x), bb->index, bb_info[INSN_UID (x)]->index);
6474 abort ();
6476 bb_info[INSN_UID (x)] = bb;
6478 if (x == bb->end)
6479 break;
6481 if (!x)
6483 error ("End insn %d for block %d not found in the insn stream.",
6484 INSN_UID (bb->end), bb->index);
6485 abort ();
6489 /* Now check the basic blocks (boundaries etc.) */
6490 for (i = n_basic_blocks - 1; i >= 0; i--)
6492 basic_block bb = BASIC_BLOCK (i);
6493 /* Check corectness of edge lists */
6494 edge e;
6496 e = bb->succ;
6497 while (e)
6499 if (e->src != bb)
6501 fprintf (stderr, "verify_flow_info: Basic block %d succ edge is corrupted\n",
6502 bb->index);
6503 fprintf (stderr, "Predecessor: ");
6504 dump_edge_info (stderr, e, 0);
6505 fprintf (stderr, "\nSuccessor: ");
6506 dump_edge_info (stderr, e, 1);
6507 fflush (stderr);
6508 abort ();
6510 if (e->dest != EXIT_BLOCK_PTR)
6512 edge e2 = e->dest->pred;
6513 while (e2 && e2 != e)
6514 e2 = e2->pred_next;
6515 if (!e2)
6517 error ("Basic block %i edge lists are corrupted", bb->index);
6518 abort ();
6521 e = e->succ_next;
6524 e = bb->pred;
6525 while (e)
6527 if (e->dest != bb)
6529 error ("Basic block %d pred edge is corrupted", bb->index);
6530 fputs ("Predecessor: ", stderr);
6531 dump_edge_info (stderr, e, 0);
6532 fputs ("\nSuccessor: ", stderr);
6533 dump_edge_info (stderr, e, 1);
6534 fputc ('\n', stderr);
6535 abort ();
6537 if (e->src != ENTRY_BLOCK_PTR)
6539 edge e2 = e->src->succ;
6540 while (e2 && e2 != e)
6541 e2 = e2->succ_next;
6542 if (!e2)
6544 error ("Basic block %i edge lists are corrupted", bb->index);
6545 abort ();
6548 e = e->pred_next;
6551 /* OK pointers are correct. Now check the header of basic
6552 block. It ought to contain optional CODE_LABEL followed
6553 by NOTE_BASIC_BLOCK. */
6554 x = bb->head;
6555 if (GET_CODE (x) == CODE_LABEL)
6557 if (bb->end == x)
6559 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
6560 bb->index);
6561 abort ();
6563 x = NEXT_INSN (x);
6565 if (GET_CODE (x) != NOTE
6566 || NOTE_LINE_NUMBER (x) != NOTE_INSN_BASIC_BLOCK
6567 || NOTE_BASIC_BLOCK (x) != bb)
6569 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d\n",
6570 bb->index);
6571 abort ();
6574 if (bb->end == x)
6576 /* Do checks for empty blocks here */
6578 else
6580 x = NEXT_INSN (x);
6581 while (x)
6583 if (GET_CODE (x) == NOTE
6584 && NOTE_LINE_NUMBER (x) == NOTE_INSN_BASIC_BLOCK)
6586 error ("NOTE_INSN_BASIC_BLOCK %d in the middle of basic block %d",
6587 INSN_UID (x), bb->index);
6588 abort ();
6591 if (x == bb->end)
6592 break;
6594 if (GET_CODE (x) == JUMP_INSN
6595 || GET_CODE (x) == CODE_LABEL
6596 || GET_CODE (x) == BARRIER)
6598 error ("In basic block %d:", bb->index);
6599 fatal_insn ("Flow control insn inside a basic block", x);
6602 x = NEXT_INSN (x);
6607 x = rtx_first;
6608 while (x)
6610 if (!bb_info[INSN_UID (x)])
6612 switch (GET_CODE (x))
6614 case BARRIER:
6615 case NOTE:
6616 break;
6618 case CODE_LABEL:
6619 /* An addr_vec is placed outside any block block. */
6620 if (NEXT_INSN (x)
6621 && GET_CODE (NEXT_INSN (x)) == JUMP_INSN
6622 && (GET_CODE (PATTERN (NEXT_INSN (x))) == ADDR_DIFF_VEC
6623 || GET_CODE (PATTERN (NEXT_INSN (x))) == ADDR_VEC))
6625 x = NEXT_INSN (x);
6628 /* But in any case, non-deletable labels can appear anywhere. */
6629 break;
6631 default:
6632 fatal_insn ("Insn outside basic block", x);
6636 x = NEXT_INSN (x);
6640 /* Functions to access an edge list with a vector representation.
6641 Enough data is kept such that given an index number, the
6642 pred and succ that edge reprsents can be determined, or
6643 given a pred and a succ, it's index number can be returned.
6644 This allows algorithms which comsume a lot of memory to
6645 represent the normally full matrix of edge (pred,succ) with a
6646 single indexed vector, edge (EDGE_INDEX (pred, succ)), with no
6647 wasted space in the client code due to sparse flow graphs. */
6649 /* This functions initializes the edge list. Basically the entire
6650 flowgraph is processed, and all edges are assigned a number,
6651 and the data structure is filed in. */
6652 struct edge_list *
6653 create_edge_list ()
6655 struct edge_list *elist;
6656 edge e;
6657 int num_edges;
6658 int x;
6659 int block_count;
6661 block_count = n_basic_blocks + 2; /* Include the entry and exit blocks. */
6663 num_edges = 0;
6665 /* Determine the number of edges in the flow graph by counting successor
6666 edges on each basic block. */
6667 for (x = 0; x < n_basic_blocks; x++)
6669 basic_block bb = BASIC_BLOCK (x);
6671 for (e = bb->succ; e; e = e->succ_next)
6672 num_edges++;
6674 /* Don't forget successors of the entry block. */
6675 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
6676 num_edges++;
6678 elist = xmalloc (sizeof (struct edge_list));
6679 elist->num_blocks = block_count;
6680 elist->num_edges = num_edges;
6681 elist->index_to_edge = xmalloc (sizeof (edge) * num_edges);
6683 num_edges = 0;
6685 /* Follow successors of the entry block, and register these edges. */
6686 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
6688 elist->index_to_edge[num_edges] = e;
6689 num_edges++;
6692 for (x = 0; x < n_basic_blocks; x++)
6694 basic_block bb = BASIC_BLOCK (x);
6696 /* Follow all successors of blocks, and register these edges. */
6697 for (e = bb->succ; e; e = e->succ_next)
6699 elist->index_to_edge[num_edges] = e;
6700 num_edges++;
6703 return elist;
6706 /* This function free's memory associated with an edge list. */
6707 void
6708 free_edge_list (elist)
6709 struct edge_list *elist;
6711 if (elist)
6713 free (elist->index_to_edge);
6714 free (elist);
6718 /* This function provides debug output showing an edge list. */
6719 void
6720 print_edge_list (f, elist)
6721 FILE *f;
6722 struct edge_list *elist;
6724 int x;
6725 fprintf(f, "Compressed edge list, %d BBs + entry & exit, and %d edges\n",
6726 elist->num_blocks - 2, elist->num_edges);
6728 for (x = 0; x < elist->num_edges; x++)
6730 fprintf (f, " %-4d - edge(", x);
6731 if (INDEX_EDGE_PRED_BB (elist, x) == ENTRY_BLOCK_PTR)
6732 fprintf (f,"entry,");
6733 else
6734 fprintf (f,"%d,", INDEX_EDGE_PRED_BB (elist, x)->index);
6736 if (INDEX_EDGE_SUCC_BB (elist, x) == EXIT_BLOCK_PTR)
6737 fprintf (f,"exit)\n");
6738 else
6739 fprintf (f,"%d)\n", INDEX_EDGE_SUCC_BB (elist, x)->index);
6743 /* This function provides an internal consistancy check of an edge list,
6744 verifying that all edges are present, and that there are no
6745 extra edges. */
6746 void
6747 verify_edge_list (f, elist)
6748 FILE *f;
6749 struct edge_list *elist;
6751 int x, pred, succ, index;
6752 edge e;
6754 for (x = 0; x < n_basic_blocks; x++)
6756 basic_block bb = BASIC_BLOCK (x);
6758 for (e = bb->succ; e; e = e->succ_next)
6760 pred = e->src->index;
6761 succ = e->dest->index;
6762 index = EDGE_INDEX (elist, e->src, e->dest);
6763 if (index == EDGE_INDEX_NO_EDGE)
6765 fprintf (f, "*p* No index for edge from %d to %d\n",pred, succ);
6766 continue;
6768 if (INDEX_EDGE_PRED_BB (elist, index)->index != pred)
6769 fprintf (f, "*p* Pred for index %d should be %d not %d\n",
6770 index, pred, INDEX_EDGE_PRED_BB (elist, index)->index);
6771 if (INDEX_EDGE_SUCC_BB (elist, index)->index != succ)
6772 fprintf (f, "*p* Succ for index %d should be %d not %d\n",
6773 index, succ, INDEX_EDGE_SUCC_BB (elist, index)->index);
6776 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
6778 pred = e->src->index;
6779 succ = e->dest->index;
6780 index = EDGE_INDEX (elist, e->src, e->dest);
6781 if (index == EDGE_INDEX_NO_EDGE)
6783 fprintf (f, "*p* No index for edge from %d to %d\n",pred, succ);
6784 continue;
6786 if (INDEX_EDGE_PRED_BB (elist, index)->index != pred)
6787 fprintf (f, "*p* Pred for index %d should be %d not %d\n",
6788 index, pred, INDEX_EDGE_PRED_BB (elist, index)->index);
6789 if (INDEX_EDGE_SUCC_BB (elist, index)->index != succ)
6790 fprintf (f, "*p* Succ for index %d should be %d not %d\n",
6791 index, succ, INDEX_EDGE_SUCC_BB (elist, index)->index);
6793 /* We've verified that all the edges are in the list, no lets make sure
6794 there are no spurious edges in the list. */
6796 for (pred = 0 ; pred < n_basic_blocks; pred++)
6797 for (succ = 0 ; succ < n_basic_blocks; succ++)
6799 basic_block p = BASIC_BLOCK (pred);
6800 basic_block s = BASIC_BLOCK (succ);
6802 int found_edge = 0;
6804 for (e = p->succ; e; e = e->succ_next)
6805 if (e->dest == s)
6807 found_edge = 1;
6808 break;
6810 for (e = s->pred; e; e = e->pred_next)
6811 if (e->src == p)
6813 found_edge = 1;
6814 break;
6816 if (EDGE_INDEX (elist, BASIC_BLOCK (pred), BASIC_BLOCK (succ))
6817 == EDGE_INDEX_NO_EDGE && found_edge != 0)
6818 fprintf (f, "*** Edge (%d, %d) appears to not have an index\n",
6819 pred, succ);
6820 if (EDGE_INDEX (elist, BASIC_BLOCK (pred), BASIC_BLOCK (succ))
6821 != EDGE_INDEX_NO_EDGE && found_edge == 0)
6822 fprintf (f, "*** Edge (%d, %d) has index %d, but there is no edge\n",
6823 pred, succ, EDGE_INDEX (elist, BASIC_BLOCK (pred),
6824 BASIC_BLOCK (succ)));
6826 for (succ = 0 ; succ < n_basic_blocks; succ++)
6828 basic_block p = ENTRY_BLOCK_PTR;
6829 basic_block s = BASIC_BLOCK (succ);
6831 int found_edge = 0;
6833 for (e = p->succ; e; e = e->succ_next)
6834 if (e->dest == s)
6836 found_edge = 1;
6837 break;
6839 for (e = s->pred; e; e = e->pred_next)
6840 if (e->src == p)
6842 found_edge = 1;
6843 break;
6845 if (EDGE_INDEX (elist, ENTRY_BLOCK_PTR, BASIC_BLOCK (succ))
6846 == EDGE_INDEX_NO_EDGE && found_edge != 0)
6847 fprintf (f, "*** Edge (entry, %d) appears to not have an index\n",
6848 succ);
6849 if (EDGE_INDEX (elist, ENTRY_BLOCK_PTR, BASIC_BLOCK (succ))
6850 != EDGE_INDEX_NO_EDGE && found_edge == 0)
6851 fprintf (f, "*** Edge (entry, %d) has index %d, but no edge exists\n",
6852 succ, EDGE_INDEX (elist, ENTRY_BLOCK_PTR,
6853 BASIC_BLOCK (succ)));
6855 for (pred = 0 ; pred < n_basic_blocks; pred++)
6857 basic_block p = BASIC_BLOCK (pred);
6858 basic_block s = EXIT_BLOCK_PTR;
6860 int found_edge = 0;
6862 for (e = p->succ; e; e = e->succ_next)
6863 if (e->dest == s)
6865 found_edge = 1;
6866 break;
6868 for (e = s->pred; e; e = e->pred_next)
6869 if (e->src == p)
6871 found_edge = 1;
6872 break;
6874 if (EDGE_INDEX (elist, BASIC_BLOCK (pred), EXIT_BLOCK_PTR)
6875 == EDGE_INDEX_NO_EDGE && found_edge != 0)
6876 fprintf (f, "*** Edge (%d, exit) appears to not have an index\n",
6877 pred);
6878 if (EDGE_INDEX (elist, BASIC_BLOCK (pred), EXIT_BLOCK_PTR)
6879 != EDGE_INDEX_NO_EDGE && found_edge == 0)
6880 fprintf (f, "*** Edge (%d, exit) has index %d, but no edge exists\n",
6881 pred, EDGE_INDEX (elist, BASIC_BLOCK (pred),
6882 EXIT_BLOCK_PTR));
6886 /* This routine will determine what, if any, edge there is between
6887 a specified predecessor and successor. */
6890 find_edge_index (edge_list, pred, succ)
6891 struct edge_list *edge_list;
6892 basic_block pred, succ;
6894 int x;
6895 for (x = 0; x < NUM_EDGES (edge_list); x++)
6897 if (INDEX_EDGE_PRED_BB (edge_list, x) == pred
6898 && INDEX_EDGE_SUCC_BB (edge_list, x) == succ)
6899 return x;
6901 return (EDGE_INDEX_NO_EDGE);
6904 /* This function will remove an edge from the flow graph. */
6905 static void
6906 remove_edge (e)
6907 edge e;
6909 edge last_pred = NULL;
6910 edge last_succ = NULL;
6911 edge tmp;
6912 basic_block src, dest;
6913 src = e->src;
6914 dest = e->dest;
6915 for (tmp = src->succ; tmp && tmp != e; tmp = tmp->succ_next)
6916 last_succ = tmp;
6918 if (!tmp)
6919 abort ();
6920 if (last_succ)
6921 last_succ->succ_next = e->succ_next;
6922 else
6923 src->succ = e->succ_next;
6925 for (tmp = dest->pred; tmp && tmp != e; tmp = tmp->pred_next)
6926 last_pred = tmp;
6928 if (!tmp)
6929 abort ();
6930 if (last_pred)
6931 last_pred->pred_next = e->pred_next;
6932 else
6933 dest->pred = e->pred_next;
6935 free (e);
6938 /* This routine will remove any fake successor edges for a basic block.
6939 When the edge is removed, it is also removed from whatever predecessor
6940 list it is in. */
6941 static void
6942 remove_fake_successors (bb)
6943 basic_block bb;
6945 edge e;
6946 for (e = bb->succ; e ; )
6948 edge tmp = e;
6949 e = e->succ_next;
6950 if ((tmp->flags & EDGE_FAKE) == EDGE_FAKE)
6951 remove_edge (tmp);
6955 /* This routine will remove all fake edges from the flow graph. If
6956 we remove all fake successors, it will automatically remove all
6957 fake predecessors. */
6958 void
6959 remove_fake_edges ()
6961 int x;
6962 edge e;
6963 basic_block bb;
6965 for (x = 0; x < n_basic_blocks; x++)
6967 edge tmp, last = NULL;
6968 bb = BASIC_BLOCK (x);
6969 remove_fake_successors (bb);
6971 /* We've handled all successors except the entry block's. */
6972 remove_fake_successors (ENTRY_BLOCK_PTR);
6975 /* This functions will add a fake edge between any block which has no
6976 successors, and the exit block. Some data flow equations require these
6977 edges to exist. */
6978 void
6979 add_noreturn_fake_exit_edges ()
6981 int x;
6983 for (x = 0; x < n_basic_blocks; x++)
6984 if (BASIC_BLOCK (x)->succ == NULL)
6985 make_edge (BASIC_BLOCK (x), EXIT_BLOCK_PTR, EDGE_FAKE);