2005-04-29 Jim Tison <jtison@us.ibm.com>
[official-gcc.git] / gcc / flow.c
blobff1d861dbe525c967adb9026a0611d29cfac6df0
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 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* This file contains the data flow analysis pass of the compiler. It
23 computes data flow information which tells combine_instructions
24 which insns to consider combining and controls register allocation.
26 Additional data flow information that is too bulky to record is
27 generated during the analysis, and is used at that time to create
28 autoincrement and autodecrement addressing.
30 The first step is dividing the function into basic blocks.
31 find_basic_blocks does this. Then life_analysis determines
32 where each register is live and where it is dead.
34 ** find_basic_blocks **
36 find_basic_blocks divides the current function's rtl into basic
37 blocks and constructs the CFG. The blocks are recorded in the
38 basic_block_info array; the CFG exists in the edge structures
39 referenced by the blocks.
41 find_basic_blocks also finds any unreachable loops and deletes them.
43 ** life_analysis **
45 life_analysis is called immediately after find_basic_blocks.
46 It uses the basic block information to determine where each
47 hard or pseudo register is live.
49 ** live-register info **
51 The information about where each register is live is in two parts:
52 the REG_NOTES of insns, and the vector basic_block->global_live_at_start.
54 basic_block->global_live_at_start has an element for each basic
55 block, and the element is a bit-vector with a bit for each hard or
56 pseudo register. The bit is 1 if the register is live at the
57 beginning of the basic block.
59 Two types of elements can be added to an insn's REG_NOTES.
60 A REG_DEAD note is added to an insn's REG_NOTES for any register
61 that meets both of two conditions: The value in the register is not
62 needed in subsequent insns and the insn does not replace the value in
63 the register (in the case of multi-word hard registers, the value in
64 each register must be replaced by the insn to avoid a REG_DEAD note).
66 In the vast majority of cases, an object in a REG_DEAD note will be
67 used somewhere in the insn. The (rare) exception to this is if an
68 insn uses a multi-word hard register and only some of the registers are
69 needed in subsequent insns. In that case, REG_DEAD notes will be
70 provided for those hard registers that are not subsequently needed.
71 Partial REG_DEAD notes of this type do not occur when an insn sets
72 only some of the hard registers used in such a multi-word operand;
73 omitting REG_DEAD notes for objects stored in an insn is optional and
74 the desire to do so does not justify the complexity of the partial
75 REG_DEAD notes.
77 REG_UNUSED notes are added for each register that is set by the insn
78 but is unused subsequently (if every register set by the insn is unused
79 and the insn does not reference memory or have some other side-effect,
80 the insn is deleted instead). If only part of a multi-word hard
81 register is used in a subsequent insn, REG_UNUSED notes are made for
82 the parts that will not be used.
84 To determine which registers are live after any insn, one can
85 start from the beginning of the basic block and scan insns, noting
86 which registers are set by each insn and which die there.
88 ** Other actions of life_analysis **
90 life_analysis sets up the LOG_LINKS fields of insns because the
91 information needed to do so is readily available.
93 life_analysis deletes insns whose only effect is to store a value
94 that is never used.
96 life_analysis notices cases where a reference to a register as
97 a memory address can be combined with a preceding or following
98 incrementation or decrementation of the register. The separate
99 instruction to increment or decrement is deleted and the address
100 is changed to a POST_INC or similar rtx.
102 Each time an incrementing or decrementing address is created,
103 a REG_INC element is added to the insn's REG_NOTES list.
105 life_analysis fills in certain vectors containing information about
106 register usage: REG_N_REFS, REG_N_DEATHS, REG_N_SETS, REG_LIVE_LENGTH,
107 REG_N_CALLS_CROSSED and REG_BASIC_BLOCK.
109 life_analysis sets current_function_sp_is_unchanging if the function
110 doesn't modify the stack pointer. */
112 /* TODO:
114 Split out from life_analysis:
115 - local property discovery
116 - global property computation
117 - log links creation
118 - pre/post modify transformation
121 #include "config.h"
122 #include "system.h"
123 #include "coretypes.h"
124 #include "tm.h"
125 #include "tree.h"
126 #include "rtl.h"
127 #include "tm_p.h"
128 #include "hard-reg-set.h"
129 #include "basic-block.h"
130 #include "insn-config.h"
131 #include "regs.h"
132 #include "flags.h"
133 #include "output.h"
134 #include "function.h"
135 #include "except.h"
136 #include "toplev.h"
137 #include "recog.h"
138 #include "expr.h"
139 #include "timevar.h"
141 #include "obstack.h"
142 #include "splay-tree.h"
144 #ifndef HAVE_epilogue
145 #define HAVE_epilogue 0
146 #endif
147 #ifndef HAVE_prologue
148 #define HAVE_prologue 0
149 #endif
150 #ifndef HAVE_sibcall_epilogue
151 #define HAVE_sibcall_epilogue 0
152 #endif
154 #ifndef EPILOGUE_USES
155 #define EPILOGUE_USES(REGNO) 0
156 #endif
157 #ifndef EH_USES
158 #define EH_USES(REGNO) 0
159 #endif
161 #ifdef HAVE_conditional_execution
162 #ifndef REVERSE_CONDEXEC_PREDICATES_P
163 #define REVERSE_CONDEXEC_PREDICATES_P(x, y) \
164 (GET_CODE ((x)) == reversed_comparison_code ((y), NULL))
165 #endif
166 #endif
168 /* This is the maximum number of times we process any given block if the
169 latest loop depth count is smaller than this number. Only used for the
170 failure strategy to avoid infinite loops in calculate_global_regs_live. */
171 #define MAX_LIVENESS_ROUNDS 20
173 /* Nonzero if the second flow pass has completed. */
174 int flow2_completed;
176 /* Maximum register number used in this function, plus one. */
178 int max_regno;
180 /* Indexed by n, giving various register information */
182 varray_type reg_n_info;
184 /* Regset of regs live when calls to `setjmp'-like functions happen. */
185 /* ??? Does this exist only for the setjmp-clobbered warning message? */
187 static regset regs_live_at_setjmp;
189 /* List made of EXPR_LIST rtx's which gives pairs of pseudo registers
190 that have to go in the same hard reg.
191 The first two regs in the list are a pair, and the next two
192 are another pair, etc. */
193 rtx regs_may_share;
195 /* Set of registers that may be eliminable. These are handled specially
196 in updating regs_ever_live. */
198 static HARD_REG_SET elim_reg_set;
200 /* Holds information for tracking conditional register life information. */
201 struct reg_cond_life_info
203 /* A boolean expression of conditions under which a register is dead. */
204 rtx condition;
205 /* Conditions under which a register is dead at the basic block end. */
206 rtx orig_condition;
208 /* A boolean expression of conditions under which a register has been
209 stored into. */
210 rtx stores;
212 /* ??? Could store mask of bytes that are dead, so that we could finally
213 track lifetimes of multi-word registers accessed via subregs. */
216 /* For use in communicating between propagate_block and its subroutines.
217 Holds all information needed to compute life and def-use information. */
219 struct propagate_block_info
221 /* The basic block we're considering. */
222 basic_block bb;
224 /* Bit N is set if register N is conditionally or unconditionally live. */
225 regset reg_live;
227 /* Bit N is set if register N is set this insn. */
228 regset new_set;
230 /* Element N is the next insn that uses (hard or pseudo) register N
231 within the current basic block; or zero, if there is no such insn. */
232 rtx *reg_next_use;
234 /* Contains a list of all the MEMs we are tracking for dead store
235 elimination. */
236 rtx mem_set_list;
238 /* If non-null, record the set of registers set unconditionally in the
239 basic block. */
240 regset local_set;
242 /* If non-null, record the set of registers set conditionally in the
243 basic block. */
244 regset cond_local_set;
246 #ifdef HAVE_conditional_execution
247 /* Indexed by register number, holds a reg_cond_life_info for each
248 register that is not unconditionally live or dead. */
249 splay_tree reg_cond_dead;
251 /* Bit N is set if register N is in an expression in reg_cond_dead. */
252 regset reg_cond_reg;
253 #endif
255 /* The length of mem_set_list. */
256 int mem_set_list_len;
258 /* Nonzero if the value of CC0 is live. */
259 int cc0_live;
261 /* Flags controlling the set of information propagate_block collects. */
262 int flags;
263 /* Index of instruction being processed. */
264 int insn_num;
267 /* Number of dead insns removed. */
268 static int ndead;
270 /* When PROP_REG_INFO set, array contains pbi->insn_num of instruction
271 where given register died. When the register is marked alive, we use the
272 information to compute amount of instructions life range cross.
273 (remember, we are walking backward). This can be computed as current
274 pbi->insn_num - reg_deaths[regno].
275 At the end of processing each basic block, the remaining live registers
276 are inspected and live ranges are increased same way so liverange of global
277 registers are computed correctly.
279 The array is maintained clear for dead registers, so it can be safely reused
280 for next basic block without expensive memset of the whole array after
281 reseting pbi->insn_num to 0. */
283 static int *reg_deaths;
285 /* Maximum length of pbi->mem_set_list before we start dropping
286 new elements on the floor. */
287 #define MAX_MEM_SET_LIST_LEN 100
289 /* Forward declarations */
290 static int verify_wide_reg_1 (rtx *, void *);
291 static void verify_wide_reg (int, basic_block);
292 static void verify_local_live_at_start (regset, basic_block);
293 static void notice_stack_pointer_modification_1 (rtx, rtx, void *);
294 static void notice_stack_pointer_modification (void);
295 static void mark_reg (rtx, void *);
296 static void mark_regs_live_at_end (regset);
297 static void calculate_global_regs_live (sbitmap, sbitmap, int);
298 static void propagate_block_delete_insn (rtx);
299 static rtx propagate_block_delete_libcall (rtx, rtx);
300 static int insn_dead_p (struct propagate_block_info *, rtx, int, rtx);
301 static int libcall_dead_p (struct propagate_block_info *, rtx, rtx);
302 static void mark_set_regs (struct propagate_block_info *, rtx, rtx);
303 static void mark_set_1 (struct propagate_block_info *, enum rtx_code, rtx,
304 rtx, rtx, int);
305 static int find_regno_partial (rtx *, void *);
307 #ifdef HAVE_conditional_execution
308 static int mark_regno_cond_dead (struct propagate_block_info *, int, rtx);
309 static void free_reg_cond_life_info (splay_tree_value);
310 static int flush_reg_cond_reg_1 (splay_tree_node, void *);
311 static void flush_reg_cond_reg (struct propagate_block_info *, int);
312 static rtx elim_reg_cond (rtx, unsigned int);
313 static rtx ior_reg_cond (rtx, rtx, int);
314 static rtx not_reg_cond (rtx);
315 static rtx and_reg_cond (rtx, rtx, int);
316 #endif
317 #ifdef AUTO_INC_DEC
318 static void attempt_auto_inc (struct propagate_block_info *, rtx, rtx, rtx,
319 rtx, rtx);
320 static void find_auto_inc (struct propagate_block_info *, rtx, rtx);
321 static int try_pre_increment_1 (struct propagate_block_info *, rtx);
322 static int try_pre_increment (rtx, rtx, HOST_WIDE_INT);
323 #endif
324 static void mark_used_reg (struct propagate_block_info *, rtx, rtx, rtx);
325 static void mark_used_regs (struct propagate_block_info *, rtx, rtx, rtx);
326 void debug_flow_info (void);
327 static void add_to_mem_set_list (struct propagate_block_info *, rtx);
328 static int invalidate_mems_from_autoinc (rtx *, void *);
329 static void invalidate_mems_from_set (struct propagate_block_info *, rtx);
330 static void clear_log_links (sbitmap);
331 static int count_or_remove_death_notes_bb (basic_block, int);
332 static void allocate_bb_life_data (void);
334 /* Return the INSN immediately following the NOTE_INSN_BASIC_BLOCK
335 note associated with the BLOCK. */
338 first_insn_after_basic_block_note (basic_block block)
340 rtx insn;
342 /* Get the first instruction in the block. */
343 insn = BB_HEAD (block);
345 if (insn == NULL_RTX)
346 return NULL_RTX;
347 if (LABEL_P (insn))
348 insn = NEXT_INSN (insn);
349 gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
351 return NEXT_INSN (insn);
354 /* Perform data flow analysis for the whole control flow graph.
355 FLAGS is a set of PROP_* flags to be used in accumulating flow info. */
357 void
358 life_analysis (FILE *file, int flags)
360 #ifdef ELIMINABLE_REGS
361 int i;
362 static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
363 #endif
365 /* Record which registers will be eliminated. We use this in
366 mark_used_regs. */
368 CLEAR_HARD_REG_SET (elim_reg_set);
370 #ifdef ELIMINABLE_REGS
371 for (i = 0; i < (int) ARRAY_SIZE (eliminables); i++)
372 SET_HARD_REG_BIT (elim_reg_set, eliminables[i].from);
373 #else
374 SET_HARD_REG_BIT (elim_reg_set, FRAME_POINTER_REGNUM);
375 #endif
378 #ifdef CANNOT_CHANGE_MODE_CLASS
379 if (flags & PROP_REG_INFO)
380 init_subregs_of_mode ();
381 #endif
383 if (! optimize)
384 flags &= ~(PROP_LOG_LINKS | PROP_AUTOINC | PROP_ALLOW_CFG_CHANGES);
386 /* The post-reload life analysis have (on a global basis) the same
387 registers live as was computed by reload itself. elimination
388 Otherwise offsets and such may be incorrect.
390 Reload will make some registers as live even though they do not
391 appear in the rtl.
393 We don't want to create new auto-incs after reload, since they
394 are unlikely to be useful and can cause problems with shared
395 stack slots. */
396 if (reload_completed)
397 flags &= ~(PROP_REG_INFO | PROP_AUTOINC);
399 /* We want alias analysis information for local dead store elimination. */
400 if (optimize && (flags & PROP_SCAN_DEAD_STORES))
401 init_alias_analysis ();
403 /* Always remove no-op moves. Do this before other processing so
404 that we don't have to keep re-scanning them. */
405 delete_noop_moves ();
407 /* Some targets can emit simpler epilogues if they know that sp was
408 not ever modified during the function. After reload, of course,
409 we've already emitted the epilogue so there's no sense searching. */
410 if (! reload_completed)
411 notice_stack_pointer_modification ();
413 /* Allocate and zero out data structures that will record the
414 data from lifetime analysis. */
415 allocate_reg_life_data ();
416 allocate_bb_life_data ();
418 /* Find the set of registers live on function exit. */
419 mark_regs_live_at_end (EXIT_BLOCK_PTR->global_live_at_start);
421 /* "Update" life info from zero. It'd be nice to begin the
422 relaxation with just the exit and noreturn blocks, but that set
423 is not immediately handy. */
425 if (flags & PROP_REG_INFO)
427 memset (regs_ever_live, 0, sizeof (regs_ever_live));
428 memset (regs_asm_clobbered, 0, sizeof (regs_asm_clobbered));
430 update_life_info (NULL, UPDATE_LIFE_GLOBAL, flags);
431 if (reg_deaths)
433 free (reg_deaths);
434 reg_deaths = NULL;
437 /* Clean up. */
438 if (optimize && (flags & PROP_SCAN_DEAD_STORES))
439 end_alias_analysis ();
441 if (file)
442 dump_flow_info (file);
444 /* Removing dead insns should have made jumptables really dead. */
445 delete_dead_jumptables ();
448 /* A subroutine of verify_wide_reg, called through for_each_rtx.
449 Search for REGNO. If found, return 2 if it is not wider than
450 word_mode. */
452 static int
453 verify_wide_reg_1 (rtx *px, void *pregno)
455 rtx x = *px;
456 unsigned int regno = *(int *) pregno;
458 if (REG_P (x) && REGNO (x) == regno)
460 if (GET_MODE_BITSIZE (GET_MODE (x)) <= BITS_PER_WORD)
461 return 2;
462 return 1;
464 return 0;
467 /* A subroutine of verify_local_live_at_start. Search through insns
468 of BB looking for register REGNO. */
470 static void
471 verify_wide_reg (int regno, basic_block bb)
473 rtx head = BB_HEAD (bb), end = BB_END (bb);
475 while (1)
477 if (INSN_P (head))
479 int r = for_each_rtx (&PATTERN (head), verify_wide_reg_1, &regno);
480 if (r == 1)
481 return;
482 if (r == 2)
483 break;
485 if (head == end)
486 break;
487 head = NEXT_INSN (head);
489 if (dump_file)
491 fprintf (dump_file, "Register %d died unexpectedly.\n", regno);
492 dump_bb (bb, dump_file, 0);
494 fatal_error ("internal consistency failure");
497 /* A subroutine of update_life_info. Verify that there are no untoward
498 changes in live_at_start during a local update. */
500 static void
501 verify_local_live_at_start (regset new_live_at_start, basic_block bb)
503 if (reload_completed)
505 /* After reload, there are no pseudos, nor subregs of multi-word
506 registers. The regsets should exactly match. */
507 if (! REG_SET_EQUAL_P (new_live_at_start, bb->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 fatal_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->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->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 fatal_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;
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 = xcalloc (sizeof (*reg_deaths), 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->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 MAX_MEM_SET_LIST_LEN 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->global_live_at_start);
641 CLEAR_REG_SET (bb->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, 1);
650 /* Clear log links in case we are asked to (re)compute them. */
651 if (prop_flags & PROP_LOG_LINKS)
652 clear_log_links (blocks);
654 if (blocks)
656 EXECUTE_IF_SET_IN_SBITMAP (blocks, 0, i,
658 bb = BASIC_BLOCK (i);
660 COPY_REG_SET (tmp, bb->global_live_at_end);
661 propagate_block (bb, tmp, NULL, NULL, stabilized_prop_flags);
663 if (extent == UPDATE_LIFE_LOCAL)
664 verify_local_live_at_start (tmp, bb);
667 else
669 FOR_EACH_BB_REVERSE (bb)
671 COPY_REG_SET (tmp, bb->global_live_at_end);
673 propagate_block (bb, tmp, NULL, NULL, stabilized_prop_flags);
675 if (extent == UPDATE_LIFE_LOCAL)
676 verify_local_live_at_start (tmp, bb);
680 FREE_REG_SET (tmp);
682 if (prop_flags & PROP_REG_INFO)
684 reg_set_iterator rsi;
686 /* The only pseudos that are live at the beginning of the function
687 are those that were not set anywhere in the function. local-alloc
688 doesn't know how to handle these correctly, so mark them as not
689 local to any one basic block. */
690 EXECUTE_IF_SET_IN_REG_SET (ENTRY_BLOCK_PTR->global_live_at_end,
691 FIRST_PSEUDO_REGISTER, i, rsi)
692 REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL;
694 /* We have a problem with any pseudoreg that lives across the setjmp.
695 ANSI says that if a user variable does not change in value between
696 the setjmp and the longjmp, then the longjmp preserves it. This
697 includes longjmp from a place where the pseudo appears dead.
698 (In principle, the value still exists if it is in scope.)
699 If the pseudo goes in a hard reg, some other value may occupy
700 that hard reg where this pseudo is dead, thus clobbering the pseudo.
701 Conclusion: such a pseudo must not go in a hard reg. */
702 EXECUTE_IF_SET_IN_REG_SET (regs_live_at_setjmp,
703 FIRST_PSEUDO_REGISTER, i, rsi)
705 if (regno_reg_rtx[i] != 0)
707 REG_LIVE_LENGTH (i) = -1;
708 REG_BASIC_BLOCK (i) = REG_BLOCK_UNKNOWN;
712 if (reg_deaths)
714 free (reg_deaths);
715 reg_deaths = NULL;
717 timevar_pop ((extent == UPDATE_LIFE_LOCAL || blocks)
718 ? TV_LIFE_UPDATE : TV_LIFE);
719 if (ndead && dump_file)
720 fprintf (dump_file, "deleted %i dead insns\n", ndead);
721 return ndead;
724 /* Update life information in all blocks where BB_DIRTY is set. */
727 update_life_info_in_dirty_blocks (enum update_life_extent extent, int prop_flags)
729 sbitmap update_life_blocks = sbitmap_alloc (last_basic_block);
730 int n = 0;
731 basic_block bb;
732 int retval = 0;
734 sbitmap_zero (update_life_blocks);
735 FOR_EACH_BB (bb)
737 if (bb->flags & BB_DIRTY)
739 SET_BIT (update_life_blocks, bb->index);
740 n++;
744 if (n)
745 retval = update_life_info (update_life_blocks, extent, prop_flags);
747 sbitmap_free (update_life_blocks);
748 return retval;
751 /* Free the variables allocated by find_basic_blocks. */
753 void
754 free_basic_block_vars (void)
756 if (basic_block_info)
758 clear_edges ();
759 basic_block_info = NULL;
761 n_basic_blocks = 0;
762 last_basic_block = 0;
763 n_edges = 0;
765 label_to_block_map = NULL;
767 ENTRY_BLOCK_PTR->aux = NULL;
768 ENTRY_BLOCK_PTR->global_live_at_end = NULL;
769 EXIT_BLOCK_PTR->aux = NULL;
770 EXIT_BLOCK_PTR->global_live_at_start = NULL;
773 /* Delete any insns that copy a register to itself. */
776 delete_noop_moves (void)
778 rtx insn, next;
779 basic_block bb;
780 int nnoops = 0;
782 FOR_EACH_BB (bb)
784 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
786 next = NEXT_INSN (insn);
787 if (INSN_P (insn) && noop_move_p (insn))
789 rtx note;
791 /* If we're about to remove the first insn of a libcall
792 then move the libcall note to the next real insn and
793 update the retval note. */
794 if ((note = find_reg_note (insn, REG_LIBCALL, NULL_RTX))
795 && XEXP (note, 0) != insn)
797 rtx new_libcall_insn = next_real_insn (insn);
798 rtx retval_note = find_reg_note (XEXP (note, 0),
799 REG_RETVAL, NULL_RTX);
800 REG_NOTES (new_libcall_insn)
801 = gen_rtx_INSN_LIST (REG_LIBCALL, XEXP (note, 0),
802 REG_NOTES (new_libcall_insn));
803 XEXP (retval_note, 0) = new_libcall_insn;
806 delete_insn_and_edges (insn);
807 nnoops++;
811 if (nnoops && dump_file)
812 fprintf (dump_file, "deleted %i noop moves", nnoops);
813 return nnoops;
816 /* Delete any jump tables never referenced. We can't delete them at the
817 time of removing tablejump insn as they are referenced by the preceding
818 insns computing the destination, so we delay deleting and garbagecollect
819 them once life information is computed. */
820 void
821 delete_dead_jumptables (void)
823 basic_block bb;
825 /* A dead jump table does not belong to any basic block. Scan insns
826 between two adjacent basic blocks. */
827 FOR_EACH_BB (bb)
829 rtx insn, next;
831 for (insn = NEXT_INSN (BB_END (bb));
832 insn && !NOTE_INSN_BASIC_BLOCK_P (insn);
833 insn = next)
835 next = NEXT_INSN (insn);
836 if (LABEL_P (insn)
837 && LABEL_NUSES (insn) == LABEL_PRESERVE_P (insn)
838 && JUMP_P (next)
839 && (GET_CODE (PATTERN (next)) == ADDR_VEC
840 || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC))
842 rtx label = insn, jump = next;
844 if (dump_file)
845 fprintf (dump_file, "Dead jumptable %i removed\n",
846 INSN_UID (insn));
848 next = NEXT_INSN (next);
849 delete_insn (jump);
850 delete_insn (label);
856 /* Determine if the stack pointer is constant over the life of the function.
857 Only useful before prologues have been emitted. */
859 static void
860 notice_stack_pointer_modification_1 (rtx x, rtx pat ATTRIBUTE_UNUSED,
861 void *data ATTRIBUTE_UNUSED)
863 if (x == stack_pointer_rtx
864 /* The stack pointer is only modified indirectly as the result
865 of a push until later in flow. See the comments in rtl.texi
866 regarding Embedded Side-Effects on Addresses. */
867 || (MEM_P (x)
868 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_AUTOINC
869 && XEXP (XEXP (x, 0), 0) == stack_pointer_rtx))
870 current_function_sp_is_unchanging = 0;
873 static void
874 notice_stack_pointer_modification (void)
876 basic_block bb;
877 rtx insn;
879 /* Assume that the stack pointer is unchanging if alloca hasn't
880 been used. */
881 current_function_sp_is_unchanging = !current_function_calls_alloca;
882 if (! current_function_sp_is_unchanging)
883 return;
885 FOR_EACH_BB (bb)
886 FOR_BB_INSNS (bb, insn)
888 if (INSN_P (insn))
890 /* Check if insn modifies the stack pointer. */
891 note_stores (PATTERN (insn),
892 notice_stack_pointer_modification_1,
893 NULL);
894 if (! current_function_sp_is_unchanging)
895 return;
900 /* Mark a register in SET. Hard registers in large modes get all
901 of their component registers set as well. */
903 static void
904 mark_reg (rtx reg, void *xset)
906 regset set = (regset) xset;
907 int regno = REGNO (reg);
909 gcc_assert (GET_MODE (reg) != BLKmode);
911 SET_REGNO_REG_SET (set, regno);
912 if (regno < FIRST_PSEUDO_REGISTER)
914 int n = hard_regno_nregs[regno][GET_MODE (reg)];
915 while (--n > 0)
916 SET_REGNO_REG_SET (set, regno + n);
920 /* Mark those regs which are needed at the end of the function as live
921 at the end of the last basic block. */
923 static void
924 mark_regs_live_at_end (regset set)
926 unsigned int i;
928 /* If exiting needs the right stack value, consider the stack pointer
929 live at the end of the function. */
930 if ((HAVE_epilogue && epilogue_completed)
931 || ! EXIT_IGNORE_STACK
932 || (! FRAME_POINTER_REQUIRED
933 && ! current_function_calls_alloca
934 && flag_omit_frame_pointer)
935 || current_function_sp_is_unchanging)
937 SET_REGNO_REG_SET (set, STACK_POINTER_REGNUM);
940 /* Mark the frame pointer if needed at the end of the function. If
941 we end up eliminating it, it will be removed from the live list
942 of each basic block by reload. */
944 if (! reload_completed || frame_pointer_needed)
946 SET_REGNO_REG_SET (set, FRAME_POINTER_REGNUM);
947 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
948 /* If they are different, also mark the hard frame pointer as live. */
949 if (! LOCAL_REGNO (HARD_FRAME_POINTER_REGNUM))
950 SET_REGNO_REG_SET (set, HARD_FRAME_POINTER_REGNUM);
951 #endif
954 #ifndef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
955 /* Many architectures have a GP register even without flag_pic.
956 Assume the pic register is not in use, or will be handled by
957 other means, if it is not fixed. */
958 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
959 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
960 SET_REGNO_REG_SET (set, PIC_OFFSET_TABLE_REGNUM);
961 #endif
963 /* Mark all global registers, and all registers used by the epilogue
964 as being live at the end of the function since they may be
965 referenced by our caller. */
966 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
967 if (global_regs[i] || EPILOGUE_USES (i))
968 SET_REGNO_REG_SET (set, i);
970 if (HAVE_epilogue && epilogue_completed)
972 /* Mark all call-saved registers that we actually used. */
973 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
974 if (regs_ever_live[i] && ! LOCAL_REGNO (i)
975 && ! TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
976 SET_REGNO_REG_SET (set, i);
979 #ifdef EH_RETURN_DATA_REGNO
980 /* Mark the registers that will contain data for the handler. */
981 if (reload_completed && current_function_calls_eh_return)
982 for (i = 0; ; ++i)
984 unsigned regno = EH_RETURN_DATA_REGNO(i);
985 if (regno == INVALID_REGNUM)
986 break;
987 SET_REGNO_REG_SET (set, regno);
989 #endif
990 #ifdef EH_RETURN_STACKADJ_RTX
991 if ((! HAVE_epilogue || ! epilogue_completed)
992 && current_function_calls_eh_return)
994 rtx tmp = EH_RETURN_STACKADJ_RTX;
995 if (tmp && REG_P (tmp))
996 mark_reg (tmp, set);
998 #endif
999 #ifdef EH_RETURN_HANDLER_RTX
1000 if ((! HAVE_epilogue || ! epilogue_completed)
1001 && current_function_calls_eh_return)
1003 rtx tmp = EH_RETURN_HANDLER_RTX;
1004 if (tmp && REG_P (tmp))
1005 mark_reg (tmp, set);
1007 #endif
1009 /* Mark function return value. */
1010 diddle_return_value (mark_reg, set);
1013 /* Propagate global life info around the graph of basic blocks. Begin
1014 considering blocks with their corresponding bit set in BLOCKS_IN.
1015 If BLOCKS_IN is null, consider it the universal set.
1017 BLOCKS_OUT is set for every block that was changed. */
1019 static void
1020 calculate_global_regs_live (sbitmap blocks_in, sbitmap blocks_out, int flags)
1022 basic_block *queue, *qhead, *qtail, *qend, bb;
1023 regset tmp, new_live_at_end, invalidated_by_call;
1024 regset registers_made_dead;
1025 bool failure_strategy_required = false;
1026 int *block_accesses;
1028 /* The registers that are modified within this in block. */
1029 regset *local_sets;
1031 /* The registers that are conditionally modified within this block.
1032 In other words, regs that are set only as part of a COND_EXEC. */
1033 regset *cond_local_sets;
1035 int i;
1037 /* Some passes used to forget clear aux field of basic block causing
1038 sick behavior here. */
1039 #ifdef ENABLE_CHECKING
1040 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1041 gcc_assert (!bb->aux);
1042 #endif
1044 tmp = ALLOC_REG_SET (&reg_obstack);
1045 new_live_at_end = ALLOC_REG_SET (&reg_obstack);
1046 invalidated_by_call = ALLOC_REG_SET (&reg_obstack);
1047 registers_made_dead = ALLOC_REG_SET (&reg_obstack);
1049 /* Inconveniently, this is only readily available in hard reg set form. */
1050 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1051 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1052 SET_REGNO_REG_SET (invalidated_by_call, i);
1054 /* Allocate space for the sets of local properties. */
1055 local_sets = xcalloc (last_basic_block - (INVALID_BLOCK + 1),
1056 sizeof (regset));
1057 cond_local_sets = xcalloc (last_basic_block - (INVALID_BLOCK + 1),
1058 sizeof (regset));
1060 /* Create a worklist. Allocate an extra slot for ENTRY_BLOCK, and one
1061 because the `head == tail' style test for an empty queue doesn't
1062 work with a full queue. */
1063 queue = xmalloc ((n_basic_blocks - (INVALID_BLOCK + 1)) * sizeof (*queue));
1064 qtail = queue;
1065 qhead = qend = queue + n_basic_blocks - (INVALID_BLOCK + 1);
1067 /* Queue the blocks set in the initial mask. Do this in reverse block
1068 number order so that we are more likely for the first round to do
1069 useful work. We use AUX non-null to flag that the block is queued. */
1070 if (blocks_in)
1072 FOR_EACH_BB (bb)
1073 if (TEST_BIT (blocks_in, bb->index))
1075 *--qhead = bb;
1076 bb->aux = bb;
1079 else
1081 FOR_EACH_BB (bb)
1083 *--qhead = bb;
1084 bb->aux = bb;
1088 block_accesses = xcalloc (last_basic_block, sizeof (int));
1090 /* We clean aux when we remove the initially-enqueued bbs, but we
1091 don't enqueue ENTRY and EXIT initially, so clean them upfront and
1092 unconditionally. */
1093 ENTRY_BLOCK_PTR->aux = EXIT_BLOCK_PTR->aux = NULL;
1095 if (blocks_out)
1096 sbitmap_zero (blocks_out);
1098 /* We work through the queue until there are no more blocks. What
1099 is live at the end of this block is precisely the union of what
1100 is live at the beginning of all its successors. So, we set its
1101 GLOBAL_LIVE_AT_END field based on the GLOBAL_LIVE_AT_START field
1102 for its successors. Then, we compute GLOBAL_LIVE_AT_START for
1103 this block by walking through the instructions in this block in
1104 reverse order and updating as we go. If that changed
1105 GLOBAL_LIVE_AT_START, we add the predecessors of the block to the
1106 queue; they will now need to recalculate GLOBAL_LIVE_AT_END.
1108 We are guaranteed to terminate, because GLOBAL_LIVE_AT_START
1109 never shrinks. If a register appears in GLOBAL_LIVE_AT_START, it
1110 must either be live at the end of the block, or used within the
1111 block. In the latter case, it will certainly never disappear
1112 from GLOBAL_LIVE_AT_START. In the former case, the register
1113 could go away only if it disappeared from GLOBAL_LIVE_AT_START
1114 for one of the successor blocks. By induction, that cannot
1115 occur.
1117 ??? This reasoning doesn't work if we start from non-empty initial
1118 GLOBAL_LIVE_AT_START sets. And there are actually two problems:
1119 1) Updating may not terminate (endless oscillation).
1120 2) Even if it does (and it usually does), the resulting information
1121 may be inaccurate. Consider for example the following case:
1123 a = ...;
1124 while (...) {...} -- 'a' not mentioned at all
1125 ... = a;
1127 If the use of 'a' is deleted between two calculations of liveness
1128 information and the initial sets are not cleared, the information
1129 about a's liveness will get stuck inside the loop and the set will
1130 appear not to be dead.
1132 We do not attempt to solve 2) -- the information is conservatively
1133 correct (i.e. we never claim that something live is dead) and the
1134 amount of optimization opportunities missed due to this problem is
1135 not significant.
1137 1) is more serious. In order to fix it, we monitor the number of times
1138 each block is processed. Once one of the blocks has been processed more
1139 times than the maximum number of rounds, we use the following strategy:
1140 When a register disappears from one of the sets, we add it to a MAKE_DEAD
1141 set, remove all registers in this set from all GLOBAL_LIVE_AT_* sets and
1142 add the blocks with changed sets into the queue. Thus we are guaranteed
1143 to terminate (the worst case corresponds to all registers in MADE_DEAD,
1144 in which case the original reasoning above is valid), but in general we
1145 only fix up a few offending registers.
1147 The maximum number of rounds for computing liveness is the largest of
1148 MAX_LIVENESS_ROUNDS and the latest loop depth count for this function. */
1150 while (qhead != qtail)
1152 int rescan, changed;
1153 basic_block bb;
1154 edge e;
1155 edge_iterator ei;
1157 bb = *qhead++;
1158 if (qhead == qend)
1159 qhead = queue;
1160 bb->aux = NULL;
1162 /* Should we start using the failure strategy? */
1163 if (bb != ENTRY_BLOCK_PTR)
1165 int max_liveness_rounds =
1166 MAX (MAX_LIVENESS_ROUNDS, cfun->max_loop_depth);
1168 block_accesses[bb->index]++;
1169 if (block_accesses[bb->index] > max_liveness_rounds)
1170 failure_strategy_required = true;
1173 /* Begin by propagating live_at_start from the successor blocks. */
1174 CLEAR_REG_SET (new_live_at_end);
1176 if (EDGE_COUNT (bb->succs) > 0)
1177 FOR_EACH_EDGE (e, ei, bb->succs)
1179 basic_block sb = e->dest;
1181 /* Call-clobbered registers die across exception and
1182 call edges. */
1183 /* ??? Abnormal call edges ignored for the moment, as this gets
1184 confused by sibling call edges, which crashes reg-stack. */
1185 if (e->flags & EDGE_EH)
1186 bitmap_ior_and_compl_into (new_live_at_end,
1187 sb->global_live_at_start,
1188 invalidated_by_call);
1189 else
1190 IOR_REG_SET (new_live_at_end, sb->global_live_at_start);
1192 /* If a target saves one register in another (instead of on
1193 the stack) the save register will need to be live for EH. */
1194 if (e->flags & EDGE_EH)
1195 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1196 if (EH_USES (i))
1197 SET_REGNO_REG_SET (new_live_at_end, i);
1199 else
1201 /* This might be a noreturn function that throws. And
1202 even if it isn't, getting the unwind info right helps
1203 debugging. */
1204 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1205 if (EH_USES (i))
1206 SET_REGNO_REG_SET (new_live_at_end, i);
1209 /* The all-important stack pointer must always be live. */
1210 SET_REGNO_REG_SET (new_live_at_end, STACK_POINTER_REGNUM);
1212 /* Before reload, there are a few registers that must be forced
1213 live everywhere -- which might not already be the case for
1214 blocks within infinite loops. */
1215 if (! reload_completed)
1217 /* Any reference to any pseudo before reload is a potential
1218 reference of the frame pointer. */
1219 SET_REGNO_REG_SET (new_live_at_end, FRAME_POINTER_REGNUM);
1221 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1222 /* Pseudos with argument area equivalences may require
1223 reloading via the argument pointer. */
1224 if (fixed_regs[ARG_POINTER_REGNUM])
1225 SET_REGNO_REG_SET (new_live_at_end, ARG_POINTER_REGNUM);
1226 #endif
1228 /* Any constant, or pseudo with constant equivalences, may
1229 require reloading from memory using the pic register. */
1230 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1231 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1232 SET_REGNO_REG_SET (new_live_at_end, PIC_OFFSET_TABLE_REGNUM);
1235 if (bb == ENTRY_BLOCK_PTR)
1237 COPY_REG_SET (bb->global_live_at_end, new_live_at_end);
1238 continue;
1241 /* On our first pass through this block, we'll go ahead and continue.
1242 Recognize first pass by checking if local_set is NULL for this
1243 basic block. On subsequent passes, we get to skip out early if
1244 live_at_end wouldn't have changed. */
1246 if (local_sets[bb->index - (INVALID_BLOCK + 1)] == NULL)
1248 local_sets[bb->index - (INVALID_BLOCK + 1)]
1249 = ALLOC_REG_SET (&reg_obstack);
1250 cond_local_sets[bb->index - (INVALID_BLOCK + 1)]
1251 = ALLOC_REG_SET (&reg_obstack);
1252 rescan = 1;
1254 else
1256 /* If any bits were removed from live_at_end, we'll have to
1257 rescan the block. This wouldn't be necessary if we had
1258 precalculated local_live, however with PROP_SCAN_DEAD_CODE
1259 local_live is really dependent on live_at_end. */
1260 rescan = bitmap_intersect_compl_p (bb->global_live_at_end,
1261 new_live_at_end);
1263 if (!rescan)
1265 regset cond_local_set;
1267 /* If any of the registers in the new live_at_end set are
1268 conditionally set in this basic block, we must rescan.
1269 This is because conditional lifetimes at the end of the
1270 block do not just take the live_at_end set into
1271 account, but also the liveness at the start of each
1272 successor block. We can miss changes in those sets if
1273 we only compare the new live_at_end against the
1274 previous one. */
1275 cond_local_set = cond_local_sets[bb->index - (INVALID_BLOCK + 1)];
1276 rescan = bitmap_intersect_p (new_live_at_end, cond_local_set);
1279 if (!rescan)
1281 regset local_set;
1283 /* Find the set of changed bits. Take this opportunity
1284 to notice that this set is empty and early out. */
1285 bitmap_xor (tmp, bb->global_live_at_end, new_live_at_end);
1286 if (bitmap_empty_p (tmp))
1287 continue;
1289 /* If any of the changed bits overlap with local_sets[bb],
1290 we'll have to rescan the block. */
1291 local_set = local_sets[bb->index - (INVALID_BLOCK + 1)];
1292 rescan = bitmap_intersect_p (tmp, local_set);
1296 /* Let our caller know that BB changed enough to require its
1297 death notes updated. */
1298 if (blocks_out)
1299 SET_BIT (blocks_out, bb->index);
1301 if (! rescan)
1303 /* Add to live_at_start the set of all registers in
1304 new_live_at_end that aren't in the old live_at_end. */
1306 changed = bitmap_ior_and_compl_into (bb->global_live_at_start,
1307 new_live_at_end,
1308 bb->global_live_at_end);
1309 COPY_REG_SET (bb->global_live_at_end, new_live_at_end);
1310 if (! changed)
1311 continue;
1313 else
1315 COPY_REG_SET (bb->global_live_at_end, new_live_at_end);
1317 /* Rescan the block insn by insn to turn (a copy of) live_at_end
1318 into live_at_start. */
1319 propagate_block (bb, new_live_at_end,
1320 local_sets[bb->index - (INVALID_BLOCK + 1)],
1321 cond_local_sets[bb->index - (INVALID_BLOCK + 1)],
1322 flags);
1324 /* If live_at start didn't change, no need to go farther. */
1325 if (REG_SET_EQUAL_P (bb->global_live_at_start, new_live_at_end))
1326 continue;
1328 if (failure_strategy_required)
1330 /* Get the list of registers that were removed from the
1331 bb->global_live_at_start set. */
1332 bitmap_and_compl (tmp, bb->global_live_at_start,
1333 new_live_at_end);
1334 if (!bitmap_empty_p (tmp))
1336 bool pbb_changed;
1337 basic_block pbb;
1339 /* It should not happen that one of registers we have
1340 removed last time is disappears again before any other
1341 register does. */
1342 pbb_changed = bitmap_ior_into (registers_made_dead, tmp);
1343 gcc_assert (pbb_changed);
1345 /* Now remove the registers from all sets. */
1346 FOR_EACH_BB (pbb)
1348 pbb_changed = false;
1350 pbb_changed
1351 |= bitmap_and_compl_into (pbb->global_live_at_start,
1352 registers_made_dead);
1353 pbb_changed
1354 |= bitmap_and_compl_into (pbb->global_live_at_end,
1355 registers_made_dead);
1356 if (!pbb_changed)
1357 continue;
1359 /* Note the (possible) change. */
1360 if (blocks_out)
1361 SET_BIT (blocks_out, pbb->index);
1363 /* Makes sure to really rescan the block. */
1364 if (local_sets[pbb->index - (INVALID_BLOCK + 1)])
1366 FREE_REG_SET (local_sets[pbb->index - (INVALID_BLOCK + 1)]);
1367 FREE_REG_SET (cond_local_sets[pbb->index - (INVALID_BLOCK + 1)]);
1368 local_sets[pbb->index - (INVALID_BLOCK + 1)] = 0;
1371 /* Add it to the queue. */
1372 if (pbb->aux == NULL)
1374 *qtail++ = pbb;
1375 if (qtail == qend)
1376 qtail = queue;
1377 pbb->aux = pbb;
1380 continue;
1382 } /* end of failure_strategy_required */
1384 COPY_REG_SET (bb->global_live_at_start, new_live_at_end);
1387 /* Queue all predecessors of BB so that we may re-examine
1388 their live_at_end. */
1389 FOR_EACH_EDGE (e, ei, bb->preds)
1391 basic_block pb = e->src;
1392 if (pb->aux == NULL)
1394 *qtail++ = pb;
1395 if (qtail == qend)
1396 qtail = queue;
1397 pb->aux = pb;
1402 FREE_REG_SET (tmp);
1403 FREE_REG_SET (new_live_at_end);
1404 FREE_REG_SET (invalidated_by_call);
1405 FREE_REG_SET (registers_made_dead);
1407 if (blocks_out)
1409 EXECUTE_IF_SET_IN_SBITMAP (blocks_out, 0, i,
1411 basic_block bb = BASIC_BLOCK (i);
1412 FREE_REG_SET (local_sets[bb->index - (INVALID_BLOCK + 1)]);
1413 FREE_REG_SET (cond_local_sets[bb->index - (INVALID_BLOCK + 1)]);
1416 else
1418 FOR_EACH_BB (bb)
1420 FREE_REG_SET (local_sets[bb->index - (INVALID_BLOCK + 1)]);
1421 FREE_REG_SET (cond_local_sets[bb->index - (INVALID_BLOCK + 1)]);
1425 free (block_accesses);
1426 free (queue);
1427 free (cond_local_sets);
1428 free (local_sets);
1432 /* This structure is used to pass parameters to and from the
1433 the function find_regno_partial(). It is used to pass in the
1434 register number we are looking, as well as to return any rtx
1435 we find. */
1437 typedef struct {
1438 unsigned regno_to_find;
1439 rtx retval;
1440 } find_regno_partial_param;
1443 /* Find the rtx for the reg numbers specified in 'data' if it is
1444 part of an expression which only uses part of the register. Return
1445 it in the structure passed in. */
1446 static int
1447 find_regno_partial (rtx *ptr, void *data)
1449 find_regno_partial_param *param = (find_regno_partial_param *)data;
1450 unsigned reg = param->regno_to_find;
1451 param->retval = NULL_RTX;
1453 if (*ptr == NULL_RTX)
1454 return 0;
1456 switch (GET_CODE (*ptr))
1458 case ZERO_EXTRACT:
1459 case SIGN_EXTRACT:
1460 case STRICT_LOW_PART:
1461 if (REG_P (XEXP (*ptr, 0)) && REGNO (XEXP (*ptr, 0)) == reg)
1463 param->retval = XEXP (*ptr, 0);
1464 return 1;
1466 break;
1468 case SUBREG:
1469 if (REG_P (SUBREG_REG (*ptr))
1470 && REGNO (SUBREG_REG (*ptr)) == reg)
1472 param->retval = SUBREG_REG (*ptr);
1473 return 1;
1475 break;
1477 default:
1478 break;
1481 return 0;
1484 /* Process all immediate successors of the entry block looking for pseudo
1485 registers which are live on entry. Find all of those whose first
1486 instance is a partial register reference of some kind, and initialize
1487 them to 0 after the entry block. This will prevent bit sets within
1488 registers whose value is unknown, and may contain some kind of sticky
1489 bits we don't want. */
1492 initialize_uninitialized_subregs (void)
1494 rtx insn;
1495 edge e;
1496 unsigned reg, did_something = 0;
1497 find_regno_partial_param param;
1498 edge_iterator ei;
1500 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
1502 basic_block bb = e->dest;
1503 regset map = bb->global_live_at_start;
1504 reg_set_iterator rsi;
1506 EXECUTE_IF_SET_IN_REG_SET (map, FIRST_PSEUDO_REGISTER, reg, rsi)
1508 int uid = REGNO_FIRST_UID (reg);
1509 rtx i;
1511 /* Find an insn which mentions the register we are looking for.
1512 Its preferable to have an instance of the register's rtl since
1513 there may be various flags set which we need to duplicate.
1514 If we can't find it, its probably an automatic whose initial
1515 value doesn't matter, or hopefully something we don't care about. */
1516 for (i = get_insns (); i && INSN_UID (i) != uid; i = NEXT_INSN (i))
1518 if (i != NULL_RTX)
1520 /* Found the insn, now get the REG rtx, if we can. */
1521 param.regno_to_find = reg;
1522 for_each_rtx (&i, find_regno_partial, &param);
1523 if (param.retval != NULL_RTX)
1525 start_sequence ();
1526 emit_move_insn (param.retval,
1527 CONST0_RTX (GET_MODE (param.retval)));
1528 insn = get_insns ();
1529 end_sequence ();
1530 insert_insn_on_edge (insn, e);
1531 did_something = 1;
1537 if (did_something)
1538 commit_edge_insertions ();
1539 return did_something;
1543 /* Subroutines of life analysis. */
1545 /* Allocate the permanent data structures that represent the results
1546 of life analysis. */
1548 static void
1549 allocate_bb_life_data (void)
1551 basic_block bb;
1553 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1555 bb->global_live_at_start = ALLOC_REG_SET (&reg_obstack);
1556 bb->global_live_at_end = ALLOC_REG_SET (&reg_obstack);
1559 regs_live_at_setjmp = ALLOC_REG_SET (&reg_obstack);
1562 void
1563 allocate_reg_life_data (void)
1565 int i;
1567 max_regno = max_reg_num ();
1568 gcc_assert (!reg_deaths);
1569 reg_deaths = xcalloc (sizeof (*reg_deaths), max_regno);
1571 /* Recalculate the register space, in case it has grown. Old style
1572 vector oriented regsets would set regset_{size,bytes} here also. */
1573 allocate_reg_info (max_regno, FALSE, FALSE);
1575 /* Reset all the data we'll collect in propagate_block and its
1576 subroutines. */
1577 for (i = 0; i < max_regno; i++)
1579 REG_N_SETS (i) = 0;
1580 REG_N_REFS (i) = 0;
1581 REG_N_DEATHS (i) = 0;
1582 REG_N_CALLS_CROSSED (i) = 0;
1583 REG_LIVE_LENGTH (i) = 0;
1584 REG_FREQ (i) = 0;
1585 REG_BASIC_BLOCK (i) = REG_BLOCK_UNKNOWN;
1589 /* Delete dead instructions for propagate_block. */
1591 static void
1592 propagate_block_delete_insn (rtx insn)
1594 rtx inote = find_reg_note (insn, REG_LABEL, NULL_RTX);
1596 /* If the insn referred to a label, and that label was attached to
1597 an ADDR_VEC, it's safe to delete the ADDR_VEC. In fact, it's
1598 pretty much mandatory to delete it, because the ADDR_VEC may be
1599 referencing labels that no longer exist.
1601 INSN may reference a deleted label, particularly when a jump
1602 table has been optimized into a direct jump. There's no
1603 real good way to fix up the reference to the deleted label
1604 when the label is deleted, so we just allow it here. */
1606 if (inote && LABEL_P (inote))
1608 rtx label = XEXP (inote, 0);
1609 rtx next;
1611 /* The label may be forced if it has been put in the constant
1612 pool. If that is the only use we must discard the table
1613 jump following it, but not the label itself. */
1614 if (LABEL_NUSES (label) == 1 + LABEL_PRESERVE_P (label)
1615 && (next = next_nonnote_insn (label)) != NULL
1616 && JUMP_P (next)
1617 && (GET_CODE (PATTERN (next)) == ADDR_VEC
1618 || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC))
1620 rtx pat = PATTERN (next);
1621 int diff_vec_p = GET_CODE (pat) == ADDR_DIFF_VEC;
1622 int len = XVECLEN (pat, diff_vec_p);
1623 int i;
1625 for (i = 0; i < len; i++)
1626 LABEL_NUSES (XEXP (XVECEXP (pat, diff_vec_p, i), 0))--;
1628 delete_insn_and_edges (next);
1629 ndead++;
1633 delete_insn_and_edges (insn);
1634 ndead++;
1637 /* Delete dead libcalls for propagate_block. Return the insn
1638 before the libcall. */
1640 static rtx
1641 propagate_block_delete_libcall (rtx insn, rtx note)
1643 rtx first = XEXP (note, 0);
1644 rtx before = PREV_INSN (first);
1646 delete_insn_chain_and_edges (first, insn);
1647 ndead++;
1648 return before;
1651 /* Update the life-status of regs for one insn. Return the previous insn. */
1654 propagate_one_insn (struct propagate_block_info *pbi, rtx insn)
1656 rtx prev = PREV_INSN (insn);
1657 int flags = pbi->flags;
1658 int insn_is_dead = 0;
1659 int libcall_is_dead = 0;
1660 rtx note;
1661 unsigned i;
1663 if (! INSN_P (insn))
1664 return prev;
1666 note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
1667 if (flags & PROP_SCAN_DEAD_CODE)
1669 insn_is_dead = insn_dead_p (pbi, PATTERN (insn), 0, REG_NOTES (insn));
1670 libcall_is_dead = (insn_is_dead && note != 0
1671 && libcall_dead_p (pbi, note, insn));
1674 /* If an instruction consists of just dead store(s) on final pass,
1675 delete it. */
1676 if ((flags & PROP_KILL_DEAD_CODE) && insn_is_dead)
1678 /* If we're trying to delete a prologue or epilogue instruction
1679 that isn't flagged as possibly being dead, something is wrong.
1680 But if we are keeping the stack pointer depressed, we might well
1681 be deleting insns that are used to compute the amount to update
1682 it by, so they are fine. */
1683 if (reload_completed
1684 && !(TREE_CODE (TREE_TYPE (current_function_decl)) == FUNCTION_TYPE
1685 && (TYPE_RETURNS_STACK_DEPRESSED
1686 (TREE_TYPE (current_function_decl))))
1687 && (((HAVE_epilogue || HAVE_prologue)
1688 && prologue_epilogue_contains (insn))
1689 || (HAVE_sibcall_epilogue
1690 && sibcall_epilogue_contains (insn)))
1691 && find_reg_note (insn, REG_MAYBE_DEAD, NULL_RTX) == 0)
1692 fatal_insn ("Attempt to delete prologue/epilogue insn:", insn);
1694 /* Record sets. Do this even for dead instructions, since they
1695 would have killed the values if they hadn't been deleted. */
1696 mark_set_regs (pbi, PATTERN (insn), insn);
1698 /* CC0 is now known to be dead. Either this insn used it,
1699 in which case it doesn't anymore, or clobbered it,
1700 so the next insn can't use it. */
1701 pbi->cc0_live = 0;
1703 if (libcall_is_dead)
1704 prev = propagate_block_delete_libcall (insn, note);
1705 else
1708 /* If INSN contains a RETVAL note and is dead, but the libcall
1709 as a whole is not dead, then we want to remove INSN, but
1710 not the whole libcall sequence.
1712 However, we need to also remove the dangling REG_LIBCALL
1713 note so that we do not have mis-matched LIBCALL/RETVAL
1714 notes. In theory we could find a new location for the
1715 REG_RETVAL note, but it hardly seems worth the effort.
1717 NOTE at this point will be the RETVAL note if it exists. */
1718 if (note)
1720 rtx libcall_note;
1722 libcall_note
1723 = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
1724 remove_note (XEXP (note, 0), libcall_note);
1727 /* Similarly if INSN contains a LIBCALL note, remove the
1728 dangling REG_RETVAL note. */
1729 note = find_reg_note (insn, REG_LIBCALL, NULL_RTX);
1730 if (note)
1732 rtx retval_note;
1734 retval_note
1735 = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
1736 remove_note (XEXP (note, 0), retval_note);
1739 /* Now delete INSN. */
1740 propagate_block_delete_insn (insn);
1743 return prev;
1746 /* See if this is an increment or decrement that can be merged into
1747 a following memory address. */
1748 #ifdef AUTO_INC_DEC
1750 rtx x = single_set (insn);
1752 /* Does this instruction increment or decrement a register? */
1753 if ((flags & PROP_AUTOINC)
1754 && x != 0
1755 && REG_P (SET_DEST (x))
1756 && (GET_CODE (SET_SRC (x)) == PLUS
1757 || GET_CODE (SET_SRC (x)) == MINUS)
1758 && XEXP (SET_SRC (x), 0) == SET_DEST (x)
1759 && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
1760 /* Ok, look for a following memory ref we can combine with.
1761 If one is found, change the memory ref to a PRE_INC
1762 or PRE_DEC, cancel this insn, and return 1.
1763 Return 0 if nothing has been done. */
1764 && try_pre_increment_1 (pbi, insn))
1765 return prev;
1767 #endif /* AUTO_INC_DEC */
1769 CLEAR_REG_SET (pbi->new_set);
1771 /* If this is not the final pass, and this insn is copying the value of
1772 a library call and it's dead, don't scan the insns that perform the
1773 library call, so that the call's arguments are not marked live. */
1774 if (libcall_is_dead)
1776 /* Record the death of the dest reg. */
1777 mark_set_regs (pbi, PATTERN (insn), insn);
1779 insn = XEXP (note, 0);
1780 return PREV_INSN (insn);
1782 else if (GET_CODE (PATTERN (insn)) == SET
1783 && SET_DEST (PATTERN (insn)) == stack_pointer_rtx
1784 && GET_CODE (SET_SRC (PATTERN (insn))) == PLUS
1785 && XEXP (SET_SRC (PATTERN (insn)), 0) == stack_pointer_rtx
1786 && GET_CODE (XEXP (SET_SRC (PATTERN (insn)), 1)) == CONST_INT)
1788 /* We have an insn to pop a constant amount off the stack.
1789 (Such insns use PLUS regardless of the direction of the stack,
1790 and any insn to adjust the stack by a constant is always a pop
1791 or part of a push.)
1792 These insns, if not dead stores, have no effect on life, though
1793 they do have an effect on the memory stores we are tracking. */
1794 invalidate_mems_from_set (pbi, stack_pointer_rtx);
1795 /* Still, we need to update local_set, lest ifcvt.c:dead_or_predicable
1796 concludes that the stack pointer is not modified. */
1797 mark_set_regs (pbi, PATTERN (insn), insn);
1799 else
1801 /* Any regs live at the time of a call instruction must not go
1802 in a register clobbered by calls. Find all regs now live and
1803 record this for them. */
1805 if (CALL_P (insn) && (flags & PROP_REG_INFO))
1807 reg_set_iterator rsi;
1808 EXECUTE_IF_SET_IN_REG_SET (pbi->reg_live, 0, i, rsi)
1809 REG_N_CALLS_CROSSED (i)++;
1812 /* Record sets. Do this even for dead instructions, since they
1813 would have killed the values if they hadn't been deleted. */
1814 mark_set_regs (pbi, PATTERN (insn), insn);
1816 if (CALL_P (insn))
1818 regset live_at_end;
1819 bool sibcall_p;
1820 rtx note, cond;
1821 int i;
1823 cond = NULL_RTX;
1824 if (GET_CODE (PATTERN (insn)) == COND_EXEC)
1825 cond = COND_EXEC_TEST (PATTERN (insn));
1827 /* Non-constant calls clobber memory, constant calls do not
1828 clobber memory, though they may clobber outgoing arguments
1829 on the stack. */
1830 if (! CONST_OR_PURE_CALL_P (insn))
1832 free_EXPR_LIST_list (&pbi->mem_set_list);
1833 pbi->mem_set_list_len = 0;
1835 else
1836 invalidate_mems_from_set (pbi, stack_pointer_rtx);
1838 /* There may be extra registers to be clobbered. */
1839 for (note = CALL_INSN_FUNCTION_USAGE (insn);
1840 note;
1841 note = XEXP (note, 1))
1842 if (GET_CODE (XEXP (note, 0)) == CLOBBER)
1843 mark_set_1 (pbi, CLOBBER, XEXP (XEXP (note, 0), 0),
1844 cond, insn, pbi->flags);
1846 /* Calls change all call-used and global registers; sibcalls do not
1847 clobber anything that must be preserved at end-of-function,
1848 except for return values. */
1850 sibcall_p = SIBLING_CALL_P (insn);
1851 live_at_end = EXIT_BLOCK_PTR->global_live_at_start;
1852 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1853 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i)
1854 && ! (sibcall_p
1855 && REGNO_REG_SET_P (live_at_end, i)
1856 && ! refers_to_regno_p (i, i+1,
1857 current_function_return_rtx,
1858 (rtx *) 0)))
1860 enum rtx_code code = global_regs[i] ? SET : CLOBBER;
1861 /* We do not want REG_UNUSED notes for these registers. */
1862 mark_set_1 (pbi, code, regno_reg_rtx[i], cond, insn,
1863 pbi->flags & ~(PROP_DEATH_NOTES | PROP_REG_INFO));
1867 /* If an insn doesn't use CC0, it becomes dead since we assume
1868 that every insn clobbers it. So show it dead here;
1869 mark_used_regs will set it live if it is referenced. */
1870 pbi->cc0_live = 0;
1872 /* Record uses. */
1873 if (! insn_is_dead)
1874 mark_used_regs (pbi, PATTERN (insn), NULL_RTX, insn);
1876 /* Sometimes we may have inserted something before INSN (such as a move)
1877 when we make an auto-inc. So ensure we will scan those insns. */
1878 #ifdef AUTO_INC_DEC
1879 prev = PREV_INSN (insn);
1880 #endif
1882 if (! insn_is_dead && CALL_P (insn))
1884 int i;
1885 rtx note, cond;
1887 cond = NULL_RTX;
1888 if (GET_CODE (PATTERN (insn)) == COND_EXEC)
1889 cond = COND_EXEC_TEST (PATTERN (insn));
1891 /* Calls use their arguments, and may clobber memory which
1892 address involves some register. */
1893 for (note = CALL_INSN_FUNCTION_USAGE (insn);
1894 note;
1895 note = XEXP (note, 1))
1896 /* We find USE or CLOBBER entities in a FUNCTION_USAGE list: both
1897 of which mark_used_regs knows how to handle. */
1898 mark_used_regs (pbi, XEXP (XEXP (note, 0), 0), cond, insn);
1900 /* The stack ptr is used (honorarily) by a CALL insn. */
1901 if ((flags & PROP_REG_INFO)
1902 && !REGNO_REG_SET_P (pbi->reg_live, STACK_POINTER_REGNUM))
1903 reg_deaths[STACK_POINTER_REGNUM] = pbi->insn_num;
1904 SET_REGNO_REG_SET (pbi->reg_live, STACK_POINTER_REGNUM);
1906 /* Calls may also reference any of the global registers,
1907 so they are made live. */
1908 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1909 if (global_regs[i])
1910 mark_used_reg (pbi, regno_reg_rtx[i], cond, insn);
1914 pbi->insn_num++;
1916 return prev;
1919 /* Initialize a propagate_block_info struct for public consumption.
1920 Note that the structure itself is opaque to this file, but that
1921 the user can use the regsets provided here. */
1923 struct propagate_block_info *
1924 init_propagate_block_info (basic_block bb, regset live, regset local_set,
1925 regset cond_local_set, int flags)
1927 struct propagate_block_info *pbi = xmalloc (sizeof (*pbi));
1929 pbi->bb = bb;
1930 pbi->reg_live = live;
1931 pbi->mem_set_list = NULL_RTX;
1932 pbi->mem_set_list_len = 0;
1933 pbi->local_set = local_set;
1934 pbi->cond_local_set = cond_local_set;
1935 pbi->cc0_live = 0;
1936 pbi->flags = flags;
1937 pbi->insn_num = 0;
1939 if (flags & (PROP_LOG_LINKS | PROP_AUTOINC))
1940 pbi->reg_next_use = xcalloc (max_reg_num (), sizeof (rtx));
1941 else
1942 pbi->reg_next_use = NULL;
1944 pbi->new_set = BITMAP_ALLOC (NULL);
1946 #ifdef HAVE_conditional_execution
1947 pbi->reg_cond_dead = splay_tree_new (splay_tree_compare_ints, NULL,
1948 free_reg_cond_life_info);
1949 pbi->reg_cond_reg = BITMAP_ALLOC (NULL);
1951 /* If this block ends in a conditional branch, for each register
1952 live from one side of the branch and not the other, record the
1953 register as conditionally dead. */
1954 if (JUMP_P (BB_END (bb))
1955 && any_condjump_p (BB_END (bb)))
1957 regset diff = ALLOC_REG_SET (&reg_obstack);
1958 basic_block bb_true, bb_false;
1959 unsigned i;
1961 /* Identify the successor blocks. */
1962 bb_true = EDGE_SUCC (bb, 0)->dest;
1963 if (!single_succ_p (bb))
1965 bb_false = EDGE_SUCC (bb, 1)->dest;
1967 if (EDGE_SUCC (bb, 0)->flags & EDGE_FALLTHRU)
1969 basic_block t = bb_false;
1970 bb_false = bb_true;
1971 bb_true = t;
1973 else
1974 gcc_assert (EDGE_SUCC (bb, 1)->flags & EDGE_FALLTHRU);
1976 else
1978 /* This can happen with a conditional jump to the next insn. */
1979 gcc_assert (JUMP_LABEL (BB_END (bb)) == BB_HEAD (bb_true));
1981 /* Simplest way to do nothing. */
1982 bb_false = bb_true;
1985 /* Compute which register lead different lives in the successors. */
1986 bitmap_xor (diff, bb_true->global_live_at_start,
1987 bb_false->global_live_at_start);
1989 if (!bitmap_empty_p (diff))
1991 /* Extract the condition from the branch. */
1992 rtx set_src = SET_SRC (pc_set (BB_END (bb)));
1993 rtx cond_true = XEXP (set_src, 0);
1994 rtx reg = XEXP (cond_true, 0);
1995 enum rtx_code inv_cond;
1997 if (GET_CODE (reg) == SUBREG)
1998 reg = SUBREG_REG (reg);
2000 /* We can only track conditional lifetimes if the condition is
2001 in the form of a reversible comparison of a register against
2002 zero. If the condition is more complex than that, then it is
2003 safe not to record any information. */
2004 inv_cond = reversed_comparison_code (cond_true, BB_END (bb));
2005 if (inv_cond != UNKNOWN
2006 && REG_P (reg)
2007 && XEXP (cond_true, 1) == const0_rtx)
2009 rtx cond_false
2010 = gen_rtx_fmt_ee (inv_cond,
2011 GET_MODE (cond_true), XEXP (cond_true, 0),
2012 XEXP (cond_true, 1));
2013 reg_set_iterator rsi;
2015 if (GET_CODE (XEXP (set_src, 1)) == PC)
2017 rtx t = cond_false;
2018 cond_false = cond_true;
2019 cond_true = t;
2022 SET_REGNO_REG_SET (pbi->reg_cond_reg, REGNO (reg));
2024 /* For each such register, mark it conditionally dead. */
2025 EXECUTE_IF_SET_IN_REG_SET (diff, 0, i, rsi)
2027 struct reg_cond_life_info *rcli;
2028 rtx cond;
2030 rcli = xmalloc (sizeof (*rcli));
2032 if (REGNO_REG_SET_P (bb_true->global_live_at_start, i))
2033 cond = cond_false;
2034 else
2035 cond = cond_true;
2036 rcli->condition = cond;
2037 rcli->stores = const0_rtx;
2038 rcli->orig_condition = cond;
2040 splay_tree_insert (pbi->reg_cond_dead, i,
2041 (splay_tree_value) rcli);
2046 FREE_REG_SET (diff);
2048 #endif
2050 /* If this block has no successors, any stores to the frame that aren't
2051 used later in the block are dead. So make a pass over the block
2052 recording any such that are made and show them dead at the end. We do
2053 a very conservative and simple job here. */
2054 if (optimize
2055 && ! (TREE_CODE (TREE_TYPE (current_function_decl)) == FUNCTION_TYPE
2056 && (TYPE_RETURNS_STACK_DEPRESSED
2057 (TREE_TYPE (current_function_decl))))
2058 && (flags & PROP_SCAN_DEAD_STORES)
2059 && (EDGE_COUNT (bb->succs) == 0
2060 || (single_succ_p (bb)
2061 && single_succ (bb) == EXIT_BLOCK_PTR
2062 && ! current_function_calls_eh_return)))
2064 rtx insn, set;
2065 for (insn = BB_END (bb); insn != BB_HEAD (bb); insn = PREV_INSN (insn))
2066 if (NONJUMP_INSN_P (insn)
2067 && (set = single_set (insn))
2068 && MEM_P (SET_DEST (set)))
2070 rtx mem = SET_DEST (set);
2071 rtx canon_mem = canon_rtx (mem);
2073 if (XEXP (canon_mem, 0) == frame_pointer_rtx
2074 || (GET_CODE (XEXP (canon_mem, 0)) == PLUS
2075 && XEXP (XEXP (canon_mem, 0), 0) == frame_pointer_rtx
2076 && GET_CODE (XEXP (XEXP (canon_mem, 0), 1)) == CONST_INT))
2077 add_to_mem_set_list (pbi, canon_mem);
2081 return pbi;
2084 /* Release a propagate_block_info struct. */
2086 void
2087 free_propagate_block_info (struct propagate_block_info *pbi)
2089 free_EXPR_LIST_list (&pbi->mem_set_list);
2091 BITMAP_FREE (pbi->new_set);
2093 #ifdef HAVE_conditional_execution
2094 splay_tree_delete (pbi->reg_cond_dead);
2095 BITMAP_FREE (pbi->reg_cond_reg);
2096 #endif
2098 if (pbi->flags & PROP_REG_INFO)
2100 int num = pbi->insn_num;
2101 unsigned i;
2102 reg_set_iterator rsi;
2104 EXECUTE_IF_SET_IN_REG_SET (pbi->reg_live, 0, i, rsi)
2106 REG_LIVE_LENGTH (i) += num - reg_deaths[i];
2107 reg_deaths[i] = 0;
2110 if (pbi->reg_next_use)
2111 free (pbi->reg_next_use);
2113 free (pbi);
2116 /* Compute the registers live at the beginning of a basic block BB from
2117 those live at the end.
2119 When called, REG_LIVE contains those live at the end. On return, it
2120 contains those live at the beginning.
2122 LOCAL_SET, if non-null, will be set with all registers killed
2123 unconditionally by this basic block.
2124 Likewise, COND_LOCAL_SET, if non-null, will be set with all registers
2125 killed conditionally by this basic block. If there is any unconditional
2126 set of a register, then the corresponding bit will be set in LOCAL_SET
2127 and cleared in COND_LOCAL_SET.
2128 It is valid for LOCAL_SET and COND_LOCAL_SET to be the same set. In this
2129 case, the resulting set will be equal to the union of the two sets that
2130 would otherwise be computed.
2132 Return nonzero if an INSN is deleted (i.e. by dead code removal). */
2135 propagate_block (basic_block bb, regset live, regset local_set,
2136 regset cond_local_set, int flags)
2138 struct propagate_block_info *pbi;
2139 rtx insn, prev;
2140 int changed;
2142 pbi = init_propagate_block_info (bb, live, local_set, cond_local_set, flags);
2144 if (flags & PROP_REG_INFO)
2146 unsigned i;
2147 reg_set_iterator rsi;
2149 /* Process the regs live at the end of the block.
2150 Mark them as not local to any one basic block. */
2151 EXECUTE_IF_SET_IN_REG_SET (live, 0, i, rsi)
2152 REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL;
2155 /* Scan the block an insn at a time from end to beginning. */
2157 changed = 0;
2158 for (insn = BB_END (bb); ; insn = prev)
2160 /* If this is a call to `setjmp' et al, warn if any
2161 non-volatile datum is live. */
2162 if ((flags & PROP_REG_INFO)
2163 && CALL_P (insn)
2164 && find_reg_note (insn, REG_SETJMP, NULL))
2165 IOR_REG_SET (regs_live_at_setjmp, pbi->reg_live);
2167 prev = propagate_one_insn (pbi, insn);
2168 if (!prev)
2169 changed |= insn != get_insns ();
2170 else
2171 changed |= NEXT_INSN (prev) != insn;
2173 if (insn == BB_HEAD (bb))
2174 break;
2177 free_propagate_block_info (pbi);
2179 return changed;
2182 /* Return 1 if X (the body of an insn, or part of it) is just dead stores
2183 (SET expressions whose destinations are registers dead after the insn).
2184 NEEDED is the regset that says which regs are alive after the insn.
2186 Unless CALL_OK is nonzero, an insn is needed if it contains a CALL.
2188 If X is the entire body of an insn, NOTES contains the reg notes
2189 pertaining to the insn. */
2191 static int
2192 insn_dead_p (struct propagate_block_info *pbi, rtx x, int call_ok,
2193 rtx notes ATTRIBUTE_UNUSED)
2195 enum rtx_code code = GET_CODE (x);
2197 /* Don't eliminate insns that may trap. */
2198 if (flag_non_call_exceptions && may_trap_p (x))
2199 return 0;
2201 #ifdef AUTO_INC_DEC
2202 /* As flow is invoked after combine, we must take existing AUTO_INC
2203 expressions into account. */
2204 for (; notes; notes = XEXP (notes, 1))
2206 if (REG_NOTE_KIND (notes) == REG_INC)
2208 int regno = REGNO (XEXP (notes, 0));
2210 /* Don't delete insns to set global regs. */
2211 if ((regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
2212 || REGNO_REG_SET_P (pbi->reg_live, regno))
2213 return 0;
2216 #endif
2218 /* If setting something that's a reg or part of one,
2219 see if that register's altered value will be live. */
2221 if (code == SET)
2223 rtx r = SET_DEST (x);
2225 #ifdef HAVE_cc0
2226 if (GET_CODE (r) == CC0)
2227 return ! pbi->cc0_live;
2228 #endif
2230 /* A SET that is a subroutine call cannot be dead. */
2231 if (GET_CODE (SET_SRC (x)) == CALL)
2233 if (! call_ok)
2234 return 0;
2237 /* Don't eliminate loads from volatile memory or volatile asms. */
2238 else if (volatile_refs_p (SET_SRC (x)))
2239 return 0;
2241 if (MEM_P (r))
2243 rtx temp, canon_r;
2245 if (MEM_VOLATILE_P (r) || GET_MODE (r) == BLKmode)
2246 return 0;
2248 canon_r = canon_rtx (r);
2250 /* Walk the set of memory locations we are currently tracking
2251 and see if one is an identical match to this memory location.
2252 If so, this memory write is dead (remember, we're walking
2253 backwards from the end of the block to the start). Since
2254 rtx_equal_p does not check the alias set or flags, we also
2255 must have the potential for them to conflict (anti_dependence). */
2256 for (temp = pbi->mem_set_list; temp != 0; temp = XEXP (temp, 1))
2257 if (anti_dependence (r, XEXP (temp, 0)))
2259 rtx mem = XEXP (temp, 0);
2261 if (rtx_equal_p (XEXP (canon_r, 0), XEXP (mem, 0))
2262 && (GET_MODE_SIZE (GET_MODE (canon_r))
2263 <= GET_MODE_SIZE (GET_MODE (mem))))
2264 return 1;
2266 #ifdef AUTO_INC_DEC
2267 /* Check if memory reference matches an auto increment. Only
2268 post increment/decrement or modify are valid. */
2269 if (GET_MODE (mem) == GET_MODE (r)
2270 && (GET_CODE (XEXP (mem, 0)) == POST_DEC
2271 || GET_CODE (XEXP (mem, 0)) == POST_INC
2272 || GET_CODE (XEXP (mem, 0)) == POST_MODIFY)
2273 && GET_MODE (XEXP (mem, 0)) == GET_MODE (r)
2274 && rtx_equal_p (XEXP (XEXP (mem, 0), 0), XEXP (r, 0)))
2275 return 1;
2276 #endif
2279 else
2281 while (GET_CODE (r) == SUBREG
2282 || GET_CODE (r) == STRICT_LOW_PART
2283 || GET_CODE (r) == ZERO_EXTRACT)
2284 r = XEXP (r, 0);
2286 if (REG_P (r))
2288 int regno = REGNO (r);
2290 /* Obvious. */
2291 if (REGNO_REG_SET_P (pbi->reg_live, regno))
2292 return 0;
2294 /* If this is a hard register, verify that subsequent
2295 words are not needed. */
2296 if (regno < FIRST_PSEUDO_REGISTER)
2298 int n = hard_regno_nregs[regno][GET_MODE (r)];
2300 while (--n > 0)
2301 if (REGNO_REG_SET_P (pbi->reg_live, regno+n))
2302 return 0;
2305 /* Don't delete insns to set global regs. */
2306 if (regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
2307 return 0;
2309 /* Make sure insns to set the stack pointer aren't deleted. */
2310 if (regno == STACK_POINTER_REGNUM)
2311 return 0;
2313 /* ??? These bits might be redundant with the force live bits
2314 in calculate_global_regs_live. We would delete from
2315 sequential sets; whether this actually affects real code
2316 for anything but the stack pointer I don't know. */
2317 /* Make sure insns to set the frame pointer aren't deleted. */
2318 if (regno == FRAME_POINTER_REGNUM
2319 && (! reload_completed || frame_pointer_needed))
2320 return 0;
2321 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2322 if (regno == HARD_FRAME_POINTER_REGNUM
2323 && (! reload_completed || frame_pointer_needed))
2324 return 0;
2325 #endif
2327 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2328 /* Make sure insns to set arg pointer are never deleted
2329 (if the arg pointer isn't fixed, there will be a USE
2330 for it, so we can treat it normally). */
2331 if (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
2332 return 0;
2333 #endif
2335 /* Otherwise, the set is dead. */
2336 return 1;
2341 /* If performing several activities, insn is dead if each activity
2342 is individually dead. Also, CLOBBERs and USEs can be ignored; a
2343 CLOBBER or USE that's inside a PARALLEL doesn't make the insn
2344 worth keeping. */
2345 else if (code == PARALLEL)
2347 int i = XVECLEN (x, 0);
2349 for (i--; i >= 0; i--)
2350 if (GET_CODE (XVECEXP (x, 0, i)) != CLOBBER
2351 && GET_CODE (XVECEXP (x, 0, i)) != USE
2352 && ! insn_dead_p (pbi, XVECEXP (x, 0, i), call_ok, NULL_RTX))
2353 return 0;
2355 return 1;
2358 /* A CLOBBER of a pseudo-register that is dead serves no purpose. That
2359 is not necessarily true for hard registers until after reload. */
2360 else if (code == CLOBBER)
2362 if (REG_P (XEXP (x, 0))
2363 && (REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER
2364 || reload_completed)
2365 && ! REGNO_REG_SET_P (pbi->reg_live, REGNO (XEXP (x, 0))))
2366 return 1;
2369 /* ??? A base USE is a historical relic. It ought not be needed anymore.
2370 Instances where it is still used are either (1) temporary and the USE
2371 escaped the pass, (2) cruft and the USE need not be emitted anymore,
2372 or (3) hiding bugs elsewhere that are not properly representing data
2373 flow. */
2375 return 0;
2378 /* If INSN is the last insn in a libcall, and assuming INSN is dead,
2379 return 1 if the entire library call is dead.
2380 This is true if INSN copies a register (hard or pseudo)
2381 and if the hard return reg of the call insn is dead.
2382 (The caller should have tested the destination of the SET inside
2383 INSN already for death.)
2385 If this insn doesn't just copy a register, then we don't
2386 have an ordinary libcall. In that case, cse could not have
2387 managed to substitute the source for the dest later on,
2388 so we can assume the libcall is dead.
2390 PBI is the block info giving pseudoregs live before this insn.
2391 NOTE is the REG_RETVAL note of the insn. */
2393 static int
2394 libcall_dead_p (struct propagate_block_info *pbi, rtx note, rtx insn)
2396 rtx x = single_set (insn);
2398 if (x)
2400 rtx r = SET_SRC (x);
2402 if (REG_P (r) || GET_CODE (r) == SUBREG)
2404 rtx call = XEXP (note, 0);
2405 rtx call_pat;
2406 int i;
2408 /* Find the call insn. */
2409 while (call != insn && !CALL_P (call))
2410 call = NEXT_INSN (call);
2412 /* If there is none, do nothing special,
2413 since ordinary death handling can understand these insns. */
2414 if (call == insn)
2415 return 0;
2417 /* See if the hard reg holding the value is dead.
2418 If this is a PARALLEL, find the call within it. */
2419 call_pat = PATTERN (call);
2420 if (GET_CODE (call_pat) == PARALLEL)
2422 for (i = XVECLEN (call_pat, 0) - 1; i >= 0; i--)
2423 if (GET_CODE (XVECEXP (call_pat, 0, i)) == SET
2424 && GET_CODE (SET_SRC (XVECEXP (call_pat, 0, i))) == CALL)
2425 break;
2427 /* This may be a library call that is returning a value
2428 via invisible pointer. Do nothing special, since
2429 ordinary death handling can understand these insns. */
2430 if (i < 0)
2431 return 0;
2433 call_pat = XVECEXP (call_pat, 0, i);
2436 if (! insn_dead_p (pbi, call_pat, 1, REG_NOTES (call)))
2437 return 0;
2439 while ((insn = PREV_INSN (insn)) != call)
2441 if (! INSN_P (insn))
2442 continue;
2443 if (! insn_dead_p (pbi, PATTERN (insn), 0, REG_NOTES (insn)))
2444 return 0;
2446 return 1;
2449 return 0;
2452 /* 1 if register REGNO was alive at a place where `setjmp' was called
2453 and was set more than once or is an argument.
2454 Such regs may be clobbered by `longjmp'. */
2457 regno_clobbered_at_setjmp (int regno)
2459 if (n_basic_blocks == 0)
2460 return 0;
2462 return ((REG_N_SETS (regno) > 1
2463 || REGNO_REG_SET_P (ENTRY_BLOCK_PTR->global_live_at_end, regno))
2464 && REGNO_REG_SET_P (regs_live_at_setjmp, regno));
2467 /* Add MEM to PBI->MEM_SET_LIST. MEM should be canonical. Respect the
2468 maximal list size; look for overlaps in mode and select the largest. */
2469 static void
2470 add_to_mem_set_list (struct propagate_block_info *pbi, rtx mem)
2472 rtx i;
2474 /* We don't know how large a BLKmode store is, so we must not
2475 take them into consideration. */
2476 if (GET_MODE (mem) == BLKmode)
2477 return;
2479 for (i = pbi->mem_set_list; i ; i = XEXP (i, 1))
2481 rtx e = XEXP (i, 0);
2482 if (rtx_equal_p (XEXP (mem, 0), XEXP (e, 0)))
2484 if (GET_MODE_SIZE (GET_MODE (mem)) > GET_MODE_SIZE (GET_MODE (e)))
2486 #ifdef AUTO_INC_DEC
2487 /* If we must store a copy of the mem, we can just modify
2488 the mode of the stored copy. */
2489 if (pbi->flags & PROP_AUTOINC)
2490 PUT_MODE (e, GET_MODE (mem));
2491 else
2492 #endif
2493 XEXP (i, 0) = mem;
2495 return;
2499 if (pbi->mem_set_list_len < MAX_MEM_SET_LIST_LEN)
2501 #ifdef AUTO_INC_DEC
2502 /* Store a copy of mem, otherwise the address may be
2503 scrogged by find_auto_inc. */
2504 if (pbi->flags & PROP_AUTOINC)
2505 mem = shallow_copy_rtx (mem);
2506 #endif
2507 pbi->mem_set_list = alloc_EXPR_LIST (0, mem, pbi->mem_set_list);
2508 pbi->mem_set_list_len++;
2512 /* INSN references memory, possibly using autoincrement addressing modes.
2513 Find any entries on the mem_set_list that need to be invalidated due
2514 to an address change. */
2516 static int
2517 invalidate_mems_from_autoinc (rtx *px, void *data)
2519 rtx x = *px;
2520 struct propagate_block_info *pbi = data;
2522 if (GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
2524 invalidate_mems_from_set (pbi, XEXP (x, 0));
2525 return -1;
2528 return 0;
2531 /* EXP is a REG or MEM. Remove any dependent entries from
2532 pbi->mem_set_list. */
2534 static void
2535 invalidate_mems_from_set (struct propagate_block_info *pbi, rtx exp)
2537 rtx temp = pbi->mem_set_list;
2538 rtx prev = NULL_RTX;
2539 rtx next;
2541 while (temp)
2543 next = XEXP (temp, 1);
2544 if ((REG_P (exp) && reg_overlap_mentioned_p (exp, XEXP (temp, 0)))
2545 /* When we get an EXP that is a mem here, we want to check if EXP
2546 overlaps the *address* of any of the mems in the list (i.e. not
2547 whether the mems actually overlap; that's done elsewhere). */
2548 || (MEM_P (exp)
2549 && reg_overlap_mentioned_p (exp, XEXP (XEXP (temp, 0), 0))))
2551 /* Splice this entry out of the list. */
2552 if (prev)
2553 XEXP (prev, 1) = next;
2554 else
2555 pbi->mem_set_list = next;
2556 free_EXPR_LIST_node (temp);
2557 pbi->mem_set_list_len--;
2559 else
2560 prev = temp;
2561 temp = next;
2565 /* Process the registers that are set within X. Their bits are set to
2566 1 in the regset DEAD, because they are dead prior to this insn.
2568 If INSN is nonzero, it is the insn being processed.
2570 FLAGS is the set of operations to perform. */
2572 static void
2573 mark_set_regs (struct propagate_block_info *pbi, rtx x, rtx insn)
2575 rtx cond = NULL_RTX;
2576 rtx link;
2577 enum rtx_code code;
2578 int flags = pbi->flags;
2580 if (insn)
2581 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2583 if (REG_NOTE_KIND (link) == REG_INC)
2584 mark_set_1 (pbi, SET, XEXP (link, 0),
2585 (GET_CODE (x) == COND_EXEC
2586 ? COND_EXEC_TEST (x) : NULL_RTX),
2587 insn, flags);
2589 retry:
2590 switch (code = GET_CODE (x))
2592 case SET:
2593 if (GET_CODE (XEXP (x, 1)) == ASM_OPERANDS)
2594 flags |= PROP_ASM_SCAN;
2595 /* Fall through */
2596 case CLOBBER:
2597 mark_set_1 (pbi, code, SET_DEST (x), cond, insn, flags);
2598 return;
2600 case COND_EXEC:
2601 cond = COND_EXEC_TEST (x);
2602 x = COND_EXEC_CODE (x);
2603 goto retry;
2605 case PARALLEL:
2607 int i;
2609 /* We must scan forwards. If we have an asm, we need to set
2610 the PROP_ASM_SCAN flag before scanning the clobbers. */
2611 for (i = 0; i < XVECLEN (x, 0); i++)
2613 rtx sub = XVECEXP (x, 0, i);
2614 switch (code = GET_CODE (sub))
2616 case COND_EXEC:
2617 gcc_assert (!cond);
2619 cond = COND_EXEC_TEST (sub);
2620 sub = COND_EXEC_CODE (sub);
2621 if (GET_CODE (sub) == SET)
2622 goto mark_set;
2623 if (GET_CODE (sub) == CLOBBER)
2624 goto mark_clob;
2625 break;
2627 case SET:
2628 mark_set:
2629 if (GET_CODE (XEXP (sub, 1)) == ASM_OPERANDS)
2630 flags |= PROP_ASM_SCAN;
2631 /* Fall through */
2632 case CLOBBER:
2633 mark_clob:
2634 mark_set_1 (pbi, code, SET_DEST (sub), cond, insn, flags);
2635 break;
2637 case ASM_OPERANDS:
2638 flags |= PROP_ASM_SCAN;
2639 break;
2641 default:
2642 break;
2645 break;
2648 default:
2649 break;
2653 /* Process a single set, which appears in INSN. REG (which may not
2654 actually be a REG, it may also be a SUBREG, PARALLEL, etc.) is
2655 being set using the CODE (which may be SET, CLOBBER, or COND_EXEC).
2656 If the set is conditional (because it appear in a COND_EXEC), COND
2657 will be the condition. */
2659 static void
2660 mark_set_1 (struct propagate_block_info *pbi, enum rtx_code code, rtx reg, rtx cond, rtx insn, int flags)
2662 int regno_first = -1, regno_last = -1;
2663 unsigned long not_dead = 0;
2664 int i;
2666 /* Modifying just one hardware register of a multi-reg value or just a
2667 byte field of a register does not mean the value from before this insn
2668 is now dead. Of course, if it was dead after it's unused now. */
2670 switch (GET_CODE (reg))
2672 case PARALLEL:
2673 /* Some targets place small structures in registers for return values of
2674 functions. We have to detect this case specially here to get correct
2675 flow information. */
2676 for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
2677 if (XEXP (XVECEXP (reg, 0, i), 0) != 0)
2678 mark_set_1 (pbi, code, XEXP (XVECEXP (reg, 0, i), 0), cond, insn,
2679 flags);
2680 return;
2682 case SIGN_EXTRACT:
2683 /* SIGN_EXTRACT cannot be an lvalue. */
2684 gcc_unreachable ();
2686 case ZERO_EXTRACT:
2687 case STRICT_LOW_PART:
2688 /* ??? Assumes STRICT_LOW_PART not used on multi-word registers. */
2690 reg = XEXP (reg, 0);
2691 while (GET_CODE (reg) == SUBREG
2692 || GET_CODE (reg) == ZERO_EXTRACT
2693 || GET_CODE (reg) == STRICT_LOW_PART);
2694 if (MEM_P (reg))
2695 break;
2696 not_dead = (unsigned long) REGNO_REG_SET_P (pbi->reg_live, REGNO (reg));
2697 /* Fall through. */
2699 case REG:
2700 regno_last = regno_first = REGNO (reg);
2701 if (regno_first < FIRST_PSEUDO_REGISTER)
2702 regno_last += hard_regno_nregs[regno_first][GET_MODE (reg)] - 1;
2703 break;
2705 case SUBREG:
2706 if (REG_P (SUBREG_REG (reg)))
2708 enum machine_mode outer_mode = GET_MODE (reg);
2709 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (reg));
2711 /* Identify the range of registers affected. This is moderately
2712 tricky for hard registers. See alter_subreg. */
2714 regno_last = regno_first = REGNO (SUBREG_REG (reg));
2715 if (regno_first < FIRST_PSEUDO_REGISTER)
2717 regno_first += subreg_regno_offset (regno_first, inner_mode,
2718 SUBREG_BYTE (reg),
2719 outer_mode);
2720 regno_last = (regno_first
2721 + hard_regno_nregs[regno_first][outer_mode] - 1);
2723 /* Since we've just adjusted the register number ranges, make
2724 sure REG matches. Otherwise some_was_live will be clear
2725 when it shouldn't have been, and we'll create incorrect
2726 REG_UNUSED notes. */
2727 reg = gen_rtx_REG (outer_mode, regno_first);
2729 else
2731 /* If the number of words in the subreg is less than the number
2732 of words in the full register, we have a well-defined partial
2733 set. Otherwise the high bits are undefined.
2735 This is only really applicable to pseudos, since we just took
2736 care of multi-word hard registers. */
2737 if (((GET_MODE_SIZE (outer_mode)
2738 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
2739 < ((GET_MODE_SIZE (inner_mode)
2740 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))
2741 not_dead = (unsigned long) REGNO_REG_SET_P (pbi->reg_live,
2742 regno_first);
2744 reg = SUBREG_REG (reg);
2747 else
2748 reg = SUBREG_REG (reg);
2749 break;
2751 default:
2752 break;
2755 /* If this set is a MEM, then it kills any aliased writes and any
2756 other MEMs which use it.
2757 If this set is a REG, then it kills any MEMs which use the reg. */
2758 if (optimize && (flags & PROP_SCAN_DEAD_STORES))
2760 if (REG_P (reg) || MEM_P (reg))
2761 invalidate_mems_from_set (pbi, reg);
2763 /* If the memory reference had embedded side effects (autoincrement
2764 address modes) then we may need to kill some entries on the
2765 memory set list. */
2766 if (insn && MEM_P (reg))
2767 for_each_rtx (&PATTERN (insn), invalidate_mems_from_autoinc, pbi);
2769 if (MEM_P (reg) && ! side_effects_p (reg)
2770 /* ??? With more effort we could track conditional memory life. */
2771 && ! cond)
2772 add_to_mem_set_list (pbi, canon_rtx (reg));
2775 if (REG_P (reg)
2776 && ! (regno_first == FRAME_POINTER_REGNUM
2777 && (! reload_completed || frame_pointer_needed))
2778 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2779 && ! (regno_first == HARD_FRAME_POINTER_REGNUM
2780 && (! reload_completed || frame_pointer_needed))
2781 #endif
2782 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2783 && ! (regno_first == ARG_POINTER_REGNUM && fixed_regs[regno_first])
2784 #endif
2787 int some_was_live = 0, some_was_dead = 0;
2789 for (i = regno_first; i <= regno_last; ++i)
2791 int needed_regno = REGNO_REG_SET_P (pbi->reg_live, i);
2792 if (pbi->local_set)
2794 /* Order of the set operation matters here since both
2795 sets may be the same. */
2796 CLEAR_REGNO_REG_SET (pbi->cond_local_set, i);
2797 if (cond != NULL_RTX
2798 && ! REGNO_REG_SET_P (pbi->local_set, i))
2799 SET_REGNO_REG_SET (pbi->cond_local_set, i);
2800 else
2801 SET_REGNO_REG_SET (pbi->local_set, i);
2803 if (code != CLOBBER)
2804 SET_REGNO_REG_SET (pbi->new_set, i);
2806 some_was_live |= needed_regno;
2807 some_was_dead |= ! needed_regno;
2810 #ifdef HAVE_conditional_execution
2811 /* Consider conditional death in deciding that the register needs
2812 a death note. */
2813 if (some_was_live && ! not_dead
2814 /* The stack pointer is never dead. Well, not strictly true,
2815 but it's very difficult to tell from here. Hopefully
2816 combine_stack_adjustments will fix up the most egregious
2817 errors. */
2818 && regno_first != STACK_POINTER_REGNUM)
2820 for (i = regno_first; i <= regno_last; ++i)
2821 if (! mark_regno_cond_dead (pbi, i, cond))
2822 not_dead |= ((unsigned long) 1) << (i - regno_first);
2824 #endif
2826 /* Additional data to record if this is the final pass. */
2827 if (flags & (PROP_LOG_LINKS | PROP_REG_INFO
2828 | PROP_DEATH_NOTES | PROP_AUTOINC))
2830 rtx y;
2831 int blocknum = pbi->bb->index;
2833 y = NULL_RTX;
2834 if (flags & (PROP_LOG_LINKS | PROP_AUTOINC))
2836 y = pbi->reg_next_use[regno_first];
2838 /* The next use is no longer next, since a store intervenes. */
2839 for (i = regno_first; i <= regno_last; ++i)
2840 pbi->reg_next_use[i] = 0;
2843 if (flags & PROP_REG_INFO)
2845 for (i = regno_first; i <= regno_last; ++i)
2847 /* Count (weighted) references, stores, etc. This counts a
2848 register twice if it is modified, but that is correct. */
2849 REG_N_SETS (i) += 1;
2850 REG_N_REFS (i) += 1;
2851 REG_FREQ (i) += REG_FREQ_FROM_BB (pbi->bb);
2853 /* The insns where a reg is live are normally counted
2854 elsewhere, but we want the count to include the insn
2855 where the reg is set, and the normal counting mechanism
2856 would not count it. */
2857 REG_LIVE_LENGTH (i) += 1;
2860 /* If this is a hard reg, record this function uses the reg. */
2861 if (regno_first < FIRST_PSEUDO_REGISTER)
2863 for (i = regno_first; i <= regno_last; i++)
2864 regs_ever_live[i] = 1;
2865 if (flags & PROP_ASM_SCAN)
2866 for (i = regno_first; i <= regno_last; i++)
2867 regs_asm_clobbered[i] = 1;
2869 else
2871 /* Keep track of which basic blocks each reg appears in. */
2872 if (REG_BASIC_BLOCK (regno_first) == REG_BLOCK_UNKNOWN)
2873 REG_BASIC_BLOCK (regno_first) = blocknum;
2874 else if (REG_BASIC_BLOCK (regno_first) != blocknum)
2875 REG_BASIC_BLOCK (regno_first) = REG_BLOCK_GLOBAL;
2879 if (! some_was_dead)
2881 if (flags & PROP_LOG_LINKS)
2883 /* Make a logical link from the next following insn
2884 that uses this register, back to this insn.
2885 The following insns have already been processed.
2887 We don't build a LOG_LINK for hard registers containing
2888 in ASM_OPERANDs. If these registers get replaced,
2889 we might wind up changing the semantics of the insn,
2890 even if reload can make what appear to be valid
2891 assignments later.
2893 We don't build a LOG_LINK for global registers to
2894 or from a function call. We don't want to let
2895 combine think that it knows what is going on with
2896 global registers. */
2897 if (y && (BLOCK_NUM (y) == blocknum)
2898 && (regno_first >= FIRST_PSEUDO_REGISTER
2899 || (asm_noperands (PATTERN (y)) < 0
2900 && ! ((CALL_P (insn)
2901 || CALL_P (y))
2902 && global_regs[regno_first]))))
2903 LOG_LINKS (y) = alloc_INSN_LIST (insn, LOG_LINKS (y));
2906 else if (not_dead)
2908 else if (! some_was_live)
2910 if (flags & PROP_REG_INFO)
2911 REG_N_DEATHS (regno_first) += 1;
2913 if (flags & PROP_DEATH_NOTES)
2915 /* Note that dead stores have already been deleted
2916 when possible. If we get here, we have found a
2917 dead store that cannot be eliminated (because the
2918 same insn does something useful). Indicate this
2919 by marking the reg being set as dying here. */
2920 REG_NOTES (insn)
2921 = alloc_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn));
2924 else
2926 if (flags & PROP_DEATH_NOTES)
2928 /* This is a case where we have a multi-word hard register
2929 and some, but not all, of the words of the register are
2930 needed in subsequent insns. Write REG_UNUSED notes
2931 for those parts that were not needed. This case should
2932 be rare. */
2934 for (i = regno_first; i <= regno_last; ++i)
2935 if (! REGNO_REG_SET_P (pbi->reg_live, i))
2936 REG_NOTES (insn)
2937 = alloc_EXPR_LIST (REG_UNUSED,
2938 regno_reg_rtx[i],
2939 REG_NOTES (insn));
2944 /* Mark the register as being dead. */
2945 if (some_was_live
2946 /* The stack pointer is never dead. Well, not strictly true,
2947 but it's very difficult to tell from here. Hopefully
2948 combine_stack_adjustments will fix up the most egregious
2949 errors. */
2950 && regno_first != STACK_POINTER_REGNUM)
2952 for (i = regno_first; i <= regno_last; ++i)
2953 if (!(not_dead & (((unsigned long) 1) << (i - regno_first))))
2955 if ((pbi->flags & PROP_REG_INFO)
2956 && REGNO_REG_SET_P (pbi->reg_live, i))
2958 REG_LIVE_LENGTH (i) += pbi->insn_num - reg_deaths[i];
2959 reg_deaths[i] = 0;
2961 CLEAR_REGNO_REG_SET (pbi->reg_live, i);
2965 else if (REG_P (reg))
2967 if (flags & (PROP_LOG_LINKS | PROP_AUTOINC))
2968 pbi->reg_next_use[regno_first] = 0;
2970 if ((flags & PROP_REG_INFO) != 0
2971 && (flags & PROP_ASM_SCAN) != 0
2972 && regno_first < FIRST_PSEUDO_REGISTER)
2974 for (i = regno_first; i <= regno_last; i++)
2975 regs_asm_clobbered[i] = 1;
2979 /* If this is the last pass and this is a SCRATCH, show it will be dying
2980 here and count it. */
2981 else if (GET_CODE (reg) == SCRATCH)
2983 if (flags & PROP_DEATH_NOTES)
2984 REG_NOTES (insn)
2985 = alloc_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn));
2989 #ifdef HAVE_conditional_execution
2990 /* Mark REGNO conditionally dead.
2991 Return true if the register is now unconditionally dead. */
2993 static int
2994 mark_regno_cond_dead (struct propagate_block_info *pbi, int regno, rtx cond)
2996 /* If this is a store to a predicate register, the value of the
2997 predicate is changing, we don't know that the predicate as seen
2998 before is the same as that seen after. Flush all dependent
2999 conditions from reg_cond_dead. This will make all such
3000 conditionally live registers unconditionally live. */
3001 if (REGNO_REG_SET_P (pbi->reg_cond_reg, regno))
3002 flush_reg_cond_reg (pbi, regno);
3004 /* If this is an unconditional store, remove any conditional
3005 life that may have existed. */
3006 if (cond == NULL_RTX)
3007 splay_tree_remove (pbi->reg_cond_dead, regno);
3008 else
3010 splay_tree_node node;
3011 struct reg_cond_life_info *rcli;
3012 rtx ncond;
3014 /* Otherwise this is a conditional set. Record that fact.
3015 It may have been conditionally used, or there may be a
3016 subsequent set with a complementary condition. */
3018 node = splay_tree_lookup (pbi->reg_cond_dead, regno);
3019 if (node == NULL)
3021 /* The register was unconditionally live previously.
3022 Record the current condition as the condition under
3023 which it is dead. */
3024 rcli = xmalloc (sizeof (*rcli));
3025 rcli->condition = cond;
3026 rcli->stores = cond;
3027 rcli->orig_condition = const0_rtx;
3028 splay_tree_insert (pbi->reg_cond_dead, regno,
3029 (splay_tree_value) rcli);
3031 SET_REGNO_REG_SET (pbi->reg_cond_reg, REGNO (XEXP (cond, 0)));
3033 /* Not unconditionally dead. */
3034 return 0;
3036 else
3038 /* The register was conditionally live previously.
3039 Add the new condition to the old. */
3040 rcli = (struct reg_cond_life_info *) node->value;
3041 ncond = rcli->condition;
3042 ncond = ior_reg_cond (ncond, cond, 1);
3043 if (rcli->stores == const0_rtx)
3044 rcli->stores = cond;
3045 else if (rcli->stores != const1_rtx)
3046 rcli->stores = ior_reg_cond (rcli->stores, cond, 1);
3048 /* If the register is now unconditionally dead, remove the entry
3049 in the splay_tree. A register is unconditionally dead if the
3050 dead condition ncond is true. A register is also unconditionally
3051 dead if the sum of all conditional stores is an unconditional
3052 store (stores is true), and the dead condition is identically the
3053 same as the original dead condition initialized at the end of
3054 the block. This is a pointer compare, not an rtx_equal_p
3055 compare. */
3056 if (ncond == const1_rtx
3057 || (ncond == rcli->orig_condition && rcli->stores == const1_rtx))
3058 splay_tree_remove (pbi->reg_cond_dead, regno);
3059 else
3061 rcli->condition = ncond;
3063 SET_REGNO_REG_SET (pbi->reg_cond_reg, REGNO (XEXP (cond, 0)));
3065 /* Not unconditionally dead. */
3066 return 0;
3071 return 1;
3074 /* Called from splay_tree_delete for pbi->reg_cond_life. */
3076 static void
3077 free_reg_cond_life_info (splay_tree_value value)
3079 struct reg_cond_life_info *rcli = (struct reg_cond_life_info *) value;
3080 free (rcli);
3083 /* Helper function for flush_reg_cond_reg. */
3085 static int
3086 flush_reg_cond_reg_1 (splay_tree_node node, void *data)
3088 struct reg_cond_life_info *rcli;
3089 int *xdata = (int *) data;
3090 unsigned int regno = xdata[0];
3092 /* Don't need to search if last flushed value was farther on in
3093 the in-order traversal. */
3094 if (xdata[1] >= (int) node->key)
3095 return 0;
3097 /* Splice out portions of the expression that refer to regno. */
3098 rcli = (struct reg_cond_life_info *) node->value;
3099 rcli->condition = elim_reg_cond (rcli->condition, regno);
3100 if (rcli->stores != const0_rtx && rcli->stores != const1_rtx)
3101 rcli->stores = elim_reg_cond (rcli->stores, regno);
3103 /* If the entire condition is now false, signal the node to be removed. */
3104 if (rcli->condition == const0_rtx)
3106 xdata[1] = node->key;
3107 return -1;
3109 else
3110 gcc_assert (rcli->condition != const1_rtx);
3112 return 0;
3115 /* Flush all (sub) expressions referring to REGNO from REG_COND_LIVE. */
3117 static void
3118 flush_reg_cond_reg (struct propagate_block_info *pbi, int regno)
3120 int pair[2];
3122 pair[0] = regno;
3123 pair[1] = -1;
3124 while (splay_tree_foreach (pbi->reg_cond_dead,
3125 flush_reg_cond_reg_1, pair) == -1)
3126 splay_tree_remove (pbi->reg_cond_dead, pair[1]);
3128 CLEAR_REGNO_REG_SET (pbi->reg_cond_reg, regno);
3131 /* Logical arithmetic on predicate conditions. IOR, NOT and AND.
3132 For ior/and, the ADD flag determines whether we want to add the new
3133 condition X to the old one unconditionally. If it is zero, we will
3134 only return a new expression if X allows us to simplify part of
3135 OLD, otherwise we return NULL to the caller.
3136 If ADD is nonzero, we will return a new condition in all cases. The
3137 toplevel caller of one of these functions should always pass 1 for
3138 ADD. */
3140 static rtx
3141 ior_reg_cond (rtx old, rtx x, int add)
3143 rtx op0, op1;
3145 if (COMPARISON_P (old))
3147 if (COMPARISON_P (x)
3148 && REVERSE_CONDEXEC_PREDICATES_P (x, old)
3149 && REGNO (XEXP (x, 0)) == REGNO (XEXP (old, 0)))
3150 return const1_rtx;
3151 if (GET_CODE (x) == GET_CODE (old)
3152 && REGNO (XEXP (x, 0)) == REGNO (XEXP (old, 0)))
3153 return old;
3154 if (! add)
3155 return NULL;
3156 return gen_rtx_IOR (0, old, x);
3159 switch (GET_CODE (old))
3161 case IOR:
3162 op0 = ior_reg_cond (XEXP (old, 0), x, 0);
3163 op1 = ior_reg_cond (XEXP (old, 1), x, 0);
3164 if (op0 != NULL || op1 != NULL)
3166 if (op0 == const0_rtx)
3167 return op1 ? op1 : gen_rtx_IOR (0, XEXP (old, 1), x);
3168 if (op1 == const0_rtx)
3169 return op0 ? op0 : gen_rtx_IOR (0, XEXP (old, 0), x);
3170 if (op0 == const1_rtx || op1 == const1_rtx)
3171 return const1_rtx;
3172 if (op0 == NULL)
3173 op0 = gen_rtx_IOR (0, XEXP (old, 0), x);
3174 else if (rtx_equal_p (x, op0))
3175 /* (x | A) | x ~ (x | A). */
3176 return old;
3177 if (op1 == NULL)
3178 op1 = gen_rtx_IOR (0, XEXP (old, 1), x);
3179 else if (rtx_equal_p (x, op1))
3180 /* (A | x) | x ~ (A | x). */
3181 return old;
3182 return gen_rtx_IOR (0, op0, op1);
3184 if (! add)
3185 return NULL;
3186 return gen_rtx_IOR (0, old, x);
3188 case AND:
3189 op0 = ior_reg_cond (XEXP (old, 0), x, 0);
3190 op1 = ior_reg_cond (XEXP (old, 1), x, 0);
3191 if (op0 != NULL || op1 != NULL)
3193 if (op0 == const1_rtx)
3194 return op1 ? op1 : gen_rtx_IOR (0, XEXP (old, 1), x);
3195 if (op1 == const1_rtx)
3196 return op0 ? op0 : gen_rtx_IOR (0, XEXP (old, 0), x);
3197 if (op0 == const0_rtx || op1 == const0_rtx)
3198 return const0_rtx;
3199 if (op0 == NULL)
3200 op0 = gen_rtx_IOR (0, XEXP (old, 0), x);
3201 else if (rtx_equal_p (x, op0))
3202 /* (x & A) | x ~ x. */
3203 return op0;
3204 if (op1 == NULL)
3205 op1 = gen_rtx_IOR (0, XEXP (old, 1), x);
3206 else if (rtx_equal_p (x, op1))
3207 /* (A & x) | x ~ x. */
3208 return op1;
3209 return gen_rtx_AND (0, op0, op1);
3211 if (! add)
3212 return NULL;
3213 return gen_rtx_IOR (0, old, x);
3215 case NOT:
3216 op0 = and_reg_cond (XEXP (old, 0), not_reg_cond (x), 0);
3217 if (op0 != NULL)
3218 return not_reg_cond (op0);
3219 if (! add)
3220 return NULL;
3221 return gen_rtx_IOR (0, old, x);
3223 default:
3224 gcc_unreachable ();
3228 static rtx
3229 not_reg_cond (rtx x)
3231 if (x == const0_rtx)
3232 return const1_rtx;
3233 else if (x == const1_rtx)
3234 return const0_rtx;
3235 if (GET_CODE (x) == NOT)
3236 return XEXP (x, 0);
3237 if (COMPARISON_P (x)
3238 && REG_P (XEXP (x, 0)))
3240 gcc_assert (XEXP (x, 1) == const0_rtx);
3242 return gen_rtx_fmt_ee (reversed_comparison_code (x, NULL),
3243 VOIDmode, XEXP (x, 0), const0_rtx);
3245 return gen_rtx_NOT (0, x);
3248 static rtx
3249 and_reg_cond (rtx old, rtx x, int add)
3251 rtx op0, op1;
3253 if (COMPARISON_P (old))
3255 if (COMPARISON_P (x)
3256 && GET_CODE (x) == reversed_comparison_code (old, NULL)
3257 && REGNO (XEXP (x, 0)) == REGNO (XEXP (old, 0)))
3258 return const0_rtx;
3259 if (GET_CODE (x) == GET_CODE (old)
3260 && REGNO (XEXP (x, 0)) == REGNO (XEXP (old, 0)))
3261 return old;
3262 if (! add)
3263 return NULL;
3264 return gen_rtx_AND (0, old, x);
3267 switch (GET_CODE (old))
3269 case IOR:
3270 op0 = and_reg_cond (XEXP (old, 0), x, 0);
3271 op1 = and_reg_cond (XEXP (old, 1), x, 0);
3272 if (op0 != NULL || op1 != NULL)
3274 if (op0 == const0_rtx)
3275 return op1 ? op1 : gen_rtx_AND (0, XEXP (old, 1), x);
3276 if (op1 == const0_rtx)
3277 return op0 ? op0 : gen_rtx_AND (0, XEXP (old, 0), x);
3278 if (op0 == const1_rtx || op1 == const1_rtx)
3279 return const1_rtx;
3280 if (op0 == NULL)
3281 op0 = gen_rtx_AND (0, XEXP (old, 0), x);
3282 else if (rtx_equal_p (x, op0))
3283 /* (x | A) & x ~ x. */
3284 return op0;
3285 if (op1 == NULL)
3286 op1 = gen_rtx_AND (0, XEXP (old, 1), x);
3287 else if (rtx_equal_p (x, op1))
3288 /* (A | x) & x ~ x. */
3289 return op1;
3290 return gen_rtx_IOR (0, op0, op1);
3292 if (! add)
3293 return NULL;
3294 return gen_rtx_AND (0, old, x);
3296 case AND:
3297 op0 = and_reg_cond (XEXP (old, 0), x, 0);
3298 op1 = and_reg_cond (XEXP (old, 1), x, 0);
3299 if (op0 != NULL || op1 != NULL)
3301 if (op0 == const1_rtx)
3302 return op1 ? op1 : gen_rtx_AND (0, XEXP (old, 1), x);
3303 if (op1 == const1_rtx)
3304 return op0 ? op0 : gen_rtx_AND (0, XEXP (old, 0), x);
3305 if (op0 == const0_rtx || op1 == const0_rtx)
3306 return const0_rtx;
3307 if (op0 == NULL)
3308 op0 = gen_rtx_AND (0, XEXP (old, 0), x);
3309 else if (rtx_equal_p (x, op0))
3310 /* (x & A) & x ~ (x & A). */
3311 return old;
3312 if (op1 == NULL)
3313 op1 = gen_rtx_AND (0, XEXP (old, 1), x);
3314 else if (rtx_equal_p (x, op1))
3315 /* (A & x) & x ~ (A & x). */
3316 return old;
3317 return gen_rtx_AND (0, op0, op1);
3319 if (! add)
3320 return NULL;
3321 return gen_rtx_AND (0, old, x);
3323 case NOT:
3324 op0 = ior_reg_cond (XEXP (old, 0), not_reg_cond (x), 0);
3325 if (op0 != NULL)
3326 return not_reg_cond (op0);
3327 if (! add)
3328 return NULL;
3329 return gen_rtx_AND (0, old, x);
3331 default:
3332 gcc_unreachable ();
3336 /* Given a condition X, remove references to reg REGNO and return the
3337 new condition. The removal will be done so that all conditions
3338 involving REGNO are considered to evaluate to false. This function
3339 is used when the value of REGNO changes. */
3341 static rtx
3342 elim_reg_cond (rtx x, unsigned int regno)
3344 rtx op0, op1;
3346 if (COMPARISON_P (x))
3348 if (REGNO (XEXP (x, 0)) == regno)
3349 return const0_rtx;
3350 return x;
3353 switch (GET_CODE (x))
3355 case AND:
3356 op0 = elim_reg_cond (XEXP (x, 0), regno);
3357 op1 = elim_reg_cond (XEXP (x, 1), regno);
3358 if (op0 == const0_rtx || op1 == const0_rtx)
3359 return const0_rtx;
3360 if (op0 == const1_rtx)
3361 return op1;
3362 if (op1 == const1_rtx)
3363 return op0;
3364 if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
3365 return x;
3366 return gen_rtx_AND (0, op0, op1);
3368 case IOR:
3369 op0 = elim_reg_cond (XEXP (x, 0), regno);
3370 op1 = elim_reg_cond (XEXP (x, 1), regno);
3371 if (op0 == const1_rtx || op1 == const1_rtx)
3372 return const1_rtx;
3373 if (op0 == const0_rtx)
3374 return op1;
3375 if (op1 == const0_rtx)
3376 return op0;
3377 if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
3378 return x;
3379 return gen_rtx_IOR (0, op0, op1);
3381 case NOT:
3382 op0 = elim_reg_cond (XEXP (x, 0), regno);
3383 if (op0 == const0_rtx)
3384 return const1_rtx;
3385 if (op0 == const1_rtx)
3386 return const0_rtx;
3387 if (op0 != XEXP (x, 0))
3388 return not_reg_cond (op0);
3389 return x;
3391 default:
3392 gcc_unreachable ();
3395 #endif /* HAVE_conditional_execution */
3397 #ifdef AUTO_INC_DEC
3399 /* Try to substitute the auto-inc expression INC as the address inside
3400 MEM which occurs in INSN. Currently, the address of MEM is an expression
3401 involving INCR_REG, and INCR is the next use of INCR_REG; it is an insn
3402 that has a single set whose source is a PLUS of INCR_REG and something
3403 else. */
3405 static void
3406 attempt_auto_inc (struct propagate_block_info *pbi, rtx inc, rtx insn,
3407 rtx mem, rtx incr, rtx incr_reg)
3409 int regno = REGNO (incr_reg);
3410 rtx set = single_set (incr);
3411 rtx q = SET_DEST (set);
3412 rtx y = SET_SRC (set);
3413 int opnum = XEXP (y, 0) == incr_reg ? 0 : 1;
3414 int changed;
3416 /* Make sure this reg appears only once in this insn. */
3417 if (count_occurrences (PATTERN (insn), incr_reg, 1) != 1)
3418 return;
3420 if (dead_or_set_p (incr, incr_reg)
3421 /* Mustn't autoinc an eliminable register. */
3422 && (regno >= FIRST_PSEUDO_REGISTER
3423 || ! TEST_HARD_REG_BIT (elim_reg_set, regno)))
3425 /* This is the simple case. Try to make the auto-inc. If
3426 we can't, we are done. Otherwise, we will do any
3427 needed updates below. */
3428 if (! validate_change (insn, &XEXP (mem, 0), inc, 0))
3429 return;
3431 else if (REG_P (q)
3432 /* PREV_INSN used here to check the semi-open interval
3433 [insn,incr). */
3434 && ! reg_used_between_p (q, PREV_INSN (insn), incr)
3435 /* We must also check for sets of q as q may be
3436 a call clobbered hard register and there may
3437 be a call between PREV_INSN (insn) and incr. */
3438 && ! reg_set_between_p (q, PREV_INSN (insn), incr))
3440 /* We have *p followed sometime later by q = p+size.
3441 Both p and q must be live afterward,
3442 and q is not used between INSN and its assignment.
3443 Change it to q = p, ...*q..., q = q+size.
3444 Then fall into the usual case. */
3445 rtx insns, temp;
3447 start_sequence ();
3448 emit_move_insn (q, incr_reg);
3449 insns = get_insns ();
3450 end_sequence ();
3452 /* If we can't make the auto-inc, or can't make the
3453 replacement into Y, exit. There's no point in making
3454 the change below if we can't do the auto-inc and doing
3455 so is not correct in the pre-inc case. */
3457 XEXP (inc, 0) = q;
3458 validate_change (insn, &XEXP (mem, 0), inc, 1);
3459 validate_change (incr, &XEXP (y, opnum), q, 1);
3460 if (! apply_change_group ())
3461 return;
3463 /* We now know we'll be doing this change, so emit the
3464 new insn(s) and do the updates. */
3465 emit_insn_before (insns, insn);
3467 if (BB_HEAD (pbi->bb) == insn)
3468 BB_HEAD (pbi->bb) = insns;
3470 /* INCR will become a NOTE and INSN won't contain a
3471 use of INCR_REG. If a use of INCR_REG was just placed in
3472 the insn before INSN, make that the next use.
3473 Otherwise, invalidate it. */
3474 if (NONJUMP_INSN_P (PREV_INSN (insn))
3475 && GET_CODE (PATTERN (PREV_INSN (insn))) == SET
3476 && SET_SRC (PATTERN (PREV_INSN (insn))) == incr_reg)
3477 pbi->reg_next_use[regno] = PREV_INSN (insn);
3478 else
3479 pbi->reg_next_use[regno] = 0;
3481 incr_reg = q;
3482 regno = REGNO (q);
3484 if ((pbi->flags & PROP_REG_INFO)
3485 && !REGNO_REG_SET_P (pbi->reg_live, regno))
3486 reg_deaths[regno] = pbi->insn_num;
3488 /* REGNO is now used in INCR which is below INSN, but
3489 it previously wasn't live here. If we don't mark
3490 it as live, we'll put a REG_DEAD note for it
3491 on this insn, which is incorrect. */
3492 SET_REGNO_REG_SET (pbi->reg_live, regno);
3494 /* If there are any calls between INSN and INCR, show
3495 that REGNO now crosses them. */
3496 for (temp = insn; temp != incr; temp = NEXT_INSN (temp))
3497 if (CALL_P (temp))
3498 REG_N_CALLS_CROSSED (regno)++;
3500 /* Invalidate alias info for Q since we just changed its value. */
3501 clear_reg_alias_info (q);
3503 else
3504 return;
3506 /* If we haven't returned, it means we were able to make the
3507 auto-inc, so update the status. First, record that this insn
3508 has an implicit side effect. */
3510 REG_NOTES (insn) = alloc_EXPR_LIST (REG_INC, incr_reg, REG_NOTES (insn));
3512 /* Modify the old increment-insn to simply copy
3513 the already-incremented value of our register. */
3514 changed = validate_change (incr, &SET_SRC (set), incr_reg, 0);
3515 gcc_assert (changed);
3517 /* If that makes it a no-op (copying the register into itself) delete
3518 it so it won't appear to be a "use" and a "set" of this
3519 register. */
3520 if (REGNO (SET_DEST (set)) == REGNO (incr_reg))
3522 /* If the original source was dead, it's dead now. */
3523 rtx note;
3525 while ((note = find_reg_note (incr, REG_DEAD, NULL_RTX)) != NULL_RTX)
3527 remove_note (incr, note);
3528 if (XEXP (note, 0) != incr_reg)
3530 unsigned int regno = REGNO (XEXP (note, 0));
3532 if ((pbi->flags & PROP_REG_INFO)
3533 && REGNO_REG_SET_P (pbi->reg_live, regno))
3535 REG_LIVE_LENGTH (regno) += pbi->insn_num - reg_deaths[regno];
3536 reg_deaths[regno] = 0;
3538 CLEAR_REGNO_REG_SET (pbi->reg_live, REGNO (XEXP (note, 0)));
3542 SET_INSN_DELETED (incr);
3545 if (regno >= FIRST_PSEUDO_REGISTER)
3547 /* Count an extra reference to the reg. When a reg is
3548 incremented, spilling it is worse, so we want to make
3549 that less likely. */
3550 REG_FREQ (regno) += REG_FREQ_FROM_BB (pbi->bb);
3552 /* Count the increment as a setting of the register,
3553 even though it isn't a SET in rtl. */
3554 REG_N_SETS (regno)++;
3558 /* X is a MEM found in INSN. See if we can convert it into an auto-increment
3559 reference. */
3561 static void
3562 find_auto_inc (struct propagate_block_info *pbi, rtx x, rtx insn)
3564 rtx addr = XEXP (x, 0);
3565 HOST_WIDE_INT offset = 0;
3566 rtx set, y, incr, inc_val;
3567 int regno;
3568 int size = GET_MODE_SIZE (GET_MODE (x));
3570 if (JUMP_P (insn))
3571 return;
3573 /* Here we detect use of an index register which might be good for
3574 postincrement, postdecrement, preincrement, or predecrement. */
3576 if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
3577 offset = INTVAL (XEXP (addr, 1)), addr = XEXP (addr, 0);
3579 if (!REG_P (addr))
3580 return;
3582 regno = REGNO (addr);
3584 /* Is the next use an increment that might make auto-increment? */
3585 incr = pbi->reg_next_use[regno];
3586 if (incr == 0 || BLOCK_NUM (incr) != BLOCK_NUM (insn))
3587 return;
3588 set = single_set (incr);
3589 if (set == 0 || GET_CODE (set) != SET)
3590 return;
3591 y = SET_SRC (set);
3593 if (GET_CODE (y) != PLUS)
3594 return;
3596 if (REG_P (XEXP (y, 0)) && REGNO (XEXP (y, 0)) == REGNO (addr))
3597 inc_val = XEXP (y, 1);
3598 else if (REG_P (XEXP (y, 1)) && REGNO (XEXP (y, 1)) == REGNO (addr))
3599 inc_val = XEXP (y, 0);
3600 else
3601 return;
3603 if (GET_CODE (inc_val) == CONST_INT)
3605 if (HAVE_POST_INCREMENT
3606 && (INTVAL (inc_val) == size && offset == 0))
3607 attempt_auto_inc (pbi, gen_rtx_POST_INC (Pmode, addr), insn, x,
3608 incr, addr);
3609 else if (HAVE_POST_DECREMENT
3610 && (INTVAL (inc_val) == -size && offset == 0))
3611 attempt_auto_inc (pbi, gen_rtx_POST_DEC (Pmode, addr), insn, x,
3612 incr, addr);
3613 else if (HAVE_PRE_INCREMENT
3614 && (INTVAL (inc_val) == size && offset == size))
3615 attempt_auto_inc (pbi, gen_rtx_PRE_INC (Pmode, addr), insn, x,
3616 incr, addr);
3617 else if (HAVE_PRE_DECREMENT
3618 && (INTVAL (inc_val) == -size && offset == -size))
3619 attempt_auto_inc (pbi, gen_rtx_PRE_DEC (Pmode, addr), insn, x,
3620 incr, addr);
3621 else if (HAVE_POST_MODIFY_DISP && offset == 0)
3622 attempt_auto_inc (pbi, gen_rtx_POST_MODIFY (Pmode, addr,
3623 gen_rtx_PLUS (Pmode,
3624 addr,
3625 inc_val)),
3626 insn, x, incr, addr);
3627 else if (HAVE_PRE_MODIFY_DISP && offset == INTVAL (inc_val))
3628 attempt_auto_inc (pbi, gen_rtx_PRE_MODIFY (Pmode, addr,
3629 gen_rtx_PLUS (Pmode,
3630 addr,
3631 inc_val)),
3632 insn, x, incr, addr);
3634 else if (REG_P (inc_val)
3635 && ! reg_set_between_p (inc_val, PREV_INSN (insn),
3636 NEXT_INSN (incr)))
3639 if (HAVE_POST_MODIFY_REG && offset == 0)
3640 attempt_auto_inc (pbi, gen_rtx_POST_MODIFY (Pmode, addr,
3641 gen_rtx_PLUS (Pmode,
3642 addr,
3643 inc_val)),
3644 insn, x, incr, addr);
3648 #endif /* AUTO_INC_DEC */
3650 static void
3651 mark_used_reg (struct propagate_block_info *pbi, rtx reg,
3652 rtx cond ATTRIBUTE_UNUSED, rtx insn)
3654 unsigned int regno_first, regno_last, i;
3655 int some_was_live, some_was_dead, some_not_set;
3657 regno_last = regno_first = REGNO (reg);
3658 if (regno_first < FIRST_PSEUDO_REGISTER)
3659 regno_last += hard_regno_nregs[regno_first][GET_MODE (reg)] - 1;
3661 /* Find out if any of this register is live after this instruction. */
3662 some_was_live = some_was_dead = 0;
3663 for (i = regno_first; i <= regno_last; ++i)
3665 int needed_regno = REGNO_REG_SET_P (pbi->reg_live, i);
3666 some_was_live |= needed_regno;
3667 some_was_dead |= ! needed_regno;
3670 /* Find out if any of the register was set this insn. */
3671 some_not_set = 0;
3672 for (i = regno_first; i <= regno_last; ++i)
3673 some_not_set |= ! REGNO_REG_SET_P (pbi->new_set, i);
3675 if (pbi->flags & (PROP_LOG_LINKS | PROP_AUTOINC))
3677 /* Record where each reg is used, so when the reg is set we know
3678 the next insn that uses it. */
3679 pbi->reg_next_use[regno_first] = insn;
3682 if (pbi->flags & PROP_REG_INFO)
3684 if (regno_first < FIRST_PSEUDO_REGISTER)
3686 /* If this is a register we are going to try to eliminate,
3687 don't mark it live here. If we are successful in
3688 eliminating it, it need not be live unless it is used for
3689 pseudos, in which case it will have been set live when it
3690 was allocated to the pseudos. If the register will not
3691 be eliminated, reload will set it live at that point.
3693 Otherwise, record that this function uses this register. */
3694 /* ??? The PPC backend tries to "eliminate" on the pic
3695 register to itself. This should be fixed. In the mean
3696 time, hack around it. */
3698 if (! (TEST_HARD_REG_BIT (elim_reg_set, regno_first)
3699 && (regno_first == FRAME_POINTER_REGNUM
3700 || regno_first == ARG_POINTER_REGNUM)))
3701 for (i = regno_first; i <= regno_last; ++i)
3702 regs_ever_live[i] = 1;
3704 else
3706 /* Keep track of which basic block each reg appears in. */
3708 int blocknum = pbi->bb->index;
3709 if (REG_BASIC_BLOCK (regno_first) == REG_BLOCK_UNKNOWN)
3710 REG_BASIC_BLOCK (regno_first) = blocknum;
3711 else if (REG_BASIC_BLOCK (regno_first) != blocknum)
3712 REG_BASIC_BLOCK (regno_first) = REG_BLOCK_GLOBAL;
3714 /* Count (weighted) number of uses of each reg. */
3715 REG_FREQ (regno_first) += REG_FREQ_FROM_BB (pbi->bb);
3716 REG_N_REFS (regno_first)++;
3718 for (i = regno_first; i <= regno_last; ++i)
3719 if (! REGNO_REG_SET_P (pbi->reg_live, i))
3721 gcc_assert (!reg_deaths[i]);
3722 reg_deaths[i] = pbi->insn_num;
3726 /* Record and count the insns in which a reg dies. If it is used in
3727 this insn and was dead below the insn then it dies in this insn.
3728 If it was set in this insn, we do not make a REG_DEAD note;
3729 likewise if we already made such a note. */
3730 if ((pbi->flags & (PROP_DEATH_NOTES | PROP_REG_INFO))
3731 && some_was_dead
3732 && some_not_set)
3734 /* Check for the case where the register dying partially
3735 overlaps the register set by this insn. */
3736 if (regno_first != regno_last)
3737 for (i = regno_first; i <= regno_last; ++i)
3738 some_was_live |= REGNO_REG_SET_P (pbi->new_set, i);
3740 /* If none of the words in X is needed, make a REG_DEAD note.
3741 Otherwise, we must make partial REG_DEAD notes. */
3742 if (! some_was_live)
3744 if ((pbi->flags & PROP_DEATH_NOTES)
3745 && ! find_regno_note (insn, REG_DEAD, regno_first))
3746 REG_NOTES (insn)
3747 = alloc_EXPR_LIST (REG_DEAD, reg, REG_NOTES (insn));
3749 if (pbi->flags & PROP_REG_INFO)
3750 REG_N_DEATHS (regno_first)++;
3752 else
3754 /* Don't make a REG_DEAD note for a part of a register
3755 that is set in the insn. */
3756 for (i = regno_first; i <= regno_last; ++i)
3757 if (! REGNO_REG_SET_P (pbi->reg_live, i)
3758 && ! dead_or_set_regno_p (insn, i))
3759 REG_NOTES (insn)
3760 = alloc_EXPR_LIST (REG_DEAD,
3761 regno_reg_rtx[i],
3762 REG_NOTES (insn));
3766 /* Mark the register as being live. */
3767 for (i = regno_first; i <= regno_last; ++i)
3769 #ifdef HAVE_conditional_execution
3770 int this_was_live = REGNO_REG_SET_P (pbi->reg_live, i);
3771 #endif
3773 SET_REGNO_REG_SET (pbi->reg_live, i);
3775 #ifdef HAVE_conditional_execution
3776 /* If this is a conditional use, record that fact. If it is later
3777 conditionally set, we'll know to kill the register. */
3778 if (cond != NULL_RTX)
3780 splay_tree_node node;
3781 struct reg_cond_life_info *rcli;
3782 rtx ncond;
3784 if (this_was_live)
3786 node = splay_tree_lookup (pbi->reg_cond_dead, i);
3787 if (node == NULL)
3789 /* The register was unconditionally live previously.
3790 No need to do anything. */
3792 else
3794 /* The register was conditionally live previously.
3795 Subtract the new life cond from the old death cond. */
3796 rcli = (struct reg_cond_life_info *) node->value;
3797 ncond = rcli->condition;
3798 ncond = and_reg_cond (ncond, not_reg_cond (cond), 1);
3800 /* If the register is now unconditionally live,
3801 remove the entry in the splay_tree. */
3802 if (ncond == const0_rtx)
3803 splay_tree_remove (pbi->reg_cond_dead, i);
3804 else
3806 rcli->condition = ncond;
3807 SET_REGNO_REG_SET (pbi->reg_cond_reg,
3808 REGNO (XEXP (cond, 0)));
3812 else
3814 /* The register was not previously live at all. Record
3815 the condition under which it is still dead. */
3816 rcli = xmalloc (sizeof (*rcli));
3817 rcli->condition = not_reg_cond (cond);
3818 rcli->stores = const0_rtx;
3819 rcli->orig_condition = const0_rtx;
3820 splay_tree_insert (pbi->reg_cond_dead, i,
3821 (splay_tree_value) rcli);
3823 SET_REGNO_REG_SET (pbi->reg_cond_reg, REGNO (XEXP (cond, 0)));
3826 else if (this_was_live)
3828 /* The register may have been conditionally live previously, but
3829 is now unconditionally live. Remove it from the conditionally
3830 dead list, so that a conditional set won't cause us to think
3831 it dead. */
3832 splay_tree_remove (pbi->reg_cond_dead, i);
3834 #endif
3838 /* Scan expression X and store a 1-bit in NEW_LIVE for each reg it uses.
3839 This is done assuming the registers needed from X are those that
3840 have 1-bits in PBI->REG_LIVE.
3842 INSN is the containing instruction. If INSN is dead, this function
3843 is not called. */
3845 static void
3846 mark_used_regs (struct propagate_block_info *pbi, rtx x, rtx cond, rtx insn)
3848 RTX_CODE code;
3849 int regno;
3850 int flags = pbi->flags;
3852 retry:
3853 if (!x)
3854 return;
3855 code = GET_CODE (x);
3856 switch (code)
3858 case LABEL_REF:
3859 case SYMBOL_REF:
3860 case CONST_INT:
3861 case CONST:
3862 case CONST_DOUBLE:
3863 case CONST_VECTOR:
3864 case PC:
3865 case ADDR_VEC:
3866 case ADDR_DIFF_VEC:
3867 return;
3869 #ifdef HAVE_cc0
3870 case CC0:
3871 pbi->cc0_live = 1;
3872 return;
3873 #endif
3875 case CLOBBER:
3876 /* If we are clobbering a MEM, mark any registers inside the address
3877 as being used. */
3878 if (MEM_P (XEXP (x, 0)))
3879 mark_used_regs (pbi, XEXP (XEXP (x, 0), 0), cond, insn);
3880 return;
3882 case MEM:
3883 /* Don't bother watching stores to mems if this is not the
3884 final pass. We'll not be deleting dead stores this round. */
3885 if (optimize && (flags & PROP_SCAN_DEAD_STORES))
3887 /* Invalidate the data for the last MEM stored, but only if MEM is
3888 something that can be stored into. */
3889 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
3890 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
3891 /* Needn't clear the memory set list. */
3893 else
3895 rtx temp = pbi->mem_set_list;
3896 rtx prev = NULL_RTX;
3897 rtx next;
3899 while (temp)
3901 next = XEXP (temp, 1);
3902 if (anti_dependence (XEXP (temp, 0), x))
3904 /* Splice temp out of the list. */
3905 if (prev)
3906 XEXP (prev, 1) = next;
3907 else
3908 pbi->mem_set_list = next;
3909 free_EXPR_LIST_node (temp);
3910 pbi->mem_set_list_len--;
3912 else
3913 prev = temp;
3914 temp = next;
3918 /* If the memory reference had embedded side effects (autoincrement
3919 address modes. Then we may need to kill some entries on the
3920 memory set list. */
3921 if (insn)
3922 for_each_rtx (&PATTERN (insn), invalidate_mems_from_autoinc, pbi);
3925 #ifdef AUTO_INC_DEC
3926 if (flags & PROP_AUTOINC)
3927 find_auto_inc (pbi, x, insn);
3928 #endif
3929 break;
3931 case SUBREG:
3932 #ifdef CANNOT_CHANGE_MODE_CLASS
3933 if (flags & PROP_REG_INFO)
3934 record_subregs_of_mode (x);
3935 #endif
3937 /* While we're here, optimize this case. */
3938 x = SUBREG_REG (x);
3939 if (!REG_P (x))
3940 goto retry;
3941 /* Fall through. */
3943 case REG:
3944 /* See a register other than being set => mark it as needed. */
3945 mark_used_reg (pbi, x, cond, insn);
3946 return;
3948 case SET:
3950 rtx testreg = SET_DEST (x);
3951 int mark_dest = 0;
3953 /* If storing into MEM, don't show it as being used. But do
3954 show the address as being used. */
3955 if (MEM_P (testreg))
3957 #ifdef AUTO_INC_DEC
3958 if (flags & PROP_AUTOINC)
3959 find_auto_inc (pbi, testreg, insn);
3960 #endif
3961 mark_used_regs (pbi, XEXP (testreg, 0), cond, insn);
3962 mark_used_regs (pbi, SET_SRC (x), cond, insn);
3963 return;
3966 /* Storing in STRICT_LOW_PART is like storing in a reg
3967 in that this SET might be dead, so ignore it in TESTREG.
3968 but in some other ways it is like using the reg.
3970 Storing in a SUBREG or a bit field is like storing the entire
3971 register in that if the register's value is not used
3972 then this SET is not needed. */
3973 while (GET_CODE (testreg) == STRICT_LOW_PART
3974 || GET_CODE (testreg) == ZERO_EXTRACT
3975 || GET_CODE (testreg) == SUBREG)
3977 #ifdef CANNOT_CHANGE_MODE_CLASS
3978 if ((flags & PROP_REG_INFO) && GET_CODE (testreg) == SUBREG)
3979 record_subregs_of_mode (testreg);
3980 #endif
3982 /* Modifying a single register in an alternate mode
3983 does not use any of the old value. But these other
3984 ways of storing in a register do use the old value. */
3985 if (GET_CODE (testreg) == SUBREG
3986 && !((REG_BYTES (SUBREG_REG (testreg))
3987 + UNITS_PER_WORD - 1) / UNITS_PER_WORD
3988 > (REG_BYTES (testreg)
3989 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))
3991 else
3992 mark_dest = 1;
3994 testreg = XEXP (testreg, 0);
3997 /* If this is a store into a register or group of registers,
3998 recursively scan the value being stored. */
4000 if ((GET_CODE (testreg) == PARALLEL
4001 && GET_MODE (testreg) == BLKmode)
4002 || (REG_P (testreg)
4003 && (regno = REGNO (testreg),
4004 ! (regno == FRAME_POINTER_REGNUM
4005 && (! reload_completed || frame_pointer_needed)))
4006 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
4007 && ! (regno == HARD_FRAME_POINTER_REGNUM
4008 && (! reload_completed || frame_pointer_needed))
4009 #endif
4010 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
4011 && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
4012 #endif
4015 if (mark_dest)
4016 mark_used_regs (pbi, SET_DEST (x), cond, insn);
4017 mark_used_regs (pbi, SET_SRC (x), cond, insn);
4018 return;
4021 break;
4023 case ASM_OPERANDS:
4024 case UNSPEC_VOLATILE:
4025 case TRAP_IF:
4026 case ASM_INPUT:
4028 /* Traditional and volatile asm instructions must be considered to use
4029 and clobber all hard registers, all pseudo-registers and all of
4030 memory. So must TRAP_IF and UNSPEC_VOLATILE operations.
4032 Consider for instance a volatile asm that changes the fpu rounding
4033 mode. An insn should not be moved across this even if it only uses
4034 pseudo-regs because it might give an incorrectly rounded result.
4036 ?!? Unfortunately, marking all hard registers as live causes massive
4037 problems for the register allocator and marking all pseudos as live
4038 creates mountains of uninitialized variable warnings.
4040 So for now, just clear the memory set list and mark any regs
4041 we can find in ASM_OPERANDS as used. */
4042 if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
4044 free_EXPR_LIST_list (&pbi->mem_set_list);
4045 pbi->mem_set_list_len = 0;
4048 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
4049 We can not just fall through here since then we would be confused
4050 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
4051 traditional asms unlike their normal usage. */
4052 if (code == ASM_OPERANDS)
4054 int j;
4056 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
4057 mark_used_regs (pbi, ASM_OPERANDS_INPUT (x, j), cond, insn);
4059 break;
4062 case COND_EXEC:
4063 gcc_assert (!cond);
4065 mark_used_regs (pbi, COND_EXEC_TEST (x), NULL_RTX, insn);
4067 cond = COND_EXEC_TEST (x);
4068 x = COND_EXEC_CODE (x);
4069 goto retry;
4071 default:
4072 break;
4075 /* Recursively scan the operands of this expression. */
4078 const char * const fmt = GET_RTX_FORMAT (code);
4079 int i;
4081 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4083 if (fmt[i] == 'e')
4085 /* Tail recursive case: save a function call level. */
4086 if (i == 0)
4088 x = XEXP (x, 0);
4089 goto retry;
4091 mark_used_regs (pbi, XEXP (x, i), cond, insn);
4093 else if (fmt[i] == 'E')
4095 int j;
4096 for (j = 0; j < XVECLEN (x, i); j++)
4097 mark_used_regs (pbi, XVECEXP (x, i, j), cond, insn);
4103 #ifdef AUTO_INC_DEC
4105 static int
4106 try_pre_increment_1 (struct propagate_block_info *pbi, rtx insn)
4108 /* Find the next use of this reg. If in same basic block,
4109 make it do pre-increment or pre-decrement if appropriate. */
4110 rtx x = single_set (insn);
4111 HOST_WIDE_INT amount = ((GET_CODE (SET_SRC (x)) == PLUS ? 1 : -1)
4112 * INTVAL (XEXP (SET_SRC (x), 1)));
4113 int regno = REGNO (SET_DEST (x));
4114 rtx y = pbi->reg_next_use[regno];
4115 if (y != 0
4116 && SET_DEST (x) != stack_pointer_rtx
4117 && BLOCK_NUM (y) == BLOCK_NUM (insn)
4118 /* Don't do this if the reg dies, or gets set in y; a standard addressing
4119 mode would be better. */
4120 && ! dead_or_set_p (y, SET_DEST (x))
4121 && try_pre_increment (y, SET_DEST (x), amount))
4123 /* We have found a suitable auto-increment and already changed
4124 insn Y to do it. So flush this increment instruction. */
4125 propagate_block_delete_insn (insn);
4127 /* Count a reference to this reg for the increment insn we are
4128 deleting. When a reg is incremented, spilling it is worse,
4129 so we want to make that less likely. */
4130 if (regno >= FIRST_PSEUDO_REGISTER)
4132 REG_FREQ (regno) += REG_FREQ_FROM_BB (pbi->bb);
4133 REG_N_SETS (regno)++;
4136 /* Flush any remembered memories depending on the value of
4137 the incremented register. */
4138 invalidate_mems_from_set (pbi, SET_DEST (x));
4140 return 1;
4142 return 0;
4145 /* Try to change INSN so that it does pre-increment or pre-decrement
4146 addressing on register REG in order to add AMOUNT to REG.
4147 AMOUNT is negative for pre-decrement.
4148 Returns 1 if the change could be made.
4149 This checks all about the validity of the result of modifying INSN. */
4151 static int
4152 try_pre_increment (rtx insn, rtx reg, HOST_WIDE_INT amount)
4154 rtx use;
4156 /* Nonzero if we can try to make a pre-increment or pre-decrement.
4157 For example, addl $4,r1; movl (r1),... can become movl +(r1),... */
4158 int pre_ok = 0;
4159 /* Nonzero if we can try to make a post-increment or post-decrement.
4160 For example, addl $4,r1; movl -4(r1),... can become movl (r1)+,...
4161 It is possible for both PRE_OK and POST_OK to be nonzero if the machine
4162 supports both pre-inc and post-inc, or both pre-dec and post-dec. */
4163 int post_ok = 0;
4165 /* Nonzero if the opportunity actually requires post-inc or post-dec. */
4166 int do_post = 0;
4168 /* From the sign of increment, see which possibilities are conceivable
4169 on this target machine. */
4170 if (HAVE_PRE_INCREMENT && amount > 0)
4171 pre_ok = 1;
4172 if (HAVE_POST_INCREMENT && amount > 0)
4173 post_ok = 1;
4175 if (HAVE_PRE_DECREMENT && amount < 0)
4176 pre_ok = 1;
4177 if (HAVE_POST_DECREMENT && amount < 0)
4178 post_ok = 1;
4180 if (! (pre_ok || post_ok))
4181 return 0;
4183 /* It is not safe to add a side effect to a jump insn
4184 because if the incremented register is spilled and must be reloaded
4185 there would be no way to store the incremented value back in memory. */
4187 if (JUMP_P (insn))
4188 return 0;
4190 use = 0;
4191 if (pre_ok)
4192 use = find_use_as_address (PATTERN (insn), reg, 0);
4193 if (post_ok && (use == 0 || use == (rtx) (size_t) 1))
4195 use = find_use_as_address (PATTERN (insn), reg, -amount);
4196 do_post = 1;
4199 if (use == 0 || use == (rtx) (size_t) 1)
4200 return 0;
4202 if (GET_MODE_SIZE (GET_MODE (use)) != (amount > 0 ? amount : - amount))
4203 return 0;
4205 /* See if this combination of instruction and addressing mode exists. */
4206 if (! validate_change (insn, &XEXP (use, 0),
4207 gen_rtx_fmt_e (amount > 0
4208 ? (do_post ? POST_INC : PRE_INC)
4209 : (do_post ? POST_DEC : PRE_DEC),
4210 Pmode, reg), 0))
4211 return 0;
4213 /* Record that this insn now has an implicit side effect on X. */
4214 REG_NOTES (insn) = alloc_EXPR_LIST (REG_INC, reg, REG_NOTES (insn));
4215 return 1;
4218 #endif /* AUTO_INC_DEC */
4220 /* Find the place in the rtx X where REG is used as a memory address.
4221 Return the MEM rtx that so uses it.
4222 If PLUSCONST is nonzero, search instead for a memory address equivalent to
4223 (plus REG (const_int PLUSCONST)).
4225 If such an address does not appear, return 0.
4226 If REG appears more than once, or is used other than in such an address,
4227 return (rtx) 1. */
4230 find_use_as_address (rtx x, rtx reg, HOST_WIDE_INT plusconst)
4232 enum rtx_code code = GET_CODE (x);
4233 const char * const fmt = GET_RTX_FORMAT (code);
4234 int i;
4235 rtx value = 0;
4236 rtx tem;
4238 if (code == MEM && XEXP (x, 0) == reg && plusconst == 0)
4239 return x;
4241 if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
4242 && XEXP (XEXP (x, 0), 0) == reg
4243 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4244 && INTVAL (XEXP (XEXP (x, 0), 1)) == plusconst)
4245 return x;
4247 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
4249 /* If REG occurs inside a MEM used in a bit-field reference,
4250 that is unacceptable. */
4251 if (find_use_as_address (XEXP (x, 0), reg, 0) != 0)
4252 return (rtx) (size_t) 1;
4255 if (x == reg)
4256 return (rtx) (size_t) 1;
4258 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4260 if (fmt[i] == 'e')
4262 tem = find_use_as_address (XEXP (x, i), reg, plusconst);
4263 if (value == 0)
4264 value = tem;
4265 else if (tem != 0)
4266 return (rtx) (size_t) 1;
4268 else if (fmt[i] == 'E')
4270 int j;
4271 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4273 tem = find_use_as_address (XVECEXP (x, i, j), reg, plusconst);
4274 if (value == 0)
4275 value = tem;
4276 else if (tem != 0)
4277 return (rtx) (size_t) 1;
4282 return value;
4285 /* Write information about registers and basic blocks into FILE.
4286 This is part of making a debugging dump. */
4288 void
4289 dump_regset (regset r, FILE *outf)
4291 unsigned i;
4292 reg_set_iterator rsi;
4294 if (r == NULL)
4296 fputs (" (nil)", outf);
4297 return;
4300 EXECUTE_IF_SET_IN_REG_SET (r, 0, i, rsi)
4302 fprintf (outf, " %d", i);
4303 if (i < FIRST_PSEUDO_REGISTER)
4304 fprintf (outf, " [%s]",
4305 reg_names[i]);
4309 /* Print a human-readable representation of R on the standard error
4310 stream. This function is designed to be used from within the
4311 debugger. */
4313 void
4314 debug_regset (regset r)
4316 dump_regset (r, stderr);
4317 putc ('\n', stderr);
4320 /* Recompute register set/reference counts immediately prior to register
4321 allocation.
4323 This avoids problems with set/reference counts changing to/from values
4324 which have special meanings to the register allocators.
4326 Additionally, the reference counts are the primary component used by the
4327 register allocators to prioritize pseudos for allocation to hard regs.
4328 More accurate reference counts generally lead to better register allocation.
4330 It might be worthwhile to update REG_LIVE_LENGTH, REG_BASIC_BLOCK and
4331 possibly other information which is used by the register allocators. */
4333 void
4334 recompute_reg_usage (void)
4336 allocate_reg_life_data ();
4337 /* distribute_notes in combiner fails to convert some of the
4338 REG_UNUSED notes to REG_DEAD notes. This causes CHECK_DEAD_NOTES
4339 in sched1 to die. To solve this update the DEATH_NOTES
4340 here. */
4341 update_life_info (NULL, UPDATE_LIFE_LOCAL, PROP_REG_INFO | PROP_DEATH_NOTES);
4344 /* Optionally removes all the REG_DEAD and REG_UNUSED notes from a set of
4345 blocks. If BLOCKS is NULL, assume the universal set. Returns a count
4346 of the number of registers that died. */
4349 count_or_remove_death_notes (sbitmap blocks, int kill)
4351 int count = 0;
4352 int i;
4353 basic_block bb;
4355 /* This used to be a loop over all the blocks with a membership test
4356 inside the loop. That can be amazingly expensive on a large CFG
4357 when only a small number of bits are set in BLOCKs (for example,
4358 the calls from the scheduler typically have very few bits set).
4360 For extra credit, someone should convert BLOCKS to a bitmap rather
4361 than an sbitmap. */
4362 if (blocks)
4364 EXECUTE_IF_SET_IN_SBITMAP (blocks, 0, i,
4366 count += count_or_remove_death_notes_bb (BASIC_BLOCK (i), kill);
4369 else
4371 FOR_EACH_BB (bb)
4373 count += count_or_remove_death_notes_bb (bb, kill);
4377 return count;
4380 /* Optionally removes all the REG_DEAD and REG_UNUSED notes from basic
4381 block BB. Returns a count of the number of registers that died. */
4383 static int
4384 count_or_remove_death_notes_bb (basic_block bb, int kill)
4386 int count = 0;
4387 rtx insn;
4389 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
4391 if (INSN_P (insn))
4393 rtx *pprev = &REG_NOTES (insn);
4394 rtx link = *pprev;
4396 while (link)
4398 switch (REG_NOTE_KIND (link))
4400 case REG_DEAD:
4401 if (REG_P (XEXP (link, 0)))
4403 rtx reg = XEXP (link, 0);
4404 int n;
4406 if (REGNO (reg) >= FIRST_PSEUDO_REGISTER)
4407 n = 1;
4408 else
4409 n = hard_regno_nregs[REGNO (reg)][GET_MODE (reg)];
4410 count += n;
4413 /* Fall through. */
4415 case REG_UNUSED:
4416 if (kill)
4418 rtx next = XEXP (link, 1);
4419 free_EXPR_LIST_node (link);
4420 *pprev = link = next;
4421 break;
4423 /* Fall through. */
4425 default:
4426 pprev = &XEXP (link, 1);
4427 link = *pprev;
4428 break;
4433 if (insn == BB_END (bb))
4434 break;
4437 return count;
4440 /* Clear LOG_LINKS fields of insns in a selected blocks or whole chain
4441 if blocks is NULL. */
4443 static void
4444 clear_log_links (sbitmap blocks)
4446 rtx insn;
4447 int i;
4449 if (!blocks)
4451 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4452 if (INSN_P (insn))
4453 free_INSN_LIST_list (&LOG_LINKS (insn));
4455 else
4456 EXECUTE_IF_SET_IN_SBITMAP (blocks, 0, i,
4458 basic_block bb = BASIC_BLOCK (i);
4460 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb));
4461 insn = NEXT_INSN (insn))
4462 if (INSN_P (insn))
4463 free_INSN_LIST_list (&LOG_LINKS (insn));
4467 /* Given a register bitmap, turn on the bits in a HARD_REG_SET that
4468 correspond to the hard registers, if any, set in that map. This
4469 could be done far more efficiently by having all sorts of special-cases
4470 with moving single words, but probably isn't worth the trouble. */
4472 void
4473 reg_set_to_hard_reg_set (HARD_REG_SET *to, bitmap from)
4475 unsigned i;
4476 bitmap_iterator bi;
4478 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
4480 if (i >= FIRST_PSEUDO_REGISTER)
4481 return;
4482 SET_HARD_REG_BIT (*to, i);