xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/cprop.c (revision d909946ca08dceb44d7d0f22ec9488679695d976)
1 /* Global constant/copy propagation for RTL.
2    Copyright (C) 1997-2013 Free Software Foundation, Inc.
3 
4 This file is part of GCC.
5 
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10 
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19 
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "diagnostic-core.h"
25 #include "toplev.h"
26 
27 #include "rtl.h"
28 #include "tree.h"
29 #include "tm_p.h"
30 #include "regs.h"
31 #include "hard-reg-set.h"
32 #include "flags.h"
33 #include "insn-config.h"
34 #include "recog.h"
35 #include "basic-block.h"
36 #include "function.h"
37 #include "expr.h"
38 #include "except.h"
39 #include "params.h"
40 #include "cselib.h"
41 #include "intl.h"
42 #include "obstack.h"
43 #include "tree-pass.h"
44 #include "hashtab.h"
45 #include "df.h"
46 #include "dbgcnt.h"
47 #include "target.h"
48 #include "cfgloop.h"
49 
50 
51 /* An obstack for our working variables.  */
52 static struct obstack cprop_obstack;
53 
54 /* Occurrence of an expression.
55    There is one per basic block.  If a pattern appears more than once the
56    last appearance is used.  */
57 
58 struct occr
59 {
60   /* Next occurrence of this expression.  */
61   struct occr *next;
62   /* The insn that computes the expression.  */
63   rtx insn;
64 };
65 
66 typedef struct occr *occr_t;
67 
68 /* Hash table entry for assignment expressions.  */
69 
70 struct expr
71 {
72   /* The expression (DEST := SRC).  */
73   rtx dest;
74   rtx src;
75 
76   /* Index in the available expression bitmaps.  */
77   int bitmap_index;
78   /* Next entry with the same hash.  */
79   struct expr *next_same_hash;
80   /* List of available occurrence in basic blocks in the function.
81      An "available occurrence" is one that is the last occurrence in the
82      basic block and whose operands are not modified by following statements
83      in the basic block [including this insn].  */
84   struct occr *avail_occr;
85 };
86 
87 /* Hash table for copy propagation expressions.
88    Each hash table is an array of buckets.
89    ??? It is known that if it were an array of entries, structure elements
90    `next_same_hash' and `bitmap_index' wouldn't be necessary.  However, it is
91    not clear whether in the final analysis a sufficient amount of memory would
92    be saved as the size of the available expression bitmaps would be larger
93    [one could build a mapping table without holes afterwards though].
94    Someday I'll perform the computation and figure it out.  */
95 
96 struct hash_table_d
97 {
98   /* The table itself.
99      This is an array of `set_hash_table_size' elements.  */
100   struct expr **table;
101 
102   /* Size of the hash table, in elements.  */
103   unsigned int size;
104 
105   /* Number of hash table elements.  */
106   unsigned int n_elems;
107 };
108 
109 /* Copy propagation hash table.  */
110 static struct hash_table_d set_hash_table;
111 
112 /* Array of implicit set patterns indexed by basic block index.  */
113 static rtx *implicit_sets;
114 
115 /* Array of indexes of expressions for implicit set patterns indexed by basic
116    block index.  In other words, implicit_set_indexes[i] is the bitmap_index
117    of the expression whose RTX is implicit_sets[i].  */
118 static int *implicit_set_indexes;
119 
120 /* Bitmap containing one bit for each register in the program.
121    Used when performing GCSE to track which registers have been set since
122    the start or end of the basic block while traversing that block.  */
123 static regset reg_set_bitmap;
124 
125 /* Various variables for statistics gathering.  */
126 
127 /* Memory used in a pass.
128    This isn't intended to be absolutely precise.  Its intent is only
129    to keep an eye on memory usage.  */
130 static int bytes_used;
131 
132 /* Number of local constants propagated.  */
133 static int local_const_prop_count;
134 /* Number of local copies propagated.  */
135 static int local_copy_prop_count;
136 /* Number of global constants propagated.  */
137 static int global_const_prop_count;
138 /* Number of global copies propagated.  */
139 static int global_copy_prop_count;
140 
141 #define GOBNEW(T)		((T *) cprop_alloc (sizeof (T)))
142 #define GOBNEWVAR(T, S)		((T *) cprop_alloc ((S)))
143 
144 /* Cover function to obstack_alloc.  */
145 
146 static void *
147 cprop_alloc (unsigned long size)
148 {
149   bytes_used += size;
150   return obstack_alloc (&cprop_obstack, size);
151 }
152 
153 /* Return nonzero if register X is unchanged from INSN to the end
154    of INSN's basic block.  */
155 
156 static int
157 reg_available_p (const_rtx x, const_rtx insn ATTRIBUTE_UNUSED)
158 {
159   return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
160 }
161 
162 /* Hash a set of register REGNO.
163 
164    Sets are hashed on the register that is set.  This simplifies the PRE copy
165    propagation code.
166 
167    ??? May need to make things more elaborate.  Later, as necessary.  */
168 
169 static unsigned int
170 hash_set (int regno, int hash_table_size)
171 {
172   return (unsigned) regno % hash_table_size;
173 }
174 
175 /* Insert assignment DEST:=SET from INSN in the hash table.
176    DEST is a register and SET is a register or a suitable constant.
177    If the assignment is already present in the table, record it as
178    the last occurrence in INSN's basic block.
179    IMPLICIT is true if it's an implicit set, false otherwise.  */
180 
181 static void
182 insert_set_in_table (rtx dest, rtx src, rtx insn, struct hash_table_d *table,
183 		     bool implicit)
184 {
185   bool found = false;
186   unsigned int hash;
187   struct expr *cur_expr, *last_expr = NULL;
188   struct occr *cur_occr;
189 
190   hash = hash_set (REGNO (dest), table->size);
191 
192   for (cur_expr = table->table[hash]; cur_expr;
193        cur_expr = cur_expr->next_same_hash)
194     {
195       if (dest == cur_expr->dest
196 	  && src == cur_expr->src)
197 	{
198 	  found = true;
199 	  break;
200 	}
201       last_expr = cur_expr;
202     }
203 
204   if (! found)
205     {
206       cur_expr = GOBNEW (struct expr);
207       bytes_used += sizeof (struct expr);
208       if (table->table[hash] == NULL)
209 	/* This is the first pattern that hashed to this index.  */
210 	table->table[hash] = cur_expr;
211       else
212 	/* Add EXPR to end of this hash chain.  */
213 	last_expr->next_same_hash = cur_expr;
214 
215       /* Set the fields of the expr element.
216 	 We must copy X because it can be modified when copy propagation is
217 	 performed on its operands.  */
218       cur_expr->dest = copy_rtx (dest);
219       cur_expr->src = copy_rtx (src);
220       cur_expr->bitmap_index = table->n_elems++;
221       cur_expr->next_same_hash = NULL;
222       cur_expr->avail_occr = NULL;
223     }
224 
225   /* Now record the occurrence.  */
226   cur_occr = cur_expr->avail_occr;
227 
228   if (cur_occr
229       && BLOCK_FOR_INSN (cur_occr->insn) == BLOCK_FOR_INSN (insn))
230     {
231       /* Found another instance of the expression in the same basic block.
232 	 Prefer this occurrence to the currently recorded one.  We want
233 	 the last one in the block and the block is scanned from start
234 	 to end.  */
235       cur_occr->insn = insn;
236     }
237   else
238     {
239       /* First occurrence of this expression in this basic block.  */
240       cur_occr = GOBNEW (struct occr);
241       bytes_used += sizeof (struct occr);
242       cur_occr->insn = insn;
243       cur_occr->next = cur_expr->avail_occr;
244       cur_expr->avail_occr = cur_occr;
245     }
246 
247   /* Record bitmap_index of the implicit set in implicit_set_indexes.  */
248   if (implicit)
249     implicit_set_indexes[BLOCK_FOR_INSN(insn)->index] = cur_expr->bitmap_index;
250 }
251 
252 /* Determine whether the rtx X should be treated as a constant for CPROP.
253    Since X might be inserted more than once we have to take care that it
254    is sharable.  */
255 
256 static bool
257 cprop_constant_p (const_rtx x)
258 {
259   return CONSTANT_P (x) && (GET_CODE (x) != CONST || shared_const_p (x));
260 }
261 
262 /* Scan SET present in INSN and add an entry to the hash TABLE.
263    IMPLICIT is true if it's an implicit set, false otherwise.  */
264 
265 static void
266 hash_scan_set (rtx set, rtx insn, struct hash_table_d *table, bool implicit)
267 {
268   rtx src = SET_SRC (set);
269   rtx dest = SET_DEST (set);
270 
271   if (REG_P (dest)
272       && ! HARD_REGISTER_P (dest)
273       && reg_available_p (dest, insn)
274       && can_copy_p (GET_MODE (dest)))
275     {
276       /* See if a REG_EQUAL note shows this equivalent to a simpler expression.
277 
278 	 This allows us to do a single CPROP pass and still eliminate
279 	 redundant constants, addresses or other expressions that are
280 	 constructed with multiple instructions.
281 
282 	 However, keep the original SRC if INSN is a simple reg-reg move.  In
283 	 In this case, there will almost always be a REG_EQUAL note on the
284 	 insn that sets SRC.  By recording the REG_EQUAL value here as SRC
285 	 for INSN, we miss copy propagation opportunities.
286 
287 	 Note that this does not impede profitable constant propagations.  We
288 	 "look through" reg-reg sets in lookup_set.  */
289       rtx note = find_reg_equal_equiv_note (insn);
290       if (note != 0
291 	  && REG_NOTE_KIND (note) == REG_EQUAL
292 	  && !REG_P (src)
293 	  && cprop_constant_p (XEXP (note, 0)))
294 	src = XEXP (note, 0), set = gen_rtx_SET (VOIDmode, dest, src);
295 
296       /* Record sets for constant/copy propagation.  */
297       if ((REG_P (src)
298 	   && src != dest
299 	   && ! HARD_REGISTER_P (src)
300 	   && reg_available_p (src, insn))
301 	  || cprop_constant_p (src))
302 	insert_set_in_table (dest, src, insn, table, implicit);
303     }
304 }
305 
306 /* Process INSN and add hash table entries as appropriate.  */
307 
308 static void
309 hash_scan_insn (rtx insn, struct hash_table_d *table)
310 {
311   rtx pat = PATTERN (insn);
312   int i;
313 
314   /* Pick out the sets of INSN and for other forms of instructions record
315      what's been modified.  */
316 
317   if (GET_CODE (pat) == SET)
318     hash_scan_set (pat, insn, table, false);
319   else if (GET_CODE (pat) == PARALLEL)
320     for (i = 0; i < XVECLEN (pat, 0); i++)
321       {
322 	rtx x = XVECEXP (pat, 0, i);
323 
324 	if (GET_CODE (x) == SET)
325 	  hash_scan_set (x, insn, table, false);
326       }
327 }
328 
329 /* Dump the hash table TABLE to file FILE under the name NAME.  */
330 
331 static void
332 dump_hash_table (FILE *file, const char *name, struct hash_table_d *table)
333 {
334   int i;
335   /* Flattened out table, so it's printed in proper order.  */
336   struct expr **flat_table;
337   unsigned int *hash_val;
338   struct expr *expr;
339 
340   flat_table = XCNEWVEC (struct expr *, table->n_elems);
341   hash_val = XNEWVEC (unsigned int, table->n_elems);
342 
343   for (i = 0; i < (int) table->size; i++)
344     for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
345       {
346 	flat_table[expr->bitmap_index] = expr;
347 	hash_val[expr->bitmap_index] = i;
348       }
349 
350   fprintf (file, "%s hash table (%d buckets, %d entries)\n",
351 	   name, table->size, table->n_elems);
352 
353   for (i = 0; i < (int) table->n_elems; i++)
354     if (flat_table[i] != 0)
355       {
356 	expr = flat_table[i];
357 	fprintf (file, "Index %d (hash value %d)\n  ",
358 		 expr->bitmap_index, hash_val[i]);
359 	print_rtl (file, expr->dest);
360 	fprintf (file, " := ");
361 	print_rtl (file, expr->src);
362 	fprintf (file, "\n");
363       }
364 
365   fprintf (file, "\n");
366 
367   free (flat_table);
368   free (hash_val);
369 }
370 
371 /* Record as unavailable all registers that are DEF operands of INSN.  */
372 
373 static void
374 make_set_regs_unavailable (rtx insn)
375 {
376   struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
377   df_ref *def_rec;
378 
379   for (def_rec = DF_INSN_INFO_DEFS (insn_info); *def_rec; def_rec++)
380     SET_REGNO_REG_SET (reg_set_bitmap, DF_REF_REGNO (*def_rec));
381 }
382 
383 /* Top level function to create an assignment hash table.
384 
385    Assignment entries are placed in the hash table if
386    - they are of the form (set (pseudo-reg) src),
387    - src is something we want to perform const/copy propagation on,
388    - none of the operands or target are subsequently modified in the block
389 
390    Currently src must be a pseudo-reg or a const_int.
391 
392    TABLE is the table computed.  */
393 
394 static void
395 compute_hash_table_work (struct hash_table_d *table)
396 {
397   basic_block bb;
398 
399   /* Allocate vars to track sets of regs.  */
400   reg_set_bitmap = ALLOC_REG_SET (NULL);
401 
402   FOR_EACH_BB (bb)
403     {
404       rtx insn;
405 
406       /* Reset tables used to keep track of what's not yet invalid [since
407 	 the end of the block].  */
408       CLEAR_REG_SET (reg_set_bitmap);
409 
410       /* Go over all insns from the last to the first.  This is convenient
411 	 for tracking available registers, i.e. not set between INSN and
412 	 the end of the basic block BB.  */
413       FOR_BB_INSNS_REVERSE (bb, insn)
414         {
415 	  /* Only real insns are interesting.  */
416 	  if (!NONDEBUG_INSN_P (insn))
417 	    continue;
418 
419 	  /* Record interesting sets from INSN in the hash table.  */
420 	  hash_scan_insn (insn, table);
421 
422 	  /* Any registers set in INSN will make SETs above it not AVAIL.  */
423 	  make_set_regs_unavailable (insn);
424 	}
425 
426       /* Insert implicit sets in the hash table, pretending they appear as
427 	 insns at the head of the basic block.  */
428       if (implicit_sets[bb->index] != NULL_RTX)
429 	hash_scan_set (implicit_sets[bb->index], BB_HEAD (bb), table, true);
430     }
431 
432   FREE_REG_SET (reg_set_bitmap);
433 }
434 
435 /* Allocate space for the set/expr hash TABLE.
436    It is used to determine the number of buckets to use.  */
437 
438 static void
439 alloc_hash_table (struct hash_table_d *table)
440 {
441   int n;
442 
443   n = get_max_insn_count ();
444 
445   table->size = n / 4;
446   if (table->size < 11)
447     table->size = 11;
448 
449   /* Attempt to maintain efficient use of hash table.
450      Making it an odd number is simplest for now.
451      ??? Later take some measurements.  */
452   table->size |= 1;
453   n = table->size * sizeof (struct expr *);
454   table->table = XNEWVAR (struct expr *, n);
455 }
456 
457 /* Free things allocated by alloc_hash_table.  */
458 
459 static void
460 free_hash_table (struct hash_table_d *table)
461 {
462   free (table->table);
463 }
464 
465 /* Compute the hash TABLE for doing copy/const propagation or
466    expression hash table.  */
467 
468 static void
469 compute_hash_table (struct hash_table_d *table)
470 {
471   /* Initialize count of number of entries in hash table.  */
472   table->n_elems = 0;
473   memset (table->table, 0, table->size * sizeof (struct expr *));
474 
475   compute_hash_table_work (table);
476 }
477 
478 /* Expression tracking support.  */
479 
480 /* Lookup REGNO in the set TABLE.  The result is a pointer to the
481    table entry, or NULL if not found.  */
482 
483 static struct expr *
484 lookup_set (unsigned int regno, struct hash_table_d *table)
485 {
486   unsigned int hash = hash_set (regno, table->size);
487   struct expr *expr;
488 
489   expr = table->table[hash];
490 
491   while (expr && REGNO (expr->dest) != regno)
492     expr = expr->next_same_hash;
493 
494   return expr;
495 }
496 
497 /* Return the next entry for REGNO in list EXPR.  */
498 
499 static struct expr *
500 next_set (unsigned int regno, struct expr *expr)
501 {
502   do
503     expr = expr->next_same_hash;
504   while (expr && REGNO (expr->dest) != regno);
505 
506   return expr;
507 }
508 
509 /* Reset tables used to keep track of what's still available [since the
510    start of the block].  */
511 
512 static void
513 reset_opr_set_tables (void)
514 {
515   /* Maintain a bitmap of which regs have been set since beginning of
516      the block.  */
517   CLEAR_REG_SET (reg_set_bitmap);
518 }
519 
520 /* Return nonzero if the register X has not been set yet [since the
521    start of the basic block containing INSN].  */
522 
523 static int
524 reg_not_set_p (const_rtx x, const_rtx insn ATTRIBUTE_UNUSED)
525 {
526   return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
527 }
528 
529 /* Record things set by INSN.
530    This data is used by reg_not_set_p.  */
531 
532 static void
533 mark_oprs_set (rtx insn)
534 {
535   struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
536   df_ref *def_rec;
537 
538   for (def_rec = DF_INSN_INFO_DEFS (insn_info); *def_rec; def_rec++)
539     SET_REGNO_REG_SET (reg_set_bitmap, DF_REF_REGNO (*def_rec));
540 }
541 
542 /* Compute copy/constant propagation working variables.  */
543 
544 /* Local properties of assignments.  */
545 static sbitmap *cprop_avloc;
546 static sbitmap *cprop_kill;
547 
548 /* Global properties of assignments (computed from the local properties).  */
549 static sbitmap *cprop_avin;
550 static sbitmap *cprop_avout;
551 
552 /* Allocate vars used for copy/const propagation.  N_BLOCKS is the number of
553    basic blocks.  N_SETS is the number of sets.  */
554 
555 static void
556 alloc_cprop_mem (int n_blocks, int n_sets)
557 {
558   cprop_avloc = sbitmap_vector_alloc (n_blocks, n_sets);
559   cprop_kill = sbitmap_vector_alloc (n_blocks, n_sets);
560 
561   cprop_avin = sbitmap_vector_alloc (n_blocks, n_sets);
562   cprop_avout = sbitmap_vector_alloc (n_blocks, n_sets);
563 }
564 
565 /* Free vars used by copy/const propagation.  */
566 
567 static void
568 free_cprop_mem (void)
569 {
570   sbitmap_vector_free (cprop_avloc);
571   sbitmap_vector_free (cprop_kill);
572   sbitmap_vector_free (cprop_avin);
573   sbitmap_vector_free (cprop_avout);
574 }
575 
576 /* Compute the local properties of each recorded expression.
577 
578    Local properties are those that are defined by the block, irrespective of
579    other blocks.
580 
581    An expression is killed in a block if its operands, either DEST or SRC, are
582    modified in the block.
583 
584    An expression is computed (locally available) in a block if it is computed
585    at least once and expression would contain the same value if the
586    computation was moved to the end of the block.
587 
588    KILL and COMP are destination sbitmaps for recording local properties.  */
589 
590 static void
591 compute_local_properties (sbitmap *kill, sbitmap *comp,
592 			  struct hash_table_d *table)
593 {
594   unsigned int i;
595 
596   /* Initialize the bitmaps that were passed in.  */
597   bitmap_vector_clear (kill, last_basic_block);
598   bitmap_vector_clear (comp, last_basic_block);
599 
600   for (i = 0; i < table->size; i++)
601     {
602       struct expr *expr;
603 
604       for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
605 	{
606 	  int indx = expr->bitmap_index;
607 	  df_ref def;
608 	  struct occr *occr;
609 
610 	  /* For each definition of the destination pseudo-reg, the expression
611 	     is killed in the block where the definition is.  */
612 	  for (def = DF_REG_DEF_CHAIN (REGNO (expr->dest));
613 	       def; def = DF_REF_NEXT_REG (def))
614 	    bitmap_set_bit (kill[DF_REF_BB (def)->index], indx);
615 
616 	  /* If the source is a pseudo-reg, for each definition of the source,
617 	     the expression is killed in the block where the definition is.  */
618 	  if (REG_P (expr->src))
619 	    for (def = DF_REG_DEF_CHAIN (REGNO (expr->src));
620 		 def; def = DF_REF_NEXT_REG (def))
621 	      bitmap_set_bit (kill[DF_REF_BB (def)->index], indx);
622 
623 	  /* The occurrences recorded in avail_occr are exactly those that
624 	     are locally available in the block where they are.  */
625 	  for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
626 	    {
627 	      bitmap_set_bit (comp[BLOCK_FOR_INSN (occr->insn)->index], indx);
628 	    }
629 	}
630     }
631 }
632 
633 /* Hash table support.  */
634 
635 /* Top level routine to do the dataflow analysis needed by copy/const
636    propagation.  */
637 
638 static void
639 compute_cprop_data (void)
640 {
641   basic_block bb;
642 
643   compute_local_properties (cprop_kill, cprop_avloc, &set_hash_table);
644   compute_available (cprop_avloc, cprop_kill, cprop_avout, cprop_avin);
645 
646   /* Merge implicit sets into CPROP_AVIN.  They are always available at the
647      entry of their basic block.  We need to do this because 1) implicit sets
648      aren't recorded for the local pass so they cannot be propagated within
649      their basic block by this pass and 2) the global pass would otherwise
650      propagate them only in the successors of their basic block.  */
651   FOR_EACH_BB (bb)
652     {
653       int index = implicit_set_indexes[bb->index];
654       if (index != -1)
655 	bitmap_set_bit (cprop_avin[bb->index], index);
656     }
657 }
658 
659 /* Copy/constant propagation.  */
660 
661 /* Maximum number of register uses in an insn that we handle.  */
662 #define MAX_USES 8
663 
664 /* Table of uses (registers, both hard and pseudo) found in an insn.
665    Allocated statically to avoid alloc/free complexity and overhead.  */
666 static rtx reg_use_table[MAX_USES];
667 
668 /* Index into `reg_use_table' while building it.  */
669 static unsigned reg_use_count;
670 
671 /* Set up a list of register numbers used in INSN.  The found uses are stored
672    in `reg_use_table'.  `reg_use_count' is initialized to zero before entry,
673    and contains the number of uses in the table upon exit.
674 
675    ??? If a register appears multiple times we will record it multiple times.
676    This doesn't hurt anything but it will slow things down.  */
677 
678 static void
679 find_used_regs (rtx *xptr, void *data ATTRIBUTE_UNUSED)
680 {
681   int i, j;
682   enum rtx_code code;
683   const char *fmt;
684   rtx x = *xptr;
685 
686   /* repeat is used to turn tail-recursion into iteration since GCC
687      can't do it when there's no return value.  */
688  repeat:
689   if (x == 0)
690     return;
691 
692   code = GET_CODE (x);
693   if (REG_P (x))
694     {
695       if (reg_use_count == MAX_USES)
696 	return;
697 
698       reg_use_table[reg_use_count] = x;
699       reg_use_count++;
700     }
701 
702   /* Recursively scan the operands of this expression.  */
703 
704   for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
705     {
706       if (fmt[i] == 'e')
707 	{
708 	  /* If we are about to do the last recursive call
709 	     needed at this level, change it into iteration.
710 	     This function is called enough to be worth it.  */
711 	  if (i == 0)
712 	    {
713 	      x = XEXP (x, 0);
714 	      goto repeat;
715 	    }
716 
717 	  find_used_regs (&XEXP (x, i), data);
718 	}
719       else if (fmt[i] == 'E')
720 	for (j = 0; j < XVECLEN (x, i); j++)
721 	  find_used_regs (&XVECEXP (x, i, j), data);
722     }
723 }
724 
725 /* Try to replace all uses of FROM in INSN with TO.
726    Return nonzero if successful.  */
727 
728 static int
729 try_replace_reg (rtx from, rtx to, rtx insn)
730 {
731   rtx note = find_reg_equal_equiv_note (insn);
732   rtx src = 0;
733   int success = 0;
734   rtx set = single_set (insn);
735 
736   /* Usually we substitute easy stuff, so we won't copy everything.
737      We however need to take care to not duplicate non-trivial CONST
738      expressions.  */
739   to = copy_rtx (to);
740 
741   validate_replace_src_group (from, to, insn);
742   if (num_changes_pending () && apply_change_group ())
743     success = 1;
744 
745   /* Try to simplify SET_SRC if we have substituted a constant.  */
746   if (success && set && CONSTANT_P (to))
747     {
748       src = simplify_rtx (SET_SRC (set));
749 
750       if (src)
751 	validate_change (insn, &SET_SRC (set), src, 0);
752     }
753 
754   /* If there is already a REG_EQUAL note, update the expression in it
755      with our replacement.  */
756   if (note != 0 && REG_NOTE_KIND (note) == REG_EQUAL)
757     set_unique_reg_note (insn, REG_EQUAL,
758 			 simplify_replace_rtx (XEXP (note, 0), from, to));
759   if (!success && set && reg_mentioned_p (from, SET_SRC (set)))
760     {
761       /* If above failed and this is a single set, try to simplify the source
762 	 of the set given our substitution.  We could perhaps try this for
763 	 multiple SETs, but it probably won't buy us anything.  */
764       src = simplify_replace_rtx (SET_SRC (set), from, to);
765 
766       if (!rtx_equal_p (src, SET_SRC (set))
767 	  && validate_change (insn, &SET_SRC (set), src, 0))
768 	success = 1;
769 
770       /* If we've failed perform the replacement, have a single SET to
771 	 a REG destination and don't yet have a note, add a REG_EQUAL note
772 	 to not lose information.  */
773       if (!success && note == 0 && set != 0 && REG_P (SET_DEST (set)))
774 	note = set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src));
775     }
776 
777   if (set && MEM_P (SET_DEST (set)) && reg_mentioned_p (from, SET_DEST (set)))
778     {
779       /* Registers can also appear as uses in SET_DEST if it is a MEM.
780          We could perhaps try this for multiple SETs, but it probably
781          won't buy us anything.  */
782       rtx dest = simplify_replace_rtx (SET_DEST (set), from, to);
783 
784       if (!rtx_equal_p (dest, SET_DEST (set))
785           && validate_change (insn, &SET_DEST (set), dest, 0))
786         success = 1;
787     }
788 
789   /* REG_EQUAL may get simplified into register.
790      We don't allow that. Remove that note. This code ought
791      not to happen, because previous code ought to synthesize
792      reg-reg move, but be on the safe side.  */
793   if (note && REG_NOTE_KIND (note) == REG_EQUAL && REG_P (XEXP (note, 0)))
794     remove_note (insn, note);
795 
796   return success;
797 }
798 
799 /* Find a set of REGNOs that are available on entry to INSN's block.  Return
800    NULL no such set is found.  */
801 
802 static struct expr *
803 find_avail_set (int regno, rtx insn)
804 {
805   /* SET1 contains the last set found that can be returned to the caller for
806      use in a substitution.  */
807   struct expr *set1 = 0;
808 
809   /* Loops are not possible here.  To get a loop we would need two sets
810      available at the start of the block containing INSN.  i.e. we would
811      need two sets like this available at the start of the block:
812 
813        (set (reg X) (reg Y))
814        (set (reg Y) (reg X))
815 
816      This can not happen since the set of (reg Y) would have killed the
817      set of (reg X) making it unavailable at the start of this block.  */
818   while (1)
819     {
820       rtx src;
821       struct expr *set = lookup_set (regno, &set_hash_table);
822 
823       /* Find a set that is available at the start of the block
824 	 which contains INSN.  */
825       while (set)
826 	{
827 	  if (bitmap_bit_p (cprop_avin[BLOCK_FOR_INSN (insn)->index],
828 			set->bitmap_index))
829 	    break;
830 	  set = next_set (regno, set);
831 	}
832 
833       /* If no available set was found we've reached the end of the
834 	 (possibly empty) copy chain.  */
835       if (set == 0)
836 	break;
837 
838       src = set->src;
839 
840       /* We know the set is available.
841 	 Now check that SRC is locally anticipatable (i.e. none of the
842 	 source operands have changed since the start of the block).
843 
844          If the source operand changed, we may still use it for the next
845          iteration of this loop, but we may not use it for substitutions.  */
846 
847       if (cprop_constant_p (src) || reg_not_set_p (src, insn))
848 	set1 = set;
849 
850       /* If the source of the set is anything except a register, then
851 	 we have reached the end of the copy chain.  */
852       if (! REG_P (src))
853 	break;
854 
855       /* Follow the copy chain, i.e. start another iteration of the loop
856 	 and see if we have an available copy into SRC.  */
857       regno = REGNO (src);
858     }
859 
860   /* SET1 holds the last set that was available and anticipatable at
861      INSN.  */
862   return set1;
863 }
864 
865 /* Subroutine of cprop_insn that tries to propagate constants into
866    JUMP_INSNS.  JUMP must be a conditional jump.  If SETCC is non-NULL
867    it is the instruction that immediately precedes JUMP, and must be a
868    single SET of a register.  FROM is what we will try to replace,
869    SRC is the constant we will try to substitute for it.  Return nonzero
870    if a change was made.  */
871 
872 static int
873 cprop_jump (basic_block bb, rtx setcc, rtx jump, rtx from, rtx src)
874 {
875   rtx new_rtx, set_src, note_src;
876   rtx set = pc_set (jump);
877   rtx note = find_reg_equal_equiv_note (jump);
878 
879   if (note)
880     {
881       note_src = XEXP (note, 0);
882       if (GET_CODE (note_src) == EXPR_LIST)
883 	note_src = NULL_RTX;
884     }
885   else note_src = NULL_RTX;
886 
887   /* Prefer REG_EQUAL notes except those containing EXPR_LISTs.  */
888   set_src = note_src ? note_src : SET_SRC (set);
889 
890   /* First substitute the SETCC condition into the JUMP instruction,
891      then substitute that given values into this expanded JUMP.  */
892   if (setcc != NULL_RTX
893       && !modified_between_p (from, setcc, jump)
894       && !modified_between_p (src, setcc, jump))
895     {
896       rtx setcc_src;
897       rtx setcc_set = single_set (setcc);
898       rtx setcc_note = find_reg_equal_equiv_note (setcc);
899       setcc_src = (setcc_note && GET_CODE (XEXP (setcc_note, 0)) != EXPR_LIST)
900 		? XEXP (setcc_note, 0) : SET_SRC (setcc_set);
901       set_src = simplify_replace_rtx (set_src, SET_DEST (setcc_set),
902 				      setcc_src);
903     }
904   else
905     setcc = NULL_RTX;
906 
907   new_rtx = simplify_replace_rtx (set_src, from, src);
908 
909   /* If no simplification can be made, then try the next register.  */
910   if (rtx_equal_p (new_rtx, SET_SRC (set)))
911     return 0;
912 
913   /* If this is now a no-op delete it, otherwise this must be a valid insn.  */
914   if (new_rtx == pc_rtx)
915     delete_insn (jump);
916   else
917     {
918       /* Ensure the value computed inside the jump insn to be equivalent
919          to one computed by setcc.  */
920       if (setcc && modified_in_p (new_rtx, setcc))
921 	return 0;
922       if (! validate_unshare_change (jump, &SET_SRC (set), new_rtx, 0))
923 	{
924 	  /* When (some) constants are not valid in a comparison, and there
925 	     are two registers to be replaced by constants before the entire
926 	     comparison can be folded into a constant, we need to keep
927 	     intermediate information in REG_EQUAL notes.  For targets with
928 	     separate compare insns, such notes are added by try_replace_reg.
929 	     When we have a combined compare-and-branch instruction, however,
930 	     we need to attach a note to the branch itself to make this
931 	     optimization work.  */
932 
933 	  if (!rtx_equal_p (new_rtx, note_src))
934 	    set_unique_reg_note (jump, REG_EQUAL, copy_rtx (new_rtx));
935 	  return 0;
936 	}
937 
938       /* Remove REG_EQUAL note after simplification.  */
939       if (note_src)
940 	remove_note (jump, note);
941      }
942 
943 #ifdef HAVE_cc0
944   /* Delete the cc0 setter.  */
945   if (setcc != NULL && CC0_P (SET_DEST (single_set (setcc))))
946     delete_insn (setcc);
947 #endif
948 
949   global_const_prop_count++;
950   if (dump_file != NULL)
951     {
952       fprintf (dump_file,
953 	       "GLOBAL CONST-PROP: Replacing reg %d in jump_insn %d with"
954 	       "constant ", REGNO (from), INSN_UID (jump));
955       print_rtl (dump_file, src);
956       fprintf (dump_file, "\n");
957     }
958   purge_dead_edges (bb);
959 
960   /* If a conditional jump has been changed into unconditional jump, remove
961      the jump and make the edge fallthru - this is always called in
962      cfglayout mode.  */
963   if (new_rtx != pc_rtx && simplejump_p (jump))
964     {
965       edge e;
966       edge_iterator ei;
967 
968       FOR_EACH_EDGE (e, ei, bb->succs)
969 	if (e->dest != EXIT_BLOCK_PTR
970 	    && BB_HEAD (e->dest) == JUMP_LABEL (jump))
971 	  {
972 	    e->flags |= EDGE_FALLTHRU;
973 	    break;
974 	  }
975       delete_insn (jump);
976     }
977 
978   return 1;
979 }
980 
981 /* Subroutine of cprop_insn that tries to propagate constants.  FROM is what
982    we will try to replace, SRC is the constant we will try to substitute for
983    it and INSN is the instruction where this will be happening.  */
984 
985 static int
986 constprop_register (rtx from, rtx src, rtx insn)
987 {
988   rtx sset;
989 
990   /* Check for reg or cc0 setting instructions followed by
991      conditional branch instructions first.  */
992   if ((sset = single_set (insn)) != NULL
993       && NEXT_INSN (insn)
994       && any_condjump_p (NEXT_INSN (insn)) && onlyjump_p (NEXT_INSN (insn)))
995     {
996       rtx dest = SET_DEST (sset);
997       if ((REG_P (dest) || CC0_P (dest))
998 	  && cprop_jump (BLOCK_FOR_INSN (insn), insn, NEXT_INSN (insn),
999 			 from, src))
1000 	return 1;
1001     }
1002 
1003   /* Handle normal insns next.  */
1004   if (NONJUMP_INSN_P (insn) && try_replace_reg (from, src, insn))
1005     return 1;
1006 
1007   /* Try to propagate a CONST_INT into a conditional jump.
1008      We're pretty specific about what we will handle in this
1009      code, we can extend this as necessary over time.
1010 
1011      Right now the insn in question must look like
1012      (set (pc) (if_then_else ...))  */
1013   else if (any_condjump_p (insn) && onlyjump_p (insn))
1014     return cprop_jump (BLOCK_FOR_INSN (insn), NULL, insn, from, src);
1015   return 0;
1016 }
1017 
1018 /* Perform constant and copy propagation on INSN.
1019    Return nonzero if a change was made.  */
1020 
1021 static int
1022 cprop_insn (rtx insn)
1023 {
1024   unsigned i;
1025   int changed = 0, changed_this_round;
1026   rtx note;
1027 
1028 retry:
1029   changed_this_round = 0;
1030   reg_use_count = 0;
1031   note_uses (&PATTERN (insn), find_used_regs, NULL);
1032 
1033   /* We may win even when propagating constants into notes.  */
1034   note = find_reg_equal_equiv_note (insn);
1035   if (note)
1036     find_used_regs (&XEXP (note, 0), NULL);
1037 
1038   for (i = 0; i < reg_use_count; i++)
1039     {
1040       rtx reg_used = reg_use_table[i];
1041       unsigned int regno = REGNO (reg_used);
1042       rtx src;
1043       struct expr *set;
1044 
1045       /* If the register has already been set in this block, there's
1046 	 nothing we can do.  */
1047       if (! reg_not_set_p (reg_used, insn))
1048 	continue;
1049 
1050       /* Find an assignment that sets reg_used and is available
1051 	 at the start of the block.  */
1052       set = find_avail_set (regno, insn);
1053       if (! set)
1054 	continue;
1055 
1056       src = set->src;
1057 
1058       /* Constant propagation.  */
1059       if (cprop_constant_p (src))
1060 	{
1061           if (constprop_register (reg_used, src, insn))
1062 	    {
1063 	      changed_this_round = changed = 1;
1064 	      global_const_prop_count++;
1065 	      if (dump_file != NULL)
1066 		{
1067 		  fprintf (dump_file,
1068 			   "GLOBAL CONST-PROP: Replacing reg %d in ", regno);
1069 		  fprintf (dump_file, "insn %d with constant ",
1070 			   INSN_UID (insn));
1071 		  print_rtl (dump_file, src);
1072 		  fprintf (dump_file, "\n");
1073 		}
1074 	      if (INSN_DELETED_P (insn))
1075 		return 1;
1076 	    }
1077 	}
1078       else if (REG_P (src)
1079 	       && REGNO (src) >= FIRST_PSEUDO_REGISTER
1080 	       && REGNO (src) != regno)
1081 	{
1082 	  if (try_replace_reg (reg_used, src, insn))
1083 	    {
1084 	      changed_this_round = changed = 1;
1085 	      global_copy_prop_count++;
1086 	      if (dump_file != NULL)
1087 		{
1088 		  fprintf (dump_file,
1089 			   "GLOBAL COPY-PROP: Replacing reg %d in insn %d",
1090 			   regno, INSN_UID (insn));
1091 		  fprintf (dump_file, " with reg %d\n", REGNO (src));
1092 		}
1093 
1094 	      /* The original insn setting reg_used may or may not now be
1095 		 deletable.  We leave the deletion to DCE.  */
1096 	      /* FIXME: If it turns out that the insn isn't deletable,
1097 		 then we may have unnecessarily extended register lifetimes
1098 		 and made things worse.  */
1099 	    }
1100 	}
1101 
1102       /* If try_replace_reg simplified the insn, the regs found
1103 	 by find_used_regs may not be valid anymore.  Start over.  */
1104       if (changed_this_round)
1105 	goto retry;
1106     }
1107 
1108   if (changed && DEBUG_INSN_P (insn))
1109     return 0;
1110 
1111   return changed;
1112 }
1113 
1114 /* Like find_used_regs, but avoid recording uses that appear in
1115    input-output contexts such as zero_extract or pre_dec.  This
1116    restricts the cases we consider to those for which local cprop
1117    can legitimately make replacements.  */
1118 
1119 static void
1120 local_cprop_find_used_regs (rtx *xptr, void *data)
1121 {
1122   rtx x = *xptr;
1123 
1124   if (x == 0)
1125     return;
1126 
1127   switch (GET_CODE (x))
1128     {
1129     case ZERO_EXTRACT:
1130     case SIGN_EXTRACT:
1131     case STRICT_LOW_PART:
1132       return;
1133 
1134     case PRE_DEC:
1135     case PRE_INC:
1136     case POST_DEC:
1137     case POST_INC:
1138     case PRE_MODIFY:
1139     case POST_MODIFY:
1140       /* Can only legitimately appear this early in the context of
1141 	 stack pushes for function arguments, but handle all of the
1142 	 codes nonetheless.  */
1143       return;
1144 
1145     case SUBREG:
1146       /* Setting a subreg of a register larger than word_mode leaves
1147 	 the non-written words unchanged.  */
1148       if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) > BITS_PER_WORD)
1149 	return;
1150       break;
1151 
1152     default:
1153       break;
1154     }
1155 
1156   find_used_regs (xptr, data);
1157 }
1158 
1159 /* Try to perform local const/copy propagation on X in INSN.  */
1160 
1161 static bool
1162 do_local_cprop (rtx x, rtx insn)
1163 {
1164   rtx newreg = NULL, newcnst = NULL;
1165 
1166   /* Rule out USE instructions and ASM statements as we don't want to
1167      change the hard registers mentioned.  */
1168   if (REG_P (x)
1169       && (REGNO (x) >= FIRST_PSEUDO_REGISTER
1170           || (GET_CODE (PATTERN (insn)) != USE
1171 	      && asm_noperands (PATTERN (insn)) < 0)))
1172     {
1173       cselib_val *val = cselib_lookup (x, GET_MODE (x), 0, VOIDmode);
1174       struct elt_loc_list *l;
1175 
1176       if (!val)
1177 	return false;
1178       for (l = val->locs; l; l = l->next)
1179 	{
1180 	  rtx this_rtx = l->loc;
1181 	  rtx note;
1182 
1183 	  if (cprop_constant_p (this_rtx))
1184 	    newcnst = this_rtx;
1185 	  if (REG_P (this_rtx) && REGNO (this_rtx) >= FIRST_PSEUDO_REGISTER
1186 	      /* Don't copy propagate if it has attached REG_EQUIV note.
1187 		 At this point this only function parameters should have
1188 		 REG_EQUIV notes and if the argument slot is used somewhere
1189 		 explicitly, it means address of parameter has been taken,
1190 		 so we should not extend the lifetime of the pseudo.  */
1191 	      && (!(note = find_reg_note (l->setting_insn, REG_EQUIV, NULL_RTX))
1192 		  || ! MEM_P (XEXP (note, 0))))
1193 	    newreg = this_rtx;
1194 	}
1195       if (newcnst && constprop_register (x, newcnst, insn))
1196 	{
1197 	  if (dump_file != NULL)
1198 	    {
1199 	      fprintf (dump_file, "LOCAL CONST-PROP: Replacing reg %d in ",
1200 		       REGNO (x));
1201 	      fprintf (dump_file, "insn %d with constant ",
1202 		       INSN_UID (insn));
1203 	      print_rtl (dump_file, newcnst);
1204 	      fprintf (dump_file, "\n");
1205 	    }
1206 	  local_const_prop_count++;
1207 	  return true;
1208 	}
1209       else if (newreg && newreg != x && try_replace_reg (x, newreg, insn))
1210 	{
1211 	  if (dump_file != NULL)
1212 	    {
1213 	      fprintf (dump_file,
1214 		       "LOCAL COPY-PROP: Replacing reg %d in insn %d",
1215 		       REGNO (x), INSN_UID (insn));
1216 	      fprintf (dump_file, " with reg %d\n", REGNO (newreg));
1217 	    }
1218 	  local_copy_prop_count++;
1219 	  return true;
1220 	}
1221     }
1222   return false;
1223 }
1224 
1225 /* Do local const/copy propagation (i.e. within each basic block).  */
1226 
1227 static int
1228 local_cprop_pass (void)
1229 {
1230   basic_block bb;
1231   rtx insn;
1232   bool changed = false;
1233   unsigned i;
1234 
1235   cselib_init (0);
1236   FOR_EACH_BB (bb)
1237     {
1238       FOR_BB_INSNS (bb, insn)
1239 	{
1240 	  if (INSN_P (insn))
1241 	    {
1242 	      rtx note = find_reg_equal_equiv_note (insn);
1243 	      do
1244 		{
1245 		  reg_use_count = 0;
1246 		  note_uses (&PATTERN (insn), local_cprop_find_used_regs,
1247 			     NULL);
1248 		  if (note)
1249 		    local_cprop_find_used_regs (&XEXP (note, 0), NULL);
1250 
1251 		  for (i = 0; i < reg_use_count; i++)
1252 		    {
1253 		      if (do_local_cprop (reg_use_table[i], insn))
1254 			{
1255 			  if (!DEBUG_INSN_P (insn))
1256 			    changed = true;
1257 			  break;
1258 			}
1259 		    }
1260 		  if (INSN_DELETED_P (insn))
1261 		    break;
1262 		}
1263 	      while (i < reg_use_count);
1264 	    }
1265 	  cselib_process_insn (insn);
1266 	}
1267 
1268       /* Forget everything at the end of a basic block.  */
1269       cselib_clear_table ();
1270     }
1271 
1272   cselib_finish ();
1273 
1274   return changed;
1275 }
1276 
1277 /* Similar to get_condition, only the resulting condition must be
1278    valid at JUMP, instead of at EARLIEST.
1279 
1280    This differs from noce_get_condition in ifcvt.c in that we prefer not to
1281    settle for the condition variable in the jump instruction being integral.
1282    We prefer to be able to record the value of a user variable, rather than
1283    the value of a temporary used in a condition.  This could be solved by
1284    recording the value of *every* register scanned by canonicalize_condition,
1285    but this would require some code reorganization.  */
1286 
1287 rtx
1288 fis_get_condition (rtx jump)
1289 {
1290   return get_condition (jump, NULL, false, true);
1291 }
1292 
1293 /* Check the comparison COND to see if we can safely form an implicit
1294    set from it.  */
1295 
1296 static bool
1297 implicit_set_cond_p (const_rtx cond)
1298 {
1299   enum machine_mode mode;
1300   rtx cst;
1301 
1302   /* COND must be either an EQ or NE comparison.  */
1303   if (GET_CODE (cond) != EQ && GET_CODE (cond) != NE)
1304     return false;
1305 
1306   /* The first operand of COND must be a pseudo-reg.  */
1307   if (! REG_P (XEXP (cond, 0))
1308       || HARD_REGISTER_P (XEXP (cond, 0)))
1309     return false;
1310 
1311   /* The second operand of COND must be a suitable constant.  */
1312   mode = GET_MODE (XEXP (cond, 0));
1313   cst = XEXP (cond, 1);
1314 
1315   /* We can't perform this optimization if either operand might be or might
1316      contain a signed zero.  */
1317   if (HONOR_SIGNED_ZEROS (mode))
1318     {
1319       /* It is sufficient to check if CST is or contains a zero.  We must
1320 	 handle float, complex, and vector.  If any subpart is a zero, then
1321 	 the optimization can't be performed.  */
1322       /* ??? The complex and vector checks are not implemented yet.  We just
1323 	 always return zero for them.  */
1324       if (CONST_DOUBLE_AS_FLOAT_P (cst))
1325 	{
1326 	  REAL_VALUE_TYPE d;
1327 	  REAL_VALUE_FROM_CONST_DOUBLE (d, cst);
1328 	  if (REAL_VALUES_EQUAL (d, dconst0))
1329 	    return 0;
1330 	}
1331       else
1332 	return 0;
1333     }
1334 
1335   return cprop_constant_p (cst);
1336 }
1337 
1338 /* Find the implicit sets of a function.  An "implicit set" is a constraint
1339    on the value of a variable, implied by a conditional jump.  For example,
1340    following "if (x == 2)", the then branch may be optimized as though the
1341    conditional performed an "explicit set", in this example, "x = 2".  This
1342    function records the set patterns that are implicit at the start of each
1343    basic block.
1344 
1345    If an implicit set is found but the set is implicit on a critical edge,
1346    this critical edge is split.
1347 
1348    Return true if the CFG was modified, false otherwise.  */
1349 
1350 static bool
1351 find_implicit_sets (void)
1352 {
1353   basic_block bb, dest;
1354   rtx cond, new_rtx;
1355   unsigned int count = 0;
1356   bool edges_split = false;
1357   size_t implicit_sets_size = last_basic_block + 10;
1358 
1359   implicit_sets = XCNEWVEC (rtx, implicit_sets_size);
1360 
1361   FOR_EACH_BB (bb)
1362     {
1363       /* Check for more than one successor.  */
1364       if (EDGE_COUNT (bb->succs) <= 1)
1365 	continue;
1366 
1367       cond = fis_get_condition (BB_END (bb));
1368 
1369       /* If no condition is found or if it isn't of a suitable form,
1370 	 ignore it.  */
1371       if (! cond || ! implicit_set_cond_p (cond))
1372 	continue;
1373 
1374       dest = GET_CODE (cond) == EQ
1375 	? BRANCH_EDGE (bb)->dest : FALLTHRU_EDGE (bb)->dest;
1376 
1377       /* If DEST doesn't go anywhere, ignore it.  */
1378       if (! dest || dest == EXIT_BLOCK_PTR)
1379 	continue;
1380 
1381       /* We have found a suitable implicit set.  Try to record it now as
1382 	 a SET in DEST.  If DEST has more than one predecessor, the edge
1383 	 between BB and DEST is a critical edge and we must split it,
1384 	 because we can only record one implicit set per DEST basic block.  */
1385       if (! single_pred_p (dest))
1386         {
1387 	  dest = split_edge (find_edge (bb, dest));
1388 	  edges_split = true;
1389 	}
1390 
1391       if (implicit_sets_size <= (size_t) dest->index)
1392       {
1393         size_t old_implicit_sets_size = implicit_sets_size;
1394 	implicit_sets_size *= 2;
1395 	implicit_sets = XRESIZEVEC (rtx, implicit_sets, implicit_sets_size);
1396 	memset (implicit_sets + old_implicit_sets_size, 0,
1397 		(implicit_sets_size - old_implicit_sets_size) * sizeof (rtx));
1398       }
1399 
1400       new_rtx = gen_rtx_SET (VOIDmode, XEXP (cond, 0),
1401 			     XEXP (cond, 1));
1402       implicit_sets[dest->index] = new_rtx;
1403       if (dump_file)
1404 	{
1405 	  fprintf(dump_file, "Implicit set of reg %d in ",
1406 		  REGNO (XEXP (cond, 0)));
1407 	  fprintf(dump_file, "basic block %d\n", dest->index);
1408 	}
1409       count++;
1410     }
1411 
1412   if (dump_file)
1413     fprintf (dump_file, "Found %d implicit sets\n", count);
1414 
1415   /* Confess our sins.  */
1416   return edges_split;
1417 }
1418 
1419 /* Bypass conditional jumps.  */
1420 
1421 /* The value of last_basic_block at the beginning of the jump_bypass
1422    pass.  The use of redirect_edge_and_branch_force may introduce new
1423    basic blocks, but the data flow analysis is only valid for basic
1424    block indices less than bypass_last_basic_block.  */
1425 
1426 static int bypass_last_basic_block;
1427 
1428 /* Find a set of REGNO to a constant that is available at the end of basic
1429    block BB.  Return NULL if no such set is found.  Based heavily upon
1430    find_avail_set.  */
1431 
1432 static struct expr *
1433 find_bypass_set (int regno, int bb)
1434 {
1435   struct expr *result = 0;
1436 
1437   for (;;)
1438     {
1439       rtx src;
1440       struct expr *set = lookup_set (regno, &set_hash_table);
1441 
1442       while (set)
1443 	{
1444 	  if (bitmap_bit_p (cprop_avout[bb], set->bitmap_index))
1445 	    break;
1446 	  set = next_set (regno, set);
1447 	}
1448 
1449       if (set == 0)
1450 	break;
1451 
1452       src = set->src;
1453       if (cprop_constant_p (src))
1454 	result = set;
1455 
1456       if (! REG_P (src))
1457 	break;
1458 
1459       regno = REGNO (src);
1460     }
1461   return result;
1462 }
1463 
1464 /* Subroutine of bypass_block that checks whether a pseudo is killed by
1465    any of the instructions inserted on an edge.  Jump bypassing places
1466    condition code setters on CFG edges using insert_insn_on_edge.  This
1467    function is required to check that our data flow analysis is still
1468    valid prior to commit_edge_insertions.  */
1469 
1470 static bool
1471 reg_killed_on_edge (const_rtx reg, const_edge e)
1472 {
1473   rtx insn;
1474 
1475   for (insn = e->insns.r; insn; insn = NEXT_INSN (insn))
1476     if (INSN_P (insn) && reg_set_p (reg, insn))
1477       return true;
1478 
1479   return false;
1480 }
1481 
1482 /* Subroutine of bypass_conditional_jumps that attempts to bypass the given
1483    basic block BB which has more than one predecessor.  If not NULL, SETCC
1484    is the first instruction of BB, which is immediately followed by JUMP_INSN
1485    JUMP.  Otherwise, SETCC is NULL, and JUMP is the first insn of BB.
1486    Returns nonzero if a change was made.
1487 
1488    During the jump bypassing pass, we may place copies of SETCC instructions
1489    on CFG edges.  The following routine must be careful to pay attention to
1490    these inserted insns when performing its transformations.  */
1491 
1492 static int
1493 bypass_block (basic_block bb, rtx setcc, rtx jump)
1494 {
1495   rtx insn, note;
1496   edge e, edest;
1497   int change;
1498   int may_be_loop_header = false;
1499   unsigned removed_p;
1500   unsigned i;
1501   edge_iterator ei;
1502 
1503   insn = (setcc != NULL) ? setcc : jump;
1504 
1505   /* Determine set of register uses in INSN.  */
1506   reg_use_count = 0;
1507   note_uses (&PATTERN (insn), find_used_regs, NULL);
1508   note = find_reg_equal_equiv_note (insn);
1509   if (note)
1510     find_used_regs (&XEXP (note, 0), NULL);
1511 
1512   if (current_loops)
1513     {
1514       /* If we are to preserve loop structure then do not bypass
1515          a loop header.  This will either rotate the loop, create
1516 	 multiple entry loops or even irreducible regions.  */
1517       if (bb == bb->loop_father->header)
1518 	return 0;
1519     }
1520   else
1521     {
1522       FOR_EACH_EDGE (e, ei, bb->preds)
1523 	if (e->flags & EDGE_DFS_BACK)
1524 	  {
1525 	    may_be_loop_header = true;
1526 	    break;
1527 	  }
1528     }
1529 
1530   change = 0;
1531   for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
1532     {
1533       removed_p = 0;
1534 
1535       if (e->flags & EDGE_COMPLEX)
1536 	{
1537 	  ei_next (&ei);
1538 	  continue;
1539 	}
1540 
1541       /* We can't redirect edges from new basic blocks.  */
1542       if (e->src->index >= bypass_last_basic_block)
1543 	{
1544 	  ei_next (&ei);
1545 	  continue;
1546 	}
1547 
1548       /* The irreducible loops created by redirecting of edges entering the
1549 	 loop from outside would decrease effectiveness of some of the
1550 	 following optimizations, so prevent this.  */
1551       if (may_be_loop_header
1552 	  && !(e->flags & EDGE_DFS_BACK))
1553 	{
1554 	  ei_next (&ei);
1555 	  continue;
1556 	}
1557 
1558       for (i = 0; i < reg_use_count; i++)
1559 	{
1560 	  rtx reg_used = reg_use_table[i];
1561 	  unsigned int regno = REGNO (reg_used);
1562 	  basic_block dest, old_dest;
1563 	  struct expr *set;
1564 	  rtx src, new_rtx;
1565 
1566 	  set = find_bypass_set (regno, e->src->index);
1567 
1568 	  if (! set)
1569 	    continue;
1570 
1571 	  /* Check the data flow is valid after edge insertions.  */
1572 	  if (e->insns.r && reg_killed_on_edge (reg_used, e))
1573 	    continue;
1574 
1575 	  src = SET_SRC (pc_set (jump));
1576 
1577 	  if (setcc != NULL)
1578 	    src = simplify_replace_rtx (src,
1579 					SET_DEST (PATTERN (setcc)),
1580 					SET_SRC (PATTERN (setcc)));
1581 
1582 	  new_rtx = simplify_replace_rtx (src, reg_used, set->src);
1583 
1584 	  /* Jump bypassing may have already placed instructions on
1585 	     edges of the CFG.  We can't bypass an outgoing edge that
1586 	     has instructions associated with it, as these insns won't
1587 	     get executed if the incoming edge is redirected.  */
1588 	  if (new_rtx == pc_rtx)
1589 	    {
1590 	      edest = FALLTHRU_EDGE (bb);
1591 	      dest = edest->insns.r ? NULL : edest->dest;
1592 	    }
1593 	  else if (GET_CODE (new_rtx) == LABEL_REF)
1594 	    {
1595 	      dest = BLOCK_FOR_INSN (XEXP (new_rtx, 0));
1596 	      /* Don't bypass edges containing instructions.  */
1597 	      edest = find_edge (bb, dest);
1598 	      if (edest && edest->insns.r)
1599 		dest = NULL;
1600 	    }
1601 	  else
1602 	    dest = NULL;
1603 
1604 	  /* Avoid unification of the edge with other edges from original
1605 	     branch.  We would end up emitting the instruction on "both"
1606 	     edges.  */
1607 	  if (dest && setcc && !CC0_P (SET_DEST (PATTERN (setcc)))
1608 	      && find_edge (e->src, dest))
1609 	    dest = NULL;
1610 
1611 	  old_dest = e->dest;
1612 	  if (dest != NULL
1613 	      && dest != old_dest
1614 	      && dest != EXIT_BLOCK_PTR)
1615             {
1616 	      redirect_edge_and_branch_force (e, dest);
1617 
1618 	      /* Copy the register setter to the redirected edge.
1619 		 Don't copy CC0 setters, as CC0 is dead after jump.  */
1620 	      if (setcc)
1621 		{
1622 		  rtx pat = PATTERN (setcc);
1623 		  if (!CC0_P (SET_DEST (pat)))
1624 		    insert_insn_on_edge (copy_insn (pat), e);
1625 		}
1626 
1627 	      if (dump_file != NULL)
1628 		{
1629 		  fprintf (dump_file, "JUMP-BYPASS: Proved reg %d "
1630 				      "in jump_insn %d equals constant ",
1631 			   regno, INSN_UID (jump));
1632 		  print_rtl (dump_file, set->src);
1633 		  fprintf (dump_file, "\n\t     when BB %d is entered from "
1634 				      "BB %d.  Redirect edge %d->%d to %d.\n",
1635 			   old_dest->index, e->src->index, e->src->index,
1636 			   old_dest->index, dest->index);
1637 		}
1638 	      change = 1;
1639 	      removed_p = 1;
1640 	      break;
1641 	    }
1642 	}
1643       if (!removed_p)
1644 	ei_next (&ei);
1645     }
1646   return change;
1647 }
1648 
1649 /* Find basic blocks with more than one predecessor that only contain a
1650    single conditional jump.  If the result of the comparison is known at
1651    compile-time from any incoming edge, redirect that edge to the
1652    appropriate target.  Return nonzero if a change was made.
1653 
1654    This function is now mis-named, because we also handle indirect jumps.  */
1655 
1656 static int
1657 bypass_conditional_jumps (void)
1658 {
1659   basic_block bb;
1660   int changed;
1661   rtx setcc;
1662   rtx insn;
1663   rtx dest;
1664 
1665   /* Note we start at block 1.  */
1666   if (ENTRY_BLOCK_PTR->next_bb == EXIT_BLOCK_PTR)
1667     return 0;
1668 
1669   bypass_last_basic_block = last_basic_block;
1670   mark_dfs_back_edges ();
1671 
1672   changed = 0;
1673   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb->next_bb,
1674 		  EXIT_BLOCK_PTR, next_bb)
1675     {
1676       /* Check for more than one predecessor.  */
1677       if (!single_pred_p (bb))
1678 	{
1679 	  setcc = NULL_RTX;
1680 	  FOR_BB_INSNS (bb, insn)
1681 	    if (DEBUG_INSN_P (insn))
1682 	      continue;
1683 	    else if (NONJUMP_INSN_P (insn))
1684 	      {
1685 		if (setcc)
1686 		  break;
1687 		if (GET_CODE (PATTERN (insn)) != SET)
1688 		  break;
1689 
1690 		dest = SET_DEST (PATTERN (insn));
1691 		if (REG_P (dest) || CC0_P (dest))
1692 		  setcc = insn;
1693 		else
1694 		  break;
1695 	      }
1696 	    else if (JUMP_P (insn))
1697 	      {
1698 		if ((any_condjump_p (insn) || computed_jump_p (insn))
1699 		    && onlyjump_p (insn))
1700 		  changed |= bypass_block (bb, setcc, insn);
1701 		break;
1702 	      }
1703 	    else if (INSN_P (insn))
1704 	      break;
1705 	}
1706     }
1707 
1708   /* If we bypassed any register setting insns, we inserted a
1709      copy on the redirected edge.  These need to be committed.  */
1710   if (changed)
1711     commit_edge_insertions ();
1712 
1713   return changed;
1714 }
1715 
1716 /* Return true if the graph is too expensive to optimize.  PASS is the
1717    optimization about to be performed.  */
1718 
1719 static bool
1720 is_too_expensive (const char *pass)
1721 {
1722   /* Trying to perform global optimizations on flow graphs which have
1723      a high connectivity will take a long time and is unlikely to be
1724      particularly useful.
1725 
1726      In normal circumstances a cfg should have about twice as many
1727      edges as blocks.  But we do not want to punish small functions
1728      which have a couple switch statements.  Rather than simply
1729      threshold the number of blocks, uses something with a more
1730      graceful degradation.  */
1731   if (n_edges > 20000 + n_basic_blocks * 4)
1732     {
1733       warning (OPT_Wdisabled_optimization,
1734 	       "%s: %d basic blocks and %d edges/basic block",
1735 	       pass, n_basic_blocks, n_edges / n_basic_blocks);
1736 
1737       return true;
1738     }
1739 
1740   /* If allocating memory for the cprop bitmap would take up too much
1741      storage it's better just to disable the optimization.  */
1742   if ((n_basic_blocks
1743        * SBITMAP_SET_SIZE (max_reg_num ())
1744        * sizeof (SBITMAP_ELT_TYPE)) > MAX_GCSE_MEMORY)
1745     {
1746       warning (OPT_Wdisabled_optimization,
1747 	       "%s: %d basic blocks and %d registers",
1748 	       pass, n_basic_blocks, max_reg_num ());
1749 
1750       return true;
1751     }
1752 
1753   return false;
1754 }
1755 
1756 /* Main function for the CPROP pass.  */
1757 
1758 static int
1759 one_cprop_pass (void)
1760 {
1761   int i;
1762   int changed = 0;
1763 
1764   /* Return if there's nothing to do, or it is too expensive.  */
1765   if (n_basic_blocks <= NUM_FIXED_BLOCKS + 1
1766       || is_too_expensive (_ ("const/copy propagation disabled")))
1767     return 0;
1768 
1769   global_const_prop_count = local_const_prop_count = 0;
1770   global_copy_prop_count = local_copy_prop_count = 0;
1771 
1772   bytes_used = 0;
1773   gcc_obstack_init (&cprop_obstack);
1774 
1775   /* Do a local const/copy propagation pass first.  The global pass
1776      only handles global opportunities.
1777      If the local pass changes something, remove any unreachable blocks
1778      because the CPROP global dataflow analysis may get into infinite
1779      loops for CFGs with unreachable blocks.
1780 
1781      FIXME: This local pass should not be necessary after CSE (but for
1782 	    some reason it still is).  It is also (proven) not necessary
1783 	    to run the local pass right after FWPWOP.
1784 
1785      FIXME: The global analysis would not get into infinite loops if it
1786 	    would use the DF solver (via df_simple_dataflow) instead of
1787 	    the solver implemented in this file.  */
1788   changed |= local_cprop_pass ();
1789   if (changed)
1790     delete_unreachable_blocks ();
1791 
1792   /* Determine implicit sets.  This may change the CFG (split critical
1793      edges if that exposes an implicit set).
1794      Note that find_implicit_sets() does not rely on up-to-date DF caches
1795      so that we do not have to re-run df_analyze() even if local CPROP
1796      changed something.
1797      ??? This could run earlier so that any uncovered implicit sets
1798 	 sets could be exploited in local_cprop_pass() also.  Later.  */
1799   changed |= find_implicit_sets ();
1800 
1801   /* If local_cprop_pass() or find_implicit_sets() changed something,
1802      run df_analyze() to bring all insn caches up-to-date, and to take
1803      new basic blocks from edge splitting on the DF radar.
1804      NB: This also runs the fast DCE pass, because execute_rtl_cprop
1805      sets DF_LR_RUN_DCE.  */
1806   if (changed)
1807     df_analyze ();
1808 
1809   /* Initialize implicit_set_indexes array.  */
1810   implicit_set_indexes = XNEWVEC (int, last_basic_block);
1811   for (i = 0; i < last_basic_block; i++)
1812     implicit_set_indexes[i] = -1;
1813 
1814   alloc_hash_table (&set_hash_table);
1815   compute_hash_table (&set_hash_table);
1816 
1817   /* Free implicit_sets before peak usage.  */
1818   free (implicit_sets);
1819   implicit_sets = NULL;
1820 
1821   if (dump_file)
1822     dump_hash_table (dump_file, "SET", &set_hash_table);
1823   if (set_hash_table.n_elems > 0)
1824     {
1825       basic_block bb;
1826       rtx insn;
1827 
1828       alloc_cprop_mem (last_basic_block, set_hash_table.n_elems);
1829       compute_cprop_data ();
1830 
1831       free (implicit_set_indexes);
1832       implicit_set_indexes = NULL;
1833 
1834       /* Allocate vars to track sets of regs.  */
1835       reg_set_bitmap = ALLOC_REG_SET (NULL);
1836 
1837       FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb->next_bb, EXIT_BLOCK_PTR,
1838 		      next_bb)
1839 	{
1840 	  /* Reset tables used to keep track of what's still valid [since
1841 	     the start of the block].  */
1842 	  reset_opr_set_tables ();
1843 
1844 	  FOR_BB_INSNS (bb, insn)
1845 	    if (INSN_P (insn))
1846 	      {
1847 		changed |= cprop_insn (insn);
1848 
1849 		/* Keep track of everything modified by this insn.  */
1850 		/* ??? Need to be careful w.r.t. mods done to INSN.
1851 		       Don't call mark_oprs_set if we turned the
1852 		       insn into a NOTE, or deleted the insn.  */
1853 		if (! NOTE_P (insn) && ! INSN_DELETED_P (insn))
1854 		  mark_oprs_set (insn);
1855 	      }
1856 	}
1857 
1858       changed |= bypass_conditional_jumps ();
1859 
1860       FREE_REG_SET (reg_set_bitmap);
1861       free_cprop_mem ();
1862     }
1863   else
1864     {
1865       free (implicit_set_indexes);
1866       implicit_set_indexes = NULL;
1867     }
1868 
1869   free_hash_table (&set_hash_table);
1870   obstack_free (&cprop_obstack, NULL);
1871 
1872   if (dump_file)
1873     {
1874       fprintf (dump_file, "CPROP of %s, %d basic blocks, %d bytes needed, ",
1875 	       current_function_name (), n_basic_blocks, bytes_used);
1876       fprintf (dump_file, "%d local const props, %d local copy props, ",
1877 	       local_const_prop_count, local_copy_prop_count);
1878       fprintf (dump_file, "%d global const props, %d global copy props\n\n",
1879 	       global_const_prop_count, global_copy_prop_count);
1880     }
1881 
1882   return changed;
1883 }
1884 
1885 /* All the passes implemented in this file.  Each pass has its
1886    own gate and execute function, and at the end of the file a
1887    pass definition for passes.c.
1888 
1889    We do not construct an accurate cfg in functions which call
1890    setjmp, so none of these passes runs if the function calls
1891    setjmp.
1892    FIXME: Should just handle setjmp via REG_SETJMP notes.  */
1893 
1894 static bool
1895 gate_rtl_cprop (void)
1896 {
1897   return optimize > 0 && flag_gcse
1898     && !cfun->calls_setjmp
1899     && dbg_cnt (cprop);
1900 }
1901 
1902 static unsigned int
1903 execute_rtl_cprop (void)
1904 {
1905   int changed;
1906   delete_unreachable_blocks ();
1907   df_set_flags (DF_LR_RUN_DCE);
1908   df_analyze ();
1909   changed = one_cprop_pass ();
1910   flag_rerun_cse_after_global_opts |= changed;
1911   if (changed)
1912     cleanup_cfg (CLEANUP_CFG_CHANGED);
1913   return 0;
1914 }
1915 
1916 struct rtl_opt_pass pass_rtl_cprop =
1917 {
1918  {
1919   RTL_PASS,
1920   "cprop",                              /* name */
1921   OPTGROUP_NONE,                        /* optinfo_flags */
1922   gate_rtl_cprop,                       /* gate */
1923   execute_rtl_cprop,  			/* execute */
1924   NULL,                                 /* sub */
1925   NULL,                                 /* next */
1926   0,                                    /* static_pass_number */
1927   TV_CPROP,                             /* tv_id */
1928   PROP_cfglayout,                       /* properties_required */
1929   0,                                    /* properties_provided */
1930   0,                                    /* properties_destroyed */
1931   0,                                    /* todo_flags_start */
1932   TODO_df_finish | TODO_verify_rtl_sharing |
1933   TODO_verify_flow | TODO_ggc_collect   /* todo_flags_finish */
1934  }
1935 };
1936