OSDN Git Service

* de.po: Update.
[pf3gnuchains/gcc-fork.git] / gcc / flow.c
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 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
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.
11
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.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
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.
25
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.
29
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.
33
34    ** find_basic_blocks **
35
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.
40
41    find_basic_blocks also finds any unreachable loops and deletes them.
42
43    ** life_analysis **
44
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.
48
49    ** live-register info **
50
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.
53
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.
58
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).
65
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.
76
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.
83
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.
87
88    ** Other actions of life_analysis **
89
90    life_analysis sets up the LOG_LINKS fields of insns because the
91    information needed to do so is readily available.
92
93    life_analysis deletes insns whose only effect is to store a value
94    that is never used.
95
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.
101
102    Each time an incrementing or decrementing address is created,
103    a REG_INC element is added to the insn's REG_NOTES list.
104
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.
108
109    life_analysis sets current_function_sp_is_unchanging if the function
110    doesn't modify the stack pointer.  */
111
112 /* TODO:
113
114    Split out from life_analysis:
115         - local property discovery (bb->local_live, bb->local_set)
116         - global property computation
117         - log links creation
118         - pre/post modify transformation
119 */
120 \f
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"
140
141 #include "obstack.h"
142 #include "splay-tree.h"
143
144 /* EXIT_IGNORE_STACK should be nonzero if, when returning from a function,
145    the stack pointer does not matter.  The value is tested only in
146    functions that have frame pointers.
147    No definition is equivalent to always zero.  */
148 #ifndef EXIT_IGNORE_STACK
149 #define EXIT_IGNORE_STACK 0
150 #endif
151
152 #ifndef HAVE_epilogue
153 #define HAVE_epilogue 0
154 #endif
155 #ifndef HAVE_prologue
156 #define HAVE_prologue 0
157 #endif
158 #ifndef HAVE_sibcall_epilogue
159 #define HAVE_sibcall_epilogue 0
160 #endif
161
162 #ifndef LOCAL_REGNO
163 #define LOCAL_REGNO(REGNO)  0
164 #endif
165 #ifndef EPILOGUE_USES
166 #define EPILOGUE_USES(REGNO)  0
167 #endif
168 #ifndef EH_USES
169 #define EH_USES(REGNO)  0
170 #endif
171
172 #ifdef HAVE_conditional_execution
173 #ifndef REVERSE_CONDEXEC_PREDICATES_P
174 #define REVERSE_CONDEXEC_PREDICATES_P(x, y) ((x) == reverse_condition (y))
175 #endif
176 #endif
177
178 /* Nonzero if the second flow pass has completed.  */
179 int flow2_completed;
180
181 /* Maximum register number used in this function, plus one.  */
182
183 int max_regno;
184
185 /* Indexed by n, giving various register information */
186
187 varray_type reg_n_info;
188
189 /* Size of a regset for the current function,
190    in (1) bytes and (2) elements.  */
191
192 int regset_bytes;
193 int regset_size;
194
195 /* Regset of regs live when calls to `setjmp'-like functions happen.  */
196 /* ??? Does this exist only for the setjmp-clobbered warning message?  */
197
198 regset regs_live_at_setjmp;
199
200 /* List made of EXPR_LIST rtx's which gives pairs of pseudo registers
201    that have to go in the same hard reg.
202    The first two regs in the list are a pair, and the next two
203    are another pair, etc.  */
204 rtx regs_may_share;
205
206 /* Callback that determines if it's ok for a function to have no
207    noreturn attribute.  */
208 int (*lang_missing_noreturn_ok_p) (tree);
209
210 /* Set of registers that may be eliminable.  These are handled specially
211    in updating regs_ever_live.  */
212
213 static HARD_REG_SET elim_reg_set;
214
215 /* Holds information for tracking conditional register life information.  */
216 struct reg_cond_life_info
217 {
218   /* A boolean expression of conditions under which a register is dead.  */
219   rtx condition;
220   /* Conditions under which a register is dead at the basic block end.  */
221   rtx orig_condition;
222
223   /* A boolean expression of conditions under which a register has been
224      stored into.  */
225   rtx stores;
226
227   /* ??? Could store mask of bytes that are dead, so that we could finally
228      track lifetimes of multi-word registers accessed via subregs.  */
229 };
230
231 /* For use in communicating between propagate_block and its subroutines.
232    Holds all information needed to compute life and def-use information.  */
233
234 struct propagate_block_info
235 {
236   /* The basic block we're considering.  */
237   basic_block bb;
238
239   /* Bit N is set if register N is conditionally or unconditionally live.  */
240   regset reg_live;
241
242   /* Bit N is set if register N is set this insn.  */
243   regset new_set;
244
245   /* Element N is the next insn that uses (hard or pseudo) register N
246      within the current basic block; or zero, if there is no such insn.  */
247   rtx *reg_next_use;
248
249   /* Contains a list of all the MEMs we are tracking for dead store
250      elimination.  */
251   rtx mem_set_list;
252
253   /* If non-null, record the set of registers set unconditionally in the
254      basic block.  */
255   regset local_set;
256
257   /* If non-null, record the set of registers set conditionally in the
258      basic block.  */
259   regset cond_local_set;
260
261 #ifdef HAVE_conditional_execution
262   /* Indexed by register number, holds a reg_cond_life_info for each
263      register that is not unconditionally live or dead.  */
264   splay_tree reg_cond_dead;
265
266   /* Bit N is set if register N is in an expression in reg_cond_dead.  */
267   regset reg_cond_reg;
268 #endif
269
270   /* The length of mem_set_list.  */
271   int mem_set_list_len;
272
273   /* Nonzero if the value of CC0 is live.  */
274   int cc0_live;
275
276   /* Flags controlling the set of information propagate_block collects.  */
277   int flags;
278 };
279
280 /* Number of dead insns removed.  */
281 static int ndead;
282
283 /* Maximum length of pbi->mem_set_list before we start dropping
284    new elements on the floor.  */
285 #define MAX_MEM_SET_LIST_LEN    100
286
287 /* Forward declarations */
288 static int verify_wide_reg_1 (rtx *, void *);
289 static void verify_wide_reg (int, basic_block);
290 static void verify_local_live_at_start (regset, basic_block);
291 static void notice_stack_pointer_modification_1 (rtx, rtx, void *);
292 static void notice_stack_pointer_modification (rtx);
293 static void mark_reg (rtx, void *);
294 static void mark_regs_live_at_end (regset);
295 static void calculate_global_regs_live (sbitmap, sbitmap, int);
296 static void propagate_block_delete_insn (rtx);
297 static rtx propagate_block_delete_libcall (rtx, rtx);
298 static int insn_dead_p (struct propagate_block_info *, rtx, int, rtx);
299 static int libcall_dead_p (struct propagate_block_info *, rtx, rtx);
300 static void mark_set_regs (struct propagate_block_info *, rtx, rtx);
301 static void mark_set_1 (struct propagate_block_info *, enum rtx_code, rtx,
302                         rtx, rtx, int);
303 static int find_regno_partial (rtx *, void *);
304
305 #ifdef HAVE_conditional_execution
306 static int mark_regno_cond_dead (struct propagate_block_info *, int, rtx);
307 static void free_reg_cond_life_info (splay_tree_value);
308 static int flush_reg_cond_reg_1 (splay_tree_node, void *);
309 static void flush_reg_cond_reg (struct propagate_block_info *, int);
310 static rtx elim_reg_cond (rtx, unsigned int);
311 static rtx ior_reg_cond (rtx, rtx, int);
312 static rtx not_reg_cond (rtx);
313 static rtx and_reg_cond (rtx, rtx, int);
314 #endif
315 #ifdef AUTO_INC_DEC
316 static void attempt_auto_inc (struct propagate_block_info *, rtx, rtx, rtx,
317                               rtx, rtx);
318 static void find_auto_inc (struct propagate_block_info *, rtx, rtx);
319 static int try_pre_increment_1 (struct propagate_block_info *, rtx);
320 static int try_pre_increment (rtx, rtx, HOST_WIDE_INT);
321 #endif
322 static void mark_used_reg (struct propagate_block_info *, rtx, rtx, rtx);
323 static void mark_used_regs (struct propagate_block_info *, rtx, rtx, rtx);
324 void debug_flow_info (void);
325 static void add_to_mem_set_list (struct propagate_block_info *, rtx);
326 static int invalidate_mems_from_autoinc (rtx *, void *);
327 static void invalidate_mems_from_set (struct propagate_block_info *, rtx);
328 static void clear_log_links (sbitmap);
329 \f
330
331 void
332 check_function_return_warnings (void)
333 {
334   if (warn_missing_noreturn
335       && !TREE_THIS_VOLATILE (cfun->decl)
336       && EXIT_BLOCK_PTR->pred == NULL
337       && (lang_missing_noreturn_ok_p
338           && !lang_missing_noreturn_ok_p (cfun->decl)))
339     warning ("function might be possible candidate for attribute `noreturn'");
340
341   /* If we have a path to EXIT, then we do return.  */
342   if (TREE_THIS_VOLATILE (cfun->decl)
343       && EXIT_BLOCK_PTR->pred != NULL)
344     warning ("`noreturn' function does return");
345
346   /* If the clobber_return_insn appears in some basic block, then we
347      do reach the end without returning a value.  */
348   else if (warn_return_type
349            && cfun->x_clobber_return_insn != NULL
350            && EXIT_BLOCK_PTR->pred != NULL)
351     {
352       int max_uid = get_max_uid ();
353
354       /* If clobber_return_insn was excised by jump1, then renumber_insns
355          can make max_uid smaller than the number still recorded in our rtx.
356          That's fine, since this is a quick way of verifying that the insn
357          is no longer in the chain.  */
358       if (INSN_UID (cfun->x_clobber_return_insn) < max_uid)
359         {
360           rtx insn;
361
362           for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
363             if (insn == cfun->x_clobber_return_insn)
364               {
365                 warning ("control reaches end of non-void function");
366                 break;
367               }
368         }
369     }
370 }
371 \f
372 /* Return the INSN immediately following the NOTE_INSN_BASIC_BLOCK
373    note associated with the BLOCK.  */
374
375 rtx
376 first_insn_after_basic_block_note (basic_block block)
377 {
378   rtx insn;
379
380   /* Get the first instruction in the block.  */
381   insn = block->head;
382
383   if (insn == NULL_RTX)
384     return NULL_RTX;
385   if (GET_CODE (insn) == CODE_LABEL)
386     insn = NEXT_INSN (insn);
387   if (!NOTE_INSN_BASIC_BLOCK_P (insn))
388     abort ();
389
390   return NEXT_INSN (insn);
391 }
392 \f
393 /* Perform data flow analysis.
394    F is the first insn of the function; FLAGS is a set of PROP_* flags
395    to be used in accumulating flow info.  */
396
397 void
398 life_analysis (rtx f, FILE *file, int flags)
399 {
400 #ifdef ELIMINABLE_REGS
401   int i;
402   static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
403 #endif
404
405   /* Record which registers will be eliminated.  We use this in
406      mark_used_regs.  */
407
408   CLEAR_HARD_REG_SET (elim_reg_set);
409
410 #ifdef ELIMINABLE_REGS
411   for (i = 0; i < (int) ARRAY_SIZE (eliminables); i++)
412     SET_HARD_REG_BIT (elim_reg_set, eliminables[i].from);
413 #else
414   SET_HARD_REG_BIT (elim_reg_set, FRAME_POINTER_REGNUM);
415 #endif
416
417
418 #ifdef CANNOT_CHANGE_MODE_CLASS
419   if (flags & PROP_REG_INFO)
420     bitmap_initialize (&subregs_of_mode, 1);
421 #endif
422
423   if (! optimize)
424     flags &= ~(PROP_LOG_LINKS | PROP_AUTOINC | PROP_ALLOW_CFG_CHANGES);
425
426   /* The post-reload life analysis have (on a global basis) the same
427      registers live as was computed by reload itself.  elimination
428      Otherwise offsets and such may be incorrect.
429
430      Reload will make some registers as live even though they do not
431      appear in the rtl.
432
433      We don't want to create new auto-incs after reload, since they
434      are unlikely to be useful and can cause problems with shared
435      stack slots.  */
436   if (reload_completed)
437     flags &= ~(PROP_REG_INFO | PROP_AUTOINC);
438
439   /* We want alias analysis information for local dead store elimination.  */
440   if (optimize && (flags & PROP_SCAN_DEAD_STORES))
441     init_alias_analysis ();
442
443   /* Always remove no-op moves.  Do this before other processing so
444      that we don't have to keep re-scanning them.  */
445   delete_noop_moves (f);
446
447   /* Some targets can emit simpler epilogues if they know that sp was
448      not ever modified during the function.  After reload, of course,
449      we've already emitted the epilogue so there's no sense searching.  */
450   if (! reload_completed)
451     notice_stack_pointer_modification (f);
452
453   /* Allocate and zero out data structures that will record the
454      data from lifetime analysis.  */
455   allocate_reg_life_data ();
456   allocate_bb_life_data ();
457
458   /* Find the set of registers live on function exit.  */
459   mark_regs_live_at_end (EXIT_BLOCK_PTR->global_live_at_start);
460
461   /* "Update" life info from zero.  It'd be nice to begin the
462      relaxation with just the exit and noreturn blocks, but that set
463      is not immediately handy.  */
464
465   if (flags & PROP_REG_INFO)
466     memset (regs_ever_live, 0, sizeof (regs_ever_live));
467   update_life_info (NULL, UPDATE_LIFE_GLOBAL, flags);
468
469   /* Clean up.  */
470   if (optimize && (flags & PROP_SCAN_DEAD_STORES))
471     end_alias_analysis ();
472
473   if (file)
474     dump_flow_info (file);
475
476   free_basic_block_vars (1);
477
478   /* Removing dead insns should've made jumptables really dead.  */
479   delete_dead_jumptables ();
480 }
481
482 /* A subroutine of verify_wide_reg, called through for_each_rtx.
483    Search for REGNO.  If found, return 2 if it is not wider than
484    word_mode.  */
485
486 static int
487 verify_wide_reg_1 (rtx *px, void *pregno)
488 {
489   rtx x = *px;
490   unsigned int regno = *(int *) pregno;
491
492   if (GET_CODE (x) == REG && REGNO (x) == regno)
493     {
494       if (GET_MODE_BITSIZE (GET_MODE (x)) <= BITS_PER_WORD)
495         return 2;
496       return 1;
497     }
498   return 0;
499 }
500
501 /* A subroutine of verify_local_live_at_start.  Search through insns
502    of BB looking for register REGNO.  */
503
504 static void
505 verify_wide_reg (int regno, basic_block bb)
506 {
507   rtx head = bb->head, end = bb->end;
508
509   while (1)
510     {
511       if (INSN_P (head))
512         {
513           int r = for_each_rtx (&PATTERN (head), verify_wide_reg_1, &regno);
514           if (r == 1)
515             return;
516           if (r == 2)
517             break;
518         }
519       if (head == end)
520         break;
521       head = NEXT_INSN (head);
522     }
523
524   if (rtl_dump_file)
525     {
526       fprintf (rtl_dump_file, "Register %d died unexpectedly.\n", regno);
527       dump_bb (bb, rtl_dump_file);
528     }
529   abort ();
530 }
531
532 /* A subroutine of update_life_info.  Verify that there are no untoward
533    changes in live_at_start during a local update.  */
534
535 static void
536 verify_local_live_at_start (regset new_live_at_start, basic_block bb)
537 {
538   if (reload_completed)
539     {
540       /* After reload, there are no pseudos, nor subregs of multi-word
541          registers.  The regsets should exactly match.  */
542       if (! REG_SET_EQUAL_P (new_live_at_start, bb->global_live_at_start))
543         {
544           if (rtl_dump_file)
545             {
546               fprintf (rtl_dump_file,
547                        "live_at_start mismatch in bb %d, aborting\nNew:\n",
548                        bb->index);
549               debug_bitmap_file (rtl_dump_file, new_live_at_start);
550               fputs ("Old:\n", rtl_dump_file);
551               dump_bb (bb, rtl_dump_file);
552             }
553           abort ();
554         }
555     }
556   else
557     {
558       int i;
559
560       /* Find the set of changed registers.  */
561       XOR_REG_SET (new_live_at_start, bb->global_live_at_start);
562
563       EXECUTE_IF_SET_IN_REG_SET (new_live_at_start, 0, i,
564         {
565           /* No registers should die.  */
566           if (REGNO_REG_SET_P (bb->global_live_at_start, i))
567             {
568               if (rtl_dump_file)
569                 {
570                   fprintf (rtl_dump_file,
571                            "Register %d died unexpectedly.\n", i);
572                   dump_bb (bb, rtl_dump_file);
573                 }
574               abort ();
575             }
576
577           /* Verify that the now-live register is wider than word_mode.  */
578           verify_wide_reg (i, bb);
579         });
580     }
581 }
582
583 /* Updates life information starting with the basic blocks set in BLOCKS.
584    If BLOCKS is null, consider it to be the universal set.
585
586    If EXTENT is UPDATE_LIFE_LOCAL, such as after splitting or peepholing,
587    we are only expecting local modifications to basic blocks.  If we find
588    extra registers live at the beginning of a block, then we either killed
589    useful data, or we have a broken split that wants data not provided.
590    If we find registers removed from live_at_start, that means we have
591    a broken peephole that is killing a register it shouldn't.
592
593    ??? This is not true in one situation -- when a pre-reload splitter
594    generates subregs of a multi-word pseudo, current life analysis will
595    lose the kill.  So we _can_ have a pseudo go live.  How irritating.
596
597    It is also not true when a peephole decides that it doesn't need one
598    or more of the inputs.
599
600    Including PROP_REG_INFO does not properly refresh regs_ever_live
601    unless the caller resets it to zero.  */
602
603 int
604 update_life_info (sbitmap blocks, enum update_life_extent extent, int prop_flags)
605 {
606   regset tmp;
607   regset_head tmp_head;
608   int i;
609   int stabilized_prop_flags = prop_flags;
610   basic_block bb;
611
612   tmp = INITIALIZE_REG_SET (tmp_head);
613   ndead = 0;
614
615   timevar_push ((extent == UPDATE_LIFE_LOCAL || blocks)
616                 ? TV_LIFE_UPDATE : TV_LIFE);
617
618   /* Changes to the CFG are only allowed when
619      doing a global update for the entire CFG.  */
620   if ((prop_flags & PROP_ALLOW_CFG_CHANGES)
621       && (extent == UPDATE_LIFE_LOCAL || blocks))
622     abort ();
623
624   /* For a global update, we go through the relaxation process again.  */
625   if (extent != UPDATE_LIFE_LOCAL)
626     {
627       for ( ; ; )
628         {
629           int changed = 0;
630
631           calculate_global_regs_live (blocks, blocks,
632                                 prop_flags & (PROP_SCAN_DEAD_CODE
633                                               | PROP_SCAN_DEAD_STORES
634                                               | PROP_ALLOW_CFG_CHANGES));
635
636           if ((prop_flags & (PROP_KILL_DEAD_CODE | PROP_ALLOW_CFG_CHANGES))
637               != (PROP_KILL_DEAD_CODE | PROP_ALLOW_CFG_CHANGES))
638             break;
639
640           /* Removing dead code may allow the CFG to be simplified which
641              in turn may allow for further dead code detection / removal.  */
642           FOR_EACH_BB_REVERSE (bb)
643             {
644               COPY_REG_SET (tmp, bb->global_live_at_end);
645               changed |= propagate_block (bb, tmp, NULL, NULL,
646                                 prop_flags & (PROP_SCAN_DEAD_CODE
647                                               | PROP_SCAN_DEAD_STORES
648                                               | PROP_KILL_DEAD_CODE));
649             }
650
651           /* Don't pass PROP_SCAN_DEAD_CODE or PROP_KILL_DEAD_CODE to
652              subsequent propagate_block calls, since removing or acting as
653              removing dead code can affect global register liveness, which
654              is supposed to be finalized for this call after this loop.  */
655           stabilized_prop_flags
656             &= ~(PROP_SCAN_DEAD_CODE | PROP_SCAN_DEAD_STORES
657                  | PROP_KILL_DEAD_CODE);
658
659           if (! changed)
660             break;
661
662           /* We repeat regardless of what cleanup_cfg says.  If there were
663              instructions deleted above, that might have been only a
664              partial improvement (see MAX_MEM_SET_LIST_LEN usage).
665              Further improvement may be possible.  */
666           cleanup_cfg (CLEANUP_EXPENSIVE);
667
668           /* Zap the life information from the last round.  If we don't
669              do this, we can wind up with registers that no longer appear
670              in the code being marked live at entry, which twiggs bogus
671              warnings from regno_uninitialized.  */
672           FOR_EACH_BB (bb)
673             {
674               CLEAR_REG_SET (bb->global_live_at_start);
675               CLEAR_REG_SET (bb->global_live_at_end);
676             }
677         }
678
679       /* If asked, remove notes from the blocks we'll update.  */
680       if (extent == UPDATE_LIFE_GLOBAL_RM_NOTES)
681         count_or_remove_death_notes (blocks, 1);
682     }
683
684   /* Clear log links in case we are asked to (re)compute them.  */
685   if (prop_flags & PROP_LOG_LINKS)
686     clear_log_links (blocks);
687
688   if (blocks)
689     {
690       EXECUTE_IF_SET_IN_SBITMAP (blocks, 0, i,
691         {
692           bb = BASIC_BLOCK (i);
693
694           COPY_REG_SET (tmp, bb->global_live_at_end);
695           propagate_block (bb, tmp, NULL, NULL, stabilized_prop_flags);
696
697           if (extent == UPDATE_LIFE_LOCAL)
698             verify_local_live_at_start (tmp, bb);
699         });
700     }
701   else
702     {
703       FOR_EACH_BB_REVERSE (bb)
704         {
705           COPY_REG_SET (tmp, bb->global_live_at_end);
706
707           propagate_block (bb, tmp, NULL, NULL, stabilized_prop_flags);
708
709           if (extent == UPDATE_LIFE_LOCAL)
710             verify_local_live_at_start (tmp, bb);
711         }
712     }
713
714   FREE_REG_SET (tmp);
715
716   if (prop_flags & PROP_REG_INFO)
717     {
718       /* The only pseudos that are live at the beginning of the function
719          are those that were not set anywhere in the function.  local-alloc
720          doesn't know how to handle these correctly, so mark them as not
721          local to any one basic block.  */
722       EXECUTE_IF_SET_IN_REG_SET (ENTRY_BLOCK_PTR->global_live_at_end,
723                                  FIRST_PSEUDO_REGISTER, i,
724                                  { REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL; });
725
726       /* We have a problem with any pseudoreg that lives across the setjmp.
727          ANSI says that if a user variable does not change in value between
728          the setjmp and the longjmp, then the longjmp preserves it.  This
729          includes longjmp from a place where the pseudo appears dead.
730          (In principle, the value still exists if it is in scope.)
731          If the pseudo goes in a hard reg, some other value may occupy
732          that hard reg where this pseudo is dead, thus clobbering the pseudo.
733          Conclusion: such a pseudo must not go in a hard reg.  */
734       EXECUTE_IF_SET_IN_REG_SET (regs_live_at_setjmp,
735                                  FIRST_PSEUDO_REGISTER, i,
736                                  {
737                                    if (regno_reg_rtx[i] != 0)
738                                      {
739                                        REG_LIVE_LENGTH (i) = -1;
740                                        REG_BASIC_BLOCK (i) = REG_BLOCK_UNKNOWN;
741                                      }
742                                  });
743     }
744   timevar_pop ((extent == UPDATE_LIFE_LOCAL || blocks)
745                ? TV_LIFE_UPDATE : TV_LIFE);
746   if (ndead && rtl_dump_file)
747     fprintf (rtl_dump_file, "deleted %i dead insns\n", ndead);
748   return ndead;
749 }
750
751 /* Update life information in all blocks where BB_DIRTY is set.  */
752
753 int
754 update_life_info_in_dirty_blocks (enum update_life_extent extent, int prop_flags)
755 {
756   sbitmap update_life_blocks = sbitmap_alloc (last_basic_block);
757   int n = 0;
758   basic_block bb;
759   int retval = 0;
760
761   sbitmap_zero (update_life_blocks);
762   FOR_EACH_BB (bb)
763     {
764       if (extent == UPDATE_LIFE_LOCAL)
765         {
766           if (bb->flags & BB_DIRTY)
767             {
768               SET_BIT (update_life_blocks, bb->index);
769               n++;
770             }
771         }
772       else
773         {
774           /* ??? Bootstrap with -march=pentium4 fails to terminate
775              with only a partial life update.  */
776           SET_BIT (update_life_blocks, bb->index);
777           if (bb->flags & BB_DIRTY)
778             n++;
779         }
780     }
781
782   if (n)
783     retval = update_life_info (update_life_blocks, extent, prop_flags);
784
785   sbitmap_free (update_life_blocks);
786   return retval;
787 }
788
789 /* Free the variables allocated by find_basic_blocks.
790
791    KEEP_HEAD_END_P is nonzero if basic_block_info is not to be freed.  */
792
793 void
794 free_basic_block_vars (int keep_head_end_p)
795 {
796   if (! keep_head_end_p)
797     {
798       if (basic_block_info)
799         {
800           clear_edges ();
801           VARRAY_FREE (basic_block_info);
802         }
803       n_basic_blocks = 0;
804       last_basic_block = 0;
805
806       ENTRY_BLOCK_PTR->aux = NULL;
807       ENTRY_BLOCK_PTR->global_live_at_end = NULL;
808       EXIT_BLOCK_PTR->aux = NULL;
809       EXIT_BLOCK_PTR->global_live_at_start = NULL;
810     }
811 }
812
813 /* Delete any insns that copy a register to itself.  */
814
815 int
816 delete_noop_moves (rtx f ATTRIBUTE_UNUSED)
817 {
818   rtx insn, next;
819   basic_block bb;
820   int nnoops = 0;
821
822   FOR_EACH_BB (bb)
823     {
824       for (insn = bb->head; insn != NEXT_INSN (bb->end); insn = next)
825         {
826           next = NEXT_INSN (insn);
827           if (INSN_P (insn) && noop_move_p (insn))
828             {
829               rtx note;
830
831               /* If we're about to remove the first insn of a libcall
832                  then move the libcall note to the next real insn and
833                  update the retval note.  */
834               if ((note = find_reg_note (insn, REG_LIBCALL, NULL_RTX))
835                        && XEXP (note, 0) != insn)
836                 {
837                   rtx new_libcall_insn = next_real_insn (insn);
838                   rtx retval_note = find_reg_note (XEXP (note, 0),
839                                                    REG_RETVAL, NULL_RTX);
840                   REG_NOTES (new_libcall_insn)
841                     = gen_rtx_INSN_LIST (REG_LIBCALL, XEXP (note, 0),
842                                          REG_NOTES (new_libcall_insn));
843                   XEXP (retval_note, 0) = new_libcall_insn;
844                 }
845
846               delete_insn_and_edges (insn);
847               nnoops++;
848             }
849         }
850     }
851   if (nnoops && rtl_dump_file)
852     fprintf (rtl_dump_file, "deleted %i noop moves", nnoops);
853   return nnoops;
854 }
855
856 /* Delete any jump tables never referenced.  We can't delete them at the
857    time of removing tablejump insn as they are referenced by the preceding
858    insns computing the destination, so we delay deleting and garbagecollect
859    them once life information is computed.  */
860 void
861 delete_dead_jumptables (void)
862 {
863   rtx insn, next;
864   for (insn = get_insns (); insn; insn = next)
865     {
866       next = NEXT_INSN (insn);
867       if (GET_CODE (insn) == CODE_LABEL
868           && LABEL_NUSES (insn) == LABEL_PRESERVE_P (insn)
869           && GET_CODE (next) == JUMP_INSN
870           && (GET_CODE (PATTERN (next)) == ADDR_VEC
871               || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC))
872         {
873           if (rtl_dump_file)
874             fprintf (rtl_dump_file, "Dead jumptable %i removed\n", INSN_UID (insn));
875           delete_insn (NEXT_INSN (insn));
876           delete_insn (insn);
877           next = NEXT_INSN (next);
878         }
879     }
880 }
881
882 /* Determine if the stack pointer is constant over the life of the function.
883    Only useful before prologues have been emitted.  */
884
885 static void
886 notice_stack_pointer_modification_1 (rtx x, rtx pat ATTRIBUTE_UNUSED,
887                                      void *data ATTRIBUTE_UNUSED)
888 {
889   if (x == stack_pointer_rtx
890       /* The stack pointer is only modified indirectly as the result
891          of a push until later in flow.  See the comments in rtl.texi
892          regarding Embedded Side-Effects on Addresses.  */
893       || (GET_CODE (x) == MEM
894           && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == 'a'
895           && XEXP (XEXP (x, 0), 0) == stack_pointer_rtx))
896     current_function_sp_is_unchanging = 0;
897 }
898
899 static void
900 notice_stack_pointer_modification (rtx f)
901 {
902   rtx insn;
903
904   /* Assume that the stack pointer is unchanging if alloca hasn't
905      been used.  */
906   current_function_sp_is_unchanging = !current_function_calls_alloca;
907   if (! current_function_sp_is_unchanging)
908     return;
909
910   for (insn = f; insn; insn = NEXT_INSN (insn))
911     {
912       if (INSN_P (insn))
913         {
914           /* Check if insn modifies the stack pointer.  */
915           note_stores (PATTERN (insn), notice_stack_pointer_modification_1,
916                        NULL);
917           if (! current_function_sp_is_unchanging)
918             return;
919         }
920     }
921 }
922
923 /* Mark a register in SET.  Hard registers in large modes get all
924    of their component registers set as well.  */
925
926 static void
927 mark_reg (rtx reg, void *xset)
928 {
929   regset set = (regset) xset;
930   int regno = REGNO (reg);
931
932   if (GET_MODE (reg) == BLKmode)
933     abort ();
934
935   SET_REGNO_REG_SET (set, regno);
936   if (regno < FIRST_PSEUDO_REGISTER)
937     {
938       int n = HARD_REGNO_NREGS (regno, GET_MODE (reg));
939       while (--n > 0)
940         SET_REGNO_REG_SET (set, regno + n);
941     }
942 }
943
944 /* Mark those regs which are needed at the end of the function as live
945    at the end of the last basic block.  */
946
947 static void
948 mark_regs_live_at_end (regset set)
949 {
950   unsigned int i;
951
952   /* If exiting needs the right stack value, consider the stack pointer
953      live at the end of the function.  */
954   if ((HAVE_epilogue && epilogue_completed)
955       || ! EXIT_IGNORE_STACK
956       || (! FRAME_POINTER_REQUIRED
957           && ! current_function_calls_alloca
958           && flag_omit_frame_pointer)
959       || current_function_sp_is_unchanging)
960     {
961       SET_REGNO_REG_SET (set, STACK_POINTER_REGNUM);
962     }
963
964   /* Mark the frame pointer if needed at the end of the function.  If
965      we end up eliminating it, it will be removed from the live list
966      of each basic block by reload.  */
967
968   if (! reload_completed || frame_pointer_needed)
969     {
970       SET_REGNO_REG_SET (set, FRAME_POINTER_REGNUM);
971 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
972       /* If they are different, also mark the hard frame pointer as live.  */
973       if (! LOCAL_REGNO (HARD_FRAME_POINTER_REGNUM))
974         SET_REGNO_REG_SET (set, HARD_FRAME_POINTER_REGNUM);
975 #endif
976     }
977
978 #ifndef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
979   /* Many architectures have a GP register even without flag_pic.
980      Assume the pic register is not in use, or will be handled by
981      other means, if it is not fixed.  */
982   if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
983       && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
984     SET_REGNO_REG_SET (set, PIC_OFFSET_TABLE_REGNUM);
985 #endif
986
987   /* Mark all global registers, and all registers used by the epilogue
988      as being live at the end of the function since they may be
989      referenced by our caller.  */
990   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
991     if (global_regs[i] || EPILOGUE_USES (i))
992       SET_REGNO_REG_SET (set, i);
993
994   if (HAVE_epilogue && epilogue_completed)
995     {
996       /* Mark all call-saved registers that we actually used.  */
997       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
998         if (regs_ever_live[i] && ! LOCAL_REGNO (i)
999             && ! TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1000           SET_REGNO_REG_SET (set, i);
1001     }
1002
1003 #ifdef EH_RETURN_DATA_REGNO
1004   /* Mark the registers that will contain data for the handler.  */
1005   if (reload_completed && current_function_calls_eh_return)
1006     for (i = 0; ; ++i)
1007       {
1008         unsigned regno = EH_RETURN_DATA_REGNO(i);
1009         if (regno == INVALID_REGNUM)
1010           break;
1011         SET_REGNO_REG_SET (set, regno);
1012       }
1013 #endif
1014 #ifdef EH_RETURN_STACKADJ_RTX
1015   if ((! HAVE_epilogue || ! epilogue_completed)
1016       && current_function_calls_eh_return)
1017     {
1018       rtx tmp = EH_RETURN_STACKADJ_RTX;
1019       if (tmp && REG_P (tmp))
1020         mark_reg (tmp, set);
1021     }
1022 #endif
1023 #ifdef EH_RETURN_HANDLER_RTX
1024   if ((! HAVE_epilogue || ! epilogue_completed)
1025       && current_function_calls_eh_return)
1026     {
1027       rtx tmp = EH_RETURN_HANDLER_RTX;
1028       if (tmp && REG_P (tmp))
1029         mark_reg (tmp, set);
1030     }
1031 #endif
1032
1033   /* Mark function return value.  */
1034   diddle_return_value (mark_reg, set);
1035 }
1036
1037 /* Propagate global life info around the graph of basic blocks.  Begin
1038    considering blocks with their corresponding bit set in BLOCKS_IN.
1039    If BLOCKS_IN is null, consider it the universal set.
1040
1041    BLOCKS_OUT is set for every block that was changed.  */
1042
1043 static void
1044 calculate_global_regs_live (sbitmap blocks_in, sbitmap blocks_out, int flags)
1045 {
1046   basic_block *queue, *qhead, *qtail, *qend, bb;
1047   regset tmp, new_live_at_end, invalidated_by_call;
1048   regset_head tmp_head, invalidated_by_call_head;
1049   regset_head new_live_at_end_head;
1050   int i;
1051
1052   /* Some passes used to forget clear aux field of basic block causing
1053      sick behavior here.  */
1054 #ifdef ENABLE_CHECKING
1055   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1056     if (bb->aux)
1057       abort ();
1058 #endif
1059
1060   tmp = INITIALIZE_REG_SET (tmp_head);
1061   new_live_at_end = INITIALIZE_REG_SET (new_live_at_end_head);
1062   invalidated_by_call = INITIALIZE_REG_SET (invalidated_by_call_head);
1063
1064   /* Inconveniently, this is only readily available in hard reg set form.  */
1065   for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1066     if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1067       SET_REGNO_REG_SET (invalidated_by_call, i);
1068
1069   /* Create a worklist.  Allocate an extra slot for ENTRY_BLOCK, and one
1070      because the `head == tail' style test for an empty queue doesn't
1071      work with a full queue.  */
1072   queue = xmalloc ((n_basic_blocks + 2) * sizeof (*queue));
1073   qtail = queue;
1074   qhead = qend = queue + n_basic_blocks + 2;
1075
1076   /* Queue the blocks set in the initial mask.  Do this in reverse block
1077      number order so that we are more likely for the first round to do
1078      useful work.  We use AUX non-null to flag that the block is queued.  */
1079   if (blocks_in)
1080     {
1081       FOR_EACH_BB (bb)
1082         if (TEST_BIT (blocks_in, bb->index))
1083           {
1084             *--qhead = bb;
1085             bb->aux = bb;
1086           }
1087     }
1088   else
1089     {
1090       FOR_EACH_BB (bb)
1091         {
1092           *--qhead = bb;
1093           bb->aux = bb;
1094         }
1095     }
1096
1097   /* We clean aux when we remove the initially-enqueued bbs, but we
1098      don't enqueue ENTRY and EXIT initially, so clean them upfront and
1099      unconditionally.  */
1100   ENTRY_BLOCK_PTR->aux = EXIT_BLOCK_PTR->aux = NULL;
1101
1102   if (blocks_out)
1103     sbitmap_zero (blocks_out);
1104
1105   /* We work through the queue until there are no more blocks.  What
1106      is live at the end of this block is precisely the union of what
1107      is live at the beginning of all its successors.  So, we set its
1108      GLOBAL_LIVE_AT_END field based on the GLOBAL_LIVE_AT_START field
1109      for its successors.  Then, we compute GLOBAL_LIVE_AT_START for
1110      this block by walking through the instructions in this block in
1111      reverse order and updating as we go.  If that changed
1112      GLOBAL_LIVE_AT_START, we add the predecessors of the block to the
1113      queue; they will now need to recalculate GLOBAL_LIVE_AT_END.
1114
1115      We are guaranteed to terminate, because GLOBAL_LIVE_AT_START
1116      never shrinks.  If a register appears in GLOBAL_LIVE_AT_START, it
1117      must either be live at the end of the block, or used within the
1118      block.  In the latter case, it will certainly never disappear
1119      from GLOBAL_LIVE_AT_START.  In the former case, the register
1120      could go away only if it disappeared from GLOBAL_LIVE_AT_START
1121      for one of the successor blocks.  By induction, that cannot
1122      occur.  */
1123   while (qhead != qtail)
1124     {
1125       int rescan, changed;
1126       basic_block bb;
1127       edge e;
1128
1129       bb = *qhead++;
1130       if (qhead == qend)
1131         qhead = queue;
1132       bb->aux = NULL;
1133
1134       /* Begin by propagating live_at_start from the successor blocks.  */
1135       CLEAR_REG_SET (new_live_at_end);
1136
1137       if (bb->succ)
1138         for (e = bb->succ; e; e = e->succ_next)
1139           {
1140             basic_block sb = e->dest;
1141
1142             /* Call-clobbered registers die across exception and
1143                call edges.  */
1144             /* ??? Abnormal call edges ignored for the moment, as this gets
1145                confused by sibling call edges, which crashes reg-stack.  */
1146             if (e->flags & EDGE_EH)
1147               {
1148                 bitmap_operation (tmp, sb->global_live_at_start,
1149                                   invalidated_by_call, BITMAP_AND_COMPL);
1150                 IOR_REG_SET (new_live_at_end, tmp);
1151               }
1152             else
1153               IOR_REG_SET (new_live_at_end, sb->global_live_at_start);
1154
1155             /* If a target saves one register in another (instead of on
1156                the stack) the save register will need to be live for EH.  */
1157             if (e->flags & EDGE_EH)
1158               for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1159                 if (EH_USES (i))
1160                   SET_REGNO_REG_SET (new_live_at_end, i);
1161           }
1162       else
1163         {
1164           /* This might be a noreturn function that throws.  And
1165              even if it isn't, getting the unwind info right helps
1166              debugging.  */
1167           for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1168             if (EH_USES (i))
1169               SET_REGNO_REG_SET (new_live_at_end, i);
1170         }
1171
1172       /* The all-important stack pointer must always be live.  */
1173       SET_REGNO_REG_SET (new_live_at_end, STACK_POINTER_REGNUM);
1174
1175       /* Before reload, there are a few registers that must be forced
1176          live everywhere -- which might not already be the case for
1177          blocks within infinite loops.  */
1178       if (! reload_completed)
1179         {
1180           /* Any reference to any pseudo before reload is a potential
1181              reference of the frame pointer.  */
1182           SET_REGNO_REG_SET (new_live_at_end, FRAME_POINTER_REGNUM);
1183
1184 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1185           /* Pseudos with argument area equivalences may require
1186              reloading via the argument pointer.  */
1187           if (fixed_regs[ARG_POINTER_REGNUM])
1188             SET_REGNO_REG_SET (new_live_at_end, ARG_POINTER_REGNUM);
1189 #endif
1190
1191           /* Any constant, or pseudo with constant equivalences, may
1192              require reloading from memory using the pic register.  */
1193           if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1194               && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1195             SET_REGNO_REG_SET (new_live_at_end, PIC_OFFSET_TABLE_REGNUM);
1196         }
1197
1198       if (bb == ENTRY_BLOCK_PTR)
1199         {
1200           COPY_REG_SET (bb->global_live_at_end, new_live_at_end);
1201           continue;
1202         }
1203
1204       /* On our first pass through this block, we'll go ahead and continue.
1205          Recognize first pass by local_set NULL.  On subsequent passes, we
1206          get to skip out early if live_at_end wouldn't have changed.  */
1207
1208       if (bb->local_set == NULL)
1209         {
1210           bb->local_set = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1211           bb->cond_local_set = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1212           rescan = 1;
1213         }
1214       else
1215         {
1216           /* If any bits were removed from live_at_end, we'll have to
1217              rescan the block.  This wouldn't be necessary if we had
1218              precalculated local_live, however with PROP_SCAN_DEAD_CODE
1219              local_live is really dependent on live_at_end.  */
1220           CLEAR_REG_SET (tmp);
1221           rescan = bitmap_operation (tmp, bb->global_live_at_end,
1222                                      new_live_at_end, BITMAP_AND_COMPL);
1223
1224           if (! rescan)
1225             {
1226               /* If any of the registers in the new live_at_end set are
1227                  conditionally set in this basic block, we must rescan.
1228                  This is because conditional lifetimes at the end of the
1229                  block do not just take the live_at_end set into account,
1230                  but also the liveness at the start of each successor
1231                  block.  We can miss changes in those sets if we only
1232                  compare the new live_at_end against the previous one.  */
1233               CLEAR_REG_SET (tmp);
1234               rescan = bitmap_operation (tmp, new_live_at_end,
1235                                          bb->cond_local_set, BITMAP_AND);
1236             }
1237
1238           if (! rescan)
1239             {
1240               /* Find the set of changed bits.  Take this opportunity
1241                  to notice that this set is empty and early out.  */
1242               CLEAR_REG_SET (tmp);
1243               changed = bitmap_operation (tmp, bb->global_live_at_end,
1244                                           new_live_at_end, BITMAP_XOR);
1245               if (! changed)
1246                 continue;
1247
1248               /* If any of the changed bits overlap with local_set,
1249                  we'll have to rescan the block.  Detect overlap by
1250                  the AND with ~local_set turning off bits.  */
1251               rescan = bitmap_operation (tmp, tmp, bb->local_set,
1252                                          BITMAP_AND_COMPL);
1253             }
1254         }
1255
1256       /* Let our caller know that BB changed enough to require its
1257          death notes updated.  */
1258       if (blocks_out)
1259         SET_BIT (blocks_out, bb->index);
1260
1261       if (! rescan)
1262         {
1263           /* Add to live_at_start the set of all registers in
1264              new_live_at_end that aren't in the old live_at_end.  */
1265
1266           bitmap_operation (tmp, new_live_at_end, bb->global_live_at_end,
1267                             BITMAP_AND_COMPL);
1268           COPY_REG_SET (bb->global_live_at_end, new_live_at_end);
1269
1270           changed = bitmap_operation (bb->global_live_at_start,
1271                                       bb->global_live_at_start,
1272                                       tmp, BITMAP_IOR);
1273           if (! changed)
1274             continue;
1275         }
1276       else
1277         {
1278           COPY_REG_SET (bb->global_live_at_end, new_live_at_end);
1279
1280           /* Rescan the block insn by insn to turn (a copy of) live_at_end
1281              into live_at_start.  */
1282           propagate_block (bb, new_live_at_end, bb->local_set,
1283                            bb->cond_local_set, flags);
1284
1285           /* If live_at start didn't change, no need to go farther.  */
1286           if (REG_SET_EQUAL_P (bb->global_live_at_start, new_live_at_end))
1287             continue;
1288
1289           COPY_REG_SET (bb->global_live_at_start, new_live_at_end);
1290         }
1291
1292       /* Queue all predecessors of BB so that we may re-examine
1293          their live_at_end.  */
1294       for (e = bb->pred; e; e = e->pred_next)
1295         {
1296           basic_block pb = e->src;
1297           if (pb->aux == NULL)
1298             {
1299               *qtail++ = pb;
1300               if (qtail == qend)
1301                 qtail = queue;
1302               pb->aux = pb;
1303             }
1304         }
1305     }
1306
1307   FREE_REG_SET (tmp);
1308   FREE_REG_SET (new_live_at_end);
1309   FREE_REG_SET (invalidated_by_call);
1310
1311   if (blocks_out)
1312     {
1313       EXECUTE_IF_SET_IN_SBITMAP (blocks_out, 0, i,
1314         {
1315           basic_block bb = BASIC_BLOCK (i);
1316           FREE_REG_SET (bb->local_set);
1317           FREE_REG_SET (bb->cond_local_set);
1318         });
1319     }
1320   else
1321     {
1322       FOR_EACH_BB (bb)
1323         {
1324           FREE_REG_SET (bb->local_set);
1325           FREE_REG_SET (bb->cond_local_set);
1326         }
1327     }
1328
1329   free (queue);
1330 }
1331
1332 \f
1333 /* This structure is used to pass parameters to and from the
1334    the function find_regno_partial(). It is used to pass in the
1335    register number we are looking, as well as to return any rtx
1336    we find.  */
1337
1338 typedef struct {
1339   unsigned regno_to_find;
1340   rtx retval;
1341 } find_regno_partial_param;
1342
1343
1344 /* Find the rtx for the reg numbers specified in 'data' if it is
1345    part of an expression which only uses part of the register.  Return
1346    it in the structure passed in.  */
1347 static int
1348 find_regno_partial (rtx *ptr, void *data)
1349 {
1350   find_regno_partial_param *param = (find_regno_partial_param *)data;
1351   unsigned reg = param->regno_to_find;
1352   param->retval = NULL_RTX;
1353
1354   if (*ptr == NULL_RTX)
1355     return 0;
1356
1357   switch (GET_CODE (*ptr))
1358     {
1359     case ZERO_EXTRACT:
1360     case SIGN_EXTRACT:
1361     case STRICT_LOW_PART:
1362       if (GET_CODE (XEXP (*ptr, 0)) == REG && REGNO (XEXP (*ptr, 0)) == reg)
1363         {
1364           param->retval = XEXP (*ptr, 0);
1365           return 1;
1366         }
1367       break;
1368
1369     case SUBREG:
1370       if (GET_CODE (SUBREG_REG (*ptr)) == REG
1371           && REGNO (SUBREG_REG (*ptr)) == reg)
1372         {
1373           param->retval = SUBREG_REG (*ptr);
1374           return 1;
1375         }
1376       break;
1377
1378     default:
1379       break;
1380     }
1381
1382   return 0;
1383 }
1384
1385 /* Process all immediate successors of the entry block looking for pseudo
1386    registers which are live on entry. Find all of those whose first
1387    instance is a partial register reference of some kind, and initialize
1388    them to 0 after the entry block.  This will prevent bit sets within
1389    registers whose value is unknown, and may contain some kind of sticky
1390    bits we don't want.  */
1391
1392 int
1393 initialize_uninitialized_subregs (void)
1394 {
1395   rtx insn;
1396   edge e;
1397   int reg, did_something = 0;
1398   find_regno_partial_param param;
1399
1400   for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
1401     {
1402       basic_block bb = e->dest;
1403       regset map = bb->global_live_at_start;
1404       EXECUTE_IF_SET_IN_REG_SET (map,
1405                                  FIRST_PSEUDO_REGISTER, reg,
1406         {
1407           int uid = REGNO_FIRST_UID (reg);
1408           rtx i;
1409
1410           /* Find an insn which mentions the register we are looking for.
1411              Its preferable to have an instance of the register's rtl since
1412              there may be various flags set which we need to duplicate.
1413              If we can't find it, its probably an automatic whose initial
1414              value doesn't matter, or hopefully something we don't care about.  */
1415           for (i = get_insns (); i && INSN_UID (i) != uid; i = NEXT_INSN (i))
1416             ;
1417           if (i != NULL_RTX)
1418             {
1419               /* Found the insn, now get the REG rtx, if we can.  */
1420               param.regno_to_find = reg;
1421               for_each_rtx (&i, find_regno_partial, &param);
1422               if (param.retval != NULL_RTX)
1423                 {
1424                   start_sequence ();
1425                   emit_move_insn (param.retval,
1426                                   CONST0_RTX (GET_MODE (param.retval)));
1427                   insn = get_insns ();
1428                   end_sequence ();
1429                   insert_insn_on_edge (insn, e);
1430                   did_something = 1;
1431                 }
1432             }
1433         });
1434     }
1435
1436   if (did_something)
1437     commit_edge_insertions ();
1438   return did_something;
1439 }
1440
1441 \f
1442 /* Subroutines of life analysis.  */
1443
1444 /* Allocate the permanent data structures that represent the results
1445    of life analysis.  Not static since used also for stupid life analysis.  */
1446
1447 void
1448 allocate_bb_life_data (void)
1449 {
1450   basic_block bb;
1451
1452   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1453     {
1454       bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1455       bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1456     }
1457
1458   regs_live_at_setjmp = OBSTACK_ALLOC_REG_SET (&flow_obstack);
1459 }
1460
1461 void
1462 allocate_reg_life_data (void)
1463 {
1464   int i;
1465
1466   max_regno = max_reg_num ();
1467
1468   /* Recalculate the register space, in case it has grown.  Old style
1469      vector oriented regsets would set regset_{size,bytes} here also.  */
1470   allocate_reg_info (max_regno, FALSE, FALSE);
1471
1472   /* Reset all the data we'll collect in propagate_block and its
1473      subroutines.  */
1474   for (i = 0; i < max_regno; i++)
1475     {
1476       REG_N_SETS (i) = 0;
1477       REG_N_REFS (i) = 0;
1478       REG_N_DEATHS (i) = 0;
1479       REG_N_CALLS_CROSSED (i) = 0;
1480       REG_LIVE_LENGTH (i) = 0;
1481       REG_FREQ (i) = 0;
1482       REG_BASIC_BLOCK (i) = REG_BLOCK_UNKNOWN;
1483     }
1484 }
1485
1486 /* Delete dead instructions for propagate_block.  */
1487
1488 static void
1489 propagate_block_delete_insn (rtx insn)
1490 {
1491   rtx inote = find_reg_note (insn, REG_LABEL, NULL_RTX);
1492
1493   /* If the insn referred to a label, and that label was attached to
1494      an ADDR_VEC, it's safe to delete the ADDR_VEC.  In fact, it's
1495      pretty much mandatory to delete it, because the ADDR_VEC may be
1496      referencing labels that no longer exist.
1497
1498      INSN may reference a deleted label, particularly when a jump
1499      table has been optimized into a direct jump.  There's no
1500      real good way to fix up the reference to the deleted label
1501      when the label is deleted, so we just allow it here.  */
1502
1503   if (inote && GET_CODE (inote) == CODE_LABEL)
1504     {
1505       rtx label = XEXP (inote, 0);
1506       rtx next;
1507
1508       /* The label may be forced if it has been put in the constant
1509          pool.  If that is the only use we must discard the table
1510          jump following it, but not the label itself.  */
1511       if (LABEL_NUSES (label) == 1 + LABEL_PRESERVE_P (label)
1512           && (next = next_nonnote_insn (label)) != NULL
1513           && GET_CODE (next) == JUMP_INSN
1514           && (GET_CODE (PATTERN (next)) == ADDR_VEC
1515               || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC))
1516         {
1517           rtx pat = PATTERN (next);
1518           int diff_vec_p = GET_CODE (pat) == ADDR_DIFF_VEC;
1519           int len = XVECLEN (pat, diff_vec_p);
1520           int i;
1521
1522           for (i = 0; i < len; i++)
1523             LABEL_NUSES (XEXP (XVECEXP (pat, diff_vec_p, i), 0))--;
1524
1525           delete_insn_and_edges (next);
1526           ndead++;
1527         }
1528     }
1529
1530   delete_insn_and_edges (insn);
1531   ndead++;
1532 }
1533
1534 /* Delete dead libcalls for propagate_block.  Return the insn
1535    before the libcall.  */
1536
1537 static rtx
1538 propagate_block_delete_libcall (rtx insn, rtx note)
1539 {
1540   rtx first = XEXP (note, 0);
1541   rtx before = PREV_INSN (first);
1542
1543   delete_insn_chain_and_edges (first, insn);
1544   ndead++;
1545   return before;
1546 }
1547
1548 /* Update the life-status of regs for one insn.  Return the previous insn.  */
1549
1550 rtx
1551 propagate_one_insn (struct propagate_block_info *pbi, rtx insn)
1552 {
1553   rtx prev = PREV_INSN (insn);
1554   int flags = pbi->flags;
1555   int insn_is_dead = 0;
1556   int libcall_is_dead = 0;
1557   rtx note;
1558   int i;
1559
1560   if (! INSN_P (insn))
1561     return prev;
1562
1563   note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
1564   if (flags & PROP_SCAN_DEAD_CODE)
1565     {
1566       insn_is_dead = insn_dead_p (pbi, PATTERN (insn), 0, REG_NOTES (insn));
1567       libcall_is_dead = (insn_is_dead && note != 0
1568                          && libcall_dead_p (pbi, note, insn));
1569     }
1570
1571   /* If an instruction consists of just dead store(s) on final pass,
1572      delete it.  */
1573   if ((flags & PROP_KILL_DEAD_CODE) && insn_is_dead)
1574     {
1575       /* If we're trying to delete a prologue or epilogue instruction
1576          that isn't flagged as possibly being dead, something is wrong.
1577          But if we are keeping the stack pointer depressed, we might well
1578          be deleting insns that are used to compute the amount to update
1579          it by, so they are fine.  */
1580       if (reload_completed
1581           && !(TREE_CODE (TREE_TYPE (current_function_decl)) == FUNCTION_TYPE
1582                 && (TYPE_RETURNS_STACK_DEPRESSED
1583                     (TREE_TYPE (current_function_decl))))
1584           && (((HAVE_epilogue || HAVE_prologue)
1585                && prologue_epilogue_contains (insn))
1586               || (HAVE_sibcall_epilogue
1587                   && sibcall_epilogue_contains (insn)))
1588           && find_reg_note (insn, REG_MAYBE_DEAD, NULL_RTX) == 0)
1589         fatal_insn ("Attempt to delete prologue/epilogue insn:", insn);
1590
1591       /* Record sets.  Do this even for dead instructions, since they
1592          would have killed the values if they hadn't been deleted.  */
1593       mark_set_regs (pbi, PATTERN (insn), insn);
1594
1595       /* CC0 is now known to be dead.  Either this insn used it,
1596          in which case it doesn't anymore, or clobbered it,
1597          so the next insn can't use it.  */
1598       pbi->cc0_live = 0;
1599
1600       if (libcall_is_dead)
1601         prev = propagate_block_delete_libcall ( insn, note);
1602       else
1603         {
1604
1605         /* If INSN contains a RETVAL note and is dead, but the libcall
1606            as a whole is not dead, then we want to remove INSN, but
1607            not the whole libcall sequence.
1608
1609            However, we need to also remove the dangling REG_LIBCALL
1610            note so that we do not have mis-matched LIBCALL/RETVAL
1611            notes.  In theory we could find a new location for the
1612            REG_RETVAL note, but it hardly seems worth the effort.
1613
1614            NOTE at this point will be the RETVAL note if it exists.  */
1615           if (note)
1616             {
1617               rtx libcall_note;
1618
1619               libcall_note
1620                 = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
1621               remove_note (XEXP (note, 0), libcall_note);
1622             }
1623
1624           /* Similarly if INSN contains a LIBCALL note, remove the
1625              dangling REG_RETVAL note.  */
1626           note = find_reg_note (insn, REG_LIBCALL, NULL_RTX);
1627           if (note)
1628             {
1629               rtx retval_note;
1630
1631               retval_note
1632                 = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
1633               remove_note (XEXP (note, 0), retval_note);
1634             }
1635
1636           /* Now delete INSN.  */
1637           propagate_block_delete_insn (insn);
1638         }
1639
1640       return prev;
1641     }
1642
1643   /* See if this is an increment or decrement that can be merged into
1644      a following memory address.  */
1645 #ifdef AUTO_INC_DEC
1646   {
1647     rtx x = single_set (insn);
1648
1649     /* Does this instruction increment or decrement a register?  */
1650     if ((flags & PROP_AUTOINC)
1651         && x != 0
1652         && GET_CODE (SET_DEST (x)) == REG
1653         && (GET_CODE (SET_SRC (x)) == PLUS
1654             || GET_CODE (SET_SRC (x)) == MINUS)
1655         && XEXP (SET_SRC (x), 0) == SET_DEST (x)
1656         && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
1657         /* Ok, look for a following memory ref we can combine with.
1658            If one is found, change the memory ref to a PRE_INC
1659            or PRE_DEC, cancel this insn, and return 1.
1660            Return 0 if nothing has been done.  */
1661         && try_pre_increment_1 (pbi, insn))
1662       return prev;
1663   }
1664 #endif /* AUTO_INC_DEC */
1665
1666   CLEAR_REG_SET (pbi->new_set);
1667
1668   /* If this is not the final pass, and this insn is copying the value of
1669      a library call and it's dead, don't scan the insns that perform the
1670      library call, so that the call's arguments are not marked live.  */
1671   if (libcall_is_dead)
1672     {
1673       /* Record the death of the dest reg.  */
1674       mark_set_regs (pbi, PATTERN (insn), insn);
1675
1676       insn = XEXP (note, 0);
1677       return PREV_INSN (insn);
1678     }
1679   else if (GET_CODE (PATTERN (insn)) == SET
1680            && SET_DEST (PATTERN (insn)) == stack_pointer_rtx
1681            && GET_CODE (SET_SRC (PATTERN (insn))) == PLUS
1682            && XEXP (SET_SRC (PATTERN (insn)), 0) == stack_pointer_rtx
1683            && GET_CODE (XEXP (SET_SRC (PATTERN (insn)), 1)) == CONST_INT)
1684     /* We have an insn to pop a constant amount off the stack.
1685        (Such insns use PLUS regardless of the direction of the stack,
1686        and any insn to adjust the stack by a constant is always a pop.)
1687        These insns, if not dead stores, have no effect on life, though
1688        they do have an effect on the memory stores we are tracking.  */
1689     invalidate_mems_from_set (pbi, stack_pointer_rtx);
1690   else
1691     {
1692       rtx note;
1693       /* Any regs live at the time of a call instruction must not go
1694          in a register clobbered by calls.  Find all regs now live and
1695          record this for them.  */
1696
1697       if (GET_CODE (insn) == CALL_INSN && (flags & PROP_REG_INFO))
1698         EXECUTE_IF_SET_IN_REG_SET (pbi->reg_live, 0, i,
1699                                    { REG_N_CALLS_CROSSED (i)++; });
1700
1701       /* Record sets.  Do this even for dead instructions, since they
1702          would have killed the values if they hadn't been deleted.  */
1703       mark_set_regs (pbi, PATTERN (insn), insn);
1704
1705       if (GET_CODE (insn) == CALL_INSN)
1706         {
1707           regset live_at_end;
1708           bool sibcall_p;
1709           rtx note, cond;
1710           int i;
1711
1712           cond = NULL_RTX;
1713           if (GET_CODE (PATTERN (insn)) == COND_EXEC)
1714             cond = COND_EXEC_TEST (PATTERN (insn));
1715
1716           /* Non-constant calls clobber memory, constant calls do not
1717              clobber memory, though they may clobber outgoing arguments
1718              on the stack.  */
1719           if (! CONST_OR_PURE_CALL_P (insn))
1720             {
1721               free_EXPR_LIST_list (&pbi->mem_set_list);
1722               pbi->mem_set_list_len = 0;
1723             }
1724           else
1725             invalidate_mems_from_set (pbi, stack_pointer_rtx);
1726
1727           /* There may be extra registers to be clobbered.  */
1728           for (note = CALL_INSN_FUNCTION_USAGE (insn);
1729                note;
1730                note = XEXP (note, 1))
1731             if (GET_CODE (XEXP (note, 0)) == CLOBBER)
1732               mark_set_1 (pbi, CLOBBER, XEXP (XEXP (note, 0), 0),
1733                           cond, insn, pbi->flags);
1734
1735           /* Calls change all call-used and global registers; sibcalls do not
1736              clobber anything that must be preserved at end-of-function,
1737              except for return values.  */
1738
1739           sibcall_p = SIBLING_CALL_P (insn);
1740           live_at_end = EXIT_BLOCK_PTR->global_live_at_start;
1741           for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1742             if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i)
1743                 && ! (sibcall_p
1744                       && REGNO_REG_SET_P (live_at_end, i)
1745                       && ! refers_to_regno_p (i, i+1,
1746                                               current_function_return_rtx,
1747                                               (rtx *) 0)))
1748               {
1749                 /* We do not want REG_UNUSED notes for these registers.  */
1750                 mark_set_1 (pbi, CLOBBER, regno_reg_rtx[i], cond, insn,
1751                             pbi->flags & ~(PROP_DEATH_NOTES | PROP_REG_INFO));
1752               }
1753         }
1754
1755       /* If an insn doesn't use CC0, it becomes dead since we assume
1756          that every insn clobbers it.  So show it dead here;
1757          mark_used_regs will set it live if it is referenced.  */
1758       pbi->cc0_live = 0;
1759
1760       /* Record uses.  */
1761       if (! insn_is_dead)
1762         mark_used_regs (pbi, PATTERN (insn), NULL_RTX, insn);
1763       if ((flags & PROP_EQUAL_NOTES)
1764           && ((note = find_reg_note (insn, REG_EQUAL, NULL_RTX))
1765               || (note = find_reg_note (insn, REG_EQUIV, NULL_RTX))))
1766         mark_used_regs (pbi, XEXP (note, 0), NULL_RTX, insn);
1767
1768       /* Sometimes we may have inserted something before INSN (such as a move)
1769          when we make an auto-inc.  So ensure we will scan those insns.  */
1770 #ifdef AUTO_INC_DEC
1771       prev = PREV_INSN (insn);
1772 #endif
1773
1774       if (! insn_is_dead && GET_CODE (insn) == CALL_INSN)
1775         {
1776           int i;
1777           rtx note, cond;
1778
1779           cond = NULL_RTX;
1780           if (GET_CODE (PATTERN (insn)) == COND_EXEC)
1781             cond = COND_EXEC_TEST (PATTERN (insn));
1782
1783           /* Calls use their arguments, and may clobber memory which
1784              address involves some register.  */
1785           for (note = CALL_INSN_FUNCTION_USAGE (insn);
1786                note;
1787                note = XEXP (note, 1))
1788             /* We find USE or CLOBBER entities in a FUNCTION_USAGE list: both
1789                of which mark_used_regs knows how to handle.  */
1790             mark_used_regs (pbi, XEXP (XEXP (note, 0), 0), cond, insn);
1791
1792           /* The stack ptr is used (honorarily) by a CALL insn.  */
1793           SET_REGNO_REG_SET (pbi->reg_live, STACK_POINTER_REGNUM);
1794
1795           /* Calls may also reference any of the global registers,
1796              so they are made live.  */
1797           for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1798             if (global_regs[i])
1799               mark_used_reg (pbi, regno_reg_rtx[i], cond, insn);
1800         }
1801     }
1802
1803   /* On final pass, update counts of how many insns in which each reg
1804      is live.  */
1805   if (flags & PROP_REG_INFO)
1806     EXECUTE_IF_SET_IN_REG_SET (pbi->reg_live, 0, i,
1807                                { REG_LIVE_LENGTH (i)++; });
1808
1809   return prev;
1810 }
1811
1812 /* Initialize a propagate_block_info struct for public consumption.
1813    Note that the structure itself is opaque to this file, but that
1814    the user can use the regsets provided here.  */
1815
1816 struct propagate_block_info *
1817 init_propagate_block_info (basic_block bb, regset live, regset local_set,
1818                            regset cond_local_set, int flags)
1819 {
1820   struct propagate_block_info *pbi = xmalloc (sizeof (*pbi));
1821
1822   pbi->bb = bb;
1823   pbi->reg_live = live;
1824   pbi->mem_set_list = NULL_RTX;
1825   pbi->mem_set_list_len = 0;
1826   pbi->local_set = local_set;
1827   pbi->cond_local_set = cond_local_set;
1828   pbi->cc0_live = 0;
1829   pbi->flags = flags;
1830
1831   if (flags & (PROP_LOG_LINKS | PROP_AUTOINC))
1832     pbi->reg_next_use = xcalloc (max_reg_num (), sizeof (rtx));
1833   else
1834     pbi->reg_next_use = NULL;
1835
1836   pbi->new_set = BITMAP_XMALLOC ();
1837
1838 #ifdef HAVE_conditional_execution
1839   pbi->reg_cond_dead = splay_tree_new (splay_tree_compare_ints, NULL,
1840                                        free_reg_cond_life_info);
1841   pbi->reg_cond_reg = BITMAP_XMALLOC ();
1842
1843   /* If this block ends in a conditional branch, for each register
1844      live from one side of the branch and not the other, record the
1845      register as conditionally dead.  */
1846   if (GET_CODE (bb->end) == JUMP_INSN
1847       && any_condjump_p (bb->end))
1848     {
1849       regset_head diff_head;
1850       regset diff = INITIALIZE_REG_SET (diff_head);
1851       basic_block bb_true, bb_false;
1852       int i;
1853
1854       /* Identify the successor blocks.  */
1855       bb_true = bb->succ->dest;
1856       if (bb->succ->succ_next != NULL)
1857         {
1858           bb_false = bb->succ->succ_next->dest;
1859
1860           if (bb->succ->flags & EDGE_FALLTHRU)
1861             {
1862               basic_block t = bb_false;
1863               bb_false = bb_true;
1864               bb_true = t;
1865             }
1866           else if (! (bb->succ->succ_next->flags & EDGE_FALLTHRU))
1867             abort ();
1868         }
1869       else
1870         {
1871           /* This can happen with a conditional jump to the next insn.  */
1872           if (JUMP_LABEL (bb->end) != bb_true->head)
1873             abort ();
1874
1875           /* Simplest way to do nothing.  */
1876           bb_false = bb_true;
1877         }
1878
1879       /* Compute which register lead different lives in the successors.  */
1880       if (bitmap_operation (diff, bb_true->global_live_at_start,
1881                             bb_false->global_live_at_start, BITMAP_XOR))
1882         {
1883           /* Extract the condition from the branch.  */
1884           rtx set_src = SET_SRC (pc_set (bb->end));
1885           rtx cond_true = XEXP (set_src, 0);
1886           rtx reg = XEXP (cond_true, 0);
1887
1888           if (GET_CODE (reg) == SUBREG)
1889             reg = SUBREG_REG (reg);
1890
1891           /* We can only track conditional lifetimes if the condition is
1892              in the form of a comparison of a register against zero.  
1893              If the condition is more complex than that, then it is safe
1894              not to record any information.  */
1895           if (GET_CODE (reg) == REG
1896               && XEXP (cond_true, 1) == const0_rtx)
1897             {
1898               rtx cond_false
1899                 = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond_true)),
1900                                   GET_MODE (cond_true), XEXP (cond_true, 0),
1901                                   XEXP (cond_true, 1));
1902               if (GET_CODE (XEXP (set_src, 1)) == PC)
1903                 {
1904                   rtx t = cond_false;
1905                   cond_false = cond_true;
1906                   cond_true = t;
1907                 }
1908
1909               SET_REGNO_REG_SET (pbi->reg_cond_reg, REGNO (reg));
1910
1911               /* For each such register, mark it conditionally dead.  */
1912               EXECUTE_IF_SET_IN_REG_SET
1913                 (diff, 0, i,
1914                  {
1915                    struct reg_cond_life_info *rcli;
1916                    rtx cond;
1917
1918                    rcli = xmalloc (sizeof (*rcli));
1919
1920                    if (REGNO_REG_SET_P (bb_true->global_live_at_start, i))
1921                      cond = cond_false;
1922                    else
1923                      cond = cond_true;
1924                    rcli->condition = cond;
1925                    rcli->stores = const0_rtx;
1926                    rcli->orig_condition = cond;
1927
1928                    splay_tree_insert (pbi->reg_cond_dead, i,
1929                                       (splay_tree_value) rcli);
1930                  });
1931             }
1932         }
1933
1934       FREE_REG_SET (diff);
1935     }
1936 #endif
1937
1938   /* If this block has no successors, any stores to the frame that aren't
1939      used later in the block are dead.  So make a pass over the block
1940      recording any such that are made and show them dead at the end.  We do
1941      a very conservative and simple job here.  */
1942   if (optimize
1943       && ! (TREE_CODE (TREE_TYPE (current_function_decl)) == FUNCTION_TYPE
1944             && (TYPE_RETURNS_STACK_DEPRESSED
1945                 (TREE_TYPE (current_function_decl))))
1946       && (flags & PROP_SCAN_DEAD_STORES)
1947       && (bb->succ == NULL
1948           || (bb->succ->succ_next == NULL
1949               && bb->succ->dest == EXIT_BLOCK_PTR
1950               && ! current_function_calls_eh_return)))
1951     {
1952       rtx insn, set;
1953       for (insn = bb->end; insn != bb->head; insn = PREV_INSN (insn))
1954         if (GET_CODE (insn) == INSN
1955             && (set = single_set (insn))
1956             && GET_CODE (SET_DEST (set)) == MEM)
1957           {
1958             rtx mem = SET_DEST (set);
1959             rtx canon_mem = canon_rtx (mem);
1960
1961             if (XEXP (canon_mem, 0) == frame_pointer_rtx
1962                 || (GET_CODE (XEXP (canon_mem, 0)) == PLUS
1963                     && XEXP (XEXP (canon_mem, 0), 0) == frame_pointer_rtx
1964                     && GET_CODE (XEXP (XEXP (canon_mem, 0), 1)) == CONST_INT))
1965               add_to_mem_set_list (pbi, canon_mem);
1966           }
1967     }
1968
1969   return pbi;
1970 }
1971
1972 /* Release a propagate_block_info struct.  */
1973
1974 void
1975 free_propagate_block_info (struct propagate_block_info *pbi)
1976 {
1977   free_EXPR_LIST_list (&pbi->mem_set_list);
1978
1979   BITMAP_XFREE (pbi->new_set);
1980
1981 #ifdef HAVE_conditional_execution
1982   splay_tree_delete (pbi->reg_cond_dead);
1983   BITMAP_XFREE (pbi->reg_cond_reg);
1984 #endif
1985
1986   if (pbi->reg_next_use)
1987     free (pbi->reg_next_use);
1988
1989   free (pbi);
1990 }
1991
1992 /* Compute the registers live at the beginning of a basic block BB from
1993    those live at the end.
1994
1995    When called, REG_LIVE contains those live at the end.  On return, it
1996    contains those live at the beginning.
1997
1998    LOCAL_SET, if non-null, will be set with all registers killed
1999    unconditionally by this basic block.
2000    Likewise, COND_LOCAL_SET, if non-null, will be set with all registers
2001    killed conditionally by this basic block.  If there is any unconditional
2002    set of a register, then the corresponding bit will be set in LOCAL_SET
2003    and cleared in COND_LOCAL_SET.
2004    It is valid for LOCAL_SET and COND_LOCAL_SET to be the same set.  In this
2005    case, the resulting set will be equal to the union of the two sets that
2006    would otherwise be computed.
2007
2008    Return nonzero if an INSN is deleted (i.e. by dead code removal).  */
2009
2010 int
2011 propagate_block (basic_block bb, regset live, regset local_set,
2012                  regset cond_local_set, int flags)
2013 {
2014   struct propagate_block_info *pbi;
2015   rtx insn, prev;
2016   int changed;
2017
2018   pbi = init_propagate_block_info (bb, live, local_set, cond_local_set, flags);
2019
2020   if (flags & PROP_REG_INFO)
2021     {
2022       int i;
2023
2024       /* Process the regs live at the end of the block.
2025          Mark them as not local to any one basic block.  */
2026       EXECUTE_IF_SET_IN_REG_SET (live, 0, i,
2027                                  { REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL; });
2028     }
2029
2030   /* Scan the block an insn at a time from end to beginning.  */
2031
2032   changed = 0;
2033   for (insn = bb->end;; insn = prev)
2034     {
2035       /* If this is a call to `setjmp' et al, warn if any
2036          non-volatile datum is live.  */
2037       if ((flags & PROP_REG_INFO)
2038           && GET_CODE (insn) == CALL_INSN
2039           && find_reg_note (insn, REG_SETJMP, NULL))
2040         IOR_REG_SET (regs_live_at_setjmp, pbi->reg_live);
2041
2042       prev = propagate_one_insn (pbi, insn);
2043       if (!prev)
2044         changed |= insn != get_insns ();
2045       else
2046         changed |= NEXT_INSN (prev) != insn;
2047
2048       if (insn == bb->head)
2049         break;
2050     }
2051
2052   free_propagate_block_info (pbi);
2053
2054   return changed;
2055 }
2056 \f
2057 /* Return 1 if X (the body of an insn, or part of it) is just dead stores
2058    (SET expressions whose destinations are registers dead after the insn).
2059    NEEDED is the regset that says which regs are alive after the insn.
2060
2061    Unless CALL_OK is nonzero, an insn is needed if it contains a CALL.
2062
2063    If X is the entire body of an insn, NOTES contains the reg notes
2064    pertaining to the insn.  */
2065
2066 static int
2067 insn_dead_p (struct propagate_block_info *pbi, rtx x, int call_ok,
2068              rtx notes ATTRIBUTE_UNUSED)
2069 {
2070   enum rtx_code code = GET_CODE (x);
2071
2072   /* Don't eliminate insns that may trap.  */
2073   if (flag_non_call_exceptions && may_trap_p (x))
2074     return 0;
2075
2076 #ifdef AUTO_INC_DEC
2077   /* As flow is invoked after combine, we must take existing AUTO_INC
2078      expressions into account.  */
2079   for (; notes; notes = XEXP (notes, 1))
2080     {
2081       if (REG_NOTE_KIND (notes) == REG_INC)
2082         {
2083           int regno = REGNO (XEXP (notes, 0));
2084
2085           /* Don't delete insns to set global regs.  */
2086           if ((regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
2087               || REGNO_REG_SET_P (pbi->reg_live, regno))
2088             return 0;
2089         }
2090     }
2091 #endif
2092
2093   /* If setting something that's a reg or part of one,
2094      see if that register's altered value will be live.  */
2095
2096   if (code == SET)
2097     {
2098       rtx r = SET_DEST (x);
2099
2100 #ifdef HAVE_cc0
2101       if (GET_CODE (r) == CC0)
2102         return ! pbi->cc0_live;
2103 #endif
2104
2105       /* A SET that is a subroutine call cannot be dead.  */
2106       if (GET_CODE (SET_SRC (x)) == CALL)
2107         {
2108           if (! call_ok)
2109             return 0;
2110         }
2111
2112       /* Don't eliminate loads from volatile memory or volatile asms.  */
2113       else if (volatile_refs_p (SET_SRC (x)))
2114         return 0;
2115
2116       if (GET_CODE (r) == MEM)
2117         {
2118           rtx temp, canon_r;
2119
2120           if (MEM_VOLATILE_P (r) || GET_MODE (r) == BLKmode)
2121             return 0;
2122
2123           canon_r = canon_rtx (r);
2124
2125           /* Walk the set of memory locations we are currently tracking
2126              and see if one is an identical match to this memory location.
2127              If so, this memory write is dead (remember, we're walking
2128              backwards from the end of the block to the start).  Since
2129              rtx_equal_p does not check the alias set or flags, we also
2130              must have the potential for them to conflict (anti_dependence).  */
2131           for (temp = pbi->mem_set_list; temp != 0; temp = XEXP (temp, 1))
2132             if (unchanging_anti_dependence (r, XEXP (temp, 0)))
2133               {
2134                 rtx mem = XEXP (temp, 0);
2135
2136                 if (rtx_equal_p (XEXP (canon_r, 0), XEXP (mem, 0))
2137                     && (GET_MODE_SIZE (GET_MODE (canon_r))
2138                         <= GET_MODE_SIZE (GET_MODE (mem))))
2139                   return 1;
2140
2141 #ifdef AUTO_INC_DEC
2142                 /* Check if memory reference matches an auto increment. Only
2143                    post increment/decrement or modify are valid.  */
2144                 if (GET_MODE (mem) == GET_MODE (r)
2145                     && (GET_CODE (XEXP (mem, 0)) == POST_DEC
2146                         || GET_CODE (XEXP (mem, 0)) == POST_INC
2147                         || GET_CODE (XEXP (mem, 0)) == POST_MODIFY)
2148                     && GET_MODE (XEXP (mem, 0)) == GET_MODE (r)
2149                     && rtx_equal_p (XEXP (XEXP (mem, 0), 0), XEXP (r, 0)))
2150                   return 1;
2151 #endif
2152               }
2153         }
2154       else
2155         {
2156           while (GET_CODE (r) == SUBREG
2157                  || GET_CODE (r) == STRICT_LOW_PART
2158                  || GET_CODE (r) == ZERO_EXTRACT)
2159             r = XEXP (r, 0);
2160
2161           if (GET_CODE (r) == REG)
2162             {
2163               int regno = REGNO (r);
2164
2165               /* Obvious.  */
2166               if (REGNO_REG_SET_P (pbi->reg_live, regno))
2167                 return 0;
2168
2169               /* If this is a hard register, verify that subsequent
2170                  words are not needed.  */
2171               if (regno < FIRST_PSEUDO_REGISTER)
2172                 {
2173                   int n = HARD_REGNO_NREGS (regno, GET_MODE (r));
2174
2175                   while (--n > 0)
2176                     if (REGNO_REG_SET_P (pbi->reg_live, regno+n))
2177                       return 0;
2178                 }
2179
2180               /* Don't delete insns to set global regs.  */
2181               if (regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
2182                 return 0;
2183
2184               /* Make sure insns to set the stack pointer aren't deleted.  */
2185               if (regno == STACK_POINTER_REGNUM)
2186                 return 0;
2187
2188               /* ??? These bits might be redundant with the force live bits
2189                  in calculate_global_regs_live.  We would delete from
2190                  sequential sets; whether this actually affects real code
2191                  for anything but the stack pointer I don't know.  */
2192               /* Make sure insns to set the frame pointer aren't deleted.  */
2193               if (regno == FRAME_POINTER_REGNUM
2194                   && (! reload_completed || frame_pointer_needed))
2195                 return 0;
2196 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2197               if (regno == HARD_FRAME_POINTER_REGNUM
2198                   && (! reload_completed || frame_pointer_needed))
2199                 return 0;
2200 #endif
2201
2202 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2203               /* Make sure insns to set arg pointer are never deleted
2204                  (if the arg pointer isn't fixed, there will be a USE
2205                  for it, so we can treat it normally).  */
2206               if (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
2207                 return 0;
2208 #endif
2209
2210               /* Otherwise, the set is dead.  */
2211               return 1;
2212             }
2213         }
2214     }
2215
2216   /* If performing several activities, insn is dead if each activity
2217      is individually dead.  Also, CLOBBERs and USEs can be ignored; a
2218      CLOBBER or USE that's inside a PARALLEL doesn't make the insn
2219      worth keeping.  */
2220   else if (code == PARALLEL)
2221     {
2222       int i = XVECLEN (x, 0);
2223
2224       for (i--; i >= 0; i--)
2225         if (GET_CODE (XVECEXP (x, 0, i)) != CLOBBER
2226             && GET_CODE (XVECEXP (x, 0, i)) != USE
2227             && ! insn_dead_p (pbi, XVECEXP (x, 0, i), call_ok, NULL_RTX))
2228           return 0;
2229
2230       return 1;
2231     }
2232
2233   /* A CLOBBER of a pseudo-register that is dead serves no purpose.  That
2234      is not necessarily true for hard registers.  */
2235   else if (code == CLOBBER && GET_CODE (XEXP (x, 0)) == REG
2236            && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER
2237            && ! REGNO_REG_SET_P (pbi->reg_live, REGNO (XEXP (x, 0))))
2238     return 1;
2239
2240   /* We do not check other CLOBBER or USE here.  An insn consisting of just
2241      a CLOBBER or just a USE should not be deleted.  */
2242   return 0;
2243 }
2244
2245 /* If INSN is the last insn in a libcall, and assuming INSN is dead,
2246    return 1 if the entire library call is dead.
2247    This is true if INSN copies a register (hard or pseudo)
2248    and if the hard return reg of the call insn is dead.
2249    (The caller should have tested the destination of the SET inside
2250    INSN already for death.)
2251
2252    If this insn doesn't just copy a register, then we don't
2253    have an ordinary libcall.  In that case, cse could not have
2254    managed to substitute the source for the dest later on,
2255    so we can assume the libcall is dead.
2256
2257    PBI is the block info giving pseudoregs live before this insn.
2258    NOTE is the REG_RETVAL note of the insn.  */
2259
2260 static int
2261 libcall_dead_p (struct propagate_block_info *pbi, rtx note, rtx insn)
2262 {
2263   rtx x = single_set (insn);
2264
2265   if (x)
2266     {
2267       rtx r = SET_SRC (x);
2268
2269       if (GET_CODE (r) == REG)
2270         {
2271           rtx call = XEXP (note, 0);
2272           rtx call_pat;
2273           int i;
2274
2275           /* Find the call insn.  */
2276           while (call != insn && GET_CODE (call) != CALL_INSN)
2277             call = NEXT_INSN (call);
2278
2279           /* If there is none, do nothing special,
2280              since ordinary death handling can understand these insns.  */
2281           if (call == insn)
2282             return 0;
2283
2284           /* See if the hard reg holding the value is dead.
2285              If this is a PARALLEL, find the call within it.  */
2286           call_pat = PATTERN (call);
2287           if (GET_CODE (call_pat) == PARALLEL)
2288             {
2289               for (i = XVECLEN (call_pat, 0) - 1; i >= 0; i--)
2290                 if (GET_CODE (XVECEXP (call_pat, 0, i)) == SET
2291                     && GET_CODE (SET_SRC (XVECEXP (call_pat, 0, i))) == CALL)
2292                   break;
2293
2294               /* This may be a library call that is returning a value
2295                  via invisible pointer.  Do nothing special, since
2296                  ordinary death handling can understand these insns.  */
2297               if (i < 0)
2298                 return 0;
2299
2300               call_pat = XVECEXP (call_pat, 0, i);
2301             }
2302
2303           return insn_dead_p (pbi, call_pat, 1, REG_NOTES (call));
2304         }
2305     }
2306   return 1;
2307 }
2308
2309 /* Return 1 if register REGNO was used before it was set, i.e. if it is
2310    live at function entry.  Don't count global register variables, variables
2311    in registers that can be used for function arg passing, or variables in
2312    fixed hard registers.  */
2313
2314 int
2315 regno_uninitialized (unsigned int regno)
2316 {
2317   if (n_basic_blocks == 0
2318       || (regno < FIRST_PSEUDO_REGISTER
2319           && (global_regs[regno]
2320               || fixed_regs[regno]
2321               || FUNCTION_ARG_REGNO_P (regno))))
2322     return 0;
2323
2324   return REGNO_REG_SET_P (ENTRY_BLOCK_PTR->global_live_at_end, regno);
2325 }
2326
2327 /* 1 if register REGNO was alive at a place where `setjmp' was called
2328    and was set more than once or is an argument.
2329    Such regs may be clobbered by `longjmp'.  */
2330
2331 int
2332 regno_clobbered_at_setjmp (int regno)
2333 {
2334   if (n_basic_blocks == 0)
2335     return 0;
2336
2337   return ((REG_N_SETS (regno) > 1
2338            || REGNO_REG_SET_P (ENTRY_BLOCK_PTR->global_live_at_end, regno))
2339           && REGNO_REG_SET_P (regs_live_at_setjmp, regno));
2340 }
2341 \f
2342 /* Add MEM to PBI->MEM_SET_LIST.  MEM should be canonical.  Respect the
2343    maximal list size; look for overlaps in mode and select the largest.  */
2344 static void
2345 add_to_mem_set_list (struct propagate_block_info *pbi, rtx mem)
2346 {
2347   rtx i;
2348
2349   /* We don't know how large a BLKmode store is, so we must not
2350      take them into consideration.  */
2351   if (GET_MODE (mem) == BLKmode)
2352     return;
2353
2354   for (i = pbi->mem_set_list; i ; i = XEXP (i, 1))
2355     {
2356       rtx e = XEXP (i, 0);
2357       if (rtx_equal_p (XEXP (mem, 0), XEXP (e, 0)))
2358         {
2359           if (GET_MODE_SIZE (GET_MODE (mem)) > GET_MODE_SIZE (GET_MODE (e)))
2360             {
2361 #ifdef AUTO_INC_DEC
2362               /* If we must store a copy of the mem, we can just modify
2363                  the mode of the stored copy.  */
2364               if (pbi->flags & PROP_AUTOINC)
2365                 PUT_MODE (e, GET_MODE (mem));
2366               else
2367 #endif
2368                 XEXP (i, 0) = mem;
2369             }
2370           return;
2371         }
2372     }
2373
2374   if (pbi->mem_set_list_len < MAX_MEM_SET_LIST_LEN)
2375     {
2376 #ifdef AUTO_INC_DEC
2377       /* Store a copy of mem, otherwise the address may be
2378          scrogged by find_auto_inc.  */
2379       if (pbi->flags & PROP_AUTOINC)
2380         mem = shallow_copy_rtx (mem);
2381 #endif
2382       pbi->mem_set_list = alloc_EXPR_LIST (0, mem, pbi->mem_set_list);
2383       pbi->mem_set_list_len++;
2384     }
2385 }
2386
2387 /* INSN references memory, possibly using autoincrement addressing modes.
2388    Find any entries on the mem_set_list that need to be invalidated due
2389    to an address change.  */
2390
2391 static int
2392 invalidate_mems_from_autoinc (rtx *px, void *data)
2393 {
2394   rtx x = *px;
2395   struct propagate_block_info *pbi = data;
2396
2397   if (GET_RTX_CLASS (GET_CODE (x)) == 'a')
2398     {
2399       invalidate_mems_from_set (pbi, XEXP (x, 0));
2400       return -1;
2401     }
2402
2403   return 0;
2404 }
2405
2406 /* EXP is a REG.  Remove any dependent entries from pbi->mem_set_list.  */
2407
2408 static void
2409 invalidate_mems_from_set (struct propagate_block_info *pbi, rtx exp)
2410 {
2411   rtx temp = pbi->mem_set_list;
2412   rtx prev = NULL_RTX;
2413   rtx next;
2414
2415   while (temp)
2416     {
2417       next = XEXP (temp, 1);
2418       if (reg_overlap_mentioned_p (exp, XEXP (temp, 0)))
2419         {
2420           /* Splice this entry out of the list.  */
2421           if (prev)
2422             XEXP (prev, 1) = next;
2423           else
2424             pbi->mem_set_list = next;
2425           free_EXPR_LIST_node (temp);
2426           pbi->mem_set_list_len--;
2427         }
2428       else
2429         prev = temp;
2430       temp = next;
2431     }
2432 }
2433
2434 /* Process the registers that are set within X.  Their bits are set to
2435    1 in the regset DEAD, because they are dead prior to this insn.
2436
2437    If INSN is nonzero, it is the insn being processed.
2438
2439    FLAGS is the set of operations to perform.  */
2440
2441 static void
2442 mark_set_regs (struct propagate_block_info *pbi, rtx x, rtx insn)
2443 {
2444   rtx cond = NULL_RTX;
2445   rtx link;
2446   enum rtx_code code;
2447
2448   if (insn)
2449     for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2450       {
2451         if (REG_NOTE_KIND (link) == REG_INC)
2452           mark_set_1 (pbi, SET, XEXP (link, 0),
2453                       (GET_CODE (x) == COND_EXEC
2454                        ? COND_EXEC_TEST (x) : NULL_RTX),
2455                       insn, pbi->flags);
2456       }
2457  retry:
2458   switch (code = GET_CODE (x))
2459     {
2460     case SET:
2461     case CLOBBER:
2462       mark_set_1 (pbi, code, SET_DEST (x), cond, insn, pbi->flags);
2463       return;
2464
2465     case COND_EXEC:
2466       cond = COND_EXEC_TEST (x);
2467       x = COND_EXEC_CODE (x);
2468       goto retry;
2469
2470     case PARALLEL:
2471       {
2472         int i;
2473
2474         for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
2475           {
2476             rtx sub = XVECEXP (x, 0, i);
2477             switch (code = GET_CODE (sub))
2478               {
2479               case COND_EXEC:
2480                 if (cond != NULL_RTX)
2481                   abort ();
2482
2483                 cond = COND_EXEC_TEST (sub);
2484                 sub = COND_EXEC_CODE (sub);
2485                 if (GET_CODE (sub) != SET && GET_CODE (sub) != CLOBBER)
2486                   break;
2487                 /* Fall through.  */
2488
2489               case SET:
2490               case CLOBBER:
2491                 mark_set_1 (pbi, code, SET_DEST (sub), cond, insn, pbi->flags);
2492                 break;
2493
2494               default:
2495                 break;
2496               }
2497           }
2498         break;
2499       }
2500
2501     default:
2502       break;
2503     }
2504 }
2505
2506 /* Process a single set, which appears in INSN.  REG (which may not
2507    actually be a REG, it may also be a SUBREG, PARALLEL, etc.) is
2508    being set using the CODE (which may be SET, CLOBBER, or COND_EXEC).
2509    If the set is conditional (because it appear in a COND_EXEC), COND
2510    will be the condition.  */
2511
2512 static void
2513 mark_set_1 (struct propagate_block_info *pbi, enum rtx_code code, rtx reg, rtx cond, rtx insn, int flags)
2514 {
2515   int regno_first = -1, regno_last = -1;
2516   unsigned long not_dead = 0;
2517   int i;
2518
2519   /* Modifying just one hardware register of a multi-reg value or just a
2520      byte field of a register does not mean the value from before this insn
2521      is now dead.  Of course, if it was dead after it's unused now.  */
2522
2523   switch (GET_CODE (reg))
2524     {
2525     case PARALLEL:
2526       /* Some targets place small structures in registers for return values of
2527          functions.  We have to detect this case specially here to get correct
2528          flow information.  */
2529       for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
2530         if (XEXP (XVECEXP (reg, 0, i), 0) != 0)
2531           mark_set_1 (pbi, code, XEXP (XVECEXP (reg, 0, i), 0), cond, insn,
2532                       flags);
2533       return;
2534
2535     case ZERO_EXTRACT:
2536     case SIGN_EXTRACT:
2537     case STRICT_LOW_PART:
2538       /* ??? Assumes STRICT_LOW_PART not used on multi-word registers.  */
2539       do
2540         reg = XEXP (reg, 0);
2541       while (GET_CODE (reg) == SUBREG
2542              || GET_CODE (reg) == ZERO_EXTRACT
2543              || GET_CODE (reg) == SIGN_EXTRACT
2544              || GET_CODE (reg) == STRICT_LOW_PART);
2545       if (GET_CODE (reg) == MEM)
2546         break;
2547       not_dead = (unsigned long) REGNO_REG_SET_P (pbi->reg_live, REGNO (reg));
2548       /* Fall through.  */
2549
2550     case REG:
2551       regno_last = regno_first = REGNO (reg);
2552       if (regno_first < FIRST_PSEUDO_REGISTER)
2553         regno_last += HARD_REGNO_NREGS (regno_first, GET_MODE (reg)) - 1;
2554       break;
2555
2556     case SUBREG:
2557       if (GET_CODE (SUBREG_REG (reg)) == REG)
2558         {
2559           enum machine_mode outer_mode = GET_MODE (reg);
2560           enum machine_mode inner_mode = GET_MODE (SUBREG_REG (reg));
2561
2562           /* Identify the range of registers affected.  This is moderately
2563              tricky for hard registers.  See alter_subreg.  */
2564
2565           regno_last = regno_first = REGNO (SUBREG_REG (reg));
2566           if (regno_first < FIRST_PSEUDO_REGISTER)
2567             {
2568               regno_first += subreg_regno_offset (regno_first, inner_mode,
2569                                                   SUBREG_BYTE (reg),
2570                                                   outer_mode);
2571               regno_last = (regno_first
2572                             + HARD_REGNO_NREGS (regno_first, outer_mode) - 1);
2573
2574               /* Since we've just adjusted the register number ranges, make
2575                  sure REG matches.  Otherwise some_was_live will be clear
2576                  when it shouldn't have been, and we'll create incorrect
2577                  REG_UNUSED notes.  */
2578               reg = gen_rtx_REG (outer_mode, regno_first);
2579             }
2580           else
2581             {
2582               /* If the number of words in the subreg is less than the number
2583                  of words in the full register, we have a well-defined partial
2584                  set.  Otherwise the high bits are undefined.
2585
2586                  This is only really applicable to pseudos, since we just took
2587                  care of multi-word hard registers.  */
2588               if (((GET_MODE_SIZE (outer_mode)
2589                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
2590                   < ((GET_MODE_SIZE (inner_mode)
2591                       + UNITS_PER_WORD - 1) / UNITS_PER_WORD))
2592                 not_dead = (unsigned long) REGNO_REG_SET_P (pbi->reg_live,
2593                                                             regno_first);
2594
2595               reg = SUBREG_REG (reg);
2596             }
2597         }
2598       else
2599         reg = SUBREG_REG (reg);
2600       break;
2601
2602     default:
2603       break;
2604     }
2605
2606   /* If this set is a MEM, then it kills any aliased writes.
2607      If this set is a REG, then it kills any MEMs which use the reg.  */
2608   if (optimize && (flags & PROP_SCAN_DEAD_STORES))
2609     {
2610       if (GET_CODE (reg) == REG)
2611         invalidate_mems_from_set (pbi, reg);
2612
2613       /* If the memory reference had embedded side effects (autoincrement
2614          address modes.  Then we may need to kill some entries on the
2615          memory set list.  */
2616       if (insn && GET_CODE (reg) == MEM)
2617         for_each_rtx (&PATTERN (insn), invalidate_mems_from_autoinc, pbi);
2618
2619       if (GET_CODE (reg) == MEM && ! side_effects_p (reg)
2620           /* ??? With more effort we could track conditional memory life.  */
2621           && ! cond)
2622         add_to_mem_set_list (pbi, canon_rtx (reg));
2623     }
2624
2625   if (GET_CODE (reg) == REG
2626       && ! (regno_first == FRAME_POINTER_REGNUM
2627             && (! reload_completed || frame_pointer_needed))
2628 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2629       && ! (regno_first == HARD_FRAME_POINTER_REGNUM
2630             && (! reload_completed || frame_pointer_needed))
2631 #endif
2632 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2633       && ! (regno_first == ARG_POINTER_REGNUM && fixed_regs[regno_first])
2634 #endif
2635       )
2636     {
2637       int some_was_live = 0, some_was_dead = 0;
2638
2639       for (i = regno_first; i <= regno_last; ++i)
2640         {
2641           int needed_regno = REGNO_REG_SET_P (pbi->reg_live, i);
2642           if (pbi->local_set)
2643             {
2644               /* Order of the set operation matters here since both
2645                  sets may be the same.  */
2646               CLEAR_REGNO_REG_SET (pbi->cond_local_set, i);
2647               if (cond != NULL_RTX
2648                   && ! REGNO_REG_SET_P (pbi->local_set, i))
2649                 SET_REGNO_REG_SET (pbi->cond_local_set, i);
2650               else
2651                 SET_REGNO_REG_SET (pbi->local_set, i);
2652             }
2653           if (code != CLOBBER)
2654             SET_REGNO_REG_SET (pbi->new_set, i);
2655
2656           some_was_live |= needed_regno;
2657           some_was_dead |= ! needed_regno;
2658         }
2659
2660 #ifdef HAVE_conditional_execution
2661       /* Consider conditional death in deciding that the register needs
2662          a death note.  */
2663       if (some_was_live && ! not_dead
2664           /* The stack pointer is never dead.  Well, not strictly true,
2665              but it's very difficult to tell from here.  Hopefully
2666              combine_stack_adjustments will fix up the most egregious
2667              errors.  */
2668           && regno_first != STACK_POINTER_REGNUM)
2669         {
2670           for (i = regno_first; i <= regno_last; ++i)
2671             if (! mark_regno_cond_dead (pbi, i, cond))
2672               not_dead |= ((unsigned long) 1) << (i - regno_first);
2673         }
2674 #endif
2675
2676       /* Additional data to record if this is the final pass.  */
2677       if (flags & (PROP_LOG_LINKS | PROP_REG_INFO
2678                    | PROP_DEATH_NOTES | PROP_AUTOINC))
2679         {
2680           rtx y;
2681           int blocknum = pbi->bb->index;
2682
2683           y = NULL_RTX;
2684           if (flags & (PROP_LOG_LINKS | PROP_AUTOINC))
2685             {
2686               y = pbi->reg_next_use[regno_first];
2687
2688               /* The next use is no longer next, since a store intervenes.  */
2689               for (i = regno_first; i <= regno_last; ++i)
2690                 pbi->reg_next_use[i] = 0;
2691             }
2692
2693           if (flags & PROP_REG_INFO)
2694             {
2695               for (i = regno_first; i <= regno_last; ++i)
2696                 {
2697                   /* Count (weighted) references, stores, etc.  This counts a
2698                      register twice if it is modified, but that is correct.  */
2699                   REG_N_SETS (i) += 1;
2700                   REG_N_REFS (i) += 1;
2701                   REG_FREQ (i) += REG_FREQ_FROM_BB (pbi->bb);
2702
2703                   /* The insns where a reg is live are normally counted
2704                      elsewhere, but we want the count to include the insn
2705                      where the reg is set, and the normal counting mechanism
2706                      would not count it.  */
2707                   REG_LIVE_LENGTH (i) += 1;
2708                 }
2709
2710               /* If this is a hard reg, record this function uses the reg.  */
2711               if (regno_first < FIRST_PSEUDO_REGISTER)
2712                 {
2713                   for (i = regno_first; i <= regno_last; i++)
2714                     regs_ever_live[i] = 1;
2715                 }
2716               else
2717                 {
2718                   /* Keep track of which basic blocks each reg appears in.  */
2719                   if (REG_BASIC_BLOCK (regno_first) == REG_BLOCK_UNKNOWN)
2720                     REG_BASIC_BLOCK (regno_first) = blocknum;
2721                   else if (REG_BASIC_BLOCK (regno_first) != blocknum)
2722                     REG_BASIC_BLOCK (regno_first) = REG_BLOCK_GLOBAL;
2723                 }
2724             }
2725
2726           if (! some_was_dead)
2727             {
2728               if (flags & PROP_LOG_LINKS)
2729                 {
2730                   /* Make a logical link from the next following insn
2731                      that uses this register, back to this insn.
2732                      The following insns have already been processed.
2733
2734                      We don't build a LOG_LINK for hard registers containing
2735                      in ASM_OPERANDs.  If these registers get replaced,
2736                      we might wind up changing the semantics of the insn,
2737                      even if reload can make what appear to be valid
2738                      assignments later.  */
2739                   if (y && (BLOCK_NUM (y) == blocknum)
2740                       && (regno_first >= FIRST_PSEUDO_REGISTER
2741                           || asm_noperands (PATTERN (y)) < 0))
2742                     LOG_LINKS (y) = alloc_INSN_LIST (insn, LOG_LINKS (y));
2743                 }
2744             }
2745           else if (not_dead)
2746             ;
2747           else if (! some_was_live)
2748             {
2749               if (flags & PROP_REG_INFO)
2750                 REG_N_DEATHS (regno_first) += 1;
2751
2752               if (flags & PROP_DEATH_NOTES)
2753                 {
2754                   /* Note that dead stores have already been deleted
2755                      when possible.  If we get here, we have found a
2756                      dead store that cannot be eliminated (because the
2757                      same insn does something useful).  Indicate this
2758                      by marking the reg being set as dying here.  */
2759                   REG_NOTES (insn)
2760                     = alloc_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn));
2761                 }
2762             }
2763           else
2764             {
2765               if (flags & PROP_DEATH_NOTES)
2766                 {
2767                   /* This is a case where we have a multi-word hard register
2768                      and some, but not all, of the words of the register are
2769                      needed in subsequent insns.  Write REG_UNUSED notes
2770                      for those parts that were not needed.  This case should
2771                      be rare.  */
2772
2773                   for (i = regno_first; i <= regno_last; ++i)
2774                     if (! REGNO_REG_SET_P (pbi->reg_live, i))
2775                       REG_NOTES (insn)
2776                         = alloc_EXPR_LIST (REG_UNUSED,
2777                                            regno_reg_rtx[i],
2778                                            REG_NOTES (insn));
2779                 }
2780             }
2781         }
2782
2783       /* Mark the register as being dead.  */
2784       if (some_was_live
2785           /* The stack pointer is never dead.  Well, not strictly true,
2786              but it's very difficult to tell from here.  Hopefully
2787              combine_stack_adjustments will fix up the most egregious
2788              errors.  */
2789           && regno_first != STACK_POINTER_REGNUM)
2790         {
2791           for (i = regno_first; i <= regno_last; ++i)
2792             if (!(not_dead & (((unsigned long) 1) << (i - regno_first))))
2793               CLEAR_REGNO_REG_SET (pbi->reg_live, i);
2794         }
2795     }
2796   else if (GET_CODE (reg) == REG)
2797     {
2798       if (flags & (PROP_LOG_LINKS | PROP_AUTOINC))
2799         pbi->reg_next_use[regno_first] = 0;
2800     }
2801
2802   /* If this is the last pass and this is a SCRATCH, show it will be dying
2803      here and count it.  */
2804   else if (GET_CODE (reg) == SCRATCH)
2805     {
2806       if (flags & PROP_DEATH_NOTES)
2807         REG_NOTES (insn)
2808           = alloc_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn));
2809     }
2810 }
2811 \f
2812 #ifdef HAVE_conditional_execution
2813 /* Mark REGNO conditionally dead.
2814    Return true if the register is now unconditionally dead.  */
2815
2816 static int
2817 mark_regno_cond_dead (struct propagate_block_info *pbi, int regno, rtx cond)
2818 {
2819   /* If this is a store to a predicate register, the value of the
2820      predicate is changing, we don't know that the predicate as seen
2821      before is the same as that seen after.  Flush all dependent
2822      conditions from reg_cond_dead.  This will make all such
2823      conditionally live registers unconditionally live.  */
2824   if (REGNO_REG_SET_P (pbi->reg_cond_reg, regno))
2825     flush_reg_cond_reg (pbi, regno);
2826
2827   /* If this is an unconditional store, remove any conditional
2828      life that may have existed.  */
2829   if (cond == NULL_RTX)
2830     splay_tree_remove (pbi->reg_cond_dead, regno);
2831   else
2832     {
2833       splay_tree_node node;
2834       struct reg_cond_life_info *rcli;
2835       rtx ncond;
2836
2837       /* Otherwise this is a conditional set.  Record that fact.
2838          It may have been conditionally used, or there may be a
2839          subsequent set with a complimentary condition.  */
2840
2841       node = splay_tree_lookup (pbi->reg_cond_dead, regno);
2842       if (node == NULL)
2843         {
2844           /* The register was unconditionally live previously.
2845              Record the current condition as the condition under
2846              which it is dead.  */
2847           rcli = xmalloc (sizeof (*rcli));
2848           rcli->condition = cond;
2849           rcli->stores = cond;
2850           rcli->orig_condition = const0_rtx;
2851           splay_tree_insert (pbi->reg_cond_dead, regno,
2852                              (splay_tree_value) rcli);
2853
2854           SET_REGNO_REG_SET (pbi->reg_cond_reg, REGNO (XEXP (cond, 0)));
2855
2856           /* Not unconditionally dead.  */
2857           return 0;
2858         }
2859       else
2860         {
2861           /* The register was conditionally live previously.
2862              Add the new condition to the old.  */
2863           rcli = (struct reg_cond_life_info *) node->value;
2864           ncond = rcli->condition;
2865           ncond = ior_reg_cond (ncond, cond, 1);
2866           if (rcli->stores == const0_rtx)
2867             rcli->stores = cond;
2868           else if (rcli->stores != const1_rtx)
2869             rcli->stores = ior_reg_cond (rcli->stores, cond, 1);
2870
2871           /* If the register is now unconditionally dead, remove the entry
2872              in the splay_tree.  A register is unconditionally dead if the
2873              dead condition ncond is true.  A register is also unconditionally
2874              dead if the sum of all conditional stores is an unconditional
2875              store (stores is true), and the dead condition is identically the
2876              same as the original dead condition initialized at the end of
2877              the block.  This is a pointer compare, not an rtx_equal_p
2878              compare.  */
2879           if (ncond == const1_rtx
2880               || (ncond == rcli->orig_condition && rcli->stores == const1_rtx))
2881             splay_tree_remove (pbi->reg_cond_dead, regno);
2882           else
2883             {
2884               rcli->condition = ncond;
2885
2886               SET_REGNO_REG_SET (pbi->reg_cond_reg, REGNO (XEXP (cond, 0)));
2887
2888               /* Not unconditionally dead.  */
2889               return 0;
2890             }
2891         }
2892     }
2893
2894   return 1;
2895 }
2896
2897 /* Called from splay_tree_delete for pbi->reg_cond_life.  */
2898
2899 static void
2900 free_reg_cond_life_info (splay_tree_value value)
2901 {
2902   struct reg_cond_life_info *rcli = (struct reg_cond_life_info *) value;
2903   free (rcli);
2904 }
2905
2906 /* Helper function for flush_reg_cond_reg.  */
2907
2908 static int
2909 flush_reg_cond_reg_1 (splay_tree_node node, void *data)
2910 {
2911   struct reg_cond_life_info *rcli;
2912   int *xdata = (int *) data;
2913   unsigned int regno = xdata[0];
2914
2915   /* Don't need to search if last flushed value was farther on in
2916      the in-order traversal.  */
2917   if (xdata[1] >= (int) node->key)
2918     return 0;
2919
2920   /* Splice out portions of the expression that refer to regno.  */
2921   rcli = (struct reg_cond_life_info *) node->value;
2922   rcli->condition = elim_reg_cond (rcli->condition, regno);
2923   if (rcli->stores != const0_rtx && rcli->stores != const1_rtx)
2924     rcli->stores = elim_reg_cond (rcli->stores, regno);
2925
2926   /* If the entire condition is now false, signal the node to be removed.  */
2927   if (rcli->condition == const0_rtx)
2928     {
2929       xdata[1] = node->key;
2930       return -1;
2931     }
2932   else if (rcli->condition == const1_rtx)
2933     abort ();
2934
2935   return 0;
2936 }
2937
2938 /* Flush all (sub) expressions referring to REGNO from REG_COND_LIVE.  */
2939
2940 static void
2941 flush_reg_cond_reg (struct propagate_block_info *pbi, int regno)
2942 {
2943   int pair[2];
2944
2945   pair[0] = regno;
2946   pair[1] = -1;
2947   while (splay_tree_foreach (pbi->reg_cond_dead,
2948                              flush_reg_cond_reg_1, pair) == -1)
2949     splay_tree_remove (pbi->reg_cond_dead, pair[1]);
2950
2951   CLEAR_REGNO_REG_SET (pbi->reg_cond_reg, regno);
2952 }
2953
2954 /* Logical arithmetic on predicate conditions.  IOR, NOT and AND.
2955    For ior/and, the ADD flag determines whether we want to add the new
2956    condition X to the old one unconditionally.  If it is zero, we will
2957    only return a new expression if X allows us to simplify part of
2958    OLD, otherwise we return NULL to the caller.
2959    If ADD is nonzero, we will return a new condition in all cases.  The
2960    toplevel caller of one of these functions should always pass 1 for
2961    ADD.  */
2962
2963 static rtx
2964 ior_reg_cond (rtx old, rtx x, int add)
2965 {
2966   rtx op0, op1;
2967
2968   if (GET_RTX_CLASS (GET_CODE (old)) == '<')
2969     {
2970       if (GET_RTX_CLASS (GET_CODE (x)) == '<'
2971           && REVERSE_CONDEXEC_PREDICATES_P (GET_CODE (x), GET_CODE (old))
2972           && REGNO (XEXP (x, 0)) == REGNO (XEXP (old, 0)))
2973         return const1_rtx;
2974       if (GET_CODE (x) == GET_CODE (old)
2975           && REGNO (XEXP (x, 0)) == REGNO (XEXP (old, 0)))
2976         return old;
2977       if (! add)
2978         return NULL;
2979       return gen_rtx_IOR (0, old, x);
2980     }
2981
2982   switch (GET_CODE (old))
2983     {
2984     case IOR:
2985       op0 = ior_reg_cond (XEXP (old, 0), x, 0);
2986       op1 = ior_reg_cond (XEXP (old, 1), x, 0);
2987       if (op0 != NULL || op1 != NULL)
2988         {
2989           if (op0 == const0_rtx)
2990             return op1 ? op1 : gen_rtx_IOR (0, XEXP (old, 1), x);
2991           if (op1 == const0_rtx)
2992             return op0 ? op0 : gen_rtx_IOR (0, XEXP (old, 0), x);
2993           if (op0 == const1_rtx || op1 == const1_rtx)
2994             return const1_rtx;
2995           if (op0 == NULL)
2996             op0 = gen_rtx_IOR (0, XEXP (old, 0), x);
2997           else if (rtx_equal_p (x, op0))
2998             /* (x | A) | x ~ (x | A).  */
2999             return old;
3000           if (op1 == NULL)
3001             op1 = gen_rtx_IOR (0, XEXP (old, 1), x);
3002           else if (rtx_equal_p (x, op1))
3003             /* (A | x) | x ~ (A | x).  */
3004             return old;
3005           return gen_rtx_IOR (0, op0, op1);
3006         }
3007       if (! add)
3008         return NULL;
3009       return gen_rtx_IOR (0, old, x);
3010
3011     case AND:
3012       op0 = ior_reg_cond (XEXP (old, 0), x, 0);
3013       op1 = ior_reg_cond (XEXP (old, 1), x, 0);
3014       if (op0 != NULL || op1 != NULL)
3015         {
3016           if (op0 == const1_rtx)
3017             return op1 ? op1 : gen_rtx_IOR (0, XEXP (old, 1), x);
3018           if (op1 == const1_rtx)
3019             return op0 ? op0 : gen_rtx_IOR (0, XEXP (old, 0), x);
3020           if (op0 == const0_rtx || op1 == const0_rtx)
3021             return const0_rtx;
3022           if (op0 == NULL)
3023             op0 = gen_rtx_IOR (0, XEXP (old, 0), x);
3024           else if (rtx_equal_p (x, op0))
3025             /* (x & A) | x ~ x.  */
3026             return op0;
3027           if (op1 == NULL)
3028             op1 = gen_rtx_IOR (0, XEXP (old, 1), x);
3029           else if (rtx_equal_p (x, op1))
3030             /* (A & x) | x ~ x.  */
3031             return op1;
3032           return gen_rtx_AND (0, op0, op1);
3033         }
3034       if (! add)
3035         return NULL;
3036       return gen_rtx_IOR (0, old, x);
3037
3038     case NOT:
3039       op0 = and_reg_cond (XEXP (old, 0), not_reg_cond (x), 0);
3040       if (op0 != NULL)
3041         return not_reg_cond (op0);
3042       if (! add)
3043         return NULL;
3044       return gen_rtx_IOR (0, old, x);
3045
3046     default:
3047       abort ();
3048     }
3049 }
3050
3051 static rtx
3052 not_reg_cond (rtx x)
3053 {
3054   enum rtx_code x_code;
3055
3056   if (x == const0_rtx)
3057     return const1_rtx;
3058   else if (x == const1_rtx)
3059     return const0_rtx;
3060   x_code = GET_CODE (x);
3061   if (x_code == NOT)
3062     return XEXP (x, 0);
3063   if (GET_RTX_CLASS (x_code) == '<'
3064       && GET_CODE (XEXP (x, 0)) == REG)
3065     {
3066       if (XEXP (x, 1) != const0_rtx)
3067         abort ();
3068
3069       return gen_rtx_fmt_ee (reverse_condition (x_code),
3070                              VOIDmode, XEXP (x, 0), const0_rtx);
3071     }
3072   return gen_rtx_NOT (0, x);
3073 }
3074
3075 static rtx
3076 and_reg_cond (rtx old, rtx x, int add)
3077 {
3078   rtx op0, op1;
3079
3080   if (GET_RTX_CLASS (GET_CODE (old)) == '<')
3081     {
3082       if (GET_RTX_CLASS (GET_CODE (x)) == '<'
3083           && GET_CODE (x) == reverse_condition (GET_CODE (old))
3084           && REGNO (XEXP (x, 0)) == REGNO (XEXP (old, 0)))
3085         return const0_rtx;
3086       if (GET_CODE (x) == GET_CODE (old)
3087           && REGNO (XEXP (x, 0)) == REGNO (XEXP (old, 0)))
3088         return old;
3089       if (! add)
3090         return NULL;
3091       return gen_rtx_AND (0, old, x);
3092     }
3093
3094   switch (GET_CODE (old))
3095     {
3096     case IOR:
3097       op0 = and_reg_cond (XEXP (old, 0), x, 0);
3098       op1 = and_reg_cond (XEXP (old, 1), x, 0);
3099       if (op0 != NULL || op1 != NULL)
3100         {
3101           if (op0 == const0_rtx)
3102             return op1 ? op1 : gen_rtx_AND (0, XEXP (old, 1), x);
3103           if (op1 == const0_rtx)
3104             return op0 ? op0 : gen_rtx_AND (0, XEXP (old, 0), x);
3105           if (op0 == const1_rtx || op1 == const1_rtx)
3106             return const1_rtx;
3107           if (op0 == NULL)
3108             op0 = gen_rtx_AND (0, XEXP (old, 0), x);
3109           else if (rtx_equal_p (x, op0))
3110             /* (x | A) & x ~ x.  */
3111             return op0;
3112           if (op1 == NULL)
3113             op1 = gen_rtx_AND (0, XEXP (old, 1), x);
3114           else if (rtx_equal_p (x, op1))
3115             /* (A | x) & x ~ x.  */
3116             return op1;
3117           return gen_rtx_IOR (0, op0, op1);
3118         }
3119       if (! add)
3120         return NULL;
3121       return gen_rtx_AND (0, old, x);
3122
3123     case AND:
3124       op0 = and_reg_cond (XEXP (old, 0), x, 0);
3125       op1 = and_reg_cond (XEXP (old, 1), x, 0);
3126       if (op0 != NULL || op1 != NULL)
3127         {
3128           if (op0 == const1_rtx)
3129             return op1 ? op1 : gen_rtx_AND (0, XEXP (old, 1), x);
3130           if (op1 == const1_rtx)
3131             return op0 ? op0 : gen_rtx_AND (0, XEXP (old, 0), x);
3132           if (op0 == const0_rtx || op1 == const0_rtx)
3133             return const0_rtx;
3134           if (op0 == NULL)
3135             op0 = gen_rtx_AND (0, XEXP (old, 0), x);
3136           else if (rtx_equal_p (x, op0))
3137             /* (x & A) & x ~ (x & A).  */
3138             return old;
3139           if (op1 == NULL)
3140             op1 = gen_rtx_AND (0, XEXP (old, 1), x);
3141           else if (rtx_equal_p (x, op1))
3142             /* (A & x) & x ~ (A & x).  */
3143             return old;
3144           return gen_rtx_AND (0, op0, op1);
3145         }
3146       if (! add)
3147         return NULL;
3148       return gen_rtx_AND (0, old, x);
3149
3150     case NOT:
3151       op0 = ior_reg_cond (XEXP (old, 0), not_reg_cond (x), 0);
3152       if (op0 != NULL)
3153         return not_reg_cond (op0);
3154       if (! add)
3155         return NULL;
3156       return gen_rtx_AND (0, old, x);
3157
3158     default:
3159       abort ();
3160     }
3161 }
3162
3163 /* Given a condition X, remove references to reg REGNO and return the
3164    new condition.  The removal will be done so that all conditions
3165    involving REGNO are considered to evaluate to false.  This function
3166    is used when the value of REGNO changes.  */
3167
3168 static rtx
3169 elim_reg_cond (rtx x, unsigned int regno)
3170 {
3171   rtx op0, op1;
3172
3173   if (GET_RTX_CLASS (GET_CODE (x)) == '<')
3174     {
3175       if (REGNO (XEXP (x, 0)) == regno)
3176         return const0_rtx;
3177       return x;
3178     }
3179
3180   switch (GET_CODE (x))
3181     {
3182     case AND:
3183       op0 = elim_reg_cond (XEXP (x, 0), regno);
3184       op1 = elim_reg_cond (XEXP (x, 1), regno);
3185       if (op0 == const0_rtx || op1 == const0_rtx)
3186         return const0_rtx;
3187       if (op0 == const1_rtx)
3188         return op1;
3189       if (op1 == const1_rtx)
3190         return op0;
3191       if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
3192         return x;
3193       return gen_rtx_AND (0, op0, op1);
3194
3195     case IOR:
3196       op0 = elim_reg_cond (XEXP (x, 0), regno);
3197       op1 = elim_reg_cond (XEXP (x, 1), regno);
3198       if (op0 == const1_rtx || op1 == const1_rtx)
3199         return const1_rtx;
3200       if (op0 == const0_rtx)
3201         return op1;
3202       if (op1 == const0_rtx)
3203         return op0;
3204       if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
3205         return x;
3206       return gen_rtx_IOR (0, op0, op1);
3207
3208     case NOT:
3209       op0 = elim_reg_cond (XEXP (x, 0), regno);
3210       if (op0 == const0_rtx)
3211         return const1_rtx;
3212       if (op0 == const1_rtx)
3213         return const0_rtx;
3214       if (op0 != XEXP (x, 0))
3215         return not_reg_cond (op0);
3216       return x;
3217
3218     default:
3219       abort ();
3220     }
3221 }
3222 #endif /* HAVE_conditional_execution */
3223 \f
3224 #ifdef AUTO_INC_DEC
3225
3226 /* Try to substitute the auto-inc expression INC as the address inside
3227    MEM which occurs in INSN.  Currently, the address of MEM is an expression
3228    involving INCR_REG, and INCR is the next use of INCR_REG; it is an insn
3229    that has a single set whose source is a PLUS of INCR_REG and something
3230    else.  */
3231
3232 static void
3233 attempt_auto_inc (struct propagate_block_info *pbi, rtx inc, rtx insn,
3234                   rtx mem, rtx incr, rtx incr_reg)
3235 {
3236   int regno = REGNO (incr_reg);
3237   rtx set = single_set (incr);
3238   rtx q = SET_DEST (set);
3239   rtx y = SET_SRC (set);
3240   int opnum = XEXP (y, 0) == incr_reg ? 0 : 1;
3241
3242   /* Make sure this reg appears only once in this insn.  */
3243   if (count_occurrences (PATTERN (insn), incr_reg, 1) != 1)
3244     return;
3245
3246   if (dead_or_set_p (incr, incr_reg)
3247       /* Mustn't autoinc an eliminable register.  */
3248       && (regno >= FIRST_PSEUDO_REGISTER
3249           || ! TEST_HARD_REG_BIT (elim_reg_set, regno)))
3250     {
3251       /* This is the simple case.  Try to make the auto-inc.  If
3252          we can't, we are done.  Otherwise, we will do any
3253          needed updates below.  */
3254       if (! validate_change (insn, &XEXP (mem, 0), inc, 0))
3255         return;
3256     }
3257   else if (GET_CODE (q) == REG
3258            /* PREV_INSN used here to check the semi-open interval
3259               [insn,incr).  */
3260            && ! reg_used_between_p (q,  PREV_INSN (insn), incr)
3261            /* We must also check for sets of q as q may be
3262               a call clobbered hard register and there may
3263               be a call between PREV_INSN (insn) and incr.  */
3264            && ! reg_set_between_p (q,  PREV_INSN (insn), incr))
3265     {
3266       /* We have *p followed sometime later by q = p+size.
3267          Both p and q must be live afterward,
3268          and q is not used between INSN and its assignment.
3269          Change it to q = p, ...*q..., q = q+size.
3270          Then fall into the usual case.  */
3271       rtx insns, temp;
3272
3273       start_sequence ();
3274       emit_move_insn (q, incr_reg);
3275       insns = get_insns ();
3276       end_sequence ();
3277
3278       /* If we can't make the auto-inc, or can't make the
3279          replacement into Y, exit.  There's no point in making
3280          the change below if we can't do the auto-inc and doing
3281          so is not correct in the pre-inc case.  */
3282
3283       XEXP (inc, 0) = q;
3284       validate_change (insn, &XEXP (mem, 0), inc, 1);
3285       validate_change (incr, &XEXP (y, opnum), q, 1);
3286       if (! apply_change_group ())
3287         return;
3288
3289       /* We now know we'll be doing this change, so emit the
3290          new insn(s) and do the updates.  */
3291       emit_insn_before (insns, insn);
3292
3293       if (pbi->bb->head == insn)
3294         pbi->bb->head = insns;
3295
3296       /* INCR will become a NOTE and INSN won't contain a
3297          use of INCR_REG.  If a use of INCR_REG was just placed in
3298          the insn before INSN, make that the next use.
3299          Otherwise, invalidate it.  */
3300       if (GET_CODE (PREV_INSN (insn)) == INSN
3301           && GET_CODE (PATTERN (PREV_INSN (insn))) == SET
3302           && SET_SRC (PATTERN (PREV_INSN (insn))) == incr_reg)
3303         pbi->reg_next_use[regno] = PREV_INSN (insn);
3304       else
3305         pbi->reg_next_use[regno] = 0;
3306
3307       incr_reg = q;
3308       regno = REGNO (q);
3309
3310       /* REGNO is now used in INCR which is below INSN, but
3311          it previously wasn't live here.  If we don't mark
3312          it as live, we'll put a REG_DEAD note for it
3313          on this insn, which is incorrect.  */
3314       SET_REGNO_REG_SET (pbi->reg_live, regno);
3315
3316       /* If there are any calls between INSN and INCR, show
3317          that REGNO now crosses them.  */
3318       for (temp = insn; temp != incr; temp = NEXT_INSN (temp))
3319         if (GET_CODE (temp) == CALL_INSN)
3320           REG_N_CALLS_CROSSED (regno)++;
3321
3322       /* Invalidate alias info for Q since we just changed its value.  */
3323       clear_reg_alias_info (q);
3324     }
3325   else
3326     return;
3327
3328   /* If we haven't returned, it means we were able to make the
3329      auto-inc, so update the status.  First, record that this insn
3330      has an implicit side effect.  */
3331
3332   REG_NOTES (insn) = alloc_EXPR_LIST (REG_INC, incr_reg, REG_NOTES (insn));
3333
3334   /* Modify the old increment-insn to simply copy
3335      the already-incremented value of our register.  */
3336   if (! validate_change (incr, &SET_SRC (set), incr_reg, 0))
3337     abort ();
3338
3339   /* If that makes it a no-op (copying the register into itself) delete
3340      it so it won't appear to be a "use" and a "set" of this
3341      register.  */
3342   if (REGNO (SET_DEST (set)) == REGNO (incr_reg))
3343     {
3344       /* If the original source was dead, it's dead now.  */
3345       rtx note;
3346
3347       while ((note = find_reg_note (incr, REG_DEAD, NULL_RTX)) != NULL_RTX)
3348         {
3349           remove_note (incr, note);
3350           if (XEXP (note, 0) != incr_reg)
3351             CLEAR_REGNO_REG_SET (pbi->reg_live, REGNO (XEXP (note, 0)));
3352         }
3353
3354       PUT_CODE (incr, NOTE);
3355       NOTE_LINE_NUMBER (incr) = NOTE_INSN_DELETED;
3356       NOTE_SOURCE_FILE (incr) = 0;
3357     }
3358
3359   if (regno >= FIRST_PSEUDO_REGISTER)
3360     {
3361       /* Count an extra reference to the reg.  When a reg is
3362          incremented, spilling it is worse, so we want to make
3363          that less likely.  */
3364       REG_FREQ (regno) += REG_FREQ_FROM_BB (pbi->bb);
3365
3366       /* Count the increment as a setting of the register,
3367          even though it isn't a SET in rtl.  */
3368       REG_N_SETS (regno)++;
3369     }
3370 }
3371
3372 /* X is a MEM found in INSN.  See if we can convert it into an auto-increment
3373    reference.  */
3374
3375 static void
3376 find_auto_inc (struct propagate_block_info *pbi, rtx x, rtx insn)
3377 {
3378   rtx addr = XEXP (x, 0);
3379   HOST_WIDE_INT offset = 0;
3380   rtx set, y, incr, inc_val;
3381   int regno;
3382   int size = GET_MODE_SIZE (GET_MODE (x));
3383
3384   if (GET_CODE (insn) == JUMP_INSN)
3385     return;
3386
3387   /* Here we detect use of an index register which might be good for
3388      postincrement, postdecrement, preincrement, or predecrement.  */
3389
3390   if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
3391     offset = INTVAL (XEXP (addr, 1)), addr = XEXP (addr, 0);
3392
3393   if (GET_CODE (addr) != REG)
3394     return;
3395
3396   regno = REGNO (addr);
3397
3398   /* Is the next use an increment that might make auto-increment? */
3399   incr = pbi->reg_next_use[regno];
3400   if (incr == 0 || BLOCK_NUM (incr) != BLOCK_NUM (insn))
3401     return;
3402   set = single_set (incr);
3403   if (set == 0 || GET_CODE (set) != SET)
3404     return;
3405   y = SET_SRC (set);
3406
3407   if (GET_CODE (y) != PLUS)
3408     return;
3409
3410   if (REG_P (XEXP (y, 0)) && REGNO (XEXP (y, 0)) == REGNO (addr))
3411     inc_val = XEXP (y, 1);
3412   else if (REG_P (XEXP (y, 1)) && REGNO (XEXP (y, 1)) == REGNO (addr))
3413     inc_val = XEXP (y, 0);
3414   else
3415     return;
3416
3417   if (GET_CODE (inc_val) == CONST_INT)
3418     {
3419       if (HAVE_POST_INCREMENT
3420           && (INTVAL (inc_val) == size && offset == 0))
3421         attempt_auto_inc (pbi, gen_rtx_POST_INC (Pmode, addr), insn, x,
3422                           incr, addr);
3423       else if (HAVE_POST_DECREMENT
3424                && (INTVAL (inc_val) == -size && offset == 0))
3425         attempt_auto_inc (pbi, gen_rtx_POST_DEC (Pmode, addr), insn, x,
3426                           incr, addr);
3427       else if (HAVE_PRE_INCREMENT
3428                && (INTVAL (inc_val) == size && offset == size))
3429         attempt_auto_inc (pbi, gen_rtx_PRE_INC (Pmode, addr), insn, x,
3430                           incr, addr);
3431       else if (HAVE_PRE_DECREMENT
3432                && (INTVAL (inc_val) == -size && offset == -size))
3433         attempt_auto_inc (pbi, gen_rtx_PRE_DEC (Pmode, addr), insn, x,
3434                           incr, addr);
3435       else if (HAVE_POST_MODIFY_DISP && offset == 0)
3436         attempt_auto_inc (pbi, gen_rtx_POST_MODIFY (Pmode, addr,
3437                                                     gen_rtx_PLUS (Pmode,
3438                                                                   addr,
3439                                                                   inc_val)),
3440                           insn, x, incr, addr);
3441       else if (HAVE_PRE_MODIFY_DISP && offset == INTVAL (inc_val))
3442         attempt_auto_inc (pbi, gen_rtx_PRE_MODIFY (Pmode, addr,
3443                                                     gen_rtx_PLUS (Pmode,
3444                                                                   addr,
3445                                                                   inc_val)),
3446                           insn, x, incr, addr);
3447     }
3448   else if (GET_CODE (inc_val) == REG
3449            && ! reg_set_between_p (inc_val, PREV_INSN (insn),
3450                                    NEXT_INSN (incr)))
3451
3452     {
3453       if (HAVE_POST_MODIFY_REG && offset == 0)
3454         attempt_auto_inc (pbi, gen_rtx_POST_MODIFY (Pmode, addr,
3455                                                     gen_rtx_PLUS (Pmode,
3456                                                                   addr,
3457                                                                   inc_val)),
3458                           insn, x, incr, addr);
3459     }
3460 }
3461
3462 #endif /* AUTO_INC_DEC */
3463 \f
3464 static void
3465 mark_used_reg (struct propagate_block_info *pbi, rtx reg,
3466                rtx cond ATTRIBUTE_UNUSED, rtx insn)
3467 {
3468   unsigned int regno_first, regno_last, i;
3469   int some_was_live, some_was_dead, some_not_set;
3470
3471   regno_last = regno_first = REGNO (reg);
3472   if (regno_first < FIRST_PSEUDO_REGISTER)
3473     regno_last += HARD_REGNO_NREGS (regno_first, GET_MODE (reg)) - 1;
3474
3475   /* Find out if any of this register is live after this instruction.  */
3476   some_was_live = some_was_dead = 0;
3477   for (i = regno_first; i <= regno_last; ++i)
3478     {
3479       int needed_regno = REGNO_REG_SET_P (pbi->reg_live, i);
3480       some_was_live |= needed_regno;
3481       some_was_dead |= ! needed_regno;
3482     }
3483
3484   /* Find out if any of the register was set this insn.  */
3485   some_not_set = 0;
3486   for (i = regno_first; i <= regno_last; ++i)
3487     some_not_set |= ! REGNO_REG_SET_P (pbi->new_set, i);
3488
3489   if (pbi->flags & (PROP_LOG_LINKS | PROP_AUTOINC))
3490     {
3491       /* Record where each reg is used, so when the reg is set we know
3492          the next insn that uses it.  */
3493       pbi->reg_next_use[regno_first] = insn;
3494     }
3495
3496   if (pbi->flags & PROP_REG_INFO)
3497     {
3498       if (regno_first < FIRST_PSEUDO_REGISTER)
3499         {
3500           /* If this is a register we are going to try to eliminate,
3501              don't mark it live here.  If we are successful in
3502              eliminating it, it need not be live unless it is used for
3503              pseudos, in which case it will have been set live when it
3504              was allocated to the pseudos.  If the register will not
3505              be eliminated, reload will set it live at that point.
3506
3507              Otherwise, record that this function uses this register.  */
3508           /* ??? The PPC backend tries to "eliminate" on the pic
3509              register to itself.  This should be fixed.  In the mean
3510              time, hack around it.  */
3511
3512           if (! (TEST_HARD_REG_BIT (elim_reg_set, regno_first)
3513                  && (regno_first == FRAME_POINTER_REGNUM
3514                      || regno_first == ARG_POINTER_REGNUM)))
3515             for (i = regno_first; i <= regno_last; ++i)
3516               regs_ever_live[i] = 1;
3517         }
3518       else
3519         {
3520           /* Keep track of which basic block each reg appears in.  */
3521
3522           int blocknum = pbi->bb->index;
3523           if (REG_BASIC_BLOCK (regno_first) == REG_BLOCK_UNKNOWN)
3524             REG_BASIC_BLOCK (regno_first) = blocknum;
3525           else if (REG_BASIC_BLOCK (regno_first) != blocknum)
3526             REG_BASIC_BLOCK (regno_first) = REG_BLOCK_GLOBAL;
3527
3528           /* Count (weighted) number of uses of each reg.  */
3529           REG_FREQ (regno_first) += REG_FREQ_FROM_BB (pbi->bb);
3530           REG_N_REFS (regno_first)++;
3531         }
3532     }
3533
3534   /* Record and count the insns in which a reg dies.  If it is used in
3535      this insn and was dead below the insn then it dies in this insn.
3536      If it was set in this insn, we do not make a REG_DEAD note;
3537      likewise if we already made such a note.  */
3538   if ((pbi->flags & (PROP_DEATH_NOTES | PROP_REG_INFO))
3539       && some_was_dead
3540       && some_not_set)
3541     {
3542       /* Check for the case where the register dying partially
3543          overlaps the register set by this insn.  */
3544       if (regno_first != regno_last)
3545         for (i = regno_first; i <= regno_last; ++i)
3546           some_was_live |= REGNO_REG_SET_P (pbi->new_set, i);
3547
3548       /* If none of the words in X is needed, make a REG_DEAD note.
3549          Otherwise, we must make partial REG_DEAD notes.  */
3550       if (! some_was_live)
3551         {
3552           if ((pbi->flags & PROP_DEATH_NOTES)
3553               && ! find_regno_note (insn, REG_DEAD, regno_first))
3554             REG_NOTES (insn)
3555               = alloc_EXPR_LIST (REG_DEAD, reg, REG_NOTES (insn));
3556
3557           if (pbi->flags & PROP_REG_INFO)
3558             REG_N_DEATHS (regno_first)++;
3559         }
3560       else
3561         {
3562           /* Don't make a REG_DEAD note for a part of a register
3563              that is set in the insn.  */
3564           for (i = regno_first; i <= regno_last; ++i)
3565             if (! REGNO_REG_SET_P (pbi->reg_live, i)
3566                 && ! dead_or_set_regno_p (insn, i))
3567               REG_NOTES (insn)
3568                 = alloc_EXPR_LIST (REG_DEAD,
3569                                    regno_reg_rtx[i],
3570                                    REG_NOTES (insn));
3571         }
3572     }
3573
3574   /* Mark the register as being live.  */
3575   for (i = regno_first; i <= regno_last; ++i)
3576     {
3577 #ifdef HAVE_conditional_execution
3578       int this_was_live = REGNO_REG_SET_P (pbi->reg_live, i);
3579 #endif
3580
3581       SET_REGNO_REG_SET (pbi->reg_live, i);
3582
3583 #ifdef HAVE_conditional_execution
3584       /* If this is a conditional use, record that fact.  If it is later
3585          conditionally set, we'll know to kill the register.  */
3586       if (cond != NULL_RTX)
3587         {
3588           splay_tree_node node;
3589           struct reg_cond_life_info *rcli;
3590           rtx ncond;
3591
3592           if (this_was_live)
3593             {
3594               node = splay_tree_lookup (pbi->reg_cond_dead, i);
3595               if (node == NULL)
3596                 {
3597                   /* The register was unconditionally live previously.
3598                      No need to do anything.  */
3599                 }
3600               else
3601                 {
3602                   /* The register was conditionally live previously.
3603                      Subtract the new life cond from the old death cond.  */
3604                   rcli = (struct reg_cond_life_info *) node->value;
3605                   ncond = rcli->condition;
3606                   ncond = and_reg_cond (ncond, not_reg_cond (cond), 1);
3607
3608                   /* If the register is now unconditionally live,
3609                      remove the entry in the splay_tree.  */
3610                   if (ncond == const0_rtx)
3611                     splay_tree_remove (pbi->reg_cond_dead, i);
3612                   else
3613                     {
3614                       rcli->condition = ncond;
3615                       SET_REGNO_REG_SET (pbi->reg_cond_reg,
3616                                          REGNO (XEXP (cond, 0)));
3617                     }
3618                 }
3619             }
3620           else
3621             {
3622               /* The register was not previously live at all.  Record
3623                  the condition under which it is still dead.  */
3624               rcli = xmalloc (sizeof (*rcli));
3625               rcli->condition = not_reg_cond (cond);
3626               rcli->stores = const0_rtx;
3627               rcli->orig_condition = const0_rtx;
3628               splay_tree_insert (pbi->reg_cond_dead, i,
3629                                  (splay_tree_value) rcli);
3630
3631               SET_REGNO_REG_SET (pbi->reg_cond_reg, REGNO (XEXP (cond, 0)));
3632             }
3633         }
3634       else if (this_was_live)
3635         {
3636           /* The register may have been conditionally live previously, but
3637              is now unconditionally live.  Remove it from the conditionally
3638              dead list, so that a conditional set won't cause us to think
3639              it dead.  */
3640           splay_tree_remove (pbi->reg_cond_dead, i);
3641         }
3642 #endif
3643     }
3644 }
3645
3646 /* Scan expression X and store a 1-bit in NEW_LIVE for each reg it uses.
3647    This is done assuming the registers needed from X are those that
3648    have 1-bits in PBI->REG_LIVE.
3649
3650    INSN is the containing instruction.  If INSN is dead, this function
3651    is not called.  */
3652
3653 static void
3654 mark_used_regs (struct propagate_block_info *pbi, rtx x, rtx cond, rtx insn)
3655 {
3656   RTX_CODE code;
3657   int regno;
3658   int flags = pbi->flags;
3659
3660  retry:
3661   if (!x)
3662     return;
3663   code = GET_CODE (x);
3664   switch (code)
3665     {
3666     case LABEL_REF:
3667     case SYMBOL_REF:
3668     case CONST_INT:
3669     case CONST:
3670     case CONST_DOUBLE:
3671     case CONST_VECTOR:
3672     case PC:
3673     case ADDR_VEC:
3674     case ADDR_DIFF_VEC:
3675       return;
3676
3677 #ifdef HAVE_cc0
3678     case CC0:
3679       pbi->cc0_live = 1;
3680       return;
3681 #endif
3682
3683     case CLOBBER:
3684       /* If we are clobbering a MEM, mark any registers inside the address
3685          as being used.  */
3686       if (GET_CODE (XEXP (x, 0)) == MEM)
3687         mark_used_regs (pbi, XEXP (XEXP (x, 0), 0), cond, insn);
3688       return;
3689
3690     case MEM:
3691       /* Don't bother watching stores to mems if this is not the
3692          final pass.  We'll not be deleting dead stores this round.  */
3693       if (optimize && (flags & PROP_SCAN_DEAD_STORES))
3694         {
3695           /* Invalidate the data for the last MEM stored, but only if MEM is
3696              something that can be stored into.  */
3697           if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
3698               && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
3699             /* Needn't clear the memory set list.  */
3700             ;
3701           else
3702             {
3703               rtx temp = pbi->mem_set_list;
3704               rtx prev = NULL_RTX;
3705               rtx next;
3706
3707               while (temp)
3708                 {
3709                   next = XEXP (temp, 1);
3710                   if (unchanging_anti_dependence (XEXP (temp, 0), x))
3711                     {
3712                       /* Splice temp out of the list.  */
3713                       if (prev)
3714                         XEXP (prev, 1) = next;
3715                       else
3716                         pbi->mem_set_list = next;
3717                       free_EXPR_LIST_node (temp);
3718                       pbi->mem_set_list_len--;
3719                     }
3720                   else
3721                     prev = temp;
3722                   temp = next;
3723                 }
3724             }
3725
3726           /* If the memory reference had embedded side effects (autoincrement
3727              address modes.  Then we may need to kill some entries on the
3728              memory set list.  */
3729           if (insn)
3730             for_each_rtx (&PATTERN (insn), invalidate_mems_from_autoinc, pbi);
3731         }
3732
3733 #ifdef AUTO_INC_DEC
3734       if (flags & PROP_AUTOINC)
3735         find_auto_inc (pbi, x, insn);
3736 #endif
3737       break;
3738
3739     case SUBREG:
3740 #ifdef CANNOT_CHANGE_MODE_CLASS
3741       if ((flags & PROP_REG_INFO)
3742           && GET_CODE (SUBREG_REG (x)) == REG
3743           && REGNO (SUBREG_REG (x)) >= FIRST_PSEUDO_REGISTER)
3744         bitmap_set_bit (&subregs_of_mode, REGNO (SUBREG_REG (x))
3745                                           * MAX_MACHINE_MODE
3746                                           + GET_MODE (x));
3747 #endif
3748
3749       /* While we're here, optimize this case.  */
3750       x = SUBREG_REG (x);
3751       if (GET_CODE (x) != REG)
3752         goto retry;
3753       /* Fall through.  */
3754
3755     case REG:
3756       /* See a register other than being set => mark it as needed.  */
3757       mark_used_reg (pbi, x, cond, insn);
3758       return;
3759
3760     case SET:
3761       {
3762         rtx testreg = SET_DEST (x);
3763         int mark_dest = 0;
3764
3765         /* If storing into MEM, don't show it as being used.  But do
3766            show the address as being used.  */
3767         if (GET_CODE (testreg) == MEM)
3768           {
3769 #ifdef AUTO_INC_DEC
3770             if (flags & PROP_AUTOINC)
3771               find_auto_inc (pbi, testreg, insn);
3772 #endif
3773             mark_used_regs (pbi, XEXP (testreg, 0), cond, insn);
3774             mark_used_regs (pbi, SET_SRC (x), cond, insn);
3775             return;
3776           }
3777
3778         /* Storing in STRICT_LOW_PART is like storing in a reg
3779            in that this SET might be dead, so ignore it in TESTREG.
3780            but in some other ways it is like using the reg.
3781
3782            Storing in a SUBREG or a bit field is like storing the entire
3783            register in that if the register's value is not used
3784            then this SET is not needed.  */
3785         while (GET_CODE (testreg) == STRICT_LOW_PART
3786                || GET_CODE (testreg) == ZERO_EXTRACT
3787                || GET_CODE (testreg) == SIGN_EXTRACT
3788                || GET_CODE (testreg) == SUBREG)
3789           {
3790 #ifdef CANNOT_CHANGE_MODE_CLASS
3791             if ((flags & PROP_REG_INFO)
3792                 && GET_CODE (testreg) == SUBREG
3793                 && GET_CODE (SUBREG_REG (testreg)) == REG
3794                 && REGNO (SUBREG_REG (testreg)) >= FIRST_PSEUDO_REGISTER)
3795               bitmap_set_bit (&subregs_of_mode, REGNO (SUBREG_REG (testreg))
3796                                                 * MAX_MACHINE_MODE
3797                                                 + GET_MODE (testreg));
3798 #endif
3799
3800             /* Modifying a single register in an alternate mode
3801                does not use any of the old value.  But these other
3802                ways of storing in a register do use the old value.  */
3803             if (GET_CODE (testreg) == SUBREG
3804                 && !((REG_BYTES (SUBREG_REG (testreg))
3805                       + UNITS_PER_WORD - 1) / UNITS_PER_WORD
3806                      > (REG_BYTES (testreg)
3807                         + UNITS_PER_WORD - 1) / UNITS_PER_WORD))
3808               ;
3809             else
3810               mark_dest = 1;
3811
3812             testreg = XEXP (testreg, 0);
3813           }
3814
3815         /* If this is a store into a register or group of registers,
3816            recursively scan the value being stored.  */
3817
3818         if ((GET_CODE (testreg) == PARALLEL
3819              && GET_MODE (testreg) == BLKmode)
3820             || (GET_CODE (testreg) == REG
3821                 && (regno = REGNO (testreg),
3822                     ! (regno == FRAME_POINTER_REGNUM
3823                        && (! reload_completed || frame_pointer_needed)))
3824 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3825                 && ! (regno == HARD_FRAME_POINTER_REGNUM
3826                       && (! reload_completed || frame_pointer_needed))
3827 #endif
3828 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3829                 && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
3830 #endif
3831                 ))
3832           {
3833             if (mark_dest)
3834               mark_used_regs (pbi, SET_DEST (x), cond, insn);
3835             mark_used_regs (pbi, SET_SRC (x), cond, insn);
3836             return;
3837           }
3838       }
3839       break;
3840
3841     case ASM_OPERANDS:
3842     case UNSPEC_VOLATILE:
3843     case TRAP_IF:
3844     case ASM_INPUT:
3845       {
3846         /* Traditional and volatile asm instructions must be considered to use
3847            and clobber all hard registers, all pseudo-registers and all of
3848            memory.  So must TRAP_IF and UNSPEC_VOLATILE operations.
3849
3850            Consider for instance a volatile asm that changes the fpu rounding
3851            mode.  An insn should not be moved across this even if it only uses
3852            pseudo-regs because it might give an incorrectly rounded result.
3853
3854            ?!? Unfortunately, marking all hard registers as live causes massive
3855            problems for the register allocator and marking all pseudos as live
3856            creates mountains of uninitialized variable warnings.
3857
3858            So for now, just clear the memory set list and mark any regs
3859            we can find in ASM_OPERANDS as used.  */
3860         if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
3861           {
3862             free_EXPR_LIST_list (&pbi->mem_set_list);
3863             pbi->mem_set_list_len = 0;
3864           }
3865
3866         /* For all ASM_OPERANDS, we must traverse the vector of input operands.
3867            We can not just fall through here since then we would be confused
3868            by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
3869            traditional asms unlike their normal usage.  */
3870         if (code == ASM_OPERANDS)
3871           {
3872             int j;
3873
3874             for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
3875               mark_used_regs (pbi, ASM_OPERANDS_INPUT (x, j), cond, insn);
3876           }
3877         break;
3878       }
3879
3880     case COND_EXEC:
3881       if (cond != NULL_RTX)
3882         abort ();
3883
3884       mark_used_regs (pbi, COND_EXEC_TEST (x), NULL_RTX, insn);
3885
3886       cond = COND_EXEC_TEST (x);
3887       x = COND_EXEC_CODE (x);
3888       goto retry;
3889
3890     default:
3891       break;
3892     }
3893
3894   /* Recursively scan the operands of this expression.  */
3895
3896   {
3897     const char * const fmt = GET_RTX_FORMAT (code);
3898     int i;
3899
3900     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3901       {
3902         if (fmt[i] == 'e')
3903           {
3904             /* Tail recursive case: save a function call level.  */
3905             if (i == 0)
3906               {
3907                 x = XEXP (x, 0);
3908                 goto retry;
3909               }
3910             mark_used_regs (pbi, XEXP (x, i), cond, insn);
3911           }
3912         else if (fmt[i] == 'E')
3913           {
3914             int j;
3915             for (j = 0; j < XVECLEN (x, i); j++)
3916               mark_used_regs (pbi, XVECEXP (x, i, j), cond, insn);
3917           }
3918       }
3919   }
3920 }
3921 \f
3922 #ifdef AUTO_INC_DEC
3923
3924 static int
3925 try_pre_increment_1 (struct propagate_block_info *pbi, rtx insn)
3926 {
3927   /* Find the next use of this reg.  If in same basic block,
3928      make it do pre-increment or pre-decrement if appropriate.  */
3929   rtx x = single_set (insn);
3930   HOST_WIDE_INT amount = ((GET_CODE (SET_SRC (x)) == PLUS ? 1 : -1)
3931                           * INTVAL (XEXP (SET_SRC (x), 1)));
3932   int regno = REGNO (SET_DEST (x));
3933   rtx y = pbi->reg_next_use[regno];
3934   if (y != 0
3935       && SET_DEST (x) != stack_pointer_rtx
3936       && BLOCK_NUM (y) == BLOCK_NUM (insn)
3937       /* Don't do this if the reg dies, or gets set in y; a standard addressing
3938          mode would be better.  */
3939       && ! dead_or_set_p (y, SET_DEST (x))
3940       && try_pre_increment (y, SET_DEST (x), amount))
3941     {
3942       /* We have found a suitable auto-increment and already changed
3943          insn Y to do it.  So flush this increment instruction.  */
3944       propagate_block_delete_insn (insn);
3945
3946       /* Count a reference to this reg for the increment insn we are
3947          deleting.  When a reg is incremented, spilling it is worse,
3948          so we want to make that less likely.  */
3949       if (regno >= FIRST_PSEUDO_REGISTER)
3950         {
3951           REG_FREQ (regno) += REG_FREQ_FROM_BB (pbi->bb);
3952           REG_N_SETS (regno)++;
3953         }
3954
3955       /* Flush any remembered memories depending on the value of
3956          the incremented register.  */
3957       invalidate_mems_from_set (pbi, SET_DEST (x));
3958
3959       return 1;
3960     }
3961   return 0;
3962 }
3963
3964 /* Try to change INSN so that it does pre-increment or pre-decrement
3965    addressing on register REG in order to add AMOUNT to REG.
3966    AMOUNT is negative for pre-decrement.
3967    Returns 1 if the change could be made.
3968    This checks all about the validity of the result of modifying INSN.  */
3969
3970 static int
3971 try_pre_increment (rtx insn, rtx reg, HOST_WIDE_INT amount)
3972 {
3973   rtx use;
3974
3975   /* Nonzero if we can try to make a pre-increment or pre-decrement.
3976      For example, addl $4,r1; movl (r1),... can become movl +(r1),...  */
3977   int pre_ok = 0;
3978   /* Nonzero if we can try to make a post-increment or post-decrement.
3979      For example, addl $4,r1; movl -4(r1),... can become movl (r1)+,...
3980      It is possible for both PRE_OK and POST_OK to be nonzero if the machine
3981      supports both pre-inc and post-inc, or both pre-dec and post-dec.  */
3982   int post_ok = 0;
3983
3984   /* Nonzero if the opportunity actually requires post-inc or post-dec.  */
3985   int do_post = 0;
3986
3987   /* From the sign of increment, see which possibilities are conceivable
3988      on this target machine.  */
3989   if (HAVE_PRE_INCREMENT && amount > 0)
3990     pre_ok = 1;
3991   if (HAVE_POST_INCREMENT && amount > 0)
3992     post_ok = 1;
3993
3994   if (HAVE_PRE_DECREMENT && amount < 0)
3995     pre_ok = 1;
3996   if (HAVE_POST_DECREMENT && amount < 0)
3997     post_ok = 1;
3998
3999   if (! (pre_ok || post_ok))
4000     return 0;
4001
4002   /* It is not safe to add a side effect to a jump insn
4003      because if the incremented register is spilled and must be reloaded
4004      there would be no way to store the incremented value back in memory.  */
4005
4006   if (GET_CODE (insn) == JUMP_INSN)
4007     return 0;
4008
4009   use = 0;
4010   if (pre_ok)
4011     use = find_use_as_address (PATTERN (insn), reg, 0);
4012   if (post_ok && (use == 0 || use == (rtx) (size_t) 1))
4013     {
4014       use = find_use_as_address (PATTERN (insn), reg, -amount);
4015       do_post = 1;
4016     }
4017
4018   if (use == 0 || use == (rtx) (size_t) 1)
4019     return 0;
4020
4021   if (GET_MODE_SIZE (GET_MODE (use)) != (amount > 0 ? amount : - amount))
4022     return 0;
4023
4024   /* See if this combination of instruction and addressing mode exists.  */
4025   if (! validate_change (insn, &XEXP (use, 0),
4026                          gen_rtx_fmt_e (amount > 0
4027                                         ? (do_post ? POST_INC : PRE_INC)
4028                                         : (do_post ? POST_DEC : PRE_DEC),
4029                                         Pmode, reg), 0))
4030     return 0;
4031
4032   /* Record that this insn now has an implicit side effect on X.  */
4033   REG_NOTES (insn) = alloc_EXPR_LIST (REG_INC, reg, REG_NOTES (insn));
4034   return 1;
4035 }
4036
4037 #endif /* AUTO_INC_DEC */
4038 \f
4039 /* Find the place in the rtx X where REG is used as a memory address.
4040    Return the MEM rtx that so uses it.
4041    If PLUSCONST is nonzero, search instead for a memory address equivalent to
4042    (plus REG (const_int PLUSCONST)).
4043
4044    If such an address does not appear, return 0.
4045    If REG appears more than once, or is used other than in such an address,
4046    return (rtx) 1.  */
4047
4048 rtx
4049 find_use_as_address (rtx x, rtx reg, HOST_WIDE_INT plusconst)
4050 {
4051   enum rtx_code code = GET_CODE (x);
4052   const char * const fmt = GET_RTX_FORMAT (code);
4053   int i;
4054   rtx value = 0;
4055   rtx tem;
4056
4057   if (code == MEM && XEXP (x, 0) == reg && plusconst == 0)
4058     return x;
4059
4060   if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
4061       && XEXP (XEXP (x, 0), 0) == reg
4062       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4063       && INTVAL (XEXP (XEXP (x, 0), 1)) == plusconst)
4064     return x;
4065
4066   if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
4067     {
4068       /* If REG occurs inside a MEM used in a bit-field reference,
4069          that is unacceptable.  */
4070       if (find_use_as_address (XEXP (x, 0), reg, 0) != 0)
4071         return (rtx) (size_t) 1;
4072     }
4073
4074   if (x == reg)
4075     return (rtx) (size_t) 1;
4076
4077   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4078     {
4079       if (fmt[i] == 'e')
4080         {
4081           tem = find_use_as_address (XEXP (x, i), reg, plusconst);
4082           if (value == 0)
4083             value = tem;
4084           else if (tem != 0)
4085             return (rtx) (size_t) 1;
4086         }
4087       else if (fmt[i] == 'E')
4088         {
4089           int j;
4090           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4091             {
4092               tem = find_use_as_address (XVECEXP (x, i, j), reg, plusconst);
4093               if (value == 0)
4094                 value = tem;
4095               else if (tem != 0)
4096                 return (rtx) (size_t) 1;
4097             }
4098         }
4099     }
4100
4101   return value;
4102 }
4103 \f
4104 /* Write information about registers and basic blocks into FILE.
4105    This is part of making a debugging dump.  */
4106
4107 void
4108 dump_regset (regset r, FILE *outf)
4109 {
4110   int i;
4111   if (r == NULL)
4112     {
4113       fputs (" (nil)", outf);
4114       return;
4115     }
4116
4117   EXECUTE_IF_SET_IN_REG_SET (r, 0, i,
4118     {
4119       fprintf (outf, " %d", i);
4120       if (i < FIRST_PSEUDO_REGISTER)
4121         fprintf (outf, " [%s]",
4122                  reg_names[i]);
4123     });
4124 }
4125
4126 /* Print a human-readable representation of R on the standard error
4127    stream.  This function is designed to be used from within the
4128    debugger.  */
4129
4130 void
4131 debug_regset (regset r)
4132 {
4133   dump_regset (r, stderr);
4134   putc ('\n', stderr);
4135 }
4136
4137 /* Recompute register set/reference counts immediately prior to register
4138    allocation.
4139
4140    This avoids problems with set/reference counts changing to/from values
4141    which have special meanings to the register allocators.
4142
4143    Additionally, the reference counts are the primary component used by the
4144    register allocators to prioritize pseudos for allocation to hard regs.
4145    More accurate reference counts generally lead to better register allocation.
4146
4147    F is the first insn to be scanned.
4148
4149    LOOP_STEP denotes how much loop_depth should be incremented per
4150    loop nesting level in order to increase the ref count more for
4151    references in a loop.
4152
4153    It might be worthwhile to update REG_LIVE_LENGTH, REG_BASIC_BLOCK and
4154    possibly other information which is used by the register allocators.  */
4155
4156 void
4157 recompute_reg_usage (rtx f ATTRIBUTE_UNUSED, int loop_step ATTRIBUTE_UNUSED)
4158 {
4159   allocate_reg_life_data ();
4160   update_life_info (NULL, UPDATE_LIFE_LOCAL, PROP_REG_INFO);
4161 }
4162
4163 /* Optionally removes all the REG_DEAD and REG_UNUSED notes from a set of
4164    blocks.  If BLOCKS is NULL, assume the universal set.  Returns a count
4165    of the number of registers that died.  */
4166
4167 int
4168 count_or_remove_death_notes (sbitmap blocks, int kill)
4169 {
4170   int count = 0;
4171   basic_block bb;
4172
4173   FOR_EACH_BB_REVERSE (bb)
4174     {
4175       rtx insn;
4176
4177       if (blocks && ! TEST_BIT (blocks, bb->index))
4178         continue;
4179
4180       for (insn = bb->head;; insn = NEXT_INSN (insn))
4181         {
4182           if (INSN_P (insn))
4183             {
4184               rtx *pprev = &REG_NOTES (insn);
4185               rtx link = *pprev;
4186
4187               while (link)
4188                 {
4189                   switch (REG_NOTE_KIND (link))
4190                     {
4191                     case REG_DEAD:
4192                       if (GET_CODE (XEXP (link, 0)) == REG)
4193                         {
4194                           rtx reg = XEXP (link, 0);
4195                           int n;
4196
4197                           if (REGNO (reg) >= FIRST_PSEUDO_REGISTER)
4198                             n = 1;
4199                           else
4200                             n = HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg));
4201                           count += n;
4202                         }
4203                       /* Fall through.  */
4204
4205                     case REG_UNUSED:
4206                       if (kill)
4207                         {
4208                           rtx next = XEXP (link, 1);
4209                           free_EXPR_LIST_node (link);
4210                           *pprev = link = next;
4211                           break;
4212                         }
4213                       /* Fall through.  */
4214
4215                     default:
4216                       pprev = &XEXP (link, 1);
4217                       link = *pprev;
4218                       break;
4219                     }
4220                 }
4221             }
4222
4223           if (insn == bb->end)
4224             break;
4225         }
4226     }
4227
4228   return count;
4229 }
4230 /* Clear LOG_LINKS fields of insns in a selected blocks or whole chain
4231    if blocks is NULL.  */
4232
4233 static void
4234 clear_log_links (sbitmap blocks)
4235 {
4236   rtx insn;
4237   int i;
4238
4239   if (!blocks)
4240     {
4241       for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4242         if (INSN_P (insn))
4243           free_INSN_LIST_list (&LOG_LINKS (insn));
4244     }
4245   else
4246     EXECUTE_IF_SET_IN_SBITMAP (blocks, 0, i,
4247       {
4248         basic_block bb = BASIC_BLOCK (i);
4249
4250         for (insn = bb->head; insn != NEXT_INSN (bb->end);
4251              insn = NEXT_INSN (insn))
4252           if (INSN_P (insn))
4253             free_INSN_LIST_list (&LOG_LINKS (insn));
4254       });
4255 }
4256
4257 /* Given a register bitmap, turn on the bits in a HARD_REG_SET that
4258    correspond to the hard registers, if any, set in that map.  This
4259    could be done far more efficiently by having all sorts of special-cases
4260    with moving single words, but probably isn't worth the trouble.  */
4261
4262 void
4263 reg_set_to_hard_reg_set (HARD_REG_SET *to, bitmap from)
4264 {
4265   int i;
4266
4267   EXECUTE_IF_SET_IN_BITMAP
4268     (from, 0, i,
4269      {
4270        if (i >= FIRST_PSEUDO_REGISTER)
4271          return;
4272        SET_HARD_REG_BIT (*to, i);
4273      });
4274 }