2007-03-01 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / flow.c
blob78c23281c6b25c4d4c5a8b9dea4ed274b11fd689
1 /* Data flow analysis for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation,
4 Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 /* This file contains the data flow analysis pass of the compiler. It
24 computes data flow information which tells combine_instructions
25 which insns to consider combining and controls register allocation.
27 Additional data flow information that is too bulky to record is
28 generated during the analysis, and is used at that time to create
29 autoincrement and autodecrement addressing.
31 The first step is dividing the function into basic blocks.
32 find_basic_blocks does this. Then life_analysis determines
33 where each register is live and where it is dead.
35 ** find_basic_blocks **
37 find_basic_blocks divides the current function's rtl into basic
38 blocks and constructs the CFG. The blocks are recorded in the
39 basic_block_info array; the CFG exists in the edge structures
40 referenced by the blocks.
42 find_basic_blocks also finds any unreachable loops and deletes them.
44 ** life_analysis **
46 life_analysis is called immediately after find_basic_blocks.
47 It uses the basic block information to determine where each
48 hard or pseudo register is live.
50 ** live-register info **
52 The information about where each register is live is in two parts:
53 the REG_NOTES of insns, and the vector basic_block->global_live_at_start.
55 basic_block->global_live_at_start has an element for each basic
56 block, and the element is a bit-vector with a bit for each hard or
57 pseudo register. The bit is 1 if the register is live at the
58 beginning of the basic block.
60 Two types of elements can be added to an insn's REG_NOTES.
61 A REG_DEAD note is added to an insn's REG_NOTES for any register
62 that meets both of two conditions: The value in the register is not
63 needed in subsequent insns and the insn does not replace the value in
64 the register (in the case of multi-word hard registers, the value in
65 each register must be replaced by the insn to avoid a REG_DEAD note).
67 In the vast majority of cases, an object in a REG_DEAD note will be
68 used somewhere in the insn. The (rare) exception to this is if an
69 insn uses a multi-word hard register and only some of the registers are
70 needed in subsequent insns. In that case, REG_DEAD notes will be
71 provided for those hard registers that are not subsequently needed.
72 Partial REG_DEAD notes of this type do not occur when an insn sets
73 only some of the hard registers used in such a multi-word operand;
74 omitting REG_DEAD notes for objects stored in an insn is optional and
75 the desire to do so does not justify the complexity of the partial
76 REG_DEAD notes.
78 REG_UNUSED notes are added for each register that is set by the insn
79 but is unused subsequently (if every register set by the insn is unused
80 and the insn does not reference memory or have some other side-effect,
81 the insn is deleted instead). If only part of a multi-word hard
82 register is used in a subsequent insn, REG_UNUSED notes are made for
83 the parts that will not be used.
85 To determine which registers are live after any insn, one can
86 start from the beginning of the basic block and scan insns, noting
87 which registers are set by each insn and which die there.
89 ** Other actions of life_analysis **
91 life_analysis sets up the LOG_LINKS fields of insns because the
92 information needed to do so is readily available.
94 life_analysis deletes insns whose only effect is to store a value
95 that is never used.
97 life_analysis notices cases where a reference to a register as
98 a memory address can be combined with a preceding or following
99 incrementation or decrementation of the register. The separate
100 instruction to increment or decrement is deleted and the address
101 is changed to a POST_INC or similar rtx.
103 Each time an incrementing or decrementing address is created,
104 a REG_INC element is added to the insn's REG_NOTES list.
106 life_analysis fills in certain vectors containing information about
107 register usage: REG_N_REFS, REG_N_DEATHS, REG_N_SETS, REG_LIVE_LENGTH,
108 REG_N_CALLS_CROSSED, REG_N_THROWING_CALLS_CROSSED and REG_BASIC_BLOCK.
110 life_analysis sets current_function_sp_is_unchanging if the function
111 doesn't modify the stack pointer. */
113 /* TODO:
115 Split out from life_analysis:
116 - local property discovery
117 - global property computation
118 - log links creation
119 - pre/post modify transformation
122 #include "config.h"
123 #include "system.h"
124 #include "coretypes.h"
125 #include "tm.h"
126 #include "tree.h"
127 #include "rtl.h"
128 #include "tm_p.h"
129 #include "hard-reg-set.h"
130 #include "basic-block.h"
131 #include "insn-config.h"
132 #include "regs.h"
133 #include "flags.h"
134 #include "output.h"
135 #include "function.h"
136 #include "except.h"
137 #include "toplev.h"
138 #include "recog.h"
139 #include "expr.h"
140 #include "timevar.h"
142 #include "obstack.h"
143 #include "splay-tree.h"
144 #include "tree-pass.h"
145 #include "params.h"
147 #ifndef HAVE_epilogue
148 #define HAVE_epilogue 0
149 #endif
150 #ifndef HAVE_prologue
151 #define HAVE_prologue 0
152 #endif
153 #ifndef HAVE_sibcall_epilogue
154 #define HAVE_sibcall_epilogue 0
155 #endif
157 #ifndef EPILOGUE_USES
158 #define EPILOGUE_USES(REGNO) 0
159 #endif
160 #ifndef EH_USES
161 #define EH_USES(REGNO) 0
162 #endif
164 #ifdef HAVE_conditional_execution
165 #ifndef REVERSE_CONDEXEC_PREDICATES_P
166 #define REVERSE_CONDEXEC_PREDICATES_P(x, y) \
167 (GET_CODE ((x)) == reversed_comparison_code ((y), NULL))
168 #endif
169 #endif
171 /* This is the maximum number of times we process any given block if the
172 latest loop depth count is smaller than this number. Only used for the
173 failure strategy to avoid infinite loops in calculate_global_regs_live. */
174 #define MAX_LIVENESS_ROUNDS 20
176 /* Nonzero if the second flow pass has completed. */
177 int flow2_completed;
179 /* Maximum register number used in this function, plus one. */
181 int max_regno;
183 /* Indexed by n, giving various register information */
185 VEC(reg_info_p,heap) *reg_n_info;
187 /* Regset of regs live when calls to `setjmp'-like functions happen. */
188 /* ??? Does this exist only for the setjmp-clobbered warning message? */
190 static regset regs_live_at_setjmp;
192 /* List made of EXPR_LIST rtx's which gives pairs of pseudo registers
193 that have to go in the same hard reg.
194 The first two regs in the list are a pair, and the next two
195 are another pair, etc. */
196 rtx regs_may_share;
198 /* Set of registers that may be eliminable. These are handled specially
199 in updating regs_ever_live. */
201 static HARD_REG_SET elim_reg_set;
203 /* Holds information for tracking conditional register life information. */
204 struct reg_cond_life_info
206 /* A boolean expression of conditions under which a register is dead. */
207 rtx condition;
208 /* Conditions under which a register is dead at the basic block end. */
209 rtx orig_condition;
211 /* A boolean expression of conditions under which a register has been
212 stored into. */
213 rtx stores;
215 /* ??? Could store mask of bytes that are dead, so that we could finally
216 track lifetimes of multi-word registers accessed via subregs. */
219 /* For use in communicating between propagate_block and its subroutines.
220 Holds all information needed to compute life and def-use information. */
222 struct propagate_block_info
224 /* The basic block we're considering. */
225 basic_block bb;
227 /* Bit N is set if register N is conditionally or unconditionally live. */
228 regset reg_live;
230 /* Bit N is set if register N is set this insn. */
231 regset new_set;
233 /* Element N is the next insn that uses (hard or pseudo) register N
234 within the current basic block; or zero, if there is no such insn. */
235 rtx *reg_next_use;
237 /* Contains a list of all the MEMs we are tracking for dead store
238 elimination. */
239 rtx mem_set_list;
241 /* If non-null, record the set of registers set unconditionally in the
242 basic block. */
243 regset local_set;
245 /* If non-null, record the set of registers set conditionally in the
246 basic block. */
247 regset cond_local_set;
249 #ifdef HAVE_conditional_execution
250 /* Indexed by register number, holds a reg_cond_life_info for each
251 register that is not unconditionally live or dead. */
252 splay_tree reg_cond_dead;
254 /* Bit N is set if register N is in an expression in reg_cond_dead. */
255 regset reg_cond_reg;
256 #endif
258 /* The length of mem_set_list. */
259 int mem_set_list_len;
261 /* Nonzero if the value of CC0 is live. */
262 int cc0_live;
264 /* Flags controlling the set of information propagate_block collects. */
265 int flags;
266 /* Index of instruction being processed. */
267 int insn_num;
270 /* Number of dead insns removed. */
271 static int ndead;
273 /* When PROP_REG_INFO set, array contains pbi->insn_num of instruction
274 where given register died. When the register is marked alive, we use the
275 information to compute amount of instructions life range cross.
276 (remember, we are walking backward). This can be computed as current
277 pbi->insn_num - reg_deaths[regno].
278 At the end of processing each basic block, the remaining live registers
279 are inspected and live ranges are increased same way so liverange of global
280 registers are computed correctly.
282 The array is maintained clear for dead registers, so it can be safely reused
283 for next basic block without expensive memset of the whole array after
284 reseting pbi->insn_num to 0. */
286 static int *reg_deaths;
288 /* Forward declarations */
289 static int verify_wide_reg_1 (rtx *, void *);
290 static void verify_wide_reg (int, basic_block);
291 static void verify_local_live_at_start (regset, basic_block);
292 static void notice_stack_pointer_modification_1 (rtx, rtx, void *);
293 static void notice_stack_pointer_modification (void);
294 static void mark_reg (rtx, void *);
295 static void mark_regs_live_at_end (regset);
296 static void calculate_global_regs_live (sbitmap, sbitmap, int);
297 static void propagate_block_delete_insn (rtx);
298 static rtx propagate_block_delete_libcall (rtx, rtx);
299 static int insn_dead_p (struct propagate_block_info *, rtx, int, rtx);
300 static int libcall_dead_p (struct propagate_block_info *, rtx, rtx);
301 static void mark_set_regs (struct propagate_block_info *, rtx, rtx);
302 static void mark_set_1 (struct propagate_block_info *, enum rtx_code, rtx,
303 rtx, rtx, int);
304 static int find_regno_partial (rtx *, void *);
306 #ifdef HAVE_conditional_execution
307 static int mark_regno_cond_dead (struct propagate_block_info *, int, rtx);
308 static void free_reg_cond_life_info (splay_tree_value);
309 static int flush_reg_cond_reg_1 (splay_tree_node, void *);
310 static void flush_reg_cond_reg (struct propagate_block_info *, int);
311 static rtx elim_reg_cond (rtx, unsigned int);
312 static rtx ior_reg_cond (rtx, rtx, int);
313 static rtx not_reg_cond (rtx);
314 static rtx and_reg_cond (rtx, rtx, int);
315 #endif
316 #ifdef AUTO_INC_DEC
317 static void attempt_auto_inc (struct propagate_block_info *, rtx, rtx, rtx,
318 rtx, rtx);
319 static void find_auto_inc (struct propagate_block_info *, rtx, rtx);
320 static int try_pre_increment_1 (struct propagate_block_info *, rtx);
321 static int try_pre_increment (rtx, rtx, HOST_WIDE_INT);
322 #endif
323 static void mark_used_reg (struct propagate_block_info *, rtx, rtx, rtx);
324 static void mark_used_regs (struct propagate_block_info *, rtx, rtx, rtx);
325 void debug_flow_info (void);
326 static void add_to_mem_set_list (struct propagate_block_info *, rtx);
327 static int invalidate_mems_from_autoinc (rtx *, void *);
328 static void invalidate_mems_from_set (struct propagate_block_info *, rtx);
329 static void clear_log_links (sbitmap);
330 static int count_or_remove_death_notes_bb (basic_block, int);
331 static void allocate_bb_life_data (void);
333 /* Return the INSN immediately following the NOTE_INSN_BASIC_BLOCK
334 note associated with the BLOCK. */
337 first_insn_after_basic_block_note (basic_block block)
339 rtx insn;
341 /* Get the first instruction in the block. */
342 insn = BB_HEAD (block);
344 if (insn == NULL_RTX)
345 return NULL_RTX;
346 if (LABEL_P (insn))
347 insn = NEXT_INSN (insn);
348 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
350 return NEXT_INSN (insn);
353 /* Perform data flow analysis for the whole control flow graph.
354 FLAGS is a set of PROP_* flags to be used in accumulating flow info. */
356 void
357 life_analysis (int flags)
359 #ifdef ELIMINABLE_REGS
360 int i;
361 static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
362 #endif
364 /* Record which registers will be eliminated. We use this in
365 mark_used_regs. */
367 CLEAR_HARD_REG_SET (elim_reg_set);
369 #ifdef ELIMINABLE_REGS
370 for (i = 0; i < (int) ARRAY_SIZE (eliminables); i++)
371 SET_HARD_REG_BIT (elim_reg_set, eliminables[i].from);
372 #else
373 SET_HARD_REG_BIT (elim_reg_set, FRAME_POINTER_REGNUM);
374 #endif
377 #ifdef CANNOT_CHANGE_MODE_CLASS
378 if (flags & PROP_REG_INFO)
379 init_subregs_of_mode ();
380 #endif
382 if (! optimize)
383 flags &= ~(PROP_LOG_LINKS | PROP_AUTOINC | PROP_ALLOW_CFG_CHANGES);
385 /* The post-reload life analysis have (on a global basis) the same
386 registers live as was computed by reload itself. elimination
387 Otherwise offsets and such may be incorrect.
389 Reload will make some registers as live even though they do not
390 appear in the rtl.
392 We don't want to create new auto-incs after reload, since they
393 are unlikely to be useful and can cause problems with shared
394 stack slots. */
395 if (reload_completed)
396 flags &= ~(PROP_REG_INFO | PROP_AUTOINC);
398 /* We want alias analysis information for local dead store elimination. */
399 if (optimize && (flags & PROP_SCAN_DEAD_STORES))
400 init_alias_analysis ();
402 /* Always remove no-op moves. Do this before other processing so
403 that we don't have to keep re-scanning them. */
404 delete_noop_moves ();
406 /* Some targets can emit simpler epilogues if they know that sp was
407 not ever modified during the function. After reload, of course,
408 we've already emitted the epilogue so there's no sense searching. */
409 if (! reload_completed)
410 notice_stack_pointer_modification ();
412 /* Allocate and zero out data structures that will record the
413 data from lifetime analysis. */
414 allocate_reg_life_data ();
415 allocate_bb_life_data ();
417 /* Find the set of registers live on function exit. */
418 mark_regs_live_at_end (EXIT_BLOCK_PTR->il.rtl->global_live_at_start);
420 /* "Update" life info from zero. It'd be nice to begin the
421 relaxation with just the exit and noreturn blocks, but that set
422 is not immediately handy. */
424 if (flags & PROP_REG_INFO)
426 memset (regs_ever_live, 0, sizeof (regs_ever_live));
427 memset (regs_asm_clobbered, 0, sizeof (regs_asm_clobbered));
429 update_life_info (NULL, UPDATE_LIFE_GLOBAL, flags);
430 if (reg_deaths)
432 free (reg_deaths);
433 reg_deaths = NULL;
436 /* Clean up. */
437 if (optimize && (flags & PROP_SCAN_DEAD_STORES))
438 end_alias_analysis ();
440 if (dump_file)
441 dump_flow_info (dump_file, dump_flags);
443 /* Removing dead insns should have made jumptables really dead. */
444 delete_dead_jumptables ();
447 /* A subroutine of verify_wide_reg, called through for_each_rtx.
448 Search for REGNO. If found, return 2 if it is not wider than
449 word_mode. */
451 static int
452 verify_wide_reg_1 (rtx *px, void *pregno)
454 rtx x = *px;
455 unsigned int regno = *(int *) pregno;
457 if (REG_P (x) && REGNO (x) == regno)
459 if (GET_MODE_BITSIZE (GET_MODE (x)) <= BITS_PER_WORD)
460 return 2;
461 return 1;
463 return 0;
466 /* A subroutine of verify_local_live_at_start. Search through insns
467 of BB looking for register REGNO. */
469 static void
470 verify_wide_reg (int regno, basic_block bb)
472 rtx head = BB_HEAD (bb), end = BB_END (bb);
474 while (1)
476 if (INSN_P (head))
478 int r = for_each_rtx (&PATTERN (head), verify_wide_reg_1, &regno);
479 if (r == 1)
480 return;
481 if (r == 2)
482 break;
484 if (head == end)
485 break;
486 head = NEXT_INSN (head);
488 if (dump_file)
490 fprintf (dump_file, "Register %d died unexpectedly.\n", regno);
491 dump_bb (bb, dump_file, 0);
493 internal_error ("internal consistency failure");
496 /* A subroutine of update_life_info. Verify that there are no untoward
497 changes in live_at_start during a local update. */
499 static void
500 verify_local_live_at_start (regset new_live_at_start, basic_block bb)
502 if (reload_completed)
504 /* After reload, there are no pseudos, nor subregs of multi-word
505 registers. The regsets should exactly match. */
506 if (! REG_SET_EQUAL_P (new_live_at_start,
507 bb->il.rtl->global_live_at_start))
509 if (dump_file)
511 fprintf (dump_file,
512 "live_at_start mismatch in bb %d, aborting\nNew:\n",
513 bb->index);
514 debug_bitmap_file (dump_file, new_live_at_start);
515 fputs ("Old:\n", dump_file);
516 dump_bb (bb, dump_file, 0);
518 internal_error ("internal consistency failure");
521 else
523 unsigned i;
524 reg_set_iterator rsi;
526 /* Find the set of changed registers. */
527 XOR_REG_SET (new_live_at_start, bb->il.rtl->global_live_at_start);
529 EXECUTE_IF_SET_IN_REG_SET (new_live_at_start, 0, i, rsi)
531 /* No registers should die. */
532 if (REGNO_REG_SET_P (bb->il.rtl->global_live_at_start, i))
534 if (dump_file)
536 fprintf (dump_file,
537 "Register %d died unexpectedly.\n", i);
538 dump_bb (bb, dump_file, 0);
540 internal_error ("internal consistency failure");
542 /* Verify that the now-live register is wider than word_mode. */
543 verify_wide_reg (i, bb);
548 /* Updates life information starting with the basic blocks set in BLOCKS.
549 If BLOCKS is null, consider it to be the universal set.
551 If EXTENT is UPDATE_LIFE_LOCAL, such as after splitting or peepholing,
552 we are only expecting local modifications to basic blocks. If we find
553 extra registers live at the beginning of a block, then we either killed
554 useful data, or we have a broken split that wants data not provided.
555 If we find registers removed from live_at_start, that means we have
556 a broken peephole that is killing a register it shouldn't.
558 ??? This is not true in one situation -- when a pre-reload splitter
559 generates subregs of a multi-word pseudo, current life analysis will
560 lose the kill. So we _can_ have a pseudo go live. How irritating.
562 It is also not true when a peephole decides that it doesn't need one
563 or more of the inputs.
565 Including PROP_REG_INFO does not properly refresh regs_ever_live
566 unless the caller resets it to zero. */
569 update_life_info (sbitmap blocks, enum update_life_extent extent,
570 int prop_flags)
572 regset tmp;
573 unsigned i = 0;
574 int stabilized_prop_flags = prop_flags;
575 basic_block bb;
577 tmp = ALLOC_REG_SET (&reg_obstack);
578 ndead = 0;
580 if ((prop_flags & PROP_REG_INFO) && !reg_deaths)
581 reg_deaths = XCNEWVEC (int, max_regno);
583 timevar_push ((extent == UPDATE_LIFE_LOCAL || blocks)
584 ? TV_LIFE_UPDATE : TV_LIFE);
586 /* Changes to the CFG are only allowed when
587 doing a global update for the entire CFG. */
588 gcc_assert (!(prop_flags & PROP_ALLOW_CFG_CHANGES)
589 || (extent != UPDATE_LIFE_LOCAL && !blocks));
591 /* For a global update, we go through the relaxation process again. */
592 if (extent != UPDATE_LIFE_LOCAL)
594 for ( ; ; )
596 int changed = 0;
598 calculate_global_regs_live (blocks, blocks,
599 prop_flags & (PROP_SCAN_DEAD_CODE
600 | PROP_SCAN_DEAD_STORES
601 | PROP_ALLOW_CFG_CHANGES));
603 if ((prop_flags & (PROP_KILL_DEAD_CODE | PROP_ALLOW_CFG_CHANGES))
604 != (PROP_KILL_DEAD_CODE | PROP_ALLOW_CFG_CHANGES))
605 break;
607 /* Removing dead code may allow the CFG to be simplified which
608 in turn may allow for further dead code detection / removal. */
609 FOR_EACH_BB_REVERSE (bb)
611 COPY_REG_SET (tmp, bb->il.rtl->global_live_at_end);
612 changed |= propagate_block (bb, tmp, NULL, NULL,
613 prop_flags & (PROP_SCAN_DEAD_CODE
614 | PROP_SCAN_DEAD_STORES
615 | PROP_KILL_DEAD_CODE));
618 /* Don't pass PROP_SCAN_DEAD_CODE or PROP_KILL_DEAD_CODE to
619 subsequent propagate_block calls, since removing or acting as
620 removing dead code can affect global register liveness, which
621 is supposed to be finalized for this call after this loop. */
622 stabilized_prop_flags
623 &= ~(PROP_SCAN_DEAD_CODE | PROP_SCAN_DEAD_STORES
624 | PROP_KILL_DEAD_CODE);
626 if (! changed)
627 break;
629 /* We repeat regardless of what cleanup_cfg says. If there were
630 instructions deleted above, that might have been only a
631 partial improvement (see PARAM_MAX_FLOW_MEMORY_LOCATIONS usage).
632 Further improvement may be possible. */
633 cleanup_cfg (CLEANUP_EXPENSIVE);
635 /* Zap the life information from the last round. If we don't
636 do this, we can wind up with registers that no longer appear
637 in the code being marked live at entry. */
638 FOR_EACH_BB (bb)
640 CLEAR_REG_SET (bb->il.rtl->global_live_at_start);
641 CLEAR_REG_SET (bb->il.rtl->global_live_at_end);
645 /* If asked, remove notes from the blocks we'll update. */
646 if (extent == UPDATE_LIFE_GLOBAL_RM_NOTES)
647 count_or_remove_death_notes (blocks,
648 prop_flags & PROP_POST_REGSTACK ? -1 : 1);
650 else
652 /* FIXME: This can go when the dataflow branch has been merged in. */
653 /* For a local update, if we are creating new REG_DEAD notes, then we
654 must delete the old ones first to avoid conflicts if they are
655 different. */
656 if (prop_flags & PROP_DEATH_NOTES)
657 count_or_remove_death_notes (blocks,
658 prop_flags & PROP_POST_REGSTACK ? -1 : 1);
662 /* Clear log links in case we are asked to (re)compute them. */
663 if (prop_flags & PROP_LOG_LINKS)
664 clear_log_links (blocks);
666 if (blocks)
668 sbitmap_iterator sbi;
670 EXECUTE_IF_SET_IN_SBITMAP (blocks, 0, i, sbi)
672 bb = BASIC_BLOCK (i);
673 if (bb)
675 /* The bitmap may be flawed in that one of the basic
676 blocks may have been deleted before you get here. */
677 COPY_REG_SET (tmp, bb->il.rtl->global_live_at_end);
678 propagate_block (bb, tmp, NULL, NULL, stabilized_prop_flags);
680 if (extent == UPDATE_LIFE_LOCAL)
681 verify_local_live_at_start (tmp, bb);
685 else
687 FOR_EACH_BB_REVERSE (bb)
689 COPY_REG_SET (tmp, bb->il.rtl->global_live_at_end);
691 propagate_block (bb, tmp, NULL, NULL, stabilized_prop_flags);
693 if (extent == UPDATE_LIFE_LOCAL)
694 verify_local_live_at_start (tmp, bb);
698 FREE_REG_SET (tmp);
700 if (prop_flags & PROP_REG_INFO)
702 reg_set_iterator rsi;
704 /* The only pseudos that are live at the beginning of the function
705 are those that were not set anywhere in the function. local-alloc
706 doesn't know how to handle these correctly, so mark them as not
707 local to any one basic block. */
708 EXECUTE_IF_SET_IN_REG_SET (ENTRY_BLOCK_PTR->il.rtl->global_live_at_end,
709 FIRST_PSEUDO_REGISTER, i, rsi)
710 REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL;
712 /* We have a problem with any pseudoreg that lives across the setjmp.
713 ANSI says that if a user variable does not change in value between
714 the setjmp and the longjmp, then the longjmp preserves it. This
715 includes longjmp from a place where the pseudo appears dead.
716 (In principle, the value still exists if it is in scope.)
717 If the pseudo goes in a hard reg, some other value may occupy
718 that hard reg where this pseudo is dead, thus clobbering the pseudo.
719 Conclusion: such a pseudo must not go in a hard reg. */
720 EXECUTE_IF_SET_IN_REG_SET (regs_live_at_setjmp,
721 FIRST_PSEUDO_REGISTER, i, rsi)
723 if (regno_reg_rtx[i] != 0)
725 REG_LIVE_LENGTH (i) = -1;
726 REG_BASIC_BLOCK (i) = REG_BLOCK_UNKNOWN;
730 if (reg_deaths)
732 free (reg_deaths);
733 reg_deaths = NULL;
735 timevar_pop ((extent == UPDATE_LIFE_LOCAL || blocks)
736 ? TV_LIFE_UPDATE : TV_LIFE);
737 if (ndead && dump_file)
738 fprintf (dump_file, "deleted %i dead insns\n", ndead);
739 return ndead;
742 /* Update life information in all blocks where BB_DIRTY is set. */
745 update_life_info_in_dirty_blocks (enum update_life_extent extent, int prop_flags)
747 sbitmap update_life_blocks = sbitmap_alloc (last_basic_block);
748 int n = 0;
749 basic_block bb;
750 int retval = 0;
752 sbitmap_zero (update_life_blocks);
753 FOR_EACH_BB (bb)
755 if (bb->flags & BB_DIRTY)
757 SET_BIT (update_life_blocks, bb->index);
758 n++;
762 if (n)
763 retval = update_life_info (update_life_blocks, extent, prop_flags);
765 sbitmap_free (update_life_blocks);
766 return retval;
769 /* Free the variables allocated by find_basic_blocks. */
771 void
772 free_basic_block_vars (void)
774 if (basic_block_info)
776 clear_edges ();
777 basic_block_info = NULL;
779 n_basic_blocks = 0;
780 last_basic_block = 0;
781 n_edges = 0;
783 label_to_block_map = NULL;
785 ENTRY_BLOCK_PTR->aux = NULL;
786 ENTRY_BLOCK_PTR->il.rtl->global_live_at_end = NULL;
787 EXIT_BLOCK_PTR->aux = NULL;
788 EXIT_BLOCK_PTR->il.rtl->global_live_at_start = NULL;
791 /* Delete any insns that copy a register to itself. */
794 delete_noop_moves (void)
796 rtx insn, next;
797 basic_block bb;
798 int nnoops = 0;
800 FOR_EACH_BB (bb)
802 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
804 next = NEXT_INSN (insn);
805 if (INSN_P (insn) && noop_move_p (insn))
807 rtx note;
809 /* If we're about to remove the first insn of a libcall
810 then move the libcall note to the next real insn and
811 update the retval note. */
812 if ((note = find_reg_note (insn, REG_LIBCALL, NULL_RTX))
813 && XEXP (note, 0) != insn)
815 rtx new_libcall_insn = next_real_insn (insn);
816 rtx retval_note = find_reg_note (XEXP (note, 0),
817 REG_RETVAL, NULL_RTX);
818 REG_NOTES (new_libcall_insn)
819 = gen_rtx_INSN_LIST (REG_LIBCALL, XEXP (note, 0),
820 REG_NOTES (new_libcall_insn));
821 XEXP (retval_note, 0) = new_libcall_insn;
824 delete_insn_and_edges (insn);
825 nnoops++;
830 if (nnoops && dump_file)
831 fprintf (dump_file, "deleted %i noop moves\n", nnoops);
833 return nnoops;
836 /* Delete any jump tables never referenced. We can't delete them at the
837 time of removing tablejump insn as they are referenced by the preceding
838 insns computing the destination, so we delay deleting and garbagecollect
839 them once life information is computed. */
840 void
841 delete_dead_jumptables (void)
843 basic_block bb;
845 /* A dead jump table does not belong to any basic block. Scan insns
846 between two adjacent basic blocks. */
847 FOR_EACH_BB (bb)
849 rtx insn, next;
851 for (insn = NEXT_INSN (BB_END (bb));
852 insn && !NOTE_INSN_BASIC_BLOCK_P (insn);
853 insn = next)
855 next = NEXT_INSN (insn);
856 if (LABEL_P (insn)
857 && LABEL_NUSES (insn) == LABEL_PRESERVE_P (insn)
858 && JUMP_P (next)
859 && (GET_CODE (PATTERN (next)) == ADDR_VEC
860 || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC))
862 rtx label = insn, jump = next;
864 if (dump_file)
865 fprintf (dump_file, "Dead jumptable %i removed\n",
866 INSN_UID (insn));
868 next = NEXT_INSN (next);
869 delete_insn (jump);
870 delete_insn (label);
876 /* Determine if the stack pointer is constant over the life of the function.
877 Only useful before prologues have been emitted. */
879 static void
880 notice_stack_pointer_modification_1 (rtx x, rtx pat ATTRIBUTE_UNUSED,
881 void *data ATTRIBUTE_UNUSED)
883 if (x == stack_pointer_rtx
884 /* The stack pointer is only modified indirectly as the result
885 of a push until later in flow. See the comments in rtl.texi
886 regarding Embedded Side-Effects on Addresses. */
887 || (MEM_P (x)
888 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_AUTOINC
889 && XEXP (XEXP (x, 0), 0) == stack_pointer_rtx))
890 current_function_sp_is_unchanging = 0;
893 static void
894 notice_stack_pointer_modification (void)
896 basic_block bb;
897 rtx insn;
899 /* Assume that the stack pointer is unchanging if alloca hasn't
900 been used. */
901 current_function_sp_is_unchanging = !current_function_calls_alloca;
902 if (! current_function_sp_is_unchanging)
903 return;
905 FOR_EACH_BB (bb)
906 FOR_BB_INSNS (bb, insn)
908 if (INSN_P (insn))
910 /* Check if insn modifies the stack pointer. */
911 note_stores (PATTERN (insn),
912 notice_stack_pointer_modification_1,
913 NULL);
914 if (! current_function_sp_is_unchanging)
915 return;
920 /* Mark a register in SET. Hard registers in large modes get all
921 of their component registers set as well. */
923 static void
924 mark_reg (rtx reg, void *xset)
926 regset set = (regset) xset;
927 int regno = REGNO (reg);
929 gcc_assert (GET_MODE (reg) != BLKmode);
931 SET_REGNO_REG_SET (set, regno);
932 if (regno < FIRST_PSEUDO_REGISTER)
934 int n = hard_regno_nregs[regno][GET_MODE (reg)];
935 while (--n > 0)
936 SET_REGNO_REG_SET (set, regno + n);
940 /* Mark those regs which are needed at the end of the function as live
941 at the end of the last basic block. */
943 static void
944 mark_regs_live_at_end (regset set)
946 unsigned int i;
948 /* If exiting needs the right stack value, consider the stack pointer
949 live at the end of the function. */
950 if ((HAVE_epilogue && epilogue_completed)
951 || ! EXIT_IGNORE_STACK
952 || (! FRAME_POINTER_REQUIRED
953 && ! current_function_calls_alloca
954 && flag_omit_frame_pointer)
955 || current_function_sp_is_unchanging)
957 SET_REGNO_REG_SET (set, STACK_POINTER_REGNUM);
960 /* Mark the frame pointer if needed at the end of the function. If
961 we end up eliminating it, it will be removed from the live list
962 of each basic block by reload. */
964 if (! reload_completed || frame_pointer_needed)
966 SET_REGNO_REG_SET (set, FRAME_POINTER_REGNUM);
967 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
968 /* If they are different, also mark the hard frame pointer as live. */
969 if (! LOCAL_REGNO (HARD_FRAME_POINTER_REGNUM))
970 SET_REGNO_REG_SET (set, HARD_FRAME_POINTER_REGNUM);
971 #endif
974 #ifndef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
975 /* Many architectures have a GP register even without flag_pic.
976 Assume the pic register is not in use, or will be handled by
977 other means, if it is not fixed. */
978 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
979 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
980 SET_REGNO_REG_SET (set, PIC_OFFSET_TABLE_REGNUM);
981 #endif
983 /* Mark all global registers, and all registers used by the epilogue
984 as being live at the end of the function since they may be
985 referenced by our caller. */
986 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
987 if (global_regs[i] || EPILOGUE_USES (i))
988 SET_REGNO_REG_SET (set, i);
990 if (HAVE_epilogue && epilogue_completed)
992 /* Mark all call-saved registers that we actually used. */
993 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
994 if (regs_ever_live[i] && ! LOCAL_REGNO (i)
995 && ! TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
996 SET_REGNO_REG_SET (set, i);
999 #ifdef EH_RETURN_DATA_REGNO
1000 /* Mark the registers that will contain data for the handler. */
1001 if (reload_completed && current_function_calls_eh_return)
1002 for (i = 0; ; ++i)
1004 unsigned regno = EH_RETURN_DATA_REGNO(i);
1005 if (regno == INVALID_REGNUM)
1006 break;
1007 SET_REGNO_REG_SET (set, regno);
1009 #endif
1010 #ifdef EH_RETURN_STACKADJ_RTX
1011 if ((! HAVE_epilogue || ! epilogue_completed)
1012 && current_function_calls_eh_return)
1014 rtx tmp = EH_RETURN_STACKADJ_RTX;
1015 if (tmp && REG_P (tmp))
1016 mark_reg (tmp, set);
1018 #endif
1019 #ifdef EH_RETURN_HANDLER_RTX
1020 if ((! HAVE_epilogue || ! epilogue_completed)
1021 && current_function_calls_eh_return)
1023 rtx tmp = EH_RETURN_HANDLER_RTX;
1024 if (tmp && REG_P (tmp))
1025 mark_reg (tmp, set);
1027 #endif
1029 /* Mark function return value. */
1030 diddle_return_value (mark_reg, set);
1033 /* Propagate global life info around the graph of basic blocks. Begin
1034 considering blocks with their corresponding bit set in BLOCKS_IN.
1035 If BLOCKS_IN is null, consider it the universal set.
1037 BLOCKS_OUT is set for every block that was changed. */
1039 static void
1040 calculate_global_regs_live (sbitmap blocks_in, sbitmap blocks_out, int flags)
1042 basic_block *queue, *qhead, *qtail, *qend, bb;
1043 regset tmp, new_live_at_end, invalidated_by_eh_edge;
1044 regset registers_made_dead;
1045 bool failure_strategy_required = false;
1046 int *block_accesses;
1048 /* The registers that are modified within this in block. */
1049 regset *local_sets;
1051 /* The registers that are conditionally modified within this block.
1052 In other words, regs that are set only as part of a COND_EXEC. */
1053 regset *cond_local_sets;
1055 unsigned int i;
1057 /* Some passes used to forget clear aux field of basic block causing
1058 sick behavior here. */
1059 #ifdef ENABLE_CHECKING
1060 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1061 gcc_assert (!bb->aux);
1062 #endif
1064 tmp = ALLOC_REG_SET (&reg_obstack);
1065 new_live_at_end = ALLOC_REG_SET (&reg_obstack);
1066 invalidated_by_eh_edge = ALLOC_REG_SET (&reg_obstack);
1067 registers_made_dead = ALLOC_REG_SET (&reg_obstack);
1069 /* Inconveniently, this is only readily available in hard reg set form. */
1070 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1071 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1072 SET_REGNO_REG_SET (invalidated_by_eh_edge, i);
1074 /* The exception handling registers die at eh edges. */
1075 #ifdef EH_RETURN_DATA_REGNO
1076 for (i = 0; ; ++i)
1078 unsigned regno = EH_RETURN_DATA_REGNO (i);
1079 if (regno == INVALID_REGNUM)
1080 break;
1081 SET_REGNO_REG_SET (invalidated_by_eh_edge, regno);
1083 #endif
1085 /* Allocate space for the sets of local properties. */
1086 local_sets = XCNEWVEC (bitmap, last_basic_block);
1087 cond_local_sets = XCNEWVEC (bitmap, last_basic_block);
1089 /* Create a worklist. Allocate an extra slot for the `head == tail'
1090 style test for an empty queue doesn't work with a full queue. */
1091 queue = XNEWVEC (basic_block, n_basic_blocks + 1);
1092 qtail = queue;
1093 qhead = qend = queue + n_basic_blocks;
1095 /* Queue the blocks set in the initial mask. Do this in reverse block
1096 number order so that we are more likely for the first round to do
1097 useful work. We use AUX non-null to flag that the block is queued. */
1098 if (blocks_in)
1100 FOR_EACH_BB (bb)
1101 if (TEST_BIT (blocks_in, bb->index))
1103 *--qhead = bb;
1104 bb->aux = bb;
1107 else
1109 FOR_EACH_BB (bb)
1111 *--qhead = bb;
1112 bb->aux = bb;
1116 block_accesses = XCNEWVEC (int, last_basic_block);
1118 /* We clean aux when we remove the initially-enqueued bbs, but we
1119 don't enqueue ENTRY and EXIT initially, so clean them upfront and
1120 unconditionally. */
1121 ENTRY_BLOCK_PTR->aux = EXIT_BLOCK_PTR->aux = NULL;
1123 if (blocks_out)
1124 sbitmap_zero (blocks_out);
1126 /* We work through the queue until there are no more blocks. What
1127 is live at the end of this block is precisely the union of what
1128 is live at the beginning of all its successors. So, we set its
1129 GLOBAL_LIVE_AT_END field based on the GLOBAL_LIVE_AT_START field
1130 for its successors. Then, we compute GLOBAL_LIVE_AT_START for
1131 this block by walking through the instructions in this block in
1132 reverse order and updating as we go. If that changed
1133 GLOBAL_LIVE_AT_START, we add the predecessors of the block to the
1134 queue; they will now need to recalculate GLOBAL_LIVE_AT_END.
1136 We are guaranteed to terminate, because GLOBAL_LIVE_AT_START
1137 never shrinks. If a register appears in GLOBAL_LIVE_AT_START, it
1138 must either be live at the end of the block, or used within the
1139 block. In the latter case, it will certainly never disappear
1140 from GLOBAL_LIVE_AT_START. In the former case, the register
1141 could go away only if it disappeared from GLOBAL_LIVE_AT_START
1142 for one of the successor blocks. By induction, that cannot
1143 occur.
1145 ??? This reasoning doesn't work if we start from non-empty initial
1146 GLOBAL_LIVE_AT_START sets. And there are actually two problems:
1147 1) Updating may not terminate (endless oscillation).
1148 2) Even if it does (and it usually does), the resulting information
1149 may be inaccurate. Consider for example the following case:
1151 a = ...;
1152 while (...) {...} -- 'a' not mentioned at all
1153 ... = a;
1155 If the use of 'a' is deleted between two calculations of liveness
1156 information and the initial sets are not cleared, the information
1157 about a's liveness will get stuck inside the loop and the set will
1158 appear not to be dead.
1160 We do not attempt to solve 2) -- the information is conservatively
1161 correct (i.e. we never claim that something live is dead) and the
1162 amount of optimization opportunities missed due to this problem is
1163 not significant.
1165 1) is more serious. In order to fix it, we monitor the number of times
1166 each block is processed. Once one of the blocks has been processed more
1167 times than the maximum number of rounds, we use the following strategy:
1168 When a register disappears from one of the sets, we add it to a MAKE_DEAD
1169 set, remove all registers in this set from all GLOBAL_LIVE_AT_* sets and
1170 add the blocks with changed sets into the queue. Thus we are guaranteed
1171 to terminate (the worst case corresponds to all registers in MADE_DEAD,
1172 in which case the original reasoning above is valid), but in general we
1173 only fix up a few offending registers.
1175 The maximum number of rounds for computing liveness is the largest of
1176 MAX_LIVENESS_ROUNDS and the latest loop depth count for this function. */
1178 while (qhead != qtail)
1180 int rescan, changed;
1181 basic_block bb;
1182 edge e;
1183 edge_iterator ei;
1185 bb = *qhead++;
1186 if (qhead == qend)
1187 qhead = queue;
1188 bb->aux = NULL;
1190 /* Should we start using the failure strategy? */
1191 if (bb != ENTRY_BLOCK_PTR)
1193 int max_liveness_rounds =
1194 MAX (MAX_LIVENESS_ROUNDS, cfun->max_loop_depth);
1196 block_accesses[bb->index]++;
1197 if (block_accesses[bb->index] > max_liveness_rounds)
1198 failure_strategy_required = true;
1201 /* Begin by propagating live_at_start from the successor blocks. */
1202 CLEAR_REG_SET (new_live_at_end);
1204 if (EDGE_COUNT (bb->succs) > 0)
1205 FOR_EACH_EDGE (e, ei, bb->succs)
1207 basic_block sb = e->dest;
1209 /* Call-clobbered registers die across exception and
1210 call edges. */
1211 /* ??? Abnormal call edges ignored for the moment, as this gets
1212 confused by sibling call edges, which crashes reg-stack. */
1213 if (e->flags & EDGE_EH)
1214 bitmap_ior_and_compl_into (new_live_at_end,
1215 sb->il.rtl->global_live_at_start,
1216 invalidated_by_eh_edge);
1217 else
1218 IOR_REG_SET (new_live_at_end, sb->il.rtl->global_live_at_start);
1220 /* If a target saves one register in another (instead of on
1221 the stack) the save register will need to be live for EH. */
1222 if (e->flags & EDGE_EH)
1223 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1224 if (EH_USES (i))
1225 SET_REGNO_REG_SET (new_live_at_end, i);
1227 else
1229 /* This might be a noreturn function that throws. And
1230 even if it isn't, getting the unwind info right helps
1231 debugging. */
1232 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1233 if (EH_USES (i))
1234 SET_REGNO_REG_SET (new_live_at_end, i);
1237 /* The all-important stack pointer must always be live. */
1238 SET_REGNO_REG_SET (new_live_at_end, STACK_POINTER_REGNUM);
1240 /* Before reload, there are a few registers that must be forced
1241 live everywhere -- which might not already be the case for
1242 blocks within infinite loops. */
1243 if (! reload_completed)
1245 /* Any reference to any pseudo before reload is a potential
1246 reference of the frame pointer. */
1247 SET_REGNO_REG_SET (new_live_at_end, FRAME_POINTER_REGNUM);
1249 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1250 /* Pseudos with argument area equivalences may require
1251 reloading via the argument pointer. */
1252 if (fixed_regs[ARG_POINTER_REGNUM])
1253 SET_REGNO_REG_SET (new_live_at_end, ARG_POINTER_REGNUM);
1254 #endif
1256 /* Any constant, or pseudo with constant equivalences, may
1257 require reloading from memory using the pic register. */
1258 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1259 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1260 SET_REGNO_REG_SET (new_live_at_end, PIC_OFFSET_TABLE_REGNUM);
1263 if (bb == ENTRY_BLOCK_PTR)
1265 COPY_REG_SET (bb->il.rtl->global_live_at_end, new_live_at_end);
1266 continue;
1269 /* On our first pass through this block, we'll go ahead and continue.
1270 Recognize first pass by checking if local_set is NULL for this
1271 basic block. On subsequent passes, we get to skip out early if
1272 live_at_end wouldn't have changed. */
1274 if (local_sets[bb->index] == NULL)
1276 local_sets[bb->index] = ALLOC_REG_SET (&reg_obstack);
1277 cond_local_sets[bb->index] = ALLOC_REG_SET (&reg_obstack);
1278 rescan = 1;
1280 else
1282 /* If any bits were removed from live_at_end, we'll have to
1283 rescan the block. This wouldn't be necessary if we had
1284 precalculated local_live, however with PROP_SCAN_DEAD_CODE
1285 local_live is really dependent on live_at_end. */
1286 rescan = bitmap_intersect_compl_p (bb->il.rtl->global_live_at_end,
1287 new_live_at_end);
1289 if (!rescan)
1291 regset cond_local_set;
1293 /* If any of the registers in the new live_at_end set are
1294 conditionally set in this basic block, we must rescan.
1295 This is because conditional lifetimes at the end of the
1296 block do not just take the live_at_end set into
1297 account, but also the liveness at the start of each
1298 successor block. We can miss changes in those sets if
1299 we only compare the new live_at_end against the
1300 previous one. */
1301 cond_local_set = cond_local_sets[bb->index];
1302 rescan = bitmap_intersect_p (new_live_at_end, cond_local_set);
1305 if (!rescan)
1307 regset local_set;
1309 /* Find the set of changed bits. Take this opportunity
1310 to notice that this set is empty and early out. */
1311 bitmap_xor (tmp, bb->il.rtl->global_live_at_end, new_live_at_end);
1312 if (bitmap_empty_p (tmp))
1313 continue;
1315 /* If any of the changed bits overlap with local_sets[bb],
1316 we'll have to rescan the block. */
1317 local_set = local_sets[bb->index];
1318 rescan = bitmap_intersect_p (tmp, local_set);
1322 /* Let our caller know that BB changed enough to require its
1323 death notes updated. */
1324 if (blocks_out)
1325 SET_BIT (blocks_out, bb->index);
1327 if (! rescan)
1329 /* Add to live_at_start the set of all registers in
1330 new_live_at_end that aren't in the old live_at_end. */
1332 changed = bitmap_ior_and_compl_into (bb->il.rtl->global_live_at_start,
1333 new_live_at_end,
1334 bb->il.rtl->global_live_at_end);
1335 COPY_REG_SET (bb->il.rtl->global_live_at_end, new_live_at_end);
1336 if (! changed)
1337 continue;
1339 else
1341 COPY_REG_SET (bb->il.rtl->global_live_at_end, new_live_at_end);
1343 /* Rescan the block insn by insn to turn (a copy of) live_at_end
1344 into live_at_start. */
1345 propagate_block (bb, new_live_at_end,
1346 local_sets[bb->index],
1347 cond_local_sets[bb->index],
1348 flags);
1350 /* If live_at start didn't change, no need to go farther. */
1351 if (REG_SET_EQUAL_P (bb->il.rtl->global_live_at_start,
1352 new_live_at_end))
1353 continue;
1355 if (failure_strategy_required)
1357 /* Get the list of registers that were removed from the
1358 bb->global_live_at_start set. */
1359 bitmap_and_compl (tmp, bb->il.rtl->global_live_at_start,
1360 new_live_at_end);
1361 if (!bitmap_empty_p (tmp))
1363 bool pbb_changed;
1364 basic_block pbb;
1366 /* It should not happen that one of registers we have
1367 removed last time is disappears again before any other
1368 register does. */
1369 pbb_changed = bitmap_ior_into (registers_made_dead, tmp);
1370 gcc_assert (pbb_changed);
1372 /* Now remove the registers from all sets. */
1373 FOR_EACH_BB (pbb)
1375 pbb_changed = false;
1377 pbb_changed
1378 |= bitmap_and_compl_into
1379 (pbb->il.rtl->global_live_at_start,
1380 registers_made_dead);
1381 pbb_changed
1382 |= bitmap_and_compl_into
1383 (pbb->il.rtl->global_live_at_end,
1384 registers_made_dead);
1385 if (!pbb_changed)
1386 continue;
1388 /* Note the (possible) change. */
1389 if (blocks_out)
1390 SET_BIT (blocks_out, pbb->index);
1392 /* Makes sure to really rescan the block. */
1393 if (local_sets[pbb->index])
1395 FREE_REG_SET (local_sets[pbb->index]);
1396 FREE_REG_SET (cond_local_sets[pbb->index]);
1397 local_sets[pbb->index] = 0;
1400 /* Add it to the queue. */
1401 if (pbb->aux == NULL)
1403 *qtail++ = pbb;
1404 if (qtail == qend)
1405 qtail = queue;
1406 pbb->aux = pbb;
1409 continue;
1411 } /* end of failure_strategy_required */
1413 COPY_REG_SET (bb->il.rtl->global_live_at_start, new_live_at_end);
1416 /* Queue all predecessors of BB so that we may re-examine
1417 their live_at_end. */
1418 FOR_EACH_EDGE (e, ei, bb->preds)
1420 basic_block pb = e->src;
1422 gcc_assert ((e->flags & EDGE_FAKE) == 0);
1424 if (pb->aux == NULL)
1426 *qtail++ = pb;
1427 if (qtail == qend)
1428 qtail = queue;
1429 pb->aux = pb;
1434 FREE_REG_SET (tmp);
1435 FREE_REG_SET (new_live_at_end);
1436 FREE_REG_SET (invalidated_by_eh_edge);
1437 FREE_REG_SET (registers_made_dead);
1439 if (blocks_out)
1441 sbitmap_iterator sbi;
1443 EXECUTE_IF_SET_IN_SBITMAP (blocks_out, 0, i, sbi)
1445 basic_block bb = BASIC_BLOCK (i);
1446 FREE_REG_SET (local_sets[bb->index]);
1447 FREE_REG_SET (cond_local_sets[bb->index]);
1450 else
1452 FOR_EACH_BB (bb)
1454 FREE_REG_SET (local_sets[bb->index]);
1455 FREE_REG_SET (cond_local_sets[bb->index]);
1459 free (block_accesses);
1460 free (queue);
1461 free (cond_local_sets);
1462 free (local_sets);
1466 /* This structure is used to pass parameters to and from the
1467 the function find_regno_partial(). It is used to pass in the
1468 register number we are looking, as well as to return any rtx
1469 we find. */
1471 typedef struct {
1472 unsigned regno_to_find;
1473 rtx retval;
1474 } find_regno_partial_param;
1477 /* Find the rtx for the reg numbers specified in 'data' if it is
1478 part of an expression which only uses part of the register. Return
1479 it in the structure passed in. */
1480 static int
1481 find_regno_partial (rtx *ptr, void *data)
1483 find_regno_partial_param *param = (find_regno_partial_param *)data;
1484 unsigned reg = param->regno_to_find;
1485 param->retval = NULL_RTX;
1487 if (*ptr == NULL_RTX)
1488 return 0;
1490 switch (GET_CODE (*ptr))
1492 case ZERO_EXTRACT:
1493 case SIGN_EXTRACT:
1494 case STRICT_LOW_PART:
1495 if (REG_P (XEXP (*ptr, 0)) && REGNO (XEXP (*ptr, 0)) == reg)
1497 param->retval = XEXP (*ptr, 0);
1498 return 1;
1500 break;
1502 case SUBREG:
1503 if (REG_P (SUBREG_REG (*ptr))
1504 && REGNO (SUBREG_REG (*ptr)) == reg)
1506 param->retval = SUBREG_REG (*ptr);
1507 return 1;
1509 break;
1511 default:
1512 break;
1515 return 0;
1518 /* Process all immediate successors of the entry block looking for pseudo
1519 registers which are live on entry. Find all of those whose first
1520 instance is a partial register reference of some kind, and initialize
1521 them to 0 after the entry block. This will prevent bit sets within
1522 registers whose value is unknown, and may contain some kind of sticky
1523 bits we don't want. */
1525 static int
1526 initialize_uninitialized_subregs (void)
1528 rtx insn;
1529 edge e;
1530 unsigned reg, did_something = 0;
1531 find_regno_partial_param param;
1532 edge_iterator ei;
1534 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
1536 basic_block bb = e->dest;
1537 regset map = bb->il.rtl->global_live_at_start;
1538 reg_set_iterator rsi;
1540 EXECUTE_IF_SET_IN_REG_SET (map, FIRST_PSEUDO_REGISTER, reg, rsi)
1542 int uid = REGNO_FIRST_UID (reg);
1543 rtx i;
1545 /* Find an insn which mentions the register we are looking for.
1546 Its preferable to have an instance of the register's rtl since
1547 there may be various flags set which we need to duplicate.
1548 If we can't find it, its probably an automatic whose initial
1549 value doesn't matter, or hopefully something we don't care about. */
1550 for (i = get_insns (); i && INSN_UID (i) != uid; i = NEXT_INSN (i))
1552 if (i != NULL_RTX)
1554 /* Found the insn, now get the REG rtx, if we can. */
1555 param.regno_to_find = reg;
1556 for_each_rtx (&i, find_regno_partial, &param);
1557 if (param.retval != NULL_RTX)
1559 start_sequence ();
1560 emit_move_insn (param.retval,
1561 CONST0_RTX (GET_MODE (param.retval)));
1562 insn = get_insns ();
1563 end_sequence ();
1564 insert_insn_on_edge (insn, e);
1565 did_something = 1;
1571 if (did_something)
1572 commit_edge_insertions ();
1573 return did_something;
1577 /* Subroutines of life analysis. */
1579 /* Allocate the permanent data structures that represent the results
1580 of life analysis. */
1582 static void
1583 allocate_bb_life_data (void)
1585 basic_block bb;
1587 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1589 if (bb->il.rtl->global_live_at_start)
1591 CLEAR_REG_SET (bb->il.rtl->global_live_at_start);
1592 CLEAR_REG_SET (bb->il.rtl->global_live_at_end);
1594 else
1596 bb->il.rtl->global_live_at_start = ALLOC_REG_SET (&reg_obstack);
1597 bb->il.rtl->global_live_at_end = ALLOC_REG_SET (&reg_obstack);
1601 regs_live_at_setjmp = ALLOC_REG_SET (&reg_obstack);
1604 void
1605 allocate_reg_life_data (void)
1607 int i;
1609 max_regno = max_reg_num ();
1610 gcc_assert (!reg_deaths);
1611 reg_deaths = XCNEWVEC (int, max_regno);
1613 /* Recalculate the register space, in case it has grown. Old style
1614 vector oriented regsets would set regset_{size,bytes} here also. */
1615 allocate_reg_info (max_regno, FALSE, FALSE);
1617 /* Reset all the data we'll collect in propagate_block and its
1618 subroutines. */
1619 for (i = 0; i < max_regno; i++)
1621 REG_N_SETS (i) = 0;
1622 REG_N_REFS (i) = 0;
1623 REG_N_DEATHS (i) = 0;
1624 REG_N_CALLS_CROSSED (i) = 0;
1625 REG_N_THROWING_CALLS_CROSSED (i) = 0;
1626 REG_LIVE_LENGTH (i) = 0;
1627 REG_FREQ (i) = 0;
1628 REG_BASIC_BLOCK (i) = REG_BLOCK_UNKNOWN;
1632 /* Delete dead instructions for propagate_block. */
1634 static void
1635 propagate_block_delete_insn (rtx insn)
1637 rtx inote = find_reg_note (insn, REG_LABEL, NULL_RTX);
1639 /* If the insn referred to a label, and that label was attached to
1640 an ADDR_VEC, it's safe to delete the ADDR_VEC. In fact, it's
1641 pretty much mandatory to delete it, because the ADDR_VEC may be
1642 referencing labels that no longer exist.
1644 INSN may reference a deleted label, particularly when a jump
1645 table has been optimized into a direct jump. There's no
1646 real good way to fix up the reference to the deleted label
1647 when the label is deleted, so we just allow it here. */
1649 if (inote && LABEL_P (inote))
1651 rtx label = XEXP (inote, 0);
1652 rtx next;
1654 /* The label may be forced if it has been put in the constant
1655 pool. If that is the only use we must discard the table
1656 jump following it, but not the label itself. */
1657 if (LABEL_NUSES (label) == 1 + LABEL_PRESERVE_P (label)
1658 && (next = next_nonnote_insn (label)) != NULL
1659 && JUMP_P (next)
1660 && (GET_CODE (PATTERN (next)) == ADDR_VEC
1661 || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC))
1663 rtx pat = PATTERN (next);
1664 int diff_vec_p = GET_CODE (pat) == ADDR_DIFF_VEC;
1665 int len = XVECLEN (pat, diff_vec_p);
1666 int i;
1668 for (i = 0; i < len; i++)
1669 LABEL_NUSES (XEXP (XVECEXP (pat, diff_vec_p, i), 0))--;
1671 delete_insn_and_edges (next);
1672 ndead++;
1676 delete_insn_and_edges (insn);
1677 ndead++;
1680 /* Delete dead libcalls for propagate_block. Return the insn
1681 before the libcall. */
1683 static rtx
1684 propagate_block_delete_libcall (rtx insn, rtx note)
1686 rtx first = XEXP (note, 0);
1687 rtx before = PREV_INSN (first);
1689 delete_insn_chain_and_edges (first, insn);
1690 ndead++;
1691 return before;
1694 /* Update the life-status of regs for one insn. Return the previous insn. */
1697 propagate_one_insn (struct propagate_block_info *pbi, rtx insn)
1699 rtx prev = PREV_INSN (insn);
1700 int flags = pbi->flags;
1701 int insn_is_dead = 0;
1702 int libcall_is_dead = 0;
1703 rtx note;
1704 unsigned i;
1706 if (! INSN_P (insn))
1707 return prev;
1709 note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
1710 if (flags & PROP_SCAN_DEAD_CODE)
1712 insn_is_dead = insn_dead_p (pbi, PATTERN (insn), 0, REG_NOTES (insn));
1713 libcall_is_dead = (insn_is_dead && note != 0
1714 && libcall_dead_p (pbi, note, insn));
1717 /* If an instruction consists of just dead store(s) on final pass,
1718 delete it. */
1719 if ((flags & PROP_KILL_DEAD_CODE) && insn_is_dead)
1721 /* If we're trying to delete a prologue or epilogue instruction
1722 that isn't flagged as possibly being dead, something is wrong.
1723 But if we are keeping the stack pointer depressed, we might well
1724 be deleting insns that are used to compute the amount to update
1725 it by, so they are fine. */
1726 if (reload_completed
1727 && !(TREE_CODE (TREE_TYPE (current_function_decl)) == FUNCTION_TYPE
1728 && (TYPE_RETURNS_STACK_DEPRESSED
1729 (TREE_TYPE (current_function_decl))))
1730 && (((HAVE_epilogue || HAVE_prologue)
1731 && prologue_epilogue_contains (insn))
1732 || (HAVE_sibcall_epilogue
1733 && sibcall_epilogue_contains (insn)))
1734 && find_reg_note (insn, REG_MAYBE_DEAD, NULL_RTX) == 0)
1735 fatal_insn ("Attempt to delete prologue/epilogue insn:", insn);
1737 /* Record sets. Do this even for dead instructions, since they
1738 would have killed the values if they hadn't been deleted. To
1739 be consistent, we also have to emit a clobber when we delete
1740 an insn that clobbers a live register. */
1741 pbi->flags |= PROP_DEAD_INSN;
1742 mark_set_regs (pbi, PATTERN (insn), insn);
1743 pbi->flags &= ~PROP_DEAD_INSN;
1745 /* CC0 is now known to be dead. Either this insn used it,
1746 in which case it doesn't anymore, or clobbered it,
1747 so the next insn can't use it. */
1748 pbi->cc0_live = 0;
1750 if (libcall_is_dead)
1751 prev = propagate_block_delete_libcall (insn, note);
1752 else
1755 /* If INSN contains a RETVAL note and is dead, but the libcall
1756 as a whole is not dead, then we want to remove INSN, but
1757 not the whole libcall sequence.
1759 However, we need to also remove the dangling REG_LIBCALL
1760 note so that we do not have mis-matched LIBCALL/RETVAL
1761 notes. In theory we could find a new location for the
1762 REG_RETVAL note, but it hardly seems worth the effort.
1764 NOTE at this point will be the RETVAL note if it exists. */
1765 if (note)
1767 rtx libcall_note;
1769 libcall_note
1770 = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
1771 remove_note (XEXP (note, 0), libcall_note);
1774 /* Similarly if INSN contains a LIBCALL note, remove the
1775 dangling REG_RETVAL note. */
1776 note = find_reg_note (insn, REG_LIBCALL, NULL_RTX);
1777 if (note)
1779 rtx retval_note;
1781 retval_note
1782 = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
1783 remove_note (XEXP (note, 0), retval_note);
1786 /* Now delete INSN. */
1787 propagate_block_delete_insn (insn);
1790 return prev;
1793 /* See if this is an increment or decrement that can be merged into
1794 a following memory address. */
1795 #ifdef AUTO_INC_DEC
1797 rtx x = single_set (insn);
1799 /* Does this instruction increment or decrement a register? */
1800 if ((flags & PROP_AUTOINC)
1801 && x != 0
1802 && REG_P (SET_DEST (x))
1803 && (GET_CODE (SET_SRC (x)) == PLUS
1804 || GET_CODE (SET_SRC (x)) == MINUS)
1805 && XEXP (SET_SRC (x), 0) == SET_DEST (x)
1806 && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
1807 /* Ok, look for a following memory ref we can combine with.
1808 If one is found, change the memory ref to a PRE_INC
1809 or PRE_DEC, cancel this insn, and return 1.
1810 Return 0 if nothing has been done. */
1811 && try_pre_increment_1 (pbi, insn))
1812 return prev;
1814 #endif /* AUTO_INC_DEC */
1816 CLEAR_REG_SET (pbi->new_set);
1818 /* If this is not the final pass, and this insn is copying the value of
1819 a library call and it's dead, don't scan the insns that perform the
1820 library call, so that the call's arguments are not marked live. */
1821 if (libcall_is_dead)
1823 /* Record the death of the dest reg. */
1824 mark_set_regs (pbi, PATTERN (insn), insn);
1826 insn = XEXP (note, 0);
1827 return PREV_INSN (insn);
1829 else if (GET_CODE (PATTERN (insn)) == SET
1830 && SET_DEST (PATTERN (insn)) == stack_pointer_rtx
1831 && GET_CODE (SET_SRC (PATTERN (insn))) == PLUS
1832 && XEXP (SET_SRC (PATTERN (insn)), 0) == stack_pointer_rtx
1833 && GET_CODE (XEXP (SET_SRC (PATTERN (insn)), 1)) == CONST_INT)
1835 /* We have an insn to pop a constant amount off the stack.
1836 (Such insns use PLUS regardless of the direction of the stack,
1837 and any insn to adjust the stack by a constant is always a pop
1838 or part of a push.)
1839 These insns, if not dead stores, have no effect on life, though
1840 they do have an effect on the memory stores we are tracking. */
1841 invalidate_mems_from_set (pbi, stack_pointer_rtx);
1842 /* Still, we need to update local_set, lest ifcvt.c:dead_or_predicable
1843 concludes that the stack pointer is not modified. */
1844 mark_set_regs (pbi, PATTERN (insn), insn);
1846 else
1848 /* Any regs live at the time of a call instruction must not go
1849 in a register clobbered by calls. Find all regs now live and
1850 record this for them. */
1852 if (CALL_P (insn) && (flags & PROP_REG_INFO))
1854 reg_set_iterator rsi;
1855 EXECUTE_IF_SET_IN_REG_SET (pbi->reg_live, 0, i, rsi)
1856 REG_N_CALLS_CROSSED (i)++;
1857 if (can_throw_internal (insn))
1858 EXECUTE_IF_SET_IN_REG_SET (pbi->reg_live, 0, i, rsi)
1859 REG_N_THROWING_CALLS_CROSSED (i)++;
1862 /* Record sets. Do this even for dead instructions, since they
1863 would have killed the values if they hadn't been deleted. */
1864 mark_set_regs (pbi, PATTERN (insn), insn);
1866 if (CALL_P (insn))
1868 regset live_at_end;
1869 bool sibcall_p;
1870 rtx note, cond;
1871 int i;
1873 cond = NULL_RTX;
1874 if (GET_CODE (PATTERN (insn)) == COND_EXEC)
1875 cond = COND_EXEC_TEST (PATTERN (insn));
1877 /* Non-constant calls clobber memory, constant calls do not
1878 clobber memory, though they may clobber outgoing arguments
1879 on the stack. */
1880 if (! CONST_OR_PURE_CALL_P (insn))
1882 free_EXPR_LIST_list (&pbi->mem_set_list);
1883 pbi->mem_set_list_len = 0;
1885 else
1886 invalidate_mems_from_set (pbi, stack_pointer_rtx);
1888 /* There may be extra registers to be clobbered. */
1889 for (note = CALL_INSN_FUNCTION_USAGE (insn);
1890 note;
1891 note = XEXP (note, 1))
1892 if (GET_CODE (XEXP (note, 0)) == CLOBBER)
1893 mark_set_1 (pbi, CLOBBER, XEXP (XEXP (note, 0), 0),
1894 cond, insn, pbi->flags);
1896 /* Calls change all call-used and global registers; sibcalls do not
1897 clobber anything that must be preserved at end-of-function,
1898 except for return values. */
1900 sibcall_p = SIBLING_CALL_P (insn);
1901 live_at_end = EXIT_BLOCK_PTR->il.rtl->global_live_at_start;
1902 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1903 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i)
1904 && ! (sibcall_p
1905 && REGNO_REG_SET_P (live_at_end, i)
1906 && ! refers_to_regno_p (i, i+1,
1907 current_function_return_rtx,
1908 (rtx *) 0)))
1910 enum rtx_code code = global_regs[i] ? SET : CLOBBER;
1911 /* We do not want REG_UNUSED notes for these registers. */
1912 mark_set_1 (pbi, code, regno_reg_rtx[i], cond, insn,
1913 pbi->flags & ~(PROP_DEATH_NOTES | PROP_REG_INFO));
1917 /* If an insn doesn't use CC0, it becomes dead since we assume
1918 that every insn clobbers it. So show it dead here;
1919 mark_used_regs will set it live if it is referenced. */
1920 pbi->cc0_live = 0;
1922 /* Record uses. */
1923 if (! insn_is_dead)
1924 mark_used_regs (pbi, PATTERN (insn), NULL_RTX, insn);
1926 /* Sometimes we may have inserted something before INSN (such as a move)
1927 when we make an auto-inc. So ensure we will scan those insns. */
1928 #ifdef AUTO_INC_DEC
1929 prev = PREV_INSN (insn);
1930 #endif
1932 if (! insn_is_dead && CALL_P (insn))
1934 int i;
1935 rtx note, cond;
1937 cond = NULL_RTX;
1938 if (GET_CODE (PATTERN (insn)) == COND_EXEC)
1939 cond = COND_EXEC_TEST (PATTERN (insn));
1941 /* Calls use their arguments, and may clobber memory which
1942 address involves some register. */
1943 for (note = CALL_INSN_FUNCTION_USAGE (insn);
1944 note;
1945 note = XEXP (note, 1))
1946 /* We find USE or CLOBBER entities in a FUNCTION_USAGE list: both
1947 of which mark_used_regs knows how to handle. */
1948 mark_used_regs (pbi, XEXP (XEXP (note, 0), 0), cond, insn);
1950 /* The stack ptr is used (honorarily) by a CALL insn. */
1951 if ((flags & PROP_REG_INFO)
1952 && !REGNO_REG_SET_P (pbi->reg_live, STACK_POINTER_REGNUM))
1953 reg_deaths[STACK_POINTER_REGNUM] = pbi->insn_num;
1954 SET_REGNO_REG_SET (pbi->reg_live, STACK_POINTER_REGNUM);
1956 /* Calls may also reference any of the global registers,
1957 so they are made live. */
1958 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1959 if (global_regs[i])
1960 mark_used_reg (pbi, regno_reg_rtx[i], cond, insn);
1964 pbi->insn_num++;
1966 return prev;
1969 /* Initialize a propagate_block_info struct for public consumption.
1970 Note that the structure itself is opaque to this file, but that
1971 the user can use the regsets provided here. */
1973 struct propagate_block_info *
1974 init_propagate_block_info (basic_block bb, regset live, regset local_set,
1975 regset cond_local_set, int flags)
1977 struct propagate_block_info *pbi = XNEW (struct propagate_block_info);
1979 pbi->bb = bb;
1980 pbi->reg_live = live;
1981 pbi->mem_set_list = NULL_RTX;
1982 pbi->mem_set_list_len = 0;
1983 pbi->local_set = local_set;
1984 pbi->cond_local_set = cond_local_set;
1985 pbi->cc0_live = 0;
1986 pbi->flags = flags;
1987 pbi->insn_num = 0;
1989 if (flags & (PROP_LOG_LINKS | PROP_AUTOINC))
1990 pbi->reg_next_use = XCNEWVEC (rtx, max_reg_num ());
1991 else
1992 pbi->reg_next_use = NULL;
1994 pbi->new_set = BITMAP_ALLOC (NULL);
1996 #ifdef HAVE_conditional_execution
1997 pbi->reg_cond_dead = splay_tree_new (splay_tree_compare_ints, NULL,
1998 free_reg_cond_life_info);
1999 pbi->reg_cond_reg = BITMAP_ALLOC (NULL);
2001 /* If this block ends in a conditional branch, for each register
2002 live from one side of the branch and not the other, record the
2003 register as conditionally dead. */
2004 if (JUMP_P (BB_END (bb))
2005 && any_condjump_p (BB_END (bb)))
2007 regset diff = ALLOC_REG_SET (&reg_obstack);
2008 basic_block bb_true, bb_false;
2009 unsigned i;
2011 /* Identify the successor blocks. */
2012 bb_true = EDGE_SUCC (bb, 0)->dest;
2013 if (!single_succ_p (bb))
2015 bb_false = EDGE_SUCC (bb, 1)->dest;
2017 if (EDGE_SUCC (bb, 0)->flags & EDGE_FALLTHRU)
2019 basic_block t = bb_false;
2020 bb_false = bb_true;
2021 bb_true = t;
2023 else
2024 gcc_assert (EDGE_SUCC (bb, 1)->flags & EDGE_FALLTHRU);
2026 else
2028 /* This can happen with a conditional jump to the next insn. */
2029 gcc_assert (JUMP_LABEL (BB_END (bb)) == BB_HEAD (bb_true));
2031 /* Simplest way to do nothing. */
2032 bb_false = bb_true;
2035 /* Compute which register lead different lives in the successors. */
2036 bitmap_xor (diff, bb_true->il.rtl->global_live_at_start,
2037 bb_false->il.rtl->global_live_at_start);
2039 if (!bitmap_empty_p (diff))
2041 /* Extract the condition from the branch. */
2042 rtx set_src = SET_SRC (pc_set (BB_END (bb)));
2043 rtx cond_true = XEXP (set_src, 0);
2044 rtx reg = XEXP (cond_true, 0);
2045 enum rtx_code inv_cond;
2047 if (GET_CODE (reg) == SUBREG)
2048 reg = SUBREG_REG (reg);
2050 /* We can only track conditional lifetimes if the condition is
2051 in the form of a reversible comparison of a register against
2052 zero. If the condition is more complex than that, then it is
2053 safe not to record any information. */
2054 inv_cond = reversed_comparison_code (cond_true, BB_END (bb));
2055 if (inv_cond != UNKNOWN
2056 && REG_P (reg)
2057 && XEXP (cond_true, 1) == const0_rtx)
2059 rtx cond_false
2060 = gen_rtx_fmt_ee (inv_cond,
2061 GET_MODE (cond_true), XEXP (cond_true, 0),
2062 XEXP (cond_true, 1));
2063 reg_set_iterator rsi;
2065 if (GET_CODE (XEXP (set_src, 1)) == PC)
2067 rtx t = cond_false;
2068 cond_false = cond_true;
2069 cond_true = t;
2072 SET_REGNO_REG_SET (pbi->reg_cond_reg, REGNO (reg));
2074 /* For each such register, mark it conditionally dead. */
2075 EXECUTE_IF_SET_IN_REG_SET (diff, 0, i, rsi)
2077 struct reg_cond_life_info *rcli;
2078 rtx cond;
2080 rcli = XNEW (struct reg_cond_life_info);
2082 if (REGNO_REG_SET_P (bb_true->il.rtl->global_live_at_start,
2084 cond = cond_false;
2085 else
2086 cond = cond_true;
2087 rcli->condition = cond;
2088 rcli->stores = const0_rtx;
2089 rcli->orig_condition = cond;
2091 splay_tree_insert (pbi->reg_cond_dead, i,
2092 (splay_tree_value) rcli);
2097 FREE_REG_SET (diff);
2099 #endif
2101 /* If this block has no successors, any stores to the frame that aren't
2102 used later in the block are dead. So make a pass over the block
2103 recording any such that are made and show them dead at the end. We do
2104 a very conservative and simple job here. */
2105 if (optimize
2106 && ! (TREE_CODE (TREE_TYPE (current_function_decl)) == FUNCTION_TYPE
2107 && (TYPE_RETURNS_STACK_DEPRESSED
2108 (TREE_TYPE (current_function_decl))))
2109 && (flags & PROP_SCAN_DEAD_STORES)
2110 && (EDGE_COUNT (bb->succs) == 0
2111 || (single_succ_p (bb)
2112 && single_succ (bb) == EXIT_BLOCK_PTR
2113 && ! current_function_calls_eh_return)))
2115 rtx insn, set;
2116 for (insn = BB_END (bb); insn != BB_HEAD (bb); insn = PREV_INSN (insn))
2117 if (NONJUMP_INSN_P (insn)
2118 && (set = single_set (insn))
2119 && MEM_P (SET_DEST (set)))
2121 rtx mem = SET_DEST (set);
2122 rtx canon_mem = canon_rtx (mem);
2124 if (XEXP (canon_mem, 0) == frame_pointer_rtx
2125 || (GET_CODE (XEXP (canon_mem, 0)) == PLUS
2126 && XEXP (XEXP (canon_mem, 0), 0) == frame_pointer_rtx
2127 && GET_CODE (XEXP (XEXP (canon_mem, 0), 1)) == CONST_INT))
2128 add_to_mem_set_list (pbi, canon_mem);
2132 return pbi;
2135 /* Release a propagate_block_info struct. */
2137 void
2138 free_propagate_block_info (struct propagate_block_info *pbi)
2140 free_EXPR_LIST_list (&pbi->mem_set_list);
2142 BITMAP_FREE (pbi->new_set);
2144 #ifdef HAVE_conditional_execution
2145 splay_tree_delete (pbi->reg_cond_dead);
2146 BITMAP_FREE (pbi->reg_cond_reg);
2147 #endif
2149 if (pbi->flags & PROP_REG_INFO)
2151 int num = pbi->insn_num;
2152 unsigned i;
2153 reg_set_iterator rsi;
2155 EXECUTE_IF_SET_IN_REG_SET (pbi->reg_live, 0, i, rsi)
2157 REG_LIVE_LENGTH (i) += num - reg_deaths[i];
2158 reg_deaths[i] = 0;
2161 if (pbi->reg_next_use)
2162 free (pbi->reg_next_use);
2164 free (pbi);
2167 /* Compute the registers live at the beginning of a basic block BB from
2168 those live at the end.
2170 When called, REG_LIVE contains those live at the end. On return, it
2171 contains those live at the beginning.
2173 LOCAL_SET, if non-null, will be set with all registers killed
2174 unconditionally by this basic block.
2175 Likewise, COND_LOCAL_SET, if non-null, will be set with all registers
2176 killed conditionally by this basic block. If there is any unconditional
2177 set of a register, then the corresponding bit will be set in LOCAL_SET
2178 and cleared in COND_LOCAL_SET.
2179 It is valid for LOCAL_SET and COND_LOCAL_SET to be the same set. In this
2180 case, the resulting set will be equal to the union of the two sets that
2181 would otherwise be computed.
2183 Return nonzero if an INSN is deleted (i.e. by dead code removal). */
2186 propagate_block (basic_block bb, regset live, regset local_set,
2187 regset cond_local_set, int flags)
2189 struct propagate_block_info *pbi;
2190 rtx insn, prev;
2191 int changed;
2193 pbi = init_propagate_block_info (bb, live, local_set, cond_local_set, flags);
2195 if (flags & PROP_REG_INFO)
2197 unsigned i;
2198 reg_set_iterator rsi;
2200 /* Process the regs live at the end of the block.
2201 Mark them as not local to any one basic block. */
2202 EXECUTE_IF_SET_IN_REG_SET (live, 0, i, rsi)
2203 REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL;
2206 /* Scan the block an insn at a time from end to beginning. */
2208 changed = 0;
2209 for (insn = BB_END (bb); ; insn = prev)
2211 /* If this is a call to `setjmp' et al, warn if any
2212 non-volatile datum is live. */
2213 if ((flags & PROP_REG_INFO)
2214 && CALL_P (insn)
2215 && find_reg_note (insn, REG_SETJMP, NULL))
2216 IOR_REG_SET (regs_live_at_setjmp, pbi->reg_live);
2218 prev = propagate_one_insn (pbi, insn);
2219 if (!prev)
2220 changed |= insn != get_insns ();
2221 else
2222 changed |= NEXT_INSN (prev) != insn;
2224 if (insn == BB_HEAD (bb))
2225 break;
2228 #ifdef EH_RETURN_DATA_REGNO
2229 if (bb_has_eh_pred (bb))
2231 unsigned int i;
2232 for (i = 0; ; ++i)
2234 unsigned regno = EH_RETURN_DATA_REGNO (i);
2235 if (regno == INVALID_REGNUM)
2236 break;
2237 if (pbi->local_set)
2239 CLEAR_REGNO_REG_SET (pbi->cond_local_set, regno);
2240 SET_REGNO_REG_SET (pbi->local_set, regno);
2242 if (REGNO_REG_SET_P (pbi->reg_live, regno))
2243 SET_REGNO_REG_SET (pbi->new_set, regno);
2245 regs_ever_live[regno] = 1;
2248 #endif
2250 free_propagate_block_info (pbi);
2252 return changed;
2255 /* Return 1 if X (the body of an insn, or part of it) is just dead stores
2256 (SET expressions whose destinations are registers dead after the insn).
2257 NEEDED is the regset that says which regs are alive after the insn.
2259 Unless CALL_OK is nonzero, an insn is needed if it contains a CALL.
2261 If X is the entire body of an insn, NOTES contains the reg notes
2262 pertaining to the insn. */
2264 static int
2265 insn_dead_p (struct propagate_block_info *pbi, rtx x, int call_ok,
2266 rtx notes ATTRIBUTE_UNUSED)
2268 enum rtx_code code = GET_CODE (x);
2270 /* Don't eliminate insns that may trap. */
2271 if (flag_non_call_exceptions && may_trap_p (x))
2272 return 0;
2274 #ifdef AUTO_INC_DEC
2275 /* As flow is invoked after combine, we must take existing AUTO_INC
2276 expressions into account. */
2277 for (; notes; notes = XEXP (notes, 1))
2279 if (REG_NOTE_KIND (notes) == REG_INC)
2281 int regno = REGNO (XEXP (notes, 0));
2283 /* Don't delete insns to set global regs. */
2284 if ((regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
2285 || REGNO_REG_SET_P (pbi->reg_live, regno))
2286 return 0;
2289 #endif
2291 /* If setting something that's a reg or part of one,
2292 see if that register's altered value will be live. */
2294 if (code == SET)
2296 rtx r = SET_DEST (x);
2298 #ifdef HAVE_cc0
2299 if (GET_CODE (r) == CC0)
2300 return ! pbi->cc0_live;
2301 #endif
2303 /* A SET that is a subroutine call cannot be dead. */
2304 if (GET_CODE (SET_SRC (x)) == CALL)
2306 if (! call_ok)
2307 return 0;
2310 /* Don't eliminate loads from volatile memory or volatile asms. */
2311 else if (volatile_refs_p (SET_SRC (x)))
2312 return 0;
2314 if (MEM_P (r))
2316 rtx temp, canon_r;
2318 if (MEM_VOLATILE_P (r) || GET_MODE (r) == BLKmode)
2319 return 0;
2321 canon_r = canon_rtx (r);
2323 /* Walk the set of memory locations we are currently tracking
2324 and see if one is an identical match to this memory location.
2325 If so, this memory write is dead (remember, we're walking
2326 backwards from the end of the block to the start). Since
2327 rtx_equal_p does not check the alias set or flags, we also
2328 must have the potential for them to conflict (anti_dependence). */
2329 for (temp = pbi->mem_set_list; temp != 0; temp = XEXP (temp, 1))
2330 if (anti_dependence (r, XEXP (temp, 0)))
2332 rtx mem = XEXP (temp, 0);
2334 if (rtx_equal_p (XEXP (canon_r, 0), XEXP (mem, 0))
2335 && (GET_MODE_SIZE (GET_MODE (canon_r))
2336 <= GET_MODE_SIZE (GET_MODE (mem))))
2337 return 1;
2339 #ifdef AUTO_INC_DEC
2340 /* Check if memory reference matches an auto increment. Only
2341 post increment/decrement or modify are valid. */
2342 if (GET_MODE (mem) == GET_MODE (r)
2343 && (GET_CODE (XEXP (mem, 0)) == POST_DEC
2344 || GET_CODE (XEXP (mem, 0)) == POST_INC
2345 || GET_CODE (XEXP (mem, 0)) == POST_MODIFY)
2346 && GET_MODE (XEXP (mem, 0)) == GET_MODE (r)
2347 && rtx_equal_p (XEXP (XEXP (mem, 0), 0), XEXP (r, 0)))
2348 return 1;
2349 #endif
2352 else
2354 while (GET_CODE (r) == SUBREG
2355 || GET_CODE (r) == STRICT_LOW_PART
2356 || GET_CODE (r) == ZERO_EXTRACT)
2357 r = XEXP (r, 0);
2359 if (REG_P (r))
2361 int regno = REGNO (r);
2363 /* Obvious. */
2364 if (REGNO_REG_SET_P (pbi->reg_live, regno))
2365 return 0;
2367 /* If this is a hard register, verify that subsequent
2368 words are not needed. */
2369 if (regno < FIRST_PSEUDO_REGISTER)
2371 int n = hard_regno_nregs[regno][GET_MODE (r)];
2373 while (--n > 0)
2374 if (REGNO_REG_SET_P (pbi->reg_live, regno+n))
2375 return 0;
2378 /* Don't delete insns to set global regs. */
2379 if (regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
2380 return 0;
2382 /* Make sure insns to set the stack pointer aren't deleted. */
2383 if (regno == STACK_POINTER_REGNUM)
2384 return 0;
2386 /* ??? These bits might be redundant with the force live bits
2387 in calculate_global_regs_live. We would delete from
2388 sequential sets; whether this actually affects real code
2389 for anything but the stack pointer I don't know. */
2390 /* Make sure insns to set the frame pointer aren't deleted. */
2391 if (regno == FRAME_POINTER_REGNUM
2392 && (! reload_completed || frame_pointer_needed))
2393 return 0;
2394 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2395 if (regno == HARD_FRAME_POINTER_REGNUM
2396 && (! reload_completed || frame_pointer_needed))
2397 return 0;
2398 #endif
2400 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2401 /* Make sure insns to set arg pointer are never deleted
2402 (if the arg pointer isn't fixed, there will be a USE
2403 for it, so we can treat it normally). */
2404 if (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
2405 return 0;
2406 #endif
2408 /* Otherwise, the set is dead. */
2409 return 1;
2414 /* If performing several activities, insn is dead if each activity
2415 is individually dead. Also, CLOBBERs and USEs can be ignored; a
2416 CLOBBER or USE that's inside a PARALLEL doesn't make the insn
2417 worth keeping. */
2418 else if (code == PARALLEL)
2420 int i = XVECLEN (x, 0);
2422 for (i--; i >= 0; i--)
2423 if (GET_CODE (XVECEXP (x, 0, i)) != CLOBBER
2424 && GET_CODE (XVECEXP (x, 0, i)) != USE
2425 && ! insn_dead_p (pbi, XVECEXP (x, 0, i), call_ok, NULL_RTX))
2426 return 0;
2428 return 1;
2431 /* A CLOBBER of a pseudo-register that is dead serves no purpose. That
2432 is not necessarily true for hard registers until after reload. */
2433 else if (code == CLOBBER)
2435 if (REG_P (XEXP (x, 0))
2436 && (REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER
2437 || reload_completed)
2438 && ! REGNO_REG_SET_P (pbi->reg_live, REGNO (XEXP (x, 0))))
2439 return 1;
2442 /* ??? A base USE is a historical relic. It ought not be needed anymore.
2443 Instances where it is still used are either (1) temporary and the USE
2444 escaped the pass, (2) cruft and the USE need not be emitted anymore,
2445 or (3) hiding bugs elsewhere that are not properly representing data
2446 flow. */
2448 return 0;
2451 /* If INSN is the last insn in a libcall, and assuming INSN is dead,
2452 return 1 if the entire library call is dead.
2453 This is true if INSN copies a register (hard or pseudo)
2454 and if the hard return reg of the call insn is dead.
2455 (The caller should have tested the destination of the SET inside
2456 INSN already for death.)
2458 If this insn doesn't just copy a register, then we don't
2459 have an ordinary libcall. In that case, cse could not have
2460 managed to substitute the source for the dest later on,
2461 so we can assume the libcall is dead.
2463 PBI is the block info giving pseudoregs live before this insn.
2464 NOTE is the REG_RETVAL note of the insn. */
2466 static int
2467 libcall_dead_p (struct propagate_block_info *pbi, rtx note, rtx insn)
2469 rtx x = single_set (insn);
2471 if (x)
2473 rtx r = SET_SRC (x);
2475 if (REG_P (r) || GET_CODE (r) == SUBREG)
2477 rtx call = XEXP (note, 0);
2478 rtx call_pat;
2479 int i;
2481 /* Find the call insn. */
2482 while (call != insn && !CALL_P (call))
2483 call = NEXT_INSN (call);
2485 /* If there is none, do nothing special,
2486 since ordinary death handling can understand these insns. */
2487 if (call == insn)
2488 return 0;
2490 /* See if the hard reg holding the value is dead.
2491 If this is a PARALLEL, find the call within it. */
2492 call_pat = PATTERN (call);
2493 if (GET_CODE (call_pat) == PARALLEL)
2495 for (i = XVECLEN (call_pat, 0) - 1; i >= 0; i--)
2496 if (GET_CODE (XVECEXP (call_pat, 0, i)) == SET
2497 && GET_CODE (SET_SRC (XVECEXP (call_pat, 0, i))) == CALL)
2498 break;
2500 /* This may be a library call that is returning a value
2501 via invisible pointer. Do nothing special, since
2502 ordinary death handling can understand these insns. */
2503 if (i < 0)
2504 return 0;
2506 call_pat = XVECEXP (call_pat, 0, i);
2509 if (! insn_dead_p (pbi, call_pat, 1, REG_NOTES (call)))
2510 return 0;
2512 while ((insn = PREV_INSN (insn)) != call)
2514 if (! INSN_P (insn))
2515 continue;
2516 if (! insn_dead_p (pbi, PATTERN (insn), 0, REG_NOTES (insn)))
2517 return 0;
2519 return 1;
2522 return 0;
2525 /* 1 if register REGNO was alive at a place where `setjmp' was called
2526 and was set more than once or is an argument.
2527 Such regs may be clobbered by `longjmp'. */
2530 regno_clobbered_at_setjmp (int regno)
2532 if (n_basic_blocks == NUM_FIXED_BLOCKS)
2533 return 0;
2535 return ((REG_N_SETS (regno) > 1
2536 || REGNO_REG_SET_P (ENTRY_BLOCK_PTR->il.rtl->global_live_at_end,
2537 regno))
2538 && REGNO_REG_SET_P (regs_live_at_setjmp, regno));
2541 /* Add MEM to PBI->MEM_SET_LIST. MEM should be canonical. Respect the
2542 maximal list size; look for overlaps in mode and select the largest. */
2543 static void
2544 add_to_mem_set_list (struct propagate_block_info *pbi, rtx mem)
2546 rtx i;
2548 /* We don't know how large a BLKmode store is, so we must not
2549 take them into consideration. */
2550 if (GET_MODE (mem) == BLKmode)
2551 return;
2553 for (i = pbi->mem_set_list; i ; i = XEXP (i, 1))
2555 rtx e = XEXP (i, 0);
2556 if (rtx_equal_p (XEXP (mem, 0), XEXP (e, 0)))
2558 if (GET_MODE_SIZE (GET_MODE (mem)) > GET_MODE_SIZE (GET_MODE (e)))
2560 #ifdef AUTO_INC_DEC
2561 /* If we must store a copy of the mem, we can just modify
2562 the mode of the stored copy. */
2563 if (pbi->flags & PROP_AUTOINC)
2564 PUT_MODE (e, GET_MODE (mem));
2565 else
2566 #endif
2567 XEXP (i, 0) = mem;
2569 return;
2573 if (pbi->mem_set_list_len < PARAM_VALUE (PARAM_MAX_FLOW_MEMORY_LOCATIONS))
2575 #ifdef AUTO_INC_DEC
2576 /* Store a copy of mem, otherwise the address may be
2577 scrogged by find_auto_inc. */
2578 if (pbi->flags & PROP_AUTOINC)
2579 mem = shallow_copy_rtx (mem);
2580 #endif
2581 pbi->mem_set_list = alloc_EXPR_LIST (0, mem, pbi->mem_set_list);
2582 pbi->mem_set_list_len++;
2586 /* INSN references memory, possibly using autoincrement addressing modes.
2587 Find any entries on the mem_set_list that need to be invalidated due
2588 to an address change. */
2590 static int
2591 invalidate_mems_from_autoinc (rtx *px, void *data)
2593 rtx x = *px;
2594 struct propagate_block_info *pbi = data;
2596 if (GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
2598 invalidate_mems_from_set (pbi, XEXP (x, 0));
2599 return -1;
2602 return 0;
2605 /* EXP is a REG or MEM. Remove any dependent entries from
2606 pbi->mem_set_list. */
2608 static void
2609 invalidate_mems_from_set (struct propagate_block_info *pbi, rtx exp)
2611 rtx temp = pbi->mem_set_list;
2612 rtx prev = NULL_RTX;
2613 rtx next;
2615 while (temp)
2617 next = XEXP (temp, 1);
2618 if ((REG_P (exp) && reg_overlap_mentioned_p (exp, XEXP (temp, 0)))
2619 /* When we get an EXP that is a mem here, we want to check if EXP
2620 overlaps the *address* of any of the mems in the list (i.e. not
2621 whether the mems actually overlap; that's done elsewhere). */
2622 || (MEM_P (exp)
2623 && reg_overlap_mentioned_p (exp, XEXP (XEXP (temp, 0), 0))))
2625 /* Splice this entry out of the list. */
2626 if (prev)
2627 XEXP (prev, 1) = next;
2628 else
2629 pbi->mem_set_list = next;
2630 free_EXPR_LIST_node (temp);
2631 pbi->mem_set_list_len--;
2633 else
2634 prev = temp;
2635 temp = next;
2639 /* Process the registers that are set within X. Their bits are set to
2640 1 in the regset DEAD, because they are dead prior to this insn.
2642 If INSN is nonzero, it is the insn being processed.
2644 FLAGS is the set of operations to perform. */
2646 static void
2647 mark_set_regs (struct propagate_block_info *pbi, rtx x, rtx insn)
2649 rtx cond = NULL_RTX;
2650 rtx link;
2651 enum rtx_code code;
2652 int flags = pbi->flags;
2654 if (insn)
2655 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2657 if (REG_NOTE_KIND (link) == REG_INC)
2658 mark_set_1 (pbi, SET, XEXP (link, 0),
2659 (GET_CODE (x) == COND_EXEC
2660 ? COND_EXEC_TEST (x) : NULL_RTX),
2661 insn, flags);
2663 retry:
2664 switch (code = GET_CODE (x))
2666 case SET:
2667 if (GET_CODE (XEXP (x, 1)) == ASM_OPERANDS)
2668 flags |= PROP_ASM_SCAN;
2669 /* Fall through */
2670 case CLOBBER:
2671 mark_set_1 (pbi, code, SET_DEST (x), cond, insn, flags);
2672 return;
2674 case COND_EXEC:
2675 cond = COND_EXEC_TEST (x);
2676 x = COND_EXEC_CODE (x);
2677 goto retry;
2679 case PARALLEL:
2681 int i;
2683 /* We must scan forwards. If we have an asm, we need to set
2684 the PROP_ASM_SCAN flag before scanning the clobbers. */
2685 for (i = 0; i < XVECLEN (x, 0); i++)
2687 rtx sub = XVECEXP (x, 0, i);
2688 switch (code = GET_CODE (sub))
2690 case COND_EXEC:
2691 gcc_assert (!cond);
2693 cond = COND_EXEC_TEST (sub);
2694 sub = COND_EXEC_CODE (sub);
2695 if (GET_CODE (sub) == SET)
2696 goto mark_set;
2697 if (GET_CODE (sub) == CLOBBER)
2698 goto mark_clob;
2699 break;
2701 case SET:
2702 mark_set:
2703 if (GET_CODE (XEXP (sub, 1)) == ASM_OPERANDS)
2704 flags |= PROP_ASM_SCAN;
2705 /* Fall through */
2706 case CLOBBER:
2707 mark_clob:
2708 mark_set_1 (pbi, code, SET_DEST (sub), cond, insn, flags);
2709 break;
2711 case ASM_OPERANDS:
2712 flags |= PROP_ASM_SCAN;
2713 break;
2715 default:
2716 break;
2719 break;
2722 default:
2723 break;
2727 /* Process a single set, which appears in INSN. REG (which may not
2728 actually be a REG, it may also be a SUBREG, PARALLEL, etc.) is
2729 being set using the CODE (which may be SET, CLOBBER, or COND_EXEC).
2730 If the set is conditional (because it appear in a COND_EXEC), COND
2731 will be the condition. */
2733 static void
2734 mark_set_1 (struct propagate_block_info *pbi, enum rtx_code code, rtx reg, rtx cond, rtx insn, int flags)
2736 int regno_first = -1, regno_last = -1;
2737 unsigned long not_dead = 0;
2738 int i;
2740 /* Modifying just one hardware register of a multi-reg value or just a
2741 byte field of a register does not mean the value from before this insn
2742 is now dead. Of course, if it was dead after it's unused now. */
2744 switch (GET_CODE (reg))
2746 case PARALLEL:
2747 /* Some targets place small structures in registers for return values of
2748 functions. We have to detect this case specially here to get correct
2749 flow information. */
2750 for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
2751 if (XEXP (XVECEXP (reg, 0, i), 0) != 0)
2752 mark_set_1 (pbi, code, XEXP (XVECEXP (reg, 0, i), 0), cond, insn,
2753 flags);
2754 return;
2756 case SIGN_EXTRACT:
2757 /* SIGN_EXTRACT cannot be an lvalue. */
2758 gcc_unreachable ();
2760 case ZERO_EXTRACT:
2761 case STRICT_LOW_PART:
2762 /* ??? Assumes STRICT_LOW_PART not used on multi-word registers. */
2764 reg = XEXP (reg, 0);
2765 while (GET_CODE (reg) == SUBREG
2766 || GET_CODE (reg) == ZERO_EXTRACT
2767 || GET_CODE (reg) == STRICT_LOW_PART);
2768 if (MEM_P (reg))
2769 break;
2770 not_dead = (unsigned long) REGNO_REG_SET_P (pbi->reg_live, REGNO (reg));
2771 /* Fall through. */
2773 case REG:
2774 regno_last = regno_first = REGNO (reg);
2775 if (regno_first < FIRST_PSEUDO_REGISTER)
2776 regno_last += hard_regno_nregs[regno_first][GET_MODE (reg)] - 1;
2777 break;
2779 case SUBREG:
2780 if (REG_P (SUBREG_REG (reg)))
2782 enum machine_mode outer_mode = GET_MODE (reg);
2783 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (reg));
2785 /* Identify the range of registers affected. This is moderately
2786 tricky for hard registers. See alter_subreg. */
2788 regno_last = regno_first = REGNO (SUBREG_REG (reg));
2789 if (regno_first < FIRST_PSEUDO_REGISTER)
2791 regno_first += subreg_regno_offset (regno_first, inner_mode,
2792 SUBREG_BYTE (reg),
2793 outer_mode);
2794 regno_last = regno_first + subreg_nregs (reg) - 1;
2796 /* Since we've just adjusted the register number ranges, make
2797 sure REG matches. Otherwise some_was_live will be clear
2798 when it shouldn't have been, and we'll create incorrect
2799 REG_UNUSED notes. */
2800 reg = gen_rtx_REG (outer_mode, regno_first);
2802 else
2804 /* If the number of words in the subreg is less than the number
2805 of words in the full register, we have a well-defined partial
2806 set. Otherwise the high bits are undefined.
2808 This is only really applicable to pseudos, since we just took
2809 care of multi-word hard registers. */
2810 if (((GET_MODE_SIZE (outer_mode)
2811 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
2812 < ((GET_MODE_SIZE (inner_mode)
2813 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))
2814 not_dead = (unsigned long) REGNO_REG_SET_P (pbi->reg_live,
2815 regno_first);
2817 reg = SUBREG_REG (reg);
2820 else
2821 reg = SUBREG_REG (reg);
2822 break;
2824 default:
2825 break;
2828 /* If this set is a MEM, then it kills any aliased writes and any
2829 other MEMs which use it.
2830 If this set is a REG, then it kills any MEMs which use the reg. */
2831 if (optimize && (flags & PROP_SCAN_DEAD_STORES))
2833 if (REG_P (reg) || MEM_P (reg))
2834 invalidate_mems_from_set (pbi, reg);
2836 /* If the memory reference had embedded side effects (autoincrement
2837 address modes) then we may need to kill some entries on the
2838 memory set list. */
2839 if (insn && MEM_P (reg))
2840 for_each_rtx (&PATTERN (insn), invalidate_mems_from_autoinc, pbi);
2842 if (MEM_P (reg) && ! side_effects_p (reg)
2843 /* ??? With more effort we could track conditional memory life. */
2844 && ! cond)
2845 add_to_mem_set_list (pbi, canon_rtx (reg));
2848 if (REG_P (reg)
2849 && ! (regno_first == FRAME_POINTER_REGNUM
2850 && (! reload_completed || frame_pointer_needed))
2851 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2852 && ! (regno_first == HARD_FRAME_POINTER_REGNUM
2853 && (! reload_completed || frame_pointer_needed))
2854 #endif
2855 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2856 && ! (regno_first == ARG_POINTER_REGNUM && fixed_regs[regno_first])
2857 #endif
2860 int some_was_live = 0, some_was_dead = 0;
2862 for (i = regno_first; i <= regno_last; ++i)
2864 int needed_regno = REGNO_REG_SET_P (pbi->reg_live, i);
2865 if (pbi->local_set)
2867 /* Order of the set operation matters here since both
2868 sets may be the same. */
2869 CLEAR_REGNO_REG_SET (pbi->cond_local_set, i);
2870 if (cond != NULL_RTX
2871 && ! REGNO_REG_SET_P (pbi->local_set, i))
2872 SET_REGNO_REG_SET (pbi->cond_local_set, i);
2873 else
2874 SET_REGNO_REG_SET (pbi->local_set, i);
2876 if (code != CLOBBER || needed_regno)
2877 SET_REGNO_REG_SET (pbi->new_set, i);
2879 some_was_live |= needed_regno;
2880 some_was_dead |= ! needed_regno;
2883 #ifdef HAVE_conditional_execution
2884 /* Consider conditional death in deciding that the register needs
2885 a death note. */
2886 if (some_was_live && ! not_dead
2887 /* The stack pointer is never dead. Well, not strictly true,
2888 but it's very difficult to tell from here. Hopefully
2889 combine_stack_adjustments will fix up the most egregious
2890 errors. */
2891 && regno_first != STACK_POINTER_REGNUM)
2893 for (i = regno_first; i <= regno_last; ++i)
2894 if (! mark_regno_cond_dead (pbi, i, cond))
2895 not_dead |= ((unsigned long) 1) << (i - regno_first);
2897 #endif
2899 /* Additional data to record if this is the final pass. */
2900 if (flags & (PROP_LOG_LINKS | PROP_REG_INFO
2901 | PROP_DEATH_NOTES | PROP_AUTOINC))
2903 rtx y;
2904 int blocknum = pbi->bb->index;
2906 y = NULL_RTX;
2907 if (flags & (PROP_LOG_LINKS | PROP_AUTOINC))
2909 y = pbi->reg_next_use[regno_first];
2911 /* The next use is no longer next, since a store intervenes. */
2912 for (i = regno_first; i <= regno_last; ++i)
2913 pbi->reg_next_use[i] = 0;
2916 if (flags & PROP_REG_INFO)
2918 for (i = regno_first; i <= regno_last; ++i)
2920 /* Count (weighted) references, stores, etc. This counts a
2921 register twice if it is modified, but that is correct. */
2922 REG_N_SETS (i) += 1;
2923 REG_N_REFS (i) += 1;
2924 REG_FREQ (i) += REG_FREQ_FROM_BB (pbi->bb);
2926 /* The insns where a reg is live are normally counted
2927 elsewhere, but we want the count to include the insn
2928 where the reg is set, and the normal counting mechanism
2929 would not count it. */
2930 REG_LIVE_LENGTH (i) += 1;
2933 /* If this is a hard reg, record this function uses the reg. */
2934 if (regno_first < FIRST_PSEUDO_REGISTER)
2936 for (i = regno_first; i <= regno_last; i++)
2937 regs_ever_live[i] = 1;
2938 if (flags & PROP_ASM_SCAN)
2939 for (i = regno_first; i <= regno_last; i++)
2940 regs_asm_clobbered[i] = 1;
2942 else
2944 /* Keep track of which basic blocks each reg appears in. */
2945 if (REG_BASIC_BLOCK (regno_first) == REG_BLOCK_UNKNOWN)
2946 REG_BASIC_BLOCK (regno_first) = blocknum;
2947 else if (REG_BASIC_BLOCK (regno_first) != blocknum)
2948 REG_BASIC_BLOCK (regno_first) = REG_BLOCK_GLOBAL;
2952 if (! some_was_dead)
2954 if (flags & PROP_LOG_LINKS)
2956 /* Make a logical link from the next following insn
2957 that uses this register, back to this insn.
2958 The following insns have already been processed.
2960 We don't build a LOG_LINK for hard registers containing
2961 in ASM_OPERANDs. If these registers get replaced,
2962 we might wind up changing the semantics of the insn,
2963 even if reload can make what appear to be valid
2964 assignments later.
2966 We don't build a LOG_LINK for global registers to
2967 or from a function call. We don't want to let
2968 combine think that it knows what is going on with
2969 global registers. */
2970 if (y && (BLOCK_NUM (y) == blocknum)
2971 && (regno_first >= FIRST_PSEUDO_REGISTER
2972 || (asm_noperands (PATTERN (y)) < 0
2973 && ! ((CALL_P (insn)
2974 || CALL_P (y))
2975 && global_regs[regno_first]))))
2976 LOG_LINKS (y) = alloc_INSN_LIST (insn, LOG_LINKS (y));
2979 else if (not_dead)
2981 else if (! some_was_live)
2983 if (flags & PROP_REG_INFO)
2984 REG_N_DEATHS (regno_first) += 1;
2986 if (flags & PROP_DEATH_NOTES
2987 #ifdef STACK_REGS
2988 && (!(flags & PROP_POST_REGSTACK)
2989 || !IN_RANGE (REGNO (reg), FIRST_STACK_REG,
2990 LAST_STACK_REG))
2991 #endif
2994 /* Note that dead stores have already been deleted
2995 when possible. If we get here, we have found a
2996 dead store that cannot be eliminated (because the
2997 same insn does something useful). Indicate this
2998 by marking the reg being set as dying here. */
2999 REG_NOTES (insn)
3000 = alloc_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn));
3003 else
3005 if (flags & PROP_DEATH_NOTES
3006 #ifdef STACK_REGS
3007 && (!(flags & PROP_POST_REGSTACK)
3008 || !IN_RANGE (REGNO (reg), FIRST_STACK_REG,
3009 LAST_STACK_REG))
3010 #endif
3013 /* This is a case where we have a multi-word hard register
3014 and some, but not all, of the words of the register are
3015 needed in subsequent insns. Write REG_UNUSED notes
3016 for those parts that were not needed. This case should
3017 be rare. */
3019 for (i = regno_first; i <= regno_last; ++i)
3020 if (! REGNO_REG_SET_P (pbi->reg_live, i))
3021 REG_NOTES (insn)
3022 = alloc_EXPR_LIST (REG_UNUSED,
3023 regno_reg_rtx[i],
3024 REG_NOTES (insn));
3029 /* Mark the register as being dead. */
3030 if (some_was_live
3031 /* The stack pointer is never dead. Well, not strictly true,
3032 but it's very difficult to tell from here. Hopefully
3033 combine_stack_adjustments will fix up the most egregious
3034 errors. */
3035 && regno_first != STACK_POINTER_REGNUM)
3037 for (i = regno_first; i <= regno_last; ++i)
3038 if (!(not_dead & (((unsigned long) 1) << (i - regno_first))))
3040 if ((pbi->flags & PROP_REG_INFO)
3041 && REGNO_REG_SET_P (pbi->reg_live, i))
3043 REG_LIVE_LENGTH (i) += pbi->insn_num - reg_deaths[i];
3044 reg_deaths[i] = 0;
3046 CLEAR_REGNO_REG_SET (pbi->reg_live, i);
3048 if (flags & PROP_DEAD_INSN)
3049 emit_insn_after (gen_rtx_CLOBBER (VOIDmode, reg), insn);
3052 else if (REG_P (reg))
3054 if (flags & (PROP_LOG_LINKS | PROP_AUTOINC))
3055 pbi->reg_next_use[regno_first] = 0;
3057 if ((flags & PROP_REG_INFO) != 0
3058 && (flags & PROP_ASM_SCAN) != 0
3059 && regno_first < FIRST_PSEUDO_REGISTER)
3061 for (i = regno_first; i <= regno_last; i++)
3062 regs_asm_clobbered[i] = 1;
3066 /* If this is the last pass and this is a SCRATCH, show it will be dying
3067 here and count it. */
3068 else if (GET_CODE (reg) == SCRATCH)
3070 if (flags & PROP_DEATH_NOTES
3071 #ifdef STACK_REGS
3072 && (!(flags & PROP_POST_REGSTACK)
3073 || !IN_RANGE (REGNO (reg), FIRST_STACK_REG, LAST_STACK_REG))
3074 #endif
3076 REG_NOTES (insn)
3077 = alloc_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn));
3081 #ifdef HAVE_conditional_execution
3082 /* Mark REGNO conditionally dead.
3083 Return true if the register is now unconditionally dead. */
3085 static int
3086 mark_regno_cond_dead (struct propagate_block_info *pbi, int regno, rtx cond)
3088 /* If this is a store to a predicate register, the value of the
3089 predicate is changing, we don't know that the predicate as seen
3090 before is the same as that seen after. Flush all dependent
3091 conditions from reg_cond_dead. This will make all such
3092 conditionally live registers unconditionally live. */
3093 if (REGNO_REG_SET_P (pbi->reg_cond_reg, regno))
3094 flush_reg_cond_reg (pbi, regno);
3096 /* If this is an unconditional store, remove any conditional
3097 life that may have existed. */
3098 if (cond == NULL_RTX)
3099 splay_tree_remove (pbi->reg_cond_dead, regno);
3100 else
3102 splay_tree_node node;
3103 struct reg_cond_life_info *rcli;
3104 rtx ncond;
3106 /* Otherwise this is a conditional set. Record that fact.
3107 It may have been conditionally used, or there may be a
3108 subsequent set with a complementary condition. */
3110 node = splay_tree_lookup (pbi->reg_cond_dead, regno);
3111 if (node == NULL)
3113 /* The register was unconditionally live previously.
3114 Record the current condition as the condition under
3115 which it is dead. */
3116 rcli = XNEW (struct reg_cond_life_info);
3117 rcli->condition = cond;
3118 rcli->stores = cond;
3119 rcli->orig_condition = const0_rtx;
3120 splay_tree_insert (pbi->reg_cond_dead, regno,
3121 (splay_tree_value) rcli);
3123 SET_REGNO_REG_SET (pbi->reg_cond_reg, REGNO (XEXP (cond, 0)));
3125 /* Not unconditionally dead. */
3126 return 0;
3128 else
3130 /* The register was conditionally live previously.
3131 Add the new condition to the old. */
3132 rcli = (struct reg_cond_life_info *) node->value;
3133 ncond = rcli->condition;
3134 ncond = ior_reg_cond (ncond, cond, 1);
3135 if (rcli->stores == const0_rtx)
3136 rcli->stores = cond;
3137 else if (rcli->stores != const1_rtx)
3138 rcli->stores = ior_reg_cond (rcli->stores, cond, 1);
3140 /* If the register is now unconditionally dead, remove the entry
3141 in the splay_tree. A register is unconditionally dead if the
3142 dead condition ncond is true. A register is also unconditionally
3143 dead if the sum of all conditional stores is an unconditional
3144 store (stores is true), and the dead condition is identically the
3145 same as the original dead condition initialized at the end of
3146 the block. This is a pointer compare, not an rtx_equal_p
3147 compare. */
3148 if (ncond == const1_rtx
3149 || (ncond == rcli->orig_condition && rcli->stores == const1_rtx))
3150 splay_tree_remove (pbi->reg_cond_dead, regno);
3151 else
3153 rcli->condition = ncond;
3155 SET_REGNO_REG_SET (pbi->reg_cond_reg, REGNO (XEXP (cond, 0)));
3157 /* Not unconditionally dead. */
3158 return 0;
3163 return 1;
3166 /* Called from splay_tree_delete for pbi->reg_cond_life. */
3168 static void
3169 free_reg_cond_life_info (splay_tree_value value)
3171 struct reg_cond_life_info *rcli = (struct reg_cond_life_info *) value;
3172 free (rcli);
3175 /* Helper function for flush_reg_cond_reg. */
3177 static int
3178 flush_reg_cond_reg_1 (splay_tree_node node, void *data)
3180 struct reg_cond_life_info *rcli;
3181 int *xdata = (int *) data;
3182 unsigned int regno = xdata[0];
3184 /* Don't need to search if last flushed value was farther on in
3185 the in-order traversal. */
3186 if (xdata[1] >= (int) node->key)
3187 return 0;
3189 /* Splice out portions of the expression that refer to regno. */
3190 rcli = (struct reg_cond_life_info *) node->value;
3191 rcli->condition = elim_reg_cond (rcli->condition, regno);
3192 if (rcli->stores != const0_rtx && rcli->stores != const1_rtx)
3193 rcli->stores = elim_reg_cond (rcli->stores, regno);
3195 /* If the entire condition is now false, signal the node to be removed. */
3196 if (rcli->condition == const0_rtx)
3198 xdata[1] = node->key;
3199 return -1;
3201 else
3202 gcc_assert (rcli->condition != const1_rtx);
3204 return 0;
3207 /* Flush all (sub) expressions referring to REGNO from REG_COND_LIVE. */
3209 static void
3210 flush_reg_cond_reg (struct propagate_block_info *pbi, int regno)
3212 int pair[2];
3214 pair[0] = regno;
3215 pair[1] = -1;
3216 while (splay_tree_foreach (pbi->reg_cond_dead,
3217 flush_reg_cond_reg_1, pair) == -1)
3218 splay_tree_remove (pbi->reg_cond_dead, pair[1]);
3220 CLEAR_REGNO_REG_SET (pbi->reg_cond_reg, regno);
3223 /* Logical arithmetic on predicate conditions. IOR, NOT and AND.
3224 For ior/and, the ADD flag determines whether we want to add the new
3225 condition X to the old one unconditionally. If it is zero, we will
3226 only return a new expression if X allows us to simplify part of
3227 OLD, otherwise we return NULL to the caller.
3228 If ADD is nonzero, we will return a new condition in all cases. The
3229 toplevel caller of one of these functions should always pass 1 for
3230 ADD. */
3232 static rtx
3233 ior_reg_cond (rtx old, rtx x, int add)
3235 rtx op0, op1;
3237 if (COMPARISON_P (old))
3239 if (COMPARISON_P (x)
3240 && REVERSE_CONDEXEC_PREDICATES_P (x, old)
3241 && REGNO (XEXP (x, 0)) == REGNO (XEXP (old, 0)))
3242 return const1_rtx;
3243 if (GET_CODE (x) == GET_CODE (old)
3244 && REGNO (XEXP (x, 0)) == REGNO (XEXP (old, 0)))
3245 return old;
3246 if (! add)
3247 return NULL;
3248 return gen_rtx_IOR (0, old, x);
3251 switch (GET_CODE (old))
3253 case IOR:
3254 op0 = ior_reg_cond (XEXP (old, 0), x, 0);
3255 op1 = ior_reg_cond (XEXP (old, 1), x, 0);
3256 if (op0 != NULL || op1 != NULL)
3258 if (op0 == const0_rtx)
3259 return op1 ? op1 : gen_rtx_IOR (0, XEXP (old, 1), x);
3260 if (op1 == const0_rtx)
3261 return op0 ? op0 : gen_rtx_IOR (0, XEXP (old, 0), x);
3262 if (op0 == const1_rtx || op1 == const1_rtx)
3263 return const1_rtx;
3264 if (op0 == NULL)
3265 op0 = gen_rtx_IOR (0, XEXP (old, 0), x);
3266 else if (rtx_equal_p (x, op0))
3267 /* (x | A) | x ~ (x | A). */
3268 return old;
3269 if (op1 == NULL)
3270 op1 = gen_rtx_IOR (0, XEXP (old, 1), x);
3271 else if (rtx_equal_p (x, op1))
3272 /* (A | x) | x ~ (A | x). */
3273 return old;
3274 return gen_rtx_IOR (0, op0, op1);
3276 if (! add)
3277 return NULL;
3278 return gen_rtx_IOR (0, old, x);
3280 case AND:
3281 op0 = ior_reg_cond (XEXP (old, 0), x, 0);
3282 op1 = ior_reg_cond (XEXP (old, 1), x, 0);
3283 if (op0 != NULL || op1 != NULL)
3285 if (op0 == const1_rtx)
3286 return op1 ? op1 : gen_rtx_IOR (0, XEXP (old, 1), x);
3287 if (op1 == const1_rtx)
3288 return op0 ? op0 : gen_rtx_IOR (0, XEXP (old, 0), x);
3289 if (op0 == const0_rtx || op1 == const0_rtx)
3290 return const0_rtx;
3291 if (op0 == NULL)
3292 op0 = gen_rtx_IOR (0, XEXP (old, 0), x);
3293 else if (rtx_equal_p (x, op0))
3294 /* (x & A) | x ~ x. */
3295 return op0;
3296 if (op1 == NULL)
3297 op1 = gen_rtx_IOR (0, XEXP (old, 1), x);
3298 else if (rtx_equal_p (x, op1))
3299 /* (A & x) | x ~ x. */
3300 return op1;
3301 return gen_rtx_AND (0, op0, op1);
3303 if (! add)
3304 return NULL;
3305 return gen_rtx_IOR (0, old, x);
3307 case NOT:
3308 op0 = and_reg_cond (XEXP (old, 0), not_reg_cond (x), 0);
3309 if (op0 != NULL)
3310 return not_reg_cond (op0);
3311 if (! add)
3312 return NULL;
3313 return gen_rtx_IOR (0, old, x);
3315 default:
3316 gcc_unreachable ();
3320 static rtx
3321 not_reg_cond (rtx x)
3323 if (x == const0_rtx)
3324 return const1_rtx;
3325 else if (x == const1_rtx)
3326 return const0_rtx;
3327 if (GET_CODE (x) == NOT)
3328 return XEXP (x, 0);
3329 if (COMPARISON_P (x)
3330 && REG_P (XEXP (x, 0)))
3332 gcc_assert (XEXP (x, 1) == const0_rtx);
3334 return gen_rtx_fmt_ee (reversed_comparison_code (x, NULL),
3335 VOIDmode, XEXP (x, 0), const0_rtx);
3337 return gen_rtx_NOT (0, x);
3340 static rtx
3341 and_reg_cond (rtx old, rtx x, int add)
3343 rtx op0, op1;
3345 if (COMPARISON_P (old))
3347 if (COMPARISON_P (x)
3348 && GET_CODE (x) == reversed_comparison_code (old, NULL)
3349 && REGNO (XEXP (x, 0)) == REGNO (XEXP (old, 0)))
3350 return const0_rtx;
3351 if (GET_CODE (x) == GET_CODE (old)
3352 && REGNO (XEXP (x, 0)) == REGNO (XEXP (old, 0)))
3353 return old;
3354 if (! add)
3355 return NULL;
3356 return gen_rtx_AND (0, old, x);
3359 switch (GET_CODE (old))
3361 case IOR:
3362 op0 = and_reg_cond (XEXP (old, 0), x, 0);
3363 op1 = and_reg_cond (XEXP (old, 1), x, 0);
3364 if (op0 != NULL || op1 != NULL)
3366 if (op0 == const0_rtx)
3367 return op1 ? op1 : gen_rtx_AND (0, XEXP (old, 1), x);
3368 if (op1 == const0_rtx)
3369 return op0 ? op0 : gen_rtx_AND (0, XEXP (old, 0), x);
3370 if (op0 == const1_rtx || op1 == const1_rtx)
3371 return const1_rtx;
3372 if (op0 == NULL)
3373 op0 = gen_rtx_AND (0, XEXP (old, 0), x);
3374 else if (rtx_equal_p (x, op0))
3375 /* (x | A) & x ~ x. */
3376 return op0;
3377 if (op1 == NULL)
3378 op1 = gen_rtx_AND (0, XEXP (old, 1), x);
3379 else if (rtx_equal_p (x, op1))
3380 /* (A | x) & x ~ x. */
3381 return op1;
3382 return gen_rtx_IOR (0, op0, op1);
3384 if (! add)
3385 return NULL;
3386 return gen_rtx_AND (0, old, x);
3388 case AND:
3389 op0 = and_reg_cond (XEXP (old, 0), x, 0);
3390 op1 = and_reg_cond (XEXP (old, 1), x, 0);
3391 if (op0 != NULL || op1 != NULL)
3393 if (op0 == const1_rtx)
3394 return op1 ? op1 : gen_rtx_AND (0, XEXP (old, 1), x);
3395 if (op1 == const1_rtx)
3396 return op0 ? op0 : gen_rtx_AND (0, XEXP (old, 0), x);
3397 if (op0 == const0_rtx || op1 == const0_rtx)
3398 return const0_rtx;
3399 if (op0 == NULL)
3400 op0 = gen_rtx_AND (0, XEXP (old, 0), x);
3401 else if (rtx_equal_p (x, op0))
3402 /* (x & A) & x ~ (x & A). */
3403 return old;
3404 if (op1 == NULL)
3405 op1 = gen_rtx_AND (0, XEXP (old, 1), x);
3406 else if (rtx_equal_p (x, op1))
3407 /* (A & x) & x ~ (A & x). */
3408 return old;
3409 return gen_rtx_AND (0, op0, op1);
3411 if (! add)
3412 return NULL;
3413 return gen_rtx_AND (0, old, x);
3415 case NOT:
3416 op0 = ior_reg_cond (XEXP (old, 0), not_reg_cond (x), 0);
3417 if (op0 != NULL)
3418 return not_reg_cond (op0);
3419 if (! add)
3420 return NULL;
3421 return gen_rtx_AND (0, old, x);
3423 default:
3424 gcc_unreachable ();
3428 /* Given a condition X, remove references to reg REGNO and return the
3429 new condition. The removal will be done so that all conditions
3430 involving REGNO are considered to evaluate to false. This function
3431 is used when the value of REGNO changes. */
3433 static rtx
3434 elim_reg_cond (rtx x, unsigned int regno)
3436 rtx op0, op1;
3438 if (COMPARISON_P (x))
3440 if (REGNO (XEXP (x, 0)) == regno)
3441 return const0_rtx;
3442 return x;
3445 switch (GET_CODE (x))
3447 case AND:
3448 op0 = elim_reg_cond (XEXP (x, 0), regno);
3449 op1 = elim_reg_cond (XEXP (x, 1), regno);
3450 if (op0 == const0_rtx || op1 == const0_rtx)
3451 return const0_rtx;
3452 if (op0 == const1_rtx)
3453 return op1;
3454 if (op1 == const1_rtx)
3455 return op0;
3456 if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
3457 return x;
3458 return gen_rtx_AND (0, op0, op1);
3460 case IOR:
3461 op0 = elim_reg_cond (XEXP (x, 0), regno);
3462 op1 = elim_reg_cond (XEXP (x, 1), regno);
3463 if (op0 == const1_rtx || op1 == const1_rtx)
3464 return const1_rtx;
3465 if (op0 == const0_rtx)
3466 return op1;
3467 if (op1 == const0_rtx)
3468 return op0;
3469 if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
3470 return x;
3471 return gen_rtx_IOR (0, op0, op1);
3473 case NOT:
3474 op0 = elim_reg_cond (XEXP (x, 0), regno);
3475 if (op0 == const0_rtx)
3476 return const1_rtx;
3477 if (op0 == const1_rtx)
3478 return const0_rtx;
3479 if (op0 != XEXP (x, 0))
3480 return not_reg_cond (op0);
3481 return x;
3483 default:
3484 gcc_unreachable ();
3487 #endif /* HAVE_conditional_execution */
3489 #ifdef AUTO_INC_DEC
3491 /* Try to substitute the auto-inc expression INC as the address inside
3492 MEM which occurs in INSN. Currently, the address of MEM is an expression
3493 involving INCR_REG, and INCR is the next use of INCR_REG; it is an insn
3494 that has a single set whose source is a PLUS of INCR_REG and something
3495 else. */
3497 static void
3498 attempt_auto_inc (struct propagate_block_info *pbi, rtx inc, rtx insn,
3499 rtx mem, rtx incr, rtx incr_reg)
3501 int regno = REGNO (incr_reg);
3502 rtx set = single_set (incr);
3503 rtx q = SET_DEST (set);
3504 rtx y = SET_SRC (set);
3505 int opnum = XEXP (y, 0) == incr_reg ? 0 : 1;
3506 int changed;
3508 /* Make sure this reg appears only once in this insn. */
3509 if (count_occurrences (PATTERN (insn), incr_reg, 1) != 1)
3510 return;
3512 if (dead_or_set_p (incr, incr_reg)
3513 /* Mustn't autoinc an eliminable register. */
3514 && (regno >= FIRST_PSEUDO_REGISTER
3515 || ! TEST_HARD_REG_BIT (elim_reg_set, regno)))
3517 /* This is the simple case. Try to make the auto-inc. If
3518 we can't, we are done. Otherwise, we will do any
3519 needed updates below. */
3520 if (! validate_change (insn, &XEXP (mem, 0), inc, 0))
3521 return;
3523 else if (REG_P (q)
3524 /* PREV_INSN used here to check the semi-open interval
3525 [insn,incr). */
3526 && ! reg_used_between_p (q, PREV_INSN (insn), incr)
3527 /* We must also check for sets of q as q may be
3528 a call clobbered hard register and there may
3529 be a call between PREV_INSN (insn) and incr. */
3530 && ! reg_set_between_p (q, PREV_INSN (insn), incr))
3532 /* We have *p followed sometime later by q = p+size.
3533 Both p and q must be live afterward,
3534 and q is not used between INSN and its assignment.
3535 Change it to q = p, ...*q..., q = q+size.
3536 Then fall into the usual case. */
3537 rtx insns, temp;
3539 start_sequence ();
3540 emit_move_insn (q, incr_reg);
3541 insns = get_insns ();
3542 end_sequence ();
3544 /* If we can't make the auto-inc, or can't make the
3545 replacement into Y, exit. There's no point in making
3546 the change below if we can't do the auto-inc and doing
3547 so is not correct in the pre-inc case. */
3549 XEXP (inc, 0) = q;
3550 validate_change (insn, &XEXP (mem, 0), inc, 1);
3551 validate_change (incr, &XEXP (y, opnum), q, 1);
3552 if (! apply_change_group ())
3553 return;
3555 /* We now know we'll be doing this change, so emit the
3556 new insn(s) and do the updates. */
3557 emit_insn_before (insns, insn);
3559 if (BB_HEAD (pbi->bb) == insn)
3560 BB_HEAD (pbi->bb) = insns;
3562 /* INCR will become a NOTE and INSN won't contain a
3563 use of INCR_REG. If a use of INCR_REG was just placed in
3564 the insn before INSN, make that the next use.
3565 Otherwise, invalidate it. */
3566 if (NONJUMP_INSN_P (PREV_INSN (insn))
3567 && GET_CODE (PATTERN (PREV_INSN (insn))) == SET
3568 && SET_SRC (PATTERN (PREV_INSN (insn))) == incr_reg)
3569 pbi->reg_next_use[regno] = PREV_INSN (insn);
3570 else
3571 pbi->reg_next_use[regno] = 0;
3573 incr_reg = q;
3574 regno = REGNO (q);
3576 if ((pbi->flags & PROP_REG_INFO)
3577 && !REGNO_REG_SET_P (pbi->reg_live, regno))
3578 reg_deaths[regno] = pbi->insn_num;
3580 /* REGNO is now used in INCR which is below INSN, but
3581 it previously wasn't live here. If we don't mark
3582 it as live, we'll put a REG_DEAD note for it
3583 on this insn, which is incorrect. */
3584 SET_REGNO_REG_SET (pbi->reg_live, regno);
3586 /* If there are any calls between INSN and INCR, show
3587 that REGNO now crosses them. */
3588 for (temp = insn; temp != incr; temp = NEXT_INSN (temp))
3589 if (CALL_P (temp))
3591 REG_N_CALLS_CROSSED (regno)++;
3592 if (can_throw_internal (temp))
3593 REG_N_THROWING_CALLS_CROSSED (regno)++;
3596 /* Invalidate alias info for Q since we just changed its value. */
3597 clear_reg_alias_info (q);
3599 else
3600 return;
3602 /* If we haven't returned, it means we were able to make the
3603 auto-inc, so update the status. First, record that this insn
3604 has an implicit side effect. */
3606 REG_NOTES (insn) = alloc_EXPR_LIST (REG_INC, incr_reg, REG_NOTES (insn));
3608 /* Modify the old increment-insn to simply copy
3609 the already-incremented value of our register. */
3610 changed = validate_change (incr, &SET_SRC (set), incr_reg, 0);
3611 gcc_assert (changed);
3613 /* If that makes it a no-op (copying the register into itself) delete
3614 it so it won't appear to be a "use" and a "set" of this
3615 register. */
3616 if (REGNO (SET_DEST (set)) == REGNO (incr_reg))
3618 /* If the original source was dead, it's dead now. */
3619 rtx note;
3621 while ((note = find_reg_note (incr, REG_DEAD, NULL_RTX)) != NULL_RTX)
3623 remove_note (incr, note);
3624 if (XEXP (note, 0) != incr_reg)
3626 unsigned int regno = REGNO (XEXP (note, 0));
3628 if ((pbi->flags & PROP_REG_INFO)
3629 && REGNO_REG_SET_P (pbi->reg_live, regno))
3631 REG_LIVE_LENGTH (regno) += pbi->insn_num - reg_deaths[regno];
3632 reg_deaths[regno] = 0;
3634 CLEAR_REGNO_REG_SET (pbi->reg_live, REGNO (XEXP (note, 0)));
3638 SET_INSN_DELETED (incr);
3641 if (regno >= FIRST_PSEUDO_REGISTER)
3643 /* Count an extra reference to the reg. When a reg is
3644 incremented, spilling it is worse, so we want to make
3645 that less likely. */
3646 REG_FREQ (regno) += REG_FREQ_FROM_BB (pbi->bb);
3648 /* Count the increment as a setting of the register,
3649 even though it isn't a SET in rtl. */
3650 REG_N_SETS (regno)++;
3654 /* X is a MEM found in INSN. See if we can convert it into an auto-increment
3655 reference. */
3657 static void
3658 find_auto_inc (struct propagate_block_info *pbi, rtx x, rtx insn)
3660 rtx addr = XEXP (x, 0);
3661 HOST_WIDE_INT offset = 0;
3662 rtx set, y, incr, inc_val;
3663 int regno;
3664 int size = GET_MODE_SIZE (GET_MODE (x));
3666 if (JUMP_P (insn))
3667 return;
3669 /* Here we detect use of an index register which might be good for
3670 postincrement, postdecrement, preincrement, or predecrement. */
3672 if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
3673 offset = INTVAL (XEXP (addr, 1)), addr = XEXP (addr, 0);
3675 if (!REG_P (addr))
3676 return;
3678 regno = REGNO (addr);
3680 /* Is the next use an increment that might make auto-increment? */
3681 incr = pbi->reg_next_use[regno];
3682 if (incr == 0 || BLOCK_NUM (incr) != BLOCK_NUM (insn))
3683 return;
3684 set = single_set (incr);
3685 if (set == 0 || GET_CODE (set) != SET)
3686 return;
3687 y = SET_SRC (set);
3689 if (GET_CODE (y) != PLUS)
3690 return;
3692 if (REG_P (XEXP (y, 0)) && REGNO (XEXP (y, 0)) == REGNO (addr))
3693 inc_val = XEXP (y, 1);
3694 else if (REG_P (XEXP (y, 1)) && REGNO (XEXP (y, 1)) == REGNO (addr))
3695 inc_val = XEXP (y, 0);
3696 else
3697 return;
3699 if (GET_CODE (inc_val) == CONST_INT)
3701 if (HAVE_POST_INCREMENT
3702 && (INTVAL (inc_val) == size && offset == 0))
3703 attempt_auto_inc (pbi, gen_rtx_POST_INC (Pmode, addr), insn, x,
3704 incr, addr);
3705 else if (HAVE_POST_DECREMENT
3706 && (INTVAL (inc_val) == -size && offset == 0))
3707 attempt_auto_inc (pbi, gen_rtx_POST_DEC (Pmode, addr), insn, x,
3708 incr, addr);
3709 else if (HAVE_PRE_INCREMENT
3710 && (INTVAL (inc_val) == size && offset == size))
3711 attempt_auto_inc (pbi, gen_rtx_PRE_INC (Pmode, addr), insn, x,
3712 incr, addr);
3713 else if (HAVE_PRE_DECREMENT
3714 && (INTVAL (inc_val) == -size && offset == -size))
3715 attempt_auto_inc (pbi, gen_rtx_PRE_DEC (Pmode, addr), insn, x,
3716 incr, addr);
3717 else if (HAVE_POST_MODIFY_DISP && offset == 0)
3718 attempt_auto_inc (pbi, gen_rtx_POST_MODIFY (Pmode, addr,
3719 gen_rtx_PLUS (Pmode,
3720 addr,
3721 inc_val)),
3722 insn, x, incr, addr);
3723 else if (HAVE_PRE_MODIFY_DISP && offset == INTVAL (inc_val))
3724 attempt_auto_inc (pbi, gen_rtx_PRE_MODIFY (Pmode, addr,
3725 gen_rtx_PLUS (Pmode,
3726 addr,
3727 inc_val)),
3728 insn, x, incr, addr);
3730 else if (REG_P (inc_val)
3731 && ! reg_set_between_p (inc_val, PREV_INSN (insn),
3732 NEXT_INSN (incr)))
3735 if (HAVE_POST_MODIFY_REG && offset == 0)
3736 attempt_auto_inc (pbi, gen_rtx_POST_MODIFY (Pmode, addr,
3737 gen_rtx_PLUS (Pmode,
3738 addr,
3739 inc_val)),
3740 insn, x, incr, addr);
3744 #endif /* AUTO_INC_DEC */
3746 static void
3747 mark_used_reg (struct propagate_block_info *pbi, rtx reg,
3748 rtx cond ATTRIBUTE_UNUSED, rtx insn)
3750 unsigned int regno_first, regno_last, i;
3751 int some_was_live, some_was_dead, some_not_set;
3753 regno_last = regno_first = REGNO (reg);
3754 if (regno_first < FIRST_PSEUDO_REGISTER)
3755 regno_last += hard_regno_nregs[regno_first][GET_MODE (reg)] - 1;
3757 /* Find out if any of this register is live after this instruction. */
3758 some_was_live = some_was_dead = 0;
3759 for (i = regno_first; i <= regno_last; ++i)
3761 int needed_regno = REGNO_REG_SET_P (pbi->reg_live, i);
3762 some_was_live |= needed_regno;
3763 some_was_dead |= ! needed_regno;
3766 /* Find out if any of the register was set this insn. */
3767 some_not_set = 0;
3768 for (i = regno_first; i <= regno_last; ++i)
3769 some_not_set |= ! REGNO_REG_SET_P (pbi->new_set, i);
3771 if (pbi->flags & (PROP_LOG_LINKS | PROP_AUTOINC))
3773 /* Record where each reg is used, so when the reg is set we know
3774 the next insn that uses it. */
3775 pbi->reg_next_use[regno_first] = insn;
3778 if (pbi->flags & PROP_REG_INFO)
3780 if (regno_first < FIRST_PSEUDO_REGISTER)
3782 /* If this is a register we are going to try to eliminate,
3783 don't mark it live here. If we are successful in
3784 eliminating it, it need not be live unless it is used for
3785 pseudos, in which case it will have been set live when it
3786 was allocated to the pseudos. If the register will not
3787 be eliminated, reload will set it live at that point.
3789 Otherwise, record that this function uses this register. */
3790 /* ??? The PPC backend tries to "eliminate" on the pic
3791 register to itself. This should be fixed. In the mean
3792 time, hack around it. */
3794 if (! (TEST_HARD_REG_BIT (elim_reg_set, regno_first)
3795 && (regno_first == FRAME_POINTER_REGNUM
3796 || regno_first == ARG_POINTER_REGNUM)))
3797 for (i = regno_first; i <= regno_last; ++i)
3798 regs_ever_live[i] = 1;
3800 else
3802 /* Keep track of which basic block each reg appears in. */
3804 int blocknum = pbi->bb->index;
3805 if (REG_BASIC_BLOCK (regno_first) == REG_BLOCK_UNKNOWN)
3806 REG_BASIC_BLOCK (regno_first) = blocknum;
3807 else if (REG_BASIC_BLOCK (regno_first) != blocknum)
3808 REG_BASIC_BLOCK (regno_first) = REG_BLOCK_GLOBAL;
3810 /* Count (weighted) number of uses of each reg. */
3811 REG_FREQ (regno_first) += REG_FREQ_FROM_BB (pbi->bb);
3812 REG_N_REFS (regno_first)++;
3814 for (i = regno_first; i <= regno_last; ++i)
3815 if (! REGNO_REG_SET_P (pbi->reg_live, i))
3817 gcc_assert (!reg_deaths[i]);
3818 reg_deaths[i] = pbi->insn_num;
3822 /* Record and count the insns in which a reg dies. If it is used in
3823 this insn and was dead below the insn then it dies in this insn.
3824 If it was set in this insn, we do not make a REG_DEAD note;
3825 likewise if we already made such a note. */
3826 if ((pbi->flags & (PROP_DEATH_NOTES | PROP_REG_INFO))
3827 && some_was_dead
3828 && some_not_set)
3830 /* Check for the case where the register dying partially
3831 overlaps the register set by this insn. */
3832 if (regno_first != regno_last)
3833 for (i = regno_first; i <= regno_last; ++i)
3834 some_was_live |= REGNO_REG_SET_P (pbi->new_set, i);
3836 /* If none of the words in X is needed, make a REG_DEAD note.
3837 Otherwise, we must make partial REG_DEAD notes. */
3838 if (! some_was_live)
3840 if ((pbi->flags & PROP_DEATH_NOTES)
3841 #ifdef STACK_REGS
3842 && (!(pbi->flags & PROP_POST_REGSTACK)
3843 || !IN_RANGE (REGNO (reg), FIRST_STACK_REG, LAST_STACK_REG))
3844 #endif
3845 && ! find_regno_note (insn, REG_DEAD, regno_first))
3846 REG_NOTES (insn)
3847 = alloc_EXPR_LIST (REG_DEAD, reg, REG_NOTES (insn));
3849 if (pbi->flags & PROP_REG_INFO)
3850 REG_N_DEATHS (regno_first)++;
3852 else
3854 /* Don't make a REG_DEAD note for a part of a register
3855 that is set in the insn. */
3856 for (i = regno_first; i <= regno_last; ++i)
3857 if (! REGNO_REG_SET_P (pbi->reg_live, i)
3858 && ! dead_or_set_regno_p (insn, i))
3859 REG_NOTES (insn)
3860 = alloc_EXPR_LIST (REG_DEAD,
3861 regno_reg_rtx[i],
3862 REG_NOTES (insn));
3866 /* Mark the register as being live. */
3867 for (i = regno_first; i <= regno_last; ++i)
3869 #ifdef HAVE_conditional_execution
3870 int this_was_live = REGNO_REG_SET_P (pbi->reg_live, i);
3871 #endif
3873 SET_REGNO_REG_SET (pbi->reg_live, i);
3875 #ifdef HAVE_conditional_execution
3876 /* If this is a conditional use, record that fact. If it is later
3877 conditionally set, we'll know to kill the register. */
3878 if (cond != NULL_RTX)
3880 splay_tree_node node;
3881 struct reg_cond_life_info *rcli;
3882 rtx ncond;
3884 if (this_was_live)
3886 node = splay_tree_lookup (pbi->reg_cond_dead, i);
3887 if (node == NULL)
3889 /* The register was unconditionally live previously.
3890 No need to do anything. */
3892 else
3894 /* The register was conditionally live previously.
3895 Subtract the new life cond from the old death cond. */
3896 rcli = (struct reg_cond_life_info *) node->value;
3897 ncond = rcli->condition;
3898 ncond = and_reg_cond (ncond, not_reg_cond (cond), 1);
3900 /* If the register is now unconditionally live,
3901 remove the entry in the splay_tree. */
3902 if (ncond == const0_rtx)
3903 splay_tree_remove (pbi->reg_cond_dead, i);
3904 else
3906 rcli->condition = ncond;
3907 SET_REGNO_REG_SET (pbi->reg_cond_reg,
3908 REGNO (XEXP (cond, 0)));
3912 else
3914 /* The register was not previously live at all. Record
3915 the condition under which it is still dead. */
3916 rcli = XNEW (struct reg_cond_life_info);
3917 rcli->condition = not_reg_cond (cond);
3918 rcli->stores = const0_rtx;
3919 rcli->orig_condition = const0_rtx;
3920 splay_tree_insert (pbi->reg_cond_dead, i,
3921 (splay_tree_value) rcli);
3923 SET_REGNO_REG_SET (pbi->reg_cond_reg, REGNO (XEXP (cond, 0)));
3926 else if (this_was_live)
3928 /* The register may have been conditionally live previously, but
3929 is now unconditionally live. Remove it from the conditionally
3930 dead list, so that a conditional set won't cause us to think
3931 it dead. */
3932 splay_tree_remove (pbi->reg_cond_dead, i);
3934 #endif
3938 /* Scan expression X for registers which have to be marked used in PBI.
3939 X is considered to be the SET_DEST rtx of SET. TRUE is returned if
3940 X could be handled by this function. */
3942 static bool
3943 mark_used_dest_regs (struct propagate_block_info *pbi, rtx x, rtx cond, rtx insn)
3945 int regno;
3946 bool mark_dest = false;
3947 rtx dest = x;
3949 /* On some platforms calls return values spread over several
3950 locations. These locations are wrapped in a EXPR_LIST rtx
3951 together with a CONST_INT offset. */
3952 if (GET_CODE (x) == EXPR_LIST
3953 && GET_CODE (XEXP (x, 1)) == CONST_INT)
3954 x = XEXP (x, 0);
3956 if (x == NULL_RTX)
3957 return false;
3959 /* If storing into MEM, don't show it as being used. But do
3960 show the address as being used. */
3961 if (MEM_P (x))
3963 #ifdef AUTO_INC_DEC
3964 if (pbi->flags & PROP_AUTOINC)
3965 find_auto_inc (pbi, x, insn);
3966 #endif
3967 mark_used_regs (pbi, XEXP (x, 0), cond, insn);
3968 return true;
3971 /* Storing in STRICT_LOW_PART is like storing in a reg
3972 in that this SET might be dead, so ignore it in TESTREG.
3973 but in some other ways it is like using the reg.
3975 Storing in a SUBREG or a bit field is like storing the entire
3976 register in that if the register's value is not used
3977 then this SET is not needed. */
3978 while (GET_CODE (x) == STRICT_LOW_PART
3979 || GET_CODE (x) == ZERO_EXTRACT
3980 || GET_CODE (x) == SUBREG)
3982 #ifdef CANNOT_CHANGE_MODE_CLASS
3983 if ((pbi->flags & PROP_REG_INFO) && GET_CODE (x) == SUBREG)
3984 record_subregs_of_mode (x);
3985 #endif
3987 /* Modifying a single register in an alternate mode
3988 does not use any of the old value. But these other
3989 ways of storing in a register do use the old value. */
3990 if (GET_CODE (x) == SUBREG
3991 && !((REG_BYTES (SUBREG_REG (x))
3992 + UNITS_PER_WORD - 1) / UNITS_PER_WORD
3993 > (REG_BYTES (x)
3994 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))
3996 else
3997 mark_dest = true;
3999 x = XEXP (x, 0);
4002 /* If this is a store into a register or group of registers,
4003 recursively scan the value being stored. */
4004 if (REG_P (x)
4005 && (regno = REGNO (x),
4006 !(regno == FRAME_POINTER_REGNUM
4007 && (!reload_completed || frame_pointer_needed)))
4008 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
4009 && !(regno == HARD_FRAME_POINTER_REGNUM
4010 && (!reload_completed || frame_pointer_needed))
4011 #endif
4012 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
4013 && !(regno == ARG_POINTER_REGNUM && fixed_regs[regno])
4014 #endif
4017 if (mark_dest)
4018 mark_used_regs (pbi, dest, cond, insn);
4019 return true;
4021 return false;
4024 /* Scan expression X and store a 1-bit in NEW_LIVE for each reg it uses.
4025 This is done assuming the registers needed from X are those that
4026 have 1-bits in PBI->REG_LIVE.
4028 INSN is the containing instruction. If INSN is dead, this function
4029 is not called. */
4031 static void
4032 mark_used_regs (struct propagate_block_info *pbi, rtx x, rtx cond, rtx insn)
4034 RTX_CODE code;
4035 int flags = pbi->flags;
4037 retry:
4038 if (!x)
4039 return;
4040 code = GET_CODE (x);
4041 switch (code)
4043 case LABEL_REF:
4044 case SYMBOL_REF:
4045 case CONST_INT:
4046 case CONST:
4047 case CONST_DOUBLE:
4048 case CONST_VECTOR:
4049 case PC:
4050 case ADDR_VEC:
4051 case ADDR_DIFF_VEC:
4052 return;
4054 #ifdef HAVE_cc0
4055 case CC0:
4056 pbi->cc0_live = 1;
4057 return;
4058 #endif
4060 case CLOBBER:
4061 /* If we are clobbering a MEM, mark any registers inside the address
4062 as being used. */
4063 if (MEM_P (XEXP (x, 0)))
4064 mark_used_regs (pbi, XEXP (XEXP (x, 0), 0), cond, insn);
4065 return;
4067 case MEM:
4068 /* Don't bother watching stores to mems if this is not the
4069 final pass. We'll not be deleting dead stores this round. */
4070 if (optimize && (flags & PROP_SCAN_DEAD_STORES))
4072 /* Invalidate the data for the last MEM stored, but only if MEM is
4073 something that can be stored into. */
4074 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
4075 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
4076 /* Needn't clear the memory set list. */
4078 else
4080 rtx temp = pbi->mem_set_list;
4081 rtx prev = NULL_RTX;
4082 rtx next;
4084 while (temp)
4086 next = XEXP (temp, 1);
4087 if (anti_dependence (XEXP (temp, 0), x))
4089 /* Splice temp out of the list. */
4090 if (prev)
4091 XEXP (prev, 1) = next;
4092 else
4093 pbi->mem_set_list = next;
4094 free_EXPR_LIST_node (temp);
4095 pbi->mem_set_list_len--;
4097 else
4098 prev = temp;
4099 temp = next;
4103 /* If the memory reference had embedded side effects (autoincrement
4104 address modes. Then we may need to kill some entries on the
4105 memory set list. */
4106 if (insn)
4107 for_each_rtx (&PATTERN (insn), invalidate_mems_from_autoinc, pbi);
4110 #ifdef AUTO_INC_DEC
4111 if (flags & PROP_AUTOINC)
4112 find_auto_inc (pbi, x, insn);
4113 #endif
4114 break;
4116 case SUBREG:
4117 #ifdef CANNOT_CHANGE_MODE_CLASS
4118 if (flags & PROP_REG_INFO)
4119 record_subregs_of_mode (x);
4120 #endif
4122 /* While we're here, optimize this case. */
4123 x = SUBREG_REG (x);
4124 if (!REG_P (x))
4125 goto retry;
4126 /* Fall through. */
4128 case REG:
4129 /* See a register other than being set => mark it as needed. */
4130 mark_used_reg (pbi, x, cond, insn);
4131 return;
4133 case SET:
4135 rtx dest = SET_DEST (x);
4136 int i;
4137 bool ret = false;
4139 if (GET_CODE (dest) == PARALLEL)
4140 for (i = 0; i < XVECLEN (dest, 0); i++)
4141 ret |= mark_used_dest_regs (pbi, XVECEXP (dest, 0, i), cond, insn);
4142 else
4143 ret = mark_used_dest_regs (pbi, dest, cond, insn);
4145 if (ret)
4147 mark_used_regs (pbi, SET_SRC (x), cond, insn);
4148 return;
4151 break;
4153 case ASM_OPERANDS:
4154 case UNSPEC_VOLATILE:
4155 case TRAP_IF:
4156 case ASM_INPUT:
4158 /* Traditional and volatile asm instructions must be considered to use
4159 and clobber all hard registers, all pseudo-registers and all of
4160 memory. So must TRAP_IF and UNSPEC_VOLATILE operations.
4162 Consider for instance a volatile asm that changes the fpu rounding
4163 mode. An insn should not be moved across this even if it only uses
4164 pseudo-regs because it might give an incorrectly rounded result.
4166 ?!? Unfortunately, marking all hard registers as live causes massive
4167 problems for the register allocator and marking all pseudos as live
4168 creates mountains of uninitialized variable warnings.
4170 So for now, just clear the memory set list and mark any regs
4171 we can find in ASM_OPERANDS as used. */
4172 if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
4174 free_EXPR_LIST_list (&pbi->mem_set_list);
4175 pbi->mem_set_list_len = 0;
4178 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
4179 We can not just fall through here since then we would be confused
4180 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
4181 traditional asms unlike their normal usage. */
4182 if (code == ASM_OPERANDS)
4184 int j;
4186 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
4187 mark_used_regs (pbi, ASM_OPERANDS_INPUT (x, j), cond, insn);
4189 break;
4192 case COND_EXEC:
4193 gcc_assert (!cond);
4195 mark_used_regs (pbi, COND_EXEC_TEST (x), NULL_RTX, insn);
4197 cond = COND_EXEC_TEST (x);
4198 x = COND_EXEC_CODE (x);
4199 goto retry;
4201 default:
4202 break;
4205 /* Recursively scan the operands of this expression. */
4208 const char * const fmt = GET_RTX_FORMAT (code);
4209 int i;
4211 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4213 if (fmt[i] == 'e')
4215 /* Tail recursive case: save a function call level. */
4216 if (i == 0)
4218 x = XEXP (x, 0);
4219 goto retry;
4221 mark_used_regs (pbi, XEXP (x, i), cond, insn);
4223 else if (fmt[i] == 'E')
4225 int j;
4226 for (j = 0; j < XVECLEN (x, i); j++)
4227 mark_used_regs (pbi, XVECEXP (x, i, j), cond, insn);
4233 #ifdef AUTO_INC_DEC
4235 static int
4236 try_pre_increment_1 (struct propagate_block_info *pbi, rtx insn)
4238 /* Find the next use of this reg. If in same basic block,
4239 make it do pre-increment or pre-decrement if appropriate. */
4240 rtx x = single_set (insn);
4241 HOST_WIDE_INT amount = ((GET_CODE (SET_SRC (x)) == PLUS ? 1 : -1)
4242 * INTVAL (XEXP (SET_SRC (x), 1)));
4243 int regno = REGNO (SET_DEST (x));
4244 rtx y = pbi->reg_next_use[regno];
4245 if (y != 0
4246 && SET_DEST (x) != stack_pointer_rtx
4247 && BLOCK_NUM (y) == BLOCK_NUM (insn)
4248 /* Don't do this if the reg dies, or gets set in y; a standard addressing
4249 mode would be better. */
4250 && ! dead_or_set_p (y, SET_DEST (x))
4251 && try_pre_increment (y, SET_DEST (x), amount))
4253 /* We have found a suitable auto-increment and already changed
4254 insn Y to do it. So flush this increment instruction. */
4255 propagate_block_delete_insn (insn);
4257 /* Count a reference to this reg for the increment insn we are
4258 deleting. When a reg is incremented, spilling it is worse,
4259 so we want to make that less likely. */
4260 if (regno >= FIRST_PSEUDO_REGISTER)
4262 REG_FREQ (regno) += REG_FREQ_FROM_BB (pbi->bb);
4263 REG_N_SETS (regno)++;
4266 /* Flush any remembered memories depending on the value of
4267 the incremented register. */
4268 invalidate_mems_from_set (pbi, SET_DEST (x));
4270 return 1;
4272 return 0;
4275 /* Try to change INSN so that it does pre-increment or pre-decrement
4276 addressing on register REG in order to add AMOUNT to REG.
4277 AMOUNT is negative for pre-decrement.
4278 Returns 1 if the change could be made.
4279 This checks all about the validity of the result of modifying INSN. */
4281 static int
4282 try_pre_increment (rtx insn, rtx reg, HOST_WIDE_INT amount)
4284 rtx use;
4286 /* Nonzero if we can try to make a pre-increment or pre-decrement.
4287 For example, addl $4,r1; movl (r1),... can become movl +(r1),... */
4288 int pre_ok = 0;
4289 /* Nonzero if we can try to make a post-increment or post-decrement.
4290 For example, addl $4,r1; movl -4(r1),... can become movl (r1)+,...
4291 It is possible for both PRE_OK and POST_OK to be nonzero if the machine
4292 supports both pre-inc and post-inc, or both pre-dec and post-dec. */
4293 int post_ok = 0;
4295 /* Nonzero if the opportunity actually requires post-inc or post-dec. */
4296 int do_post = 0;
4298 /* From the sign of increment, see which possibilities are conceivable
4299 on this target machine. */
4300 if (HAVE_PRE_INCREMENT && amount > 0)
4301 pre_ok = 1;
4302 if (HAVE_POST_INCREMENT && amount > 0)
4303 post_ok = 1;
4305 if (HAVE_PRE_DECREMENT && amount < 0)
4306 pre_ok = 1;
4307 if (HAVE_POST_DECREMENT && amount < 0)
4308 post_ok = 1;
4310 if (! (pre_ok || post_ok))
4311 return 0;
4313 /* It is not safe to add a side effect to a jump insn
4314 because if the incremented register is spilled and must be reloaded
4315 there would be no way to store the incremented value back in memory. */
4317 if (JUMP_P (insn))
4318 return 0;
4320 use = 0;
4321 if (pre_ok)
4322 use = find_use_as_address (PATTERN (insn), reg, 0);
4323 if (post_ok && (use == 0 || use == (rtx) (size_t) 1))
4325 use = find_use_as_address (PATTERN (insn), reg, -amount);
4326 do_post = 1;
4329 if (use == 0 || use == (rtx) (size_t) 1)
4330 return 0;
4332 if (GET_MODE_SIZE (GET_MODE (use)) != (amount > 0 ? amount : - amount))
4333 return 0;
4335 /* See if this combination of instruction and addressing mode exists. */
4336 if (! validate_change (insn, &XEXP (use, 0),
4337 gen_rtx_fmt_e (amount > 0
4338 ? (do_post ? POST_INC : PRE_INC)
4339 : (do_post ? POST_DEC : PRE_DEC),
4340 Pmode, reg), 0))
4341 return 0;
4343 /* Record that this insn now has an implicit side effect on X. */
4344 REG_NOTES (insn) = alloc_EXPR_LIST (REG_INC, reg, REG_NOTES (insn));
4345 return 1;
4348 #endif /* AUTO_INC_DEC */
4350 /* Find the place in the rtx X where REG is used as a memory address.
4351 Return the MEM rtx that so uses it.
4352 If PLUSCONST is nonzero, search instead for a memory address equivalent to
4353 (plus REG (const_int PLUSCONST)).
4355 If such an address does not appear, return 0.
4356 If REG appears more than once, or is used other than in such an address,
4357 return (rtx) 1. */
4360 find_use_as_address (rtx x, rtx reg, HOST_WIDE_INT plusconst)
4362 enum rtx_code code = GET_CODE (x);
4363 const char * const fmt = GET_RTX_FORMAT (code);
4364 int i;
4365 rtx value = 0;
4366 rtx tem;
4368 if (code == MEM && XEXP (x, 0) == reg && plusconst == 0)
4369 return x;
4371 if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
4372 && XEXP (XEXP (x, 0), 0) == reg
4373 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4374 && INTVAL (XEXP (XEXP (x, 0), 1)) == plusconst)
4375 return x;
4377 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
4379 /* If REG occurs inside a MEM used in a bit-field reference,
4380 that is unacceptable. */
4381 if (find_use_as_address (XEXP (x, 0), reg, 0) != 0)
4382 return (rtx) (size_t) 1;
4385 if (x == reg)
4386 return (rtx) (size_t) 1;
4388 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4390 if (fmt[i] == 'e')
4392 tem = find_use_as_address (XEXP (x, i), reg, plusconst);
4393 if (value == 0)
4394 value = tem;
4395 else if (tem != 0)
4396 return (rtx) (size_t) 1;
4398 else if (fmt[i] == 'E')
4400 int j;
4401 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4403 tem = find_use_as_address (XVECEXP (x, i, j), reg, plusconst);
4404 if (value == 0)
4405 value = tem;
4406 else if (tem != 0)
4407 return (rtx) (size_t) 1;
4412 return value;
4415 /* Write information about registers and basic blocks into FILE.
4416 This is part of making a debugging dump. */
4418 void
4419 dump_regset (regset r, FILE *outf)
4421 unsigned i;
4422 reg_set_iterator rsi;
4424 if (r == NULL)
4426 fputs (" (nil)", outf);
4427 return;
4430 EXECUTE_IF_SET_IN_REG_SET (r, 0, i, rsi)
4432 fprintf (outf, " %d", i);
4433 if (i < FIRST_PSEUDO_REGISTER)
4434 fprintf (outf, " [%s]",
4435 reg_names[i]);
4439 /* Print a human-readable representation of R on the standard error
4440 stream. This function is designed to be used from within the
4441 debugger. */
4443 void
4444 debug_regset (regset r)
4446 dump_regset (r, stderr);
4447 putc ('\n', stderr);
4450 /* Recompute register set/reference counts immediately prior to register
4451 allocation.
4453 This avoids problems with set/reference counts changing to/from values
4454 which have special meanings to the register allocators.
4456 Additionally, the reference counts are the primary component used by the
4457 register allocators to prioritize pseudos for allocation to hard regs.
4458 More accurate reference counts generally lead to better register allocation.
4460 It might be worthwhile to update REG_LIVE_LENGTH, REG_BASIC_BLOCK and
4461 possibly other information which is used by the register allocators. */
4463 static unsigned int
4464 recompute_reg_usage (void)
4466 allocate_reg_life_data ();
4467 /* distribute_notes in combiner fails to convert some of the
4468 REG_UNUSED notes to REG_DEAD notes. This causes CHECK_DEAD_NOTES
4469 in sched1 to die. To solve this update the DEATH_NOTES
4470 here. */
4471 update_life_info (NULL, UPDATE_LIFE_LOCAL, PROP_REG_INFO | PROP_DEATH_NOTES);
4473 if (dump_file)
4474 dump_flow_info (dump_file, dump_flags);
4475 return 0;
4478 struct tree_opt_pass pass_recompute_reg_usage =
4480 "life2", /* name */
4481 NULL, /* gate */
4482 recompute_reg_usage, /* execute */
4483 NULL, /* sub */
4484 NULL, /* next */
4485 0, /* static_pass_number */
4486 0, /* tv_id */
4487 0, /* properties_required */
4488 0, /* properties_provided */
4489 0, /* properties_destroyed */
4490 0, /* todo_flags_start */
4491 TODO_dump_func, /* todo_flags_finish */
4492 'f' /* letter */
4495 /* Optionally removes all the REG_DEAD and REG_UNUSED notes from a set of
4496 blocks. If BLOCKS is NULL, assume the universal set. Returns a count
4497 of the number of registers that died.
4498 If KILL is 1, remove old REG_DEAD / REG_UNUSED notes. If it is 0, don't.
4499 if it is -1, remove them unless they pertain to a stack reg. */
4502 count_or_remove_death_notes (sbitmap blocks, int kill)
4504 int count = 0;
4505 unsigned int i = 0;
4506 basic_block bb;
4508 /* This used to be a loop over all the blocks with a membership test
4509 inside the loop. That can be amazingly expensive on a large CFG
4510 when only a small number of bits are set in BLOCKs (for example,
4511 the calls from the scheduler typically have very few bits set).
4513 For extra credit, someone should convert BLOCKS to a bitmap rather
4514 than an sbitmap. */
4515 if (blocks)
4517 sbitmap_iterator sbi;
4519 EXECUTE_IF_SET_IN_SBITMAP (blocks, 0, i, sbi)
4521 basic_block bb = BASIC_BLOCK (i);
4522 /* The bitmap may be flawed in that one of the basic blocks
4523 may have been deleted before you get here. */
4524 if (bb)
4525 count += count_or_remove_death_notes_bb (bb, kill);
4528 else
4530 FOR_EACH_BB (bb)
4532 count += count_or_remove_death_notes_bb (bb, kill);
4536 return count;
4539 /* Optionally removes all the REG_DEAD and REG_UNUSED notes from basic
4540 block BB. Returns a count of the number of registers that died. */
4542 static int
4543 count_or_remove_death_notes_bb (basic_block bb, int kill)
4545 int count = 0;
4546 rtx insn;
4548 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
4550 if (INSN_P (insn))
4552 rtx *pprev = &REG_NOTES (insn);
4553 rtx link = *pprev;
4555 while (link)
4557 switch (REG_NOTE_KIND (link))
4559 case REG_DEAD:
4560 if (REG_P (XEXP (link, 0)))
4562 rtx reg = XEXP (link, 0);
4563 int n;
4565 if (REGNO (reg) >= FIRST_PSEUDO_REGISTER)
4566 n = 1;
4567 else
4568 n = hard_regno_nregs[REGNO (reg)][GET_MODE (reg)];
4569 count += n;
4572 /* Fall through. */
4574 case REG_UNUSED:
4575 if (kill > 0
4576 || (kill
4577 #ifdef STACK_REGS
4578 && (!REG_P (XEXP (link, 0))
4579 || !IN_RANGE (REGNO (XEXP (link, 0)),
4580 FIRST_STACK_REG, LAST_STACK_REG))
4581 #endif
4584 rtx next = XEXP (link, 1);
4585 free_EXPR_LIST_node (link);
4586 *pprev = link = next;
4587 break;
4589 /* Fall through. */
4591 default:
4592 pprev = &XEXP (link, 1);
4593 link = *pprev;
4594 break;
4599 if (insn == BB_END (bb))
4600 break;
4603 return count;
4606 /* Clear LOG_LINKS fields of insns in a selected blocks or whole chain
4607 if blocks is NULL. */
4609 static void
4610 clear_log_links (sbitmap blocks)
4612 rtx insn;
4614 if (!blocks)
4616 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4617 if (INSN_P (insn))
4618 free_INSN_LIST_list (&LOG_LINKS (insn));
4620 else
4622 unsigned int i = 0;
4623 sbitmap_iterator sbi;
4625 EXECUTE_IF_SET_IN_SBITMAP (blocks, 0, i, sbi)
4627 basic_block bb = BASIC_BLOCK (i);
4629 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb));
4630 insn = NEXT_INSN (insn))
4631 if (INSN_P (insn))
4632 free_INSN_LIST_list (&LOG_LINKS (insn));
4637 /* Given a register bitmap, turn on the bits in a HARD_REG_SET that
4638 correspond to the hard registers, if any, set in that map. This
4639 could be done far more efficiently by having all sorts of special-cases
4640 with moving single words, but probably isn't worth the trouble. */
4642 void
4643 reg_set_to_hard_reg_set (HARD_REG_SET *to, bitmap from)
4645 unsigned i;
4646 bitmap_iterator bi;
4648 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
4650 if (i >= FIRST_PSEUDO_REGISTER)
4651 return;
4652 SET_HARD_REG_BIT (*to, i);
4657 static bool
4658 gate_remove_death_notes (void)
4660 return flag_profile_values;
4663 static unsigned int
4664 rest_of_handle_remove_death_notes (void)
4666 count_or_remove_death_notes (NULL, 1);
4667 return 0;
4670 struct tree_opt_pass pass_remove_death_notes =
4672 "ednotes", /* name */
4673 gate_remove_death_notes, /* gate */
4674 rest_of_handle_remove_death_notes, /* execute */
4675 NULL, /* sub */
4676 NULL, /* next */
4677 0, /* static_pass_number */
4678 0, /* tv_id */
4679 0, /* properties_required */
4680 0, /* properties_provided */
4681 0, /* properties_destroyed */
4682 0, /* todo_flags_start */
4683 0, /* todo_flags_finish */
4684 0 /* letter */
4687 /* Perform life analysis. */
4688 static unsigned int
4689 rest_of_handle_life (void)
4691 regclass_init ();
4693 life_analysis (PROP_FINAL);
4694 if (optimize)
4695 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_UPDATE_LIFE | CLEANUP_LOG_LINKS
4696 | (flag_thread_jumps ? CLEANUP_THREADING : 0));
4698 if (warn_clobbered)
4700 setjmp_vars_warning (DECL_INITIAL (current_function_decl));
4701 setjmp_args_warning ();
4704 if (optimize)
4706 if (initialize_uninitialized_subregs ())
4708 /* Insns were inserted, and possibly pseudos created, so
4709 things might look a bit different. */
4710 allocate_reg_life_data ();
4711 update_life_info (NULL, UPDATE_LIFE_GLOBAL_RM_NOTES,
4712 PROP_LOG_LINKS | PROP_REG_INFO | PROP_DEATH_NOTES);
4716 no_new_pseudos = 1;
4717 return 0;
4720 struct tree_opt_pass pass_life =
4722 "life1", /* name */
4723 NULL, /* gate */
4724 rest_of_handle_life, /* execute */
4725 NULL, /* sub */
4726 NULL, /* next */
4727 0, /* static_pass_number */
4728 TV_FLOW, /* tv_id */
4729 0, /* properties_required */
4730 0, /* properties_provided */
4731 0, /* properties_destroyed */
4732 TODO_verify_flow, /* todo_flags_start */
4733 TODO_dump_func |
4734 TODO_ggc_collect, /* todo_flags_finish */
4735 'f' /* letter */
4738 static unsigned int
4739 rest_of_handle_flow2 (void)
4741 /* If optimizing, then go ahead and split insns now. */
4742 #ifndef STACK_REGS
4743 if (optimize > 0)
4744 #endif
4745 split_all_insns (0);
4747 if (flag_branch_target_load_optimize)
4748 branch_target_load_optimize (epilogue_completed);
4750 if (optimize)
4751 cleanup_cfg (CLEANUP_EXPENSIVE);
4753 /* On some machines, the prologue and epilogue code, or parts thereof,
4754 can be represented as RTL. Doing so lets us schedule insns between
4755 it and the rest of the code and also allows delayed branch
4756 scheduling to operate in the epilogue. */
4757 thread_prologue_and_epilogue_insns (get_insns ());
4758 epilogue_completed = 1;
4759 flow2_completed = 1;
4760 return 0;
4763 struct tree_opt_pass pass_flow2 =
4765 "flow2", /* name */
4766 NULL, /* gate */
4767 rest_of_handle_flow2, /* execute */
4768 NULL, /* sub */
4769 NULL, /* next */
4770 0, /* static_pass_number */
4771 TV_FLOW2, /* tv_id */
4772 0, /* properties_required */
4773 0, /* properties_provided */
4774 0, /* properties_destroyed */
4775 TODO_verify_flow, /* todo_flags_start */
4776 TODO_dump_func |
4777 TODO_ggc_collect, /* todo_flags_finish */
4778 'w' /* letter */