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