2007-01-19 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / flow.c
blobfe07ec8add8c5e3d188a8a594182c773ac120a91
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;
1872 HARD_REG_SET clobbered_regs;
1874 cond = NULL_RTX;
1875 if (GET_CODE (PATTERN (insn)) == COND_EXEC)
1876 cond = COND_EXEC_TEST (PATTERN (insn));
1878 /* Non-constant calls clobber memory, constant calls do not
1879 clobber memory, though they may clobber outgoing arguments
1880 on the stack. */
1881 if (! CONST_OR_PURE_CALL_P (insn))
1883 free_EXPR_LIST_list (&pbi->mem_set_list);
1884 pbi->mem_set_list_len = 0;
1886 else
1887 invalidate_mems_from_set (pbi, stack_pointer_rtx);
1889 /* There may be extra registers to be clobbered. */
1890 for (note = CALL_INSN_FUNCTION_USAGE (insn);
1891 note;
1892 note = XEXP (note, 1))
1893 if (GET_CODE (XEXP (note, 0)) == CLOBBER)
1894 mark_set_1 (pbi, CLOBBER, XEXP (XEXP (note, 0), 0),
1895 cond, insn, pbi->flags);
1897 /* Calls change all call-used and global registers; sibcalls do not
1898 clobber anything that must be preserved at end-of-function,
1899 except for return values. */
1901 sibcall_p = SIBLING_CALL_P (insn);
1902 live_at_end = EXIT_BLOCK_PTR->il.rtl->global_live_at_start;
1903 get_call_invalidated_used_regs (insn, &clobbered_regs, true);
1904 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1905 if (TEST_HARD_REG_BIT (clobbered_regs, i)
1906 && ! (sibcall_p
1907 && REGNO_REG_SET_P (live_at_end, i)
1908 && ! refers_to_regno_p (i, i+1,
1909 current_function_return_rtx,
1910 (rtx *) 0)))
1912 enum rtx_code code = global_regs[i] ? SET : CLOBBER;
1913 /* We do not want REG_UNUSED notes for these registers. */
1914 mark_set_1 (pbi, code, regno_reg_rtx[i], cond, insn,
1915 pbi->flags & ~(PROP_DEATH_NOTES | PROP_REG_INFO));
1919 /* If an insn doesn't use CC0, it becomes dead since we assume
1920 that every insn clobbers it. So show it dead here;
1921 mark_used_regs will set it live if it is referenced. */
1922 pbi->cc0_live = 0;
1924 /* Record uses. */
1925 if (! insn_is_dead)
1926 mark_used_regs (pbi, PATTERN (insn), NULL_RTX, insn);
1928 /* Sometimes we may have inserted something before INSN (such as a move)
1929 when we make an auto-inc. So ensure we will scan those insns. */
1930 #ifdef AUTO_INC_DEC
1931 prev = PREV_INSN (insn);
1932 #endif
1934 if (! insn_is_dead && CALL_P (insn))
1936 int i;
1937 rtx note, cond;
1939 cond = NULL_RTX;
1940 if (GET_CODE (PATTERN (insn)) == COND_EXEC)
1941 cond = COND_EXEC_TEST (PATTERN (insn));
1943 /* Calls use their arguments, and may clobber memory which
1944 address involves some register. */
1945 for (note = CALL_INSN_FUNCTION_USAGE (insn);
1946 note;
1947 note = XEXP (note, 1))
1948 /* We find USE or CLOBBER entities in a FUNCTION_USAGE list: both
1949 of which mark_used_regs knows how to handle. */
1950 mark_used_regs (pbi, XEXP (XEXP (note, 0), 0), cond, insn);
1952 /* The stack ptr is used (honorarily) by a CALL insn. */
1953 if ((flags & PROP_REG_INFO)
1954 && !REGNO_REG_SET_P (pbi->reg_live, STACK_POINTER_REGNUM))
1955 reg_deaths[STACK_POINTER_REGNUM] = pbi->insn_num;
1956 SET_REGNO_REG_SET (pbi->reg_live, STACK_POINTER_REGNUM);
1958 /* Calls may also reference any of the global registers,
1959 so they are made live. */
1960 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1961 if (global_regs[i])
1962 mark_used_reg (pbi, regno_reg_rtx[i], cond, insn);
1966 pbi->insn_num++;
1968 return prev;
1971 /* Initialize a propagate_block_info struct for public consumption.
1972 Note that the structure itself is opaque to this file, but that
1973 the user can use the regsets provided here. */
1975 struct propagate_block_info *
1976 init_propagate_block_info (basic_block bb, regset live, regset local_set,
1977 regset cond_local_set, int flags)
1979 struct propagate_block_info *pbi = XNEW (struct propagate_block_info);
1981 pbi->bb = bb;
1982 pbi->reg_live = live;
1983 pbi->mem_set_list = NULL_RTX;
1984 pbi->mem_set_list_len = 0;
1985 pbi->local_set = local_set;
1986 pbi->cond_local_set = cond_local_set;
1987 pbi->cc0_live = 0;
1988 pbi->flags = flags;
1989 pbi->insn_num = 0;
1991 if (flags & (PROP_LOG_LINKS | PROP_AUTOINC))
1992 pbi->reg_next_use = XCNEWVEC (rtx, max_reg_num ());
1993 else
1994 pbi->reg_next_use = NULL;
1996 pbi->new_set = BITMAP_ALLOC (NULL);
1998 #ifdef HAVE_conditional_execution
1999 pbi->reg_cond_dead = splay_tree_new (splay_tree_compare_ints, NULL,
2000 free_reg_cond_life_info);
2001 pbi->reg_cond_reg = BITMAP_ALLOC (NULL);
2003 /* If this block ends in a conditional branch, for each register
2004 live from one side of the branch and not the other, record the
2005 register as conditionally dead. */
2006 if (JUMP_P (BB_END (bb))
2007 && any_condjump_p (BB_END (bb)))
2009 regset diff = ALLOC_REG_SET (&reg_obstack);
2010 basic_block bb_true, bb_false;
2011 unsigned i;
2013 /* Identify the successor blocks. */
2014 bb_true = EDGE_SUCC (bb, 0)->dest;
2015 if (!single_succ_p (bb))
2017 bb_false = EDGE_SUCC (bb, 1)->dest;
2019 if (EDGE_SUCC (bb, 0)->flags & EDGE_FALLTHRU)
2021 basic_block t = bb_false;
2022 bb_false = bb_true;
2023 bb_true = t;
2025 else
2026 gcc_assert (EDGE_SUCC (bb, 1)->flags & EDGE_FALLTHRU);
2028 else
2030 /* This can happen with a conditional jump to the next insn. */
2031 gcc_assert (JUMP_LABEL (BB_END (bb)) == BB_HEAD (bb_true));
2033 /* Simplest way to do nothing. */
2034 bb_false = bb_true;
2037 /* Compute which register lead different lives in the successors. */
2038 bitmap_xor (diff, bb_true->il.rtl->global_live_at_start,
2039 bb_false->il.rtl->global_live_at_start);
2041 if (!bitmap_empty_p (diff))
2043 /* Extract the condition from the branch. */
2044 rtx set_src = SET_SRC (pc_set (BB_END (bb)));
2045 rtx cond_true = XEXP (set_src, 0);
2046 rtx reg = XEXP (cond_true, 0);
2047 enum rtx_code inv_cond;
2049 if (GET_CODE (reg) == SUBREG)
2050 reg = SUBREG_REG (reg);
2052 /* We can only track conditional lifetimes if the condition is
2053 in the form of a reversible comparison of a register against
2054 zero. If the condition is more complex than that, then it is
2055 safe not to record any information. */
2056 inv_cond = reversed_comparison_code (cond_true, BB_END (bb));
2057 if (inv_cond != UNKNOWN
2058 && REG_P (reg)
2059 && XEXP (cond_true, 1) == const0_rtx)
2061 rtx cond_false
2062 = gen_rtx_fmt_ee (inv_cond,
2063 GET_MODE (cond_true), XEXP (cond_true, 0),
2064 XEXP (cond_true, 1));
2065 reg_set_iterator rsi;
2067 if (GET_CODE (XEXP (set_src, 1)) == PC)
2069 rtx t = cond_false;
2070 cond_false = cond_true;
2071 cond_true = t;
2074 SET_REGNO_REG_SET (pbi->reg_cond_reg, REGNO (reg));
2076 /* For each such register, mark it conditionally dead. */
2077 EXECUTE_IF_SET_IN_REG_SET (diff, 0, i, rsi)
2079 struct reg_cond_life_info *rcli;
2080 rtx cond;
2082 rcli = XNEW (struct reg_cond_life_info);
2084 if (REGNO_REG_SET_P (bb_true->il.rtl->global_live_at_start,
2086 cond = cond_false;
2087 else
2088 cond = cond_true;
2089 rcli->condition = cond;
2090 rcli->stores = const0_rtx;
2091 rcli->orig_condition = cond;
2093 splay_tree_insert (pbi->reg_cond_dead, i,
2094 (splay_tree_value) rcli);
2099 FREE_REG_SET (diff);
2101 #endif
2103 /* If this block has no successors, any stores to the frame that aren't
2104 used later in the block are dead. So make a pass over the block
2105 recording any such that are made and show them dead at the end. We do
2106 a very conservative and simple job here. */
2107 if (optimize
2108 && ! (TREE_CODE (TREE_TYPE (current_function_decl)) == FUNCTION_TYPE
2109 && (TYPE_RETURNS_STACK_DEPRESSED
2110 (TREE_TYPE (current_function_decl))))
2111 && (flags & PROP_SCAN_DEAD_STORES)
2112 && (EDGE_COUNT (bb->succs) == 0
2113 || (single_succ_p (bb)
2114 && single_succ (bb) == EXIT_BLOCK_PTR
2115 && ! current_function_calls_eh_return)))
2117 rtx insn, set;
2118 for (insn = BB_END (bb); insn != BB_HEAD (bb); insn = PREV_INSN (insn))
2119 if (NONJUMP_INSN_P (insn)
2120 && (set = single_set (insn))
2121 && MEM_P (SET_DEST (set)))
2123 rtx mem = SET_DEST (set);
2124 rtx canon_mem = canon_rtx (mem);
2126 if (XEXP (canon_mem, 0) == frame_pointer_rtx
2127 || (GET_CODE (XEXP (canon_mem, 0)) == PLUS
2128 && XEXP (XEXP (canon_mem, 0), 0) == frame_pointer_rtx
2129 && GET_CODE (XEXP (XEXP (canon_mem, 0), 1)) == CONST_INT))
2130 add_to_mem_set_list (pbi, canon_mem);
2134 return pbi;
2137 /* Release a propagate_block_info struct. */
2139 void
2140 free_propagate_block_info (struct propagate_block_info *pbi)
2142 free_EXPR_LIST_list (&pbi->mem_set_list);
2144 BITMAP_FREE (pbi->new_set);
2146 #ifdef HAVE_conditional_execution
2147 splay_tree_delete (pbi->reg_cond_dead);
2148 BITMAP_FREE (pbi->reg_cond_reg);
2149 #endif
2151 if (pbi->flags & PROP_REG_INFO)
2153 int num = pbi->insn_num;
2154 unsigned i;
2155 reg_set_iterator rsi;
2157 EXECUTE_IF_SET_IN_REG_SET (pbi->reg_live, 0, i, rsi)
2159 REG_LIVE_LENGTH (i) += num - reg_deaths[i];
2160 reg_deaths[i] = 0;
2163 if (pbi->reg_next_use)
2164 free (pbi->reg_next_use);
2166 free (pbi);
2169 /* Compute the registers live at the beginning of a basic block BB from
2170 those live at the end.
2172 When called, REG_LIVE contains those live at the end. On return, it
2173 contains those live at the beginning.
2175 LOCAL_SET, if non-null, will be set with all registers killed
2176 unconditionally by this basic block.
2177 Likewise, COND_LOCAL_SET, if non-null, will be set with all registers
2178 killed conditionally by this basic block. If there is any unconditional
2179 set of a register, then the corresponding bit will be set in LOCAL_SET
2180 and cleared in COND_LOCAL_SET.
2181 It is valid for LOCAL_SET and COND_LOCAL_SET to be the same set. In this
2182 case, the resulting set will be equal to the union of the two sets that
2183 would otherwise be computed.
2185 Return nonzero if an INSN is deleted (i.e. by dead code removal). */
2188 propagate_block (basic_block bb, regset live, regset local_set,
2189 regset cond_local_set, int flags)
2191 struct propagate_block_info *pbi;
2192 rtx insn, prev;
2193 int changed;
2195 pbi = init_propagate_block_info (bb, live, local_set, cond_local_set, flags);
2197 if (flags & PROP_REG_INFO)
2199 unsigned i;
2200 reg_set_iterator rsi;
2202 /* Process the regs live at the end of the block.
2203 Mark them as not local to any one basic block. */
2204 EXECUTE_IF_SET_IN_REG_SET (live, 0, i, rsi)
2205 REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL;
2208 /* Scan the block an insn at a time from end to beginning. */
2210 changed = 0;
2211 for (insn = BB_END (bb); ; insn = prev)
2213 /* If this is a call to `setjmp' et al, warn if any
2214 non-volatile datum is live. */
2215 if ((flags & PROP_REG_INFO)
2216 && CALL_P (insn)
2217 && find_reg_note (insn, REG_SETJMP, NULL))
2218 IOR_REG_SET (regs_live_at_setjmp, pbi->reg_live);
2220 prev = propagate_one_insn (pbi, insn);
2221 if (!prev)
2222 changed |= insn != get_insns ();
2223 else
2224 changed |= NEXT_INSN (prev) != insn;
2226 if (insn == BB_HEAD (bb))
2227 break;
2230 #ifdef EH_RETURN_DATA_REGNO
2231 if (bb_has_eh_pred (bb))
2233 unsigned int i;
2234 for (i = 0; ; ++i)
2236 unsigned regno = EH_RETURN_DATA_REGNO (i);
2237 if (regno == INVALID_REGNUM)
2238 break;
2239 if (pbi->local_set)
2241 CLEAR_REGNO_REG_SET (pbi->cond_local_set, regno);
2242 SET_REGNO_REG_SET (pbi->local_set, regno);
2244 if (REGNO_REG_SET_P (pbi->reg_live, regno))
2245 SET_REGNO_REG_SET (pbi->new_set, regno);
2247 regs_ever_live[regno] = 1;
2250 #endif
2252 free_propagate_block_info (pbi);
2254 return changed;
2257 /* Return 1 if X (the body of an insn, or part of it) is just dead stores
2258 (SET expressions whose destinations are registers dead after the insn).
2259 NEEDED is the regset that says which regs are alive after the insn.
2261 Unless CALL_OK is nonzero, an insn is needed if it contains a CALL.
2263 If X is the entire body of an insn, NOTES contains the reg notes
2264 pertaining to the insn. */
2266 static int
2267 insn_dead_p (struct propagate_block_info *pbi, rtx x, int call_ok,
2268 rtx notes ATTRIBUTE_UNUSED)
2270 enum rtx_code code = GET_CODE (x);
2272 /* Don't eliminate insns that may trap. */
2273 if (flag_non_call_exceptions && may_trap_p (x))
2274 return 0;
2276 #ifdef AUTO_INC_DEC
2277 /* As flow is invoked after combine, we must take existing AUTO_INC
2278 expressions into account. */
2279 for (; notes; notes = XEXP (notes, 1))
2281 if (REG_NOTE_KIND (notes) == REG_INC)
2283 int regno = REGNO (XEXP (notes, 0));
2285 /* Don't delete insns to set global regs. */
2286 if ((regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
2287 || REGNO_REG_SET_P (pbi->reg_live, regno))
2288 return 0;
2291 #endif
2293 /* If setting something that's a reg or part of one,
2294 see if that register's altered value will be live. */
2296 if (code == SET)
2298 rtx r = SET_DEST (x);
2300 #ifdef HAVE_cc0
2301 if (GET_CODE (r) == CC0)
2302 return ! pbi->cc0_live;
2303 #endif
2305 /* A SET that is a subroutine call cannot be dead. */
2306 if (GET_CODE (SET_SRC (x)) == CALL)
2308 if (! call_ok)
2309 return 0;
2312 /* Don't eliminate loads from volatile memory or volatile asms. */
2313 else if (volatile_refs_p (SET_SRC (x)))
2314 return 0;
2316 if (MEM_P (r))
2318 rtx temp, canon_r;
2320 if (MEM_VOLATILE_P (r) || GET_MODE (r) == BLKmode)
2321 return 0;
2323 canon_r = canon_rtx (r);
2325 /* Walk the set of memory locations we are currently tracking
2326 and see if one is an identical match to this memory location.
2327 If so, this memory write is dead (remember, we're walking
2328 backwards from the end of the block to the start). Since
2329 rtx_equal_p does not check the alias set or flags, we also
2330 must have the potential for them to conflict (anti_dependence). */
2331 for (temp = pbi->mem_set_list; temp != 0; temp = XEXP (temp, 1))
2332 if (anti_dependence (r, XEXP (temp, 0)))
2334 rtx mem = XEXP (temp, 0);
2336 if (rtx_equal_p (XEXP (canon_r, 0), XEXP (mem, 0))
2337 && (GET_MODE_SIZE (GET_MODE (canon_r))
2338 <= GET_MODE_SIZE (GET_MODE (mem))))
2339 return 1;
2341 #ifdef AUTO_INC_DEC
2342 /* Check if memory reference matches an auto increment. Only
2343 post increment/decrement or modify are valid. */
2344 if (GET_MODE (mem) == GET_MODE (r)
2345 && (GET_CODE (XEXP (mem, 0)) == POST_DEC
2346 || GET_CODE (XEXP (mem, 0)) == POST_INC
2347 || GET_CODE (XEXP (mem, 0)) == POST_MODIFY)
2348 && GET_MODE (XEXP (mem, 0)) == GET_MODE (r)
2349 && rtx_equal_p (XEXP (XEXP (mem, 0), 0), XEXP (r, 0)))
2350 return 1;
2351 #endif
2354 else
2356 while (GET_CODE (r) == SUBREG
2357 || GET_CODE (r) == STRICT_LOW_PART
2358 || GET_CODE (r) == ZERO_EXTRACT)
2359 r = XEXP (r, 0);
2361 if (REG_P (r))
2363 int regno = REGNO (r);
2365 /* Obvious. */
2366 if (REGNO_REG_SET_P (pbi->reg_live, regno))
2367 return 0;
2369 /* If this is a hard register, verify that subsequent
2370 words are not needed. */
2371 if (regno < FIRST_PSEUDO_REGISTER)
2373 int n = hard_regno_nregs[regno][GET_MODE (r)];
2375 while (--n > 0)
2376 if (REGNO_REG_SET_P (pbi->reg_live, regno+n))
2377 return 0;
2380 /* Don't delete insns to set global regs. */
2381 if (regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
2382 return 0;
2384 /* Make sure insns to set the stack pointer aren't deleted. */
2385 if (regno == STACK_POINTER_REGNUM)
2386 return 0;
2388 /* ??? These bits might be redundant with the force live bits
2389 in calculate_global_regs_live. We would delete from
2390 sequential sets; whether this actually affects real code
2391 for anything but the stack pointer I don't know. */
2392 /* Make sure insns to set the frame pointer aren't deleted. */
2393 if (regno == FRAME_POINTER_REGNUM
2394 && (! reload_completed || frame_pointer_needed))
2395 return 0;
2396 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2397 if (regno == HARD_FRAME_POINTER_REGNUM
2398 && (! reload_completed || frame_pointer_needed))
2399 return 0;
2400 #endif
2402 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2403 /* Make sure insns to set arg pointer are never deleted
2404 (if the arg pointer isn't fixed, there will be a USE
2405 for it, so we can treat it normally). */
2406 if (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
2407 return 0;
2408 #endif
2410 /* Otherwise, the set is dead. */
2411 return 1;
2416 /* If performing several activities, insn is dead if each activity
2417 is individually dead. Also, CLOBBERs and USEs can be ignored; a
2418 CLOBBER or USE that's inside a PARALLEL doesn't make the insn
2419 worth keeping. */
2420 else if (code == PARALLEL)
2422 int i = XVECLEN (x, 0);
2424 for (i--; i >= 0; i--)
2425 if (GET_CODE (XVECEXP (x, 0, i)) != CLOBBER
2426 && GET_CODE (XVECEXP (x, 0, i)) != USE
2427 && ! insn_dead_p (pbi, XVECEXP (x, 0, i), call_ok, NULL_RTX))
2428 return 0;
2430 return 1;
2433 /* A CLOBBER of a pseudo-register that is dead serves no purpose. That
2434 is not necessarily true for hard registers until after reload. */
2435 else if (code == CLOBBER)
2437 if (REG_P (XEXP (x, 0))
2438 && (REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER
2439 || reload_completed)
2440 && ! REGNO_REG_SET_P (pbi->reg_live, REGNO (XEXP (x, 0))))
2441 return 1;
2444 /* ??? A base USE is a historical relic. It ought not be needed anymore.
2445 Instances where it is still used are either (1) temporary and the USE
2446 escaped the pass, (2) cruft and the USE need not be emitted anymore,
2447 or (3) hiding bugs elsewhere that are not properly representing data
2448 flow. */
2450 return 0;
2453 /* If INSN is the last insn in a libcall, and assuming INSN is dead,
2454 return 1 if the entire library call is dead.
2455 This is true if INSN copies a register (hard or pseudo)
2456 and if the hard return reg of the call insn is dead.
2457 (The caller should have tested the destination of the SET inside
2458 INSN already for death.)
2460 If this insn doesn't just copy a register, then we don't
2461 have an ordinary libcall. In that case, cse could not have
2462 managed to substitute the source for the dest later on,
2463 so we can assume the libcall is dead.
2465 PBI is the block info giving pseudoregs live before this insn.
2466 NOTE is the REG_RETVAL note of the insn. */
2468 static int
2469 libcall_dead_p (struct propagate_block_info *pbi, rtx note, rtx insn)
2471 rtx x = single_set (insn);
2473 if (x)
2475 rtx r = SET_SRC (x);
2477 if (REG_P (r) || GET_CODE (r) == SUBREG)
2479 rtx call = XEXP (note, 0);
2480 rtx call_pat;
2481 int i;
2483 /* Find the call insn. */
2484 while (call != insn && !CALL_P (call))
2485 call = NEXT_INSN (call);
2487 /* If there is none, do nothing special,
2488 since ordinary death handling can understand these insns. */
2489 if (call == insn)
2490 return 0;
2492 /* See if the hard reg holding the value is dead.
2493 If this is a PARALLEL, find the call within it. */
2494 call_pat = PATTERN (call);
2495 if (GET_CODE (call_pat) == PARALLEL)
2497 for (i = XVECLEN (call_pat, 0) - 1; i >= 0; i--)
2498 if (GET_CODE (XVECEXP (call_pat, 0, i)) == SET
2499 && GET_CODE (SET_SRC (XVECEXP (call_pat, 0, i))) == CALL)
2500 break;
2502 /* This may be a library call that is returning a value
2503 via invisible pointer. Do nothing special, since
2504 ordinary death handling can understand these insns. */
2505 if (i < 0)
2506 return 0;
2508 call_pat = XVECEXP (call_pat, 0, i);
2511 if (! insn_dead_p (pbi, call_pat, 1, REG_NOTES (call)))
2512 return 0;
2514 while ((insn = PREV_INSN (insn)) != call)
2516 if (! INSN_P (insn))
2517 continue;
2518 if (! insn_dead_p (pbi, PATTERN (insn), 0, REG_NOTES (insn)))
2519 return 0;
2521 return 1;
2524 return 0;
2527 /* 1 if register REGNO was alive at a place where `setjmp' was called
2528 and was set more than once or is an argument.
2529 Such regs may be clobbered by `longjmp'. */
2532 regno_clobbered_at_setjmp (int regno)
2534 if (n_basic_blocks == NUM_FIXED_BLOCKS)
2535 return 0;
2537 return ((REG_N_SETS (regno) > 1
2538 || REGNO_REG_SET_P (ENTRY_BLOCK_PTR->il.rtl->global_live_at_end,
2539 regno))
2540 && REGNO_REG_SET_P (regs_live_at_setjmp, regno));
2543 /* Add MEM to PBI->MEM_SET_LIST. MEM should be canonical. Respect the
2544 maximal list size; look for overlaps in mode and select the largest. */
2545 static void
2546 add_to_mem_set_list (struct propagate_block_info *pbi, rtx mem)
2548 rtx i;
2550 /* We don't know how large a BLKmode store is, so we must not
2551 take them into consideration. */
2552 if (GET_MODE (mem) == BLKmode)
2553 return;
2555 for (i = pbi->mem_set_list; i ; i = XEXP (i, 1))
2557 rtx e = XEXP (i, 0);
2558 if (rtx_equal_p (XEXP (mem, 0), XEXP (e, 0)))
2560 if (GET_MODE_SIZE (GET_MODE (mem)) > GET_MODE_SIZE (GET_MODE (e)))
2562 #ifdef AUTO_INC_DEC
2563 /* If we must store a copy of the mem, we can just modify
2564 the mode of the stored copy. */
2565 if (pbi->flags & PROP_AUTOINC)
2566 PUT_MODE (e, GET_MODE (mem));
2567 else
2568 #endif
2569 XEXP (i, 0) = mem;
2571 return;
2575 if (pbi->mem_set_list_len < PARAM_VALUE (PARAM_MAX_FLOW_MEMORY_LOCATIONS))
2577 #ifdef AUTO_INC_DEC
2578 /* Store a copy of mem, otherwise the address may be
2579 scrogged by find_auto_inc. */
2580 if (pbi->flags & PROP_AUTOINC)
2581 mem = shallow_copy_rtx (mem);
2582 #endif
2583 pbi->mem_set_list = alloc_EXPR_LIST (0, mem, pbi->mem_set_list);
2584 pbi->mem_set_list_len++;
2588 /* INSN references memory, possibly using autoincrement addressing modes.
2589 Find any entries on the mem_set_list that need to be invalidated due
2590 to an address change. */
2592 static int
2593 invalidate_mems_from_autoinc (rtx *px, void *data)
2595 rtx x = *px;
2596 struct propagate_block_info *pbi = data;
2598 if (GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
2600 invalidate_mems_from_set (pbi, XEXP (x, 0));
2601 return -1;
2604 return 0;
2607 /* EXP is a REG or MEM. Remove any dependent entries from
2608 pbi->mem_set_list. */
2610 static void
2611 invalidate_mems_from_set (struct propagate_block_info *pbi, rtx exp)
2613 rtx temp = pbi->mem_set_list;
2614 rtx prev = NULL_RTX;
2615 rtx next;
2617 while (temp)
2619 next = XEXP (temp, 1);
2620 if ((REG_P (exp) && reg_overlap_mentioned_p (exp, XEXP (temp, 0)))
2621 /* When we get an EXP that is a mem here, we want to check if EXP
2622 overlaps the *address* of any of the mems in the list (i.e. not
2623 whether the mems actually overlap; that's done elsewhere). */
2624 || (MEM_P (exp)
2625 && reg_overlap_mentioned_p (exp, XEXP (XEXP (temp, 0), 0))))
2627 /* Splice this entry out of the list. */
2628 if (prev)
2629 XEXP (prev, 1) = next;
2630 else
2631 pbi->mem_set_list = next;
2632 free_EXPR_LIST_node (temp);
2633 pbi->mem_set_list_len--;
2635 else
2636 prev = temp;
2637 temp = next;
2641 /* Process the registers that are set within X. Their bits are set to
2642 1 in the regset DEAD, because they are dead prior to this insn.
2644 If INSN is nonzero, it is the insn being processed.
2646 FLAGS is the set of operations to perform. */
2648 static void
2649 mark_set_regs (struct propagate_block_info *pbi, rtx x, rtx insn)
2651 rtx cond = NULL_RTX;
2652 rtx link;
2653 enum rtx_code code;
2654 int flags = pbi->flags;
2656 if (insn)
2657 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2659 if (REG_NOTE_KIND (link) == REG_INC)
2660 mark_set_1 (pbi, SET, XEXP (link, 0),
2661 (GET_CODE (x) == COND_EXEC
2662 ? COND_EXEC_TEST (x) : NULL_RTX),
2663 insn, flags);
2665 retry:
2666 switch (code = GET_CODE (x))
2668 case SET:
2669 if (GET_CODE (XEXP (x, 1)) == ASM_OPERANDS)
2670 flags |= PROP_ASM_SCAN;
2671 /* Fall through */
2672 case CLOBBER:
2673 mark_set_1 (pbi, code, SET_DEST (x), cond, insn, flags);
2674 return;
2676 case COND_EXEC:
2677 cond = COND_EXEC_TEST (x);
2678 x = COND_EXEC_CODE (x);
2679 goto retry;
2681 case PARALLEL:
2683 int i;
2685 /* We must scan forwards. If we have an asm, we need to set
2686 the PROP_ASM_SCAN flag before scanning the clobbers. */
2687 for (i = 0; i < XVECLEN (x, 0); i++)
2689 rtx sub = XVECEXP (x, 0, i);
2690 switch (code = GET_CODE (sub))
2692 case COND_EXEC:
2693 gcc_assert (!cond);
2695 cond = COND_EXEC_TEST (sub);
2696 sub = COND_EXEC_CODE (sub);
2697 if (GET_CODE (sub) == SET)
2698 goto mark_set;
2699 if (GET_CODE (sub) == CLOBBER)
2700 goto mark_clob;
2701 break;
2703 case SET:
2704 mark_set:
2705 if (GET_CODE (XEXP (sub, 1)) == ASM_OPERANDS)
2706 flags |= PROP_ASM_SCAN;
2707 /* Fall through */
2708 case CLOBBER:
2709 mark_clob:
2710 mark_set_1 (pbi, code, SET_DEST (sub), cond, insn, flags);
2711 break;
2713 case ASM_OPERANDS:
2714 flags |= PROP_ASM_SCAN;
2715 break;
2717 default:
2718 break;
2721 break;
2724 default:
2725 break;
2729 /* Process a single set, which appears in INSN. REG (which may not
2730 actually be a REG, it may also be a SUBREG, PARALLEL, etc.) is
2731 being set using the CODE (which may be SET, CLOBBER, or COND_EXEC).
2732 If the set is conditional (because it appear in a COND_EXEC), COND
2733 will be the condition. */
2735 static void
2736 mark_set_1 (struct propagate_block_info *pbi, enum rtx_code code, rtx reg, rtx cond, rtx insn, int flags)
2738 int regno_first = -1, regno_last = -1;
2739 unsigned long not_dead = 0;
2740 int i;
2742 /* Modifying just one hardware register of a multi-reg value or just a
2743 byte field of a register does not mean the value from before this insn
2744 is now dead. Of course, if it was dead after it's unused now. */
2746 switch (GET_CODE (reg))
2748 case PARALLEL:
2749 /* Some targets place small structures in registers for return values of
2750 functions. We have to detect this case specially here to get correct
2751 flow information. */
2752 for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
2753 if (XEXP (XVECEXP (reg, 0, i), 0) != 0)
2754 mark_set_1 (pbi, code, XEXP (XVECEXP (reg, 0, i), 0), cond, insn,
2755 flags);
2756 return;
2758 case SIGN_EXTRACT:
2759 /* SIGN_EXTRACT cannot be an lvalue. */
2760 gcc_unreachable ();
2762 case ZERO_EXTRACT:
2763 case STRICT_LOW_PART:
2764 /* ??? Assumes STRICT_LOW_PART not used on multi-word registers. */
2766 reg = XEXP (reg, 0);
2767 while (GET_CODE (reg) == SUBREG
2768 || GET_CODE (reg) == ZERO_EXTRACT
2769 || GET_CODE (reg) == STRICT_LOW_PART);
2770 if (MEM_P (reg))
2771 break;
2772 not_dead = (unsigned long) REGNO_REG_SET_P (pbi->reg_live, REGNO (reg));
2773 /* Fall through. */
2775 case REG:
2776 regno_last = regno_first = REGNO (reg);
2777 if (regno_first < FIRST_PSEUDO_REGISTER)
2778 regno_last += hard_regno_nregs[regno_first][GET_MODE (reg)] - 1;
2779 break;
2781 case SUBREG:
2782 if (REG_P (SUBREG_REG (reg)))
2784 enum machine_mode outer_mode = GET_MODE (reg);
2785 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (reg));
2787 /* Identify the range of registers affected. This is moderately
2788 tricky for hard registers. See alter_subreg. */
2790 regno_last = regno_first = REGNO (SUBREG_REG (reg));
2791 if (regno_first < FIRST_PSEUDO_REGISTER)
2793 regno_first += subreg_regno_offset (regno_first, inner_mode,
2794 SUBREG_BYTE (reg),
2795 outer_mode);
2796 regno_last = (regno_first
2797 + hard_regno_nregs[regno_first][outer_mode] - 1);
2799 /* Since we've just adjusted the register number ranges, make
2800 sure REG matches. Otherwise some_was_live will be clear
2801 when it shouldn't have been, and we'll create incorrect
2802 REG_UNUSED notes. */
2803 reg = gen_rtx_REG (outer_mode, regno_first);
2805 else
2807 /* If the number of words in the subreg is less than the number
2808 of words in the full register, we have a well-defined partial
2809 set. Otherwise the high bits are undefined.
2811 This is only really applicable to pseudos, since we just took
2812 care of multi-word hard registers. */
2813 if (((GET_MODE_SIZE (outer_mode)
2814 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
2815 < ((GET_MODE_SIZE (inner_mode)
2816 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))
2817 not_dead = (unsigned long) REGNO_REG_SET_P (pbi->reg_live,
2818 regno_first);
2820 reg = SUBREG_REG (reg);
2823 else
2824 reg = SUBREG_REG (reg);
2825 break;
2827 default:
2828 break;
2831 /* If this set is a MEM, then it kills any aliased writes and any
2832 other MEMs which use it.
2833 If this set is a REG, then it kills any MEMs which use the reg. */
2834 if (optimize && (flags & PROP_SCAN_DEAD_STORES))
2836 if (REG_P (reg) || MEM_P (reg))
2837 invalidate_mems_from_set (pbi, reg);
2839 /* If the memory reference had embedded side effects (autoincrement
2840 address modes) then we may need to kill some entries on the
2841 memory set list. */
2842 if (insn && MEM_P (reg))
2843 for_each_rtx (&PATTERN (insn), invalidate_mems_from_autoinc, pbi);
2845 if (MEM_P (reg) && ! side_effects_p (reg)
2846 /* ??? With more effort we could track conditional memory life. */
2847 && ! cond)
2848 add_to_mem_set_list (pbi, canon_rtx (reg));
2851 if (REG_P (reg)
2852 && ! (regno_first == FRAME_POINTER_REGNUM
2853 && (! reload_completed || frame_pointer_needed))
2854 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2855 && ! (regno_first == HARD_FRAME_POINTER_REGNUM
2856 && (! reload_completed || frame_pointer_needed))
2857 #endif
2858 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2859 && ! (regno_first == ARG_POINTER_REGNUM && fixed_regs[regno_first])
2860 #endif
2863 int some_was_live = 0, some_was_dead = 0;
2865 for (i = regno_first; i <= regno_last; ++i)
2867 int needed_regno = REGNO_REG_SET_P (pbi->reg_live, i);
2868 if (pbi->local_set)
2870 /* Order of the set operation matters here since both
2871 sets may be the same. */
2872 CLEAR_REGNO_REG_SET (pbi->cond_local_set, i);
2873 if (cond != NULL_RTX
2874 && ! REGNO_REG_SET_P (pbi->local_set, i))
2875 SET_REGNO_REG_SET (pbi->cond_local_set, i);
2876 else
2877 SET_REGNO_REG_SET (pbi->local_set, i);
2879 if (code != CLOBBER || needed_regno)
2880 SET_REGNO_REG_SET (pbi->new_set, i);
2882 some_was_live |= needed_regno;
2883 some_was_dead |= ! needed_regno;
2886 #ifdef HAVE_conditional_execution
2887 /* Consider conditional death in deciding that the register needs
2888 a death note. */
2889 if (some_was_live && ! not_dead
2890 /* The stack pointer is never dead. Well, not strictly true,
2891 but it's very difficult to tell from here. Hopefully
2892 combine_stack_adjustments will fix up the most egregious
2893 errors. */
2894 && regno_first != STACK_POINTER_REGNUM)
2896 for (i = regno_first; i <= regno_last; ++i)
2897 if (! mark_regno_cond_dead (pbi, i, cond))
2898 not_dead |= ((unsigned long) 1) << (i - regno_first);
2900 #endif
2902 /* Additional data to record if this is the final pass. */
2903 if (flags & (PROP_LOG_LINKS | PROP_REG_INFO
2904 | PROP_DEATH_NOTES | PROP_AUTOINC))
2906 rtx y;
2907 int blocknum = pbi->bb->index;
2909 y = NULL_RTX;
2910 if (flags & (PROP_LOG_LINKS | PROP_AUTOINC))
2912 y = pbi->reg_next_use[regno_first];
2914 /* The next use is no longer next, since a store intervenes. */
2915 for (i = regno_first; i <= regno_last; ++i)
2916 pbi->reg_next_use[i] = 0;
2919 if (flags & PROP_REG_INFO)
2921 for (i = regno_first; i <= regno_last; ++i)
2923 /* Count (weighted) references, stores, etc. This counts a
2924 register twice if it is modified, but that is correct. */
2925 REG_N_SETS (i) += 1;
2926 REG_N_REFS (i) += 1;
2927 REG_FREQ (i) += REG_FREQ_FROM_BB (pbi->bb);
2929 /* The insns where a reg is live are normally counted
2930 elsewhere, but we want the count to include the insn
2931 where the reg is set, and the normal counting mechanism
2932 would not count it. */
2933 REG_LIVE_LENGTH (i) += 1;
2936 /* If this is a hard reg, record this function uses the reg. */
2937 if (regno_first < FIRST_PSEUDO_REGISTER)
2939 for (i = regno_first; i <= regno_last; i++)
2940 regs_ever_live[i] = 1;
2941 if (flags & PROP_ASM_SCAN)
2942 for (i = regno_first; i <= regno_last; i++)
2943 regs_asm_clobbered[i] = 1;
2945 else
2947 /* Keep track of which basic blocks each reg appears in. */
2948 if (REG_BASIC_BLOCK (regno_first) == REG_BLOCK_UNKNOWN)
2949 REG_BASIC_BLOCK (regno_first) = blocknum;
2950 else if (REG_BASIC_BLOCK (regno_first) != blocknum)
2951 REG_BASIC_BLOCK (regno_first) = REG_BLOCK_GLOBAL;
2955 if (! some_was_dead)
2957 if (flags & PROP_LOG_LINKS)
2959 /* Make a logical link from the next following insn
2960 that uses this register, back to this insn.
2961 The following insns have already been processed.
2963 We don't build a LOG_LINK for hard registers containing
2964 in ASM_OPERANDs. If these registers get replaced,
2965 we might wind up changing the semantics of the insn,
2966 even if reload can make what appear to be valid
2967 assignments later.
2969 We don't build a LOG_LINK for global registers to
2970 or from a function call. We don't want to let
2971 combine think that it knows what is going on with
2972 global registers. */
2973 if (y && (BLOCK_NUM (y) == blocknum)
2974 && (regno_first >= FIRST_PSEUDO_REGISTER
2975 || (asm_noperands (PATTERN (y)) < 0
2976 && ! ((CALL_P (insn)
2977 || CALL_P (y))
2978 && global_regs[regno_first]))))
2979 LOG_LINKS (y) = alloc_INSN_LIST (insn, LOG_LINKS (y));
2982 else if (not_dead)
2984 else if (! some_was_live)
2986 if (flags & PROP_REG_INFO)
2987 REG_N_DEATHS (regno_first) += 1;
2989 if (flags & PROP_DEATH_NOTES
2990 #ifdef STACK_REGS
2991 && (!(flags & PROP_POST_REGSTACK)
2992 || !IN_RANGE (REGNO (reg), FIRST_STACK_REG,
2993 LAST_STACK_REG))
2994 #endif
2997 /* Note that dead stores have already been deleted
2998 when possible. If we get here, we have found a
2999 dead store that cannot be eliminated (because the
3000 same insn does something useful). Indicate this
3001 by marking the reg being set as dying here. */
3002 REG_NOTES (insn)
3003 = alloc_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn));
3006 else
3008 if (flags & PROP_DEATH_NOTES
3009 #ifdef STACK_REGS
3010 && (!(flags & PROP_POST_REGSTACK)
3011 || !IN_RANGE (REGNO (reg), FIRST_STACK_REG,
3012 LAST_STACK_REG))
3013 #endif
3016 /* This is a case where we have a multi-word hard register
3017 and some, but not all, of the words of the register are
3018 needed in subsequent insns. Write REG_UNUSED notes
3019 for those parts that were not needed. This case should
3020 be rare. */
3022 for (i = regno_first; i <= regno_last; ++i)
3023 if (! REGNO_REG_SET_P (pbi->reg_live, i))
3024 REG_NOTES (insn)
3025 = alloc_EXPR_LIST (REG_UNUSED,
3026 regno_reg_rtx[i],
3027 REG_NOTES (insn));
3032 /* Mark the register as being dead. */
3033 if (some_was_live
3034 /* The stack pointer is never dead. Well, not strictly true,
3035 but it's very difficult to tell from here. Hopefully
3036 combine_stack_adjustments will fix up the most egregious
3037 errors. */
3038 && regno_first != STACK_POINTER_REGNUM)
3040 for (i = regno_first; i <= regno_last; ++i)
3041 if (!(not_dead & (((unsigned long) 1) << (i - regno_first))))
3043 if ((pbi->flags & PROP_REG_INFO)
3044 && REGNO_REG_SET_P (pbi->reg_live, i))
3046 REG_LIVE_LENGTH (i) += pbi->insn_num - reg_deaths[i];
3047 reg_deaths[i] = 0;
3049 CLEAR_REGNO_REG_SET (pbi->reg_live, i);
3051 if (flags & PROP_DEAD_INSN)
3052 emit_insn_after (gen_rtx_CLOBBER (VOIDmode, reg), insn);
3055 else if (REG_P (reg))
3057 if (flags & (PROP_LOG_LINKS | PROP_AUTOINC))
3058 pbi->reg_next_use[regno_first] = 0;
3060 if ((flags & PROP_REG_INFO) != 0
3061 && (flags & PROP_ASM_SCAN) != 0
3062 && regno_first < FIRST_PSEUDO_REGISTER)
3064 for (i = regno_first; i <= regno_last; i++)
3065 regs_asm_clobbered[i] = 1;
3069 /* If this is the last pass and this is a SCRATCH, show it will be dying
3070 here and count it. */
3071 else if (GET_CODE (reg) == SCRATCH)
3073 if (flags & PROP_DEATH_NOTES
3074 #ifdef STACK_REGS
3075 && (!(flags & PROP_POST_REGSTACK)
3076 || !IN_RANGE (REGNO (reg), FIRST_STACK_REG, LAST_STACK_REG))
3077 #endif
3079 REG_NOTES (insn)
3080 = alloc_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn));
3084 #ifdef HAVE_conditional_execution
3085 /* Mark REGNO conditionally dead.
3086 Return true if the register is now unconditionally dead. */
3088 static int
3089 mark_regno_cond_dead (struct propagate_block_info *pbi, int regno, rtx cond)
3091 /* If this is a store to a predicate register, the value of the
3092 predicate is changing, we don't know that the predicate as seen
3093 before is the same as that seen after. Flush all dependent
3094 conditions from reg_cond_dead. This will make all such
3095 conditionally live registers unconditionally live. */
3096 if (REGNO_REG_SET_P (pbi->reg_cond_reg, regno))
3097 flush_reg_cond_reg (pbi, regno);
3099 /* If this is an unconditional store, remove any conditional
3100 life that may have existed. */
3101 if (cond == NULL_RTX)
3102 splay_tree_remove (pbi->reg_cond_dead, regno);
3103 else
3105 splay_tree_node node;
3106 struct reg_cond_life_info *rcli;
3107 rtx ncond;
3109 /* Otherwise this is a conditional set. Record that fact.
3110 It may have been conditionally used, or there may be a
3111 subsequent set with a complementary condition. */
3113 node = splay_tree_lookup (pbi->reg_cond_dead, regno);
3114 if (node == NULL)
3116 /* The register was unconditionally live previously.
3117 Record the current condition as the condition under
3118 which it is dead. */
3119 rcli = XNEW (struct reg_cond_life_info);
3120 rcli->condition = cond;
3121 rcli->stores = cond;
3122 rcli->orig_condition = const0_rtx;
3123 splay_tree_insert (pbi->reg_cond_dead, regno,
3124 (splay_tree_value) rcli);
3126 SET_REGNO_REG_SET (pbi->reg_cond_reg, REGNO (XEXP (cond, 0)));
3128 /* Not unconditionally dead. */
3129 return 0;
3131 else
3133 /* The register was conditionally live previously.
3134 Add the new condition to the old. */
3135 rcli = (struct reg_cond_life_info *) node->value;
3136 ncond = rcli->condition;
3137 ncond = ior_reg_cond (ncond, cond, 1);
3138 if (rcli->stores == const0_rtx)
3139 rcli->stores = cond;
3140 else if (rcli->stores != const1_rtx)
3141 rcli->stores = ior_reg_cond (rcli->stores, cond, 1);
3143 /* If the register is now unconditionally dead, remove the entry
3144 in the splay_tree. A register is unconditionally dead if the
3145 dead condition ncond is true. A register is also unconditionally
3146 dead if the sum of all conditional stores is an unconditional
3147 store (stores is true), and the dead condition is identically the
3148 same as the original dead condition initialized at the end of
3149 the block. This is a pointer compare, not an rtx_equal_p
3150 compare. */
3151 if (ncond == const1_rtx
3152 || (ncond == rcli->orig_condition && rcli->stores == const1_rtx))
3153 splay_tree_remove (pbi->reg_cond_dead, regno);
3154 else
3156 rcli->condition = ncond;
3158 SET_REGNO_REG_SET (pbi->reg_cond_reg, REGNO (XEXP (cond, 0)));
3160 /* Not unconditionally dead. */
3161 return 0;
3166 return 1;
3169 /* Called from splay_tree_delete for pbi->reg_cond_life. */
3171 static void
3172 free_reg_cond_life_info (splay_tree_value value)
3174 struct reg_cond_life_info *rcli = (struct reg_cond_life_info *) value;
3175 free (rcli);
3178 /* Helper function for flush_reg_cond_reg. */
3180 static int
3181 flush_reg_cond_reg_1 (splay_tree_node node, void *data)
3183 struct reg_cond_life_info *rcli;
3184 int *xdata = (int *) data;
3185 unsigned int regno = xdata[0];
3187 /* Don't need to search if last flushed value was farther on in
3188 the in-order traversal. */
3189 if (xdata[1] >= (int) node->key)
3190 return 0;
3192 /* Splice out portions of the expression that refer to regno. */
3193 rcli = (struct reg_cond_life_info *) node->value;
3194 rcli->condition = elim_reg_cond (rcli->condition, regno);
3195 if (rcli->stores != const0_rtx && rcli->stores != const1_rtx)
3196 rcli->stores = elim_reg_cond (rcli->stores, regno);
3198 /* If the entire condition is now false, signal the node to be removed. */
3199 if (rcli->condition == const0_rtx)
3201 xdata[1] = node->key;
3202 return -1;
3204 else
3205 gcc_assert (rcli->condition != const1_rtx);
3207 return 0;
3210 /* Flush all (sub) expressions referring to REGNO from REG_COND_LIVE. */
3212 static void
3213 flush_reg_cond_reg (struct propagate_block_info *pbi, int regno)
3215 int pair[2];
3217 pair[0] = regno;
3218 pair[1] = -1;
3219 while (splay_tree_foreach (pbi->reg_cond_dead,
3220 flush_reg_cond_reg_1, pair) == -1)
3221 splay_tree_remove (pbi->reg_cond_dead, pair[1]);
3223 CLEAR_REGNO_REG_SET (pbi->reg_cond_reg, regno);
3226 /* Logical arithmetic on predicate conditions. IOR, NOT and AND.
3227 For ior/and, the ADD flag determines whether we want to add the new
3228 condition X to the old one unconditionally. If it is zero, we will
3229 only return a new expression if X allows us to simplify part of
3230 OLD, otherwise we return NULL to the caller.
3231 If ADD is nonzero, we will return a new condition in all cases. The
3232 toplevel caller of one of these functions should always pass 1 for
3233 ADD. */
3235 static rtx
3236 ior_reg_cond (rtx old, rtx x, int add)
3238 rtx op0, op1;
3240 if (COMPARISON_P (old))
3242 if (COMPARISON_P (x)
3243 && REVERSE_CONDEXEC_PREDICATES_P (x, old)
3244 && REGNO (XEXP (x, 0)) == REGNO (XEXP (old, 0)))
3245 return const1_rtx;
3246 if (GET_CODE (x) == GET_CODE (old)
3247 && REGNO (XEXP (x, 0)) == REGNO (XEXP (old, 0)))
3248 return old;
3249 if (! add)
3250 return NULL;
3251 return gen_rtx_IOR (0, old, x);
3254 switch (GET_CODE (old))
3256 case IOR:
3257 op0 = ior_reg_cond (XEXP (old, 0), x, 0);
3258 op1 = ior_reg_cond (XEXP (old, 1), x, 0);
3259 if (op0 != NULL || op1 != NULL)
3261 if (op0 == const0_rtx)
3262 return op1 ? op1 : gen_rtx_IOR (0, XEXP (old, 1), x);
3263 if (op1 == const0_rtx)
3264 return op0 ? op0 : gen_rtx_IOR (0, XEXP (old, 0), x);
3265 if (op0 == const1_rtx || op1 == const1_rtx)
3266 return const1_rtx;
3267 if (op0 == NULL)
3268 op0 = gen_rtx_IOR (0, XEXP (old, 0), x);
3269 else if (rtx_equal_p (x, op0))
3270 /* (x | A) | x ~ (x | A). */
3271 return old;
3272 if (op1 == NULL)
3273 op1 = gen_rtx_IOR (0, XEXP (old, 1), x);
3274 else if (rtx_equal_p (x, op1))
3275 /* (A | x) | x ~ (A | x). */
3276 return old;
3277 return gen_rtx_IOR (0, op0, op1);
3279 if (! add)
3280 return NULL;
3281 return gen_rtx_IOR (0, old, x);
3283 case AND:
3284 op0 = ior_reg_cond (XEXP (old, 0), x, 0);
3285 op1 = ior_reg_cond (XEXP (old, 1), x, 0);
3286 if (op0 != NULL || op1 != NULL)
3288 if (op0 == const1_rtx)
3289 return op1 ? op1 : gen_rtx_IOR (0, XEXP (old, 1), x);
3290 if (op1 == const1_rtx)
3291 return op0 ? op0 : gen_rtx_IOR (0, XEXP (old, 0), x);
3292 if (op0 == const0_rtx || op1 == const0_rtx)
3293 return const0_rtx;
3294 if (op0 == NULL)
3295 op0 = gen_rtx_IOR (0, XEXP (old, 0), x);
3296 else if (rtx_equal_p (x, op0))
3297 /* (x & A) | x ~ x. */
3298 return op0;
3299 if (op1 == NULL)
3300 op1 = gen_rtx_IOR (0, XEXP (old, 1), x);
3301 else if (rtx_equal_p (x, op1))
3302 /* (A & x) | x ~ x. */
3303 return op1;
3304 return gen_rtx_AND (0, op0, op1);
3306 if (! add)
3307 return NULL;
3308 return gen_rtx_IOR (0, old, x);
3310 case NOT:
3311 op0 = and_reg_cond (XEXP (old, 0), not_reg_cond (x), 0);
3312 if (op0 != NULL)
3313 return not_reg_cond (op0);
3314 if (! add)
3315 return NULL;
3316 return gen_rtx_IOR (0, old, x);
3318 default:
3319 gcc_unreachable ();
3323 static rtx
3324 not_reg_cond (rtx x)
3326 if (x == const0_rtx)
3327 return const1_rtx;
3328 else if (x == const1_rtx)
3329 return const0_rtx;
3330 if (GET_CODE (x) == NOT)
3331 return XEXP (x, 0);
3332 if (COMPARISON_P (x)
3333 && REG_P (XEXP (x, 0)))
3335 gcc_assert (XEXP (x, 1) == const0_rtx);
3337 return gen_rtx_fmt_ee (reversed_comparison_code (x, NULL),
3338 VOIDmode, XEXP (x, 0), const0_rtx);
3340 return gen_rtx_NOT (0, x);
3343 static rtx
3344 and_reg_cond (rtx old, rtx x, int add)
3346 rtx op0, op1;
3348 if (COMPARISON_P (old))
3350 if (COMPARISON_P (x)
3351 && GET_CODE (x) == reversed_comparison_code (old, NULL)
3352 && REGNO (XEXP (x, 0)) == REGNO (XEXP (old, 0)))
3353 return const0_rtx;
3354 if (GET_CODE (x) == GET_CODE (old)
3355 && REGNO (XEXP (x, 0)) == REGNO (XEXP (old, 0)))
3356 return old;
3357 if (! add)
3358 return NULL;
3359 return gen_rtx_AND (0, old, x);
3362 switch (GET_CODE (old))
3364 case IOR:
3365 op0 = and_reg_cond (XEXP (old, 0), x, 0);
3366 op1 = and_reg_cond (XEXP (old, 1), x, 0);
3367 if (op0 != NULL || op1 != NULL)
3369 if (op0 == const0_rtx)
3370 return op1 ? op1 : gen_rtx_AND (0, XEXP (old, 1), x);
3371 if (op1 == const0_rtx)
3372 return op0 ? op0 : gen_rtx_AND (0, XEXP (old, 0), x);
3373 if (op0 == const1_rtx || op1 == const1_rtx)
3374 return const1_rtx;
3375 if (op0 == NULL)
3376 op0 = gen_rtx_AND (0, XEXP (old, 0), x);
3377 else if (rtx_equal_p (x, op0))
3378 /* (x | A) & x ~ x. */
3379 return op0;
3380 if (op1 == NULL)
3381 op1 = gen_rtx_AND (0, XEXP (old, 1), x);
3382 else if (rtx_equal_p (x, op1))
3383 /* (A | x) & x ~ x. */
3384 return op1;
3385 return gen_rtx_IOR (0, op0, op1);
3387 if (! add)
3388 return NULL;
3389 return gen_rtx_AND (0, old, x);
3391 case AND:
3392 op0 = and_reg_cond (XEXP (old, 0), x, 0);
3393 op1 = and_reg_cond (XEXP (old, 1), x, 0);
3394 if (op0 != NULL || op1 != NULL)
3396 if (op0 == const1_rtx)
3397 return op1 ? op1 : gen_rtx_AND (0, XEXP (old, 1), x);
3398 if (op1 == const1_rtx)
3399 return op0 ? op0 : gen_rtx_AND (0, XEXP (old, 0), x);
3400 if (op0 == const0_rtx || op1 == const0_rtx)
3401 return const0_rtx;
3402 if (op0 == NULL)
3403 op0 = gen_rtx_AND (0, XEXP (old, 0), x);
3404 else if (rtx_equal_p (x, op0))
3405 /* (x & A) & x ~ (x & A). */
3406 return old;
3407 if (op1 == NULL)
3408 op1 = gen_rtx_AND (0, XEXP (old, 1), x);
3409 else if (rtx_equal_p (x, op1))
3410 /* (A & x) & x ~ (A & x). */
3411 return old;
3412 return gen_rtx_AND (0, op0, op1);
3414 if (! add)
3415 return NULL;
3416 return gen_rtx_AND (0, old, x);
3418 case NOT:
3419 op0 = ior_reg_cond (XEXP (old, 0), not_reg_cond (x), 0);
3420 if (op0 != NULL)
3421 return not_reg_cond (op0);
3422 if (! add)
3423 return NULL;
3424 return gen_rtx_AND (0, old, x);
3426 default:
3427 gcc_unreachable ();
3431 /* Given a condition X, remove references to reg REGNO and return the
3432 new condition. The removal will be done so that all conditions
3433 involving REGNO are considered to evaluate to false. This function
3434 is used when the value of REGNO changes. */
3436 static rtx
3437 elim_reg_cond (rtx x, unsigned int regno)
3439 rtx op0, op1;
3441 if (COMPARISON_P (x))
3443 if (REGNO (XEXP (x, 0)) == regno)
3444 return const0_rtx;
3445 return x;
3448 switch (GET_CODE (x))
3450 case AND:
3451 op0 = elim_reg_cond (XEXP (x, 0), regno);
3452 op1 = elim_reg_cond (XEXP (x, 1), regno);
3453 if (op0 == const0_rtx || op1 == const0_rtx)
3454 return const0_rtx;
3455 if (op0 == const1_rtx)
3456 return op1;
3457 if (op1 == const1_rtx)
3458 return op0;
3459 if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
3460 return x;
3461 return gen_rtx_AND (0, op0, op1);
3463 case IOR:
3464 op0 = elim_reg_cond (XEXP (x, 0), regno);
3465 op1 = elim_reg_cond (XEXP (x, 1), regno);
3466 if (op0 == const1_rtx || op1 == const1_rtx)
3467 return const1_rtx;
3468 if (op0 == const0_rtx)
3469 return op1;
3470 if (op1 == const0_rtx)
3471 return op0;
3472 if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
3473 return x;
3474 return gen_rtx_IOR (0, op0, op1);
3476 case NOT:
3477 op0 = elim_reg_cond (XEXP (x, 0), regno);
3478 if (op0 == const0_rtx)
3479 return const1_rtx;
3480 if (op0 == const1_rtx)
3481 return const0_rtx;
3482 if (op0 != XEXP (x, 0))
3483 return not_reg_cond (op0);
3484 return x;
3486 default:
3487 gcc_unreachable ();
3490 #endif /* HAVE_conditional_execution */
3492 #ifdef AUTO_INC_DEC
3494 /* Try to substitute the auto-inc expression INC as the address inside
3495 MEM which occurs in INSN. Currently, the address of MEM is an expression
3496 involving INCR_REG, and INCR is the next use of INCR_REG; it is an insn
3497 that has a single set whose source is a PLUS of INCR_REG and something
3498 else. */
3500 static void
3501 attempt_auto_inc (struct propagate_block_info *pbi, rtx inc, rtx insn,
3502 rtx mem, rtx incr, rtx incr_reg)
3504 int regno = REGNO (incr_reg);
3505 rtx set = single_set (incr);
3506 rtx q = SET_DEST (set);
3507 rtx y = SET_SRC (set);
3508 int opnum = XEXP (y, 0) == incr_reg ? 0 : 1;
3509 int changed;
3511 /* Make sure this reg appears only once in this insn. */
3512 if (count_occurrences (PATTERN (insn), incr_reg, 1) != 1)
3513 return;
3515 if (dead_or_set_p (incr, incr_reg)
3516 /* Mustn't autoinc an eliminable register. */
3517 && (regno >= FIRST_PSEUDO_REGISTER
3518 || ! TEST_HARD_REG_BIT (elim_reg_set, regno)))
3520 /* This is the simple case. Try to make the auto-inc. If
3521 we can't, we are done. Otherwise, we will do any
3522 needed updates below. */
3523 if (! validate_change (insn, &XEXP (mem, 0), inc, 0))
3524 return;
3526 else if (REG_P (q)
3527 /* PREV_INSN used here to check the semi-open interval
3528 [insn,incr). */
3529 && ! reg_used_between_p (q, PREV_INSN (insn), incr)
3530 /* We must also check for sets of q as q may be
3531 a call clobbered hard register and there may
3532 be a call between PREV_INSN (insn) and incr. */
3533 && ! reg_set_between_p (q, PREV_INSN (insn), incr))
3535 /* We have *p followed sometime later by q = p+size.
3536 Both p and q must be live afterward,
3537 and q is not used between INSN and its assignment.
3538 Change it to q = p, ...*q..., q = q+size.
3539 Then fall into the usual case. */
3540 rtx insns, temp;
3542 start_sequence ();
3543 emit_move_insn (q, incr_reg);
3544 insns = get_insns ();
3545 end_sequence ();
3547 /* If we can't make the auto-inc, or can't make the
3548 replacement into Y, exit. There's no point in making
3549 the change below if we can't do the auto-inc and doing
3550 so is not correct in the pre-inc case. */
3552 XEXP (inc, 0) = q;
3553 validate_change (insn, &XEXP (mem, 0), inc, 1);
3554 validate_change (incr, &XEXP (y, opnum), q, 1);
3555 if (! apply_change_group ())
3556 return;
3558 /* We now know we'll be doing this change, so emit the
3559 new insn(s) and do the updates. */
3560 emit_insn_before (insns, insn);
3562 if (BB_HEAD (pbi->bb) == insn)
3563 BB_HEAD (pbi->bb) = insns;
3565 /* INCR will become a NOTE and INSN won't contain a
3566 use of INCR_REG. If a use of INCR_REG was just placed in
3567 the insn before INSN, make that the next use.
3568 Otherwise, invalidate it. */
3569 if (NONJUMP_INSN_P (PREV_INSN (insn))
3570 && GET_CODE (PATTERN (PREV_INSN (insn))) == SET
3571 && SET_SRC (PATTERN (PREV_INSN (insn))) == incr_reg)
3572 pbi->reg_next_use[regno] = PREV_INSN (insn);
3573 else
3574 pbi->reg_next_use[regno] = 0;
3576 incr_reg = q;
3577 regno = REGNO (q);
3579 if ((pbi->flags & PROP_REG_INFO)
3580 && !REGNO_REG_SET_P (pbi->reg_live, regno))
3581 reg_deaths[regno] = pbi->insn_num;
3583 /* REGNO is now used in INCR which is below INSN, but
3584 it previously wasn't live here. If we don't mark
3585 it as live, we'll put a REG_DEAD note for it
3586 on this insn, which is incorrect. */
3587 SET_REGNO_REG_SET (pbi->reg_live, regno);
3589 /* If there are any calls between INSN and INCR, show
3590 that REGNO now crosses them. */
3591 for (temp = insn; temp != incr; temp = NEXT_INSN (temp))
3592 if (CALL_P (temp))
3594 REG_N_CALLS_CROSSED (regno)++;
3595 if (can_throw_internal (temp))
3596 REG_N_THROWING_CALLS_CROSSED (regno)++;
3599 /* Invalidate alias info for Q since we just changed its value. */
3600 clear_reg_alias_info (q);
3602 else
3603 return;
3605 /* If we haven't returned, it means we were able to make the
3606 auto-inc, so update the status. First, record that this insn
3607 has an implicit side effect. */
3609 REG_NOTES (insn) = alloc_EXPR_LIST (REG_INC, incr_reg, REG_NOTES (insn));
3611 /* Modify the old increment-insn to simply copy
3612 the already-incremented value of our register. */
3613 changed = validate_change (incr, &SET_SRC (set), incr_reg, 0);
3614 gcc_assert (changed);
3616 /* If that makes it a no-op (copying the register into itself) delete
3617 it so it won't appear to be a "use" and a "set" of this
3618 register. */
3619 if (REGNO (SET_DEST (set)) == REGNO (incr_reg))
3621 /* If the original source was dead, it's dead now. */
3622 rtx note;
3624 while ((note = find_reg_note (incr, REG_DEAD, NULL_RTX)) != NULL_RTX)
3626 remove_note (incr, note);
3627 if (XEXP (note, 0) != incr_reg)
3629 unsigned int regno = REGNO (XEXP (note, 0));
3631 if ((pbi->flags & PROP_REG_INFO)
3632 && REGNO_REG_SET_P (pbi->reg_live, regno))
3634 REG_LIVE_LENGTH (regno) += pbi->insn_num - reg_deaths[regno];
3635 reg_deaths[regno] = 0;
3637 CLEAR_REGNO_REG_SET (pbi->reg_live, REGNO (XEXP (note, 0)));
3641 SET_INSN_DELETED (incr);
3644 if (regno >= FIRST_PSEUDO_REGISTER)
3646 /* Count an extra reference to the reg. When a reg is
3647 incremented, spilling it is worse, so we want to make
3648 that less likely. */
3649 REG_FREQ (regno) += REG_FREQ_FROM_BB (pbi->bb);
3651 /* Count the increment as a setting of the register,
3652 even though it isn't a SET in rtl. */
3653 REG_N_SETS (regno)++;
3657 /* X is a MEM found in INSN. See if we can convert it into an auto-increment
3658 reference. */
3660 static void
3661 find_auto_inc (struct propagate_block_info *pbi, rtx x, rtx insn)
3663 rtx addr = XEXP (x, 0);
3664 HOST_WIDE_INT offset = 0;
3665 rtx set, y, incr, inc_val;
3666 int regno;
3667 int size = GET_MODE_SIZE (GET_MODE (x));
3669 if (JUMP_P (insn))
3670 return;
3672 /* Here we detect use of an index register which might be good for
3673 postincrement, postdecrement, preincrement, or predecrement. */
3675 if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
3676 offset = INTVAL (XEXP (addr, 1)), addr = XEXP (addr, 0);
3678 if (!REG_P (addr))
3679 return;
3681 regno = REGNO (addr);
3683 /* Is the next use an increment that might make auto-increment? */
3684 incr = pbi->reg_next_use[regno];
3685 if (incr == 0 || BLOCK_NUM (incr) != BLOCK_NUM (insn))
3686 return;
3687 set = single_set (incr);
3688 if (set == 0 || GET_CODE (set) != SET)
3689 return;
3690 y = SET_SRC (set);
3692 if (GET_CODE (y) != PLUS)
3693 return;
3695 if (REG_P (XEXP (y, 0)) && REGNO (XEXP (y, 0)) == REGNO (addr))
3696 inc_val = XEXP (y, 1);
3697 else if (REG_P (XEXP (y, 1)) && REGNO (XEXP (y, 1)) == REGNO (addr))
3698 inc_val = XEXP (y, 0);
3699 else
3700 return;
3702 if (GET_CODE (inc_val) == CONST_INT)
3704 if (HAVE_POST_INCREMENT
3705 && (INTVAL (inc_val) == size && offset == 0))
3706 attempt_auto_inc (pbi, gen_rtx_POST_INC (Pmode, addr), insn, x,
3707 incr, addr);
3708 else if (HAVE_POST_DECREMENT
3709 && (INTVAL (inc_val) == -size && offset == 0))
3710 attempt_auto_inc (pbi, gen_rtx_POST_DEC (Pmode, addr), insn, x,
3711 incr, addr);
3712 else if (HAVE_PRE_INCREMENT
3713 && (INTVAL (inc_val) == size && offset == size))
3714 attempt_auto_inc (pbi, gen_rtx_PRE_INC (Pmode, addr), insn, x,
3715 incr, addr);
3716 else if (HAVE_PRE_DECREMENT
3717 && (INTVAL (inc_val) == -size && offset == -size))
3718 attempt_auto_inc (pbi, gen_rtx_PRE_DEC (Pmode, addr), insn, x,
3719 incr, addr);
3720 else if (HAVE_POST_MODIFY_DISP && offset == 0)
3721 attempt_auto_inc (pbi, gen_rtx_POST_MODIFY (Pmode, addr,
3722 gen_rtx_PLUS (Pmode,
3723 addr,
3724 inc_val)),
3725 insn, x, incr, addr);
3726 else if (HAVE_PRE_MODIFY_DISP && offset == INTVAL (inc_val))
3727 attempt_auto_inc (pbi, gen_rtx_PRE_MODIFY (Pmode, addr,
3728 gen_rtx_PLUS (Pmode,
3729 addr,
3730 inc_val)),
3731 insn, x, incr, addr);
3733 else if (REG_P (inc_val)
3734 && ! reg_set_between_p (inc_val, PREV_INSN (insn),
3735 NEXT_INSN (incr)))
3738 if (HAVE_POST_MODIFY_REG && offset == 0)
3739 attempt_auto_inc (pbi, gen_rtx_POST_MODIFY (Pmode, addr,
3740 gen_rtx_PLUS (Pmode,
3741 addr,
3742 inc_val)),
3743 insn, x, incr, addr);
3747 #endif /* AUTO_INC_DEC */
3749 static void
3750 mark_used_reg (struct propagate_block_info *pbi, rtx reg,
3751 rtx cond ATTRIBUTE_UNUSED, rtx insn)
3753 unsigned int regno_first, regno_last, i;
3754 int some_was_live, some_was_dead, some_not_set;
3756 regno_last = regno_first = REGNO (reg);
3757 if (regno_first < FIRST_PSEUDO_REGISTER)
3758 regno_last += hard_regno_nregs[regno_first][GET_MODE (reg)] - 1;
3760 /* Find out if any of this register is live after this instruction. */
3761 some_was_live = some_was_dead = 0;
3762 for (i = regno_first; i <= regno_last; ++i)
3764 int needed_regno = REGNO_REG_SET_P (pbi->reg_live, i);
3765 some_was_live |= needed_regno;
3766 some_was_dead |= ! needed_regno;
3769 /* Find out if any of the register was set this insn. */
3770 some_not_set = 0;
3771 for (i = regno_first; i <= regno_last; ++i)
3772 some_not_set |= ! REGNO_REG_SET_P (pbi->new_set, i);
3774 if (pbi->flags & (PROP_LOG_LINKS | PROP_AUTOINC))
3776 /* Record where each reg is used, so when the reg is set we know
3777 the next insn that uses it. */
3778 pbi->reg_next_use[regno_first] = insn;
3781 if (pbi->flags & PROP_REG_INFO)
3783 if (regno_first < FIRST_PSEUDO_REGISTER)
3785 /* If this is a register we are going to try to eliminate,
3786 don't mark it live here. If we are successful in
3787 eliminating it, it need not be live unless it is used for
3788 pseudos, in which case it will have been set live when it
3789 was allocated to the pseudos. If the register will not
3790 be eliminated, reload will set it live at that point.
3792 Otherwise, record that this function uses this register. */
3793 /* ??? The PPC backend tries to "eliminate" on the pic
3794 register to itself. This should be fixed. In the mean
3795 time, hack around it. */
3797 if (! (TEST_HARD_REG_BIT (elim_reg_set, regno_first)
3798 && (regno_first == FRAME_POINTER_REGNUM
3799 || regno_first == ARG_POINTER_REGNUM)))
3800 for (i = regno_first; i <= regno_last; ++i)
3801 regs_ever_live[i] = 1;
3803 else
3805 /* Keep track of which basic block each reg appears in. */
3807 int blocknum = pbi->bb->index;
3808 if (REG_BASIC_BLOCK (regno_first) == REG_BLOCK_UNKNOWN)
3809 REG_BASIC_BLOCK (regno_first) = blocknum;
3810 else if (REG_BASIC_BLOCK (regno_first) != blocknum)
3811 REG_BASIC_BLOCK (regno_first) = REG_BLOCK_GLOBAL;
3813 /* Count (weighted) number of uses of each reg. */
3814 REG_FREQ (regno_first) += REG_FREQ_FROM_BB (pbi->bb);
3815 REG_N_REFS (regno_first)++;
3817 for (i = regno_first; i <= regno_last; ++i)
3818 if (! REGNO_REG_SET_P (pbi->reg_live, i))
3820 gcc_assert (!reg_deaths[i]);
3821 reg_deaths[i] = pbi->insn_num;
3825 /* Record and count the insns in which a reg dies. If it is used in
3826 this insn and was dead below the insn then it dies in this insn.
3827 If it was set in this insn, we do not make a REG_DEAD note;
3828 likewise if we already made such a note. */
3829 if ((pbi->flags & (PROP_DEATH_NOTES | PROP_REG_INFO))
3830 && some_was_dead
3831 && some_not_set)
3833 /* Check for the case where the register dying partially
3834 overlaps the register set by this insn. */
3835 if (regno_first != regno_last)
3836 for (i = regno_first; i <= regno_last; ++i)
3837 some_was_live |= REGNO_REG_SET_P (pbi->new_set, i);
3839 /* If none of the words in X is needed, make a REG_DEAD note.
3840 Otherwise, we must make partial REG_DEAD notes. */
3841 if (! some_was_live)
3843 if ((pbi->flags & PROP_DEATH_NOTES)
3844 #ifdef STACK_REGS
3845 && (!(pbi->flags & PROP_POST_REGSTACK)
3846 || !IN_RANGE (REGNO (reg), FIRST_STACK_REG, LAST_STACK_REG))
3847 #endif
3848 && ! find_regno_note (insn, REG_DEAD, regno_first))
3849 REG_NOTES (insn)
3850 = alloc_EXPR_LIST (REG_DEAD, reg, REG_NOTES (insn));
3852 if (pbi->flags & PROP_REG_INFO)
3853 REG_N_DEATHS (regno_first)++;
3855 else
3857 /* Don't make a REG_DEAD note for a part of a register
3858 that is set in the insn. */
3859 for (i = regno_first; i <= regno_last; ++i)
3860 if (! REGNO_REG_SET_P (pbi->reg_live, i)
3861 && ! dead_or_set_regno_p (insn, i))
3862 REG_NOTES (insn)
3863 = alloc_EXPR_LIST (REG_DEAD,
3864 regno_reg_rtx[i],
3865 REG_NOTES (insn));
3869 /* Mark the register as being live. */
3870 for (i = regno_first; i <= regno_last; ++i)
3872 #ifdef HAVE_conditional_execution
3873 int this_was_live = REGNO_REG_SET_P (pbi->reg_live, i);
3874 #endif
3876 SET_REGNO_REG_SET (pbi->reg_live, i);
3878 #ifdef HAVE_conditional_execution
3879 /* If this is a conditional use, record that fact. If it is later
3880 conditionally set, we'll know to kill the register. */
3881 if (cond != NULL_RTX)
3883 splay_tree_node node;
3884 struct reg_cond_life_info *rcli;
3885 rtx ncond;
3887 if (this_was_live)
3889 node = splay_tree_lookup (pbi->reg_cond_dead, i);
3890 if (node == NULL)
3892 /* The register was unconditionally live previously.
3893 No need to do anything. */
3895 else
3897 /* The register was conditionally live previously.
3898 Subtract the new life cond from the old death cond. */
3899 rcli = (struct reg_cond_life_info *) node->value;
3900 ncond = rcli->condition;
3901 ncond = and_reg_cond (ncond, not_reg_cond (cond), 1);
3903 /* If the register is now unconditionally live,
3904 remove the entry in the splay_tree. */
3905 if (ncond == const0_rtx)
3906 splay_tree_remove (pbi->reg_cond_dead, i);
3907 else
3909 rcli->condition = ncond;
3910 SET_REGNO_REG_SET (pbi->reg_cond_reg,
3911 REGNO (XEXP (cond, 0)));
3915 else
3917 /* The register was not previously live at all. Record
3918 the condition under which it is still dead. */
3919 rcli = XNEW (struct reg_cond_life_info);
3920 rcli->condition = not_reg_cond (cond);
3921 rcli->stores = const0_rtx;
3922 rcli->orig_condition = const0_rtx;
3923 splay_tree_insert (pbi->reg_cond_dead, i,
3924 (splay_tree_value) rcli);
3926 SET_REGNO_REG_SET (pbi->reg_cond_reg, REGNO (XEXP (cond, 0)));
3929 else if (this_was_live)
3931 /* The register may have been conditionally live previously, but
3932 is now unconditionally live. Remove it from the conditionally
3933 dead list, so that a conditional set won't cause us to think
3934 it dead. */
3935 splay_tree_remove (pbi->reg_cond_dead, i);
3937 #endif
3941 /* Scan expression X for registers which have to be marked used in PBI.
3942 X is considered to be the SET_DEST rtx of SET. TRUE is returned if
3943 X could be handled by this function. */
3945 static bool
3946 mark_used_dest_regs (struct propagate_block_info *pbi, rtx x, rtx cond, rtx insn)
3948 int regno;
3949 bool mark_dest = false;
3950 rtx dest = x;
3952 /* On some platforms calls return values spread over several
3953 locations. These locations are wrapped in a EXPR_LIST rtx
3954 together with a CONST_INT offset. */
3955 if (GET_CODE (x) == EXPR_LIST
3956 && GET_CODE (XEXP (x, 1)) == CONST_INT)
3957 x = XEXP (x, 0);
3959 if (x == NULL_RTX)
3960 return false;
3962 /* If storing into MEM, don't show it as being used. But do
3963 show the address as being used. */
3964 if (MEM_P (x))
3966 #ifdef AUTO_INC_DEC
3967 if (pbi->flags & PROP_AUTOINC)
3968 find_auto_inc (pbi, x, insn);
3969 #endif
3970 mark_used_regs (pbi, XEXP (x, 0), cond, insn);
3971 return true;
3974 /* Storing in STRICT_LOW_PART is like storing in a reg
3975 in that this SET might be dead, so ignore it in TESTREG.
3976 but in some other ways it is like using the reg.
3978 Storing in a SUBREG or a bit field is like storing the entire
3979 register in that if the register's value is not used
3980 then this SET is not needed. */
3981 while (GET_CODE (x) == STRICT_LOW_PART
3982 || GET_CODE (x) == ZERO_EXTRACT
3983 || GET_CODE (x) == SUBREG)
3985 #ifdef CANNOT_CHANGE_MODE_CLASS
3986 if ((pbi->flags & PROP_REG_INFO) && GET_CODE (x) == SUBREG)
3987 record_subregs_of_mode (x);
3988 #endif
3990 /* Modifying a single register in an alternate mode
3991 does not use any of the old value. But these other
3992 ways of storing in a register do use the old value. */
3993 if (GET_CODE (x) == SUBREG
3994 && !((REG_BYTES (SUBREG_REG (x))
3995 + UNITS_PER_WORD - 1) / UNITS_PER_WORD
3996 > (REG_BYTES (x)
3997 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))
3999 else
4000 mark_dest = true;
4002 x = XEXP (x, 0);
4005 /* If this is a store into a register or group of registers,
4006 recursively scan the value being stored. */
4007 if (REG_P (x)
4008 && (regno = REGNO (x),
4009 !(regno == FRAME_POINTER_REGNUM
4010 && (!reload_completed || frame_pointer_needed)))
4011 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
4012 && !(regno == HARD_FRAME_POINTER_REGNUM
4013 && (!reload_completed || frame_pointer_needed))
4014 #endif
4015 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
4016 && !(regno == ARG_POINTER_REGNUM && fixed_regs[regno])
4017 #endif
4020 if (mark_dest)
4021 mark_used_regs (pbi, dest, cond, insn);
4022 return true;
4024 return false;
4027 /* Scan expression X and store a 1-bit in NEW_LIVE for each reg it uses.
4028 This is done assuming the registers needed from X are those that
4029 have 1-bits in PBI->REG_LIVE.
4031 INSN is the containing instruction. If INSN is dead, this function
4032 is not called. */
4034 static void
4035 mark_used_regs (struct propagate_block_info *pbi, rtx x, rtx cond, rtx insn)
4037 RTX_CODE code;
4038 int flags = pbi->flags;
4040 retry:
4041 if (!x)
4042 return;
4043 code = GET_CODE (x);
4044 switch (code)
4046 case LABEL_REF:
4047 case SYMBOL_REF:
4048 case CONST_INT:
4049 case CONST:
4050 case CONST_DOUBLE:
4051 case CONST_VECTOR:
4052 case PC:
4053 case ADDR_VEC:
4054 case ADDR_DIFF_VEC:
4055 return;
4057 #ifdef HAVE_cc0
4058 case CC0:
4059 pbi->cc0_live = 1;
4060 return;
4061 #endif
4063 case CLOBBER:
4064 /* If we are clobbering a MEM, mark any registers inside the address
4065 as being used. */
4066 if (MEM_P (XEXP (x, 0)))
4067 mark_used_regs (pbi, XEXP (XEXP (x, 0), 0), cond, insn);
4068 return;
4070 case MEM:
4071 /* Don't bother watching stores to mems if this is not the
4072 final pass. We'll not be deleting dead stores this round. */
4073 if (optimize && (flags & PROP_SCAN_DEAD_STORES))
4075 /* Invalidate the data for the last MEM stored, but only if MEM is
4076 something that can be stored into. */
4077 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
4078 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
4079 /* Needn't clear the memory set list. */
4081 else
4083 rtx temp = pbi->mem_set_list;
4084 rtx prev = NULL_RTX;
4085 rtx next;
4087 while (temp)
4089 next = XEXP (temp, 1);
4090 if (anti_dependence (XEXP (temp, 0), x))
4092 /* Splice temp out of the list. */
4093 if (prev)
4094 XEXP (prev, 1) = next;
4095 else
4096 pbi->mem_set_list = next;
4097 free_EXPR_LIST_node (temp);
4098 pbi->mem_set_list_len--;
4100 else
4101 prev = temp;
4102 temp = next;
4106 /* If the memory reference had embedded side effects (autoincrement
4107 address modes. Then we may need to kill some entries on the
4108 memory set list. */
4109 if (insn)
4110 for_each_rtx (&PATTERN (insn), invalidate_mems_from_autoinc, pbi);
4113 #ifdef AUTO_INC_DEC
4114 if (flags & PROP_AUTOINC)
4115 find_auto_inc (pbi, x, insn);
4116 #endif
4117 break;
4119 case SUBREG:
4120 #ifdef CANNOT_CHANGE_MODE_CLASS
4121 if (flags & PROP_REG_INFO)
4122 record_subregs_of_mode (x);
4123 #endif
4125 /* While we're here, optimize this case. */
4126 x = SUBREG_REG (x);
4127 if (!REG_P (x))
4128 goto retry;
4129 /* Fall through. */
4131 case REG:
4132 /* See a register other than being set => mark it as needed. */
4133 mark_used_reg (pbi, x, cond, insn);
4134 return;
4136 case SET:
4138 rtx dest = SET_DEST (x);
4139 int i;
4140 bool ret = false;
4142 if (GET_CODE (dest) == PARALLEL)
4143 for (i = 0; i < XVECLEN (dest, 0); i++)
4144 ret |= mark_used_dest_regs (pbi, XVECEXP (dest, 0, i), cond, insn);
4145 else
4146 ret = mark_used_dest_regs (pbi, dest, cond, insn);
4148 if (ret)
4150 mark_used_regs (pbi, SET_SRC (x), cond, insn);
4151 return;
4154 break;
4156 case ASM_OPERANDS:
4157 case UNSPEC_VOLATILE:
4158 case TRAP_IF:
4159 case ASM_INPUT:
4161 /* Traditional and volatile asm instructions must be considered to use
4162 and clobber all hard registers, all pseudo-registers and all of
4163 memory. So must TRAP_IF and UNSPEC_VOLATILE operations.
4165 Consider for instance a volatile asm that changes the fpu rounding
4166 mode. An insn should not be moved across this even if it only uses
4167 pseudo-regs because it might give an incorrectly rounded result.
4169 ?!? Unfortunately, marking all hard registers as live causes massive
4170 problems for the register allocator and marking all pseudos as live
4171 creates mountains of uninitialized variable warnings.
4173 So for now, just clear the memory set list and mark any regs
4174 we can find in ASM_OPERANDS as used. */
4175 if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
4177 free_EXPR_LIST_list (&pbi->mem_set_list);
4178 pbi->mem_set_list_len = 0;
4181 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
4182 We can not just fall through here since then we would be confused
4183 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
4184 traditional asms unlike their normal usage. */
4185 if (code == ASM_OPERANDS)
4187 int j;
4189 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
4190 mark_used_regs (pbi, ASM_OPERANDS_INPUT (x, j), cond, insn);
4192 break;
4195 case COND_EXEC:
4196 gcc_assert (!cond);
4198 mark_used_regs (pbi, COND_EXEC_TEST (x), NULL_RTX, insn);
4200 cond = COND_EXEC_TEST (x);
4201 x = COND_EXEC_CODE (x);
4202 goto retry;
4204 default:
4205 break;
4208 /* Recursively scan the operands of this expression. */
4211 const char * const fmt = GET_RTX_FORMAT (code);
4212 int i;
4214 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4216 if (fmt[i] == 'e')
4218 /* Tail recursive case: save a function call level. */
4219 if (i == 0)
4221 x = XEXP (x, 0);
4222 goto retry;
4224 mark_used_regs (pbi, XEXP (x, i), cond, insn);
4226 else if (fmt[i] == 'E')
4228 int j;
4229 for (j = 0; j < XVECLEN (x, i); j++)
4230 mark_used_regs (pbi, XVECEXP (x, i, j), cond, insn);
4236 #ifdef AUTO_INC_DEC
4238 static int
4239 try_pre_increment_1 (struct propagate_block_info *pbi, rtx insn)
4241 /* Find the next use of this reg. If in same basic block,
4242 make it do pre-increment or pre-decrement if appropriate. */
4243 rtx x = single_set (insn);
4244 HOST_WIDE_INT amount = ((GET_CODE (SET_SRC (x)) == PLUS ? 1 : -1)
4245 * INTVAL (XEXP (SET_SRC (x), 1)));
4246 int regno = REGNO (SET_DEST (x));
4247 rtx y = pbi->reg_next_use[regno];
4248 if (y != 0
4249 && SET_DEST (x) != stack_pointer_rtx
4250 && BLOCK_NUM (y) == BLOCK_NUM (insn)
4251 /* Don't do this if the reg dies, or gets set in y; a standard addressing
4252 mode would be better. */
4253 && ! dead_or_set_p (y, SET_DEST (x))
4254 && try_pre_increment (y, SET_DEST (x), amount))
4256 /* We have found a suitable auto-increment and already changed
4257 insn Y to do it. So flush this increment instruction. */
4258 propagate_block_delete_insn (insn);
4260 /* Count a reference to this reg for the increment insn we are
4261 deleting. When a reg is incremented, spilling it is worse,
4262 so we want to make that less likely. */
4263 if (regno >= FIRST_PSEUDO_REGISTER)
4265 REG_FREQ (regno) += REG_FREQ_FROM_BB (pbi->bb);
4266 REG_N_SETS (regno)++;
4269 /* Flush any remembered memories depending on the value of
4270 the incremented register. */
4271 invalidate_mems_from_set (pbi, SET_DEST (x));
4273 return 1;
4275 return 0;
4278 /* Try to change INSN so that it does pre-increment or pre-decrement
4279 addressing on register REG in order to add AMOUNT to REG.
4280 AMOUNT is negative for pre-decrement.
4281 Returns 1 if the change could be made.
4282 This checks all about the validity of the result of modifying INSN. */
4284 static int
4285 try_pre_increment (rtx insn, rtx reg, HOST_WIDE_INT amount)
4287 rtx use;
4289 /* Nonzero if we can try to make a pre-increment or pre-decrement.
4290 For example, addl $4,r1; movl (r1),... can become movl +(r1),... */
4291 int pre_ok = 0;
4292 /* Nonzero if we can try to make a post-increment or post-decrement.
4293 For example, addl $4,r1; movl -4(r1),... can become movl (r1)+,...
4294 It is possible for both PRE_OK and POST_OK to be nonzero if the machine
4295 supports both pre-inc and post-inc, or both pre-dec and post-dec. */
4296 int post_ok = 0;
4298 /* Nonzero if the opportunity actually requires post-inc or post-dec. */
4299 int do_post = 0;
4301 /* From the sign of increment, see which possibilities are conceivable
4302 on this target machine. */
4303 if (HAVE_PRE_INCREMENT && amount > 0)
4304 pre_ok = 1;
4305 if (HAVE_POST_INCREMENT && amount > 0)
4306 post_ok = 1;
4308 if (HAVE_PRE_DECREMENT && amount < 0)
4309 pre_ok = 1;
4310 if (HAVE_POST_DECREMENT && amount < 0)
4311 post_ok = 1;
4313 if (! (pre_ok || post_ok))
4314 return 0;
4316 /* It is not safe to add a side effect to a jump insn
4317 because if the incremented register is spilled and must be reloaded
4318 there would be no way to store the incremented value back in memory. */
4320 if (JUMP_P (insn))
4321 return 0;
4323 use = 0;
4324 if (pre_ok)
4325 use = find_use_as_address (PATTERN (insn), reg, 0);
4326 if (post_ok && (use == 0 || use == (rtx) (size_t) 1))
4328 use = find_use_as_address (PATTERN (insn), reg, -amount);
4329 do_post = 1;
4332 if (use == 0 || use == (rtx) (size_t) 1)
4333 return 0;
4335 if (GET_MODE_SIZE (GET_MODE (use)) != (amount > 0 ? amount : - amount))
4336 return 0;
4338 /* See if this combination of instruction and addressing mode exists. */
4339 if (! validate_change (insn, &XEXP (use, 0),
4340 gen_rtx_fmt_e (amount > 0
4341 ? (do_post ? POST_INC : PRE_INC)
4342 : (do_post ? POST_DEC : PRE_DEC),
4343 Pmode, reg), 0))
4344 return 0;
4346 /* Record that this insn now has an implicit side effect on X. */
4347 REG_NOTES (insn) = alloc_EXPR_LIST (REG_INC, reg, REG_NOTES (insn));
4348 return 1;
4351 #endif /* AUTO_INC_DEC */
4353 /* Find the place in the rtx X where REG is used as a memory address.
4354 Return the MEM rtx that so uses it.
4355 If PLUSCONST is nonzero, search instead for a memory address equivalent to
4356 (plus REG (const_int PLUSCONST)).
4358 If such an address does not appear, return 0.
4359 If REG appears more than once, or is used other than in such an address,
4360 return (rtx) 1. */
4363 find_use_as_address (rtx x, rtx reg, HOST_WIDE_INT plusconst)
4365 enum rtx_code code = GET_CODE (x);
4366 const char * const fmt = GET_RTX_FORMAT (code);
4367 int i;
4368 rtx value = 0;
4369 rtx tem;
4371 if (code == MEM && XEXP (x, 0) == reg && plusconst == 0)
4372 return x;
4374 if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
4375 && XEXP (XEXP (x, 0), 0) == reg
4376 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4377 && INTVAL (XEXP (XEXP (x, 0), 1)) == plusconst)
4378 return x;
4380 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
4382 /* If REG occurs inside a MEM used in a bit-field reference,
4383 that is unacceptable. */
4384 if (find_use_as_address (XEXP (x, 0), reg, 0) != 0)
4385 return (rtx) (size_t) 1;
4388 if (x == reg)
4389 return (rtx) (size_t) 1;
4391 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4393 if (fmt[i] == 'e')
4395 tem = find_use_as_address (XEXP (x, i), reg, plusconst);
4396 if (value == 0)
4397 value = tem;
4398 else if (tem != 0)
4399 return (rtx) (size_t) 1;
4401 else if (fmt[i] == 'E')
4403 int j;
4404 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4406 tem = find_use_as_address (XVECEXP (x, i, j), reg, plusconst);
4407 if (value == 0)
4408 value = tem;
4409 else if (tem != 0)
4410 return (rtx) (size_t) 1;
4415 return value;
4418 /* Write information about registers and basic blocks into FILE.
4419 This is part of making a debugging dump. */
4421 void
4422 dump_regset (regset r, FILE *outf)
4424 unsigned i;
4425 reg_set_iterator rsi;
4427 if (r == NULL)
4429 fputs (" (nil)", outf);
4430 return;
4433 EXECUTE_IF_SET_IN_REG_SET (r, 0, i, rsi)
4435 fprintf (outf, " %d", i);
4436 if (i < FIRST_PSEUDO_REGISTER)
4437 fprintf (outf, " [%s]",
4438 reg_names[i]);
4442 /* Print a human-readable representation of R on the standard error
4443 stream. This function is designed to be used from within the
4444 debugger. */
4446 void
4447 debug_regset (regset r)
4449 dump_regset (r, stderr);
4450 putc ('\n', stderr);
4453 /* Recompute register set/reference counts immediately prior to register
4454 allocation.
4456 This avoids problems with set/reference counts changing to/from values
4457 which have special meanings to the register allocators.
4459 Additionally, the reference counts are the primary component used by the
4460 register allocators to prioritize pseudos for allocation to hard regs.
4461 More accurate reference counts generally lead to better register allocation.
4463 It might be worthwhile to update REG_LIVE_LENGTH, REG_BASIC_BLOCK and
4464 possibly other information which is used by the register allocators. */
4466 static unsigned int
4467 recompute_reg_usage (void)
4469 allocate_reg_life_data ();
4470 /* distribute_notes in combiner fails to convert some of the
4471 REG_UNUSED notes to REG_DEAD notes. This causes CHECK_DEAD_NOTES
4472 in sched1 to die. To solve this update the DEATH_NOTES
4473 here. */
4474 update_life_info (NULL, UPDATE_LIFE_LOCAL, PROP_REG_INFO | PROP_DEATH_NOTES);
4476 if (dump_file)
4477 dump_flow_info (dump_file, dump_flags);
4478 return 0;
4481 struct tree_opt_pass pass_recompute_reg_usage =
4483 "life2", /* name */
4484 NULL, /* gate */
4485 recompute_reg_usage, /* execute */
4486 NULL, /* sub */
4487 NULL, /* next */
4488 0, /* static_pass_number */
4489 0, /* tv_id */
4490 0, /* properties_required */
4491 0, /* properties_provided */
4492 0, /* properties_destroyed */
4493 0, /* todo_flags_start */
4494 TODO_dump_func, /* todo_flags_finish */
4495 'f' /* letter */
4498 /* Optionally removes all the REG_DEAD and REG_UNUSED notes from a set of
4499 blocks. If BLOCKS is NULL, assume the universal set. Returns a count
4500 of the number of registers that died.
4501 If KILL is 1, remove old REG_DEAD / REG_UNUSED notes. If it is 0, don't.
4502 if it is -1, remove them unless they pertain to a stack reg. */
4505 count_or_remove_death_notes (sbitmap blocks, int kill)
4507 int count = 0;
4508 unsigned int i = 0;
4509 basic_block bb;
4511 /* This used to be a loop over all the blocks with a membership test
4512 inside the loop. That can be amazingly expensive on a large CFG
4513 when only a small number of bits are set in BLOCKs (for example,
4514 the calls from the scheduler typically have very few bits set).
4516 For extra credit, someone should convert BLOCKS to a bitmap rather
4517 than an sbitmap. */
4518 if (blocks)
4520 sbitmap_iterator sbi;
4522 EXECUTE_IF_SET_IN_SBITMAP (blocks, 0, i, sbi)
4524 basic_block bb = BASIC_BLOCK (i);
4525 /* The bitmap may be flawed in that one of the basic blocks
4526 may have been deleted before you get here. */
4527 if (bb)
4528 count += count_or_remove_death_notes_bb (bb, kill);
4531 else
4533 FOR_EACH_BB (bb)
4535 count += count_or_remove_death_notes_bb (bb, kill);
4539 return count;
4542 /* Optionally removes all the REG_DEAD and REG_UNUSED notes from basic
4543 block BB. Returns a count of the number of registers that died. */
4545 static int
4546 count_or_remove_death_notes_bb (basic_block bb, int kill)
4548 int count = 0;
4549 rtx insn;
4551 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
4553 if (INSN_P (insn))
4555 rtx *pprev = &REG_NOTES (insn);
4556 rtx link = *pprev;
4558 while (link)
4560 switch (REG_NOTE_KIND (link))
4562 case REG_DEAD:
4563 if (REG_P (XEXP (link, 0)))
4565 rtx reg = XEXP (link, 0);
4566 int n;
4568 if (REGNO (reg) >= FIRST_PSEUDO_REGISTER)
4569 n = 1;
4570 else
4571 n = hard_regno_nregs[REGNO (reg)][GET_MODE (reg)];
4572 count += n;
4575 /* Fall through. */
4577 case REG_UNUSED:
4578 if (kill > 0
4579 || (kill
4580 #ifdef STACK_REGS
4581 && (!REG_P (XEXP (link, 0))
4582 || !IN_RANGE (REGNO (XEXP (link, 0)),
4583 FIRST_STACK_REG, LAST_STACK_REG))
4584 #endif
4587 rtx next = XEXP (link, 1);
4588 free_EXPR_LIST_node (link);
4589 *pprev = link = next;
4590 break;
4592 /* Fall through. */
4594 default:
4595 pprev = &XEXP (link, 1);
4596 link = *pprev;
4597 break;
4602 if (insn == BB_END (bb))
4603 break;
4606 return count;
4609 /* Clear LOG_LINKS fields of insns in a selected blocks or whole chain
4610 if blocks is NULL. */
4612 static void
4613 clear_log_links (sbitmap blocks)
4615 rtx insn;
4617 if (!blocks)
4619 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4620 if (INSN_P (insn))
4621 free_INSN_LIST_list (&LOG_LINKS (insn));
4623 else
4625 unsigned int i = 0;
4626 sbitmap_iterator sbi;
4628 EXECUTE_IF_SET_IN_SBITMAP (blocks, 0, i, sbi)
4630 basic_block bb = BASIC_BLOCK (i);
4632 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb));
4633 insn = NEXT_INSN (insn))
4634 if (INSN_P (insn))
4635 free_INSN_LIST_list (&LOG_LINKS (insn));
4640 /* Given a register bitmap, turn on the bits in a HARD_REG_SET that
4641 correspond to the hard registers, if any, set in that map. This
4642 could be done far more efficiently by having all sorts of special-cases
4643 with moving single words, but probably isn't worth the trouble. */
4645 void
4646 reg_set_to_hard_reg_set (HARD_REG_SET *to, bitmap from)
4648 unsigned i;
4649 bitmap_iterator bi;
4651 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
4653 if (i >= FIRST_PSEUDO_REGISTER)
4654 return;
4655 SET_HARD_REG_BIT (*to, i);
4660 static bool
4661 gate_remove_death_notes (void)
4663 return flag_profile_values;
4666 static unsigned int
4667 rest_of_handle_remove_death_notes (void)
4669 count_or_remove_death_notes (NULL, 1);
4670 return 0;
4673 struct tree_opt_pass pass_remove_death_notes =
4675 "ednotes", /* name */
4676 gate_remove_death_notes, /* gate */
4677 rest_of_handle_remove_death_notes, /* execute */
4678 NULL, /* sub */
4679 NULL, /* next */
4680 0, /* static_pass_number */
4681 0, /* tv_id */
4682 0, /* properties_required */
4683 0, /* properties_provided */
4684 0, /* properties_destroyed */
4685 0, /* todo_flags_start */
4686 0, /* todo_flags_finish */
4687 0 /* letter */
4690 /* Perform life analysis. */
4691 static unsigned int
4692 rest_of_handle_life (void)
4694 regclass_init ();
4696 life_analysis (PROP_FINAL);
4697 if (optimize)
4698 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_UPDATE_LIFE | CLEANUP_LOG_LINKS
4699 | (flag_thread_jumps ? CLEANUP_THREADING : 0));
4701 if (extra_warnings)
4703 setjmp_vars_warning (DECL_INITIAL (current_function_decl));
4704 setjmp_args_warning ();
4707 if (optimize)
4709 if (initialize_uninitialized_subregs ())
4711 /* Insns were inserted, and possibly pseudos created, so
4712 things might look a bit different. */
4713 allocate_reg_life_data ();
4714 update_life_info (NULL, UPDATE_LIFE_GLOBAL_RM_NOTES,
4715 PROP_LOG_LINKS | PROP_REG_INFO | PROP_DEATH_NOTES);
4719 no_new_pseudos = 1;
4720 return 0;
4723 struct tree_opt_pass pass_life =
4725 "life1", /* name */
4726 NULL, /* gate */
4727 rest_of_handle_life, /* execute */
4728 NULL, /* sub */
4729 NULL, /* next */
4730 0, /* static_pass_number */
4731 TV_FLOW, /* tv_id */
4732 0, /* properties_required */
4733 0, /* properties_provided */
4734 0, /* properties_destroyed */
4735 TODO_verify_flow, /* todo_flags_start */
4736 TODO_dump_func |
4737 TODO_ggc_collect, /* todo_flags_finish */
4738 'f' /* letter */
4741 static unsigned int
4742 rest_of_handle_flow2 (void)
4744 /* If optimizing, then go ahead and split insns now. */
4745 #ifndef STACK_REGS
4746 if (optimize > 0)
4747 #endif
4748 split_all_insns (0);
4750 if (flag_branch_target_load_optimize)
4751 branch_target_load_optimize (epilogue_completed);
4753 if (optimize)
4754 cleanup_cfg (CLEANUP_EXPENSIVE);
4756 /* On some machines, the prologue and epilogue code, or parts thereof,
4757 can be represented as RTL. Doing so lets us schedule insns between
4758 it and the rest of the code and also allows delayed branch
4759 scheduling to operate in the epilogue. */
4760 thread_prologue_and_epilogue_insns (get_insns ());
4761 epilogue_completed = 1;
4762 flow2_completed = 1;
4763 return 0;
4766 struct tree_opt_pass pass_flow2 =
4768 "flow2", /* name */
4769 NULL, /* gate */
4770 rest_of_handle_flow2, /* execute */
4771 NULL, /* sub */
4772 NULL, /* next */
4773 0, /* static_pass_number */
4774 TV_FLOW2, /* tv_id */
4775 0, /* properties_required */
4776 0, /* properties_provided */
4777 0, /* properties_destroyed */
4778 TODO_verify_flow, /* todo_flags_start */
4779 TODO_dump_func |
4780 TODO_ggc_collect, /* todo_flags_finish */
4781 'w' /* letter */