OSDN Git Service

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