* final.c (shorten_branches): Fix last change.
[official-gcc.git] / gcc / flow.c
blob9ee464022a3b0c372dec792c628baee51dd4ba1d
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 "basic-block.h"
125 #include "insn-config.h"
126 #include "regs.h"
127 #include "hard-reg-set.h"
128 #include "flags.h"
129 #include "output.h"
130 #include "function.h"
131 #include "except.h"
132 #include "toplev.h"
133 #include "recog.h"
134 #include "insn-flags.h"
136 #include "obstack.h"
138 #define obstack_chunk_alloc xmalloc
139 #define obstack_chunk_free free
142 /* EXIT_IGNORE_STACK should be nonzero if, when returning from a function,
143 the stack pointer does not matter. The value is tested only in
144 functions that have frame pointers.
145 No definition is equivalent to always zero. */
146 #ifndef EXIT_IGNORE_STACK
147 #define EXIT_IGNORE_STACK 0
148 #endif
151 /* The contents of the current function definition are allocated
152 in this obstack, and all are freed at the end of the function.
153 For top-level functions, this is temporary_obstack.
154 Separate obstacks are made for nested functions. */
156 extern struct obstack *function_obstack;
158 /* Number of basic blocks in the current function. */
160 int n_basic_blocks;
162 /* The basic block array. */
164 varray_type basic_block_info;
166 /* The special entry and exit blocks. */
168 struct basic_block_def entry_exit_blocks[2] =
171 NULL, /* head */
172 NULL, /* end */
173 NULL, /* pred */
174 NULL, /* succ */
175 NULL, /* local_set */
176 NULL, /* global_live_at_start */
177 NULL, /* global_live_at_end */
178 NULL, /* aux */
179 ENTRY_BLOCK, /* index */
180 0 /* loop_depth */
183 NULL, /* head */
184 NULL, /* end */
185 NULL, /* pred */
186 NULL, /* succ */
187 NULL, /* local_set */
188 NULL, /* global_live_at_start */
189 NULL, /* global_live_at_end */
190 NULL, /* aux */
191 EXIT_BLOCK, /* index */
192 0 /* loop_depth */
196 /* Nonzero if the second flow pass has completed. */
197 int flow2_completed;
199 /* Maximum register number used in this function, plus one. */
201 int max_regno;
203 /* Indexed by n, giving various register information */
205 varray_type reg_n_info;
207 /* Size of the reg_n_info table. */
209 unsigned int reg_n_max;
211 /* Element N is the next insn that uses (hard or pseudo) register number N
212 within the current basic block; or zero, if there is no such insn.
213 This is valid only during the final backward scan in propagate_block. */
215 static rtx *reg_next_use;
217 /* Size of a regset for the current function,
218 in (1) bytes and (2) elements. */
220 int regset_bytes;
221 int regset_size;
223 /* Regset of regs live when calls to `setjmp'-like functions happen. */
224 /* ??? Does this exist only for the setjmp-clobbered warning message? */
226 regset regs_live_at_setjmp;
228 /* List made of EXPR_LIST rtx's which gives pairs of pseudo registers
229 that have to go in the same hard reg.
230 The first two regs in the list are a pair, and the next two
231 are another pair, etc. */
232 rtx regs_may_share;
234 /* Depth within loops of basic block being scanned for lifetime analysis,
235 plus one. This is the weight attached to references to registers. */
237 static int loop_depth;
239 /* During propagate_block, this is non-zero if the value of CC0 is live. */
241 static int cc0_live;
243 /* During propagate_block, this contains a list of all the MEMs we are
244 tracking for dead store elimination.
246 ?!? Note we leak memory by not free-ing items on this list. We need to
247 write some generic routines to operate on memory lists since cse, gcse,
248 loop, sched, flow and possibly other passes all need to do basically the
249 same operations on these lists. */
251 static rtx mem_set_list;
253 /* Set of registers that may be eliminable. These are handled specially
254 in updating regs_ever_live. */
256 static HARD_REG_SET elim_reg_set;
258 /* The basic block structure for every insn, indexed by uid. */
260 varray_type basic_block_for_insn;
262 /* The labels mentioned in non-jump rtl. Valid during find_basic_blocks. */
263 /* ??? Should probably be using LABEL_NUSES instead. It would take a
264 bit of surgery to be able to use or co-opt the routines in jump. */
266 static rtx label_value_list;
268 /* INSN_VOLATILE (insn) is 1 if the insn refers to anything volatile. */
270 #define INSN_VOLATILE(INSN) bitmap_bit_p (uid_volatile, INSN_UID (INSN))
271 #define SET_INSN_VOLATILE(INSN) bitmap_set_bit (uid_volatile, INSN_UID (INSN))
272 static bitmap uid_volatile;
274 /* Forward declarations */
275 static int count_basic_blocks PROTO((rtx));
276 static rtx find_basic_blocks_1 PROTO((rtx, rtx*));
277 static void create_basic_block PROTO((int, rtx, rtx, rtx));
278 static void compute_bb_for_insn PROTO((varray_type, int));
279 static void clear_edges PROTO((void));
280 static void make_edges PROTO((rtx, rtx*));
281 static void make_edge PROTO((basic_block, basic_block, int));
282 static void make_label_edge PROTO((basic_block, rtx, int));
283 static void mark_critical_edges PROTO((void));
285 static void commit_one_edge_insertion PROTO((edge));
287 static void delete_unreachable_blocks PROTO((void));
288 static void delete_eh_regions PROTO((void));
289 static int can_delete_note_p PROTO((rtx));
290 static void flow_delete_insn_chain PROTO((rtx, rtx));
291 static int delete_block PROTO((basic_block));
292 static void expunge_block PROTO((basic_block));
293 static rtx flow_delete_insn PROTO((rtx));
294 static int can_delete_label_p PROTO((rtx));
295 static void merge_blocks_nomove PROTO((basic_block, basic_block));
296 static int merge_blocks PROTO((edge,basic_block,basic_block));
297 static void tidy_fallthru_edge PROTO((edge,basic_block,basic_block));
298 static void calculate_loop_depth PROTO((rtx));
300 static int set_noop_p PROTO((rtx));
301 static int noop_move_p PROTO((rtx));
302 static void notice_stack_pointer_modification PROTO ((rtx, rtx));
303 static void record_volatile_insns PROTO((rtx));
304 static void mark_regs_live_at_end PROTO((regset));
305 static void life_analysis_1 PROTO((rtx, int, int));
306 static void init_regset_vector PROTO ((regset *, int,
307 struct obstack *));
308 static void propagate_block PROTO((regset, rtx, rtx, int,
309 regset, int, int));
310 static int insn_dead_p PROTO((rtx, regset, int, rtx));
311 static int libcall_dead_p PROTO((rtx, regset, rtx, rtx));
312 static void mark_set_regs PROTO((regset, regset, rtx,
313 rtx, regset));
314 static void mark_set_1 PROTO((regset, regset, rtx,
315 rtx, regset));
316 #ifdef AUTO_INC_DEC
317 static void find_auto_inc PROTO((regset, rtx, rtx));
318 static int try_pre_increment_1 PROTO((rtx));
319 static int try_pre_increment PROTO((rtx, rtx, HOST_WIDE_INT));
320 #endif
321 static void mark_used_regs PROTO((regset, regset, rtx, int, rtx));
322 void dump_flow_info PROTO((FILE *));
323 static void dump_edge_info PROTO((FILE *, edge, int));
325 static int_list_ptr alloc_int_list_node PROTO ((int_list_block **));
326 static int_list_ptr add_int_list_node PROTO ((int_list_block **,
327 int_list **, int));
329 static void add_pred_succ PROTO ((int, int, int_list_ptr *,
330 int_list_ptr *, int *, int *));
332 static void count_reg_sets_1 PROTO ((rtx));
333 static void count_reg_sets PROTO ((rtx));
334 static void count_reg_references PROTO ((rtx));
335 static void notice_stack_pointer_modification PROTO ((rtx, rtx));
336 static void invalidate_mems_from_autoinc PROTO ((rtx));
337 static void maybe_remove_dead_notes PROTO ((rtx, rtx, rtx, rtx,
338 rtx, rtx));
339 static int maybe_add_dead_note_use PROTO ((rtx, rtx));
340 static int maybe_add_dead_note PROTO ((rtx, rtx, rtx));
341 static int sets_reg_or_subreg PROTO ((rtx, rtx));
342 static void update_n_sets PROTO ((rtx, int));
343 static void new_insn_dead_notes PROTO ((rtx, rtx, rtx, rtx, rtx, rtx));
344 void verify_flow_info PROTO ((void));
346 /* Find basic blocks of the current function.
347 F is the first insn of the function and NREGS the number of register
348 numbers in use. */
350 void
351 find_basic_blocks (f, nregs, file, do_cleanup)
352 rtx f;
353 int nregs ATTRIBUTE_UNUSED;
354 FILE *file ATTRIBUTE_UNUSED;
355 int do_cleanup;
357 rtx *bb_eh_end;
358 int max_uid;
360 /* Flush out existing data. */
361 if (basic_block_info != NULL)
363 int i;
365 clear_edges ();
367 /* Clear bb->aux on all extant basic blocks. We'll use this as a
368 tag for reuse during create_basic_block, just in case some pass
369 copies around basic block notes improperly. */
370 for (i = 0; i < n_basic_blocks; ++i)
371 BASIC_BLOCK (i)->aux = NULL;
373 VARRAY_FREE (basic_block_info);
376 n_basic_blocks = count_basic_blocks (f);
378 /* Size the basic block table. The actual structures will be allocated
379 by find_basic_blocks_1, since we want to keep the structure pointers
380 stable across calls to find_basic_blocks. */
381 /* ??? This whole issue would be much simpler if we called find_basic_blocks
382 exactly once, and thereafter we don't have a single long chain of
383 instructions at all until close to the end of compilation when we
384 actually lay them out. */
386 VARRAY_BB_INIT (basic_block_info, n_basic_blocks, "basic_block_info");
388 /* An array to record the active exception region at the end of each
389 basic block. It is filled in by find_basic_blocks_1 for make_edges. */
390 bb_eh_end = (rtx *) alloca (n_basic_blocks * sizeof (rtx));
392 label_value_list = find_basic_blocks_1 (f, bb_eh_end);
394 /* Record the block to which an insn belongs. */
395 /* ??? This should be done another way, by which (perhaps) a label is
396 tagged directly with the basic block that it starts. It is used for
397 more than that currently, but IMO that is the only valid use. */
399 max_uid = get_max_uid ();
400 #ifdef AUTO_INC_DEC
401 /* Leave space for insns life_analysis makes in some cases for auto-inc.
402 These cases are rare, so we don't need too much space. */
403 max_uid += max_uid / 10;
404 #endif
406 VARRAY_BB_INIT (basic_block_for_insn, max_uid, "basic_block_for_insn");
407 compute_bb_for_insn (basic_block_for_insn, max_uid);
409 /* Discover the edges of our cfg. */
411 make_edges (label_value_list, bb_eh_end);
413 /* Delete unreachable blocks. */
415 if (do_cleanup)
416 delete_unreachable_blocks ();
418 /* Mark critical edges. */
420 mark_critical_edges ();
422 /* Discover the loop depth at the start of each basic block to aid
423 register allocation. */
424 calculate_loop_depth (f);
426 /* Kill the data we won't maintain. */
427 label_value_list = 0;
429 #ifdef ENABLE_CHECKING
430 verify_flow_info ();
431 #endif
434 /* Count the basic blocks of the function. */
436 static int
437 count_basic_blocks (f)
438 rtx f;
440 register rtx insn;
441 register RTX_CODE prev_code;
442 register int count = 0;
443 int eh_region = 0;
444 int call_had_abnormal_edge = 0;
445 rtx prev_call = NULL_RTX;
447 prev_code = JUMP_INSN;
448 for (insn = f; insn; insn = NEXT_INSN (insn))
450 register RTX_CODE code = GET_CODE (insn);
452 if (code == CODE_LABEL
453 || (GET_RTX_CLASS (code) == 'i'
454 && (prev_code == JUMP_INSN
455 || prev_code == BARRIER
456 || (prev_code == CALL_INSN && call_had_abnormal_edge))))
458 count++;
460 /* If the previous insn was a call that did not create an
461 abnormal edge, we want to add a nop so that the CALL_INSN
462 itself is not at basic_block_end. This allows us to
463 easily distinguish between normal calls and those which
464 create abnormal edges in the flow graph. */
466 if (count > 0 && prev_call != 0 && !call_had_abnormal_edge)
468 rtx nop = gen_rtx_USE (VOIDmode, const0_rtx);
469 emit_insn_after (nop, prev_call);
473 /* Record whether this call created an edge. */
474 if (code == CALL_INSN)
476 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
477 int region = (note ? XWINT (XEXP (note, 0), 0) : 1);
478 prev_call = insn;
479 call_had_abnormal_edge = 0;
481 /* If there is a specified EH region, we have an edge. */
482 if (eh_region && region > 0)
483 call_had_abnormal_edge = 1;
484 else
486 /* If there is a nonlocal goto label and the specified
487 region number isn't -1, we have an edge. (0 means
488 no throw, but might have a nonlocal goto). */
489 if (nonlocal_goto_handler_labels && region >= 0)
490 call_had_abnormal_edge = 1;
493 else if (code != NOTE)
494 prev_call = NULL_RTX;
496 if (code != NOTE)
497 prev_code = code;
498 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG)
499 ++eh_region;
500 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END)
501 --eh_region;
505 /* The rest of the compiler works a bit smoother when we don't have to
506 check for the edge case of do-nothing functions with no basic blocks. */
507 if (count == 0)
509 emit_insn (gen_rtx_USE (VOIDmode, const0_rtx));
510 count = 1;
513 return count;
516 /* Find all basic blocks of the function whose first insn is F.
517 Store the correct data in the tables that describe the basic blocks,
518 set up the chains of references for each CODE_LABEL, and
519 delete any entire basic blocks that cannot be reached.
521 NONLOCAL_LABEL_LIST is a list of non-local labels in the function. Blocks
522 that are otherwise unreachable may be reachable with a non-local goto.
524 BB_EH_END is an array in which we record the list of exception regions
525 active at the end of every basic block. */
527 static rtx
528 find_basic_blocks_1 (f, bb_eh_end)
529 rtx f;
530 rtx *bb_eh_end;
532 register rtx insn, next;
533 int call_has_abnormal_edge = 0;
534 int i = 0;
535 rtx bb_note = NULL_RTX;
536 rtx eh_list = NULL_RTX;
537 rtx label_value_list = NULL_RTX;
538 rtx head = NULL_RTX;
539 rtx end = NULL_RTX;
541 /* We process the instructions in a slightly different way than we did
542 previously. This is so that we see a NOTE_BASIC_BLOCK after we have
543 closed out the previous block, so that it gets attached at the proper
544 place. Since this form should be equivalent to the previous,
545 find_basic_blocks_0 continues to use the old form as a check. */
547 for (insn = f; insn; insn = next)
549 enum rtx_code code = GET_CODE (insn);
551 next = NEXT_INSN (insn);
553 if (code == CALL_INSN)
555 /* Record whether this call created an edge. */
556 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
557 int region = (note ? XWINT (XEXP (note, 0), 0) : 1);
558 call_has_abnormal_edge = 0;
560 /* If there is an EH region, we have an edge. */
561 if (eh_list && region > 0)
562 call_has_abnormal_edge = 1;
563 else
565 /* If there is a nonlocal goto label and the specified
566 region number isn't -1, we have an edge. (0 means
567 no throw, but might have a nonlocal goto). */
568 if (nonlocal_goto_handler_labels && region >= 0)
569 call_has_abnormal_edge = 1;
573 switch (code)
575 case NOTE:
577 int kind = NOTE_LINE_NUMBER (insn);
579 /* Keep a LIFO list of the currently active exception notes. */
580 if (kind == NOTE_INSN_EH_REGION_BEG)
581 eh_list = gen_rtx_INSN_LIST (VOIDmode, insn, eh_list);
582 else if (kind == NOTE_INSN_EH_REGION_END)
583 eh_list = XEXP (eh_list, 1);
585 /* Look for basic block notes with which to keep the
586 basic_block_info pointers stable. Unthread the note now;
587 we'll put it back at the right place in create_basic_block.
588 Or not at all if we've already found a note in this block. */
589 else if (kind == NOTE_INSN_BASIC_BLOCK)
591 if (bb_note == NULL_RTX)
592 bb_note = insn;
593 next = flow_delete_insn (insn);
596 break;
599 case CODE_LABEL:
600 /* A basic block starts at a label. If we've closed one off due
601 to a barrier or some such, no need to do it again. */
602 if (head != NULL_RTX)
604 /* While we now have edge lists with which other portions of
605 the compiler might determine a call ending a basic block
606 does not imply an abnormal edge, it will be a bit before
607 everything can be updated. So continue to emit a noop at
608 the end of such a block. */
609 if (GET_CODE (end) == CALL_INSN)
611 rtx nop = gen_rtx_USE (VOIDmode, const0_rtx);
612 end = emit_insn_after (nop, end);
615 bb_eh_end[i] = eh_list;
616 create_basic_block (i++, head, end, bb_note);
617 bb_note = NULL_RTX;
619 head = end = insn;
620 break;
622 case JUMP_INSN:
623 /* A basic block ends at a jump. */
624 if (head == NULL_RTX)
625 head = insn;
626 else
628 /* ??? Make a special check for table jumps. The way this
629 happens is truely and amazingly gross. We are about to
630 create a basic block that contains just a code label and
631 an addr*vec jump insn. Worse, an addr_diff_vec creates
632 its own natural loop.
634 Prevent this bit of brain damage, pasting things together
635 correctly in make_edges.
637 The correct solution involves emitting the table directly
638 on the tablejump instruction as a note, or JUMP_LABEL. */
640 if (GET_CODE (PATTERN (insn)) == ADDR_VEC
641 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
643 head = end = NULL;
644 n_basic_blocks--;
645 break;
648 end = insn;
649 goto new_bb_inclusive;
651 case BARRIER:
652 /* A basic block ends at a barrier. It may be that an unconditional
653 jump already closed the basic block -- no need to do it again. */
654 if (head == NULL_RTX)
655 break;
657 /* While we now have edge lists with which other portions of the
658 compiler might determine a call ending a basic block does not
659 imply an abnormal edge, it will be a bit before everything can
660 be updated. So continue to emit a noop at the end of such a
661 block. */
662 if (GET_CODE (end) == CALL_INSN)
664 rtx nop = gen_rtx_USE (VOIDmode, const0_rtx);
665 end = emit_insn_after (nop, end);
667 goto new_bb_exclusive;
669 case CALL_INSN:
670 /* A basic block ends at a call that can either throw or
671 do a non-local goto. */
672 if (call_has_abnormal_edge)
674 new_bb_inclusive:
675 if (head == NULL_RTX)
676 head = insn;
677 end = insn;
679 new_bb_exclusive:
680 bb_eh_end[i] = eh_list;
681 create_basic_block (i++, head, end, bb_note);
682 head = end = NULL_RTX;
683 bb_note = NULL_RTX;
684 break;
686 /* FALLTHRU */
688 default:
689 if (GET_RTX_CLASS (code) == 'i')
691 if (head == NULL_RTX)
692 head = insn;
693 end = insn;
695 break;
698 if (GET_RTX_CLASS (code) == 'i')
700 rtx note;
702 /* Make a list of all labels referred to other than by jumps
703 (which just don't have the REG_LABEL notes).
705 Make a special exception for labels followed by an ADDR*VEC,
706 as this would be a part of the tablejump setup code.
708 Make a special exception for the eh_return_stub_label, which
709 we know isn't part of any otherwise visible control flow. */
711 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
712 if (REG_NOTE_KIND (note) == REG_LABEL)
714 rtx lab = XEXP (note, 0), next;
716 if (lab == eh_return_stub_label)
718 else if ((next = next_nonnote_insn (lab)) != NULL
719 && GET_CODE (next) == JUMP_INSN
720 && (GET_CODE (PATTERN (next)) == ADDR_VEC
721 || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC))
723 else
724 label_value_list
725 = gen_rtx_EXPR_LIST (VOIDmode, XEXP (note, 0),
726 label_value_list);
731 if (head != NULL_RTX)
733 bb_eh_end[i] = eh_list;
734 create_basic_block (i++, head, end, bb_note);
737 if (i != n_basic_blocks)
738 abort ();
740 return label_value_list;
743 /* Create a new basic block consisting of the instructions between
744 HEAD and END inclusive. Reuses the note and basic block struct
745 in BB_NOTE, if any. */
747 static void
748 create_basic_block (index, head, end, bb_note)
749 int index;
750 rtx head, end, bb_note;
752 basic_block bb;
754 if (bb_note
755 && ! RTX_INTEGRATED_P (bb_note)
756 && (bb = NOTE_BASIC_BLOCK (bb_note)) != NULL
757 && bb->aux == NULL)
759 /* If we found an existing note, thread it back onto the chain. */
761 if (GET_CODE (head) == CODE_LABEL)
762 add_insn_after (bb_note, head);
763 else
765 add_insn_before (bb_note, head);
766 head = bb_note;
769 else
771 /* Otherwise we must create a note and a basic block structure.
772 Since we allow basic block structs in rtl, give the struct
773 the same lifetime by allocating it off the function obstack
774 rather than using malloc. */
776 bb = (basic_block) obstack_alloc (function_obstack, sizeof (*bb));
777 memset (bb, 0, sizeof (*bb));
779 if (GET_CODE (head) == CODE_LABEL)
780 bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK, head);
781 else
783 bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, head);
784 head = bb_note;
786 NOTE_BASIC_BLOCK (bb_note) = bb;
789 /* Always include the bb note in the block. */
790 if (NEXT_INSN (end) == bb_note)
791 end = bb_note;
793 bb->head = head;
794 bb->end = end;
795 bb->index = index;
796 BASIC_BLOCK (index) = bb;
798 /* Tag the block so that we know it has been used when considering
799 other basic block notes. */
800 bb->aux = bb;
803 /* Records the basic block struct in BB_FOR_INSN, for every instruction
804 indexed by INSN_UID. MAX is the size of the array. */
806 static void
807 compute_bb_for_insn (bb_for_insn, max)
808 varray_type bb_for_insn;
809 int max;
811 int i;
813 for (i = 0; i < n_basic_blocks; ++i)
815 basic_block bb = BASIC_BLOCK (i);
816 rtx insn, end;
818 end = bb->end;
819 insn = bb->head;
820 while (1)
822 int uid = INSN_UID (insn);
823 if (uid < max)
824 VARRAY_BB (bb_for_insn, uid) = bb;
825 if (insn == end)
826 break;
827 insn = NEXT_INSN (insn);
832 /* Free the memory associated with the edge structures. */
834 static void
835 clear_edges ()
837 int i;
838 edge n, e;
840 for (i = 0; i < n_basic_blocks; ++i)
842 basic_block bb = BASIC_BLOCK (i);
844 for (e = bb->succ; e ; e = n)
846 n = e->succ_next;
847 free (e);
850 bb->succ = 0;
851 bb->pred = 0;
854 for (e = ENTRY_BLOCK_PTR->succ; e ; e = n)
856 n = e->succ_next;
857 free (e);
860 ENTRY_BLOCK_PTR->succ = 0;
861 EXIT_BLOCK_PTR->pred = 0;
864 /* Identify the edges between basic blocks.
866 NONLOCAL_LABEL_LIST is a list of non-local labels in the function. Blocks
867 that are otherwise unreachable may be reachable with a non-local goto.
869 BB_EH_END is an array indexed by basic block number in which we record
870 the list of exception regions active at the end of the basic block. */
872 static void
873 make_edges (label_value_list, bb_eh_end)
874 rtx label_value_list;
875 rtx *bb_eh_end;
877 int i;
878 eh_nesting_info *eh_nest_info = init_eh_nesting_info ();
880 /* Assume no computed jump; revise as we create edges. */
881 current_function_has_computed_jump = 0;
883 /* By nature of the way these get numbered, block 0 is always the entry. */
884 make_edge (ENTRY_BLOCK_PTR, BASIC_BLOCK (0), EDGE_FALLTHRU);
886 for (i = 0; i < n_basic_blocks; ++i)
888 basic_block bb = BASIC_BLOCK (i);
889 rtx insn, x, eh_list;
890 enum rtx_code code;
891 int force_fallthru = 0;
893 /* If we have asynchronous exceptions, scan the notes for all exception
894 regions active in the block. In the normal case, we only need the
895 one active at the end of the block, which is bb_eh_end[i]. */
897 eh_list = bb_eh_end[i];
898 if (asynchronous_exceptions)
900 for (insn = bb->end; insn != bb->head; insn = PREV_INSN (insn))
902 if (GET_CODE (insn) == NOTE
903 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END)
904 eh_list = gen_rtx_INSN_LIST (VOIDmode, insn, eh_list);
908 /* Now examine the last instruction of the block, and discover the
909 ways we can leave the block. */
911 insn = bb->end;
912 code = GET_CODE (insn);
914 /* A branch. */
915 if (code == JUMP_INSN)
917 rtx tmp;
919 /* ??? Recognize a tablejump and do the right thing. */
920 if ((tmp = JUMP_LABEL (insn)) != NULL_RTX
921 && (tmp = NEXT_INSN (tmp)) != NULL_RTX
922 && GET_CODE (tmp) == JUMP_INSN
923 && (GET_CODE (PATTERN (tmp)) == ADDR_VEC
924 || GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC))
926 rtvec vec;
927 int j;
929 if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
930 vec = XVEC (PATTERN (tmp), 0);
931 else
932 vec = XVEC (PATTERN (tmp), 1);
934 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
935 make_label_edge (bb, XEXP (RTVEC_ELT (vec, j), 0), 0);
937 /* Some targets (eg, ARM) emit a conditional jump that also
938 contains the out-of-range target. Scan for these and
939 add an edge if necessary. */
940 if ((tmp = single_set (insn)) != NULL
941 && SET_DEST (tmp) == pc_rtx
942 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
943 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
944 make_label_edge (bb, XEXP (XEXP (SET_SRC (tmp), 2), 0), 0);
946 #ifdef CASE_DROPS_THROUGH
947 /* Silly VAXen. The ADDR_VEC is going to be in the way of
948 us naturally detecting fallthru into the next block. */
949 force_fallthru = 1;
950 #endif
953 /* If this is a computed jump, then mark it as reaching
954 everything on the label_value_list and forced_labels list. */
955 else if (computed_jump_p (insn))
957 current_function_has_computed_jump = 1;
959 for (x = label_value_list; x; x = XEXP (x, 1))
960 make_label_edge (bb, XEXP (x, 0), EDGE_ABNORMAL);
962 for (x = forced_labels; x; x = XEXP (x, 1))
963 make_label_edge (bb, XEXP (x, 0), EDGE_ABNORMAL);
966 /* Returns create an exit out. */
967 else if (returnjump_p (insn))
968 make_edge (bb, EXIT_BLOCK_PTR, 0);
970 /* Otherwise, we have a plain conditional or unconditional jump. */
971 else
973 if (! JUMP_LABEL (insn))
974 abort ();
975 make_label_edge (bb, JUMP_LABEL (insn), 0);
979 /* If this is a CALL_INSN, then mark it as reaching the active EH
980 handler for this CALL_INSN. If we're handling asynchronous
981 exceptions then any insn can reach any of the active handlers.
983 Also mark the CALL_INSN as reaching any nonlocal goto handler. */
985 if (code == CALL_INSN || asynchronous_exceptions)
987 int is_call = (code == CALL_INSN ? EDGE_ABNORMAL_CALL : 0);
988 handler_info **handler_list;
989 int eh_region = -1;
990 int num;
992 if (eh_list)
993 eh_region = NOTE_BLOCK_NUMBER (XEXP (eh_list, 0));
995 num = reachable_handlers (eh_region, eh_nest_info,
996 insn, &handler_list);
997 for ( ; num > 0; num--)
999 make_label_edge (bb, handler_list[num - 1]->handler_label,
1000 EDGE_ABNORMAL | EDGE_EH | is_call);
1003 if (code == CALL_INSN && nonlocal_goto_handler_labels)
1005 /* ??? This could be made smarter: in some cases it's possible
1006 to tell that certain calls will not do a nonlocal goto.
1008 For example, if the nested functions that do the nonlocal
1009 gotos do not have their addresses taken, then only calls to
1010 those functions or to other nested functions that use them
1011 could possibly do nonlocal gotos. */
1012 /* We do know that a REG_EH_REGION note with a value less
1013 than 0 is guaranteed not to perform a non-local goto. */
1014 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1015 if (!note || XINT (XEXP (note, 0), 0) >= 0)
1016 for (x = nonlocal_goto_handler_labels; x ; x = XEXP (x, 1))
1017 make_label_edge (bb, XEXP (x, 0),
1018 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
1022 /* We know something about the structure of the function __throw in
1023 libgcc2.c. It is the only function that ever contains eh_stub
1024 labels. It modifies its return address so that the last block
1025 returns to one of the eh_stub labels within it. So we have to
1026 make additional edges in the flow graph. */
1027 if (i + 1 == n_basic_blocks && eh_return_stub_label != 0)
1028 make_label_edge (bb, eh_return_stub_label, EDGE_EH);
1030 /* Find out if we can drop through to the next block. */
1031 insn = next_nonnote_insn (insn);
1032 if (!insn || (i + 1 == n_basic_blocks && force_fallthru))
1033 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
1034 else if (i + 1 < n_basic_blocks)
1036 rtx tmp = BLOCK_HEAD (i + 1);
1037 if (GET_CODE (tmp) == NOTE)
1038 tmp = next_nonnote_insn (tmp);
1039 if (force_fallthru || insn == tmp)
1040 make_edge (bb, BASIC_BLOCK (i + 1), EDGE_FALLTHRU);
1043 free_eh_nesting_info (eh_nest_info);
1046 /* Create an edge between two basic blocks. FLAGS are auxiliary information
1047 about the edge that is accumulated between calls. */
1049 static void
1050 make_edge (src, dst, flags)
1051 basic_block src, dst;
1052 int flags;
1054 edge e;
1056 /* Make sure we don't add duplicate edges. */
1058 for (e = src->succ; e ; e = e->succ_next)
1059 if (e->dest == dst)
1061 e->flags |= flags;
1062 return;
1065 e = (edge) xcalloc (1, sizeof (*e));
1067 e->succ_next = src->succ;
1068 e->pred_next = dst->pred;
1069 e->src = src;
1070 e->dest = dst;
1071 e->flags = flags;
1073 src->succ = e;
1074 dst->pred = e;
1077 /* Create an edge from a basic block to a label. */
1079 static void
1080 make_label_edge (src, label, flags)
1081 basic_block src;
1082 rtx label;
1083 int flags;
1085 if (GET_CODE (label) != CODE_LABEL)
1086 abort ();
1088 /* If the label was never emitted, this insn is junk, but avoid a
1089 crash trying to refer to BLOCK_FOR_INSN (label). This can happen
1090 as a result of a syntax error and a diagnostic has already been
1091 printed. */
1093 if (INSN_UID (label) == 0)
1094 return;
1096 make_edge (src, BLOCK_FOR_INSN (label), flags);
1099 /* Identify critical edges and set the bits appropriately. */
1100 static void
1101 mark_critical_edges ()
1103 int i, n = n_basic_blocks;
1104 basic_block bb;
1106 /* We begin with the entry block. This is not terribly important now,
1107 but could be if a front end (Fortran) implemented alternate entry
1108 points. */
1109 bb = ENTRY_BLOCK_PTR;
1110 i = -1;
1112 while (1)
1114 edge e;
1116 /* (1) Critical edges must have a source with multiple successors. */
1117 if (bb->succ && bb->succ->succ_next)
1119 for (e = bb->succ; e ; e = e->succ_next)
1121 /* (2) Critical edges must have a destination with multiple
1122 predecessors. Note that we know there is at least one
1123 predecessor -- the edge we followed to get here. */
1124 if (e->dest->pred->pred_next)
1125 e->flags |= EDGE_CRITICAL;
1126 else
1127 e->flags &= ~EDGE_CRITICAL;
1130 else
1132 for (e = bb->succ; e ; e = e->succ_next)
1133 e->flags &= ~EDGE_CRITICAL;
1136 if (++i >= n)
1137 break;
1138 bb = BASIC_BLOCK (i);
1142 /* Split a (typically critical) edge. Return the new block.
1143 Abort on abnormal edges.
1145 ??? The code generally expects to be called on critical edges.
1146 The case of a block ending in an unconditional jump to a
1147 block with multiple predecessors is not handled optimally. */
1149 basic_block
1150 split_edge (edge_in)
1151 edge edge_in;
1153 basic_block old_pred, bb, old_succ;
1154 edge edge_out;
1155 rtx bb_note;
1156 int i;
1158 /* Abnormal edges cannot be split. */
1159 if ((edge_in->flags & EDGE_ABNORMAL) != 0)
1160 abort ();
1162 old_pred = edge_in->src;
1163 old_succ = edge_in->dest;
1165 /* Remove the existing edge from the destination's pred list. */
1167 edge *pp;
1168 for (pp = &old_succ->pred; *pp != edge_in; pp = &(*pp)->pred_next)
1169 continue;
1170 *pp = edge_in->pred_next;
1171 edge_in->pred_next = NULL;
1174 /* Create the new structures. */
1175 bb = (basic_block) obstack_alloc (function_obstack, sizeof (*bb));
1176 edge_out = (edge) xcalloc (1, sizeof (*edge_out));
1178 memset (bb, 0, sizeof (*bb));
1179 bb->local_set = OBSTACK_ALLOC_REG_SET (function_obstack);
1180 bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (function_obstack);
1181 bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (function_obstack);
1183 /* ??? This info is likely going to be out of date very soon. */
1184 CLEAR_REG_SET (bb->local_set);
1185 if (old_succ->global_live_at_start)
1187 COPY_REG_SET (bb->global_live_at_start, old_succ->global_live_at_start);
1188 COPY_REG_SET (bb->global_live_at_end, old_succ->global_live_at_start);
1190 else
1192 CLEAR_REG_SET (bb->global_live_at_start);
1193 CLEAR_REG_SET (bb->global_live_at_end);
1196 /* Wire them up. */
1197 bb->pred = edge_in;
1198 bb->succ = edge_out;
1200 edge_in->dest = bb;
1201 edge_in->flags &= ~EDGE_CRITICAL;
1203 edge_out->pred_next = old_succ->pred;
1204 edge_out->succ_next = NULL;
1205 edge_out->src = bb;
1206 edge_out->dest = old_succ;
1207 edge_out->flags = EDGE_FALLTHRU;
1208 edge_out->probability = REG_BR_PROB_BASE;
1210 old_succ->pred = edge_out;
1212 /* Tricky case -- if there existed a fallthru into the successor
1213 (and we're not it) we must add a new unconditional jump around
1214 the new block we're actually interested in.
1216 Further, if that edge is critical, this means a second new basic
1217 block must be created to hold it. In order to simplify correct
1218 insn placement, do this before we touch the existing basic block
1219 ordering for the block we were really wanting. */
1220 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1222 edge e;
1223 for (e = edge_out->pred_next; e ; e = e->pred_next)
1224 if (e->flags & EDGE_FALLTHRU)
1225 break;
1227 if (e)
1229 basic_block jump_block;
1230 rtx pos;
1232 if ((e->flags & EDGE_CRITICAL) == 0)
1234 /* Non critical -- we can simply add a jump to the end
1235 of the existing predecessor. */
1236 jump_block = e->src;
1238 else
1240 /* We need a new block to hold the jump. The simplest
1241 way to do the bulk of the work here is to recursively
1242 call ourselves. */
1243 jump_block = split_edge (e);
1244 e = jump_block->succ;
1247 /* Now add the jump insn ... */
1248 pos = emit_jump_insn_after (gen_jump (old_succ->head),
1249 jump_block->end);
1250 jump_block->end = pos;
1251 emit_barrier_after (pos);
1253 /* ... let jump know that label is in use, ... */
1254 JUMP_LABEL (pos) = old_succ->head;
1255 ++LABEL_NUSES (old_succ->head);
1257 /* ... and clear fallthru on the outgoing edge. */
1258 e->flags &= ~EDGE_FALLTHRU;
1260 /* Continue splitting the interesting edge. */
1264 /* Place the new block just in front of the successor. */
1265 VARRAY_GROW (basic_block_info, ++n_basic_blocks);
1266 for (i = n_basic_blocks - 1; i > old_succ->index; --i)
1268 basic_block tmp = BASIC_BLOCK (i - 1);
1269 BASIC_BLOCK (i) = tmp;
1270 tmp->index = i;
1272 BASIC_BLOCK (i) = bb;
1273 bb->index = i;
1275 /* Create the basic block note. */
1276 bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, old_succ->head);
1277 NOTE_BASIC_BLOCK (bb_note) = bb;
1278 bb->head = bb->end = bb_note;
1280 /* Not quite simple -- for non-fallthru edges, we must adjust the
1281 predecessor's jump instruction to target our new block. */
1282 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1284 rtx tmp, insn = old_pred->end;
1285 rtx old_label = old_succ->head;
1286 rtx new_label = gen_label_rtx ();
1288 if (GET_CODE (insn) != JUMP_INSN)
1289 abort ();
1291 /* ??? Recognize a tablejump and adjust all matching cases. */
1292 if ((tmp = JUMP_LABEL (insn)) != NULL_RTX
1293 && (tmp = NEXT_INSN (tmp)) != NULL_RTX
1294 && GET_CODE (tmp) == JUMP_INSN
1295 && (GET_CODE (PATTERN (tmp)) == ADDR_VEC
1296 || GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC))
1298 rtvec vec;
1299 int j;
1301 if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
1302 vec = XVEC (PATTERN (tmp), 0);
1303 else
1304 vec = XVEC (PATTERN (tmp), 1);
1306 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
1307 if (XEXP (RTVEC_ELT (vec, j), 0) == old_label)
1309 RTVEC_ELT (vec, j) = gen_rtx_LABEL_REF (VOIDmode, new_label);
1310 --LABEL_NUSES (old_label);
1311 ++LABEL_NUSES (new_label);
1314 else
1316 /* This would have indicated an abnormal edge. */
1317 if (computed_jump_p (insn))
1318 abort ();
1320 /* A return instruction can't be redirected. */
1321 if (returnjump_p (insn))
1322 abort ();
1324 /* If the insn doesn't go where we think, we're confused. */
1325 if (JUMP_LABEL (insn) != old_label)
1326 abort ();
1328 redirect_jump (insn, new_label);
1331 emit_label_before (new_label, bb_note);
1332 bb->head = new_label;
1335 return bb;
1338 /* Queue instructions for insertion on an edge between two basic blocks.
1339 The new instructions and basic blocks (if any) will not appear in the
1340 CFG until commit_edge_insertions is called. */
1342 void
1343 insert_insn_on_edge (pattern, e)
1344 rtx pattern;
1345 edge e;
1347 /* We cannot insert instructions on an abnormal critical edge.
1348 It will be easier to find the culprit if we die now. */
1349 if ((e->flags & (EDGE_ABNORMAL|EDGE_CRITICAL))
1350 == (EDGE_ABNORMAL|EDGE_CRITICAL))
1351 abort ();
1353 if (e->insns == NULL_RTX)
1354 start_sequence ();
1355 else
1356 push_to_sequence (e->insns);
1358 emit_insn (pattern);
1360 e->insns = get_insns ();
1361 end_sequence();
1364 /* Update the CFG for the instructions queued on edge E. */
1366 static void
1367 commit_one_edge_insertion (e)
1368 edge e;
1370 rtx before = NULL_RTX, after = NULL_RTX, tmp;
1371 basic_block bb;
1373 /* Figure out where to put these things. If the destination has
1374 one predecessor, insert there. Except for the exit block. */
1375 if (e->dest->pred->pred_next == NULL
1376 && e->dest != EXIT_BLOCK_PTR)
1378 bb = e->dest;
1380 /* Get the location correct wrt a code label, and "nice" wrt
1381 a basic block note, and before everything else. */
1382 tmp = bb->head;
1383 if (GET_CODE (tmp) == CODE_LABEL)
1384 tmp = NEXT_INSN (tmp);
1385 if (GET_CODE (tmp) == NOTE
1386 && NOTE_LINE_NUMBER (tmp) == NOTE_INSN_BASIC_BLOCK)
1387 tmp = NEXT_INSN (tmp);
1388 if (tmp == bb->head)
1389 before = tmp;
1390 else
1391 after = PREV_INSN (tmp);
1394 /* If the source has one successor and the edge is not abnormal,
1395 insert there. Except for the entry block. */
1396 else if ((e->flags & EDGE_ABNORMAL) == 0
1397 && e->src->succ->succ_next == NULL
1398 && e->src != ENTRY_BLOCK_PTR)
1400 bb = e->src;
1401 if (GET_CODE (bb->end) == JUMP_INSN)
1403 /* ??? Is it possible to wind up with non-simple jumps? Perhaps
1404 a jump with delay slots already filled? */
1405 if (! simplejump_p (bb->end))
1406 abort ();
1408 before = bb->end;
1410 else
1412 /* We'd better be fallthru, or we've lost track of what's what. */
1413 if ((e->flags & EDGE_FALLTHRU) == 0)
1414 abort ();
1416 after = bb->end;
1420 /* Otherwise we must split the edge. */
1421 else
1423 bb = split_edge (e);
1424 after = bb->end;
1427 /* Now that we've found the spot, do the insertion. */
1428 tmp = e->insns;
1429 e->insns = NULL_RTX;
1431 /* Set the new block number for these insns, if structure is allocated. */
1432 if (basic_block_for_insn)
1434 rtx i;
1435 for (i = tmp; i != NULL_RTX; i = NEXT_INSN (i))
1436 set_block_for_insn (i, bb);
1439 if (before)
1441 emit_insns_before (tmp, before);
1442 if (before == bb->head)
1443 bb->head = tmp;
1445 else
1447 tmp = emit_insns_after (tmp, after);
1448 if (after == bb->end)
1449 bb->end = tmp;
1453 /* Update the CFG for all queued instructions. */
1455 void
1456 commit_edge_insertions ()
1458 int i;
1459 basic_block bb;
1461 i = -1;
1462 bb = ENTRY_BLOCK_PTR;
1463 while (1)
1465 edge e, next;
1467 for (e = bb->succ; e ; e = next)
1469 next = e->succ_next;
1470 if (e->insns)
1471 commit_one_edge_insertion (e);
1474 if (++i >= n_basic_blocks)
1475 break;
1476 bb = BASIC_BLOCK (i);
1480 /* Delete all unreachable basic blocks. */
1482 static void
1483 delete_unreachable_blocks ()
1485 basic_block *worklist, *tos;
1486 int deleted_handler;
1487 edge e;
1488 int i, n;
1490 n = n_basic_blocks;
1491 tos = worklist = (basic_block *) alloca (sizeof (basic_block) * n);
1493 /* Use basic_block->aux as a marker. Clear them all. */
1495 for (i = 0; i < n; ++i)
1496 BASIC_BLOCK (i)->aux = NULL;
1498 /* Add our starting points to the worklist. Almost always there will
1499 be only one. It isn't inconcievable that we might one day directly
1500 support Fortran alternate entry points. */
1502 for (e = ENTRY_BLOCK_PTR->succ; e ; e = e->succ_next)
1504 *tos++ = e->dest;
1506 /* Mark the block with a handy non-null value. */
1507 e->dest->aux = e;
1510 /* Iterate: find everything reachable from what we've already seen. */
1512 while (tos != worklist)
1514 basic_block b = *--tos;
1516 for (e = b->succ; e ; e = e->succ_next)
1517 if (!e->dest->aux)
1519 *tos++ = e->dest;
1520 e->dest->aux = e;
1524 /* Delete all unreachable basic blocks. Count down so that we don't
1525 interfere with the block renumbering that happens in delete_block. */
1527 deleted_handler = 0;
1529 for (i = n - 1; i >= 0; --i)
1531 basic_block b = BASIC_BLOCK (i);
1533 if (b->aux != NULL)
1534 /* This block was found. Tidy up the mark. */
1535 b->aux = NULL;
1536 else
1537 deleted_handler |= delete_block (b);
1540 /* Fix up edges that now fall through, or rather should now fall through
1541 but previously required a jump around now deleted blocks. Simplify
1542 the search by only examining blocks numerically adjacent, since this
1543 is how find_basic_blocks created them. */
1545 for (i = 1; i < n_basic_blocks; ++i)
1547 basic_block b = BASIC_BLOCK (i - 1);
1548 basic_block c = BASIC_BLOCK (i);
1549 edge s;
1551 /* We care about simple conditional or unconditional jumps with
1552 a single successor.
1554 If we had a conditional branch to the next instruction when
1555 find_basic_blocks was called, then there will only be one
1556 out edge for the block which ended with the conditional
1557 branch (since we do not create duplicate edges).
1559 Furthermore, the edge will be marked as a fallthru because we
1560 merge the flags for the duplicate edges. So we do not want to
1561 check that the edge is not a FALLTHRU edge. */
1562 if ((s = b->succ) != NULL
1563 && s->succ_next == NULL
1564 && s->dest == c
1565 /* If the jump insn has side effects, we can't tidy the edge. */
1566 && (GET_CODE (b->end) != JUMP_INSN
1567 || onlyjump_p (b->end)))
1568 tidy_fallthru_edge (s, b, c);
1571 /* Attempt to merge blocks as made possible by edge removal. If a block
1572 has only one successor, and the successor has only one predecessor,
1573 they may be combined. */
1575 for (i = 0; i < n_basic_blocks; )
1577 basic_block c, b = BASIC_BLOCK (i);
1578 edge s;
1580 /* A loop because chains of blocks might be combineable. */
1581 while ((s = b->succ) != NULL
1582 && s->succ_next == NULL
1583 && (s->flags & EDGE_EH) == 0
1584 && (c = s->dest) != EXIT_BLOCK_PTR
1585 && c->pred->pred_next == NULL
1586 /* If the jump insn has side effects, we can't kill the edge. */
1587 && (GET_CODE (b->end) != JUMP_INSN
1588 || onlyjump_p (b->end))
1589 && merge_blocks (s, b, c))
1590 continue;
1592 /* Don't get confused by the index shift caused by deleting blocks. */
1593 i = b->index + 1;
1596 /* If we deleted an exception handler, we may have EH region begin/end
1597 blocks to remove as well. */
1598 if (deleted_handler)
1599 delete_eh_regions ();
1602 /* Find EH regions for which there is no longer a handler, and delete them. */
1604 static void
1605 delete_eh_regions ()
1607 rtx insn;
1609 update_rethrow_references ();
1611 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1612 if (GET_CODE (insn) == NOTE)
1614 if ((NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG) ||
1615 (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END))
1617 int num = NOTE_EH_HANDLER (insn);
1618 /* A NULL handler indicates a region is no longer needed,
1619 as long as it isn't the target of a rethrow. */
1620 if (get_first_handler (num) == NULL && ! rethrow_used (num))
1622 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1623 NOTE_SOURCE_FILE (insn) = 0;
1629 /* Return true if NOTE is not one of the ones that must be kept paired,
1630 so that we may simply delete them. */
1632 static int
1633 can_delete_note_p (note)
1634 rtx note;
1636 return (NOTE_LINE_NUMBER (note) == NOTE_INSN_DELETED
1637 || NOTE_LINE_NUMBER (note) == NOTE_INSN_BASIC_BLOCK);
1640 /* Unlink a chain of insns between START and FINISH, leaving notes
1641 that must be paired. */
1643 static void
1644 flow_delete_insn_chain (start, finish)
1645 rtx start, finish;
1647 /* Unchain the insns one by one. It would be quicker to delete all
1648 of these with a single unchaining, rather than one at a time, but
1649 we need to keep the NOTE's. */
1651 rtx next;
1653 while (1)
1655 next = NEXT_INSN (start);
1656 if (GET_CODE (start) == NOTE && !can_delete_note_p (start))
1658 else if (GET_CODE (start) == CODE_LABEL && !can_delete_label_p (start))
1660 else
1661 next = flow_delete_insn (start);
1663 if (start == finish)
1664 break;
1665 start = next;
1669 /* Delete the insns in a (non-live) block. We physically delete every
1670 non-deleted-note insn, and update the flow graph appropriately.
1672 Return nonzero if we deleted an exception handler. */
1674 /* ??? Preserving all such notes strikes me as wrong. It would be nice
1675 to post-process the stream to remove empty blocks, loops, ranges, etc. */
1677 static int
1678 delete_block (b)
1679 basic_block b;
1681 int deleted_handler = 0;
1682 rtx insn, end;
1684 /* If the head of this block is a CODE_LABEL, then it might be the
1685 label for an exception handler which can't be reached.
1687 We need to remove the label from the exception_handler_label list
1688 and remove the associated NOTE_INSN_EH_REGION_BEG and
1689 NOTE_INSN_EH_REGION_END notes. */
1691 insn = b->head;
1693 never_reached_warning (insn);
1695 if (GET_CODE (insn) == CODE_LABEL)
1697 rtx x, *prev = &exception_handler_labels;
1699 for (x = exception_handler_labels; x; x = XEXP (x, 1))
1701 if (XEXP (x, 0) == insn)
1703 /* Found a match, splice this label out of the EH label list. */
1704 *prev = XEXP (x, 1);
1705 XEXP (x, 1) = NULL_RTX;
1706 XEXP (x, 0) = NULL_RTX;
1708 /* Remove the handler from all regions */
1709 remove_handler (insn);
1710 deleted_handler = 1;
1711 break;
1713 prev = &XEXP (x, 1);
1716 /* This label may be referenced by code solely for its value, or
1717 referenced by static data, or something. We have determined
1718 that it is not reachable, but cannot delete the label itself.
1719 Save code space and continue to delete the balance of the block,
1720 along with properly updating the cfg. */
1721 if (!can_delete_label_p (insn))
1723 /* If we've only got one of these, skip the whole deleting
1724 insns thing. */
1725 if (insn == b->end)
1726 goto no_delete_insns;
1727 insn = NEXT_INSN (insn);
1731 /* Selectively unlink the insn chain. Include any BARRIER that may
1732 follow the basic block. */
1733 end = next_nonnote_insn (b->end);
1734 if (!end || GET_CODE (end) != BARRIER)
1735 end = b->end;
1736 flow_delete_insn_chain (insn, end);
1738 no_delete_insns:
1740 /* Remove the edges into and out of this block. Note that there may
1741 indeed be edges in, if we are removing an unreachable loop. */
1743 edge e, next, *q;
1745 for (e = b->pred; e ; e = next)
1747 for (q = &e->src->succ; *q != e; q = &(*q)->succ_next)
1748 continue;
1749 *q = e->succ_next;
1750 next = e->pred_next;
1751 free (e);
1753 for (e = b->succ; e ; e = next)
1755 for (q = &e->dest->pred; *q != e; q = &(*q)->pred_next)
1756 continue;
1757 *q = e->pred_next;
1758 next = e->succ_next;
1759 free (e);
1762 b->pred = NULL;
1763 b->succ = NULL;
1766 /* Remove the basic block from the array, and compact behind it. */
1767 expunge_block (b);
1769 return deleted_handler;
1772 /* Remove block B from the basic block array and compact behind it. */
1774 static void
1775 expunge_block (b)
1776 basic_block b;
1778 int i, n = n_basic_blocks;
1780 for (i = b->index; i + 1 < n; ++i)
1782 basic_block x = BASIC_BLOCK (i + 1);
1783 BASIC_BLOCK (i) = x;
1784 x->index = i;
1787 basic_block_info->num_elements--;
1788 n_basic_blocks--;
1791 /* Delete INSN by patching it out. Return the next insn. */
1793 static rtx
1794 flow_delete_insn (insn)
1795 rtx insn;
1797 rtx prev = PREV_INSN (insn);
1798 rtx next = NEXT_INSN (insn);
1800 PREV_INSN (insn) = NULL_RTX;
1801 NEXT_INSN (insn) = NULL_RTX;
1803 if (prev)
1804 NEXT_INSN (prev) = next;
1805 if (next)
1806 PREV_INSN (next) = prev;
1807 else
1808 set_last_insn (prev);
1810 if (GET_CODE (insn) == CODE_LABEL)
1811 remove_node_from_expr_list (insn, &nonlocal_goto_handler_labels);
1813 /* If deleting a jump, decrement the use count of the label. Deleting
1814 the label itself should happen in the normal course of block merging. */
1815 if (GET_CODE (insn) == JUMP_INSN && JUMP_LABEL (insn))
1816 LABEL_NUSES (JUMP_LABEL (insn))--;
1818 return next;
1821 /* True if a given label can be deleted. */
1823 static int
1824 can_delete_label_p (label)
1825 rtx label;
1827 rtx x;
1829 if (LABEL_PRESERVE_P (label))
1830 return 0;
1832 for (x = forced_labels; x ; x = XEXP (x, 1))
1833 if (label == XEXP (x, 0))
1834 return 0;
1835 for (x = label_value_list; x ; x = XEXP (x, 1))
1836 if (label == XEXP (x, 0))
1837 return 0;
1838 for (x = exception_handler_labels; x ; x = XEXP (x, 1))
1839 if (label == XEXP (x, 0))
1840 return 0;
1842 /* User declared labels must be preserved. */
1843 if (LABEL_NAME (label) != 0)
1844 return 0;
1846 return 1;
1849 /* Blocks A and B are to be merged into a single block. The insns
1850 are already contiguous, hence `nomove'. */
1852 static void
1853 merge_blocks_nomove (a, b)
1854 basic_block a, b;
1856 edge e;
1857 rtx b_head, b_end, a_end;
1858 int b_empty = 0;
1860 /* If there was a CODE_LABEL beginning B, delete it. */
1861 b_head = b->head;
1862 b_end = b->end;
1863 if (GET_CODE (b_head) == CODE_LABEL)
1865 /* Detect basic blocks with nothing but a label. This can happen
1866 in particular at the end of a function. */
1867 if (b_head == b_end)
1868 b_empty = 1;
1869 b_head = flow_delete_insn (b_head);
1872 /* Delete the basic block note. */
1873 if (GET_CODE (b_head) == NOTE
1874 && NOTE_LINE_NUMBER (b_head) == NOTE_INSN_BASIC_BLOCK)
1876 if (b_head == b_end)
1877 b_empty = 1;
1878 b_head = flow_delete_insn (b_head);
1881 /* If there was a jump out of A, delete it. */
1882 a_end = a->end;
1883 if (GET_CODE (a_end) == JUMP_INSN)
1885 rtx prev;
1887 prev = prev_nonnote_insn (a_end);
1888 if (!prev)
1889 prev = a->head;
1891 #ifdef HAVE_cc0
1892 /* If this was a conditional jump, we need to also delete
1893 the insn that set cc0. */
1895 if (prev && sets_cc0_p (prev))
1897 rtx tmp = prev;
1898 prev = prev_nonnote_insn (prev);
1899 if (!prev)
1900 prev = a->head;
1901 flow_delete_insn (tmp);
1903 #endif
1905 /* Note that a->head != a->end, since we should have at least a
1906 bb note plus the jump, so prev != insn. */
1907 flow_delete_insn (a_end);
1908 a_end = prev;
1911 /* By definition, there should only be one successor of A, and that is
1912 B. Free that edge struct. */
1913 free (a->succ);
1915 /* Adjust the edges out of B for the new owner. */
1916 for (e = b->succ; e ; e = e->succ_next)
1917 e->src = a;
1918 a->succ = b->succ;
1920 /* Reassociate the insns of B with A. */
1921 if (!b_empty)
1923 BLOCK_FOR_INSN (b_head) = a;
1924 while (b_head != b_end)
1926 b_head = NEXT_INSN (b_head);
1927 BLOCK_FOR_INSN (b_head) = a;
1929 a_end = b_head;
1931 a->end = a_end;
1933 /* Compact the basic block array. */
1934 expunge_block (b);
1937 /* Attempt to merge basic blocks that are potentially non-adjacent.
1938 Return true iff the attempt succeeded. */
1940 static int
1941 merge_blocks (e, b, c)
1942 edge e;
1943 basic_block b, c;
1945 /* If B has a fallthru edge to C, no need to move anything. */
1946 if (!(e->flags & EDGE_FALLTHRU))
1948 /* ??? From here on out we must make sure to not munge nesting
1949 of exception regions and lexical blocks. Need to think about
1950 these cases before this gets implemented. */
1951 return 0;
1953 /* If C has an outgoing fallthru, and B does not have an incoming
1954 fallthru, move B before C. The later clause is somewhat arbitrary,
1955 but avoids modifying blocks other than the two we've been given. */
1957 /* Otherwise, move C after B. If C had a fallthru, which doesn't
1958 happen to be the physical successor to B, insert an unconditional
1959 branch. If C already ended with a conditional branch, the new
1960 jump must go in a new basic block D. */
1963 /* If a label still appears somewhere and we cannot delete the label,
1964 then we cannot merge the blocks. The edge was tidied already. */
1966 rtx insn, stop = NEXT_INSN (c->head);
1967 for (insn = NEXT_INSN (b->end); insn != stop; insn = NEXT_INSN (insn))
1968 if (GET_CODE (insn) == CODE_LABEL && !can_delete_label_p (insn))
1969 return 0;
1972 merge_blocks_nomove (b, c);
1973 return 1;
1976 /* The given edge should potentially a fallthru edge. If that is in
1977 fact true, delete the unconditional jump and barriers that are in
1978 the way. */
1980 static void
1981 tidy_fallthru_edge (e, b, c)
1982 edge e;
1983 basic_block b, c;
1985 rtx q;
1987 /* ??? In a late-running flow pass, other folks may have deleted basic
1988 blocks by nopping out blocks, leaving multiple BARRIERs between here
1989 and the target label. They ought to be chastized and fixed.
1991 We can also wind up with a sequence of undeletable labels between
1992 one block and the next.
1994 So search through a sequence of barriers, labels, and notes for
1995 the head of block C and assert that we really do fall through. */
1997 if (next_real_insn (b->end) != next_real_insn (PREV_INSN (c->head)))
1998 return;
2000 /* Remove what will soon cease being the jump insn from the source block.
2001 If block B consisted only of this single jump, turn it into a deleted
2002 note. */
2003 q = b->end;
2004 if (GET_CODE (q) == JUMP_INSN)
2006 #ifdef HAVE_cc0
2007 /* If this was a conditional jump, we need to also delete
2008 the insn that set cc0. */
2009 if (! simplejump_p (q) && condjump_p (q) && sets_cc0_p (PREV_INSN (q)))
2010 q = PREV_INSN (q);
2011 #endif
2013 if (b->head == q)
2015 PUT_CODE (q, NOTE);
2016 NOTE_LINE_NUMBER (q) = NOTE_INSN_DELETED;
2017 NOTE_SOURCE_FILE (q) = 0;
2019 else
2020 b->end = q = PREV_INSN (q);
2023 /* Selectively unlink the sequence. */
2024 if (q != PREV_INSN (c->head))
2025 flow_delete_insn_chain (NEXT_INSN (q), PREV_INSN (c->head));
2027 e->flags |= EDGE_FALLTHRU;
2030 /* Discover and record the loop depth at the head of each basic block. */
2032 static void
2033 calculate_loop_depth (insns)
2034 rtx insns;
2036 basic_block bb;
2037 rtx insn;
2038 int i = 0, depth = 1;
2040 bb = BASIC_BLOCK (i);
2041 for (insn = insns; insn ; insn = NEXT_INSN (insn))
2043 if (insn == bb->head)
2045 bb->loop_depth = depth;
2046 if (++i >= n_basic_blocks)
2047 break;
2048 bb = BASIC_BLOCK (i);
2051 if (GET_CODE (insn) == NOTE)
2053 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
2054 depth++;
2055 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
2056 depth--;
2058 /* If we have LOOP_DEPTH == 0, there has been a bookkeeping error. */
2059 if (depth == 0)
2060 abort ();
2065 /* Perform data flow analysis.
2066 F is the first insn of the function and NREGS the number of register numbers
2067 in use. */
2069 void
2070 life_analysis (f, nregs, file, remove_dead_code)
2071 rtx f;
2072 int nregs;
2073 FILE *file;
2074 int remove_dead_code;
2076 #ifdef ELIMINABLE_REGS
2077 register size_t i;
2078 static struct {int from, to; } eliminables[] = ELIMINABLE_REGS;
2079 #endif
2081 /* Record which registers will be eliminated. We use this in
2082 mark_used_regs. */
2084 CLEAR_HARD_REG_SET (elim_reg_set);
2086 #ifdef ELIMINABLE_REGS
2087 for (i = 0; i < sizeof eliminables / sizeof eliminables[0]; i++)
2088 SET_HARD_REG_BIT (elim_reg_set, eliminables[i].from);
2089 #else
2090 SET_HARD_REG_BIT (elim_reg_set, FRAME_POINTER_REGNUM);
2091 #endif
2093 /* Allocate a bitmap to be filled in by record_volatile_insns. */
2094 uid_volatile = BITMAP_ALLOCA ();
2096 /* We want alias analysis information for local dead store elimination. */
2097 init_alias_analysis ();
2099 life_analysis_1 (f, nregs, remove_dead_code);
2101 if (! reload_completed)
2102 mark_constant_function ();
2104 end_alias_analysis ();
2106 if (file)
2107 dump_flow_info (file);
2109 BITMAP_FREE (uid_volatile);
2110 free_basic_block_vars (1);
2113 /* Free the variables allocated by find_basic_blocks.
2115 KEEP_HEAD_END_P is non-zero if basic_block_info is not to be freed. */
2117 void
2118 free_basic_block_vars (keep_head_end_p)
2119 int keep_head_end_p;
2121 if (basic_block_for_insn)
2123 VARRAY_FREE (basic_block_for_insn);
2124 basic_block_for_insn = NULL;
2127 if (! keep_head_end_p)
2129 clear_edges ();
2130 VARRAY_FREE (basic_block_info);
2131 n_basic_blocks = 0;
2133 ENTRY_BLOCK_PTR->aux = NULL;
2134 ENTRY_BLOCK_PTR->global_live_at_end = NULL;
2135 EXIT_BLOCK_PTR->aux = NULL;
2136 EXIT_BLOCK_PTR->global_live_at_start = NULL;
2140 /* Return nonzero if the destination of SET equals the source. */
2141 static int
2142 set_noop_p (set)
2143 rtx set;
2145 rtx src = SET_SRC (set);
2146 rtx dst = SET_DEST (set);
2147 if (GET_CODE (src) == REG && GET_CODE (dst) == REG
2148 && REGNO (src) == REGNO (dst))
2149 return 1;
2150 if (GET_CODE (src) != SUBREG || GET_CODE (dst) != SUBREG
2151 || SUBREG_WORD (src) != SUBREG_WORD (dst))
2152 return 0;
2153 src = SUBREG_REG (src);
2154 dst = SUBREG_REG (dst);
2155 if (GET_CODE (src) == REG && GET_CODE (dst) == REG
2156 && REGNO (src) == REGNO (dst))
2157 return 1;
2158 return 0;
2161 /* Return nonzero if an insn consists only of SETs, each of which only sets a
2162 value to itself. */
2163 static int
2164 noop_move_p (insn)
2165 rtx insn;
2167 rtx pat = PATTERN (insn);
2169 /* Insns carrying these notes are useful later on. */
2170 if (find_reg_note (insn, REG_EQUAL, NULL_RTX))
2171 return 0;
2173 if (GET_CODE (pat) == SET && set_noop_p (pat))
2174 return 1;
2176 if (GET_CODE (pat) == PARALLEL)
2178 int i;
2179 /* If nothing but SETs of registers to themselves,
2180 this insn can also be deleted. */
2181 for (i = 0; i < XVECLEN (pat, 0); i++)
2183 rtx tem = XVECEXP (pat, 0, i);
2185 if (GET_CODE (tem) == USE
2186 || GET_CODE (tem) == CLOBBER)
2187 continue;
2189 if (GET_CODE (tem) != SET || ! set_noop_p (tem))
2190 return 0;
2193 return 1;
2195 return 0;
2198 static void
2199 notice_stack_pointer_modification (x, pat)
2200 rtx x;
2201 rtx pat ATTRIBUTE_UNUSED;
2203 if (x == stack_pointer_rtx
2204 /* The stack pointer is only modified indirectly as the result
2205 of a push until later in flow. See the comments in rtl.texi
2206 regarding Embedded Side-Effects on Addresses. */
2207 || (GET_CODE (x) == MEM
2208 && (GET_CODE (XEXP (x, 0)) == PRE_DEC
2209 || GET_CODE (XEXP (x, 0)) == PRE_INC
2210 || GET_CODE (XEXP (x, 0)) == POST_DEC
2211 || GET_CODE (XEXP (x, 0)) == POST_INC)
2212 && XEXP (XEXP (x, 0), 0) == stack_pointer_rtx))
2213 current_function_sp_is_unchanging = 0;
2216 /* Record which insns refer to any volatile memory
2217 or for any reason can't be deleted just because they are dead stores.
2218 Also, delete any insns that copy a register to itself.
2219 And see if the stack pointer is modified. */
2220 static void
2221 record_volatile_insns (f)
2222 rtx f;
2224 rtx insn;
2225 for (insn = f; insn; insn = NEXT_INSN (insn))
2227 enum rtx_code code1 = GET_CODE (insn);
2228 if (code1 == CALL_INSN)
2229 SET_INSN_VOLATILE (insn);
2230 else if (code1 == INSN || code1 == JUMP_INSN)
2232 if (GET_CODE (PATTERN (insn)) != USE
2233 && volatile_refs_p (PATTERN (insn)))
2234 SET_INSN_VOLATILE (insn);
2236 /* A SET that makes space on the stack cannot be dead.
2237 (Such SETs occur only for allocating variable-size data,
2238 so they will always have a PLUS or MINUS according to the
2239 direction of stack growth.)
2240 Even if this function never uses this stack pointer value,
2241 signal handlers do! */
2242 else if (code1 == INSN && GET_CODE (PATTERN (insn)) == SET
2243 && SET_DEST (PATTERN (insn)) == stack_pointer_rtx
2244 #ifdef STACK_GROWS_DOWNWARD
2245 && GET_CODE (SET_SRC (PATTERN (insn))) == MINUS
2246 #else
2247 && GET_CODE (SET_SRC (PATTERN (insn))) == PLUS
2248 #endif
2249 && XEXP (SET_SRC (PATTERN (insn)), 0) == stack_pointer_rtx)
2250 SET_INSN_VOLATILE (insn);
2252 /* Delete (in effect) any obvious no-op moves. */
2253 else if (noop_move_p (insn))
2255 PUT_CODE (insn, NOTE);
2256 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2257 NOTE_SOURCE_FILE (insn) = 0;
2261 /* Check if insn modifies the stack pointer. */
2262 if ( current_function_sp_is_unchanging
2263 && GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2264 note_stores (PATTERN (insn), notice_stack_pointer_modification);
2268 /* Mark those regs which are needed at the end of the function as live
2269 at the end of the last basic block. */
2270 static void
2271 mark_regs_live_at_end (set)
2272 regset set;
2274 int i;
2276 /* If exiting needs the right stack value, consider the stack pointer
2277 live at the end of the function. */
2278 if (! EXIT_IGNORE_STACK
2279 || (! FRAME_POINTER_REQUIRED
2280 && ! current_function_calls_alloca
2281 && flag_omit_frame_pointer)
2282 || current_function_sp_is_unchanging)
2284 SET_REGNO_REG_SET (set, STACK_POINTER_REGNUM);
2287 /* Mark the frame pointer if needed at the end of the function. If
2288 we end up eliminating it, it will be removed from the live list
2289 of each basic block by reload. */
2291 if (! reload_completed || frame_pointer_needed)
2293 SET_REGNO_REG_SET (set, FRAME_POINTER_REGNUM);
2294 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2295 /* If they are different, also mark the hard frame pointer as live */
2296 SET_REGNO_REG_SET (set, HARD_FRAME_POINTER_REGNUM);
2297 #endif
2300 /* Mark all global registers, and all registers used by the epilogue
2301 as being live at the end of the function since they may be
2302 referenced by our caller. */
2303 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2304 if (global_regs[i]
2305 #ifdef EPILOGUE_USES
2306 || EPILOGUE_USES (i)
2307 #endif
2309 SET_REGNO_REG_SET (set, i);
2311 /* ??? Mark function return value here rather than as uses. */
2314 /* Determine which registers are live at the start of each
2315 basic block of the function whose first insn is F.
2316 NREGS is the number of registers used in F.
2317 We allocate the vector basic_block_live_at_start
2318 and the regsets that it points to, and fill them with the data.
2319 regset_size and regset_bytes are also set here. */
2321 static void
2322 life_analysis_1 (f, nregs, remove_dead_code)
2323 rtx f;
2324 int nregs;
2325 int remove_dead_code;
2327 int first_pass;
2328 int changed;
2329 register int i;
2330 char save_regs_ever_live[FIRST_PSEUDO_REGISTER];
2331 regset *new_live_at_end;
2333 struct obstack flow_obstack;
2335 gcc_obstack_init (&flow_obstack);
2337 max_regno = nregs;
2339 /* Allocate and zero out many data structures
2340 that will record the data from lifetime analysis. */
2342 allocate_reg_life_data ();
2343 allocate_bb_life_data ();
2345 reg_next_use = (rtx *) alloca (nregs * sizeof (rtx));
2346 memset (reg_next_use, 0, nregs * sizeof (rtx));
2348 /* Set up regset-vectors used internally within this function.
2349 Their meanings are documented above, with their declarations. */
2351 new_live_at_end = (regset *) alloca ((n_basic_blocks + 1) * sizeof (regset));
2352 init_regset_vector (new_live_at_end, n_basic_blocks + 1, &flow_obstack);
2354 /* Stick these vectors into the AUX field of the basic block, so that
2355 we don't have to keep going through the index. */
2357 for (i = 0; i < n_basic_blocks; ++i)
2358 BASIC_BLOCK (i)->aux = new_live_at_end[i];
2359 ENTRY_BLOCK_PTR->aux = new_live_at_end[i];
2361 /* Assume that the stack pointer is unchanging if alloca hasn't been used.
2362 This will be cleared by record_volatile_insns if it encounters an insn
2363 which modifies the stack pointer. */
2364 current_function_sp_is_unchanging = !current_function_calls_alloca;
2366 record_volatile_insns (f);
2368 if (n_basic_blocks > 0)
2370 regset theend;
2371 register edge e;
2373 theend = EXIT_BLOCK_PTR->global_live_at_start;
2374 mark_regs_live_at_end (theend);
2376 /* Propogate this exit data to each of EXIT's predecessors. */
2377 for (e = EXIT_BLOCK_PTR->pred; e ; e = e->pred_next)
2379 COPY_REG_SET (e->src->global_live_at_end, theend);
2380 COPY_REG_SET ((regset) e->src->aux, theend);
2384 /* The post-reload life analysis have (on a global basis) the same registers
2385 live as was computed by reload itself.
2387 Otherwise elimination offsets and such may be incorrect.
2389 Reload will make some registers as live even though they do not appear
2390 in the rtl. */
2391 if (reload_completed)
2392 memcpy (save_regs_ever_live, regs_ever_live, sizeof (regs_ever_live));
2393 memset (regs_ever_live, 0, sizeof regs_ever_live);
2395 /* Propagate life info through the basic blocks
2396 around the graph of basic blocks.
2398 This is a relaxation process: each time a new register
2399 is live at the end of the basic block, we must scan the block
2400 to determine which registers are, as a consequence, live at the beginning
2401 of that block. These registers must then be marked live at the ends
2402 of all the blocks that can transfer control to that block.
2403 The process continues until it reaches a fixed point. */
2405 first_pass = 1;
2406 changed = 1;
2407 while (changed)
2409 changed = 0;
2410 for (i = n_basic_blocks - 1; i >= 0; i--)
2412 basic_block bb = BASIC_BLOCK (i);
2413 int consider = first_pass;
2414 int must_rescan = first_pass;
2415 register int j;
2417 if (!first_pass)
2419 /* Set CONSIDER if this block needs thinking about at all
2420 (that is, if the regs live now at the end of it
2421 are not the same as were live at the end of it when
2422 we last thought about it).
2423 Set must_rescan if it needs to be thought about
2424 instruction by instruction (that is, if any additional
2425 reg that is live at the end now but was not live there before
2426 is one of the significant regs of this basic block). */
2428 EXECUTE_IF_AND_COMPL_IN_REG_SET
2429 ((regset) bb->aux, bb->global_live_at_end, 0, j,
2431 consider = 1;
2432 if (REGNO_REG_SET_P (bb->local_set, j))
2434 must_rescan = 1;
2435 goto done;
2438 done:
2439 if (! consider)
2440 continue;
2443 /* The live_at_start of this block may be changing,
2444 so another pass will be required after this one. */
2445 changed = 1;
2447 if (! must_rescan)
2449 /* No complete rescan needed;
2450 just record those variables newly known live at end
2451 as live at start as well. */
2452 IOR_AND_COMPL_REG_SET (bb->global_live_at_start,
2453 (regset) bb->aux,
2454 bb->global_live_at_end);
2456 IOR_AND_COMPL_REG_SET (bb->global_live_at_end,
2457 (regset) bb->aux,
2458 bb->global_live_at_end);
2460 else
2462 /* Update the basic_block_live_at_start
2463 by propagation backwards through the block. */
2464 COPY_REG_SET (bb->global_live_at_end, (regset) bb->aux);
2465 COPY_REG_SET (bb->global_live_at_start,
2466 bb->global_live_at_end);
2467 propagate_block (bb->global_live_at_start,
2468 bb->head, bb->end, 0,
2469 first_pass ? bb->local_set : (regset) 0,
2470 i, remove_dead_code);
2473 /* Update the new_live_at_end's of the block's predecessors. */
2475 register edge e;
2477 for (e = bb->pred; e ; e = e->pred_next)
2478 IOR_REG_SET ((regset) e->src->aux, bb->global_live_at_start);
2481 #ifdef USE_C_ALLOCA
2482 alloca (0);
2483 #endif
2485 first_pass = 0;
2488 /* The only pseudos that are live at the beginning of the function are
2489 those that were not set anywhere in the function. local-alloc doesn't
2490 know how to handle these correctly, so mark them as not local to any
2491 one basic block. */
2493 if (n_basic_blocks > 0)
2494 EXECUTE_IF_SET_IN_REG_SET (BASIC_BLOCK (0)->global_live_at_start,
2495 FIRST_PSEUDO_REGISTER, i,
2497 REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL;
2500 /* Now the life information is accurate. Make one more pass over each
2501 basic block to delete dead stores, create autoincrement addressing
2502 and record how many times each register is used, is set, or dies. */
2504 for (i = 0; i < n_basic_blocks; i++)
2506 basic_block bb = BASIC_BLOCK (i);
2508 /* We start with global_live_at_end to determine which stores are
2509 dead. This process is destructive, and we wish to preserve the
2510 contents of global_live_at_end for posterity. Fortunately,
2511 new_live_at_end, due to the way we converged on a solution,
2512 contains a duplicate of global_live_at_end that we can kill. */
2513 propagate_block ((regset) bb->aux, bb->head, bb->end, 1, (regset) 0, i, remove_dead_code);
2515 #ifdef USE_C_ALLOCA
2516 alloca (0);
2517 #endif
2520 /* We have a problem with any pseudoreg that lives across the setjmp.
2521 ANSI says that if a user variable does not change in value between
2522 the setjmp and the longjmp, then the longjmp preserves it. This
2523 includes longjmp from a place where the pseudo appears dead.
2524 (In principle, the value still exists if it is in scope.)
2525 If the pseudo goes in a hard reg, some other value may occupy
2526 that hard reg where this pseudo is dead, thus clobbering the pseudo.
2527 Conclusion: such a pseudo must not go in a hard reg. */
2528 EXECUTE_IF_SET_IN_REG_SET (regs_live_at_setjmp,
2529 FIRST_PSEUDO_REGISTER, i,
2531 if (regno_reg_rtx[i] != 0)
2533 REG_LIVE_LENGTH (i) = -1;
2534 REG_BASIC_BLOCK (i) = -1;
2538 /* Restore regs_ever_live that was provided by reload. */
2539 if (reload_completed)
2540 memcpy (regs_ever_live, save_regs_ever_live, sizeof (regs_ever_live));
2542 free_regset_vector (new_live_at_end, n_basic_blocks);
2543 obstack_free (&flow_obstack, NULL_PTR);
2545 for (i = 0; i < n_basic_blocks; ++i)
2546 BASIC_BLOCK (i)->aux = NULL;
2547 ENTRY_BLOCK_PTR->aux = NULL;
2550 /* Subroutines of life analysis. */
2552 /* Allocate the permanent data structures that represent the results
2553 of life analysis. Not static since used also for stupid life analysis. */
2555 void
2556 allocate_bb_life_data ()
2558 register int i;
2560 for (i = 0; i < n_basic_blocks; i++)
2562 basic_block bb = BASIC_BLOCK (i);
2564 bb->local_set = OBSTACK_ALLOC_REG_SET (function_obstack);
2565 bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (function_obstack);
2566 bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (function_obstack);
2569 ENTRY_BLOCK_PTR->global_live_at_end
2570 = OBSTACK_ALLOC_REG_SET (function_obstack);
2571 EXIT_BLOCK_PTR->global_live_at_start
2572 = OBSTACK_ALLOC_REG_SET (function_obstack);
2574 regs_live_at_setjmp = OBSTACK_ALLOC_REG_SET (function_obstack);
2577 void
2578 allocate_reg_life_data ()
2580 int i;
2582 /* Recalculate the register space, in case it has grown. Old style
2583 vector oriented regsets would set regset_{size,bytes} here also. */
2584 allocate_reg_info (max_regno, FALSE, FALSE);
2586 /* Because both reg_scan and flow_analysis want to set up the REG_N_SETS
2587 information, explicitly reset it here. The allocation should have
2588 already happened on the previous reg_scan pass. Make sure in case
2589 some more registers were allocated. */
2590 for (i = 0; i < max_regno; i++)
2591 REG_N_SETS (i) = 0;
2594 /* Make each element of VECTOR point at a regset. The vector has
2595 NELTS elements, and space is allocated from the ALLOC_OBSTACK
2596 obstack. */
2598 static void
2599 init_regset_vector (vector, nelts, alloc_obstack)
2600 regset *vector;
2601 int nelts;
2602 struct obstack *alloc_obstack;
2604 register int i;
2606 for (i = 0; i < nelts; i++)
2608 vector[i] = OBSTACK_ALLOC_REG_SET (alloc_obstack);
2609 CLEAR_REG_SET (vector[i]);
2613 /* Release any additional space allocated for each element of VECTOR point
2614 other than the regset header itself. The vector has NELTS elements. */
2616 void
2617 free_regset_vector (vector, nelts)
2618 regset *vector;
2619 int nelts;
2621 register int i;
2623 for (i = 0; i < nelts; i++)
2624 FREE_REG_SET (vector[i]);
2627 /* Compute the registers live at the beginning of a basic block
2628 from those live at the end.
2630 When called, OLD contains those live at the end.
2631 On return, it contains those live at the beginning.
2632 FIRST and LAST are the first and last insns of the basic block.
2634 FINAL is nonzero if we are doing the final pass which is not
2635 for computing the life info (since that has already been done)
2636 but for acting on it. On this pass, we delete dead stores,
2637 set up the logical links and dead-variables lists of instructions,
2638 and merge instructions for autoincrement and autodecrement addresses.
2640 SIGNIFICANT is nonzero only the first time for each basic block.
2641 If it is nonzero, it points to a regset in which we store
2642 a 1 for each register that is set within the block.
2644 BNUM is the number of the basic block. */
2646 static void
2647 propagate_block (old, first, last, final, significant, bnum, remove_dead_code)
2648 register regset old;
2649 rtx first;
2650 rtx last;
2651 int final;
2652 regset significant;
2653 int bnum;
2654 int remove_dead_code;
2656 register rtx insn;
2657 rtx prev;
2658 regset live;
2659 regset dead;
2661 /* Find the loop depth for this block. Ignore loop level changes in the
2662 middle of the basic block -- for register allocation purposes, the
2663 important uses will be in the blocks wholely contained within the loop
2664 not in the loop pre-header or post-trailer. */
2665 loop_depth = BASIC_BLOCK (bnum)->loop_depth;
2667 dead = ALLOCA_REG_SET ();
2668 live = ALLOCA_REG_SET ();
2670 cc0_live = 0;
2671 mem_set_list = NULL_RTX;
2673 if (final)
2675 register int i;
2677 /* Process the regs live at the end of the block.
2678 Mark them as not local to any one basic block. */
2679 EXECUTE_IF_SET_IN_REG_SET (old, 0, i,
2681 REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL;
2685 /* Scan the block an insn at a time from end to beginning. */
2687 for (insn = last; ; insn = prev)
2689 prev = PREV_INSN (insn);
2691 if (GET_CODE (insn) == NOTE)
2693 /* If this is a call to `setjmp' et al,
2694 warn if any non-volatile datum is live. */
2696 if (final && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
2697 IOR_REG_SET (regs_live_at_setjmp, old);
2700 /* Update the life-status of regs for this insn.
2701 First DEAD gets which regs are set in this insn
2702 then LIVE gets which regs are used in this insn.
2703 Then the regs live before the insn
2704 are those live after, with DEAD regs turned off,
2705 and then LIVE regs turned on. */
2707 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2709 register int i;
2710 rtx note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
2711 int insn_is_dead = 0;
2712 int libcall_is_dead = 0;
2714 if (remove_dead_code)
2716 insn_is_dead = (insn_dead_p (PATTERN (insn), old, 0, REG_NOTES (insn))
2717 /* Don't delete something that refers to volatile storage! */
2718 && ! INSN_VOLATILE (insn));
2719 libcall_is_dead = (insn_is_dead && note != 0
2720 && libcall_dead_p (PATTERN (insn), old, note, insn));
2723 /* If an instruction consists of just dead store(s) on final pass,
2724 "delete" it by turning it into a NOTE of type NOTE_INSN_DELETED.
2725 We could really delete it with delete_insn, but that
2726 can cause trouble for first or last insn in a basic block. */
2727 if (final && insn_is_dead)
2729 PUT_CODE (insn, NOTE);
2730 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2731 NOTE_SOURCE_FILE (insn) = 0;
2733 /* CC0 is now known to be dead. Either this insn used it,
2734 in which case it doesn't anymore, or clobbered it,
2735 so the next insn can't use it. */
2736 cc0_live = 0;
2738 /* If this insn is copying the return value from a library call,
2739 delete the entire library call. */
2740 if (libcall_is_dead)
2742 rtx first = XEXP (note, 0);
2743 rtx p = insn;
2744 while (INSN_DELETED_P (first))
2745 first = NEXT_INSN (first);
2746 while (p != first)
2748 p = PREV_INSN (p);
2749 PUT_CODE (p, NOTE);
2750 NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
2751 NOTE_SOURCE_FILE (p) = 0;
2754 goto flushed;
2757 CLEAR_REG_SET (dead);
2758 CLEAR_REG_SET (live);
2760 /* See if this is an increment or decrement that can be
2761 merged into a following memory address. */
2762 #ifdef AUTO_INC_DEC
2764 register rtx x = single_set (insn);
2766 /* Does this instruction increment or decrement a register? */
2767 if (!reload_completed
2768 && final && x != 0
2769 && GET_CODE (SET_DEST (x)) == REG
2770 && (GET_CODE (SET_SRC (x)) == PLUS
2771 || GET_CODE (SET_SRC (x)) == MINUS)
2772 && XEXP (SET_SRC (x), 0) == SET_DEST (x)
2773 && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2774 /* Ok, look for a following memory ref we can combine with.
2775 If one is found, change the memory ref to a PRE_INC
2776 or PRE_DEC, cancel this insn, and return 1.
2777 Return 0 if nothing has been done. */
2778 && try_pre_increment_1 (insn))
2779 goto flushed;
2781 #endif /* AUTO_INC_DEC */
2783 /* If this is not the final pass, and this insn is copying the
2784 value of a library call and it's dead, don't scan the
2785 insns that perform the library call, so that the call's
2786 arguments are not marked live. */
2787 if (libcall_is_dead)
2789 /* Mark the dest reg as `significant'. */
2790 mark_set_regs (old, dead, PATTERN (insn), NULL_RTX, significant);
2792 insn = XEXP (note, 0);
2793 prev = PREV_INSN (insn);
2795 else if (GET_CODE (PATTERN (insn)) == SET
2796 && SET_DEST (PATTERN (insn)) == stack_pointer_rtx
2797 && GET_CODE (SET_SRC (PATTERN (insn))) == PLUS
2798 && XEXP (SET_SRC (PATTERN (insn)), 0) == stack_pointer_rtx
2799 && GET_CODE (XEXP (SET_SRC (PATTERN (insn)), 1)) == CONST_INT)
2800 /* We have an insn to pop a constant amount off the stack.
2801 (Such insns use PLUS regardless of the direction of the stack,
2802 and any insn to adjust the stack by a constant is always a pop.)
2803 These insns, if not dead stores, have no effect on life. */
2805 else
2807 /* Any regs live at the time of a call instruction
2808 must not go in a register clobbered by calls.
2809 Find all regs now live and record this for them. */
2811 if (GET_CODE (insn) == CALL_INSN && final)
2812 EXECUTE_IF_SET_IN_REG_SET (old, 0, i,
2814 REG_N_CALLS_CROSSED (i)++;
2817 /* LIVE gets the regs used in INSN;
2818 DEAD gets those set by it. Dead insns don't make anything
2819 live. */
2821 mark_set_regs (old, dead, PATTERN (insn),
2822 final ? insn : NULL_RTX, significant);
2824 /* If an insn doesn't use CC0, it becomes dead since we
2825 assume that every insn clobbers it. So show it dead here;
2826 mark_used_regs will set it live if it is referenced. */
2827 cc0_live = 0;
2829 if (! insn_is_dead)
2830 mark_used_regs (old, live, PATTERN (insn), final, insn);
2832 /* Sometimes we may have inserted something before INSN (such as
2833 a move) when we make an auto-inc. So ensure we will scan
2834 those insns. */
2835 #ifdef AUTO_INC_DEC
2836 prev = PREV_INSN (insn);
2837 #endif
2839 if (! insn_is_dead && GET_CODE (insn) == CALL_INSN)
2841 register int i;
2843 rtx note;
2845 for (note = CALL_INSN_FUNCTION_USAGE (insn);
2846 note;
2847 note = XEXP (note, 1))
2848 if (GET_CODE (XEXP (note, 0)) == USE)
2849 mark_used_regs (old, live, XEXP (XEXP (note, 0), 0),
2850 final, insn);
2852 /* Each call clobbers all call-clobbered regs that are not
2853 global or fixed. Note that the function-value reg is a
2854 call-clobbered reg, and mark_set_regs has already had
2855 a chance to handle it. */
2857 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2858 if (call_used_regs[i] && ! global_regs[i]
2859 && ! fixed_regs[i])
2860 SET_REGNO_REG_SET (dead, i);
2862 /* The stack ptr is used (honorarily) by a CALL insn. */
2863 SET_REGNO_REG_SET (live, STACK_POINTER_REGNUM);
2865 /* Calls may also reference any of the global registers,
2866 so they are made live. */
2867 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2868 if (global_regs[i])
2869 mark_used_regs (old, live,
2870 gen_rtx_REG (reg_raw_mode[i], i),
2871 final, insn);
2873 /* Calls also clobber memory. */
2874 mem_set_list = NULL_RTX;
2877 /* Update OLD for the registers used or set. */
2878 AND_COMPL_REG_SET (old, dead);
2879 IOR_REG_SET (old, live);
2883 /* On final pass, update counts of how many insns each reg is live
2884 at. */
2885 if (final)
2886 EXECUTE_IF_SET_IN_REG_SET (old, 0, i,
2887 { REG_LIVE_LENGTH (i)++; });
2889 flushed: ;
2890 if (insn == first)
2891 break;
2894 FREE_REG_SET (dead);
2895 FREE_REG_SET (live);
2898 /* Return 1 if X (the body of an insn, or part of it) is just dead stores
2899 (SET expressions whose destinations are registers dead after the insn).
2900 NEEDED is the regset that says which regs are alive after the insn.
2902 Unless CALL_OK is non-zero, an insn is needed if it contains a CALL.
2904 If X is the entire body of an insn, NOTES contains the reg notes
2905 pertaining to the insn. */
2907 static int
2908 insn_dead_p (x, needed, call_ok, notes)
2909 rtx x;
2910 regset needed;
2911 int call_ok;
2912 rtx notes ATTRIBUTE_UNUSED;
2914 enum rtx_code code = GET_CODE (x);
2916 #ifdef AUTO_INC_DEC
2917 /* If flow is invoked after reload, we must take existing AUTO_INC
2918 expresions into account. */
2919 if (reload_completed)
2921 for ( ; notes; notes = XEXP (notes, 1))
2923 if (REG_NOTE_KIND (notes) == REG_INC)
2925 int regno = REGNO (XEXP (notes, 0));
2927 /* Don't delete insns to set global regs. */
2928 if ((regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
2929 || REGNO_REG_SET_P (needed, regno))
2930 return 0;
2934 #endif
2936 /* If setting something that's a reg or part of one,
2937 see if that register's altered value will be live. */
2939 if (code == SET)
2941 rtx r = SET_DEST (x);
2943 /* A SET that is a subroutine call cannot be dead. */
2944 if (! call_ok && GET_CODE (SET_SRC (x)) == CALL)
2945 return 0;
2947 #ifdef HAVE_cc0
2948 if (GET_CODE (r) == CC0)
2949 return ! cc0_live;
2950 #endif
2952 if (GET_CODE (r) == MEM && ! MEM_VOLATILE_P (r))
2954 rtx temp;
2955 /* Walk the set of memory locations we are currently tracking
2956 and see if one is an identical match to this memory location.
2957 If so, this memory write is dead (remember, we're walking
2958 backwards from the end of the block to the start. */
2959 temp = mem_set_list;
2960 while (temp)
2962 if (rtx_equal_p (XEXP (temp, 0), r))
2963 return 1;
2964 temp = XEXP (temp, 1);
2968 while (GET_CODE (r) == SUBREG || GET_CODE (r) == STRICT_LOW_PART
2969 || GET_CODE (r) == ZERO_EXTRACT)
2970 r = SUBREG_REG (r);
2972 if (GET_CODE (r) == REG)
2974 int regno = REGNO (r);
2976 /* Don't delete insns to set global regs. */
2977 if ((regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
2978 /* Make sure insns to set frame pointer aren't deleted. */
2979 || (regno == FRAME_POINTER_REGNUM
2980 && (! reload_completed || frame_pointer_needed))
2981 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2982 || (regno == HARD_FRAME_POINTER_REGNUM
2983 && (! reload_completed || frame_pointer_needed))
2984 #endif
2985 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2986 /* Make sure insns to set arg pointer are never deleted
2987 (if the arg pointer isn't fixed, there will be a USE for
2988 it, so we can treat it normally). */
2989 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
2990 #endif
2991 || REGNO_REG_SET_P (needed, regno))
2992 return 0;
2994 /* If this is a hard register, verify that subsequent words are
2995 not needed. */
2996 if (regno < FIRST_PSEUDO_REGISTER)
2998 int n = HARD_REGNO_NREGS (regno, GET_MODE (r));
3000 while (--n > 0)
3001 if (REGNO_REG_SET_P (needed, regno+n))
3002 return 0;
3005 return 1;
3009 /* If performing several activities,
3010 insn is dead if each activity is individually dead.
3011 Also, CLOBBERs and USEs can be ignored; a CLOBBER or USE
3012 that's inside a PARALLEL doesn't make the insn worth keeping. */
3013 else if (code == PARALLEL)
3015 int i = XVECLEN (x, 0);
3017 for (i--; i >= 0; i--)
3018 if (GET_CODE (XVECEXP (x, 0, i)) != CLOBBER
3019 && GET_CODE (XVECEXP (x, 0, i)) != USE
3020 && ! insn_dead_p (XVECEXP (x, 0, i), needed, call_ok, NULL_RTX))
3021 return 0;
3023 return 1;
3026 /* A CLOBBER of a pseudo-register that is dead serves no purpose. That
3027 is not necessarily true for hard registers. */
3028 else if (code == CLOBBER && GET_CODE (XEXP (x, 0)) == REG
3029 && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER
3030 && ! REGNO_REG_SET_P (needed, REGNO (XEXP (x, 0))))
3031 return 1;
3033 /* We do not check other CLOBBER or USE here. An insn consisting of just
3034 a CLOBBER or just a USE should not be deleted. */
3035 return 0;
3038 /* If X is the pattern of the last insn in a libcall, and assuming X is dead,
3039 return 1 if the entire library call is dead.
3040 This is true if X copies a register (hard or pseudo)
3041 and if the hard return reg of the call insn is dead.
3042 (The caller should have tested the destination of X already for death.)
3044 If this insn doesn't just copy a register, then we don't
3045 have an ordinary libcall. In that case, cse could not have
3046 managed to substitute the source for the dest later on,
3047 so we can assume the libcall is dead.
3049 NEEDED is the bit vector of pseudoregs live before this insn.
3050 NOTE is the REG_RETVAL note of the insn. INSN is the insn itself. */
3052 static int
3053 libcall_dead_p (x, needed, note, insn)
3054 rtx x;
3055 regset needed;
3056 rtx note;
3057 rtx insn;
3059 register RTX_CODE code = GET_CODE (x);
3061 if (code == SET)
3063 register rtx r = SET_SRC (x);
3064 if (GET_CODE (r) == REG)
3066 rtx call = XEXP (note, 0);
3067 rtx call_pat;
3068 register int i;
3070 /* Find the call insn. */
3071 while (call != insn && GET_CODE (call) != CALL_INSN)
3072 call = NEXT_INSN (call);
3074 /* If there is none, do nothing special,
3075 since ordinary death handling can understand these insns. */
3076 if (call == insn)
3077 return 0;
3079 /* See if the hard reg holding the value is dead.
3080 If this is a PARALLEL, find the call within it. */
3081 call_pat = PATTERN (call);
3082 if (GET_CODE (call_pat) == PARALLEL)
3084 for (i = XVECLEN (call_pat, 0) - 1; i >= 0; i--)
3085 if (GET_CODE (XVECEXP (call_pat, 0, i)) == SET
3086 && GET_CODE (SET_SRC (XVECEXP (call_pat, 0, i))) == CALL)
3087 break;
3089 /* This may be a library call that is returning a value
3090 via invisible pointer. Do nothing special, since
3091 ordinary death handling can understand these insns. */
3092 if (i < 0)
3093 return 0;
3095 call_pat = XVECEXP (call_pat, 0, i);
3098 return insn_dead_p (call_pat, needed, 1, REG_NOTES (call));
3101 return 1;
3104 /* Return 1 if register REGNO was used before it was set, i.e. if it is
3105 live at function entry. Don't count global register variables, variables
3106 in registers that can be used for function arg passing, or variables in
3107 fixed hard registers. */
3110 regno_uninitialized (regno)
3111 int regno;
3113 if (n_basic_blocks == 0
3114 || (regno < FIRST_PSEUDO_REGISTER
3115 && (global_regs[regno]
3116 || fixed_regs[regno]
3117 || FUNCTION_ARG_REGNO_P (regno))))
3118 return 0;
3120 return REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, regno);
3123 /* 1 if register REGNO was alive at a place where `setjmp' was called
3124 and was set more than once or is an argument.
3125 Such regs may be clobbered by `longjmp'. */
3128 regno_clobbered_at_setjmp (regno)
3129 int regno;
3131 if (n_basic_blocks == 0)
3132 return 0;
3134 return ((REG_N_SETS (regno) > 1
3135 || REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, regno))
3136 && REGNO_REG_SET_P (regs_live_at_setjmp, regno));
3139 /* INSN references memory, possibly using autoincrement addressing modes.
3140 Find any entries on the mem_set_list that need to be invalidated due
3141 to an address change. */
3142 static void
3143 invalidate_mems_from_autoinc (insn)
3144 rtx insn;
3146 rtx note = REG_NOTES (insn);
3147 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
3149 if (REG_NOTE_KIND (note) == REG_INC)
3151 rtx temp = mem_set_list;
3152 rtx prev = NULL_RTX;
3154 while (temp)
3156 if (reg_overlap_mentioned_p (XEXP (note, 0), XEXP (temp, 0)))
3158 /* Splice temp out of list. */
3159 if (prev)
3160 XEXP (prev, 1) = XEXP (temp, 1);
3161 else
3162 mem_set_list = XEXP (temp, 1);
3164 else
3165 prev = temp;
3166 temp = XEXP (temp, 1);
3172 /* Process the registers that are set within X.
3173 Their bits are set to 1 in the regset DEAD,
3174 because they are dead prior to this insn.
3176 If INSN is nonzero, it is the insn being processed
3177 and the fact that it is nonzero implies this is the FINAL pass
3178 in propagate_block. In this case, various info about register
3179 usage is stored, LOG_LINKS fields of insns are set up. */
3181 static void
3182 mark_set_regs (needed, dead, x, insn, significant)
3183 regset needed;
3184 regset dead;
3185 rtx x;
3186 rtx insn;
3187 regset significant;
3189 register RTX_CODE code = GET_CODE (x);
3191 if (code == SET || code == CLOBBER)
3192 mark_set_1 (needed, dead, x, insn, significant);
3193 else if (code == PARALLEL)
3195 register int i;
3196 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
3198 code = GET_CODE (XVECEXP (x, 0, i));
3199 if (code == SET || code == CLOBBER)
3200 mark_set_1 (needed, dead, XVECEXP (x, 0, i), insn, significant);
3205 /* Process a single SET rtx, X. */
3207 static void
3208 mark_set_1 (needed, dead, x, insn, significant)
3209 regset needed;
3210 regset dead;
3211 rtx x;
3212 rtx insn;
3213 regset significant;
3215 register int regno;
3216 register rtx reg = SET_DEST (x);
3218 /* Some targets place small structures in registers for
3219 return values of functions. We have to detect this
3220 case specially here to get correct flow information. */
3221 if (GET_CODE (reg) == PARALLEL
3222 && GET_MODE (reg) == BLKmode)
3224 register int i;
3226 for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
3227 mark_set_1 (needed, dead, XVECEXP (reg, 0, i), insn, significant);
3228 return;
3231 /* Modifying just one hardware register of a multi-reg value
3232 or just a byte field of a register
3233 does not mean the value from before this insn is now dead.
3234 But it does mean liveness of that register at the end of the block
3235 is significant.
3237 Within mark_set_1, however, we treat it as if the register is
3238 indeed modified. mark_used_regs will, however, also treat this
3239 register as being used. Thus, we treat these insns as setting a
3240 new value for the register as a function of its old value. This
3241 cases LOG_LINKS to be made appropriately and this will help combine. */
3243 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
3244 || GET_CODE (reg) == SIGN_EXTRACT
3245 || GET_CODE (reg) == STRICT_LOW_PART)
3246 reg = XEXP (reg, 0);
3248 /* If this set is a MEM, then it kills any aliased writes.
3249 If this set is a REG, then it kills any MEMs which use the reg. */
3250 if (GET_CODE (reg) == MEM
3251 || GET_CODE (reg) == REG)
3253 rtx temp = mem_set_list;
3254 rtx prev = NULL_RTX;
3256 while (temp)
3258 if ((GET_CODE (reg) == MEM
3259 && output_dependence (XEXP (temp, 0), reg))
3260 || (GET_CODE (reg) == REG
3261 && reg_overlap_mentioned_p (reg, XEXP (temp, 0))))
3263 /* Splice this entry out of the list. */
3264 if (prev)
3265 XEXP (prev, 1) = XEXP (temp, 1);
3266 else
3267 mem_set_list = XEXP (temp, 1);
3269 else
3270 prev = temp;
3271 temp = XEXP (temp, 1);
3275 /* If the memory reference had embedded side effects (autoincrement
3276 address modes. Then we may need to kill some entries on the
3277 memory set list. */
3278 if (insn && GET_CODE (reg) == MEM)
3279 invalidate_mems_from_autoinc (insn);
3281 if (GET_CODE (reg) == MEM && ! side_effects_p (reg)
3282 /* We do not know the size of a BLKmode store, so we do not track
3283 them for redundant store elimination. */
3284 && GET_MODE (reg) != BLKmode
3285 /* There are no REG_INC notes for SP, so we can't assume we'll see
3286 everything that invalidates it. To be safe, don't eliminate any
3287 stores though SP; none of them should be redundant anyway. */
3288 && ! reg_mentioned_p (stack_pointer_rtx, reg))
3289 mem_set_list = gen_rtx_EXPR_LIST (VOIDmode, reg, mem_set_list);
3291 if (GET_CODE (reg) == REG
3292 && (regno = REGNO (reg), ! (regno == FRAME_POINTER_REGNUM
3293 && (! reload_completed || frame_pointer_needed)))
3294 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3295 && ! (regno == HARD_FRAME_POINTER_REGNUM
3296 && (! reload_completed || frame_pointer_needed))
3297 #endif
3298 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3299 && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
3300 #endif
3301 && ! (regno < FIRST_PSEUDO_REGISTER && global_regs[regno]))
3302 /* && regno != STACK_POINTER_REGNUM) -- let's try without this. */
3304 int some_needed = REGNO_REG_SET_P (needed, regno);
3305 int some_not_needed = ! some_needed;
3307 /* Mark it as a significant register for this basic block. */
3308 if (significant)
3309 SET_REGNO_REG_SET (significant, regno);
3311 /* Mark it as dead before this insn. */
3312 SET_REGNO_REG_SET (dead, regno);
3314 /* A hard reg in a wide mode may really be multiple registers.
3315 If so, mark all of them just like the first. */
3316 if (regno < FIRST_PSEUDO_REGISTER)
3318 int n;
3320 /* Nothing below is needed for the stack pointer; get out asap.
3321 Eg, log links aren't needed, since combine won't use them. */
3322 if (regno == STACK_POINTER_REGNUM)
3323 return;
3325 n = HARD_REGNO_NREGS (regno, GET_MODE (reg));
3326 while (--n > 0)
3328 int regno_n = regno + n;
3329 int needed_regno = REGNO_REG_SET_P (needed, regno_n);
3330 if (significant)
3331 SET_REGNO_REG_SET (significant, regno_n);
3333 SET_REGNO_REG_SET (dead, regno_n);
3334 some_needed |= needed_regno;
3335 some_not_needed |= ! needed_regno;
3338 /* Additional data to record if this is the final pass. */
3339 if (insn)
3341 register rtx y = reg_next_use[regno];
3342 register int blocknum = BLOCK_NUM (insn);
3344 /* If this is a hard reg, record this function uses the reg. */
3346 if (regno < FIRST_PSEUDO_REGISTER)
3348 register int i;
3349 int endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
3351 for (i = regno; i < endregno; i++)
3353 /* The next use is no longer "next", since a store
3354 intervenes. */
3355 reg_next_use[i] = 0;
3357 regs_ever_live[i] = 1;
3358 REG_N_SETS (i)++;
3361 else
3363 /* The next use is no longer "next", since a store
3364 intervenes. */
3365 reg_next_use[regno] = 0;
3367 /* Keep track of which basic blocks each reg appears in. */
3369 if (REG_BASIC_BLOCK (regno) == REG_BLOCK_UNKNOWN)
3370 REG_BASIC_BLOCK (regno) = blocknum;
3371 else if (REG_BASIC_BLOCK (regno) != blocknum)
3372 REG_BASIC_BLOCK (regno) = REG_BLOCK_GLOBAL;
3374 /* Count (weighted) references, stores, etc. This counts a
3375 register twice if it is modified, but that is correct. */
3376 REG_N_SETS (regno)++;
3378 REG_N_REFS (regno) += loop_depth;
3380 /* The insns where a reg is live are normally counted
3381 elsewhere, but we want the count to include the insn
3382 where the reg is set, and the normal counting mechanism
3383 would not count it. */
3384 REG_LIVE_LENGTH (regno)++;
3387 if (! some_not_needed)
3389 /* Make a logical link from the next following insn
3390 that uses this register, back to this insn.
3391 The following insns have already been processed.
3393 We don't build a LOG_LINK for hard registers containing
3394 in ASM_OPERANDs. If these registers get replaced,
3395 we might wind up changing the semantics of the insn,
3396 even if reload can make what appear to be valid assignments
3397 later. */
3398 if (y && (BLOCK_NUM (y) == blocknum)
3399 && (regno >= FIRST_PSEUDO_REGISTER
3400 || asm_noperands (PATTERN (y)) < 0))
3401 LOG_LINKS (y)
3402 = gen_rtx_INSN_LIST (VOIDmode, insn, LOG_LINKS (y));
3404 else if (! some_needed)
3406 /* Note that dead stores have already been deleted when possible
3407 If we get here, we have found a dead store that cannot
3408 be eliminated (because the same insn does something useful).
3409 Indicate this by marking the reg being set as dying here. */
3410 REG_NOTES (insn)
3411 = gen_rtx_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn));
3412 REG_N_DEATHS (REGNO (reg))++;
3414 else
3416 /* This is a case where we have a multi-word hard register
3417 and some, but not all, of the words of the register are
3418 needed in subsequent insns. Write REG_UNUSED notes
3419 for those parts that were not needed. This case should
3420 be rare. */
3422 int i;
3424 for (i = HARD_REGNO_NREGS (regno, GET_MODE (reg)) - 1;
3425 i >= 0; i--)
3426 if (!REGNO_REG_SET_P (needed, regno + i))
3427 REG_NOTES (insn)
3428 = gen_rtx_EXPR_LIST (REG_UNUSED,
3429 gen_rtx_REG (reg_raw_mode[regno + i],
3430 regno + i),
3431 REG_NOTES (insn));
3435 else if (GET_CODE (reg) == REG)
3436 reg_next_use[regno] = 0;
3438 /* If this is the last pass and this is a SCRATCH, show it will be dying
3439 here and count it. */
3440 else if (GET_CODE (reg) == SCRATCH && insn != 0)
3442 REG_NOTES (insn)
3443 = gen_rtx_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn));
3447 #ifdef AUTO_INC_DEC
3449 /* X is a MEM found in INSN. See if we can convert it into an auto-increment
3450 reference. */
3452 static void
3453 find_auto_inc (needed, x, insn)
3454 regset needed;
3455 rtx x;
3456 rtx insn;
3458 rtx addr = XEXP (x, 0);
3459 HOST_WIDE_INT offset = 0;
3460 rtx set;
3462 /* Here we detect use of an index register which might be good for
3463 postincrement, postdecrement, preincrement, or predecrement. */
3465 if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
3466 offset = INTVAL (XEXP (addr, 1)), addr = XEXP (addr, 0);
3468 if (GET_CODE (addr) == REG)
3470 register rtx y;
3471 register int size = GET_MODE_SIZE (GET_MODE (x));
3472 rtx use;
3473 rtx incr;
3474 int regno = REGNO (addr);
3476 /* Is the next use an increment that might make auto-increment? */
3477 if ((incr = reg_next_use[regno]) != 0
3478 && (set = single_set (incr)) != 0
3479 && GET_CODE (set) == SET
3480 && BLOCK_NUM (incr) == BLOCK_NUM (insn)
3481 /* Can't add side effects to jumps; if reg is spilled and
3482 reloaded, there's no way to store back the altered value. */
3483 && GET_CODE (insn) != JUMP_INSN
3484 && (y = SET_SRC (set), GET_CODE (y) == PLUS)
3485 && XEXP (y, 0) == addr
3486 && GET_CODE (XEXP (y, 1)) == CONST_INT
3487 && ((HAVE_POST_INCREMENT
3488 && (INTVAL (XEXP (y, 1)) == size && offset == 0))
3489 || (HAVE_POST_DECREMENT
3490 && (INTVAL (XEXP (y, 1)) == - size && offset == 0))
3491 || (HAVE_PRE_INCREMENT
3492 && (INTVAL (XEXP (y, 1)) == size && offset == size))
3493 || (HAVE_PRE_DECREMENT
3494 && (INTVAL (XEXP (y, 1)) == - size && offset == - size)))
3495 /* Make sure this reg appears only once in this insn. */
3496 && (use = find_use_as_address (PATTERN (insn), addr, offset),
3497 use != 0 && use != (rtx) 1))
3499 rtx q = SET_DEST (set);
3500 enum rtx_code inc_code = (INTVAL (XEXP (y, 1)) == size
3501 ? (offset ? PRE_INC : POST_INC)
3502 : (offset ? PRE_DEC : POST_DEC));
3504 if (dead_or_set_p (incr, addr))
3506 /* This is the simple case. Try to make the auto-inc. If
3507 we can't, we are done. Otherwise, we will do any
3508 needed updates below. */
3509 if (! validate_change (insn, &XEXP (x, 0),
3510 gen_rtx_fmt_e (inc_code, Pmode, addr),
3512 return;
3514 else if (GET_CODE (q) == REG
3515 /* PREV_INSN used here to check the semi-open interval
3516 [insn,incr). */
3517 && ! reg_used_between_p (q, PREV_INSN (insn), incr)
3518 /* We must also check for sets of q as q may be
3519 a call clobbered hard register and there may
3520 be a call between PREV_INSN (insn) and incr. */
3521 && ! reg_set_between_p (q, PREV_INSN (insn), incr))
3523 /* We have *p followed sometime later by q = p+size.
3524 Both p and q must be live afterward,
3525 and q is not used between INSN and its assignment.
3526 Change it to q = p, ...*q..., q = q+size.
3527 Then fall into the usual case. */
3528 rtx insns, temp;
3529 basic_block bb;
3531 start_sequence ();
3532 emit_move_insn (q, addr);
3533 insns = get_insns ();
3534 end_sequence ();
3536 bb = BLOCK_FOR_INSN (insn);
3537 for (temp = insns; temp; temp = NEXT_INSN (temp))
3538 set_block_for_insn (temp, bb);
3540 /* If we can't make the auto-inc, or can't make the
3541 replacement into Y, exit. There's no point in making
3542 the change below if we can't do the auto-inc and doing
3543 so is not correct in the pre-inc case. */
3545 validate_change (insn, &XEXP (x, 0),
3546 gen_rtx_fmt_e (inc_code, Pmode, q),
3548 validate_change (incr, &XEXP (y, 0), q, 1);
3549 if (! apply_change_group ())
3550 return;
3552 /* We now know we'll be doing this change, so emit the
3553 new insn(s) and do the updates. */
3554 emit_insns_before (insns, insn);
3556 if (BLOCK_FOR_INSN (insn)->head == insn)
3557 BLOCK_FOR_INSN (insn)->head = insns;
3559 /* INCR will become a NOTE and INSN won't contain a
3560 use of ADDR. If a use of ADDR was just placed in
3561 the insn before INSN, make that the next use.
3562 Otherwise, invalidate it. */
3563 if (GET_CODE (PREV_INSN (insn)) == INSN
3564 && GET_CODE (PATTERN (PREV_INSN (insn))) == SET
3565 && SET_SRC (PATTERN (PREV_INSN (insn))) == addr)
3566 reg_next_use[regno] = PREV_INSN (insn);
3567 else
3568 reg_next_use[regno] = 0;
3570 addr = q;
3571 regno = REGNO (q);
3573 /* REGNO is now used in INCR which is below INSN, but
3574 it previously wasn't live here. If we don't mark
3575 it as needed, we'll put a REG_DEAD note for it
3576 on this insn, which is incorrect. */
3577 SET_REGNO_REG_SET (needed, regno);
3579 /* If there are any calls between INSN and INCR, show
3580 that REGNO now crosses them. */
3581 for (temp = insn; temp != incr; temp = NEXT_INSN (temp))
3582 if (GET_CODE (temp) == CALL_INSN)
3583 REG_N_CALLS_CROSSED (regno)++;
3585 else
3586 return;
3588 /* If we haven't returned, it means we were able to make the
3589 auto-inc, so update the status. First, record that this insn
3590 has an implicit side effect. */
3592 REG_NOTES (insn)
3593 = gen_rtx_EXPR_LIST (REG_INC, addr, REG_NOTES (insn));
3595 /* Modify the old increment-insn to simply copy
3596 the already-incremented value of our register. */
3597 if (! validate_change (incr, &SET_SRC (set), addr, 0))
3598 abort ();
3600 /* If that makes it a no-op (copying the register into itself) delete
3601 it so it won't appear to be a "use" and a "set" of this
3602 register. */
3603 if (SET_DEST (set) == addr)
3605 PUT_CODE (incr, NOTE);
3606 NOTE_LINE_NUMBER (incr) = NOTE_INSN_DELETED;
3607 NOTE_SOURCE_FILE (incr) = 0;
3610 if (regno >= FIRST_PSEUDO_REGISTER)
3612 /* Count an extra reference to the reg. When a reg is
3613 incremented, spilling it is worse, so we want to make
3614 that less likely. */
3615 REG_N_REFS (regno) += loop_depth;
3617 /* Count the increment as a setting of the register,
3618 even though it isn't a SET in rtl. */
3619 REG_N_SETS (regno)++;
3624 #endif /* AUTO_INC_DEC */
3626 /* Scan expression X and store a 1-bit in LIVE for each reg it uses.
3627 This is done assuming the registers needed from X
3628 are those that have 1-bits in NEEDED.
3630 On the final pass, FINAL is 1. This means try for autoincrement
3631 and count the uses and deaths of each pseudo-reg.
3633 INSN is the containing instruction. If INSN is dead, this function is not
3634 called. */
3636 static void
3637 mark_used_regs (needed, live, x, final, insn)
3638 regset needed;
3639 regset live;
3640 rtx x;
3641 int final;
3642 rtx insn;
3644 register RTX_CODE code;
3645 register int regno;
3646 int i;
3648 retry:
3649 code = GET_CODE (x);
3650 switch (code)
3652 case LABEL_REF:
3653 case SYMBOL_REF:
3654 case CONST_INT:
3655 case CONST:
3656 case CONST_DOUBLE:
3657 case PC:
3658 case ADDR_VEC:
3659 case ADDR_DIFF_VEC:
3660 return;
3662 #ifdef HAVE_cc0
3663 case CC0:
3664 cc0_live = 1;
3665 return;
3666 #endif
3668 case CLOBBER:
3669 /* If we are clobbering a MEM, mark any registers inside the address
3670 as being used. */
3671 if (GET_CODE (XEXP (x, 0)) == MEM)
3672 mark_used_regs (needed, live, XEXP (XEXP (x, 0), 0), final, insn);
3673 return;
3675 case MEM:
3676 /* Invalidate the data for the last MEM stored, but only if MEM is
3677 something that can be stored into. */
3678 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
3679 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
3680 ; /* needn't clear the memory set list */
3681 else
3683 rtx temp = mem_set_list;
3684 rtx prev = NULL_RTX;
3686 while (temp)
3688 if (anti_dependence (XEXP (temp, 0), x))
3690 /* Splice temp out of the list. */
3691 if (prev)
3692 XEXP (prev, 1) = XEXP (temp, 1);
3693 else
3694 mem_set_list = XEXP (temp, 1);
3696 else
3697 prev = temp;
3698 temp = XEXP (temp, 1);
3702 /* If the memory reference had embedded side effects (autoincrement
3703 address modes. Then we may need to kill some entries on the
3704 memory set list. */
3705 if (insn)
3706 invalidate_mems_from_autoinc (insn);
3708 #ifdef AUTO_INC_DEC
3709 if (final)
3710 find_auto_inc (needed, x, insn);
3711 #endif
3712 break;
3714 case SUBREG:
3715 if (GET_CODE (SUBREG_REG (x)) == REG
3716 && REGNO (SUBREG_REG (x)) >= FIRST_PSEUDO_REGISTER
3717 && (GET_MODE_SIZE (GET_MODE (x))
3718 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
3719 REG_CHANGES_SIZE (REGNO (SUBREG_REG (x))) = 1;
3721 /* While we're here, optimize this case. */
3722 x = SUBREG_REG (x);
3724 /* In case the SUBREG is not of a register, don't optimize */
3725 if (GET_CODE (x) != REG)
3727 mark_used_regs (needed, live, x, final, insn);
3728 return;
3731 /* ... fall through ... */
3733 case REG:
3734 /* See a register other than being set
3735 => mark it as needed. */
3737 regno = REGNO (x);
3739 int some_needed = REGNO_REG_SET_P (needed, regno);
3740 int some_not_needed = ! some_needed;
3742 SET_REGNO_REG_SET (live, regno);
3744 /* A hard reg in a wide mode may really be multiple registers.
3745 If so, mark all of them just like the first. */
3746 if (regno < FIRST_PSEUDO_REGISTER)
3748 int n;
3750 /* For stack ptr or fixed arg pointer,
3751 nothing below can be necessary, so waste no more time. */
3752 if (regno == STACK_POINTER_REGNUM
3753 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3754 || (regno == HARD_FRAME_POINTER_REGNUM
3755 && (! reload_completed || frame_pointer_needed))
3756 #endif
3757 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3758 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
3759 #endif
3760 || (regno == FRAME_POINTER_REGNUM
3761 && (! reload_completed || frame_pointer_needed)))
3763 /* If this is a register we are going to try to eliminate,
3764 don't mark it live here. If we are successful in
3765 eliminating it, it need not be live unless it is used for
3766 pseudos, in which case it will have been set live when
3767 it was allocated to the pseudos. If the register will not
3768 be eliminated, reload will set it live at that point. */
3770 if (! TEST_HARD_REG_BIT (elim_reg_set, regno))
3771 regs_ever_live[regno] = 1;
3772 return;
3774 /* No death notes for global register variables;
3775 their values are live after this function exits. */
3776 if (global_regs[regno])
3778 if (final)
3779 reg_next_use[regno] = insn;
3780 return;
3783 n = HARD_REGNO_NREGS (regno, GET_MODE (x));
3784 while (--n > 0)
3786 int regno_n = regno + n;
3787 int needed_regno = REGNO_REG_SET_P (needed, regno_n);
3789 SET_REGNO_REG_SET (live, regno_n);
3790 some_needed |= needed_regno;
3791 some_not_needed |= ! needed_regno;
3794 if (final)
3796 /* Record where each reg is used, so when the reg
3797 is set we know the next insn that uses it. */
3799 reg_next_use[regno] = insn;
3801 if (regno < FIRST_PSEUDO_REGISTER)
3803 /* If a hard reg is being used,
3804 record that this function does use it. */
3806 i = HARD_REGNO_NREGS (regno, GET_MODE (x));
3807 if (i == 0)
3808 i = 1;
3810 regs_ever_live[regno + --i] = 1;
3811 while (i > 0);
3813 else
3815 /* Keep track of which basic block each reg appears in. */
3817 register int blocknum = BLOCK_NUM (insn);
3819 if (REG_BASIC_BLOCK (regno) == REG_BLOCK_UNKNOWN)
3820 REG_BASIC_BLOCK (regno) = blocknum;
3821 else if (REG_BASIC_BLOCK (regno) != blocknum)
3822 REG_BASIC_BLOCK (regno) = REG_BLOCK_GLOBAL;
3824 /* Count (weighted) number of uses of each reg. */
3826 REG_N_REFS (regno) += loop_depth;
3829 /* Record and count the insns in which a reg dies.
3830 If it is used in this insn and was dead below the insn
3831 then it dies in this insn. If it was set in this insn,
3832 we do not make a REG_DEAD note; likewise if we already
3833 made such a note. */
3835 if (some_not_needed
3836 && ! dead_or_set_p (insn, x)
3837 #if 0
3838 && (regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
3839 #endif
3842 /* Check for the case where the register dying partially
3843 overlaps the register set by this insn. */
3844 if (regno < FIRST_PSEUDO_REGISTER
3845 && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
3847 int n = HARD_REGNO_NREGS (regno, GET_MODE (x));
3848 while (--n >= 0)
3849 some_needed |= dead_or_set_regno_p (insn, regno + n);
3852 /* If none of the words in X is needed, make a REG_DEAD
3853 note. Otherwise, we must make partial REG_DEAD notes. */
3854 if (! some_needed)
3856 REG_NOTES (insn)
3857 = gen_rtx_EXPR_LIST (REG_DEAD, x, REG_NOTES (insn));
3858 REG_N_DEATHS (regno)++;
3860 else
3862 int i;
3864 /* Don't make a REG_DEAD note for a part of a register
3865 that is set in the insn. */
3867 for (i = HARD_REGNO_NREGS (regno, GET_MODE (x)) - 1;
3868 i >= 0; i--)
3869 if (!REGNO_REG_SET_P (needed, regno + i)
3870 && ! dead_or_set_regno_p (insn, regno + i))
3871 REG_NOTES (insn)
3872 = gen_rtx_EXPR_LIST (REG_DEAD,
3873 gen_rtx_REG (reg_raw_mode[regno + i],
3874 regno + i),
3875 REG_NOTES (insn));
3880 return;
3882 case SET:
3884 register rtx testreg = SET_DEST (x);
3885 int mark_dest = 0;
3887 /* If storing into MEM, don't show it as being used. But do
3888 show the address as being used. */
3889 if (GET_CODE (testreg) == MEM)
3891 #ifdef AUTO_INC_DEC
3892 if (final)
3893 find_auto_inc (needed, testreg, insn);
3894 #endif
3895 mark_used_regs (needed, live, XEXP (testreg, 0), final, insn);
3896 mark_used_regs (needed, live, SET_SRC (x), final, insn);
3897 return;
3900 /* Storing in STRICT_LOW_PART is like storing in a reg
3901 in that this SET might be dead, so ignore it in TESTREG.
3902 but in some other ways it is like using the reg.
3904 Storing in a SUBREG or a bit field is like storing the entire
3905 register in that if the register's value is not used
3906 then this SET is not needed. */
3907 while (GET_CODE (testreg) == STRICT_LOW_PART
3908 || GET_CODE (testreg) == ZERO_EXTRACT
3909 || GET_CODE (testreg) == SIGN_EXTRACT
3910 || GET_CODE (testreg) == SUBREG)
3912 if (GET_CODE (testreg) == SUBREG
3913 && GET_CODE (SUBREG_REG (testreg)) == REG
3914 && REGNO (SUBREG_REG (testreg)) >= FIRST_PSEUDO_REGISTER
3915 && (GET_MODE_SIZE (GET_MODE (testreg))
3916 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (testreg)))))
3917 REG_CHANGES_SIZE (REGNO (SUBREG_REG (testreg))) = 1;
3919 /* Modifying a single register in an alternate mode
3920 does not use any of the old value. But these other
3921 ways of storing in a register do use the old value. */
3922 if (GET_CODE (testreg) == SUBREG
3923 && !(REG_SIZE (SUBREG_REG (testreg)) > REG_SIZE (testreg)))
3925 else
3926 mark_dest = 1;
3928 testreg = XEXP (testreg, 0);
3931 /* If this is a store into a register,
3932 recursively scan the value being stored. */
3934 if ((GET_CODE (testreg) == PARALLEL
3935 && GET_MODE (testreg) == BLKmode)
3936 || (GET_CODE (testreg) == REG
3937 && (regno = REGNO (testreg), ! (regno == FRAME_POINTER_REGNUM
3938 && (! reload_completed || frame_pointer_needed)))
3939 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3940 && ! (regno == HARD_FRAME_POINTER_REGNUM
3941 && (! reload_completed || frame_pointer_needed))
3942 #endif
3943 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3944 && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
3945 #endif
3947 /* We used to exclude global_regs here, but that seems wrong.
3948 Storing in them is like storing in mem. */
3950 mark_used_regs (needed, live, SET_SRC (x), final, insn);
3951 if (mark_dest)
3952 mark_used_regs (needed, live, SET_DEST (x), final, insn);
3953 return;
3956 break;
3958 case RETURN:
3959 /* If exiting needs the right stack value, consider this insn as
3960 using the stack pointer. In any event, consider it as using
3961 all global registers and all registers used by return. */
3962 if (! EXIT_IGNORE_STACK
3963 || (! FRAME_POINTER_REQUIRED
3964 && ! current_function_calls_alloca
3965 && flag_omit_frame_pointer)
3966 || current_function_sp_is_unchanging)
3967 SET_REGNO_REG_SET (live, STACK_POINTER_REGNUM);
3969 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3970 if (global_regs[i]
3971 #ifdef EPILOGUE_USES
3972 || EPILOGUE_USES (i)
3973 #endif
3975 SET_REGNO_REG_SET (live, i);
3976 break;
3978 case ASM_OPERANDS:
3979 case UNSPEC_VOLATILE:
3980 case TRAP_IF:
3981 case ASM_INPUT:
3983 /* Traditional and volatile asm instructions must be considered to use
3984 and clobber all hard registers, all pseudo-registers and all of
3985 memory. So must TRAP_IF and UNSPEC_VOLATILE operations.
3987 Consider for instance a volatile asm that changes the fpu rounding
3988 mode. An insn should not be moved across this even if it only uses
3989 pseudo-regs because it might give an incorrectly rounded result.
3991 ?!? Unfortunately, marking all hard registers as live causes massive
3992 problems for the register allocator and marking all pseudos as live
3993 creates mountains of uninitialized variable warnings.
3995 So for now, just clear the memory set list and mark any regs
3996 we can find in ASM_OPERANDS as used. */
3997 if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
3998 mem_set_list = NULL_RTX;
4000 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
4001 We can not just fall through here since then we would be confused
4002 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
4003 traditional asms unlike their normal usage. */
4004 if (code == ASM_OPERANDS)
4006 int j;
4008 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
4009 mark_used_regs (needed, live, ASM_OPERANDS_INPUT (x, j),
4010 final, insn);
4012 break;
4016 default:
4017 break;
4020 /* Recursively scan the operands of this expression. */
4023 register const char *fmt = GET_RTX_FORMAT (code);
4024 register int i;
4026 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4028 if (fmt[i] == 'e')
4030 /* Tail recursive case: save a function call level. */
4031 if (i == 0)
4033 x = XEXP (x, 0);
4034 goto retry;
4036 mark_used_regs (needed, live, XEXP (x, i), final, insn);
4038 else if (fmt[i] == 'E')
4040 register int j;
4041 for (j = 0; j < XVECLEN (x, i); j++)
4042 mark_used_regs (needed, live, XVECEXP (x, i, j), final, insn);
4048 #ifdef AUTO_INC_DEC
4050 static int
4051 try_pre_increment_1 (insn)
4052 rtx insn;
4054 /* Find the next use of this reg. If in same basic block,
4055 make it do pre-increment or pre-decrement if appropriate. */
4056 rtx x = single_set (insn);
4057 HOST_WIDE_INT amount = ((GET_CODE (SET_SRC (x)) == PLUS ? 1 : -1)
4058 * INTVAL (XEXP (SET_SRC (x), 1)));
4059 int regno = REGNO (SET_DEST (x));
4060 rtx y = reg_next_use[regno];
4061 if (y != 0
4062 && BLOCK_NUM (y) == BLOCK_NUM (insn)
4063 /* Don't do this if the reg dies, or gets set in y; a standard addressing
4064 mode would be better. */
4065 && ! dead_or_set_p (y, SET_DEST (x))
4066 && try_pre_increment (y, SET_DEST (x), amount))
4068 /* We have found a suitable auto-increment
4069 and already changed insn Y to do it.
4070 So flush this increment-instruction. */
4071 PUT_CODE (insn, NOTE);
4072 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
4073 NOTE_SOURCE_FILE (insn) = 0;
4074 /* Count a reference to this reg for the increment
4075 insn we are deleting. When a reg is incremented.
4076 spilling it is worse, so we want to make that
4077 less likely. */
4078 if (regno >= FIRST_PSEUDO_REGISTER)
4080 REG_N_REFS (regno) += loop_depth;
4081 REG_N_SETS (regno)++;
4083 return 1;
4085 return 0;
4088 /* Try to change INSN so that it does pre-increment or pre-decrement
4089 addressing on register REG in order to add AMOUNT to REG.
4090 AMOUNT is negative for pre-decrement.
4091 Returns 1 if the change could be made.
4092 This checks all about the validity of the result of modifying INSN. */
4094 static int
4095 try_pre_increment (insn, reg, amount)
4096 rtx insn, reg;
4097 HOST_WIDE_INT amount;
4099 register rtx use;
4101 /* Nonzero if we can try to make a pre-increment or pre-decrement.
4102 For example, addl $4,r1; movl (r1),... can become movl +(r1),... */
4103 int pre_ok = 0;
4104 /* Nonzero if we can try to make a post-increment or post-decrement.
4105 For example, addl $4,r1; movl -4(r1),... can become movl (r1)+,...
4106 It is possible for both PRE_OK and POST_OK to be nonzero if the machine
4107 supports both pre-inc and post-inc, or both pre-dec and post-dec. */
4108 int post_ok = 0;
4110 /* Nonzero if the opportunity actually requires post-inc or post-dec. */
4111 int do_post = 0;
4113 /* From the sign of increment, see which possibilities are conceivable
4114 on this target machine. */
4115 if (HAVE_PRE_INCREMENT && amount > 0)
4116 pre_ok = 1;
4117 if (HAVE_POST_INCREMENT && amount > 0)
4118 post_ok = 1;
4120 if (HAVE_PRE_DECREMENT && amount < 0)
4121 pre_ok = 1;
4122 if (HAVE_POST_DECREMENT && amount < 0)
4123 post_ok = 1;
4125 if (! (pre_ok || post_ok))
4126 return 0;
4128 /* It is not safe to add a side effect to a jump insn
4129 because if the incremented register is spilled and must be reloaded
4130 there would be no way to store the incremented value back in memory. */
4132 if (GET_CODE (insn) == JUMP_INSN)
4133 return 0;
4135 use = 0;
4136 if (pre_ok)
4137 use = find_use_as_address (PATTERN (insn), reg, 0);
4138 if (post_ok && (use == 0 || use == (rtx) 1))
4140 use = find_use_as_address (PATTERN (insn), reg, -amount);
4141 do_post = 1;
4144 if (use == 0 || use == (rtx) 1)
4145 return 0;
4147 if (GET_MODE_SIZE (GET_MODE (use)) != (amount > 0 ? amount : - amount))
4148 return 0;
4150 /* See if this combination of instruction and addressing mode exists. */
4151 if (! validate_change (insn, &XEXP (use, 0),
4152 gen_rtx_fmt_e (amount > 0
4153 ? (do_post ? POST_INC : PRE_INC)
4154 : (do_post ? POST_DEC : PRE_DEC),
4155 Pmode, reg), 0))
4156 return 0;
4158 /* Record that this insn now has an implicit side effect on X. */
4159 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_INC, reg, REG_NOTES (insn));
4160 return 1;
4163 #endif /* AUTO_INC_DEC */
4165 /* Find the place in the rtx X where REG is used as a memory address.
4166 Return the MEM rtx that so uses it.
4167 If PLUSCONST is nonzero, search instead for a memory address equivalent to
4168 (plus REG (const_int PLUSCONST)).
4170 If such an address does not appear, return 0.
4171 If REG appears more than once, or is used other than in such an address,
4172 return (rtx)1. */
4175 find_use_as_address (x, reg, plusconst)
4176 register rtx x;
4177 rtx reg;
4178 HOST_WIDE_INT plusconst;
4180 enum rtx_code code = GET_CODE (x);
4181 const char *fmt = GET_RTX_FORMAT (code);
4182 register int i;
4183 register rtx value = 0;
4184 register rtx tem;
4186 if (code == MEM && XEXP (x, 0) == reg && plusconst == 0)
4187 return x;
4189 if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
4190 && XEXP (XEXP (x, 0), 0) == reg
4191 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4192 && INTVAL (XEXP (XEXP (x, 0), 1)) == plusconst)
4193 return x;
4195 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
4197 /* If REG occurs inside a MEM used in a bit-field reference,
4198 that is unacceptable. */
4199 if (find_use_as_address (XEXP (x, 0), reg, 0) != 0)
4200 return (rtx) (HOST_WIDE_INT) 1;
4203 if (x == reg)
4204 return (rtx) (HOST_WIDE_INT) 1;
4206 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4208 if (fmt[i] == 'e')
4210 tem = find_use_as_address (XEXP (x, i), reg, plusconst);
4211 if (value == 0)
4212 value = tem;
4213 else if (tem != 0)
4214 return (rtx) (HOST_WIDE_INT) 1;
4216 if (fmt[i] == 'E')
4218 register int j;
4219 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4221 tem = find_use_as_address (XVECEXP (x, i, j), reg, plusconst);
4222 if (value == 0)
4223 value = tem;
4224 else if (tem != 0)
4225 return (rtx) (HOST_WIDE_INT) 1;
4230 return value;
4233 /* Write information about registers and basic blocks into FILE.
4234 This is part of making a debugging dump. */
4236 void
4237 dump_flow_info (file)
4238 FILE *file;
4240 register int i;
4241 static const char * const reg_class_names[] = REG_CLASS_NAMES;
4243 fprintf (file, "%d registers.\n", max_regno);
4244 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
4245 if (REG_N_REFS (i))
4247 enum reg_class class, altclass;
4248 fprintf (file, "\nRegister %d used %d times across %d insns",
4249 i, REG_N_REFS (i), REG_LIVE_LENGTH (i));
4250 if (REG_BASIC_BLOCK (i) >= 0)
4251 fprintf (file, " in block %d", REG_BASIC_BLOCK (i));
4252 if (REG_N_SETS (i))
4253 fprintf (file, "; set %d time%s", REG_N_SETS (i),
4254 (REG_N_SETS (i) == 1) ? "" : "s");
4255 if (REG_USERVAR_P (regno_reg_rtx[i]))
4256 fprintf (file, "; user var");
4257 if (REG_N_DEATHS (i) != 1)
4258 fprintf (file, "; dies in %d places", REG_N_DEATHS (i));
4259 if (REG_N_CALLS_CROSSED (i) == 1)
4260 fprintf (file, "; crosses 1 call");
4261 else if (REG_N_CALLS_CROSSED (i))
4262 fprintf (file, "; crosses %d calls", REG_N_CALLS_CROSSED (i));
4263 if (PSEUDO_REGNO_BYTES (i) != UNITS_PER_WORD)
4264 fprintf (file, "; %d bytes", PSEUDO_REGNO_BYTES (i));
4265 class = reg_preferred_class (i);
4266 altclass = reg_alternate_class (i);
4267 if (class != GENERAL_REGS || altclass != ALL_REGS)
4269 if (altclass == ALL_REGS || class == ALL_REGS)
4270 fprintf (file, "; pref %s", reg_class_names[(int) class]);
4271 else if (altclass == NO_REGS)
4272 fprintf (file, "; %s or none", reg_class_names[(int) class]);
4273 else
4274 fprintf (file, "; pref %s, else %s",
4275 reg_class_names[(int) class],
4276 reg_class_names[(int) altclass]);
4278 if (REGNO_POINTER_FLAG (i))
4279 fprintf (file, "; pointer");
4280 fprintf (file, ".\n");
4283 fprintf (file, "\n%d basic blocks.\n", n_basic_blocks);
4284 for (i = 0; i < n_basic_blocks; i++)
4286 register basic_block bb = BASIC_BLOCK (i);
4287 register int regno;
4288 register edge e;
4290 fprintf (file, "\nBasic block %d: first insn %d, last %d.\n",
4291 i, INSN_UID (bb->head), INSN_UID (bb->end));
4293 fprintf (file, "Predecessors: ");
4294 for (e = bb->pred; e ; e = e->pred_next)
4295 dump_edge_info (file, e, 0);
4297 fprintf (file, "\nSuccessors: ");
4298 for (e = bb->succ; e ; e = e->succ_next)
4299 dump_edge_info (file, e, 1);
4301 fprintf (file, "\nRegisters live at start:");
4302 if (bb->global_live_at_start)
4304 for (regno = 0; regno < max_regno; regno++)
4305 if (REGNO_REG_SET_P (bb->global_live_at_start, regno))
4306 fprintf (file, " %d", regno);
4308 else
4309 fprintf (file, " n/a");
4311 fprintf (file, "\nRegisters live at end:");
4312 if (bb->global_live_at_end)
4314 for (regno = 0; regno < max_regno; regno++)
4315 if (REGNO_REG_SET_P (bb->global_live_at_end, regno))
4316 fprintf (file, " %d", regno);
4318 else
4319 fprintf (file, " n/a");
4321 putc('\n', file);
4324 putc('\n', file);
4327 static void
4328 dump_edge_info (file, e, do_succ)
4329 FILE *file;
4330 edge e;
4331 int do_succ;
4333 basic_block side = (do_succ ? e->dest : e->src);
4335 if (side == ENTRY_BLOCK_PTR)
4336 fputs (" ENTRY", file);
4337 else if (side == EXIT_BLOCK_PTR)
4338 fputs (" EXIT", file);
4339 else
4340 fprintf (file, " %d", side->index);
4342 if (e->flags)
4344 static const char * const bitnames[] = {
4345 "fallthru", "crit", "ab", "abcall", "eh", "fake"
4347 int comma = 0;
4348 int i, flags = e->flags;
4350 fputc (' ', file);
4351 fputc ('(', file);
4352 for (i = 0; flags; i++)
4353 if (flags & (1 << i))
4355 flags &= ~(1 << i);
4357 if (comma)
4358 fputc (',', file);
4359 if (i < (int)(sizeof (bitnames) / sizeof (*bitnames)))
4360 fputs (bitnames[i], file);
4361 else
4362 fprintf (file, "%d", i);
4363 comma = 1;
4365 fputc (')', file);
4370 /* Like print_rtl, but also print out live information for the start of each
4371 basic block. */
4373 void
4374 print_rtl_with_bb (outf, rtx_first)
4375 FILE *outf;
4376 rtx rtx_first;
4378 register rtx tmp_rtx;
4380 if (rtx_first == 0)
4381 fprintf (outf, "(nil)\n");
4382 else
4384 int i;
4385 enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
4386 int max_uid = get_max_uid ();
4387 basic_block *start = (basic_block *)
4388 alloca (max_uid * sizeof (basic_block));
4389 basic_block *end = (basic_block *)
4390 alloca (max_uid * sizeof (basic_block));
4391 enum bb_state *in_bb_p = (enum bb_state *)
4392 alloca (max_uid * sizeof (enum bb_state));
4394 memset (start, 0, max_uid * sizeof (basic_block));
4395 memset (end, 0, max_uid * sizeof (basic_block));
4396 memset (in_bb_p, 0, max_uid * sizeof (enum bb_state));
4398 for (i = n_basic_blocks - 1; i >= 0; i--)
4400 basic_block bb = BASIC_BLOCK (i);
4401 rtx x;
4403 start[INSN_UID (bb->head)] = bb;
4404 end[INSN_UID (bb->end)] = bb;
4405 for (x = bb->head; x != NULL_RTX; x = NEXT_INSN (x))
4407 enum bb_state state = IN_MULTIPLE_BB;
4408 if (in_bb_p[INSN_UID(x)] == NOT_IN_BB)
4409 state = IN_ONE_BB;
4410 in_bb_p[INSN_UID(x)] = state;
4412 if (x == bb->end)
4413 break;
4417 for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx))
4419 int did_output;
4420 basic_block bb;
4422 if ((bb = start[INSN_UID (tmp_rtx)]) != NULL)
4424 fprintf (outf, ";; Start of basic block %d, registers live:",
4425 bb->index);
4427 EXECUTE_IF_SET_IN_REG_SET (bb->global_live_at_start, 0, i,
4429 fprintf (outf, " %d", i);
4430 if (i < FIRST_PSEUDO_REGISTER)
4431 fprintf (outf, " [%s]",
4432 reg_names[i]);
4434 putc ('\n', outf);
4437 if (in_bb_p[INSN_UID(tmp_rtx)] == NOT_IN_BB
4438 && GET_CODE (tmp_rtx) != NOTE
4439 && GET_CODE (tmp_rtx) != BARRIER
4440 && ! obey_regdecls)
4441 fprintf (outf, ";; Insn is not within a basic block\n");
4442 else if (in_bb_p[INSN_UID(tmp_rtx)] == IN_MULTIPLE_BB)
4443 fprintf (outf, ";; Insn is in multiple basic blocks\n");
4445 did_output = print_rtl_single (outf, tmp_rtx);
4447 if ((bb = end[INSN_UID (tmp_rtx)]) != NULL)
4448 fprintf (outf, ";; End of basic block %d\n", bb->index);
4450 if (did_output)
4451 putc ('\n', outf);
4455 if (current_function_epilogue_delay_list != 0)
4457 fprintf (outf, "\n;; Insns in epilogue delay list:\n\n");
4458 for (tmp_rtx = current_function_epilogue_delay_list; tmp_rtx != 0;
4459 tmp_rtx = XEXP (tmp_rtx, 1))
4460 print_rtl_single (outf, XEXP (tmp_rtx, 0));
4465 /* Integer list support. */
4467 /* Allocate a node from list *HEAD_PTR. */
4469 static int_list_ptr
4470 alloc_int_list_node (head_ptr)
4471 int_list_block **head_ptr;
4473 struct int_list_block *first_blk = *head_ptr;
4475 if (first_blk == NULL || first_blk->nodes_left <= 0)
4477 first_blk = (struct int_list_block *) xmalloc (sizeof (struct int_list_block));
4478 first_blk->nodes_left = INT_LIST_NODES_IN_BLK;
4479 first_blk->next = *head_ptr;
4480 *head_ptr = first_blk;
4483 first_blk->nodes_left--;
4484 return &first_blk->nodes[first_blk->nodes_left];
4487 /* Pointer to head of predecessor/successor block list. */
4488 static int_list_block *pred_int_list_blocks;
4490 /* Add a new node to integer list LIST with value VAL.
4491 LIST is a pointer to a list object to allow for different implementations.
4492 If *LIST is initially NULL, the list is empty.
4493 The caller must not care whether the element is added to the front or
4494 to the end of the list (to allow for different implementations). */
4496 static int_list_ptr
4497 add_int_list_node (blk_list, list, val)
4498 int_list_block **blk_list;
4499 int_list **list;
4500 int val;
4502 int_list_ptr p = alloc_int_list_node (blk_list);
4504 p->val = val;
4505 p->next = *list;
4506 *list = p;
4507 return p;
4510 /* Free the blocks of lists at BLK_LIST. */
4512 void
4513 free_int_list (blk_list)
4514 int_list_block **blk_list;
4516 int_list_block *p, *next;
4518 for (p = *blk_list; p != NULL; p = next)
4520 next = p->next;
4521 free (p);
4524 /* Mark list as empty for the next function we compile. */
4525 *blk_list = NULL;
4528 /* Predecessor/successor computation. */
4530 /* Mark PRED_BB a precessor of SUCC_BB,
4531 and conversely SUCC_BB a successor of PRED_BB. */
4533 static void
4534 add_pred_succ (pred_bb, succ_bb, s_preds, s_succs, num_preds, num_succs)
4535 int pred_bb;
4536 int succ_bb;
4537 int_list_ptr *s_preds;
4538 int_list_ptr *s_succs;
4539 int *num_preds;
4540 int *num_succs;
4542 if (succ_bb != EXIT_BLOCK)
4544 add_int_list_node (&pred_int_list_blocks, &s_preds[succ_bb], pred_bb);
4545 num_preds[succ_bb]++;
4547 if (pred_bb != ENTRY_BLOCK)
4549 add_int_list_node (&pred_int_list_blocks, &s_succs[pred_bb], succ_bb);
4550 num_succs[pred_bb]++;
4554 /* Convert edge lists into pred/succ lists for backward compatibility. */
4556 void
4557 compute_preds_succs (s_preds, s_succs, num_preds, num_succs)
4558 int_list_ptr *s_preds;
4559 int_list_ptr *s_succs;
4560 int *num_preds;
4561 int *num_succs;
4563 int i, n = n_basic_blocks;
4564 edge e;
4566 memset (s_preds, 0, n_basic_blocks * sizeof (int_list_ptr));
4567 memset (s_succs, 0, n_basic_blocks * sizeof (int_list_ptr));
4568 memset (num_preds, 0, n_basic_blocks * sizeof (int));
4569 memset (num_succs, 0, n_basic_blocks * sizeof (int));
4571 for (i = 0; i < n; ++i)
4573 basic_block bb = BASIC_BLOCK (i);
4575 for (e = bb->succ; e ; e = e->succ_next)
4576 add_pred_succ (i, e->dest->index, s_preds, s_succs,
4577 num_preds, num_succs);
4580 for (e = ENTRY_BLOCK_PTR->succ; e ; e = e->succ_next)
4581 add_pred_succ (ENTRY_BLOCK, e->dest->index, s_preds, s_succs,
4582 num_preds, num_succs);
4585 void
4586 dump_bb_data (file, preds, succs, live_info)
4587 FILE *file;
4588 int_list_ptr *preds;
4589 int_list_ptr *succs;
4590 int live_info;
4592 int bb;
4593 int_list_ptr p;
4595 fprintf (file, "BB data\n\n");
4596 for (bb = 0; bb < n_basic_blocks; bb++)
4598 fprintf (file, "BB %d, start %d, end %d\n", bb,
4599 INSN_UID (BLOCK_HEAD (bb)), INSN_UID (BLOCK_END (bb)));
4600 fprintf (file, " preds:");
4601 for (p = preds[bb]; p != NULL; p = p->next)
4603 int pred_bb = INT_LIST_VAL (p);
4604 if (pred_bb == ENTRY_BLOCK)
4605 fprintf (file, " entry");
4606 else
4607 fprintf (file, " %d", pred_bb);
4609 fprintf (file, "\n");
4610 fprintf (file, " succs:");
4611 for (p = succs[bb]; p != NULL; p = p->next)
4613 int succ_bb = INT_LIST_VAL (p);
4614 if (succ_bb == EXIT_BLOCK)
4615 fprintf (file, " exit");
4616 else
4617 fprintf (file, " %d", succ_bb);
4619 if (live_info)
4621 int regno;
4622 fprintf (file, "\nRegisters live at start:");
4623 for (regno = 0; regno < max_regno; regno++)
4624 if (REGNO_REG_SET_P (BASIC_BLOCK (bb)->global_live_at_start, regno))
4625 fprintf (file, " %d", regno);
4626 fprintf (file, "\n");
4628 fprintf (file, "\n");
4630 fprintf (file, "\n");
4633 /* Free basic block data storage. */
4635 void
4636 free_bb_mem ()
4638 free_int_list (&pred_int_list_blocks);
4641 /* Compute dominator relationships. */
4642 void
4643 compute_dominators (dominators, post_dominators, s_preds, s_succs)
4644 sbitmap *dominators;
4645 sbitmap *post_dominators;
4646 int_list_ptr *s_preds;
4647 int_list_ptr *s_succs;
4649 int bb, changed, passes;
4650 sbitmap *temp_bitmap;
4652 temp_bitmap = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
4653 sbitmap_vector_ones (dominators, n_basic_blocks);
4654 sbitmap_vector_ones (post_dominators, n_basic_blocks);
4655 sbitmap_vector_zero (temp_bitmap, n_basic_blocks);
4657 sbitmap_zero (dominators[0]);
4658 SET_BIT (dominators[0], 0);
4660 sbitmap_zero (post_dominators[n_basic_blocks - 1]);
4661 SET_BIT (post_dominators[n_basic_blocks - 1], 0);
4663 passes = 0;
4664 changed = 1;
4665 while (changed)
4667 changed = 0;
4668 for (bb = 1; bb < n_basic_blocks; bb++)
4670 sbitmap_intersect_of_predecessors (temp_bitmap[bb], dominators,
4671 bb, s_preds);
4672 SET_BIT (temp_bitmap[bb], bb);
4673 changed |= sbitmap_a_and_b (dominators[bb],
4674 dominators[bb],
4675 temp_bitmap[bb]);
4676 sbitmap_intersect_of_successors (temp_bitmap[bb], post_dominators,
4677 bb, s_succs);
4678 SET_BIT (temp_bitmap[bb], bb);
4679 changed |= sbitmap_a_and_b (post_dominators[bb],
4680 post_dominators[bb],
4681 temp_bitmap[bb]);
4683 passes++;
4686 free (temp_bitmap);
4689 /* Compute dominator relationships using new flow graph structures. */
4690 void
4691 compute_flow_dominators (dominators, post_dominators)
4692 sbitmap *dominators;
4693 sbitmap *post_dominators;
4695 int bb, changed, passes;
4696 sbitmap *temp_bitmap;
4698 temp_bitmap = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
4699 sbitmap_vector_ones (dominators, n_basic_blocks);
4700 sbitmap_vector_ones (post_dominators, n_basic_blocks);
4701 sbitmap_vector_zero (temp_bitmap, n_basic_blocks);
4703 sbitmap_zero (dominators[0]);
4704 SET_BIT (dominators[0], 0);
4706 sbitmap_zero (post_dominators[n_basic_blocks - 1]);
4707 SET_BIT (post_dominators[n_basic_blocks - 1], 0);
4709 passes = 0;
4710 changed = 1;
4711 while (changed)
4713 changed = 0;
4714 for (bb = 1; bb < n_basic_blocks; bb++)
4716 sbitmap_intersection_of_preds (temp_bitmap[bb], dominators, bb);
4717 SET_BIT (temp_bitmap[bb], bb);
4718 changed |= sbitmap_a_and_b (dominators[bb],
4719 dominators[bb],
4720 temp_bitmap[bb]);
4721 sbitmap_intersection_of_succs (temp_bitmap[bb], post_dominators, bb);
4722 SET_BIT (temp_bitmap[bb], bb);
4723 changed |= sbitmap_a_and_b (post_dominators[bb],
4724 post_dominators[bb],
4725 temp_bitmap[bb]);
4727 passes++;
4730 free (temp_bitmap);
4733 /* Given DOMINATORS, compute the immediate dominators into IDOM. */
4735 void
4736 compute_immediate_dominators (idom, dominators)
4737 int *idom;
4738 sbitmap *dominators;
4740 sbitmap *tmp;
4741 int b;
4743 tmp = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
4745 /* Begin with tmp(n) = dom(n) - { n }. */
4746 for (b = n_basic_blocks; --b >= 0; )
4748 sbitmap_copy (tmp[b], dominators[b]);
4749 RESET_BIT (tmp[b], b);
4752 /* Subtract out all of our dominator's dominators. */
4753 for (b = n_basic_blocks; --b >= 0; )
4755 sbitmap tmp_b = tmp[b];
4756 int s;
4758 for (s = n_basic_blocks; --s >= 0; )
4759 if (TEST_BIT (tmp_b, s))
4760 sbitmap_difference (tmp_b, tmp_b, tmp[s]);
4763 /* Find the one bit set in the bitmap and put it in the output array. */
4764 for (b = n_basic_blocks; --b >= 0; )
4766 int t;
4767 EXECUTE_IF_SET_IN_SBITMAP (tmp[b], 0, t, { idom[b] = t; });
4770 sbitmap_vector_free (tmp);
4773 /* Count for a single SET rtx, X. */
4775 static void
4776 count_reg_sets_1 (x)
4777 rtx x;
4779 register int regno;
4780 register rtx reg = SET_DEST (x);
4782 /* Find the register that's set/clobbered. */
4783 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
4784 || GET_CODE (reg) == SIGN_EXTRACT
4785 || GET_CODE (reg) == STRICT_LOW_PART)
4786 reg = XEXP (reg, 0);
4788 if (GET_CODE (reg) == PARALLEL
4789 && GET_MODE (reg) == BLKmode)
4791 register int i;
4792 for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
4793 count_reg_sets_1 (XVECEXP (reg, 0, i));
4794 return;
4797 if (GET_CODE (reg) == REG)
4799 regno = REGNO (reg);
4800 if (regno >= FIRST_PSEUDO_REGISTER)
4802 /* Count (weighted) references, stores, etc. This counts a
4803 register twice if it is modified, but that is correct. */
4804 REG_N_SETS (regno)++;
4806 REG_N_REFS (regno) += loop_depth;
4811 /* Increment REG_N_SETS for each SET or CLOBBER found in X; also increment
4812 REG_N_REFS by the current loop depth for each SET or CLOBBER found. */
4814 static void
4815 count_reg_sets (x)
4816 rtx x;
4818 register RTX_CODE code = GET_CODE (x);
4820 if (code == SET || code == CLOBBER)
4821 count_reg_sets_1 (x);
4822 else if (code == PARALLEL)
4824 register int i;
4825 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
4827 code = GET_CODE (XVECEXP (x, 0, i));
4828 if (code == SET || code == CLOBBER)
4829 count_reg_sets_1 (XVECEXP (x, 0, i));
4834 /* Increment REG_N_REFS by the current loop depth each register reference
4835 found in X. */
4837 static void
4838 count_reg_references (x)
4839 rtx x;
4841 register RTX_CODE code;
4843 retry:
4844 code = GET_CODE (x);
4845 switch (code)
4847 case LABEL_REF:
4848 case SYMBOL_REF:
4849 case CONST_INT:
4850 case CONST:
4851 case CONST_DOUBLE:
4852 case PC:
4853 case ADDR_VEC:
4854 case ADDR_DIFF_VEC:
4855 case ASM_INPUT:
4856 return;
4858 #ifdef HAVE_cc0
4859 case CC0:
4860 return;
4861 #endif
4863 case CLOBBER:
4864 /* If we are clobbering a MEM, mark any registers inside the address
4865 as being used. */
4866 if (GET_CODE (XEXP (x, 0)) == MEM)
4867 count_reg_references (XEXP (XEXP (x, 0), 0));
4868 return;
4870 case SUBREG:
4871 /* While we're here, optimize this case. */
4872 x = SUBREG_REG (x);
4874 /* In case the SUBREG is not of a register, don't optimize */
4875 if (GET_CODE (x) != REG)
4877 count_reg_references (x);
4878 return;
4881 /* ... fall through ... */
4883 case REG:
4884 if (REGNO (x) >= FIRST_PSEUDO_REGISTER)
4885 REG_N_REFS (REGNO (x)) += loop_depth;
4886 return;
4888 case SET:
4890 register rtx testreg = SET_DEST (x);
4891 int mark_dest = 0;
4893 /* If storing into MEM, don't show it as being used. But do
4894 show the address as being used. */
4895 if (GET_CODE (testreg) == MEM)
4897 count_reg_references (XEXP (testreg, 0));
4898 count_reg_references (SET_SRC (x));
4899 return;
4902 /* Storing in STRICT_LOW_PART is like storing in a reg
4903 in that this SET might be dead, so ignore it in TESTREG.
4904 but in some other ways it is like using the reg.
4906 Storing in a SUBREG or a bit field is like storing the entire
4907 register in that if the register's value is not used
4908 then this SET is not needed. */
4909 while (GET_CODE (testreg) == STRICT_LOW_PART
4910 || GET_CODE (testreg) == ZERO_EXTRACT
4911 || GET_CODE (testreg) == SIGN_EXTRACT
4912 || GET_CODE (testreg) == SUBREG)
4914 /* Modifying a single register in an alternate mode
4915 does not use any of the old value. But these other
4916 ways of storing in a register do use the old value. */
4917 if (GET_CODE (testreg) == SUBREG
4918 && !(REG_SIZE (SUBREG_REG (testreg)) > REG_SIZE (testreg)))
4920 else
4921 mark_dest = 1;
4923 testreg = XEXP (testreg, 0);
4926 /* If this is a store into a register,
4927 recursively scan the value being stored. */
4929 if ((GET_CODE (testreg) == PARALLEL
4930 && GET_MODE (testreg) == BLKmode)
4931 || GET_CODE (testreg) == REG)
4933 count_reg_references (SET_SRC (x));
4934 if (mark_dest)
4935 count_reg_references (SET_DEST (x));
4936 return;
4939 break;
4941 default:
4942 break;
4945 /* Recursively scan the operands of this expression. */
4948 register const char *fmt = GET_RTX_FORMAT (code);
4949 register int i;
4951 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4953 if (fmt[i] == 'e')
4955 /* Tail recursive case: save a function call level. */
4956 if (i == 0)
4958 x = XEXP (x, 0);
4959 goto retry;
4961 count_reg_references (XEXP (x, i));
4963 else if (fmt[i] == 'E')
4965 register int j;
4966 for (j = 0; j < XVECLEN (x, i); j++)
4967 count_reg_references (XVECEXP (x, i, j));
4973 /* Recompute register set/reference counts immediately prior to register
4974 allocation.
4976 This avoids problems with set/reference counts changing to/from values
4977 which have special meanings to the register allocators.
4979 Additionally, the reference counts are the primary component used by the
4980 register allocators to prioritize pseudos for allocation to hard regs.
4981 More accurate reference counts generally lead to better register allocation.
4983 F is the first insn to be scanned.
4984 LOOP_STEP denotes how much loop_depth should be incremented per
4985 loop nesting level in order to increase the ref count more for references
4986 in a loop.
4988 It might be worthwhile to update REG_LIVE_LENGTH, REG_BASIC_BLOCK and
4989 possibly other information which is used by the register allocators. */
4991 void
4992 recompute_reg_usage (f, loop_step)
4993 rtx f;
4994 int loop_step;
4996 rtx insn;
4997 int i, max_reg;
4999 /* Clear out the old data. */
5000 max_reg = max_reg_num ();
5001 for (i = FIRST_PSEUDO_REGISTER; i < max_reg; i++)
5003 REG_N_SETS (i) = 0;
5004 REG_N_REFS (i) = 0;
5007 /* Scan each insn in the chain and count how many times each register is
5008 set/used. */
5009 loop_depth = 1;
5010 for (insn = f; insn; insn = NEXT_INSN (insn))
5012 /* Keep track of loop depth. */
5013 if (GET_CODE (insn) == NOTE)
5015 /* Look for loop boundaries. */
5016 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
5017 loop_depth -= loop_step;
5018 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
5019 loop_depth += loop_step;
5021 /* If we have LOOP_DEPTH == 0, there has been a bookkeeping error.
5022 Abort now rather than setting register status incorrectly. */
5023 if (loop_depth == 0)
5024 abort ();
5026 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
5028 rtx links;
5030 /* This call will increment REG_N_SETS for each SET or CLOBBER
5031 of a register in INSN. It will also increment REG_N_REFS
5032 by the loop depth for each set of a register in INSN. */
5033 count_reg_sets (PATTERN (insn));
5035 /* count_reg_sets does not detect autoincrement address modes, so
5036 detect them here by looking at the notes attached to INSN. */
5037 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
5039 if (REG_NOTE_KIND (links) == REG_INC)
5040 /* Count (weighted) references, stores, etc. This counts a
5041 register twice if it is modified, but that is correct. */
5042 REG_N_SETS (REGNO (XEXP (links, 0)))++;
5045 /* This call will increment REG_N_REFS by the current loop depth for
5046 each reference to a register in INSN. */
5047 count_reg_references (PATTERN (insn));
5049 /* count_reg_references will not include counts for arguments to
5050 function calls, so detect them here by examining the
5051 CALL_INSN_FUNCTION_USAGE data. */
5052 if (GET_CODE (insn) == CALL_INSN)
5054 rtx note;
5056 for (note = CALL_INSN_FUNCTION_USAGE (insn);
5057 note;
5058 note = XEXP (note, 1))
5059 if (GET_CODE (XEXP (note, 0)) == USE)
5060 count_reg_references (XEXP (XEXP (note, 0), 0));
5066 /* Record INSN's block as BB. */
5068 void
5069 set_block_for_insn (insn, bb)
5070 rtx insn;
5071 basic_block bb;
5073 size_t uid = INSN_UID (insn);
5074 if (uid >= basic_block_for_insn->num_elements)
5076 int new_size;
5078 /* Add one-eighth the size so we don't keep calling xrealloc. */
5079 new_size = uid + (uid + 7) / 8;
5081 VARRAY_GROW (basic_block_for_insn, new_size);
5083 VARRAY_BB (basic_block_for_insn, uid) = bb;
5086 /* Record INSN's block number as BB. */
5087 /* ??? This has got to go. */
5089 void
5090 set_block_num (insn, bb)
5091 rtx insn;
5092 int bb;
5094 set_block_for_insn (insn, BASIC_BLOCK (bb));
5097 /* Unlink a chain of insns between START and FINISH inclusive, leaving notes
5098 that must be paired, and return the new chain. */
5101 unlink_insn_chain (start, finish)
5102 rtx start, finish;
5104 rtx insert_point = PREV_INSN (start);
5105 rtx chain = NULL_RTX, curr;
5107 /* Unchain the insns one by one. It would be quicker to delete all
5108 of these with a single unchaining, rather than one at a time, but
5109 we need to keep the NOTE's. */
5111 while (1)
5113 rtx next = NEXT_INSN (start);
5115 remove_insn (start);
5117 /* ??? Despite the fact that we're patching out the insn, it's
5118 still referenced in LOG_LINKS. Rather than try and track
5119 them all down and remove them, just mark the insn deleted. */
5120 INSN_DELETED_P (start) = 1;
5122 if (GET_CODE (start) == NOTE && ! can_delete_note_p (start))
5124 add_insn_after (start, insert_point);
5125 insert_point = start;
5127 else
5129 if (chain != NULL)
5131 NEXT_INSN (curr) = start;
5132 PREV_INSN (start) = curr;
5133 curr = start;
5135 else
5137 chain = start;
5138 curr = start;
5139 PREV_INSN (chain) = NULL_RTX;
5143 if (start == finish)
5144 break;
5145 start = next;
5148 if (chain != NULL_RTX)
5149 NEXT_INSN (curr) = NULL_RTX;
5151 return chain;
5154 /* Subroutine of update_life_info. Determines whether multiple
5155 REG_NOTEs need to be distributed for the hard register mentioned in
5156 NOTE. This can happen if a reference to a hard register in the
5157 original insns was split into several smaller hard register
5158 references in the new insns. */
5160 static void
5161 split_hard_reg_notes (curr_insn, note, first, last)
5162 rtx curr_insn, note, first, last;
5164 rtx reg, temp, link;
5165 rtx insn;
5166 int n_regs, i, new_reg;
5168 reg = XEXP (note, 0);
5170 if (REG_NOTE_KIND (note) != REG_DEAD
5171 || GET_CODE (reg) != REG
5172 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
5173 || HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg)) == 1)
5175 XEXP (note, 1) = REG_NOTES (curr_insn);
5176 REG_NOTES (curr_insn) = note;
5177 return;
5180 n_regs = HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg));
5182 for (i = 0; i < n_regs; i++)
5184 new_reg = REGNO (reg) + i;
5186 /* Check for references to new_reg in the split insns. */
5187 for (insn = last; ; insn = PREV_INSN (insn))
5189 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
5190 && (temp = regno_use_in (new_reg, PATTERN (insn))))
5192 /* Create a new reg dead note here. */
5193 link = rtx_alloc (EXPR_LIST);
5194 PUT_REG_NOTE_KIND (link, REG_DEAD);
5195 XEXP (link, 0) = temp;
5196 XEXP (link, 1) = REG_NOTES (insn);
5197 REG_NOTES (insn) = link;
5199 /* If killed multiple registers here, then add in the excess. */
5200 i += HARD_REGNO_NREGS (REGNO (temp), GET_MODE (temp)) - 1;
5202 break;
5204 /* It isn't mentioned anywhere, so no new reg note is needed for
5205 this register. */
5206 if (insn == first)
5207 break;
5212 /* SET_INSN kills REG; add a REG_DEAD note mentioning REG to the last
5213 use of REG in the insns after SET_INSN and before or including
5214 LAST, if necessary.
5216 A non-zero value is returned if we added a REG_DEAD note, or if we
5217 determined that a REG_DEAD note because of this particular SET
5218 wasn't necessary. */
5220 static int
5221 maybe_add_dead_note (reg, set_insn, last)
5222 rtx reg, set_insn, last;
5224 rtx insn;
5226 for (insn = last; insn != set_insn; insn = PREV_INSN (insn))
5228 rtx set;
5230 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
5231 && reg_overlap_mentioned_p (reg, PATTERN (insn))
5232 && (set = single_set (insn)))
5234 rtx insn_dest = SET_DEST (set);
5236 while (GET_CODE (insn_dest) == ZERO_EXTRACT
5237 || GET_CODE (insn_dest) == SUBREG
5238 || GET_CODE (insn_dest) == STRICT_LOW_PART
5239 || GET_CODE (insn_dest) == SIGN_EXTRACT)
5240 insn_dest = XEXP (insn_dest, 0);
5242 if (! rtx_equal_p (insn_dest, reg))
5244 /* Use the same scheme as combine.c, don't put both REG_DEAD
5245 and REG_UNUSED notes on the same insn. */
5246 if (! find_regno_note (insn, REG_UNUSED, REGNO (reg))
5247 && ! find_regno_note (insn, REG_DEAD, REGNO (reg)))
5249 rtx note = rtx_alloc (EXPR_LIST);
5250 PUT_REG_NOTE_KIND (note, REG_DEAD);
5251 XEXP (note, 0) = reg;
5252 XEXP (note, 1) = REG_NOTES (insn);
5253 REG_NOTES (insn) = note;
5255 return 1;
5257 else if (reg_overlap_mentioned_p (reg, SET_SRC (set)))
5259 /* We found an instruction that both uses the register and
5260 sets it, so no new REG_NOTE is needed for the previous
5261 set. */
5262 return 0;
5266 return 0;
5269 static int
5270 maybe_add_dead_note_use (insn, dest)
5271 rtx insn, dest;
5273 rtx set;
5275 /* We need to add a REG_DEAD note to the last place DEST is
5276 referenced. */
5278 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
5279 && reg_mentioned_p (dest, PATTERN (insn))
5280 && (set = single_set (insn)))
5282 rtx insn_dest = SET_DEST (set);
5284 while (GET_CODE (insn_dest) == ZERO_EXTRACT
5285 || GET_CODE (insn_dest) == SUBREG
5286 || GET_CODE (insn_dest) == STRICT_LOW_PART
5287 || GET_CODE (insn_dest) == SIGN_EXTRACT)
5288 insn_dest = XEXP (insn_dest, 0);
5290 if (! rtx_equal_p (insn_dest, dest))
5292 /* Use the same scheme as combine.c, don't put both REG_DEAD
5293 and REG_UNUSED notes on the same insn. */
5294 if (! find_regno_note (insn, REG_UNUSED, REGNO (dest))
5295 && ! find_regno_note (insn, REG_DEAD, REGNO (dest)))
5297 rtx note = rtx_alloc (EXPR_LIST);
5298 PUT_REG_NOTE_KIND (note, REG_DEAD);
5299 XEXP (note, 0) = dest;
5300 XEXP (note, 1) = REG_NOTES (insn);
5301 REG_NOTES (insn) = note;
5303 return 1;
5306 return 0;
5309 /* Find the first insn in the set of insns from FIRST to LAST inclusive
5310 that contains the note NOTE. */
5312 find_insn_with_note (note, first, last)
5313 rtx note, first, last;
5315 rtx insn;
5317 for (insn = first; insn != NULL_RTX; insn = NEXT_INSN (insn))
5319 rtx temp = find_reg_note (insn, REG_NOTE_KIND (note), XEXP (note, 0));
5320 if (temp == note)
5322 return insn;
5324 if (insn == last)
5326 break;
5329 return NULL_RTX;
5332 /* Subroutine of update_life_info. Determines whether a SET or
5333 CLOBBER in an insn created by splitting needs a REG_DEAD or
5334 REG_UNUSED note added. */
5336 static void
5337 new_insn_dead_notes (pat, insn, first, last, orig_first_insn, orig_last_insn)
5338 rtx pat, insn, first, last, orig_first_insn, orig_last_insn;
5340 rtx dest, tem;
5342 if (GET_CODE (pat) != CLOBBER && GET_CODE (pat) != SET)
5343 abort ();
5345 dest = XEXP (pat, 0);
5347 while (GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SUBREG
5348 || GET_CODE (dest) == STRICT_LOW_PART
5349 || GET_CODE (dest) == SIGN_EXTRACT)
5350 dest = XEXP (dest, 0);
5352 if (GET_CODE (dest) == REG)
5354 /* If the original insns already used this register, we may not
5355 add new notes for it. One example for a replacement that
5356 needs this test is when a multi-word memory access with
5357 register-indirect addressing is changed into multiple memory
5358 accesses with auto-increment and one adjusting add
5359 instruction for the address register.
5361 However, there is a problem with this code. We're assuming
5362 that any registers that are set in the new insns are either
5363 set/referenced in the old insns (and thus "inherit" the
5364 liveness of the old insns), or are registers that are dead
5365 before we enter this part of the stream (and thus should be
5366 dead when we leave).
5368 To do this absolutely correctly, we must determine the actual
5369 liveness of the registers before we go randomly adding
5370 REG_DEAD notes. This can probably be accurately done by
5371 calling mark_referenced_resources() on the old stream before
5372 replacing the old insns. */
5374 for (tem = orig_first_insn; tem != NULL_RTX; tem = NEXT_INSN (tem))
5376 if (GET_RTX_CLASS (GET_CODE (tem)) == 'i'
5377 && reg_referenced_p (dest, PATTERN (tem)))
5378 return;
5379 if (tem == orig_last_insn)
5380 break;
5383 /* So it's a new register, presumably only used within this
5384 group of insns. Find the last insn in the set of new insns
5385 that DEST is referenced in, and add a dead note to it. */
5386 if (! maybe_add_dead_note (dest, insn, last))
5388 /* If this is a set, it must die somewhere, unless it is the
5389 dest of the original insn, and thus is live after the
5390 original insn. Abort if it isn't supposed to be live after
5391 the original insn.
5393 If this is a clobber, then just add a REG_UNUSED note. */
5394 if (GET_CODE (pat) == CLOBBER)
5396 rtx note = rtx_alloc (EXPR_LIST);
5397 PUT_REG_NOTE_KIND (note, REG_UNUSED);
5398 XEXP (note, 0) = dest;
5399 XEXP (note, 1) = REG_NOTES (insn);
5400 REG_NOTES (insn) = note;
5401 return;
5403 else
5405 rtx curr;
5406 int got_set = 0;
5408 for (curr = orig_first_insn; curr; curr = NEXT_INSN (curr))
5410 got_set = sets_reg_or_subreg (curr, dest);
5411 if (got_set)
5412 break;
5413 if (curr == orig_last_insn)
5414 break;
5417 /* In case reg was not used later, it is dead store.
5418 add REG_UNUSED note. */
5419 if (! got_set)
5421 rtx note = rtx_alloc (EXPR_LIST);
5422 PUT_REG_NOTE_KIND (note, REG_UNUSED);
5423 XEXP (note, 0) = dest;
5424 XEXP (note, 1) = REG_NOTES (insn);
5425 REG_NOTES (insn) = note;
5426 return;
5431 if (insn != first)
5433 rtx set = single_set (insn);
5435 /* If this is a set, scan backwards for a previous
5436 reference, and attach a REG_DEAD note to it. But we don't
5437 want to do it if the insn is both using and setting the
5438 register.
5440 Global registers are always live. */
5441 if (set && ! reg_overlap_mentioned_p (dest, SET_SRC (pat))
5442 && (REGNO (dest) >= FIRST_PSEUDO_REGISTER
5443 || ! global_regs[REGNO (dest)]))
5445 for (tem = PREV_INSN (insn);
5446 tem != NULL_RTX; tem = PREV_INSN (tem))
5448 if (maybe_add_dead_note_use (tem, dest))
5449 break;
5450 if (tem == first)
5451 break;
5458 /* Subroutine of update_life_info. Update the value of reg_n_sets for all
5459 registers modified by X. INC is -1 if the containing insn is being deleted,
5460 and is 1 if the containing insn is a newly generated insn. */
5462 static void
5463 update_n_sets (x, inc)
5464 rtx x;
5465 int inc;
5467 rtx dest = SET_DEST (x);
5469 while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SUBREG
5470 || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SIGN_EXTRACT)
5471 dest = SUBREG_REG (dest);
5473 if (GET_CODE (dest) == REG)
5475 int regno = REGNO (dest);
5477 if (regno < FIRST_PSEUDO_REGISTER)
5479 register int i;
5480 int endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (dest));
5482 for (i = regno; i < endregno; i++)
5483 REG_N_SETS (i) += inc;
5485 else
5486 REG_N_SETS (regno) += inc;
5490 /* Scan INSN for a SET that sets REG. If it sets REG via a SUBREG,
5491 then return 2. If it sets REG directly, return 1. Otherwise, return
5492 0. */
5494 static int sets_reg_or_subreg_ret;
5495 static rtx sets_reg_or_subreg_rtx;
5497 static void
5498 sets_reg_or_subreg_1 (x, set)
5499 rtx x, set;
5501 if (rtx_equal_p (x, sets_reg_or_subreg_rtx))
5503 if (x == XEXP (set, 0))
5504 sets_reg_or_subreg_ret = 1;
5505 else if (GET_CODE (XEXP (set, 0)) == SUBREG)
5506 sets_reg_or_subreg_ret = 2;
5510 static int
5511 sets_reg_or_subreg (insn, reg)
5512 rtx insn;
5513 rtx reg;
5515 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
5516 return 0;
5518 sets_reg_or_subreg_ret = 0;
5519 sets_reg_or_subreg_rtx = reg;
5520 note_stores (PATTERN (insn), sets_reg_or_subreg_1);
5521 return sets_reg_or_subreg_ret;
5524 /* If a replaced SET_INSN (which is part of the insns between
5525 OLD_FIRST_INSN and OLD_LAST_INSN inclusive) is modifying a multiple
5526 register target, and the original dest is now set in the new insns
5527 (between FIRST_INSN and LAST_INSN inclusive) by one or more subreg
5528 sets, then the new insns no longer kill the destination of the
5529 original insn.
5531 We may also be directly using the register in the new insns before
5532 setting it.
5534 In either case, if there exists an instruction in the same basic
5535 block before the replaced insns which uses the original dest (and
5536 contains a corresponding REG_DEAD note), then we must remove this
5537 REG_DEAD note.
5539 SET_INSN is the insn that contains the SET; it may be a PARALLEL
5540 containing the SET insn.
5542 SET is the actual SET insn proper. */
5544 static void
5545 maybe_remove_dead_notes (set_insn, set, first_insn, last_insn,
5546 old_first_insn, old_last_insn)
5547 rtx set_insn, set;
5548 rtx first_insn, last_insn;
5549 rtx old_first_insn, old_last_insn;
5551 rtx insn;
5552 rtx stop_insn = NEXT_INSN (last_insn);
5553 int set_type = 0;
5554 rtx set_dest;
5555 rtx set_pattern;
5557 if (GET_RTX_CLASS (GET_CODE (set)) != 'i')
5558 return;
5560 set_pattern = PATTERN (set);
5562 if (GET_CODE (set_pattern) == PARALLEL)
5564 int i;
5566 for (i = 0; i < XVECLEN (set_pattern, 0); i++)
5568 maybe_remove_dead_notes (set_insn, XVECEXP (set_pattern, 0, i),
5569 first_insn, last_insn,
5570 old_first_insn, old_last_insn);
5572 return;
5575 if (GET_CODE (set_pattern) != SET)
5577 return;
5580 set_dest = SET_DEST (set_pattern);
5582 if (GET_CODE (set_dest) != REG)
5584 return;
5587 /* We have a set of a REG. First we need to determine if this set is
5588 both using and setting the register. (FIXME: if this is in a
5589 PARALLEL, we will have to check the other exprs as well.) */
5590 if (reg_overlap_mentioned_p (set_dest, SET_SRC (set_pattern)))
5592 return;
5595 /* Now determine if we used or set the register in the old insns
5596 previous to this one. */
5598 for (insn = old_first_insn; insn != set_insn; insn = NEXT_INSN (insn))
5600 if (reg_overlap_mentioned_p (set_dest, insn))
5602 return;
5606 /* Now determine if we're setting it in the new insns, or using
5607 it. */
5608 for (insn = first_insn; insn != stop_insn; insn = NEXT_INSN (insn))
5610 set_type = sets_reg_or_subreg (insn, set_dest);
5611 if (set_type != 0)
5613 break;
5615 else if (reg_overlap_mentioned_p (set_dest, insn))
5617 /* Is the reg now used in this new insn? -- This is probably an
5618 error. */
5619 set_type = 2;
5620 break;
5623 if (set_type == 2)
5625 /* The register is being set via a SUBREG or is being used in
5626 some other way, so it's no longer dead.
5628 Search backwards from first_insn, looking for the first insn
5629 that uses the original dest. Stop if we pass a CODE_LABEL or
5630 a JUMP_INSN.
5632 If we find such an insn and it has a REG_DEAD note referring
5633 to the original dest, then delete the note. */
5635 for (insn = first_insn; insn != NULL_RTX; insn = PREV_INSN (insn))
5637 if (GET_CODE (insn) == CODE_LABEL
5638 || GET_CODE (insn) == JUMP_INSN)
5639 break;
5640 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
5641 && reg_mentioned_p (set_dest, insn))
5643 rtx note = find_regno_note (insn, REG_DEAD, REGNO (set_dest));
5644 if (note != NULL_RTX)
5646 remove_note (insn, note);
5648 /* ??? -- Is this right? */
5649 break;
5653 else if (set_type == 0)
5655 /* The reg is not being set or used in the new insns at all. */
5656 int i, regno;
5658 /* Should never reach here for a pseudo reg. */
5659 if (REGNO (set_dest) >= FIRST_PSEUDO_REGISTER)
5660 abort ();
5662 /* This can happen for a hard register, if the new insns do not
5663 contain instructions which would be no-ops referring to the
5664 old registers.
5666 We try to verify that this is the case by checking to see if
5667 the original instruction uses all of the registers that it
5668 set. This case is OK, because deleting a no-op can not affect
5669 REG_DEAD notes on other insns. If this is not the case, then
5670 abort. */
5672 regno = REGNO (set_dest);
5673 for (i = HARD_REGNO_NREGS (regno, GET_MODE (set_dest)) - 1;
5674 i >= 0; i--)
5676 if (! refers_to_regno_p (regno + i, regno + i + 1, set,
5677 NULL_PTR))
5678 break;
5680 if (i >= 0)
5681 abort ();
5685 /* Updates all flow-analysis related quantities (including REG_NOTES) for
5686 the insns from FIRST to LAST inclusive that were created by replacing
5687 the insns from ORIG_INSN_FIRST to ORIG_INSN_LAST inclusive. NOTES
5688 are the original REG_NOTES. */
5690 void
5691 update_life_info (notes, first, last, orig_first_insn, orig_last_insn)
5692 rtx notes;
5693 rtx first, last;
5694 rtx orig_first_insn, orig_last_insn;
5696 rtx insn, note;
5697 rtx next;
5698 rtx orig_dest, temp;
5699 rtx orig_insn;
5700 rtx tem;
5702 /* Get and save the destination set by the original insn, if there
5703 was only one insn replaced. */
5705 if (orig_first_insn == orig_last_insn)
5707 orig_insn = orig_first_insn;
5708 orig_dest = single_set (orig_insn);
5709 if (orig_dest)
5710 orig_dest = SET_DEST (orig_dest);
5712 else
5714 orig_insn = NULL_RTX;
5715 orig_dest = NULL_RTX;
5718 /* Move REG_NOTES from the original insns to where they now belong. */
5720 for (note = notes; note; note = next)
5722 next = XEXP (note, 1);
5723 switch (REG_NOTE_KIND (note))
5725 case REG_DEAD:
5726 case REG_UNUSED:
5727 /* Move these notes from the original insn to the last new
5728 insn where the register is mentioned. */
5730 for (insn = last; ; insn = PREV_INSN (insn))
5732 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
5733 && reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
5735 /* Sometimes need to convert REG_UNUSED notes to
5736 REG_DEAD notes. */
5737 if (REG_NOTE_KIND (note) == REG_UNUSED
5738 && GET_CODE (XEXP (note, 0)) == REG
5739 && ! dead_or_set_p (insn, XEXP (note, 0)))
5741 PUT_REG_NOTE_KIND (note, REG_DEAD);
5743 split_hard_reg_notes (insn, note, first, last);
5744 /* The reg only dies in one insn, the last one that uses
5745 it. */
5746 break;
5748 /* It must die somewhere, fail if we couldn't find where it died.
5750 We abort because otherwise the register will be live
5751 longer than it should, and we'll probably take an
5752 abort later. What we should do instead is search back
5753 and find the appropriate places to insert the note. */
5754 if (insn == first)
5756 if (REG_NOTE_KIND (note) == REG_DEAD)
5758 abort ();
5760 break;
5763 break;
5765 case REG_WAS_0:
5767 rtx note_dest;
5769 /* If the insn that set the register to 0 was deleted, this
5770 note cannot be relied on any longer. The destination might
5771 even have been moved to memory.
5772 This was observed for SH4 with execute/920501-6.c compilation,
5773 -O2 -fomit-frame-pointer -finline-functions . */
5775 if (GET_CODE (XEXP (note, 0)) == NOTE
5776 || INSN_DELETED_P (XEXP (note, 0)))
5777 break;
5778 if (orig_insn != NULL_RTX)
5780 note_dest = orig_dest;
5782 else
5784 note_dest = find_insn_with_note (note, orig_first_insn,
5785 orig_last_insn);
5786 if (note_dest != NULL_RTX)
5788 note_dest = single_set (note_dest);
5789 if (note_dest != NULL_RTX)
5790 note_dest = SET_DEST (note_dest);
5793 /* This note applies to the dest of the original insn. Find the
5794 first new insn that now has the same dest, and move the note
5795 there. */
5797 if (! note_dest)
5798 break;
5800 for (insn = first; ; insn = NEXT_INSN (insn))
5802 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
5803 && (temp = single_set (insn))
5804 && rtx_equal_p (SET_DEST (temp), note_dest))
5806 XEXP (note, 1) = REG_NOTES (insn);
5807 REG_NOTES (insn) = note;
5808 /* The reg is only zero before one insn, the first that
5809 uses it. */
5810 break;
5812 /* If this note refers to a multiple word hard
5813 register, it may have been split into several smaller
5814 hard register references. We could split the notes,
5815 but simply dropping them is good enough. */
5816 if (GET_CODE (note_dest) == REG
5817 && REGNO (note_dest) < FIRST_PSEUDO_REGISTER
5818 && HARD_REGNO_NREGS (REGNO (note_dest),
5819 GET_MODE (note_dest)) > 1)
5820 break;
5822 /* It must be set somewhere; bail if we couldn't find
5823 where it was set. */
5826 break;
5828 case REG_EQUAL:
5829 case REG_EQUIV:
5830 /* A REG_EQUIV or REG_EQUAL note on an insn with more than one
5831 set is meaningless. Just drop the note. */
5832 if (! orig_dest)
5833 break;
5835 case REG_NO_CONFLICT:
5836 case REG_NOALIAS:
5837 /* These notes apply to the dest of the original insn. Find the last
5838 new insn that now has the same dest, and move the note there.
5840 If we are replacing multiple insns, just drop the note. */
5842 if (! orig_insn)
5843 break;
5845 if (! orig_dest)
5846 abort ();
5848 for (insn = last; ; insn = PREV_INSN (insn))
5850 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
5851 && (temp = single_set (insn))
5852 && rtx_equal_p (SET_DEST (temp), orig_dest))
5854 XEXP (note, 1) = REG_NOTES (insn);
5855 REG_NOTES (insn) = note;
5856 /* Only put this note on one of the new insns. */
5857 break;
5860 /* The original dest must still be set someplace. Abort if we
5861 couldn't find it. */
5862 if (insn == first)
5864 /* However, if this note refers to a multiple word hard
5865 register, it may have been split into several smaller
5866 hard register references. We could split the notes,
5867 but simply dropping them is good enough. */
5868 if (GET_CODE (orig_dest) == REG
5869 && REGNO (orig_dest) < FIRST_PSEUDO_REGISTER
5870 && HARD_REGNO_NREGS (REGNO (orig_dest),
5871 GET_MODE (orig_dest)) > 1)
5872 break;
5873 /* Likewise for multi-word memory references. */
5874 if (GET_CODE (orig_dest) == MEM
5875 && GET_MODE_SIZE (GET_MODE (orig_dest)) > MOVE_MAX)
5876 break;
5877 abort ();
5880 break;
5882 case REG_LIBCALL:
5883 /* Move a REG_LIBCALL note to the first insn created, and update
5884 the corresponding REG_RETVAL note. */
5885 XEXP (note, 1) = REG_NOTES (first);
5886 REG_NOTES (first) = note;
5888 insn = XEXP (note, 0);
5889 note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
5890 if (note)
5891 XEXP (note, 0) = first;
5892 break;
5894 case REG_EXEC_COUNT:
5895 /* Move a REG_EXEC_COUNT note to the first insn created. */
5896 XEXP (note, 1) = REG_NOTES (first);
5897 REG_NOTES (first) = note;
5898 break;
5900 case REG_RETVAL:
5901 /* Move a REG_RETVAL note to the last insn created, and update
5902 the corresponding REG_LIBCALL note. */
5903 XEXP (note, 1) = REG_NOTES (last);
5904 REG_NOTES (last) = note;
5906 insn = XEXP (note, 0);
5907 note = find_reg_note (insn, REG_LIBCALL, NULL_RTX);
5908 if (note)
5909 XEXP (note, 0) = last;
5910 break;
5912 case REG_NONNEG:
5913 case REG_BR_PROB:
5914 /* This should be moved to whichever instruction is a JUMP_INSN. */
5916 for (insn = last; ; insn = PREV_INSN (insn))
5918 if (GET_CODE (insn) == JUMP_INSN)
5920 XEXP (note, 1) = REG_NOTES (insn);
5921 REG_NOTES (insn) = note;
5922 /* Only put this note on one of the new insns. */
5923 break;
5925 /* Fail if we couldn't find a JUMP_INSN. */
5926 if (insn == first)
5927 abort ();
5929 break;
5931 case REG_INC:
5932 /* reload sometimes leaves obsolete REG_INC notes around. */
5933 if (reload_completed)
5934 break;
5935 /* This should be moved to whichever instruction now has the
5936 increment operation. */
5937 abort ();
5939 case REG_LABEL:
5940 /* Should be moved to the new insn(s) which use the label. */
5941 for (insn = first; insn != NEXT_INSN (last); insn = NEXT_INSN (insn))
5942 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
5943 && reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
5944 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_LABEL,
5945 XEXP (note, 0),
5946 REG_NOTES (insn));
5947 break;
5949 case REG_CC_SETTER:
5950 case REG_CC_USER:
5951 /* These two notes will never appear until after reorg, so we don't
5952 have to handle them here. */
5953 default:
5954 abort ();
5958 /* Each new insn created has a new set. If the destination is a
5959 register, then this reg is now live across several insns, whereas
5960 previously the dest reg was born and died within the same insn.
5961 To reflect this, we now need a REG_DEAD note on the insn where
5962 this dest reg dies.
5964 Similarly, the new insns may have clobbers that need REG_UNUSED
5965 notes. */
5967 for (insn = first; ;insn = NEXT_INSN (insn))
5969 rtx pat;
5970 int i;
5972 pat = PATTERN (insn);
5973 if (GET_CODE (pat) == SET || GET_CODE (pat) == CLOBBER)
5974 new_insn_dead_notes (pat, insn, first, last,
5975 orig_first_insn, orig_last_insn);
5976 else if (GET_CODE (pat) == PARALLEL)
5978 for (i = 0; i < XVECLEN (pat, 0); i++)
5980 if (GET_CODE (XVECEXP (pat, 0, i)) == SET
5981 || GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER)
5983 rtx parpat = XVECEXP (pat, 0, i);
5985 new_insn_dead_notes (parpat, insn, first, last,
5986 orig_first_insn, orig_last_insn);
5990 if (insn == last)
5992 break;
5996 /* Check to see if we have any REG_DEAD notes on insns previous to
5997 the new ones that are now incorrect and need to be removed. */
5999 for (insn = orig_first_insn; ; insn = NEXT_INSN (insn))
6001 maybe_remove_dead_notes (insn, insn, first, last,
6002 orig_first_insn, orig_last_insn);
6004 if (insn == orig_last_insn)
6005 break;
6008 /* Update reg_n_sets. This is necessary to prevent local alloc from
6009 converting REG_EQUAL notes to REG_EQUIV when the new insns are setting
6010 a reg multiple times instead of once. */
6012 for (tem = orig_first_insn; tem != NULL_RTX; tem = NEXT_INSN (tem))
6014 rtx x;
6015 RTX_CODE code;
6017 if (GET_RTX_CLASS (GET_CODE (tem)) != 'i')
6018 continue;
6020 x = PATTERN (tem);
6021 code = GET_CODE (x);
6022 if (code == SET || code == CLOBBER)
6023 update_n_sets (x, -1);
6024 else if (code == PARALLEL)
6026 int i;
6027 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
6029 code = GET_CODE (XVECEXP (x, 0, i));
6030 if (code == SET || code == CLOBBER)
6031 update_n_sets (XVECEXP (x, 0, i), -1);
6034 if (tem == orig_last_insn)
6035 break;
6038 for (insn = first; ; insn = NEXT_INSN (insn))
6040 rtx x = PATTERN (insn);
6041 RTX_CODE code = GET_CODE (x);
6043 if (code == SET || code == CLOBBER)
6044 update_n_sets (x, 1);
6045 else if (code == PARALLEL)
6047 int i;
6048 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
6050 code = GET_CODE (XVECEXP (x, 0, i));
6051 if (code == SET || code == CLOBBER)
6052 update_n_sets (XVECEXP (x, 0, i), 1);
6056 if (insn == last)
6057 break;
6061 /* Prepends the set of REG_NOTES in NEW to NOTES, and returns NEW. */
6062 static rtx
6063 prepend_reg_notes (notes, new)
6064 rtx notes, new;
6066 rtx end;
6068 if (new == NULL_RTX)
6070 return notes;
6072 if (notes == NULL_RTX)
6074 return new;
6076 end = new;
6077 while (XEXP (end, 1) != NULL_RTX)
6079 end = XEXP (end, 1);
6081 XEXP (end, 1) = notes;
6082 return new;
6085 /* Replace the insns from FIRST to LAST inclusive with the set of insns in
6086 NEW, and update the life analysis info accordingly. */
6087 void
6088 replace_insns (first, last, first_new, notes)
6089 rtx first, last, first_new, notes;
6091 rtx stop = NEXT_INSN (last);
6092 rtx prev = PREV_INSN (first);
6093 rtx last_new, curr;
6094 int i;
6096 if (notes == NULL_RTX)
6098 for (curr = first; curr != stop; curr = NEXT_INSN (curr))
6099 if (GET_RTX_CLASS (GET_CODE (curr)) == 'i')
6100 notes = prepend_reg_notes (notes, REG_NOTES (curr));
6103 last_new = emit_insn_after (first_new, prev);
6104 first_new = NEXT_INSN (prev);
6106 for (i = 0; i < n_basic_blocks; i++)
6108 if (BLOCK_HEAD (i) == first)
6109 BLOCK_HEAD (i) = first_new;
6110 if (BLOCK_END (i) == last)
6111 BLOCK_END (i) = last_new;
6113 /* This is probably bogus. */
6114 if (first_new == last_new)
6116 if (GET_CODE (first_new) == SEQUENCE)
6118 first_new = XVECEXP (first_new, 0, 0);
6119 last_new = XVECEXP (last_new, 0, XVECLEN (last_new, 0) - 1);
6122 update_life_info (notes, first_new, last_new, first, last);
6123 flow_delete_insn_chain (first, last);
6126 /* Verify the CFG consistency. This function check some CFG invariants and
6127 aborts when something is wrong. Hope that this function will help to
6128 convert many optimization passes to preserve CFG consistent.
6130 Currently it does following checks:
6132 - test head/end pointers
6133 - overlapping of basic blocks
6134 - edge list corectness
6135 - headers of basic blocks (the NOTE_INSN_BASIC_BLOCK note)
6136 - tails of basic blocks (ensure that boundary is necesary)
6137 - scans body of the basic block for JUMP_INSN, CODE_LABEL
6138 and NOTE_INSN_BASIC_BLOCK
6139 - check that all insns are in the basic blocks
6140 (except the switch handling code, barriers and notes)
6142 In future it can be extended check a lot of other stuff as well
6143 (reachability of basic blocks, life information, etc. etc.). */
6145 void
6146 verify_flow_info ()
6148 const int max_uid = get_max_uid ();
6149 const rtx rtx_first = get_insns ();
6150 basic_block *bb_info;
6151 rtx x;
6152 int i;
6154 bb_info = (basic_block *) alloca (max_uid * sizeof (basic_block));
6155 memset (bb_info, 0, max_uid * sizeof (basic_block));
6157 /* First pass check head/end pointers and set bb_info array used by
6158 later passes. */
6159 for (i = n_basic_blocks - 1; i >= 0; i--)
6161 basic_block bb = BASIC_BLOCK (i);
6163 /* Check the head pointer and make sure that it is pointing into
6164 insn list. */
6165 for (x = rtx_first; x != NULL_RTX; x = NEXT_INSN (x))
6166 if (x == bb->head)
6167 break;
6168 if (!x)
6170 error ("Head insn %d for block %d not found in the insn stream.",
6171 INSN_UID (bb->head), bb->index);
6172 abort ();
6175 /* Check the end pointer and make sure that it is pointing into
6176 insn list. */
6177 for (x = bb->head; x != NULL_RTX; x = NEXT_INSN (x))
6179 if (bb_info[INSN_UID (x)] != NULL)
6181 error ("Insn %d is in multiple basic blocks (%d and %d)",
6182 INSN_UID (x), bb->index, bb_info[INSN_UID (x)]->index);
6183 abort ();
6185 bb_info[INSN_UID (x)] = bb;
6187 if (x == bb->end)
6188 break;
6190 if (!x)
6192 error ("End insn %d for block %d not found in the insn stream.",
6193 INSN_UID (bb->end), bb->index);
6194 abort ();
6198 /* Now check the basic blocks (boundaries etc.) */
6199 for (i = n_basic_blocks - 1; i >= 0; i--)
6201 basic_block bb = BASIC_BLOCK (i);
6202 /* Check corectness of edge lists */
6203 edge e;
6205 e = bb->succ;
6206 while (e)
6208 if (e->src != bb)
6210 fprintf (stderr, "verify_flow_info: Basic block %d succ edge is corrupted\n",
6211 bb->index);
6212 fprintf (stderr, "Predecessor: ");
6213 dump_edge_info (stderr, e, 0);
6214 fprintf (stderr, "\nSuccessor: ");
6215 dump_edge_info (stderr, e, 1);
6216 fflush (stderr);
6217 abort ();
6219 if (e->dest != EXIT_BLOCK_PTR)
6221 edge e2 = e->dest->pred;
6222 while (e2 && e2 != e)
6223 e2 = e2->pred_next;
6224 if (!e2)
6226 error ("Basic block %i edge lists are corrupted", bb->index);
6227 abort ();
6230 e = e->succ_next;
6233 e = bb->pred;
6234 while (e)
6236 if (e->dest != bb)
6238 error ("Basic block %d pred edge is corrupted", bb->index);
6239 fputs ("Predecessor: ", stderr);
6240 dump_edge_info (stderr, e, 0);
6241 fputs ("\nSuccessor: ", stderr);
6242 dump_edge_info (stderr, e, 1);
6243 fputc ('\n', stderr);
6244 abort ();
6246 if (e->src != ENTRY_BLOCK_PTR)
6248 edge e2 = e->src->succ;
6249 while (e2 && e2 != e)
6250 e2 = e2->succ_next;
6251 if (!e2)
6253 error ("Basic block %i edge lists are corrupted", bb->index);
6254 abort;
6257 e = e->pred_next;
6260 /* OK pointers are correct. Now check the header of basic
6261 block. It ought to contain optional CODE_LABEL followed
6262 by NOTE_BASIC_BLOCK. */
6263 x = bb->head;
6264 if (GET_CODE (x) == CODE_LABEL)
6266 if (bb->end == x)
6268 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
6269 bb->index);
6270 abort ();
6272 x = NEXT_INSN (x);
6274 if (GET_CODE (x) != NOTE
6275 || NOTE_LINE_NUMBER (x) != NOTE_INSN_BASIC_BLOCK
6276 || NOTE_BASIC_BLOCK (x) != bb)
6278 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d\n",
6279 bb->index);
6280 abort ();
6283 if (bb->end == x)
6285 /* Do checks for empty blocks here */
6287 else
6289 x = NEXT_INSN (x);
6290 while (x)
6292 if (GET_CODE (x) == NOTE
6293 && NOTE_LINE_NUMBER (x) == NOTE_INSN_BASIC_BLOCK)
6295 error ("NOTE_INSN_BASIC_BLOCK %d in the middle of basic block %d",
6296 INSN_UID (x), bb->index);
6297 abort ();
6300 if (x == bb->end)
6301 break;
6303 if (GET_CODE (x) == JUMP_INSN
6304 || GET_CODE (x) == CODE_LABEL
6305 || GET_CODE (x) == BARRIER)
6307 error ("In basic block %d:", bb->index);
6308 fatal_insn ("Flow control insn inside a basic block", x);
6311 x = NEXT_INSN (x);
6316 x = rtx_first;
6317 while (x)
6319 if (!bb_info[INSN_UID (x)])
6321 switch (GET_CODE (x))
6323 case BARRIER:
6324 case NOTE:
6325 break;
6327 case CODE_LABEL:
6328 /* An addr_vec is placed outside any block block. */
6329 if (NEXT_INSN (x)
6330 && GET_CODE (NEXT_INSN (x)) == JUMP_INSN
6331 && (GET_CODE (PATTERN (NEXT_INSN (x))) == ADDR_DIFF_VEC
6332 || GET_CODE (PATTERN (NEXT_INSN (x))) == ADDR_VEC))
6334 x = NEXT_INSN (x);
6337 /* But in any case, non-deletable labels can appear anywhere. */
6338 break;
6340 default:
6341 fatal_insn ("Insn outside basic block", x);
6345 x = NEXT_INSN (x);
6349 /* Functions to access an edge list with a vector representation.
6350 Enough data is kept such that given an index number, the
6351 pred and succ that edge reprsents can be determined, or
6352 given a pred and a succ, it's index number can be returned.
6353 This allows algorithms which comsume a lot of memory to
6354 represent the normally full matrix of edge (pred,succ) with a
6355 single indexed vector, edge (EDGE_INDEX (pred, succ)), with no
6356 wasted space in the client code due to sparse flow graphs. */
6358 /* This functions initializes the edge list. Basically the entire
6359 flowgraph is processed, and all edges are assigned a number,
6360 and the data structure is filed in. */
6361 struct edge_list *
6362 create_edge_list ()
6364 struct edge_list *elist;
6365 edge e;
6366 int num_edges;
6367 int x;
6368 int block_count;
6370 block_count = n_basic_blocks + 2; /* Include the entry and exit blocks. */
6372 num_edges = 0;
6374 /* Determine the number of edges in the flow graph by counting successor
6375 edges on each basic block. */
6376 for (x = 0; x < n_basic_blocks; x++)
6378 basic_block bb = BASIC_BLOCK (x);
6380 for (e = bb->succ; e; e = e->succ_next)
6381 num_edges++;
6383 /* Don't forget successors of the entry block. */
6384 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
6385 num_edges++;
6387 elist = xmalloc (sizeof (struct edge_list));
6388 elist->num_blocks = block_count;
6389 elist->num_edges = num_edges;
6390 elist->index_to_edge = xmalloc (sizeof (edge) * num_edges);
6392 num_edges = 0;
6394 /* Follow successors of the entry block, and register these edges. */
6395 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
6397 elist->index_to_edge[num_edges] = e;
6398 num_edges++;
6401 for (x = 0; x < n_basic_blocks; x++)
6403 basic_block bb = BASIC_BLOCK (x);
6405 /* Follow all successors of blocks, and register these edges. */
6406 for (e = bb->succ; e; e = e->succ_next)
6408 elist->index_to_edge[num_edges] = e;
6409 num_edges++;
6412 return elist;
6415 /* This function free's memory associated with an edge list. */
6416 void
6417 free_edge_list (elist)
6418 struct edge_list *elist;
6420 if (elist)
6422 free (elist->index_to_edge);
6423 free (elist);
6427 /* This function provides debug output showing an edge list. */
6428 void
6429 print_edge_list (f, elist)
6430 FILE *f;
6431 struct edge_list *elist;
6433 int x;
6434 fprintf(f, "Compressed edge list, %d BBs + entry & exit, and %d edges\n",
6435 elist->num_blocks - 2, elist->num_edges);
6437 for (x = 0; x < elist->num_edges; x++)
6439 fprintf (f, " %-4d - edge(", x);
6440 if (INDEX_EDGE_PRED_BB (elist, x) == ENTRY_BLOCK_PTR)
6441 fprintf (f,"entry,");
6442 else
6443 fprintf (f,"%d,", INDEX_EDGE_PRED_BB (elist, x)->index);
6445 if (INDEX_EDGE_SUCC_BB (elist, x) == EXIT_BLOCK_PTR)
6446 fprintf (f,"exit)\n");
6447 else
6448 fprintf (f,"%d)\n", INDEX_EDGE_SUCC_BB (elist, x)->index);
6452 /* This function provides an internal consistancy check of an edge list,
6453 verifying that all edges are present, and that there are no
6454 extra edges. */
6455 void
6456 verify_edge_list (f, elist)
6457 FILE *f;
6458 struct edge_list *elist;
6460 int x, pred, succ, index;
6461 edge e;
6463 for (x = 0; x < n_basic_blocks; x++)
6465 basic_block bb = BASIC_BLOCK (x);
6467 for (e = bb->succ; e; e = e->succ_next)
6469 pred = e->src->index;
6470 succ = e->dest->index;
6471 index = EDGE_INDEX (elist, pred, succ);
6472 if (index == EDGE_INDEX_NO_EDGE)
6474 fprintf (f, "*p* No index for edge from %d to %d\n",pred, succ);
6475 continue;
6477 if (INDEX_EDGE_PRED_BB (elist, index)->index != pred)
6478 fprintf (f, "*p* Pred for index %d should be %d not %d\n",
6479 index, pred, INDEX_EDGE_PRED_BB (elist, index)->index);
6480 if (INDEX_EDGE_SUCC_BB (elist, index)->index != succ)
6481 fprintf (f, "*p* Succ for index %d should be %d not %d\n",
6482 index, succ, INDEX_EDGE_SUCC_BB (elist, index)->index);
6485 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
6487 pred = e->src->index;
6488 succ = e->dest->index;
6489 index = EDGE_INDEX (elist, pred, succ);
6490 if (index == EDGE_INDEX_NO_EDGE)
6492 fprintf (f, "*p* No index for edge from %d to %d\n",pred, succ);
6493 continue;
6495 if (INDEX_EDGE_PRED_BB (elist, index)->index != pred)
6496 fprintf (f, "*p* Pred for index %d should be %d not %d\n",
6497 index, pred, INDEX_EDGE_PRED_BB (elist, index)->index);
6498 if (INDEX_EDGE_SUCC_BB (elist, index)->index != succ)
6499 fprintf (f, "*p* Succ for index %d should be %d not %d\n",
6500 index, succ, INDEX_EDGE_SUCC_BB (elist, index)->index);
6502 /* We've verified that all the edges are in the list, no lets make sure
6503 there are no spurious edges in the list. */
6505 for (pred = 0 ; pred < n_basic_blocks; pred++)
6506 for (succ = 0 ; succ < n_basic_blocks; succ++)
6508 basic_block p = BASIC_BLOCK (pred);
6509 basic_block s = BASIC_BLOCK (succ);
6511 int found_edge = 0;
6513 for (e = p->succ; e; e = e->succ_next)
6514 if (e->dest == s)
6516 found_edge = 1;
6517 break;
6519 for (e = s->pred; e; e = e->pred_next)
6520 if (e->src == p)
6522 found_edge = 1;
6523 break;
6525 if (EDGE_INDEX (elist, pred, succ) == EDGE_INDEX_NO_EDGE
6526 && found_edge != 0)
6527 fprintf (f, "*** Edge (%d, %d) appears to not have an index\n",
6528 pred, succ);
6529 if (EDGE_INDEX (elist, pred, succ) != EDGE_INDEX_NO_EDGE
6530 && found_edge == 0)
6531 fprintf (f, "*** Edge (%d, %d) has index %d, but there is no edge\n",
6532 pred, succ, EDGE_INDEX (elist, pred, succ));
6534 for (succ = 0 ; succ < n_basic_blocks; succ++)
6536 basic_block p = ENTRY_BLOCK_PTR;
6537 basic_block s = BASIC_BLOCK (succ);
6539 int found_edge = 0;
6541 for (e = p->succ; e; e = e->succ_next)
6542 if (e->dest == s)
6544 found_edge = 1;
6545 break;
6547 for (e = s->pred; e; e = e->pred_next)
6548 if (e->src == p)
6550 found_edge = 1;
6551 break;
6553 if (EDGE_INDEX (elist, ENTRY_BLOCK, succ) == EDGE_INDEX_NO_EDGE
6554 && found_edge != 0)
6555 fprintf (f, "*** Edge (entry, %d) appears to not have an index\n",
6556 succ);
6557 if (EDGE_INDEX (elist, ENTRY_BLOCK, succ) != EDGE_INDEX_NO_EDGE
6558 && found_edge == 0)
6559 fprintf (f, "*** Edge (entry, %d) has index %d, but no edge exists\n",
6560 succ, EDGE_INDEX (elist, ENTRY_BLOCK, succ));
6562 for (pred = 0 ; pred < n_basic_blocks; pred++)
6564 basic_block p = BASIC_BLOCK (pred);
6565 basic_block s = EXIT_BLOCK_PTR;
6567 int found_edge = 0;
6569 for (e = p->succ; e; e = e->succ_next)
6570 if (e->dest == s)
6572 found_edge = 1;
6573 break;
6575 for (e = s->pred; e; e = e->pred_next)
6576 if (e->src == p)
6578 found_edge = 1;
6579 break;
6581 if (EDGE_INDEX (elist, pred, EXIT_BLOCK) == EDGE_INDEX_NO_EDGE
6582 && found_edge != 0)
6583 fprintf (f, "*** Edge (%d, exit) appears to not have an index\n",
6584 pred);
6585 if (EDGE_INDEX (elist, pred, EXIT_BLOCK) != EDGE_INDEX_NO_EDGE
6586 && found_edge == 0)
6587 fprintf (f, "*** Edge (%d, exit) has index %d, but no edge exists\n",
6588 pred, EDGE_INDEX (elist, pred, EXIT_BLOCK));
6592 /* This routine will determine what, if any, edge there is between
6593 a specified predecessor and successor. */
6596 find_edge_index (edge_list, pred, succ)
6597 struct edge_list *edge_list;
6598 int pred, succ;
6600 int x;
6601 for (x = 0; x < NUM_EDGES (edge_list); x++)
6603 if (INDEX_EDGE_PRED_BB (edge_list, x)->index == pred
6604 && INDEX_EDGE_SUCC_BB (edge_list, x)->index == succ)
6605 return x;
6607 return (EDGE_INDEX_NO_EDGE);