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
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
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/>. */
22 #include "coretypes.h"
24 #include "diagnostic-core.h"
26 #include "hash-table.h"
31 #include "double-int.h"
40 #include "hard-reg-set.h"
42 #include "insn-config.h"
46 #include "dominance.h"
49 #include "basic-block.h"
52 #include "statistics.h"
54 #include "fixed-value.h"
68 #include "tree-pass.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
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.
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
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.
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
105 /* Keep statistics of this pass. */
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. */
120 /* The expression (SET_SRC for expressions, PATTERN for assignments). */
123 /* The same hash for this entry. */
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 value_type
*);
140 static inline bool equal (const value_type
*, const compare_type
*);
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
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. */
162 expr_hasher::hash (const value_type
*exp
)
167 /* Callback for hashtab.
168 Return nonzero if exp1 is equivalent to exp2. */
171 expr_hasher::equal (const value_type
*exp1
, const compare_type
*exp2
)
173 int equiv_p
= exp_equiv_p (exp1
->expr
, exp2
->expr
, 0, true);
175 gcc_assert (!equiv_p
|| exp1
->hash
== exp2
->hash
);
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. */
191 /* Next occurrence of this expression. */
193 /* The insn that computes the expression. */
195 /* Nonzero if this [anticipatable] occurrence has been deleted. */
199 static struct obstack occr_obstack
;
201 /* The following structure holds the information about the occurrences of
202 the redundant instructions. */
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
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. */
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
,
300 static void eliminate_partially_redundant_loads (void);
303 /* Allocate memory for the CUID mapping array and register/memory
313 /* Find the largest UID and create a mapping from UIDs to CUIDs. */
314 uid_cuid
= XCNEWVEC (int, get_max_uid () + 1);
316 FOR_EACH_BB_FN (bb
, cfun
)
317 FOR_BB_INSNS (bb
, insn
)
320 uid_cuid
[INSN_UID (insn
)] = i
++;
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. */
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
);
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
392 insert_expr_in_table (rtx x
, rtx_insn
*insn
)
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. */
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
));
415 cur_expr
->hash
= hash
;
416 cur_expr
->avail_occr
= NULL
;
418 slot
= expr_table
->find_slot_with_hash (cur_expr
, hash
, INSERT
);
422 /* The expression isn't found, so insert it. */
425 /* Anytime we add an entry to the table, record the index
426 of the new entry. The bitmap index starts counting
428 cur_expr
->bitmap_index
= expr_table
->elements () - 1;
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
);
438 /* Search for another occurrence in the same basic block. */
439 avail_occr
= cur_expr
->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
445 last_occr
= avail_occr
;
446 avail_occr
= avail_occr
->next
;
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
454 avail_occr
->insn
= insn
;
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
;
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. */
478 lookup_expr_in_table (rtx pat
)
481 struct expr
**slot
, *tmp_expr
;
482 hashval_t hash
= hash_expr (pat
, &do_not_record_p
);
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
);
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
;
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
;
520 rtx_insn
*insn
= occr
->insn
;
521 print_rtl_single (file
, insn
);
522 fprintf (file
, "\n");
525 fprintf (file
, "\n");
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. */
549 reg_changed_after_insn_p (rtx x
, int cuid
)
551 unsigned int regno
, end_regno
;
554 end_regno
= END_HARD_REGNO (x
);
556 if (reg_avail_info
[regno
] > cuid
)
558 while (++regno
< end_regno
);
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. */
568 oprs_unchanged_p (rtx x
, rtx_insn
*insn
, bool after_insn
)
581 /* We are called after register allocation. */
582 gcc_assert (REGNO (x
) < FIRST_PSEUDO_REGISTER
);
584 return !reg_changed_after_insn_p (x
, INSN_CUID (insn
) - 1);
586 return !reg_changed_after_insn_p (x
, 0);
589 if (load_killed_in_block_p (INSN_CUID (insn
), x
, after_insn
))
592 return oprs_unchanged_p (XEXP (x
, 0), insn
, after_insn
);
618 for (i
= GET_RTX_LENGTH (code
) - 1, fmt
= GET_RTX_FORMAT (code
); i
>= 0; i
--)
622 if (! oprs_unchanged_p (XEXP (x
, i
), insn
, after_insn
))
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
))
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. */
646 find_mem_conflicts (rtx dest
, const_rtx setter ATTRIBUTE_UNUSED
,
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
662 if (true_dependence (dest
, GET_MODE (dest
), mem_op
))
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. */
676 load_killed_in_block_p (int uid_limit
, rtx x
, bool after_insn
)
678 struct modifies_mem
*list_entry
= modifies_mem_list
;
682 rtx_insn
*setter
= list_entry
->insn
;
684 /* Ignore entries in the list that do not apply. */
686 && INSN_CUID (setter
) < uid_limit
)
688 && INSN_CUID (setter
) > uid_limit
))
690 list_entry
= list_entry
->next
;
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
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. */
705 note_stores (PATTERN (setter
), find_mem_conflicts
, x
);
709 list_entry
= list_entry
->next
;
715 /* Record register first/last/block set information for REGNO in INSN. */
718 record_last_reg_set_info (rtx_insn
*insn
, rtx reg
)
720 unsigned int regno
, end_regno
;
723 end_regno
= END_HARD_REGNO (reg
);
725 reg_avail_info
[regno
] = INSN_CUID (insn
);
726 while (++regno
< end_regno
);
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. */
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
,
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. */
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
);
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
);
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. */
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. */
802 record_opr_changes (rtx_insn
*insn
)
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. */
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);
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. */
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
))
854 /* Don't mess with jumps and nops. */
855 if (JUMP_P (insn
) || set_noop_p (pat
))
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
))
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
)
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
))
885 /* As above for STACK_REGS. */
886 && (REGNO (src
) < FIRST_STACK_REG
|| REGNO (src
) > LAST_STACK_REG
)
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. */
906 compute_hash_table (void)
910 FOR_EACH_BB_FN (bb
, cfun
)
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
919 reset_opr_set_tables ();
920 FOR_BB_INSNS (bb
, 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. */
939 reg_killed_on_edge (rtx reg
, edge e
)
943 for (insn
= e
->insns
.r
; insn
; insn
= NEXT_INSN (insn
))
944 if (INSN_P (insn
) && reg_set_p (reg
, insn
))
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. */
956 reg_used_on_edge (rtx reg
, edge e
)
960 for (insn
= e
->insns
.r
; insn
; insn
= NEXT_INSN (insn
))
961 if (INSN_P (insn
) && reg_overlap_mentioned_p (reg
, PATTERN (insn
)))
967 /* Return the loaded/stored register of a load/store instruction. */
970 get_avail_load_store_reg (rtx_insn
*insn
)
972 if (REG_P (SET_DEST (PATTERN (insn
))))
974 return SET_DEST (PATTERN (insn
));
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". */
986 bb_has_well_behaved_predecessors (basic_block bb
)
991 if (EDGE_COUNT (bb
->preds
) == 0)
994 FOR_EACH_EDGE (pred
, ei
, bb
->preds
)
996 if ((pred
->flags
& EDGE_ABNORMAL
) && EDGE_CRITICAL_P (pred
))
999 if ((pred
->flags
& EDGE_ABNORMAL_CALL
) && cfun
->has_nonlocal_label
)
1002 if (tablejump_p (BB_END (pred
->src
), NULL
, NULL
))
1009 /* Search for the occurrences of expression in BB. */
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
)
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
)))
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
);
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
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
1061 See the function body for the heuristics that determine if eliminating
1062 a redundancy is also worth doing, assuming it is possible. */
1065 eliminate_partially_redundant_load (basic_block bb
, rtx_insn
*insn
,
1069 rtx_insn
*avail_insn
= NULL
;
1072 struct occr
*a_occr
;
1073 struct unoccr
*occr
, *avail_occrs
= NULL
;
1074 struct unoccr
*unoccr
, *unavail_occrs
= NULL
, *rollback_unoccr
= NULL
;
1076 gcov_type ok_count
= 0; /* Redundant load execution count. */
1077 gcov_type critical_count
= 0; /* Execution count of critical edges. */
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
))
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
;
1101 avail_reg
= NULL_RTX
;
1102 pred_bb
= pred
->src
;
1103 for (a_occr
= get_bb_avail_insn (pred_bb
,
1105 expr
->bitmap_index
);
1107 a_occr
= get_bb_avail_insn (pred_bb
,
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
1118 rtx_insn
*move
= as_a
<rtx_insn
*>
1119 (gen_move_insn (copy_rtx (dest
), copy_rtx (avail_reg
)));
1120 extract_insn (move
);
1121 if (! constrain_operands (1, get_preferred_alternatives (insn
,
1123 || reg_killed_on_edge (avail_reg
, pred
)
1124 || reg_used_on_edge (dest
, pred
))
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. */
1137 if (EDGE_CRITICAL_P (pred
))
1138 critical_count
+= pred
->count
;
1140 if (avail_insn
!= NULL_RTX
)
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. */
1153 occr
= (struct unoccr
*) obstack_alloc (&unoccr_obstack
,
1154 sizeof (struct unoccr
));
1155 occr
->insn
= avail_insn
;
1157 occr
->next
= avail_occrs
;
1159 if (! rollback_unoccr
)
1160 rollback_unoccr
= occr
;
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. */
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
))
1190 /* Check if it's worth applying the partial redundancy elimination. */
1191 if (ok_count
< GCSE_AFTER_RELOAD_PARTIAL_FRACTION
* not_ok_count
)
1193 if (ok_count
< GCSE_AFTER_RELOAD_CRITICAL_FRACTION
* critical_count
)
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
;
1202 /* Set avail_reg to be the register having the value of the
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
)),
1210 stats
.moves_inserted
++;
1214 "generating move from %d to %d on edge from %d to %d\n",
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
++;
1231 "generating on edge from %d to %d a copy of load: ",
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
))
1249 stats
.insns_deleted
++;
1253 fprintf (dump_file
, "deleting insn:\n");
1254 print_rtl_single (dump_file
, insn
);
1255 fprintf (dump_file
, "\n");
1260 a_occr
->deleted_p
= 1;
1263 if (rollback_unoccr
)
1264 obstack_free (&unoccr_obstack
, rollback_unoccr
);
1267 /* Performing the redundancy elimination as described before. */
1270 eliminate_partially_redundant_loads (void)
1275 /* Note we start at block 1. */
1277 if (ENTRY_BLOCK_PTR_FOR_FN (cfun
)->next_bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
1281 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->next_bb
->next_bb
,
1282 EXIT_BLOCK_PTR_FOR_FN (cfun
),
1285 /* Don't try anything on basic blocks with strange predecessors. */
1286 if (! bb_has_well_behaved_predecessors (bb
))
1289 /* Do not try anything on cold basic blocks. */
1290 if (optimize_bb_for_size_p (bb
))
1293 /* Reset the table of things changed since the start of the current
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
);
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
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
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
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
;
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
++;
1359 fprintf (dump_file
, "deleting insn:\n");
1360 print_rtl_single (dump_file
, occr
->insn
);
1361 fprintf (dump_file
, "\n");
1370 delete_redundant_insns (void)
1372 expr_table
->traverse
<void *, delete_redundant_insns_1
> (NULL
);
1374 fprintf (dump_file
, "\n");
1377 /* Main entry point of the GCSE after reload - clean some redundant loads
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. */
1390 /* We need alias analysis. */
1391 init_alias_analysis ();
1393 compute_hash_table ();
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. */
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
);
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 ();
1440 rest_of_handle_gcse2 (void)
1442 gcse_after_reload_main (get_insns ());
1443 rebuild_jump_labels (get_insns ());
1449 const pass_data pass_data_gcse2
=
1451 RTL_PASS
, /* type */
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
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
1483 make_pass_gcse2 (gcc::context
*ctxt
)
1485 return new pass_gcse2 (ctxt
);