xref: /dflybsd-src/contrib/gcc-4.7/gcc/postreload-gcse.c (revision 04febcfb30580676d3e95f58a16c5137ee478b32)
1*e4b17023SJohn Marino /* Post reload partially redundant load elimination
2*e4b17023SJohn Marino    Copyright (C) 2004, 2005, 2006, 2007, 2008, 2010, 2011
3*e4b17023SJohn Marino    Free Software Foundation, Inc.
4*e4b17023SJohn Marino 
5*e4b17023SJohn Marino This file is part of GCC.
6*e4b17023SJohn Marino 
7*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
8*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
9*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later
10*e4b17023SJohn Marino version.
11*e4b17023SJohn Marino 
12*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
14*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15*e4b17023SJohn Marino for more details.
16*e4b17023SJohn Marino 
17*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
18*e4b17023SJohn Marino along with GCC; see the file COPYING3.  If not see
19*e4b17023SJohn Marino <http://www.gnu.org/licenses/>.  */
20*e4b17023SJohn Marino 
21*e4b17023SJohn Marino #include "config.h"
22*e4b17023SJohn Marino #include "system.h"
23*e4b17023SJohn Marino #include "coretypes.h"
24*e4b17023SJohn Marino #include "tm.h"
25*e4b17023SJohn Marino #include "diagnostic-core.h"
26*e4b17023SJohn Marino 
27*e4b17023SJohn Marino #include "rtl.h"
28*e4b17023SJohn Marino #include "tree.h"
29*e4b17023SJohn Marino #include "tm_p.h"
30*e4b17023SJohn Marino #include "regs.h"
31*e4b17023SJohn Marino #include "hard-reg-set.h"
32*e4b17023SJohn Marino #include "flags.h"
33*e4b17023SJohn Marino #include "insn-config.h"
34*e4b17023SJohn Marino #include "recog.h"
35*e4b17023SJohn Marino #include "basic-block.h"
36*e4b17023SJohn Marino #include "output.h"
37*e4b17023SJohn Marino #include "function.h"
38*e4b17023SJohn Marino #include "expr.h"
39*e4b17023SJohn Marino #include "except.h"
40*e4b17023SJohn Marino #include "intl.h"
41*e4b17023SJohn Marino #include "obstack.h"
42*e4b17023SJohn Marino #include "hashtab.h"
43*e4b17023SJohn Marino #include "params.h"
44*e4b17023SJohn Marino #include "target.h"
45*e4b17023SJohn Marino #include "timevar.h"
46*e4b17023SJohn Marino #include "tree-pass.h"
47*e4b17023SJohn Marino #include "dbgcnt.h"
48*e4b17023SJohn Marino 
49*e4b17023SJohn Marino /* The following code implements gcse after reload, the purpose of this
50*e4b17023SJohn Marino    pass is to cleanup redundant loads generated by reload and other
51*e4b17023SJohn Marino    optimizations that come after gcse. It searches for simple inter-block
52*e4b17023SJohn Marino    redundancies and tries to eliminate them by adding moves and loads
53*e4b17023SJohn Marino    in cold places.
54*e4b17023SJohn Marino 
55*e4b17023SJohn Marino    Perform partially redundant load elimination, try to eliminate redundant
56*e4b17023SJohn Marino    loads created by the reload pass.  We try to look for full or partial
57*e4b17023SJohn Marino    redundant loads fed by one or more loads/stores in predecessor BBs,
58*e4b17023SJohn Marino    and try adding loads to make them fully redundant.  We also check if
59*e4b17023SJohn Marino    it's worth adding loads to be able to delete the redundant load.
60*e4b17023SJohn Marino 
61*e4b17023SJohn Marino    Algorithm:
62*e4b17023SJohn Marino    1. Build available expressions hash table:
63*e4b17023SJohn Marino        For each load/store instruction, if the loaded/stored memory didn't
64*e4b17023SJohn Marino        change until the end of the basic block add this memory expression to
65*e4b17023SJohn Marino        the hash table.
66*e4b17023SJohn Marino    2. Perform Redundancy elimination:
67*e4b17023SJohn Marino       For each load instruction do the following:
68*e4b17023SJohn Marino 	 perform partial redundancy elimination, check if it's worth adding
69*e4b17023SJohn Marino 	 loads to make the load fully redundant.  If so add loads and
70*e4b17023SJohn Marino 	 register copies and delete the load.
71*e4b17023SJohn Marino    3. Delete instructions made redundant in step 2.
72*e4b17023SJohn Marino 
73*e4b17023SJohn Marino    Future enhancement:
74*e4b17023SJohn Marino      If the loaded register is used/defined between load and some store,
75*e4b17023SJohn Marino      look for some other free register between load and all its stores,
76*e4b17023SJohn Marino      and replace the load with a copy from this register to the loaded
77*e4b17023SJohn Marino      register.
78*e4b17023SJohn Marino */
79*e4b17023SJohn Marino 
80*e4b17023SJohn Marino 
81*e4b17023SJohn Marino /* Keep statistics of this pass.  */
82*e4b17023SJohn Marino static struct
83*e4b17023SJohn Marino {
84*e4b17023SJohn Marino   int moves_inserted;
85*e4b17023SJohn Marino   int copies_inserted;
86*e4b17023SJohn Marino   int insns_deleted;
87*e4b17023SJohn Marino } stats;
88*e4b17023SJohn Marino 
89*e4b17023SJohn Marino /* We need to keep a hash table of expressions.  The table entries are of
90*e4b17023SJohn Marino    type 'struct expr', and for each expression there is a single linked
91*e4b17023SJohn Marino    list of occurrences.  */
92*e4b17023SJohn Marino 
93*e4b17023SJohn Marino /* The table itself.  */
94*e4b17023SJohn Marino static htab_t expr_table;
95*e4b17023SJohn Marino 
96*e4b17023SJohn Marino /* Expression elements in the hash table.  */
97*e4b17023SJohn Marino struct expr
98*e4b17023SJohn Marino {
99*e4b17023SJohn Marino   /* The expression (SET_SRC for expressions, PATTERN for assignments).  */
100*e4b17023SJohn Marino   rtx expr;
101*e4b17023SJohn Marino 
102*e4b17023SJohn Marino   /* The same hash for this entry.  */
103*e4b17023SJohn Marino   hashval_t hash;
104*e4b17023SJohn Marino 
105*e4b17023SJohn Marino   /* List of available occurrence in basic blocks in the function.  */
106*e4b17023SJohn Marino   struct occr *avail_occr;
107*e4b17023SJohn Marino };
108*e4b17023SJohn Marino 
109*e4b17023SJohn Marino static struct obstack expr_obstack;
110*e4b17023SJohn Marino 
111*e4b17023SJohn Marino /* Occurrence of an expression.
112*e4b17023SJohn Marino    There is at most one occurrence per basic block.  If a pattern appears
113*e4b17023SJohn Marino    more than once, the last appearance is used.  */
114*e4b17023SJohn Marino 
115*e4b17023SJohn Marino struct occr
116*e4b17023SJohn Marino {
117*e4b17023SJohn Marino   /* Next occurrence of this expression.  */
118*e4b17023SJohn Marino   struct occr *next;
119*e4b17023SJohn Marino   /* The insn that computes the expression.  */
120*e4b17023SJohn Marino   rtx insn;
121*e4b17023SJohn Marino   /* Nonzero if this [anticipatable] occurrence has been deleted.  */
122*e4b17023SJohn Marino   char deleted_p;
123*e4b17023SJohn Marino };
124*e4b17023SJohn Marino 
125*e4b17023SJohn Marino static struct obstack occr_obstack;
126*e4b17023SJohn Marino 
127*e4b17023SJohn Marino /* The following structure holds the information about the occurrences of
128*e4b17023SJohn Marino    the redundant instructions.  */
129*e4b17023SJohn Marino struct unoccr
130*e4b17023SJohn Marino {
131*e4b17023SJohn Marino   struct unoccr *next;
132*e4b17023SJohn Marino   edge pred;
133*e4b17023SJohn Marino   rtx insn;
134*e4b17023SJohn Marino };
135*e4b17023SJohn Marino 
136*e4b17023SJohn Marino static struct obstack unoccr_obstack;
137*e4b17023SJohn Marino 
138*e4b17023SJohn Marino /* Array where each element is the CUID if the insn that last set the hard
139*e4b17023SJohn Marino    register with the number of the element, since the start of the current
140*e4b17023SJohn Marino    basic block.
141*e4b17023SJohn Marino 
142*e4b17023SJohn Marino    This array is used during the building of the hash table (step 1) to
143*e4b17023SJohn Marino    determine if a reg is killed before the end of a basic block.
144*e4b17023SJohn Marino 
145*e4b17023SJohn Marino    It is also used when eliminating partial redundancies (step 2) to see
146*e4b17023SJohn Marino    if a reg was modified since the start of a basic block.  */
147*e4b17023SJohn Marino static int *reg_avail_info;
148*e4b17023SJohn Marino 
149*e4b17023SJohn Marino /* A list of insns that may modify memory within the current basic block.  */
150*e4b17023SJohn Marino struct modifies_mem
151*e4b17023SJohn Marino {
152*e4b17023SJohn Marino   rtx insn;
153*e4b17023SJohn Marino   struct modifies_mem *next;
154*e4b17023SJohn Marino };
155*e4b17023SJohn Marino static struct modifies_mem *modifies_mem_list;
156*e4b17023SJohn Marino 
157*e4b17023SJohn Marino /* The modifies_mem structs also go on an obstack, only this obstack is
158*e4b17023SJohn Marino    freed each time after completing the analysis or transformations on
159*e4b17023SJohn Marino    a basic block.  So we allocate a dummy modifies_mem_obstack_bottom
160*e4b17023SJohn Marino    object on the obstack to keep track of the bottom of the obstack.  */
161*e4b17023SJohn Marino static struct obstack modifies_mem_obstack;
162*e4b17023SJohn Marino static struct modifies_mem  *modifies_mem_obstack_bottom;
163*e4b17023SJohn Marino 
164*e4b17023SJohn Marino /* Mapping of insn UIDs to CUIDs.
165*e4b17023SJohn Marino    CUIDs are like UIDs except they increase monotonically in each basic
166*e4b17023SJohn Marino    block, have no gaps, and only apply to real insns.  */
167*e4b17023SJohn Marino static int *uid_cuid;
168*e4b17023SJohn Marino #define INSN_CUID(INSN) (uid_cuid[INSN_UID (INSN)])
169*e4b17023SJohn Marino 
170*e4b17023SJohn Marino 
171*e4b17023SJohn Marino /* Helpers for memory allocation/freeing.  */
172*e4b17023SJohn Marino static void alloc_mem (void);
173*e4b17023SJohn Marino static void free_mem (void);
174*e4b17023SJohn Marino 
175*e4b17023SJohn Marino /* Support for hash table construction and transformations.  */
176*e4b17023SJohn Marino static bool oprs_unchanged_p (rtx, rtx, bool);
177*e4b17023SJohn Marino static void record_last_reg_set_info (rtx, rtx);
178*e4b17023SJohn Marino static void record_last_reg_set_info_regno (rtx, int);
179*e4b17023SJohn Marino static void record_last_mem_set_info (rtx);
180*e4b17023SJohn Marino static void record_last_set_info (rtx, const_rtx, void *);
181*e4b17023SJohn Marino static void record_opr_changes (rtx);
182*e4b17023SJohn Marino 
183*e4b17023SJohn Marino static void find_mem_conflicts (rtx, const_rtx, void *);
184*e4b17023SJohn Marino static int load_killed_in_block_p (int, rtx, bool);
185*e4b17023SJohn Marino static void reset_opr_set_tables (void);
186*e4b17023SJohn Marino 
187*e4b17023SJohn Marino /* Hash table support.  */
188*e4b17023SJohn Marino static hashval_t hash_expr (rtx, int *);
189*e4b17023SJohn Marino static hashval_t hash_expr_for_htab (const void *);
190*e4b17023SJohn Marino static int expr_equiv_p (const void *, const void *);
191*e4b17023SJohn Marino static void insert_expr_in_table (rtx, rtx);
192*e4b17023SJohn Marino static struct expr *lookup_expr_in_table (rtx);
193*e4b17023SJohn Marino static int dump_hash_table_entry (void **, void *);
194*e4b17023SJohn Marino static void dump_hash_table (FILE *);
195*e4b17023SJohn Marino 
196*e4b17023SJohn Marino /* Helpers for eliminate_partially_redundant_load.  */
197*e4b17023SJohn Marino static bool reg_killed_on_edge (rtx, edge);
198*e4b17023SJohn Marino static bool reg_used_on_edge (rtx, edge);
199*e4b17023SJohn Marino 
200*e4b17023SJohn Marino static rtx get_avail_load_store_reg (rtx);
201*e4b17023SJohn Marino 
202*e4b17023SJohn Marino static bool bb_has_well_behaved_predecessors (basic_block);
203*e4b17023SJohn Marino static struct occr* get_bb_avail_insn (basic_block, struct occr *);
204*e4b17023SJohn Marino static void hash_scan_set (rtx);
205*e4b17023SJohn Marino static void compute_hash_table (void);
206*e4b17023SJohn Marino 
207*e4b17023SJohn Marino /* The work horses of this pass.  */
208*e4b17023SJohn Marino static void eliminate_partially_redundant_load (basic_block,
209*e4b17023SJohn Marino 						rtx,
210*e4b17023SJohn Marino 						struct expr *);
211*e4b17023SJohn Marino static void eliminate_partially_redundant_loads (void);
212*e4b17023SJohn Marino 
213*e4b17023SJohn Marino 
214*e4b17023SJohn Marino /* Allocate memory for the CUID mapping array and register/memory
215*e4b17023SJohn Marino    tracking tables.  */
216*e4b17023SJohn Marino 
217*e4b17023SJohn Marino static void
alloc_mem(void)218*e4b17023SJohn Marino alloc_mem (void)
219*e4b17023SJohn Marino {
220*e4b17023SJohn Marino   int i;
221*e4b17023SJohn Marino   basic_block bb;
222*e4b17023SJohn Marino   rtx insn;
223*e4b17023SJohn Marino 
224*e4b17023SJohn Marino   /* Find the largest UID and create a mapping from UIDs to CUIDs.  */
225*e4b17023SJohn Marino   uid_cuid = XCNEWVEC (int, get_max_uid () + 1);
226*e4b17023SJohn Marino   i = 1;
227*e4b17023SJohn Marino   FOR_EACH_BB (bb)
228*e4b17023SJohn Marino     FOR_BB_INSNS (bb, insn)
229*e4b17023SJohn Marino       {
230*e4b17023SJohn Marino         if (INSN_P (insn))
231*e4b17023SJohn Marino 	  uid_cuid[INSN_UID (insn)] = i++;
232*e4b17023SJohn Marino 	else
233*e4b17023SJohn Marino 	  uid_cuid[INSN_UID (insn)] = i;
234*e4b17023SJohn Marino       }
235*e4b17023SJohn Marino 
236*e4b17023SJohn Marino   /* Allocate the available expressions hash table.  We don't want to
237*e4b17023SJohn Marino      make the hash table too small, but unnecessarily making it too large
238*e4b17023SJohn Marino      also doesn't help.  The i/4 is a gcse.c relic, and seems like a
239*e4b17023SJohn Marino      reasonable choice.  */
240*e4b17023SJohn Marino   expr_table = htab_create (MAX (i / 4, 13),
241*e4b17023SJohn Marino 			    hash_expr_for_htab, expr_equiv_p, NULL);
242*e4b17023SJohn Marino 
243*e4b17023SJohn Marino   /* We allocate everything on obstacks because we often can roll back
244*e4b17023SJohn Marino      the whole obstack to some point.  Freeing obstacks is very fast.  */
245*e4b17023SJohn Marino   gcc_obstack_init (&expr_obstack);
246*e4b17023SJohn Marino   gcc_obstack_init (&occr_obstack);
247*e4b17023SJohn Marino   gcc_obstack_init (&unoccr_obstack);
248*e4b17023SJohn Marino   gcc_obstack_init (&modifies_mem_obstack);
249*e4b17023SJohn Marino 
250*e4b17023SJohn Marino   /* Working array used to track the last set for each register
251*e4b17023SJohn Marino      in the current block.  */
252*e4b17023SJohn Marino   reg_avail_info = (int *) xmalloc (FIRST_PSEUDO_REGISTER * sizeof (int));
253*e4b17023SJohn Marino 
254*e4b17023SJohn Marino   /* Put a dummy modifies_mem object on the modifies_mem_obstack, so we
255*e4b17023SJohn Marino      can roll it back in reset_opr_set_tables.  */
256*e4b17023SJohn Marino   modifies_mem_obstack_bottom =
257*e4b17023SJohn Marino     (struct modifies_mem *) obstack_alloc (&modifies_mem_obstack,
258*e4b17023SJohn Marino 					   sizeof (struct modifies_mem));
259*e4b17023SJohn Marino }
260*e4b17023SJohn Marino 
261*e4b17023SJohn Marino /* Free memory allocated by alloc_mem.  */
262*e4b17023SJohn Marino 
263*e4b17023SJohn Marino static void
free_mem(void)264*e4b17023SJohn Marino free_mem (void)
265*e4b17023SJohn Marino {
266*e4b17023SJohn Marino   free (uid_cuid);
267*e4b17023SJohn Marino 
268*e4b17023SJohn Marino   htab_delete (expr_table);
269*e4b17023SJohn Marino 
270*e4b17023SJohn Marino   obstack_free (&expr_obstack, NULL);
271*e4b17023SJohn Marino   obstack_free (&occr_obstack, NULL);
272*e4b17023SJohn Marino   obstack_free (&unoccr_obstack, NULL);
273*e4b17023SJohn Marino   obstack_free (&modifies_mem_obstack, NULL);
274*e4b17023SJohn Marino 
275*e4b17023SJohn Marino   free (reg_avail_info);
276*e4b17023SJohn Marino }
277*e4b17023SJohn Marino 
278*e4b17023SJohn Marino 
279*e4b17023SJohn Marino /* Hash expression X.
280*e4b17023SJohn Marino    DO_NOT_RECORD_P is a boolean indicating if a volatile operand is found
281*e4b17023SJohn Marino    or if the expression contains something we don't want to insert in the
282*e4b17023SJohn Marino    table.  */
283*e4b17023SJohn Marino 
284*e4b17023SJohn Marino static hashval_t
hash_expr(rtx x,int * do_not_record_p)285*e4b17023SJohn Marino hash_expr (rtx x, int *do_not_record_p)
286*e4b17023SJohn Marino {
287*e4b17023SJohn Marino   *do_not_record_p = 0;
288*e4b17023SJohn Marino   return hash_rtx (x, GET_MODE (x), do_not_record_p,
289*e4b17023SJohn Marino 		   NULL,  /*have_reg_qty=*/false);
290*e4b17023SJohn Marino }
291*e4b17023SJohn Marino 
292*e4b17023SJohn Marino /* Callback for hashtab.
293*e4b17023SJohn Marino    Return the hash value for expression EXP.  We don't actually hash
294*e4b17023SJohn Marino    here, we just return the cached hash value.  */
295*e4b17023SJohn Marino 
296*e4b17023SJohn Marino static hashval_t
hash_expr_for_htab(const void * expp)297*e4b17023SJohn Marino hash_expr_for_htab (const void *expp)
298*e4b17023SJohn Marino {
299*e4b17023SJohn Marino   const struct expr *const exp = (const struct expr *) expp;
300*e4b17023SJohn Marino   return exp->hash;
301*e4b17023SJohn Marino }
302*e4b17023SJohn Marino 
303*e4b17023SJohn Marino /* Callback for hashtab.
304*e4b17023SJohn Marino    Return nonzero if exp1 is equivalent to exp2.  */
305*e4b17023SJohn Marino 
306*e4b17023SJohn Marino static int
expr_equiv_p(const void * exp1p,const void * exp2p)307*e4b17023SJohn Marino expr_equiv_p (const void *exp1p, const void *exp2p)
308*e4b17023SJohn Marino {
309*e4b17023SJohn Marino   const struct expr *const exp1 = (const struct expr *) exp1p;
310*e4b17023SJohn Marino   const struct expr *const exp2 = (const struct expr *) exp2p;
311*e4b17023SJohn Marino   int equiv_p = exp_equiv_p (exp1->expr, exp2->expr, 0, true);
312*e4b17023SJohn Marino 
313*e4b17023SJohn Marino   gcc_assert (!equiv_p || exp1->hash == exp2->hash);
314*e4b17023SJohn Marino   return equiv_p;
315*e4b17023SJohn Marino }
316*e4b17023SJohn Marino 
317*e4b17023SJohn Marino 
318*e4b17023SJohn Marino /* Insert expression X in INSN in the hash TABLE.
319*e4b17023SJohn Marino    If it is already present, record it as the last occurrence in INSN's
320*e4b17023SJohn Marino    basic block.  */
321*e4b17023SJohn Marino 
322*e4b17023SJohn Marino static void
insert_expr_in_table(rtx x,rtx insn)323*e4b17023SJohn Marino insert_expr_in_table (rtx x, rtx insn)
324*e4b17023SJohn Marino {
325*e4b17023SJohn Marino   int do_not_record_p;
326*e4b17023SJohn Marino   hashval_t hash;
327*e4b17023SJohn Marino   struct expr *cur_expr, **slot;
328*e4b17023SJohn Marino   struct occr *avail_occr, *last_occr = NULL;
329*e4b17023SJohn Marino 
330*e4b17023SJohn Marino   hash = hash_expr (x, &do_not_record_p);
331*e4b17023SJohn Marino 
332*e4b17023SJohn Marino   /* Do not insert expression in the table if it contains volatile operands,
333*e4b17023SJohn Marino      or if hash_expr determines the expression is something we don't want
334*e4b17023SJohn Marino      to or can't handle.  */
335*e4b17023SJohn Marino   if (do_not_record_p)
336*e4b17023SJohn Marino     return;
337*e4b17023SJohn Marino 
338*e4b17023SJohn Marino   /* We anticipate that redundant expressions are rare, so for convenience
339*e4b17023SJohn Marino      allocate a new hash table element here already and set its fields.
340*e4b17023SJohn Marino      If we don't do this, we need a hack with a static struct expr.  Anyway,
341*e4b17023SJohn Marino      obstack_free is really fast and one more obstack_alloc doesn't hurt if
342*e4b17023SJohn Marino      we're going to see more expressions later on.  */
343*e4b17023SJohn Marino   cur_expr = (struct expr *) obstack_alloc (&expr_obstack,
344*e4b17023SJohn Marino 					    sizeof (struct expr));
345*e4b17023SJohn Marino   cur_expr->expr = x;
346*e4b17023SJohn Marino   cur_expr->hash = hash;
347*e4b17023SJohn Marino   cur_expr->avail_occr = NULL;
348*e4b17023SJohn Marino 
349*e4b17023SJohn Marino   slot = (struct expr **) htab_find_slot_with_hash (expr_table, cur_expr,
350*e4b17023SJohn Marino 						    hash, INSERT);
351*e4b17023SJohn Marino 
352*e4b17023SJohn Marino   if (! (*slot))
353*e4b17023SJohn Marino     /* The expression isn't found, so insert it.  */
354*e4b17023SJohn Marino     *slot = cur_expr;
355*e4b17023SJohn Marino   else
356*e4b17023SJohn Marino     {
357*e4b17023SJohn Marino       /* The expression is already in the table, so roll back the
358*e4b17023SJohn Marino 	 obstack and use the existing table entry.  */
359*e4b17023SJohn Marino       obstack_free (&expr_obstack, cur_expr);
360*e4b17023SJohn Marino       cur_expr = *slot;
361*e4b17023SJohn Marino     }
362*e4b17023SJohn Marino 
363*e4b17023SJohn Marino   /* Search for another occurrence in the same basic block.  */
364*e4b17023SJohn Marino   avail_occr = cur_expr->avail_occr;
365*e4b17023SJohn Marino   while (avail_occr
366*e4b17023SJohn Marino 	 && BLOCK_FOR_INSN (avail_occr->insn) != BLOCK_FOR_INSN (insn))
367*e4b17023SJohn Marino     {
368*e4b17023SJohn Marino       /* If an occurrence isn't found, save a pointer to the end of
369*e4b17023SJohn Marino 	 the list.  */
370*e4b17023SJohn Marino       last_occr = avail_occr;
371*e4b17023SJohn Marino       avail_occr = avail_occr->next;
372*e4b17023SJohn Marino     }
373*e4b17023SJohn Marino 
374*e4b17023SJohn Marino   if (avail_occr)
375*e4b17023SJohn Marino     /* Found another instance of the expression in the same basic block.
376*e4b17023SJohn Marino        Prefer this occurrence to the currently recorded one.  We want
377*e4b17023SJohn Marino        the last one in the block and the block is scanned from start
378*e4b17023SJohn Marino        to end.  */
379*e4b17023SJohn Marino     avail_occr->insn = insn;
380*e4b17023SJohn Marino   else
381*e4b17023SJohn Marino     {
382*e4b17023SJohn Marino       /* First occurrence of this expression in this basic block.  */
383*e4b17023SJohn Marino       avail_occr = (struct occr *) obstack_alloc (&occr_obstack,
384*e4b17023SJohn Marino 						  sizeof (struct occr));
385*e4b17023SJohn Marino 
386*e4b17023SJohn Marino       /* First occurrence of this expression in any block?  */
387*e4b17023SJohn Marino       if (cur_expr->avail_occr == NULL)
388*e4b17023SJohn Marino         cur_expr->avail_occr = avail_occr;
389*e4b17023SJohn Marino       else
390*e4b17023SJohn Marino         last_occr->next = avail_occr;
391*e4b17023SJohn Marino 
392*e4b17023SJohn Marino       avail_occr->insn = insn;
393*e4b17023SJohn Marino       avail_occr->next = NULL;
394*e4b17023SJohn Marino       avail_occr->deleted_p = 0;
395*e4b17023SJohn Marino     }
396*e4b17023SJohn Marino }
397*e4b17023SJohn Marino 
398*e4b17023SJohn Marino 
399*e4b17023SJohn Marino /* Lookup pattern PAT in the expression hash table.
400*e4b17023SJohn Marino    The result is a pointer to the table entry, or NULL if not found.  */
401*e4b17023SJohn Marino 
402*e4b17023SJohn Marino static struct expr *
lookup_expr_in_table(rtx pat)403*e4b17023SJohn Marino lookup_expr_in_table (rtx pat)
404*e4b17023SJohn Marino {
405*e4b17023SJohn Marino   int do_not_record_p;
406*e4b17023SJohn Marino   struct expr **slot, *tmp_expr;
407*e4b17023SJohn Marino   hashval_t hash = hash_expr (pat, &do_not_record_p);
408*e4b17023SJohn Marino 
409*e4b17023SJohn Marino   if (do_not_record_p)
410*e4b17023SJohn Marino     return NULL;
411*e4b17023SJohn Marino 
412*e4b17023SJohn Marino   tmp_expr = (struct expr *) obstack_alloc (&expr_obstack,
413*e4b17023SJohn Marino 					    sizeof (struct expr));
414*e4b17023SJohn Marino   tmp_expr->expr = pat;
415*e4b17023SJohn Marino   tmp_expr->hash = hash;
416*e4b17023SJohn Marino   tmp_expr->avail_occr = NULL;
417*e4b17023SJohn Marino 
418*e4b17023SJohn Marino   slot = (struct expr **) htab_find_slot_with_hash (expr_table, tmp_expr,
419*e4b17023SJohn Marino                                                     hash, INSERT);
420*e4b17023SJohn Marino   obstack_free (&expr_obstack, tmp_expr);
421*e4b17023SJohn Marino 
422*e4b17023SJohn Marino   if (!slot)
423*e4b17023SJohn Marino     return NULL;
424*e4b17023SJohn Marino   else
425*e4b17023SJohn Marino     return (*slot);
426*e4b17023SJohn Marino }
427*e4b17023SJohn Marino 
428*e4b17023SJohn Marino 
429*e4b17023SJohn Marino /* Dump all expressions and occurrences that are currently in the
430*e4b17023SJohn Marino    expression hash table to FILE.  */
431*e4b17023SJohn Marino 
432*e4b17023SJohn Marino /* This helper is called via htab_traverse.  */
433*e4b17023SJohn Marino static int
dump_hash_table_entry(void ** slot,void * filep)434*e4b17023SJohn Marino dump_hash_table_entry (void **slot, void *filep)
435*e4b17023SJohn Marino {
436*e4b17023SJohn Marino   struct expr *expr = (struct expr *) *slot;
437*e4b17023SJohn Marino   FILE *file = (FILE *) filep;
438*e4b17023SJohn Marino   struct occr *occr;
439*e4b17023SJohn Marino 
440*e4b17023SJohn Marino   fprintf (file, "expr: ");
441*e4b17023SJohn Marino   print_rtl (file, expr->expr);
442*e4b17023SJohn Marino   fprintf (file,"\nhashcode: %u\n", expr->hash);
443*e4b17023SJohn Marino   fprintf (file,"list of occurrences:\n");
444*e4b17023SJohn Marino   occr = expr->avail_occr;
445*e4b17023SJohn Marino   while (occr)
446*e4b17023SJohn Marino     {
447*e4b17023SJohn Marino       rtx insn = occr->insn;
448*e4b17023SJohn Marino       print_rtl_single (file, insn);
449*e4b17023SJohn Marino       fprintf (file, "\n");
450*e4b17023SJohn Marino       occr = occr->next;
451*e4b17023SJohn Marino     }
452*e4b17023SJohn Marino   fprintf (file, "\n");
453*e4b17023SJohn Marino   return 1;
454*e4b17023SJohn Marino }
455*e4b17023SJohn Marino 
456*e4b17023SJohn Marino static void
dump_hash_table(FILE * file)457*e4b17023SJohn Marino dump_hash_table (FILE *file)
458*e4b17023SJohn Marino {
459*e4b17023SJohn Marino   fprintf (file, "\n\nexpression hash table\n");
460*e4b17023SJohn Marino   fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
461*e4b17023SJohn Marino            (long) htab_size (expr_table),
462*e4b17023SJohn Marino            (long) htab_elements (expr_table),
463*e4b17023SJohn Marino            htab_collisions (expr_table));
464*e4b17023SJohn Marino   if (htab_elements (expr_table) > 0)
465*e4b17023SJohn Marino     {
466*e4b17023SJohn Marino       fprintf (file, "\n\ntable entries:\n");
467*e4b17023SJohn Marino       htab_traverse (expr_table, dump_hash_table_entry, file);
468*e4b17023SJohn Marino     }
469*e4b17023SJohn Marino   fprintf (file, "\n");
470*e4b17023SJohn Marino }
471*e4b17023SJohn Marino 
472*e4b17023SJohn Marino /* Return true if register X is recorded as being set by an instruction
473*e4b17023SJohn Marino    whose CUID is greater than the one given.  */
474*e4b17023SJohn Marino 
475*e4b17023SJohn Marino static bool
reg_changed_after_insn_p(rtx x,int cuid)476*e4b17023SJohn Marino reg_changed_after_insn_p (rtx x, int cuid)
477*e4b17023SJohn Marino {
478*e4b17023SJohn Marino   unsigned int regno, end_regno;
479*e4b17023SJohn Marino 
480*e4b17023SJohn Marino   regno = REGNO (x);
481*e4b17023SJohn Marino   end_regno = END_HARD_REGNO (x);
482*e4b17023SJohn Marino   do
483*e4b17023SJohn Marino     if (reg_avail_info[regno] > cuid)
484*e4b17023SJohn Marino       return true;
485*e4b17023SJohn Marino   while (++regno < end_regno);
486*e4b17023SJohn Marino   return false;
487*e4b17023SJohn Marino }
488*e4b17023SJohn Marino 
489*e4b17023SJohn Marino /* Return nonzero if the operands of expression X are unchanged
490*e4b17023SJohn Marino    1) from the start of INSN's basic block up to but not including INSN
491*e4b17023SJohn Marino       if AFTER_INSN is false, or
492*e4b17023SJohn Marino    2) from INSN to the end of INSN's basic block if AFTER_INSN is true.  */
493*e4b17023SJohn Marino 
494*e4b17023SJohn Marino static bool
oprs_unchanged_p(rtx x,rtx insn,bool after_insn)495*e4b17023SJohn Marino oprs_unchanged_p (rtx x, rtx insn, bool after_insn)
496*e4b17023SJohn Marino {
497*e4b17023SJohn Marino   int i, j;
498*e4b17023SJohn Marino   enum rtx_code code;
499*e4b17023SJohn Marino   const char *fmt;
500*e4b17023SJohn Marino 
501*e4b17023SJohn Marino   if (x == 0)
502*e4b17023SJohn Marino     return 1;
503*e4b17023SJohn Marino 
504*e4b17023SJohn Marino   code = GET_CODE (x);
505*e4b17023SJohn Marino   switch (code)
506*e4b17023SJohn Marino     {
507*e4b17023SJohn Marino     case REG:
508*e4b17023SJohn Marino       /* We are called after register allocation.  */
509*e4b17023SJohn Marino       gcc_assert (REGNO (x) < FIRST_PSEUDO_REGISTER);
510*e4b17023SJohn Marino       if (after_insn)
511*e4b17023SJohn Marino 	return !reg_changed_after_insn_p (x, INSN_CUID (insn) - 1);
512*e4b17023SJohn Marino       else
513*e4b17023SJohn Marino 	return !reg_changed_after_insn_p (x, 0);
514*e4b17023SJohn Marino 
515*e4b17023SJohn Marino     case MEM:
516*e4b17023SJohn Marino       if (load_killed_in_block_p (INSN_CUID (insn), x, after_insn))
517*e4b17023SJohn Marino 	return 0;
518*e4b17023SJohn Marino       else
519*e4b17023SJohn Marino 	return oprs_unchanged_p (XEXP (x, 0), insn, after_insn);
520*e4b17023SJohn Marino 
521*e4b17023SJohn Marino     case PC:
522*e4b17023SJohn Marino     case CC0: /*FIXME*/
523*e4b17023SJohn Marino     case CONST:
524*e4b17023SJohn Marino     case CONST_INT:
525*e4b17023SJohn Marino     case CONST_DOUBLE:
526*e4b17023SJohn Marino     case CONST_FIXED:
527*e4b17023SJohn Marino     case CONST_VECTOR:
528*e4b17023SJohn Marino     case SYMBOL_REF:
529*e4b17023SJohn Marino     case LABEL_REF:
530*e4b17023SJohn Marino     case ADDR_VEC:
531*e4b17023SJohn Marino     case ADDR_DIFF_VEC:
532*e4b17023SJohn Marino       return 1;
533*e4b17023SJohn Marino 
534*e4b17023SJohn Marino     case PRE_DEC:
535*e4b17023SJohn Marino     case PRE_INC:
536*e4b17023SJohn Marino     case POST_DEC:
537*e4b17023SJohn Marino     case POST_INC:
538*e4b17023SJohn Marino     case PRE_MODIFY:
539*e4b17023SJohn Marino     case POST_MODIFY:
540*e4b17023SJohn Marino       if (after_insn)
541*e4b17023SJohn Marino 	return 0;
542*e4b17023SJohn Marino       break;
543*e4b17023SJohn Marino 
544*e4b17023SJohn Marino     default:
545*e4b17023SJohn Marino       break;
546*e4b17023SJohn Marino     }
547*e4b17023SJohn Marino 
548*e4b17023SJohn Marino   for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
549*e4b17023SJohn Marino     {
550*e4b17023SJohn Marino       if (fmt[i] == 'e')
551*e4b17023SJohn Marino 	{
552*e4b17023SJohn Marino 	  if (! oprs_unchanged_p (XEXP (x, i), insn, after_insn))
553*e4b17023SJohn Marino 	    return 0;
554*e4b17023SJohn Marino 	}
555*e4b17023SJohn Marino       else if (fmt[i] == 'E')
556*e4b17023SJohn Marino 	for (j = 0; j < XVECLEN (x, i); j++)
557*e4b17023SJohn Marino 	  if (! oprs_unchanged_p (XVECEXP (x, i, j), insn, after_insn))
558*e4b17023SJohn Marino 	    return 0;
559*e4b17023SJohn Marino     }
560*e4b17023SJohn Marino 
561*e4b17023SJohn Marino   return 1;
562*e4b17023SJohn Marino }
563*e4b17023SJohn Marino 
564*e4b17023SJohn Marino 
565*e4b17023SJohn Marino /* Used for communication between find_mem_conflicts and
566*e4b17023SJohn Marino    load_killed_in_block_p.  Nonzero if find_mem_conflicts finds a
567*e4b17023SJohn Marino    conflict between two memory references.
568*e4b17023SJohn Marino    This is a bit of a hack to work around the limitations of note_stores.  */
569*e4b17023SJohn Marino static int mems_conflict_p;
570*e4b17023SJohn Marino 
571*e4b17023SJohn Marino /* DEST is the output of an instruction.  If it is a memory reference, and
572*e4b17023SJohn Marino    possibly conflicts with the load found in DATA, then set mems_conflict_p
573*e4b17023SJohn Marino    to a nonzero value.  */
574*e4b17023SJohn Marino 
575*e4b17023SJohn Marino static void
find_mem_conflicts(rtx dest,const_rtx setter ATTRIBUTE_UNUSED,void * data)576*e4b17023SJohn Marino find_mem_conflicts (rtx dest, const_rtx setter ATTRIBUTE_UNUSED,
577*e4b17023SJohn Marino 		    void *data)
578*e4b17023SJohn Marino {
579*e4b17023SJohn Marino   rtx mem_op = (rtx) data;
580*e4b17023SJohn Marino 
581*e4b17023SJohn Marino   while (GET_CODE (dest) == SUBREG
582*e4b17023SJohn Marino 	 || GET_CODE (dest) == ZERO_EXTRACT
583*e4b17023SJohn Marino 	 || GET_CODE (dest) == STRICT_LOW_PART)
584*e4b17023SJohn Marino     dest = XEXP (dest, 0);
585*e4b17023SJohn Marino 
586*e4b17023SJohn Marino   /* If DEST is not a MEM, then it will not conflict with the load.  Note
587*e4b17023SJohn Marino      that function calls are assumed to clobber memory, but are handled
588*e4b17023SJohn Marino      elsewhere.  */
589*e4b17023SJohn Marino   if (! MEM_P (dest))
590*e4b17023SJohn Marino     return;
591*e4b17023SJohn Marino 
592*e4b17023SJohn Marino   if (true_dependence (dest, GET_MODE (dest), mem_op))
593*e4b17023SJohn Marino     mems_conflict_p = 1;
594*e4b17023SJohn Marino }
595*e4b17023SJohn Marino 
596*e4b17023SJohn Marino 
597*e4b17023SJohn Marino /* Return nonzero if the expression in X (a memory reference) is killed
598*e4b17023SJohn Marino    in the current basic block before (if AFTER_INSN is false) or after
599*e4b17023SJohn Marino    (if AFTER_INSN is true) the insn with the CUID in UID_LIMIT.
600*e4b17023SJohn Marino 
601*e4b17023SJohn Marino    This function assumes that the modifies_mem table is flushed when
602*e4b17023SJohn Marino    the hash table construction or redundancy elimination phases start
603*e4b17023SJohn Marino    processing a new basic block.  */
604*e4b17023SJohn Marino 
605*e4b17023SJohn Marino static int
load_killed_in_block_p(int uid_limit,rtx x,bool after_insn)606*e4b17023SJohn Marino load_killed_in_block_p (int uid_limit, rtx x, bool after_insn)
607*e4b17023SJohn Marino {
608*e4b17023SJohn Marino   struct modifies_mem *list_entry = modifies_mem_list;
609*e4b17023SJohn Marino 
610*e4b17023SJohn Marino   while (list_entry)
611*e4b17023SJohn Marino     {
612*e4b17023SJohn Marino       rtx setter = list_entry->insn;
613*e4b17023SJohn Marino 
614*e4b17023SJohn Marino       /* Ignore entries in the list that do not apply.  */
615*e4b17023SJohn Marino       if ((after_insn
616*e4b17023SJohn Marino 	   && INSN_CUID (setter) < uid_limit)
617*e4b17023SJohn Marino 	  || (! after_insn
618*e4b17023SJohn Marino 	      && INSN_CUID (setter) > uid_limit))
619*e4b17023SJohn Marino 	{
620*e4b17023SJohn Marino 	  list_entry = list_entry->next;
621*e4b17023SJohn Marino 	  continue;
622*e4b17023SJohn Marino 	}
623*e4b17023SJohn Marino 
624*e4b17023SJohn Marino       /* If SETTER is a call everything is clobbered.  Note that calls
625*e4b17023SJohn Marino 	 to pure functions are never put on the list, so we need not
626*e4b17023SJohn Marino 	 worry about them.  */
627*e4b17023SJohn Marino       if (CALL_P (setter))
628*e4b17023SJohn Marino 	return 1;
629*e4b17023SJohn Marino 
630*e4b17023SJohn Marino       /* SETTER must be an insn of some kind that sets memory.  Call
631*e4b17023SJohn Marino 	 note_stores to examine each hunk of memory that is modified.
632*e4b17023SJohn Marino 	 It will set mems_conflict_p to nonzero if there may be a
633*e4b17023SJohn Marino 	 conflict between X and SETTER.  */
634*e4b17023SJohn Marino       mems_conflict_p = 0;
635*e4b17023SJohn Marino       note_stores (PATTERN (setter), find_mem_conflicts, x);
636*e4b17023SJohn Marino       if (mems_conflict_p)
637*e4b17023SJohn Marino 	return 1;
638*e4b17023SJohn Marino 
639*e4b17023SJohn Marino       list_entry = list_entry->next;
640*e4b17023SJohn Marino     }
641*e4b17023SJohn Marino   return 0;
642*e4b17023SJohn Marino }
643*e4b17023SJohn Marino 
644*e4b17023SJohn Marino 
645*e4b17023SJohn Marino /* Record register first/last/block set information for REGNO in INSN.  */
646*e4b17023SJohn Marino 
647*e4b17023SJohn Marino static inline void
record_last_reg_set_info(rtx insn,rtx reg)648*e4b17023SJohn Marino record_last_reg_set_info (rtx insn, rtx reg)
649*e4b17023SJohn Marino {
650*e4b17023SJohn Marino   unsigned int regno, end_regno;
651*e4b17023SJohn Marino 
652*e4b17023SJohn Marino   regno = REGNO (reg);
653*e4b17023SJohn Marino   end_regno = END_HARD_REGNO (reg);
654*e4b17023SJohn Marino   do
655*e4b17023SJohn Marino     reg_avail_info[regno] = INSN_CUID (insn);
656*e4b17023SJohn Marino   while (++regno < end_regno);
657*e4b17023SJohn Marino }
658*e4b17023SJohn Marino 
659*e4b17023SJohn Marino static inline void
record_last_reg_set_info_regno(rtx insn,int regno)660*e4b17023SJohn Marino record_last_reg_set_info_regno (rtx insn, int regno)
661*e4b17023SJohn Marino {
662*e4b17023SJohn Marino   reg_avail_info[regno] = INSN_CUID (insn);
663*e4b17023SJohn Marino }
664*e4b17023SJohn Marino 
665*e4b17023SJohn Marino 
666*e4b17023SJohn Marino /* Record memory modification information for INSN.  We do not actually care
667*e4b17023SJohn Marino    about the memory location(s) that are set, or even how they are set (consider
668*e4b17023SJohn Marino    a CALL_INSN).  We merely need to record which insns modify memory.  */
669*e4b17023SJohn Marino 
670*e4b17023SJohn Marino static void
record_last_mem_set_info(rtx insn)671*e4b17023SJohn Marino record_last_mem_set_info (rtx insn)
672*e4b17023SJohn Marino {
673*e4b17023SJohn Marino   struct modifies_mem *list_entry;
674*e4b17023SJohn Marino 
675*e4b17023SJohn Marino   list_entry = (struct modifies_mem *) obstack_alloc (&modifies_mem_obstack,
676*e4b17023SJohn Marino 						      sizeof (struct modifies_mem));
677*e4b17023SJohn Marino   list_entry->insn = insn;
678*e4b17023SJohn Marino   list_entry->next = modifies_mem_list;
679*e4b17023SJohn Marino   modifies_mem_list = list_entry;
680*e4b17023SJohn Marino }
681*e4b17023SJohn Marino 
682*e4b17023SJohn Marino /* Called from compute_hash_table via note_stores to handle one
683*e4b17023SJohn Marino    SET or CLOBBER in an insn.  DATA is really the instruction in which
684*e4b17023SJohn Marino    the SET is taking place.  */
685*e4b17023SJohn Marino 
686*e4b17023SJohn Marino static void
record_last_set_info(rtx dest,const_rtx setter ATTRIBUTE_UNUSED,void * data)687*e4b17023SJohn Marino record_last_set_info (rtx dest, const_rtx setter ATTRIBUTE_UNUSED, void *data)
688*e4b17023SJohn Marino {
689*e4b17023SJohn Marino   rtx last_set_insn = (rtx) data;
690*e4b17023SJohn Marino 
691*e4b17023SJohn Marino   if (GET_CODE (dest) == SUBREG)
692*e4b17023SJohn Marino     dest = SUBREG_REG (dest);
693*e4b17023SJohn Marino 
694*e4b17023SJohn Marino   if (REG_P (dest))
695*e4b17023SJohn Marino     record_last_reg_set_info (last_set_insn, dest);
696*e4b17023SJohn Marino   else if (MEM_P (dest))
697*e4b17023SJohn Marino     {
698*e4b17023SJohn Marino       /* Ignore pushes, they don't clobber memory.  They may still
699*e4b17023SJohn Marino 	 clobber the stack pointer though.  Some targets do argument
700*e4b17023SJohn Marino 	 pushes without adding REG_INC notes.  See e.g. PR25196,
701*e4b17023SJohn Marino 	 where a pushsi2 on i386 doesn't have REG_INC notes.  Note
702*e4b17023SJohn Marino 	 such changes here too.  */
703*e4b17023SJohn Marino       if (! push_operand (dest, GET_MODE (dest)))
704*e4b17023SJohn Marino 	record_last_mem_set_info (last_set_insn);
705*e4b17023SJohn Marino       else
706*e4b17023SJohn Marino 	record_last_reg_set_info_regno (last_set_insn, STACK_POINTER_REGNUM);
707*e4b17023SJohn Marino     }
708*e4b17023SJohn Marino }
709*e4b17023SJohn Marino 
710*e4b17023SJohn Marino 
711*e4b17023SJohn Marino /* Reset tables used to keep track of what's still available since the
712*e4b17023SJohn Marino    start of the block.  */
713*e4b17023SJohn Marino 
714*e4b17023SJohn Marino static void
reset_opr_set_tables(void)715*e4b17023SJohn Marino reset_opr_set_tables (void)
716*e4b17023SJohn Marino {
717*e4b17023SJohn Marino   memset (reg_avail_info, 0, FIRST_PSEUDO_REGISTER * sizeof (int));
718*e4b17023SJohn Marino   obstack_free (&modifies_mem_obstack, modifies_mem_obstack_bottom);
719*e4b17023SJohn Marino   modifies_mem_list = NULL;
720*e4b17023SJohn Marino }
721*e4b17023SJohn Marino 
722*e4b17023SJohn Marino 
723*e4b17023SJohn Marino /* Record things set by INSN.
724*e4b17023SJohn Marino    This data is used by oprs_unchanged_p.  */
725*e4b17023SJohn Marino 
726*e4b17023SJohn Marino static void
record_opr_changes(rtx insn)727*e4b17023SJohn Marino record_opr_changes (rtx insn)
728*e4b17023SJohn Marino {
729*e4b17023SJohn Marino   rtx note;
730*e4b17023SJohn Marino 
731*e4b17023SJohn Marino   /* Find all stores and record them.  */
732*e4b17023SJohn Marino   note_stores (PATTERN (insn), record_last_set_info, insn);
733*e4b17023SJohn Marino 
734*e4b17023SJohn Marino   /* Also record autoincremented REGs for this insn as changed.  */
735*e4b17023SJohn Marino   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
736*e4b17023SJohn Marino     if (REG_NOTE_KIND (note) == REG_INC)
737*e4b17023SJohn Marino       record_last_reg_set_info (insn, XEXP (note, 0));
738*e4b17023SJohn Marino 
739*e4b17023SJohn Marino   /* Finally, if this is a call, record all call clobbers.  */
740*e4b17023SJohn Marino   if (CALL_P (insn))
741*e4b17023SJohn Marino     {
742*e4b17023SJohn Marino       unsigned int regno;
743*e4b17023SJohn Marino       rtx link, x;
744*e4b17023SJohn Marino 
745*e4b17023SJohn Marino       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
746*e4b17023SJohn Marino 	if (TEST_HARD_REG_BIT (regs_invalidated_by_call, regno))
747*e4b17023SJohn Marino 	  record_last_reg_set_info_regno (insn, regno);
748*e4b17023SJohn Marino 
749*e4b17023SJohn Marino       for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
750*e4b17023SJohn Marino 	if (GET_CODE (XEXP (link, 0)) == CLOBBER)
751*e4b17023SJohn Marino 	  {
752*e4b17023SJohn Marino 	    x = XEXP (XEXP (link, 0), 0);
753*e4b17023SJohn Marino 	    if (REG_P (x))
754*e4b17023SJohn Marino 	      {
755*e4b17023SJohn Marino 		gcc_assert (HARD_REGISTER_P (x));
756*e4b17023SJohn Marino 		record_last_reg_set_info (insn, x);
757*e4b17023SJohn Marino 	      }
758*e4b17023SJohn Marino 	  }
759*e4b17023SJohn Marino 
760*e4b17023SJohn Marino       if (! RTL_CONST_OR_PURE_CALL_P (insn))
761*e4b17023SJohn Marino 	record_last_mem_set_info (insn);
762*e4b17023SJohn Marino     }
763*e4b17023SJohn Marino }
764*e4b17023SJohn Marino 
765*e4b17023SJohn Marino 
766*e4b17023SJohn Marino /* Scan the pattern of INSN and add an entry to the hash TABLE.
767*e4b17023SJohn Marino    After reload we are interested in loads/stores only.  */
768*e4b17023SJohn Marino 
769*e4b17023SJohn Marino static void
hash_scan_set(rtx insn)770*e4b17023SJohn Marino hash_scan_set (rtx insn)
771*e4b17023SJohn Marino {
772*e4b17023SJohn Marino   rtx pat = PATTERN (insn);
773*e4b17023SJohn Marino   rtx src = SET_SRC (pat);
774*e4b17023SJohn Marino   rtx dest = SET_DEST (pat);
775*e4b17023SJohn Marino 
776*e4b17023SJohn Marino   /* We are only interested in loads and stores.  */
777*e4b17023SJohn Marino   if (! MEM_P (src) && ! MEM_P (dest))
778*e4b17023SJohn Marino     return;
779*e4b17023SJohn Marino 
780*e4b17023SJohn Marino   /* Don't mess with jumps and nops.  */
781*e4b17023SJohn Marino   if (JUMP_P (insn) || set_noop_p (pat))
782*e4b17023SJohn Marino     return;
783*e4b17023SJohn Marino 
784*e4b17023SJohn Marino   if (REG_P (dest))
785*e4b17023SJohn Marino     {
786*e4b17023SJohn Marino       if (/* Don't CSE something if we can't do a reg/reg copy.  */
787*e4b17023SJohn Marino 	  can_copy_p (GET_MODE (dest))
788*e4b17023SJohn Marino 	  /* Is SET_SRC something we want to gcse?  */
789*e4b17023SJohn Marino 	  && general_operand (src, GET_MODE (src))
790*e4b17023SJohn Marino #ifdef STACK_REGS
791*e4b17023SJohn Marino 	  /* Never consider insns touching the register stack.  It may
792*e4b17023SJohn Marino 	     create situations that reg-stack cannot handle (e.g. a stack
793*e4b17023SJohn Marino 	     register live across an abnormal edge).  */
794*e4b17023SJohn Marino 	  && (REGNO (dest) < FIRST_STACK_REG || REGNO (dest) > LAST_STACK_REG)
795*e4b17023SJohn Marino #endif
796*e4b17023SJohn Marino 	  /* An expression is not available if its operands are
797*e4b17023SJohn Marino 	     subsequently modified, including this insn.  */
798*e4b17023SJohn Marino 	  && oprs_unchanged_p (src, insn, true))
799*e4b17023SJohn Marino 	{
800*e4b17023SJohn Marino 	  insert_expr_in_table (src, insn);
801*e4b17023SJohn Marino 	}
802*e4b17023SJohn Marino     }
803*e4b17023SJohn Marino   else if (REG_P (src))
804*e4b17023SJohn Marino     {
805*e4b17023SJohn Marino       /* Only record sets of pseudo-regs in the hash table.  */
806*e4b17023SJohn Marino       if (/* Don't CSE something if we can't do a reg/reg copy.  */
807*e4b17023SJohn Marino 	  can_copy_p (GET_MODE (src))
808*e4b17023SJohn Marino 	  /* Is SET_DEST something we want to gcse?  */
809*e4b17023SJohn Marino 	  && general_operand (dest, GET_MODE (dest))
810*e4b17023SJohn Marino #ifdef STACK_REGS
811*e4b17023SJohn Marino 	  /* As above for STACK_REGS.  */
812*e4b17023SJohn Marino 	  && (REGNO (src) < FIRST_STACK_REG || REGNO (src) > LAST_STACK_REG)
813*e4b17023SJohn Marino #endif
814*e4b17023SJohn Marino 	  && ! (flag_float_store && FLOAT_MODE_P (GET_MODE (dest)))
815*e4b17023SJohn Marino 	  /* Check if the memory expression is killed after insn.  */
816*e4b17023SJohn Marino 	  && ! load_killed_in_block_p (INSN_CUID (insn) + 1, dest, true)
817*e4b17023SJohn Marino 	  && oprs_unchanged_p (XEXP (dest, 0), insn, true))
818*e4b17023SJohn Marino 	{
819*e4b17023SJohn Marino 	  insert_expr_in_table (dest, insn);
820*e4b17023SJohn Marino 	}
821*e4b17023SJohn Marino     }
822*e4b17023SJohn Marino }
823*e4b17023SJohn Marino 
824*e4b17023SJohn Marino 
825*e4b17023SJohn Marino /* Create hash table of memory expressions available at end of basic
826*e4b17023SJohn Marino    blocks.  Basically you should think of this hash table as the
827*e4b17023SJohn Marino    representation of AVAIL_OUT.  This is the set of expressions that
828*e4b17023SJohn Marino    is generated in a basic block and not killed before the end of the
829*e4b17023SJohn Marino    same basic block.  Notice that this is really a local computation.  */
830*e4b17023SJohn Marino 
831*e4b17023SJohn Marino static void
compute_hash_table(void)832*e4b17023SJohn Marino compute_hash_table (void)
833*e4b17023SJohn Marino {
834*e4b17023SJohn Marino   basic_block bb;
835*e4b17023SJohn Marino 
836*e4b17023SJohn Marino   FOR_EACH_BB (bb)
837*e4b17023SJohn Marino     {
838*e4b17023SJohn Marino       rtx insn;
839*e4b17023SJohn Marino 
840*e4b17023SJohn Marino       /* First pass over the instructions records information used to
841*e4b17023SJohn Marino 	 determine when registers and memory are last set.
842*e4b17023SJohn Marino 	 Since we compute a "local" AVAIL_OUT, reset the tables that
843*e4b17023SJohn Marino 	 help us keep track of what has been modified since the start
844*e4b17023SJohn Marino 	 of the block.  */
845*e4b17023SJohn Marino       reset_opr_set_tables ();
846*e4b17023SJohn Marino       FOR_BB_INSNS (bb, insn)
847*e4b17023SJohn Marino 	{
848*e4b17023SJohn Marino 	  if (INSN_P (insn))
849*e4b17023SJohn Marino             record_opr_changes (insn);
850*e4b17023SJohn Marino 	}
851*e4b17023SJohn Marino 
852*e4b17023SJohn Marino       /* The next pass actually builds the hash table.  */
853*e4b17023SJohn Marino       FOR_BB_INSNS (bb, insn)
854*e4b17023SJohn Marino 	if (INSN_P (insn) && GET_CODE (PATTERN (insn)) == SET)
855*e4b17023SJohn Marino 	  hash_scan_set (insn);
856*e4b17023SJohn Marino     }
857*e4b17023SJohn Marino }
858*e4b17023SJohn Marino 
859*e4b17023SJohn Marino 
860*e4b17023SJohn Marino /* Check if register REG is killed in any insn waiting to be inserted on
861*e4b17023SJohn Marino    edge E.  This function is required to check that our data flow analysis
862*e4b17023SJohn Marino    is still valid prior to commit_edge_insertions.  */
863*e4b17023SJohn Marino 
864*e4b17023SJohn Marino static bool
reg_killed_on_edge(rtx reg,edge e)865*e4b17023SJohn Marino reg_killed_on_edge (rtx reg, edge e)
866*e4b17023SJohn Marino {
867*e4b17023SJohn Marino   rtx insn;
868*e4b17023SJohn Marino 
869*e4b17023SJohn Marino   for (insn = e->insns.r; insn; insn = NEXT_INSN (insn))
870*e4b17023SJohn Marino     if (INSN_P (insn) && reg_set_p (reg, insn))
871*e4b17023SJohn Marino       return true;
872*e4b17023SJohn Marino 
873*e4b17023SJohn Marino   return false;
874*e4b17023SJohn Marino }
875*e4b17023SJohn Marino 
876*e4b17023SJohn Marino /* Similar to above - check if register REG is used in any insn waiting
877*e4b17023SJohn Marino    to be inserted on edge E.
878*e4b17023SJohn Marino    Assumes no such insn can be a CALL_INSN; if so call reg_used_between_p
879*e4b17023SJohn Marino    with PREV(insn),NEXT(insn) instead of calling reg_overlap_mentioned_p.  */
880*e4b17023SJohn Marino 
881*e4b17023SJohn Marino static bool
reg_used_on_edge(rtx reg,edge e)882*e4b17023SJohn Marino reg_used_on_edge (rtx reg, edge e)
883*e4b17023SJohn Marino {
884*e4b17023SJohn Marino   rtx insn;
885*e4b17023SJohn Marino 
886*e4b17023SJohn Marino   for (insn = e->insns.r; insn; insn = NEXT_INSN (insn))
887*e4b17023SJohn Marino     if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
888*e4b17023SJohn Marino       return true;
889*e4b17023SJohn Marino 
890*e4b17023SJohn Marino   return false;
891*e4b17023SJohn Marino }
892*e4b17023SJohn Marino 
893*e4b17023SJohn Marino /* Return the loaded/stored register of a load/store instruction.  */
894*e4b17023SJohn Marino 
895*e4b17023SJohn Marino static rtx
get_avail_load_store_reg(rtx insn)896*e4b17023SJohn Marino get_avail_load_store_reg (rtx insn)
897*e4b17023SJohn Marino {
898*e4b17023SJohn Marino   if (REG_P (SET_DEST (PATTERN (insn))))
899*e4b17023SJohn Marino     /* A load.  */
900*e4b17023SJohn Marino     return SET_DEST(PATTERN(insn));
901*e4b17023SJohn Marino   else
902*e4b17023SJohn Marino     {
903*e4b17023SJohn Marino       /* A store.  */
904*e4b17023SJohn Marino       gcc_assert (REG_P (SET_SRC (PATTERN (insn))));
905*e4b17023SJohn Marino       return SET_SRC (PATTERN (insn));
906*e4b17023SJohn Marino     }
907*e4b17023SJohn Marino }
908*e4b17023SJohn Marino 
909*e4b17023SJohn Marino /* Return nonzero if the predecessors of BB are "well behaved".  */
910*e4b17023SJohn Marino 
911*e4b17023SJohn Marino static bool
bb_has_well_behaved_predecessors(basic_block bb)912*e4b17023SJohn Marino bb_has_well_behaved_predecessors (basic_block bb)
913*e4b17023SJohn Marino {
914*e4b17023SJohn Marino   edge pred;
915*e4b17023SJohn Marino   edge_iterator ei;
916*e4b17023SJohn Marino 
917*e4b17023SJohn Marino   if (EDGE_COUNT (bb->preds) == 0)
918*e4b17023SJohn Marino     return false;
919*e4b17023SJohn Marino 
920*e4b17023SJohn Marino   FOR_EACH_EDGE (pred, ei, bb->preds)
921*e4b17023SJohn Marino     {
922*e4b17023SJohn Marino       if ((pred->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (pred))
923*e4b17023SJohn Marino 	return false;
924*e4b17023SJohn Marino 
925*e4b17023SJohn Marino       if ((pred->flags & EDGE_ABNORMAL_CALL) && cfun->has_nonlocal_label)
926*e4b17023SJohn Marino 	return false;
927*e4b17023SJohn Marino 
928*e4b17023SJohn Marino       if (JUMP_TABLE_DATA_P (BB_END (pred->src)))
929*e4b17023SJohn Marino 	return false;
930*e4b17023SJohn Marino     }
931*e4b17023SJohn Marino   return true;
932*e4b17023SJohn Marino }
933*e4b17023SJohn Marino 
934*e4b17023SJohn Marino 
935*e4b17023SJohn Marino /* Search for the occurrences of expression in BB.  */
936*e4b17023SJohn Marino 
937*e4b17023SJohn Marino static struct occr*
get_bb_avail_insn(basic_block bb,struct occr * occr)938*e4b17023SJohn Marino get_bb_avail_insn (basic_block bb, struct occr *occr)
939*e4b17023SJohn Marino {
940*e4b17023SJohn Marino   for (; occr != NULL; occr = occr->next)
941*e4b17023SJohn Marino     if (BLOCK_FOR_INSN (occr->insn) == bb)
942*e4b17023SJohn Marino       return occr;
943*e4b17023SJohn Marino   return NULL;
944*e4b17023SJohn Marino }
945*e4b17023SJohn Marino 
946*e4b17023SJohn Marino 
947*e4b17023SJohn Marino /* This handles the case where several stores feed a partially redundant
948*e4b17023SJohn Marino    load. It checks if the redundancy elimination is possible and if it's
949*e4b17023SJohn Marino    worth it.
950*e4b17023SJohn Marino 
951*e4b17023SJohn Marino    Redundancy elimination is possible if,
952*e4b17023SJohn Marino    1) None of the operands of an insn have been modified since the start
953*e4b17023SJohn Marino       of the current basic block.
954*e4b17023SJohn Marino    2) In any predecessor of the current basic block, the same expression
955*e4b17023SJohn Marino       is generated.
956*e4b17023SJohn Marino 
957*e4b17023SJohn Marino    See the function body for the heuristics that determine if eliminating
958*e4b17023SJohn Marino    a redundancy is also worth doing, assuming it is possible.  */
959*e4b17023SJohn Marino 
960*e4b17023SJohn Marino static void
eliminate_partially_redundant_load(basic_block bb,rtx insn,struct expr * expr)961*e4b17023SJohn Marino eliminate_partially_redundant_load (basic_block bb, rtx insn,
962*e4b17023SJohn Marino 				    struct expr *expr)
963*e4b17023SJohn Marino {
964*e4b17023SJohn Marino   edge pred;
965*e4b17023SJohn Marino   rtx avail_insn = NULL_RTX;
966*e4b17023SJohn Marino   rtx avail_reg;
967*e4b17023SJohn Marino   rtx dest, pat;
968*e4b17023SJohn Marino   struct occr *a_occr;
969*e4b17023SJohn Marino   struct unoccr *occr, *avail_occrs = NULL;
970*e4b17023SJohn Marino   struct unoccr *unoccr, *unavail_occrs = NULL, *rollback_unoccr = NULL;
971*e4b17023SJohn Marino   int npred_ok = 0;
972*e4b17023SJohn Marino   gcov_type ok_count = 0; /* Redundant load execution count.  */
973*e4b17023SJohn Marino   gcov_type critical_count = 0; /* Execution count of critical edges.  */
974*e4b17023SJohn Marino   edge_iterator ei;
975*e4b17023SJohn Marino   bool critical_edge_split = false;
976*e4b17023SJohn Marino 
977*e4b17023SJohn Marino   /* The execution count of the loads to be added to make the
978*e4b17023SJohn Marino      load fully redundant.  */
979*e4b17023SJohn Marino   gcov_type not_ok_count = 0;
980*e4b17023SJohn Marino   basic_block pred_bb;
981*e4b17023SJohn Marino 
982*e4b17023SJohn Marino   pat = PATTERN (insn);
983*e4b17023SJohn Marino   dest = SET_DEST (pat);
984*e4b17023SJohn Marino 
985*e4b17023SJohn Marino   /* Check that the loaded register is not used, set, or killed from the
986*e4b17023SJohn Marino      beginning of the block.  */
987*e4b17023SJohn Marino   if (reg_changed_after_insn_p (dest, 0)
988*e4b17023SJohn Marino       || reg_used_between_p (dest, PREV_INSN (BB_HEAD (bb)), insn))
989*e4b17023SJohn Marino     return;
990*e4b17023SJohn Marino 
991*e4b17023SJohn Marino   /* Check potential for replacing load with copy for predecessors.  */
992*e4b17023SJohn Marino   FOR_EACH_EDGE (pred, ei, bb->preds)
993*e4b17023SJohn Marino     {
994*e4b17023SJohn Marino       rtx next_pred_bb_end;
995*e4b17023SJohn Marino 
996*e4b17023SJohn Marino       avail_insn = NULL_RTX;
997*e4b17023SJohn Marino       avail_reg = NULL_RTX;
998*e4b17023SJohn Marino       pred_bb = pred->src;
999*e4b17023SJohn Marino       next_pred_bb_end = NEXT_INSN (BB_END (pred_bb));
1000*e4b17023SJohn Marino       for (a_occr = get_bb_avail_insn (pred_bb, expr->avail_occr); a_occr;
1001*e4b17023SJohn Marino 	   a_occr = get_bb_avail_insn (pred_bb, a_occr->next))
1002*e4b17023SJohn Marino 	{
1003*e4b17023SJohn Marino 	  /* Check if the loaded register is not used.  */
1004*e4b17023SJohn Marino 	  avail_insn = a_occr->insn;
1005*e4b17023SJohn Marino 	  avail_reg = get_avail_load_store_reg (avail_insn);
1006*e4b17023SJohn Marino 	  gcc_assert (avail_reg);
1007*e4b17023SJohn Marino 
1008*e4b17023SJohn Marino 	  /* Make sure we can generate a move from register avail_reg to
1009*e4b17023SJohn Marino 	     dest.  */
1010*e4b17023SJohn Marino 	  extract_insn (gen_move_insn (copy_rtx (dest),
1011*e4b17023SJohn Marino 				       copy_rtx (avail_reg)));
1012*e4b17023SJohn Marino 	  if (! constrain_operands (1)
1013*e4b17023SJohn Marino 	      || reg_killed_on_edge (avail_reg, pred)
1014*e4b17023SJohn Marino 	      || reg_used_on_edge (dest, pred))
1015*e4b17023SJohn Marino 	    {
1016*e4b17023SJohn Marino 	      avail_insn = NULL;
1017*e4b17023SJohn Marino 	      continue;
1018*e4b17023SJohn Marino 	    }
1019*e4b17023SJohn Marino 	  if (!reg_set_between_p (avail_reg, avail_insn, next_pred_bb_end))
1020*e4b17023SJohn Marino 	    /* AVAIL_INSN remains non-null.  */
1021*e4b17023SJohn Marino 	    break;
1022*e4b17023SJohn Marino 	  else
1023*e4b17023SJohn Marino 	    avail_insn = NULL;
1024*e4b17023SJohn Marino 	}
1025*e4b17023SJohn Marino 
1026*e4b17023SJohn Marino       if (EDGE_CRITICAL_P (pred))
1027*e4b17023SJohn Marino 	critical_count += pred->count;
1028*e4b17023SJohn Marino 
1029*e4b17023SJohn Marino       if (avail_insn != NULL_RTX)
1030*e4b17023SJohn Marino 	{
1031*e4b17023SJohn Marino 	  npred_ok++;
1032*e4b17023SJohn Marino 	  ok_count += pred->count;
1033*e4b17023SJohn Marino 	  if (! set_noop_p (PATTERN (gen_move_insn (copy_rtx (dest),
1034*e4b17023SJohn Marino 						    copy_rtx (avail_reg)))))
1035*e4b17023SJohn Marino 	    {
1036*e4b17023SJohn Marino 	      /* Check if there is going to be a split.  */
1037*e4b17023SJohn Marino 	      if (EDGE_CRITICAL_P (pred))
1038*e4b17023SJohn Marino 		critical_edge_split = true;
1039*e4b17023SJohn Marino 	    }
1040*e4b17023SJohn Marino 	  else /* Its a dead move no need to generate.  */
1041*e4b17023SJohn Marino 	    continue;
1042*e4b17023SJohn Marino 	  occr = (struct unoccr *) obstack_alloc (&unoccr_obstack,
1043*e4b17023SJohn Marino 						  sizeof (struct unoccr));
1044*e4b17023SJohn Marino 	  occr->insn = avail_insn;
1045*e4b17023SJohn Marino 	  occr->pred = pred;
1046*e4b17023SJohn Marino 	  occr->next = avail_occrs;
1047*e4b17023SJohn Marino 	  avail_occrs = occr;
1048*e4b17023SJohn Marino 	  if (! rollback_unoccr)
1049*e4b17023SJohn Marino 	    rollback_unoccr = occr;
1050*e4b17023SJohn Marino 	}
1051*e4b17023SJohn Marino       else
1052*e4b17023SJohn Marino 	{
1053*e4b17023SJohn Marino 	  /* Adding a load on a critical edge will cause a split.  */
1054*e4b17023SJohn Marino 	  if (EDGE_CRITICAL_P (pred))
1055*e4b17023SJohn Marino 	    critical_edge_split = true;
1056*e4b17023SJohn Marino 	  not_ok_count += pred->count;
1057*e4b17023SJohn Marino 	  unoccr = (struct unoccr *) obstack_alloc (&unoccr_obstack,
1058*e4b17023SJohn Marino 						    sizeof (struct unoccr));
1059*e4b17023SJohn Marino 	  unoccr->insn = NULL_RTX;
1060*e4b17023SJohn Marino 	  unoccr->pred = pred;
1061*e4b17023SJohn Marino 	  unoccr->next = unavail_occrs;
1062*e4b17023SJohn Marino 	  unavail_occrs = unoccr;
1063*e4b17023SJohn Marino 	  if (! rollback_unoccr)
1064*e4b17023SJohn Marino 	    rollback_unoccr = unoccr;
1065*e4b17023SJohn Marino 	}
1066*e4b17023SJohn Marino     }
1067*e4b17023SJohn Marino 
1068*e4b17023SJohn Marino   if (/* No load can be replaced by copy.  */
1069*e4b17023SJohn Marino       npred_ok == 0
1070*e4b17023SJohn Marino       /* Prevent exploding the code.  */
1071*e4b17023SJohn Marino       || (optimize_bb_for_size_p (bb) && npred_ok > 1)
1072*e4b17023SJohn Marino       /* If we don't have profile information we cannot tell if splitting
1073*e4b17023SJohn Marino          a critical edge is profitable or not so don't do it.  */
1074*e4b17023SJohn Marino       || ((! profile_info || ! flag_branch_probabilities
1075*e4b17023SJohn Marino 	   || targetm.cannot_modify_jumps_p ())
1076*e4b17023SJohn Marino 	  && critical_edge_split))
1077*e4b17023SJohn Marino     goto cleanup;
1078*e4b17023SJohn Marino 
1079*e4b17023SJohn Marino   /* Check if it's worth applying the partial redundancy elimination.  */
1080*e4b17023SJohn Marino   if (ok_count < GCSE_AFTER_RELOAD_PARTIAL_FRACTION * not_ok_count)
1081*e4b17023SJohn Marino     goto cleanup;
1082*e4b17023SJohn Marino   if (ok_count < GCSE_AFTER_RELOAD_CRITICAL_FRACTION * critical_count)
1083*e4b17023SJohn Marino     goto cleanup;
1084*e4b17023SJohn Marino 
1085*e4b17023SJohn Marino   /* Generate moves to the loaded register from where
1086*e4b17023SJohn Marino      the memory is available.  */
1087*e4b17023SJohn Marino   for (occr = avail_occrs; occr; occr = occr->next)
1088*e4b17023SJohn Marino     {
1089*e4b17023SJohn Marino       avail_insn = occr->insn;
1090*e4b17023SJohn Marino       pred = occr->pred;
1091*e4b17023SJohn Marino       /* Set avail_reg to be the register having the value of the
1092*e4b17023SJohn Marino 	 memory.  */
1093*e4b17023SJohn Marino       avail_reg = get_avail_load_store_reg (avail_insn);
1094*e4b17023SJohn Marino       gcc_assert (avail_reg);
1095*e4b17023SJohn Marino 
1096*e4b17023SJohn Marino       insert_insn_on_edge (gen_move_insn (copy_rtx (dest),
1097*e4b17023SJohn Marino 					  copy_rtx (avail_reg)),
1098*e4b17023SJohn Marino 			   pred);
1099*e4b17023SJohn Marino       stats.moves_inserted++;
1100*e4b17023SJohn Marino 
1101*e4b17023SJohn Marino       if (dump_file)
1102*e4b17023SJohn Marino 	fprintf (dump_file,
1103*e4b17023SJohn Marino 		 "generating move from %d to %d on edge from %d to %d\n",
1104*e4b17023SJohn Marino 		 REGNO (avail_reg),
1105*e4b17023SJohn Marino 		 REGNO (dest),
1106*e4b17023SJohn Marino 		 pred->src->index,
1107*e4b17023SJohn Marino 		 pred->dest->index);
1108*e4b17023SJohn Marino     }
1109*e4b17023SJohn Marino 
1110*e4b17023SJohn Marino   /* Regenerate loads where the memory is unavailable.  */
1111*e4b17023SJohn Marino   for (unoccr = unavail_occrs; unoccr; unoccr = unoccr->next)
1112*e4b17023SJohn Marino     {
1113*e4b17023SJohn Marino       pred = unoccr->pred;
1114*e4b17023SJohn Marino       insert_insn_on_edge (copy_insn (PATTERN (insn)), pred);
1115*e4b17023SJohn Marino       stats.copies_inserted++;
1116*e4b17023SJohn Marino 
1117*e4b17023SJohn Marino       if (dump_file)
1118*e4b17023SJohn Marino 	{
1119*e4b17023SJohn Marino 	  fprintf (dump_file,
1120*e4b17023SJohn Marino 		   "generating on edge from %d to %d a copy of load: ",
1121*e4b17023SJohn Marino 		   pred->src->index,
1122*e4b17023SJohn Marino 		   pred->dest->index);
1123*e4b17023SJohn Marino 	  print_rtl (dump_file, PATTERN (insn));
1124*e4b17023SJohn Marino 	  fprintf (dump_file, "\n");
1125*e4b17023SJohn Marino 	}
1126*e4b17023SJohn Marino     }
1127*e4b17023SJohn Marino 
1128*e4b17023SJohn Marino   /* Delete the insn if it is not available in this block and mark it
1129*e4b17023SJohn Marino      for deletion if it is available. If insn is available it may help
1130*e4b17023SJohn Marino      discover additional redundancies, so mark it for later deletion.  */
1131*e4b17023SJohn Marino   for (a_occr = get_bb_avail_insn (bb, expr->avail_occr);
1132*e4b17023SJohn Marino        a_occr && (a_occr->insn != insn);
1133*e4b17023SJohn Marino        a_occr = get_bb_avail_insn (bb, a_occr->next))
1134*e4b17023SJohn Marino     ;
1135*e4b17023SJohn Marino 
1136*e4b17023SJohn Marino   if (!a_occr)
1137*e4b17023SJohn Marino     {
1138*e4b17023SJohn Marino       stats.insns_deleted++;
1139*e4b17023SJohn Marino 
1140*e4b17023SJohn Marino       if (dump_file)
1141*e4b17023SJohn Marino 	{
1142*e4b17023SJohn Marino 	  fprintf (dump_file, "deleting insn:\n");
1143*e4b17023SJohn Marino           print_rtl_single (dump_file, insn);
1144*e4b17023SJohn Marino           fprintf (dump_file, "\n");
1145*e4b17023SJohn Marino 	}
1146*e4b17023SJohn Marino       delete_insn (insn);
1147*e4b17023SJohn Marino     }
1148*e4b17023SJohn Marino   else
1149*e4b17023SJohn Marino     a_occr->deleted_p = 1;
1150*e4b17023SJohn Marino 
1151*e4b17023SJohn Marino cleanup:
1152*e4b17023SJohn Marino   if (rollback_unoccr)
1153*e4b17023SJohn Marino     obstack_free (&unoccr_obstack, rollback_unoccr);
1154*e4b17023SJohn Marino }
1155*e4b17023SJohn Marino 
1156*e4b17023SJohn Marino /* Performing the redundancy elimination as described before.  */
1157*e4b17023SJohn Marino 
1158*e4b17023SJohn Marino static void
eliminate_partially_redundant_loads(void)1159*e4b17023SJohn Marino eliminate_partially_redundant_loads (void)
1160*e4b17023SJohn Marino {
1161*e4b17023SJohn Marino   rtx insn;
1162*e4b17023SJohn Marino   basic_block bb;
1163*e4b17023SJohn Marino 
1164*e4b17023SJohn Marino   /* Note we start at block 1.  */
1165*e4b17023SJohn Marino 
1166*e4b17023SJohn Marino   if (ENTRY_BLOCK_PTR->next_bb == EXIT_BLOCK_PTR)
1167*e4b17023SJohn Marino     return;
1168*e4b17023SJohn Marino 
1169*e4b17023SJohn Marino   FOR_BB_BETWEEN (bb,
1170*e4b17023SJohn Marino 		  ENTRY_BLOCK_PTR->next_bb->next_bb,
1171*e4b17023SJohn Marino 		  EXIT_BLOCK_PTR,
1172*e4b17023SJohn Marino 		  next_bb)
1173*e4b17023SJohn Marino     {
1174*e4b17023SJohn Marino       /* Don't try anything on basic blocks with strange predecessors.  */
1175*e4b17023SJohn Marino       if (! bb_has_well_behaved_predecessors (bb))
1176*e4b17023SJohn Marino 	continue;
1177*e4b17023SJohn Marino 
1178*e4b17023SJohn Marino       /* Do not try anything on cold basic blocks.  */
1179*e4b17023SJohn Marino       if (optimize_bb_for_size_p (bb))
1180*e4b17023SJohn Marino 	continue;
1181*e4b17023SJohn Marino 
1182*e4b17023SJohn Marino       /* Reset the table of things changed since the start of the current
1183*e4b17023SJohn Marino 	 basic block.  */
1184*e4b17023SJohn Marino       reset_opr_set_tables ();
1185*e4b17023SJohn Marino 
1186*e4b17023SJohn Marino       /* Look at all insns in the current basic block and see if there are
1187*e4b17023SJohn Marino 	 any loads in it that we can record.  */
1188*e4b17023SJohn Marino       FOR_BB_INSNS (bb, insn)
1189*e4b17023SJohn Marino 	{
1190*e4b17023SJohn Marino 	  /* Is it a load - of the form (set (reg) (mem))?  */
1191*e4b17023SJohn Marino 	  if (NONJUMP_INSN_P (insn)
1192*e4b17023SJohn Marino               && GET_CODE (PATTERN (insn)) == SET
1193*e4b17023SJohn Marino 	      && REG_P (SET_DEST (PATTERN (insn)))
1194*e4b17023SJohn Marino 	      && MEM_P (SET_SRC (PATTERN (insn))))
1195*e4b17023SJohn Marino 	    {
1196*e4b17023SJohn Marino 	      rtx pat = PATTERN (insn);
1197*e4b17023SJohn Marino 	      rtx src = SET_SRC (pat);
1198*e4b17023SJohn Marino 	      struct expr *expr;
1199*e4b17023SJohn Marino 
1200*e4b17023SJohn Marino 	      if (!MEM_VOLATILE_P (src)
1201*e4b17023SJohn Marino 		  && GET_MODE (src) != BLKmode
1202*e4b17023SJohn Marino 		  && general_operand (src, GET_MODE (src))
1203*e4b17023SJohn Marino 		  /* Are the operands unchanged since the start of the
1204*e4b17023SJohn Marino 		     block?  */
1205*e4b17023SJohn Marino 		  && oprs_unchanged_p (src, insn, false)
1206*e4b17023SJohn Marino 		  && !(cfun->can_throw_non_call_exceptions && may_trap_p (src))
1207*e4b17023SJohn Marino 		  && !side_effects_p (src)
1208*e4b17023SJohn Marino 		  /* Is the expression recorded?  */
1209*e4b17023SJohn Marino 		  && (expr = lookup_expr_in_table (src)) != NULL)
1210*e4b17023SJohn Marino 		{
1211*e4b17023SJohn Marino 		  /* We now have a load (insn) and an available memory at
1212*e4b17023SJohn Marino 		     its BB start (expr). Try to remove the loads if it is
1213*e4b17023SJohn Marino 		     redundant.  */
1214*e4b17023SJohn Marino 		  eliminate_partially_redundant_load (bb, insn, expr);
1215*e4b17023SJohn Marino 		}
1216*e4b17023SJohn Marino 	    }
1217*e4b17023SJohn Marino 
1218*e4b17023SJohn Marino 	  /* Keep track of everything modified by this insn, so that we
1219*e4b17023SJohn Marino 	     know what has been modified since the start of the current
1220*e4b17023SJohn Marino 	     basic block.  */
1221*e4b17023SJohn Marino 	  if (INSN_P (insn))
1222*e4b17023SJohn Marino 	    record_opr_changes (insn);
1223*e4b17023SJohn Marino 	}
1224*e4b17023SJohn Marino     }
1225*e4b17023SJohn Marino 
1226*e4b17023SJohn Marino   commit_edge_insertions ();
1227*e4b17023SJohn Marino }
1228*e4b17023SJohn Marino 
1229*e4b17023SJohn Marino /* Go over the expression hash table and delete insns that were
1230*e4b17023SJohn Marino    marked for later deletion.  */
1231*e4b17023SJohn Marino 
1232*e4b17023SJohn Marino /* This helper is called via htab_traverse.  */
1233*e4b17023SJohn Marino static int
delete_redundant_insns_1(void ** slot,void * data ATTRIBUTE_UNUSED)1234*e4b17023SJohn Marino delete_redundant_insns_1 (void **slot, void *data ATTRIBUTE_UNUSED)
1235*e4b17023SJohn Marino {
1236*e4b17023SJohn Marino   struct expr *expr = (struct expr *) *slot;
1237*e4b17023SJohn Marino   struct occr *occr;
1238*e4b17023SJohn Marino 
1239*e4b17023SJohn Marino   for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
1240*e4b17023SJohn Marino     {
1241*e4b17023SJohn Marino       if (occr->deleted_p && dbg_cnt (gcse2_delete))
1242*e4b17023SJohn Marino 	{
1243*e4b17023SJohn Marino 	  delete_insn (occr->insn);
1244*e4b17023SJohn Marino 	  stats.insns_deleted++;
1245*e4b17023SJohn Marino 
1246*e4b17023SJohn Marino 	  if (dump_file)
1247*e4b17023SJohn Marino 	    {
1248*e4b17023SJohn Marino 	      fprintf (dump_file, "deleting insn:\n");
1249*e4b17023SJohn Marino 	      print_rtl_single (dump_file, occr->insn);
1250*e4b17023SJohn Marino 	      fprintf (dump_file, "\n");
1251*e4b17023SJohn Marino 	    }
1252*e4b17023SJohn Marino 	}
1253*e4b17023SJohn Marino     }
1254*e4b17023SJohn Marino 
1255*e4b17023SJohn Marino   return 1;
1256*e4b17023SJohn Marino }
1257*e4b17023SJohn Marino 
1258*e4b17023SJohn Marino static void
delete_redundant_insns(void)1259*e4b17023SJohn Marino delete_redundant_insns (void)
1260*e4b17023SJohn Marino {
1261*e4b17023SJohn Marino   htab_traverse (expr_table, delete_redundant_insns_1, NULL);
1262*e4b17023SJohn Marino   if (dump_file)
1263*e4b17023SJohn Marino     fprintf (dump_file, "\n");
1264*e4b17023SJohn Marino }
1265*e4b17023SJohn Marino 
1266*e4b17023SJohn Marino /* Main entry point of the GCSE after reload - clean some redundant loads
1267*e4b17023SJohn Marino    due to spilling.  */
1268*e4b17023SJohn Marino 
1269*e4b17023SJohn Marino static void
gcse_after_reload_main(rtx f ATTRIBUTE_UNUSED)1270*e4b17023SJohn Marino gcse_after_reload_main (rtx f ATTRIBUTE_UNUSED)
1271*e4b17023SJohn Marino {
1272*e4b17023SJohn Marino 
1273*e4b17023SJohn Marino   memset (&stats, 0, sizeof (stats));
1274*e4b17023SJohn Marino 
1275*e4b17023SJohn Marino   /* Allocate memory for this pass.
1276*e4b17023SJohn Marino      Also computes and initializes the insns' CUIDs.  */
1277*e4b17023SJohn Marino   alloc_mem ();
1278*e4b17023SJohn Marino 
1279*e4b17023SJohn Marino   /* We need alias analysis.  */
1280*e4b17023SJohn Marino   init_alias_analysis ();
1281*e4b17023SJohn Marino 
1282*e4b17023SJohn Marino   compute_hash_table ();
1283*e4b17023SJohn Marino 
1284*e4b17023SJohn Marino   if (dump_file)
1285*e4b17023SJohn Marino     dump_hash_table (dump_file);
1286*e4b17023SJohn Marino 
1287*e4b17023SJohn Marino   if (htab_elements (expr_table) > 0)
1288*e4b17023SJohn Marino     {
1289*e4b17023SJohn Marino       eliminate_partially_redundant_loads ();
1290*e4b17023SJohn Marino       delete_redundant_insns ();
1291*e4b17023SJohn Marino 
1292*e4b17023SJohn Marino       if (dump_file)
1293*e4b17023SJohn Marino 	{
1294*e4b17023SJohn Marino 	  fprintf (dump_file, "GCSE AFTER RELOAD stats:\n");
1295*e4b17023SJohn Marino 	  fprintf (dump_file, "copies inserted: %d\n", stats.copies_inserted);
1296*e4b17023SJohn Marino 	  fprintf (dump_file, "moves inserted:  %d\n", stats.moves_inserted);
1297*e4b17023SJohn Marino 	  fprintf (dump_file, "insns deleted:   %d\n", stats.insns_deleted);
1298*e4b17023SJohn Marino 	  fprintf (dump_file, "\n\n");
1299*e4b17023SJohn Marino 	}
1300*e4b17023SJohn Marino 
1301*e4b17023SJohn Marino       statistics_counter_event (cfun, "copies inserted",
1302*e4b17023SJohn Marino 				stats.copies_inserted);
1303*e4b17023SJohn Marino       statistics_counter_event (cfun, "moves inserted",
1304*e4b17023SJohn Marino 				stats.moves_inserted);
1305*e4b17023SJohn Marino       statistics_counter_event (cfun, "insns deleted",
1306*e4b17023SJohn Marino 				stats.insns_deleted);
1307*e4b17023SJohn Marino     }
1308*e4b17023SJohn Marino 
1309*e4b17023SJohn Marino   /* We are finished with alias.  */
1310*e4b17023SJohn Marino   end_alias_analysis ();
1311*e4b17023SJohn Marino 
1312*e4b17023SJohn Marino   free_mem ();
1313*e4b17023SJohn Marino }
1314*e4b17023SJohn Marino 
1315*e4b17023SJohn Marino 
1316*e4b17023SJohn Marino static bool
gate_handle_gcse2(void)1317*e4b17023SJohn Marino gate_handle_gcse2 (void)
1318*e4b17023SJohn Marino {
1319*e4b17023SJohn Marino   return (optimize > 0 && flag_gcse_after_reload
1320*e4b17023SJohn Marino 	  && optimize_function_for_speed_p (cfun));
1321*e4b17023SJohn Marino }
1322*e4b17023SJohn Marino 
1323*e4b17023SJohn Marino 
1324*e4b17023SJohn Marino static unsigned int
rest_of_handle_gcse2(void)1325*e4b17023SJohn Marino rest_of_handle_gcse2 (void)
1326*e4b17023SJohn Marino {
1327*e4b17023SJohn Marino   gcse_after_reload_main (get_insns ());
1328*e4b17023SJohn Marino   rebuild_jump_labels (get_insns ());
1329*e4b17023SJohn Marino   return 0;
1330*e4b17023SJohn Marino }
1331*e4b17023SJohn Marino 
1332*e4b17023SJohn Marino struct rtl_opt_pass pass_gcse2 =
1333*e4b17023SJohn Marino {
1334*e4b17023SJohn Marino  {
1335*e4b17023SJohn Marino   RTL_PASS,
1336*e4b17023SJohn Marino   "gcse2",                              /* name */
1337*e4b17023SJohn Marino   gate_handle_gcse2,                    /* gate */
1338*e4b17023SJohn Marino   rest_of_handle_gcse2,                 /* execute */
1339*e4b17023SJohn Marino   NULL,                                 /* sub */
1340*e4b17023SJohn Marino   NULL,                                 /* next */
1341*e4b17023SJohn Marino   0,                                    /* static_pass_number */
1342*e4b17023SJohn Marino   TV_GCSE_AFTER_RELOAD,                 /* tv_id */
1343*e4b17023SJohn Marino   0,                                    /* properties_required */
1344*e4b17023SJohn Marino   0,                                    /* properties_provided */
1345*e4b17023SJohn Marino   0,                                    /* properties_destroyed */
1346*e4b17023SJohn Marino   0,                                    /* todo_flags_start */
1347*e4b17023SJohn Marino   TODO_verify_rtl_sharing
1348*e4b17023SJohn Marino   | TODO_verify_flow | TODO_ggc_collect /* todo_flags_finish */
1349*e4b17023SJohn Marino  }
1350*e4b17023SJohn Marino };
1351