1*e4b17023SJohn Marino /* Routines for performing Temporary Expression Replacement (TER) in SSA trees.
2*e4b17023SJohn Marino    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
3*e4b17023SJohn Marino    Free Software Foundation, Inc.
4*e4b17023SJohn Marino    Contributed by Andrew MacLeod  <amacleod@redhat.com>
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
9*e4b17023SJohn Marino it under the terms of the GNU General Public License as published by
10*e4b17023SJohn Marino the Free Software Foundation; either version 3, or (at your option)
11*e4b17023SJohn Marino any later version.
12*e4b17023SJohn Marino 
13*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful,
14*e4b17023SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
15*e4b17023SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*e4b17023SJohn Marino GNU General Public License 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 
23*e4b17023SJohn Marino #include "config.h"
24*e4b17023SJohn Marino #include "system.h"
25*e4b17023SJohn Marino #include "coretypes.h"
26*e4b17023SJohn Marino #include "tm.h"
27*e4b17023SJohn Marino #include "tree.h"
28*e4b17023SJohn Marino #include "tree-pretty-print.h"
29*e4b17023SJohn Marino #include "gimple-pretty-print.h"
30*e4b17023SJohn Marino #include "bitmap.h"
31*e4b17023SJohn Marino #include "tree-flow.h"
32*e4b17023SJohn Marino #include "tree-dump.h"
33*e4b17023SJohn Marino #include "tree-ssa-live.h"
34*e4b17023SJohn Marino #include "flags.h"
35*e4b17023SJohn Marino 
36*e4b17023SJohn Marino 
37*e4b17023SJohn Marino /* Temporary Expression Replacement (TER)
38*e4b17023SJohn Marino 
39*e4b17023SJohn Marino    Replace SSA version variables during out-of-ssa with their defining
40*e4b17023SJohn Marino    expression if there is only one use of the variable.
41*e4b17023SJohn Marino 
42*e4b17023SJohn Marino    This pass is required in order for the RTL expansion pass to see larger
43*e4b17023SJohn Marino    chunks of code.  This allows it to make better choices on RTL pattern
44*e4b17023SJohn Marino    selection.  When expand is rewritten and merged with out-of-ssa, and
45*e4b17023SJohn Marino    understands SSA, this should be eliminated.
46*e4b17023SJohn Marino 
47*e4b17023SJohn Marino    A pass is made through the function, one block at a time.  No cross block
48*e4b17023SJohn Marino    information is tracked.
49*e4b17023SJohn Marino 
50*e4b17023SJohn Marino    Variables which only have one use, and whose defining stmt is considered
51*e4b17023SJohn Marino    a replaceable expression (see is_replaceable_p) are tracked to see whether
52*e4b17023SJohn Marino    they can be replaced at their use location.
53*e4b17023SJohn Marino 
54*e4b17023SJohn Marino    n_12 = C * 10
55*e4b17023SJohn Marino    a_2 = b_5 + 6
56*e4b17023SJohn Marino    v_9 = a_2 * n_12
57*e4b17023SJohn Marino 
58*e4b17023SJohn Marino    if there are the only use of n_12 and a_2, TER will substitute in their
59*e4b17023SJohn Marino    expressions in v_9, and end up with:
60*e4b17023SJohn Marino 
61*e4b17023SJohn Marino    v_9 = (b_5 + 6) * (C * 10)
62*e4b17023SJohn Marino 
63*e4b17023SJohn Marino    which will then have the ssa_name assigned to regular variables, and the
64*e4b17023SJohn Marino    resulting code which will be passed to the expander looks something like:
65*e4b17023SJohn Marino 
66*e4b17023SJohn Marino    v = (b + 6) * (C * 10)
67*e4b17023SJohn Marino 
68*e4b17023SJohn Marino 
69*e4b17023SJohn Marino    This requires ensuring that none of the variables used by the expression
70*e4b17023SJohn Marino    change between the def point and where it is used.  Furthermore, if any
71*e4b17023SJohn Marino    of the ssa_names used in this expression are themselves replaceable, we
72*e4b17023SJohn Marino    have to ensure none of that expressions' arguments change either.
73*e4b17023SJohn Marino    Although SSA_NAMES themselves don't change, this pass is performed after
74*e4b17023SJohn Marino    coalescing has coalesced different SSA_NAMES together, so there could be a
75*e4b17023SJohn Marino    definition of an SSA_NAME which is coalesced with a use that causes a
76*e4b17023SJohn Marino    problem, i.e.,
77*e4b17023SJohn Marino 
78*e4b17023SJohn Marino    PHI b_5 = <b_8(2), b_14(1)>
79*e4b17023SJohn Marino    <...>
80*e4b17023SJohn Marino    a_2 = b_5 + 6
81*e4b17023SJohn Marino    b_8 = c_4 + 4
82*e4b17023SJohn Marino    v_9 = a_2 * n_12
83*e4b17023SJohn Marino    <...>
84*e4b17023SJohn Marino 
85*e4b17023SJohn Marino    If b_5, b_8 and b_14 are all coalesced together...
86*e4b17023SJohn Marino    The expression b_5 + 6 CANNOT replace the use in the statement defining v_9
87*e4b17023SJohn Marino    because b_8 is in fact killing the value of b_5 since they share a partition
88*e4b17023SJohn Marino    and will be assigned the same memory or register location.
89*e4b17023SJohn Marino 
90*e4b17023SJohn Marino    TER implements this but stepping through the instructions in a block and
91*e4b17023SJohn Marino    tracking potential expressions for replacement, and the partitions they are
92*e4b17023SJohn Marino    dependent on.  Expressions are represented by the SSA_NAME_VERSION of the
93*e4b17023SJohn Marino    DEF on the LHS of a GIMPLE_ASSIGN and the expression is the RHS.
94*e4b17023SJohn Marino 
95*e4b17023SJohn Marino    When a stmt is determined to be a possible replacement expression, the
96*e4b17023SJohn Marino    following steps are taken:
97*e4b17023SJohn Marino 
98*e4b17023SJohn Marino    EXPR_DECL_UID bitmap is allocated and set to the base variable UID of the
99*e4b17023SJohn Marino    def and any uses in the expression.  non-NULL means the expression is being
100*e4b17023SJohn Marino    tracked.  The UID's themselves are used to prevent TER substitution into
101*e4b17023SJohn Marino    accumulating sequences, i.e.,
102*e4b17023SJohn Marino 
103*e4b17023SJohn Marino    x = x + y
104*e4b17023SJohn Marino    x = x + z
105*e4b17023SJohn Marino    x = x + w
106*e4b17023SJohn Marino    etc.
107*e4b17023SJohn Marino    this can result in very large expressions which don't accomplish anything
108*e4b17023SJohn Marino    see PR tree-optimization/17549.
109*e4b17023SJohn Marino 
110*e4b17023SJohn Marino    PARTITION_DEPENDENCIES is another bitmap array, and it has a bit set for any
111*e4b17023SJohn Marino    partition which is used in the expression.  This is primarily used to remove
112*e4b17023SJohn Marino    an expression from the partition kill lists when a decision is made whether
113*e4b17023SJohn Marino    to replace it or not.  This is indexed by ssa version number as well, and
114*e4b17023SJohn Marino    indicates a partition number.  virtual operands are not tracked individually,
115*e4b17023SJohn Marino    but they are summarized by an artificial partition called VIRTUAL_PARTITION.
116*e4b17023SJohn Marino    This means a MAY or MUST def will kill *ALL* expressions that are dependent
117*e4b17023SJohn Marino    on a virtual operand.
118*e4b17023SJohn Marino    Note that the EXPR_DECL_UID and this bitmap represent very similar
119*e4b17023SJohn Marino    information, but the info in one is not easy to obtain from the other.
120*e4b17023SJohn Marino 
121*e4b17023SJohn Marino    KILL_LIST is yet another bitmap array, this time it is indexed by partition
122*e4b17023SJohn Marino    number, and represents a list of active expressions which will will no
123*e4b17023SJohn Marino    longer be valid if a definition into this partition takes place.
124*e4b17023SJohn Marino 
125*e4b17023SJohn Marino    PARTITION_IN_USE is simply a bitmap which is used to track which partitions
126*e4b17023SJohn Marino    currently have something in their kill list.  This is used at the end of
127*e4b17023SJohn Marino    a block to clear out the KILL_LIST bitmaps at the end of each block.
128*e4b17023SJohn Marino 
129*e4b17023SJohn Marino    NEW_REPLACEABLE_DEPENDENCIES is used as a temporary place to store
130*e4b17023SJohn Marino    dependencies which will be reused by the current definition. All the uses
131*e4b17023SJohn Marino    on an expression are processed before anything else is done. If a use is
132*e4b17023SJohn Marino    determined to be a replaceable expression AND the current stmt is also going
133*e4b17023SJohn Marino    to be replaceable, all the dependencies of this replaceable use will be
134*e4b17023SJohn Marino    picked up by the current stmt's expression. Rather than recreate them, they
135*e4b17023SJohn Marino    are simply copied here and then copied into the new expression when it is
136*e4b17023SJohn Marino    processed.
137*e4b17023SJohn Marino 
138*e4b17023SJohn Marino    a_2 = b_5 + 6
139*e4b17023SJohn Marino    v_8 = a_2 + c_4
140*e4b17023SJohn Marino 
141*e4b17023SJohn Marino    a_2's expression 'b_5 + 6' is determined to be replaceable at the use
142*e4b17023SJohn Marino    location. It is dependent on the partition 'b_5' is in. This is cached into
143*e4b17023SJohn Marino    the NEW_REPLACEABLE_DEPENDENCIES bitmap, and when v_8 is examined for
144*e4b17023SJohn Marino    replaceability, it is a candidate, and it is dependent on the partition
145*e4b17023SJohn Marino    b_5 is in *NOT* a_2, as well as c_4's partition.
146*e4b17023SJohn Marino 
147*e4b17023SJohn Marino    if v_8 is also replaceable:
148*e4b17023SJohn Marino 
149*e4b17023SJohn Marino    x_9 = v_8 * 5
150*e4b17023SJohn Marino 
151*e4b17023SJohn Marino    x_9 is dependent on partitions b_5, and c_4
152*e4b17023SJohn Marino 
153*e4b17023SJohn Marino    if a statement is found which has either of those partitions written to
154*e4b17023SJohn Marino    before x_9 is used, then x_9 itself is NOT replaceable.  */
155*e4b17023SJohn Marino 
156*e4b17023SJohn Marino 
157*e4b17023SJohn Marino /* Temporary Expression Replacement (TER) table information.  */
158*e4b17023SJohn Marino 
159*e4b17023SJohn Marino typedef struct temp_expr_table_d
160*e4b17023SJohn Marino {
161*e4b17023SJohn Marino   var_map map;
162*e4b17023SJohn Marino   bitmap *partition_dependencies;	/* Partitions expr is dependent on.  */
163*e4b17023SJohn Marino   bitmap replaceable_expressions;	/* Replacement expression table.  */
164*e4b17023SJohn Marino   bitmap *expr_decl_uids;		/* Base uids of exprs.  */
165*e4b17023SJohn Marino   bitmap *kill_list;			/* Expr's killed by a partition.  */
166*e4b17023SJohn Marino   int virtual_partition;		/* Pseudo partition for virtual ops.  */
167*e4b17023SJohn Marino   bitmap partition_in_use;		/* Partitions with kill entries.  */
168*e4b17023SJohn Marino   bitmap new_replaceable_dependencies;	/* Holding place for pending dep's.  */
169*e4b17023SJohn Marino   int *num_in_part;			/* # of ssa_names in a partition.  */
170*e4b17023SJohn Marino   int *call_cnt;			/* Call count at definition.  */
171*e4b17023SJohn Marino } *temp_expr_table_p;
172*e4b17023SJohn Marino 
173*e4b17023SJohn Marino /* Used to indicate a dependency on VDEFs.  */
174*e4b17023SJohn Marino #define VIRTUAL_PARTITION(table)	(table->virtual_partition)
175*e4b17023SJohn Marino 
176*e4b17023SJohn Marino #ifdef ENABLE_CHECKING
177*e4b17023SJohn Marino extern void debug_ter (FILE *, temp_expr_table_p);
178*e4b17023SJohn Marino #endif
179*e4b17023SJohn Marino 
180*e4b17023SJohn Marino 
181*e4b17023SJohn Marino /* Create a new TER table for MAP.  */
182*e4b17023SJohn Marino 
183*e4b17023SJohn Marino static temp_expr_table_p
new_temp_expr_table(var_map map)184*e4b17023SJohn Marino new_temp_expr_table (var_map map)
185*e4b17023SJohn Marino {
186*e4b17023SJohn Marino   temp_expr_table_p t;
187*e4b17023SJohn Marino   unsigned x;
188*e4b17023SJohn Marino 
189*e4b17023SJohn Marino   t = XNEW (struct temp_expr_table_d);
190*e4b17023SJohn Marino   t->map = map;
191*e4b17023SJohn Marino 
192*e4b17023SJohn Marino   t->partition_dependencies = XCNEWVEC (bitmap, num_ssa_names + 1);
193*e4b17023SJohn Marino   t->expr_decl_uids = XCNEWVEC (bitmap, num_ssa_names + 1);
194*e4b17023SJohn Marino   t->kill_list = XCNEWVEC (bitmap, num_var_partitions (map) + 1);
195*e4b17023SJohn Marino 
196*e4b17023SJohn Marino   t->partition_in_use = BITMAP_ALLOC (NULL);
197*e4b17023SJohn Marino 
198*e4b17023SJohn Marino   t->virtual_partition = num_var_partitions (map);
199*e4b17023SJohn Marino   t->new_replaceable_dependencies = BITMAP_ALLOC (NULL);
200*e4b17023SJohn Marino 
201*e4b17023SJohn Marino   t->replaceable_expressions = NULL;
202*e4b17023SJohn Marino   t->num_in_part = XCNEWVEC (int, num_var_partitions (map));
203*e4b17023SJohn Marino   for (x = 1; x < num_ssa_names; x++)
204*e4b17023SJohn Marino     {
205*e4b17023SJohn Marino       int p;
206*e4b17023SJohn Marino       tree name = ssa_name (x);
207*e4b17023SJohn Marino       if (!name)
208*e4b17023SJohn Marino         continue;
209*e4b17023SJohn Marino       p = var_to_partition (map, name);
210*e4b17023SJohn Marino       if (p != NO_PARTITION)
211*e4b17023SJohn Marino         t->num_in_part[p]++;
212*e4b17023SJohn Marino     }
213*e4b17023SJohn Marino   t->call_cnt = XCNEWVEC (int, num_ssa_names + 1);
214*e4b17023SJohn Marino 
215*e4b17023SJohn Marino   return t;
216*e4b17023SJohn Marino }
217*e4b17023SJohn Marino 
218*e4b17023SJohn Marino 
219*e4b17023SJohn Marino /* Free TER table T.  If there are valid replacements, return the expression
220*e4b17023SJohn Marino    vector.  */
221*e4b17023SJohn Marino 
222*e4b17023SJohn Marino static bitmap
free_temp_expr_table(temp_expr_table_p t)223*e4b17023SJohn Marino free_temp_expr_table (temp_expr_table_p t)
224*e4b17023SJohn Marino {
225*e4b17023SJohn Marino   bitmap ret = NULL;
226*e4b17023SJohn Marino 
227*e4b17023SJohn Marino #ifdef ENABLE_CHECKING
228*e4b17023SJohn Marino   unsigned x;
229*e4b17023SJohn Marino   for (x = 0; x <= num_var_partitions (t->map); x++)
230*e4b17023SJohn Marino     gcc_assert (!t->kill_list[x]);
231*e4b17023SJohn Marino   for (x = 0; x < num_ssa_names; x++)
232*e4b17023SJohn Marino     {
233*e4b17023SJohn Marino       gcc_assert (t->expr_decl_uids[x] == NULL);
234*e4b17023SJohn Marino       gcc_assert (t->partition_dependencies[x] == NULL);
235*e4b17023SJohn Marino     }
236*e4b17023SJohn Marino #endif
237*e4b17023SJohn Marino 
238*e4b17023SJohn Marino   BITMAP_FREE (t->partition_in_use);
239*e4b17023SJohn Marino   BITMAP_FREE (t->new_replaceable_dependencies);
240*e4b17023SJohn Marino 
241*e4b17023SJohn Marino   free (t->expr_decl_uids);
242*e4b17023SJohn Marino   free (t->kill_list);
243*e4b17023SJohn Marino   free (t->partition_dependencies);
244*e4b17023SJohn Marino   free (t->num_in_part);
245*e4b17023SJohn Marino   free (t->call_cnt);
246*e4b17023SJohn Marino 
247*e4b17023SJohn Marino   if (t->replaceable_expressions)
248*e4b17023SJohn Marino     ret = t->replaceable_expressions;
249*e4b17023SJohn Marino 
250*e4b17023SJohn Marino   free (t);
251*e4b17023SJohn Marino   return ret;
252*e4b17023SJohn Marino }
253*e4b17023SJohn Marino 
254*e4b17023SJohn Marino 
255*e4b17023SJohn Marino /* Return TRUE if VERSION is to be replaced by an expression in TAB.  */
256*e4b17023SJohn Marino 
257*e4b17023SJohn Marino static inline bool
version_to_be_replaced_p(temp_expr_table_p tab,int version)258*e4b17023SJohn Marino version_to_be_replaced_p (temp_expr_table_p tab, int version)
259*e4b17023SJohn Marino {
260*e4b17023SJohn Marino   if (!tab->replaceable_expressions)
261*e4b17023SJohn Marino     return false;
262*e4b17023SJohn Marino   return bitmap_bit_p (tab->replaceable_expressions, version);
263*e4b17023SJohn Marino }
264*e4b17023SJohn Marino 
265*e4b17023SJohn Marino 
266*e4b17023SJohn Marino /* Add partition P to the list if partitions VERSION is dependent on.  TAB is
267*e4b17023SJohn Marino    the expression table */
268*e4b17023SJohn Marino 
269*e4b17023SJohn Marino static inline void
make_dependent_on_partition(temp_expr_table_p tab,int version,int p)270*e4b17023SJohn Marino make_dependent_on_partition (temp_expr_table_p tab, int version, int p)
271*e4b17023SJohn Marino {
272*e4b17023SJohn Marino   if (!tab->partition_dependencies[version])
273*e4b17023SJohn Marino     tab->partition_dependencies[version] = BITMAP_ALLOC (NULL);
274*e4b17023SJohn Marino 
275*e4b17023SJohn Marino   bitmap_set_bit (tab->partition_dependencies[version], p);
276*e4b17023SJohn Marino }
277*e4b17023SJohn Marino 
278*e4b17023SJohn Marino 
279*e4b17023SJohn Marino /* Add VER to the kill list for P.  TAB is the expression table */
280*e4b17023SJohn Marino 
281*e4b17023SJohn Marino static inline void
add_to_partition_kill_list(temp_expr_table_p tab,int p,int ver)282*e4b17023SJohn Marino add_to_partition_kill_list (temp_expr_table_p tab, int p, int ver)
283*e4b17023SJohn Marino {
284*e4b17023SJohn Marino   if (!tab->kill_list[p])
285*e4b17023SJohn Marino     {
286*e4b17023SJohn Marino       tab->kill_list[p] = BITMAP_ALLOC (NULL);
287*e4b17023SJohn Marino       bitmap_set_bit (tab->partition_in_use, p);
288*e4b17023SJohn Marino     }
289*e4b17023SJohn Marino   bitmap_set_bit (tab->kill_list[p], ver);
290*e4b17023SJohn Marino }
291*e4b17023SJohn Marino 
292*e4b17023SJohn Marino 
293*e4b17023SJohn Marino /* Remove VER from the partition kill list for P.  TAB is the expression
294*e4b17023SJohn Marino    table.  */
295*e4b17023SJohn Marino 
296*e4b17023SJohn Marino static inline void
remove_from_partition_kill_list(temp_expr_table_p tab,int p,int version)297*e4b17023SJohn Marino remove_from_partition_kill_list (temp_expr_table_p tab, int p, int version)
298*e4b17023SJohn Marino {
299*e4b17023SJohn Marino   gcc_checking_assert (tab->kill_list[p]);
300*e4b17023SJohn Marino   bitmap_clear_bit (tab->kill_list[p], version);
301*e4b17023SJohn Marino   if (bitmap_empty_p (tab->kill_list[p]))
302*e4b17023SJohn Marino     {
303*e4b17023SJohn Marino       bitmap_clear_bit (tab->partition_in_use, p);
304*e4b17023SJohn Marino       BITMAP_FREE (tab->kill_list[p]);
305*e4b17023SJohn Marino     }
306*e4b17023SJohn Marino }
307*e4b17023SJohn Marino 
308*e4b17023SJohn Marino 
309*e4b17023SJohn Marino /* Add a dependency between the def of ssa VERSION and VAR.  If VAR is
310*e4b17023SJohn Marino    replaceable by an expression, add a dependence each of the elements of the
311*e4b17023SJohn Marino    expression.  These are contained in the new_replaceable list.  TAB is the
312*e4b17023SJohn Marino    expression table.  */
313*e4b17023SJohn Marino 
314*e4b17023SJohn Marino static void
add_dependence(temp_expr_table_p tab,int version,tree var)315*e4b17023SJohn Marino add_dependence (temp_expr_table_p tab, int version, tree var)
316*e4b17023SJohn Marino {
317*e4b17023SJohn Marino   int i;
318*e4b17023SJohn Marino   bitmap_iterator bi;
319*e4b17023SJohn Marino   unsigned x;
320*e4b17023SJohn Marino 
321*e4b17023SJohn Marino   i = SSA_NAME_VERSION (var);
322*e4b17023SJohn Marino   if (version_to_be_replaced_p (tab, i))
323*e4b17023SJohn Marino     {
324*e4b17023SJohn Marino       if (!bitmap_empty_p (tab->new_replaceable_dependencies))
325*e4b17023SJohn Marino         {
326*e4b17023SJohn Marino 	  /* Version will now be killed by a write to any partition the
327*e4b17023SJohn Marino 	     substituted expression would have been killed by.  */
328*e4b17023SJohn Marino 	  EXECUTE_IF_SET_IN_BITMAP (tab->new_replaceable_dependencies, 0, x, bi)
329*e4b17023SJohn Marino 	    add_to_partition_kill_list (tab, x, version);
330*e4b17023SJohn Marino 
331*e4b17023SJohn Marino 	  /* Rather than set partition_dependencies and in_use lists bit by
332*e4b17023SJohn Marino 	     bit, simply OR in the new_replaceable_dependencies bits.  */
333*e4b17023SJohn Marino 	  if (!tab->partition_dependencies[version])
334*e4b17023SJohn Marino 	    tab->partition_dependencies[version] = BITMAP_ALLOC (NULL);
335*e4b17023SJohn Marino 	  bitmap_ior_into (tab->partition_dependencies[version],
336*e4b17023SJohn Marino 			   tab->new_replaceable_dependencies);
337*e4b17023SJohn Marino 	  bitmap_ior_into (tab->partition_in_use,
338*e4b17023SJohn Marino 			   tab->new_replaceable_dependencies);
339*e4b17023SJohn Marino 	  /* It is only necessary to add these once.  */
340*e4b17023SJohn Marino 	  bitmap_clear (tab->new_replaceable_dependencies);
341*e4b17023SJohn Marino 	}
342*e4b17023SJohn Marino     }
343*e4b17023SJohn Marino   else
344*e4b17023SJohn Marino     {
345*e4b17023SJohn Marino       i = var_to_partition (tab->map, var);
346*e4b17023SJohn Marino       gcc_checking_assert (i != NO_PARTITION);
347*e4b17023SJohn Marino       gcc_checking_assert (tab->num_in_part[i] != 0);
348*e4b17023SJohn Marino       /* Only dependencies on ssa_names which are coalesced with something need
349*e4b17023SJohn Marino          to be tracked.  Partitions with containing only a single SSA_NAME
350*e4b17023SJohn Marino 	 *cannot* have their value changed.  */
351*e4b17023SJohn Marino       if (tab->num_in_part[i] > 1)
352*e4b17023SJohn Marino         {
353*e4b17023SJohn Marino 	  add_to_partition_kill_list (tab, i, version);
354*e4b17023SJohn Marino 	  make_dependent_on_partition (tab, version, i);
355*e4b17023SJohn Marino 	}
356*e4b17023SJohn Marino     }
357*e4b17023SJohn Marino }
358*e4b17023SJohn Marino 
359*e4b17023SJohn Marino 
360*e4b17023SJohn Marino /* Return TRUE if expression STMT is suitable for replacement.
361*e4b17023SJohn Marino    TER is true if is_replaceable_p is called from within TER, false
362*e4b17023SJohn Marino    when used from within stmt_is_replaceable_p, i.e. EXPAND_INITIALIZER
363*e4b17023SJohn Marino    expansion.  The differences are that with !TER some tests are skipped
364*e4b17023SJohn Marino    to make it more aggressive (doesn't require the same bb, or for -O0
365*e4b17023SJohn Marino    same locus and same BLOCK), on the other side never considers memory
366*e4b17023SJohn Marino    loads as replaceable, because those don't ever lead into constant
367*e4b17023SJohn Marino    expressions.  */
368*e4b17023SJohn Marino 
369*e4b17023SJohn Marino static inline bool
is_replaceable_p(gimple stmt,bool ter)370*e4b17023SJohn Marino is_replaceable_p (gimple stmt, bool ter)
371*e4b17023SJohn Marino {
372*e4b17023SJohn Marino   use_operand_p use_p;
373*e4b17023SJohn Marino   tree def;
374*e4b17023SJohn Marino   gimple use_stmt;
375*e4b17023SJohn Marino   location_t locus1, locus2;
376*e4b17023SJohn Marino   tree block1, block2;
377*e4b17023SJohn Marino 
378*e4b17023SJohn Marino   /* Only consider modify stmts.  */
379*e4b17023SJohn Marino   if (!is_gimple_assign (stmt))
380*e4b17023SJohn Marino     return false;
381*e4b17023SJohn Marino 
382*e4b17023SJohn Marino   /* If the statement may throw an exception, it cannot be replaced.  */
383*e4b17023SJohn Marino   if (stmt_could_throw_p (stmt))
384*e4b17023SJohn Marino     return false;
385*e4b17023SJohn Marino 
386*e4b17023SJohn Marino   /* Punt if there is more than 1 def.  */
387*e4b17023SJohn Marino   def = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF);
388*e4b17023SJohn Marino   if (!def)
389*e4b17023SJohn Marino     return false;
390*e4b17023SJohn Marino 
391*e4b17023SJohn Marino   /* Only consider definitions which have a single use.  */
392*e4b17023SJohn Marino   if (!single_imm_use (def, &use_p, &use_stmt))
393*e4b17023SJohn Marino     return false;
394*e4b17023SJohn Marino 
395*e4b17023SJohn Marino   /* If the use isn't in this block, it wont be replaced either.  */
396*e4b17023SJohn Marino   if (ter && gimple_bb (use_stmt) != gimple_bb (stmt))
397*e4b17023SJohn Marino     return false;
398*e4b17023SJohn Marino 
399*e4b17023SJohn Marino   locus1 = gimple_location (stmt);
400*e4b17023SJohn Marino   block1 = gimple_block (stmt);
401*e4b17023SJohn Marino 
402*e4b17023SJohn Marino   if (gimple_code (use_stmt) == GIMPLE_PHI)
403*e4b17023SJohn Marino     {
404*e4b17023SJohn Marino       locus2 = 0;
405*e4b17023SJohn Marino       block2 = NULL_TREE;
406*e4b17023SJohn Marino     }
407*e4b17023SJohn Marino   else
408*e4b17023SJohn Marino     {
409*e4b17023SJohn Marino       locus2 = gimple_location (use_stmt);
410*e4b17023SJohn Marino       block2 = gimple_block (use_stmt);
411*e4b17023SJohn Marino     }
412*e4b17023SJohn Marino 
413*e4b17023SJohn Marino   if (!optimize
414*e4b17023SJohn Marino       && ter
415*e4b17023SJohn Marino       && ((locus1 && locus1 != locus2) || (block1 && block1 != block2)))
416*e4b17023SJohn Marino     return false;
417*e4b17023SJohn Marino 
418*e4b17023SJohn Marino   /* Used in this block, but at the TOP of the block, not the end.  */
419*e4b17023SJohn Marino   if (gimple_code (use_stmt) == GIMPLE_PHI)
420*e4b17023SJohn Marino     return false;
421*e4b17023SJohn Marino 
422*e4b17023SJohn Marino   /* There must be no VDEFs.  */
423*e4b17023SJohn Marino   if (gimple_vdef (stmt))
424*e4b17023SJohn Marino     return false;
425*e4b17023SJohn Marino 
426*e4b17023SJohn Marino   /* Without alias info we can't move around loads.  */
427*e4b17023SJohn Marino   if ((!optimize || !ter)
428*e4b17023SJohn Marino       && gimple_assign_single_p (stmt)
429*e4b17023SJohn Marino       && !is_gimple_val (gimple_assign_rhs1 (stmt)))
430*e4b17023SJohn Marino     return false;
431*e4b17023SJohn Marino 
432*e4b17023SJohn Marino   /* Float expressions must go through memory if float-store is on.  */
433*e4b17023SJohn Marino   if (flag_float_store
434*e4b17023SJohn Marino       && FLOAT_TYPE_P (gimple_expr_type (stmt)))
435*e4b17023SJohn Marino     return false;
436*e4b17023SJohn Marino 
437*e4b17023SJohn Marino   /* An assignment with a register variable on the RHS is not
438*e4b17023SJohn Marino      replaceable.  */
439*e4b17023SJohn Marino   if (gimple_assign_rhs_code (stmt) == VAR_DECL
440*e4b17023SJohn Marino       && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt)))
441*e4b17023SJohn Marino     return false;
442*e4b17023SJohn Marino 
443*e4b17023SJohn Marino   /* No function calls can be replaced.  */
444*e4b17023SJohn Marino   if (is_gimple_call (stmt))
445*e4b17023SJohn Marino     return false;
446*e4b17023SJohn Marino 
447*e4b17023SJohn Marino   /* Leave any stmt with volatile operands alone as well.  */
448*e4b17023SJohn Marino   if (gimple_has_volatile_ops (stmt))
449*e4b17023SJohn Marino     return false;
450*e4b17023SJohn Marino 
451*e4b17023SJohn Marino   return true;
452*e4b17023SJohn Marino }
453*e4b17023SJohn Marino 
454*e4b17023SJohn Marino 
455*e4b17023SJohn Marino /* Variant of is_replaceable_p test for use in EXPAND_INITIALIZER
456*e4b17023SJohn Marino    expansion.  */
457*e4b17023SJohn Marino 
458*e4b17023SJohn Marino bool
stmt_is_replaceable_p(gimple stmt)459*e4b17023SJohn Marino stmt_is_replaceable_p (gimple stmt)
460*e4b17023SJohn Marino {
461*e4b17023SJohn Marino   return is_replaceable_p (stmt, false);
462*e4b17023SJohn Marino }
463*e4b17023SJohn Marino 
464*e4b17023SJohn Marino 
465*e4b17023SJohn Marino /* This function will remove the expression for VERSION from replacement
466*e4b17023SJohn Marino    consideration in table TAB.  If FREE_EXPR is true, then remove the
467*e4b17023SJohn Marino    expression from consideration as well by freeing the decl uid bitmap.  */
468*e4b17023SJohn Marino 
469*e4b17023SJohn Marino static void
finished_with_expr(temp_expr_table_p tab,int version,bool free_expr)470*e4b17023SJohn Marino finished_with_expr (temp_expr_table_p tab, int version, bool free_expr)
471*e4b17023SJohn Marino {
472*e4b17023SJohn Marino   unsigned i;
473*e4b17023SJohn Marino   bitmap_iterator bi;
474*e4b17023SJohn Marino 
475*e4b17023SJohn Marino   /* Remove this expression from its dependent lists.  The partition dependence
476*e4b17023SJohn Marino      list is retained and transfered later to whomever uses this version.  */
477*e4b17023SJohn Marino   if (tab->partition_dependencies[version])
478*e4b17023SJohn Marino     {
479*e4b17023SJohn Marino       EXECUTE_IF_SET_IN_BITMAP (tab->partition_dependencies[version], 0, i, bi)
480*e4b17023SJohn Marino 	remove_from_partition_kill_list (tab, i, version);
481*e4b17023SJohn Marino       BITMAP_FREE (tab->partition_dependencies[version]);
482*e4b17023SJohn Marino     }
483*e4b17023SJohn Marino   if (free_expr)
484*e4b17023SJohn Marino     BITMAP_FREE (tab->expr_decl_uids[version]);
485*e4b17023SJohn Marino }
486*e4b17023SJohn Marino 
487*e4b17023SJohn Marino 
488*e4b17023SJohn Marino /* Create an expression entry for a replaceable expression.  */
489*e4b17023SJohn Marino 
490*e4b17023SJohn Marino static void
process_replaceable(temp_expr_table_p tab,gimple stmt,int call_cnt)491*e4b17023SJohn Marino process_replaceable (temp_expr_table_p tab, gimple stmt, int call_cnt)
492*e4b17023SJohn Marino {
493*e4b17023SJohn Marino   tree var, def, basevar;
494*e4b17023SJohn Marino   int version;
495*e4b17023SJohn Marino   ssa_op_iter iter;
496*e4b17023SJohn Marino   bitmap def_vars, use_vars;
497*e4b17023SJohn Marino 
498*e4b17023SJohn Marino   gcc_checking_assert (is_replaceable_p (stmt, true));
499*e4b17023SJohn Marino 
500*e4b17023SJohn Marino   def = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF);
501*e4b17023SJohn Marino   version = SSA_NAME_VERSION (def);
502*e4b17023SJohn Marino   basevar = SSA_NAME_VAR (def);
503*e4b17023SJohn Marino   def_vars = BITMAP_ALLOC (NULL);
504*e4b17023SJohn Marino 
505*e4b17023SJohn Marino   bitmap_set_bit (def_vars, DECL_UID (basevar));
506*e4b17023SJohn Marino 
507*e4b17023SJohn Marino   /* Add this expression to the dependency list for each use partition.  */
508*e4b17023SJohn Marino   FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
509*e4b17023SJohn Marino     {
510*e4b17023SJohn Marino       int var_version = SSA_NAME_VERSION (var);
511*e4b17023SJohn Marino 
512*e4b17023SJohn Marino       use_vars = tab->expr_decl_uids[var_version];
513*e4b17023SJohn Marino       add_dependence (tab, version, var);
514*e4b17023SJohn Marino       if (use_vars)
515*e4b17023SJohn Marino         {
516*e4b17023SJohn Marino 	  bitmap_ior_into (def_vars, use_vars);
517*e4b17023SJohn Marino 	  BITMAP_FREE (tab->expr_decl_uids[var_version]);
518*e4b17023SJohn Marino 	}
519*e4b17023SJohn Marino       else
520*e4b17023SJohn Marino 	bitmap_set_bit (def_vars, DECL_UID (SSA_NAME_VAR (var)));
521*e4b17023SJohn Marino     }
522*e4b17023SJohn Marino   tab->expr_decl_uids[version] = def_vars;
523*e4b17023SJohn Marino 
524*e4b17023SJohn Marino   /* If there are VUSES, add a dependence on virtual defs.  */
525*e4b17023SJohn Marino   if (gimple_vuse (stmt))
526*e4b17023SJohn Marino     {
527*e4b17023SJohn Marino       make_dependent_on_partition (tab, version, VIRTUAL_PARTITION (tab));
528*e4b17023SJohn Marino       add_to_partition_kill_list (tab, VIRTUAL_PARTITION (tab), version);
529*e4b17023SJohn Marino     }
530*e4b17023SJohn Marino 
531*e4b17023SJohn Marino   tab->call_cnt[version] = call_cnt;
532*e4b17023SJohn Marino }
533*e4b17023SJohn Marino 
534*e4b17023SJohn Marino 
535*e4b17023SJohn Marino /* This function removes any expression in TAB which is dependent on PARTITION
536*e4b17023SJohn Marino    from consideration, making it not replaceable.  */
537*e4b17023SJohn Marino 
538*e4b17023SJohn Marino static inline void
kill_expr(temp_expr_table_p tab,int partition)539*e4b17023SJohn Marino kill_expr (temp_expr_table_p tab, int partition)
540*e4b17023SJohn Marino {
541*e4b17023SJohn Marino   unsigned version;
542*e4b17023SJohn Marino 
543*e4b17023SJohn Marino   /* Mark every active expr dependent on this var as not replaceable.
544*e4b17023SJohn Marino      finished_with_expr can modify the bitmap, so we can't execute over it.  */
545*e4b17023SJohn Marino   while (tab->kill_list[partition])
546*e4b17023SJohn Marino     {
547*e4b17023SJohn Marino       version = bitmap_first_set_bit (tab->kill_list[partition]);
548*e4b17023SJohn Marino       finished_with_expr (tab, version, true);
549*e4b17023SJohn Marino     }
550*e4b17023SJohn Marino 
551*e4b17023SJohn Marino   gcc_checking_assert (!tab->kill_list[partition]);
552*e4b17023SJohn Marino }
553*e4b17023SJohn Marino 
554*e4b17023SJohn Marino 
555*e4b17023SJohn Marino /* This function kills all expressions in TAB which are dependent on virtual
556*e4b17023SJohn Marino    partitions.  */
557*e4b17023SJohn Marino 
558*e4b17023SJohn Marino static inline void
kill_virtual_exprs(temp_expr_table_p tab)559*e4b17023SJohn Marino kill_virtual_exprs (temp_expr_table_p tab)
560*e4b17023SJohn Marino {
561*e4b17023SJohn Marino   kill_expr (tab, VIRTUAL_PARTITION (tab));
562*e4b17023SJohn Marino }
563*e4b17023SJohn Marino 
564*e4b17023SJohn Marino 
565*e4b17023SJohn Marino /* Mark the expression associated with VAR as replaceable, and enter
566*e4b17023SJohn Marino    the defining stmt into the partition_dependencies table TAB.  If
567*e4b17023SJohn Marino    MORE_REPLACING is true, accumulate the pending partition dependencies.  */
568*e4b17023SJohn Marino 
569*e4b17023SJohn Marino static void
mark_replaceable(temp_expr_table_p tab,tree var,bool more_replacing)570*e4b17023SJohn Marino mark_replaceable (temp_expr_table_p tab, tree var, bool more_replacing)
571*e4b17023SJohn Marino {
572*e4b17023SJohn Marino   int version = SSA_NAME_VERSION (var);
573*e4b17023SJohn Marino 
574*e4b17023SJohn Marino   /* Move the dependence list to the pending listpending.  */
575*e4b17023SJohn Marino   if (more_replacing && tab->partition_dependencies[version])
576*e4b17023SJohn Marino     bitmap_ior_into (tab->new_replaceable_dependencies,
577*e4b17023SJohn Marino 		     tab->partition_dependencies[version]);
578*e4b17023SJohn Marino 
579*e4b17023SJohn Marino   finished_with_expr (tab, version, !more_replacing);
580*e4b17023SJohn Marino 
581*e4b17023SJohn Marino   /* Set the replaceable expression.  */
582*e4b17023SJohn Marino   if (!tab->replaceable_expressions)
583*e4b17023SJohn Marino     tab->replaceable_expressions = BITMAP_ALLOC (NULL);
584*e4b17023SJohn Marino   bitmap_set_bit (tab->replaceable_expressions, version);
585*e4b17023SJohn Marino }
586*e4b17023SJohn Marino 
587*e4b17023SJohn Marino 
588*e4b17023SJohn Marino /* This function processes basic block BB, and looks for variables which can
589*e4b17023SJohn Marino    be replaced by their expressions.  Results are stored in the table TAB.  */
590*e4b17023SJohn Marino 
591*e4b17023SJohn Marino static void
find_replaceable_in_bb(temp_expr_table_p tab,basic_block bb)592*e4b17023SJohn Marino find_replaceable_in_bb (temp_expr_table_p tab, basic_block bb)
593*e4b17023SJohn Marino {
594*e4b17023SJohn Marino   gimple_stmt_iterator bsi;
595*e4b17023SJohn Marino   gimple stmt;
596*e4b17023SJohn Marino   tree def, use, fndecl;
597*e4b17023SJohn Marino   int partition;
598*e4b17023SJohn Marino   var_map map = tab->map;
599*e4b17023SJohn Marino   ssa_op_iter iter;
600*e4b17023SJohn Marino   bool stmt_replaceable;
601*e4b17023SJohn Marino   int cur_call_cnt = 0;
602*e4b17023SJohn Marino 
603*e4b17023SJohn Marino   for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
604*e4b17023SJohn Marino     {
605*e4b17023SJohn Marino       stmt = gsi_stmt (bsi);
606*e4b17023SJohn Marino 
607*e4b17023SJohn Marino       if (is_gimple_debug (stmt))
608*e4b17023SJohn Marino 	continue;
609*e4b17023SJohn Marino 
610*e4b17023SJohn Marino       stmt_replaceable = is_replaceable_p (stmt, true);
611*e4b17023SJohn Marino 
612*e4b17023SJohn Marino       /* Determine if this stmt finishes an existing expression.  */
613*e4b17023SJohn Marino       FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
614*e4b17023SJohn Marino 	{
615*e4b17023SJohn Marino 	  unsigned ver = SSA_NAME_VERSION (use);
616*e4b17023SJohn Marino 
617*e4b17023SJohn Marino 	  /* If this use is a potential replacement variable, process it.  */
618*e4b17023SJohn Marino 	  if (tab->expr_decl_uids[ver])
619*e4b17023SJohn Marino 	    {
620*e4b17023SJohn Marino 	      bool same_root_var = false;
621*e4b17023SJohn Marino 	      ssa_op_iter iter2;
622*e4b17023SJohn Marino 	      bitmap vars = tab->expr_decl_uids[ver];
623*e4b17023SJohn Marino 
624*e4b17023SJohn Marino 	      /* See if the root variables are the same.  If they are, we
625*e4b17023SJohn Marino 		 do not want to do the replacement to avoid problems with
626*e4b17023SJohn Marino 		 code size, see PR tree-optimization/17549.  */
627*e4b17023SJohn Marino 	      if (!bitmap_empty_p (vars))
628*e4b17023SJohn Marino 		FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter2, SSA_OP_DEF)
629*e4b17023SJohn Marino 		  {
630*e4b17023SJohn Marino 		    if (bitmap_bit_p (vars, DECL_UID (SSA_NAME_VAR (def))))
631*e4b17023SJohn Marino 		      {
632*e4b17023SJohn Marino 			same_root_var = true;
633*e4b17023SJohn Marino 			break;
634*e4b17023SJohn Marino 		      }
635*e4b17023SJohn Marino 		  }
636*e4b17023SJohn Marino 
637*e4b17023SJohn Marino 	      /* If the stmt does a memory store and the replacement
638*e4b17023SJohn Marino 	         is a load aliasing it avoid creating overlapping
639*e4b17023SJohn Marino 		 assignments which we cannot expand correctly.  */
640*e4b17023SJohn Marino 	      if (gimple_vdef (stmt)
641*e4b17023SJohn Marino 		  && gimple_assign_single_p (stmt))
642*e4b17023SJohn Marino 		{
643*e4b17023SJohn Marino 		  gimple def_stmt = SSA_NAME_DEF_STMT (use);
644*e4b17023SJohn Marino 		  while (is_gimple_assign (def_stmt)
645*e4b17023SJohn Marino 			 && gimple_assign_rhs_code (def_stmt) == SSA_NAME)
646*e4b17023SJohn Marino 		    def_stmt
647*e4b17023SJohn Marino 		      = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (def_stmt));
648*e4b17023SJohn Marino 		  if (gimple_vuse (def_stmt)
649*e4b17023SJohn Marino 		      && gimple_assign_single_p (def_stmt)
650*e4b17023SJohn Marino 		      && refs_may_alias_p (gimple_assign_lhs (stmt),
651*e4b17023SJohn Marino 					   gimple_assign_rhs1 (def_stmt)))
652*e4b17023SJohn Marino 		    same_root_var = true;
653*e4b17023SJohn Marino 		}
654*e4b17023SJohn Marino 
655*e4b17023SJohn Marino 	      /* Mark expression as replaceable unless stmt is volatile, or the
656*e4b17023SJohn Marino 		 def variable has the same root variable as something in the
657*e4b17023SJohn Marino 		 substitution list, or the def and use span a call such that
658*e4b17023SJohn Marino 		 we'll expand lifetimes across a call.  */
659*e4b17023SJohn Marino 	      if (gimple_has_volatile_ops (stmt) || same_root_var
660*e4b17023SJohn Marino 		  || (tab->call_cnt[ver] != cur_call_cnt
661*e4b17023SJohn Marino 		      && SINGLE_SSA_USE_OPERAND (SSA_NAME_DEF_STMT (use), SSA_OP_USE)
662*e4b17023SJohn Marino 			 == NULL_USE_OPERAND_P))
663*e4b17023SJohn Marino 		finished_with_expr (tab, ver, true);
664*e4b17023SJohn Marino 	      else
665*e4b17023SJohn Marino 		mark_replaceable (tab, use, stmt_replaceable);
666*e4b17023SJohn Marino 	    }
667*e4b17023SJohn Marino 	}
668*e4b17023SJohn Marino 
669*e4b17023SJohn Marino       /* Next, see if this stmt kills off an active expression.  */
670*e4b17023SJohn Marino       FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
671*e4b17023SJohn Marino 	{
672*e4b17023SJohn Marino 	  partition = var_to_partition (map, def);
673*e4b17023SJohn Marino 	  if (partition != NO_PARTITION && tab->kill_list[partition])
674*e4b17023SJohn Marino 	    kill_expr (tab, partition);
675*e4b17023SJohn Marino 	}
676*e4b17023SJohn Marino 
677*e4b17023SJohn Marino       /* Increment counter if this is a non BUILT_IN call. We allow
678*e4b17023SJohn Marino 	 replacement over BUILT_IN calls since many will expand to inline
679*e4b17023SJohn Marino 	 insns instead of a true call.  */
680*e4b17023SJohn Marino       if (is_gimple_call (stmt)
681*e4b17023SJohn Marino 	  && !((fndecl = gimple_call_fndecl (stmt))
682*e4b17023SJohn Marino 	       && DECL_BUILT_IN (fndecl)))
683*e4b17023SJohn Marino 	cur_call_cnt++;
684*e4b17023SJohn Marino 
685*e4b17023SJohn Marino       /* Now see if we are creating a new expression or not.  */
686*e4b17023SJohn Marino       if (stmt_replaceable)
687*e4b17023SJohn Marino 	process_replaceable (tab, stmt, cur_call_cnt);
688*e4b17023SJohn Marino 
689*e4b17023SJohn Marino       /* Free any unused dependency lists.  */
690*e4b17023SJohn Marino       bitmap_clear (tab->new_replaceable_dependencies);
691*e4b17023SJohn Marino 
692*e4b17023SJohn Marino       /* A V_{MAY,MUST}_DEF kills any expression using a virtual operand,
693*e4b17023SJohn Marino 	 including the current stmt.  */
694*e4b17023SJohn Marino       if (gimple_vdef (stmt))
695*e4b17023SJohn Marino         kill_virtual_exprs (tab);
696*e4b17023SJohn Marino     }
697*e4b17023SJohn Marino }
698*e4b17023SJohn Marino 
699*e4b17023SJohn Marino 
700*e4b17023SJohn Marino /* This function is the driver routine for replacement of temporary expressions
701*e4b17023SJohn Marino    in the SSA->normal phase, operating on MAP.  If there are replaceable
702*e4b17023SJohn Marino    expressions, a table is returned which maps SSA versions to the
703*e4b17023SJohn Marino    expressions they should be replaced with.  A NULL_TREE indicates no
704*e4b17023SJohn Marino    replacement should take place.  If there are no replacements at all,
705*e4b17023SJohn Marino    NULL is returned by the function, otherwise an expression vector indexed
706*e4b17023SJohn Marino    by SSA_NAME version numbers.  */
707*e4b17023SJohn Marino 
708*e4b17023SJohn Marino extern bitmap
find_replaceable_exprs(var_map map)709*e4b17023SJohn Marino find_replaceable_exprs (var_map map)
710*e4b17023SJohn Marino {
711*e4b17023SJohn Marino   basic_block bb;
712*e4b17023SJohn Marino   temp_expr_table_p table;
713*e4b17023SJohn Marino   bitmap ret;
714*e4b17023SJohn Marino 
715*e4b17023SJohn Marino   table = new_temp_expr_table (map);
716*e4b17023SJohn Marino   FOR_EACH_BB (bb)
717*e4b17023SJohn Marino     {
718*e4b17023SJohn Marino       find_replaceable_in_bb (table, bb);
719*e4b17023SJohn Marino       gcc_checking_assert (bitmap_empty_p (table->partition_in_use));
720*e4b17023SJohn Marino     }
721*e4b17023SJohn Marino 
722*e4b17023SJohn Marino   ret = free_temp_expr_table (table);
723*e4b17023SJohn Marino   return ret;
724*e4b17023SJohn Marino }
725*e4b17023SJohn Marino 
726*e4b17023SJohn Marino /* Dump TER expression table EXPR to file F.  */
727*e4b17023SJohn Marino 
728*e4b17023SJohn Marino void
dump_replaceable_exprs(FILE * f,bitmap expr)729*e4b17023SJohn Marino dump_replaceable_exprs (FILE *f, bitmap expr)
730*e4b17023SJohn Marino {
731*e4b17023SJohn Marino   tree var;
732*e4b17023SJohn Marino   unsigned x;
733*e4b17023SJohn Marino 
734*e4b17023SJohn Marino   fprintf (f, "\nReplacing Expressions\n");
735*e4b17023SJohn Marino   for (x = 0; x < num_ssa_names; x++)
736*e4b17023SJohn Marino     if (bitmap_bit_p (expr, x))
737*e4b17023SJohn Marino       {
738*e4b17023SJohn Marino 	var = ssa_name (x);
739*e4b17023SJohn Marino 	print_generic_expr (f, var, TDF_SLIM);
740*e4b17023SJohn Marino 	fprintf (f, " replace with --> ");
741*e4b17023SJohn Marino 	print_gimple_stmt (f, SSA_NAME_DEF_STMT (var), 0, TDF_SLIM);
742*e4b17023SJohn Marino 	fprintf (f, "\n");
743*e4b17023SJohn Marino       }
744*e4b17023SJohn Marino   fprintf (f, "\n");
745*e4b17023SJohn Marino }
746*e4b17023SJohn Marino 
747*e4b17023SJohn Marino 
748*e4b17023SJohn Marino #ifdef ENABLE_CHECKING
749*e4b17023SJohn Marino /* Dump the status of the various tables in the expression table.  This is used
750*e4b17023SJohn Marino    exclusively to debug TER.  F is the place to send debug info and T is the
751*e4b17023SJohn Marino    table being debugged.  */
752*e4b17023SJohn Marino 
753*e4b17023SJohn Marino DEBUG_FUNCTION void
debug_ter(FILE * f,temp_expr_table_p t)754*e4b17023SJohn Marino debug_ter (FILE *f, temp_expr_table_p t)
755*e4b17023SJohn Marino {
756*e4b17023SJohn Marino   unsigned x, y;
757*e4b17023SJohn Marino   bitmap_iterator bi;
758*e4b17023SJohn Marino 
759*e4b17023SJohn Marino   fprintf (f, "\nDumping current state of TER\n virtual partition = %d\n",
760*e4b17023SJohn Marino 	   VIRTUAL_PARTITION (t));
761*e4b17023SJohn Marino   if (t->replaceable_expressions)
762*e4b17023SJohn Marino     dump_replaceable_exprs (f, t->replaceable_expressions);
763*e4b17023SJohn Marino   fprintf (f, "Currently tracking the following expressions:\n");
764*e4b17023SJohn Marino 
765*e4b17023SJohn Marino   for (x = 1; x < num_ssa_names; x++)
766*e4b17023SJohn Marino     if (t->expr_decl_uids[x])
767*e4b17023SJohn Marino       {
768*e4b17023SJohn Marino         print_generic_expr (f, ssa_name (x), TDF_SLIM);
769*e4b17023SJohn Marino         fprintf (f, " dep-parts : ");
770*e4b17023SJohn Marino 	if (t->partition_dependencies[x]
771*e4b17023SJohn Marino 	    && !bitmap_empty_p (t->partition_dependencies[x]))
772*e4b17023SJohn Marino 	  {
773*e4b17023SJohn Marino 	    EXECUTE_IF_SET_IN_BITMAP (t->partition_dependencies[x], 0, y, bi)
774*e4b17023SJohn Marino 	      fprintf (f, "P%d ",y);
775*e4b17023SJohn Marino 	  }
776*e4b17023SJohn Marino 	fprintf (f, "   basedecls: ");
777*e4b17023SJohn Marino 	EXECUTE_IF_SET_IN_BITMAP (t->expr_decl_uids[x], 0, y, bi)
778*e4b17023SJohn Marino 	  fprintf (f, "%d ",y);
779*e4b17023SJohn Marino 	fprintf (f, "   call_cnt : %d",t->call_cnt[x]);
780*e4b17023SJohn Marino 	fprintf (f, "\n");
781*e4b17023SJohn Marino       }
782*e4b17023SJohn Marino 
783*e4b17023SJohn Marino   bitmap_print (f, t->partition_in_use, "Partitions in use ",
784*e4b17023SJohn Marino 		"\npartition KILL lists:\n");
785*e4b17023SJohn Marino 
786*e4b17023SJohn Marino   for (x = 0; x <= num_var_partitions (t->map); x++)
787*e4b17023SJohn Marino     if (t->kill_list[x])
788*e4b17023SJohn Marino       {
789*e4b17023SJohn Marino         fprintf (f, "Partition %d : ", x);
790*e4b17023SJohn Marino 	EXECUTE_IF_SET_IN_BITMAP (t->kill_list[x], 0, y, bi)
791*e4b17023SJohn Marino 	  fprintf (f, "_%d ",y);
792*e4b17023SJohn Marino       }
793*e4b17023SJohn Marino 
794*e4b17023SJohn Marino   fprintf (f, "\n----------\n");
795*e4b17023SJohn Marino }
796*e4b17023SJohn Marino #endif
797