OSDN Git Service

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