OSDN Git Service

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