xref: /netbsd-src/external/gpl3/gcc/dist/gcc/postreload-gcse.cc (revision b1e838363e3c6fc78a55519254d99869742dd33c)
1 /* Post reload partially redundant load elimination
2    Copyright (C) 2004-2022 Free Software Foundation, Inc.
3 
4 This file is part of GCC.
5 
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10 
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19 
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "backend.h"
24 #include "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"
34 
35 #include "cfgrtl.h"
36 #include "profile.h"
37 #include "expr.h"
38 #include "tree-pass.h"
39 #include "dbgcnt.h"
40 #include "intl.h"
41 #include "gcse-common.h"
42 #include "gcse.h"
43 #include "regs.h"
44 #include "function-abi.h"
45 
46 /* The following code implements gcse after reload, the purpose of this
47    pass is to cleanup redundant loads generated by reload and other
48    optimizations that come after gcse. It searches for simple inter-block
49    redundancies and tries to eliminate them by adding moves and loads
50    in cold places.
51 
52    Perform partially redundant load elimination, try to eliminate redundant
53    loads created by the reload pass.  We try to look for full or partial
54    redundant loads fed by one or more loads/stores in predecessor BBs,
55    and try adding loads to make them fully redundant.  We also check if
56    it's worth adding loads to be able to delete the redundant load.
57 
58    Algorithm:
59    1. Build available expressions hash table:
60        For each load/store instruction, if the loaded/stored memory didn't
61        change until the end of the basic block add this memory expression to
62        the hash table.
63    2. Perform Redundancy elimination:
64       For each load instruction do the following:
65 	 perform partial redundancy elimination, check if it's worth adding
66 	 loads to make the load fully redundant.  If so add loads and
67 	 register copies and delete the load.
68    3. Delete instructions made redundant in step 2.
69 
70    Future enhancement:
71      If the loaded register is used/defined between load and some store,
72      look for some other free register between load and all its stores,
73      and replace the load with a copy from this register to the loaded
74      register.
75 */
76 
77 
78 /* Keep statistics of this pass.  */
79 static struct
80 {
81   int moves_inserted;
82   int copies_inserted;
83   int insns_deleted;
84 } stats;
85 
86 /* We need to keep a hash table of expressions.  The table entries are of
87    type 'struct expr', and for each expression there is a single linked
88    list of occurrences.  */
89 
90 /* Expression elements in the hash table.  */
91 struct expr
92 {
93   /* The expression (SET_SRC for expressions, PATTERN for assignments).  */
94   rtx expr;
95 
96   /* The same hash for this entry.  */
97   hashval_t hash;
98 
99   /* Index in the transparent bitmaps.  */
100   unsigned int bitmap_index;
101 
102   /* List of available occurrence in basic blocks in the function.  */
103   struct occr *avail_occr;
104 };
105 
106 /* Hashtable helpers.  */
107 
108 struct expr_hasher : nofree_ptr_hash <expr>
109 {
110   static inline hashval_t hash (const expr *);
111   static inline bool equal (const expr *, const expr *);
112 };
113 
114 
115 /* Hash expression X.
116    DO_NOT_RECORD_P is a boolean indicating if a volatile operand is found
117    or if the expression contains something we don't want to insert in the
118    table.  */
119 
120 static hashval_t
hash_expr(rtx x,int * do_not_record_p)121 hash_expr (rtx x, int *do_not_record_p)
122 {
123   *do_not_record_p = 0;
124   return hash_rtx (x, GET_MODE (x), do_not_record_p,
125 		   NULL,  /*have_reg_qty=*/false);
126 }
127 
128 /* Callback for hashtab.
129    Return the hash value for expression EXP.  We don't actually hash
130    here, we just return the cached hash value.  */
131 
132 inline hashval_t
hash(const expr * exp)133 expr_hasher::hash (const expr *exp)
134 {
135   return exp->hash;
136 }
137 
138 /* Callback for hashtab.
139    Return nonzero if exp1 is equivalent to exp2.  */
140 
141 inline bool
equal(const expr * exp1,const expr * exp2)142 expr_hasher::equal (const expr *exp1, const expr *exp2)
143 {
144   int equiv_p = exp_equiv_p (exp1->expr, exp2->expr, 0, true);
145 
146   gcc_assert (!equiv_p || exp1->hash == exp2->hash);
147   return equiv_p;
148 }
149 
150 /* The table itself.  */
151 static hash_table<expr_hasher> *expr_table;
152 
153 
154 static struct obstack expr_obstack;
155 
156 /* Occurrence of an expression.
157    There is at most one occurrence per basic block.  If a pattern appears
158    more than once, the last appearance is used.  */
159 
160 struct occr
161 {
162   /* Next occurrence of this expression.  */
163   struct occr *next;
164   /* The insn that computes the expression.  */
165   rtx_insn *insn;
166   /* Nonzero if this [anticipatable] occurrence has been deleted.  */
167   char deleted_p;
168 };
169 
170 static struct obstack occr_obstack;
171 
172 /* The following structure holds the information about the occurrences of
173    the redundant instructions.  */
174 struct unoccr
175 {
176   struct unoccr *next;
177   edge pred;
178   rtx_insn *insn;
179 };
180 
181 static struct obstack unoccr_obstack;
182 
183 /* Array where each element is the CUID if the insn that last set the hard
184    register with the number of the element, since the start of the current
185    basic block.
186 
187    This array is used during the building of the hash table (step 1) to
188    determine if a reg is killed before the end of a basic block.
189 
190    It is also used when eliminating partial redundancies (step 2) to see
191    if a reg was modified since the start of a basic block.  */
192 static int *reg_avail_info;
193 
194 /* A list of insns that may modify memory within the current basic block.  */
195 struct modifies_mem
196 {
197   rtx_insn *insn;
198   struct modifies_mem *next;
199 };
200 static struct modifies_mem *modifies_mem_list;
201 
202 /* The modifies_mem structs also go on an obstack, only this obstack is
203    freed each time after completing the analysis or transformations on
204    a basic block.  So we allocate a dummy modifies_mem_obstack_bottom
205    object on the obstack to keep track of the bottom of the obstack.  */
206 static struct obstack modifies_mem_obstack;
207 static struct modifies_mem  *modifies_mem_obstack_bottom;
208 
209 /* Mapping of insn UIDs to CUIDs.
210    CUIDs are like UIDs except they increase monotonically in each basic
211    block, have no gaps, and only apply to real insns.  */
212 static int *uid_cuid;
213 #define INSN_CUID(INSN) (uid_cuid[INSN_UID (INSN)])
214 
215 /* Bitmap of blocks which have memory stores.  */
216 static bitmap modify_mem_list_set;
217 
218 /* Bitmap of blocks which have calls.  */
219 static bitmap blocks_with_calls;
220 
221 /* Vector indexed by block # with a list of all the insns that
222    modify memory within the block.  */
223 static vec<rtx_insn *> *modify_mem_list;
224 
225 /* Vector indexed by block # with a canonicalized list of insns
226    that modify memory in the block.  */
227 static vec<modify_pair> *canon_modify_mem_list;
228 
229 /* Vector of simple bitmaps indexed by block number.  Each component sbitmap
230    indicates which expressions are transparent through the block.  */
231 static sbitmap *transp;
232 
233 
234 /* Helpers for memory allocation/freeing.  */
235 static void alloc_mem (void);
236 static void free_mem (void);
237 
238 /* Support for hash table construction and transformations.  */
239 static bool oprs_unchanged_p (rtx, rtx_insn *, bool);
240 static void record_last_reg_set_info (rtx_insn *, rtx);
241 static void record_last_reg_set_info_regno (rtx_insn *, int);
242 static void record_last_mem_set_info (rtx_insn *);
243 static void record_last_set_info (rtx, const_rtx, void *);
244 static void record_opr_changes (rtx_insn *);
245 
246 static void find_mem_conflicts (rtx, const_rtx, void *);
247 static int load_killed_in_block_p (int, rtx, bool);
248 static void reset_opr_set_tables (void);
249 
250 /* Hash table support.  */
251 static hashval_t hash_expr (rtx, int *);
252 static void insert_expr_in_table (rtx, rtx_insn *);
253 static struct expr *lookup_expr_in_table (rtx);
254 static void dump_hash_table (FILE *);
255 
256 /* Helpers for eliminate_partially_redundant_load.  */
257 static bool reg_killed_on_edge (rtx, edge);
258 static bool reg_used_on_edge (rtx, edge);
259 
260 static rtx get_avail_load_store_reg (rtx_insn *);
261 
262 static bool bb_has_well_behaved_predecessors (basic_block);
263 static struct occr* get_bb_avail_insn (basic_block, struct occr *, int);
264 static void hash_scan_set (rtx_insn *);
265 static void compute_hash_table (void);
266 
267 /* The work horses of this pass.  */
268 static void eliminate_partially_redundant_load (basic_block,
269 						rtx_insn *,
270 						struct expr *);
271 static void eliminate_partially_redundant_loads (void);
272 
273 
274 /* Allocate memory for the CUID mapping array and register/memory
275    tracking tables.  */
276 
277 static void
alloc_mem(void)278 alloc_mem (void)
279 {
280   int i;
281   basic_block bb;
282   rtx_insn *insn;
283 
284   /* Find the largest UID and create a mapping from UIDs to CUIDs.  */
285   uid_cuid = XCNEWVEC (int, get_max_uid () + 1);
286   i = 1;
287   FOR_EACH_BB_FN (bb, cfun)
288     FOR_BB_INSNS (bb, insn)
289       {
290         if (INSN_P (insn))
291 	  uid_cuid[INSN_UID (insn)] = i++;
292 	else
293 	  uid_cuid[INSN_UID (insn)] = i;
294       }
295 
296   /* Allocate the available expressions hash table.  We don't want to
297      make the hash table too small, but unnecessarily making it too large
298      also doesn't help.  The i/4 is a gcse.cc relic, and seems like a
299      reasonable choice.  */
300   expr_table = new hash_table<expr_hasher> (MAX (i / 4, 13));
301 
302   /* We allocate everything on obstacks because we often can roll back
303      the whole obstack to some point.  Freeing obstacks is very fast.  */
304   gcc_obstack_init (&expr_obstack);
305   gcc_obstack_init (&occr_obstack);
306   gcc_obstack_init (&unoccr_obstack);
307   gcc_obstack_init (&modifies_mem_obstack);
308 
309   /* Working array used to track the last set for each register
310      in the current block.  */
311   reg_avail_info = (int *) xmalloc (FIRST_PSEUDO_REGISTER * sizeof (int));
312 
313   /* Put a dummy modifies_mem object on the modifies_mem_obstack, so we
314      can roll it back in reset_opr_set_tables.  */
315   modifies_mem_obstack_bottom =
316     (struct modifies_mem *) obstack_alloc (&modifies_mem_obstack,
317 					   sizeof (struct modifies_mem));
318 
319   blocks_with_calls = BITMAP_ALLOC (NULL);
320   modify_mem_list_set = BITMAP_ALLOC (NULL);
321 
322   modify_mem_list = (vec_rtx_heap *) xcalloc (last_basic_block_for_fn (cfun),
323 					      sizeof (vec_rtx_heap));
324   canon_modify_mem_list
325     = (vec_modify_pair_heap *) xcalloc (last_basic_block_for_fn (cfun),
326 					sizeof (vec_modify_pair_heap));
327 }
328 
329 /* Free memory allocated by alloc_mem.  */
330 
331 static void
free_mem(void)332 free_mem (void)
333 {
334   free (uid_cuid);
335 
336   delete expr_table;
337   expr_table = NULL;
338 
339   obstack_free (&expr_obstack, NULL);
340   obstack_free (&occr_obstack, NULL);
341   obstack_free (&unoccr_obstack, NULL);
342   obstack_free (&modifies_mem_obstack, NULL);
343 
344   unsigned i;
345   bitmap_iterator bi;
346   EXECUTE_IF_SET_IN_BITMAP (modify_mem_list_set, 0, i, bi)
347     {
348       modify_mem_list[i].release ();
349       canon_modify_mem_list[i].release ();
350     }
351 
352   BITMAP_FREE (blocks_with_calls);
353   BITMAP_FREE (modify_mem_list_set);
354   free (reg_avail_info);
355   free (modify_mem_list);
356   free (canon_modify_mem_list);
357 }
358 
359 
360 /* Insert expression X in INSN in the hash TABLE.
361    If it is already present, record it as the last occurrence in INSN's
362    basic block.  */
363 
364 static void
insert_expr_in_table(rtx x,rtx_insn * insn)365 insert_expr_in_table (rtx x, rtx_insn *insn)
366 {
367   int do_not_record_p;
368   hashval_t hash;
369   struct expr *cur_expr, **slot;
370   struct occr *avail_occr;
371 
372   hash = hash_expr (x, &do_not_record_p);
373 
374   /* Do not insert expression in the table if it contains volatile operands,
375      or if hash_expr determines the expression is something we don't want
376      to or can't handle.  */
377   if (do_not_record_p)
378     return;
379 
380   /* We anticipate that redundant expressions are rare, so for convenience
381      allocate a new hash table element here already and set its fields.
382      If we don't do this, we need a hack with a static struct expr.  Anyway,
383      obstack_free is really fast and one more obstack_alloc doesn't hurt if
384      we're going to see more expressions later on.  */
385   cur_expr = (struct expr *) obstack_alloc (&expr_obstack,
386 					    sizeof (struct expr));
387   cur_expr->expr = x;
388   cur_expr->hash = hash;
389   cur_expr->avail_occr = NULL;
390 
391   slot = expr_table->find_slot_with_hash (cur_expr, hash, INSERT);
392 
393   if (! (*slot))
394     {
395       /* The expression isn't found, so insert it.  */
396       *slot = cur_expr;
397 
398       /* Anytime we add an entry to the table, record the index
399 	 of the new entry.  The bitmap index starts counting
400 	 at zero.  */
401       cur_expr->bitmap_index = expr_table->elements () - 1;
402     }
403   else
404     {
405       /* The expression is already in the table, so roll back the
406 	 obstack and use the existing table entry.  */
407       obstack_free (&expr_obstack, cur_expr);
408       cur_expr = *slot;
409     }
410 
411   /* Search for another occurrence in the same basic block.  We insert
412      insns blockwise from start to end, so keep appending to the
413      start of the list so we have to check only a single element.  */
414   avail_occr = cur_expr->avail_occr;
415   if (avail_occr
416       && BLOCK_FOR_INSN (avail_occr->insn) == BLOCK_FOR_INSN (insn))
417     avail_occr->insn = insn;
418   else
419     {
420       /* First occurrence of this expression in this basic block.  */
421       avail_occr = (struct occr *) obstack_alloc (&occr_obstack,
422 						  sizeof (struct occr));
423       avail_occr->insn = insn;
424       avail_occr->next = cur_expr->avail_occr;
425       avail_occr->deleted_p = 0;
426       cur_expr->avail_occr = avail_occr;
427     }
428 }
429 
430 
431 /* Lookup pattern PAT in the expression hash table.
432    The result is a pointer to the table entry, or NULL if not found.  */
433 
434 static struct expr *
lookup_expr_in_table(rtx pat)435 lookup_expr_in_table (rtx pat)
436 {
437   int do_not_record_p;
438   struct expr **slot, *tmp_expr;
439   hashval_t hash = hash_expr (pat, &do_not_record_p);
440 
441   if (do_not_record_p)
442     return NULL;
443 
444   tmp_expr = (struct expr *) obstack_alloc (&expr_obstack,
445 					    sizeof (struct expr));
446   tmp_expr->expr = pat;
447   tmp_expr->hash = hash;
448   tmp_expr->avail_occr = NULL;
449 
450   slot = expr_table->find_slot_with_hash (tmp_expr, hash, INSERT);
451   obstack_free (&expr_obstack, tmp_expr);
452 
453   if (!slot)
454     return NULL;
455   else
456     return (*slot);
457 }
458 
459 
460 /* Dump all expressions and occurrences that are currently in the
461    expression hash table to FILE.  */
462 
463 /* This helper is called via htab_traverse.  */
464 int
dump_expr_hash_table_entry(expr ** slot,FILE * file)465 dump_expr_hash_table_entry (expr **slot, FILE *file)
466 {
467   struct expr *exprs = *slot;
468   struct occr *occr;
469 
470   fprintf (file, "expr: ");
471   print_rtl (file, exprs->expr);
472   fprintf (file,"\nhashcode: %u\n", exprs->hash);
473   fprintf (file,"list of occurrences:\n");
474   occr = exprs->avail_occr;
475   while (occr)
476     {
477       rtx_insn *insn = occr->insn;
478       print_rtl_single (file, insn);
479       fprintf (file, "\n");
480       occr = occr->next;
481     }
482   fprintf (file, "\n");
483   return 1;
484 }
485 
486 static void
dump_hash_table(FILE * file)487 dump_hash_table (FILE *file)
488 {
489   fprintf (file, "\n\nexpression hash table\n");
490   fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
491            (long) expr_table->size (),
492            (long) expr_table->elements (),
493            expr_table->collisions ());
494   if (!expr_table->is_empty ())
495     {
496       fprintf (file, "\n\ntable entries:\n");
497       expr_table->traverse <FILE *, dump_expr_hash_table_entry> (file);
498     }
499   fprintf (file, "\n");
500 }
501 
502 /* Return true if register X is recorded as being set by an instruction
503    whose CUID is greater than the one given.  */
504 
505 static bool
reg_changed_after_insn_p(rtx x,int cuid)506 reg_changed_after_insn_p (rtx x, int cuid)
507 {
508   unsigned int regno, end_regno;
509 
510   regno = REGNO (x);
511   end_regno = END_REGNO (x);
512   do
513     if (reg_avail_info[regno] > cuid)
514       return true;
515   while (++regno < end_regno);
516   return false;
517 }
518 
519 /* Return nonzero if the operands of expression X are unchanged
520    1) from the start of INSN's basic block up to but not including INSN
521       if AFTER_INSN is false, or
522    2) from INSN to the end of INSN's basic block if AFTER_INSN is true.  */
523 
524 static bool
oprs_unchanged_p(rtx x,rtx_insn * insn,bool after_insn)525 oprs_unchanged_p (rtx x, rtx_insn *insn, bool after_insn)
526 {
527   int i, j;
528   enum rtx_code code;
529   const char *fmt;
530 
531   if (x == 0)
532     return 1;
533 
534   code = GET_CODE (x);
535   switch (code)
536     {
537     case REG:
538       /* We are called after register allocation.  */
539       gcc_assert (REGNO (x) < FIRST_PSEUDO_REGISTER);
540       if (after_insn)
541 	return !reg_changed_after_insn_p (x, INSN_CUID (insn) - 1);
542       else
543 	return !reg_changed_after_insn_p (x, 0);
544 
545     case MEM:
546       if (load_killed_in_block_p (INSN_CUID (insn), x, after_insn))
547 	return 0;
548       else
549 	return oprs_unchanged_p (XEXP (x, 0), insn, after_insn);
550 
551     case PC:
552     case CONST:
553     CASE_CONST_ANY:
554     case SYMBOL_REF:
555     case LABEL_REF:
556     case ADDR_VEC:
557     case ADDR_DIFF_VEC:
558       return 1;
559 
560     case PRE_DEC:
561     case PRE_INC:
562     case POST_DEC:
563     case POST_INC:
564     case PRE_MODIFY:
565     case POST_MODIFY:
566       if (after_insn)
567 	return 0;
568       break;
569 
570     default:
571       break;
572     }
573 
574   for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
575     {
576       if (fmt[i] == 'e')
577 	{
578 	  if (! oprs_unchanged_p (XEXP (x, i), insn, after_insn))
579 	    return 0;
580 	}
581       else if (fmt[i] == 'E')
582 	for (j = 0; j < XVECLEN (x, i); j++)
583 	  if (! oprs_unchanged_p (XVECEXP (x, i, j), insn, after_insn))
584 	    return 0;
585     }
586 
587   return 1;
588 }
589 
590 
591 /* Used for communication between find_mem_conflicts and
592    load_killed_in_block_p.  Nonzero if find_mem_conflicts finds a
593    conflict between two memory references.
594    This is a bit of a hack to work around the limitations of note_stores.  */
595 static int mems_conflict_p;
596 
597 /* DEST is the output of an instruction.  If it is a memory reference, and
598    possibly conflicts with the load found in DATA, then set mems_conflict_p
599    to a nonzero value.  */
600 
601 static void
find_mem_conflicts(rtx dest,const_rtx setter ATTRIBUTE_UNUSED,void * data)602 find_mem_conflicts (rtx dest, const_rtx setter ATTRIBUTE_UNUSED,
603 		    void *data)
604 {
605   rtx mem_op = (rtx) data;
606 
607   while (GET_CODE (dest) == SUBREG
608 	 || GET_CODE (dest) == ZERO_EXTRACT
609 	 || GET_CODE (dest) == STRICT_LOW_PART)
610     dest = XEXP (dest, 0);
611 
612   /* If DEST is not a MEM, then it will not conflict with the load.  Note
613      that function calls are assumed to clobber memory, but are handled
614      elsewhere.  */
615   if (! MEM_P (dest))
616     return;
617 
618   if (true_dependence (dest, GET_MODE (dest), mem_op))
619     mems_conflict_p = 1;
620 }
621 
622 
623 /* Return nonzero if the expression in X (a memory reference) is killed
624    in the current basic block before (if AFTER_INSN is false) or after
625    (if AFTER_INSN is true) the insn with the CUID in UID_LIMIT.
626 
627    This function assumes that the modifies_mem table is flushed when
628    the hash table construction or redundancy elimination phases start
629    processing a new basic block.  */
630 
631 static int
load_killed_in_block_p(int uid_limit,rtx x,bool after_insn)632 load_killed_in_block_p (int uid_limit, rtx x, bool after_insn)
633 {
634   struct modifies_mem *list_entry = modifies_mem_list;
635 
636   while (list_entry)
637     {
638       rtx_insn *setter = list_entry->insn;
639 
640       /* Ignore entries in the list that do not apply.  */
641       if ((after_insn
642 	   && INSN_CUID (setter) < uid_limit)
643 	  || (! after_insn
644 	      && INSN_CUID (setter) > uid_limit))
645 	{
646 	  list_entry = list_entry->next;
647 	  continue;
648 	}
649 
650       /* If SETTER is a call everything is clobbered.  Note that calls
651 	 to pure functions are never put on the list, so we need not
652 	 worry about them.  */
653       if (CALL_P (setter))
654 	return 1;
655 
656       /* SETTER must be an insn of some kind that sets memory.  Call
657 	 note_stores to examine each hunk of memory that is modified.
658 	 It will set mems_conflict_p to nonzero if there may be a
659 	 conflict between X and SETTER.  */
660       mems_conflict_p = 0;
661       note_stores (setter, find_mem_conflicts, x);
662       if (mems_conflict_p)
663 	return 1;
664 
665       list_entry = list_entry->next;
666     }
667   return 0;
668 }
669 
670 
671 /* Record register first/last/block set information for REGNO in INSN.  */
672 
673 static inline void
record_last_reg_set_info(rtx_insn * insn,rtx reg)674 record_last_reg_set_info (rtx_insn *insn, rtx reg)
675 {
676   unsigned int regno, end_regno;
677 
678   regno = REGNO (reg);
679   end_regno = END_REGNO (reg);
680   do
681     reg_avail_info[regno] = INSN_CUID (insn);
682   while (++regno < end_regno);
683 }
684 
685 static inline void
record_last_reg_set_info_regno(rtx_insn * insn,int regno)686 record_last_reg_set_info_regno (rtx_insn *insn, int regno)
687 {
688   reg_avail_info[regno] = INSN_CUID (insn);
689 }
690 
691 
692 /* Record memory modification information for INSN.  We do not actually care
693    about the memory location(s) that are set, or even how they are set (consider
694    a CALL_INSN).  We merely need to record which insns modify memory.  */
695 
696 static void
record_last_mem_set_info(rtx_insn * insn)697 record_last_mem_set_info (rtx_insn *insn)
698 {
699   struct modifies_mem *list_entry;
700 
701   list_entry = (struct modifies_mem *) obstack_alloc (&modifies_mem_obstack,
702 						      sizeof (struct modifies_mem));
703   list_entry->insn = insn;
704   list_entry->next = modifies_mem_list;
705   modifies_mem_list = list_entry;
706 
707   record_last_mem_set_info_common (insn, modify_mem_list,
708 				   canon_modify_mem_list,
709 				   modify_mem_list_set,
710 				   blocks_with_calls);
711 }
712 
713 /* Called from compute_hash_table via note_stores to handle one
714    SET or CLOBBER in an insn.  DATA is really the instruction in which
715    the SET is taking place.  */
716 
717 static void
record_last_set_info(rtx dest,const_rtx setter ATTRIBUTE_UNUSED,void * data)718 record_last_set_info (rtx dest, const_rtx setter ATTRIBUTE_UNUSED, void *data)
719 {
720   rtx_insn *last_set_insn = (rtx_insn *) data;
721 
722   if (GET_CODE (dest) == SUBREG)
723     dest = SUBREG_REG (dest);
724 
725   if (REG_P (dest))
726     record_last_reg_set_info (last_set_insn, dest);
727   else if (MEM_P (dest))
728     {
729       /* Ignore pushes, they don't clobber memory.  They may still
730 	 clobber the stack pointer though.  Some targets do argument
731 	 pushes without adding REG_INC notes.  See e.g. PR25196,
732 	 where a pushsi2 on i386 doesn't have REG_INC notes.  Note
733 	 such changes here too.  */
734       if (! push_operand (dest, GET_MODE (dest)))
735 	record_last_mem_set_info (last_set_insn);
736       else
737 	record_last_reg_set_info_regno (last_set_insn, STACK_POINTER_REGNUM);
738     }
739 }
740 
741 
742 /* Reset tables used to keep track of what's still available since the
743    start of the block.  */
744 
745 static void
reset_opr_set_tables(void)746 reset_opr_set_tables (void)
747 {
748   memset (reg_avail_info, 0, FIRST_PSEUDO_REGISTER * sizeof (int));
749   obstack_free (&modifies_mem_obstack, modifies_mem_obstack_bottom);
750   modifies_mem_list = NULL;
751 }
752 
753 
754 /* Record things set by INSN.
755    This data is used by oprs_unchanged_p.  */
756 
757 static void
record_opr_changes(rtx_insn * insn)758 record_opr_changes (rtx_insn *insn)
759 {
760   rtx note;
761 
762   /* Find all stores and record them.  */
763   note_stores (insn, record_last_set_info, insn);
764 
765   /* Also record autoincremented REGs for this insn as changed.  */
766   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
767     if (REG_NOTE_KIND (note) == REG_INC)
768       record_last_reg_set_info (insn, XEXP (note, 0));
769 
770   /* Finally, if this is a call, record all call clobbers.  */
771   if (CALL_P (insn))
772     {
773       unsigned int regno;
774       hard_reg_set_iterator hrsi;
775       /* We don't track modes of hard registers, so we need to be
776 	 conservative and assume that partial kills are full kills.  */
777       HARD_REG_SET callee_clobbers
778 	= insn_callee_abi (insn).full_and_partial_reg_clobbers ();
779       EXECUTE_IF_SET_IN_HARD_REG_SET (callee_clobbers, 0, regno, hrsi)
780 	record_last_reg_set_info_regno (insn, regno);
781 
782       if (! RTL_CONST_OR_PURE_CALL_P (insn)
783 	  || RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
784 	  || can_throw_external (insn))
785 	record_last_mem_set_info (insn);
786     }
787 }
788 
789 
790 /* Scan the pattern of INSN and add an entry to the hash TABLE.
791    After reload we are interested in loads/stores only.  */
792 
793 static void
hash_scan_set(rtx_insn * insn)794 hash_scan_set (rtx_insn *insn)
795 {
796   rtx pat = PATTERN (insn);
797   rtx src = SET_SRC (pat);
798   rtx dest = SET_DEST (pat);
799 
800   /* We are only interested in loads and stores.  */
801   if (! MEM_P (src) && ! MEM_P (dest))
802     return;
803 
804   /* Don't mess with jumps and nops.  */
805   if (JUMP_P (insn) || set_noop_p (pat))
806     return;
807 
808   if (REG_P (dest))
809     {
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))
823 	{
824 	  insert_expr_in_table (src, insn);
825 	}
826     }
827   else if (REG_P (src))
828     {
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))
842 	{
843 	  insert_expr_in_table (dest, insn);
844 	}
845     }
846 }
847 
848 
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.  */
854 
855 static void
compute_hash_table(void)856 compute_hash_table (void)
857 {
858   basic_block bb;
859 
860   FOR_EACH_BB_FN (bb, cfun)
861     {
862       rtx_insn *insn;
863 
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)
871 	{
872 	  if (INSN_P (insn))
873             record_opr_changes (insn);
874 	}
875 
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);
880     }
881 }
882 
883 
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.  */
887 
888 static bool
reg_killed_on_edge(rtx reg,edge e)889 reg_killed_on_edge (rtx reg, edge e)
890 {
891   rtx_insn *insn;
892 
893   for (insn = e->insns.r; insn; insn = NEXT_INSN (insn))
894     if (INSN_P (insn) && reg_set_p (reg, insn))
895       return true;
896 
897   return false;
898 }
899 
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.  */
904 
905 static bool
reg_used_on_edge(rtx reg,edge e)906 reg_used_on_edge (rtx reg, edge e)
907 {
908   rtx_insn *insn;
909 
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;
913 
914   return false;
915 }
916 
917 /* Return the loaded/stored register of a load/store instruction.  */
918 
919 static rtx
get_avail_load_store_reg(rtx_insn * insn)920 get_avail_load_store_reg (rtx_insn *insn)
921 {
922   if (REG_P (SET_DEST (PATTERN (insn))))
923     /* A load.  */
924     return SET_DEST (PATTERN (insn));
925   else
926     {
927       /* A store.  */
928       gcc_assert (REG_P (SET_SRC (PATTERN (insn))));
929       return SET_SRC (PATTERN (insn));
930     }
931 }
932 
933 /* Return nonzero if the predecessors of BB are "well behaved".  */
934 
935 static bool
bb_has_well_behaved_predecessors(basic_block bb)936 bb_has_well_behaved_predecessors (basic_block bb)
937 {
938   edge pred;
939   edge_iterator ei;
940 
941   if (EDGE_COUNT (bb->preds) == 0)
942     return false;
943 
944   FOR_EACH_EDGE (pred, ei, bb->preds)
945     {
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;
950 
951       if ((pred->flags & EDGE_ABNORMAL_CALL) && cfun->has_nonlocal_label)
952 	return false;
953 
954       if (tablejump_p (BB_END (pred->src), NULL, NULL))
955 	return false;
956     }
957   return true;
958 }
959 
960 
961 /* Search for the occurrences of expression in BB.  */
962 
963 static struct occr*
get_bb_avail_insn(basic_block bb,struct occr * orig_occr,int bitmap_index)964 get_bb_avail_insn (basic_block bb, struct occr *orig_occr, int bitmap_index)
965 {
966   struct occr *occr = orig_occr;
967 
968   for (; occr != NULL; occr = occr->next)
969     if (BLOCK_FOR_INSN (occr->insn) == bb)
970       return occr;
971 
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)))
979     {
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;
986     }
987 
988   return NULL;
989 }
990 
991 
992 /* This helper is called via htab_traverse.  */
993 int
compute_expr_transp(expr ** slot,FILE * dump_file ATTRIBUTE_UNUSED)994 compute_expr_transp (expr **slot, FILE *dump_file ATTRIBUTE_UNUSED)
995 {
996   struct expr *expr = *slot;
997 
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;
1002 }
1003 
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.
1007 
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.
1013 
1014    See the function body for the heuristics that determine if eliminating
1015    a redundancy is also worth doing, assuming it is possible.  */
1016 
1017 static void
eliminate_partially_redundant_load(basic_block bb,rtx_insn * insn,struct expr * expr)1018 eliminate_partially_redundant_load (basic_block bb, rtx_insn *insn,
1019 				    struct expr *expr)
1020 {
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;
1035 
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;
1040 
1041   pat = PATTERN (insn);
1042   dest = SET_DEST (pat);
1043 
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;
1049 
1050   /* Check potential for replacing load with copy for predecessors.  */
1051   FOR_EACH_EDGE (pred, ei, bb->preds)
1052     {
1053       rtx_insn *next_pred_bb_end;
1054 
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))
1065 	{
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);
1070 
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))
1080 	    {
1081 	      avail_insn = NULL;
1082 	      continue;
1083 	    }
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;
1090 	}
1091 
1092       if (EDGE_CRITICAL_P (pred) && pred->count ().initialized_p ())
1093 	critical_count += pred->count ();
1094 
1095       if (avail_insn != NULL_RTX)
1096 	{
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)))))
1102 	    {
1103 	      /* Check if there is going to be a split.  */
1104 	      if (EDGE_CRITICAL_P (pred))
1105 		critical_edge_split = true;
1106 	    }
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;
1117 	}
1118       else
1119 	{
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;
1133 	}
1134     }
1135 
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;
1146 
1147   /* Check if it's worth applying the partial redundancy elimination.  */
1148   if (ok_count.to_gcov_type ()
1149       < param_gcse_after_reload_partial_fraction * not_ok_count.to_gcov_type ())
1150     goto cleanup;
1151 
1152   gcov_type threshold;
1153 #if (GCC_VERSION >= 5000)
1154   if (__builtin_mul_overflow (param_gcse_after_reload_critical_fraction,
1155 			      critical_count.to_gcov_type (), &threshold))
1156     threshold = profile_count::max_count;
1157 #else
1158   threshold
1159     = (param_gcse_after_reload_critical_fraction
1160        * critical_count.to_gcov_type ());
1161 #endif
1162 
1163   if (ok_count.to_gcov_type () < threshold)
1164     goto cleanup;
1165 
1166   /* Generate moves to the loaded register from where
1167      the memory is available.  */
1168   for (occr = avail_occrs; occr; occr = occr->next)
1169     {
1170       avail_insn = occr->insn;
1171       pred = occr->pred;
1172       /* Set avail_reg to be the register having the value of the
1173 	 memory.  */
1174       avail_reg = get_avail_load_store_reg (avail_insn);
1175       gcc_assert (avail_reg);
1176 
1177       insert_insn_on_edge (gen_move_insn (copy_rtx (dest),
1178 					  copy_rtx (avail_reg)),
1179 			   pred);
1180       stats.moves_inserted++;
1181 
1182       if (dump_file)
1183 	fprintf (dump_file,
1184 		 "generating move from %d to %d on edge from %d to %d\n",
1185 		 REGNO (avail_reg),
1186 		 REGNO (dest),
1187 		 pred->src->index,
1188 		 pred->dest->index);
1189     }
1190 
1191   /* Regenerate loads where the memory is unavailable.  */
1192   for (unoccr = unavail_occrs; unoccr; unoccr = unoccr->next)
1193     {
1194       pred = unoccr->pred;
1195       insert_insn_on_edge (copy_insn (PATTERN (insn)), pred);
1196       stats.copies_inserted++;
1197 
1198       if (dump_file)
1199 	{
1200 	  fprintf (dump_file,
1201 		   "generating on edge from %d to %d a copy of load: ",
1202 		   pred->src->index,
1203 		   pred->dest->index);
1204 	  print_rtl (dump_file, PATTERN (insn));
1205 	  fprintf (dump_file, "\n");
1206 	}
1207     }
1208 
1209   /* Delete the insn if it is not available in this block and mark it
1210      for deletion if it is available. If insn is available it may help
1211      discover additional redundancies, so mark it for later deletion.  */
1212   for (a_occr = get_bb_avail_insn (bb, expr->avail_occr, expr->bitmap_index);
1213        a_occr && (a_occr->insn != insn);
1214        a_occr = get_bb_avail_insn (bb, a_occr->next, expr->bitmap_index))
1215     ;
1216 
1217   if (!a_occr)
1218     {
1219       stats.insns_deleted++;
1220 
1221       if (dump_file)
1222 	{
1223 	  fprintf (dump_file, "deleting insn:\n");
1224           print_rtl_single (dump_file, insn);
1225           fprintf (dump_file, "\n");
1226 	}
1227       delete_insn (insn);
1228     }
1229   else
1230     a_occr->deleted_p = 1;
1231 
1232 cleanup:
1233   if (rollback_unoccr)
1234     obstack_free (&unoccr_obstack, rollback_unoccr);
1235 }
1236 
1237 /* Performing the redundancy elimination as described before.  */
1238 
1239 static void
eliminate_partially_redundant_loads(void)1240 eliminate_partially_redundant_loads (void)
1241 {
1242   rtx_insn *insn;
1243   basic_block bb;
1244 
1245   /* Note we start at block 1.  */
1246 
1247   if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1248     return;
1249 
1250   FOR_BB_BETWEEN (bb,
1251 		  ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb,
1252 		  EXIT_BLOCK_PTR_FOR_FN (cfun),
1253 		  next_bb)
1254     {
1255       /* Don't try anything on basic blocks with strange predecessors.  */
1256       if (! bb_has_well_behaved_predecessors (bb))
1257 	continue;
1258 
1259       /* Do not try anything on cold basic blocks.  */
1260       if (optimize_bb_for_size_p (bb))
1261 	continue;
1262 
1263       /* Reset the table of things changed since the start of the current
1264 	 basic block.  */
1265       reset_opr_set_tables ();
1266 
1267       /* Look at all insns in the current basic block and see if there are
1268 	 any loads in it that we can record.  */
1269       FOR_BB_INSNS (bb, insn)
1270 	{
1271 	  /* Is it a load - of the form (set (reg) (mem))?  */
1272 	  if (NONJUMP_INSN_P (insn)
1273               && GET_CODE (PATTERN (insn)) == SET
1274 	      && REG_P (SET_DEST (PATTERN (insn)))
1275 	      && MEM_P (SET_SRC (PATTERN (insn))))
1276 	    {
1277 	      rtx pat = PATTERN (insn);
1278 	      rtx src = SET_SRC (pat);
1279 	      struct expr *expr;
1280 
1281 	      if (!MEM_VOLATILE_P (src)
1282 		  && GET_MODE (src) != BLKmode
1283 		  && general_operand (src, GET_MODE (src))
1284 		  /* Are the operands unchanged since the start of the
1285 		     block?  */
1286 		  && oprs_unchanged_p (src, insn, false)
1287 		  && !(cfun->can_throw_non_call_exceptions && may_trap_p (src))
1288 		  && !side_effects_p (src)
1289 		  /* Is the expression recorded?  */
1290 		  && (expr = lookup_expr_in_table (src)) != NULL)
1291 		{
1292 		  /* We now have a load (insn) and an available memory at
1293 		     its BB start (expr). Try to remove the loads if it is
1294 		     redundant.  */
1295 		  eliminate_partially_redundant_load (bb, insn, expr);
1296 		}
1297 	    }
1298 
1299 	  /* Keep track of everything modified by this insn, so that we
1300 	     know what has been modified since the start of the current
1301 	     basic block.  */
1302 	  if (INSN_P (insn))
1303 	    record_opr_changes (insn);
1304 	}
1305     }
1306 
1307   commit_edge_insertions ();
1308 }
1309 
1310 /* Go over the expression hash table and delete insns that were
1311    marked for later deletion.  */
1312 
1313 /* This helper is called via htab_traverse.  */
1314 int
delete_redundant_insns_1(expr ** slot,void * data ATTRIBUTE_UNUSED)1315 delete_redundant_insns_1 (expr **slot, void *data ATTRIBUTE_UNUSED)
1316 {
1317   struct expr *exprs = *slot;
1318   struct occr *occr;
1319 
1320   for (occr = exprs->avail_occr; occr != NULL; occr = occr->next)
1321     {
1322       if (occr->deleted_p && dbg_cnt (gcse2_delete))
1323 	{
1324 	  delete_insn (occr->insn);
1325 	  stats.insns_deleted++;
1326 
1327 	  if (dump_file)
1328 	    {
1329 	      fprintf (dump_file, "deleting insn:\n");
1330 	      print_rtl_single (dump_file, occr->insn);
1331 	      fprintf (dump_file, "\n");
1332 	    }
1333 	}
1334     }
1335 
1336   return 1;
1337 }
1338 
1339 static void
delete_redundant_insns(void)1340 delete_redundant_insns (void)
1341 {
1342   expr_table->traverse <void *, delete_redundant_insns_1> (NULL);
1343   if (dump_file)
1344     fprintf (dump_file, "\n");
1345 }
1346 
1347 /* Main entry point of the GCSE after reload - clean some redundant loads
1348    due to spilling.  */
1349 
1350 static void
gcse_after_reload_main(rtx f ATTRIBUTE_UNUSED)1351 gcse_after_reload_main (rtx f ATTRIBUTE_UNUSED)
1352 {
1353   /* Disable computing transparentness if it is too expensive.  */
1354   bool do_transp
1355     = !gcse_or_cprop_is_too_expensive (_("using simple load CSE after register "
1356 					 "allocation"));
1357 
1358   memset (&stats, 0, sizeof (stats));
1359 
1360   /* Allocate memory for this pass.
1361      Also computes and initializes the insns' CUIDs.  */
1362   alloc_mem ();
1363 
1364   /* We need alias analysis.  */
1365   init_alias_analysis ();
1366 
1367   compute_hash_table ();
1368 
1369   if (dump_file)
1370     dump_hash_table (dump_file);
1371 
1372   if (!expr_table->is_empty ())
1373     {
1374       /* Knowing which MEMs are transparent through a block can signifiantly
1375 	 increase the number of redundant loads found.  So compute transparency
1376 	 information for each memory expression in the hash table.  */
1377       df_analyze ();
1378       if (do_transp)
1379 	{
1380 	  /* This cannot be part of the normal allocation routine because
1381 	     we have to know the number of elements in the hash table.  */
1382 	  transp = sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
1383 					 expr_table->elements ());
1384 	  bitmap_vector_ones (transp, last_basic_block_for_fn (cfun));
1385 	  expr_table->traverse <FILE *, compute_expr_transp> (dump_file);
1386 	}
1387       else
1388 	transp = NULL;
1389       eliminate_partially_redundant_loads ();
1390       delete_redundant_insns ();
1391       if (do_transp)
1392 	sbitmap_vector_free (transp);
1393 
1394       if (dump_file)
1395 	{
1396 	  fprintf (dump_file, "GCSE AFTER RELOAD stats:\n");
1397 	  fprintf (dump_file, "copies inserted: %d\n", stats.copies_inserted);
1398 	  fprintf (dump_file, "moves inserted:  %d\n", stats.moves_inserted);
1399 	  fprintf (dump_file, "insns deleted:   %d\n", stats.insns_deleted);
1400 	  fprintf (dump_file, "\n\n");
1401 	}
1402 
1403       statistics_counter_event (cfun, "copies inserted",
1404 				stats.copies_inserted);
1405       statistics_counter_event (cfun, "moves inserted",
1406 				stats.moves_inserted);
1407       statistics_counter_event (cfun, "insns deleted",
1408 				stats.insns_deleted);
1409     }
1410 
1411   /* We are finished with alias.  */
1412   end_alias_analysis ();
1413 
1414   free_mem ();
1415 }
1416 
1417 
1418 
1419 static unsigned int
rest_of_handle_gcse2(void)1420 rest_of_handle_gcse2 (void)
1421 {
1422   gcse_after_reload_main (get_insns ());
1423   rebuild_jump_labels (get_insns ());
1424   return 0;
1425 }
1426 
1427 namespace {
1428 
1429 const pass_data pass_data_gcse2 =
1430 {
1431   RTL_PASS, /* type */
1432   "gcse2", /* name */
1433   OPTGROUP_NONE, /* optinfo_flags */
1434   TV_GCSE_AFTER_RELOAD, /* tv_id */
1435   0, /* properties_required */
1436   0, /* properties_provided */
1437   0, /* properties_destroyed */
1438   0, /* todo_flags_start */
1439   0, /* todo_flags_finish */
1440 };
1441 
1442 class pass_gcse2 : public rtl_opt_pass
1443 {
1444 public:
pass_gcse2(gcc::context * ctxt)1445   pass_gcse2 (gcc::context *ctxt)
1446     : rtl_opt_pass (pass_data_gcse2, ctxt)
1447   {}
1448 
1449   /* opt_pass methods: */
gate(function * fun)1450   virtual bool gate (function *fun)
1451     {
1452       return (optimize > 0 && flag_gcse_after_reload
1453 	      && optimize_function_for_speed_p (fun));
1454     }
1455 
execute(function *)1456   virtual unsigned int execute (function *) { return rest_of_handle_gcse2 (); }
1457 
1458 }; // class pass_gcse2
1459 
1460 } // anon namespace
1461 
1462 rtl_opt_pass *
make_pass_gcse2(gcc::context * ctxt)1463 make_pass_gcse2 (gcc::context *ctxt)
1464 {
1465   return new pass_gcse2 (ctxt);
1466 }
1467