1 /* Post reload partially redundant load elimination
2 Copyright (C) 2004-2019 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"
31 #include "insn-config.h"
39 #include "tree-pass.h"
42 #include "gcse-common.h"
45 #include "function-abi.h"
47 /* The following code implements gcse after reload, the purpose of this
48 pass is to cleanup redundant loads generated by reload and other
49 optimizations that come after gcse. It searches for simple inter-block
50 redundancies and tries to eliminate them by adding moves and loads
53 Perform partially redundant load elimination, try to eliminate redundant
54 loads created by the reload pass. We try to look for full or partial
55 redundant loads fed by one or more loads/stores in predecessor BBs,
56 and try adding loads to make them fully redundant. We also check if
57 it's worth adding loads to be able to delete the redundant load.
60 1. Build available expressions hash table:
61 For each load/store instruction, if the loaded/stored memory didn't
62 change until the end of the basic block add this memory expression to
64 2. Perform Redundancy elimination:
65 For each load instruction do the following:
66 perform partial redundancy elimination, check if it's worth adding
67 loads to make the load fully redundant. If so add loads and
68 register copies and delete the load.
69 3. Delete instructions made redundant in step 2.
72 If the loaded register is used/defined between load and some store,
73 look for some other free register between load and all its stores,
74 and replace the load with a copy from this register to the loaded
79 /* Keep statistics of this pass. */
87 /* We need to keep a hash table of expressions. The table entries are of
88 type 'struct expr', and for each expression there is a single linked
89 list of occurrences. */
91 /* Expression elements in the hash table. */
94 /* The expression (SET_SRC for expressions, PATTERN for assignments). */
97 /* The same hash for this entry. */
100 /* Index in the transparent bitmaps. */
101 unsigned int bitmap_index
;
103 /* List of available occurrence in basic blocks in the function. */
104 struct occr
*avail_occr
;
107 /* Hashtable helpers. */
109 struct expr_hasher
: nofree_ptr_hash
<expr
>
111 static inline hashval_t
hash (const expr
*);
112 static inline bool equal (const expr
*, const expr
*);
116 /* Hash expression X.
117 DO_NOT_RECORD_P is a boolean indicating if a volatile operand is found
118 or if the expression contains something we don't want to insert in the
122 hash_expr (rtx x
, int *do_not_record_p
)
124 *do_not_record_p
= 0;
125 return hash_rtx (x
, GET_MODE (x
), do_not_record_p
,
126 NULL
, /*have_reg_qty=*/false);
129 /* Callback for hashtab.
130 Return the hash value for expression EXP. We don't actually hash
131 here, we just return the cached hash value. */
134 expr_hasher::hash (const expr
*exp
)
139 /* Callback for hashtab.
140 Return nonzero if exp1 is equivalent to exp2. */
143 expr_hasher::equal (const expr
*exp1
, const expr
*exp2
)
145 int equiv_p
= exp_equiv_p (exp1
->expr
, exp2
->expr
, 0, true);
147 gcc_assert (!equiv_p
|| exp1
->hash
== exp2
->hash
);
151 /* The table itself. */
152 static hash_table
<expr_hasher
> *expr_table
;
155 static struct obstack expr_obstack
;
157 /* Occurrence of an expression.
158 There is at most one occurrence per basic block. If a pattern appears
159 more than once, the last appearance is used. */
163 /* Next occurrence of this expression. */
165 /* The insn that computes the expression. */
167 /* Nonzero if this [anticipatable] occurrence has been deleted. */
171 static struct obstack occr_obstack
;
173 /* The following structure holds the information about the occurrences of
174 the redundant instructions. */
182 static struct obstack unoccr_obstack
;
184 /* Array where each element is the CUID if the insn that last set the hard
185 register with the number of the element, since the start of the current
188 This array is used during the building of the hash table (step 1) to
189 determine if a reg is killed before the end of a basic block.
191 It is also used when eliminating partial redundancies (step 2) to see
192 if a reg was modified since the start of a basic block. */
193 static int *reg_avail_info
;
195 /* A list of insns that may modify memory within the current basic block. */
199 struct modifies_mem
*next
;
201 static struct modifies_mem
*modifies_mem_list
;
203 /* The modifies_mem structs also go on an obstack, only this obstack is
204 freed each time after completing the analysis or transformations on
205 a basic block. So we allocate a dummy modifies_mem_obstack_bottom
206 object on the obstack to keep track of the bottom of the obstack. */
207 static struct obstack modifies_mem_obstack
;
208 static struct modifies_mem
*modifies_mem_obstack_bottom
;
210 /* Mapping of insn UIDs to CUIDs.
211 CUIDs are like UIDs except they increase monotonically in each basic
212 block, have no gaps, and only apply to real insns. */
213 static int *uid_cuid
;
214 #define INSN_CUID(INSN) (uid_cuid[INSN_UID (INSN)])
216 /* Bitmap of blocks which have memory stores. */
217 static bitmap modify_mem_list_set
;
219 /* Bitmap of blocks which have calls. */
220 static bitmap blocks_with_calls
;
222 /* Vector indexed by block # with a list of all the insns that
223 modify memory within the block. */
224 static vec
<rtx_insn
*> *modify_mem_list
;
226 /* Vector indexed by block # with a canonicalized list of insns
227 that modify memory in the block. */
228 static vec
<modify_pair
> *canon_modify_mem_list
;
230 /* Vector of simple bitmaps indexed by block number. Each component sbitmap
231 indicates which expressions are transparent through the block. */
232 static sbitmap
*transp
;
235 /* Helpers for memory allocation/freeing. */
236 static void alloc_mem (void);
237 static void free_mem (void);
239 /* Support for hash table construction and transformations. */
240 static bool oprs_unchanged_p (rtx
, rtx_insn
*, bool);
241 static void record_last_reg_set_info (rtx_insn
*, rtx
);
242 static void record_last_reg_set_info_regno (rtx_insn
*, int);
243 static void record_last_mem_set_info (rtx_insn
*);
244 static void record_last_set_info (rtx
, const_rtx
, void *);
245 static void record_opr_changes (rtx_insn
*);
247 static void find_mem_conflicts (rtx
, const_rtx
, void *);
248 static int load_killed_in_block_p (int, rtx
, bool);
249 static void reset_opr_set_tables (void);
251 /* Hash table support. */
252 static hashval_t
hash_expr (rtx
, int *);
253 static void insert_expr_in_table (rtx
, rtx_insn
*);
254 static struct expr
*lookup_expr_in_table (rtx
);
255 static void dump_hash_table (FILE *);
257 /* Helpers for eliminate_partially_redundant_load. */
258 static bool reg_killed_on_edge (rtx
, edge
);
259 static bool reg_used_on_edge (rtx
, edge
);
261 static rtx
get_avail_load_store_reg (rtx_insn
*);
263 static bool bb_has_well_behaved_predecessors (basic_block
);
264 static struct occr
* get_bb_avail_insn (basic_block
, struct occr
*, int);
265 static void hash_scan_set (rtx_insn
*);
266 static void compute_hash_table (void);
268 /* The work horses of this pass. */
269 static void eliminate_partially_redundant_load (basic_block
,
272 static void eliminate_partially_redundant_loads (void);
275 /* Allocate memory for the CUID mapping array and register/memory
285 /* Find the largest UID and create a mapping from UIDs to CUIDs. */
286 uid_cuid
= XCNEWVEC (int, get_max_uid () + 1);
288 FOR_EACH_BB_FN (bb
, cfun
)
289 FOR_BB_INSNS (bb
, insn
)
292 uid_cuid
[INSN_UID (insn
)] = i
++;
294 uid_cuid
[INSN_UID (insn
)] = i
;
297 /* Allocate the available expressions hash table. We don't want to
298 make the hash table too small, but unnecessarily making it too large
299 also doesn't help. The i/4 is a gcse.c relic, and seems like a
300 reasonable choice. */
301 expr_table
= new hash_table
<expr_hasher
> (MAX (i
/ 4, 13));
303 /* We allocate everything on obstacks because we often can roll back
304 the whole obstack to some point. Freeing obstacks is very fast. */
305 gcc_obstack_init (&expr_obstack
);
306 gcc_obstack_init (&occr_obstack
);
307 gcc_obstack_init (&unoccr_obstack
);
308 gcc_obstack_init (&modifies_mem_obstack
);
310 /* Working array used to track the last set for each register
311 in the current block. */
312 reg_avail_info
= (int *) xmalloc (FIRST_PSEUDO_REGISTER
* sizeof (int));
314 /* Put a dummy modifies_mem object on the modifies_mem_obstack, so we
315 can roll it back in reset_opr_set_tables. */
316 modifies_mem_obstack_bottom
=
317 (struct modifies_mem
*) obstack_alloc (&modifies_mem_obstack
,
318 sizeof (struct modifies_mem
));
320 blocks_with_calls
= BITMAP_ALLOC (NULL
);
321 modify_mem_list_set
= BITMAP_ALLOC (NULL
);
323 modify_mem_list
= (vec_rtx_heap
*) xcalloc (last_basic_block_for_fn (cfun
),
324 sizeof (vec_rtx_heap
));
325 canon_modify_mem_list
326 = (vec_modify_pair_heap
*) xcalloc (last_basic_block_for_fn (cfun
),
327 sizeof (vec_modify_pair_heap
));
330 /* Free memory allocated by alloc_mem. */
340 obstack_free (&expr_obstack
, NULL
);
341 obstack_free (&occr_obstack
, NULL
);
342 obstack_free (&unoccr_obstack
, NULL
);
343 obstack_free (&modifies_mem_obstack
, NULL
);
347 EXECUTE_IF_SET_IN_BITMAP (modify_mem_list_set
, 0, i
, bi
)
349 modify_mem_list
[i
].release ();
350 canon_modify_mem_list
[i
].release ();
353 BITMAP_FREE (blocks_with_calls
);
354 BITMAP_FREE (modify_mem_list_set
);
355 free (reg_avail_info
);
356 free (modify_mem_list
);
357 free (canon_modify_mem_list
);
361 /* Insert expression X in INSN in the hash TABLE.
362 If it is already present, record it as the last occurrence in INSN's
366 insert_expr_in_table (rtx x
, rtx_insn
*insn
)
370 struct expr
*cur_expr
, **slot
;
371 struct occr
*avail_occr
;
373 hash
= hash_expr (x
, &do_not_record_p
);
375 /* Do not insert expression in the table if it contains volatile operands,
376 or if hash_expr determines the expression is something we don't want
377 to or can't handle. */
381 /* We anticipate that redundant expressions are rare, so for convenience
382 allocate a new hash table element here already and set its fields.
383 If we don't do this, we need a hack with a static struct expr. Anyway,
384 obstack_free is really fast and one more obstack_alloc doesn't hurt if
385 we're going to see more expressions later on. */
386 cur_expr
= (struct expr
*) obstack_alloc (&expr_obstack
,
387 sizeof (struct expr
));
389 cur_expr
->hash
= hash
;
390 cur_expr
->avail_occr
= NULL
;
392 slot
= expr_table
->find_slot_with_hash (cur_expr
, hash
, INSERT
);
396 /* The expression isn't found, so insert it. */
399 /* Anytime we add an entry to the table, record the index
400 of the new entry. The bitmap index starts counting
402 cur_expr
->bitmap_index
= expr_table
->elements () - 1;
406 /* The expression is already in the table, so roll back the
407 obstack and use the existing table entry. */
408 obstack_free (&expr_obstack
, cur_expr
);
412 /* Search for another occurrence in the same basic block. We insert
413 insns blockwise from start to end, so keep appending to the
414 start of the list so we have to check only a single element. */
415 avail_occr
= cur_expr
->avail_occr
;
417 && BLOCK_FOR_INSN (avail_occr
->insn
) == BLOCK_FOR_INSN (insn
))
418 avail_occr
->insn
= insn
;
421 /* First occurrence of this expression in this basic block. */
422 avail_occr
= (struct occr
*) obstack_alloc (&occr_obstack
,
423 sizeof (struct occr
));
424 avail_occr
->insn
= insn
;
425 avail_occr
->next
= cur_expr
->avail_occr
;
426 avail_occr
->deleted_p
= 0;
427 cur_expr
->avail_occr
= avail_occr
;
432 /* Lookup pattern PAT in the expression hash table.
433 The result is a pointer to the table entry, or NULL if not found. */
436 lookup_expr_in_table (rtx pat
)
439 struct expr
**slot
, *tmp_expr
;
440 hashval_t hash
= hash_expr (pat
, &do_not_record_p
);
445 tmp_expr
= (struct expr
*) obstack_alloc (&expr_obstack
,
446 sizeof (struct expr
));
447 tmp_expr
->expr
= pat
;
448 tmp_expr
->hash
= hash
;
449 tmp_expr
->avail_occr
= NULL
;
451 slot
= expr_table
->find_slot_with_hash (tmp_expr
, hash
, INSERT
);
452 obstack_free (&expr_obstack
, tmp_expr
);
461 /* Dump all expressions and occurrences that are currently in the
462 expression hash table to FILE. */
464 /* This helper is called via htab_traverse. */
466 dump_expr_hash_table_entry (expr
**slot
, FILE *file
)
468 struct expr
*exprs
= *slot
;
471 fprintf (file
, "expr: ");
472 print_rtl (file
, exprs
->expr
);
473 fprintf (file
,"\nhashcode: %u\n", exprs
->hash
);
474 fprintf (file
,"list of occurrences:\n");
475 occr
= exprs
->avail_occr
;
478 rtx_insn
*insn
= occr
->insn
;
479 print_rtl_single (file
, insn
);
480 fprintf (file
, "\n");
483 fprintf (file
, "\n");
488 dump_hash_table (FILE *file
)
490 fprintf (file
, "\n\nexpression hash table\n");
491 fprintf (file
, "size %ld, %ld elements, %f collision/search ratio\n",
492 (long) expr_table
->size (),
493 (long) expr_table
->elements (),
494 expr_table
->collisions ());
495 if (!expr_table
->is_empty ())
497 fprintf (file
, "\n\ntable entries:\n");
498 expr_table
->traverse
<FILE *, dump_expr_hash_table_entry
> (file
);
500 fprintf (file
, "\n");
503 /* Return true if register X is recorded as being set by an instruction
504 whose CUID is greater than the one given. */
507 reg_changed_after_insn_p (rtx x
, int cuid
)
509 unsigned int regno
, end_regno
;
512 end_regno
= END_REGNO (x
);
514 if (reg_avail_info
[regno
] > cuid
)
516 while (++regno
< end_regno
);
520 /* Return nonzero if the operands of expression X are unchanged
521 1) from the start of INSN's basic block up to but not including INSN
522 if AFTER_INSN is false, or
523 2) from INSN to the end of INSN's basic block if AFTER_INSN is true. */
526 oprs_unchanged_p (rtx x
, rtx_insn
*insn
, bool after_insn
)
539 /* We are called after register allocation. */
540 gcc_assert (REGNO (x
) < FIRST_PSEUDO_REGISTER
);
542 return !reg_changed_after_insn_p (x
, INSN_CUID (insn
) - 1);
544 return !reg_changed_after_insn_p (x
, 0);
547 if (load_killed_in_block_p (INSN_CUID (insn
), x
, after_insn
))
550 return oprs_unchanged_p (XEXP (x
, 0), insn
, after_insn
);
576 for (i
= GET_RTX_LENGTH (code
) - 1, fmt
= GET_RTX_FORMAT (code
); i
>= 0; i
--)
580 if (! oprs_unchanged_p (XEXP (x
, i
), insn
, after_insn
))
583 else if (fmt
[i
] == 'E')
584 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
585 if (! oprs_unchanged_p (XVECEXP (x
, i
, j
), insn
, after_insn
))
593 /* Used for communication between find_mem_conflicts and
594 load_killed_in_block_p. Nonzero if find_mem_conflicts finds a
595 conflict between two memory references.
596 This is a bit of a hack to work around the limitations of note_stores. */
597 static int mems_conflict_p
;
599 /* DEST is the output of an instruction. If it is a memory reference, and
600 possibly conflicts with the load found in DATA, then set mems_conflict_p
601 to a nonzero value. */
604 find_mem_conflicts (rtx dest
, const_rtx setter ATTRIBUTE_UNUSED
,
607 rtx mem_op
= (rtx
) data
;
609 while (GET_CODE (dest
) == SUBREG
610 || GET_CODE (dest
) == ZERO_EXTRACT
611 || GET_CODE (dest
) == STRICT_LOW_PART
)
612 dest
= XEXP (dest
, 0);
614 /* If DEST is not a MEM, then it will not conflict with the load. Note
615 that function calls are assumed to clobber memory, but are handled
620 if (true_dependence (dest
, GET_MODE (dest
), mem_op
))
625 /* Return nonzero if the expression in X (a memory reference) is killed
626 in the current basic block before (if AFTER_INSN is false) or after
627 (if AFTER_INSN is true) the insn with the CUID in UID_LIMIT.
629 This function assumes that the modifies_mem table is flushed when
630 the hash table construction or redundancy elimination phases start
631 processing a new basic block. */
634 load_killed_in_block_p (int uid_limit
, rtx x
, bool after_insn
)
636 struct modifies_mem
*list_entry
= modifies_mem_list
;
640 rtx_insn
*setter
= list_entry
->insn
;
642 /* Ignore entries in the list that do not apply. */
644 && INSN_CUID (setter
) < uid_limit
)
646 && INSN_CUID (setter
) > uid_limit
))
648 list_entry
= list_entry
->next
;
652 /* If SETTER is a call everything is clobbered. Note that calls
653 to pure functions are never put on the list, so we need not
658 /* SETTER must be an insn of some kind that sets memory. Call
659 note_stores to examine each hunk of memory that is modified.
660 It will set mems_conflict_p to nonzero if there may be a
661 conflict between X and SETTER. */
663 note_stores (setter
, find_mem_conflicts
, x
);
667 list_entry
= list_entry
->next
;
673 /* Record register first/last/block set information for REGNO in INSN. */
676 record_last_reg_set_info (rtx_insn
*insn
, rtx reg
)
678 unsigned int regno
, end_regno
;
681 end_regno
= END_REGNO (reg
);
683 reg_avail_info
[regno
] = INSN_CUID (insn
);
684 while (++regno
< end_regno
);
688 record_last_reg_set_info_regno (rtx_insn
*insn
, int regno
)
690 reg_avail_info
[regno
] = INSN_CUID (insn
);
694 /* Record memory modification information for INSN. We do not actually care
695 about the memory location(s) that are set, or even how they are set (consider
696 a CALL_INSN). We merely need to record which insns modify memory. */
699 record_last_mem_set_info (rtx_insn
*insn
)
701 struct modifies_mem
*list_entry
;
703 list_entry
= (struct modifies_mem
*) obstack_alloc (&modifies_mem_obstack
,
704 sizeof (struct modifies_mem
));
705 list_entry
->insn
= insn
;
706 list_entry
->next
= modifies_mem_list
;
707 modifies_mem_list
= list_entry
;
709 record_last_mem_set_info_common (insn
, modify_mem_list
,
710 canon_modify_mem_list
,
715 /* Called from compute_hash_table via note_stores to handle one
716 SET or CLOBBER in an insn. DATA is really the instruction in which
717 the SET is taking place. */
720 record_last_set_info (rtx dest
, const_rtx setter ATTRIBUTE_UNUSED
, void *data
)
722 rtx_insn
*last_set_insn
= (rtx_insn
*) data
;
724 if (GET_CODE (dest
) == SUBREG
)
725 dest
= SUBREG_REG (dest
);
728 record_last_reg_set_info (last_set_insn
, dest
);
729 else if (MEM_P (dest
))
731 /* Ignore pushes, they don't clobber memory. They may still
732 clobber the stack pointer though. Some targets do argument
733 pushes without adding REG_INC notes. See e.g. PR25196,
734 where a pushsi2 on i386 doesn't have REG_INC notes. Note
735 such changes here too. */
736 if (! push_operand (dest
, GET_MODE (dest
)))
737 record_last_mem_set_info (last_set_insn
);
739 record_last_reg_set_info_regno (last_set_insn
, STACK_POINTER_REGNUM
);
744 /* Reset tables used to keep track of what's still available since the
745 start of the block. */
748 reset_opr_set_tables (void)
750 memset (reg_avail_info
, 0, FIRST_PSEUDO_REGISTER
* sizeof (int));
751 obstack_free (&modifies_mem_obstack
, modifies_mem_obstack_bottom
);
752 modifies_mem_list
= NULL
;
756 /* Record things set by INSN.
757 This data is used by oprs_unchanged_p. */
760 record_opr_changes (rtx_insn
*insn
)
764 /* Find all stores and record them. */
765 note_stores (insn
, record_last_set_info
, insn
);
767 /* Also record autoincremented REGs for this insn as changed. */
768 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
769 if (REG_NOTE_KIND (note
) == REG_INC
)
770 record_last_reg_set_info (insn
, XEXP (note
, 0));
772 /* Finally, if this is a call, record all call clobbers. */
776 hard_reg_set_iterator hrsi
;
777 /* We don't track modes of hard registers, so we need to be
778 conservative and assume that partial kills are full kills. */
779 HARD_REG_SET callee_clobbers
780 = insn_callee_abi (insn
).full_and_partial_reg_clobbers ();
781 EXECUTE_IF_SET_IN_HARD_REG_SET (callee_clobbers
, 0, regno
, hrsi
)
782 record_last_reg_set_info_regno (insn
, regno
);
784 if (! RTL_CONST_OR_PURE_CALL_P (insn
))
785 record_last_mem_set_info (insn
);
790 /* Scan the pattern of INSN and add an entry to the hash TABLE.
791 After reload we are interested in loads/stores only. */
794 hash_scan_set (rtx_insn
*insn
)
796 rtx pat
= PATTERN (insn
);
797 rtx src
= SET_SRC (pat
);
798 rtx dest
= SET_DEST (pat
);
800 /* We are only interested in loads and stores. */
801 if (! MEM_P (src
) && ! MEM_P (dest
))
804 /* Don't mess with jumps and nops. */
805 if (JUMP_P (insn
) || set_noop_p (pat
))
810 if (/* Don't CSE something if we can't do a reg/reg copy. */
811 can_copy_p (GET_MODE (dest
))
812 /* Is SET_SRC something we want to gcse? */
813 && general_operand (src
, GET_MODE (src
))
815 /* Never consider insns touching the register stack. It may
816 create situations that reg-stack cannot handle (e.g. a stack
817 register live across an abnormal edge). */
818 && (REGNO (dest
) < FIRST_STACK_REG
|| REGNO (dest
) > LAST_STACK_REG
)
820 /* An expression is not available if its operands are
821 subsequently modified, including this insn. */
822 && oprs_unchanged_p (src
, insn
, true))
824 insert_expr_in_table (src
, insn
);
827 else if (REG_P (src
))
829 /* Only record sets of pseudo-regs in the hash table. */
830 if (/* Don't CSE something if we can't do a reg/reg copy. */
831 can_copy_p (GET_MODE (src
))
832 /* Is SET_DEST something we want to gcse? */
833 && general_operand (dest
, GET_MODE (dest
))
835 /* As above for STACK_REGS. */
836 && (REGNO (src
) < FIRST_STACK_REG
|| REGNO (src
) > LAST_STACK_REG
)
838 && ! (flag_float_store
&& FLOAT_MODE_P (GET_MODE (dest
)))
839 /* Check if the memory expression is killed after insn. */
840 && ! load_killed_in_block_p (INSN_CUID (insn
) + 1, dest
, true)
841 && oprs_unchanged_p (XEXP (dest
, 0), insn
, true))
843 insert_expr_in_table (dest
, insn
);
849 /* Create hash table of memory expressions available at end of basic
850 blocks. Basically you should think of this hash table as the
851 representation of AVAIL_OUT. This is the set of expressions that
852 is generated in a basic block and not killed before the end of the
853 same basic block. Notice that this is really a local computation. */
856 compute_hash_table (void)
860 FOR_EACH_BB_FN (bb
, cfun
)
864 /* First pass over the instructions records information used to
865 determine when registers and memory are last set.
866 Since we compute a "local" AVAIL_OUT, reset the tables that
867 help us keep track of what has been modified since the start
869 reset_opr_set_tables ();
870 FOR_BB_INSNS (bb
, insn
)
873 record_opr_changes (insn
);
876 /* The next pass actually builds the hash table. */
877 FOR_BB_INSNS (bb
, insn
)
878 if (INSN_P (insn
) && GET_CODE (PATTERN (insn
)) == SET
)
879 hash_scan_set (insn
);
884 /* Check if register REG is killed in any insn waiting to be inserted on
885 edge E. This function is required to check that our data flow analysis
886 is still valid prior to commit_edge_insertions. */
889 reg_killed_on_edge (rtx reg
, edge e
)
893 for (insn
= e
->insns
.r
; insn
; insn
= NEXT_INSN (insn
))
894 if (INSN_P (insn
) && reg_set_p (reg
, insn
))
900 /* Similar to above - check if register REG is used in any insn waiting
901 to be inserted on edge E.
902 Assumes no such insn can be a CALL_INSN; if so call reg_used_between_p
903 with PREV(insn),NEXT(insn) instead of calling reg_overlap_mentioned_p. */
906 reg_used_on_edge (rtx reg
, edge e
)
910 for (insn
= e
->insns
.r
; insn
; insn
= NEXT_INSN (insn
))
911 if (INSN_P (insn
) && reg_overlap_mentioned_p (reg
, PATTERN (insn
)))
917 /* Return the loaded/stored register of a load/store instruction. */
920 get_avail_load_store_reg (rtx_insn
*insn
)
922 if (REG_P (SET_DEST (PATTERN (insn
))))
924 return SET_DEST (PATTERN (insn
));
928 gcc_assert (REG_P (SET_SRC (PATTERN (insn
))));
929 return SET_SRC (PATTERN (insn
));
933 /* Return nonzero if the predecessors of BB are "well behaved". */
936 bb_has_well_behaved_predecessors (basic_block bb
)
941 if (EDGE_COUNT (bb
->preds
) == 0)
944 FOR_EACH_EDGE (pred
, ei
, bb
->preds
)
946 /* commit_one_edge_insertion refuses to insert on abnormal edges even if
947 the source has only one successor so EDGE_CRITICAL_P is too weak. */
948 if ((pred
->flags
& EDGE_ABNORMAL
) && !single_pred_p (pred
->dest
))
951 if ((pred
->flags
& EDGE_ABNORMAL_CALL
) && cfun
->has_nonlocal_label
)
954 if (tablejump_p (BB_END (pred
->src
), NULL
, NULL
))
961 /* Search for the occurrences of expression in BB. */
964 get_bb_avail_insn (basic_block bb
, struct occr
*orig_occr
, int bitmap_index
)
966 struct occr
*occr
= orig_occr
;
968 for (; occr
!= NULL
; occr
= occr
->next
)
969 if (BLOCK_FOR_INSN (occr
->insn
) == bb
)
972 /* If we could not find an occurrence in BB, see if BB
973 has a single predecessor with an occurrence that is
974 transparent through BB. */
976 && single_pred_p (bb
)
977 && bitmap_bit_p (transp
[bb
->index
], bitmap_index
)
978 && (occr
= get_bb_avail_insn (single_pred (bb
), orig_occr
, bitmap_index
)))
980 rtx avail_reg
= get_avail_load_store_reg (occr
->insn
);
981 if (!reg_set_between_p (avail_reg
,
982 PREV_INSN (BB_HEAD (bb
)),
983 NEXT_INSN (BB_END (bb
)))
984 && !reg_killed_on_edge (avail_reg
, single_pred_edge (bb
)))
992 /* This helper is called via htab_traverse. */
994 compute_expr_transp (expr
**slot
, FILE *dump_file ATTRIBUTE_UNUSED
)
996 struct expr
*expr
= *slot
;
998 compute_transp (expr
->expr
, expr
->bitmap_index
, transp
,
999 blocks_with_calls
, modify_mem_list_set
,
1000 canon_modify_mem_list
);
1004 /* This handles the case where several stores feed a partially redundant
1005 load. It checks if the redundancy elimination is possible and if it's
1008 Redundancy elimination is possible if,
1009 1) None of the operands of an insn have been modified since the start
1010 of the current basic block.
1011 2) In any predecessor of the current basic block, the same expression
1014 See the function body for the heuristics that determine if eliminating
1015 a redundancy is also worth doing, assuming it is possible. */
1018 eliminate_partially_redundant_load (basic_block bb
, rtx_insn
*insn
,
1022 rtx_insn
*avail_insn
= NULL
;
1025 struct occr
*a_occr
;
1026 struct unoccr
*occr
, *avail_occrs
= NULL
;
1027 struct unoccr
*unoccr
, *unavail_occrs
= NULL
, *rollback_unoccr
= NULL
;
1029 profile_count ok_count
= profile_count::zero ();
1030 /* Redundant load execution count. */
1031 profile_count critical_count
= profile_count::zero ();
1032 /* Execution count of critical edges. */
1034 bool critical_edge_split
= false;
1036 /* The execution count of the loads to be added to make the
1037 load fully redundant. */
1038 profile_count not_ok_count
= profile_count::zero ();
1039 basic_block pred_bb
;
1041 pat
= PATTERN (insn
);
1042 dest
= SET_DEST (pat
);
1044 /* Check that the loaded register is not used, set, or killed from the
1045 beginning of the block. */
1046 if (reg_changed_after_insn_p (dest
, 0)
1047 || reg_used_between_p (dest
, PREV_INSN (BB_HEAD (bb
)), insn
))
1050 /* Check potential for replacing load with copy for predecessors. */
1051 FOR_EACH_EDGE (pred
, ei
, bb
->preds
)
1053 rtx_insn
*next_pred_bb_end
;
1056 avail_reg
= NULL_RTX
;
1057 pred_bb
= pred
->src
;
1058 for (a_occr
= get_bb_avail_insn (pred_bb
,
1060 expr
->bitmap_index
);
1062 a_occr
= get_bb_avail_insn (pred_bb
,
1064 expr
->bitmap_index
))
1066 /* Check if the loaded register is not used. */
1067 avail_insn
= a_occr
->insn
;
1068 avail_reg
= get_avail_load_store_reg (avail_insn
);
1069 gcc_assert (avail_reg
);
1071 /* Make sure we can generate a move from register avail_reg to
1073 rtx_insn
*move
= gen_move_insn (copy_rtx (dest
),
1074 copy_rtx (avail_reg
));
1075 extract_insn (move
);
1076 if (! constrain_operands (1, get_preferred_alternatives (insn
,
1078 || reg_killed_on_edge (avail_reg
, pred
)
1079 || reg_used_on_edge (dest
, pred
))
1084 next_pred_bb_end
= NEXT_INSN (BB_END (BLOCK_FOR_INSN (avail_insn
)));
1085 if (!reg_set_between_p (avail_reg
, avail_insn
, next_pred_bb_end
))
1086 /* AVAIL_INSN remains non-null. */
1092 if (EDGE_CRITICAL_P (pred
) && pred
->count ().initialized_p ())
1093 critical_count
+= pred
->count ();
1095 if (avail_insn
!= NULL_RTX
)
1098 if (pred
->count ().initialized_p ())
1099 ok_count
= ok_count
+ pred
->count ();
1100 if (! set_noop_p (PATTERN (gen_move_insn (copy_rtx (dest
),
1101 copy_rtx (avail_reg
)))))
1103 /* Check if there is going to be a split. */
1104 if (EDGE_CRITICAL_P (pred
))
1105 critical_edge_split
= true;
1107 else /* Its a dead move no need to generate. */
1109 occr
= (struct unoccr
*) obstack_alloc (&unoccr_obstack
,
1110 sizeof (struct unoccr
));
1111 occr
->insn
= avail_insn
;
1113 occr
->next
= avail_occrs
;
1115 if (! rollback_unoccr
)
1116 rollback_unoccr
= occr
;
1120 /* Adding a load on a critical edge will cause a split. */
1121 if (EDGE_CRITICAL_P (pred
))
1122 critical_edge_split
= true;
1123 if (pred
->count ().initialized_p ())
1124 not_ok_count
= not_ok_count
+ pred
->count ();
1125 unoccr
= (struct unoccr
*) obstack_alloc (&unoccr_obstack
,
1126 sizeof (struct unoccr
));
1127 unoccr
->insn
= NULL
;
1128 unoccr
->pred
= pred
;
1129 unoccr
->next
= unavail_occrs
;
1130 unavail_occrs
= unoccr
;
1131 if (! rollback_unoccr
)
1132 rollback_unoccr
= unoccr
;
1136 if (/* No load can be replaced by copy. */
1138 /* Prevent exploding the code. */
1139 || (optimize_bb_for_size_p (bb
) && npred_ok
> 1)
1140 /* If we don't have profile information we cannot tell if splitting
1141 a critical edge is profitable or not so don't do it. */
1142 || ((!profile_info
|| profile_status_for_fn (cfun
) != PROFILE_READ
1143 || targetm
.cannot_modify_jumps_p ())
1144 && critical_edge_split
))
1147 /* Check if it's worth applying the partial redundancy elimination. */
1148 if (ok_count
.to_gcov_type ()
1149 < GCSE_AFTER_RELOAD_PARTIAL_FRACTION
* not_ok_count
.to_gcov_type ())
1152 gcov_type threshold
;
1153 #if (GCC_VERSION >= 5000)
1154 if (__builtin_mul_overflow (GCSE_AFTER_RELOAD_CRITICAL_FRACTION
,
1155 critical_count
.to_gcov_type (), &threshold
))
1156 threshold
= profile_count::max_count
;
1159 = GCSE_AFTER_RELOAD_CRITICAL_FRACTION
* critical_count
.to_gcov_type ();
1162 if (ok_count
.to_gcov_type () < threshold
)
1165 /* Generate moves to the loaded register from where
1166 the memory is available. */
1167 for (occr
= avail_occrs
; occr
; occr
= occr
->next
)
1169 avail_insn
= occr
->insn
;
1171 /* Set avail_reg to be the register having the value of the
1173 avail_reg
= get_avail_load_store_reg (avail_insn
);
1174 gcc_assert (avail_reg
);
1176 insert_insn_on_edge (gen_move_insn (copy_rtx (dest
),
1177 copy_rtx (avail_reg
)),
1179 stats
.moves_inserted
++;
1183 "generating move from %d to %d on edge from %d to %d\n",
1190 /* Regenerate loads where the memory is unavailable. */
1191 for (unoccr
= unavail_occrs
; unoccr
; unoccr
= unoccr
->next
)
1193 pred
= unoccr
->pred
;
1194 insert_insn_on_edge (copy_insn (PATTERN (insn
)), pred
);
1195 stats
.copies_inserted
++;
1200 "generating on edge from %d to %d a copy of load: ",
1203 print_rtl (dump_file
, PATTERN (insn
));
1204 fprintf (dump_file
, "\n");
1208 /* Delete the insn if it is not available in this block and mark it
1209 for deletion if it is available. If insn is available it may help
1210 discover additional redundancies, so mark it for later deletion. */
1211 for (a_occr
= get_bb_avail_insn (bb
, expr
->avail_occr
, expr
->bitmap_index
);
1212 a_occr
&& (a_occr
->insn
!= insn
);
1213 a_occr
= get_bb_avail_insn (bb
, a_occr
->next
, expr
->bitmap_index
))
1218 stats
.insns_deleted
++;
1222 fprintf (dump_file
, "deleting insn:\n");
1223 print_rtl_single (dump_file
, insn
);
1224 fprintf (dump_file
, "\n");
1229 a_occr
->deleted_p
= 1;
1232 if (rollback_unoccr
)
1233 obstack_free (&unoccr_obstack
, rollback_unoccr
);
1236 /* Performing the redundancy elimination as described before. */
1239 eliminate_partially_redundant_loads (void)
1244 /* Note we start at block 1. */
1246 if (ENTRY_BLOCK_PTR_FOR_FN (cfun
)->next_bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
1250 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->next_bb
->next_bb
,
1251 EXIT_BLOCK_PTR_FOR_FN (cfun
),
1254 /* Don't try anything on basic blocks with strange predecessors. */
1255 if (! bb_has_well_behaved_predecessors (bb
))
1258 /* Do not try anything on cold basic blocks. */
1259 if (optimize_bb_for_size_p (bb
))
1262 /* Reset the table of things changed since the start of the current
1264 reset_opr_set_tables ();
1266 /* Look at all insns in the current basic block and see if there are
1267 any loads in it that we can record. */
1268 FOR_BB_INSNS (bb
, insn
)
1270 /* Is it a load - of the form (set (reg) (mem))? */
1271 if (NONJUMP_INSN_P (insn
)
1272 && GET_CODE (PATTERN (insn
)) == SET
1273 && REG_P (SET_DEST (PATTERN (insn
)))
1274 && MEM_P (SET_SRC (PATTERN (insn
))))
1276 rtx pat
= PATTERN (insn
);
1277 rtx src
= SET_SRC (pat
);
1280 if (!MEM_VOLATILE_P (src
)
1281 && GET_MODE (src
) != BLKmode
1282 && general_operand (src
, GET_MODE (src
))
1283 /* Are the operands unchanged since the start of the
1285 && oprs_unchanged_p (src
, insn
, false)
1286 && !(cfun
->can_throw_non_call_exceptions
&& may_trap_p (src
))
1287 && !side_effects_p (src
)
1288 /* Is the expression recorded? */
1289 && (expr
= lookup_expr_in_table (src
)) != NULL
)
1291 /* We now have a load (insn) and an available memory at
1292 its BB start (expr). Try to remove the loads if it is
1294 eliminate_partially_redundant_load (bb
, insn
, expr
);
1298 /* Keep track of everything modified by this insn, so that we
1299 know what has been modified since the start of the current
1302 record_opr_changes (insn
);
1306 commit_edge_insertions ();
1309 /* Go over the expression hash table and delete insns that were
1310 marked for later deletion. */
1312 /* This helper is called via htab_traverse. */
1314 delete_redundant_insns_1 (expr
**slot
, void *data ATTRIBUTE_UNUSED
)
1316 struct expr
*exprs
= *slot
;
1319 for (occr
= exprs
->avail_occr
; occr
!= NULL
; occr
= occr
->next
)
1321 if (occr
->deleted_p
&& dbg_cnt (gcse2_delete
))
1323 delete_insn (occr
->insn
);
1324 stats
.insns_deleted
++;
1328 fprintf (dump_file
, "deleting insn:\n");
1329 print_rtl_single (dump_file
, occr
->insn
);
1330 fprintf (dump_file
, "\n");
1339 delete_redundant_insns (void)
1341 expr_table
->traverse
<void *, delete_redundant_insns_1
> (NULL
);
1343 fprintf (dump_file
, "\n");
1346 /* Main entry point of the GCSE after reload - clean some redundant loads
1350 gcse_after_reload_main (rtx f ATTRIBUTE_UNUSED
)
1352 /* Disable computing transparentness if it is too expensive. */
1354 = !gcse_or_cprop_is_too_expensive (_("using simple load CSE after register "
1357 memset (&stats
, 0, sizeof (stats
));
1359 /* Allocate memory for this pass.
1360 Also computes and initializes the insns' CUIDs. */
1363 /* We need alias analysis. */
1364 init_alias_analysis ();
1366 compute_hash_table ();
1369 dump_hash_table (dump_file
);
1371 if (!expr_table
->is_empty ())
1373 /* Knowing which MEMs are transparent through a block can signifiantly
1374 increase the number of redundant loads found. So compute transparency
1375 information for each memory expression in the hash table. */
1379 /* This cannot be part of the normal allocation routine because
1380 we have to know the number of elements in the hash table. */
1381 transp
= sbitmap_vector_alloc (last_basic_block_for_fn (cfun
),
1382 expr_table
->elements ());
1383 bitmap_vector_ones (transp
, last_basic_block_for_fn (cfun
));
1384 expr_table
->traverse
<FILE *, compute_expr_transp
> (dump_file
);
1388 eliminate_partially_redundant_loads ();
1389 delete_redundant_insns ();
1391 sbitmap_vector_free (transp
);
1395 fprintf (dump_file
, "GCSE AFTER RELOAD stats:\n");
1396 fprintf (dump_file
, "copies inserted: %d\n", stats
.copies_inserted
);
1397 fprintf (dump_file
, "moves inserted: %d\n", stats
.moves_inserted
);
1398 fprintf (dump_file
, "insns deleted: %d\n", stats
.insns_deleted
);
1399 fprintf (dump_file
, "\n\n");
1402 statistics_counter_event (cfun
, "copies inserted",
1403 stats
.copies_inserted
);
1404 statistics_counter_event (cfun
, "moves inserted",
1405 stats
.moves_inserted
);
1406 statistics_counter_event (cfun
, "insns deleted",
1407 stats
.insns_deleted
);
1410 /* We are finished with alias. */
1411 end_alias_analysis ();
1419 rest_of_handle_gcse2 (void)
1421 gcse_after_reload_main (get_insns ());
1422 rebuild_jump_labels (get_insns ());
1428 const pass_data pass_data_gcse2
=
1430 RTL_PASS
, /* type */
1432 OPTGROUP_NONE
, /* optinfo_flags */
1433 TV_GCSE_AFTER_RELOAD
, /* tv_id */
1434 0, /* properties_required */
1435 0, /* properties_provided */
1436 0, /* properties_destroyed */
1437 0, /* todo_flags_start */
1438 0, /* todo_flags_finish */
1441 class pass_gcse2
: public rtl_opt_pass
1444 pass_gcse2 (gcc::context
*ctxt
)
1445 : rtl_opt_pass (pass_data_gcse2
, ctxt
)
1448 /* opt_pass methods: */
1449 virtual bool gate (function
*fun
)
1451 return (optimize
> 0 && flag_gcse_after_reload
1452 && optimize_function_for_speed_p (fun
));
1455 virtual unsigned int execute (function
*) { return rest_of_handle_gcse2 (); }
1457 }; // class pass_gcse2
1462 make_pass_gcse2 (gcc::context
*ctxt
)
1464 return new pass_gcse2 (ctxt
);