1*e4b17023SJohn Marino /* Copy propagation and SSA_NAME replacement support routines.
2*e4b17023SJohn Marino Copyright (C) 2004, 2005, 2006, 2007, 2008, 2010
3*e4b17023SJohn Marino Free Software Foundation, Inc.
4*e4b17023SJohn Marino
5*e4b17023SJohn Marino This file is part of GCC.
6*e4b17023SJohn Marino
7*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify
8*e4b17023SJohn Marino it under the terms of the GNU General Public License as published by
9*e4b17023SJohn Marino the Free Software Foundation; either version 3, or (at your option)
10*e4b17023SJohn Marino any later version.
11*e4b17023SJohn Marino
12*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful,
13*e4b17023SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
14*e4b17023SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15*e4b17023SJohn Marino GNU General Public License for more details.
16*e4b17023SJohn Marino
17*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
18*e4b17023SJohn Marino along with GCC; see the file COPYING3. If not see
19*e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */
20*e4b17023SJohn Marino
21*e4b17023SJohn Marino #include "config.h"
22*e4b17023SJohn Marino #include "system.h"
23*e4b17023SJohn Marino #include "coretypes.h"
24*e4b17023SJohn Marino #include "tm.h"
25*e4b17023SJohn Marino #include "tree.h"
26*e4b17023SJohn Marino #include "flags.h"
27*e4b17023SJohn Marino #include "tm_p.h"
28*e4b17023SJohn Marino #include "basic-block.h"
29*e4b17023SJohn Marino #include "output.h"
30*e4b17023SJohn Marino #include "function.h"
31*e4b17023SJohn Marino #include "tree-pretty-print.h"
32*e4b17023SJohn Marino #include "gimple-pretty-print.h"
33*e4b17023SJohn Marino #include "timevar.h"
34*e4b17023SJohn Marino #include "tree-dump.h"
35*e4b17023SJohn Marino #include "tree-flow.h"
36*e4b17023SJohn Marino #include "tree-pass.h"
37*e4b17023SJohn Marino #include "tree-ssa-propagate.h"
38*e4b17023SJohn Marino #include "langhooks.h"
39*e4b17023SJohn Marino #include "cfgloop.h"
40*e4b17023SJohn Marino
41*e4b17023SJohn Marino /* This file implements the copy propagation pass and provides a
42*e4b17023SJohn Marino handful of interfaces for performing const/copy propagation and
43*e4b17023SJohn Marino simple expression replacement which keep variable annotations
44*e4b17023SJohn Marino up-to-date.
45*e4b17023SJohn Marino
46*e4b17023SJohn Marino We require that for any copy operation where the RHS and LHS have
47*e4b17023SJohn Marino a non-null memory tag the memory tag be the same. It is OK
48*e4b17023SJohn Marino for one or both of the memory tags to be NULL.
49*e4b17023SJohn Marino
50*e4b17023SJohn Marino We also require tracking if a variable is dereferenced in a load or
51*e4b17023SJohn Marino store operation.
52*e4b17023SJohn Marino
53*e4b17023SJohn Marino We enforce these requirements by having all copy propagation and
54*e4b17023SJohn Marino replacements of one SSA_NAME with a different SSA_NAME to use the
55*e4b17023SJohn Marino APIs defined in this file. */
56*e4b17023SJohn Marino
57*e4b17023SJohn Marino /* Return true if we may propagate ORIG into DEST, false otherwise. */
58*e4b17023SJohn Marino
59*e4b17023SJohn Marino bool
may_propagate_copy(tree dest,tree orig)60*e4b17023SJohn Marino may_propagate_copy (tree dest, tree orig)
61*e4b17023SJohn Marino {
62*e4b17023SJohn Marino tree type_d = TREE_TYPE (dest);
63*e4b17023SJohn Marino tree type_o = TREE_TYPE (orig);
64*e4b17023SJohn Marino
65*e4b17023SJohn Marino /* If ORIG flows in from an abnormal edge, it cannot be propagated. */
66*e4b17023SJohn Marino if (TREE_CODE (orig) == SSA_NAME
67*e4b17023SJohn Marino && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig))
68*e4b17023SJohn Marino return false;
69*e4b17023SJohn Marino
70*e4b17023SJohn Marino /* If DEST is an SSA_NAME that flows from an abnormal edge, then it
71*e4b17023SJohn Marino cannot be replaced. */
72*e4b17023SJohn Marino if (TREE_CODE (dest) == SSA_NAME
73*e4b17023SJohn Marino && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (dest))
74*e4b17023SJohn Marino return false;
75*e4b17023SJohn Marino
76*e4b17023SJohn Marino /* Do not copy between types for which we *do* need a conversion. */
77*e4b17023SJohn Marino if (!useless_type_conversion_p (type_d, type_o))
78*e4b17023SJohn Marino return false;
79*e4b17023SJohn Marino
80*e4b17023SJohn Marino /* Propagating virtual operands is always ok. */
81*e4b17023SJohn Marino if (TREE_CODE (dest) == SSA_NAME && !is_gimple_reg (dest))
82*e4b17023SJohn Marino {
83*e4b17023SJohn Marino /* But only between virtual operands. */
84*e4b17023SJohn Marino gcc_assert (TREE_CODE (orig) == SSA_NAME && !is_gimple_reg (orig));
85*e4b17023SJohn Marino
86*e4b17023SJohn Marino return true;
87*e4b17023SJohn Marino }
88*e4b17023SJohn Marino
89*e4b17023SJohn Marino /* Anything else is OK. */
90*e4b17023SJohn Marino return true;
91*e4b17023SJohn Marino }
92*e4b17023SJohn Marino
93*e4b17023SJohn Marino /* Like may_propagate_copy, but use as the destination expression
94*e4b17023SJohn Marino the principal expression (typically, the RHS) contained in
95*e4b17023SJohn Marino statement DEST. This is more efficient when working with the
96*e4b17023SJohn Marino gimple tuples representation. */
97*e4b17023SJohn Marino
98*e4b17023SJohn Marino bool
may_propagate_copy_into_stmt(gimple dest,tree orig)99*e4b17023SJohn Marino may_propagate_copy_into_stmt (gimple dest, tree orig)
100*e4b17023SJohn Marino {
101*e4b17023SJohn Marino tree type_d;
102*e4b17023SJohn Marino tree type_o;
103*e4b17023SJohn Marino
104*e4b17023SJohn Marino /* If the statement is a switch or a single-rhs assignment,
105*e4b17023SJohn Marino then the expression to be replaced by the propagation may
106*e4b17023SJohn Marino be an SSA_NAME. Fortunately, there is an explicit tree
107*e4b17023SJohn Marino for the expression, so we delegate to may_propagate_copy. */
108*e4b17023SJohn Marino
109*e4b17023SJohn Marino if (gimple_assign_single_p (dest))
110*e4b17023SJohn Marino return may_propagate_copy (gimple_assign_rhs1 (dest), orig);
111*e4b17023SJohn Marino else if (gimple_code (dest) == GIMPLE_SWITCH)
112*e4b17023SJohn Marino return may_propagate_copy (gimple_switch_index (dest), orig);
113*e4b17023SJohn Marino
114*e4b17023SJohn Marino /* In other cases, the expression is not materialized, so there
115*e4b17023SJohn Marino is no destination to pass to may_propagate_copy. On the other
116*e4b17023SJohn Marino hand, the expression cannot be an SSA_NAME, so the analysis
117*e4b17023SJohn Marino is much simpler. */
118*e4b17023SJohn Marino
119*e4b17023SJohn Marino if (TREE_CODE (orig) == SSA_NAME
120*e4b17023SJohn Marino && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig))
121*e4b17023SJohn Marino return false;
122*e4b17023SJohn Marino
123*e4b17023SJohn Marino if (is_gimple_assign (dest))
124*e4b17023SJohn Marino type_d = TREE_TYPE (gimple_assign_lhs (dest));
125*e4b17023SJohn Marino else if (gimple_code (dest) == GIMPLE_COND)
126*e4b17023SJohn Marino type_d = boolean_type_node;
127*e4b17023SJohn Marino else if (is_gimple_call (dest)
128*e4b17023SJohn Marino && gimple_call_lhs (dest) != NULL_TREE)
129*e4b17023SJohn Marino type_d = TREE_TYPE (gimple_call_lhs (dest));
130*e4b17023SJohn Marino else
131*e4b17023SJohn Marino gcc_unreachable ();
132*e4b17023SJohn Marino
133*e4b17023SJohn Marino type_o = TREE_TYPE (orig);
134*e4b17023SJohn Marino
135*e4b17023SJohn Marino if (!useless_type_conversion_p (type_d, type_o))
136*e4b17023SJohn Marino return false;
137*e4b17023SJohn Marino
138*e4b17023SJohn Marino return true;
139*e4b17023SJohn Marino }
140*e4b17023SJohn Marino
141*e4b17023SJohn Marino /* Similarly, but we know that we're propagating into an ASM_EXPR. */
142*e4b17023SJohn Marino
143*e4b17023SJohn Marino bool
may_propagate_copy_into_asm(tree dest)144*e4b17023SJohn Marino may_propagate_copy_into_asm (tree dest)
145*e4b17023SJohn Marino {
146*e4b17023SJohn Marino /* Hard register operands of asms are special. Do not bypass. */
147*e4b17023SJohn Marino return !(TREE_CODE (dest) == SSA_NAME
148*e4b17023SJohn Marino && TREE_CODE (SSA_NAME_VAR (dest)) == VAR_DECL
149*e4b17023SJohn Marino && DECL_HARD_REGISTER (SSA_NAME_VAR (dest)));
150*e4b17023SJohn Marino }
151*e4b17023SJohn Marino
152*e4b17023SJohn Marino
153*e4b17023SJohn Marino /* Common code for propagate_value and replace_exp.
154*e4b17023SJohn Marino
155*e4b17023SJohn Marino Replace use operand OP_P with VAL. FOR_PROPAGATION indicates if the
156*e4b17023SJohn Marino replacement is done to propagate a value or not. */
157*e4b17023SJohn Marino
158*e4b17023SJohn Marino static void
replace_exp_1(use_operand_p op_p,tree val,bool for_propagation ATTRIBUTE_UNUSED)159*e4b17023SJohn Marino replace_exp_1 (use_operand_p op_p, tree val,
160*e4b17023SJohn Marino bool for_propagation ATTRIBUTE_UNUSED)
161*e4b17023SJohn Marino {
162*e4b17023SJohn Marino #if defined ENABLE_CHECKING
163*e4b17023SJohn Marino tree op = USE_FROM_PTR (op_p);
164*e4b17023SJohn Marino
165*e4b17023SJohn Marino gcc_assert (!(for_propagation
166*e4b17023SJohn Marino && TREE_CODE (op) == SSA_NAME
167*e4b17023SJohn Marino && TREE_CODE (val) == SSA_NAME
168*e4b17023SJohn Marino && !may_propagate_copy (op, val)));
169*e4b17023SJohn Marino #endif
170*e4b17023SJohn Marino
171*e4b17023SJohn Marino if (TREE_CODE (val) == SSA_NAME)
172*e4b17023SJohn Marino SET_USE (op_p, val);
173*e4b17023SJohn Marino else
174*e4b17023SJohn Marino SET_USE (op_p, unsave_expr_now (val));
175*e4b17023SJohn Marino }
176*e4b17023SJohn Marino
177*e4b17023SJohn Marino
178*e4b17023SJohn Marino /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
179*e4b17023SJohn Marino into the operand pointed to by OP_P.
180*e4b17023SJohn Marino
181*e4b17023SJohn Marino Use this version for const/copy propagation as it will perform additional
182*e4b17023SJohn Marino checks to ensure validity of the const/copy propagation. */
183*e4b17023SJohn Marino
184*e4b17023SJohn Marino void
propagate_value(use_operand_p op_p,tree val)185*e4b17023SJohn Marino propagate_value (use_operand_p op_p, tree val)
186*e4b17023SJohn Marino {
187*e4b17023SJohn Marino replace_exp_1 (op_p, val, true);
188*e4b17023SJohn Marino }
189*e4b17023SJohn Marino
190*e4b17023SJohn Marino /* Replace *OP_P with value VAL (assumed to be a constant or another SSA_NAME).
191*e4b17023SJohn Marino
192*e4b17023SJohn Marino Use this version when not const/copy propagating values. For example,
193*e4b17023SJohn Marino PRE uses this version when building expressions as they would appear
194*e4b17023SJohn Marino in specific blocks taking into account actions of PHI nodes.
195*e4b17023SJohn Marino
196*e4b17023SJohn Marino The statement in which an expression has been replaced should be
197*e4b17023SJohn Marino folded using fold_stmt_inplace. */
198*e4b17023SJohn Marino
199*e4b17023SJohn Marino void
replace_exp(use_operand_p op_p,tree val)200*e4b17023SJohn Marino replace_exp (use_operand_p op_p, tree val)
201*e4b17023SJohn Marino {
202*e4b17023SJohn Marino replace_exp_1 (op_p, val, false);
203*e4b17023SJohn Marino }
204*e4b17023SJohn Marino
205*e4b17023SJohn Marino
206*e4b17023SJohn Marino /* Propagate the value VAL (assumed to be a constant or another SSA_NAME)
207*e4b17023SJohn Marino into the tree pointed to by OP_P.
208*e4b17023SJohn Marino
209*e4b17023SJohn Marino Use this version for const/copy propagation when SSA operands are not
210*e4b17023SJohn Marino available. It will perform the additional checks to ensure validity of
211*e4b17023SJohn Marino the const/copy propagation, but will not update any operand information.
212*e4b17023SJohn Marino Be sure to mark the stmt as modified. */
213*e4b17023SJohn Marino
214*e4b17023SJohn Marino void
propagate_tree_value(tree * op_p,tree val)215*e4b17023SJohn Marino propagate_tree_value (tree *op_p, tree val)
216*e4b17023SJohn Marino {
217*e4b17023SJohn Marino gcc_checking_assert (!(TREE_CODE (val) == SSA_NAME
218*e4b17023SJohn Marino && *op_p
219*e4b17023SJohn Marino && TREE_CODE (*op_p) == SSA_NAME
220*e4b17023SJohn Marino && !may_propagate_copy (*op_p, val)));
221*e4b17023SJohn Marino
222*e4b17023SJohn Marino if (TREE_CODE (val) == SSA_NAME)
223*e4b17023SJohn Marino *op_p = val;
224*e4b17023SJohn Marino else
225*e4b17023SJohn Marino *op_p = unsave_expr_now (val);
226*e4b17023SJohn Marino }
227*e4b17023SJohn Marino
228*e4b17023SJohn Marino
229*e4b17023SJohn Marino /* Like propagate_tree_value, but use as the operand to replace
230*e4b17023SJohn Marino the principal expression (typically, the RHS) contained in the
231*e4b17023SJohn Marino statement referenced by iterator GSI. Note that it is not
232*e4b17023SJohn Marino always possible to update the statement in-place, so a new
233*e4b17023SJohn Marino statement may be created to replace the original. */
234*e4b17023SJohn Marino
235*e4b17023SJohn Marino void
propagate_tree_value_into_stmt(gimple_stmt_iterator * gsi,tree val)236*e4b17023SJohn Marino propagate_tree_value_into_stmt (gimple_stmt_iterator *gsi, tree val)
237*e4b17023SJohn Marino {
238*e4b17023SJohn Marino gimple stmt = gsi_stmt (*gsi);
239*e4b17023SJohn Marino
240*e4b17023SJohn Marino if (is_gimple_assign (stmt))
241*e4b17023SJohn Marino {
242*e4b17023SJohn Marino tree expr = NULL_TREE;
243*e4b17023SJohn Marino if (gimple_assign_single_p (stmt))
244*e4b17023SJohn Marino expr = gimple_assign_rhs1 (stmt);
245*e4b17023SJohn Marino propagate_tree_value (&expr, val);
246*e4b17023SJohn Marino gimple_assign_set_rhs_from_tree (gsi, expr);
247*e4b17023SJohn Marino }
248*e4b17023SJohn Marino else if (gimple_code (stmt) == GIMPLE_COND)
249*e4b17023SJohn Marino {
250*e4b17023SJohn Marino tree lhs = NULL_TREE;
251*e4b17023SJohn Marino tree rhs = build_zero_cst (TREE_TYPE (val));
252*e4b17023SJohn Marino propagate_tree_value (&lhs, val);
253*e4b17023SJohn Marino gimple_cond_set_code (stmt, NE_EXPR);
254*e4b17023SJohn Marino gimple_cond_set_lhs (stmt, lhs);
255*e4b17023SJohn Marino gimple_cond_set_rhs (stmt, rhs);
256*e4b17023SJohn Marino }
257*e4b17023SJohn Marino else if (is_gimple_call (stmt)
258*e4b17023SJohn Marino && gimple_call_lhs (stmt) != NULL_TREE)
259*e4b17023SJohn Marino {
260*e4b17023SJohn Marino gimple new_stmt;
261*e4b17023SJohn Marino
262*e4b17023SJohn Marino tree expr = NULL_TREE;
263*e4b17023SJohn Marino propagate_tree_value (&expr, val);
264*e4b17023SJohn Marino new_stmt = gimple_build_assign (gimple_call_lhs (stmt), expr);
265*e4b17023SJohn Marino move_ssa_defining_stmt_for_defs (new_stmt, stmt);
266*e4b17023SJohn Marino gsi_replace (gsi, new_stmt, false);
267*e4b17023SJohn Marino }
268*e4b17023SJohn Marino else if (gimple_code (stmt) == GIMPLE_SWITCH)
269*e4b17023SJohn Marino propagate_tree_value (gimple_switch_index_ptr (stmt), val);
270*e4b17023SJohn Marino else
271*e4b17023SJohn Marino gcc_unreachable ();
272*e4b17023SJohn Marino }
273*e4b17023SJohn Marino
274*e4b17023SJohn Marino /*---------------------------------------------------------------------------
275*e4b17023SJohn Marino Copy propagation
276*e4b17023SJohn Marino ---------------------------------------------------------------------------*/
277*e4b17023SJohn Marino /* Lattice for copy-propagation. The lattice is initialized to
278*e4b17023SJohn Marino UNDEFINED (value == NULL) for SSA names that can become a copy
279*e4b17023SJohn Marino of something or VARYING (value == self) if not (see get_copy_of_val
280*e4b17023SJohn Marino and stmt_may_generate_copy). Other values make the name a COPY
281*e4b17023SJohn Marino of that value.
282*e4b17023SJohn Marino
283*e4b17023SJohn Marino When visiting a statement or PHI node the lattice value for an
284*e4b17023SJohn Marino SSA name can transition from UNDEFINED to COPY to VARYING. */
285*e4b17023SJohn Marino
286*e4b17023SJohn Marino struct prop_value_d {
287*e4b17023SJohn Marino /* Copy-of value. */
288*e4b17023SJohn Marino tree value;
289*e4b17023SJohn Marino };
290*e4b17023SJohn Marino typedef struct prop_value_d prop_value_t;
291*e4b17023SJohn Marino
292*e4b17023SJohn Marino static prop_value_t *copy_of;
293*e4b17023SJohn Marino
294*e4b17023SJohn Marino
295*e4b17023SJohn Marino /* Return true if this statement may generate a useful copy. */
296*e4b17023SJohn Marino
297*e4b17023SJohn Marino static bool
stmt_may_generate_copy(gimple stmt)298*e4b17023SJohn Marino stmt_may_generate_copy (gimple stmt)
299*e4b17023SJohn Marino {
300*e4b17023SJohn Marino if (gimple_code (stmt) == GIMPLE_PHI)
301*e4b17023SJohn Marino return !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_phi_result (stmt));
302*e4b17023SJohn Marino
303*e4b17023SJohn Marino if (gimple_code (stmt) != GIMPLE_ASSIGN)
304*e4b17023SJohn Marino return false;
305*e4b17023SJohn Marino
306*e4b17023SJohn Marino /* If the statement has volatile operands, it won't generate a
307*e4b17023SJohn Marino useful copy. */
308*e4b17023SJohn Marino if (gimple_has_volatile_ops (stmt))
309*e4b17023SJohn Marino return false;
310*e4b17023SJohn Marino
311*e4b17023SJohn Marino /* Statements with loads and/or stores will never generate a useful copy. */
312*e4b17023SJohn Marino if (gimple_vuse (stmt))
313*e4b17023SJohn Marino return false;
314*e4b17023SJohn Marino
315*e4b17023SJohn Marino /* Otherwise, the only statements that generate useful copies are
316*e4b17023SJohn Marino assignments whose RHS is just an SSA name that doesn't flow
317*e4b17023SJohn Marino through abnormal edges. */
318*e4b17023SJohn Marino return ((gimple_assign_rhs_code (stmt) == SSA_NAME
319*e4b17023SJohn Marino && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (stmt)))
320*e4b17023SJohn Marino || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)));
321*e4b17023SJohn Marino }
322*e4b17023SJohn Marino
323*e4b17023SJohn Marino
324*e4b17023SJohn Marino /* Return the copy-of value for VAR. */
325*e4b17023SJohn Marino
326*e4b17023SJohn Marino static inline prop_value_t *
get_copy_of_val(tree var)327*e4b17023SJohn Marino get_copy_of_val (tree var)
328*e4b17023SJohn Marino {
329*e4b17023SJohn Marino prop_value_t *val = ©_of[SSA_NAME_VERSION (var)];
330*e4b17023SJohn Marino
331*e4b17023SJohn Marino if (val->value == NULL_TREE
332*e4b17023SJohn Marino && !stmt_may_generate_copy (SSA_NAME_DEF_STMT (var)))
333*e4b17023SJohn Marino {
334*e4b17023SJohn Marino /* If the variable will never generate a useful copy relation,
335*e4b17023SJohn Marino make it its own copy. */
336*e4b17023SJohn Marino val->value = var;
337*e4b17023SJohn Marino }
338*e4b17023SJohn Marino
339*e4b17023SJohn Marino return val;
340*e4b17023SJohn Marino }
341*e4b17023SJohn Marino
342*e4b17023SJohn Marino /* Return the variable VAR is a copy of or VAR if VAR isn't the result
343*e4b17023SJohn Marino of a copy. */
344*e4b17023SJohn Marino
345*e4b17023SJohn Marino static inline tree
valueize_val(tree var)346*e4b17023SJohn Marino valueize_val (tree var)
347*e4b17023SJohn Marino {
348*e4b17023SJohn Marino if (TREE_CODE (var) == SSA_NAME)
349*e4b17023SJohn Marino {
350*e4b17023SJohn Marino tree val = get_copy_of_val (var)->value;
351*e4b17023SJohn Marino if (val)
352*e4b17023SJohn Marino return val;
353*e4b17023SJohn Marino }
354*e4b17023SJohn Marino return var;
355*e4b17023SJohn Marino }
356*e4b17023SJohn Marino
357*e4b17023SJohn Marino /* Set VAL to be the copy of VAR. If that changed return true. */
358*e4b17023SJohn Marino
359*e4b17023SJohn Marino static inline bool
set_copy_of_val(tree var,tree val)360*e4b17023SJohn Marino set_copy_of_val (tree var, tree val)
361*e4b17023SJohn Marino {
362*e4b17023SJohn Marino unsigned int ver = SSA_NAME_VERSION (var);
363*e4b17023SJohn Marino tree old;
364*e4b17023SJohn Marino
365*e4b17023SJohn Marino /* Set FIRST to be the first link in COPY_OF[DEST]. If that
366*e4b17023SJohn Marino changed, return true. */
367*e4b17023SJohn Marino old = copy_of[ver].value;
368*e4b17023SJohn Marino copy_of[ver].value = val;
369*e4b17023SJohn Marino
370*e4b17023SJohn Marino if (old != val
371*e4b17023SJohn Marino || (val && !operand_equal_p (old, val, 0)))
372*e4b17023SJohn Marino return true;
373*e4b17023SJohn Marino
374*e4b17023SJohn Marino return false;
375*e4b17023SJohn Marino }
376*e4b17023SJohn Marino
377*e4b17023SJohn Marino
378*e4b17023SJohn Marino /* Dump the copy-of value for variable VAR to FILE. */
379*e4b17023SJohn Marino
380*e4b17023SJohn Marino static void
dump_copy_of(FILE * file,tree var)381*e4b17023SJohn Marino dump_copy_of (FILE *file, tree var)
382*e4b17023SJohn Marino {
383*e4b17023SJohn Marino tree val;
384*e4b17023SJohn Marino
385*e4b17023SJohn Marino print_generic_expr (file, var, dump_flags);
386*e4b17023SJohn Marino if (TREE_CODE (var) != SSA_NAME)
387*e4b17023SJohn Marino return;
388*e4b17023SJohn Marino
389*e4b17023SJohn Marino val = copy_of[SSA_NAME_VERSION (var)].value;
390*e4b17023SJohn Marino fprintf (file, " copy-of chain: ");
391*e4b17023SJohn Marino print_generic_expr (file, var, 0);
392*e4b17023SJohn Marino fprintf (file, " ");
393*e4b17023SJohn Marino if (!val)
394*e4b17023SJohn Marino fprintf (file, "[UNDEFINED]");
395*e4b17023SJohn Marino else if (val == var)
396*e4b17023SJohn Marino fprintf (file, "[NOT A COPY]");
397*e4b17023SJohn Marino else
398*e4b17023SJohn Marino {
399*e4b17023SJohn Marino fprintf (file, "-> ");
400*e4b17023SJohn Marino print_generic_expr (file, val, 0);
401*e4b17023SJohn Marino fprintf (file, " ");
402*e4b17023SJohn Marino fprintf (file, "[COPY]");
403*e4b17023SJohn Marino }
404*e4b17023SJohn Marino }
405*e4b17023SJohn Marino
406*e4b17023SJohn Marino
407*e4b17023SJohn Marino /* Evaluate the RHS of STMT. If it produces a valid copy, set the LHS
408*e4b17023SJohn Marino value and store the LHS into *RESULT_P. */
409*e4b17023SJohn Marino
410*e4b17023SJohn Marino static enum ssa_prop_result
copy_prop_visit_assignment(gimple stmt,tree * result_p)411*e4b17023SJohn Marino copy_prop_visit_assignment (gimple stmt, tree *result_p)
412*e4b17023SJohn Marino {
413*e4b17023SJohn Marino tree lhs, rhs;
414*e4b17023SJohn Marino
415*e4b17023SJohn Marino lhs = gimple_assign_lhs (stmt);
416*e4b17023SJohn Marino rhs = valueize_val (gimple_assign_rhs1 (stmt));
417*e4b17023SJohn Marino
418*e4b17023SJohn Marino if (TREE_CODE (lhs) == SSA_NAME)
419*e4b17023SJohn Marino {
420*e4b17023SJohn Marino /* Straight copy between two SSA names. First, make sure that
421*e4b17023SJohn Marino we can propagate the RHS into uses of LHS. */
422*e4b17023SJohn Marino if (!may_propagate_copy (lhs, rhs))
423*e4b17023SJohn Marino return SSA_PROP_VARYING;
424*e4b17023SJohn Marino
425*e4b17023SJohn Marino *result_p = lhs;
426*e4b17023SJohn Marino if (set_copy_of_val (*result_p, rhs))
427*e4b17023SJohn Marino return SSA_PROP_INTERESTING;
428*e4b17023SJohn Marino else
429*e4b17023SJohn Marino return SSA_PROP_NOT_INTERESTING;
430*e4b17023SJohn Marino }
431*e4b17023SJohn Marino
432*e4b17023SJohn Marino return SSA_PROP_VARYING;
433*e4b17023SJohn Marino }
434*e4b17023SJohn Marino
435*e4b17023SJohn Marino
436*e4b17023SJohn Marino /* Visit the GIMPLE_COND STMT. Return SSA_PROP_INTERESTING
437*e4b17023SJohn Marino if it can determine which edge will be taken. Otherwise, return
438*e4b17023SJohn Marino SSA_PROP_VARYING. */
439*e4b17023SJohn Marino
440*e4b17023SJohn Marino static enum ssa_prop_result
copy_prop_visit_cond_stmt(gimple stmt,edge * taken_edge_p)441*e4b17023SJohn Marino copy_prop_visit_cond_stmt (gimple stmt, edge *taken_edge_p)
442*e4b17023SJohn Marino {
443*e4b17023SJohn Marino enum ssa_prop_result retval = SSA_PROP_VARYING;
444*e4b17023SJohn Marino location_t loc = gimple_location (stmt);
445*e4b17023SJohn Marino
446*e4b17023SJohn Marino tree op0 = gimple_cond_lhs (stmt);
447*e4b17023SJohn Marino tree op1 = gimple_cond_rhs (stmt);
448*e4b17023SJohn Marino
449*e4b17023SJohn Marino /* The only conditionals that we may be able to compute statically
450*e4b17023SJohn Marino are predicates involving two SSA_NAMEs. */
451*e4b17023SJohn Marino if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
452*e4b17023SJohn Marino {
453*e4b17023SJohn Marino op0 = valueize_val (op0);
454*e4b17023SJohn Marino op1 = valueize_val (op1);
455*e4b17023SJohn Marino
456*e4b17023SJohn Marino /* See if we can determine the predicate's value. */
457*e4b17023SJohn Marino if (dump_file && (dump_flags & TDF_DETAILS))
458*e4b17023SJohn Marino {
459*e4b17023SJohn Marino fprintf (dump_file, "Trying to determine truth value of ");
460*e4b17023SJohn Marino fprintf (dump_file, "predicate ");
461*e4b17023SJohn Marino print_gimple_stmt (dump_file, stmt, 0, 0);
462*e4b17023SJohn Marino }
463*e4b17023SJohn Marino
464*e4b17023SJohn Marino /* We can fold COND and get a useful result only when we have
465*e4b17023SJohn Marino the same SSA_NAME on both sides of a comparison operator. */
466*e4b17023SJohn Marino if (op0 == op1)
467*e4b17023SJohn Marino {
468*e4b17023SJohn Marino tree folded_cond = fold_binary_loc (loc, gimple_cond_code (stmt),
469*e4b17023SJohn Marino boolean_type_node, op0, op1);
470*e4b17023SJohn Marino if (folded_cond)
471*e4b17023SJohn Marino {
472*e4b17023SJohn Marino basic_block bb = gimple_bb (stmt);
473*e4b17023SJohn Marino *taken_edge_p = find_taken_edge (bb, folded_cond);
474*e4b17023SJohn Marino if (*taken_edge_p)
475*e4b17023SJohn Marino retval = SSA_PROP_INTERESTING;
476*e4b17023SJohn Marino }
477*e4b17023SJohn Marino }
478*e4b17023SJohn Marino }
479*e4b17023SJohn Marino
480*e4b17023SJohn Marino if (dump_file && (dump_flags & TDF_DETAILS) && *taken_edge_p)
481*e4b17023SJohn Marino fprintf (dump_file, "\nConditional will always take edge %d->%d\n",
482*e4b17023SJohn Marino (*taken_edge_p)->src->index, (*taken_edge_p)->dest->index);
483*e4b17023SJohn Marino
484*e4b17023SJohn Marino return retval;
485*e4b17023SJohn Marino }
486*e4b17023SJohn Marino
487*e4b17023SJohn Marino
488*e4b17023SJohn Marino /* Evaluate statement STMT. If the statement produces a new output
489*e4b17023SJohn Marino value, return SSA_PROP_INTERESTING and store the SSA_NAME holding
490*e4b17023SJohn Marino the new value in *RESULT_P.
491*e4b17023SJohn Marino
492*e4b17023SJohn Marino If STMT is a conditional branch and we can determine its truth
493*e4b17023SJohn Marino value, set *TAKEN_EDGE_P accordingly.
494*e4b17023SJohn Marino
495*e4b17023SJohn Marino If the new value produced by STMT is varying, return
496*e4b17023SJohn Marino SSA_PROP_VARYING. */
497*e4b17023SJohn Marino
498*e4b17023SJohn Marino static enum ssa_prop_result
copy_prop_visit_stmt(gimple stmt,edge * taken_edge_p,tree * result_p)499*e4b17023SJohn Marino copy_prop_visit_stmt (gimple stmt, edge *taken_edge_p, tree *result_p)
500*e4b17023SJohn Marino {
501*e4b17023SJohn Marino enum ssa_prop_result retval;
502*e4b17023SJohn Marino
503*e4b17023SJohn Marino if (dump_file && (dump_flags & TDF_DETAILS))
504*e4b17023SJohn Marino {
505*e4b17023SJohn Marino fprintf (dump_file, "\nVisiting statement:\n");
506*e4b17023SJohn Marino print_gimple_stmt (dump_file, stmt, 0, dump_flags);
507*e4b17023SJohn Marino fprintf (dump_file, "\n");
508*e4b17023SJohn Marino }
509*e4b17023SJohn Marino
510*e4b17023SJohn Marino if (gimple_assign_single_p (stmt)
511*e4b17023SJohn Marino && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME
512*e4b17023SJohn Marino && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
513*e4b17023SJohn Marino || is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
514*e4b17023SJohn Marino {
515*e4b17023SJohn Marino /* If the statement is a copy assignment, evaluate its RHS to
516*e4b17023SJohn Marino see if the lattice value of its output has changed. */
517*e4b17023SJohn Marino retval = copy_prop_visit_assignment (stmt, result_p);
518*e4b17023SJohn Marino }
519*e4b17023SJohn Marino else if (gimple_code (stmt) == GIMPLE_COND)
520*e4b17023SJohn Marino {
521*e4b17023SJohn Marino /* See if we can determine which edge goes out of a conditional
522*e4b17023SJohn Marino jump. */
523*e4b17023SJohn Marino retval = copy_prop_visit_cond_stmt (stmt, taken_edge_p);
524*e4b17023SJohn Marino }
525*e4b17023SJohn Marino else
526*e4b17023SJohn Marino retval = SSA_PROP_VARYING;
527*e4b17023SJohn Marino
528*e4b17023SJohn Marino if (retval == SSA_PROP_VARYING)
529*e4b17023SJohn Marino {
530*e4b17023SJohn Marino tree def;
531*e4b17023SJohn Marino ssa_op_iter i;
532*e4b17023SJohn Marino
533*e4b17023SJohn Marino /* Any other kind of statement is not interesting for constant
534*e4b17023SJohn Marino propagation and, therefore, not worth simulating. */
535*e4b17023SJohn Marino if (dump_file && (dump_flags & TDF_DETAILS))
536*e4b17023SJohn Marino fprintf (dump_file, "No interesting values produced.\n");
537*e4b17023SJohn Marino
538*e4b17023SJohn Marino /* The assignment is not a copy operation. Don't visit this
539*e4b17023SJohn Marino statement again and mark all the definitions in the statement
540*e4b17023SJohn Marino to be copies of nothing. */
541*e4b17023SJohn Marino FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_ALL_DEFS)
542*e4b17023SJohn Marino set_copy_of_val (def, def);
543*e4b17023SJohn Marino }
544*e4b17023SJohn Marino
545*e4b17023SJohn Marino return retval;
546*e4b17023SJohn Marino }
547*e4b17023SJohn Marino
548*e4b17023SJohn Marino
549*e4b17023SJohn Marino /* Visit PHI node PHI. If all the arguments produce the same value,
550*e4b17023SJohn Marino set it to be the value of the LHS of PHI. */
551*e4b17023SJohn Marino
552*e4b17023SJohn Marino static enum ssa_prop_result
copy_prop_visit_phi_node(gimple phi)553*e4b17023SJohn Marino copy_prop_visit_phi_node (gimple phi)
554*e4b17023SJohn Marino {
555*e4b17023SJohn Marino enum ssa_prop_result retval;
556*e4b17023SJohn Marino unsigned i;
557*e4b17023SJohn Marino prop_value_t phi_val = { NULL_TREE };
558*e4b17023SJohn Marino
559*e4b17023SJohn Marino tree lhs = gimple_phi_result (phi);
560*e4b17023SJohn Marino
561*e4b17023SJohn Marino if (dump_file && (dump_flags & TDF_DETAILS))
562*e4b17023SJohn Marino {
563*e4b17023SJohn Marino fprintf (dump_file, "\nVisiting PHI node: ");
564*e4b17023SJohn Marino print_gimple_stmt (dump_file, phi, 0, dump_flags);
565*e4b17023SJohn Marino }
566*e4b17023SJohn Marino
567*e4b17023SJohn Marino for (i = 0; i < gimple_phi_num_args (phi); i++)
568*e4b17023SJohn Marino {
569*e4b17023SJohn Marino prop_value_t *arg_val;
570*e4b17023SJohn Marino tree arg_value;
571*e4b17023SJohn Marino tree arg = gimple_phi_arg_def (phi, i);
572*e4b17023SJohn Marino edge e = gimple_phi_arg_edge (phi, i);
573*e4b17023SJohn Marino
574*e4b17023SJohn Marino /* We don't care about values flowing through non-executable
575*e4b17023SJohn Marino edges. */
576*e4b17023SJohn Marino if (!(e->flags & EDGE_EXECUTABLE))
577*e4b17023SJohn Marino continue;
578*e4b17023SJohn Marino
579*e4b17023SJohn Marino /* Names that flow through abnormal edges cannot be used to
580*e4b17023SJohn Marino derive copies. */
581*e4b17023SJohn Marino if (TREE_CODE (arg) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (arg))
582*e4b17023SJohn Marino {
583*e4b17023SJohn Marino phi_val.value = lhs;
584*e4b17023SJohn Marino break;
585*e4b17023SJohn Marino }
586*e4b17023SJohn Marino
587*e4b17023SJohn Marino if (dump_file && (dump_flags & TDF_DETAILS))
588*e4b17023SJohn Marino {
589*e4b17023SJohn Marino fprintf (dump_file, "\tArgument #%d: ", i);
590*e4b17023SJohn Marino dump_copy_of (dump_file, arg);
591*e4b17023SJohn Marino fprintf (dump_file, "\n");
592*e4b17023SJohn Marino }
593*e4b17023SJohn Marino
594*e4b17023SJohn Marino if (TREE_CODE (arg) == SSA_NAME)
595*e4b17023SJohn Marino {
596*e4b17023SJohn Marino arg_val = get_copy_of_val (arg);
597*e4b17023SJohn Marino
598*e4b17023SJohn Marino /* If we didn't visit the definition of arg yet treat it as
599*e4b17023SJohn Marino UNDEFINED. This also handles PHI arguments that are the
600*e4b17023SJohn Marino same as lhs. We'll come here again. */
601*e4b17023SJohn Marino if (!arg_val->value)
602*e4b17023SJohn Marino continue;
603*e4b17023SJohn Marino
604*e4b17023SJohn Marino arg_value = arg_val->value;
605*e4b17023SJohn Marino }
606*e4b17023SJohn Marino else
607*e4b17023SJohn Marino arg_value = valueize_val (arg);
608*e4b17023SJohn Marino
609*e4b17023SJohn Marino /* Avoid copy propagation from an inner into an outer loop.
610*e4b17023SJohn Marino Otherwise, this may move loop variant variables outside of
611*e4b17023SJohn Marino their loops and prevent coalescing opportunities. If the
612*e4b17023SJohn Marino value was loop invariant, it will be hoisted by LICM and
613*e4b17023SJohn Marino exposed for copy propagation.
614*e4b17023SJohn Marino ??? The value will be always loop invariant.
615*e4b17023SJohn Marino In loop-closed SSA form do not copy-propagate through
616*e4b17023SJohn Marino PHI nodes in blocks with a loop exit edge predecessor. */
617*e4b17023SJohn Marino if (current_loops
618*e4b17023SJohn Marino && TREE_CODE (arg_value) == SSA_NAME
619*e4b17023SJohn Marino && (loop_depth_of_name (arg_value) > loop_depth_of_name (lhs)
620*e4b17023SJohn Marino || (loops_state_satisfies_p (LOOP_CLOSED_SSA)
621*e4b17023SJohn Marino && loop_exit_edge_p (e->src->loop_father, e))))
622*e4b17023SJohn Marino {
623*e4b17023SJohn Marino phi_val.value = lhs;
624*e4b17023SJohn Marino break;
625*e4b17023SJohn Marino }
626*e4b17023SJohn Marino
627*e4b17023SJohn Marino /* If the LHS didn't have a value yet, make it a copy of the
628*e4b17023SJohn Marino first argument we find. */
629*e4b17023SJohn Marino if (phi_val.value == NULL_TREE)
630*e4b17023SJohn Marino {
631*e4b17023SJohn Marino phi_val.value = arg_value;
632*e4b17023SJohn Marino continue;
633*e4b17023SJohn Marino }
634*e4b17023SJohn Marino
635*e4b17023SJohn Marino /* If PHI_VAL and ARG don't have a common copy-of chain, then
636*e4b17023SJohn Marino this PHI node cannot be a copy operation. */
637*e4b17023SJohn Marino if (phi_val.value != arg_value
638*e4b17023SJohn Marino && !operand_equal_p (phi_val.value, arg_value, 0))
639*e4b17023SJohn Marino {
640*e4b17023SJohn Marino phi_val.value = lhs;
641*e4b17023SJohn Marino break;
642*e4b17023SJohn Marino }
643*e4b17023SJohn Marino }
644*e4b17023SJohn Marino
645*e4b17023SJohn Marino if (phi_val.value
646*e4b17023SJohn Marino && may_propagate_copy (lhs, phi_val.value)
647*e4b17023SJohn Marino && set_copy_of_val (lhs, phi_val.value))
648*e4b17023SJohn Marino retval = (phi_val.value != lhs) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
649*e4b17023SJohn Marino else
650*e4b17023SJohn Marino retval = SSA_PROP_NOT_INTERESTING;
651*e4b17023SJohn Marino
652*e4b17023SJohn Marino if (dump_file && (dump_flags & TDF_DETAILS))
653*e4b17023SJohn Marino {
654*e4b17023SJohn Marino fprintf (dump_file, "PHI node ");
655*e4b17023SJohn Marino dump_copy_of (dump_file, lhs);
656*e4b17023SJohn Marino fprintf (dump_file, "\nTelling the propagator to ");
657*e4b17023SJohn Marino if (retval == SSA_PROP_INTERESTING)
658*e4b17023SJohn Marino fprintf (dump_file, "add SSA edges out of this PHI and continue.");
659*e4b17023SJohn Marino else if (retval == SSA_PROP_VARYING)
660*e4b17023SJohn Marino fprintf (dump_file, "add SSA edges out of this PHI and never visit again.");
661*e4b17023SJohn Marino else
662*e4b17023SJohn Marino fprintf (dump_file, "do nothing with SSA edges and keep iterating.");
663*e4b17023SJohn Marino fprintf (dump_file, "\n\n");
664*e4b17023SJohn Marino }
665*e4b17023SJohn Marino
666*e4b17023SJohn Marino return retval;
667*e4b17023SJohn Marino }
668*e4b17023SJohn Marino
669*e4b17023SJohn Marino
670*e4b17023SJohn Marino /* Initialize structures used for copy propagation. */
671*e4b17023SJohn Marino
672*e4b17023SJohn Marino static void
init_copy_prop(void)673*e4b17023SJohn Marino init_copy_prop (void)
674*e4b17023SJohn Marino {
675*e4b17023SJohn Marino basic_block bb;
676*e4b17023SJohn Marino
677*e4b17023SJohn Marino copy_of = XCNEWVEC (prop_value_t, num_ssa_names);
678*e4b17023SJohn Marino
679*e4b17023SJohn Marino FOR_EACH_BB (bb)
680*e4b17023SJohn Marino {
681*e4b17023SJohn Marino gimple_stmt_iterator si;
682*e4b17023SJohn Marino int depth = bb->loop_depth;
683*e4b17023SJohn Marino
684*e4b17023SJohn Marino for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
685*e4b17023SJohn Marino {
686*e4b17023SJohn Marino gimple stmt = gsi_stmt (si);
687*e4b17023SJohn Marino ssa_op_iter iter;
688*e4b17023SJohn Marino tree def;
689*e4b17023SJohn Marino
690*e4b17023SJohn Marino /* The only statements that we care about are those that may
691*e4b17023SJohn Marino generate useful copies. We also need to mark conditional
692*e4b17023SJohn Marino jumps so that their outgoing edges are added to the work
693*e4b17023SJohn Marino lists of the propagator.
694*e4b17023SJohn Marino
695*e4b17023SJohn Marino Avoid copy propagation from an inner into an outer loop.
696*e4b17023SJohn Marino Otherwise, this may move loop variant variables outside of
697*e4b17023SJohn Marino their loops and prevent coalescing opportunities. If the
698*e4b17023SJohn Marino value was loop invariant, it will be hoisted by LICM and
699*e4b17023SJohn Marino exposed for copy propagation.
700*e4b17023SJohn Marino ??? This doesn't make sense. */
701*e4b17023SJohn Marino if (stmt_ends_bb_p (stmt))
702*e4b17023SJohn Marino prop_set_simulate_again (stmt, true);
703*e4b17023SJohn Marino else if (stmt_may_generate_copy (stmt)
704*e4b17023SJohn Marino /* Since we are iterating over the statements in
705*e4b17023SJohn Marino BB, not the phi nodes, STMT will always be an
706*e4b17023SJohn Marino assignment. */
707*e4b17023SJohn Marino && loop_depth_of_name (gimple_assign_rhs1 (stmt)) <= depth)
708*e4b17023SJohn Marino prop_set_simulate_again (stmt, true);
709*e4b17023SJohn Marino else
710*e4b17023SJohn Marino prop_set_simulate_again (stmt, false);
711*e4b17023SJohn Marino
712*e4b17023SJohn Marino /* Mark all the outputs of this statement as not being
713*e4b17023SJohn Marino the copy of anything. */
714*e4b17023SJohn Marino FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
715*e4b17023SJohn Marino if (!prop_simulate_again_p (stmt))
716*e4b17023SJohn Marino set_copy_of_val (def, def);
717*e4b17023SJohn Marino }
718*e4b17023SJohn Marino
719*e4b17023SJohn Marino for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
720*e4b17023SJohn Marino {
721*e4b17023SJohn Marino gimple phi = gsi_stmt (si);
722*e4b17023SJohn Marino tree def;
723*e4b17023SJohn Marino
724*e4b17023SJohn Marino def = gimple_phi_result (phi);
725*e4b17023SJohn Marino if (!is_gimple_reg (def))
726*e4b17023SJohn Marino prop_set_simulate_again (phi, false);
727*e4b17023SJohn Marino else
728*e4b17023SJohn Marino prop_set_simulate_again (phi, true);
729*e4b17023SJohn Marino
730*e4b17023SJohn Marino if (!prop_simulate_again_p (phi))
731*e4b17023SJohn Marino set_copy_of_val (def, def);
732*e4b17023SJohn Marino }
733*e4b17023SJohn Marino }
734*e4b17023SJohn Marino }
735*e4b17023SJohn Marino
736*e4b17023SJohn Marino /* Callback for substitute_and_fold to get at the final copy-of values. */
737*e4b17023SJohn Marino
738*e4b17023SJohn Marino static tree
get_value(tree name)739*e4b17023SJohn Marino get_value (tree name)
740*e4b17023SJohn Marino {
741*e4b17023SJohn Marino tree val = copy_of[SSA_NAME_VERSION (name)].value;
742*e4b17023SJohn Marino if (val && val != name)
743*e4b17023SJohn Marino return val;
744*e4b17023SJohn Marino return NULL_TREE;
745*e4b17023SJohn Marino }
746*e4b17023SJohn Marino
747*e4b17023SJohn Marino /* Deallocate memory used in copy propagation and do final
748*e4b17023SJohn Marino substitution. */
749*e4b17023SJohn Marino
750*e4b17023SJohn Marino static void
fini_copy_prop(void)751*e4b17023SJohn Marino fini_copy_prop (void)
752*e4b17023SJohn Marino {
753*e4b17023SJohn Marino unsigned i;
754*e4b17023SJohn Marino
755*e4b17023SJohn Marino /* Set the final copy-of value for each variable by traversing the
756*e4b17023SJohn Marino copy-of chains. */
757*e4b17023SJohn Marino for (i = 1; i < num_ssa_names; i++)
758*e4b17023SJohn Marino {
759*e4b17023SJohn Marino tree var = ssa_name (i);
760*e4b17023SJohn Marino if (!var
761*e4b17023SJohn Marino || !copy_of[i].value
762*e4b17023SJohn Marino || copy_of[i].value == var)
763*e4b17023SJohn Marino continue;
764*e4b17023SJohn Marino
765*e4b17023SJohn Marino /* In theory the points-to solution of all members of the
766*e4b17023SJohn Marino copy chain is their intersection. For now we do not bother
767*e4b17023SJohn Marino to compute this but only make sure we do not lose points-to
768*e4b17023SJohn Marino information completely by setting the points-to solution
769*e4b17023SJohn Marino of the representative to the first solution we find if
770*e4b17023SJohn Marino it doesn't have one already. */
771*e4b17023SJohn Marino if (copy_of[i].value != var
772*e4b17023SJohn Marino && TREE_CODE (copy_of[i].value) == SSA_NAME
773*e4b17023SJohn Marino && POINTER_TYPE_P (TREE_TYPE (var))
774*e4b17023SJohn Marino && SSA_NAME_PTR_INFO (var)
775*e4b17023SJohn Marino && !SSA_NAME_PTR_INFO (copy_of[i].value))
776*e4b17023SJohn Marino duplicate_ssa_name_ptr_info (copy_of[i].value, SSA_NAME_PTR_INFO (var));
777*e4b17023SJohn Marino }
778*e4b17023SJohn Marino
779*e4b17023SJohn Marino /* Don't do DCE if we have loops. That's the simplest way to not
780*e4b17023SJohn Marino destroy the scev cache. */
781*e4b17023SJohn Marino substitute_and_fold (get_value, NULL, !current_loops);
782*e4b17023SJohn Marino
783*e4b17023SJohn Marino free (copy_of);
784*e4b17023SJohn Marino }
785*e4b17023SJohn Marino
786*e4b17023SJohn Marino
787*e4b17023SJohn Marino /* Main entry point to the copy propagator.
788*e4b17023SJohn Marino
789*e4b17023SJohn Marino PHIS_ONLY is true if we should only consider PHI nodes as generating
790*e4b17023SJohn Marino copy propagation opportunities.
791*e4b17023SJohn Marino
792*e4b17023SJohn Marino The algorithm propagates the value COPY-OF using ssa_propagate. For
793*e4b17023SJohn Marino every variable X_i, COPY-OF(X_i) indicates which variable is X_i created
794*e4b17023SJohn Marino from. The following example shows how the algorithm proceeds at a
795*e4b17023SJohn Marino high level:
796*e4b17023SJohn Marino
797*e4b17023SJohn Marino 1 a_24 = x_1
798*e4b17023SJohn Marino 2 a_2 = PHI <a_24, x_1>
799*e4b17023SJohn Marino 3 a_5 = PHI <a_2>
800*e4b17023SJohn Marino 4 x_1 = PHI <x_298, a_5, a_2>
801*e4b17023SJohn Marino
802*e4b17023SJohn Marino The end result should be that a_2, a_5, a_24 and x_1 are a copy of
803*e4b17023SJohn Marino x_298. Propagation proceeds as follows.
804*e4b17023SJohn Marino
805*e4b17023SJohn Marino Visit #1: a_24 is copy-of x_1. Value changed.
806*e4b17023SJohn Marino Visit #2: a_2 is copy-of x_1. Value changed.
807*e4b17023SJohn Marino Visit #3: a_5 is copy-of x_1. Value changed.
808*e4b17023SJohn Marino Visit #4: x_1 is copy-of x_298. Value changed.
809*e4b17023SJohn Marino Visit #1: a_24 is copy-of x_298. Value changed.
810*e4b17023SJohn Marino Visit #2: a_2 is copy-of x_298. Value changed.
811*e4b17023SJohn Marino Visit #3: a_5 is copy-of x_298. Value changed.
812*e4b17023SJohn Marino Visit #4: x_1 is copy-of x_298. Stable state reached.
813*e4b17023SJohn Marino
814*e4b17023SJohn Marino When visiting PHI nodes, we only consider arguments that flow
815*e4b17023SJohn Marino through edges marked executable by the propagation engine. So,
816*e4b17023SJohn Marino when visiting statement #2 for the first time, we will only look at
817*e4b17023SJohn Marino the first argument (a_24) and optimistically assume that its value
818*e4b17023SJohn Marino is the copy of a_24 (x_1). */
819*e4b17023SJohn Marino
820*e4b17023SJohn Marino static unsigned int
execute_copy_prop(void)821*e4b17023SJohn Marino execute_copy_prop (void)
822*e4b17023SJohn Marino {
823*e4b17023SJohn Marino init_copy_prop ();
824*e4b17023SJohn Marino ssa_propagate (copy_prop_visit_stmt, copy_prop_visit_phi_node);
825*e4b17023SJohn Marino fini_copy_prop ();
826*e4b17023SJohn Marino return 0;
827*e4b17023SJohn Marino }
828*e4b17023SJohn Marino
829*e4b17023SJohn Marino static bool
gate_copy_prop(void)830*e4b17023SJohn Marino gate_copy_prop (void)
831*e4b17023SJohn Marino {
832*e4b17023SJohn Marino return flag_tree_copy_prop != 0;
833*e4b17023SJohn Marino }
834*e4b17023SJohn Marino
835*e4b17023SJohn Marino struct gimple_opt_pass pass_copy_prop =
836*e4b17023SJohn Marino {
837*e4b17023SJohn Marino {
838*e4b17023SJohn Marino GIMPLE_PASS,
839*e4b17023SJohn Marino "copyprop", /* name */
840*e4b17023SJohn Marino gate_copy_prop, /* gate */
841*e4b17023SJohn Marino execute_copy_prop, /* execute */
842*e4b17023SJohn Marino NULL, /* sub */
843*e4b17023SJohn Marino NULL, /* next */
844*e4b17023SJohn Marino 0, /* static_pass_number */
845*e4b17023SJohn Marino TV_TREE_COPY_PROP, /* tv_id */
846*e4b17023SJohn Marino PROP_ssa | PROP_cfg, /* properties_required */
847*e4b17023SJohn Marino 0, /* properties_provided */
848*e4b17023SJohn Marino 0, /* properties_destroyed */
849*e4b17023SJohn Marino 0, /* todo_flags_start */
850*e4b17023SJohn Marino TODO_cleanup_cfg
851*e4b17023SJohn Marino | TODO_ggc_collect
852*e4b17023SJohn Marino | TODO_verify_ssa
853*e4b17023SJohn Marino | TODO_update_ssa /* todo_flags_finish */
854*e4b17023SJohn Marino }
855*e4b17023SJohn Marino };
856