1 /* Data flow analysis for GNU compiler.
2 Copyright (C) 1987, 88, 92-96, 1997 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)
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.
23 It computes data flow information
24 which tells combine_instructions which insns to consider combining
25 and controls register allocation.
27 Additional data flow information that is too bulky to record
28 is generated during the analysis, and is used at that time to
29 create autoincrement and autodecrement addressing.
31 The first step is dividing the function into basic blocks.
32 find_basic_blocks does this. Then life_analysis determines
33 where each register is live and where it is dead.
35 ** find_basic_blocks **
37 find_basic_blocks divides the current function's rtl
38 into basic blocks. It records the beginnings and ends of the
39 basic blocks in the vectors basic_block_head and basic_block_end,
40 and the number of blocks in n_basic_blocks.
42 find_basic_blocks also finds any unreachable loops
47 life_analysis is called immediately after find_basic_blocks.
48 It uses the basic block information to determine where each
49 hard or pseudo register is live.
51 ** live-register info **
53 The information about where each register is live is in two parts:
54 the REG_NOTES of insns, and the vector basic_block_live_at_start.
56 basic_block_live_at_start has an element for each basic block,
57 and the element is a bit-vector with a bit for each hard or pseudo
58 register. The bit is 1 if the register is live at the beginning
61 Two types of elements can be added to an insn's REG_NOTES.
62 A REG_DEAD note is added to an insn's REG_NOTES for any register
63 that meets both of two conditions: The value in the register is not
64 needed in subsequent insns and the insn does not replace the value in
65 the register (in the case of multi-word hard registers, the value in
66 each register must be replaced by the insn to avoid a REG_DEAD note).
68 In the vast majority of cases, an object in a REG_DEAD note will be
69 used somewhere in the insn. The (rare) exception to this is if an
70 insn uses a multi-word hard register and only some of the registers are
71 needed in subsequent insns. In that case, REG_DEAD notes will be
72 provided for those hard registers that are not subsequently needed.
73 Partial REG_DEAD notes of this type do not occur when an insn sets
74 only some of the hard registers used in such a multi-word operand;
75 omitting REG_DEAD notes for objects stored in an insn is optional and
76 the desire to do so does not justify the complexity of the partial
79 REG_UNUSED notes are added for each register that is set by the insn
80 but is unused subsequently (if every register set by the insn is unused
81 and the insn does not reference memory or have some other side-effect,
82 the insn is deleted instead). If only part of a multi-word hard
83 register is used in a subsequent insn, REG_UNUSED notes are made for
84 the parts that will not be used.
86 To determine which registers are live after any insn, one can
87 start from the beginning of the basic block and scan insns, noting
88 which registers are set by each insn and which die there.
90 ** Other actions of life_analysis **
92 life_analysis sets up the LOG_LINKS fields of insns because the
93 information needed to do so is readily available.
95 life_analysis deletes insns whose only effect is to store a value
98 life_analysis notices cases where a reference to a register as
99 a memory address can be combined with a preceding or following
100 incrementation or decrementation of the register. The separate
101 instruction to increment or decrement is deleted and the address
102 is changed to a POST_INC or similar rtx.
104 Each time an incrementing or decrementing address is created,
105 a REG_INC element is added to the insn's REG_NOTES list.
107 life_analysis fills in certain vectors containing information about
108 register usage: reg_n_refs, reg_n_deaths, reg_n_sets, reg_live_length,
109 reg_n_calls_crosses and reg_basic_block. */
114 #include "basic-block.h"
115 #include "insn-config.h"
117 #include "hard-reg-set.h"
123 #define obstack_chunk_alloc xmalloc
124 #define obstack_chunk_free free
126 /* The contents of the current function definition are allocated
127 in this obstack, and all are freed at the end of the function.
128 For top-level functions, this is temporary_obstack.
129 Separate obstacks are made for nested functions. */
131 extern struct obstack *function_obstack;
133 /* List of labels that must never be deleted. */
134 extern rtx forced_labels;
136 /* Get the basic block number of an insn.
137 This info should not be expected to remain available
138 after the end of life_analysis. */
140 /* This is the limit of the allocated space in the following two arrays. */
142 static int max_uid_for_flow;
144 #define BLOCK_NUM(INSN) uid_block_number[INSN_UID (INSN)]
146 /* This is where the BLOCK_NUM values are really stored.
147 This is set up by find_basic_blocks and used there and in life_analysis,
150 static int *uid_block_number;
152 /* INSN_VOLATILE (insn) is 1 if the insn refers to anything volatile. */
154 #define INSN_VOLATILE(INSN) uid_volatile[INSN_UID (INSN)]
155 static char *uid_volatile;
157 /* Number of basic blocks in the current function. */
161 /* Maximum register number used in this function, plus one. */
165 /* Maximum number of SCRATCH rtx's used in any basic block of this
170 /* Number of SCRATCH rtx's in the current block. */
172 static int num_scratch;
174 /* Indexed by n, giving various register information */
176 reg_info *reg_n_info;
178 /* Element N is the next insn that uses (hard or pseudo) register number N
179 within the current basic block; or zero, if there is no such insn.
180 This is valid only during the final backward scan in propagate_block. */
182 static rtx *reg_next_use;
184 /* Size of a regset for the current function,
185 in (1) bytes and (2) elements. */
190 /* Element N is first insn in basic block N.
191 This info lasts until we finish compiling the function. */
193 rtx *basic_block_head;
195 /* Element N is last insn in basic block N.
196 This info lasts until we finish compiling the function. */
198 rtx *basic_block_end;
200 /* Element N is a regset describing the registers live
201 at the start of basic block N.
202 This info lasts until we finish compiling the function. */
204 regset *basic_block_live_at_start;
206 /* Regset of regs live when calls to `setjmp'-like functions happen. */
208 regset regs_live_at_setjmp;
210 /* List made of EXPR_LIST rtx's which gives pairs of pseudo registers
211 that have to go in the same hard reg.
212 The first two regs in the list are a pair, and the next two
213 are another pair, etc. */
216 /* Element N is nonzero if control can drop into basic block N
217 from the preceding basic block. Freed after life_analysis. */
219 static char *basic_block_drops_in;
221 /* Element N is depth within loops of the last insn in basic block number N.
222 Freed after life_analysis. */
224 static short *basic_block_loop_depth;
226 /* Element N nonzero if basic block N can actually be reached.
227 Vector exists only during find_basic_blocks. */
229 static char *block_live_static;
231 /* Depth within loops of basic block being scanned for lifetime analysis,
232 plus one. This is the weight attached to references to registers. */
234 static int loop_depth;
236 /* During propagate_block, this is non-zero if the value of CC0 is live. */
240 /* During propagate_block, this contains the last MEM stored into. It
241 is used to eliminate consecutive stores to the same location. */
243 static rtx last_mem_set;
245 /* Set of registers that may be eliminable. These are handled specially
246 in updating regs_ever_live. */
248 static HARD_REG_SET elim_reg_set;
250 /* Forward declarations */
251 static void find_basic_blocks PROTO((rtx, rtx));
252 static void mark_label_ref PROTO((rtx, rtx, int));
253 static void life_analysis PROTO((rtx, int));
254 void allocate_for_life_analysis PROTO((void));
255 void init_regset_vector PROTO((regset *, int, struct obstack *));
256 void free_regset_vector PROTO((regset *, int));
257 static void propagate_block PROTO((regset, rtx, rtx, int,
259 static rtx flow_delete_insn PROTO((rtx));
260 static int insn_dead_p PROTO((rtx, regset, int));
261 static int libcall_dead_p PROTO((rtx, regset, rtx, rtx));
262 static void mark_set_regs PROTO((regset, regset, rtx,
264 static void mark_set_1 PROTO((regset, regset, rtx,
266 static void find_auto_inc PROTO((regset, rtx, rtx));
267 static void mark_used_regs PROTO((regset, regset, rtx, int, rtx));
268 static int try_pre_increment_1 PROTO((rtx));
269 static int try_pre_increment PROTO((rtx, rtx, HOST_WIDE_INT));
270 void dump_flow_info PROTO((FILE *));
272 /* Find basic blocks of the current function and perform data flow analysis.
273 F is the first insn of the function and NREGS the number of register numbers
277 flow_analysis (f, nregs, file)
284 rtx nonlocal_label_list = nonlocal_label_rtx_list ();
286 #ifdef ELIMINABLE_REGS
287 static struct {int from, to; } eliminables[] = ELIMINABLE_REGS;
290 /* Record which registers will be eliminated. We use this in
293 CLEAR_HARD_REG_SET (elim_reg_set);
295 #ifdef ELIMINABLE_REGS
296 for (i = 0; i < sizeof eliminables / sizeof eliminables[0]; i++)
297 SET_HARD_REG_BIT (elim_reg_set, eliminables[i].from);
299 SET_HARD_REG_BIT (elim_reg_set, FRAME_POINTER_REGNUM);
302 /* Count the basic blocks. Also find maximum insn uid value used. */
305 register RTX_CODE prev_code = JUMP_INSN;
306 register RTX_CODE code;
309 max_uid_for_flow = 0;
311 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
313 code = GET_CODE (insn);
314 if (INSN_UID (insn) > max_uid_for_flow)
315 max_uid_for_flow = INSN_UID (insn);
316 if (code == CODE_LABEL
317 || (GET_RTX_CLASS (code) == 'i'
318 && (prev_code == JUMP_INSN
319 || (prev_code == CALL_INSN
320 && (nonlocal_label_list != 0 || eh_region))
321 || prev_code == BARRIER)))
324 if (code == CALL_INSN && find_reg_note (insn, REG_RETVAL, NULL_RTX))
329 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG)
331 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END)
337 /* Leave space for insns we make in some cases for auto-inc. These cases
338 are rare, so we don't need too much space. */
339 max_uid_for_flow += max_uid_for_flow / 10;
342 /* Allocate some tables that last till end of compiling this function
343 and some needed only in find_basic_blocks and life_analysis. */
346 basic_block_head = (rtx *) oballoc (n_basic_blocks * sizeof (rtx));
347 basic_block_end = (rtx *) oballoc (n_basic_blocks * sizeof (rtx));
348 basic_block_drops_in = (char *) alloca (n_basic_blocks);
349 basic_block_loop_depth = (short *) alloca (n_basic_blocks * sizeof (short));
351 = (int *) alloca ((max_uid_for_flow + 1) * sizeof (int));
352 uid_volatile = (char *) alloca (max_uid_for_flow + 1);
353 bzero (uid_volatile, max_uid_for_flow + 1);
355 find_basic_blocks (f, nonlocal_label_list);
356 life_analysis (f, nregs);
358 dump_flow_info (file);
360 basic_block_drops_in = 0;
361 uid_block_number = 0;
362 basic_block_loop_depth = 0;
365 /* Find all basic blocks of the function whose first insn is F.
366 Store the correct data in the tables that describe the basic blocks,
367 set up the chains of references for each CODE_LABEL, and
368 delete any entire basic blocks that cannot be reached.
370 NONLOCAL_LABEL_LIST is the same local variable from flow_analysis. */
373 find_basic_blocks (f, nonlocal_label_list)
374 rtx f, nonlocal_label_list;
378 register char *block_live = (char *) alloca (n_basic_blocks);
379 register char *block_marked = (char *) alloca (n_basic_blocks);
380 /* An array of CODE_LABELs, indexed by UID for the start of the active
381 EH handler for each insn in F. */
382 rtx *active_eh_handler;
383 /* List of label_refs to all labels whose addresses are taken
385 rtx label_value_list;
386 rtx x, note, eh_note;
387 enum rtx_code prev_code, code;
391 active_eh_handler = (rtx *) alloca ((max_uid_for_flow + 1) * sizeof (rtx));
394 label_value_list = 0;
395 block_live_static = block_live;
396 bzero (block_live, n_basic_blocks);
397 bzero (block_marked, n_basic_blocks);
398 bzero (active_eh_handler, (max_uid_for_flow + 1) * sizeof (rtx));
400 /* Initialize with just block 0 reachable and no blocks marked. */
401 if (n_basic_blocks > 0)
404 /* Initialize the ref chain of each label to 0. Record where all the
405 blocks start and end and their depth in loops. For each insn, record
406 the block it is in. Also mark as reachable any blocks headed by labels
407 that must not be deleted. */
409 for (eh_note = NULL_RTX, insn = f, i = -1, prev_code = JUMP_INSN, depth = 1;
410 insn; insn = NEXT_INSN (insn))
412 code = GET_CODE (insn);
415 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
417 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
421 /* A basic block starts at label, or after something that can jump. */
422 else if (code == CODE_LABEL
423 || (GET_RTX_CLASS (code) == 'i'
424 && (prev_code == JUMP_INSN
425 || (prev_code == CALL_INSN
426 && (nonlocal_label_list != 0 || eh_note)
427 && ! find_reg_note (insn, REG_RETVAL, NULL_RTX))
428 || prev_code == BARRIER)))
430 basic_block_head[++i] = insn;
431 basic_block_end[i] = insn;
432 basic_block_loop_depth[i] = depth;
434 if (code == CODE_LABEL)
436 LABEL_REFS (insn) = insn;
437 /* Any label that cannot be deleted
438 is considered to start a reachable block. */
439 if (LABEL_PRESERVE_P (insn))
444 else if (GET_RTX_CLASS (code) == 'i')
446 basic_block_end[i] = insn;
447 basic_block_loop_depth[i] = depth;
450 if (GET_RTX_CLASS (code) == 'i')
452 /* Make a list of all labels referred to other than by jumps. */
453 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
454 if (REG_NOTE_KIND (note) == REG_LABEL)
455 label_value_list = gen_rtx (EXPR_LIST, VOIDmode, XEXP (note, 0),
459 /* Keep a lifo list of the currently active exception handlers. */
460 if (GET_CODE (insn) == NOTE)
462 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG)
464 for (x = exception_handler_labels; x; x = XEXP (x, 1))
465 if (CODE_LABEL_NUMBER (XEXP (x, 0)) == NOTE_BLOCK_NUMBER (insn))
467 eh_note = gen_rtx (EXPR_LIST, VOIDmode,
468 XEXP (x, 0), eh_note);
474 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END)
475 eh_note = XEXP (eh_note, 1);
477 /* If we encounter a CALL_INSN, note which exception handler it
478 might pass control to.
480 If doing asynchronous exceptions, record the active EH handler
481 for every insn, since most insns can throw. */
483 && (asynchronous_exceptions
484 || (GET_CODE (insn) == CALL_INSN
485 && ! find_reg_note (insn, REG_RETVAL, NULL_RTX))))
486 active_eh_handler[INSN_UID (insn)] = XEXP (eh_note, 0);
488 BLOCK_NUM (insn) = i;
494 /* During the second pass, `n_basic_blocks' is only an upper bound.
495 Only perform the sanity check for the first pass, and on the second
496 pass ensure `n_basic_blocks' is set to the correct value. */
497 if (pass == 1 && i + 1 != n_basic_blocks)
499 n_basic_blocks = i + 1;
501 /* Record which basic blocks control can drop in to. */
503 for (i = 0; i < n_basic_blocks; i++)
505 for (insn = PREV_INSN (basic_block_head[i]);
506 insn && GET_CODE (insn) == NOTE; insn = PREV_INSN (insn))
509 basic_block_drops_in[i] = insn && GET_CODE (insn) != BARRIER;
512 /* Now find which basic blocks can actually be reached
513 and put all jump insns' LABEL_REFS onto the ref-chains
514 of their target labels. */
516 if (n_basic_blocks > 0)
518 int something_marked = 1;
521 /* Pass over all blocks, marking each block that is reachable
522 and has not yet been marked.
523 Keep doing this until, in one pass, no blocks have been marked.
524 Then blocks_live and blocks_marked are identical and correct.
525 In addition, all jumps actually reachable have been marked. */
527 while (something_marked)
529 something_marked = 0;
530 for (i = 0; i < n_basic_blocks; i++)
531 if (block_live[i] && !block_marked[i])
534 something_marked = 1;
535 if (i + 1 < n_basic_blocks && basic_block_drops_in[i + 1])
536 block_live[i + 1] = 1;
537 insn = basic_block_end[i];
538 if (GET_CODE (insn) == JUMP_INSN)
539 mark_label_ref (PATTERN (insn), insn, 0);
541 /* If we have any forced labels, mark them as potentially
542 reachable from this block. */
543 for (x = forced_labels; x; x = XEXP (x, 1))
544 if (! LABEL_REF_NONLOCAL_P (x))
545 mark_label_ref (gen_rtx (LABEL_REF, VOIDmode, XEXP (x, 0)),
548 /* Now scan the insns for this block, we may need to make
549 edges for some of them to various non-obvious locations
550 (exception handlers, nonlocal labels, etc). */
551 for (insn = basic_block_head[i];
552 insn != NEXT_INSN (basic_block_end[i]);
553 insn = NEXT_INSN (insn))
555 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
558 /* References to labels in non-jumping insns have
559 REG_LABEL notes attached to them.
561 This can happen for computed gotos; we don't care
562 about them here since the values are also on the
563 label_value_list and will be marked live if we find
564 a live computed goto.
566 This can also happen when we take the address of
567 a label to pass as an argument to __throw. Note
568 throw only uses the value to determine what handler
569 should be called -- ie the label is not used as
570 a jump target, it just marks regions in the code.
572 In theory we should be able to ignore the REG_LABEL
573 notes, but we have to make sure that the label and
574 associated insns aren't marked dead, so we make
575 the block in question live and create an edge from
576 this insn to the label. This is not strictly
577 correct, but it is close enough for now. */
578 for (note = REG_NOTES (insn);
580 note = XEXP (note, 1))
582 if (REG_NOTE_KIND (note) == REG_LABEL)
585 block_live[BLOCK_NUM (x)] = 1;
586 mark_label_ref (gen_rtx (LABEL_REF,
592 /* If this is a computed jump, then mark it as
593 reaching everything on the label_value_list
594 and forced_labels list. */
595 if (computed_jump_p (insn))
597 for (x = label_value_list; x; x = XEXP (x, 1))
598 mark_label_ref (gen_rtx (LABEL_REF, VOIDmode,
602 for (x = forced_labels; x; x = XEXP (x, 1))
603 mark_label_ref (gen_rtx (LABEL_REF, VOIDmode,
608 /* If this is a CALL_INSN, then mark it as reaching
609 the active EH handler for this CALL_INSN. If
610 we're handling asynchronous exceptions mark every
611 insn as reaching the active EH handler.
613 Also mark the CALL_INSN as reaching any nonlocal
615 else if (asynchronous_exceptions
616 || (GET_CODE (insn) == CALL_INSN
617 && ! find_reg_note (insn, REG_RETVAL,
620 if (active_eh_handler[INSN_UID (insn)])
621 mark_label_ref (gen_rtx (LABEL_REF, VOIDmode,
622 active_eh_handler[INSN_UID (insn)]),
625 if (!asynchronous_exceptions)
627 for (x = nonlocal_label_list;
630 mark_label_ref (gen_rtx (LABEL_REF, VOIDmode,
634 /* ??? This could be made smarter:
635 in some cases it's possible to tell that
636 certain calls will not do a nonlocal goto.
638 For example, if the nested functions that
639 do the nonlocal gotos do not have their
640 addresses taken, then only calls to those
641 functions or to other nested functions that
642 use them could possibly do nonlocal gotos. */
649 /* This should never happen. If it does that means we've computed an
650 incorrect flow graph, which can lead to aborts/crashes later in the
651 compiler or incorrect code generation.
653 We used to try and continue here, but that's just asking for trouble
654 later during the compile or at runtime. It's easier to debug the
655 problem here than later! */
656 for (i = 1; i < n_basic_blocks; i++)
657 if (block_live[i] && ! basic_block_drops_in[i]
658 && GET_CODE (basic_block_head[i]) == CODE_LABEL
659 && LABEL_REFS (basic_block_head[i]) == basic_block_head[i])
662 /* Now delete the code for any basic blocks that can't be reached.
663 They can occur because jump_optimize does not recognize
664 unreachable loops as unreachable. */
667 for (i = 0; i < n_basic_blocks; i++)
672 /* Delete the insns in a (non-live) block. We physically delete
673 every non-note insn except the start and end (so
674 basic_block_head/end needn't be updated), we turn the latter
675 into NOTE_INSN_DELETED notes.
676 We use to "delete" the insns by turning them into notes, but
677 we may be deleting lots of insns that subsequent passes would
678 otherwise have to process. Secondly, lots of deleted blocks in
679 a row can really slow down propagate_block since it will
680 otherwise process insn-turned-notes multiple times when it
681 looks for loop begin/end notes. */
682 if (basic_block_head[i] != basic_block_end[i])
684 /* It would be quicker to delete all of these with a single
685 unchaining, rather than one at a time, but we need to keep
687 insn = NEXT_INSN (basic_block_head[i]);
688 while (insn != basic_block_end[i])
690 if (GET_CODE (insn) == BARRIER)
692 else if (GET_CODE (insn) != NOTE)
693 insn = flow_delete_insn (insn);
695 insn = NEXT_INSN (insn);
698 insn = basic_block_head[i];
699 if (GET_CODE (insn) != NOTE)
701 /* Turn the head into a deleted insn note. */
702 if (GET_CODE (insn) == BARRIER)
705 /* If the head of this block is a CODE_LABEL, then it might
706 be the label for an exception handler which can't be
709 We need to remove the label from the exception_handler_label
710 list and remove the associated NOTE_EH_REGION_BEG and
711 NOTE_EH_REGION_END notes. */
712 if (GET_CODE (insn) == CODE_LABEL)
714 rtx x, *prev = &exception_handler_labels;
716 for (x = exception_handler_labels; x; x = XEXP (x, 1))
718 if (XEXP (x, 0) == insn)
720 /* Found a match, splice this label out of the
723 XEXP (x, 1) = NULL_RTX;
724 XEXP (x, 0) = NULL_RTX;
726 /* Now we have to find the EH_BEG and EH_END notes
727 associated with this label and remove them. */
729 for (x = get_insns (); x; x = NEXT_INSN (x))
731 if (GET_CODE (x) == NOTE
732 && ((NOTE_LINE_NUMBER (x)
733 == NOTE_INSN_EH_REGION_BEG)
734 || (NOTE_LINE_NUMBER (x)
735 == NOTE_INSN_EH_REGION_END))
736 && (NOTE_BLOCK_NUMBER (x)
737 == CODE_LABEL_NUMBER (insn)))
739 NOTE_LINE_NUMBER (x) = NOTE_INSN_DELETED;
740 NOTE_SOURCE_FILE (x) = 0;
749 PUT_CODE (insn, NOTE);
750 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
751 NOTE_SOURCE_FILE (insn) = 0;
753 insn = basic_block_end[i];
754 if (GET_CODE (insn) != NOTE)
756 /* Turn the tail into a deleted insn note. */
757 if (GET_CODE (insn) == BARRIER)
759 PUT_CODE (insn, NOTE);
760 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
761 NOTE_SOURCE_FILE (insn) = 0;
763 /* BARRIERs are between basic blocks, not part of one.
764 Delete a BARRIER if the preceding jump is deleted.
765 We cannot alter a BARRIER into a NOTE
766 because it is too short; but we can really delete
767 it because it is not part of a basic block. */
768 if (NEXT_INSN (insn) != 0
769 && GET_CODE (NEXT_INSN (insn)) == BARRIER)
770 delete_insn (NEXT_INSN (insn));
772 /* Each time we delete some basic blocks,
773 see if there is a jump around them that is
774 being turned into a no-op. If so, delete it. */
776 if (block_live[i - 1])
779 for (j = i + 1; j < n_basic_blocks; j++)
783 insn = basic_block_end[i - 1];
784 if (GET_CODE (insn) == JUMP_INSN
785 /* An unconditional jump is the only possibility
786 we must check for, since a conditional one
787 would make these blocks live. */
788 && simplejump_p (insn)
789 && (label = XEXP (SET_SRC (PATTERN (insn)), 0), 1)
790 && INSN_UID (label) != 0
791 && BLOCK_NUM (label) == j)
793 PUT_CODE (insn, NOTE);
794 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
795 NOTE_SOURCE_FILE (insn) = 0;
796 if (GET_CODE (NEXT_INSN (insn)) != BARRIER)
798 delete_insn (NEXT_INSN (insn));
805 /* There are pathological cases where one function calling hundreds of
806 nested inline functions can generate lots and lots of unreachable
807 blocks that jump can't delete. Since we don't use sparse matrices
808 a lot of memory will be needed to compile such functions.
809 Implementing sparse matrices is a fair bit of work and it is not
810 clear that they win more than they lose (we don't want to
811 unnecessarily slow down compilation of normal code). By making
812 another pass for the pathological case, we can greatly speed up
813 their compilation without hurting normal code. This works because
814 all the insns in the unreachable blocks have either been deleted or
816 Note that we're talking about reducing memory usage by 10's of
817 megabytes and reducing compilation time by several minutes. */
818 /* ??? The choice of when to make another pass is a bit arbitrary,
819 and was derived from empirical data. */
824 n_basic_blocks -= deleted;
825 /* `n_basic_blocks' may not be correct at this point: two previously
826 separate blocks may now be merged. That's ok though as we
827 recalculate it during the second pass. It certainly can't be
828 any larger than the current value. */
834 /* Subroutines of find_basic_blocks. */
836 /* Check expression X for label references;
837 if one is found, add INSN to the label's chain of references.
839 CHECKDUP means check for and avoid creating duplicate references
840 from the same insn. Such duplicates do no serious harm but
841 can slow life analysis. CHECKDUP is set only when duplicates
845 mark_label_ref (x, insn, checkdup)
849 register RTX_CODE code;
853 /* We can be called with NULL when scanning label_value_list. */
858 if (code == LABEL_REF)
860 register rtx label = XEXP (x, 0);
862 if (GET_CODE (label) != CODE_LABEL)
864 /* If the label was never emitted, this insn is junk,
865 but avoid a crash trying to refer to BLOCK_NUM (label).
866 This can happen as a result of a syntax error
867 and a diagnostic has already been printed. */
868 if (INSN_UID (label) == 0)
870 CONTAINING_INSN (x) = insn;
871 /* if CHECKDUP is set, check for duplicate ref from same insn
874 for (y = LABEL_REFS (label); y != label; y = LABEL_NEXTREF (y))
875 if (CONTAINING_INSN (y) == insn)
877 LABEL_NEXTREF (x) = LABEL_REFS (label);
878 LABEL_REFS (label) = x;
879 block_live_static[BLOCK_NUM (label)] = 1;
883 fmt = GET_RTX_FORMAT (code);
884 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
887 mark_label_ref (XEXP (x, i), insn, 0);
891 for (j = 0; j < XVECLEN (x, i); j++)
892 mark_label_ref (XVECEXP (x, i, j), insn, 1);
897 /* Delete INSN by patching it out.
898 Return the next insn. */
901 flow_delete_insn (insn)
904 /* ??? For the moment we assume we don't have to watch for NULLs here
905 since the start/end of basic blocks aren't deleted like this. */
906 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
907 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
908 return NEXT_INSN (insn);
911 /* Determine which registers are live at the start of each
912 basic block of the function whose first insn is F.
913 NREGS is the number of registers used in F.
914 We allocate the vector basic_block_live_at_start
915 and the regsets that it points to, and fill them with the data.
916 regset_size and regset_bytes are also set here. */
919 life_analysis (f, nregs)
925 /* For each basic block, a bitmask of regs
926 live on exit from the block. */
927 regset *basic_block_live_at_end;
928 /* For each basic block, a bitmask of regs
929 live on entry to a successor-block of this block.
930 If this does not match basic_block_live_at_end,
931 that must be updated, and the block must be rescanned. */
932 regset *basic_block_new_live_at_end;
933 /* For each basic block, a bitmask of regs
934 whose liveness at the end of the basic block
935 can make a difference in which regs are live on entry to the block.
936 These are the regs that are set within the basic block,
937 possibly excluding those that are used after they are set. */
938 regset *basic_block_significant;
942 struct obstack flow_obstack;
944 gcc_obstack_init (&flow_obstack);
948 bzero (regs_ever_live, sizeof regs_ever_live);
950 /* Allocate and zero out many data structures
951 that will record the data from lifetime analysis. */
953 allocate_for_life_analysis ();
955 reg_next_use = (rtx *) alloca (nregs * sizeof (rtx));
956 bzero ((char *) reg_next_use, nregs * sizeof (rtx));
958 /* Set up several regset-vectors used internally within this function.
959 Their meanings are documented above, with their declarations. */
961 basic_block_live_at_end
962 = (regset *) alloca (n_basic_blocks * sizeof (regset));
964 /* Don't use alloca since that leads to a crash rather than an error message
965 if there isn't enough space.
966 Don't use oballoc since we may need to allocate other things during
967 this function on the temporary obstack. */
968 init_regset_vector (basic_block_live_at_end, n_basic_blocks, &flow_obstack);
970 basic_block_new_live_at_end
971 = (regset *) alloca (n_basic_blocks * sizeof (regset));
972 init_regset_vector (basic_block_new_live_at_end, n_basic_blocks,
975 basic_block_significant
976 = (regset *) alloca (n_basic_blocks * sizeof (regset));
977 init_regset_vector (basic_block_significant, n_basic_blocks, &flow_obstack);
979 /* Record which insns refer to any volatile memory
980 or for any reason can't be deleted just because they are dead stores.
981 Also, delete any insns that copy a register to itself. */
983 for (insn = f; insn; insn = NEXT_INSN (insn))
985 enum rtx_code code1 = GET_CODE (insn);
986 if (code1 == CALL_INSN)
987 INSN_VOLATILE (insn) = 1;
988 else if (code1 == INSN || code1 == JUMP_INSN)
990 /* Delete (in effect) any obvious no-op moves. */
991 if (GET_CODE (PATTERN (insn)) == SET
992 && GET_CODE (SET_DEST (PATTERN (insn))) == REG
993 && GET_CODE (SET_SRC (PATTERN (insn))) == REG
994 && (REGNO (SET_DEST (PATTERN (insn)))
995 == REGNO (SET_SRC (PATTERN (insn))))
996 /* Insns carrying these notes are useful later on. */
997 && ! find_reg_note (insn, REG_EQUAL, NULL_RTX))
999 PUT_CODE (insn, NOTE);
1000 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1001 NOTE_SOURCE_FILE (insn) = 0;
1003 /* Delete (in effect) any obvious no-op moves. */
1004 else if (GET_CODE (PATTERN (insn)) == SET
1005 && GET_CODE (SET_DEST (PATTERN (insn))) == SUBREG
1006 && GET_CODE (SUBREG_REG (SET_DEST (PATTERN (insn)))) == REG
1007 && GET_CODE (SET_SRC (PATTERN (insn))) == SUBREG
1008 && GET_CODE (SUBREG_REG (SET_SRC (PATTERN (insn)))) == REG
1009 && (REGNO (SUBREG_REG (SET_DEST (PATTERN (insn))))
1010 == REGNO (SUBREG_REG (SET_SRC (PATTERN (insn)))))
1011 && SUBREG_WORD (SET_DEST (PATTERN (insn))) ==
1012 SUBREG_WORD (SET_SRC (PATTERN (insn)))
1013 /* Insns carrying these notes are useful later on. */
1014 && ! find_reg_note (insn, REG_EQUAL, NULL_RTX))
1016 PUT_CODE (insn, NOTE);
1017 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1018 NOTE_SOURCE_FILE (insn) = 0;
1020 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
1022 /* If nothing but SETs of registers to themselves,
1023 this insn can also be deleted. */
1024 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1026 rtx tem = XVECEXP (PATTERN (insn), 0, i);
1028 if (GET_CODE (tem) == USE
1029 || GET_CODE (tem) == CLOBBER)
1032 if (GET_CODE (tem) != SET
1033 || GET_CODE (SET_DEST (tem)) != REG
1034 || GET_CODE (SET_SRC (tem)) != REG
1035 || REGNO (SET_DEST (tem)) != REGNO (SET_SRC (tem)))
1039 if (i == XVECLEN (PATTERN (insn), 0)
1040 /* Insns carrying these notes are useful later on. */
1041 && ! find_reg_note (insn, REG_EQUAL, NULL_RTX))
1043 PUT_CODE (insn, NOTE);
1044 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1045 NOTE_SOURCE_FILE (insn) = 0;
1048 INSN_VOLATILE (insn) = volatile_refs_p (PATTERN (insn));
1050 else if (GET_CODE (PATTERN (insn)) != USE)
1051 INSN_VOLATILE (insn) = volatile_refs_p (PATTERN (insn));
1052 /* A SET that makes space on the stack cannot be dead.
1053 (Such SETs occur only for allocating variable-size data,
1054 so they will always have a PLUS or MINUS according to the
1055 direction of stack growth.)
1056 Even if this function never uses this stack pointer value,
1057 signal handlers do! */
1058 else if (code1 == INSN && GET_CODE (PATTERN (insn)) == SET
1059 && SET_DEST (PATTERN (insn)) == stack_pointer_rtx
1060 #ifdef STACK_GROWS_DOWNWARD
1061 && GET_CODE (SET_SRC (PATTERN (insn))) == MINUS
1063 && GET_CODE (SET_SRC (PATTERN (insn))) == PLUS
1065 && XEXP (SET_SRC (PATTERN (insn)), 0) == stack_pointer_rtx)
1066 INSN_VOLATILE (insn) = 1;
1070 if (n_basic_blocks > 0)
1071 #ifdef EXIT_IGNORE_STACK
1072 if (! EXIT_IGNORE_STACK
1073 || (! FRAME_POINTER_REQUIRED && flag_omit_frame_pointer))
1076 /* If exiting needs the right stack value,
1077 consider the stack pointer live at the end of the function. */
1078 SET_REGNO_REG_SET (basic_block_live_at_end[n_basic_blocks - 1],
1079 STACK_POINTER_REGNUM);
1080 SET_REGNO_REG_SET (basic_block_new_live_at_end[n_basic_blocks - 1],
1081 STACK_POINTER_REGNUM);
1084 /* Mark the frame pointer is needed at the end of the function. If
1085 we end up eliminating it, it will be removed from the live list
1086 of each basic block by reload. */
1088 if (n_basic_blocks > 0)
1090 SET_REGNO_REG_SET (basic_block_live_at_end[n_basic_blocks - 1],
1091 FRAME_POINTER_REGNUM);
1092 SET_REGNO_REG_SET (basic_block_new_live_at_end[n_basic_blocks - 1],
1093 FRAME_POINTER_REGNUM);
1094 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
1095 /* If they are different, also mark the hard frame pointer as live */
1096 SET_REGNO_REG_SET (basic_block_live_at_end[n_basic_blocks - 1],
1097 HARD_FRAME_POINTER_REGNUM);
1098 SET_REGNO_REG_SET (basic_block_new_live_at_end[n_basic_blocks - 1],
1099 HARD_FRAME_POINTER_REGNUM);
1103 /* Mark all global registers and all registers used by the epilogue
1104 as being live at the end of the function since they may be
1105 referenced by our caller. */
1107 if (n_basic_blocks > 0)
1108 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1110 #ifdef EPILOGUE_USES
1111 || EPILOGUE_USES (i)
1115 SET_REGNO_REG_SET (basic_block_live_at_end[n_basic_blocks - 1], i);
1116 SET_REGNO_REG_SET (basic_block_new_live_at_end[n_basic_blocks - 1], i);
1119 /* Propagate life info through the basic blocks
1120 around the graph of basic blocks.
1122 This is a relaxation process: each time a new register
1123 is live at the end of the basic block, we must scan the block
1124 to determine which registers are, as a consequence, live at the beginning
1125 of that block. These registers must then be marked live at the ends
1126 of all the blocks that can transfer control to that block.
1127 The process continues until it reaches a fixed point. */
1134 for (i = n_basic_blocks - 1; i >= 0; i--)
1136 int consider = first_pass;
1137 int must_rescan = first_pass;
1142 /* Set CONSIDER if this block needs thinking about at all
1143 (that is, if the regs live now at the end of it
1144 are not the same as were live at the end of it when
1145 we last thought about it).
1146 Set must_rescan if it needs to be thought about
1147 instruction by instruction (that is, if any additional
1148 reg that is live at the end now but was not live there before
1149 is one of the significant regs of this basic block). */
1151 EXECUTE_IF_AND_COMPL_IN_REG_SET
1152 (basic_block_new_live_at_end[i],
1153 basic_block_live_at_end[i], 0, j,
1156 if (REGNO_REG_SET_P (basic_block_significant[i], j))
1167 /* The live_at_start of this block may be changing,
1168 so another pass will be required after this one. */
1173 /* No complete rescan needed;
1174 just record those variables newly known live at end
1175 as live at start as well. */
1176 IOR_AND_COMPL_REG_SET (basic_block_live_at_start[i],
1177 basic_block_new_live_at_end[i],
1178 basic_block_live_at_end[i]);
1180 IOR_AND_COMPL_REG_SET (basic_block_live_at_end[i],
1181 basic_block_new_live_at_end[i],
1182 basic_block_live_at_end[i]);
1186 /* Update the basic_block_live_at_start
1187 by propagation backwards through the block. */
1188 COPY_REG_SET (basic_block_live_at_end[i],
1189 basic_block_new_live_at_end[i]);
1190 COPY_REG_SET (basic_block_live_at_start[i],
1191 basic_block_live_at_end[i]);
1192 propagate_block (basic_block_live_at_start[i],
1193 basic_block_head[i], basic_block_end[i], 0,
1194 first_pass ? basic_block_significant[i]
1200 register rtx jump, head;
1202 /* Update the basic_block_new_live_at_end's of the block
1203 that falls through into this one (if any). */
1204 head = basic_block_head[i];
1205 if (basic_block_drops_in[i])
1206 IOR_REG_SET (basic_block_new_live_at_end[i-1],
1207 basic_block_live_at_start[i]);
1209 /* Update the basic_block_new_live_at_end's of
1210 all the blocks that jump to this one. */
1211 if (GET_CODE (head) == CODE_LABEL)
1212 for (jump = LABEL_REFS (head);
1214 jump = LABEL_NEXTREF (jump))
1216 register int from_block = BLOCK_NUM (CONTAINING_INSN (jump));
1217 IOR_REG_SET (basic_block_new_live_at_end[from_block],
1218 basic_block_live_at_start[i]);
1228 /* The only pseudos that are live at the beginning of the function are
1229 those that were not set anywhere in the function. local-alloc doesn't
1230 know how to handle these correctly, so mark them as not local to any
1233 if (n_basic_blocks > 0)
1234 EXECUTE_IF_SET_IN_REG_SET (basic_block_live_at_start[0],
1235 FIRST_PSEUDO_REGISTER, i,
1237 REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL;
1240 /* Now the life information is accurate.
1241 Make one more pass over each basic block
1242 to delete dead stores, create autoincrement addressing
1243 and record how many times each register is used, is set, or dies.
1245 To save time, we operate directly in basic_block_live_at_end[i],
1246 thus destroying it (in fact, converting it into a copy of
1247 basic_block_live_at_start[i]). This is ok now because
1248 basic_block_live_at_end[i] is no longer used past this point. */
1252 for (i = 0; i < n_basic_blocks; i++)
1254 propagate_block (basic_block_live_at_end[i],
1255 basic_block_head[i], basic_block_end[i], 1,
1263 /* Something live during a setjmp should not be put in a register
1264 on certain machines which restore regs from stack frames
1265 rather than from the jmpbuf.
1266 But we don't need to do this for the user's variables, since
1267 ANSI says only volatile variables need this. */
1268 #ifdef LONGJMP_RESTORE_FROM_STACK
1269 EXECUTE_IF_SET_IN_REG_SET (regs_live_at_setjmp,
1270 FIRST_PSEUDO_REGISTER, i,
1272 if (regno_reg_rtx[i] != 0
1273 && ! REG_USERVAR_P (regno_reg_rtx[i]))
1275 REG_LIVE_LENGTH (i) = -1;
1276 REG_BASIC_BLOCK (i) = -1;
1282 /* We have a problem with any pseudoreg that
1283 lives across the setjmp. ANSI says that if a
1284 user variable does not change in value
1285 between the setjmp and the longjmp, then the longjmp preserves it.
1286 This includes longjmp from a place where the pseudo appears dead.
1287 (In principle, the value still exists if it is in scope.)
1288 If the pseudo goes in a hard reg, some other value may occupy
1289 that hard reg where this pseudo is dead, thus clobbering the pseudo.
1290 Conclusion: such a pseudo must not go in a hard reg. */
1291 EXECUTE_IF_SET_IN_REG_SET (regs_live_at_setjmp,
1292 FIRST_PSEUDO_REGISTER, i,
1294 if (regno_reg_rtx[i] != 0)
1296 REG_LIVE_LENGTH (i) = -1;
1297 REG_BASIC_BLOCK (i) = -1;
1302 free_regset_vector (basic_block_live_at_end, n_basic_blocks);
1303 free_regset_vector (basic_block_new_live_at_end, n_basic_blocks);
1304 free_regset_vector (basic_block_significant, n_basic_blocks);
1305 basic_block_live_at_end = (regset *)0;
1306 basic_block_new_live_at_end = (regset *)0;
1307 basic_block_significant = (regset *)0;
1309 obstack_free (&flow_obstack, NULL_PTR);
1312 /* Subroutines of life analysis. */
1314 /* Allocate the permanent data structures that represent the results
1315 of life analysis. Not static since used also for stupid life analysis. */
1318 allocate_for_life_analysis ()
1322 /* Recalculate the register space, in case it has grown. Old style
1323 vector oriented regsets would set regset_{size,bytes} here also. */
1324 allocate_reg_info (max_regno, FALSE, FALSE);
1326 /* Because both reg_scan and flow_analysis want to set up the REG_N_SETS
1327 information, explicitly reset it here. The allocation should have
1328 already happened on the previous reg_scan pass. Make sure in case
1329 some more registers were allocated. */
1330 for (i = 0; i < max_regno; i++)
1333 basic_block_live_at_start
1334 = (regset *) oballoc (n_basic_blocks * sizeof (regset));
1335 init_regset_vector (basic_block_live_at_start, n_basic_blocks,
1338 regs_live_at_setjmp = OBSTACK_ALLOC_REG_SET (function_obstack);
1339 CLEAR_REG_SET (regs_live_at_setjmp);
1342 /* Make each element of VECTOR point at a regset. The vector has
1343 NELTS elements, and space is allocated from the ALLOC_OBSTACK
1347 init_regset_vector (vector, nelts, alloc_obstack)
1350 struct obstack *alloc_obstack;
1354 for (i = 0; i < nelts; i++)
1356 vector[i] = OBSTACK_ALLOC_REG_SET (alloc_obstack);
1357 CLEAR_REG_SET (vector[i]);
1361 /* Release any additional space allocated for each element of VECTOR point
1362 other than the regset header itself. The vector has NELTS elements. */
1365 free_regset_vector (vector, nelts)
1371 for (i = 0; i < nelts; i++)
1372 FREE_REG_SET (vector[i]);
1375 /* Compute the registers live at the beginning of a basic block
1376 from those live at the end.
1378 When called, OLD contains those live at the end.
1379 On return, it contains those live at the beginning.
1380 FIRST and LAST are the first and last insns of the basic block.
1382 FINAL is nonzero if we are doing the final pass which is not
1383 for computing the life info (since that has already been done)
1384 but for acting on it. On this pass, we delete dead stores,
1385 set up the logical links and dead-variables lists of instructions,
1386 and merge instructions for autoincrement and autodecrement addresses.
1388 SIGNIFICANT is nonzero only the first time for each basic block.
1389 If it is nonzero, it points to a regset in which we store
1390 a 1 for each register that is set within the block.
1392 BNUM is the number of the basic block. */
1395 propagate_block (old, first, last, final, significant, bnum)
1396 register regset old;
1408 /* The following variables are used only if FINAL is nonzero. */
1409 /* This vector gets one element for each reg that has been live
1410 at any point in the basic block that has been scanned so far.
1411 SOMETIMES_MAX says how many elements are in use so far. */
1412 register int *regs_sometimes_live;
1413 int sometimes_max = 0;
1414 /* This regset has 1 for each reg that we have seen live so far.
1415 It and REGS_SOMETIMES_LIVE are updated together. */
1418 /* The loop depth may change in the middle of a basic block. Since we
1419 scan from end to beginning, we start with the depth at the end of the
1420 current basic block, and adjust as we pass ends and starts of loops. */
1421 loop_depth = basic_block_loop_depth[bnum];
1423 dead = ALLOCA_REG_SET ();
1424 live = ALLOCA_REG_SET ();
1429 /* Include any notes at the end of the block in the scan.
1430 This is in case the block ends with a call to setjmp. */
1432 while (NEXT_INSN (last) != 0 && GET_CODE (NEXT_INSN (last)) == NOTE)
1434 /* Look for loop boundaries, we are going forward here. */
1435 last = NEXT_INSN (last);
1436 if (NOTE_LINE_NUMBER (last) == NOTE_INSN_LOOP_BEG)
1438 else if (NOTE_LINE_NUMBER (last) == NOTE_INSN_LOOP_END)
1447 maxlive = ALLOCA_REG_SET ();
1448 COPY_REG_SET (maxlive, old);
1449 regs_sometimes_live = (int *) alloca (max_regno * sizeof (int));
1451 /* Process the regs live at the end of the block.
1452 Enter them in MAXLIVE and REGS_SOMETIMES_LIVE.
1453 Also mark them as not local to any one basic block. */
1454 EXECUTE_IF_SET_IN_REG_SET (old, 0, i,
1456 REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL;
1457 regs_sometimes_live[sometimes_max] = i;
1462 /* Scan the block an insn at a time from end to beginning. */
1464 for (insn = last; ; insn = prev)
1466 prev = PREV_INSN (insn);
1468 if (GET_CODE (insn) == NOTE)
1470 /* Look for loop boundaries, remembering that we are going
1472 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
1474 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
1477 /* If we have LOOP_DEPTH == 0, there has been a bookkeeping error.
1478 Abort now rather than setting register status incorrectly. */
1479 if (loop_depth == 0)
1482 /* If this is a call to `setjmp' et al,
1483 warn if any non-volatile datum is live. */
1485 if (final && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
1486 IOR_REG_SET (regs_live_at_setjmp, old);
1489 /* Update the life-status of regs for this insn.
1490 First DEAD gets which regs are set in this insn
1491 then LIVE gets which regs are used in this insn.
1492 Then the regs live before the insn
1493 are those live after, with DEAD regs turned off,
1494 and then LIVE regs turned on. */
1496 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1499 rtx note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
1501 = (insn_dead_p (PATTERN (insn), old, 0)
1502 /* Don't delete something that refers to volatile storage! */
1503 && ! INSN_VOLATILE (insn));
1505 = (insn_is_dead && note != 0
1506 && libcall_dead_p (PATTERN (insn), old, note, insn));
1508 /* If an instruction consists of just dead store(s) on final pass,
1509 "delete" it by turning it into a NOTE of type NOTE_INSN_DELETED.
1510 We could really delete it with delete_insn, but that
1511 can cause trouble for first or last insn in a basic block. */
1512 if (final && insn_is_dead)
1514 PUT_CODE (insn, NOTE);
1515 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1516 NOTE_SOURCE_FILE (insn) = 0;
1518 /* CC0 is now known to be dead. Either this insn used it,
1519 in which case it doesn't anymore, or clobbered it,
1520 so the next insn can't use it. */
1523 /* If this insn is copying the return value from a library call,
1524 delete the entire library call. */
1525 if (libcall_is_dead)
1527 rtx first = XEXP (note, 0);
1529 while (INSN_DELETED_P (first))
1530 first = NEXT_INSN (first);
1535 NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
1536 NOTE_SOURCE_FILE (p) = 0;
1542 CLEAR_REG_SET (dead);
1543 CLEAR_REG_SET (live);
1545 /* See if this is an increment or decrement that can be
1546 merged into a following memory address. */
1549 register rtx x = single_set (insn);
1551 /* Does this instruction increment or decrement a register? */
1553 && GET_CODE (SET_DEST (x)) == REG
1554 && (GET_CODE (SET_SRC (x)) == PLUS
1555 || GET_CODE (SET_SRC (x)) == MINUS)
1556 && XEXP (SET_SRC (x), 0) == SET_DEST (x)
1557 && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
1558 /* Ok, look for a following memory ref we can combine with.
1559 If one is found, change the memory ref to a PRE_INC
1560 or PRE_DEC, cancel this insn, and return 1.
1561 Return 0 if nothing has been done. */
1562 && try_pre_increment_1 (insn))
1565 #endif /* AUTO_INC_DEC */
1567 /* If this is not the final pass, and this insn is copying the
1568 value of a library call and it's dead, don't scan the
1569 insns that perform the library call, so that the call's
1570 arguments are not marked live. */
1571 if (libcall_is_dead)
1573 /* Mark the dest reg as `significant'. */
1574 mark_set_regs (old, dead, PATTERN (insn), NULL_RTX, significant);
1576 insn = XEXP (note, 0);
1577 prev = PREV_INSN (insn);
1579 else if (GET_CODE (PATTERN (insn)) == SET
1580 && SET_DEST (PATTERN (insn)) == stack_pointer_rtx
1581 && GET_CODE (SET_SRC (PATTERN (insn))) == PLUS
1582 && XEXP (SET_SRC (PATTERN (insn)), 0) == stack_pointer_rtx
1583 && GET_CODE (XEXP (SET_SRC (PATTERN (insn)), 1)) == CONST_INT)
1584 /* We have an insn to pop a constant amount off the stack.
1585 (Such insns use PLUS regardless of the direction of the stack,
1586 and any insn to adjust the stack by a constant is always a pop.)
1587 These insns, if not dead stores, have no effect on life. */
1591 /* LIVE gets the regs used in INSN;
1592 DEAD gets those set by it. Dead insns don't make anything
1595 mark_set_regs (old, dead, PATTERN (insn),
1596 final ? insn : NULL_RTX, significant);
1598 /* If an insn doesn't use CC0, it becomes dead since we
1599 assume that every insn clobbers it. So show it dead here;
1600 mark_used_regs will set it live if it is referenced. */
1604 mark_used_regs (old, live, PATTERN (insn), final, insn);
1606 /* Sometimes we may have inserted something before INSN (such as
1607 a move) when we make an auto-inc. So ensure we will scan
1610 prev = PREV_INSN (insn);
1613 if (! insn_is_dead && GET_CODE (insn) == CALL_INSN)
1619 for (note = CALL_INSN_FUNCTION_USAGE (insn);
1621 note = XEXP (note, 1))
1622 if (GET_CODE (XEXP (note, 0)) == USE)
1623 mark_used_regs (old, live, SET_DEST (XEXP (note, 0)),
1626 /* Each call clobbers all call-clobbered regs that are not
1627 global or fixed. Note that the function-value reg is a
1628 call-clobbered reg, and mark_set_regs has already had
1629 a chance to handle it. */
1631 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1632 if (call_used_regs[i] && ! global_regs[i]
1634 SET_REGNO_REG_SET (dead, i);
1636 /* The stack ptr is used (honorarily) by a CALL insn. */
1637 SET_REGNO_REG_SET (live, STACK_POINTER_REGNUM);
1639 /* Calls may also reference any of the global registers,
1640 so they are made live. */
1641 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1643 mark_used_regs (old, live,
1644 gen_rtx (REG, reg_raw_mode[i], i),
1647 /* Calls also clobber memory. */
1651 /* Update OLD for the registers used or set. */
1652 AND_COMPL_REG_SET (old, dead);
1653 IOR_REG_SET (old, live);
1655 if (GET_CODE (insn) == CALL_INSN && final)
1657 /* Any regs live at the time of a call instruction
1658 must not go in a register clobbered by calls.
1659 Find all regs now live and record this for them. */
1661 register int *p = regs_sometimes_live;
1663 for (i = 0; i < sometimes_max; i++, p++)
1664 if (REGNO_REG_SET_P (old, *p))
1665 REG_N_CALLS_CROSSED (*p)++;
1669 /* On final pass, add any additional sometimes-live regs
1670 into MAXLIVE and REGS_SOMETIMES_LIVE.
1671 Also update counts of how many insns each reg is live at. */
1678 EXECUTE_IF_AND_COMPL_IN_REG_SET
1679 (live, maxlive, 0, regno,
1681 regs_sometimes_live[sometimes_max++] = regno;
1682 SET_REGNO_REG_SET (maxlive, regno);
1685 p = regs_sometimes_live;
1686 for (i = 0; i < sometimes_max; i++)
1689 if (REGNO_REG_SET_P (old, regno))
1690 REG_LIVE_LENGTH (regno)++;
1699 FREE_REG_SET (dead);
1700 FREE_REG_SET (live);
1702 FREE_REG_SET (maxlive);
1704 if (num_scratch > max_scratch)
1705 max_scratch = num_scratch;
1708 /* Return 1 if X (the body of an insn, or part of it) is just dead stores
1709 (SET expressions whose destinations are registers dead after the insn).
1710 NEEDED is the regset that says which regs are alive after the insn.
1712 Unless CALL_OK is non-zero, an insn is needed if it contains a CALL. */
1715 insn_dead_p (x, needed, call_ok)
1720 register RTX_CODE code = GET_CODE (x);
1721 /* If setting something that's a reg or part of one,
1722 see if that register's altered value will be live. */
1726 register rtx r = SET_DEST (x);
1727 /* A SET that is a subroutine call cannot be dead. */
1728 if (! call_ok && GET_CODE (SET_SRC (x)) == CALL)
1732 if (GET_CODE (r) == CC0)
1736 if (GET_CODE (r) == MEM && last_mem_set && ! MEM_VOLATILE_P (r)
1737 && rtx_equal_p (r, last_mem_set))
1740 while (GET_CODE (r) == SUBREG
1741 || GET_CODE (r) == STRICT_LOW_PART
1742 || GET_CODE (r) == ZERO_EXTRACT
1743 || GET_CODE (r) == SIGN_EXTRACT)
1746 if (GET_CODE (r) == REG)
1748 register int regno = REGNO (r);
1750 /* Don't delete insns to set global regs. */
1751 if ((regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
1752 /* Make sure insns to set frame pointer aren't deleted. */
1753 || regno == FRAME_POINTER_REGNUM
1754 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
1755 || regno == HARD_FRAME_POINTER_REGNUM
1757 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1758 /* Make sure insns to set arg pointer are never deleted
1759 (if the arg pointer isn't fixed, there will be a USE for
1760 it, so we can treat it normally). */
1761 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
1763 || REGNO_REG_SET_P (needed, regno))
1766 /* If this is a hard register, verify that subsequent words are
1768 if (regno < FIRST_PSEUDO_REGISTER)
1770 int n = HARD_REGNO_NREGS (regno, GET_MODE (r));
1773 if (REGNO_REG_SET_P (needed, regno+n))
1780 /* If performing several activities,
1781 insn is dead if each activity is individually dead.
1782 Also, CLOBBERs and USEs can be ignored; a CLOBBER or USE
1783 that's inside a PARALLEL doesn't make the insn worth keeping. */
1784 else if (code == PARALLEL)
1786 register int i = XVECLEN (x, 0);
1787 for (i--; i >= 0; i--)
1789 rtx elt = XVECEXP (x, 0, i);
1790 if (!insn_dead_p (elt, needed, call_ok)
1791 && GET_CODE (elt) != CLOBBER
1792 && GET_CODE (elt) != USE)
1797 /* We do not check CLOBBER or USE here.
1798 An insn consisting of just a CLOBBER or just a USE
1799 should not be deleted. */
1803 /* If X is the pattern of the last insn in a libcall, and assuming X is dead,
1804 return 1 if the entire library call is dead.
1805 This is true if X copies a register (hard or pseudo)
1806 and if the hard return reg of the call insn is dead.
1807 (The caller should have tested the destination of X already for death.)
1809 If this insn doesn't just copy a register, then we don't
1810 have an ordinary libcall. In that case, cse could not have
1811 managed to substitute the source for the dest later on,
1812 so we can assume the libcall is dead.
1814 NEEDED is the bit vector of pseudoregs live before this insn.
1815 NOTE is the REG_RETVAL note of the insn. INSN is the insn itself. */
1818 libcall_dead_p (x, needed, note, insn)
1824 register RTX_CODE code = GET_CODE (x);
1828 register rtx r = SET_SRC (x);
1829 if (GET_CODE (r) == REG)
1831 rtx call = XEXP (note, 0);
1834 /* Find the call insn. */
1835 while (call != insn && GET_CODE (call) != CALL_INSN)
1836 call = NEXT_INSN (call);
1838 /* If there is none, do nothing special,
1839 since ordinary death handling can understand these insns. */
1843 /* See if the hard reg holding the value is dead.
1844 If this is a PARALLEL, find the call within it. */
1845 call = PATTERN (call);
1846 if (GET_CODE (call) == PARALLEL)
1848 for (i = XVECLEN (call, 0) - 1; i >= 0; i--)
1849 if (GET_CODE (XVECEXP (call, 0, i)) == SET
1850 && GET_CODE (SET_SRC (XVECEXP (call, 0, i))) == CALL)
1853 /* This may be a library call that is returning a value
1854 via invisible pointer. Do nothing special, since
1855 ordinary death handling can understand these insns. */
1859 call = XVECEXP (call, 0, i);
1862 return insn_dead_p (call, needed, 1);
1868 /* Return 1 if register REGNO was used before it was set.
1869 In other words, if it is live at function entry.
1870 Don't count global register variables or variables in registers
1871 that can be used for function arg passing, though. */
1874 regno_uninitialized (regno)
1877 if (n_basic_blocks == 0
1878 || (regno < FIRST_PSEUDO_REGISTER
1879 && (global_regs[regno] || FUNCTION_ARG_REGNO_P (regno))))
1882 return REGNO_REG_SET_P (basic_block_live_at_start[0], regno);
1885 /* 1 if register REGNO was alive at a place where `setjmp' was called
1886 and was set more than once or is an argument.
1887 Such regs may be clobbered by `longjmp'. */
1890 regno_clobbered_at_setjmp (regno)
1893 if (n_basic_blocks == 0)
1896 return ((REG_N_SETS (regno) > 1
1897 || REGNO_REG_SET_P (basic_block_live_at_start[0], regno))
1898 && REGNO_REG_SET_P (regs_live_at_setjmp, regno));
1901 /* Process the registers that are set within X.
1902 Their bits are set to 1 in the regset DEAD,
1903 because they are dead prior to this insn.
1905 If INSN is nonzero, it is the insn being processed
1906 and the fact that it is nonzero implies this is the FINAL pass
1907 in propagate_block. In this case, various info about register
1908 usage is stored, LOG_LINKS fields of insns are set up. */
1911 mark_set_regs (needed, dead, x, insn, significant)
1918 register RTX_CODE code = GET_CODE (x);
1920 if (code == SET || code == CLOBBER)
1921 mark_set_1 (needed, dead, x, insn, significant);
1922 else if (code == PARALLEL)
1925 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1927 code = GET_CODE (XVECEXP (x, 0, i));
1928 if (code == SET || code == CLOBBER)
1929 mark_set_1 (needed, dead, XVECEXP (x, 0, i), insn, significant);
1934 /* Process a single SET rtx, X. */
1937 mark_set_1 (needed, dead, x, insn, significant)
1945 register rtx reg = SET_DEST (x);
1947 /* Modifying just one hardware register of a multi-reg value
1948 or just a byte field of a register
1949 does not mean the value from before this insn is now dead.
1950 But it does mean liveness of that register at the end of the block
1953 Within mark_set_1, however, we treat it as if the register is
1954 indeed modified. mark_used_regs will, however, also treat this
1955 register as being used. Thus, we treat these insns as setting a
1956 new value for the register as a function of its old value. This
1957 cases LOG_LINKS to be made appropriately and this will help combine. */
1959 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
1960 || GET_CODE (reg) == SIGN_EXTRACT
1961 || GET_CODE (reg) == STRICT_LOW_PART)
1962 reg = XEXP (reg, 0);
1964 /* If we are writing into memory or into a register mentioned in the
1965 address of the last thing stored into memory, show we don't know
1966 what the last store was. If we are writing memory, save the address
1967 unless it is volatile. */
1968 if (GET_CODE (reg) == MEM
1969 || (GET_CODE (reg) == REG
1970 && last_mem_set != 0 && reg_overlap_mentioned_p (reg, last_mem_set)))
1973 if (GET_CODE (reg) == MEM && ! side_effects_p (reg)
1974 /* There are no REG_INC notes for SP, so we can't assume we'll see
1975 everything that invalidates it. To be safe, don't eliminate any
1976 stores though SP; none of them should be redundant anyway. */
1977 && ! reg_mentioned_p (stack_pointer_rtx, reg))
1980 if (GET_CODE (reg) == REG
1981 && (regno = REGNO (reg), regno != FRAME_POINTER_REGNUM)
1982 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
1983 && regno != HARD_FRAME_POINTER_REGNUM
1985 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1986 && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
1988 && ! (regno < FIRST_PSEUDO_REGISTER && global_regs[regno]))
1989 /* && regno != STACK_POINTER_REGNUM) -- let's try without this. */
1991 int some_needed = REGNO_REG_SET_P (needed, regno);
1992 int some_not_needed = ! some_needed;
1994 /* Mark it as a significant register for this basic block. */
1996 SET_REGNO_REG_SET (significant, regno);
1998 /* Mark it as as dead before this insn. */
1999 SET_REGNO_REG_SET (dead, regno);
2001 /* A hard reg in a wide mode may really be multiple registers.
2002 If so, mark all of them just like the first. */
2003 if (regno < FIRST_PSEUDO_REGISTER)
2007 /* Nothing below is needed for the stack pointer; get out asap.
2008 Eg, log links aren't needed, since combine won't use them. */
2009 if (regno == STACK_POINTER_REGNUM)
2012 n = HARD_REGNO_NREGS (regno, GET_MODE (reg));
2015 int regno_n = regno + n;
2016 int needed_regno = REGNO_REG_SET_P (needed, regno_n);
2018 SET_REGNO_REG_SET (significant, regno_n);
2020 SET_REGNO_REG_SET (dead, regno_n);
2021 some_needed |= needed_regno;
2022 some_not_needed |= ! needed_regno;
2025 /* Additional data to record if this is the final pass. */
2028 register rtx y = reg_next_use[regno];
2029 register int blocknum = BLOCK_NUM (insn);
2031 /* If this is a hard reg, record this function uses the reg. */
2033 if (regno < FIRST_PSEUDO_REGISTER)
2036 int endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
2038 for (i = regno; i < endregno; i++)
2040 /* The next use is no longer "next", since a store
2042 reg_next_use[i] = 0;
2044 regs_ever_live[i] = 1;
2050 /* The next use is no longer "next", since a store
2052 reg_next_use[regno] = 0;
2054 /* Keep track of which basic blocks each reg appears in. */
2056 if (REG_BASIC_BLOCK (regno) == REG_BLOCK_UNKNOWN)
2057 REG_BASIC_BLOCK (regno) = blocknum;
2058 else if (REG_BASIC_BLOCK (regno) != blocknum)
2059 REG_BASIC_BLOCK (regno) = REG_BLOCK_GLOBAL;
2061 /* Count (weighted) references, stores, etc. This counts a
2062 register twice if it is modified, but that is correct. */
2063 REG_N_SETS (regno)++;
2065 REG_N_REFS (regno) += loop_depth;
2067 /* The insns where a reg is live are normally counted
2068 elsewhere, but we want the count to include the insn
2069 where the reg is set, and the normal counting mechanism
2070 would not count it. */
2071 REG_LIVE_LENGTH (regno)++;
2074 if (! some_not_needed)
2076 /* Make a logical link from the next following insn
2077 that uses this register, back to this insn.
2078 The following insns have already been processed.
2080 We don't build a LOG_LINK for hard registers containing
2081 in ASM_OPERANDs. If these registers get replaced,
2082 we might wind up changing the semantics of the insn,
2083 even if reload can make what appear to be valid assignments
2085 if (y && (BLOCK_NUM (y) == blocknum)
2086 && (regno >= FIRST_PSEUDO_REGISTER
2087 || asm_noperands (PATTERN (y)) < 0))
2089 = gen_rtx (INSN_LIST, VOIDmode, insn, LOG_LINKS (y));
2091 else if (! some_needed)
2093 /* Note that dead stores have already been deleted when possible
2094 If we get here, we have found a dead store that cannot
2095 be eliminated (because the same insn does something useful).
2096 Indicate this by marking the reg being set as dying here. */
2098 = gen_rtx (EXPR_LIST, REG_UNUSED, reg, REG_NOTES (insn));
2099 REG_N_DEATHS (REGNO (reg))++;
2103 /* This is a case where we have a multi-word hard register
2104 and some, but not all, of the words of the register are
2105 needed in subsequent insns. Write REG_UNUSED notes
2106 for those parts that were not needed. This case should
2111 for (i = HARD_REGNO_NREGS (regno, GET_MODE (reg)) - 1;
2113 if (!REGNO_REG_SET_P (needed, regno + i))
2115 = gen_rtx (EXPR_LIST, REG_UNUSED,
2116 gen_rtx (REG, reg_raw_mode[regno + i],
2122 else if (GET_CODE (reg) == REG)
2123 reg_next_use[regno] = 0;
2125 /* If this is the last pass and this is a SCRATCH, show it will be dying
2126 here and count it. */
2127 else if (GET_CODE (reg) == SCRATCH && insn != 0)
2130 = gen_rtx (EXPR_LIST, REG_UNUSED, reg, REG_NOTES (insn));
2137 /* X is a MEM found in INSN. See if we can convert it into an auto-increment
2141 find_auto_inc (needed, x, insn)
2146 rtx addr = XEXP (x, 0);
2147 HOST_WIDE_INT offset = 0;
2150 /* Here we detect use of an index register which might be good for
2151 postincrement, postdecrement, preincrement, or predecrement. */
2153 if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
2154 offset = INTVAL (XEXP (addr, 1)), addr = XEXP (addr, 0);
2156 if (GET_CODE (addr) == REG)
2159 register int size = GET_MODE_SIZE (GET_MODE (x));
2162 int regno = REGNO (addr);
2164 /* Is the next use an increment that might make auto-increment? */
2165 if ((incr = reg_next_use[regno]) != 0
2166 && (set = single_set (incr)) != 0
2167 && GET_CODE (set) == SET
2168 && BLOCK_NUM (incr) == BLOCK_NUM (insn)
2169 /* Can't add side effects to jumps; if reg is spilled and
2170 reloaded, there's no way to store back the altered value. */
2171 && GET_CODE (insn) != JUMP_INSN
2172 && (y = SET_SRC (set), GET_CODE (y) == PLUS)
2173 && XEXP (y, 0) == addr
2174 && GET_CODE (XEXP (y, 1)) == CONST_INT
2176 #ifdef HAVE_POST_INCREMENT
2177 || (INTVAL (XEXP (y, 1)) == size && offset == 0)
2179 #ifdef HAVE_POST_DECREMENT
2180 || (INTVAL (XEXP (y, 1)) == - size && offset == 0)
2182 #ifdef HAVE_PRE_INCREMENT
2183 || (INTVAL (XEXP (y, 1)) == size && offset == size)
2185 #ifdef HAVE_PRE_DECREMENT
2186 || (INTVAL (XEXP (y, 1)) == - size && offset == - size)
2189 /* Make sure this reg appears only once in this insn. */
2190 && (use = find_use_as_address (PATTERN (insn), addr, offset),
2191 use != 0 && use != (rtx) 1))
2193 rtx q = SET_DEST (set);
2194 enum rtx_code inc_code = (INTVAL (XEXP (y, 1)) == size
2195 ? (offset ? PRE_INC : POST_INC)
2196 : (offset ? PRE_DEC : POST_DEC));
2198 if (dead_or_set_p (incr, addr))
2200 /* This is the simple case. Try to make the auto-inc. If
2201 we can't, we are done. Otherwise, we will do any
2202 needed updates below. */
2203 if (! validate_change (insn, &XEXP (x, 0),
2204 gen_rtx (inc_code, Pmode, addr),
2208 else if (GET_CODE (q) == REG
2209 /* PREV_INSN used here to check the semi-open interval
2211 && ! reg_used_between_p (q, PREV_INSN (insn), incr)
2212 /* We must also check for sets of q as q may be
2213 a call clobbered hard register and there may
2214 be a call between PREV_INSN (insn) and incr. */
2215 && ! reg_set_between_p (q, PREV_INSN (insn), incr))
2217 /* We have *p followed sometime later by q = p+size.
2218 Both p and q must be live afterward,
2219 and q is not used between INSN and it's assignment.
2220 Change it to q = p, ...*q..., q = q+size.
2221 Then fall into the usual case. */
2225 emit_move_insn (q, addr);
2226 insns = get_insns ();
2229 /* If anything in INSNS have UID's that don't fit within the
2230 extra space we allocate earlier, we can't make this auto-inc.
2231 This should never happen. */
2232 for (temp = insns; temp; temp = NEXT_INSN (temp))
2234 if (INSN_UID (temp) > max_uid_for_flow)
2236 BLOCK_NUM (temp) = BLOCK_NUM (insn);
2239 /* If we can't make the auto-inc, or can't make the
2240 replacement into Y, exit. There's no point in making
2241 the change below if we can't do the auto-inc and doing
2242 so is not correct in the pre-inc case. */
2244 validate_change (insn, &XEXP (x, 0),
2245 gen_rtx (inc_code, Pmode, q),
2247 validate_change (incr, &XEXP (y, 0), q, 1);
2248 if (! apply_change_group ())
2251 /* We now know we'll be doing this change, so emit the
2252 new insn(s) and do the updates. */
2253 emit_insns_before (insns, insn);
2255 if (basic_block_head[BLOCK_NUM (insn)] == insn)
2256 basic_block_head[BLOCK_NUM (insn)] = insns;
2258 /* INCR will become a NOTE and INSN won't contain a
2259 use of ADDR. If a use of ADDR was just placed in
2260 the insn before INSN, make that the next use.
2261 Otherwise, invalidate it. */
2262 if (GET_CODE (PREV_INSN (insn)) == INSN
2263 && GET_CODE (PATTERN (PREV_INSN (insn))) == SET
2264 && SET_SRC (PATTERN (PREV_INSN (insn))) == addr)
2265 reg_next_use[regno] = PREV_INSN (insn);
2267 reg_next_use[regno] = 0;
2272 /* REGNO is now used in INCR which is below INSN, but
2273 it previously wasn't live here. If we don't mark
2274 it as needed, we'll put a REG_DEAD note for it
2275 on this insn, which is incorrect. */
2276 SET_REGNO_REG_SET (needed, regno);
2278 /* If there are any calls between INSN and INCR, show
2279 that REGNO now crosses them. */
2280 for (temp = insn; temp != incr; temp = NEXT_INSN (temp))
2281 if (GET_CODE (temp) == CALL_INSN)
2282 REG_N_CALLS_CROSSED (regno)++;
2287 /* If we haven't returned, it means we were able to make the
2288 auto-inc, so update the status. First, record that this insn
2289 has an implicit side effect. */
2292 = gen_rtx (EXPR_LIST, REG_INC, addr, REG_NOTES (insn));
2294 /* Modify the old increment-insn to simply copy
2295 the already-incremented value of our register. */
2296 if (! validate_change (incr, &SET_SRC (set), addr, 0))
2299 /* If that makes it a no-op (copying the register into itself) delete
2300 it so it won't appear to be a "use" and a "set" of this
2302 if (SET_DEST (set) == addr)
2304 PUT_CODE (incr, NOTE);
2305 NOTE_LINE_NUMBER (incr) = NOTE_INSN_DELETED;
2306 NOTE_SOURCE_FILE (incr) = 0;
2309 if (regno >= FIRST_PSEUDO_REGISTER)
2311 /* Count an extra reference to the reg. When a reg is
2312 incremented, spilling it is worse, so we want to make
2313 that less likely. */
2314 REG_N_REFS (regno) += loop_depth;
2316 /* Count the increment as a setting of the register,
2317 even though it isn't a SET in rtl. */
2318 REG_N_SETS (regno)++;
2323 #endif /* AUTO_INC_DEC */
2325 /* Scan expression X and store a 1-bit in LIVE for each reg it uses.
2326 This is done assuming the registers needed from X
2327 are those that have 1-bits in NEEDED.
2329 On the final pass, FINAL is 1. This means try for autoincrement
2330 and count the uses and deaths of each pseudo-reg.
2332 INSN is the containing instruction. If INSN is dead, this function is not
2336 mark_used_regs (needed, live, x, final, insn)
2343 register RTX_CODE code;
2348 code = GET_CODE (x);
2369 /* If we are clobbering a MEM, mark any registers inside the address
2371 if (GET_CODE (XEXP (x, 0)) == MEM)
2372 mark_used_regs (needed, live, XEXP (XEXP (x, 0), 0), final, insn);
2376 /* Invalidate the data for the last MEM stored, but only if MEM is
2377 something that can be stored into. */
2378 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
2379 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
2380 ; /* needn't clear last_mem_set */
2386 find_auto_inc (needed, x, insn);
2391 if (GET_CODE (SUBREG_REG (x)) == REG
2392 && REGNO (SUBREG_REG (x)) >= FIRST_PSEUDO_REGISTER
2393 && (GET_MODE_SIZE (GET_MODE (x))
2394 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
2395 REG_CHANGES_SIZE (REGNO (SUBREG_REG (x))) = 1;
2397 /* While we're here, optimize this case. */
2400 /* In case the SUBREG is not of a register, don't optimize */
2401 if (GET_CODE (x) != REG)
2403 mark_used_regs (needed, live, x, final, insn);
2407 /* ... fall through ... */
2410 /* See a register other than being set
2411 => mark it as needed. */
2415 int some_needed = REGNO_REG_SET_P (needed, regno);
2416 int some_not_needed = ! some_needed;
2418 SET_REGNO_REG_SET (live, regno);
2420 /* A hard reg in a wide mode may really be multiple registers.
2421 If so, mark all of them just like the first. */
2422 if (regno < FIRST_PSEUDO_REGISTER)
2426 /* For stack ptr or fixed arg pointer,
2427 nothing below can be necessary, so waste no more time. */
2428 if (regno == STACK_POINTER_REGNUM
2429 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2430 || regno == HARD_FRAME_POINTER_REGNUM
2432 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2433 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
2435 || regno == FRAME_POINTER_REGNUM)
2437 /* If this is a register we are going to try to eliminate,
2438 don't mark it live here. If we are successful in
2439 eliminating it, it need not be live unless it is used for
2440 pseudos, in which case it will have been set live when
2441 it was allocated to the pseudos. If the register will not
2442 be eliminated, reload will set it live at that point. */
2444 if (! TEST_HARD_REG_BIT (elim_reg_set, regno))
2445 regs_ever_live[regno] = 1;
2448 /* No death notes for global register variables;
2449 their values are live after this function exits. */
2450 if (global_regs[regno])
2453 reg_next_use[regno] = insn;
2457 n = HARD_REGNO_NREGS (regno, GET_MODE (x));
2460 int regno_n = regno + n;
2461 int needed_regno = REGNO_REG_SET_P (needed, regno_n);
2463 SET_REGNO_REG_SET (live, regno_n);
2464 some_needed |= needed_regno;
2465 some_not_needed |= ! needed_regno;
2470 /* Record where each reg is used, so when the reg
2471 is set we know the next insn that uses it. */
2473 reg_next_use[regno] = insn;
2475 if (regno < FIRST_PSEUDO_REGISTER)
2477 /* If a hard reg is being used,
2478 record that this function does use it. */
2480 i = HARD_REGNO_NREGS (regno, GET_MODE (x));
2484 regs_ever_live[regno + --i] = 1;
2489 /* Keep track of which basic block each reg appears in. */
2491 register int blocknum = BLOCK_NUM (insn);
2493 if (REG_BASIC_BLOCK (regno) == REG_BLOCK_UNKNOWN)
2494 REG_BASIC_BLOCK (regno) = blocknum;
2495 else if (REG_BASIC_BLOCK (regno) != blocknum)
2496 REG_BASIC_BLOCK (regno) = REG_BLOCK_GLOBAL;
2498 /* Count (weighted) number of uses of each reg. */
2500 REG_N_REFS (regno) += loop_depth;
2503 /* Record and count the insns in which a reg dies.
2504 If it is used in this insn and was dead below the insn
2505 then it dies in this insn. If it was set in this insn,
2506 we do not make a REG_DEAD note; likewise if we already
2507 made such a note. */
2510 && ! dead_or_set_p (insn, x)
2512 && (regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
2516 /* Check for the case where the register dying partially
2517 overlaps the register set by this insn. */
2518 if (regno < FIRST_PSEUDO_REGISTER
2519 && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
2521 int n = HARD_REGNO_NREGS (regno, GET_MODE (x));
2523 some_needed |= dead_or_set_regno_p (insn, regno + n);
2526 /* If none of the words in X is needed, make a REG_DEAD
2527 note. Otherwise, we must make partial REG_DEAD notes. */
2531 = gen_rtx (EXPR_LIST, REG_DEAD, x, REG_NOTES (insn));
2532 REG_N_DEATHS (regno)++;
2538 /* Don't make a REG_DEAD note for a part of a register
2539 that is set in the insn. */
2541 for (i = HARD_REGNO_NREGS (regno, GET_MODE (x)) - 1;
2543 if (!REGNO_REG_SET_P (needed, regno + i)
2544 && ! dead_or_set_regno_p (insn, regno + i))
2546 = gen_rtx (EXPR_LIST, REG_DEAD,
2547 gen_rtx (REG, reg_raw_mode[regno + i],
2558 register rtx testreg = SET_DEST (x);
2561 /* If storing into MEM, don't show it as being used. But do
2562 show the address as being used. */
2563 if (GET_CODE (testreg) == MEM)
2567 find_auto_inc (needed, testreg, insn);
2569 mark_used_regs (needed, live, XEXP (testreg, 0), final, insn);
2570 mark_used_regs (needed, live, SET_SRC (x), final, insn);
2574 /* Storing in STRICT_LOW_PART is like storing in a reg
2575 in that this SET might be dead, so ignore it in TESTREG.
2576 but in some other ways it is like using the reg.
2578 Storing in a SUBREG or a bit field is like storing the entire
2579 register in that if the register's value is not used
2580 then this SET is not needed. */
2581 while (GET_CODE (testreg) == STRICT_LOW_PART
2582 || GET_CODE (testreg) == ZERO_EXTRACT
2583 || GET_CODE (testreg) == SIGN_EXTRACT
2584 || GET_CODE (testreg) == SUBREG)
2586 if (GET_CODE (testreg) == SUBREG
2587 && GET_CODE (SUBREG_REG (testreg)) == REG
2588 && REGNO (SUBREG_REG (testreg)) >= FIRST_PSEUDO_REGISTER
2589 && (GET_MODE_SIZE (GET_MODE (testreg))
2590 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (testreg)))))
2591 REG_CHANGES_SIZE (REGNO (SUBREG_REG (testreg))) = 1;
2593 /* Modifying a single register in an alternate mode
2594 does not use any of the old value. But these other
2595 ways of storing in a register do use the old value. */
2596 if (GET_CODE (testreg) == SUBREG
2597 && !(REG_SIZE (SUBREG_REG (testreg)) > REG_SIZE (testreg)))
2602 testreg = XEXP (testreg, 0);
2605 /* If this is a store into a register,
2606 recursively scan the value being stored. */
2608 if (GET_CODE (testreg) == REG
2609 && (regno = REGNO (testreg), regno != FRAME_POINTER_REGNUM)
2610 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2611 && regno != HARD_FRAME_POINTER_REGNUM
2613 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2614 && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
2617 /* We used to exclude global_regs here, but that seems wrong.
2618 Storing in them is like storing in mem. */
2620 mark_used_regs (needed, live, SET_SRC (x), final, insn);
2622 mark_used_regs (needed, live, SET_DEST (x), final, insn);
2629 /* If exiting needs the right stack value, consider this insn as
2630 using the stack pointer. In any event, consider it as using
2631 all global registers and all registers used by return. */
2633 #ifdef EXIT_IGNORE_STACK
2634 if (! EXIT_IGNORE_STACK
2635 || (! FRAME_POINTER_REQUIRED && flag_omit_frame_pointer))
2637 SET_REGNO_REG_SET (live, STACK_POINTER_REGNUM);
2639 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2641 #ifdef EPILOGUE_USES
2642 || EPILOGUE_USES (i)
2645 SET_REGNO_REG_SET (live, i);
2652 /* Recursively scan the operands of this expression. */
2655 register char *fmt = GET_RTX_FORMAT (code);
2658 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2662 /* Tail recursive case: save a function call level. */
2668 mark_used_regs (needed, live, XEXP (x, i), final, insn);
2670 else if (fmt[i] == 'E')
2673 for (j = 0; j < XVECLEN (x, i); j++)
2674 mark_used_regs (needed, live, XVECEXP (x, i, j), final, insn);
2683 try_pre_increment_1 (insn)
2686 /* Find the next use of this reg. If in same basic block,
2687 make it do pre-increment or pre-decrement if appropriate. */
2688 rtx x = single_set (insn);
2689 HOST_WIDE_INT amount = ((GET_CODE (SET_SRC (x)) == PLUS ? 1 : -1)
2690 * INTVAL (XEXP (SET_SRC (x), 1)));
2691 int regno = REGNO (SET_DEST (x));
2692 rtx y = reg_next_use[regno];
2694 && BLOCK_NUM (y) == BLOCK_NUM (insn)
2695 /* Don't do this if the reg dies, or gets set in y; a standard addressing
2696 mode would be better. */
2697 && ! dead_or_set_p (y, SET_DEST (x))
2698 && try_pre_increment (y, SET_DEST (x), amount))
2700 /* We have found a suitable auto-increment
2701 and already changed insn Y to do it.
2702 So flush this increment-instruction. */
2703 PUT_CODE (insn, NOTE);
2704 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2705 NOTE_SOURCE_FILE (insn) = 0;
2706 /* Count a reference to this reg for the increment
2707 insn we are deleting. When a reg is incremented.
2708 spilling it is worse, so we want to make that
2710 if (regno >= FIRST_PSEUDO_REGISTER)
2712 REG_N_REFS (regno) += loop_depth;
2713 REG_N_SETS (regno)++;
2720 /* Try to change INSN so that it does pre-increment or pre-decrement
2721 addressing on register REG in order to add AMOUNT to REG.
2722 AMOUNT is negative for pre-decrement.
2723 Returns 1 if the change could be made.
2724 This checks all about the validity of the result of modifying INSN. */
2727 try_pre_increment (insn, reg, amount)
2729 HOST_WIDE_INT amount;
2733 /* Nonzero if we can try to make a pre-increment or pre-decrement.
2734 For example, addl $4,r1; movl (r1),... can become movl +(r1),... */
2736 /* Nonzero if we can try to make a post-increment or post-decrement.
2737 For example, addl $4,r1; movl -4(r1),... can become movl (r1)+,...
2738 It is possible for both PRE_OK and POST_OK to be nonzero if the machine
2739 supports both pre-inc and post-inc, or both pre-dec and post-dec. */
2742 /* Nonzero if the opportunity actually requires post-inc or post-dec. */
2745 /* From the sign of increment, see which possibilities are conceivable
2746 on this target machine. */
2747 #ifdef HAVE_PRE_INCREMENT
2751 #ifdef HAVE_POST_INCREMENT
2756 #ifdef HAVE_PRE_DECREMENT
2760 #ifdef HAVE_POST_DECREMENT
2765 if (! (pre_ok || post_ok))
2768 /* It is not safe to add a side effect to a jump insn
2769 because if the incremented register is spilled and must be reloaded
2770 there would be no way to store the incremented value back in memory. */
2772 if (GET_CODE (insn) == JUMP_INSN)
2777 use = find_use_as_address (PATTERN (insn), reg, 0);
2778 if (post_ok && (use == 0 || use == (rtx) 1))
2780 use = find_use_as_address (PATTERN (insn), reg, -amount);
2784 if (use == 0 || use == (rtx) 1)
2787 if (GET_MODE_SIZE (GET_MODE (use)) != (amount > 0 ? amount : - amount))
2790 /* See if this combination of instruction and addressing mode exists. */
2791 if (! validate_change (insn, &XEXP (use, 0),
2793 ? (do_post ? POST_INC : PRE_INC)
2794 : (do_post ? POST_DEC : PRE_DEC),
2798 /* Record that this insn now has an implicit side effect on X. */
2799 REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_INC, reg, REG_NOTES (insn));
2803 #endif /* AUTO_INC_DEC */
2805 /* Find the place in the rtx X where REG is used as a memory address.
2806 Return the MEM rtx that so uses it.
2807 If PLUSCONST is nonzero, search instead for a memory address equivalent to
2808 (plus REG (const_int PLUSCONST)).
2810 If such an address does not appear, return 0.
2811 If REG appears more than once, or is used other than in such an address,
2815 find_use_as_address (x, reg, plusconst)
2818 HOST_WIDE_INT plusconst;
2820 enum rtx_code code = GET_CODE (x);
2821 char *fmt = GET_RTX_FORMAT (code);
2823 register rtx value = 0;
2826 if (code == MEM && XEXP (x, 0) == reg && plusconst == 0)
2829 if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
2830 && XEXP (XEXP (x, 0), 0) == reg
2831 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2832 && INTVAL (XEXP (XEXP (x, 0), 1)) == plusconst)
2835 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
2837 /* If REG occurs inside a MEM used in a bit-field reference,
2838 that is unacceptable. */
2839 if (find_use_as_address (XEXP (x, 0), reg, 0) != 0)
2840 return (rtx) (HOST_WIDE_INT) 1;
2844 return (rtx) (HOST_WIDE_INT) 1;
2846 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2850 tem = find_use_as_address (XEXP (x, i), reg, plusconst);
2854 return (rtx) (HOST_WIDE_INT) 1;
2859 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2861 tem = find_use_as_address (XVECEXP (x, i, j), reg, plusconst);
2865 return (rtx) (HOST_WIDE_INT) 1;
2873 /* Write information about registers and basic blocks into FILE.
2874 This is part of making a debugging dump. */
2877 dump_flow_info (file)
2881 static char *reg_class_names[] = REG_CLASS_NAMES;
2883 fprintf (file, "%d registers.\n", max_regno);
2885 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
2888 enum reg_class class, altclass;
2889 fprintf (file, "\nRegister %d used %d times across %d insns",
2890 i, REG_N_REFS (i), REG_LIVE_LENGTH (i));
2891 if (REG_BASIC_BLOCK (i) >= 0)
2892 fprintf (file, " in block %d", REG_BASIC_BLOCK (i));
2893 if (REG_N_DEATHS (i) != 1)
2894 fprintf (file, "; dies in %d places", REG_N_DEATHS (i));
2895 if (REG_N_CALLS_CROSSED (i) == 1)
2896 fprintf (file, "; crosses 1 call");
2897 else if (REG_N_CALLS_CROSSED (i))
2898 fprintf (file, "; crosses %d calls", REG_N_CALLS_CROSSED (i));
2899 if (PSEUDO_REGNO_BYTES (i) != UNITS_PER_WORD)
2900 fprintf (file, "; %d bytes", PSEUDO_REGNO_BYTES (i));
2901 class = reg_preferred_class (i);
2902 altclass = reg_alternate_class (i);
2903 if (class != GENERAL_REGS || altclass != ALL_REGS)
2905 if (altclass == ALL_REGS || class == ALL_REGS)
2906 fprintf (file, "; pref %s", reg_class_names[(int) class]);
2907 else if (altclass == NO_REGS)
2908 fprintf (file, "; %s or none", reg_class_names[(int) class]);
2910 fprintf (file, "; pref %s, else %s",
2911 reg_class_names[(int) class],
2912 reg_class_names[(int) altclass]);
2914 if (REGNO_POINTER_FLAG (i))
2915 fprintf (file, "; pointer");
2916 fprintf (file, ".\n");
2918 fprintf (file, "\n%d basic blocks.\n", n_basic_blocks);
2919 for (i = 0; i < n_basic_blocks; i++)
2921 register rtx head, jump;
2923 fprintf (file, "\nBasic block %d: first insn %d, last %d.\n",
2925 INSN_UID (basic_block_head[i]),
2926 INSN_UID (basic_block_end[i]));
2927 /* The control flow graph's storage is freed
2928 now when flow_analysis returns.
2929 Don't try to print it if it is gone. */
2930 if (basic_block_drops_in)
2932 fprintf (file, "Reached from blocks: ");
2933 head = basic_block_head[i];
2934 if (GET_CODE (head) == CODE_LABEL)
2935 for (jump = LABEL_REFS (head);
2937 jump = LABEL_NEXTREF (jump))
2939 register int from_block = BLOCK_NUM (CONTAINING_INSN (jump));
2940 fprintf (file, " %d", from_block);
2942 if (basic_block_drops_in[i])
2943 fprintf (file, " previous");
2945 fprintf (file, "\nRegisters live at start:");
2946 for (regno = 0; regno < max_regno; regno++)
2947 if (REGNO_REG_SET_P (basic_block_live_at_start[i], regno))
2948 fprintf (file, " %d", regno);
2949 fprintf (file, "\n");
2951 fprintf (file, "\n");
2955 /* Like print_rtl, but also print out live information for the start of each
2959 print_rtl_with_bb (outf, rtx_first)
2963 register rtx tmp_rtx;
2966 fprintf (outf, "(nil)\n");
2971 enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
2972 int max_uid = get_max_uid ();
2973 int *start = (int *) alloca (max_uid * sizeof (int));
2974 int *end = (int *) alloca (max_uid * sizeof (int));
2975 char *in_bb_p = (char *) alloca (max_uid * sizeof (enum bb_state));
2977 for (i = 0; i < max_uid; i++)
2979 start[i] = end[i] = -1;
2980 in_bb_p[i] = NOT_IN_BB;
2983 for (i = n_basic_blocks-1; i >= 0; i--)
2986 start[INSN_UID (basic_block_head[i])] = i;
2987 end[INSN_UID (basic_block_end[i])] = i;
2988 for (x = basic_block_head[i]; x != NULL_RTX; x = NEXT_INSN (x))
2990 in_bb_p[ INSN_UID(x)]
2991 = (in_bb_p[ INSN_UID(x)] == NOT_IN_BB)
2992 ? IN_ONE_BB : IN_MULTIPLE_BB;
2993 if (x == basic_block_end[i])
2998 for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx))
3000 if ((bb = start[INSN_UID (tmp_rtx)]) >= 0)
3002 fprintf (outf, ";; Start of basic block %d, registers live:",
3005 EXECUTE_IF_SET_IN_REG_SET (basic_block_live_at_start[bb], 0, i,
3007 fprintf (outf, " %d", i);
3008 if (i < FIRST_PSEUDO_REGISTER)
3009 fprintf (outf, " [%s]",
3015 if (in_bb_p[ INSN_UID(tmp_rtx)] == NOT_IN_BB
3016 && GET_CODE (tmp_rtx) != NOTE
3017 && GET_CODE (tmp_rtx) != BARRIER)
3018 fprintf (outf, ";; Insn is not within a basic block\n");
3019 else if (in_bb_p[ INSN_UID(tmp_rtx)] == IN_MULTIPLE_BB)
3020 fprintf (outf, ";; Insn is in multiple basic blocks\n");
3022 print_rtl_single (outf, tmp_rtx);
3024 if ((bb = end[INSN_UID (tmp_rtx)]) >= 0)
3025 fprintf (outf, ";; End of basic block %d\n", bb);