xref: /dragonfly/contrib/gcc-4.7/gcc/cselib.c (revision e4b17023)
1*e4b17023SJohn Marino /* Common subexpression elimination library for GNU compiler.
2*e4b17023SJohn Marino    Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3*e4b17023SJohn Marino    1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
4*e4b17023SJohn Marino    2012 Free Software Foundation, Inc.
5*e4b17023SJohn Marino 
6*e4b17023SJohn Marino This file is part of GCC.
7*e4b17023SJohn Marino 
8*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
9*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
10*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later
11*e4b17023SJohn Marino version.
12*e4b17023SJohn Marino 
13*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
15*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16*e4b17023SJohn Marino for more details.
17*e4b17023SJohn Marino 
18*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
19*e4b17023SJohn Marino along with GCC; see the file COPYING3.  If not see
20*e4b17023SJohn Marino <http://www.gnu.org/licenses/>.  */
21*e4b17023SJohn Marino 
22*e4b17023SJohn Marino #include "config.h"
23*e4b17023SJohn Marino #include "system.h"
24*e4b17023SJohn Marino #include "coretypes.h"
25*e4b17023SJohn Marino #include "tm.h"
26*e4b17023SJohn Marino 
27*e4b17023SJohn Marino #include "rtl.h"
28*e4b17023SJohn Marino #include "tm_p.h"
29*e4b17023SJohn Marino #include "regs.h"
30*e4b17023SJohn Marino #include "hard-reg-set.h"
31*e4b17023SJohn Marino #include "flags.h"
32*e4b17023SJohn Marino #include "insn-config.h"
33*e4b17023SJohn Marino #include "recog.h"
34*e4b17023SJohn Marino #include "function.h"
35*e4b17023SJohn Marino #include "emit-rtl.h"
36*e4b17023SJohn Marino #include "diagnostic-core.h"
37*e4b17023SJohn Marino #include "output.h"
38*e4b17023SJohn Marino #include "ggc.h"
39*e4b17023SJohn Marino #include "hashtab.h"
40*e4b17023SJohn Marino #include "tree-pass.h"
41*e4b17023SJohn Marino #include "cselib.h"
42*e4b17023SJohn Marino #include "params.h"
43*e4b17023SJohn Marino #include "alloc-pool.h"
44*e4b17023SJohn Marino #include "target.h"
45*e4b17023SJohn Marino #include "bitmap.h"
46*e4b17023SJohn Marino 
47*e4b17023SJohn Marino /* A list of cselib_val structures.  */
48*e4b17023SJohn Marino struct elt_list {
49*e4b17023SJohn Marino     struct elt_list *next;
50*e4b17023SJohn Marino     cselib_val *elt;
51*e4b17023SJohn Marino };
52*e4b17023SJohn Marino 
53*e4b17023SJohn Marino static bool cselib_record_memory;
54*e4b17023SJohn Marino static bool cselib_preserve_constants;
55*e4b17023SJohn Marino static bool cselib_any_perm_equivs;
56*e4b17023SJohn Marino static int entry_and_rtx_equal_p (const void *, const void *);
57*e4b17023SJohn Marino static hashval_t get_value_hash (const void *);
58*e4b17023SJohn Marino static struct elt_list *new_elt_list (struct elt_list *, cselib_val *);
59*e4b17023SJohn Marino static void new_elt_loc_list (cselib_val *, rtx);
60*e4b17023SJohn Marino static void unchain_one_value (cselib_val *);
61*e4b17023SJohn Marino static void unchain_one_elt_list (struct elt_list **);
62*e4b17023SJohn Marino static void unchain_one_elt_loc_list (struct elt_loc_list **);
63*e4b17023SJohn Marino static int discard_useless_locs (void **, void *);
64*e4b17023SJohn Marino static int discard_useless_values (void **, void *);
65*e4b17023SJohn Marino static void remove_useless_values (void);
66*e4b17023SJohn Marino static int rtx_equal_for_cselib_1 (rtx, rtx, enum machine_mode);
67*e4b17023SJohn Marino static unsigned int cselib_hash_rtx (rtx, int, enum machine_mode);
68*e4b17023SJohn Marino static cselib_val *new_cselib_val (unsigned int, enum machine_mode, rtx);
69*e4b17023SJohn Marino static void add_mem_for_addr (cselib_val *, cselib_val *, rtx);
70*e4b17023SJohn Marino static cselib_val *cselib_lookup_mem (rtx, int);
71*e4b17023SJohn Marino static void cselib_invalidate_regno (unsigned int, enum machine_mode);
72*e4b17023SJohn Marino static void cselib_invalidate_mem (rtx);
73*e4b17023SJohn Marino static void cselib_record_set (rtx, cselib_val *, cselib_val *);
74*e4b17023SJohn Marino static void cselib_record_sets (rtx);
75*e4b17023SJohn Marino 
76*e4b17023SJohn Marino struct expand_value_data
77*e4b17023SJohn Marino {
78*e4b17023SJohn Marino   bitmap regs_active;
79*e4b17023SJohn Marino   cselib_expand_callback callback;
80*e4b17023SJohn Marino   void *callback_arg;
81*e4b17023SJohn Marino   bool dummy;
82*e4b17023SJohn Marino };
83*e4b17023SJohn Marino 
84*e4b17023SJohn Marino static rtx cselib_expand_value_rtx_1 (rtx, struct expand_value_data *, int);
85*e4b17023SJohn Marino 
86*e4b17023SJohn Marino /* There are three ways in which cselib can look up an rtx:
87*e4b17023SJohn Marino    - for a REG, the reg_values table (which is indexed by regno) is used
88*e4b17023SJohn Marino    - for a MEM, we recursively look up its address and then follow the
89*e4b17023SJohn Marino      addr_list of that value
90*e4b17023SJohn Marino    - for everything else, we compute a hash value and go through the hash
91*e4b17023SJohn Marino      table.  Since different rtx's can still have the same hash value,
92*e4b17023SJohn Marino      this involves walking the table entries for a given value and comparing
93*e4b17023SJohn Marino      the locations of the entries with the rtx we are looking up.  */
94*e4b17023SJohn Marino 
95*e4b17023SJohn Marino /* A table that enables us to look up elts by their value.  */
96*e4b17023SJohn Marino static htab_t cselib_hash_table;
97*e4b17023SJohn Marino 
98*e4b17023SJohn Marino /* This is a global so we don't have to pass this through every function.
99*e4b17023SJohn Marino    It is used in new_elt_loc_list to set SETTING_INSN.  */
100*e4b17023SJohn Marino static rtx cselib_current_insn;
101*e4b17023SJohn Marino 
102*e4b17023SJohn Marino /* The unique id that the next create value will take.  */
103*e4b17023SJohn Marino static unsigned int next_uid;
104*e4b17023SJohn Marino 
105*e4b17023SJohn Marino /* The number of registers we had when the varrays were last resized.  */
106*e4b17023SJohn Marino static unsigned int cselib_nregs;
107*e4b17023SJohn Marino 
108*e4b17023SJohn Marino /* Count values without known locations, or with only locations that
109*e4b17023SJohn Marino    wouldn't have been known except for debug insns.  Whenever this
110*e4b17023SJohn Marino    grows too big, we remove these useless values from the table.
111*e4b17023SJohn Marino 
112*e4b17023SJohn Marino    Counting values with only debug values is a bit tricky.  We don't
113*e4b17023SJohn Marino    want to increment n_useless_values when we create a value for a
114*e4b17023SJohn Marino    debug insn, for this would get n_useless_values out of sync, but we
115*e4b17023SJohn Marino    want increment it if all locs in the list that were ever referenced
116*e4b17023SJohn Marino    in nondebug insns are removed from the list.
117*e4b17023SJohn Marino 
118*e4b17023SJohn Marino    In the general case, once we do that, we'd have to stop accepting
119*e4b17023SJohn Marino    nondebug expressions in the loc list, to avoid having two values
120*e4b17023SJohn Marino    equivalent that, without debug insns, would have been made into
121*e4b17023SJohn Marino    separate values.  However, because debug insns never introduce
122*e4b17023SJohn Marino    equivalences themselves (no assignments), the only means for
123*e4b17023SJohn Marino    growing loc lists is through nondebug assignments.  If the locs
124*e4b17023SJohn Marino    also happen to be referenced in debug insns, it will work just fine.
125*e4b17023SJohn Marino 
126*e4b17023SJohn Marino    A consequence of this is that there's at most one debug-only loc in
127*e4b17023SJohn Marino    each loc list.  If we keep it in the first entry, testing whether
128*e4b17023SJohn Marino    we have a debug-only loc list takes O(1).
129*e4b17023SJohn Marino 
130*e4b17023SJohn Marino    Furthermore, since any additional entry in a loc list containing a
131*e4b17023SJohn Marino    debug loc would have to come from an assignment (nondebug) that
132*e4b17023SJohn Marino    references both the initial debug loc and the newly-equivalent loc,
133*e4b17023SJohn Marino    the initial debug loc would be promoted to a nondebug loc, and the
134*e4b17023SJohn Marino    loc list would not contain debug locs any more.
135*e4b17023SJohn Marino 
136*e4b17023SJohn Marino    So the only case we have to be careful with in order to keep
137*e4b17023SJohn Marino    n_useless_values in sync between debug and nondebug compilations is
138*e4b17023SJohn Marino    to avoid incrementing n_useless_values when removing the single loc
139*e4b17023SJohn Marino    from a value that turns out to not appear outside debug values.  We
140*e4b17023SJohn Marino    increment n_useless_debug_values instead, and leave such values
141*e4b17023SJohn Marino    alone until, for other reasons, we garbage-collect useless
142*e4b17023SJohn Marino    values.  */
143*e4b17023SJohn Marino static int n_useless_values;
144*e4b17023SJohn Marino static int n_useless_debug_values;
145*e4b17023SJohn Marino 
146*e4b17023SJohn Marino /* Count values whose locs have been taken exclusively from debug
147*e4b17023SJohn Marino    insns for the entire life of the value.  */
148*e4b17023SJohn Marino static int n_debug_values;
149*e4b17023SJohn Marino 
150*e4b17023SJohn Marino /* Number of useless values before we remove them from the hash table.  */
151*e4b17023SJohn Marino #define MAX_USELESS_VALUES 32
152*e4b17023SJohn Marino 
153*e4b17023SJohn Marino /* This table maps from register number to values.  It does not
154*e4b17023SJohn Marino    contain pointers to cselib_val structures, but rather elt_lists.
155*e4b17023SJohn Marino    The purpose is to be able to refer to the same register in
156*e4b17023SJohn Marino    different modes.  The first element of the list defines the mode in
157*e4b17023SJohn Marino    which the register was set; if the mode is unknown or the value is
158*e4b17023SJohn Marino    no longer valid in that mode, ELT will be NULL for the first
159*e4b17023SJohn Marino    element.  */
160*e4b17023SJohn Marino static struct elt_list **reg_values;
161*e4b17023SJohn Marino static unsigned int reg_values_size;
162*e4b17023SJohn Marino #define REG_VALUES(i) reg_values[i]
163*e4b17023SJohn Marino 
164*e4b17023SJohn Marino /* The largest number of hard regs used by any entry added to the
165*e4b17023SJohn Marino    REG_VALUES table.  Cleared on each cselib_clear_table() invocation.  */
166*e4b17023SJohn Marino static unsigned int max_value_regs;
167*e4b17023SJohn Marino 
168*e4b17023SJohn Marino /* Here the set of indices I with REG_VALUES(I) != 0 is saved.  This is used
169*e4b17023SJohn Marino    in cselib_clear_table() for fast emptying.  */
170*e4b17023SJohn Marino static unsigned int *used_regs;
171*e4b17023SJohn Marino static unsigned int n_used_regs;
172*e4b17023SJohn Marino 
173*e4b17023SJohn Marino /* We pass this to cselib_invalidate_mem to invalidate all of
174*e4b17023SJohn Marino    memory for a non-const call instruction.  */
175*e4b17023SJohn Marino static GTY(()) rtx callmem;
176*e4b17023SJohn Marino 
177*e4b17023SJohn Marino /* Set by discard_useless_locs if it deleted the last location of any
178*e4b17023SJohn Marino    value.  */
179*e4b17023SJohn Marino static int values_became_useless;
180*e4b17023SJohn Marino 
181*e4b17023SJohn Marino /* Used as stop element of the containing_mem list so we can check
182*e4b17023SJohn Marino    presence in the list by checking the next pointer.  */
183*e4b17023SJohn Marino static cselib_val dummy_val;
184*e4b17023SJohn Marino 
185*e4b17023SJohn Marino /* If non-NULL, value of the eliminated arg_pointer_rtx or frame_pointer_rtx
186*e4b17023SJohn Marino    that is constant through the whole function and should never be
187*e4b17023SJohn Marino    eliminated.  */
188*e4b17023SJohn Marino static cselib_val *cfa_base_preserved_val;
189*e4b17023SJohn Marino static unsigned int cfa_base_preserved_regno = INVALID_REGNUM;
190*e4b17023SJohn Marino 
191*e4b17023SJohn Marino /* Used to list all values that contain memory reference.
192*e4b17023SJohn Marino    May or may not contain the useless values - the list is compacted
193*e4b17023SJohn Marino    each time memory is invalidated.  */
194*e4b17023SJohn Marino static cselib_val *first_containing_mem = &dummy_val;
195*e4b17023SJohn Marino static alloc_pool elt_loc_list_pool, elt_list_pool, cselib_val_pool, value_pool;
196*e4b17023SJohn Marino 
197*e4b17023SJohn Marino /* If nonnull, cselib will call this function before freeing useless
198*e4b17023SJohn Marino    VALUEs.  A VALUE is deemed useless if its "locs" field is null.  */
199*e4b17023SJohn Marino void (*cselib_discard_hook) (cselib_val *);
200*e4b17023SJohn Marino 
201*e4b17023SJohn Marino /* If nonnull, cselib will call this function before recording sets or
202*e4b17023SJohn Marino    even clobbering outputs of INSN.  All the recorded sets will be
203*e4b17023SJohn Marino    represented in the array sets[n_sets].  new_val_min can be used to
204*e4b17023SJohn Marino    tell whether values present in sets are introduced by this
205*e4b17023SJohn Marino    instruction.  */
206*e4b17023SJohn Marino void (*cselib_record_sets_hook) (rtx insn, struct cselib_set *sets,
207*e4b17023SJohn Marino 				 int n_sets);
208*e4b17023SJohn Marino 
209*e4b17023SJohn Marino #define PRESERVED_VALUE_P(RTX) \
210*e4b17023SJohn Marino   (RTL_FLAG_CHECK1("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging)
211*e4b17023SJohn Marino 
212*e4b17023SJohn Marino 
213*e4b17023SJohn Marino 
214*e4b17023SJohn Marino /* Allocate a struct elt_list and fill in its two elements with the
215*e4b17023SJohn Marino    arguments.  */
216*e4b17023SJohn Marino 
217*e4b17023SJohn Marino static inline struct elt_list *
new_elt_list(struct elt_list * next,cselib_val * elt)218*e4b17023SJohn Marino new_elt_list (struct elt_list *next, cselib_val *elt)
219*e4b17023SJohn Marino {
220*e4b17023SJohn Marino   struct elt_list *el;
221*e4b17023SJohn Marino   el = (struct elt_list *) pool_alloc (elt_list_pool);
222*e4b17023SJohn Marino   el->next = next;
223*e4b17023SJohn Marino   el->elt = elt;
224*e4b17023SJohn Marino   return el;
225*e4b17023SJohn Marino }
226*e4b17023SJohn Marino 
227*e4b17023SJohn Marino /* Allocate a struct elt_loc_list with LOC and prepend it to VAL's loc
228*e4b17023SJohn Marino    list.  */
229*e4b17023SJohn Marino 
230*e4b17023SJohn Marino static inline void
new_elt_loc_list(cselib_val * val,rtx loc)231*e4b17023SJohn Marino new_elt_loc_list (cselib_val *val, rtx loc)
232*e4b17023SJohn Marino {
233*e4b17023SJohn Marino   struct elt_loc_list *el, *next = val->locs;
234*e4b17023SJohn Marino 
235*e4b17023SJohn Marino   gcc_checking_assert (!next || !next->setting_insn
236*e4b17023SJohn Marino 		       || !DEBUG_INSN_P (next->setting_insn)
237*e4b17023SJohn Marino 		       || cselib_current_insn == next->setting_insn);
238*e4b17023SJohn Marino 
239*e4b17023SJohn Marino   /* If we're creating the first loc in a debug insn context, we've
240*e4b17023SJohn Marino      just created a debug value.  Count it.  */
241*e4b17023SJohn Marino   if (!next && cselib_current_insn && DEBUG_INSN_P (cselib_current_insn))
242*e4b17023SJohn Marino     n_debug_values++;
243*e4b17023SJohn Marino 
244*e4b17023SJohn Marino   val = canonical_cselib_val (val);
245*e4b17023SJohn Marino   next = val->locs;
246*e4b17023SJohn Marino 
247*e4b17023SJohn Marino   if (GET_CODE (loc) == VALUE)
248*e4b17023SJohn Marino     {
249*e4b17023SJohn Marino       loc = canonical_cselib_val (CSELIB_VAL_PTR (loc))->val_rtx;
250*e4b17023SJohn Marino 
251*e4b17023SJohn Marino       gcc_checking_assert (PRESERVED_VALUE_P (loc)
252*e4b17023SJohn Marino 			   == PRESERVED_VALUE_P (val->val_rtx));
253*e4b17023SJohn Marino 
254*e4b17023SJohn Marino       if (val->val_rtx == loc)
255*e4b17023SJohn Marino 	return;
256*e4b17023SJohn Marino       else if (val->uid > CSELIB_VAL_PTR (loc)->uid)
257*e4b17023SJohn Marino 	{
258*e4b17023SJohn Marino 	  /* Reverse the insertion.  */
259*e4b17023SJohn Marino 	  new_elt_loc_list (CSELIB_VAL_PTR (loc), val->val_rtx);
260*e4b17023SJohn Marino 	  return;
261*e4b17023SJohn Marino 	}
262*e4b17023SJohn Marino 
263*e4b17023SJohn Marino       gcc_checking_assert (val->uid < CSELIB_VAL_PTR (loc)->uid);
264*e4b17023SJohn Marino 
265*e4b17023SJohn Marino       if (CSELIB_VAL_PTR (loc)->locs)
266*e4b17023SJohn Marino 	{
267*e4b17023SJohn Marino 	  /* Bring all locs from LOC to VAL.  */
268*e4b17023SJohn Marino 	  for (el = CSELIB_VAL_PTR (loc)->locs; el->next; el = el->next)
269*e4b17023SJohn Marino 	    {
270*e4b17023SJohn Marino 	      /* Adjust values that have LOC as canonical so that VAL
271*e4b17023SJohn Marino 		 becomes their canonical.  */
272*e4b17023SJohn Marino 	      if (el->loc && GET_CODE (el->loc) == VALUE)
273*e4b17023SJohn Marino 		{
274*e4b17023SJohn Marino 		  gcc_checking_assert (CSELIB_VAL_PTR (el->loc)->locs->loc
275*e4b17023SJohn Marino 				       == loc);
276*e4b17023SJohn Marino 		  CSELIB_VAL_PTR (el->loc)->locs->loc = val->val_rtx;
277*e4b17023SJohn Marino 		}
278*e4b17023SJohn Marino 	    }
279*e4b17023SJohn Marino 	  el->next = val->locs;
280*e4b17023SJohn Marino 	  next = val->locs = CSELIB_VAL_PTR (loc)->locs;
281*e4b17023SJohn Marino 	}
282*e4b17023SJohn Marino 
283*e4b17023SJohn Marino       if (CSELIB_VAL_PTR (loc)->addr_list)
284*e4b17023SJohn Marino 	{
285*e4b17023SJohn Marino 	  /* Bring in addr_list into canonical node.  */
286*e4b17023SJohn Marino 	  struct elt_list *last = CSELIB_VAL_PTR (loc)->addr_list;
287*e4b17023SJohn Marino 	  while (last->next)
288*e4b17023SJohn Marino 	    last = last->next;
289*e4b17023SJohn Marino 	  last->next = val->addr_list;
290*e4b17023SJohn Marino 	  val->addr_list = CSELIB_VAL_PTR (loc)->addr_list;
291*e4b17023SJohn Marino 	  CSELIB_VAL_PTR (loc)->addr_list = NULL;
292*e4b17023SJohn Marino 	}
293*e4b17023SJohn Marino 
294*e4b17023SJohn Marino       if (CSELIB_VAL_PTR (loc)->next_containing_mem != NULL
295*e4b17023SJohn Marino 	  && val->next_containing_mem == NULL)
296*e4b17023SJohn Marino 	{
297*e4b17023SJohn Marino 	  /* Add VAL to the containing_mem list after LOC.  LOC will
298*e4b17023SJohn Marino 	     be removed when we notice it doesn't contain any
299*e4b17023SJohn Marino 	     MEMs.  */
300*e4b17023SJohn Marino 	  val->next_containing_mem = CSELIB_VAL_PTR (loc)->next_containing_mem;
301*e4b17023SJohn Marino 	  CSELIB_VAL_PTR (loc)->next_containing_mem = val;
302*e4b17023SJohn Marino 	}
303*e4b17023SJohn Marino 
304*e4b17023SJohn Marino       /* Chain LOC back to VAL.  */
305*e4b17023SJohn Marino       el = (struct elt_loc_list *) pool_alloc (elt_loc_list_pool);
306*e4b17023SJohn Marino       el->loc = val->val_rtx;
307*e4b17023SJohn Marino       el->setting_insn = cselib_current_insn;
308*e4b17023SJohn Marino       el->next = NULL;
309*e4b17023SJohn Marino       CSELIB_VAL_PTR (loc)->locs = el;
310*e4b17023SJohn Marino     }
311*e4b17023SJohn Marino 
312*e4b17023SJohn Marino   el = (struct elt_loc_list *) pool_alloc (elt_loc_list_pool);
313*e4b17023SJohn Marino   el->loc = loc;
314*e4b17023SJohn Marino   el->setting_insn = cselib_current_insn;
315*e4b17023SJohn Marino   el->next = next;
316*e4b17023SJohn Marino   val->locs = el;
317*e4b17023SJohn Marino }
318*e4b17023SJohn Marino 
319*e4b17023SJohn Marino /* Promote loc L to a nondebug cselib_current_insn if L is marked as
320*e4b17023SJohn Marino    originating from a debug insn, maintaining the debug values
321*e4b17023SJohn Marino    count.  */
322*e4b17023SJohn Marino 
323*e4b17023SJohn Marino static inline void
promote_debug_loc(struct elt_loc_list * l)324*e4b17023SJohn Marino promote_debug_loc (struct elt_loc_list *l)
325*e4b17023SJohn Marino {
326*e4b17023SJohn Marino   if (l && l->setting_insn && DEBUG_INSN_P (l->setting_insn)
327*e4b17023SJohn Marino       && (!cselib_current_insn || !DEBUG_INSN_P (cselib_current_insn)))
328*e4b17023SJohn Marino     {
329*e4b17023SJohn Marino       n_debug_values--;
330*e4b17023SJohn Marino       l->setting_insn = cselib_current_insn;
331*e4b17023SJohn Marino       if (cselib_preserve_constants && l->next)
332*e4b17023SJohn Marino 	{
333*e4b17023SJohn Marino 	  gcc_assert (l->next->setting_insn
334*e4b17023SJohn Marino 		      && DEBUG_INSN_P (l->next->setting_insn)
335*e4b17023SJohn Marino 		      && !l->next->next);
336*e4b17023SJohn Marino 	  l->next->setting_insn = cselib_current_insn;
337*e4b17023SJohn Marino 	}
338*e4b17023SJohn Marino       else
339*e4b17023SJohn Marino 	gcc_assert (!l->next);
340*e4b17023SJohn Marino     }
341*e4b17023SJohn Marino }
342*e4b17023SJohn Marino 
343*e4b17023SJohn Marino /* The elt_list at *PL is no longer needed.  Unchain it and free its
344*e4b17023SJohn Marino    storage.  */
345*e4b17023SJohn Marino 
346*e4b17023SJohn Marino static inline void
unchain_one_elt_list(struct elt_list ** pl)347*e4b17023SJohn Marino unchain_one_elt_list (struct elt_list **pl)
348*e4b17023SJohn Marino {
349*e4b17023SJohn Marino   struct elt_list *l = *pl;
350*e4b17023SJohn Marino 
351*e4b17023SJohn Marino   *pl = l->next;
352*e4b17023SJohn Marino   pool_free (elt_list_pool, l);
353*e4b17023SJohn Marino }
354*e4b17023SJohn Marino 
355*e4b17023SJohn Marino /* Likewise for elt_loc_lists.  */
356*e4b17023SJohn Marino 
357*e4b17023SJohn Marino static void
unchain_one_elt_loc_list(struct elt_loc_list ** pl)358*e4b17023SJohn Marino unchain_one_elt_loc_list (struct elt_loc_list **pl)
359*e4b17023SJohn Marino {
360*e4b17023SJohn Marino   struct elt_loc_list *l = *pl;
361*e4b17023SJohn Marino 
362*e4b17023SJohn Marino   *pl = l->next;
363*e4b17023SJohn Marino   pool_free (elt_loc_list_pool, l);
364*e4b17023SJohn Marino }
365*e4b17023SJohn Marino 
366*e4b17023SJohn Marino /* Likewise for cselib_vals.  This also frees the addr_list associated with
367*e4b17023SJohn Marino    V.  */
368*e4b17023SJohn Marino 
369*e4b17023SJohn Marino static void
unchain_one_value(cselib_val * v)370*e4b17023SJohn Marino unchain_one_value (cselib_val *v)
371*e4b17023SJohn Marino {
372*e4b17023SJohn Marino   while (v->addr_list)
373*e4b17023SJohn Marino     unchain_one_elt_list (&v->addr_list);
374*e4b17023SJohn Marino 
375*e4b17023SJohn Marino   pool_free (cselib_val_pool, v);
376*e4b17023SJohn Marino }
377*e4b17023SJohn Marino 
378*e4b17023SJohn Marino /* Remove all entries from the hash table.  Also used during
379*e4b17023SJohn Marino    initialization.  */
380*e4b17023SJohn Marino 
381*e4b17023SJohn Marino void
cselib_clear_table(void)382*e4b17023SJohn Marino cselib_clear_table (void)
383*e4b17023SJohn Marino {
384*e4b17023SJohn Marino   cselib_reset_table (1);
385*e4b17023SJohn Marino }
386*e4b17023SJohn Marino 
387*e4b17023SJohn Marino /* Return TRUE if V is a constant, a function invariant or a VALUE
388*e4b17023SJohn Marino    equivalence; FALSE otherwise.  */
389*e4b17023SJohn Marino 
390*e4b17023SJohn Marino static bool
invariant_or_equiv_p(cselib_val * v)391*e4b17023SJohn Marino invariant_or_equiv_p (cselib_val *v)
392*e4b17023SJohn Marino {
393*e4b17023SJohn Marino   struct elt_loc_list *l;
394*e4b17023SJohn Marino 
395*e4b17023SJohn Marino   if (v == cfa_base_preserved_val)
396*e4b17023SJohn Marino     return true;
397*e4b17023SJohn Marino 
398*e4b17023SJohn Marino   /* Keep VALUE equivalences around.  */
399*e4b17023SJohn Marino   for (l = v->locs; l; l = l->next)
400*e4b17023SJohn Marino     if (GET_CODE (l->loc) == VALUE)
401*e4b17023SJohn Marino       return true;
402*e4b17023SJohn Marino 
403*e4b17023SJohn Marino   if (v->locs != NULL
404*e4b17023SJohn Marino       && v->locs->next == NULL)
405*e4b17023SJohn Marino     {
406*e4b17023SJohn Marino       if (CONSTANT_P (v->locs->loc)
407*e4b17023SJohn Marino 	  && (GET_CODE (v->locs->loc) != CONST
408*e4b17023SJohn Marino 	      || !references_value_p (v->locs->loc, 0)))
409*e4b17023SJohn Marino 	return true;
410*e4b17023SJohn Marino       /* Although a debug expr may be bound to different expressions,
411*e4b17023SJohn Marino 	 we can preserve it as if it was constant, to get unification
412*e4b17023SJohn Marino 	 and proper merging within var-tracking.  */
413*e4b17023SJohn Marino       if (GET_CODE (v->locs->loc) == DEBUG_EXPR
414*e4b17023SJohn Marino 	  || GET_CODE (v->locs->loc) == DEBUG_IMPLICIT_PTR
415*e4b17023SJohn Marino 	  || GET_CODE (v->locs->loc) == ENTRY_VALUE
416*e4b17023SJohn Marino 	  || GET_CODE (v->locs->loc) == DEBUG_PARAMETER_REF)
417*e4b17023SJohn Marino 	return true;
418*e4b17023SJohn Marino 
419*e4b17023SJohn Marino       /* (plus (value V) (const_int C)) is invariant iff V is invariant.  */
420*e4b17023SJohn Marino       if (GET_CODE (v->locs->loc) == PLUS
421*e4b17023SJohn Marino 	  && CONST_INT_P (XEXP (v->locs->loc, 1))
422*e4b17023SJohn Marino 	  && GET_CODE (XEXP (v->locs->loc, 0)) == VALUE
423*e4b17023SJohn Marino 	  && invariant_or_equiv_p (CSELIB_VAL_PTR (XEXP (v->locs->loc, 0))))
424*e4b17023SJohn Marino 	return true;
425*e4b17023SJohn Marino     }
426*e4b17023SJohn Marino 
427*e4b17023SJohn Marino   return false;
428*e4b17023SJohn Marino }
429*e4b17023SJohn Marino 
430*e4b17023SJohn Marino /* Remove from hash table all VALUEs except constants, function
431*e4b17023SJohn Marino    invariants and VALUE equivalences.  */
432*e4b17023SJohn Marino 
433*e4b17023SJohn Marino static int
preserve_constants_and_equivs(void ** x,void * info ATTRIBUTE_UNUSED)434*e4b17023SJohn Marino preserve_constants_and_equivs (void **x, void *info ATTRIBUTE_UNUSED)
435*e4b17023SJohn Marino {
436*e4b17023SJohn Marino   cselib_val *v = (cselib_val *)*x;
437*e4b17023SJohn Marino 
438*e4b17023SJohn Marino   if (!invariant_or_equiv_p (v))
439*e4b17023SJohn Marino     htab_clear_slot (cselib_hash_table, x);
440*e4b17023SJohn Marino   return 1;
441*e4b17023SJohn Marino }
442*e4b17023SJohn Marino 
443*e4b17023SJohn Marino /* Remove all entries from the hash table, arranging for the next
444*e4b17023SJohn Marino    value to be numbered NUM.  */
445*e4b17023SJohn Marino 
446*e4b17023SJohn Marino void
cselib_reset_table(unsigned int num)447*e4b17023SJohn Marino cselib_reset_table (unsigned int num)
448*e4b17023SJohn Marino {
449*e4b17023SJohn Marino   unsigned int i;
450*e4b17023SJohn Marino 
451*e4b17023SJohn Marino   max_value_regs = 0;
452*e4b17023SJohn Marino 
453*e4b17023SJohn Marino   if (cfa_base_preserved_val)
454*e4b17023SJohn Marino     {
455*e4b17023SJohn Marino       unsigned int regno = cfa_base_preserved_regno;
456*e4b17023SJohn Marino       unsigned int new_used_regs = 0;
457*e4b17023SJohn Marino       for (i = 0; i < n_used_regs; i++)
458*e4b17023SJohn Marino 	if (used_regs[i] == regno)
459*e4b17023SJohn Marino 	  {
460*e4b17023SJohn Marino 	    new_used_regs = 1;
461*e4b17023SJohn Marino 	    continue;
462*e4b17023SJohn Marino 	  }
463*e4b17023SJohn Marino 	else
464*e4b17023SJohn Marino 	  REG_VALUES (used_regs[i]) = 0;
465*e4b17023SJohn Marino       gcc_assert (new_used_regs == 1);
466*e4b17023SJohn Marino       n_used_regs = new_used_regs;
467*e4b17023SJohn Marino       used_regs[0] = regno;
468*e4b17023SJohn Marino       max_value_regs
469*e4b17023SJohn Marino 	= hard_regno_nregs[regno][GET_MODE (cfa_base_preserved_val->locs->loc)];
470*e4b17023SJohn Marino     }
471*e4b17023SJohn Marino   else
472*e4b17023SJohn Marino     {
473*e4b17023SJohn Marino       for (i = 0; i < n_used_regs; i++)
474*e4b17023SJohn Marino 	REG_VALUES (used_regs[i]) = 0;
475*e4b17023SJohn Marino       n_used_regs = 0;
476*e4b17023SJohn Marino     }
477*e4b17023SJohn Marino 
478*e4b17023SJohn Marino   if (cselib_preserve_constants)
479*e4b17023SJohn Marino     htab_traverse (cselib_hash_table, preserve_constants_and_equivs, NULL);
480*e4b17023SJohn Marino   else
481*e4b17023SJohn Marino     {
482*e4b17023SJohn Marino       htab_empty (cselib_hash_table);
483*e4b17023SJohn Marino       gcc_checking_assert (!cselib_any_perm_equivs);
484*e4b17023SJohn Marino     }
485*e4b17023SJohn Marino 
486*e4b17023SJohn Marino   n_useless_values = 0;
487*e4b17023SJohn Marino   n_useless_debug_values = 0;
488*e4b17023SJohn Marino   n_debug_values = 0;
489*e4b17023SJohn Marino 
490*e4b17023SJohn Marino   next_uid = num;
491*e4b17023SJohn Marino 
492*e4b17023SJohn Marino   first_containing_mem = &dummy_val;
493*e4b17023SJohn Marino }
494*e4b17023SJohn Marino 
495*e4b17023SJohn Marino /* Return the number of the next value that will be generated.  */
496*e4b17023SJohn Marino 
497*e4b17023SJohn Marino unsigned int
cselib_get_next_uid(void)498*e4b17023SJohn Marino cselib_get_next_uid (void)
499*e4b17023SJohn Marino {
500*e4b17023SJohn Marino   return next_uid;
501*e4b17023SJohn Marino }
502*e4b17023SJohn Marino 
503*e4b17023SJohn Marino /* See the documentation of cselib_find_slot below.  */
504*e4b17023SJohn Marino static enum machine_mode find_slot_memmode;
505*e4b17023SJohn Marino 
506*e4b17023SJohn Marino /* Search for X, whose hashcode is HASH, in CSELIB_HASH_TABLE,
507*e4b17023SJohn Marino    INSERTing if requested.  When X is part of the address of a MEM,
508*e4b17023SJohn Marino    MEMMODE should specify the mode of the MEM.  While searching the
509*e4b17023SJohn Marino    table, MEMMODE is held in FIND_SLOT_MEMMODE, so that autoinc RTXs
510*e4b17023SJohn Marino    in X can be resolved.  */
511*e4b17023SJohn Marino 
512*e4b17023SJohn Marino static void **
cselib_find_slot(rtx x,hashval_t hash,enum insert_option insert,enum machine_mode memmode)513*e4b17023SJohn Marino cselib_find_slot (rtx x, hashval_t hash, enum insert_option insert,
514*e4b17023SJohn Marino 		  enum machine_mode memmode)
515*e4b17023SJohn Marino {
516*e4b17023SJohn Marino   void **slot;
517*e4b17023SJohn Marino   find_slot_memmode = memmode;
518*e4b17023SJohn Marino   slot = htab_find_slot_with_hash (cselib_hash_table, x, hash, insert);
519*e4b17023SJohn Marino   find_slot_memmode = VOIDmode;
520*e4b17023SJohn Marino   return slot;
521*e4b17023SJohn Marino }
522*e4b17023SJohn Marino 
523*e4b17023SJohn Marino /* The equality test for our hash table.  The first argument ENTRY is a table
524*e4b17023SJohn Marino    element (i.e. a cselib_val), while the second arg X is an rtx.  We know
525*e4b17023SJohn Marino    that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
526*e4b17023SJohn Marino    CONST of an appropriate mode.  */
527*e4b17023SJohn Marino 
528*e4b17023SJohn Marino static int
entry_and_rtx_equal_p(const void * entry,const void * x_arg)529*e4b17023SJohn Marino entry_and_rtx_equal_p (const void *entry, const void *x_arg)
530*e4b17023SJohn Marino {
531*e4b17023SJohn Marino   struct elt_loc_list *l;
532*e4b17023SJohn Marino   const cselib_val *const v = (const cselib_val *) entry;
533*e4b17023SJohn Marino   rtx x = CONST_CAST_RTX ((const_rtx)x_arg);
534*e4b17023SJohn Marino   enum machine_mode mode = GET_MODE (x);
535*e4b17023SJohn Marino 
536*e4b17023SJohn Marino   gcc_assert (!CONST_INT_P (x) && GET_CODE (x) != CONST_FIXED
537*e4b17023SJohn Marino 	      && (mode != VOIDmode || GET_CODE (x) != CONST_DOUBLE));
538*e4b17023SJohn Marino 
539*e4b17023SJohn Marino   if (mode != GET_MODE (v->val_rtx))
540*e4b17023SJohn Marino     return 0;
541*e4b17023SJohn Marino 
542*e4b17023SJohn Marino   /* Unwrap X if necessary.  */
543*e4b17023SJohn Marino   if (GET_CODE (x) == CONST
544*e4b17023SJohn Marino       && (CONST_INT_P (XEXP (x, 0))
545*e4b17023SJohn Marino 	  || GET_CODE (XEXP (x, 0)) == CONST_FIXED
546*e4b17023SJohn Marino 	  || GET_CODE (XEXP (x, 0)) == CONST_DOUBLE))
547*e4b17023SJohn Marino     x = XEXP (x, 0);
548*e4b17023SJohn Marino 
549*e4b17023SJohn Marino   /* We don't guarantee that distinct rtx's have different hash values,
550*e4b17023SJohn Marino      so we need to do a comparison.  */
551*e4b17023SJohn Marino   for (l = v->locs; l; l = l->next)
552*e4b17023SJohn Marino     if (rtx_equal_for_cselib_1 (l->loc, x, find_slot_memmode))
553*e4b17023SJohn Marino       {
554*e4b17023SJohn Marino 	promote_debug_loc (l);
555*e4b17023SJohn Marino 	return 1;
556*e4b17023SJohn Marino       }
557*e4b17023SJohn Marino 
558*e4b17023SJohn Marino   return 0;
559*e4b17023SJohn Marino }
560*e4b17023SJohn Marino 
561*e4b17023SJohn Marino /* The hash function for our hash table.  The value is always computed with
562*e4b17023SJohn Marino    cselib_hash_rtx when adding an element; this function just extracts the
563*e4b17023SJohn Marino    hash value from a cselib_val structure.  */
564*e4b17023SJohn Marino 
565*e4b17023SJohn Marino static hashval_t
get_value_hash(const void * entry)566*e4b17023SJohn Marino get_value_hash (const void *entry)
567*e4b17023SJohn Marino {
568*e4b17023SJohn Marino   const cselib_val *const v = (const cselib_val *) entry;
569*e4b17023SJohn Marino   return v->hash;
570*e4b17023SJohn Marino }
571*e4b17023SJohn Marino 
572*e4b17023SJohn Marino /* Return true if X contains a VALUE rtx.  If ONLY_USELESS is set, we
573*e4b17023SJohn Marino    only return true for values which point to a cselib_val whose value
574*e4b17023SJohn Marino    element has been set to zero, which implies the cselib_val will be
575*e4b17023SJohn Marino    removed.  */
576*e4b17023SJohn Marino 
577*e4b17023SJohn Marino int
references_value_p(const_rtx x,int only_useless)578*e4b17023SJohn Marino references_value_p (const_rtx x, int only_useless)
579*e4b17023SJohn Marino {
580*e4b17023SJohn Marino   const enum rtx_code code = GET_CODE (x);
581*e4b17023SJohn Marino   const char *fmt = GET_RTX_FORMAT (code);
582*e4b17023SJohn Marino   int i, j;
583*e4b17023SJohn Marino 
584*e4b17023SJohn Marino   if (GET_CODE (x) == VALUE
585*e4b17023SJohn Marino       && (! only_useless ||
586*e4b17023SJohn Marino 	  (CSELIB_VAL_PTR (x)->locs == 0 && !PRESERVED_VALUE_P (x))))
587*e4b17023SJohn Marino     return 1;
588*e4b17023SJohn Marino 
589*e4b17023SJohn Marino   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
590*e4b17023SJohn Marino     {
591*e4b17023SJohn Marino       if (fmt[i] == 'e' && references_value_p (XEXP (x, i), only_useless))
592*e4b17023SJohn Marino 	return 1;
593*e4b17023SJohn Marino       else if (fmt[i] == 'E')
594*e4b17023SJohn Marino 	for (j = 0; j < XVECLEN (x, i); j++)
595*e4b17023SJohn Marino 	  if (references_value_p (XVECEXP (x, i, j), only_useless))
596*e4b17023SJohn Marino 	    return 1;
597*e4b17023SJohn Marino     }
598*e4b17023SJohn Marino 
599*e4b17023SJohn Marino   return 0;
600*e4b17023SJohn Marino }
601*e4b17023SJohn Marino 
602*e4b17023SJohn Marino /* For all locations found in X, delete locations that reference useless
603*e4b17023SJohn Marino    values (i.e. values without any location).  Called through
604*e4b17023SJohn Marino    htab_traverse.  */
605*e4b17023SJohn Marino 
606*e4b17023SJohn Marino static int
discard_useless_locs(void ** x,void * info ATTRIBUTE_UNUSED)607*e4b17023SJohn Marino discard_useless_locs (void **x, void *info ATTRIBUTE_UNUSED)
608*e4b17023SJohn Marino {
609*e4b17023SJohn Marino   cselib_val *v = (cselib_val *)*x;
610*e4b17023SJohn Marino   struct elt_loc_list **p = &v->locs;
611*e4b17023SJohn Marino   bool had_locs = v->locs != NULL;
612*e4b17023SJohn Marino   rtx setting_insn = v->locs ? v->locs->setting_insn : NULL;
613*e4b17023SJohn Marino 
614*e4b17023SJohn Marino   while (*p)
615*e4b17023SJohn Marino     {
616*e4b17023SJohn Marino       if (references_value_p ((*p)->loc, 1))
617*e4b17023SJohn Marino 	unchain_one_elt_loc_list (p);
618*e4b17023SJohn Marino       else
619*e4b17023SJohn Marino 	p = &(*p)->next;
620*e4b17023SJohn Marino     }
621*e4b17023SJohn Marino 
622*e4b17023SJohn Marino   if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
623*e4b17023SJohn Marino     {
624*e4b17023SJohn Marino       if (setting_insn && DEBUG_INSN_P (setting_insn))
625*e4b17023SJohn Marino 	n_useless_debug_values++;
626*e4b17023SJohn Marino       else
627*e4b17023SJohn Marino 	n_useless_values++;
628*e4b17023SJohn Marino       values_became_useless = 1;
629*e4b17023SJohn Marino     }
630*e4b17023SJohn Marino   return 1;
631*e4b17023SJohn Marino }
632*e4b17023SJohn Marino 
633*e4b17023SJohn Marino /* If X is a value with no locations, remove it from the hashtable.  */
634*e4b17023SJohn Marino 
635*e4b17023SJohn Marino static int
discard_useless_values(void ** x,void * info ATTRIBUTE_UNUSED)636*e4b17023SJohn Marino discard_useless_values (void **x, void *info ATTRIBUTE_UNUSED)
637*e4b17023SJohn Marino {
638*e4b17023SJohn Marino   cselib_val *v = (cselib_val *)*x;
639*e4b17023SJohn Marino 
640*e4b17023SJohn Marino   if (v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
641*e4b17023SJohn Marino     {
642*e4b17023SJohn Marino       if (cselib_discard_hook)
643*e4b17023SJohn Marino 	cselib_discard_hook (v);
644*e4b17023SJohn Marino 
645*e4b17023SJohn Marino       CSELIB_VAL_PTR (v->val_rtx) = NULL;
646*e4b17023SJohn Marino       htab_clear_slot (cselib_hash_table, x);
647*e4b17023SJohn Marino       unchain_one_value (v);
648*e4b17023SJohn Marino       n_useless_values--;
649*e4b17023SJohn Marino     }
650*e4b17023SJohn Marino 
651*e4b17023SJohn Marino   return 1;
652*e4b17023SJohn Marino }
653*e4b17023SJohn Marino 
654*e4b17023SJohn Marino /* Clean out useless values (i.e. those which no longer have locations
655*e4b17023SJohn Marino    associated with them) from the hash table.  */
656*e4b17023SJohn Marino 
657*e4b17023SJohn Marino static void
remove_useless_values(void)658*e4b17023SJohn Marino remove_useless_values (void)
659*e4b17023SJohn Marino {
660*e4b17023SJohn Marino   cselib_val **p, *v;
661*e4b17023SJohn Marino 
662*e4b17023SJohn Marino   /* First pass: eliminate locations that reference the value.  That in
663*e4b17023SJohn Marino      turn can make more values useless.  */
664*e4b17023SJohn Marino   do
665*e4b17023SJohn Marino     {
666*e4b17023SJohn Marino       values_became_useless = 0;
667*e4b17023SJohn Marino       htab_traverse (cselib_hash_table, discard_useless_locs, 0);
668*e4b17023SJohn Marino     }
669*e4b17023SJohn Marino   while (values_became_useless);
670*e4b17023SJohn Marino 
671*e4b17023SJohn Marino   /* Second pass: actually remove the values.  */
672*e4b17023SJohn Marino 
673*e4b17023SJohn Marino   p = &first_containing_mem;
674*e4b17023SJohn Marino   for (v = *p; v != &dummy_val; v = v->next_containing_mem)
675*e4b17023SJohn Marino     if (v->locs && v == canonical_cselib_val (v))
676*e4b17023SJohn Marino       {
677*e4b17023SJohn Marino 	*p = v;
678*e4b17023SJohn Marino 	p = &(*p)->next_containing_mem;
679*e4b17023SJohn Marino       }
680*e4b17023SJohn Marino   *p = &dummy_val;
681*e4b17023SJohn Marino 
682*e4b17023SJohn Marino   n_useless_values += n_useless_debug_values;
683*e4b17023SJohn Marino   n_debug_values -= n_useless_debug_values;
684*e4b17023SJohn Marino   n_useless_debug_values = 0;
685*e4b17023SJohn Marino 
686*e4b17023SJohn Marino   htab_traverse (cselib_hash_table, discard_useless_values, 0);
687*e4b17023SJohn Marino 
688*e4b17023SJohn Marino   gcc_assert (!n_useless_values);
689*e4b17023SJohn Marino }
690*e4b17023SJohn Marino 
691*e4b17023SJohn Marino /* Arrange for a value to not be removed from the hash table even if
692*e4b17023SJohn Marino    it becomes useless.  */
693*e4b17023SJohn Marino 
694*e4b17023SJohn Marino void
cselib_preserve_value(cselib_val * v)695*e4b17023SJohn Marino cselib_preserve_value (cselib_val *v)
696*e4b17023SJohn Marino {
697*e4b17023SJohn Marino   PRESERVED_VALUE_P (v->val_rtx) = 1;
698*e4b17023SJohn Marino }
699*e4b17023SJohn Marino 
700*e4b17023SJohn Marino /* Test whether a value is preserved.  */
701*e4b17023SJohn Marino 
702*e4b17023SJohn Marino bool
cselib_preserved_value_p(cselib_val * v)703*e4b17023SJohn Marino cselib_preserved_value_p (cselib_val *v)
704*e4b17023SJohn Marino {
705*e4b17023SJohn Marino   return PRESERVED_VALUE_P (v->val_rtx);
706*e4b17023SJohn Marino }
707*e4b17023SJohn Marino 
708*e4b17023SJohn Marino /* Arrange for a REG value to be assumed constant through the whole function,
709*e4b17023SJohn Marino    never invalidated and preserved across cselib_reset_table calls.  */
710*e4b17023SJohn Marino 
711*e4b17023SJohn Marino void
cselib_preserve_cfa_base_value(cselib_val * v,unsigned int regno)712*e4b17023SJohn Marino cselib_preserve_cfa_base_value (cselib_val *v, unsigned int regno)
713*e4b17023SJohn Marino {
714*e4b17023SJohn Marino   if (cselib_preserve_constants
715*e4b17023SJohn Marino       && v->locs
716*e4b17023SJohn Marino       && REG_P (v->locs->loc))
717*e4b17023SJohn Marino     {
718*e4b17023SJohn Marino       cfa_base_preserved_val = v;
719*e4b17023SJohn Marino       cfa_base_preserved_regno = regno;
720*e4b17023SJohn Marino     }
721*e4b17023SJohn Marino }
722*e4b17023SJohn Marino 
723*e4b17023SJohn Marino /* Clean all non-constant expressions in the hash table, but retain
724*e4b17023SJohn Marino    their values.  */
725*e4b17023SJohn Marino 
726*e4b17023SJohn Marino void
cselib_preserve_only_values(void)727*e4b17023SJohn Marino cselib_preserve_only_values (void)
728*e4b17023SJohn Marino {
729*e4b17023SJohn Marino   int i;
730*e4b17023SJohn Marino 
731*e4b17023SJohn Marino   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
732*e4b17023SJohn Marino     cselib_invalidate_regno (i, reg_raw_mode[i]);
733*e4b17023SJohn Marino 
734*e4b17023SJohn Marino   cselib_invalidate_mem (callmem);
735*e4b17023SJohn Marino 
736*e4b17023SJohn Marino   remove_useless_values ();
737*e4b17023SJohn Marino 
738*e4b17023SJohn Marino   gcc_assert (first_containing_mem == &dummy_val);
739*e4b17023SJohn Marino }
740*e4b17023SJohn Marino 
741*e4b17023SJohn Marino /* Return the mode in which a register was last set.  If X is not a
742*e4b17023SJohn Marino    register, return its mode.  If the mode in which the register was
743*e4b17023SJohn Marino    set is not known, or the value was already clobbered, return
744*e4b17023SJohn Marino    VOIDmode.  */
745*e4b17023SJohn Marino 
746*e4b17023SJohn Marino enum machine_mode
cselib_reg_set_mode(const_rtx x)747*e4b17023SJohn Marino cselib_reg_set_mode (const_rtx x)
748*e4b17023SJohn Marino {
749*e4b17023SJohn Marino   if (!REG_P (x))
750*e4b17023SJohn Marino     return GET_MODE (x);
751*e4b17023SJohn Marino 
752*e4b17023SJohn Marino   if (REG_VALUES (REGNO (x)) == NULL
753*e4b17023SJohn Marino       || REG_VALUES (REGNO (x))->elt == NULL)
754*e4b17023SJohn Marino     return VOIDmode;
755*e4b17023SJohn Marino 
756*e4b17023SJohn Marino   return GET_MODE (REG_VALUES (REGNO (x))->elt->val_rtx);
757*e4b17023SJohn Marino }
758*e4b17023SJohn Marino 
759*e4b17023SJohn Marino /* Return nonzero if we can prove that X and Y contain the same value, taking
760*e4b17023SJohn Marino    our gathered information into account.  */
761*e4b17023SJohn Marino 
762*e4b17023SJohn Marino int
rtx_equal_for_cselib_p(rtx x,rtx y)763*e4b17023SJohn Marino rtx_equal_for_cselib_p (rtx x, rtx y)
764*e4b17023SJohn Marino {
765*e4b17023SJohn Marino   return rtx_equal_for_cselib_1 (x, y, VOIDmode);
766*e4b17023SJohn Marino }
767*e4b17023SJohn Marino 
768*e4b17023SJohn Marino /* If x is a PLUS or an autoinc operation, expand the operation,
769*e4b17023SJohn Marino    storing the offset, if any, in *OFF.  */
770*e4b17023SJohn Marino 
771*e4b17023SJohn Marino static rtx
autoinc_split(rtx x,rtx * off,enum machine_mode memmode)772*e4b17023SJohn Marino autoinc_split (rtx x, rtx *off, enum machine_mode memmode)
773*e4b17023SJohn Marino {
774*e4b17023SJohn Marino   switch (GET_CODE (x))
775*e4b17023SJohn Marino     {
776*e4b17023SJohn Marino     case PLUS:
777*e4b17023SJohn Marino       *off = XEXP (x, 1);
778*e4b17023SJohn Marino       return XEXP (x, 0);
779*e4b17023SJohn Marino 
780*e4b17023SJohn Marino     case PRE_DEC:
781*e4b17023SJohn Marino       if (memmode == VOIDmode)
782*e4b17023SJohn Marino 	return x;
783*e4b17023SJohn Marino 
784*e4b17023SJohn Marino       *off = GEN_INT (-GET_MODE_SIZE (memmode));
785*e4b17023SJohn Marino       return XEXP (x, 0);
786*e4b17023SJohn Marino       break;
787*e4b17023SJohn Marino 
788*e4b17023SJohn Marino     case PRE_INC:
789*e4b17023SJohn Marino       if (memmode == VOIDmode)
790*e4b17023SJohn Marino 	return x;
791*e4b17023SJohn Marino 
792*e4b17023SJohn Marino       *off = GEN_INT (GET_MODE_SIZE (memmode));
793*e4b17023SJohn Marino       return XEXP (x, 0);
794*e4b17023SJohn Marino 
795*e4b17023SJohn Marino     case PRE_MODIFY:
796*e4b17023SJohn Marino       return XEXP (x, 1);
797*e4b17023SJohn Marino 
798*e4b17023SJohn Marino     case POST_DEC:
799*e4b17023SJohn Marino     case POST_INC:
800*e4b17023SJohn Marino     case POST_MODIFY:
801*e4b17023SJohn Marino       return XEXP (x, 0);
802*e4b17023SJohn Marino 
803*e4b17023SJohn Marino     default:
804*e4b17023SJohn Marino       return x;
805*e4b17023SJohn Marino     }
806*e4b17023SJohn Marino }
807*e4b17023SJohn Marino 
808*e4b17023SJohn Marino /* Return nonzero if we can prove that X and Y contain the same value,
809*e4b17023SJohn Marino    taking our gathered information into account.  MEMMODE holds the
810*e4b17023SJohn Marino    mode of the enclosing MEM, if any, as required to deal with autoinc
811*e4b17023SJohn Marino    addressing modes.  If X and Y are not (known to be) part of
812*e4b17023SJohn Marino    addresses, MEMMODE should be VOIDmode.  */
813*e4b17023SJohn Marino 
814*e4b17023SJohn Marino static int
rtx_equal_for_cselib_1(rtx x,rtx y,enum machine_mode memmode)815*e4b17023SJohn Marino rtx_equal_for_cselib_1 (rtx x, rtx y, enum machine_mode memmode)
816*e4b17023SJohn Marino {
817*e4b17023SJohn Marino   enum rtx_code code;
818*e4b17023SJohn Marino   const char *fmt;
819*e4b17023SJohn Marino   int i;
820*e4b17023SJohn Marino 
821*e4b17023SJohn Marino   if (REG_P (x) || MEM_P (x))
822*e4b17023SJohn Marino     {
823*e4b17023SJohn Marino       cselib_val *e = cselib_lookup (x, GET_MODE (x), 0, memmode);
824*e4b17023SJohn Marino 
825*e4b17023SJohn Marino       if (e)
826*e4b17023SJohn Marino 	x = e->val_rtx;
827*e4b17023SJohn Marino     }
828*e4b17023SJohn Marino 
829*e4b17023SJohn Marino   if (REG_P (y) || MEM_P (y))
830*e4b17023SJohn Marino     {
831*e4b17023SJohn Marino       cselib_val *e = cselib_lookup (y, GET_MODE (y), 0, memmode);
832*e4b17023SJohn Marino 
833*e4b17023SJohn Marino       if (e)
834*e4b17023SJohn Marino 	y = e->val_rtx;
835*e4b17023SJohn Marino     }
836*e4b17023SJohn Marino 
837*e4b17023SJohn Marino   if (x == y)
838*e4b17023SJohn Marino     return 1;
839*e4b17023SJohn Marino 
840*e4b17023SJohn Marino   if (GET_CODE (x) == VALUE)
841*e4b17023SJohn Marino     {
842*e4b17023SJohn Marino       cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (x));
843*e4b17023SJohn Marino       struct elt_loc_list *l;
844*e4b17023SJohn Marino 
845*e4b17023SJohn Marino       if (GET_CODE (y) == VALUE)
846*e4b17023SJohn Marino 	return e == canonical_cselib_val (CSELIB_VAL_PTR (y));
847*e4b17023SJohn Marino 
848*e4b17023SJohn Marino       for (l = e->locs; l; l = l->next)
849*e4b17023SJohn Marino 	{
850*e4b17023SJohn Marino 	  rtx t = l->loc;
851*e4b17023SJohn Marino 
852*e4b17023SJohn Marino 	  /* Avoid infinite recursion.  We know we have the canonical
853*e4b17023SJohn Marino 	     value, so we can just skip any values in the equivalence
854*e4b17023SJohn Marino 	     list.  */
855*e4b17023SJohn Marino 	  if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
856*e4b17023SJohn Marino 	    continue;
857*e4b17023SJohn Marino 	  else if (rtx_equal_for_cselib_1 (t, y, memmode))
858*e4b17023SJohn Marino 	    return 1;
859*e4b17023SJohn Marino 	}
860*e4b17023SJohn Marino 
861*e4b17023SJohn Marino       return 0;
862*e4b17023SJohn Marino     }
863*e4b17023SJohn Marino   else if (GET_CODE (y) == VALUE)
864*e4b17023SJohn Marino     {
865*e4b17023SJohn Marino       cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (y));
866*e4b17023SJohn Marino       struct elt_loc_list *l;
867*e4b17023SJohn Marino 
868*e4b17023SJohn Marino       for (l = e->locs; l; l = l->next)
869*e4b17023SJohn Marino 	{
870*e4b17023SJohn Marino 	  rtx t = l->loc;
871*e4b17023SJohn Marino 
872*e4b17023SJohn Marino 	  if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
873*e4b17023SJohn Marino 	    continue;
874*e4b17023SJohn Marino 	  else if (rtx_equal_for_cselib_1 (x, t, memmode))
875*e4b17023SJohn Marino 	    return 1;
876*e4b17023SJohn Marino 	}
877*e4b17023SJohn Marino 
878*e4b17023SJohn Marino       return 0;
879*e4b17023SJohn Marino     }
880*e4b17023SJohn Marino 
881*e4b17023SJohn Marino   if (GET_MODE (x) != GET_MODE (y))
882*e4b17023SJohn Marino     return 0;
883*e4b17023SJohn Marino 
884*e4b17023SJohn Marino   if (GET_CODE (x) != GET_CODE (y))
885*e4b17023SJohn Marino     {
886*e4b17023SJohn Marino       rtx xorig = x, yorig = y;
887*e4b17023SJohn Marino       rtx xoff = NULL, yoff = NULL;
888*e4b17023SJohn Marino 
889*e4b17023SJohn Marino       x = autoinc_split (x, &xoff, memmode);
890*e4b17023SJohn Marino       y = autoinc_split (y, &yoff, memmode);
891*e4b17023SJohn Marino 
892*e4b17023SJohn Marino       if (!xoff != !yoff)
893*e4b17023SJohn Marino 	return 0;
894*e4b17023SJohn Marino 
895*e4b17023SJohn Marino       if (xoff && !rtx_equal_for_cselib_1 (xoff, yoff, memmode))
896*e4b17023SJohn Marino 	return 0;
897*e4b17023SJohn Marino 
898*e4b17023SJohn Marino       /* Don't recurse if nothing changed.  */
899*e4b17023SJohn Marino       if (x != xorig || y != yorig)
900*e4b17023SJohn Marino 	return rtx_equal_for_cselib_1 (x, y, memmode);
901*e4b17023SJohn Marino 
902*e4b17023SJohn Marino       return 0;
903*e4b17023SJohn Marino     }
904*e4b17023SJohn Marino 
905*e4b17023SJohn Marino   /* These won't be handled correctly by the code below.  */
906*e4b17023SJohn Marino   switch (GET_CODE (x))
907*e4b17023SJohn Marino     {
908*e4b17023SJohn Marino     case CONST_DOUBLE:
909*e4b17023SJohn Marino     case CONST_FIXED:
910*e4b17023SJohn Marino     case DEBUG_EXPR:
911*e4b17023SJohn Marino       return 0;
912*e4b17023SJohn Marino 
913*e4b17023SJohn Marino     case DEBUG_IMPLICIT_PTR:
914*e4b17023SJohn Marino       return DEBUG_IMPLICIT_PTR_DECL (x)
915*e4b17023SJohn Marino 	     == DEBUG_IMPLICIT_PTR_DECL (y);
916*e4b17023SJohn Marino 
917*e4b17023SJohn Marino     case DEBUG_PARAMETER_REF:
918*e4b17023SJohn Marino       return DEBUG_PARAMETER_REF_DECL (x)
919*e4b17023SJohn Marino 	     == DEBUG_PARAMETER_REF_DECL (y);
920*e4b17023SJohn Marino 
921*e4b17023SJohn Marino     case ENTRY_VALUE:
922*e4b17023SJohn Marino       /* ENTRY_VALUEs are function invariant, it is thus undesirable to
923*e4b17023SJohn Marino 	 use rtx_equal_for_cselib_1 to compare the operands.  */
924*e4b17023SJohn Marino       return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
925*e4b17023SJohn Marino 
926*e4b17023SJohn Marino     case LABEL_REF:
927*e4b17023SJohn Marino       return XEXP (x, 0) == XEXP (y, 0);
928*e4b17023SJohn Marino 
929*e4b17023SJohn Marino     case MEM:
930*e4b17023SJohn Marino       /* We have to compare any autoinc operations in the addresses
931*e4b17023SJohn Marino 	 using this MEM's mode.  */
932*e4b17023SJohn Marino       return rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 0), GET_MODE (x));
933*e4b17023SJohn Marino 
934*e4b17023SJohn Marino     default:
935*e4b17023SJohn Marino       break;
936*e4b17023SJohn Marino     }
937*e4b17023SJohn Marino 
938*e4b17023SJohn Marino   code = GET_CODE (x);
939*e4b17023SJohn Marino   fmt = GET_RTX_FORMAT (code);
940*e4b17023SJohn Marino 
941*e4b17023SJohn Marino   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
942*e4b17023SJohn Marino     {
943*e4b17023SJohn Marino       int j;
944*e4b17023SJohn Marino 
945*e4b17023SJohn Marino       switch (fmt[i])
946*e4b17023SJohn Marino 	{
947*e4b17023SJohn Marino 	case 'w':
948*e4b17023SJohn Marino 	  if (XWINT (x, i) != XWINT (y, i))
949*e4b17023SJohn Marino 	    return 0;
950*e4b17023SJohn Marino 	  break;
951*e4b17023SJohn Marino 
952*e4b17023SJohn Marino 	case 'n':
953*e4b17023SJohn Marino 	case 'i':
954*e4b17023SJohn Marino 	  if (XINT (x, i) != XINT (y, i))
955*e4b17023SJohn Marino 	    return 0;
956*e4b17023SJohn Marino 	  break;
957*e4b17023SJohn Marino 
958*e4b17023SJohn Marino 	case 'V':
959*e4b17023SJohn Marino 	case 'E':
960*e4b17023SJohn Marino 	  /* Two vectors must have the same length.  */
961*e4b17023SJohn Marino 	  if (XVECLEN (x, i) != XVECLEN (y, i))
962*e4b17023SJohn Marino 	    return 0;
963*e4b17023SJohn Marino 
964*e4b17023SJohn Marino 	  /* And the corresponding elements must match.  */
965*e4b17023SJohn Marino 	  for (j = 0; j < XVECLEN (x, i); j++)
966*e4b17023SJohn Marino 	    if (! rtx_equal_for_cselib_1 (XVECEXP (x, i, j),
967*e4b17023SJohn Marino 					  XVECEXP (y, i, j), memmode))
968*e4b17023SJohn Marino 	      return 0;
969*e4b17023SJohn Marino 	  break;
970*e4b17023SJohn Marino 
971*e4b17023SJohn Marino 	case 'e':
972*e4b17023SJohn Marino 	  if (i == 1
973*e4b17023SJohn Marino 	      && targetm.commutative_p (x, UNKNOWN)
974*e4b17023SJohn Marino 	      && rtx_equal_for_cselib_1 (XEXP (x, 1), XEXP (y, 0), memmode)
975*e4b17023SJohn Marino 	      && rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 1), memmode))
976*e4b17023SJohn Marino 	    return 1;
977*e4b17023SJohn Marino 	  if (! rtx_equal_for_cselib_1 (XEXP (x, i), XEXP (y, i), memmode))
978*e4b17023SJohn Marino 	    return 0;
979*e4b17023SJohn Marino 	  break;
980*e4b17023SJohn Marino 
981*e4b17023SJohn Marino 	case 'S':
982*e4b17023SJohn Marino 	case 's':
983*e4b17023SJohn Marino 	  if (strcmp (XSTR (x, i), XSTR (y, i)))
984*e4b17023SJohn Marino 	    return 0;
985*e4b17023SJohn Marino 	  break;
986*e4b17023SJohn Marino 
987*e4b17023SJohn Marino 	case 'u':
988*e4b17023SJohn Marino 	  /* These are just backpointers, so they don't matter.  */
989*e4b17023SJohn Marino 	  break;
990*e4b17023SJohn Marino 
991*e4b17023SJohn Marino 	case '0':
992*e4b17023SJohn Marino 	case 't':
993*e4b17023SJohn Marino 	  break;
994*e4b17023SJohn Marino 
995*e4b17023SJohn Marino 	  /* It is believed that rtx's at this level will never
996*e4b17023SJohn Marino 	     contain anything but integers and other rtx's,
997*e4b17023SJohn Marino 	     except for within LABEL_REFs and SYMBOL_REFs.  */
998*e4b17023SJohn Marino 	default:
999*e4b17023SJohn Marino 	  gcc_unreachable ();
1000*e4b17023SJohn Marino 	}
1001*e4b17023SJohn Marino     }
1002*e4b17023SJohn Marino   return 1;
1003*e4b17023SJohn Marino }
1004*e4b17023SJohn Marino 
1005*e4b17023SJohn Marino /* We need to pass down the mode of constants through the hash table
1006*e4b17023SJohn Marino    functions.  For that purpose, wrap them in a CONST of the appropriate
1007*e4b17023SJohn Marino    mode.  */
1008*e4b17023SJohn Marino static rtx
wrap_constant(enum machine_mode mode,rtx x)1009*e4b17023SJohn Marino wrap_constant (enum machine_mode mode, rtx x)
1010*e4b17023SJohn Marino {
1011*e4b17023SJohn Marino   if (!CONST_INT_P (x) && GET_CODE (x) != CONST_FIXED
1012*e4b17023SJohn Marino       && (GET_CODE (x) != CONST_DOUBLE || GET_MODE (x) != VOIDmode))
1013*e4b17023SJohn Marino     return x;
1014*e4b17023SJohn Marino   gcc_assert (mode != VOIDmode);
1015*e4b17023SJohn Marino   return gen_rtx_CONST (mode, x);
1016*e4b17023SJohn Marino }
1017*e4b17023SJohn Marino 
1018*e4b17023SJohn Marino /* Hash an rtx.  Return 0 if we couldn't hash the rtx.
1019*e4b17023SJohn Marino    For registers and memory locations, we look up their cselib_val structure
1020*e4b17023SJohn Marino    and return its VALUE element.
1021*e4b17023SJohn Marino    Possible reasons for return 0 are: the object is volatile, or we couldn't
1022*e4b17023SJohn Marino    find a register or memory location in the table and CREATE is zero.  If
1023*e4b17023SJohn Marino    CREATE is nonzero, table elts are created for regs and mem.
1024*e4b17023SJohn Marino    N.B. this hash function returns the same hash value for RTXes that
1025*e4b17023SJohn Marino    differ only in the order of operands, thus it is suitable for comparisons
1026*e4b17023SJohn Marino    that take commutativity into account.
1027*e4b17023SJohn Marino    If we wanted to also support associative rules, we'd have to use a different
1028*e4b17023SJohn Marino    strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
1029*e4b17023SJohn Marino    MEMMODE indicates the mode of an enclosing MEM, and it's only
1030*e4b17023SJohn Marino    used to compute autoinc values.
1031*e4b17023SJohn Marino    We used to have a MODE argument for hashing for CONST_INTs, but that
1032*e4b17023SJohn Marino    didn't make sense, since it caused spurious hash differences between
1033*e4b17023SJohn Marino     (set (reg:SI 1) (const_int))
1034*e4b17023SJohn Marino     (plus:SI (reg:SI 2) (reg:SI 1))
1035*e4b17023SJohn Marino    and
1036*e4b17023SJohn Marino     (plus:SI (reg:SI 2) (const_int))
1037*e4b17023SJohn Marino    If the mode is important in any context, it must be checked specifically
1038*e4b17023SJohn Marino    in a comparison anyway, since relying on hash differences is unsafe.  */
1039*e4b17023SJohn Marino 
1040*e4b17023SJohn Marino static unsigned int
cselib_hash_rtx(rtx x,int create,enum machine_mode memmode)1041*e4b17023SJohn Marino cselib_hash_rtx (rtx x, int create, enum machine_mode memmode)
1042*e4b17023SJohn Marino {
1043*e4b17023SJohn Marino   cselib_val *e;
1044*e4b17023SJohn Marino   int i, j;
1045*e4b17023SJohn Marino   enum rtx_code code;
1046*e4b17023SJohn Marino   const char *fmt;
1047*e4b17023SJohn Marino   unsigned int hash = 0;
1048*e4b17023SJohn Marino 
1049*e4b17023SJohn Marino   code = GET_CODE (x);
1050*e4b17023SJohn Marino   hash += (unsigned) code + (unsigned) GET_MODE (x);
1051*e4b17023SJohn Marino 
1052*e4b17023SJohn Marino   switch (code)
1053*e4b17023SJohn Marino     {
1054*e4b17023SJohn Marino     case VALUE:
1055*e4b17023SJohn Marino       e = CSELIB_VAL_PTR (x);
1056*e4b17023SJohn Marino       return e->hash;
1057*e4b17023SJohn Marino 
1058*e4b17023SJohn Marino     case MEM:
1059*e4b17023SJohn Marino     case REG:
1060*e4b17023SJohn Marino       e = cselib_lookup (x, GET_MODE (x), create, memmode);
1061*e4b17023SJohn Marino       if (! e)
1062*e4b17023SJohn Marino 	return 0;
1063*e4b17023SJohn Marino 
1064*e4b17023SJohn Marino       return e->hash;
1065*e4b17023SJohn Marino 
1066*e4b17023SJohn Marino     case DEBUG_EXPR:
1067*e4b17023SJohn Marino       hash += ((unsigned) DEBUG_EXPR << 7)
1068*e4b17023SJohn Marino 	      + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x));
1069*e4b17023SJohn Marino       return hash ? hash : (unsigned int) DEBUG_EXPR;
1070*e4b17023SJohn Marino 
1071*e4b17023SJohn Marino     case DEBUG_IMPLICIT_PTR:
1072*e4b17023SJohn Marino       hash += ((unsigned) DEBUG_IMPLICIT_PTR << 7)
1073*e4b17023SJohn Marino 	      + DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x));
1074*e4b17023SJohn Marino       return hash ? hash : (unsigned int) DEBUG_IMPLICIT_PTR;
1075*e4b17023SJohn Marino 
1076*e4b17023SJohn Marino     case DEBUG_PARAMETER_REF:
1077*e4b17023SJohn Marino       hash += ((unsigned) DEBUG_PARAMETER_REF << 7)
1078*e4b17023SJohn Marino 	      + DECL_UID (DEBUG_PARAMETER_REF_DECL (x));
1079*e4b17023SJohn Marino       return hash ? hash : (unsigned int) DEBUG_PARAMETER_REF;
1080*e4b17023SJohn Marino 
1081*e4b17023SJohn Marino     case ENTRY_VALUE:
1082*e4b17023SJohn Marino       /* ENTRY_VALUEs are function invariant, thus try to avoid
1083*e4b17023SJohn Marino 	 recursing on argument if ENTRY_VALUE is one of the
1084*e4b17023SJohn Marino 	 forms emitted by expand_debug_expr, otherwise
1085*e4b17023SJohn Marino 	 ENTRY_VALUE hash would depend on the current value
1086*e4b17023SJohn Marino 	 in some register or memory.  */
1087*e4b17023SJohn Marino       if (REG_P (ENTRY_VALUE_EXP (x)))
1088*e4b17023SJohn Marino 	hash += (unsigned int) REG
1089*e4b17023SJohn Marino 		+ (unsigned int) GET_MODE (ENTRY_VALUE_EXP (x))
1090*e4b17023SJohn Marino 		+ (unsigned int) REGNO (ENTRY_VALUE_EXP (x));
1091*e4b17023SJohn Marino       else if (MEM_P (ENTRY_VALUE_EXP (x))
1092*e4b17023SJohn Marino 	       && REG_P (XEXP (ENTRY_VALUE_EXP (x), 0)))
1093*e4b17023SJohn Marino 	hash += (unsigned int) MEM
1094*e4b17023SJohn Marino 		+ (unsigned int) GET_MODE (XEXP (ENTRY_VALUE_EXP (x), 0))
1095*e4b17023SJohn Marino 		+ (unsigned int) REGNO (XEXP (ENTRY_VALUE_EXP (x), 0));
1096*e4b17023SJohn Marino       else
1097*e4b17023SJohn Marino 	hash += cselib_hash_rtx (ENTRY_VALUE_EXP (x), create, memmode);
1098*e4b17023SJohn Marino       return hash ? hash : (unsigned int) ENTRY_VALUE;
1099*e4b17023SJohn Marino 
1100*e4b17023SJohn Marino     case CONST_INT:
1101*e4b17023SJohn Marino       hash += ((unsigned) CONST_INT << 7) + INTVAL (x);
1102*e4b17023SJohn Marino       return hash ? hash : (unsigned int) CONST_INT;
1103*e4b17023SJohn Marino 
1104*e4b17023SJohn Marino     case CONST_DOUBLE:
1105*e4b17023SJohn Marino       /* This is like the general case, except that it only counts
1106*e4b17023SJohn Marino 	 the integers representing the constant.  */
1107*e4b17023SJohn Marino       hash += (unsigned) code + (unsigned) GET_MODE (x);
1108*e4b17023SJohn Marino       if (GET_MODE (x) != VOIDmode)
1109*e4b17023SJohn Marino 	hash += real_hash (CONST_DOUBLE_REAL_VALUE (x));
1110*e4b17023SJohn Marino       else
1111*e4b17023SJohn Marino 	hash += ((unsigned) CONST_DOUBLE_LOW (x)
1112*e4b17023SJohn Marino 		 + (unsigned) CONST_DOUBLE_HIGH (x));
1113*e4b17023SJohn Marino       return hash ? hash : (unsigned int) CONST_DOUBLE;
1114*e4b17023SJohn Marino 
1115*e4b17023SJohn Marino     case CONST_FIXED:
1116*e4b17023SJohn Marino       hash += (unsigned int) code + (unsigned int) GET_MODE (x);
1117*e4b17023SJohn Marino       hash += fixed_hash (CONST_FIXED_VALUE (x));
1118*e4b17023SJohn Marino       return hash ? hash : (unsigned int) CONST_FIXED;
1119*e4b17023SJohn Marino 
1120*e4b17023SJohn Marino     case CONST_VECTOR:
1121*e4b17023SJohn Marino       {
1122*e4b17023SJohn Marino 	int units;
1123*e4b17023SJohn Marino 	rtx elt;
1124*e4b17023SJohn Marino 
1125*e4b17023SJohn Marino 	units = CONST_VECTOR_NUNITS (x);
1126*e4b17023SJohn Marino 
1127*e4b17023SJohn Marino 	for (i = 0; i < units; ++i)
1128*e4b17023SJohn Marino 	  {
1129*e4b17023SJohn Marino 	    elt = CONST_VECTOR_ELT (x, i);
1130*e4b17023SJohn Marino 	    hash += cselib_hash_rtx (elt, 0, memmode);
1131*e4b17023SJohn Marino 	  }
1132*e4b17023SJohn Marino 
1133*e4b17023SJohn Marino 	return hash;
1134*e4b17023SJohn Marino       }
1135*e4b17023SJohn Marino 
1136*e4b17023SJohn Marino       /* Assume there is only one rtx object for any given label.  */
1137*e4b17023SJohn Marino     case LABEL_REF:
1138*e4b17023SJohn Marino       /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1139*e4b17023SJohn Marino 	 differences and differences between each stage's debugging dumps.  */
1140*e4b17023SJohn Marino       hash += (((unsigned int) LABEL_REF << 7)
1141*e4b17023SJohn Marino 	       + CODE_LABEL_NUMBER (XEXP (x, 0)));
1142*e4b17023SJohn Marino       return hash ? hash : (unsigned int) LABEL_REF;
1143*e4b17023SJohn Marino 
1144*e4b17023SJohn Marino     case SYMBOL_REF:
1145*e4b17023SJohn Marino       {
1146*e4b17023SJohn Marino 	/* Don't hash on the symbol's address to avoid bootstrap differences.
1147*e4b17023SJohn Marino 	   Different hash values may cause expressions to be recorded in
1148*e4b17023SJohn Marino 	   different orders and thus different registers to be used in the
1149*e4b17023SJohn Marino 	   final assembler.  This also avoids differences in the dump files
1150*e4b17023SJohn Marino 	   between various stages.  */
1151*e4b17023SJohn Marino 	unsigned int h = 0;
1152*e4b17023SJohn Marino 	const unsigned char *p = (const unsigned char *) XSTR (x, 0);
1153*e4b17023SJohn Marino 
1154*e4b17023SJohn Marino 	while (*p)
1155*e4b17023SJohn Marino 	  h += (h << 7) + *p++; /* ??? revisit */
1156*e4b17023SJohn Marino 
1157*e4b17023SJohn Marino 	hash += ((unsigned int) SYMBOL_REF << 7) + h;
1158*e4b17023SJohn Marino 	return hash ? hash : (unsigned int) SYMBOL_REF;
1159*e4b17023SJohn Marino       }
1160*e4b17023SJohn Marino 
1161*e4b17023SJohn Marino     case PRE_DEC:
1162*e4b17023SJohn Marino     case PRE_INC:
1163*e4b17023SJohn Marino       /* We can't compute these without knowing the MEM mode.  */
1164*e4b17023SJohn Marino       gcc_assert (memmode != VOIDmode);
1165*e4b17023SJohn Marino       i = GET_MODE_SIZE (memmode);
1166*e4b17023SJohn Marino       if (code == PRE_DEC)
1167*e4b17023SJohn Marino 	i = -i;
1168*e4b17023SJohn Marino       /* Adjust the hash so that (mem:MEMMODE (pre_* (reg))) hashes
1169*e4b17023SJohn Marino 	 like (mem:MEMMODE (plus (reg) (const_int I))).  */
1170*e4b17023SJohn Marino       hash += (unsigned) PLUS - (unsigned)code
1171*e4b17023SJohn Marino 	+ cselib_hash_rtx (XEXP (x, 0), create, memmode)
1172*e4b17023SJohn Marino 	+ cselib_hash_rtx (GEN_INT (i), create, memmode);
1173*e4b17023SJohn Marino       return hash ? hash : 1 + (unsigned) PLUS;
1174*e4b17023SJohn Marino 
1175*e4b17023SJohn Marino     case PRE_MODIFY:
1176*e4b17023SJohn Marino       gcc_assert (memmode != VOIDmode);
1177*e4b17023SJohn Marino       return cselib_hash_rtx (XEXP (x, 1), create, memmode);
1178*e4b17023SJohn Marino 
1179*e4b17023SJohn Marino     case POST_DEC:
1180*e4b17023SJohn Marino     case POST_INC:
1181*e4b17023SJohn Marino     case POST_MODIFY:
1182*e4b17023SJohn Marino       gcc_assert (memmode != VOIDmode);
1183*e4b17023SJohn Marino       return cselib_hash_rtx (XEXP (x, 0), create, memmode);
1184*e4b17023SJohn Marino 
1185*e4b17023SJohn Marino     case PC:
1186*e4b17023SJohn Marino     case CC0:
1187*e4b17023SJohn Marino     case CALL:
1188*e4b17023SJohn Marino     case UNSPEC_VOLATILE:
1189*e4b17023SJohn Marino       return 0;
1190*e4b17023SJohn Marino 
1191*e4b17023SJohn Marino     case ASM_OPERANDS:
1192*e4b17023SJohn Marino       if (MEM_VOLATILE_P (x))
1193*e4b17023SJohn Marino 	return 0;
1194*e4b17023SJohn Marino 
1195*e4b17023SJohn Marino       break;
1196*e4b17023SJohn Marino 
1197*e4b17023SJohn Marino     default:
1198*e4b17023SJohn Marino       break;
1199*e4b17023SJohn Marino     }
1200*e4b17023SJohn Marino 
1201*e4b17023SJohn Marino   i = GET_RTX_LENGTH (code) - 1;
1202*e4b17023SJohn Marino   fmt = GET_RTX_FORMAT (code);
1203*e4b17023SJohn Marino   for (; i >= 0; i--)
1204*e4b17023SJohn Marino     {
1205*e4b17023SJohn Marino       switch (fmt[i])
1206*e4b17023SJohn Marino 	{
1207*e4b17023SJohn Marino 	case 'e':
1208*e4b17023SJohn Marino 	  {
1209*e4b17023SJohn Marino 	    rtx tem = XEXP (x, i);
1210*e4b17023SJohn Marino 	    unsigned int tem_hash = cselib_hash_rtx (tem, create, memmode);
1211*e4b17023SJohn Marino 
1212*e4b17023SJohn Marino 	    if (tem_hash == 0)
1213*e4b17023SJohn Marino 	      return 0;
1214*e4b17023SJohn Marino 
1215*e4b17023SJohn Marino 	    hash += tem_hash;
1216*e4b17023SJohn Marino 	  }
1217*e4b17023SJohn Marino 	  break;
1218*e4b17023SJohn Marino 	case 'E':
1219*e4b17023SJohn Marino 	  for (j = 0; j < XVECLEN (x, i); j++)
1220*e4b17023SJohn Marino 	    {
1221*e4b17023SJohn Marino 	      unsigned int tem_hash
1222*e4b17023SJohn Marino 		= cselib_hash_rtx (XVECEXP (x, i, j), create, memmode);
1223*e4b17023SJohn Marino 
1224*e4b17023SJohn Marino 	      if (tem_hash == 0)
1225*e4b17023SJohn Marino 		return 0;
1226*e4b17023SJohn Marino 
1227*e4b17023SJohn Marino 	      hash += tem_hash;
1228*e4b17023SJohn Marino 	    }
1229*e4b17023SJohn Marino 	  break;
1230*e4b17023SJohn Marino 
1231*e4b17023SJohn Marino 	case 's':
1232*e4b17023SJohn Marino 	  {
1233*e4b17023SJohn Marino 	    const unsigned char *p = (const unsigned char *) XSTR (x, i);
1234*e4b17023SJohn Marino 
1235*e4b17023SJohn Marino 	    if (p)
1236*e4b17023SJohn Marino 	      while (*p)
1237*e4b17023SJohn Marino 		hash += *p++;
1238*e4b17023SJohn Marino 	    break;
1239*e4b17023SJohn Marino 	  }
1240*e4b17023SJohn Marino 
1241*e4b17023SJohn Marino 	case 'i':
1242*e4b17023SJohn Marino 	  hash += XINT (x, i);
1243*e4b17023SJohn Marino 	  break;
1244*e4b17023SJohn Marino 
1245*e4b17023SJohn Marino 	case '0':
1246*e4b17023SJohn Marino 	case 't':
1247*e4b17023SJohn Marino 	  /* unused */
1248*e4b17023SJohn Marino 	  break;
1249*e4b17023SJohn Marino 
1250*e4b17023SJohn Marino 	default:
1251*e4b17023SJohn Marino 	  gcc_unreachable ();
1252*e4b17023SJohn Marino 	}
1253*e4b17023SJohn Marino     }
1254*e4b17023SJohn Marino 
1255*e4b17023SJohn Marino   return hash ? hash : 1 + (unsigned int) GET_CODE (x);
1256*e4b17023SJohn Marino }
1257*e4b17023SJohn Marino 
1258*e4b17023SJohn Marino /* Create a new value structure for VALUE and initialize it.  The mode of the
1259*e4b17023SJohn Marino    value is MODE.  */
1260*e4b17023SJohn Marino 
1261*e4b17023SJohn Marino static inline cselib_val *
new_cselib_val(unsigned int hash,enum machine_mode mode,rtx x)1262*e4b17023SJohn Marino new_cselib_val (unsigned int hash, enum machine_mode mode, rtx x)
1263*e4b17023SJohn Marino {
1264*e4b17023SJohn Marino   cselib_val *e = (cselib_val *) pool_alloc (cselib_val_pool);
1265*e4b17023SJohn Marino 
1266*e4b17023SJohn Marino   gcc_assert (hash);
1267*e4b17023SJohn Marino   gcc_assert (next_uid);
1268*e4b17023SJohn Marino 
1269*e4b17023SJohn Marino   e->hash = hash;
1270*e4b17023SJohn Marino   e->uid = next_uid++;
1271*e4b17023SJohn Marino   /* We use an alloc pool to allocate this RTL construct because it
1272*e4b17023SJohn Marino      accounts for about 8% of the overall memory usage.  We know
1273*e4b17023SJohn Marino      precisely when we can have VALUE RTXen (when cselib is active)
1274*e4b17023SJohn Marino      so we don't need to put them in garbage collected memory.
1275*e4b17023SJohn Marino      ??? Why should a VALUE be an RTX in the first place?  */
1276*e4b17023SJohn Marino   e->val_rtx = (rtx) pool_alloc (value_pool);
1277*e4b17023SJohn Marino   memset (e->val_rtx, 0, RTX_HDR_SIZE);
1278*e4b17023SJohn Marino   PUT_CODE (e->val_rtx, VALUE);
1279*e4b17023SJohn Marino   PUT_MODE (e->val_rtx, mode);
1280*e4b17023SJohn Marino   CSELIB_VAL_PTR (e->val_rtx) = e;
1281*e4b17023SJohn Marino   e->addr_list = 0;
1282*e4b17023SJohn Marino   e->locs = 0;
1283*e4b17023SJohn Marino   e->next_containing_mem = 0;
1284*e4b17023SJohn Marino 
1285*e4b17023SJohn Marino   if (dump_file && (dump_flags & TDF_CSELIB))
1286*e4b17023SJohn Marino     {
1287*e4b17023SJohn Marino       fprintf (dump_file, "cselib value %u:%u ", e->uid, hash);
1288*e4b17023SJohn Marino       if (flag_dump_noaddr || flag_dump_unnumbered)
1289*e4b17023SJohn Marino 	fputs ("# ", dump_file);
1290*e4b17023SJohn Marino       else
1291*e4b17023SJohn Marino 	fprintf (dump_file, "%p ", (void*)e);
1292*e4b17023SJohn Marino       print_rtl_single (dump_file, x);
1293*e4b17023SJohn Marino       fputc ('\n', dump_file);
1294*e4b17023SJohn Marino     }
1295*e4b17023SJohn Marino 
1296*e4b17023SJohn Marino   return e;
1297*e4b17023SJohn Marino }
1298*e4b17023SJohn Marino 
1299*e4b17023SJohn Marino /* ADDR_ELT is a value that is used as address.  MEM_ELT is the value that
1300*e4b17023SJohn Marino    contains the data at this address.  X is a MEM that represents the
1301*e4b17023SJohn Marino    value.  Update the two value structures to represent this situation.  */
1302*e4b17023SJohn Marino 
1303*e4b17023SJohn Marino static void
add_mem_for_addr(cselib_val * addr_elt,cselib_val * mem_elt,rtx x)1304*e4b17023SJohn Marino add_mem_for_addr (cselib_val *addr_elt, cselib_val *mem_elt, rtx x)
1305*e4b17023SJohn Marino {
1306*e4b17023SJohn Marino   struct elt_loc_list *l;
1307*e4b17023SJohn Marino 
1308*e4b17023SJohn Marino   addr_elt = canonical_cselib_val (addr_elt);
1309*e4b17023SJohn Marino   mem_elt = canonical_cselib_val (mem_elt);
1310*e4b17023SJohn Marino 
1311*e4b17023SJohn Marino   /* Avoid duplicates.  */
1312*e4b17023SJohn Marino   for (l = mem_elt->locs; l; l = l->next)
1313*e4b17023SJohn Marino     if (MEM_P (l->loc)
1314*e4b17023SJohn Marino 	&& CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt)
1315*e4b17023SJohn Marino       {
1316*e4b17023SJohn Marino 	promote_debug_loc (l);
1317*e4b17023SJohn Marino 	return;
1318*e4b17023SJohn Marino       }
1319*e4b17023SJohn Marino 
1320*e4b17023SJohn Marino   addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
1321*e4b17023SJohn Marino   new_elt_loc_list (mem_elt,
1322*e4b17023SJohn Marino 		    replace_equiv_address_nv (x, addr_elt->val_rtx));
1323*e4b17023SJohn Marino   if (mem_elt->next_containing_mem == NULL)
1324*e4b17023SJohn Marino     {
1325*e4b17023SJohn Marino       mem_elt->next_containing_mem = first_containing_mem;
1326*e4b17023SJohn Marino       first_containing_mem = mem_elt;
1327*e4b17023SJohn Marino     }
1328*e4b17023SJohn Marino }
1329*e4b17023SJohn Marino 
1330*e4b17023SJohn Marino /* Subroutine of cselib_lookup.  Return a value for X, which is a MEM rtx.
1331*e4b17023SJohn Marino    If CREATE, make a new one if we haven't seen it before.  */
1332*e4b17023SJohn Marino 
1333*e4b17023SJohn Marino static cselib_val *
cselib_lookup_mem(rtx x,int create)1334*e4b17023SJohn Marino cselib_lookup_mem (rtx x, int create)
1335*e4b17023SJohn Marino {
1336*e4b17023SJohn Marino   enum machine_mode mode = GET_MODE (x);
1337*e4b17023SJohn Marino   enum machine_mode addr_mode;
1338*e4b17023SJohn Marino   void **slot;
1339*e4b17023SJohn Marino   cselib_val *addr;
1340*e4b17023SJohn Marino   cselib_val *mem_elt;
1341*e4b17023SJohn Marino   struct elt_list *l;
1342*e4b17023SJohn Marino 
1343*e4b17023SJohn Marino   if (MEM_VOLATILE_P (x) || mode == BLKmode
1344*e4b17023SJohn Marino       || !cselib_record_memory
1345*e4b17023SJohn Marino       || (FLOAT_MODE_P (mode) && flag_float_store))
1346*e4b17023SJohn Marino     return 0;
1347*e4b17023SJohn Marino 
1348*e4b17023SJohn Marino   addr_mode = GET_MODE (XEXP (x, 0));
1349*e4b17023SJohn Marino   if (addr_mode == VOIDmode)
1350*e4b17023SJohn Marino     addr_mode = Pmode;
1351*e4b17023SJohn Marino 
1352*e4b17023SJohn Marino   /* Look up the value for the address.  */
1353*e4b17023SJohn Marino   addr = cselib_lookup (XEXP (x, 0), addr_mode, create, mode);
1354*e4b17023SJohn Marino   if (! addr)
1355*e4b17023SJohn Marino     return 0;
1356*e4b17023SJohn Marino 
1357*e4b17023SJohn Marino   addr = canonical_cselib_val (addr);
1358*e4b17023SJohn Marino   /* Find a value that describes a value of our mode at that address.  */
1359*e4b17023SJohn Marino   for (l = addr->addr_list; l; l = l->next)
1360*e4b17023SJohn Marino     if (GET_MODE (l->elt->val_rtx) == mode)
1361*e4b17023SJohn Marino       {
1362*e4b17023SJohn Marino 	promote_debug_loc (l->elt->locs);
1363*e4b17023SJohn Marino 	return l->elt;
1364*e4b17023SJohn Marino       }
1365*e4b17023SJohn Marino 
1366*e4b17023SJohn Marino   if (! create)
1367*e4b17023SJohn Marino     return 0;
1368*e4b17023SJohn Marino 
1369*e4b17023SJohn Marino   mem_elt = new_cselib_val (next_uid, mode, x);
1370*e4b17023SJohn Marino   add_mem_for_addr (addr, mem_elt, x);
1371*e4b17023SJohn Marino   slot = cselib_find_slot (wrap_constant (mode, x), mem_elt->hash,
1372*e4b17023SJohn Marino 			   INSERT, mode);
1373*e4b17023SJohn Marino   *slot = mem_elt;
1374*e4b17023SJohn Marino   return mem_elt;
1375*e4b17023SJohn Marino }
1376*e4b17023SJohn Marino 
1377*e4b17023SJohn Marino /* Search thru the possible substitutions in P.  We prefer a non reg
1378*e4b17023SJohn Marino    substitution because this allows us to expand the tree further.  If
1379*e4b17023SJohn Marino    we find, just a reg, take the lowest regno.  There may be several
1380*e4b17023SJohn Marino    non-reg results, we just take the first one because they will all
1381*e4b17023SJohn Marino    expand to the same place.  */
1382*e4b17023SJohn Marino 
1383*e4b17023SJohn Marino static rtx
expand_loc(struct elt_loc_list * p,struct expand_value_data * evd,int max_depth)1384*e4b17023SJohn Marino expand_loc (struct elt_loc_list *p, struct expand_value_data *evd,
1385*e4b17023SJohn Marino 	    int max_depth)
1386*e4b17023SJohn Marino {
1387*e4b17023SJohn Marino   rtx reg_result = NULL;
1388*e4b17023SJohn Marino   unsigned int regno = UINT_MAX;
1389*e4b17023SJohn Marino   struct elt_loc_list *p_in = p;
1390*e4b17023SJohn Marino 
1391*e4b17023SJohn Marino   for (; p; p = p->next)
1392*e4b17023SJohn Marino     {
1393*e4b17023SJohn Marino       /* Return these right away to avoid returning stack pointer based
1394*e4b17023SJohn Marino 	 expressions for frame pointer and vice versa, which is something
1395*e4b17023SJohn Marino 	 that would confuse DSE.  See the comment in cselib_expand_value_rtx_1
1396*e4b17023SJohn Marino 	 for more details.  */
1397*e4b17023SJohn Marino       if (REG_P (p->loc)
1398*e4b17023SJohn Marino 	  && (REGNO (p->loc) == STACK_POINTER_REGNUM
1399*e4b17023SJohn Marino 	      || REGNO (p->loc) == FRAME_POINTER_REGNUM
1400*e4b17023SJohn Marino 	      || REGNO (p->loc) == HARD_FRAME_POINTER_REGNUM
1401*e4b17023SJohn Marino 	      || REGNO (p->loc) == cfa_base_preserved_regno))
1402*e4b17023SJohn Marino 	return p->loc;
1403*e4b17023SJohn Marino       /* Avoid infinite recursion trying to expand a reg into a
1404*e4b17023SJohn Marino 	 the same reg.  */
1405*e4b17023SJohn Marino       if ((REG_P (p->loc))
1406*e4b17023SJohn Marino 	  && (REGNO (p->loc) < regno)
1407*e4b17023SJohn Marino 	  && !bitmap_bit_p (evd->regs_active, REGNO (p->loc)))
1408*e4b17023SJohn Marino 	{
1409*e4b17023SJohn Marino 	  reg_result = p->loc;
1410*e4b17023SJohn Marino 	  regno = REGNO (p->loc);
1411*e4b17023SJohn Marino 	}
1412*e4b17023SJohn Marino       /* Avoid infinite recursion and do not try to expand the
1413*e4b17023SJohn Marino 	 value.  */
1414*e4b17023SJohn Marino       else if (GET_CODE (p->loc) == VALUE
1415*e4b17023SJohn Marino 	       && CSELIB_VAL_PTR (p->loc)->locs == p_in)
1416*e4b17023SJohn Marino 	continue;
1417*e4b17023SJohn Marino       else if (!REG_P (p->loc))
1418*e4b17023SJohn Marino 	{
1419*e4b17023SJohn Marino 	  rtx result, note;
1420*e4b17023SJohn Marino 	  if (dump_file && (dump_flags & TDF_CSELIB))
1421*e4b17023SJohn Marino 	    {
1422*e4b17023SJohn Marino 	      print_inline_rtx (dump_file, p->loc, 0);
1423*e4b17023SJohn Marino 	      fprintf (dump_file, "\n");
1424*e4b17023SJohn Marino 	    }
1425*e4b17023SJohn Marino 	  if (GET_CODE (p->loc) == LO_SUM
1426*e4b17023SJohn Marino 	      && GET_CODE (XEXP (p->loc, 1)) == SYMBOL_REF
1427*e4b17023SJohn Marino 	      && p->setting_insn
1428*e4b17023SJohn Marino 	      && (note = find_reg_note (p->setting_insn, REG_EQUAL, NULL_RTX))
1429*e4b17023SJohn Marino 	      && XEXP (note, 0) == XEXP (p->loc, 1))
1430*e4b17023SJohn Marino 	    return XEXP (p->loc, 1);
1431*e4b17023SJohn Marino 	  result = cselib_expand_value_rtx_1 (p->loc, evd, max_depth - 1);
1432*e4b17023SJohn Marino 	  if (result)
1433*e4b17023SJohn Marino 	    return result;
1434*e4b17023SJohn Marino 	}
1435*e4b17023SJohn Marino 
1436*e4b17023SJohn Marino     }
1437*e4b17023SJohn Marino 
1438*e4b17023SJohn Marino   if (regno != UINT_MAX)
1439*e4b17023SJohn Marino     {
1440*e4b17023SJohn Marino       rtx result;
1441*e4b17023SJohn Marino       if (dump_file && (dump_flags & TDF_CSELIB))
1442*e4b17023SJohn Marino 	fprintf (dump_file, "r%d\n", regno);
1443*e4b17023SJohn Marino 
1444*e4b17023SJohn Marino       result = cselib_expand_value_rtx_1 (reg_result, evd, max_depth - 1);
1445*e4b17023SJohn Marino       if (result)
1446*e4b17023SJohn Marino 	return result;
1447*e4b17023SJohn Marino     }
1448*e4b17023SJohn Marino 
1449*e4b17023SJohn Marino   if (dump_file && (dump_flags & TDF_CSELIB))
1450*e4b17023SJohn Marino     {
1451*e4b17023SJohn Marino       if (reg_result)
1452*e4b17023SJohn Marino 	{
1453*e4b17023SJohn Marino 	  print_inline_rtx (dump_file, reg_result, 0);
1454*e4b17023SJohn Marino 	  fprintf (dump_file, "\n");
1455*e4b17023SJohn Marino 	}
1456*e4b17023SJohn Marino       else
1457*e4b17023SJohn Marino 	fprintf (dump_file, "NULL\n");
1458*e4b17023SJohn Marino     }
1459*e4b17023SJohn Marino   return reg_result;
1460*e4b17023SJohn Marino }
1461*e4b17023SJohn Marino 
1462*e4b17023SJohn Marino 
1463*e4b17023SJohn Marino /* Forward substitute and expand an expression out to its roots.
1464*e4b17023SJohn Marino    This is the opposite of common subexpression.  Because local value
1465*e4b17023SJohn Marino    numbering is such a weak optimization, the expanded expression is
1466*e4b17023SJohn Marino    pretty much unique (not from a pointer equals point of view but
1467*e4b17023SJohn Marino    from a tree shape point of view.
1468*e4b17023SJohn Marino 
1469*e4b17023SJohn Marino    This function returns NULL if the expansion fails.  The expansion
1470*e4b17023SJohn Marino    will fail if there is no value number for one of the operands or if
1471*e4b17023SJohn Marino    one of the operands has been overwritten between the current insn
1472*e4b17023SJohn Marino    and the beginning of the basic block.  For instance x has no
1473*e4b17023SJohn Marino    expansion in:
1474*e4b17023SJohn Marino 
1475*e4b17023SJohn Marino    r1 <- r1 + 3
1476*e4b17023SJohn Marino    x <- r1 + 8
1477*e4b17023SJohn Marino 
1478*e4b17023SJohn Marino    REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1479*e4b17023SJohn Marino    It is clear on return.  */
1480*e4b17023SJohn Marino 
1481*e4b17023SJohn Marino rtx
cselib_expand_value_rtx(rtx orig,bitmap regs_active,int max_depth)1482*e4b17023SJohn Marino cselib_expand_value_rtx (rtx orig, bitmap regs_active, int max_depth)
1483*e4b17023SJohn Marino {
1484*e4b17023SJohn Marino   struct expand_value_data evd;
1485*e4b17023SJohn Marino 
1486*e4b17023SJohn Marino   evd.regs_active = regs_active;
1487*e4b17023SJohn Marino   evd.callback = NULL;
1488*e4b17023SJohn Marino   evd.callback_arg = NULL;
1489*e4b17023SJohn Marino   evd.dummy = false;
1490*e4b17023SJohn Marino 
1491*e4b17023SJohn Marino   return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1492*e4b17023SJohn Marino }
1493*e4b17023SJohn Marino 
1494*e4b17023SJohn Marino /* Same as cselib_expand_value_rtx, but using a callback to try to
1495*e4b17023SJohn Marino    resolve some expressions.  The CB function should return ORIG if it
1496*e4b17023SJohn Marino    can't or does not want to deal with a certain RTX.  Any other
1497*e4b17023SJohn Marino    return value, including NULL, will be used as the expansion for
1498*e4b17023SJohn Marino    VALUE, without any further changes.  */
1499*e4b17023SJohn Marino 
1500*e4b17023SJohn Marino rtx
cselib_expand_value_rtx_cb(rtx orig,bitmap regs_active,int max_depth,cselib_expand_callback cb,void * data)1501*e4b17023SJohn Marino cselib_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1502*e4b17023SJohn Marino 			    cselib_expand_callback cb, void *data)
1503*e4b17023SJohn Marino {
1504*e4b17023SJohn Marino   struct expand_value_data evd;
1505*e4b17023SJohn Marino 
1506*e4b17023SJohn Marino   evd.regs_active = regs_active;
1507*e4b17023SJohn Marino   evd.callback = cb;
1508*e4b17023SJohn Marino   evd.callback_arg = data;
1509*e4b17023SJohn Marino   evd.dummy = false;
1510*e4b17023SJohn Marino 
1511*e4b17023SJohn Marino   return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1512*e4b17023SJohn Marino }
1513*e4b17023SJohn Marino 
1514*e4b17023SJohn Marino /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1515*e4b17023SJohn Marino    or simplified.  Useful to find out whether cselib_expand_value_rtx_cb
1516*e4b17023SJohn Marino    would return NULL or non-NULL, without allocating new rtx.  */
1517*e4b17023SJohn Marino 
1518*e4b17023SJohn Marino bool
cselib_dummy_expand_value_rtx_cb(rtx orig,bitmap regs_active,int max_depth,cselib_expand_callback cb,void * data)1519*e4b17023SJohn Marino cselib_dummy_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1520*e4b17023SJohn Marino 				  cselib_expand_callback cb, void *data)
1521*e4b17023SJohn Marino {
1522*e4b17023SJohn Marino   struct expand_value_data evd;
1523*e4b17023SJohn Marino 
1524*e4b17023SJohn Marino   evd.regs_active = regs_active;
1525*e4b17023SJohn Marino   evd.callback = cb;
1526*e4b17023SJohn Marino   evd.callback_arg = data;
1527*e4b17023SJohn Marino   evd.dummy = true;
1528*e4b17023SJohn Marino 
1529*e4b17023SJohn Marino   return cselib_expand_value_rtx_1 (orig, &evd, max_depth) != NULL;
1530*e4b17023SJohn Marino }
1531*e4b17023SJohn Marino 
1532*e4b17023SJohn Marino /* Internal implementation of cselib_expand_value_rtx and
1533*e4b17023SJohn Marino    cselib_expand_value_rtx_cb.  */
1534*e4b17023SJohn Marino 
1535*e4b17023SJohn Marino static rtx
cselib_expand_value_rtx_1(rtx orig,struct expand_value_data * evd,int max_depth)1536*e4b17023SJohn Marino cselib_expand_value_rtx_1 (rtx orig, struct expand_value_data *evd,
1537*e4b17023SJohn Marino 			   int max_depth)
1538*e4b17023SJohn Marino {
1539*e4b17023SJohn Marino   rtx copy, scopy;
1540*e4b17023SJohn Marino   int i, j;
1541*e4b17023SJohn Marino   RTX_CODE code;
1542*e4b17023SJohn Marino   const char *format_ptr;
1543*e4b17023SJohn Marino   enum machine_mode mode;
1544*e4b17023SJohn Marino 
1545*e4b17023SJohn Marino   code = GET_CODE (orig);
1546*e4b17023SJohn Marino 
1547*e4b17023SJohn Marino   /* For the context of dse, if we end up expand into a huge tree, we
1548*e4b17023SJohn Marino      will not have a useful address, so we might as well just give up
1549*e4b17023SJohn Marino      quickly.  */
1550*e4b17023SJohn Marino   if (max_depth <= 0)
1551*e4b17023SJohn Marino     return NULL;
1552*e4b17023SJohn Marino 
1553*e4b17023SJohn Marino   switch (code)
1554*e4b17023SJohn Marino     {
1555*e4b17023SJohn Marino     case REG:
1556*e4b17023SJohn Marino       {
1557*e4b17023SJohn Marino 	struct elt_list *l = REG_VALUES (REGNO (orig));
1558*e4b17023SJohn Marino 
1559*e4b17023SJohn Marino 	if (l && l->elt == NULL)
1560*e4b17023SJohn Marino 	  l = l->next;
1561*e4b17023SJohn Marino 	for (; l; l = l->next)
1562*e4b17023SJohn Marino 	  if (GET_MODE (l->elt->val_rtx) == GET_MODE (orig))
1563*e4b17023SJohn Marino 	    {
1564*e4b17023SJohn Marino 	      rtx result;
1565*e4b17023SJohn Marino 	      unsigned regno = REGNO (orig);
1566*e4b17023SJohn Marino 
1567*e4b17023SJohn Marino 	      /* The only thing that we are not willing to do (this
1568*e4b17023SJohn Marino 		 is requirement of dse and if others potential uses
1569*e4b17023SJohn Marino 		 need this function we should add a parm to control
1570*e4b17023SJohn Marino 		 it) is that we will not substitute the
1571*e4b17023SJohn Marino 		 STACK_POINTER_REGNUM, FRAME_POINTER or the
1572*e4b17023SJohn Marino 		 HARD_FRAME_POINTER.
1573*e4b17023SJohn Marino 
1574*e4b17023SJohn Marino 		 These expansions confuses the code that notices that
1575*e4b17023SJohn Marino 		 stores into the frame go dead at the end of the
1576*e4b17023SJohn Marino 		 function and that the frame is not effected by calls
1577*e4b17023SJohn Marino 		 to subroutines.  If you allow the
1578*e4b17023SJohn Marino 		 STACK_POINTER_REGNUM substitution, then dse will
1579*e4b17023SJohn Marino 		 think that parameter pushing also goes dead which is
1580*e4b17023SJohn Marino 		 wrong.  If you allow the FRAME_POINTER or the
1581*e4b17023SJohn Marino 		 HARD_FRAME_POINTER then you lose the opportunity to
1582*e4b17023SJohn Marino 		 make the frame assumptions.  */
1583*e4b17023SJohn Marino 	      if (regno == STACK_POINTER_REGNUM
1584*e4b17023SJohn Marino 		  || regno == FRAME_POINTER_REGNUM
1585*e4b17023SJohn Marino 		  || regno == HARD_FRAME_POINTER_REGNUM
1586*e4b17023SJohn Marino 		  || regno == cfa_base_preserved_regno)
1587*e4b17023SJohn Marino 		return orig;
1588*e4b17023SJohn Marino 
1589*e4b17023SJohn Marino 	      bitmap_set_bit (evd->regs_active, regno);
1590*e4b17023SJohn Marino 
1591*e4b17023SJohn Marino 	      if (dump_file && (dump_flags & TDF_CSELIB))
1592*e4b17023SJohn Marino 		fprintf (dump_file, "expanding: r%d into: ", regno);
1593*e4b17023SJohn Marino 
1594*e4b17023SJohn Marino 	      result = expand_loc (l->elt->locs, evd, max_depth);
1595*e4b17023SJohn Marino 	      bitmap_clear_bit (evd->regs_active, regno);
1596*e4b17023SJohn Marino 
1597*e4b17023SJohn Marino 	      if (result)
1598*e4b17023SJohn Marino 		return result;
1599*e4b17023SJohn Marino 	      else
1600*e4b17023SJohn Marino 		return orig;
1601*e4b17023SJohn Marino 	    }
1602*e4b17023SJohn Marino       }
1603*e4b17023SJohn Marino 
1604*e4b17023SJohn Marino     case CONST_INT:
1605*e4b17023SJohn Marino     case CONST_DOUBLE:
1606*e4b17023SJohn Marino     case CONST_VECTOR:
1607*e4b17023SJohn Marino     case SYMBOL_REF:
1608*e4b17023SJohn Marino     case CODE_LABEL:
1609*e4b17023SJohn Marino     case PC:
1610*e4b17023SJohn Marino     case CC0:
1611*e4b17023SJohn Marino     case SCRATCH:
1612*e4b17023SJohn Marino       /* SCRATCH must be shared because they represent distinct values.  */
1613*e4b17023SJohn Marino       return orig;
1614*e4b17023SJohn Marino     case CLOBBER:
1615*e4b17023SJohn Marino       if (REG_P (XEXP (orig, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig, 0))))
1616*e4b17023SJohn Marino 	return orig;
1617*e4b17023SJohn Marino       break;
1618*e4b17023SJohn Marino 
1619*e4b17023SJohn Marino     case CONST:
1620*e4b17023SJohn Marino       if (shared_const_p (orig))
1621*e4b17023SJohn Marino 	return orig;
1622*e4b17023SJohn Marino       break;
1623*e4b17023SJohn Marino 
1624*e4b17023SJohn Marino     case SUBREG:
1625*e4b17023SJohn Marino       {
1626*e4b17023SJohn Marino 	rtx subreg;
1627*e4b17023SJohn Marino 
1628*e4b17023SJohn Marino 	if (evd->callback)
1629*e4b17023SJohn Marino 	  {
1630*e4b17023SJohn Marino 	    subreg = evd->callback (orig, evd->regs_active, max_depth,
1631*e4b17023SJohn Marino 				    evd->callback_arg);
1632*e4b17023SJohn Marino 	    if (subreg != orig)
1633*e4b17023SJohn Marino 	      return subreg;
1634*e4b17023SJohn Marino 	  }
1635*e4b17023SJohn Marino 
1636*e4b17023SJohn Marino 	subreg = cselib_expand_value_rtx_1 (SUBREG_REG (orig), evd,
1637*e4b17023SJohn Marino 					    max_depth - 1);
1638*e4b17023SJohn Marino 	if (!subreg)
1639*e4b17023SJohn Marino 	  return NULL;
1640*e4b17023SJohn Marino 	scopy = simplify_gen_subreg (GET_MODE (orig), subreg,
1641*e4b17023SJohn Marino 				     GET_MODE (SUBREG_REG (orig)),
1642*e4b17023SJohn Marino 				     SUBREG_BYTE (orig));
1643*e4b17023SJohn Marino 	if (scopy == NULL
1644*e4b17023SJohn Marino 	    || (GET_CODE (scopy) == SUBREG
1645*e4b17023SJohn Marino 		&& !REG_P (SUBREG_REG (scopy))
1646*e4b17023SJohn Marino 		&& !MEM_P (SUBREG_REG (scopy))))
1647*e4b17023SJohn Marino 	  return NULL;
1648*e4b17023SJohn Marino 
1649*e4b17023SJohn Marino 	return scopy;
1650*e4b17023SJohn Marino       }
1651*e4b17023SJohn Marino 
1652*e4b17023SJohn Marino     case VALUE:
1653*e4b17023SJohn Marino       {
1654*e4b17023SJohn Marino 	rtx result;
1655*e4b17023SJohn Marino 
1656*e4b17023SJohn Marino 	if (dump_file && (dump_flags & TDF_CSELIB))
1657*e4b17023SJohn Marino 	  {
1658*e4b17023SJohn Marino 	    fputs ("\nexpanding ", dump_file);
1659*e4b17023SJohn Marino 	    print_rtl_single (dump_file, orig);
1660*e4b17023SJohn Marino 	    fputs (" into...", dump_file);
1661*e4b17023SJohn Marino 	  }
1662*e4b17023SJohn Marino 
1663*e4b17023SJohn Marino 	if (evd->callback)
1664*e4b17023SJohn Marino 	  {
1665*e4b17023SJohn Marino 	    result = evd->callback (orig, evd->regs_active, max_depth,
1666*e4b17023SJohn Marino 				    evd->callback_arg);
1667*e4b17023SJohn Marino 
1668*e4b17023SJohn Marino 	    if (result != orig)
1669*e4b17023SJohn Marino 	      return result;
1670*e4b17023SJohn Marino 	  }
1671*e4b17023SJohn Marino 
1672*e4b17023SJohn Marino 	result = expand_loc (CSELIB_VAL_PTR (orig)->locs, evd, max_depth);
1673*e4b17023SJohn Marino 	return result;
1674*e4b17023SJohn Marino       }
1675*e4b17023SJohn Marino 
1676*e4b17023SJohn Marino     case DEBUG_EXPR:
1677*e4b17023SJohn Marino       if (evd->callback)
1678*e4b17023SJohn Marino 	return evd->callback (orig, evd->regs_active, max_depth,
1679*e4b17023SJohn Marino 			      evd->callback_arg);
1680*e4b17023SJohn Marino       return orig;
1681*e4b17023SJohn Marino 
1682*e4b17023SJohn Marino     default:
1683*e4b17023SJohn Marino       break;
1684*e4b17023SJohn Marino     }
1685*e4b17023SJohn Marino 
1686*e4b17023SJohn Marino   /* Copy the various flags, fields, and other information.  We assume
1687*e4b17023SJohn Marino      that all fields need copying, and then clear the fields that should
1688*e4b17023SJohn Marino      not be copied.  That is the sensible default behavior, and forces
1689*e4b17023SJohn Marino      us to explicitly document why we are *not* copying a flag.  */
1690*e4b17023SJohn Marino   if (evd->dummy)
1691*e4b17023SJohn Marino     copy = NULL;
1692*e4b17023SJohn Marino   else
1693*e4b17023SJohn Marino     copy = shallow_copy_rtx (orig);
1694*e4b17023SJohn Marino 
1695*e4b17023SJohn Marino   format_ptr = GET_RTX_FORMAT (code);
1696*e4b17023SJohn Marino 
1697*e4b17023SJohn Marino   for (i = 0; i < GET_RTX_LENGTH (code); i++)
1698*e4b17023SJohn Marino     switch (*format_ptr++)
1699*e4b17023SJohn Marino       {
1700*e4b17023SJohn Marino       case 'e':
1701*e4b17023SJohn Marino 	if (XEXP (orig, i) != NULL)
1702*e4b17023SJohn Marino 	  {
1703*e4b17023SJohn Marino 	    rtx result = cselib_expand_value_rtx_1 (XEXP (orig, i), evd,
1704*e4b17023SJohn Marino 						    max_depth - 1);
1705*e4b17023SJohn Marino 	    if (!result)
1706*e4b17023SJohn Marino 	      return NULL;
1707*e4b17023SJohn Marino 	    if (copy)
1708*e4b17023SJohn Marino 	      XEXP (copy, i) = result;
1709*e4b17023SJohn Marino 	  }
1710*e4b17023SJohn Marino 	break;
1711*e4b17023SJohn Marino 
1712*e4b17023SJohn Marino       case 'E':
1713*e4b17023SJohn Marino       case 'V':
1714*e4b17023SJohn Marino 	if (XVEC (orig, i) != NULL)
1715*e4b17023SJohn Marino 	  {
1716*e4b17023SJohn Marino 	    if (copy)
1717*e4b17023SJohn Marino 	      XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
1718*e4b17023SJohn Marino 	    for (j = 0; j < XVECLEN (orig, i); j++)
1719*e4b17023SJohn Marino 	      {
1720*e4b17023SJohn Marino 		rtx result = cselib_expand_value_rtx_1 (XVECEXP (orig, i, j),
1721*e4b17023SJohn Marino 							evd, max_depth - 1);
1722*e4b17023SJohn Marino 		if (!result)
1723*e4b17023SJohn Marino 		  return NULL;
1724*e4b17023SJohn Marino 		if (copy)
1725*e4b17023SJohn Marino 		  XVECEXP (copy, i, j) = result;
1726*e4b17023SJohn Marino 	      }
1727*e4b17023SJohn Marino 	  }
1728*e4b17023SJohn Marino 	break;
1729*e4b17023SJohn Marino 
1730*e4b17023SJohn Marino       case 't':
1731*e4b17023SJohn Marino       case 'w':
1732*e4b17023SJohn Marino       case 'i':
1733*e4b17023SJohn Marino       case 's':
1734*e4b17023SJohn Marino       case 'S':
1735*e4b17023SJohn Marino       case 'T':
1736*e4b17023SJohn Marino       case 'u':
1737*e4b17023SJohn Marino       case 'B':
1738*e4b17023SJohn Marino       case '0':
1739*e4b17023SJohn Marino 	/* These are left unchanged.  */
1740*e4b17023SJohn Marino 	break;
1741*e4b17023SJohn Marino 
1742*e4b17023SJohn Marino       default:
1743*e4b17023SJohn Marino 	gcc_unreachable ();
1744*e4b17023SJohn Marino       }
1745*e4b17023SJohn Marino 
1746*e4b17023SJohn Marino   if (evd->dummy)
1747*e4b17023SJohn Marino     return orig;
1748*e4b17023SJohn Marino 
1749*e4b17023SJohn Marino   mode = GET_MODE (copy);
1750*e4b17023SJohn Marino   /* If an operand has been simplified into CONST_INT, which doesn't
1751*e4b17023SJohn Marino      have a mode and the mode isn't derivable from whole rtx's mode,
1752*e4b17023SJohn Marino      try simplify_*_operation first with mode from original's operand
1753*e4b17023SJohn Marino      and as a fallback wrap CONST_INT into gen_rtx_CONST.  */
1754*e4b17023SJohn Marino   scopy = copy;
1755*e4b17023SJohn Marino   switch (GET_RTX_CLASS (code))
1756*e4b17023SJohn Marino     {
1757*e4b17023SJohn Marino     case RTX_UNARY:
1758*e4b17023SJohn Marino       if (CONST_INT_P (XEXP (copy, 0))
1759*e4b17023SJohn Marino 	  && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1760*e4b17023SJohn Marino 	{
1761*e4b17023SJohn Marino 	  scopy = simplify_unary_operation (code, mode, XEXP (copy, 0),
1762*e4b17023SJohn Marino 					    GET_MODE (XEXP (orig, 0)));
1763*e4b17023SJohn Marino 	  if (scopy)
1764*e4b17023SJohn Marino 	    return scopy;
1765*e4b17023SJohn Marino 	}
1766*e4b17023SJohn Marino       break;
1767*e4b17023SJohn Marino     case RTX_COMM_ARITH:
1768*e4b17023SJohn Marino     case RTX_BIN_ARITH:
1769*e4b17023SJohn Marino       /* These expressions can derive operand modes from the whole rtx's mode.  */
1770*e4b17023SJohn Marino       break;
1771*e4b17023SJohn Marino     case RTX_TERNARY:
1772*e4b17023SJohn Marino     case RTX_BITFIELD_OPS:
1773*e4b17023SJohn Marino       if (CONST_INT_P (XEXP (copy, 0))
1774*e4b17023SJohn Marino 	  && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1775*e4b17023SJohn Marino 	{
1776*e4b17023SJohn Marino 	  scopy = simplify_ternary_operation (code, mode,
1777*e4b17023SJohn Marino 					      GET_MODE (XEXP (orig, 0)),
1778*e4b17023SJohn Marino 					      XEXP (copy, 0), XEXP (copy, 1),
1779*e4b17023SJohn Marino 					      XEXP (copy, 2));
1780*e4b17023SJohn Marino 	  if (scopy)
1781*e4b17023SJohn Marino 	    return scopy;
1782*e4b17023SJohn Marino 	}
1783*e4b17023SJohn Marino       break;
1784*e4b17023SJohn Marino     case RTX_COMPARE:
1785*e4b17023SJohn Marino     case RTX_COMM_COMPARE:
1786*e4b17023SJohn Marino       if (CONST_INT_P (XEXP (copy, 0))
1787*e4b17023SJohn Marino 	  && GET_MODE (XEXP (copy, 1)) == VOIDmode
1788*e4b17023SJohn Marino 	  && (GET_MODE (XEXP (orig, 0)) != VOIDmode
1789*e4b17023SJohn Marino 	      || GET_MODE (XEXP (orig, 1)) != VOIDmode))
1790*e4b17023SJohn Marino 	{
1791*e4b17023SJohn Marino 	  scopy = simplify_relational_operation (code, mode,
1792*e4b17023SJohn Marino 						 (GET_MODE (XEXP (orig, 0))
1793*e4b17023SJohn Marino 						  != VOIDmode)
1794*e4b17023SJohn Marino 						 ? GET_MODE (XEXP (orig, 0))
1795*e4b17023SJohn Marino 						 : GET_MODE (XEXP (orig, 1)),
1796*e4b17023SJohn Marino 						 XEXP (copy, 0),
1797*e4b17023SJohn Marino 						 XEXP (copy, 1));
1798*e4b17023SJohn Marino 	  if (scopy)
1799*e4b17023SJohn Marino 	    return scopy;
1800*e4b17023SJohn Marino 	}
1801*e4b17023SJohn Marino       break;
1802*e4b17023SJohn Marino     default:
1803*e4b17023SJohn Marino       break;
1804*e4b17023SJohn Marino     }
1805*e4b17023SJohn Marino   scopy = simplify_rtx (copy);
1806*e4b17023SJohn Marino   if (scopy)
1807*e4b17023SJohn Marino     return scopy;
1808*e4b17023SJohn Marino   return copy;
1809*e4b17023SJohn Marino }
1810*e4b17023SJohn Marino 
1811*e4b17023SJohn Marino /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
1812*e4b17023SJohn Marino    with VALUE expressions.  This way, it becomes independent of changes
1813*e4b17023SJohn Marino    to registers and memory.
1814*e4b17023SJohn Marino    X isn't actually modified; if modifications are needed, new rtl is
1815*e4b17023SJohn Marino    allocated.  However, the return value can share rtl with X.
1816*e4b17023SJohn Marino    If X is within a MEM, MEMMODE must be the mode of the MEM.  */
1817*e4b17023SJohn Marino 
1818*e4b17023SJohn Marino rtx
cselib_subst_to_values(rtx x,enum machine_mode memmode)1819*e4b17023SJohn Marino cselib_subst_to_values (rtx x, enum machine_mode memmode)
1820*e4b17023SJohn Marino {
1821*e4b17023SJohn Marino   enum rtx_code code = GET_CODE (x);
1822*e4b17023SJohn Marino   const char *fmt = GET_RTX_FORMAT (code);
1823*e4b17023SJohn Marino   cselib_val *e;
1824*e4b17023SJohn Marino   struct elt_list *l;
1825*e4b17023SJohn Marino   rtx copy = x;
1826*e4b17023SJohn Marino   int i;
1827*e4b17023SJohn Marino 
1828*e4b17023SJohn Marino   switch (code)
1829*e4b17023SJohn Marino     {
1830*e4b17023SJohn Marino     case REG:
1831*e4b17023SJohn Marino       l = REG_VALUES (REGNO (x));
1832*e4b17023SJohn Marino       if (l && l->elt == NULL)
1833*e4b17023SJohn Marino 	l = l->next;
1834*e4b17023SJohn Marino       for (; l; l = l->next)
1835*e4b17023SJohn Marino 	if (GET_MODE (l->elt->val_rtx) == GET_MODE (x))
1836*e4b17023SJohn Marino 	  return l->elt->val_rtx;
1837*e4b17023SJohn Marino 
1838*e4b17023SJohn Marino       gcc_unreachable ();
1839*e4b17023SJohn Marino 
1840*e4b17023SJohn Marino     case MEM:
1841*e4b17023SJohn Marino       e = cselib_lookup_mem (x, 0);
1842*e4b17023SJohn Marino       /* This used to happen for autoincrements, but we deal with them
1843*e4b17023SJohn Marino 	 properly now.  Remove the if stmt for the next release.  */
1844*e4b17023SJohn Marino       if (! e)
1845*e4b17023SJohn Marino 	{
1846*e4b17023SJohn Marino 	  /* Assign a value that doesn't match any other.  */
1847*e4b17023SJohn Marino 	  e = new_cselib_val (next_uid, GET_MODE (x), x);
1848*e4b17023SJohn Marino 	}
1849*e4b17023SJohn Marino       return e->val_rtx;
1850*e4b17023SJohn Marino 
1851*e4b17023SJohn Marino     case ENTRY_VALUE:
1852*e4b17023SJohn Marino       e = cselib_lookup (x, GET_MODE (x), 0, memmode);
1853*e4b17023SJohn Marino       if (! e)
1854*e4b17023SJohn Marino 	break;
1855*e4b17023SJohn Marino       return e->val_rtx;
1856*e4b17023SJohn Marino 
1857*e4b17023SJohn Marino     case CONST_DOUBLE:
1858*e4b17023SJohn Marino     case CONST_VECTOR:
1859*e4b17023SJohn Marino     case CONST_INT:
1860*e4b17023SJohn Marino     case CONST_FIXED:
1861*e4b17023SJohn Marino       return x;
1862*e4b17023SJohn Marino 
1863*e4b17023SJohn Marino     case PRE_DEC:
1864*e4b17023SJohn Marino     case PRE_INC:
1865*e4b17023SJohn Marino       gcc_assert (memmode != VOIDmode);
1866*e4b17023SJohn Marino       i = GET_MODE_SIZE (memmode);
1867*e4b17023SJohn Marino       if (code == PRE_DEC)
1868*e4b17023SJohn Marino 	i = -i;
1869*e4b17023SJohn Marino       return cselib_subst_to_values (plus_constant (XEXP (x, 0), i),
1870*e4b17023SJohn Marino 				     memmode);
1871*e4b17023SJohn Marino 
1872*e4b17023SJohn Marino     case PRE_MODIFY:
1873*e4b17023SJohn Marino       gcc_assert (memmode != VOIDmode);
1874*e4b17023SJohn Marino       return cselib_subst_to_values (XEXP (x, 1), memmode);
1875*e4b17023SJohn Marino 
1876*e4b17023SJohn Marino     case POST_DEC:
1877*e4b17023SJohn Marino     case POST_INC:
1878*e4b17023SJohn Marino     case POST_MODIFY:
1879*e4b17023SJohn Marino       gcc_assert (memmode != VOIDmode);
1880*e4b17023SJohn Marino       return cselib_subst_to_values (XEXP (x, 0), memmode);
1881*e4b17023SJohn Marino 
1882*e4b17023SJohn Marino     default:
1883*e4b17023SJohn Marino       break;
1884*e4b17023SJohn Marino     }
1885*e4b17023SJohn Marino 
1886*e4b17023SJohn Marino   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1887*e4b17023SJohn Marino     {
1888*e4b17023SJohn Marino       if (fmt[i] == 'e')
1889*e4b17023SJohn Marino 	{
1890*e4b17023SJohn Marino 	  rtx t = cselib_subst_to_values (XEXP (x, i), memmode);
1891*e4b17023SJohn Marino 
1892*e4b17023SJohn Marino 	  if (t != XEXP (x, i))
1893*e4b17023SJohn Marino 	    {
1894*e4b17023SJohn Marino 	      if (x == copy)
1895*e4b17023SJohn Marino 		copy = shallow_copy_rtx (x);
1896*e4b17023SJohn Marino 	      XEXP (copy, i) = t;
1897*e4b17023SJohn Marino 	    }
1898*e4b17023SJohn Marino 	}
1899*e4b17023SJohn Marino       else if (fmt[i] == 'E')
1900*e4b17023SJohn Marino 	{
1901*e4b17023SJohn Marino 	  int j;
1902*e4b17023SJohn Marino 
1903*e4b17023SJohn Marino 	  for (j = 0; j < XVECLEN (x, i); j++)
1904*e4b17023SJohn Marino 	    {
1905*e4b17023SJohn Marino 	      rtx t = cselib_subst_to_values (XVECEXP (x, i, j), memmode);
1906*e4b17023SJohn Marino 
1907*e4b17023SJohn Marino 	      if (t != XVECEXP (x, i, j))
1908*e4b17023SJohn Marino 		{
1909*e4b17023SJohn Marino 		  if (XVEC (x, i) == XVEC (copy, i))
1910*e4b17023SJohn Marino 		    {
1911*e4b17023SJohn Marino 		      if (x == copy)
1912*e4b17023SJohn Marino 			copy = shallow_copy_rtx (x);
1913*e4b17023SJohn Marino 		      XVEC (copy, i) = shallow_copy_rtvec (XVEC (x, i));
1914*e4b17023SJohn Marino 		    }
1915*e4b17023SJohn Marino 		  XVECEXP (copy, i, j) = t;
1916*e4b17023SJohn Marino 		}
1917*e4b17023SJohn Marino 	    }
1918*e4b17023SJohn Marino 	}
1919*e4b17023SJohn Marino     }
1920*e4b17023SJohn Marino 
1921*e4b17023SJohn Marino   return copy;
1922*e4b17023SJohn Marino }
1923*e4b17023SJohn Marino 
1924*e4b17023SJohn Marino /* Wrapper for cselib_subst_to_values, that indicates X is in INSN.  */
1925*e4b17023SJohn Marino 
1926*e4b17023SJohn Marino rtx
cselib_subst_to_values_from_insn(rtx x,enum machine_mode memmode,rtx insn)1927*e4b17023SJohn Marino cselib_subst_to_values_from_insn (rtx x, enum machine_mode memmode, rtx insn)
1928*e4b17023SJohn Marino {
1929*e4b17023SJohn Marino   rtx ret;
1930*e4b17023SJohn Marino   gcc_assert (!cselib_current_insn);
1931*e4b17023SJohn Marino   cselib_current_insn = insn;
1932*e4b17023SJohn Marino   ret = cselib_subst_to_values (x, memmode);
1933*e4b17023SJohn Marino   cselib_current_insn = NULL;
1934*e4b17023SJohn Marino   return ret;
1935*e4b17023SJohn Marino }
1936*e4b17023SJohn Marino 
1937*e4b17023SJohn Marino /* Look up the rtl expression X in our tables and return the value it
1938*e4b17023SJohn Marino    has.  If CREATE is zero, we return NULL if we don't know the value.
1939*e4b17023SJohn Marino    Otherwise, we create a new one if possible, using mode MODE if X
1940*e4b17023SJohn Marino    doesn't have a mode (i.e. because it's a constant).  When X is part
1941*e4b17023SJohn Marino    of an address, MEMMODE should be the mode of the enclosing MEM if
1942*e4b17023SJohn Marino    we're tracking autoinc expressions.  */
1943*e4b17023SJohn Marino 
1944*e4b17023SJohn Marino static cselib_val *
cselib_lookup_1(rtx x,enum machine_mode mode,int create,enum machine_mode memmode)1945*e4b17023SJohn Marino cselib_lookup_1 (rtx x, enum machine_mode mode,
1946*e4b17023SJohn Marino 		 int create, enum machine_mode memmode)
1947*e4b17023SJohn Marino {
1948*e4b17023SJohn Marino   void **slot;
1949*e4b17023SJohn Marino   cselib_val *e;
1950*e4b17023SJohn Marino   unsigned int hashval;
1951*e4b17023SJohn Marino 
1952*e4b17023SJohn Marino   if (GET_MODE (x) != VOIDmode)
1953*e4b17023SJohn Marino     mode = GET_MODE (x);
1954*e4b17023SJohn Marino 
1955*e4b17023SJohn Marino   if (GET_CODE (x) == VALUE)
1956*e4b17023SJohn Marino     return CSELIB_VAL_PTR (x);
1957*e4b17023SJohn Marino 
1958*e4b17023SJohn Marino   if (REG_P (x))
1959*e4b17023SJohn Marino     {
1960*e4b17023SJohn Marino       struct elt_list *l;
1961*e4b17023SJohn Marino       unsigned int i = REGNO (x);
1962*e4b17023SJohn Marino 
1963*e4b17023SJohn Marino       l = REG_VALUES (i);
1964*e4b17023SJohn Marino       if (l && l->elt == NULL)
1965*e4b17023SJohn Marino 	l = l->next;
1966*e4b17023SJohn Marino       for (; l; l = l->next)
1967*e4b17023SJohn Marino 	if (mode == GET_MODE (l->elt->val_rtx))
1968*e4b17023SJohn Marino 	  {
1969*e4b17023SJohn Marino 	    promote_debug_loc (l->elt->locs);
1970*e4b17023SJohn Marino 	    return l->elt;
1971*e4b17023SJohn Marino 	  }
1972*e4b17023SJohn Marino 
1973*e4b17023SJohn Marino       if (! create)
1974*e4b17023SJohn Marino 	return 0;
1975*e4b17023SJohn Marino 
1976*e4b17023SJohn Marino       if (i < FIRST_PSEUDO_REGISTER)
1977*e4b17023SJohn Marino 	{
1978*e4b17023SJohn Marino 	  unsigned int n = hard_regno_nregs[i][mode];
1979*e4b17023SJohn Marino 
1980*e4b17023SJohn Marino 	  if (n > max_value_regs)
1981*e4b17023SJohn Marino 	    max_value_regs = n;
1982*e4b17023SJohn Marino 	}
1983*e4b17023SJohn Marino 
1984*e4b17023SJohn Marino       e = new_cselib_val (next_uid, GET_MODE (x), x);
1985*e4b17023SJohn Marino       new_elt_loc_list (e, x);
1986*e4b17023SJohn Marino       if (REG_VALUES (i) == 0)
1987*e4b17023SJohn Marino 	{
1988*e4b17023SJohn Marino 	  /* Maintain the invariant that the first entry of
1989*e4b17023SJohn Marino 	     REG_VALUES, if present, must be the value used to set the
1990*e4b17023SJohn Marino 	     register, or NULL.  */
1991*e4b17023SJohn Marino 	  used_regs[n_used_regs++] = i;
1992*e4b17023SJohn Marino 	  REG_VALUES (i) = new_elt_list (REG_VALUES (i), NULL);
1993*e4b17023SJohn Marino 	}
1994*e4b17023SJohn Marino       else if (cselib_preserve_constants
1995*e4b17023SJohn Marino 	       && GET_MODE_CLASS (mode) == MODE_INT)
1996*e4b17023SJohn Marino 	{
1997*e4b17023SJohn Marino 	  /* During var-tracking, try harder to find equivalences
1998*e4b17023SJohn Marino 	     for SUBREGs.  If a setter sets say a DImode register
1999*e4b17023SJohn Marino 	     and user uses that register only in SImode, add a lowpart
2000*e4b17023SJohn Marino 	     subreg location.  */
2001*e4b17023SJohn Marino 	  struct elt_list *lwider = NULL;
2002*e4b17023SJohn Marino 	  l = REG_VALUES (i);
2003*e4b17023SJohn Marino 	  if (l && l->elt == NULL)
2004*e4b17023SJohn Marino 	    l = l->next;
2005*e4b17023SJohn Marino 	  for (; l; l = l->next)
2006*e4b17023SJohn Marino 	    if (GET_MODE_CLASS (GET_MODE (l->elt->val_rtx)) == MODE_INT
2007*e4b17023SJohn Marino 		&& GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
2008*e4b17023SJohn Marino 		   > GET_MODE_SIZE (mode)
2009*e4b17023SJohn Marino 		&& (lwider == NULL
2010*e4b17023SJohn Marino 		    || GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
2011*e4b17023SJohn Marino 		       < GET_MODE_SIZE (GET_MODE (lwider->elt->val_rtx))))
2012*e4b17023SJohn Marino 	      {
2013*e4b17023SJohn Marino 		struct elt_loc_list *el;
2014*e4b17023SJohn Marino 		if (i < FIRST_PSEUDO_REGISTER
2015*e4b17023SJohn Marino 		    && hard_regno_nregs[i][GET_MODE (l->elt->val_rtx)] != 1)
2016*e4b17023SJohn Marino 		  continue;
2017*e4b17023SJohn Marino 		for (el = l->elt->locs; el; el = el->next)
2018*e4b17023SJohn Marino 		  if (!REG_P (el->loc))
2019*e4b17023SJohn Marino 		    break;
2020*e4b17023SJohn Marino 		if (el)
2021*e4b17023SJohn Marino 		  lwider = l;
2022*e4b17023SJohn Marino 	      }
2023*e4b17023SJohn Marino 	  if (lwider)
2024*e4b17023SJohn Marino 	    {
2025*e4b17023SJohn Marino 	      rtx sub = lowpart_subreg (mode, lwider->elt->val_rtx,
2026*e4b17023SJohn Marino 					GET_MODE (lwider->elt->val_rtx));
2027*e4b17023SJohn Marino 	      if (sub)
2028*e4b17023SJohn Marino 		new_elt_loc_list (e, sub);
2029*e4b17023SJohn Marino 	    }
2030*e4b17023SJohn Marino 	}
2031*e4b17023SJohn Marino       REG_VALUES (i)->next = new_elt_list (REG_VALUES (i)->next, e);
2032*e4b17023SJohn Marino       slot = cselib_find_slot (x, e->hash, INSERT, memmode);
2033*e4b17023SJohn Marino       *slot = e;
2034*e4b17023SJohn Marino       return e;
2035*e4b17023SJohn Marino     }
2036*e4b17023SJohn Marino 
2037*e4b17023SJohn Marino   if (MEM_P (x))
2038*e4b17023SJohn Marino     return cselib_lookup_mem (x, create);
2039*e4b17023SJohn Marino 
2040*e4b17023SJohn Marino   hashval = cselib_hash_rtx (x, create, memmode);
2041*e4b17023SJohn Marino   /* Can't even create if hashing is not possible.  */
2042*e4b17023SJohn Marino   if (! hashval)
2043*e4b17023SJohn Marino     return 0;
2044*e4b17023SJohn Marino 
2045*e4b17023SJohn Marino   slot = cselib_find_slot (wrap_constant (mode, x), hashval,
2046*e4b17023SJohn Marino 			   create ? INSERT : NO_INSERT, memmode);
2047*e4b17023SJohn Marino   if (slot == 0)
2048*e4b17023SJohn Marino     return 0;
2049*e4b17023SJohn Marino 
2050*e4b17023SJohn Marino   e = (cselib_val *) *slot;
2051*e4b17023SJohn Marino   if (e)
2052*e4b17023SJohn Marino     return e;
2053*e4b17023SJohn Marino 
2054*e4b17023SJohn Marino   e = new_cselib_val (hashval, mode, x);
2055*e4b17023SJohn Marino 
2056*e4b17023SJohn Marino   /* We have to fill the slot before calling cselib_subst_to_values:
2057*e4b17023SJohn Marino      the hash table is inconsistent until we do so, and
2058*e4b17023SJohn Marino      cselib_subst_to_values will need to do lookups.  */
2059*e4b17023SJohn Marino   *slot = (void *) e;
2060*e4b17023SJohn Marino   new_elt_loc_list (e, cselib_subst_to_values (x, memmode));
2061*e4b17023SJohn Marino   return e;
2062*e4b17023SJohn Marino }
2063*e4b17023SJohn Marino 
2064*e4b17023SJohn Marino /* Wrapper for cselib_lookup, that indicates X is in INSN.  */
2065*e4b17023SJohn Marino 
2066*e4b17023SJohn Marino cselib_val *
cselib_lookup_from_insn(rtx x,enum machine_mode mode,int create,enum machine_mode memmode,rtx insn)2067*e4b17023SJohn Marino cselib_lookup_from_insn (rtx x, enum machine_mode mode,
2068*e4b17023SJohn Marino 			 int create, enum machine_mode memmode, rtx insn)
2069*e4b17023SJohn Marino {
2070*e4b17023SJohn Marino   cselib_val *ret;
2071*e4b17023SJohn Marino 
2072*e4b17023SJohn Marino   gcc_assert (!cselib_current_insn);
2073*e4b17023SJohn Marino   cselib_current_insn = insn;
2074*e4b17023SJohn Marino 
2075*e4b17023SJohn Marino   ret = cselib_lookup (x, mode, create, memmode);
2076*e4b17023SJohn Marino 
2077*e4b17023SJohn Marino   cselib_current_insn = NULL;
2078*e4b17023SJohn Marino 
2079*e4b17023SJohn Marino   return ret;
2080*e4b17023SJohn Marino }
2081*e4b17023SJohn Marino 
2082*e4b17023SJohn Marino /* Wrapper for cselib_lookup_1, that logs the lookup result and
2083*e4b17023SJohn Marino    maintains invariants related with debug insns.  */
2084*e4b17023SJohn Marino 
2085*e4b17023SJohn Marino cselib_val *
cselib_lookup(rtx x,enum machine_mode mode,int create,enum machine_mode memmode)2086*e4b17023SJohn Marino cselib_lookup (rtx x, enum machine_mode mode,
2087*e4b17023SJohn Marino 	       int create, enum machine_mode memmode)
2088*e4b17023SJohn Marino {
2089*e4b17023SJohn Marino   cselib_val *ret = cselib_lookup_1 (x, mode, create, memmode);
2090*e4b17023SJohn Marino 
2091*e4b17023SJohn Marino   /* ??? Should we return NULL if we're not to create an entry, the
2092*e4b17023SJohn Marino      found loc is a debug loc and cselib_current_insn is not DEBUG?
2093*e4b17023SJohn Marino      If so, we should also avoid converting val to non-DEBUG; probably
2094*e4b17023SJohn Marino      easiest setting cselib_current_insn to NULL before the call
2095*e4b17023SJohn Marino      above.  */
2096*e4b17023SJohn Marino 
2097*e4b17023SJohn Marino   if (dump_file && (dump_flags & TDF_CSELIB))
2098*e4b17023SJohn Marino     {
2099*e4b17023SJohn Marino       fputs ("cselib lookup ", dump_file);
2100*e4b17023SJohn Marino       print_inline_rtx (dump_file, x, 2);
2101*e4b17023SJohn Marino       fprintf (dump_file, " => %u:%u\n",
2102*e4b17023SJohn Marino 	       ret ? ret->uid : 0,
2103*e4b17023SJohn Marino 	       ret ? ret->hash : 0);
2104*e4b17023SJohn Marino     }
2105*e4b17023SJohn Marino 
2106*e4b17023SJohn Marino   return ret;
2107*e4b17023SJohn Marino }
2108*e4b17023SJohn Marino 
2109*e4b17023SJohn Marino /* Invalidate any entries in reg_values that overlap REGNO.  This is called
2110*e4b17023SJohn Marino    if REGNO is changing.  MODE is the mode of the assignment to REGNO, which
2111*e4b17023SJohn Marino    is used to determine how many hard registers are being changed.  If MODE
2112*e4b17023SJohn Marino    is VOIDmode, then only REGNO is being changed; this is used when
2113*e4b17023SJohn Marino    invalidating call clobbered registers across a call.  */
2114*e4b17023SJohn Marino 
2115*e4b17023SJohn Marino static void
cselib_invalidate_regno(unsigned int regno,enum machine_mode mode)2116*e4b17023SJohn Marino cselib_invalidate_regno (unsigned int regno, enum machine_mode mode)
2117*e4b17023SJohn Marino {
2118*e4b17023SJohn Marino   unsigned int endregno;
2119*e4b17023SJohn Marino   unsigned int i;
2120*e4b17023SJohn Marino 
2121*e4b17023SJohn Marino   /* If we see pseudos after reload, something is _wrong_.  */
2122*e4b17023SJohn Marino   gcc_assert (!reload_completed || regno < FIRST_PSEUDO_REGISTER
2123*e4b17023SJohn Marino 	      || reg_renumber[regno] < 0);
2124*e4b17023SJohn Marino 
2125*e4b17023SJohn Marino   /* Determine the range of registers that must be invalidated.  For
2126*e4b17023SJohn Marino      pseudos, only REGNO is affected.  For hard regs, we must take MODE
2127*e4b17023SJohn Marino      into account, and we must also invalidate lower register numbers
2128*e4b17023SJohn Marino      if they contain values that overlap REGNO.  */
2129*e4b17023SJohn Marino   if (regno < FIRST_PSEUDO_REGISTER)
2130*e4b17023SJohn Marino     {
2131*e4b17023SJohn Marino       gcc_assert (mode != VOIDmode);
2132*e4b17023SJohn Marino 
2133*e4b17023SJohn Marino       if (regno < max_value_regs)
2134*e4b17023SJohn Marino 	i = 0;
2135*e4b17023SJohn Marino       else
2136*e4b17023SJohn Marino 	i = regno - max_value_regs;
2137*e4b17023SJohn Marino 
2138*e4b17023SJohn Marino       endregno = end_hard_regno (mode, regno);
2139*e4b17023SJohn Marino     }
2140*e4b17023SJohn Marino   else
2141*e4b17023SJohn Marino     {
2142*e4b17023SJohn Marino       i = regno;
2143*e4b17023SJohn Marino       endregno = regno + 1;
2144*e4b17023SJohn Marino     }
2145*e4b17023SJohn Marino 
2146*e4b17023SJohn Marino   for (; i < endregno; i++)
2147*e4b17023SJohn Marino     {
2148*e4b17023SJohn Marino       struct elt_list **l = &REG_VALUES (i);
2149*e4b17023SJohn Marino 
2150*e4b17023SJohn Marino       /* Go through all known values for this reg; if it overlaps the range
2151*e4b17023SJohn Marino 	 we're invalidating, remove the value.  */
2152*e4b17023SJohn Marino       while (*l)
2153*e4b17023SJohn Marino 	{
2154*e4b17023SJohn Marino 	  cselib_val *v = (*l)->elt;
2155*e4b17023SJohn Marino 	  bool had_locs;
2156*e4b17023SJohn Marino 	  rtx setting_insn;
2157*e4b17023SJohn Marino 	  struct elt_loc_list **p;
2158*e4b17023SJohn Marino 	  unsigned int this_last = i;
2159*e4b17023SJohn Marino 
2160*e4b17023SJohn Marino 	  if (i < FIRST_PSEUDO_REGISTER && v != NULL)
2161*e4b17023SJohn Marino 	    this_last = end_hard_regno (GET_MODE (v->val_rtx), i) - 1;
2162*e4b17023SJohn Marino 
2163*e4b17023SJohn Marino 	  if (this_last < regno || v == NULL
2164*e4b17023SJohn Marino 	      || (v == cfa_base_preserved_val
2165*e4b17023SJohn Marino 		  && i == cfa_base_preserved_regno))
2166*e4b17023SJohn Marino 	    {
2167*e4b17023SJohn Marino 	      l = &(*l)->next;
2168*e4b17023SJohn Marino 	      continue;
2169*e4b17023SJohn Marino 	    }
2170*e4b17023SJohn Marino 
2171*e4b17023SJohn Marino 	  /* We have an overlap.  */
2172*e4b17023SJohn Marino 	  if (*l == REG_VALUES (i))
2173*e4b17023SJohn Marino 	    {
2174*e4b17023SJohn Marino 	      /* Maintain the invariant that the first entry of
2175*e4b17023SJohn Marino 		 REG_VALUES, if present, must be the value used to set
2176*e4b17023SJohn Marino 		 the register, or NULL.  This is also nice because
2177*e4b17023SJohn Marino 		 then we won't push the same regno onto user_regs
2178*e4b17023SJohn Marino 		 multiple times.  */
2179*e4b17023SJohn Marino 	      (*l)->elt = NULL;
2180*e4b17023SJohn Marino 	      l = &(*l)->next;
2181*e4b17023SJohn Marino 	    }
2182*e4b17023SJohn Marino 	  else
2183*e4b17023SJohn Marino 	    unchain_one_elt_list (l);
2184*e4b17023SJohn Marino 
2185*e4b17023SJohn Marino 	  v = canonical_cselib_val (v);
2186*e4b17023SJohn Marino 
2187*e4b17023SJohn Marino 	  had_locs = v->locs != NULL;
2188*e4b17023SJohn Marino 	  setting_insn = v->locs ? v->locs->setting_insn : NULL;
2189*e4b17023SJohn Marino 
2190*e4b17023SJohn Marino 	  /* Now, we clear the mapping from value to reg.  It must exist, so
2191*e4b17023SJohn Marino 	     this code will crash intentionally if it doesn't.  */
2192*e4b17023SJohn Marino 	  for (p = &v->locs; ; p = &(*p)->next)
2193*e4b17023SJohn Marino 	    {
2194*e4b17023SJohn Marino 	      rtx x = (*p)->loc;
2195*e4b17023SJohn Marino 
2196*e4b17023SJohn Marino 	      if (REG_P (x) && REGNO (x) == i)
2197*e4b17023SJohn Marino 		{
2198*e4b17023SJohn Marino 		  unchain_one_elt_loc_list (p);
2199*e4b17023SJohn Marino 		  break;
2200*e4b17023SJohn Marino 		}
2201*e4b17023SJohn Marino 	    }
2202*e4b17023SJohn Marino 
2203*e4b17023SJohn Marino 	  if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2204*e4b17023SJohn Marino 	    {
2205*e4b17023SJohn Marino 	      if (setting_insn && DEBUG_INSN_P (setting_insn))
2206*e4b17023SJohn Marino 		n_useless_debug_values++;
2207*e4b17023SJohn Marino 	      else
2208*e4b17023SJohn Marino 		n_useless_values++;
2209*e4b17023SJohn Marino 	    }
2210*e4b17023SJohn Marino 	}
2211*e4b17023SJohn Marino     }
2212*e4b17023SJohn Marino }
2213*e4b17023SJohn Marino 
2214*e4b17023SJohn Marino /* Invalidate any locations in the table which are changed because of a
2215*e4b17023SJohn Marino    store to MEM_RTX.  If this is called because of a non-const call
2216*e4b17023SJohn Marino    instruction, MEM_RTX is (mem:BLK const0_rtx).  */
2217*e4b17023SJohn Marino 
2218*e4b17023SJohn Marino static void
cselib_invalidate_mem(rtx mem_rtx)2219*e4b17023SJohn Marino cselib_invalidate_mem (rtx mem_rtx)
2220*e4b17023SJohn Marino {
2221*e4b17023SJohn Marino   cselib_val **vp, *v, *next;
2222*e4b17023SJohn Marino   int num_mems = 0;
2223*e4b17023SJohn Marino   rtx mem_addr;
2224*e4b17023SJohn Marino 
2225*e4b17023SJohn Marino   mem_addr = canon_rtx (get_addr (XEXP (mem_rtx, 0)));
2226*e4b17023SJohn Marino   mem_rtx = canon_rtx (mem_rtx);
2227*e4b17023SJohn Marino 
2228*e4b17023SJohn Marino   vp = &first_containing_mem;
2229*e4b17023SJohn Marino   for (v = *vp; v != &dummy_val; v = next)
2230*e4b17023SJohn Marino     {
2231*e4b17023SJohn Marino       bool has_mem = false;
2232*e4b17023SJohn Marino       struct elt_loc_list **p = &v->locs;
2233*e4b17023SJohn Marino       bool had_locs = v->locs != NULL;
2234*e4b17023SJohn Marino       rtx setting_insn = v->locs ? v->locs->setting_insn : NULL;
2235*e4b17023SJohn Marino 
2236*e4b17023SJohn Marino       while (*p)
2237*e4b17023SJohn Marino 	{
2238*e4b17023SJohn Marino 	  rtx x = (*p)->loc;
2239*e4b17023SJohn Marino 	  cselib_val *addr;
2240*e4b17023SJohn Marino 	  struct elt_list **mem_chain;
2241*e4b17023SJohn Marino 
2242*e4b17023SJohn Marino 	  /* MEMs may occur in locations only at the top level; below
2243*e4b17023SJohn Marino 	     that every MEM or REG is substituted by its VALUE.  */
2244*e4b17023SJohn Marino 	  if (!MEM_P (x))
2245*e4b17023SJohn Marino 	    {
2246*e4b17023SJohn Marino 	      p = &(*p)->next;
2247*e4b17023SJohn Marino 	      continue;
2248*e4b17023SJohn Marino 	    }
2249*e4b17023SJohn Marino 	  if (num_mems < PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS)
2250*e4b17023SJohn Marino 	      && ! canon_true_dependence (mem_rtx, GET_MODE (mem_rtx),
2251*e4b17023SJohn Marino 					  mem_addr, x, NULL_RTX))
2252*e4b17023SJohn Marino 	    {
2253*e4b17023SJohn Marino 	      has_mem = true;
2254*e4b17023SJohn Marino 	      num_mems++;
2255*e4b17023SJohn Marino 	      p = &(*p)->next;
2256*e4b17023SJohn Marino 	      continue;
2257*e4b17023SJohn Marino 	    }
2258*e4b17023SJohn Marino 
2259*e4b17023SJohn Marino 	  /* This one overlaps.  */
2260*e4b17023SJohn Marino 	  /* We must have a mapping from this MEM's address to the
2261*e4b17023SJohn Marino 	     value (E).  Remove that, too.  */
2262*e4b17023SJohn Marino 	  addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0, GET_MODE (x));
2263*e4b17023SJohn Marino 	  addr = canonical_cselib_val (addr);
2264*e4b17023SJohn Marino 	  gcc_checking_assert (v == canonical_cselib_val (v));
2265*e4b17023SJohn Marino 	  mem_chain = &addr->addr_list;
2266*e4b17023SJohn Marino 	  for (;;)
2267*e4b17023SJohn Marino 	    {
2268*e4b17023SJohn Marino 	      cselib_val *canon = canonical_cselib_val ((*mem_chain)->elt);
2269*e4b17023SJohn Marino 
2270*e4b17023SJohn Marino 	      if (canon == v)
2271*e4b17023SJohn Marino 		{
2272*e4b17023SJohn Marino 		  unchain_one_elt_list (mem_chain);
2273*e4b17023SJohn Marino 		  break;
2274*e4b17023SJohn Marino 		}
2275*e4b17023SJohn Marino 
2276*e4b17023SJohn Marino 	      /* Record canonicalized elt.  */
2277*e4b17023SJohn Marino 	      (*mem_chain)->elt = canon;
2278*e4b17023SJohn Marino 
2279*e4b17023SJohn Marino 	      mem_chain = &(*mem_chain)->next;
2280*e4b17023SJohn Marino 	    }
2281*e4b17023SJohn Marino 
2282*e4b17023SJohn Marino 	  unchain_one_elt_loc_list (p);
2283*e4b17023SJohn Marino 	}
2284*e4b17023SJohn Marino 
2285*e4b17023SJohn Marino       if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2286*e4b17023SJohn Marino 	{
2287*e4b17023SJohn Marino 	  if (setting_insn && DEBUG_INSN_P (setting_insn))
2288*e4b17023SJohn Marino 	    n_useless_debug_values++;
2289*e4b17023SJohn Marino 	  else
2290*e4b17023SJohn Marino 	    n_useless_values++;
2291*e4b17023SJohn Marino 	}
2292*e4b17023SJohn Marino 
2293*e4b17023SJohn Marino       next = v->next_containing_mem;
2294*e4b17023SJohn Marino       if (has_mem)
2295*e4b17023SJohn Marino 	{
2296*e4b17023SJohn Marino 	  *vp = v;
2297*e4b17023SJohn Marino 	  vp = &(*vp)->next_containing_mem;
2298*e4b17023SJohn Marino 	}
2299*e4b17023SJohn Marino       else
2300*e4b17023SJohn Marino 	v->next_containing_mem = NULL;
2301*e4b17023SJohn Marino     }
2302*e4b17023SJohn Marino   *vp = &dummy_val;
2303*e4b17023SJohn Marino }
2304*e4b17023SJohn Marino 
2305*e4b17023SJohn Marino /* Invalidate DEST, which is being assigned to or clobbered.  */
2306*e4b17023SJohn Marino 
2307*e4b17023SJohn Marino void
cselib_invalidate_rtx(rtx dest)2308*e4b17023SJohn Marino cselib_invalidate_rtx (rtx dest)
2309*e4b17023SJohn Marino {
2310*e4b17023SJohn Marino   while (GET_CODE (dest) == SUBREG
2311*e4b17023SJohn Marino 	 || GET_CODE (dest) == ZERO_EXTRACT
2312*e4b17023SJohn Marino 	 || GET_CODE (dest) == STRICT_LOW_PART)
2313*e4b17023SJohn Marino     dest = XEXP (dest, 0);
2314*e4b17023SJohn Marino 
2315*e4b17023SJohn Marino   if (REG_P (dest))
2316*e4b17023SJohn Marino     cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
2317*e4b17023SJohn Marino   else if (MEM_P (dest))
2318*e4b17023SJohn Marino     cselib_invalidate_mem (dest);
2319*e4b17023SJohn Marino }
2320*e4b17023SJohn Marino 
2321*e4b17023SJohn Marino /* A wrapper for cselib_invalidate_rtx to be called via note_stores.  */
2322*e4b17023SJohn Marino 
2323*e4b17023SJohn Marino static void
cselib_invalidate_rtx_note_stores(rtx dest,const_rtx ignore ATTRIBUTE_UNUSED,void * data ATTRIBUTE_UNUSED)2324*e4b17023SJohn Marino cselib_invalidate_rtx_note_stores (rtx dest, const_rtx ignore ATTRIBUTE_UNUSED,
2325*e4b17023SJohn Marino 				   void *data ATTRIBUTE_UNUSED)
2326*e4b17023SJohn Marino {
2327*e4b17023SJohn Marino   cselib_invalidate_rtx (dest);
2328*e4b17023SJohn Marino }
2329*e4b17023SJohn Marino 
2330*e4b17023SJohn Marino /* Record the result of a SET instruction.  DEST is being set; the source
2331*e4b17023SJohn Marino    contains the value described by SRC_ELT.  If DEST is a MEM, DEST_ADDR_ELT
2332*e4b17023SJohn Marino    describes its address.  */
2333*e4b17023SJohn Marino 
2334*e4b17023SJohn Marino static void
cselib_record_set(rtx dest,cselib_val * src_elt,cselib_val * dest_addr_elt)2335*e4b17023SJohn Marino cselib_record_set (rtx dest, cselib_val *src_elt, cselib_val *dest_addr_elt)
2336*e4b17023SJohn Marino {
2337*e4b17023SJohn Marino   int dreg = REG_P (dest) ? (int) REGNO (dest) : -1;
2338*e4b17023SJohn Marino 
2339*e4b17023SJohn Marino   if (src_elt == 0 || side_effects_p (dest))
2340*e4b17023SJohn Marino     return;
2341*e4b17023SJohn Marino 
2342*e4b17023SJohn Marino   if (dreg >= 0)
2343*e4b17023SJohn Marino     {
2344*e4b17023SJohn Marino       if (dreg < FIRST_PSEUDO_REGISTER)
2345*e4b17023SJohn Marino 	{
2346*e4b17023SJohn Marino 	  unsigned int n = hard_regno_nregs[dreg][GET_MODE (dest)];
2347*e4b17023SJohn Marino 
2348*e4b17023SJohn Marino 	  if (n > max_value_regs)
2349*e4b17023SJohn Marino 	    max_value_regs = n;
2350*e4b17023SJohn Marino 	}
2351*e4b17023SJohn Marino 
2352*e4b17023SJohn Marino       if (REG_VALUES (dreg) == 0)
2353*e4b17023SJohn Marino 	{
2354*e4b17023SJohn Marino 	  used_regs[n_used_regs++] = dreg;
2355*e4b17023SJohn Marino 	  REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
2356*e4b17023SJohn Marino 	}
2357*e4b17023SJohn Marino       else
2358*e4b17023SJohn Marino 	{
2359*e4b17023SJohn Marino 	  /* The register should have been invalidated.  */
2360*e4b17023SJohn Marino 	  gcc_assert (REG_VALUES (dreg)->elt == 0);
2361*e4b17023SJohn Marino 	  REG_VALUES (dreg)->elt = src_elt;
2362*e4b17023SJohn Marino 	}
2363*e4b17023SJohn Marino 
2364*e4b17023SJohn Marino       if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2365*e4b17023SJohn Marino 	n_useless_values--;
2366*e4b17023SJohn Marino       new_elt_loc_list (src_elt, dest);
2367*e4b17023SJohn Marino     }
2368*e4b17023SJohn Marino   else if (MEM_P (dest) && dest_addr_elt != 0
2369*e4b17023SJohn Marino 	   && cselib_record_memory)
2370*e4b17023SJohn Marino     {
2371*e4b17023SJohn Marino       if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2372*e4b17023SJohn Marino 	n_useless_values--;
2373*e4b17023SJohn Marino       add_mem_for_addr (dest_addr_elt, src_elt, dest);
2374*e4b17023SJohn Marino     }
2375*e4b17023SJohn Marino }
2376*e4b17023SJohn Marino 
2377*e4b17023SJohn Marino /* Make ELT and X's VALUE equivalent to each other at INSN.  */
2378*e4b17023SJohn Marino 
2379*e4b17023SJohn Marino void
cselib_add_permanent_equiv(cselib_val * elt,rtx x,rtx insn)2380*e4b17023SJohn Marino cselib_add_permanent_equiv (cselib_val *elt, rtx x, rtx insn)
2381*e4b17023SJohn Marino {
2382*e4b17023SJohn Marino   cselib_val *nelt;
2383*e4b17023SJohn Marino   rtx save_cselib_current_insn = cselib_current_insn;
2384*e4b17023SJohn Marino 
2385*e4b17023SJohn Marino   gcc_checking_assert (elt);
2386*e4b17023SJohn Marino   gcc_checking_assert (PRESERVED_VALUE_P (elt->val_rtx));
2387*e4b17023SJohn Marino   gcc_checking_assert (!side_effects_p (x));
2388*e4b17023SJohn Marino 
2389*e4b17023SJohn Marino   cselib_current_insn = insn;
2390*e4b17023SJohn Marino 
2391*e4b17023SJohn Marino   nelt = cselib_lookup (x, GET_MODE (elt->val_rtx), 1, VOIDmode);
2392*e4b17023SJohn Marino 
2393*e4b17023SJohn Marino   if (nelt != elt)
2394*e4b17023SJohn Marino     {
2395*e4b17023SJohn Marino       cselib_any_perm_equivs = true;
2396*e4b17023SJohn Marino 
2397*e4b17023SJohn Marino       if (!PRESERVED_VALUE_P (nelt->val_rtx))
2398*e4b17023SJohn Marino 	cselib_preserve_value (nelt);
2399*e4b17023SJohn Marino 
2400*e4b17023SJohn Marino       new_elt_loc_list (nelt, elt->val_rtx);
2401*e4b17023SJohn Marino     }
2402*e4b17023SJohn Marino 
2403*e4b17023SJohn Marino   cselib_current_insn = save_cselib_current_insn;
2404*e4b17023SJohn Marino }
2405*e4b17023SJohn Marino 
2406*e4b17023SJohn Marino /* Return TRUE if any permanent equivalences have been recorded since
2407*e4b17023SJohn Marino    the table was last initialized.  */
2408*e4b17023SJohn Marino bool
cselib_have_permanent_equivalences(void)2409*e4b17023SJohn Marino cselib_have_permanent_equivalences (void)
2410*e4b17023SJohn Marino {
2411*e4b17023SJohn Marino   return cselib_any_perm_equivs;
2412*e4b17023SJohn Marino }
2413*e4b17023SJohn Marino 
2414*e4b17023SJohn Marino /* There is no good way to determine how many elements there can be
2415*e4b17023SJohn Marino    in a PARALLEL.  Since it's fairly cheap, use a really large number.  */
2416*e4b17023SJohn Marino #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2417*e4b17023SJohn Marino 
2418*e4b17023SJohn Marino struct cselib_record_autoinc_data
2419*e4b17023SJohn Marino {
2420*e4b17023SJohn Marino   struct cselib_set *sets;
2421*e4b17023SJohn Marino   int n_sets;
2422*e4b17023SJohn Marino };
2423*e4b17023SJohn Marino 
2424*e4b17023SJohn Marino /* Callback for for_each_inc_dec.  Records in ARG the SETs implied by
2425*e4b17023SJohn Marino    autoinc RTXs: SRC plus SRCOFF if non-NULL is stored in DEST.  */
2426*e4b17023SJohn Marino 
2427*e4b17023SJohn Marino static int
cselib_record_autoinc_cb(rtx mem ATTRIBUTE_UNUSED,rtx op ATTRIBUTE_UNUSED,rtx dest,rtx src,rtx srcoff,void * arg)2428*e4b17023SJohn Marino cselib_record_autoinc_cb (rtx mem ATTRIBUTE_UNUSED, rtx op ATTRIBUTE_UNUSED,
2429*e4b17023SJohn Marino 			  rtx dest, rtx src, rtx srcoff, void *arg)
2430*e4b17023SJohn Marino {
2431*e4b17023SJohn Marino   struct cselib_record_autoinc_data *data;
2432*e4b17023SJohn Marino   data = (struct cselib_record_autoinc_data *)arg;
2433*e4b17023SJohn Marino 
2434*e4b17023SJohn Marino   data->sets[data->n_sets].dest = dest;
2435*e4b17023SJohn Marino 
2436*e4b17023SJohn Marino   if (srcoff)
2437*e4b17023SJohn Marino     data->sets[data->n_sets].src = gen_rtx_PLUS (GET_MODE (src), src, srcoff);
2438*e4b17023SJohn Marino   else
2439*e4b17023SJohn Marino     data->sets[data->n_sets].src = src;
2440*e4b17023SJohn Marino 
2441*e4b17023SJohn Marino   data->n_sets++;
2442*e4b17023SJohn Marino 
2443*e4b17023SJohn Marino   return -1;
2444*e4b17023SJohn Marino }
2445*e4b17023SJohn Marino 
2446*e4b17023SJohn Marino /* Record the effects of any sets and autoincs in INSN.  */
2447*e4b17023SJohn Marino static void
cselib_record_sets(rtx insn)2448*e4b17023SJohn Marino cselib_record_sets (rtx insn)
2449*e4b17023SJohn Marino {
2450*e4b17023SJohn Marino   int n_sets = 0;
2451*e4b17023SJohn Marino   int i;
2452*e4b17023SJohn Marino   struct cselib_set sets[MAX_SETS];
2453*e4b17023SJohn Marino   rtx body = PATTERN (insn);
2454*e4b17023SJohn Marino   rtx cond = 0;
2455*e4b17023SJohn Marino   int n_sets_before_autoinc;
2456*e4b17023SJohn Marino   struct cselib_record_autoinc_data data;
2457*e4b17023SJohn Marino 
2458*e4b17023SJohn Marino   body = PATTERN (insn);
2459*e4b17023SJohn Marino   if (GET_CODE (body) == COND_EXEC)
2460*e4b17023SJohn Marino     {
2461*e4b17023SJohn Marino       cond = COND_EXEC_TEST (body);
2462*e4b17023SJohn Marino       body = COND_EXEC_CODE (body);
2463*e4b17023SJohn Marino     }
2464*e4b17023SJohn Marino 
2465*e4b17023SJohn Marino   /* Find all sets.  */
2466*e4b17023SJohn Marino   if (GET_CODE (body) == SET)
2467*e4b17023SJohn Marino     {
2468*e4b17023SJohn Marino       sets[0].src = SET_SRC (body);
2469*e4b17023SJohn Marino       sets[0].dest = SET_DEST (body);
2470*e4b17023SJohn Marino       n_sets = 1;
2471*e4b17023SJohn Marino     }
2472*e4b17023SJohn Marino   else if (GET_CODE (body) == PARALLEL)
2473*e4b17023SJohn Marino     {
2474*e4b17023SJohn Marino       /* Look through the PARALLEL and record the values being
2475*e4b17023SJohn Marino 	 set, if possible.  Also handle any CLOBBERs.  */
2476*e4b17023SJohn Marino       for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
2477*e4b17023SJohn Marino 	{
2478*e4b17023SJohn Marino 	  rtx x = XVECEXP (body, 0, i);
2479*e4b17023SJohn Marino 
2480*e4b17023SJohn Marino 	  if (GET_CODE (x) == SET)
2481*e4b17023SJohn Marino 	    {
2482*e4b17023SJohn Marino 	      sets[n_sets].src = SET_SRC (x);
2483*e4b17023SJohn Marino 	      sets[n_sets].dest = SET_DEST (x);
2484*e4b17023SJohn Marino 	      n_sets++;
2485*e4b17023SJohn Marino 	    }
2486*e4b17023SJohn Marino 	}
2487*e4b17023SJohn Marino     }
2488*e4b17023SJohn Marino 
2489*e4b17023SJohn Marino   if (n_sets == 1
2490*e4b17023SJohn Marino       && MEM_P (sets[0].src)
2491*e4b17023SJohn Marino       && !cselib_record_memory
2492*e4b17023SJohn Marino       && MEM_READONLY_P (sets[0].src))
2493*e4b17023SJohn Marino     {
2494*e4b17023SJohn Marino       rtx note = find_reg_equal_equiv_note (insn);
2495*e4b17023SJohn Marino 
2496*e4b17023SJohn Marino       if (note && CONSTANT_P (XEXP (note, 0)))
2497*e4b17023SJohn Marino 	sets[0].src = XEXP (note, 0);
2498*e4b17023SJohn Marino     }
2499*e4b17023SJohn Marino 
2500*e4b17023SJohn Marino   data.sets = sets;
2501*e4b17023SJohn Marino   data.n_sets = n_sets_before_autoinc = n_sets;
2502*e4b17023SJohn Marino   for_each_inc_dec (&insn, cselib_record_autoinc_cb, &data);
2503*e4b17023SJohn Marino   n_sets = data.n_sets;
2504*e4b17023SJohn Marino 
2505*e4b17023SJohn Marino   /* Look up the values that are read.  Do this before invalidating the
2506*e4b17023SJohn Marino      locations that are written.  */
2507*e4b17023SJohn Marino   for (i = 0; i < n_sets; i++)
2508*e4b17023SJohn Marino     {
2509*e4b17023SJohn Marino       rtx dest = sets[i].dest;
2510*e4b17023SJohn Marino 
2511*e4b17023SJohn Marino       /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2512*e4b17023SJohn Marino          the low part after invalidating any knowledge about larger modes.  */
2513*e4b17023SJohn Marino       if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
2514*e4b17023SJohn Marino 	sets[i].dest = dest = XEXP (dest, 0);
2515*e4b17023SJohn Marino 
2516*e4b17023SJohn Marino       /* We don't know how to record anything but REG or MEM.  */
2517*e4b17023SJohn Marino       if (REG_P (dest)
2518*e4b17023SJohn Marino 	  || (MEM_P (dest) && cselib_record_memory))
2519*e4b17023SJohn Marino         {
2520*e4b17023SJohn Marino 	  rtx src = sets[i].src;
2521*e4b17023SJohn Marino 	  if (cond)
2522*e4b17023SJohn Marino 	    src = gen_rtx_IF_THEN_ELSE (GET_MODE (dest), cond, src, dest);
2523*e4b17023SJohn Marino 	  sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1, VOIDmode);
2524*e4b17023SJohn Marino 	  if (MEM_P (dest))
2525*e4b17023SJohn Marino 	    {
2526*e4b17023SJohn Marino 	      enum machine_mode address_mode
2527*e4b17023SJohn Marino 		= targetm.addr_space.address_mode (MEM_ADDR_SPACE (dest));
2528*e4b17023SJohn Marino 
2529*e4b17023SJohn Marino 	      sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0),
2530*e4b17023SJohn Marino 						     address_mode, 1,
2531*e4b17023SJohn Marino 						     GET_MODE (dest));
2532*e4b17023SJohn Marino 	    }
2533*e4b17023SJohn Marino 	  else
2534*e4b17023SJohn Marino 	    sets[i].dest_addr_elt = 0;
2535*e4b17023SJohn Marino 	}
2536*e4b17023SJohn Marino     }
2537*e4b17023SJohn Marino 
2538*e4b17023SJohn Marino   if (cselib_record_sets_hook)
2539*e4b17023SJohn Marino     cselib_record_sets_hook (insn, sets, n_sets);
2540*e4b17023SJohn Marino 
2541*e4b17023SJohn Marino   /* Invalidate all locations written by this insn.  Note that the elts we
2542*e4b17023SJohn Marino      looked up in the previous loop aren't affected, just some of their
2543*e4b17023SJohn Marino      locations may go away.  */
2544*e4b17023SJohn Marino   note_stores (body, cselib_invalidate_rtx_note_stores, NULL);
2545*e4b17023SJohn Marino 
2546*e4b17023SJohn Marino   for (i = n_sets_before_autoinc; i < n_sets; i++)
2547*e4b17023SJohn Marino     cselib_invalidate_rtx (sets[i].dest);
2548*e4b17023SJohn Marino 
2549*e4b17023SJohn Marino   /* If this is an asm, look for duplicate sets.  This can happen when the
2550*e4b17023SJohn Marino      user uses the same value as an output multiple times.  This is valid
2551*e4b17023SJohn Marino      if the outputs are not actually used thereafter.  Treat this case as
2552*e4b17023SJohn Marino      if the value isn't actually set.  We do this by smashing the destination
2553*e4b17023SJohn Marino      to pc_rtx, so that we won't record the value later.  */
2554*e4b17023SJohn Marino   if (n_sets >= 2 && asm_noperands (body) >= 0)
2555*e4b17023SJohn Marino     {
2556*e4b17023SJohn Marino       for (i = 0; i < n_sets; i++)
2557*e4b17023SJohn Marino 	{
2558*e4b17023SJohn Marino 	  rtx dest = sets[i].dest;
2559*e4b17023SJohn Marino 	  if (REG_P (dest) || MEM_P (dest))
2560*e4b17023SJohn Marino 	    {
2561*e4b17023SJohn Marino 	      int j;
2562*e4b17023SJohn Marino 	      for (j = i + 1; j < n_sets; j++)
2563*e4b17023SJohn Marino 		if (rtx_equal_p (dest, sets[j].dest))
2564*e4b17023SJohn Marino 		  {
2565*e4b17023SJohn Marino 		    sets[i].dest = pc_rtx;
2566*e4b17023SJohn Marino 		    sets[j].dest = pc_rtx;
2567*e4b17023SJohn Marino 		  }
2568*e4b17023SJohn Marino 	    }
2569*e4b17023SJohn Marino 	}
2570*e4b17023SJohn Marino     }
2571*e4b17023SJohn Marino 
2572*e4b17023SJohn Marino   /* Now enter the equivalences in our tables.  */
2573*e4b17023SJohn Marino   for (i = 0; i < n_sets; i++)
2574*e4b17023SJohn Marino     {
2575*e4b17023SJohn Marino       rtx dest = sets[i].dest;
2576*e4b17023SJohn Marino       if (REG_P (dest)
2577*e4b17023SJohn Marino 	  || (MEM_P (dest) && cselib_record_memory))
2578*e4b17023SJohn Marino 	cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
2579*e4b17023SJohn Marino     }
2580*e4b17023SJohn Marino }
2581*e4b17023SJohn Marino 
2582*e4b17023SJohn Marino /* Record the effects of INSN.  */
2583*e4b17023SJohn Marino 
2584*e4b17023SJohn Marino void
cselib_process_insn(rtx insn)2585*e4b17023SJohn Marino cselib_process_insn (rtx insn)
2586*e4b17023SJohn Marino {
2587*e4b17023SJohn Marino   int i;
2588*e4b17023SJohn Marino   rtx x;
2589*e4b17023SJohn Marino 
2590*e4b17023SJohn Marino   cselib_current_insn = insn;
2591*e4b17023SJohn Marino 
2592*e4b17023SJohn Marino   /* Forget everything at a CODE_LABEL, a volatile asm, or a setjmp.  */
2593*e4b17023SJohn Marino   if (LABEL_P (insn)
2594*e4b17023SJohn Marino       || (CALL_P (insn)
2595*e4b17023SJohn Marino 	  && find_reg_note (insn, REG_SETJMP, NULL))
2596*e4b17023SJohn Marino       || (NONJUMP_INSN_P (insn)
2597*e4b17023SJohn Marino 	  && GET_CODE (PATTERN (insn)) == ASM_OPERANDS
2598*e4b17023SJohn Marino 	  && MEM_VOLATILE_P (PATTERN (insn))))
2599*e4b17023SJohn Marino     {
2600*e4b17023SJohn Marino       cselib_reset_table (next_uid);
2601*e4b17023SJohn Marino       cselib_current_insn = NULL_RTX;
2602*e4b17023SJohn Marino       return;
2603*e4b17023SJohn Marino     }
2604*e4b17023SJohn Marino 
2605*e4b17023SJohn Marino   if (! INSN_P (insn))
2606*e4b17023SJohn Marino     {
2607*e4b17023SJohn Marino       cselib_current_insn = NULL_RTX;
2608*e4b17023SJohn Marino       return;
2609*e4b17023SJohn Marino     }
2610*e4b17023SJohn Marino 
2611*e4b17023SJohn Marino   /* If this is a call instruction, forget anything stored in a
2612*e4b17023SJohn Marino      call clobbered register, or, if this is not a const call, in
2613*e4b17023SJohn Marino      memory.  */
2614*e4b17023SJohn Marino   if (CALL_P (insn))
2615*e4b17023SJohn Marino     {
2616*e4b17023SJohn Marino       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2617*e4b17023SJohn Marino 	if (call_used_regs[i]
2618*e4b17023SJohn Marino 	    || (REG_VALUES (i) && REG_VALUES (i)->elt
2619*e4b17023SJohn Marino 		&& HARD_REGNO_CALL_PART_CLOBBERED (i,
2620*e4b17023SJohn Marino 		      GET_MODE (REG_VALUES (i)->elt->val_rtx))))
2621*e4b17023SJohn Marino 	  cselib_invalidate_regno (i, reg_raw_mode[i]);
2622*e4b17023SJohn Marino 
2623*e4b17023SJohn Marino       /* Since it is not clear how cselib is going to be used, be
2624*e4b17023SJohn Marino 	 conservative here and treat looping pure or const functions
2625*e4b17023SJohn Marino 	 as if they were regular functions.  */
2626*e4b17023SJohn Marino       if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
2627*e4b17023SJohn Marino 	  || !(RTL_CONST_OR_PURE_CALL_P (insn)))
2628*e4b17023SJohn Marino 	cselib_invalidate_mem (callmem);
2629*e4b17023SJohn Marino     }
2630*e4b17023SJohn Marino 
2631*e4b17023SJohn Marino   cselib_record_sets (insn);
2632*e4b17023SJohn Marino 
2633*e4b17023SJohn Marino   /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
2634*e4b17023SJohn Marino      after we have processed the insn.  */
2635*e4b17023SJohn Marino   if (CALL_P (insn))
2636*e4b17023SJohn Marino     for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
2637*e4b17023SJohn Marino       if (GET_CODE (XEXP (x, 0)) == CLOBBER)
2638*e4b17023SJohn Marino 	cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0));
2639*e4b17023SJohn Marino 
2640*e4b17023SJohn Marino   cselib_current_insn = NULL_RTX;
2641*e4b17023SJohn Marino 
2642*e4b17023SJohn Marino   if (n_useless_values > MAX_USELESS_VALUES
2643*e4b17023SJohn Marino       /* remove_useless_values is linear in the hash table size.  Avoid
2644*e4b17023SJohn Marino          quadratic behavior for very large hashtables with very few
2645*e4b17023SJohn Marino 	 useless elements.  */
2646*e4b17023SJohn Marino       && ((unsigned int)n_useless_values
2647*e4b17023SJohn Marino 	  > (cselib_hash_table->n_elements
2648*e4b17023SJohn Marino 	     - cselib_hash_table->n_deleted
2649*e4b17023SJohn Marino 	     - n_debug_values) / 4))
2650*e4b17023SJohn Marino     remove_useless_values ();
2651*e4b17023SJohn Marino }
2652*e4b17023SJohn Marino 
2653*e4b17023SJohn Marino /* Initialize cselib for one pass.  The caller must also call
2654*e4b17023SJohn Marino    init_alias_analysis.  */
2655*e4b17023SJohn Marino 
2656*e4b17023SJohn Marino void
cselib_init(int record_what)2657*e4b17023SJohn Marino cselib_init (int record_what)
2658*e4b17023SJohn Marino {
2659*e4b17023SJohn Marino   elt_list_pool = create_alloc_pool ("elt_list",
2660*e4b17023SJohn Marino 				     sizeof (struct elt_list), 10);
2661*e4b17023SJohn Marino   elt_loc_list_pool = create_alloc_pool ("elt_loc_list",
2662*e4b17023SJohn Marino 				         sizeof (struct elt_loc_list), 10);
2663*e4b17023SJohn Marino   cselib_val_pool = create_alloc_pool ("cselib_val_list",
2664*e4b17023SJohn Marino 				       sizeof (cselib_val), 10);
2665*e4b17023SJohn Marino   value_pool = create_alloc_pool ("value", RTX_CODE_SIZE (VALUE), 100);
2666*e4b17023SJohn Marino   cselib_record_memory = record_what & CSELIB_RECORD_MEMORY;
2667*e4b17023SJohn Marino   cselib_preserve_constants = record_what & CSELIB_PRESERVE_CONSTANTS;
2668*e4b17023SJohn Marino   cselib_any_perm_equivs = false;
2669*e4b17023SJohn Marino 
2670*e4b17023SJohn Marino   /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
2671*e4b17023SJohn Marino      see canon_true_dependence.  This is only created once.  */
2672*e4b17023SJohn Marino   if (! callmem)
2673*e4b17023SJohn Marino     callmem = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
2674*e4b17023SJohn Marino 
2675*e4b17023SJohn Marino   cselib_nregs = max_reg_num ();
2676*e4b17023SJohn Marino 
2677*e4b17023SJohn Marino   /* We preserve reg_values to allow expensive clearing of the whole thing.
2678*e4b17023SJohn Marino      Reallocate it however if it happens to be too large.  */
2679*e4b17023SJohn Marino   if (!reg_values || reg_values_size < cselib_nregs
2680*e4b17023SJohn Marino       || (reg_values_size > 10 && reg_values_size > cselib_nregs * 4))
2681*e4b17023SJohn Marino     {
2682*e4b17023SJohn Marino       free (reg_values);
2683*e4b17023SJohn Marino       /* Some space for newly emit instructions so we don't end up
2684*e4b17023SJohn Marino 	 reallocating in between passes.  */
2685*e4b17023SJohn Marino       reg_values_size = cselib_nregs + (63 + cselib_nregs) / 16;
2686*e4b17023SJohn Marino       reg_values = XCNEWVEC (struct elt_list *, reg_values_size);
2687*e4b17023SJohn Marino     }
2688*e4b17023SJohn Marino   used_regs = XNEWVEC (unsigned int, cselib_nregs);
2689*e4b17023SJohn Marino   n_used_regs = 0;
2690*e4b17023SJohn Marino   cselib_hash_table = htab_create (31, get_value_hash,
2691*e4b17023SJohn Marino 				   entry_and_rtx_equal_p, NULL);
2692*e4b17023SJohn Marino   next_uid = 1;
2693*e4b17023SJohn Marino }
2694*e4b17023SJohn Marino 
2695*e4b17023SJohn Marino /* Called when the current user is done with cselib.  */
2696*e4b17023SJohn Marino 
2697*e4b17023SJohn Marino void
cselib_finish(void)2698*e4b17023SJohn Marino cselib_finish (void)
2699*e4b17023SJohn Marino {
2700*e4b17023SJohn Marino   cselib_discard_hook = NULL;
2701*e4b17023SJohn Marino   cselib_preserve_constants = false;
2702*e4b17023SJohn Marino   cselib_any_perm_equivs = false;
2703*e4b17023SJohn Marino   cfa_base_preserved_val = NULL;
2704*e4b17023SJohn Marino   cfa_base_preserved_regno = INVALID_REGNUM;
2705*e4b17023SJohn Marino   free_alloc_pool (elt_list_pool);
2706*e4b17023SJohn Marino   free_alloc_pool (elt_loc_list_pool);
2707*e4b17023SJohn Marino   free_alloc_pool (cselib_val_pool);
2708*e4b17023SJohn Marino   free_alloc_pool (value_pool);
2709*e4b17023SJohn Marino   cselib_clear_table ();
2710*e4b17023SJohn Marino   htab_delete (cselib_hash_table);
2711*e4b17023SJohn Marino   free (used_regs);
2712*e4b17023SJohn Marino   used_regs = 0;
2713*e4b17023SJohn Marino   cselib_hash_table = 0;
2714*e4b17023SJohn Marino   n_useless_values = 0;
2715*e4b17023SJohn Marino   n_useless_debug_values = 0;
2716*e4b17023SJohn Marino   n_debug_values = 0;
2717*e4b17023SJohn Marino   next_uid = 0;
2718*e4b17023SJohn Marino }
2719*e4b17023SJohn Marino 
2720*e4b17023SJohn Marino /* Dump the cselib_val *X to FILE *info.  */
2721*e4b17023SJohn Marino 
2722*e4b17023SJohn Marino static int
dump_cselib_val(void ** x,void * info)2723*e4b17023SJohn Marino dump_cselib_val (void **x, void *info)
2724*e4b17023SJohn Marino {
2725*e4b17023SJohn Marino   cselib_val *v = (cselib_val *)*x;
2726*e4b17023SJohn Marino   FILE *out = (FILE *)info;
2727*e4b17023SJohn Marino   bool need_lf = true;
2728*e4b17023SJohn Marino 
2729*e4b17023SJohn Marino   print_inline_rtx (out, v->val_rtx, 0);
2730*e4b17023SJohn Marino 
2731*e4b17023SJohn Marino   if (v->locs)
2732*e4b17023SJohn Marino     {
2733*e4b17023SJohn Marino       struct elt_loc_list *l = v->locs;
2734*e4b17023SJohn Marino       if (need_lf)
2735*e4b17023SJohn Marino 	{
2736*e4b17023SJohn Marino 	  fputc ('\n', out);
2737*e4b17023SJohn Marino 	  need_lf = false;
2738*e4b17023SJohn Marino 	}
2739*e4b17023SJohn Marino       fputs (" locs:", out);
2740*e4b17023SJohn Marino       do
2741*e4b17023SJohn Marino 	{
2742*e4b17023SJohn Marino 	  if (l->setting_insn)
2743*e4b17023SJohn Marino 	    fprintf (out, "\n  from insn %i ",
2744*e4b17023SJohn Marino 		     INSN_UID (l->setting_insn));
2745*e4b17023SJohn Marino 	  else
2746*e4b17023SJohn Marino 	    fprintf (out, "\n   ");
2747*e4b17023SJohn Marino 	  print_inline_rtx (out, l->loc, 4);
2748*e4b17023SJohn Marino 	}
2749*e4b17023SJohn Marino       while ((l = l->next));
2750*e4b17023SJohn Marino       fputc ('\n', out);
2751*e4b17023SJohn Marino     }
2752*e4b17023SJohn Marino   else
2753*e4b17023SJohn Marino     {
2754*e4b17023SJohn Marino       fputs (" no locs", out);
2755*e4b17023SJohn Marino       need_lf = true;
2756*e4b17023SJohn Marino     }
2757*e4b17023SJohn Marino 
2758*e4b17023SJohn Marino   if (v->addr_list)
2759*e4b17023SJohn Marino     {
2760*e4b17023SJohn Marino       struct elt_list *e = v->addr_list;
2761*e4b17023SJohn Marino       if (need_lf)
2762*e4b17023SJohn Marino 	{
2763*e4b17023SJohn Marino 	  fputc ('\n', out);
2764*e4b17023SJohn Marino 	  need_lf = false;
2765*e4b17023SJohn Marino 	}
2766*e4b17023SJohn Marino       fputs (" addr list:", out);
2767*e4b17023SJohn Marino       do
2768*e4b17023SJohn Marino 	{
2769*e4b17023SJohn Marino 	  fputs ("\n  ", out);
2770*e4b17023SJohn Marino 	  print_inline_rtx (out, e->elt->val_rtx, 2);
2771*e4b17023SJohn Marino 	}
2772*e4b17023SJohn Marino       while ((e = e->next));
2773*e4b17023SJohn Marino       fputc ('\n', out);
2774*e4b17023SJohn Marino     }
2775*e4b17023SJohn Marino   else
2776*e4b17023SJohn Marino     {
2777*e4b17023SJohn Marino       fputs (" no addrs", out);
2778*e4b17023SJohn Marino       need_lf = true;
2779*e4b17023SJohn Marino     }
2780*e4b17023SJohn Marino 
2781*e4b17023SJohn Marino   if (v->next_containing_mem == &dummy_val)
2782*e4b17023SJohn Marino     fputs (" last mem\n", out);
2783*e4b17023SJohn Marino   else if (v->next_containing_mem)
2784*e4b17023SJohn Marino     {
2785*e4b17023SJohn Marino       fputs (" next mem ", out);
2786*e4b17023SJohn Marino       print_inline_rtx (out, v->next_containing_mem->val_rtx, 2);
2787*e4b17023SJohn Marino       fputc ('\n', out);
2788*e4b17023SJohn Marino     }
2789*e4b17023SJohn Marino   else if (need_lf)
2790*e4b17023SJohn Marino     fputc ('\n', out);
2791*e4b17023SJohn Marino 
2792*e4b17023SJohn Marino   return 1;
2793*e4b17023SJohn Marino }
2794*e4b17023SJohn Marino 
2795*e4b17023SJohn Marino /* Dump to OUT everything in the CSELIB table.  */
2796*e4b17023SJohn Marino 
2797*e4b17023SJohn Marino void
dump_cselib_table(FILE * out)2798*e4b17023SJohn Marino dump_cselib_table (FILE *out)
2799*e4b17023SJohn Marino {
2800*e4b17023SJohn Marino   fprintf (out, "cselib hash table:\n");
2801*e4b17023SJohn Marino   htab_traverse (cselib_hash_table, dump_cselib_val, out);
2802*e4b17023SJohn Marino   if (first_containing_mem != &dummy_val)
2803*e4b17023SJohn Marino     {
2804*e4b17023SJohn Marino       fputs ("first mem ", out);
2805*e4b17023SJohn Marino       print_inline_rtx (out, first_containing_mem->val_rtx, 2);
2806*e4b17023SJohn Marino       fputc ('\n', out);
2807*e4b17023SJohn Marino     }
2808*e4b17023SJohn Marino   fprintf (out, "next uid %i\n", next_uid);
2809*e4b17023SJohn Marino }
2810*e4b17023SJohn Marino 
2811*e4b17023SJohn Marino #include "gt-cselib.h"
2812