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