2005-06-30 J. D. Johnston <jjohnst@us.ibm.com>
[official-gcc.git] / gcc / flow.c
blob763fbadcb5b7967796325a786be3e8cb17594d3b
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, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, 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->il.rtl->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,
508 bb->il.rtl->global_live_at_start))
510 if (dump_file)
512 fprintf (dump_file,
513 "live_at_start mismatch in bb %d, aborting\nNew:\n",
514 bb->index);
515 debug_bitmap_file (dump_file, new_live_at_start);
516 fputs ("Old:\n", dump_file);
517 dump_bb (bb, dump_file, 0);
519 fatal_error ("internal consistency failure");
522 else
524 unsigned i;
525 reg_set_iterator rsi;
527 /* Find the set of changed registers. */
528 XOR_REG_SET (new_live_at_start, bb->il.rtl->global_live_at_start);
530 EXECUTE_IF_SET_IN_REG_SET (new_live_at_start, 0, i, rsi)
532 /* No registers should die. */
533 if (REGNO_REG_SET_P (bb->il.rtl->global_live_at_start, i))
535 if (dump_file)
537 fprintf (dump_file,
538 "Register %d died unexpectedly.\n", i);
539 dump_bb (bb, dump_file, 0);
541 fatal_error ("internal consistency failure");
543 /* Verify that the now-live register is wider than word_mode. */
544 verify_wide_reg (i, bb);
549 /* Updates life information starting with the basic blocks set in BLOCKS.
550 If BLOCKS is null, consider it to be the universal set.
552 If EXTENT is UPDATE_LIFE_LOCAL, such as after splitting or peepholing,
553 we are only expecting local modifications to basic blocks. If we find
554 extra registers live at the beginning of a block, then we either killed
555 useful data, or we have a broken split that wants data not provided.
556 If we find registers removed from live_at_start, that means we have
557 a broken peephole that is killing a register it shouldn't.
559 ??? This is not true in one situation -- when a pre-reload splitter
560 generates subregs of a multi-word pseudo, current life analysis will
561 lose the kill. So we _can_ have a pseudo go live. How irritating.
563 It is also not true when a peephole decides that it doesn't need one
564 or more of the inputs.
566 Including PROP_REG_INFO does not properly refresh regs_ever_live
567 unless the caller resets it to zero. */
570 update_life_info (sbitmap blocks, enum update_life_extent extent,
571 int prop_flags)
573 regset tmp;
574 unsigned i;
575 int stabilized_prop_flags = prop_flags;
576 basic_block bb;
578 tmp = ALLOC_REG_SET (&reg_obstack);
579 ndead = 0;
581 if ((prop_flags & PROP_REG_INFO) && !reg_deaths)
582 reg_deaths = xcalloc (sizeof (*reg_deaths), max_regno);
584 timevar_push ((extent == UPDATE_LIFE_LOCAL || blocks)
585 ? TV_LIFE_UPDATE : TV_LIFE);
587 /* Changes to the CFG are only allowed when
588 doing a global update for the entire CFG. */
589 gcc_assert (!(prop_flags & PROP_ALLOW_CFG_CHANGES)
590 || (extent != UPDATE_LIFE_LOCAL && !blocks));
592 /* For a global update, we go through the relaxation process again. */
593 if (extent != UPDATE_LIFE_LOCAL)
595 for ( ; ; )
597 int changed = 0;
599 calculate_global_regs_live (blocks, blocks,
600 prop_flags & (PROP_SCAN_DEAD_CODE
601 | PROP_SCAN_DEAD_STORES
602 | PROP_ALLOW_CFG_CHANGES));
604 if ((prop_flags & (PROP_KILL_DEAD_CODE | PROP_ALLOW_CFG_CHANGES))
605 != (PROP_KILL_DEAD_CODE | PROP_ALLOW_CFG_CHANGES))
606 break;
608 /* Removing dead code may allow the CFG to be simplified which
609 in turn may allow for further dead code detection / removal. */
610 FOR_EACH_BB_REVERSE (bb)
612 COPY_REG_SET (tmp, bb->il.rtl->global_live_at_end);
613 changed |= propagate_block (bb, tmp, NULL, NULL,
614 prop_flags & (PROP_SCAN_DEAD_CODE
615 | PROP_SCAN_DEAD_STORES
616 | PROP_KILL_DEAD_CODE));
619 /* Don't pass PROP_SCAN_DEAD_CODE or PROP_KILL_DEAD_CODE to
620 subsequent propagate_block calls, since removing or acting as
621 removing dead code can affect global register liveness, which
622 is supposed to be finalized for this call after this loop. */
623 stabilized_prop_flags
624 &= ~(PROP_SCAN_DEAD_CODE | PROP_SCAN_DEAD_STORES
625 | PROP_KILL_DEAD_CODE);
627 if (! changed)
628 break;
630 /* We repeat regardless of what cleanup_cfg says. If there were
631 instructions deleted above, that might have been only a
632 partial improvement (see MAX_MEM_SET_LIST_LEN usage).
633 Further improvement may be possible. */
634 cleanup_cfg (CLEANUP_EXPENSIVE);
636 /* Zap the life information from the last round. If we don't
637 do this, we can wind up with registers that no longer appear
638 in the code being marked live at entry. */
639 FOR_EACH_BB (bb)
641 CLEAR_REG_SET (bb->il.rtl->global_live_at_start);
642 CLEAR_REG_SET (bb->il.rtl->global_live_at_end);
646 /* If asked, remove notes from the blocks we'll update. */
647 if (extent == UPDATE_LIFE_GLOBAL_RM_NOTES)
648 count_or_remove_death_notes (blocks, 1);
651 /* Clear log links in case we are asked to (re)compute them. */
652 if (prop_flags & PROP_LOG_LINKS)
653 clear_log_links (blocks);
655 if (blocks)
657 sbitmap_iterator sbi;
659 EXECUTE_IF_SET_IN_SBITMAP (blocks, 0, i, sbi)
661 bb = BASIC_BLOCK (i);
663 COPY_REG_SET (tmp, bb->il.rtl->global_live_at_end);
664 propagate_block (bb, tmp, NULL, NULL, stabilized_prop_flags);
666 if (extent == UPDATE_LIFE_LOCAL)
667 verify_local_live_at_start (tmp, bb);
670 else
672 FOR_EACH_BB_REVERSE (bb)
674 COPY_REG_SET (tmp, bb->il.rtl->global_live_at_end);
676 propagate_block (bb, tmp, NULL, NULL, stabilized_prop_flags);
678 if (extent == UPDATE_LIFE_LOCAL)
679 verify_local_live_at_start (tmp, bb);
683 FREE_REG_SET (tmp);
685 if (prop_flags & PROP_REG_INFO)
687 reg_set_iterator rsi;
689 /* The only pseudos that are live at the beginning of the function
690 are those that were not set anywhere in the function. local-alloc
691 doesn't know how to handle these correctly, so mark them as not
692 local to any one basic block. */
693 EXECUTE_IF_SET_IN_REG_SET (ENTRY_BLOCK_PTR->il.rtl->global_live_at_end,
694 FIRST_PSEUDO_REGISTER, i, rsi)
695 REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL;
697 /* We have a problem with any pseudoreg that lives across the setjmp.
698 ANSI says that if a user variable does not change in value between
699 the setjmp and the longjmp, then the longjmp preserves it. This
700 includes longjmp from a place where the pseudo appears dead.
701 (In principle, the value still exists if it is in scope.)
702 If the pseudo goes in a hard reg, some other value may occupy
703 that hard reg where this pseudo is dead, thus clobbering the pseudo.
704 Conclusion: such a pseudo must not go in a hard reg. */
705 EXECUTE_IF_SET_IN_REG_SET (regs_live_at_setjmp,
706 FIRST_PSEUDO_REGISTER, i, rsi)
708 if (regno_reg_rtx[i] != 0)
710 REG_LIVE_LENGTH (i) = -1;
711 REG_BASIC_BLOCK (i) = REG_BLOCK_UNKNOWN;
715 if (reg_deaths)
717 free (reg_deaths);
718 reg_deaths = NULL;
720 timevar_pop ((extent == UPDATE_LIFE_LOCAL || blocks)
721 ? TV_LIFE_UPDATE : TV_LIFE);
722 if (ndead && dump_file)
723 fprintf (dump_file, "deleted %i dead insns\n", ndead);
724 return ndead;
727 /* Update life information in all blocks where BB_DIRTY is set. */
730 update_life_info_in_dirty_blocks (enum update_life_extent extent, int prop_flags)
732 sbitmap update_life_blocks = sbitmap_alloc (last_basic_block);
733 int n = 0;
734 basic_block bb;
735 int retval = 0;
737 sbitmap_zero (update_life_blocks);
738 FOR_EACH_BB (bb)
740 if (bb->flags & BB_DIRTY)
742 SET_BIT (update_life_blocks, bb->index);
743 n++;
747 if (n)
748 retval = update_life_info (update_life_blocks, extent, prop_flags);
750 sbitmap_free (update_life_blocks);
751 return retval;
754 /* Free the variables allocated by find_basic_blocks. */
756 void
757 free_basic_block_vars (void)
759 if (basic_block_info)
761 clear_edges ();
762 basic_block_info = NULL;
764 n_basic_blocks = 0;
765 last_basic_block = 0;
766 n_edges = 0;
768 label_to_block_map = NULL;
770 ENTRY_BLOCK_PTR->aux = NULL;
771 ENTRY_BLOCK_PTR->il.rtl->global_live_at_end = NULL;
772 EXIT_BLOCK_PTR->aux = NULL;
773 EXIT_BLOCK_PTR->il.rtl->global_live_at_start = NULL;
776 /* Delete any insns that copy a register to itself. */
779 delete_noop_moves (void)
781 rtx insn, next;
782 basic_block bb;
783 int nnoops = 0;
785 FOR_EACH_BB (bb)
787 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
789 next = NEXT_INSN (insn);
790 if (INSN_P (insn) && noop_move_p (insn))
792 rtx note;
794 /* If we're about to remove the first insn of a libcall
795 then move the libcall note to the next real insn and
796 update the retval note. */
797 if ((note = find_reg_note (insn, REG_LIBCALL, NULL_RTX))
798 && XEXP (note, 0) != insn)
800 rtx new_libcall_insn = next_real_insn (insn);
801 rtx retval_note = find_reg_note (XEXP (note, 0),
802 REG_RETVAL, NULL_RTX);
803 REG_NOTES (new_libcall_insn)
804 = gen_rtx_INSN_LIST (REG_LIBCALL, XEXP (note, 0),
805 REG_NOTES (new_libcall_insn));
806 XEXP (retval_note, 0) = new_libcall_insn;
809 delete_insn_and_edges (insn);
810 nnoops++;
814 if (nnoops && dump_file)
815 fprintf (dump_file, "deleted %i noop moves", nnoops);
816 return nnoops;
819 /* Delete any jump tables never referenced. We can't delete them at the
820 time of removing tablejump insn as they are referenced by the preceding
821 insns computing the destination, so we delay deleting and garbagecollect
822 them once life information is computed. */
823 void
824 delete_dead_jumptables (void)
826 basic_block bb;
828 /* A dead jump table does not belong to any basic block. Scan insns
829 between two adjacent basic blocks. */
830 FOR_EACH_BB (bb)
832 rtx insn, next;
834 for (insn = NEXT_INSN (BB_END (bb));
835 insn && !NOTE_INSN_BASIC_BLOCK_P (insn);
836 insn = next)
838 next = NEXT_INSN (insn);
839 if (LABEL_P (insn)
840 && LABEL_NUSES (insn) == LABEL_PRESERVE_P (insn)
841 && JUMP_P (next)
842 && (GET_CODE (PATTERN (next)) == ADDR_VEC
843 || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC))
845 rtx label = insn, jump = next;
847 if (dump_file)
848 fprintf (dump_file, "Dead jumptable %i removed\n",
849 INSN_UID (insn));
851 next = NEXT_INSN (next);
852 delete_insn (jump);
853 delete_insn (label);
859 /* Determine if the stack pointer is constant over the life of the function.
860 Only useful before prologues have been emitted. */
862 static void
863 notice_stack_pointer_modification_1 (rtx x, rtx pat ATTRIBUTE_UNUSED,
864 void *data ATTRIBUTE_UNUSED)
866 if (x == stack_pointer_rtx
867 /* The stack pointer is only modified indirectly as the result
868 of a push until later in flow. See the comments in rtl.texi
869 regarding Embedded Side-Effects on Addresses. */
870 || (MEM_P (x)
871 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_AUTOINC
872 && XEXP (XEXP (x, 0), 0) == stack_pointer_rtx))
873 current_function_sp_is_unchanging = 0;
876 static void
877 notice_stack_pointer_modification (void)
879 basic_block bb;
880 rtx insn;
882 /* Assume that the stack pointer is unchanging if alloca hasn't
883 been used. */
884 current_function_sp_is_unchanging = !current_function_calls_alloca;
885 if (! current_function_sp_is_unchanging)
886 return;
888 FOR_EACH_BB (bb)
889 FOR_BB_INSNS (bb, insn)
891 if (INSN_P (insn))
893 /* Check if insn modifies the stack pointer. */
894 note_stores (PATTERN (insn),
895 notice_stack_pointer_modification_1,
896 NULL);
897 if (! current_function_sp_is_unchanging)
898 return;
903 /* Mark a register in SET. Hard registers in large modes get all
904 of their component registers set as well. */
906 static void
907 mark_reg (rtx reg, void *xset)
909 regset set = (regset) xset;
910 int regno = REGNO (reg);
912 gcc_assert (GET_MODE (reg) != BLKmode);
914 SET_REGNO_REG_SET (set, regno);
915 if (regno < FIRST_PSEUDO_REGISTER)
917 int n = hard_regno_nregs[regno][GET_MODE (reg)];
918 while (--n > 0)
919 SET_REGNO_REG_SET (set, regno + n);
923 /* Mark those regs which are needed at the end of the function as live
924 at the end of the last basic block. */
926 static void
927 mark_regs_live_at_end (regset set)
929 unsigned int i;
931 /* If exiting needs the right stack value, consider the stack pointer
932 live at the end of the function. */
933 if ((HAVE_epilogue && epilogue_completed)
934 || ! EXIT_IGNORE_STACK
935 || (! FRAME_POINTER_REQUIRED
936 && ! current_function_calls_alloca
937 && flag_omit_frame_pointer)
938 || current_function_sp_is_unchanging)
940 SET_REGNO_REG_SET (set, STACK_POINTER_REGNUM);
943 /* Mark the frame pointer if needed at the end of the function. If
944 we end up eliminating it, it will be removed from the live list
945 of each basic block by reload. */
947 if (! reload_completed || frame_pointer_needed)
949 SET_REGNO_REG_SET (set, FRAME_POINTER_REGNUM);
950 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
951 /* If they are different, also mark the hard frame pointer as live. */
952 if (! LOCAL_REGNO (HARD_FRAME_POINTER_REGNUM))
953 SET_REGNO_REG_SET (set, HARD_FRAME_POINTER_REGNUM);
954 #endif
957 #ifndef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
958 /* Many architectures have a GP register even without flag_pic.
959 Assume the pic register is not in use, or will be handled by
960 other means, if it is not fixed. */
961 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
962 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
963 SET_REGNO_REG_SET (set, PIC_OFFSET_TABLE_REGNUM);
964 #endif
966 /* Mark all global registers, and all registers used by the epilogue
967 as being live at the end of the function since they may be
968 referenced by our caller. */
969 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
970 if (global_regs[i] || EPILOGUE_USES (i))
971 SET_REGNO_REG_SET (set, i);
973 if (HAVE_epilogue && epilogue_completed)
975 /* Mark all call-saved registers that we actually used. */
976 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
977 if (regs_ever_live[i] && ! LOCAL_REGNO (i)
978 && ! TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
979 SET_REGNO_REG_SET (set, i);
982 #ifdef EH_RETURN_DATA_REGNO
983 /* Mark the registers that will contain data for the handler. */
984 if (reload_completed && current_function_calls_eh_return)
985 for (i = 0; ; ++i)
987 unsigned regno = EH_RETURN_DATA_REGNO(i);
988 if (regno == INVALID_REGNUM)
989 break;
990 SET_REGNO_REG_SET (set, regno);
992 #endif
993 #ifdef EH_RETURN_STACKADJ_RTX
994 if ((! HAVE_epilogue || ! epilogue_completed)
995 && current_function_calls_eh_return)
997 rtx tmp = EH_RETURN_STACKADJ_RTX;
998 if (tmp && REG_P (tmp))
999 mark_reg (tmp, set);
1001 #endif
1002 #ifdef EH_RETURN_HANDLER_RTX
1003 if ((! HAVE_epilogue || ! epilogue_completed)
1004 && current_function_calls_eh_return)
1006 rtx tmp = EH_RETURN_HANDLER_RTX;
1007 if (tmp && REG_P (tmp))
1008 mark_reg (tmp, set);
1010 #endif
1012 /* Mark function return value. */
1013 diddle_return_value (mark_reg, set);
1016 /* Propagate global life info around the graph of basic blocks. Begin
1017 considering blocks with their corresponding bit set in BLOCKS_IN.
1018 If BLOCKS_IN is null, consider it the universal set.
1020 BLOCKS_OUT is set for every block that was changed. */
1022 static void
1023 calculate_global_regs_live (sbitmap blocks_in, sbitmap blocks_out, int flags)
1025 basic_block *queue, *qhead, *qtail, *qend, bb;
1026 regset tmp, new_live_at_end, invalidated_by_call;
1027 regset registers_made_dead;
1028 bool failure_strategy_required = false;
1029 int *block_accesses;
1031 /* The registers that are modified within this in block. */
1032 regset *local_sets;
1034 /* The registers that are conditionally modified within this block.
1035 In other words, regs that are set only as part of a COND_EXEC. */
1036 regset *cond_local_sets;
1038 unsigned int i;
1040 /* Some passes used to forget clear aux field of basic block causing
1041 sick behavior here. */
1042 #ifdef ENABLE_CHECKING
1043 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1044 gcc_assert (!bb->aux);
1045 #endif
1047 tmp = ALLOC_REG_SET (&reg_obstack);
1048 new_live_at_end = ALLOC_REG_SET (&reg_obstack);
1049 invalidated_by_call = ALLOC_REG_SET (&reg_obstack);
1050 registers_made_dead = ALLOC_REG_SET (&reg_obstack);
1052 /* Inconveniently, this is only readily available in hard reg set form. */
1053 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1054 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1055 SET_REGNO_REG_SET (invalidated_by_call, i);
1057 /* Allocate space for the sets of local properties. */
1058 local_sets = xcalloc (last_basic_block - (INVALID_BLOCK + 1),
1059 sizeof (regset));
1060 cond_local_sets = xcalloc (last_basic_block - (INVALID_BLOCK + 1),
1061 sizeof (regset));
1063 /* Create a worklist. Allocate an extra slot for ENTRY_BLOCK, and one
1064 because the `head == tail' style test for an empty queue doesn't
1065 work with a full queue. */
1066 queue = xmalloc ((n_basic_blocks - (INVALID_BLOCK + 1)) * sizeof (*queue));
1067 qtail = queue;
1068 qhead = qend = queue + n_basic_blocks - (INVALID_BLOCK + 1);
1070 /* Queue the blocks set in the initial mask. Do this in reverse block
1071 number order so that we are more likely for the first round to do
1072 useful work. We use AUX non-null to flag that the block is queued. */
1073 if (blocks_in)
1075 FOR_EACH_BB (bb)
1076 if (TEST_BIT (blocks_in, bb->index))
1078 *--qhead = bb;
1079 bb->aux = bb;
1082 else
1084 FOR_EACH_BB (bb)
1086 *--qhead = bb;
1087 bb->aux = bb;
1091 block_accesses = xcalloc (last_basic_block, sizeof (int));
1093 /* We clean aux when we remove the initially-enqueued bbs, but we
1094 don't enqueue ENTRY and EXIT initially, so clean them upfront and
1095 unconditionally. */
1096 ENTRY_BLOCK_PTR->aux = EXIT_BLOCK_PTR->aux = NULL;
1098 if (blocks_out)
1099 sbitmap_zero (blocks_out);
1101 /* We work through the queue until there are no more blocks. What
1102 is live at the end of this block is precisely the union of what
1103 is live at the beginning of all its successors. So, we set its
1104 GLOBAL_LIVE_AT_END field based on the GLOBAL_LIVE_AT_START field
1105 for its successors. Then, we compute GLOBAL_LIVE_AT_START for
1106 this block by walking through the instructions in this block in
1107 reverse order and updating as we go. If that changed
1108 GLOBAL_LIVE_AT_START, we add the predecessors of the block to the
1109 queue; they will now need to recalculate GLOBAL_LIVE_AT_END.
1111 We are guaranteed to terminate, because GLOBAL_LIVE_AT_START
1112 never shrinks. If a register appears in GLOBAL_LIVE_AT_START, it
1113 must either be live at the end of the block, or used within the
1114 block. In the latter case, it will certainly never disappear
1115 from GLOBAL_LIVE_AT_START. In the former case, the register
1116 could go away only if it disappeared from GLOBAL_LIVE_AT_START
1117 for one of the successor blocks. By induction, that cannot
1118 occur.
1120 ??? This reasoning doesn't work if we start from non-empty initial
1121 GLOBAL_LIVE_AT_START sets. And there are actually two problems:
1122 1) Updating may not terminate (endless oscillation).
1123 2) Even if it does (and it usually does), the resulting information
1124 may be inaccurate. Consider for example the following case:
1126 a = ...;
1127 while (...) {...} -- 'a' not mentioned at all
1128 ... = a;
1130 If the use of 'a' is deleted between two calculations of liveness
1131 information and the initial sets are not cleared, the information
1132 about a's liveness will get stuck inside the loop and the set will
1133 appear not to be dead.
1135 We do not attempt to solve 2) -- the information is conservatively
1136 correct (i.e. we never claim that something live is dead) and the
1137 amount of optimization opportunities missed due to this problem is
1138 not significant.
1140 1) is more serious. In order to fix it, we monitor the number of times
1141 each block is processed. Once one of the blocks has been processed more
1142 times than the maximum number of rounds, we use the following strategy:
1143 When a register disappears from one of the sets, we add it to a MAKE_DEAD
1144 set, remove all registers in this set from all GLOBAL_LIVE_AT_* sets and
1145 add the blocks with changed sets into the queue. Thus we are guaranteed
1146 to terminate (the worst case corresponds to all registers in MADE_DEAD,
1147 in which case the original reasoning above is valid), but in general we
1148 only fix up a few offending registers.
1150 The maximum number of rounds for computing liveness is the largest of
1151 MAX_LIVENESS_ROUNDS and the latest loop depth count for this function. */
1153 while (qhead != qtail)
1155 int rescan, changed;
1156 basic_block bb;
1157 edge e;
1158 edge_iterator ei;
1160 bb = *qhead++;
1161 if (qhead == qend)
1162 qhead = queue;
1163 bb->aux = NULL;
1165 /* Should we start using the failure strategy? */
1166 if (bb != ENTRY_BLOCK_PTR)
1168 int max_liveness_rounds =
1169 MAX (MAX_LIVENESS_ROUNDS, cfun->max_loop_depth);
1171 block_accesses[bb->index]++;
1172 if (block_accesses[bb->index] > max_liveness_rounds)
1173 failure_strategy_required = true;
1176 /* Begin by propagating live_at_start from the successor blocks. */
1177 CLEAR_REG_SET (new_live_at_end);
1179 if (EDGE_COUNT (bb->succs) > 0)
1180 FOR_EACH_EDGE (e, ei, bb->succs)
1182 basic_block sb = e->dest;
1184 /* Call-clobbered registers die across exception and
1185 call edges. */
1186 /* ??? Abnormal call edges ignored for the moment, as this gets
1187 confused by sibling call edges, which crashes reg-stack. */
1188 if (e->flags & EDGE_EH)
1189 bitmap_ior_and_compl_into (new_live_at_end,
1190 sb->il.rtl->global_live_at_start,
1191 invalidated_by_call);
1192 else
1193 IOR_REG_SET (new_live_at_end, sb->il.rtl->global_live_at_start);
1195 /* If a target saves one register in another (instead of on
1196 the stack) the save register will need to be live for EH. */
1197 if (e->flags & EDGE_EH)
1198 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1199 if (EH_USES (i))
1200 SET_REGNO_REG_SET (new_live_at_end, i);
1202 else
1204 /* This might be a noreturn function that throws. And
1205 even if it isn't, getting the unwind info right helps
1206 debugging. */
1207 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1208 if (EH_USES (i))
1209 SET_REGNO_REG_SET (new_live_at_end, i);
1212 /* The all-important stack pointer must always be live. */
1213 SET_REGNO_REG_SET (new_live_at_end, STACK_POINTER_REGNUM);
1215 /* Before reload, there are a few registers that must be forced
1216 live everywhere -- which might not already be the case for
1217 blocks within infinite loops. */
1218 if (! reload_completed)
1220 /* Any reference to any pseudo before reload is a potential
1221 reference of the frame pointer. */
1222 SET_REGNO_REG_SET (new_live_at_end, FRAME_POINTER_REGNUM);
1224 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1225 /* Pseudos with argument area equivalences may require
1226 reloading via the argument pointer. */
1227 if (fixed_regs[ARG_POINTER_REGNUM])
1228 SET_REGNO_REG_SET (new_live_at_end, ARG_POINTER_REGNUM);
1229 #endif
1231 /* Any constant, or pseudo with constant equivalences, may
1232 require reloading from memory using the pic register. */
1233 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1234 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1235 SET_REGNO_REG_SET (new_live_at_end, PIC_OFFSET_TABLE_REGNUM);
1238 if (bb == ENTRY_BLOCK_PTR)
1240 COPY_REG_SET (bb->il.rtl->global_live_at_end, new_live_at_end);
1241 continue;
1244 /* On our first pass through this block, we'll go ahead and continue.
1245 Recognize first pass by checking if local_set is NULL for this
1246 basic block. On subsequent passes, we get to skip out early if
1247 live_at_end wouldn't have changed. */
1249 if (local_sets[bb->index - (INVALID_BLOCK + 1)] == NULL)
1251 local_sets[bb->index - (INVALID_BLOCK + 1)]
1252 = ALLOC_REG_SET (&reg_obstack);
1253 cond_local_sets[bb->index - (INVALID_BLOCK + 1)]
1254 = ALLOC_REG_SET (&reg_obstack);
1255 rescan = 1;
1257 else
1259 /* If any bits were removed from live_at_end, we'll have to
1260 rescan the block. This wouldn't be necessary if we had
1261 precalculated local_live, however with PROP_SCAN_DEAD_CODE
1262 local_live is really dependent on live_at_end. */
1263 rescan = bitmap_intersect_compl_p (bb->il.rtl->global_live_at_end,
1264 new_live_at_end);
1266 if (!rescan)
1268 regset cond_local_set;
1270 /* If any of the registers in the new live_at_end set are
1271 conditionally set in this basic block, we must rescan.
1272 This is because conditional lifetimes at the end of the
1273 block do not just take the live_at_end set into
1274 account, but also the liveness at the start of each
1275 successor block. We can miss changes in those sets if
1276 we only compare the new live_at_end against the
1277 previous one. */
1278 cond_local_set = cond_local_sets[bb->index - (INVALID_BLOCK + 1)];
1279 rescan = bitmap_intersect_p (new_live_at_end, cond_local_set);
1282 if (!rescan)
1284 regset local_set;
1286 /* Find the set of changed bits. Take this opportunity
1287 to notice that this set is empty and early out. */
1288 bitmap_xor (tmp, bb->il.rtl->global_live_at_end, new_live_at_end);
1289 if (bitmap_empty_p (tmp))
1290 continue;
1292 /* If any of the changed bits overlap with local_sets[bb],
1293 we'll have to rescan the block. */
1294 local_set = local_sets[bb->index - (INVALID_BLOCK + 1)];
1295 rescan = bitmap_intersect_p (tmp, local_set);
1299 /* Let our caller know that BB changed enough to require its
1300 death notes updated. */
1301 if (blocks_out)
1302 SET_BIT (blocks_out, bb->index);
1304 if (! rescan)
1306 /* Add to live_at_start the set of all registers in
1307 new_live_at_end that aren't in the old live_at_end. */
1309 changed = bitmap_ior_and_compl_into (bb->il.rtl->global_live_at_start,
1310 new_live_at_end,
1311 bb->il.rtl->global_live_at_end);
1312 COPY_REG_SET (bb->il.rtl->global_live_at_end, new_live_at_end);
1313 if (! changed)
1314 continue;
1316 else
1318 COPY_REG_SET (bb->il.rtl->global_live_at_end, new_live_at_end);
1320 /* Rescan the block insn by insn to turn (a copy of) live_at_end
1321 into live_at_start. */
1322 propagate_block (bb, new_live_at_end,
1323 local_sets[bb->index - (INVALID_BLOCK + 1)],
1324 cond_local_sets[bb->index - (INVALID_BLOCK + 1)],
1325 flags);
1327 /* If live_at start didn't change, no need to go farther. */
1328 if (REG_SET_EQUAL_P (bb->il.rtl->global_live_at_start,
1329 new_live_at_end))
1330 continue;
1332 if (failure_strategy_required)
1334 /* Get the list of registers that were removed from the
1335 bb->global_live_at_start set. */
1336 bitmap_and_compl (tmp, bb->il.rtl->global_live_at_start,
1337 new_live_at_end);
1338 if (!bitmap_empty_p (tmp))
1340 bool pbb_changed;
1341 basic_block pbb;
1343 /* It should not happen that one of registers we have
1344 removed last time is disappears again before any other
1345 register does. */
1346 pbb_changed = bitmap_ior_into (registers_made_dead, tmp);
1347 gcc_assert (pbb_changed);
1349 /* Now remove the registers from all sets. */
1350 FOR_EACH_BB (pbb)
1352 pbb_changed = false;
1354 pbb_changed
1355 |= bitmap_and_compl_into
1356 (pbb->il.rtl->global_live_at_start,
1357 registers_made_dead);
1358 pbb_changed
1359 |= bitmap_and_compl_into
1360 (pbb->il.rtl->global_live_at_end,
1361 registers_made_dead);
1362 if (!pbb_changed)
1363 continue;
1365 /* Note the (possible) change. */
1366 if (blocks_out)
1367 SET_BIT (blocks_out, pbb->index);
1369 /* Makes sure to really rescan the block. */
1370 if (local_sets[pbb->index - (INVALID_BLOCK + 1)])
1372 FREE_REG_SET (local_sets[pbb->index - (INVALID_BLOCK + 1)]);
1373 FREE_REG_SET (cond_local_sets[pbb->index - (INVALID_BLOCK + 1)]);
1374 local_sets[pbb->index - (INVALID_BLOCK + 1)] = 0;
1377 /* Add it to the queue. */
1378 if (pbb->aux == NULL)
1380 *qtail++ = pbb;
1381 if (qtail == qend)
1382 qtail = queue;
1383 pbb->aux = pbb;
1386 continue;
1388 } /* end of failure_strategy_required */
1390 COPY_REG_SET (bb->il.rtl->global_live_at_start, new_live_at_end);
1393 /* Queue all predecessors of BB so that we may re-examine
1394 their live_at_end. */
1395 FOR_EACH_EDGE (e, ei, bb->preds)
1397 basic_block pb = e->src;
1398 if (pb->aux == NULL)
1400 *qtail++ = pb;
1401 if (qtail == qend)
1402 qtail = queue;
1403 pb->aux = pb;
1408 FREE_REG_SET (tmp);
1409 FREE_REG_SET (new_live_at_end);
1410 FREE_REG_SET (invalidated_by_call);
1411 FREE_REG_SET (registers_made_dead);
1413 if (blocks_out)
1415 sbitmap_iterator sbi;
1417 EXECUTE_IF_SET_IN_SBITMAP (blocks_out, 0, i, sbi)
1419 basic_block bb = BASIC_BLOCK (i);
1420 FREE_REG_SET (local_sets[bb->index - (INVALID_BLOCK + 1)]);
1421 FREE_REG_SET (cond_local_sets[bb->index - (INVALID_BLOCK + 1)]);
1424 else
1426 FOR_EACH_BB (bb)
1428 FREE_REG_SET (local_sets[bb->index - (INVALID_BLOCK + 1)]);
1429 FREE_REG_SET (cond_local_sets[bb->index - (INVALID_BLOCK + 1)]);
1433 free (block_accesses);
1434 free (queue);
1435 free (cond_local_sets);
1436 free (local_sets);
1440 /* This structure is used to pass parameters to and from the
1441 the function find_regno_partial(). It is used to pass in the
1442 register number we are looking, as well as to return any rtx
1443 we find. */
1445 typedef struct {
1446 unsigned regno_to_find;
1447 rtx retval;
1448 } find_regno_partial_param;
1451 /* Find the rtx for the reg numbers specified in 'data' if it is
1452 part of an expression which only uses part of the register. Return
1453 it in the structure passed in. */
1454 static int
1455 find_regno_partial (rtx *ptr, void *data)
1457 find_regno_partial_param *param = (find_regno_partial_param *)data;
1458 unsigned reg = param->regno_to_find;
1459 param->retval = NULL_RTX;
1461 if (*ptr == NULL_RTX)
1462 return 0;
1464 switch (GET_CODE (*ptr))
1466 case ZERO_EXTRACT:
1467 case SIGN_EXTRACT:
1468 case STRICT_LOW_PART:
1469 if (REG_P (XEXP (*ptr, 0)) && REGNO (XEXP (*ptr, 0)) == reg)
1471 param->retval = XEXP (*ptr, 0);
1472 return 1;
1474 break;
1476 case SUBREG:
1477 if (REG_P (SUBREG_REG (*ptr))
1478 && REGNO (SUBREG_REG (*ptr)) == reg)
1480 param->retval = SUBREG_REG (*ptr);
1481 return 1;
1483 break;
1485 default:
1486 break;
1489 return 0;
1492 /* Process all immediate successors of the entry block looking for pseudo
1493 registers which are live on entry. Find all of those whose first
1494 instance is a partial register reference of some kind, and initialize
1495 them to 0 after the entry block. This will prevent bit sets within
1496 registers whose value is unknown, and may contain some kind of sticky
1497 bits we don't want. */
1500 initialize_uninitialized_subregs (void)
1502 rtx insn;
1503 edge e;
1504 unsigned reg, did_something = 0;
1505 find_regno_partial_param param;
1506 edge_iterator ei;
1508 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
1510 basic_block bb = e->dest;
1511 regset map = bb->il.rtl->global_live_at_start;
1512 reg_set_iterator rsi;
1514 EXECUTE_IF_SET_IN_REG_SET (map, FIRST_PSEUDO_REGISTER, reg, rsi)
1516 int uid = REGNO_FIRST_UID (reg);
1517 rtx i;
1519 /* Find an insn which mentions the register we are looking for.
1520 Its preferable to have an instance of the register's rtl since
1521 there may be various flags set which we need to duplicate.
1522 If we can't find it, its probably an automatic whose initial
1523 value doesn't matter, or hopefully something we don't care about. */
1524 for (i = get_insns (); i && INSN_UID (i) != uid; i = NEXT_INSN (i))
1526 if (i != NULL_RTX)
1528 /* Found the insn, now get the REG rtx, if we can. */
1529 param.regno_to_find = reg;
1530 for_each_rtx (&i, find_regno_partial, &param);
1531 if (param.retval != NULL_RTX)
1533 start_sequence ();
1534 emit_move_insn (param.retval,
1535 CONST0_RTX (GET_MODE (param.retval)));
1536 insn = get_insns ();
1537 end_sequence ();
1538 insert_insn_on_edge (insn, e);
1539 did_something = 1;
1545 if (did_something)
1546 commit_edge_insertions ();
1547 return did_something;
1551 /* Subroutines of life analysis. */
1553 /* Allocate the permanent data structures that represent the results
1554 of life analysis. */
1556 static void
1557 allocate_bb_life_data (void)
1559 basic_block bb;
1561 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1563 bb->il.rtl->global_live_at_start = ALLOC_REG_SET (&reg_obstack);
1564 bb->il.rtl->global_live_at_end = ALLOC_REG_SET (&reg_obstack);
1567 regs_live_at_setjmp = ALLOC_REG_SET (&reg_obstack);
1570 void
1571 allocate_reg_life_data (void)
1573 int i;
1575 max_regno = max_reg_num ();
1576 gcc_assert (!reg_deaths);
1577 reg_deaths = xcalloc (sizeof (*reg_deaths), max_regno);
1579 /* Recalculate the register space, in case it has grown. Old style
1580 vector oriented regsets would set regset_{size,bytes} here also. */
1581 allocate_reg_info (max_regno, FALSE, FALSE);
1583 /* Reset all the data we'll collect in propagate_block and its
1584 subroutines. */
1585 for (i = 0; i < max_regno; i++)
1587 REG_N_SETS (i) = 0;
1588 REG_N_REFS (i) = 0;
1589 REG_N_DEATHS (i) = 0;
1590 REG_N_CALLS_CROSSED (i) = 0;
1591 REG_LIVE_LENGTH (i) = 0;
1592 REG_FREQ (i) = 0;
1593 REG_BASIC_BLOCK (i) = REG_BLOCK_UNKNOWN;
1597 /* Delete dead instructions for propagate_block. */
1599 static void
1600 propagate_block_delete_insn (rtx insn)
1602 rtx inote = find_reg_note (insn, REG_LABEL, NULL_RTX);
1604 /* If the insn referred to a label, and that label was attached to
1605 an ADDR_VEC, it's safe to delete the ADDR_VEC. In fact, it's
1606 pretty much mandatory to delete it, because the ADDR_VEC may be
1607 referencing labels that no longer exist.
1609 INSN may reference a deleted label, particularly when a jump
1610 table has been optimized into a direct jump. There's no
1611 real good way to fix up the reference to the deleted label
1612 when the label is deleted, so we just allow it here. */
1614 if (inote && LABEL_P (inote))
1616 rtx label = XEXP (inote, 0);
1617 rtx next;
1619 /* The label may be forced if it has been put in the constant
1620 pool. If that is the only use we must discard the table
1621 jump following it, but not the label itself. */
1622 if (LABEL_NUSES (label) == 1 + LABEL_PRESERVE_P (label)
1623 && (next = next_nonnote_insn (label)) != NULL
1624 && JUMP_P (next)
1625 && (GET_CODE (PATTERN (next)) == ADDR_VEC
1626 || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC))
1628 rtx pat = PATTERN (next);
1629 int diff_vec_p = GET_CODE (pat) == ADDR_DIFF_VEC;
1630 int len = XVECLEN (pat, diff_vec_p);
1631 int i;
1633 for (i = 0; i < len; i++)
1634 LABEL_NUSES (XEXP (XVECEXP (pat, diff_vec_p, i), 0))--;
1636 delete_insn_and_edges (next);
1637 ndead++;
1641 delete_insn_and_edges (insn);
1642 ndead++;
1645 /* Delete dead libcalls for propagate_block. Return the insn
1646 before the libcall. */
1648 static rtx
1649 propagate_block_delete_libcall (rtx insn, rtx note)
1651 rtx first = XEXP (note, 0);
1652 rtx before = PREV_INSN (first);
1654 delete_insn_chain_and_edges (first, insn);
1655 ndead++;
1656 return before;
1659 /* Update the life-status of regs for one insn. Return the previous insn. */
1662 propagate_one_insn (struct propagate_block_info *pbi, rtx insn)
1664 rtx prev = PREV_INSN (insn);
1665 int flags = pbi->flags;
1666 int insn_is_dead = 0;
1667 int libcall_is_dead = 0;
1668 rtx note;
1669 unsigned i;
1671 if (! INSN_P (insn))
1672 return prev;
1674 note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
1675 if (flags & PROP_SCAN_DEAD_CODE)
1677 insn_is_dead = insn_dead_p (pbi, PATTERN (insn), 0, REG_NOTES (insn));
1678 libcall_is_dead = (insn_is_dead && note != 0
1679 && libcall_dead_p (pbi, note, insn));
1682 /* If an instruction consists of just dead store(s) on final pass,
1683 delete it. */
1684 if ((flags & PROP_KILL_DEAD_CODE) && insn_is_dead)
1686 /* If we're trying to delete a prologue or epilogue instruction
1687 that isn't flagged as possibly being dead, something is wrong.
1688 But if we are keeping the stack pointer depressed, we might well
1689 be deleting insns that are used to compute the amount to update
1690 it by, so they are fine. */
1691 if (reload_completed
1692 && !(TREE_CODE (TREE_TYPE (current_function_decl)) == FUNCTION_TYPE
1693 && (TYPE_RETURNS_STACK_DEPRESSED
1694 (TREE_TYPE (current_function_decl))))
1695 && (((HAVE_epilogue || HAVE_prologue)
1696 && prologue_epilogue_contains (insn))
1697 || (HAVE_sibcall_epilogue
1698 && sibcall_epilogue_contains (insn)))
1699 && find_reg_note (insn, REG_MAYBE_DEAD, NULL_RTX) == 0)
1700 fatal_insn ("Attempt to delete prologue/epilogue insn:", insn);
1702 /* Record sets. Do this even for dead instructions, since they
1703 would have killed the values if they hadn't been deleted. To
1704 be consistent, we also have to emit a clobber when we delete
1705 an insn that clobbers a live register. */
1706 pbi->flags |= PROP_DEAD_INSN;
1707 mark_set_regs (pbi, PATTERN (insn), insn);
1708 pbi->flags &= ~PROP_DEAD_INSN;
1710 /* CC0 is now known to be dead. Either this insn used it,
1711 in which case it doesn't anymore, or clobbered it,
1712 so the next insn can't use it. */
1713 pbi->cc0_live = 0;
1715 if (libcall_is_dead)
1716 prev = propagate_block_delete_libcall (insn, note);
1717 else
1720 /* If INSN contains a RETVAL note and is dead, but the libcall
1721 as a whole is not dead, then we want to remove INSN, but
1722 not the whole libcall sequence.
1724 However, we need to also remove the dangling REG_LIBCALL
1725 note so that we do not have mis-matched LIBCALL/RETVAL
1726 notes. In theory we could find a new location for the
1727 REG_RETVAL note, but it hardly seems worth the effort.
1729 NOTE at this point will be the RETVAL note if it exists. */
1730 if (note)
1732 rtx libcall_note;
1734 libcall_note
1735 = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
1736 remove_note (XEXP (note, 0), libcall_note);
1739 /* Similarly if INSN contains a LIBCALL note, remove the
1740 dangling REG_RETVAL note. */
1741 note = find_reg_note (insn, REG_LIBCALL, NULL_RTX);
1742 if (note)
1744 rtx retval_note;
1746 retval_note
1747 = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
1748 remove_note (XEXP (note, 0), retval_note);
1751 /* Now delete INSN. */
1752 propagate_block_delete_insn (insn);
1755 return prev;
1758 /* See if this is an increment or decrement that can be merged into
1759 a following memory address. */
1760 #ifdef AUTO_INC_DEC
1762 rtx x = single_set (insn);
1764 /* Does this instruction increment or decrement a register? */
1765 if ((flags & PROP_AUTOINC)
1766 && x != 0
1767 && REG_P (SET_DEST (x))
1768 && (GET_CODE (SET_SRC (x)) == PLUS
1769 || GET_CODE (SET_SRC (x)) == MINUS)
1770 && XEXP (SET_SRC (x), 0) == SET_DEST (x)
1771 && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
1772 /* Ok, look for a following memory ref we can combine with.
1773 If one is found, change the memory ref to a PRE_INC
1774 or PRE_DEC, cancel this insn, and return 1.
1775 Return 0 if nothing has been done. */
1776 && try_pre_increment_1 (pbi, insn))
1777 return prev;
1779 #endif /* AUTO_INC_DEC */
1781 CLEAR_REG_SET (pbi->new_set);
1783 /* If this is not the final pass, and this insn is copying the value of
1784 a library call and it's dead, don't scan the insns that perform the
1785 library call, so that the call's arguments are not marked live. */
1786 if (libcall_is_dead)
1788 /* Record the death of the dest reg. */
1789 mark_set_regs (pbi, PATTERN (insn), insn);
1791 insn = XEXP (note, 0);
1792 return PREV_INSN (insn);
1794 else if (GET_CODE (PATTERN (insn)) == SET
1795 && SET_DEST (PATTERN (insn)) == stack_pointer_rtx
1796 && GET_CODE (SET_SRC (PATTERN (insn))) == PLUS
1797 && XEXP (SET_SRC (PATTERN (insn)), 0) == stack_pointer_rtx
1798 && GET_CODE (XEXP (SET_SRC (PATTERN (insn)), 1)) == CONST_INT)
1800 /* We have an insn to pop a constant amount off the stack.
1801 (Such insns use PLUS regardless of the direction of the stack,
1802 and any insn to adjust the stack by a constant is always a pop
1803 or part of a push.)
1804 These insns, if not dead stores, have no effect on life, though
1805 they do have an effect on the memory stores we are tracking. */
1806 invalidate_mems_from_set (pbi, stack_pointer_rtx);
1807 /* Still, we need to update local_set, lest ifcvt.c:dead_or_predicable
1808 concludes that the stack pointer is not modified. */
1809 mark_set_regs (pbi, PATTERN (insn), insn);
1811 else
1813 /* Any regs live at the time of a call instruction must not go
1814 in a register clobbered by calls. Find all regs now live and
1815 record this for them. */
1817 if (CALL_P (insn) && (flags & PROP_REG_INFO))
1819 reg_set_iterator rsi;
1820 EXECUTE_IF_SET_IN_REG_SET (pbi->reg_live, 0, i, rsi)
1821 REG_N_CALLS_CROSSED (i)++;
1824 /* Record sets. Do this even for dead instructions, since they
1825 would have killed the values if they hadn't been deleted. */
1826 mark_set_regs (pbi, PATTERN (insn), insn);
1828 if (CALL_P (insn))
1830 regset live_at_end;
1831 bool sibcall_p;
1832 rtx note, cond;
1833 int i;
1835 cond = NULL_RTX;
1836 if (GET_CODE (PATTERN (insn)) == COND_EXEC)
1837 cond = COND_EXEC_TEST (PATTERN (insn));
1839 /* Non-constant calls clobber memory, constant calls do not
1840 clobber memory, though they may clobber outgoing arguments
1841 on the stack. */
1842 if (! CONST_OR_PURE_CALL_P (insn))
1844 free_EXPR_LIST_list (&pbi->mem_set_list);
1845 pbi->mem_set_list_len = 0;
1847 else
1848 invalidate_mems_from_set (pbi, stack_pointer_rtx);
1850 /* There may be extra registers to be clobbered. */
1851 for (note = CALL_INSN_FUNCTION_USAGE (insn);
1852 note;
1853 note = XEXP (note, 1))
1854 if (GET_CODE (XEXP (note, 0)) == CLOBBER)
1855 mark_set_1 (pbi, CLOBBER, XEXP (XEXP (note, 0), 0),
1856 cond, insn, pbi->flags);
1858 /* Calls change all call-used and global registers; sibcalls do not
1859 clobber anything that must be preserved at end-of-function,
1860 except for return values. */
1862 sibcall_p = SIBLING_CALL_P (insn);
1863 live_at_end = EXIT_BLOCK_PTR->il.rtl->global_live_at_start;
1864 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1865 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i)
1866 && ! (sibcall_p
1867 && REGNO_REG_SET_P (live_at_end, i)
1868 && ! refers_to_regno_p (i, i+1,
1869 current_function_return_rtx,
1870 (rtx *) 0)))
1872 enum rtx_code code = global_regs[i] ? SET : CLOBBER;
1873 /* We do not want REG_UNUSED notes for these registers. */
1874 mark_set_1 (pbi, code, regno_reg_rtx[i], cond, insn,
1875 pbi->flags & ~(PROP_DEATH_NOTES | PROP_REG_INFO));
1879 /* If an insn doesn't use CC0, it becomes dead since we assume
1880 that every insn clobbers it. So show it dead here;
1881 mark_used_regs will set it live if it is referenced. */
1882 pbi->cc0_live = 0;
1884 /* Record uses. */
1885 if (! insn_is_dead)
1886 mark_used_regs (pbi, PATTERN (insn), NULL_RTX, insn);
1888 /* Sometimes we may have inserted something before INSN (such as a move)
1889 when we make an auto-inc. So ensure we will scan those insns. */
1890 #ifdef AUTO_INC_DEC
1891 prev = PREV_INSN (insn);
1892 #endif
1894 if (! insn_is_dead && CALL_P (insn))
1896 int i;
1897 rtx note, cond;
1899 cond = NULL_RTX;
1900 if (GET_CODE (PATTERN (insn)) == COND_EXEC)
1901 cond = COND_EXEC_TEST (PATTERN (insn));
1903 /* Calls use their arguments, and may clobber memory which
1904 address involves some register. */
1905 for (note = CALL_INSN_FUNCTION_USAGE (insn);
1906 note;
1907 note = XEXP (note, 1))
1908 /* We find USE or CLOBBER entities in a FUNCTION_USAGE list: both
1909 of which mark_used_regs knows how to handle. */
1910 mark_used_regs (pbi, XEXP (XEXP (note, 0), 0), cond, insn);
1912 /* The stack ptr is used (honorarily) by a CALL insn. */
1913 if ((flags & PROP_REG_INFO)
1914 && !REGNO_REG_SET_P (pbi->reg_live, STACK_POINTER_REGNUM))
1915 reg_deaths[STACK_POINTER_REGNUM] = pbi->insn_num;
1916 SET_REGNO_REG_SET (pbi->reg_live, STACK_POINTER_REGNUM);
1918 /* Calls may also reference any of the global registers,
1919 so they are made live. */
1920 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1921 if (global_regs[i])
1922 mark_used_reg (pbi, regno_reg_rtx[i], cond, insn);
1926 pbi->insn_num++;
1928 return prev;
1931 /* Initialize a propagate_block_info struct for public consumption.
1932 Note that the structure itself is opaque to this file, but that
1933 the user can use the regsets provided here. */
1935 struct propagate_block_info *
1936 init_propagate_block_info (basic_block bb, regset live, regset local_set,
1937 regset cond_local_set, int flags)
1939 struct propagate_block_info *pbi = xmalloc (sizeof (*pbi));
1941 pbi->bb = bb;
1942 pbi->reg_live = live;
1943 pbi->mem_set_list = NULL_RTX;
1944 pbi->mem_set_list_len = 0;
1945 pbi->local_set = local_set;
1946 pbi->cond_local_set = cond_local_set;
1947 pbi->cc0_live = 0;
1948 pbi->flags = flags;
1949 pbi->insn_num = 0;
1951 if (flags & (PROP_LOG_LINKS | PROP_AUTOINC))
1952 pbi->reg_next_use = xcalloc (max_reg_num (), sizeof (rtx));
1953 else
1954 pbi->reg_next_use = NULL;
1956 pbi->new_set = BITMAP_ALLOC (NULL);
1958 #ifdef HAVE_conditional_execution
1959 pbi->reg_cond_dead = splay_tree_new (splay_tree_compare_ints, NULL,
1960 free_reg_cond_life_info);
1961 pbi->reg_cond_reg = BITMAP_ALLOC (NULL);
1963 /* If this block ends in a conditional branch, for each register
1964 live from one side of the branch and not the other, record the
1965 register as conditionally dead. */
1966 if (JUMP_P (BB_END (bb))
1967 && any_condjump_p (BB_END (bb)))
1969 regset diff = ALLOC_REG_SET (&reg_obstack);
1970 basic_block bb_true, bb_false;
1971 unsigned i;
1973 /* Identify the successor blocks. */
1974 bb_true = EDGE_SUCC (bb, 0)->dest;
1975 if (!single_succ_p (bb))
1977 bb_false = EDGE_SUCC (bb, 1)->dest;
1979 if (EDGE_SUCC (bb, 0)->flags & EDGE_FALLTHRU)
1981 basic_block t = bb_false;
1982 bb_false = bb_true;
1983 bb_true = t;
1985 else
1986 gcc_assert (EDGE_SUCC (bb, 1)->flags & EDGE_FALLTHRU);
1988 else
1990 /* This can happen with a conditional jump to the next insn. */
1991 gcc_assert (JUMP_LABEL (BB_END (bb)) == BB_HEAD (bb_true));
1993 /* Simplest way to do nothing. */
1994 bb_false = bb_true;
1997 /* Compute which register lead different lives in the successors. */
1998 bitmap_xor (diff, bb_true->il.rtl->global_live_at_start,
1999 bb_false->il.rtl->global_live_at_start);
2001 if (!bitmap_empty_p (diff))
2003 /* Extract the condition from the branch. */
2004 rtx set_src = SET_SRC (pc_set (BB_END (bb)));
2005 rtx cond_true = XEXP (set_src, 0);
2006 rtx reg = XEXP (cond_true, 0);
2007 enum rtx_code inv_cond;
2009 if (GET_CODE (reg) == SUBREG)
2010 reg = SUBREG_REG (reg);
2012 /* We can only track conditional lifetimes if the condition is
2013 in the form of a reversible comparison of a register against
2014 zero. If the condition is more complex than that, then it is
2015 safe not to record any information. */
2016 inv_cond = reversed_comparison_code (cond_true, BB_END (bb));
2017 if (inv_cond != UNKNOWN
2018 && REG_P (reg)
2019 && XEXP (cond_true, 1) == const0_rtx)
2021 rtx cond_false
2022 = gen_rtx_fmt_ee (inv_cond,
2023 GET_MODE (cond_true), XEXP (cond_true, 0),
2024 XEXP (cond_true, 1));
2025 reg_set_iterator rsi;
2027 if (GET_CODE (XEXP (set_src, 1)) == PC)
2029 rtx t = cond_false;
2030 cond_false = cond_true;
2031 cond_true = t;
2034 SET_REGNO_REG_SET (pbi->reg_cond_reg, REGNO (reg));
2036 /* For each such register, mark it conditionally dead. */
2037 EXECUTE_IF_SET_IN_REG_SET (diff, 0, i, rsi)
2039 struct reg_cond_life_info *rcli;
2040 rtx cond;
2042 rcli = xmalloc (sizeof (*rcli));
2044 if (REGNO_REG_SET_P (bb_true->il.rtl->global_live_at_start,
2046 cond = cond_false;
2047 else
2048 cond = cond_true;
2049 rcli->condition = cond;
2050 rcli->stores = const0_rtx;
2051 rcli->orig_condition = cond;
2053 splay_tree_insert (pbi->reg_cond_dead, i,
2054 (splay_tree_value) rcli);
2059 FREE_REG_SET (diff);
2061 #endif
2063 /* If this block has no successors, any stores to the frame that aren't
2064 used later in the block are dead. So make a pass over the block
2065 recording any such that are made and show them dead at the end. We do
2066 a very conservative and simple job here. */
2067 if (optimize
2068 && ! (TREE_CODE (TREE_TYPE (current_function_decl)) == FUNCTION_TYPE
2069 && (TYPE_RETURNS_STACK_DEPRESSED
2070 (TREE_TYPE (current_function_decl))))
2071 && (flags & PROP_SCAN_DEAD_STORES)
2072 && (EDGE_COUNT (bb->succs) == 0
2073 || (single_succ_p (bb)
2074 && single_succ (bb) == EXIT_BLOCK_PTR
2075 && ! current_function_calls_eh_return)))
2077 rtx insn, set;
2078 for (insn = BB_END (bb); insn != BB_HEAD (bb); insn = PREV_INSN (insn))
2079 if (NONJUMP_INSN_P (insn)
2080 && (set = single_set (insn))
2081 && MEM_P (SET_DEST (set)))
2083 rtx mem = SET_DEST (set);
2084 rtx canon_mem = canon_rtx (mem);
2086 if (XEXP (canon_mem, 0) == frame_pointer_rtx
2087 || (GET_CODE (XEXP (canon_mem, 0)) == PLUS
2088 && XEXP (XEXP (canon_mem, 0), 0) == frame_pointer_rtx
2089 && GET_CODE (XEXP (XEXP (canon_mem, 0), 1)) == CONST_INT))
2090 add_to_mem_set_list (pbi, canon_mem);
2094 return pbi;
2097 /* Release a propagate_block_info struct. */
2099 void
2100 free_propagate_block_info (struct propagate_block_info *pbi)
2102 free_EXPR_LIST_list (&pbi->mem_set_list);
2104 BITMAP_FREE (pbi->new_set);
2106 #ifdef HAVE_conditional_execution
2107 splay_tree_delete (pbi->reg_cond_dead);
2108 BITMAP_FREE (pbi->reg_cond_reg);
2109 #endif
2111 if (pbi->flags & PROP_REG_INFO)
2113 int num = pbi->insn_num;
2114 unsigned i;
2115 reg_set_iterator rsi;
2117 EXECUTE_IF_SET_IN_REG_SET (pbi->reg_live, 0, i, rsi)
2119 REG_LIVE_LENGTH (i) += num - reg_deaths[i];
2120 reg_deaths[i] = 0;
2123 if (pbi->reg_next_use)
2124 free (pbi->reg_next_use);
2126 free (pbi);
2129 /* Compute the registers live at the beginning of a basic block BB from
2130 those live at the end.
2132 When called, REG_LIVE contains those live at the end. On return, it
2133 contains those live at the beginning.
2135 LOCAL_SET, if non-null, will be set with all registers killed
2136 unconditionally by this basic block.
2137 Likewise, COND_LOCAL_SET, if non-null, will be set with all registers
2138 killed conditionally by this basic block. If there is any unconditional
2139 set of a register, then the corresponding bit will be set in LOCAL_SET
2140 and cleared in COND_LOCAL_SET.
2141 It is valid for LOCAL_SET and COND_LOCAL_SET to be the same set. In this
2142 case, the resulting set will be equal to the union of the two sets that
2143 would otherwise be computed.
2145 Return nonzero if an INSN is deleted (i.e. by dead code removal). */
2148 propagate_block (basic_block bb, regset live, regset local_set,
2149 regset cond_local_set, int flags)
2151 struct propagate_block_info *pbi;
2152 rtx insn, prev;
2153 int changed;
2155 pbi = init_propagate_block_info (bb, live, local_set, cond_local_set, flags);
2157 if (flags & PROP_REG_INFO)
2159 unsigned i;
2160 reg_set_iterator rsi;
2162 /* Process the regs live at the end of the block.
2163 Mark them as not local to any one basic block. */
2164 EXECUTE_IF_SET_IN_REG_SET (live, 0, i, rsi)
2165 REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL;
2168 /* Scan the block an insn at a time from end to beginning. */
2170 changed = 0;
2171 for (insn = BB_END (bb); ; insn = prev)
2173 /* If this is a call to `setjmp' et al, warn if any
2174 non-volatile datum is live. */
2175 if ((flags & PROP_REG_INFO)
2176 && CALL_P (insn)
2177 && find_reg_note (insn, REG_SETJMP, NULL))
2178 IOR_REG_SET (regs_live_at_setjmp, pbi->reg_live);
2180 prev = propagate_one_insn (pbi, insn);
2181 if (!prev)
2182 changed |= insn != get_insns ();
2183 else
2184 changed |= NEXT_INSN (prev) != insn;
2186 if (insn == BB_HEAD (bb))
2187 break;
2190 free_propagate_block_info (pbi);
2192 return changed;
2195 /* Return 1 if X (the body of an insn, or part of it) is just dead stores
2196 (SET expressions whose destinations are registers dead after the insn).
2197 NEEDED is the regset that says which regs are alive after the insn.
2199 Unless CALL_OK is nonzero, an insn is needed if it contains a CALL.
2201 If X is the entire body of an insn, NOTES contains the reg notes
2202 pertaining to the insn. */
2204 static int
2205 insn_dead_p (struct propagate_block_info *pbi, rtx x, int call_ok,
2206 rtx notes ATTRIBUTE_UNUSED)
2208 enum rtx_code code = GET_CODE (x);
2210 /* Don't eliminate insns that may trap. */
2211 if (flag_non_call_exceptions && may_trap_p (x))
2212 return 0;
2214 #ifdef AUTO_INC_DEC
2215 /* As flow is invoked after combine, we must take existing AUTO_INC
2216 expressions into account. */
2217 for (; notes; notes = XEXP (notes, 1))
2219 if (REG_NOTE_KIND (notes) == REG_INC)
2221 int regno = REGNO (XEXP (notes, 0));
2223 /* Don't delete insns to set global regs. */
2224 if ((regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
2225 || REGNO_REG_SET_P (pbi->reg_live, regno))
2226 return 0;
2229 #endif
2231 /* If setting something that's a reg or part of one,
2232 see if that register's altered value will be live. */
2234 if (code == SET)
2236 rtx r = SET_DEST (x);
2238 #ifdef HAVE_cc0
2239 if (GET_CODE (r) == CC0)
2240 return ! pbi->cc0_live;
2241 #endif
2243 /* A SET that is a subroutine call cannot be dead. */
2244 if (GET_CODE (SET_SRC (x)) == CALL)
2246 if (! call_ok)
2247 return 0;
2250 /* Don't eliminate loads from volatile memory or volatile asms. */
2251 else if (volatile_refs_p (SET_SRC (x)))
2252 return 0;
2254 if (MEM_P (r))
2256 rtx temp, canon_r;
2258 if (MEM_VOLATILE_P (r) || GET_MODE (r) == BLKmode)
2259 return 0;
2261 canon_r = canon_rtx (r);
2263 /* Walk the set of memory locations we are currently tracking
2264 and see if one is an identical match to this memory location.
2265 If so, this memory write is dead (remember, we're walking
2266 backwards from the end of the block to the start). Since
2267 rtx_equal_p does not check the alias set or flags, we also
2268 must have the potential for them to conflict (anti_dependence). */
2269 for (temp = pbi->mem_set_list; temp != 0; temp = XEXP (temp, 1))
2270 if (anti_dependence (r, XEXP (temp, 0)))
2272 rtx mem = XEXP (temp, 0);
2274 if (rtx_equal_p (XEXP (canon_r, 0), XEXP (mem, 0))
2275 && (GET_MODE_SIZE (GET_MODE (canon_r))
2276 <= GET_MODE_SIZE (GET_MODE (mem))))
2277 return 1;
2279 #ifdef AUTO_INC_DEC
2280 /* Check if memory reference matches an auto increment. Only
2281 post increment/decrement or modify are valid. */
2282 if (GET_MODE (mem) == GET_MODE (r)
2283 && (GET_CODE (XEXP (mem, 0)) == POST_DEC
2284 || GET_CODE (XEXP (mem, 0)) == POST_INC
2285 || GET_CODE (XEXP (mem, 0)) == POST_MODIFY)
2286 && GET_MODE (XEXP (mem, 0)) == GET_MODE (r)
2287 && rtx_equal_p (XEXP (XEXP (mem, 0), 0), XEXP (r, 0)))
2288 return 1;
2289 #endif
2292 else
2294 while (GET_CODE (r) == SUBREG
2295 || GET_CODE (r) == STRICT_LOW_PART
2296 || GET_CODE (r) == ZERO_EXTRACT)
2297 r = XEXP (r, 0);
2299 if (REG_P (r))
2301 int regno = REGNO (r);
2303 /* Obvious. */
2304 if (REGNO_REG_SET_P (pbi->reg_live, regno))
2305 return 0;
2307 /* If this is a hard register, verify that subsequent
2308 words are not needed. */
2309 if (regno < FIRST_PSEUDO_REGISTER)
2311 int n = hard_regno_nregs[regno][GET_MODE (r)];
2313 while (--n > 0)
2314 if (REGNO_REG_SET_P (pbi->reg_live, regno+n))
2315 return 0;
2318 /* Don't delete insns to set global regs. */
2319 if (regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
2320 return 0;
2322 /* Make sure insns to set the stack pointer aren't deleted. */
2323 if (regno == STACK_POINTER_REGNUM)
2324 return 0;
2326 /* ??? These bits might be redundant with the force live bits
2327 in calculate_global_regs_live. We would delete from
2328 sequential sets; whether this actually affects real code
2329 for anything but the stack pointer I don't know. */
2330 /* Make sure insns to set the frame pointer aren't deleted. */
2331 if (regno == FRAME_POINTER_REGNUM
2332 && (! reload_completed || frame_pointer_needed))
2333 return 0;
2334 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2335 if (regno == HARD_FRAME_POINTER_REGNUM
2336 && (! reload_completed || frame_pointer_needed))
2337 return 0;
2338 #endif
2340 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2341 /* Make sure insns to set arg pointer are never deleted
2342 (if the arg pointer isn't fixed, there will be a USE
2343 for it, so we can treat it normally). */
2344 if (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
2345 return 0;
2346 #endif
2348 /* Otherwise, the set is dead. */
2349 return 1;
2354 /* If performing several activities, insn is dead if each activity
2355 is individually dead. Also, CLOBBERs and USEs can be ignored; a
2356 CLOBBER or USE that's inside a PARALLEL doesn't make the insn
2357 worth keeping. */
2358 else if (code == PARALLEL)
2360 int i = XVECLEN (x, 0);
2362 for (i--; i >= 0; i--)
2363 if (GET_CODE (XVECEXP (x, 0, i)) != CLOBBER
2364 && GET_CODE (XVECEXP (x, 0, i)) != USE
2365 && ! insn_dead_p (pbi, XVECEXP (x, 0, i), call_ok, NULL_RTX))
2366 return 0;
2368 return 1;
2371 /* A CLOBBER of a pseudo-register that is dead serves no purpose. That
2372 is not necessarily true for hard registers until after reload. */
2373 else if (code == CLOBBER)
2375 if (REG_P (XEXP (x, 0))
2376 && (REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER
2377 || reload_completed)
2378 && ! REGNO_REG_SET_P (pbi->reg_live, REGNO (XEXP (x, 0))))
2379 return 1;
2382 /* ??? A base USE is a historical relic. It ought not be needed anymore.
2383 Instances where it is still used are either (1) temporary and the USE
2384 escaped the pass, (2) cruft and the USE need not be emitted anymore,
2385 or (3) hiding bugs elsewhere that are not properly representing data
2386 flow. */
2388 return 0;
2391 /* If INSN is the last insn in a libcall, and assuming INSN is dead,
2392 return 1 if the entire library call is dead.
2393 This is true if INSN copies a register (hard or pseudo)
2394 and if the hard return reg of the call insn is dead.
2395 (The caller should have tested the destination of the SET inside
2396 INSN already for death.)
2398 If this insn doesn't just copy a register, then we don't
2399 have an ordinary libcall. In that case, cse could not have
2400 managed to substitute the source for the dest later on,
2401 so we can assume the libcall is dead.
2403 PBI is the block info giving pseudoregs live before this insn.
2404 NOTE is the REG_RETVAL note of the insn. */
2406 static int
2407 libcall_dead_p (struct propagate_block_info *pbi, rtx note, rtx insn)
2409 rtx x = single_set (insn);
2411 if (x)
2413 rtx r = SET_SRC (x);
2415 if (REG_P (r) || GET_CODE (r) == SUBREG)
2417 rtx call = XEXP (note, 0);
2418 rtx call_pat;
2419 int i;
2421 /* Find the call insn. */
2422 while (call != insn && !CALL_P (call))
2423 call = NEXT_INSN (call);
2425 /* If there is none, do nothing special,
2426 since ordinary death handling can understand these insns. */
2427 if (call == insn)
2428 return 0;
2430 /* See if the hard reg holding the value is dead.
2431 If this is a PARALLEL, find the call within it. */
2432 call_pat = PATTERN (call);
2433 if (GET_CODE (call_pat) == PARALLEL)
2435 for (i = XVECLEN (call_pat, 0) - 1; i >= 0; i--)
2436 if (GET_CODE (XVECEXP (call_pat, 0, i)) == SET
2437 && GET_CODE (SET_SRC (XVECEXP (call_pat, 0, i))) == CALL)
2438 break;
2440 /* This may be a library call that is returning a value
2441 via invisible pointer. Do nothing special, since
2442 ordinary death handling can understand these insns. */
2443 if (i < 0)
2444 return 0;
2446 call_pat = XVECEXP (call_pat, 0, i);
2449 if (! insn_dead_p (pbi, call_pat, 1, REG_NOTES (call)))
2450 return 0;
2452 while ((insn = PREV_INSN (insn)) != call)
2454 if (! INSN_P (insn))
2455 continue;
2456 if (! insn_dead_p (pbi, PATTERN (insn), 0, REG_NOTES (insn)))
2457 return 0;
2459 return 1;
2462 return 0;
2465 /* 1 if register REGNO was alive at a place where `setjmp' was called
2466 and was set more than once or is an argument.
2467 Such regs may be clobbered by `longjmp'. */
2470 regno_clobbered_at_setjmp (int regno)
2472 if (n_basic_blocks == 0)
2473 return 0;
2475 return ((REG_N_SETS (regno) > 1
2476 || REGNO_REG_SET_P (ENTRY_BLOCK_PTR->il.rtl->global_live_at_end,
2477 regno))
2478 && REGNO_REG_SET_P (regs_live_at_setjmp, regno));
2481 /* Add MEM to PBI->MEM_SET_LIST. MEM should be canonical. Respect the
2482 maximal list size; look for overlaps in mode and select the largest. */
2483 static void
2484 add_to_mem_set_list (struct propagate_block_info *pbi, rtx mem)
2486 rtx i;
2488 /* We don't know how large a BLKmode store is, so we must not
2489 take them into consideration. */
2490 if (GET_MODE (mem) == BLKmode)
2491 return;
2493 for (i = pbi->mem_set_list; i ; i = XEXP (i, 1))
2495 rtx e = XEXP (i, 0);
2496 if (rtx_equal_p (XEXP (mem, 0), XEXP (e, 0)))
2498 if (GET_MODE_SIZE (GET_MODE (mem)) > GET_MODE_SIZE (GET_MODE (e)))
2500 #ifdef AUTO_INC_DEC
2501 /* If we must store a copy of the mem, we can just modify
2502 the mode of the stored copy. */
2503 if (pbi->flags & PROP_AUTOINC)
2504 PUT_MODE (e, GET_MODE (mem));
2505 else
2506 #endif
2507 XEXP (i, 0) = mem;
2509 return;
2513 if (pbi->mem_set_list_len < MAX_MEM_SET_LIST_LEN)
2515 #ifdef AUTO_INC_DEC
2516 /* Store a copy of mem, otherwise the address may be
2517 scrogged by find_auto_inc. */
2518 if (pbi->flags & PROP_AUTOINC)
2519 mem = shallow_copy_rtx (mem);
2520 #endif
2521 pbi->mem_set_list = alloc_EXPR_LIST (0, mem, pbi->mem_set_list);
2522 pbi->mem_set_list_len++;
2526 /* INSN references memory, possibly using autoincrement addressing modes.
2527 Find any entries on the mem_set_list that need to be invalidated due
2528 to an address change. */
2530 static int
2531 invalidate_mems_from_autoinc (rtx *px, void *data)
2533 rtx x = *px;
2534 struct propagate_block_info *pbi = data;
2536 if (GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
2538 invalidate_mems_from_set (pbi, XEXP (x, 0));
2539 return -1;
2542 return 0;
2545 /* EXP is a REG or MEM. Remove any dependent entries from
2546 pbi->mem_set_list. */
2548 static void
2549 invalidate_mems_from_set (struct propagate_block_info *pbi, rtx exp)
2551 rtx temp = pbi->mem_set_list;
2552 rtx prev = NULL_RTX;
2553 rtx next;
2555 while (temp)
2557 next = XEXP (temp, 1);
2558 if ((REG_P (exp) && reg_overlap_mentioned_p (exp, XEXP (temp, 0)))
2559 /* When we get an EXP that is a mem here, we want to check if EXP
2560 overlaps the *address* of any of the mems in the list (i.e. not
2561 whether the mems actually overlap; that's done elsewhere). */
2562 || (MEM_P (exp)
2563 && reg_overlap_mentioned_p (exp, XEXP (XEXP (temp, 0), 0))))
2565 /* Splice this entry out of the list. */
2566 if (prev)
2567 XEXP (prev, 1) = next;
2568 else
2569 pbi->mem_set_list = next;
2570 free_EXPR_LIST_node (temp);
2571 pbi->mem_set_list_len--;
2573 else
2574 prev = temp;
2575 temp = next;
2579 /* Process the registers that are set within X. Their bits are set to
2580 1 in the regset DEAD, because they are dead prior to this insn.
2582 If INSN is nonzero, it is the insn being processed.
2584 FLAGS is the set of operations to perform. */
2586 static void
2587 mark_set_regs (struct propagate_block_info *pbi, rtx x, rtx insn)
2589 rtx cond = NULL_RTX;
2590 rtx link;
2591 enum rtx_code code;
2592 int flags = pbi->flags;
2594 if (insn)
2595 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2597 if (REG_NOTE_KIND (link) == REG_INC)
2598 mark_set_1 (pbi, SET, XEXP (link, 0),
2599 (GET_CODE (x) == COND_EXEC
2600 ? COND_EXEC_TEST (x) : NULL_RTX),
2601 insn, flags);
2603 retry:
2604 switch (code = GET_CODE (x))
2606 case SET:
2607 if (GET_CODE (XEXP (x, 1)) == ASM_OPERANDS)
2608 flags |= PROP_ASM_SCAN;
2609 /* Fall through */
2610 case CLOBBER:
2611 mark_set_1 (pbi, code, SET_DEST (x), cond, insn, flags);
2612 return;
2614 case COND_EXEC:
2615 cond = COND_EXEC_TEST (x);
2616 x = COND_EXEC_CODE (x);
2617 goto retry;
2619 case PARALLEL:
2621 int i;
2623 /* We must scan forwards. If we have an asm, we need to set
2624 the PROP_ASM_SCAN flag before scanning the clobbers. */
2625 for (i = 0; i < XVECLEN (x, 0); i++)
2627 rtx sub = XVECEXP (x, 0, i);
2628 switch (code = GET_CODE (sub))
2630 case COND_EXEC:
2631 gcc_assert (!cond);
2633 cond = COND_EXEC_TEST (sub);
2634 sub = COND_EXEC_CODE (sub);
2635 if (GET_CODE (sub) == SET)
2636 goto mark_set;
2637 if (GET_CODE (sub) == CLOBBER)
2638 goto mark_clob;
2639 break;
2641 case SET:
2642 mark_set:
2643 if (GET_CODE (XEXP (sub, 1)) == ASM_OPERANDS)
2644 flags |= PROP_ASM_SCAN;
2645 /* Fall through */
2646 case CLOBBER:
2647 mark_clob:
2648 mark_set_1 (pbi, code, SET_DEST (sub), cond, insn, flags);
2649 break;
2651 case ASM_OPERANDS:
2652 flags |= PROP_ASM_SCAN;
2653 break;
2655 default:
2656 break;
2659 break;
2662 default:
2663 break;
2667 /* Process a single set, which appears in INSN. REG (which may not
2668 actually be a REG, it may also be a SUBREG, PARALLEL, etc.) is
2669 being set using the CODE (which may be SET, CLOBBER, or COND_EXEC).
2670 If the set is conditional (because it appear in a COND_EXEC), COND
2671 will be the condition. */
2673 static void
2674 mark_set_1 (struct propagate_block_info *pbi, enum rtx_code code, rtx reg, rtx cond, rtx insn, int flags)
2676 int regno_first = -1, regno_last = -1;
2677 unsigned long not_dead = 0;
2678 int i;
2680 /* Modifying just one hardware register of a multi-reg value or just a
2681 byte field of a register does not mean the value from before this insn
2682 is now dead. Of course, if it was dead after it's unused now. */
2684 switch (GET_CODE (reg))
2686 case PARALLEL:
2687 /* Some targets place small structures in registers for return values of
2688 functions. We have to detect this case specially here to get correct
2689 flow information. */
2690 for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
2691 if (XEXP (XVECEXP (reg, 0, i), 0) != 0)
2692 mark_set_1 (pbi, code, XEXP (XVECEXP (reg, 0, i), 0), cond, insn,
2693 flags);
2694 return;
2696 case SIGN_EXTRACT:
2697 /* SIGN_EXTRACT cannot be an lvalue. */
2698 gcc_unreachable ();
2700 case ZERO_EXTRACT:
2701 case STRICT_LOW_PART:
2702 /* ??? Assumes STRICT_LOW_PART not used on multi-word registers. */
2704 reg = XEXP (reg, 0);
2705 while (GET_CODE (reg) == SUBREG
2706 || GET_CODE (reg) == ZERO_EXTRACT
2707 || GET_CODE (reg) == STRICT_LOW_PART);
2708 if (MEM_P (reg))
2709 break;
2710 not_dead = (unsigned long) REGNO_REG_SET_P (pbi->reg_live, REGNO (reg));
2711 /* Fall through. */
2713 case REG:
2714 regno_last = regno_first = REGNO (reg);
2715 if (regno_first < FIRST_PSEUDO_REGISTER)
2716 regno_last += hard_regno_nregs[regno_first][GET_MODE (reg)] - 1;
2717 break;
2719 case SUBREG:
2720 if (REG_P (SUBREG_REG (reg)))
2722 enum machine_mode outer_mode = GET_MODE (reg);
2723 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (reg));
2725 /* Identify the range of registers affected. This is moderately
2726 tricky for hard registers. See alter_subreg. */
2728 regno_last = regno_first = REGNO (SUBREG_REG (reg));
2729 if (regno_first < FIRST_PSEUDO_REGISTER)
2731 regno_first += subreg_regno_offset (regno_first, inner_mode,
2732 SUBREG_BYTE (reg),
2733 outer_mode);
2734 regno_last = (regno_first
2735 + hard_regno_nregs[regno_first][outer_mode] - 1);
2737 /* Since we've just adjusted the register number ranges, make
2738 sure REG matches. Otherwise some_was_live will be clear
2739 when it shouldn't have been, and we'll create incorrect
2740 REG_UNUSED notes. */
2741 reg = gen_rtx_REG (outer_mode, regno_first);
2743 else
2745 /* If the number of words in the subreg is less than the number
2746 of words in the full register, we have a well-defined partial
2747 set. Otherwise the high bits are undefined.
2749 This is only really applicable to pseudos, since we just took
2750 care of multi-word hard registers. */
2751 if (((GET_MODE_SIZE (outer_mode)
2752 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
2753 < ((GET_MODE_SIZE (inner_mode)
2754 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))
2755 not_dead = (unsigned long) REGNO_REG_SET_P (pbi->reg_live,
2756 regno_first);
2758 reg = SUBREG_REG (reg);
2761 else
2762 reg = SUBREG_REG (reg);
2763 break;
2765 default:
2766 break;
2769 /* If this set is a MEM, then it kills any aliased writes and any
2770 other MEMs which use it.
2771 If this set is a REG, then it kills any MEMs which use the reg. */
2772 if (optimize && (flags & PROP_SCAN_DEAD_STORES))
2774 if (REG_P (reg) || MEM_P (reg))
2775 invalidate_mems_from_set (pbi, reg);
2777 /* If the memory reference had embedded side effects (autoincrement
2778 address modes) then we may need to kill some entries on the
2779 memory set list. */
2780 if (insn && MEM_P (reg))
2781 for_each_rtx (&PATTERN (insn), invalidate_mems_from_autoinc, pbi);
2783 if (MEM_P (reg) && ! side_effects_p (reg)
2784 /* ??? With more effort we could track conditional memory life. */
2785 && ! cond)
2786 add_to_mem_set_list (pbi, canon_rtx (reg));
2789 if (REG_P (reg)
2790 && ! (regno_first == FRAME_POINTER_REGNUM
2791 && (! reload_completed || frame_pointer_needed))
2792 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2793 && ! (regno_first == HARD_FRAME_POINTER_REGNUM
2794 && (! reload_completed || frame_pointer_needed))
2795 #endif
2796 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2797 && ! (regno_first == ARG_POINTER_REGNUM && fixed_regs[regno_first])
2798 #endif
2801 int some_was_live = 0, some_was_dead = 0;
2803 for (i = regno_first; i <= regno_last; ++i)
2805 int needed_regno = REGNO_REG_SET_P (pbi->reg_live, i);
2806 if (pbi->local_set)
2808 /* Order of the set operation matters here since both
2809 sets may be the same. */
2810 CLEAR_REGNO_REG_SET (pbi->cond_local_set, i);
2811 if (cond != NULL_RTX
2812 && ! REGNO_REG_SET_P (pbi->local_set, i))
2813 SET_REGNO_REG_SET (pbi->cond_local_set, i);
2814 else
2815 SET_REGNO_REG_SET (pbi->local_set, i);
2817 if (code != CLOBBER)
2818 SET_REGNO_REG_SET (pbi->new_set, i);
2820 some_was_live |= needed_regno;
2821 some_was_dead |= ! needed_regno;
2824 #ifdef HAVE_conditional_execution
2825 /* Consider conditional death in deciding that the register needs
2826 a death note. */
2827 if (some_was_live && ! not_dead
2828 /* The stack pointer is never dead. Well, not strictly true,
2829 but it's very difficult to tell from here. Hopefully
2830 combine_stack_adjustments will fix up the most egregious
2831 errors. */
2832 && regno_first != STACK_POINTER_REGNUM)
2834 for (i = regno_first; i <= regno_last; ++i)
2835 if (! mark_regno_cond_dead (pbi, i, cond))
2836 not_dead |= ((unsigned long) 1) << (i - regno_first);
2838 #endif
2840 /* Additional data to record if this is the final pass. */
2841 if (flags & (PROP_LOG_LINKS | PROP_REG_INFO
2842 | PROP_DEATH_NOTES | PROP_AUTOINC))
2844 rtx y;
2845 int blocknum = pbi->bb->index;
2847 y = NULL_RTX;
2848 if (flags & (PROP_LOG_LINKS | PROP_AUTOINC))
2850 y = pbi->reg_next_use[regno_first];
2852 /* The next use is no longer next, since a store intervenes. */
2853 for (i = regno_first; i <= regno_last; ++i)
2854 pbi->reg_next_use[i] = 0;
2857 if (flags & PROP_REG_INFO)
2859 for (i = regno_first; i <= regno_last; ++i)
2861 /* Count (weighted) references, stores, etc. This counts a
2862 register twice if it is modified, but that is correct. */
2863 REG_N_SETS (i) += 1;
2864 REG_N_REFS (i) += 1;
2865 REG_FREQ (i) += REG_FREQ_FROM_BB (pbi->bb);
2867 /* The insns where a reg is live are normally counted
2868 elsewhere, but we want the count to include the insn
2869 where the reg is set, and the normal counting mechanism
2870 would not count it. */
2871 REG_LIVE_LENGTH (i) += 1;
2874 /* If this is a hard reg, record this function uses the reg. */
2875 if (regno_first < FIRST_PSEUDO_REGISTER)
2877 for (i = regno_first; i <= regno_last; i++)
2878 regs_ever_live[i] = 1;
2879 if (flags & PROP_ASM_SCAN)
2880 for (i = regno_first; i <= regno_last; i++)
2881 regs_asm_clobbered[i] = 1;
2883 else
2885 /* Keep track of which basic blocks each reg appears in. */
2886 if (REG_BASIC_BLOCK (regno_first) == REG_BLOCK_UNKNOWN)
2887 REG_BASIC_BLOCK (regno_first) = blocknum;
2888 else if (REG_BASIC_BLOCK (regno_first) != blocknum)
2889 REG_BASIC_BLOCK (regno_first) = REG_BLOCK_GLOBAL;
2893 if (! some_was_dead)
2895 if (flags & PROP_LOG_LINKS)
2897 /* Make a logical link from the next following insn
2898 that uses this register, back to this insn.
2899 The following insns have already been processed.
2901 We don't build a LOG_LINK for hard registers containing
2902 in ASM_OPERANDs. If these registers get replaced,
2903 we might wind up changing the semantics of the insn,
2904 even if reload can make what appear to be valid
2905 assignments later.
2907 We don't build a LOG_LINK for global registers to
2908 or from a function call. We don't want to let
2909 combine think that it knows what is going on with
2910 global registers. */
2911 if (y && (BLOCK_NUM (y) == blocknum)
2912 && (regno_first >= FIRST_PSEUDO_REGISTER
2913 || (asm_noperands (PATTERN (y)) < 0
2914 && ! ((CALL_P (insn)
2915 || CALL_P (y))
2916 && global_regs[regno_first]))))
2917 LOG_LINKS (y) = alloc_INSN_LIST (insn, LOG_LINKS (y));
2920 else if (not_dead)
2922 else if (! some_was_live)
2924 if (flags & PROP_REG_INFO)
2925 REG_N_DEATHS (regno_first) += 1;
2927 if (flags & PROP_DEATH_NOTES)
2929 /* Note that dead stores have already been deleted
2930 when possible. If we get here, we have found a
2931 dead store that cannot be eliminated (because the
2932 same insn does something useful). Indicate this
2933 by marking the reg being set as dying here. */
2934 REG_NOTES (insn)
2935 = alloc_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn));
2938 else
2940 if (flags & PROP_DEATH_NOTES)
2942 /* This is a case where we have a multi-word hard register
2943 and some, but not all, of the words of the register are
2944 needed in subsequent insns. Write REG_UNUSED notes
2945 for those parts that were not needed. This case should
2946 be rare. */
2948 for (i = regno_first; i <= regno_last; ++i)
2949 if (! REGNO_REG_SET_P (pbi->reg_live, i))
2950 REG_NOTES (insn)
2951 = alloc_EXPR_LIST (REG_UNUSED,
2952 regno_reg_rtx[i],
2953 REG_NOTES (insn));
2958 /* Mark the register as being dead. */
2959 if (some_was_live
2960 /* The stack pointer is never dead. Well, not strictly true,
2961 but it's very difficult to tell from here. Hopefully
2962 combine_stack_adjustments will fix up the most egregious
2963 errors. */
2964 && regno_first != STACK_POINTER_REGNUM)
2966 for (i = regno_first; i <= regno_last; ++i)
2967 if (!(not_dead & (((unsigned long) 1) << (i - regno_first))))
2969 if ((pbi->flags & PROP_REG_INFO)
2970 && REGNO_REG_SET_P (pbi->reg_live, i))
2972 REG_LIVE_LENGTH (i) += pbi->insn_num - reg_deaths[i];
2973 reg_deaths[i] = 0;
2975 CLEAR_REGNO_REG_SET (pbi->reg_live, i);
2977 if (flags & PROP_DEAD_INSN)
2978 emit_insn_after (gen_rtx_CLOBBER (VOIDmode, reg), insn);
2981 else if (REG_P (reg))
2983 if (flags & (PROP_LOG_LINKS | PROP_AUTOINC))
2984 pbi->reg_next_use[regno_first] = 0;
2986 if ((flags & PROP_REG_INFO) != 0
2987 && (flags & PROP_ASM_SCAN) != 0
2988 && regno_first < FIRST_PSEUDO_REGISTER)
2990 for (i = regno_first; i <= regno_last; i++)
2991 regs_asm_clobbered[i] = 1;
2995 /* If this is the last pass and this is a SCRATCH, show it will be dying
2996 here and count it. */
2997 else if (GET_CODE (reg) == SCRATCH)
2999 if (flags & PROP_DEATH_NOTES)
3000 REG_NOTES (insn)
3001 = alloc_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn));
3005 #ifdef HAVE_conditional_execution
3006 /* Mark REGNO conditionally dead.
3007 Return true if the register is now unconditionally dead. */
3009 static int
3010 mark_regno_cond_dead (struct propagate_block_info *pbi, int regno, rtx cond)
3012 /* If this is a store to a predicate register, the value of the
3013 predicate is changing, we don't know that the predicate as seen
3014 before is the same as that seen after. Flush all dependent
3015 conditions from reg_cond_dead. This will make all such
3016 conditionally live registers unconditionally live. */
3017 if (REGNO_REG_SET_P (pbi->reg_cond_reg, regno))
3018 flush_reg_cond_reg (pbi, regno);
3020 /* If this is an unconditional store, remove any conditional
3021 life that may have existed. */
3022 if (cond == NULL_RTX)
3023 splay_tree_remove (pbi->reg_cond_dead, regno);
3024 else
3026 splay_tree_node node;
3027 struct reg_cond_life_info *rcli;
3028 rtx ncond;
3030 /* Otherwise this is a conditional set. Record that fact.
3031 It may have been conditionally used, or there may be a
3032 subsequent set with a complementary condition. */
3034 node = splay_tree_lookup (pbi->reg_cond_dead, regno);
3035 if (node == NULL)
3037 /* The register was unconditionally live previously.
3038 Record the current condition as the condition under
3039 which it is dead. */
3040 rcli = xmalloc (sizeof (*rcli));
3041 rcli->condition = cond;
3042 rcli->stores = cond;
3043 rcli->orig_condition = const0_rtx;
3044 splay_tree_insert (pbi->reg_cond_dead, regno,
3045 (splay_tree_value) rcli);
3047 SET_REGNO_REG_SET (pbi->reg_cond_reg, REGNO (XEXP (cond, 0)));
3049 /* Not unconditionally dead. */
3050 return 0;
3052 else
3054 /* The register was conditionally live previously.
3055 Add the new condition to the old. */
3056 rcli = (struct reg_cond_life_info *) node->value;
3057 ncond = rcli->condition;
3058 ncond = ior_reg_cond (ncond, cond, 1);
3059 if (rcli->stores == const0_rtx)
3060 rcli->stores = cond;
3061 else if (rcli->stores != const1_rtx)
3062 rcli->stores = ior_reg_cond (rcli->stores, cond, 1);
3064 /* If the register is now unconditionally dead, remove the entry
3065 in the splay_tree. A register is unconditionally dead if the
3066 dead condition ncond is true. A register is also unconditionally
3067 dead if the sum of all conditional stores is an unconditional
3068 store (stores is true), and the dead condition is identically the
3069 same as the original dead condition initialized at the end of
3070 the block. This is a pointer compare, not an rtx_equal_p
3071 compare. */
3072 if (ncond == const1_rtx
3073 || (ncond == rcli->orig_condition && rcli->stores == const1_rtx))
3074 splay_tree_remove (pbi->reg_cond_dead, regno);
3075 else
3077 rcli->condition = ncond;
3079 SET_REGNO_REG_SET (pbi->reg_cond_reg, REGNO (XEXP (cond, 0)));
3081 /* Not unconditionally dead. */
3082 return 0;
3087 return 1;
3090 /* Called from splay_tree_delete for pbi->reg_cond_life. */
3092 static void
3093 free_reg_cond_life_info (splay_tree_value value)
3095 struct reg_cond_life_info *rcli = (struct reg_cond_life_info *) value;
3096 free (rcli);
3099 /* Helper function for flush_reg_cond_reg. */
3101 static int
3102 flush_reg_cond_reg_1 (splay_tree_node node, void *data)
3104 struct reg_cond_life_info *rcli;
3105 int *xdata = (int *) data;
3106 unsigned int regno = xdata[0];
3108 /* Don't need to search if last flushed value was farther on in
3109 the in-order traversal. */
3110 if (xdata[1] >= (int) node->key)
3111 return 0;
3113 /* Splice out portions of the expression that refer to regno. */
3114 rcli = (struct reg_cond_life_info *) node->value;
3115 rcli->condition = elim_reg_cond (rcli->condition, regno);
3116 if (rcli->stores != const0_rtx && rcli->stores != const1_rtx)
3117 rcli->stores = elim_reg_cond (rcli->stores, regno);
3119 /* If the entire condition is now false, signal the node to be removed. */
3120 if (rcli->condition == const0_rtx)
3122 xdata[1] = node->key;
3123 return -1;
3125 else
3126 gcc_assert (rcli->condition != const1_rtx);
3128 return 0;
3131 /* Flush all (sub) expressions referring to REGNO from REG_COND_LIVE. */
3133 static void
3134 flush_reg_cond_reg (struct propagate_block_info *pbi, int regno)
3136 int pair[2];
3138 pair[0] = regno;
3139 pair[1] = -1;
3140 while (splay_tree_foreach (pbi->reg_cond_dead,
3141 flush_reg_cond_reg_1, pair) == -1)
3142 splay_tree_remove (pbi->reg_cond_dead, pair[1]);
3144 CLEAR_REGNO_REG_SET (pbi->reg_cond_reg, regno);
3147 /* Logical arithmetic on predicate conditions. IOR, NOT and AND.
3148 For ior/and, the ADD flag determines whether we want to add the new
3149 condition X to the old one unconditionally. If it is zero, we will
3150 only return a new expression if X allows us to simplify part of
3151 OLD, otherwise we return NULL to the caller.
3152 If ADD is nonzero, we will return a new condition in all cases. The
3153 toplevel caller of one of these functions should always pass 1 for
3154 ADD. */
3156 static rtx
3157 ior_reg_cond (rtx old, rtx x, int add)
3159 rtx op0, op1;
3161 if (COMPARISON_P (old))
3163 if (COMPARISON_P (x)
3164 && REVERSE_CONDEXEC_PREDICATES_P (x, old)
3165 && REGNO (XEXP (x, 0)) == REGNO (XEXP (old, 0)))
3166 return const1_rtx;
3167 if (GET_CODE (x) == GET_CODE (old)
3168 && REGNO (XEXP (x, 0)) == REGNO (XEXP (old, 0)))
3169 return old;
3170 if (! add)
3171 return NULL;
3172 return gen_rtx_IOR (0, old, x);
3175 switch (GET_CODE (old))
3177 case IOR:
3178 op0 = ior_reg_cond (XEXP (old, 0), x, 0);
3179 op1 = ior_reg_cond (XEXP (old, 1), x, 0);
3180 if (op0 != NULL || op1 != NULL)
3182 if (op0 == const0_rtx)
3183 return op1 ? op1 : gen_rtx_IOR (0, XEXP (old, 1), x);
3184 if (op1 == const0_rtx)
3185 return op0 ? op0 : gen_rtx_IOR (0, XEXP (old, 0), x);
3186 if (op0 == const1_rtx || op1 == const1_rtx)
3187 return const1_rtx;
3188 if (op0 == NULL)
3189 op0 = gen_rtx_IOR (0, XEXP (old, 0), x);
3190 else if (rtx_equal_p (x, op0))
3191 /* (x | A) | x ~ (x | A). */
3192 return old;
3193 if (op1 == NULL)
3194 op1 = gen_rtx_IOR (0, XEXP (old, 1), x);
3195 else if (rtx_equal_p (x, op1))
3196 /* (A | x) | x ~ (A | x). */
3197 return old;
3198 return gen_rtx_IOR (0, op0, op1);
3200 if (! add)
3201 return NULL;
3202 return gen_rtx_IOR (0, old, x);
3204 case AND:
3205 op0 = ior_reg_cond (XEXP (old, 0), x, 0);
3206 op1 = ior_reg_cond (XEXP (old, 1), x, 0);
3207 if (op0 != NULL || op1 != NULL)
3209 if (op0 == const1_rtx)
3210 return op1 ? op1 : gen_rtx_IOR (0, XEXP (old, 1), x);
3211 if (op1 == const1_rtx)
3212 return op0 ? op0 : gen_rtx_IOR (0, XEXP (old, 0), x);
3213 if (op0 == const0_rtx || op1 == const0_rtx)
3214 return const0_rtx;
3215 if (op0 == NULL)
3216 op0 = gen_rtx_IOR (0, XEXP (old, 0), x);
3217 else if (rtx_equal_p (x, op0))
3218 /* (x & A) | x ~ x. */
3219 return op0;
3220 if (op1 == NULL)
3221 op1 = gen_rtx_IOR (0, XEXP (old, 1), x);
3222 else if (rtx_equal_p (x, op1))
3223 /* (A & x) | x ~ x. */
3224 return op1;
3225 return gen_rtx_AND (0, op0, op1);
3227 if (! add)
3228 return NULL;
3229 return gen_rtx_IOR (0, old, x);
3231 case NOT:
3232 op0 = and_reg_cond (XEXP (old, 0), not_reg_cond (x), 0);
3233 if (op0 != NULL)
3234 return not_reg_cond (op0);
3235 if (! add)
3236 return NULL;
3237 return gen_rtx_IOR (0, old, x);
3239 default:
3240 gcc_unreachable ();
3244 static rtx
3245 not_reg_cond (rtx x)
3247 if (x == const0_rtx)
3248 return const1_rtx;
3249 else if (x == const1_rtx)
3250 return const0_rtx;
3251 if (GET_CODE (x) == NOT)
3252 return XEXP (x, 0);
3253 if (COMPARISON_P (x)
3254 && REG_P (XEXP (x, 0)))
3256 gcc_assert (XEXP (x, 1) == const0_rtx);
3258 return gen_rtx_fmt_ee (reversed_comparison_code (x, NULL),
3259 VOIDmode, XEXP (x, 0), const0_rtx);
3261 return gen_rtx_NOT (0, x);
3264 static rtx
3265 and_reg_cond (rtx old, rtx x, int add)
3267 rtx op0, op1;
3269 if (COMPARISON_P (old))
3271 if (COMPARISON_P (x)
3272 && GET_CODE (x) == reversed_comparison_code (old, NULL)
3273 && REGNO (XEXP (x, 0)) == REGNO (XEXP (old, 0)))
3274 return const0_rtx;
3275 if (GET_CODE (x) == GET_CODE (old)
3276 && REGNO (XEXP (x, 0)) == REGNO (XEXP (old, 0)))
3277 return old;
3278 if (! add)
3279 return NULL;
3280 return gen_rtx_AND (0, old, x);
3283 switch (GET_CODE (old))
3285 case IOR:
3286 op0 = and_reg_cond (XEXP (old, 0), x, 0);
3287 op1 = and_reg_cond (XEXP (old, 1), x, 0);
3288 if (op0 != NULL || op1 != NULL)
3290 if (op0 == const0_rtx)
3291 return op1 ? op1 : gen_rtx_AND (0, XEXP (old, 1), x);
3292 if (op1 == const0_rtx)
3293 return op0 ? op0 : gen_rtx_AND (0, XEXP (old, 0), x);
3294 if (op0 == const1_rtx || op1 == const1_rtx)
3295 return const1_rtx;
3296 if (op0 == NULL)
3297 op0 = gen_rtx_AND (0, XEXP (old, 0), x);
3298 else if (rtx_equal_p (x, op0))
3299 /* (x | A) & x ~ x. */
3300 return op0;
3301 if (op1 == NULL)
3302 op1 = gen_rtx_AND (0, XEXP (old, 1), x);
3303 else if (rtx_equal_p (x, op1))
3304 /* (A | x) & x ~ x. */
3305 return op1;
3306 return gen_rtx_IOR (0, op0, op1);
3308 if (! add)
3309 return NULL;
3310 return gen_rtx_AND (0, old, x);
3312 case AND:
3313 op0 = and_reg_cond (XEXP (old, 0), x, 0);
3314 op1 = and_reg_cond (XEXP (old, 1), x, 0);
3315 if (op0 != NULL || op1 != NULL)
3317 if (op0 == const1_rtx)
3318 return op1 ? op1 : gen_rtx_AND (0, XEXP (old, 1), x);
3319 if (op1 == const1_rtx)
3320 return op0 ? op0 : gen_rtx_AND (0, XEXP (old, 0), x);
3321 if (op0 == const0_rtx || op1 == const0_rtx)
3322 return const0_rtx;
3323 if (op0 == NULL)
3324 op0 = gen_rtx_AND (0, XEXP (old, 0), x);
3325 else if (rtx_equal_p (x, op0))
3326 /* (x & A) & x ~ (x & A). */
3327 return old;
3328 if (op1 == NULL)
3329 op1 = gen_rtx_AND (0, XEXP (old, 1), x);
3330 else if (rtx_equal_p (x, op1))
3331 /* (A & x) & x ~ (A & x). */
3332 return old;
3333 return gen_rtx_AND (0, op0, op1);
3335 if (! add)
3336 return NULL;
3337 return gen_rtx_AND (0, old, x);
3339 case NOT:
3340 op0 = ior_reg_cond (XEXP (old, 0), not_reg_cond (x), 0);
3341 if (op0 != NULL)
3342 return not_reg_cond (op0);
3343 if (! add)
3344 return NULL;
3345 return gen_rtx_AND (0, old, x);
3347 default:
3348 gcc_unreachable ();
3352 /* Given a condition X, remove references to reg REGNO and return the
3353 new condition. The removal will be done so that all conditions
3354 involving REGNO are considered to evaluate to false. This function
3355 is used when the value of REGNO changes. */
3357 static rtx
3358 elim_reg_cond (rtx x, unsigned int regno)
3360 rtx op0, op1;
3362 if (COMPARISON_P (x))
3364 if (REGNO (XEXP (x, 0)) == regno)
3365 return const0_rtx;
3366 return x;
3369 switch (GET_CODE (x))
3371 case AND:
3372 op0 = elim_reg_cond (XEXP (x, 0), regno);
3373 op1 = elim_reg_cond (XEXP (x, 1), regno);
3374 if (op0 == const0_rtx || op1 == const0_rtx)
3375 return const0_rtx;
3376 if (op0 == const1_rtx)
3377 return op1;
3378 if (op1 == const1_rtx)
3379 return op0;
3380 if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
3381 return x;
3382 return gen_rtx_AND (0, op0, op1);
3384 case IOR:
3385 op0 = elim_reg_cond (XEXP (x, 0), regno);
3386 op1 = elim_reg_cond (XEXP (x, 1), regno);
3387 if (op0 == const1_rtx || op1 == const1_rtx)
3388 return const1_rtx;
3389 if (op0 == const0_rtx)
3390 return op1;
3391 if (op1 == const0_rtx)
3392 return op0;
3393 if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
3394 return x;
3395 return gen_rtx_IOR (0, op0, op1);
3397 case NOT:
3398 op0 = elim_reg_cond (XEXP (x, 0), regno);
3399 if (op0 == const0_rtx)
3400 return const1_rtx;
3401 if (op0 == const1_rtx)
3402 return const0_rtx;
3403 if (op0 != XEXP (x, 0))
3404 return not_reg_cond (op0);
3405 return x;
3407 default:
3408 gcc_unreachable ();
3411 #endif /* HAVE_conditional_execution */
3413 #ifdef AUTO_INC_DEC
3415 /* Try to substitute the auto-inc expression INC as the address inside
3416 MEM which occurs in INSN. Currently, the address of MEM is an expression
3417 involving INCR_REG, and INCR is the next use of INCR_REG; it is an insn
3418 that has a single set whose source is a PLUS of INCR_REG and something
3419 else. */
3421 static void
3422 attempt_auto_inc (struct propagate_block_info *pbi, rtx inc, rtx insn,
3423 rtx mem, rtx incr, rtx incr_reg)
3425 int regno = REGNO (incr_reg);
3426 rtx set = single_set (incr);
3427 rtx q = SET_DEST (set);
3428 rtx y = SET_SRC (set);
3429 int opnum = XEXP (y, 0) == incr_reg ? 0 : 1;
3430 int changed;
3432 /* Make sure this reg appears only once in this insn. */
3433 if (count_occurrences (PATTERN (insn), incr_reg, 1) != 1)
3434 return;
3436 if (dead_or_set_p (incr, incr_reg)
3437 /* Mustn't autoinc an eliminable register. */
3438 && (regno >= FIRST_PSEUDO_REGISTER
3439 || ! TEST_HARD_REG_BIT (elim_reg_set, regno)))
3441 /* This is the simple case. Try to make the auto-inc. If
3442 we can't, we are done. Otherwise, we will do any
3443 needed updates below. */
3444 if (! validate_change (insn, &XEXP (mem, 0), inc, 0))
3445 return;
3447 else if (REG_P (q)
3448 /* PREV_INSN used here to check the semi-open interval
3449 [insn,incr). */
3450 && ! reg_used_between_p (q, PREV_INSN (insn), incr)
3451 /* We must also check for sets of q as q may be
3452 a call clobbered hard register and there may
3453 be a call between PREV_INSN (insn) and incr. */
3454 && ! reg_set_between_p (q, PREV_INSN (insn), incr))
3456 /* We have *p followed sometime later by q = p+size.
3457 Both p and q must be live afterward,
3458 and q is not used between INSN and its assignment.
3459 Change it to q = p, ...*q..., q = q+size.
3460 Then fall into the usual case. */
3461 rtx insns, temp;
3463 start_sequence ();
3464 emit_move_insn (q, incr_reg);
3465 insns = get_insns ();
3466 end_sequence ();
3468 /* If we can't make the auto-inc, or can't make the
3469 replacement into Y, exit. There's no point in making
3470 the change below if we can't do the auto-inc and doing
3471 so is not correct in the pre-inc case. */
3473 XEXP (inc, 0) = q;
3474 validate_change (insn, &XEXP (mem, 0), inc, 1);
3475 validate_change (incr, &XEXP (y, opnum), q, 1);
3476 if (! apply_change_group ())
3477 return;
3479 /* We now know we'll be doing this change, so emit the
3480 new insn(s) and do the updates. */
3481 emit_insn_before (insns, insn);
3483 if (BB_HEAD (pbi->bb) == insn)
3484 BB_HEAD (pbi->bb) = insns;
3486 /* INCR will become a NOTE and INSN won't contain a
3487 use of INCR_REG. If a use of INCR_REG was just placed in
3488 the insn before INSN, make that the next use.
3489 Otherwise, invalidate it. */
3490 if (NONJUMP_INSN_P (PREV_INSN (insn))
3491 && GET_CODE (PATTERN (PREV_INSN (insn))) == SET
3492 && SET_SRC (PATTERN (PREV_INSN (insn))) == incr_reg)
3493 pbi->reg_next_use[regno] = PREV_INSN (insn);
3494 else
3495 pbi->reg_next_use[regno] = 0;
3497 incr_reg = q;
3498 regno = REGNO (q);
3500 if ((pbi->flags & PROP_REG_INFO)
3501 && !REGNO_REG_SET_P (pbi->reg_live, regno))
3502 reg_deaths[regno] = pbi->insn_num;
3504 /* REGNO is now used in INCR which is below INSN, but
3505 it previously wasn't live here. If we don't mark
3506 it as live, we'll put a REG_DEAD note for it
3507 on this insn, which is incorrect. */
3508 SET_REGNO_REG_SET (pbi->reg_live, regno);
3510 /* If there are any calls between INSN and INCR, show
3511 that REGNO now crosses them. */
3512 for (temp = insn; temp != incr; temp = NEXT_INSN (temp))
3513 if (CALL_P (temp))
3514 REG_N_CALLS_CROSSED (regno)++;
3516 /* Invalidate alias info for Q since we just changed its value. */
3517 clear_reg_alias_info (q);
3519 else
3520 return;
3522 /* If we haven't returned, it means we were able to make the
3523 auto-inc, so update the status. First, record that this insn
3524 has an implicit side effect. */
3526 REG_NOTES (insn) = alloc_EXPR_LIST (REG_INC, incr_reg, REG_NOTES (insn));
3528 /* Modify the old increment-insn to simply copy
3529 the already-incremented value of our register. */
3530 changed = validate_change (incr, &SET_SRC (set), incr_reg, 0);
3531 gcc_assert (changed);
3533 /* If that makes it a no-op (copying the register into itself) delete
3534 it so it won't appear to be a "use" and a "set" of this
3535 register. */
3536 if (REGNO (SET_DEST (set)) == REGNO (incr_reg))
3538 /* If the original source was dead, it's dead now. */
3539 rtx note;
3541 while ((note = find_reg_note (incr, REG_DEAD, NULL_RTX)) != NULL_RTX)
3543 remove_note (incr, note);
3544 if (XEXP (note, 0) != incr_reg)
3546 unsigned int regno = REGNO (XEXP (note, 0));
3548 if ((pbi->flags & PROP_REG_INFO)
3549 && REGNO_REG_SET_P (pbi->reg_live, regno))
3551 REG_LIVE_LENGTH (regno) += pbi->insn_num - reg_deaths[regno];
3552 reg_deaths[regno] = 0;
3554 CLEAR_REGNO_REG_SET (pbi->reg_live, REGNO (XEXP (note, 0)));
3558 SET_INSN_DELETED (incr);
3561 if (regno >= FIRST_PSEUDO_REGISTER)
3563 /* Count an extra reference to the reg. When a reg is
3564 incremented, spilling it is worse, so we want to make
3565 that less likely. */
3566 REG_FREQ (regno) += REG_FREQ_FROM_BB (pbi->bb);
3568 /* Count the increment as a setting of the register,
3569 even though it isn't a SET in rtl. */
3570 REG_N_SETS (regno)++;
3574 /* X is a MEM found in INSN. See if we can convert it into an auto-increment
3575 reference. */
3577 static void
3578 find_auto_inc (struct propagate_block_info *pbi, rtx x, rtx insn)
3580 rtx addr = XEXP (x, 0);
3581 HOST_WIDE_INT offset = 0;
3582 rtx set, y, incr, inc_val;
3583 int regno;
3584 int size = GET_MODE_SIZE (GET_MODE (x));
3586 if (JUMP_P (insn))
3587 return;
3589 /* Here we detect use of an index register which might be good for
3590 postincrement, postdecrement, preincrement, or predecrement. */
3592 if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
3593 offset = INTVAL (XEXP (addr, 1)), addr = XEXP (addr, 0);
3595 if (!REG_P (addr))
3596 return;
3598 regno = REGNO (addr);
3600 /* Is the next use an increment that might make auto-increment? */
3601 incr = pbi->reg_next_use[regno];
3602 if (incr == 0 || BLOCK_NUM (incr) != BLOCK_NUM (insn))
3603 return;
3604 set = single_set (incr);
3605 if (set == 0 || GET_CODE (set) != SET)
3606 return;
3607 y = SET_SRC (set);
3609 if (GET_CODE (y) != PLUS)
3610 return;
3612 if (REG_P (XEXP (y, 0)) && REGNO (XEXP (y, 0)) == REGNO (addr))
3613 inc_val = XEXP (y, 1);
3614 else if (REG_P (XEXP (y, 1)) && REGNO (XEXP (y, 1)) == REGNO (addr))
3615 inc_val = XEXP (y, 0);
3616 else
3617 return;
3619 if (GET_CODE (inc_val) == CONST_INT)
3621 if (HAVE_POST_INCREMENT
3622 && (INTVAL (inc_val) == size && offset == 0))
3623 attempt_auto_inc (pbi, gen_rtx_POST_INC (Pmode, addr), insn, x,
3624 incr, addr);
3625 else if (HAVE_POST_DECREMENT
3626 && (INTVAL (inc_val) == -size && offset == 0))
3627 attempt_auto_inc (pbi, gen_rtx_POST_DEC (Pmode, addr), insn, x,
3628 incr, addr);
3629 else if (HAVE_PRE_INCREMENT
3630 && (INTVAL (inc_val) == size && offset == size))
3631 attempt_auto_inc (pbi, gen_rtx_PRE_INC (Pmode, addr), insn, x,
3632 incr, addr);
3633 else if (HAVE_PRE_DECREMENT
3634 && (INTVAL (inc_val) == -size && offset == -size))
3635 attempt_auto_inc (pbi, gen_rtx_PRE_DEC (Pmode, addr), insn, x,
3636 incr, addr);
3637 else if (HAVE_POST_MODIFY_DISP && offset == 0)
3638 attempt_auto_inc (pbi, gen_rtx_POST_MODIFY (Pmode, addr,
3639 gen_rtx_PLUS (Pmode,
3640 addr,
3641 inc_val)),
3642 insn, x, incr, addr);
3643 else if (HAVE_PRE_MODIFY_DISP && offset == INTVAL (inc_val))
3644 attempt_auto_inc (pbi, gen_rtx_PRE_MODIFY (Pmode, addr,
3645 gen_rtx_PLUS (Pmode,
3646 addr,
3647 inc_val)),
3648 insn, x, incr, addr);
3650 else if (REG_P (inc_val)
3651 && ! reg_set_between_p (inc_val, PREV_INSN (insn),
3652 NEXT_INSN (incr)))
3655 if (HAVE_POST_MODIFY_REG && offset == 0)
3656 attempt_auto_inc (pbi, gen_rtx_POST_MODIFY (Pmode, addr,
3657 gen_rtx_PLUS (Pmode,
3658 addr,
3659 inc_val)),
3660 insn, x, incr, addr);
3664 #endif /* AUTO_INC_DEC */
3666 static void
3667 mark_used_reg (struct propagate_block_info *pbi, rtx reg,
3668 rtx cond ATTRIBUTE_UNUSED, rtx insn)
3670 unsigned int regno_first, regno_last, i;
3671 int some_was_live, some_was_dead, some_not_set;
3673 regno_last = regno_first = REGNO (reg);
3674 if (regno_first < FIRST_PSEUDO_REGISTER)
3675 regno_last += hard_regno_nregs[regno_first][GET_MODE (reg)] - 1;
3677 /* Find out if any of this register is live after this instruction. */
3678 some_was_live = some_was_dead = 0;
3679 for (i = regno_first; i <= regno_last; ++i)
3681 int needed_regno = REGNO_REG_SET_P (pbi->reg_live, i);
3682 some_was_live |= needed_regno;
3683 some_was_dead |= ! needed_regno;
3686 /* Find out if any of the register was set this insn. */
3687 some_not_set = 0;
3688 for (i = regno_first; i <= regno_last; ++i)
3689 some_not_set |= ! REGNO_REG_SET_P (pbi->new_set, i);
3691 if (pbi->flags & (PROP_LOG_LINKS | PROP_AUTOINC))
3693 /* Record where each reg is used, so when the reg is set we know
3694 the next insn that uses it. */
3695 pbi->reg_next_use[regno_first] = insn;
3698 if (pbi->flags & PROP_REG_INFO)
3700 if (regno_first < FIRST_PSEUDO_REGISTER)
3702 /* If this is a register we are going to try to eliminate,
3703 don't mark it live here. If we are successful in
3704 eliminating it, it need not be live unless it is used for
3705 pseudos, in which case it will have been set live when it
3706 was allocated to the pseudos. If the register will not
3707 be eliminated, reload will set it live at that point.
3709 Otherwise, record that this function uses this register. */
3710 /* ??? The PPC backend tries to "eliminate" on the pic
3711 register to itself. This should be fixed. In the mean
3712 time, hack around it. */
3714 if (! (TEST_HARD_REG_BIT (elim_reg_set, regno_first)
3715 && (regno_first == FRAME_POINTER_REGNUM
3716 || regno_first == ARG_POINTER_REGNUM)))
3717 for (i = regno_first; i <= regno_last; ++i)
3718 regs_ever_live[i] = 1;
3720 else
3722 /* Keep track of which basic block each reg appears in. */
3724 int blocknum = pbi->bb->index;
3725 if (REG_BASIC_BLOCK (regno_first) == REG_BLOCK_UNKNOWN)
3726 REG_BASIC_BLOCK (regno_first) = blocknum;
3727 else if (REG_BASIC_BLOCK (regno_first) != blocknum)
3728 REG_BASIC_BLOCK (regno_first) = REG_BLOCK_GLOBAL;
3730 /* Count (weighted) number of uses of each reg. */
3731 REG_FREQ (regno_first) += REG_FREQ_FROM_BB (pbi->bb);
3732 REG_N_REFS (regno_first)++;
3734 for (i = regno_first; i <= regno_last; ++i)
3735 if (! REGNO_REG_SET_P (pbi->reg_live, i))
3737 gcc_assert (!reg_deaths[i]);
3738 reg_deaths[i] = pbi->insn_num;
3742 /* Record and count the insns in which a reg dies. If it is used in
3743 this insn and was dead below the insn then it dies in this insn.
3744 If it was set in this insn, we do not make a REG_DEAD note;
3745 likewise if we already made such a note. */
3746 if ((pbi->flags & (PROP_DEATH_NOTES | PROP_REG_INFO))
3747 && some_was_dead
3748 && some_not_set)
3750 /* Check for the case where the register dying partially
3751 overlaps the register set by this insn. */
3752 if (regno_first != regno_last)
3753 for (i = regno_first; i <= regno_last; ++i)
3754 some_was_live |= REGNO_REG_SET_P (pbi->new_set, i);
3756 /* If none of the words in X is needed, make a REG_DEAD note.
3757 Otherwise, we must make partial REG_DEAD notes. */
3758 if (! some_was_live)
3760 if ((pbi->flags & PROP_DEATH_NOTES)
3761 && ! find_regno_note (insn, REG_DEAD, regno_first))
3762 REG_NOTES (insn)
3763 = alloc_EXPR_LIST (REG_DEAD, reg, REG_NOTES (insn));
3765 if (pbi->flags & PROP_REG_INFO)
3766 REG_N_DEATHS (regno_first)++;
3768 else
3770 /* Don't make a REG_DEAD note for a part of a register
3771 that is set in the insn. */
3772 for (i = regno_first; i <= regno_last; ++i)
3773 if (! REGNO_REG_SET_P (pbi->reg_live, i)
3774 && ! dead_or_set_regno_p (insn, i))
3775 REG_NOTES (insn)
3776 = alloc_EXPR_LIST (REG_DEAD,
3777 regno_reg_rtx[i],
3778 REG_NOTES (insn));
3782 /* Mark the register as being live. */
3783 for (i = regno_first; i <= regno_last; ++i)
3785 #ifdef HAVE_conditional_execution
3786 int this_was_live = REGNO_REG_SET_P (pbi->reg_live, i);
3787 #endif
3789 SET_REGNO_REG_SET (pbi->reg_live, i);
3791 #ifdef HAVE_conditional_execution
3792 /* If this is a conditional use, record that fact. If it is later
3793 conditionally set, we'll know to kill the register. */
3794 if (cond != NULL_RTX)
3796 splay_tree_node node;
3797 struct reg_cond_life_info *rcli;
3798 rtx ncond;
3800 if (this_was_live)
3802 node = splay_tree_lookup (pbi->reg_cond_dead, i);
3803 if (node == NULL)
3805 /* The register was unconditionally live previously.
3806 No need to do anything. */
3808 else
3810 /* The register was conditionally live previously.
3811 Subtract the new life cond from the old death cond. */
3812 rcli = (struct reg_cond_life_info *) node->value;
3813 ncond = rcli->condition;
3814 ncond = and_reg_cond (ncond, not_reg_cond (cond), 1);
3816 /* If the register is now unconditionally live,
3817 remove the entry in the splay_tree. */
3818 if (ncond == const0_rtx)
3819 splay_tree_remove (pbi->reg_cond_dead, i);
3820 else
3822 rcli->condition = ncond;
3823 SET_REGNO_REG_SET (pbi->reg_cond_reg,
3824 REGNO (XEXP (cond, 0)));
3828 else
3830 /* The register was not previously live at all. Record
3831 the condition under which it is still dead. */
3832 rcli = xmalloc (sizeof (*rcli));
3833 rcli->condition = not_reg_cond (cond);
3834 rcli->stores = const0_rtx;
3835 rcli->orig_condition = const0_rtx;
3836 splay_tree_insert (pbi->reg_cond_dead, i,
3837 (splay_tree_value) rcli);
3839 SET_REGNO_REG_SET (pbi->reg_cond_reg, REGNO (XEXP (cond, 0)));
3842 else if (this_was_live)
3844 /* The register may have been conditionally live previously, but
3845 is now unconditionally live. Remove it from the conditionally
3846 dead list, so that a conditional set won't cause us to think
3847 it dead. */
3848 splay_tree_remove (pbi->reg_cond_dead, i);
3850 #endif
3854 /* Scan expression X and store a 1-bit in NEW_LIVE for each reg it uses.
3855 This is done assuming the registers needed from X are those that
3856 have 1-bits in PBI->REG_LIVE.
3858 INSN is the containing instruction. If INSN is dead, this function
3859 is not called. */
3861 static void
3862 mark_used_regs (struct propagate_block_info *pbi, rtx x, rtx cond, rtx insn)
3864 RTX_CODE code;
3865 int regno;
3866 int flags = pbi->flags;
3868 retry:
3869 if (!x)
3870 return;
3871 code = GET_CODE (x);
3872 switch (code)
3874 case LABEL_REF:
3875 case SYMBOL_REF:
3876 case CONST_INT:
3877 case CONST:
3878 case CONST_DOUBLE:
3879 case CONST_VECTOR:
3880 case PC:
3881 case ADDR_VEC:
3882 case ADDR_DIFF_VEC:
3883 return;
3885 #ifdef HAVE_cc0
3886 case CC0:
3887 pbi->cc0_live = 1;
3888 return;
3889 #endif
3891 case CLOBBER:
3892 /* If we are clobbering a MEM, mark any registers inside the address
3893 as being used. */
3894 if (MEM_P (XEXP (x, 0)))
3895 mark_used_regs (pbi, XEXP (XEXP (x, 0), 0), cond, insn);
3896 return;
3898 case MEM:
3899 /* Don't bother watching stores to mems if this is not the
3900 final pass. We'll not be deleting dead stores this round. */
3901 if (optimize && (flags & PROP_SCAN_DEAD_STORES))
3903 /* Invalidate the data for the last MEM stored, but only if MEM is
3904 something that can be stored into. */
3905 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
3906 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
3907 /* Needn't clear the memory set list. */
3909 else
3911 rtx temp = pbi->mem_set_list;
3912 rtx prev = NULL_RTX;
3913 rtx next;
3915 while (temp)
3917 next = XEXP (temp, 1);
3918 if (anti_dependence (XEXP (temp, 0), x))
3920 /* Splice temp out of the list. */
3921 if (prev)
3922 XEXP (prev, 1) = next;
3923 else
3924 pbi->mem_set_list = next;
3925 free_EXPR_LIST_node (temp);
3926 pbi->mem_set_list_len--;
3928 else
3929 prev = temp;
3930 temp = next;
3934 /* If the memory reference had embedded side effects (autoincrement
3935 address modes. Then we may need to kill some entries on the
3936 memory set list. */
3937 if (insn)
3938 for_each_rtx (&PATTERN (insn), invalidate_mems_from_autoinc, pbi);
3941 #ifdef AUTO_INC_DEC
3942 if (flags & PROP_AUTOINC)
3943 find_auto_inc (pbi, x, insn);
3944 #endif
3945 break;
3947 case SUBREG:
3948 #ifdef CANNOT_CHANGE_MODE_CLASS
3949 if (flags & PROP_REG_INFO)
3950 record_subregs_of_mode (x);
3951 #endif
3953 /* While we're here, optimize this case. */
3954 x = SUBREG_REG (x);
3955 if (!REG_P (x))
3956 goto retry;
3957 /* Fall through. */
3959 case REG:
3960 /* See a register other than being set => mark it as needed. */
3961 mark_used_reg (pbi, x, cond, insn);
3962 return;
3964 case SET:
3966 rtx testreg = SET_DEST (x);
3967 int mark_dest = 0;
3969 /* If storing into MEM, don't show it as being used. But do
3970 show the address as being used. */
3971 if (MEM_P (testreg))
3973 #ifdef AUTO_INC_DEC
3974 if (flags & PROP_AUTOINC)
3975 find_auto_inc (pbi, testreg, insn);
3976 #endif
3977 mark_used_regs (pbi, XEXP (testreg, 0), cond, insn);
3978 mark_used_regs (pbi, SET_SRC (x), cond, insn);
3979 return;
3982 /* Storing in STRICT_LOW_PART is like storing in a reg
3983 in that this SET might be dead, so ignore it in TESTREG.
3984 but in some other ways it is like using the reg.
3986 Storing in a SUBREG or a bit field is like storing the entire
3987 register in that if the register's value is not used
3988 then this SET is not needed. */
3989 while (GET_CODE (testreg) == STRICT_LOW_PART
3990 || GET_CODE (testreg) == ZERO_EXTRACT
3991 || GET_CODE (testreg) == SUBREG)
3993 #ifdef CANNOT_CHANGE_MODE_CLASS
3994 if ((flags & PROP_REG_INFO) && GET_CODE (testreg) == SUBREG)
3995 record_subregs_of_mode (testreg);
3996 #endif
3998 /* Modifying a single register in an alternate mode
3999 does not use any of the old value. But these other
4000 ways of storing in a register do use the old value. */
4001 if (GET_CODE (testreg) == SUBREG
4002 && !((REG_BYTES (SUBREG_REG (testreg))
4003 + UNITS_PER_WORD - 1) / UNITS_PER_WORD
4004 > (REG_BYTES (testreg)
4005 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))
4007 else
4008 mark_dest = 1;
4010 testreg = XEXP (testreg, 0);
4013 /* If this is a store into a register or group of registers,
4014 recursively scan the value being stored. */
4016 if ((GET_CODE (testreg) == PARALLEL
4017 && GET_MODE (testreg) == BLKmode)
4018 || (REG_P (testreg)
4019 && (regno = REGNO (testreg),
4020 ! (regno == FRAME_POINTER_REGNUM
4021 && (! reload_completed || frame_pointer_needed)))
4022 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
4023 && ! (regno == HARD_FRAME_POINTER_REGNUM
4024 && (! reload_completed || frame_pointer_needed))
4025 #endif
4026 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
4027 && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
4028 #endif
4031 if (mark_dest)
4032 mark_used_regs (pbi, SET_DEST (x), cond, insn);
4033 mark_used_regs (pbi, SET_SRC (x), cond, insn);
4034 return;
4037 break;
4039 case ASM_OPERANDS:
4040 case UNSPEC_VOLATILE:
4041 case TRAP_IF:
4042 case ASM_INPUT:
4044 /* Traditional and volatile asm instructions must be considered to use
4045 and clobber all hard registers, all pseudo-registers and all of
4046 memory. So must TRAP_IF and UNSPEC_VOLATILE operations.
4048 Consider for instance a volatile asm that changes the fpu rounding
4049 mode. An insn should not be moved across this even if it only uses
4050 pseudo-regs because it might give an incorrectly rounded result.
4052 ?!? Unfortunately, marking all hard registers as live causes massive
4053 problems for the register allocator and marking all pseudos as live
4054 creates mountains of uninitialized variable warnings.
4056 So for now, just clear the memory set list and mark any regs
4057 we can find in ASM_OPERANDS as used. */
4058 if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
4060 free_EXPR_LIST_list (&pbi->mem_set_list);
4061 pbi->mem_set_list_len = 0;
4064 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
4065 We can not just fall through here since then we would be confused
4066 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
4067 traditional asms unlike their normal usage. */
4068 if (code == ASM_OPERANDS)
4070 int j;
4072 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
4073 mark_used_regs (pbi, ASM_OPERANDS_INPUT (x, j), cond, insn);
4075 break;
4078 case COND_EXEC:
4079 gcc_assert (!cond);
4081 mark_used_regs (pbi, COND_EXEC_TEST (x), NULL_RTX, insn);
4083 cond = COND_EXEC_TEST (x);
4084 x = COND_EXEC_CODE (x);
4085 goto retry;
4087 default:
4088 break;
4091 /* Recursively scan the operands of this expression. */
4094 const char * const fmt = GET_RTX_FORMAT (code);
4095 int i;
4097 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4099 if (fmt[i] == 'e')
4101 /* Tail recursive case: save a function call level. */
4102 if (i == 0)
4104 x = XEXP (x, 0);
4105 goto retry;
4107 mark_used_regs (pbi, XEXP (x, i), cond, insn);
4109 else if (fmt[i] == 'E')
4111 int j;
4112 for (j = 0; j < XVECLEN (x, i); j++)
4113 mark_used_regs (pbi, XVECEXP (x, i, j), cond, insn);
4119 #ifdef AUTO_INC_DEC
4121 static int
4122 try_pre_increment_1 (struct propagate_block_info *pbi, rtx insn)
4124 /* Find the next use of this reg. If in same basic block,
4125 make it do pre-increment or pre-decrement if appropriate. */
4126 rtx x = single_set (insn);
4127 HOST_WIDE_INT amount = ((GET_CODE (SET_SRC (x)) == PLUS ? 1 : -1)
4128 * INTVAL (XEXP (SET_SRC (x), 1)));
4129 int regno = REGNO (SET_DEST (x));
4130 rtx y = pbi->reg_next_use[regno];
4131 if (y != 0
4132 && SET_DEST (x) != stack_pointer_rtx
4133 && BLOCK_NUM (y) == BLOCK_NUM (insn)
4134 /* Don't do this if the reg dies, or gets set in y; a standard addressing
4135 mode would be better. */
4136 && ! dead_or_set_p (y, SET_DEST (x))
4137 && try_pre_increment (y, SET_DEST (x), amount))
4139 /* We have found a suitable auto-increment and already changed
4140 insn Y to do it. So flush this increment instruction. */
4141 propagate_block_delete_insn (insn);
4143 /* Count a reference to this reg for the increment insn we are
4144 deleting. When a reg is incremented, spilling it is worse,
4145 so we want to make that less likely. */
4146 if (regno >= FIRST_PSEUDO_REGISTER)
4148 REG_FREQ (regno) += REG_FREQ_FROM_BB (pbi->bb);
4149 REG_N_SETS (regno)++;
4152 /* Flush any remembered memories depending on the value of
4153 the incremented register. */
4154 invalidate_mems_from_set (pbi, SET_DEST (x));
4156 return 1;
4158 return 0;
4161 /* Try to change INSN so that it does pre-increment or pre-decrement
4162 addressing on register REG in order to add AMOUNT to REG.
4163 AMOUNT is negative for pre-decrement.
4164 Returns 1 if the change could be made.
4165 This checks all about the validity of the result of modifying INSN. */
4167 static int
4168 try_pre_increment (rtx insn, rtx reg, HOST_WIDE_INT amount)
4170 rtx use;
4172 /* Nonzero if we can try to make a pre-increment or pre-decrement.
4173 For example, addl $4,r1; movl (r1),... can become movl +(r1),... */
4174 int pre_ok = 0;
4175 /* Nonzero if we can try to make a post-increment or post-decrement.
4176 For example, addl $4,r1; movl -4(r1),... can become movl (r1)+,...
4177 It is possible for both PRE_OK and POST_OK to be nonzero if the machine
4178 supports both pre-inc and post-inc, or both pre-dec and post-dec. */
4179 int post_ok = 0;
4181 /* Nonzero if the opportunity actually requires post-inc or post-dec. */
4182 int do_post = 0;
4184 /* From the sign of increment, see which possibilities are conceivable
4185 on this target machine. */
4186 if (HAVE_PRE_INCREMENT && amount > 0)
4187 pre_ok = 1;
4188 if (HAVE_POST_INCREMENT && amount > 0)
4189 post_ok = 1;
4191 if (HAVE_PRE_DECREMENT && amount < 0)
4192 pre_ok = 1;
4193 if (HAVE_POST_DECREMENT && amount < 0)
4194 post_ok = 1;
4196 if (! (pre_ok || post_ok))
4197 return 0;
4199 /* It is not safe to add a side effect to a jump insn
4200 because if the incremented register is spilled and must be reloaded
4201 there would be no way to store the incremented value back in memory. */
4203 if (JUMP_P (insn))
4204 return 0;
4206 use = 0;
4207 if (pre_ok)
4208 use = find_use_as_address (PATTERN (insn), reg, 0);
4209 if (post_ok && (use == 0 || use == (rtx) (size_t) 1))
4211 use = find_use_as_address (PATTERN (insn), reg, -amount);
4212 do_post = 1;
4215 if (use == 0 || use == (rtx) (size_t) 1)
4216 return 0;
4218 if (GET_MODE_SIZE (GET_MODE (use)) != (amount > 0 ? amount : - amount))
4219 return 0;
4221 /* See if this combination of instruction and addressing mode exists. */
4222 if (! validate_change (insn, &XEXP (use, 0),
4223 gen_rtx_fmt_e (amount > 0
4224 ? (do_post ? POST_INC : PRE_INC)
4225 : (do_post ? POST_DEC : PRE_DEC),
4226 Pmode, reg), 0))
4227 return 0;
4229 /* Record that this insn now has an implicit side effect on X. */
4230 REG_NOTES (insn) = alloc_EXPR_LIST (REG_INC, reg, REG_NOTES (insn));
4231 return 1;
4234 #endif /* AUTO_INC_DEC */
4236 /* Find the place in the rtx X where REG is used as a memory address.
4237 Return the MEM rtx that so uses it.
4238 If PLUSCONST is nonzero, search instead for a memory address equivalent to
4239 (plus REG (const_int PLUSCONST)).
4241 If such an address does not appear, return 0.
4242 If REG appears more than once, or is used other than in such an address,
4243 return (rtx) 1. */
4246 find_use_as_address (rtx x, rtx reg, HOST_WIDE_INT plusconst)
4248 enum rtx_code code = GET_CODE (x);
4249 const char * const fmt = GET_RTX_FORMAT (code);
4250 int i;
4251 rtx value = 0;
4252 rtx tem;
4254 if (code == MEM && XEXP (x, 0) == reg && plusconst == 0)
4255 return x;
4257 if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
4258 && XEXP (XEXP (x, 0), 0) == reg
4259 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4260 && INTVAL (XEXP (XEXP (x, 0), 1)) == plusconst)
4261 return x;
4263 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
4265 /* If REG occurs inside a MEM used in a bit-field reference,
4266 that is unacceptable. */
4267 if (find_use_as_address (XEXP (x, 0), reg, 0) != 0)
4268 return (rtx) (size_t) 1;
4271 if (x == reg)
4272 return (rtx) (size_t) 1;
4274 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4276 if (fmt[i] == 'e')
4278 tem = find_use_as_address (XEXP (x, i), reg, plusconst);
4279 if (value == 0)
4280 value = tem;
4281 else if (tem != 0)
4282 return (rtx) (size_t) 1;
4284 else if (fmt[i] == 'E')
4286 int j;
4287 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4289 tem = find_use_as_address (XVECEXP (x, i, j), reg, plusconst);
4290 if (value == 0)
4291 value = tem;
4292 else if (tem != 0)
4293 return (rtx) (size_t) 1;
4298 return value;
4301 /* Write information about registers and basic blocks into FILE.
4302 This is part of making a debugging dump. */
4304 void
4305 dump_regset (regset r, FILE *outf)
4307 unsigned i;
4308 reg_set_iterator rsi;
4310 if (r == NULL)
4312 fputs (" (nil)", outf);
4313 return;
4316 EXECUTE_IF_SET_IN_REG_SET (r, 0, i, rsi)
4318 fprintf (outf, " %d", i);
4319 if (i < FIRST_PSEUDO_REGISTER)
4320 fprintf (outf, " [%s]",
4321 reg_names[i]);
4325 /* Print a human-readable representation of R on the standard error
4326 stream. This function is designed to be used from within the
4327 debugger. */
4329 void
4330 debug_regset (regset r)
4332 dump_regset (r, stderr);
4333 putc ('\n', stderr);
4336 /* Recompute register set/reference counts immediately prior to register
4337 allocation.
4339 This avoids problems with set/reference counts changing to/from values
4340 which have special meanings to the register allocators.
4342 Additionally, the reference counts are the primary component used by the
4343 register allocators to prioritize pseudos for allocation to hard regs.
4344 More accurate reference counts generally lead to better register allocation.
4346 It might be worthwhile to update REG_LIVE_LENGTH, REG_BASIC_BLOCK and
4347 possibly other information which is used by the register allocators. */
4349 void
4350 recompute_reg_usage (void)
4352 allocate_reg_life_data ();
4353 /* distribute_notes in combiner fails to convert some of the
4354 REG_UNUSED notes to REG_DEAD notes. This causes CHECK_DEAD_NOTES
4355 in sched1 to die. To solve this update the DEATH_NOTES
4356 here. */
4357 update_life_info (NULL, UPDATE_LIFE_LOCAL, PROP_REG_INFO | PROP_DEATH_NOTES);
4360 /* Optionally removes all the REG_DEAD and REG_UNUSED notes from a set of
4361 blocks. If BLOCKS is NULL, assume the universal set. Returns a count
4362 of the number of registers that died. */
4365 count_or_remove_death_notes (sbitmap blocks, int kill)
4367 int count = 0;
4368 unsigned int i;
4369 basic_block bb;
4371 /* This used to be a loop over all the blocks with a membership test
4372 inside the loop. That can be amazingly expensive on a large CFG
4373 when only a small number of bits are set in BLOCKs (for example,
4374 the calls from the scheduler typically have very few bits set).
4376 For extra credit, someone should convert BLOCKS to a bitmap rather
4377 than an sbitmap. */
4378 if (blocks)
4380 sbitmap_iterator sbi;
4382 EXECUTE_IF_SET_IN_SBITMAP (blocks, 0, i, sbi)
4384 count += count_or_remove_death_notes_bb (BASIC_BLOCK (i), kill);
4387 else
4389 FOR_EACH_BB (bb)
4391 count += count_or_remove_death_notes_bb (bb, kill);
4395 return count;
4398 /* Optionally removes all the REG_DEAD and REG_UNUSED notes from basic
4399 block BB. Returns a count of the number of registers that died. */
4401 static int
4402 count_or_remove_death_notes_bb (basic_block bb, int kill)
4404 int count = 0;
4405 rtx insn;
4407 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
4409 if (INSN_P (insn))
4411 rtx *pprev = &REG_NOTES (insn);
4412 rtx link = *pprev;
4414 while (link)
4416 switch (REG_NOTE_KIND (link))
4418 case REG_DEAD:
4419 if (REG_P (XEXP (link, 0)))
4421 rtx reg = XEXP (link, 0);
4422 int n;
4424 if (REGNO (reg) >= FIRST_PSEUDO_REGISTER)
4425 n = 1;
4426 else
4427 n = hard_regno_nregs[REGNO (reg)][GET_MODE (reg)];
4428 count += n;
4431 /* Fall through. */
4433 case REG_UNUSED:
4434 if (kill)
4436 rtx next = XEXP (link, 1);
4437 free_EXPR_LIST_node (link);
4438 *pprev = link = next;
4439 break;
4441 /* Fall through. */
4443 default:
4444 pprev = &XEXP (link, 1);
4445 link = *pprev;
4446 break;
4451 if (insn == BB_END (bb))
4452 break;
4455 return count;
4458 /* Clear LOG_LINKS fields of insns in a selected blocks or whole chain
4459 if blocks is NULL. */
4461 static void
4462 clear_log_links (sbitmap blocks)
4464 rtx insn;
4466 if (!blocks)
4468 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4469 if (INSN_P (insn))
4470 free_INSN_LIST_list (&LOG_LINKS (insn));
4472 else
4474 unsigned int i;
4475 sbitmap_iterator sbi;
4477 EXECUTE_IF_SET_IN_SBITMAP (blocks, 0, i, sbi)
4479 basic_block bb = BASIC_BLOCK (i);
4481 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb));
4482 insn = NEXT_INSN (insn))
4483 if (INSN_P (insn))
4484 free_INSN_LIST_list (&LOG_LINKS (insn));
4489 /* Given a register bitmap, turn on the bits in a HARD_REG_SET that
4490 correspond to the hard registers, if any, set in that map. This
4491 could be done far more efficiently by having all sorts of special-cases
4492 with moving single words, but probably isn't worth the trouble. */
4494 void
4495 reg_set_to_hard_reg_set (HARD_REG_SET *to, bitmap from)
4497 unsigned i;
4498 bitmap_iterator bi;
4500 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
4502 if (i >= FIRST_PSEUDO_REGISTER)
4503 return;
4504 SET_HARD_REG_BIT (*to, i);