DWARF array bounds missing from C++ array definitions
[official-gcc.git] / gcc / postreload-gcse.c
blob0c12b3808a66b5fc26da4f571521bc2ca29fbe59
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
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 "backend.h"
24 #include "target.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "predict.h"
28 #include "df.h"
29 #include "memmodel.h"
30 #include "tm_p.h"
31 #include "insn-config.h"
32 #include "emit-rtl.h"
33 #include "recog.h"
35 #include "cfgrtl.h"
36 #include "profile.h"
37 #include "expr.h"
38 #include "params.h"
39 #include "tree-pass.h"
40 #include "dbgcnt.h"
41 #include "intl.h"
42 #include "gcse-common.h"
43 #include "gcse.h"
44 #include "regs.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
51 in cold places.
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.
59 Algorithm:
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
63 the hash table.
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.
71 Future enhancement:
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
75 register.
79 /* Keep statistics of this pass. */
80 static struct
82 int moves_inserted;
83 int copies_inserted;
84 int insns_deleted;
85 } stats;
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. */
92 struct expr
94 /* The expression (SET_SRC for expressions, PATTERN for assignments). */
95 rtx expr;
97 /* The same hash for this entry. */
98 hashval_t hash;
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
119 table. */
121 static hashval_t
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. */
133 inline hashval_t
134 expr_hasher::hash (const expr *exp)
136 return exp->hash;
139 /* Callback for hashtab.
140 Return nonzero if exp1 is equivalent to exp2. */
142 inline bool
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);
148 return equiv_p;
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. */
161 struct occr
163 /* Next occurrence of this expression. */
164 struct occr *next;
165 /* The insn that computes the expression. */
166 rtx_insn *insn;
167 /* Nonzero if this [anticipatable] occurrence has been deleted. */
168 char deleted_p;
171 static struct obstack occr_obstack;
173 /* The following structure holds the information about the occurrences of
174 the redundant instructions. */
175 struct unoccr
177 struct unoccr *next;
178 edge pred;
179 rtx_insn *insn;
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
186 basic block.
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. */
196 struct modifies_mem
198 rtx_insn *insn;
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,
270 rtx_insn *,
271 struct expr *);
272 static void eliminate_partially_redundant_loads (void);
275 /* Allocate memory for the CUID mapping array and register/memory
276 tracking tables. */
278 static void
279 alloc_mem (void)
281 int i;
282 basic_block bb;
283 rtx_insn *insn;
285 /* Find the largest UID and create a mapping from UIDs to CUIDs. */
286 uid_cuid = XCNEWVEC (int, get_max_uid () + 1);
287 i = 1;
288 FOR_EACH_BB_FN (bb, cfun)
289 FOR_BB_INSNS (bb, insn)
291 if (INSN_P (insn))
292 uid_cuid[INSN_UID (insn)] = i++;
293 else
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. */
332 static void
333 free_mem (void)
335 free (uid_cuid);
337 delete expr_table;
338 expr_table = NULL;
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);
345 unsigned i;
346 bitmap_iterator bi;
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
363 basic block. */
365 static void
366 insert_expr_in_table (rtx x, rtx_insn *insn)
368 int do_not_record_p;
369 hashval_t hash;
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. */
378 if (do_not_record_p)
379 return;
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));
388 cur_expr->expr = x;
389 cur_expr->hash = hash;
390 cur_expr->avail_occr = NULL;
392 slot = expr_table->find_slot_with_hash (cur_expr, hash, INSERT);
394 if (! (*slot))
396 /* The expression isn't found, so insert it. */
397 *slot = cur_expr;
399 /* Anytime we add an entry to the table, record the index
400 of the new entry. The bitmap index starts counting
401 at zero. */
402 cur_expr->bitmap_index = expr_table->elements () - 1;
404 else
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);
409 cur_expr = *slot;
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;
416 if (avail_occr
417 && BLOCK_FOR_INSN (avail_occr->insn) == BLOCK_FOR_INSN (insn))
418 avail_occr->insn = insn;
419 else
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. */
435 static struct expr *
436 lookup_expr_in_table (rtx pat)
438 int do_not_record_p;
439 struct expr **slot, *tmp_expr;
440 hashval_t hash = hash_expr (pat, &do_not_record_p);
442 if (do_not_record_p)
443 return NULL;
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);
454 if (!slot)
455 return NULL;
456 else
457 return (*slot);
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;
469 struct occr *occr;
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;
476 while (occr)
478 rtx_insn *insn = occr->insn;
479 print_rtl_single (file, insn);
480 fprintf (file, "\n");
481 occr = occr->next;
483 fprintf (file, "\n");
484 return 1;
487 static void
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. */
506 static bool
507 reg_changed_after_insn_p (rtx x, int cuid)
509 unsigned int regno, end_regno;
511 regno = REGNO (x);
512 end_regno = END_REGNO (x);
514 if (reg_avail_info[regno] > cuid)
515 return true;
516 while (++regno < end_regno);
517 return false;
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. */
525 static bool
526 oprs_unchanged_p (rtx x, rtx_insn *insn, bool after_insn)
528 int i, j;
529 enum rtx_code code;
530 const char *fmt;
532 if (x == 0)
533 return 1;
535 code = GET_CODE (x);
536 switch (code)
538 case REG:
539 /* We are called after register allocation. */
540 gcc_assert (REGNO (x) < FIRST_PSEUDO_REGISTER);
541 if (after_insn)
542 return !reg_changed_after_insn_p (x, INSN_CUID (insn) - 1);
543 else
544 return !reg_changed_after_insn_p (x, 0);
546 case MEM:
547 if (load_killed_in_block_p (INSN_CUID (insn), x, after_insn))
548 return 0;
549 else
550 return oprs_unchanged_p (XEXP (x, 0), insn, after_insn);
552 case PC:
553 case CC0: /*FIXME*/
554 case CONST:
555 CASE_CONST_ANY:
556 case SYMBOL_REF:
557 case LABEL_REF:
558 case ADDR_VEC:
559 case ADDR_DIFF_VEC:
560 return 1;
562 case PRE_DEC:
563 case PRE_INC:
564 case POST_DEC:
565 case POST_INC:
566 case PRE_MODIFY:
567 case POST_MODIFY:
568 if (after_insn)
569 return 0;
570 break;
572 default:
573 break;
576 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
578 if (fmt[i] == 'e')
580 if (! oprs_unchanged_p (XEXP (x, i), insn, after_insn))
581 return 0;
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))
586 return 0;
589 return 1;
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. */
603 static void
604 find_mem_conflicts (rtx dest, const_rtx setter ATTRIBUTE_UNUSED,
605 void *data)
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
616 elsewhere. */
617 if (! MEM_P (dest))
618 return;
620 if (true_dependence (dest, GET_MODE (dest), mem_op))
621 mems_conflict_p = 1;
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. */
633 static int
634 load_killed_in_block_p (int uid_limit, rtx x, bool after_insn)
636 struct modifies_mem *list_entry = modifies_mem_list;
638 while (list_entry)
640 rtx_insn *setter = list_entry->insn;
642 /* Ignore entries in the list that do not apply. */
643 if ((after_insn
644 && INSN_CUID (setter) < uid_limit)
645 || (! after_insn
646 && INSN_CUID (setter) > uid_limit))
648 list_entry = list_entry->next;
649 continue;
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
654 worry about them. */
655 if (CALL_P (setter))
656 return 1;
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. */
662 mems_conflict_p = 0;
663 note_stores (setter, find_mem_conflicts, x);
664 if (mems_conflict_p)
665 return 1;
667 list_entry = list_entry->next;
669 return 0;
673 /* Record register first/last/block set information for REGNO in INSN. */
675 static inline void
676 record_last_reg_set_info (rtx_insn *insn, rtx reg)
678 unsigned int regno, end_regno;
680 regno = REGNO (reg);
681 end_regno = END_REGNO (reg);
683 reg_avail_info[regno] = INSN_CUID (insn);
684 while (++regno < end_regno);
687 static inline void
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. */
698 static void
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,
711 modify_mem_list_set,
712 blocks_with_calls);
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. */
719 static void
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);
727 if (REG_P (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);
738 else
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. */
747 static void
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. */
759 static void
760 record_opr_changes (rtx_insn *insn)
762 rtx note;
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. */
773 if (CALL_P (insn))
775 unsigned int regno;
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. */
793 static void
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))
802 return;
804 /* Don't mess with jumps and nops. */
805 if (JUMP_P (insn) || set_noop_p (pat))
806 return;
808 if (REG_P (dest))
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))
814 #ifdef STACK_REGS
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)
819 #endif
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))
834 #ifdef STACK_REGS
835 /* As above for STACK_REGS. */
836 && (REGNO (src) < FIRST_STACK_REG || REGNO (src) > LAST_STACK_REG)
837 #endif
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. */
855 static void
856 compute_hash_table (void)
858 basic_block bb;
860 FOR_EACH_BB_FN (bb, cfun)
862 rtx_insn *insn;
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
868 of the block. */
869 reset_opr_set_tables ();
870 FOR_BB_INSNS (bb, insn)
872 if (INSN_P (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. */
888 static bool
889 reg_killed_on_edge (rtx reg, edge e)
891 rtx_insn *insn;
893 for (insn = e->insns.r; insn; insn = NEXT_INSN (insn))
894 if (INSN_P (insn) && reg_set_p (reg, insn))
895 return true;
897 return false;
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. */
905 static bool
906 reg_used_on_edge (rtx reg, edge e)
908 rtx_insn *insn;
910 for (insn = e->insns.r; insn; insn = NEXT_INSN (insn))
911 if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
912 return true;
914 return false;
917 /* Return the loaded/stored register of a load/store instruction. */
919 static rtx
920 get_avail_load_store_reg (rtx_insn *insn)
922 if (REG_P (SET_DEST (PATTERN (insn))))
923 /* A load. */
924 return SET_DEST (PATTERN (insn));
925 else
927 /* A store. */
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". */
935 static bool
936 bb_has_well_behaved_predecessors (basic_block bb)
938 edge pred;
939 edge_iterator ei;
941 if (EDGE_COUNT (bb->preds) == 0)
942 return false;
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))
949 return false;
951 if ((pred->flags & EDGE_ABNORMAL_CALL) && cfun->has_nonlocal_label)
952 return false;
954 if (tablejump_p (BB_END (pred->src), NULL, NULL))
955 return false;
957 return true;
961 /* Search for the occurrences of expression in BB. */
963 static struct occr*
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)
970 return occr;
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. */
975 if (transp
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)))
985 return occr;
988 return NULL;
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);
1001 return 1;
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
1006 worth it.
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
1012 is generated.
1014 See the function body for the heuristics that determine if eliminating
1015 a redundancy is also worth doing, assuming it is possible. */
1017 static void
1018 eliminate_partially_redundant_load (basic_block bb, rtx_insn *insn,
1019 struct expr *expr)
1021 edge pred;
1022 rtx_insn *avail_insn = NULL;
1023 rtx avail_reg;
1024 rtx dest, pat;
1025 struct occr *a_occr;
1026 struct unoccr *occr, *avail_occrs = NULL;
1027 struct unoccr *unoccr, *unavail_occrs = NULL, *rollback_unoccr = NULL;
1028 int npred_ok = 0;
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. */
1033 edge_iterator ei;
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))
1048 return;
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;
1055 avail_insn = NULL;
1056 avail_reg = NULL_RTX;
1057 pred_bb = pred->src;
1058 for (a_occr = get_bb_avail_insn (pred_bb,
1059 expr->avail_occr,
1060 expr->bitmap_index);
1061 a_occr;
1062 a_occr = get_bb_avail_insn (pred_bb,
1063 a_occr->next,
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
1072 dest. */
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,
1077 pred_bb))
1078 || reg_killed_on_edge (avail_reg, pred)
1079 || reg_used_on_edge (dest, pred))
1081 avail_insn = NULL;
1082 continue;
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. */
1087 break;
1088 else
1089 avail_insn = NULL;
1092 if (EDGE_CRITICAL_P (pred) && pred->count ().initialized_p ())
1093 critical_count += pred->count ();
1095 if (avail_insn != NULL_RTX)
1097 npred_ok++;
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. */
1108 continue;
1109 occr = (struct unoccr *) obstack_alloc (&unoccr_obstack,
1110 sizeof (struct unoccr));
1111 occr->insn = avail_insn;
1112 occr->pred = pred;
1113 occr->next = avail_occrs;
1114 avail_occrs = occr;
1115 if (! rollback_unoccr)
1116 rollback_unoccr = occr;
1118 else
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. */
1137 npred_ok == 0
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))
1145 goto cleanup;
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 ())
1150 goto cleanup;
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;
1157 #else
1158 threshold
1159 = GCSE_AFTER_RELOAD_CRITICAL_FRACTION * critical_count.to_gcov_type ();
1160 #endif
1162 if (ok_count.to_gcov_type () < threshold)
1163 goto cleanup;
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;
1170 pred = occr->pred;
1171 /* Set avail_reg to be the register having the value of the
1172 memory. */
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)),
1178 pred);
1179 stats.moves_inserted++;
1181 if (dump_file)
1182 fprintf (dump_file,
1183 "generating move from %d to %d on edge from %d to %d\n",
1184 REGNO (avail_reg),
1185 REGNO (dest),
1186 pred->src->index,
1187 pred->dest->index);
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++;
1197 if (dump_file)
1199 fprintf (dump_file,
1200 "generating on edge from %d to %d a copy of load: ",
1201 pred->src->index,
1202 pred->dest->index);
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))
1216 if (!a_occr)
1218 stats.insns_deleted++;
1220 if (dump_file)
1222 fprintf (dump_file, "deleting insn:\n");
1223 print_rtl_single (dump_file, insn);
1224 fprintf (dump_file, "\n");
1226 delete_insn (insn);
1228 else
1229 a_occr->deleted_p = 1;
1231 cleanup:
1232 if (rollback_unoccr)
1233 obstack_free (&unoccr_obstack, rollback_unoccr);
1236 /* Performing the redundancy elimination as described before. */
1238 static void
1239 eliminate_partially_redundant_loads (void)
1241 rtx_insn *insn;
1242 basic_block bb;
1244 /* Note we start at block 1. */
1246 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1247 return;
1249 FOR_BB_BETWEEN (bb,
1250 ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb,
1251 EXIT_BLOCK_PTR_FOR_FN (cfun),
1252 next_bb)
1254 /* Don't try anything on basic blocks with strange predecessors. */
1255 if (! bb_has_well_behaved_predecessors (bb))
1256 continue;
1258 /* Do not try anything on cold basic blocks. */
1259 if (optimize_bb_for_size_p (bb))
1260 continue;
1262 /* Reset the table of things changed since the start of the current
1263 basic block. */
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);
1278 struct expr *expr;
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
1284 block? */
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
1293 redundant. */
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
1300 basic block. */
1301 if (INSN_P (insn))
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;
1317 struct occr *occr;
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++;
1326 if (dump_file)
1328 fprintf (dump_file, "deleting insn:\n");
1329 print_rtl_single (dump_file, occr->insn);
1330 fprintf (dump_file, "\n");
1335 return 1;
1338 static void
1339 delete_redundant_insns (void)
1341 expr_table->traverse <void *, delete_redundant_insns_1> (NULL);
1342 if (dump_file)
1343 fprintf (dump_file, "\n");
1346 /* Main entry point of the GCSE after reload - clean some redundant loads
1347 due to spilling. */
1349 static void
1350 gcse_after_reload_main (rtx f ATTRIBUTE_UNUSED)
1352 /* Disable computing transparentness if it is too expensive. */
1353 bool do_transp
1354 = !gcse_or_cprop_is_too_expensive (_("using simple load CSE after register "
1355 "allocation"));
1357 memset (&stats, 0, sizeof (stats));
1359 /* Allocate memory for this pass.
1360 Also computes and initializes the insns' CUIDs. */
1361 alloc_mem ();
1363 /* We need alias analysis. */
1364 init_alias_analysis ();
1366 compute_hash_table ();
1368 if (dump_file)
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. */
1376 df_analyze ();
1377 if (do_transp)
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);
1386 else
1387 transp = NULL;
1388 eliminate_partially_redundant_loads ();
1389 delete_redundant_insns ();
1390 if (do_transp)
1391 sbitmap_vector_free (transp);
1393 if (dump_file)
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 ();
1413 free_mem ();
1418 static unsigned int
1419 rest_of_handle_gcse2 (void)
1421 gcse_after_reload_main (get_insns ());
1422 rebuild_jump_labels (get_insns ());
1423 return 0;
1426 namespace {
1428 const pass_data pass_data_gcse2 =
1430 RTL_PASS, /* type */
1431 "gcse2", /* name */
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
1443 public:
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
1459 } // anon namespace
1461 rtl_opt_pass *
1462 make_pass_gcse2 (gcc::context *ctxt)
1464 return new pass_gcse2 (ctxt);