config.gcc (powerpc*-*-*): Add support for a new configure option --with-advance...
[official-gcc.git] / gcc / postreload-gcse.c
blob21ac6cd35f9d494e497525474ace61fce038ed57
1 /* Post reload partially redundant load elimination
2 Copyright (C) 2004-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "diagnostic-core.h"
26 #include "hash-table.h"
27 #include "rtl.h"
28 #include "hash-set.h"
29 #include "machmode.h"
30 #include "vec.h"
31 #include "double-int.h"
32 #include "input.h"
33 #include "alias.h"
34 #include "symtab.h"
35 #include "wide-int.h"
36 #include "inchash.h"
37 #include "tree.h"
38 #include "tm_p.h"
39 #include "regs.h"
40 #include "hard-reg-set.h"
41 #include "flags.h"
42 #include "insn-config.h"
43 #include "recog.h"
44 #include "predict.h"
45 #include "function.h"
46 #include "dominance.h"
47 #include "cfg.h"
48 #include "cfgrtl.h"
49 #include "basic-block.h"
50 #include "profile.h"
51 #include "hashtab.h"
52 #include "statistics.h"
53 #include "real.h"
54 #include "fixed-value.h"
55 #include "expmed.h"
56 #include "dojump.h"
57 #include "explow.h"
58 #include "calls.h"
59 #include "emit-rtl.h"
60 #include "varasm.h"
61 #include "stmt.h"
62 #include "expr.h"
63 #include "except.h"
64 #include "intl.h"
65 #include "obstack.h"
66 #include "params.h"
67 #include "target.h"
68 #include "tree-pass.h"
69 #include "dbgcnt.h"
70 #include "df.h"
71 #include "gcse-common.h"
73 /* The following code implements gcse after reload, the purpose of this
74 pass is to cleanup redundant loads generated by reload and other
75 optimizations that come after gcse. It searches for simple inter-block
76 redundancies and tries to eliminate them by adding moves and loads
77 in cold places.
79 Perform partially redundant load elimination, try to eliminate redundant
80 loads created by the reload pass. We try to look for full or partial
81 redundant loads fed by one or more loads/stores in predecessor BBs,
82 and try adding loads to make them fully redundant. We also check if
83 it's worth adding loads to be able to delete the redundant load.
85 Algorithm:
86 1. Build available expressions hash table:
87 For each load/store instruction, if the loaded/stored memory didn't
88 change until the end of the basic block add this memory expression to
89 the hash table.
90 2. Perform Redundancy elimination:
91 For each load instruction do the following:
92 perform partial redundancy elimination, check if it's worth adding
93 loads to make the load fully redundant. If so add loads and
94 register copies and delete the load.
95 3. Delete instructions made redundant in step 2.
97 Future enhancement:
98 If the loaded register is used/defined between load and some store,
99 look for some other free register between load and all its stores,
100 and replace the load with a copy from this register to the loaded
101 register.
105 /* Keep statistics of this pass. */
106 static struct
108 int moves_inserted;
109 int copies_inserted;
110 int insns_deleted;
111 } stats;
113 /* We need to keep a hash table of expressions. The table entries are of
114 type 'struct expr', and for each expression there is a single linked
115 list of occurrences. */
117 /* Expression elements in the hash table. */
118 struct expr
120 /* The expression (SET_SRC for expressions, PATTERN for assignments). */
121 rtx expr;
123 /* The same hash for this entry. */
124 hashval_t hash;
126 /* Index in the transparent bitmaps. */
127 unsigned int bitmap_index;
129 /* List of available occurrence in basic blocks in the function. */
130 struct occr *avail_occr;
133 /* Hashtable helpers. */
135 struct expr_hasher : typed_noop_remove <expr>
137 typedef expr *value_type;
138 typedef expr *compare_type;
139 static inline hashval_t hash (const expr *);
140 static inline bool equal (const expr *, const expr *);
144 /* Hash expression X.
145 DO_NOT_RECORD_P is a boolean indicating if a volatile operand is found
146 or if the expression contains something we don't want to insert in the
147 table. */
149 static hashval_t
150 hash_expr (rtx x, int *do_not_record_p)
152 *do_not_record_p = 0;
153 return hash_rtx (x, GET_MODE (x), do_not_record_p,
154 NULL, /*have_reg_qty=*/false);
157 /* Callback for hashtab.
158 Return the hash value for expression EXP. We don't actually hash
159 here, we just return the cached hash value. */
161 inline hashval_t
162 expr_hasher::hash (const expr *exp)
164 return exp->hash;
167 /* Callback for hashtab.
168 Return nonzero if exp1 is equivalent to exp2. */
170 inline bool
171 expr_hasher::equal (const expr *exp1, const expr *exp2)
173 int equiv_p = exp_equiv_p (exp1->expr, exp2->expr, 0, true);
175 gcc_assert (!equiv_p || exp1->hash == exp2->hash);
176 return equiv_p;
179 /* The table itself. */
180 static hash_table<expr_hasher> *expr_table;
183 static struct obstack expr_obstack;
185 /* Occurrence of an expression.
186 There is at most one occurrence per basic block. If a pattern appears
187 more than once, the last appearance is used. */
189 struct occr
191 /* Next occurrence of this expression. */
192 struct occr *next;
193 /* The insn that computes the expression. */
194 rtx_insn *insn;
195 /* Nonzero if this [anticipatable] occurrence has been deleted. */
196 char deleted_p;
199 static struct obstack occr_obstack;
201 /* The following structure holds the information about the occurrences of
202 the redundant instructions. */
203 struct unoccr
205 struct unoccr *next;
206 edge pred;
207 rtx_insn *insn;
210 static struct obstack unoccr_obstack;
212 /* Array where each element is the CUID if the insn that last set the hard
213 register with the number of the element, since the start of the current
214 basic block.
216 This array is used during the building of the hash table (step 1) to
217 determine if a reg is killed before the end of a basic block.
219 It is also used when eliminating partial redundancies (step 2) to see
220 if a reg was modified since the start of a basic block. */
221 static int *reg_avail_info;
223 /* A list of insns that may modify memory within the current basic block. */
224 struct modifies_mem
226 rtx_insn *insn;
227 struct modifies_mem *next;
229 static struct modifies_mem *modifies_mem_list;
231 /* The modifies_mem structs also go on an obstack, only this obstack is
232 freed each time after completing the analysis or transformations on
233 a basic block. So we allocate a dummy modifies_mem_obstack_bottom
234 object on the obstack to keep track of the bottom of the obstack. */
235 static struct obstack modifies_mem_obstack;
236 static struct modifies_mem *modifies_mem_obstack_bottom;
238 /* Mapping of insn UIDs to CUIDs.
239 CUIDs are like UIDs except they increase monotonically in each basic
240 block, have no gaps, and only apply to real insns. */
241 static int *uid_cuid;
242 #define INSN_CUID(INSN) (uid_cuid[INSN_UID (INSN)])
244 /* Bitmap of blocks which have memory stores. */
245 static bitmap modify_mem_list_set;
247 /* Bitmap of blocks which have calls. */
248 static bitmap blocks_with_calls;
250 /* Vector indexed by block # with a list of all the insns that
251 modify memory within the block. */
252 static vec<rtx_insn *> *modify_mem_list;
254 /* Vector indexed by block # with a canonicalized list of insns
255 that modify memory in the block. */
256 static vec<modify_pair> *canon_modify_mem_list;
258 /* Vector of simple bitmaps indexed by block number. Each component sbitmap
259 indicates which expressions are transparent through the block. */
260 static sbitmap *transp;
263 /* Helpers for memory allocation/freeing. */
264 static void alloc_mem (void);
265 static void free_mem (void);
267 /* Support for hash table construction and transformations. */
268 static bool oprs_unchanged_p (rtx, rtx_insn *, bool);
269 static void record_last_reg_set_info (rtx_insn *, rtx);
270 static void record_last_reg_set_info_regno (rtx_insn *, int);
271 static void record_last_mem_set_info (rtx_insn *);
272 static void record_last_set_info (rtx, const_rtx, void *);
273 static void record_opr_changes (rtx_insn *);
275 static void find_mem_conflicts (rtx, const_rtx, void *);
276 static int load_killed_in_block_p (int, rtx, bool);
277 static void reset_opr_set_tables (void);
279 /* Hash table support. */
280 static hashval_t hash_expr (rtx, int *);
281 static void insert_expr_in_table (rtx, rtx_insn *);
282 static struct expr *lookup_expr_in_table (rtx);
283 static void dump_hash_table (FILE *);
285 /* Helpers for eliminate_partially_redundant_load. */
286 static bool reg_killed_on_edge (rtx, edge);
287 static bool reg_used_on_edge (rtx, edge);
289 static rtx get_avail_load_store_reg (rtx_insn *);
291 static bool bb_has_well_behaved_predecessors (basic_block);
292 static struct occr* get_bb_avail_insn (basic_block, struct occr *, int);
293 static void hash_scan_set (rtx_insn *);
294 static void compute_hash_table (void);
296 /* The work horses of this pass. */
297 static void eliminate_partially_redundant_load (basic_block,
298 rtx_insn *,
299 struct expr *);
300 static void eliminate_partially_redundant_loads (void);
303 /* Allocate memory for the CUID mapping array and register/memory
304 tracking tables. */
306 static void
307 alloc_mem (void)
309 int i;
310 basic_block bb;
311 rtx_insn *insn;
313 /* Find the largest UID and create a mapping from UIDs to CUIDs. */
314 uid_cuid = XCNEWVEC (int, get_max_uid () + 1);
315 i = 1;
316 FOR_EACH_BB_FN (bb, cfun)
317 FOR_BB_INSNS (bb, insn)
319 if (INSN_P (insn))
320 uid_cuid[INSN_UID (insn)] = i++;
321 else
322 uid_cuid[INSN_UID (insn)] = i;
325 /* Allocate the available expressions hash table. We don't want to
326 make the hash table too small, but unnecessarily making it too large
327 also doesn't help. The i/4 is a gcse.c relic, and seems like a
328 reasonable choice. */
329 expr_table = new hash_table<expr_hasher> (MAX (i / 4, 13));
331 /* We allocate everything on obstacks because we often can roll back
332 the whole obstack to some point. Freeing obstacks is very fast. */
333 gcc_obstack_init (&expr_obstack);
334 gcc_obstack_init (&occr_obstack);
335 gcc_obstack_init (&unoccr_obstack);
336 gcc_obstack_init (&modifies_mem_obstack);
338 /* Working array used to track the last set for each register
339 in the current block. */
340 reg_avail_info = (int *) xmalloc (FIRST_PSEUDO_REGISTER * sizeof (int));
342 /* Put a dummy modifies_mem object on the modifies_mem_obstack, so we
343 can roll it back in reset_opr_set_tables. */
344 modifies_mem_obstack_bottom =
345 (struct modifies_mem *) obstack_alloc (&modifies_mem_obstack,
346 sizeof (struct modifies_mem));
348 blocks_with_calls = BITMAP_ALLOC (NULL);
349 modify_mem_list_set = BITMAP_ALLOC (NULL);
351 modify_mem_list = (vec_rtx_heap *) xcalloc (last_basic_block_for_fn (cfun),
352 sizeof (vec_rtx_heap));
353 canon_modify_mem_list
354 = (vec_modify_pair_heap *) xcalloc (last_basic_block_for_fn (cfun),
355 sizeof (vec_modify_pair_heap));
358 /* Free memory allocated by alloc_mem. */
360 static void
361 free_mem (void)
363 free (uid_cuid);
365 delete expr_table;
366 expr_table = NULL;
368 obstack_free (&expr_obstack, NULL);
369 obstack_free (&occr_obstack, NULL);
370 obstack_free (&unoccr_obstack, NULL);
371 obstack_free (&modifies_mem_obstack, NULL);
373 unsigned i;
374 bitmap_iterator bi;
375 EXECUTE_IF_SET_IN_BITMAP (modify_mem_list_set, 0, i, bi)
377 modify_mem_list[i].release ();
378 canon_modify_mem_list[i].release ();
381 BITMAP_FREE (blocks_with_calls);
382 BITMAP_FREE (modify_mem_list_set);
383 free (reg_avail_info);
387 /* Insert expression X in INSN in the hash TABLE.
388 If it is already present, record it as the last occurrence in INSN's
389 basic block. */
391 static void
392 insert_expr_in_table (rtx x, rtx_insn *insn)
394 int do_not_record_p;
395 hashval_t hash;
396 struct expr *cur_expr, **slot;
397 struct occr *avail_occr, *last_occr = NULL;
399 hash = hash_expr (x, &do_not_record_p);
401 /* Do not insert expression in the table if it contains volatile operands,
402 or if hash_expr determines the expression is something we don't want
403 to or can't handle. */
404 if (do_not_record_p)
405 return;
407 /* We anticipate that redundant expressions are rare, so for convenience
408 allocate a new hash table element here already and set its fields.
409 If we don't do this, we need a hack with a static struct expr. Anyway,
410 obstack_free is really fast and one more obstack_alloc doesn't hurt if
411 we're going to see more expressions later on. */
412 cur_expr = (struct expr *) obstack_alloc (&expr_obstack,
413 sizeof (struct expr));
414 cur_expr->expr = x;
415 cur_expr->hash = hash;
416 cur_expr->avail_occr = NULL;
418 slot = expr_table->find_slot_with_hash (cur_expr, hash, INSERT);
420 if (! (*slot))
422 /* The expression isn't found, so insert it. */
423 *slot = cur_expr;
425 /* Anytime we add an entry to the table, record the index
426 of the new entry. The bitmap index starts counting
427 at zero. */
428 cur_expr->bitmap_index = expr_table->elements () - 1;
430 else
432 /* The expression is already in the table, so roll back the
433 obstack and use the existing table entry. */
434 obstack_free (&expr_obstack, cur_expr);
435 cur_expr = *slot;
438 /* Search for another occurrence in the same basic block. */
439 avail_occr = cur_expr->avail_occr;
440 while (avail_occr
441 && BLOCK_FOR_INSN (avail_occr->insn) != BLOCK_FOR_INSN (insn))
443 /* If an occurrence isn't found, save a pointer to the end of
444 the list. */
445 last_occr = avail_occr;
446 avail_occr = avail_occr->next;
449 if (avail_occr)
450 /* Found another instance of the expression in the same basic block.
451 Prefer this occurrence to the currently recorded one. We want
452 the last one in the block and the block is scanned from start
453 to end. */
454 avail_occr->insn = insn;
455 else
457 /* First occurrence of this expression in this basic block. */
458 avail_occr = (struct occr *) obstack_alloc (&occr_obstack,
459 sizeof (struct occr));
461 /* First occurrence of this expression in any block? */
462 if (cur_expr->avail_occr == NULL)
463 cur_expr->avail_occr = avail_occr;
464 else
465 last_occr->next = avail_occr;
467 avail_occr->insn = insn;
468 avail_occr->next = NULL;
469 avail_occr->deleted_p = 0;
474 /* Lookup pattern PAT in the expression hash table.
475 The result is a pointer to the table entry, or NULL if not found. */
477 static struct expr *
478 lookup_expr_in_table (rtx pat)
480 int do_not_record_p;
481 struct expr **slot, *tmp_expr;
482 hashval_t hash = hash_expr (pat, &do_not_record_p);
484 if (do_not_record_p)
485 return NULL;
487 tmp_expr = (struct expr *) obstack_alloc (&expr_obstack,
488 sizeof (struct expr));
489 tmp_expr->expr = pat;
490 tmp_expr->hash = hash;
491 tmp_expr->avail_occr = NULL;
493 slot = expr_table->find_slot_with_hash (tmp_expr, hash, INSERT);
494 obstack_free (&expr_obstack, tmp_expr);
496 if (!slot)
497 return NULL;
498 else
499 return (*slot);
503 /* Dump all expressions and occurrences that are currently in the
504 expression hash table to FILE. */
506 /* This helper is called via htab_traverse. */
508 dump_expr_hash_table_entry (expr **slot, FILE *file)
510 struct expr *exprs = *slot;
511 struct occr *occr;
513 fprintf (file, "expr: ");
514 print_rtl (file, exprs->expr);
515 fprintf (file,"\nhashcode: %u\n", exprs->hash);
516 fprintf (file,"list of occurrences:\n");
517 occr = exprs->avail_occr;
518 while (occr)
520 rtx_insn *insn = occr->insn;
521 print_rtl_single (file, insn);
522 fprintf (file, "\n");
523 occr = occr->next;
525 fprintf (file, "\n");
526 return 1;
529 static void
530 dump_hash_table (FILE *file)
532 fprintf (file, "\n\nexpression hash table\n");
533 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
534 (long) expr_table->size (),
535 (long) expr_table->elements (),
536 expr_table->collisions ());
537 if (expr_table->elements () > 0)
539 fprintf (file, "\n\ntable entries:\n");
540 expr_table->traverse <FILE *, dump_expr_hash_table_entry> (file);
542 fprintf (file, "\n");
545 /* Return true if register X is recorded as being set by an instruction
546 whose CUID is greater than the one given. */
548 static bool
549 reg_changed_after_insn_p (rtx x, int cuid)
551 unsigned int regno, end_regno;
553 regno = REGNO (x);
554 end_regno = END_REGNO (x);
556 if (reg_avail_info[regno] > cuid)
557 return true;
558 while (++regno < end_regno);
559 return false;
562 /* Return nonzero if the operands of expression X are unchanged
563 1) from the start of INSN's basic block up to but not including INSN
564 if AFTER_INSN is false, or
565 2) from INSN to the end of INSN's basic block if AFTER_INSN is true. */
567 static bool
568 oprs_unchanged_p (rtx x, rtx_insn *insn, bool after_insn)
570 int i, j;
571 enum rtx_code code;
572 const char *fmt;
574 if (x == 0)
575 return 1;
577 code = GET_CODE (x);
578 switch (code)
580 case REG:
581 /* We are called after register allocation. */
582 gcc_assert (REGNO (x) < FIRST_PSEUDO_REGISTER);
583 if (after_insn)
584 return !reg_changed_after_insn_p (x, INSN_CUID (insn) - 1);
585 else
586 return !reg_changed_after_insn_p (x, 0);
588 case MEM:
589 if (load_killed_in_block_p (INSN_CUID (insn), x, after_insn))
590 return 0;
591 else
592 return oprs_unchanged_p (XEXP (x, 0), insn, after_insn);
594 case PC:
595 case CC0: /*FIXME*/
596 case CONST:
597 CASE_CONST_ANY:
598 case SYMBOL_REF:
599 case LABEL_REF:
600 case ADDR_VEC:
601 case ADDR_DIFF_VEC:
602 return 1;
604 case PRE_DEC:
605 case PRE_INC:
606 case POST_DEC:
607 case POST_INC:
608 case PRE_MODIFY:
609 case POST_MODIFY:
610 if (after_insn)
611 return 0;
612 break;
614 default:
615 break;
618 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
620 if (fmt[i] == 'e')
622 if (! oprs_unchanged_p (XEXP (x, i), insn, after_insn))
623 return 0;
625 else if (fmt[i] == 'E')
626 for (j = 0; j < XVECLEN (x, i); j++)
627 if (! oprs_unchanged_p (XVECEXP (x, i, j), insn, after_insn))
628 return 0;
631 return 1;
635 /* Used for communication between find_mem_conflicts and
636 load_killed_in_block_p. Nonzero if find_mem_conflicts finds a
637 conflict between two memory references.
638 This is a bit of a hack to work around the limitations of note_stores. */
639 static int mems_conflict_p;
641 /* DEST is the output of an instruction. If it is a memory reference, and
642 possibly conflicts with the load found in DATA, then set mems_conflict_p
643 to a nonzero value. */
645 static void
646 find_mem_conflicts (rtx dest, const_rtx setter ATTRIBUTE_UNUSED,
647 void *data)
649 rtx mem_op = (rtx) data;
651 while (GET_CODE (dest) == SUBREG
652 || GET_CODE (dest) == ZERO_EXTRACT
653 || GET_CODE (dest) == STRICT_LOW_PART)
654 dest = XEXP (dest, 0);
656 /* If DEST is not a MEM, then it will not conflict with the load. Note
657 that function calls are assumed to clobber memory, but are handled
658 elsewhere. */
659 if (! MEM_P (dest))
660 return;
662 if (true_dependence (dest, GET_MODE (dest), mem_op))
663 mems_conflict_p = 1;
667 /* Return nonzero if the expression in X (a memory reference) is killed
668 in the current basic block before (if AFTER_INSN is false) or after
669 (if AFTER_INSN is true) the insn with the CUID in UID_LIMIT.
671 This function assumes that the modifies_mem table is flushed when
672 the hash table construction or redundancy elimination phases start
673 processing a new basic block. */
675 static int
676 load_killed_in_block_p (int uid_limit, rtx x, bool after_insn)
678 struct modifies_mem *list_entry = modifies_mem_list;
680 while (list_entry)
682 rtx_insn *setter = list_entry->insn;
684 /* Ignore entries in the list that do not apply. */
685 if ((after_insn
686 && INSN_CUID (setter) < uid_limit)
687 || (! after_insn
688 && INSN_CUID (setter) > uid_limit))
690 list_entry = list_entry->next;
691 continue;
694 /* If SETTER is a call everything is clobbered. Note that calls
695 to pure functions are never put on the list, so we need not
696 worry about them. */
697 if (CALL_P (setter))
698 return 1;
700 /* SETTER must be an insn of some kind that sets memory. Call
701 note_stores to examine each hunk of memory that is modified.
702 It will set mems_conflict_p to nonzero if there may be a
703 conflict between X and SETTER. */
704 mems_conflict_p = 0;
705 note_stores (PATTERN (setter), find_mem_conflicts, x);
706 if (mems_conflict_p)
707 return 1;
709 list_entry = list_entry->next;
711 return 0;
715 /* Record register first/last/block set information for REGNO in INSN. */
717 static inline void
718 record_last_reg_set_info (rtx_insn *insn, rtx reg)
720 unsigned int regno, end_regno;
722 regno = REGNO (reg);
723 end_regno = END_REGNO (reg);
725 reg_avail_info[regno] = INSN_CUID (insn);
726 while (++regno < end_regno);
729 static inline void
730 record_last_reg_set_info_regno (rtx_insn *insn, int regno)
732 reg_avail_info[regno] = INSN_CUID (insn);
736 /* Record memory modification information for INSN. We do not actually care
737 about the memory location(s) that are set, or even how they are set (consider
738 a CALL_INSN). We merely need to record which insns modify memory. */
740 static void
741 record_last_mem_set_info (rtx_insn *insn)
743 struct modifies_mem *list_entry;
745 list_entry = (struct modifies_mem *) obstack_alloc (&modifies_mem_obstack,
746 sizeof (struct modifies_mem));
747 list_entry->insn = insn;
748 list_entry->next = modifies_mem_list;
749 modifies_mem_list = list_entry;
751 record_last_mem_set_info_common (insn, modify_mem_list,
752 canon_modify_mem_list,
753 modify_mem_list_set,
754 blocks_with_calls);
757 /* Called from compute_hash_table via note_stores to handle one
758 SET or CLOBBER in an insn. DATA is really the instruction in which
759 the SET is taking place. */
761 static void
762 record_last_set_info (rtx dest, const_rtx setter ATTRIBUTE_UNUSED, void *data)
764 rtx_insn *last_set_insn = (rtx_insn *) data;
766 if (GET_CODE (dest) == SUBREG)
767 dest = SUBREG_REG (dest);
769 if (REG_P (dest))
770 record_last_reg_set_info (last_set_insn, dest);
771 else if (MEM_P (dest))
773 /* Ignore pushes, they don't clobber memory. They may still
774 clobber the stack pointer though. Some targets do argument
775 pushes without adding REG_INC notes. See e.g. PR25196,
776 where a pushsi2 on i386 doesn't have REG_INC notes. Note
777 such changes here too. */
778 if (! push_operand (dest, GET_MODE (dest)))
779 record_last_mem_set_info (last_set_insn);
780 else
781 record_last_reg_set_info_regno (last_set_insn, STACK_POINTER_REGNUM);
786 /* Reset tables used to keep track of what's still available since the
787 start of the block. */
789 static void
790 reset_opr_set_tables (void)
792 memset (reg_avail_info, 0, FIRST_PSEUDO_REGISTER * sizeof (int));
793 obstack_free (&modifies_mem_obstack, modifies_mem_obstack_bottom);
794 modifies_mem_list = NULL;
798 /* Record things set by INSN.
799 This data is used by oprs_unchanged_p. */
801 static void
802 record_opr_changes (rtx_insn *insn)
804 rtx note;
806 /* Find all stores and record them. */
807 note_stores (PATTERN (insn), record_last_set_info, insn);
809 /* Also record autoincremented REGs for this insn as changed. */
810 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
811 if (REG_NOTE_KIND (note) == REG_INC)
812 record_last_reg_set_info (insn, XEXP (note, 0));
814 /* Finally, if this is a call, record all call clobbers. */
815 if (CALL_P (insn))
817 unsigned int regno;
818 rtx link, x;
819 hard_reg_set_iterator hrsi;
820 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call, 0, regno, hrsi)
821 record_last_reg_set_info_regno (insn, regno);
823 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
824 if (GET_CODE (XEXP (link, 0)) == CLOBBER)
826 x = XEXP (XEXP (link, 0), 0);
827 if (REG_P (x))
829 gcc_assert (HARD_REGISTER_P (x));
830 record_last_reg_set_info (insn, x);
834 if (! RTL_CONST_OR_PURE_CALL_P (insn))
835 record_last_mem_set_info (insn);
840 /* Scan the pattern of INSN and add an entry to the hash TABLE.
841 After reload we are interested in loads/stores only. */
843 static void
844 hash_scan_set (rtx_insn *insn)
846 rtx pat = PATTERN (insn);
847 rtx src = SET_SRC (pat);
848 rtx dest = SET_DEST (pat);
850 /* We are only interested in loads and stores. */
851 if (! MEM_P (src) && ! MEM_P (dest))
852 return;
854 /* Don't mess with jumps and nops. */
855 if (JUMP_P (insn) || set_noop_p (pat))
856 return;
858 if (REG_P (dest))
860 if (/* Don't CSE something if we can't do a reg/reg copy. */
861 can_copy_p (GET_MODE (dest))
862 /* Is SET_SRC something we want to gcse? */
863 && general_operand (src, GET_MODE (src))
864 #ifdef STACK_REGS
865 /* Never consider insns touching the register stack. It may
866 create situations that reg-stack cannot handle (e.g. a stack
867 register live across an abnormal edge). */
868 && (REGNO (dest) < FIRST_STACK_REG || REGNO (dest) > LAST_STACK_REG)
869 #endif
870 /* An expression is not available if its operands are
871 subsequently modified, including this insn. */
872 && oprs_unchanged_p (src, insn, true))
874 insert_expr_in_table (src, insn);
877 else if (REG_P (src))
879 /* Only record sets of pseudo-regs in the hash table. */
880 if (/* Don't CSE something if we can't do a reg/reg copy. */
881 can_copy_p (GET_MODE (src))
882 /* Is SET_DEST something we want to gcse? */
883 && general_operand (dest, GET_MODE (dest))
884 #ifdef STACK_REGS
885 /* As above for STACK_REGS. */
886 && (REGNO (src) < FIRST_STACK_REG || REGNO (src) > LAST_STACK_REG)
887 #endif
888 && ! (flag_float_store && FLOAT_MODE_P (GET_MODE (dest)))
889 /* Check if the memory expression is killed after insn. */
890 && ! load_killed_in_block_p (INSN_CUID (insn) + 1, dest, true)
891 && oprs_unchanged_p (XEXP (dest, 0), insn, true))
893 insert_expr_in_table (dest, insn);
899 /* Create hash table of memory expressions available at end of basic
900 blocks. Basically you should think of this hash table as the
901 representation of AVAIL_OUT. This is the set of expressions that
902 is generated in a basic block and not killed before the end of the
903 same basic block. Notice that this is really a local computation. */
905 static void
906 compute_hash_table (void)
908 basic_block bb;
910 FOR_EACH_BB_FN (bb, cfun)
912 rtx_insn *insn;
914 /* First pass over the instructions records information used to
915 determine when registers and memory are last set.
916 Since we compute a "local" AVAIL_OUT, reset the tables that
917 help us keep track of what has been modified since the start
918 of the block. */
919 reset_opr_set_tables ();
920 FOR_BB_INSNS (bb, insn)
922 if (INSN_P (insn))
923 record_opr_changes (insn);
926 /* The next pass actually builds the hash table. */
927 FOR_BB_INSNS (bb, insn)
928 if (INSN_P (insn) && GET_CODE (PATTERN (insn)) == SET)
929 hash_scan_set (insn);
934 /* Check if register REG is killed in any insn waiting to be inserted on
935 edge E. This function is required to check that our data flow analysis
936 is still valid prior to commit_edge_insertions. */
938 static bool
939 reg_killed_on_edge (rtx reg, edge e)
941 rtx_insn *insn;
943 for (insn = e->insns.r; insn; insn = NEXT_INSN (insn))
944 if (INSN_P (insn) && reg_set_p (reg, insn))
945 return true;
947 return false;
950 /* Similar to above - check if register REG is used in any insn waiting
951 to be inserted on edge E.
952 Assumes no such insn can be a CALL_INSN; if so call reg_used_between_p
953 with PREV(insn),NEXT(insn) instead of calling reg_overlap_mentioned_p. */
955 static bool
956 reg_used_on_edge (rtx reg, edge e)
958 rtx_insn *insn;
960 for (insn = e->insns.r; insn; insn = NEXT_INSN (insn))
961 if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
962 return true;
964 return false;
967 /* Return the loaded/stored register of a load/store instruction. */
969 static rtx
970 get_avail_load_store_reg (rtx_insn *insn)
972 if (REG_P (SET_DEST (PATTERN (insn))))
973 /* A load. */
974 return SET_DEST (PATTERN (insn));
975 else
977 /* A store. */
978 gcc_assert (REG_P (SET_SRC (PATTERN (insn))));
979 return SET_SRC (PATTERN (insn));
983 /* Return nonzero if the predecessors of BB are "well behaved". */
985 static bool
986 bb_has_well_behaved_predecessors (basic_block bb)
988 edge pred;
989 edge_iterator ei;
991 if (EDGE_COUNT (bb->preds) == 0)
992 return false;
994 FOR_EACH_EDGE (pred, ei, bb->preds)
996 if ((pred->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (pred))
997 return false;
999 if ((pred->flags & EDGE_ABNORMAL_CALL) && cfun->has_nonlocal_label)
1000 return false;
1002 if (tablejump_p (BB_END (pred->src), NULL, NULL))
1003 return false;
1005 return true;
1009 /* Search for the occurrences of expression in BB. */
1011 static struct occr*
1012 get_bb_avail_insn (basic_block bb, struct occr *orig_occr, int bitmap_index)
1014 struct occr *occr = orig_occr;
1016 for (; occr != NULL; occr = occr->next)
1017 if (BLOCK_FOR_INSN (occr->insn) == bb)
1018 return occr;
1020 /* If we could not find an occurrence in BB, see if BB
1021 has a single predecessor with an occurrence that is
1022 transparent through BB. */
1023 if (single_pred_p (bb)
1024 && bitmap_bit_p (transp[bb->index], bitmap_index)
1025 && (occr = get_bb_avail_insn (single_pred (bb), orig_occr, bitmap_index)))
1027 rtx avail_reg = get_avail_load_store_reg (occr->insn);
1028 if (!reg_set_between_p (avail_reg,
1029 PREV_INSN (BB_HEAD (bb)),
1030 NEXT_INSN (BB_END (bb)))
1031 && !reg_killed_on_edge (avail_reg, single_pred_edge (bb)))
1032 return occr;
1035 return NULL;
1039 /* This helper is called via htab_traverse. */
1041 compute_expr_transp (expr **slot, FILE *dump_file ATTRIBUTE_UNUSED)
1043 struct expr *expr = *slot;
1045 compute_transp (expr->expr, expr->bitmap_index, transp,
1046 blocks_with_calls, modify_mem_list_set,
1047 canon_modify_mem_list);
1048 return 1;
1051 /* This handles the case where several stores feed a partially redundant
1052 load. It checks if the redundancy elimination is possible and if it's
1053 worth it.
1055 Redundancy elimination is possible if,
1056 1) None of the operands of an insn have been modified since the start
1057 of the current basic block.
1058 2) In any predecessor of the current basic block, the same expression
1059 is generated.
1061 See the function body for the heuristics that determine if eliminating
1062 a redundancy is also worth doing, assuming it is possible. */
1064 static void
1065 eliminate_partially_redundant_load (basic_block bb, rtx_insn *insn,
1066 struct expr *expr)
1068 edge pred;
1069 rtx_insn *avail_insn = NULL;
1070 rtx avail_reg;
1071 rtx dest, pat;
1072 struct occr *a_occr;
1073 struct unoccr *occr, *avail_occrs = NULL;
1074 struct unoccr *unoccr, *unavail_occrs = NULL, *rollback_unoccr = NULL;
1075 int npred_ok = 0;
1076 gcov_type ok_count = 0; /* Redundant load execution count. */
1077 gcov_type critical_count = 0; /* Execution count of critical edges. */
1078 edge_iterator ei;
1079 bool critical_edge_split = false;
1081 /* The execution count of the loads to be added to make the
1082 load fully redundant. */
1083 gcov_type not_ok_count = 0;
1084 basic_block pred_bb;
1086 pat = PATTERN (insn);
1087 dest = SET_DEST (pat);
1089 /* Check that the loaded register is not used, set, or killed from the
1090 beginning of the block. */
1091 if (reg_changed_after_insn_p (dest, 0)
1092 || reg_used_between_p (dest, PREV_INSN (BB_HEAD (bb)), insn))
1093 return;
1095 /* Check potential for replacing load with copy for predecessors. */
1096 FOR_EACH_EDGE (pred, ei, bb->preds)
1098 rtx_insn *next_pred_bb_end;
1100 avail_insn = NULL;
1101 avail_reg = NULL_RTX;
1102 pred_bb = pred->src;
1103 for (a_occr = get_bb_avail_insn (pred_bb,
1104 expr->avail_occr,
1105 expr->bitmap_index);
1106 a_occr;
1107 a_occr = get_bb_avail_insn (pred_bb,
1108 a_occr->next,
1109 expr->bitmap_index))
1111 /* Check if the loaded register is not used. */
1112 avail_insn = a_occr->insn;
1113 avail_reg = get_avail_load_store_reg (avail_insn);
1114 gcc_assert (avail_reg);
1116 /* Make sure we can generate a move from register avail_reg to
1117 dest. */
1118 rtx_insn *move = gen_move_insn (copy_rtx (dest),
1119 copy_rtx (avail_reg));
1120 extract_insn (move);
1121 if (! constrain_operands (1, get_preferred_alternatives (insn,
1122 pred_bb))
1123 || reg_killed_on_edge (avail_reg, pred)
1124 || reg_used_on_edge (dest, pred))
1126 avail_insn = NULL;
1127 continue;
1129 next_pred_bb_end = NEXT_INSN (BB_END (BLOCK_FOR_INSN (avail_insn)));
1130 if (!reg_set_between_p (avail_reg, avail_insn, next_pred_bb_end))
1131 /* AVAIL_INSN remains non-null. */
1132 break;
1133 else
1134 avail_insn = NULL;
1137 if (EDGE_CRITICAL_P (pred))
1138 critical_count += pred->count;
1140 if (avail_insn != NULL_RTX)
1142 npred_ok++;
1143 ok_count += pred->count;
1144 if (! set_noop_p (PATTERN (gen_move_insn (copy_rtx (dest),
1145 copy_rtx (avail_reg)))))
1147 /* Check if there is going to be a split. */
1148 if (EDGE_CRITICAL_P (pred))
1149 critical_edge_split = true;
1151 else /* Its a dead move no need to generate. */
1152 continue;
1153 occr = (struct unoccr *) obstack_alloc (&unoccr_obstack,
1154 sizeof (struct unoccr));
1155 occr->insn = avail_insn;
1156 occr->pred = pred;
1157 occr->next = avail_occrs;
1158 avail_occrs = occr;
1159 if (! rollback_unoccr)
1160 rollback_unoccr = occr;
1162 else
1164 /* Adding a load on a critical edge will cause a split. */
1165 if (EDGE_CRITICAL_P (pred))
1166 critical_edge_split = true;
1167 not_ok_count += pred->count;
1168 unoccr = (struct unoccr *) obstack_alloc (&unoccr_obstack,
1169 sizeof (struct unoccr));
1170 unoccr->insn = NULL;
1171 unoccr->pred = pred;
1172 unoccr->next = unavail_occrs;
1173 unavail_occrs = unoccr;
1174 if (! rollback_unoccr)
1175 rollback_unoccr = unoccr;
1179 if (/* No load can be replaced by copy. */
1180 npred_ok == 0
1181 /* Prevent exploding the code. */
1182 || (optimize_bb_for_size_p (bb) && npred_ok > 1)
1183 /* If we don't have profile information we cannot tell if splitting
1184 a critical edge is profitable or not so don't do it. */
1185 || ((! profile_info || ! flag_branch_probabilities
1186 || targetm.cannot_modify_jumps_p ())
1187 && critical_edge_split))
1188 goto cleanup;
1190 /* Check if it's worth applying the partial redundancy elimination. */
1191 if (ok_count < GCSE_AFTER_RELOAD_PARTIAL_FRACTION * not_ok_count)
1192 goto cleanup;
1193 if (ok_count < GCSE_AFTER_RELOAD_CRITICAL_FRACTION * critical_count)
1194 goto cleanup;
1196 /* Generate moves to the loaded register from where
1197 the memory is available. */
1198 for (occr = avail_occrs; occr; occr = occr->next)
1200 avail_insn = occr->insn;
1201 pred = occr->pred;
1202 /* Set avail_reg to be the register having the value of the
1203 memory. */
1204 avail_reg = get_avail_load_store_reg (avail_insn);
1205 gcc_assert (avail_reg);
1207 insert_insn_on_edge (gen_move_insn (copy_rtx (dest),
1208 copy_rtx (avail_reg)),
1209 pred);
1210 stats.moves_inserted++;
1212 if (dump_file)
1213 fprintf (dump_file,
1214 "generating move from %d to %d on edge from %d to %d\n",
1215 REGNO (avail_reg),
1216 REGNO (dest),
1217 pred->src->index,
1218 pred->dest->index);
1221 /* Regenerate loads where the memory is unavailable. */
1222 for (unoccr = unavail_occrs; unoccr; unoccr = unoccr->next)
1224 pred = unoccr->pred;
1225 insert_insn_on_edge (copy_insn (PATTERN (insn)), pred);
1226 stats.copies_inserted++;
1228 if (dump_file)
1230 fprintf (dump_file,
1231 "generating on edge from %d to %d a copy of load: ",
1232 pred->src->index,
1233 pred->dest->index);
1234 print_rtl (dump_file, PATTERN (insn));
1235 fprintf (dump_file, "\n");
1239 /* Delete the insn if it is not available in this block and mark it
1240 for deletion if it is available. If insn is available it may help
1241 discover additional redundancies, so mark it for later deletion. */
1242 for (a_occr = get_bb_avail_insn (bb, expr->avail_occr, expr->bitmap_index);
1243 a_occr && (a_occr->insn != insn);
1244 a_occr = get_bb_avail_insn (bb, a_occr->next, expr->bitmap_index))
1247 if (!a_occr)
1249 stats.insns_deleted++;
1251 if (dump_file)
1253 fprintf (dump_file, "deleting insn:\n");
1254 print_rtl_single (dump_file, insn);
1255 fprintf (dump_file, "\n");
1257 delete_insn (insn);
1259 else
1260 a_occr->deleted_p = 1;
1262 cleanup:
1263 if (rollback_unoccr)
1264 obstack_free (&unoccr_obstack, rollback_unoccr);
1267 /* Performing the redundancy elimination as described before. */
1269 static void
1270 eliminate_partially_redundant_loads (void)
1272 rtx_insn *insn;
1273 basic_block bb;
1275 /* Note we start at block 1. */
1277 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1278 return;
1280 FOR_BB_BETWEEN (bb,
1281 ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb,
1282 EXIT_BLOCK_PTR_FOR_FN (cfun),
1283 next_bb)
1285 /* Don't try anything on basic blocks with strange predecessors. */
1286 if (! bb_has_well_behaved_predecessors (bb))
1287 continue;
1289 /* Do not try anything on cold basic blocks. */
1290 if (optimize_bb_for_size_p (bb))
1291 continue;
1293 /* Reset the table of things changed since the start of the current
1294 basic block. */
1295 reset_opr_set_tables ();
1297 /* Look at all insns in the current basic block and see if there are
1298 any loads in it that we can record. */
1299 FOR_BB_INSNS (bb, insn)
1301 /* Is it a load - of the form (set (reg) (mem))? */
1302 if (NONJUMP_INSN_P (insn)
1303 && GET_CODE (PATTERN (insn)) == SET
1304 && REG_P (SET_DEST (PATTERN (insn)))
1305 && MEM_P (SET_SRC (PATTERN (insn))))
1307 rtx pat = PATTERN (insn);
1308 rtx src = SET_SRC (pat);
1309 struct expr *expr;
1311 if (!MEM_VOLATILE_P (src)
1312 && GET_MODE (src) != BLKmode
1313 && general_operand (src, GET_MODE (src))
1314 /* Are the operands unchanged since the start of the
1315 block? */
1316 && oprs_unchanged_p (src, insn, false)
1317 && !(cfun->can_throw_non_call_exceptions && may_trap_p (src))
1318 && !side_effects_p (src)
1319 /* Is the expression recorded? */
1320 && (expr = lookup_expr_in_table (src)) != NULL)
1322 /* We now have a load (insn) and an available memory at
1323 its BB start (expr). Try to remove the loads if it is
1324 redundant. */
1325 eliminate_partially_redundant_load (bb, insn, expr);
1329 /* Keep track of everything modified by this insn, so that we
1330 know what has been modified since the start of the current
1331 basic block. */
1332 if (INSN_P (insn))
1333 record_opr_changes (insn);
1337 commit_edge_insertions ();
1340 /* Go over the expression hash table and delete insns that were
1341 marked for later deletion. */
1343 /* This helper is called via htab_traverse. */
1345 delete_redundant_insns_1 (expr **slot, void *data ATTRIBUTE_UNUSED)
1347 struct expr *exprs = *slot;
1348 struct occr *occr;
1350 for (occr = exprs->avail_occr; occr != NULL; occr = occr->next)
1352 if (occr->deleted_p && dbg_cnt (gcse2_delete))
1354 delete_insn (occr->insn);
1355 stats.insns_deleted++;
1357 if (dump_file)
1359 fprintf (dump_file, "deleting insn:\n");
1360 print_rtl_single (dump_file, occr->insn);
1361 fprintf (dump_file, "\n");
1366 return 1;
1369 static void
1370 delete_redundant_insns (void)
1372 expr_table->traverse <void *, delete_redundant_insns_1> (NULL);
1373 if (dump_file)
1374 fprintf (dump_file, "\n");
1377 /* Main entry point of the GCSE after reload - clean some redundant loads
1378 due to spilling. */
1380 static void
1381 gcse_after_reload_main (rtx f ATTRIBUTE_UNUSED)
1384 memset (&stats, 0, sizeof (stats));
1386 /* Allocate memory for this pass.
1387 Also computes and initializes the insns' CUIDs. */
1388 alloc_mem ();
1390 /* We need alias analysis. */
1391 init_alias_analysis ();
1393 compute_hash_table ();
1395 if (dump_file)
1396 dump_hash_table (dump_file);
1398 if (expr_table->elements () > 0)
1400 /* Knowing which MEMs are transparent through a block can signifiantly
1401 increase the number of redundant loads found. So compute transparency
1402 information for each memory expression in the hash table. */
1403 df_analyze ();
1404 /* This can not be part of the normal allocation routine because
1405 we have to know the number of elements in the hash table. */
1406 transp = sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
1407 expr_table->elements ());
1408 bitmap_vector_ones (transp, last_basic_block_for_fn (cfun));
1409 expr_table->traverse <FILE *, compute_expr_transp> (dump_file);
1410 eliminate_partially_redundant_loads ();
1411 delete_redundant_insns ();
1412 sbitmap_vector_free (transp);
1414 if (dump_file)
1416 fprintf (dump_file, "GCSE AFTER RELOAD stats:\n");
1417 fprintf (dump_file, "copies inserted: %d\n", stats.copies_inserted);
1418 fprintf (dump_file, "moves inserted: %d\n", stats.moves_inserted);
1419 fprintf (dump_file, "insns deleted: %d\n", stats.insns_deleted);
1420 fprintf (dump_file, "\n\n");
1423 statistics_counter_event (cfun, "copies inserted",
1424 stats.copies_inserted);
1425 statistics_counter_event (cfun, "moves inserted",
1426 stats.moves_inserted);
1427 statistics_counter_event (cfun, "insns deleted",
1428 stats.insns_deleted);
1431 /* We are finished with alias. */
1432 end_alias_analysis ();
1434 free_mem ();
1439 static unsigned int
1440 rest_of_handle_gcse2 (void)
1442 gcse_after_reload_main (get_insns ());
1443 rebuild_jump_labels (get_insns ());
1444 return 0;
1447 namespace {
1449 const pass_data pass_data_gcse2 =
1451 RTL_PASS, /* type */
1452 "gcse2", /* name */
1453 OPTGROUP_NONE, /* optinfo_flags */
1454 TV_GCSE_AFTER_RELOAD, /* tv_id */
1455 0, /* properties_required */
1456 0, /* properties_provided */
1457 0, /* properties_destroyed */
1458 0, /* todo_flags_start */
1459 0, /* todo_flags_finish */
1462 class pass_gcse2 : public rtl_opt_pass
1464 public:
1465 pass_gcse2 (gcc::context *ctxt)
1466 : rtl_opt_pass (pass_data_gcse2, ctxt)
1469 /* opt_pass methods: */
1470 virtual bool gate (function *fun)
1472 return (optimize > 0 && flag_gcse_after_reload
1473 && optimize_function_for_speed_p (fun));
1476 virtual unsigned int execute (function *) { return rest_of_handle_gcse2 (); }
1478 }; // class pass_gcse2
1480 } // anon namespace
1482 rtl_opt_pass *
1483 make_pass_gcse2 (gcc::context *ctxt)
1485 return new pass_gcse2 (ctxt);