xref: /dflybsd-src/contrib/gcc-4.7/gcc/tree-ssa-uncprop.c (revision 04febcfb30580676d3e95f58a16c5137ee478b32)
1*e4b17023SJohn Marino /* Routines for discovering and unpropagating edge equivalences.
2*e4b17023SJohn Marino    Copyright (C) 2005, 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 "timevar.h"
32*e4b17023SJohn Marino #include "tree-dump.h"
33*e4b17023SJohn Marino #include "tree-flow.h"
34*e4b17023SJohn Marino #include "domwalk.h"
35*e4b17023SJohn Marino #include "tree-pass.h"
36*e4b17023SJohn Marino #include "tree-ssa-propagate.h"
37*e4b17023SJohn Marino #include "langhooks.h"
38*e4b17023SJohn Marino 
39*e4b17023SJohn Marino /* The basic structure describing an equivalency created by traversing
40*e4b17023SJohn Marino    an edge.  Traversing the edge effectively means that we can assume
41*e4b17023SJohn Marino    that we've seen an assignment LHS = RHS.  */
42*e4b17023SJohn Marino struct edge_equivalency
43*e4b17023SJohn Marino {
44*e4b17023SJohn Marino   tree rhs;
45*e4b17023SJohn Marino   tree lhs;
46*e4b17023SJohn Marino };
47*e4b17023SJohn Marino 
48*e4b17023SJohn Marino /* This routine finds and records edge equivalences for every edge
49*e4b17023SJohn Marino    in the CFG.
50*e4b17023SJohn Marino 
51*e4b17023SJohn Marino    When complete, each edge that creates an equivalency will have an
52*e4b17023SJohn Marino    EDGE_EQUIVALENCY structure hanging off the edge's AUX field.
53*e4b17023SJohn Marino    The caller is responsible for freeing the AUX fields.  */
54*e4b17023SJohn Marino 
55*e4b17023SJohn Marino static void
associate_equivalences_with_edges(void)56*e4b17023SJohn Marino associate_equivalences_with_edges (void)
57*e4b17023SJohn Marino {
58*e4b17023SJohn Marino   basic_block bb;
59*e4b17023SJohn Marino 
60*e4b17023SJohn Marino   /* Walk over each block.  If the block ends with a control statement,
61*e4b17023SJohn Marino      then it might create a useful equivalence.  */
62*e4b17023SJohn Marino   FOR_EACH_BB (bb)
63*e4b17023SJohn Marino     {
64*e4b17023SJohn Marino       gimple_stmt_iterator gsi = gsi_last_bb (bb);
65*e4b17023SJohn Marino       gimple stmt;
66*e4b17023SJohn Marino 
67*e4b17023SJohn Marino       /* If the block does not end with a COND_EXPR or SWITCH_EXPR
68*e4b17023SJohn Marino 	 then there is nothing to do.  */
69*e4b17023SJohn Marino       if (gsi_end_p (gsi))
70*e4b17023SJohn Marino 	continue;
71*e4b17023SJohn Marino 
72*e4b17023SJohn Marino       stmt = gsi_stmt (gsi);
73*e4b17023SJohn Marino 
74*e4b17023SJohn Marino       if (!stmt)
75*e4b17023SJohn Marino 	continue;
76*e4b17023SJohn Marino 
77*e4b17023SJohn Marino       /* A COND_EXPR may create an equivalency in a variety of different
78*e4b17023SJohn Marino 	 ways.  */
79*e4b17023SJohn Marino       if (gimple_code (stmt) == GIMPLE_COND)
80*e4b17023SJohn Marino 	{
81*e4b17023SJohn Marino 	  edge true_edge;
82*e4b17023SJohn Marino 	  edge false_edge;
83*e4b17023SJohn Marino 	  struct edge_equivalency *equivalency;
84*e4b17023SJohn Marino 	  enum tree_code code = gimple_cond_code (stmt);
85*e4b17023SJohn Marino 
86*e4b17023SJohn Marino 	  extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
87*e4b17023SJohn Marino 
88*e4b17023SJohn Marino 	  /* Equality tests may create one or two equivalences.  */
89*e4b17023SJohn Marino 	  if (code == EQ_EXPR || code == NE_EXPR)
90*e4b17023SJohn Marino 	    {
91*e4b17023SJohn Marino 	      tree op0 = gimple_cond_lhs (stmt);
92*e4b17023SJohn Marino 	      tree op1 = gimple_cond_rhs (stmt);
93*e4b17023SJohn Marino 
94*e4b17023SJohn Marino 	      /* Special case comparing booleans against a constant as we
95*e4b17023SJohn Marino 		 know the value of OP0 on both arms of the branch.  i.e., we
96*e4b17023SJohn Marino 		 can record an equivalence for OP0 rather than COND.  */
97*e4b17023SJohn Marino 	      if (TREE_CODE (op0) == SSA_NAME
98*e4b17023SJohn Marino 		  && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0)
99*e4b17023SJohn Marino 		  && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
100*e4b17023SJohn Marino 		  && is_gimple_min_invariant (op1))
101*e4b17023SJohn Marino 		{
102*e4b17023SJohn Marino 		  if (code == EQ_EXPR)
103*e4b17023SJohn Marino 		    {
104*e4b17023SJohn Marino 		      equivalency = XNEW (struct edge_equivalency);
105*e4b17023SJohn Marino 		      equivalency->lhs = op0;
106*e4b17023SJohn Marino 		      equivalency->rhs = (integer_zerop (op1)
107*e4b17023SJohn Marino 					  ? boolean_false_node
108*e4b17023SJohn Marino 					  : boolean_true_node);
109*e4b17023SJohn Marino 		      true_edge->aux = equivalency;
110*e4b17023SJohn Marino 
111*e4b17023SJohn Marino 		      equivalency = XNEW (struct edge_equivalency);
112*e4b17023SJohn Marino 		      equivalency->lhs = op0;
113*e4b17023SJohn Marino 		      equivalency->rhs = (integer_zerop (op1)
114*e4b17023SJohn Marino 					  ? boolean_true_node
115*e4b17023SJohn Marino 					  : boolean_false_node);
116*e4b17023SJohn Marino 		      false_edge->aux = equivalency;
117*e4b17023SJohn Marino 		    }
118*e4b17023SJohn Marino 		  else
119*e4b17023SJohn Marino 		    {
120*e4b17023SJohn Marino 		      equivalency = XNEW (struct edge_equivalency);
121*e4b17023SJohn Marino 		      equivalency->lhs = op0;
122*e4b17023SJohn Marino 		      equivalency->rhs = (integer_zerop (op1)
123*e4b17023SJohn Marino 					  ? boolean_true_node
124*e4b17023SJohn Marino 					  : boolean_false_node);
125*e4b17023SJohn Marino 		      true_edge->aux = equivalency;
126*e4b17023SJohn Marino 
127*e4b17023SJohn Marino 		      equivalency = XNEW (struct edge_equivalency);
128*e4b17023SJohn Marino 		      equivalency->lhs = op0;
129*e4b17023SJohn Marino 		      equivalency->rhs = (integer_zerop (op1)
130*e4b17023SJohn Marino 					  ? boolean_false_node
131*e4b17023SJohn Marino 					  : boolean_true_node);
132*e4b17023SJohn Marino 		      false_edge->aux = equivalency;
133*e4b17023SJohn Marino 		    }
134*e4b17023SJohn Marino 		}
135*e4b17023SJohn Marino 
136*e4b17023SJohn Marino 	      else if (TREE_CODE (op0) == SSA_NAME
137*e4b17023SJohn Marino 		       && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0)
138*e4b17023SJohn Marino 		       && (is_gimple_min_invariant (op1)
139*e4b17023SJohn Marino 			   || (TREE_CODE (op1) == SSA_NAME
140*e4b17023SJohn Marino 			       && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op1))))
141*e4b17023SJohn Marino 		{
142*e4b17023SJohn Marino 		  /* For IEEE, -0.0 == 0.0, so we don't necessarily know
143*e4b17023SJohn Marino 		     the sign of a variable compared against zero.  If
144*e4b17023SJohn Marino 		     we're honoring signed zeros, then we cannot record
145*e4b17023SJohn Marino 		     this value unless we know that the value is nonzero.  */
146*e4b17023SJohn Marino 		  if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op0)))
147*e4b17023SJohn Marino 		      && (TREE_CODE (op1) != REAL_CST
148*e4b17023SJohn Marino 			  || REAL_VALUES_EQUAL (dconst0, TREE_REAL_CST (op1))))
149*e4b17023SJohn Marino 		    continue;
150*e4b17023SJohn Marino 
151*e4b17023SJohn Marino 		  equivalency = XNEW (struct edge_equivalency);
152*e4b17023SJohn Marino 		  equivalency->lhs = op0;
153*e4b17023SJohn Marino 		  equivalency->rhs = op1;
154*e4b17023SJohn Marino 		  if (code == EQ_EXPR)
155*e4b17023SJohn Marino 		    true_edge->aux = equivalency;
156*e4b17023SJohn Marino 		  else
157*e4b17023SJohn Marino 		    false_edge->aux = equivalency;
158*e4b17023SJohn Marino 
159*e4b17023SJohn Marino 		}
160*e4b17023SJohn Marino 	    }
161*e4b17023SJohn Marino 
162*e4b17023SJohn Marino 	  /* ??? TRUTH_NOT_EXPR can create an equivalence too.  */
163*e4b17023SJohn Marino 	}
164*e4b17023SJohn Marino 
165*e4b17023SJohn Marino       /* For a SWITCH_EXPR, a case label which represents a single
166*e4b17023SJohn Marino 	 value and which is the only case label which reaches the
167*e4b17023SJohn Marino 	 target block creates an equivalence.  */
168*e4b17023SJohn Marino       else if (gimple_code (stmt) == GIMPLE_SWITCH)
169*e4b17023SJohn Marino 	{
170*e4b17023SJohn Marino 	  tree cond = gimple_switch_index (stmt);
171*e4b17023SJohn Marino 
172*e4b17023SJohn Marino 	  if (TREE_CODE (cond) == SSA_NAME
173*e4b17023SJohn Marino 	      && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (cond))
174*e4b17023SJohn Marino 	    {
175*e4b17023SJohn Marino 	      int i, n_labels = gimple_switch_num_labels (stmt);
176*e4b17023SJohn Marino 	      tree *info = XCNEWVEC (tree, last_basic_block);
177*e4b17023SJohn Marino 
178*e4b17023SJohn Marino 	      /* Walk over the case label vector.  Record blocks
179*e4b17023SJohn Marino 		 which are reached by a single case label which represents
180*e4b17023SJohn Marino 		 a single value.  */
181*e4b17023SJohn Marino 	      for (i = 0; i < n_labels; i++)
182*e4b17023SJohn Marino 		{
183*e4b17023SJohn Marino 		  tree label = gimple_switch_label (stmt, i);
184*e4b17023SJohn Marino 		  basic_block bb = label_to_block (CASE_LABEL (label));
185*e4b17023SJohn Marino 
186*e4b17023SJohn Marino 		  if (CASE_HIGH (label)
187*e4b17023SJohn Marino 		      || !CASE_LOW (label)
188*e4b17023SJohn Marino 		      || info[bb->index])
189*e4b17023SJohn Marino 		    info[bb->index] = error_mark_node;
190*e4b17023SJohn Marino 		  else
191*e4b17023SJohn Marino 		    info[bb->index] = label;
192*e4b17023SJohn Marino 		}
193*e4b17023SJohn Marino 
194*e4b17023SJohn Marino 	      /* Now walk over the blocks to determine which ones were
195*e4b17023SJohn Marino 		 marked as being reached by a useful case label.  */
196*e4b17023SJohn Marino 	      for (i = 0; i < n_basic_blocks; i++)
197*e4b17023SJohn Marino 		{
198*e4b17023SJohn Marino 		  tree node = info[i];
199*e4b17023SJohn Marino 
200*e4b17023SJohn Marino 		  if (node != NULL
201*e4b17023SJohn Marino 		      && node != error_mark_node)
202*e4b17023SJohn Marino 		    {
203*e4b17023SJohn Marino 		      tree x = fold_convert (TREE_TYPE (cond), CASE_LOW (node));
204*e4b17023SJohn Marino 		      struct edge_equivalency *equivalency;
205*e4b17023SJohn Marino 
206*e4b17023SJohn Marino 		      /* Record an equivalency on the edge from BB to basic
207*e4b17023SJohn Marino 			 block I.  */
208*e4b17023SJohn Marino 		      equivalency = XNEW (struct edge_equivalency);
209*e4b17023SJohn Marino 		      equivalency->rhs = x;
210*e4b17023SJohn Marino 		      equivalency->lhs = cond;
211*e4b17023SJohn Marino 		      find_edge (bb, BASIC_BLOCK (i))->aux = equivalency;
212*e4b17023SJohn Marino 		    }
213*e4b17023SJohn Marino 		}
214*e4b17023SJohn Marino 	      free (info);
215*e4b17023SJohn Marino 	    }
216*e4b17023SJohn Marino 	}
217*e4b17023SJohn Marino 
218*e4b17023SJohn Marino     }
219*e4b17023SJohn Marino }
220*e4b17023SJohn Marino 
221*e4b17023SJohn Marino 
222*e4b17023SJohn Marino /* Translating out of SSA sometimes requires inserting copies and
223*e4b17023SJohn Marino    constant initializations on edges to eliminate PHI nodes.
224*e4b17023SJohn Marino 
225*e4b17023SJohn Marino    In some cases those copies and constant initializations are
226*e4b17023SJohn Marino    redundant because the target already has the value on the
227*e4b17023SJohn Marino    RHS of the assignment.
228*e4b17023SJohn Marino 
229*e4b17023SJohn Marino    We previously tried to catch these cases after translating
230*e4b17023SJohn Marino    out of SSA form.  However, that code often missed cases.  Worse
231*e4b17023SJohn Marino    yet, the cases it missed were also often missed by the RTL
232*e4b17023SJohn Marino    optimizers.  Thus the resulting code had redundant instructions.
233*e4b17023SJohn Marino 
234*e4b17023SJohn Marino    This pass attempts to detect these situations before translating
235*e4b17023SJohn Marino    out of SSA form.
236*e4b17023SJohn Marino 
237*e4b17023SJohn Marino    The key concept that this pass is built upon is that these
238*e4b17023SJohn Marino    redundant copies and constant initializations often occur
239*e4b17023SJohn Marino    due to constant/copy propagating equivalences resulting from
240*e4b17023SJohn Marino    COND_EXPRs and SWITCH_EXPRs.
241*e4b17023SJohn Marino 
242*e4b17023SJohn Marino    We want to do those propagations as they can sometimes allow
243*e4b17023SJohn Marino    the SSA optimizers to do a better job.  However, in the cases
244*e4b17023SJohn Marino    where such propagations do not result in further optimization,
245*e4b17023SJohn Marino    we would like to "undo" the propagation to avoid the redundant
246*e4b17023SJohn Marino    copies and constant initializations.
247*e4b17023SJohn Marino 
248*e4b17023SJohn Marino    This pass works by first associating equivalences with edges in
249*e4b17023SJohn Marino    the CFG.  For example, the edge leading from a SWITCH_EXPR to
250*e4b17023SJohn Marino    its associated CASE_LABEL will have an equivalency between
251*e4b17023SJohn Marino    SWITCH_COND and the value in the case label.
252*e4b17023SJohn Marino 
253*e4b17023SJohn Marino    Once we have found the edge equivalences, we proceed to walk
254*e4b17023SJohn Marino    the CFG in dominator order.  As we traverse edges we record
255*e4b17023SJohn Marino    equivalences associated with those edges we traverse.
256*e4b17023SJohn Marino 
257*e4b17023SJohn Marino    When we encounter a PHI node, we walk its arguments to see if we
258*e4b17023SJohn Marino    have an equivalence for the PHI argument.  If so, then we replace
259*e4b17023SJohn Marino    the argument.
260*e4b17023SJohn Marino 
261*e4b17023SJohn Marino    Equivalences are looked up based on their value (think of it as
262*e4b17023SJohn Marino    the RHS of an assignment).   A value may be an SSA_NAME or an
263*e4b17023SJohn Marino    invariant.  We may have several SSA_NAMEs with the same value,
264*e4b17023SJohn Marino    so with each value we have a list of SSA_NAMEs that have the
265*e4b17023SJohn Marino    same value.  */
266*e4b17023SJohn Marino 
267*e4b17023SJohn Marino /* As we enter each block we record the value for any edge equivalency
268*e4b17023SJohn Marino    leading to this block.  If no such edge equivalency exists, then we
269*e4b17023SJohn Marino    record NULL.  These equivalences are live until we leave the dominator
270*e4b17023SJohn Marino    subtree rooted at the block where we record the equivalency.  */
VEC(tree,heap)271*e4b17023SJohn Marino static VEC(tree,heap) *equiv_stack;
272*e4b17023SJohn Marino 
273*e4b17023SJohn Marino /* Global hash table implementing a mapping from invariant values
274*e4b17023SJohn Marino    to a list of SSA_NAMEs which have the same value.  We might be
275*e4b17023SJohn Marino    able to reuse tree-vn for this code.  */
276*e4b17023SJohn Marino static htab_t equiv;
277*e4b17023SJohn Marino 
278*e4b17023SJohn Marino /* Main structure for recording equivalences into our hash table.  */
279*e4b17023SJohn Marino struct equiv_hash_elt
280*e4b17023SJohn Marino {
281*e4b17023SJohn Marino   /* The value/key of this entry.  */
282*e4b17023SJohn Marino   tree value;
283*e4b17023SJohn Marino 
284*e4b17023SJohn Marino   /* List of SSA_NAMEs which have the same value/key.  */
285*e4b17023SJohn Marino   VEC(tree,heap) *equivalences;
286*e4b17023SJohn Marino };
287*e4b17023SJohn Marino 
288*e4b17023SJohn Marino static void uncprop_enter_block (struct dom_walk_data *, basic_block);
289*e4b17023SJohn Marino static void uncprop_leave_block (struct dom_walk_data *, basic_block);
290*e4b17023SJohn Marino static void uncprop_into_successor_phis (basic_block);
291*e4b17023SJohn Marino 
292*e4b17023SJohn Marino /* Hashing and equality routines for the hash table.  */
293*e4b17023SJohn Marino 
294*e4b17023SJohn Marino static hashval_t
equiv_hash(const void * p)295*e4b17023SJohn Marino equiv_hash (const void *p)
296*e4b17023SJohn Marino {
297*e4b17023SJohn Marino   tree const value = ((const struct equiv_hash_elt *)p)->value;
298*e4b17023SJohn Marino   return iterative_hash_expr (value, 0);
299*e4b17023SJohn Marino }
300*e4b17023SJohn Marino 
301*e4b17023SJohn Marino static int
equiv_eq(const void * p1,const void * p2)302*e4b17023SJohn Marino equiv_eq (const void *p1, const void *p2)
303*e4b17023SJohn Marino {
304*e4b17023SJohn Marino   tree value1 = ((const struct equiv_hash_elt *)p1)->value;
305*e4b17023SJohn Marino   tree value2 = ((const struct equiv_hash_elt *)p2)->value;
306*e4b17023SJohn Marino 
307*e4b17023SJohn Marino   return operand_equal_p (value1, value2, 0);
308*e4b17023SJohn Marino }
309*e4b17023SJohn Marino 
310*e4b17023SJohn Marino /* Free an instance of equiv_hash_elt.  */
311*e4b17023SJohn Marino 
312*e4b17023SJohn Marino static void
equiv_free(void * p)313*e4b17023SJohn Marino equiv_free (void *p)
314*e4b17023SJohn Marino {
315*e4b17023SJohn Marino   struct equiv_hash_elt *elt = (struct equiv_hash_elt *) p;
316*e4b17023SJohn Marino   VEC_free (tree, heap, elt->equivalences);
317*e4b17023SJohn Marino   free (elt);
318*e4b17023SJohn Marino }
319*e4b17023SJohn Marino 
320*e4b17023SJohn Marino /* Remove the most recently recorded equivalency for VALUE.  */
321*e4b17023SJohn Marino 
322*e4b17023SJohn Marino static void
remove_equivalence(tree value)323*e4b17023SJohn Marino remove_equivalence (tree value)
324*e4b17023SJohn Marino {
325*e4b17023SJohn Marino   struct equiv_hash_elt equiv_hash_elt, *equiv_hash_elt_p;
326*e4b17023SJohn Marino   void **slot;
327*e4b17023SJohn Marino 
328*e4b17023SJohn Marino   equiv_hash_elt.value = value;
329*e4b17023SJohn Marino   equiv_hash_elt.equivalences = NULL;
330*e4b17023SJohn Marino 
331*e4b17023SJohn Marino   slot = htab_find_slot (equiv, &equiv_hash_elt, NO_INSERT);
332*e4b17023SJohn Marino 
333*e4b17023SJohn Marino   equiv_hash_elt_p = (struct equiv_hash_elt *) *slot;
334*e4b17023SJohn Marino   VEC_pop (tree, equiv_hash_elt_p->equivalences);
335*e4b17023SJohn Marino }
336*e4b17023SJohn Marino 
337*e4b17023SJohn Marino /* Record EQUIVALENCE = VALUE into our hash table.  */
338*e4b17023SJohn Marino 
339*e4b17023SJohn Marino static void
record_equiv(tree value,tree equivalence)340*e4b17023SJohn Marino record_equiv (tree value, tree equivalence)
341*e4b17023SJohn Marino {
342*e4b17023SJohn Marino   struct equiv_hash_elt *equiv_hash_elt;
343*e4b17023SJohn Marino   void **slot;
344*e4b17023SJohn Marino 
345*e4b17023SJohn Marino   equiv_hash_elt = XNEW (struct equiv_hash_elt);
346*e4b17023SJohn Marino   equiv_hash_elt->value = value;
347*e4b17023SJohn Marino   equiv_hash_elt->equivalences = NULL;
348*e4b17023SJohn Marino 
349*e4b17023SJohn Marino   slot = htab_find_slot (equiv, equiv_hash_elt, INSERT);
350*e4b17023SJohn Marino 
351*e4b17023SJohn Marino   if (*slot == NULL)
352*e4b17023SJohn Marino     *slot = (void *) equiv_hash_elt;
353*e4b17023SJohn Marino   else
354*e4b17023SJohn Marino      free (equiv_hash_elt);
355*e4b17023SJohn Marino 
356*e4b17023SJohn Marino   equiv_hash_elt = (struct equiv_hash_elt *) *slot;
357*e4b17023SJohn Marino 
358*e4b17023SJohn Marino   VEC_safe_push (tree, heap, equiv_hash_elt->equivalences, equivalence);
359*e4b17023SJohn Marino }
360*e4b17023SJohn Marino 
361*e4b17023SJohn Marino /* Main driver for un-cprop.  */
362*e4b17023SJohn Marino 
363*e4b17023SJohn Marino static unsigned int
tree_ssa_uncprop(void)364*e4b17023SJohn Marino tree_ssa_uncprop (void)
365*e4b17023SJohn Marino {
366*e4b17023SJohn Marino   struct dom_walk_data walk_data;
367*e4b17023SJohn Marino   basic_block bb;
368*e4b17023SJohn Marino 
369*e4b17023SJohn Marino   associate_equivalences_with_edges ();
370*e4b17023SJohn Marino 
371*e4b17023SJohn Marino   /* Create our global data structures.  */
372*e4b17023SJohn Marino   equiv = htab_create (1024, equiv_hash, equiv_eq, equiv_free);
373*e4b17023SJohn Marino   equiv_stack = VEC_alloc (tree, heap, 2);
374*e4b17023SJohn Marino 
375*e4b17023SJohn Marino   /* We're going to do a dominator walk, so ensure that we have
376*e4b17023SJohn Marino      dominance information.  */
377*e4b17023SJohn Marino   calculate_dominance_info (CDI_DOMINATORS);
378*e4b17023SJohn Marino 
379*e4b17023SJohn Marino   /* Setup callbacks for the generic dominator tree walker.  */
380*e4b17023SJohn Marino   walk_data.dom_direction = CDI_DOMINATORS;
381*e4b17023SJohn Marino   walk_data.initialize_block_local_data = NULL;
382*e4b17023SJohn Marino   walk_data.before_dom_children = uncprop_enter_block;
383*e4b17023SJohn Marino   walk_data.after_dom_children = uncprop_leave_block;
384*e4b17023SJohn Marino   walk_data.global_data = NULL;
385*e4b17023SJohn Marino   walk_data.block_local_data_size = 0;
386*e4b17023SJohn Marino 
387*e4b17023SJohn Marino   /* Now initialize the dominator walker.  */
388*e4b17023SJohn Marino   init_walk_dominator_tree (&walk_data);
389*e4b17023SJohn Marino 
390*e4b17023SJohn Marino   /* Recursively walk the dominator tree undoing unprofitable
391*e4b17023SJohn Marino      constant/copy propagations.  */
392*e4b17023SJohn Marino   walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
393*e4b17023SJohn Marino 
394*e4b17023SJohn Marino   /* Finalize and clean up.  */
395*e4b17023SJohn Marino   fini_walk_dominator_tree (&walk_data);
396*e4b17023SJohn Marino 
397*e4b17023SJohn Marino   /* EQUIV_STACK should already be empty at this point, so we just
398*e4b17023SJohn Marino      need to empty elements out of the hash table, free EQUIV_STACK,
399*e4b17023SJohn Marino      and cleanup the AUX field on the edges.  */
400*e4b17023SJohn Marino   htab_delete (equiv);
401*e4b17023SJohn Marino   VEC_free (tree, heap, equiv_stack);
402*e4b17023SJohn Marino   FOR_EACH_BB (bb)
403*e4b17023SJohn Marino     {
404*e4b17023SJohn Marino       edge e;
405*e4b17023SJohn Marino       edge_iterator ei;
406*e4b17023SJohn Marino 
407*e4b17023SJohn Marino       FOR_EACH_EDGE (e, ei, bb->succs)
408*e4b17023SJohn Marino 	{
409*e4b17023SJohn Marino 	  if (e->aux)
410*e4b17023SJohn Marino 	    {
411*e4b17023SJohn Marino 	      free (e->aux);
412*e4b17023SJohn Marino 	      e->aux = NULL;
413*e4b17023SJohn Marino 	    }
414*e4b17023SJohn Marino 	}
415*e4b17023SJohn Marino     }
416*e4b17023SJohn Marino   return 0;
417*e4b17023SJohn Marino }
418*e4b17023SJohn Marino 
419*e4b17023SJohn Marino 
420*e4b17023SJohn Marino /* We have finished processing the dominator children of BB, perform
421*e4b17023SJohn Marino    any finalization actions in preparation for leaving this node in
422*e4b17023SJohn Marino    the dominator tree.  */
423*e4b17023SJohn Marino 
424*e4b17023SJohn Marino static void
uncprop_leave_block(struct dom_walk_data * walk_data ATTRIBUTE_UNUSED,basic_block bb ATTRIBUTE_UNUSED)425*e4b17023SJohn Marino uncprop_leave_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
426*e4b17023SJohn Marino 		     basic_block bb ATTRIBUTE_UNUSED)
427*e4b17023SJohn Marino {
428*e4b17023SJohn Marino   /* Pop the topmost value off the equiv stack.  */
429*e4b17023SJohn Marino   tree value = VEC_pop (tree, equiv_stack);
430*e4b17023SJohn Marino 
431*e4b17023SJohn Marino   /* If that value was non-null, then pop the topmost equivalency off
432*e4b17023SJohn Marino      its equivalency stack.  */
433*e4b17023SJohn Marino   if (value != NULL)
434*e4b17023SJohn Marino     remove_equivalence (value);
435*e4b17023SJohn Marino }
436*e4b17023SJohn Marino 
437*e4b17023SJohn Marino /* Unpropagate values from PHI nodes in successor blocks of BB.  */
438*e4b17023SJohn Marino 
439*e4b17023SJohn Marino static void
uncprop_into_successor_phis(basic_block bb)440*e4b17023SJohn Marino uncprop_into_successor_phis (basic_block bb)
441*e4b17023SJohn Marino {
442*e4b17023SJohn Marino   edge e;
443*e4b17023SJohn Marino   edge_iterator ei;
444*e4b17023SJohn Marino 
445*e4b17023SJohn Marino   /* For each successor edge, first temporarily record any equivalence
446*e4b17023SJohn Marino      on that edge.  Then unpropagate values in any PHI nodes at the
447*e4b17023SJohn Marino      destination of the edge.  Then remove the temporary equivalence.  */
448*e4b17023SJohn Marino   FOR_EACH_EDGE (e, ei, bb->succs)
449*e4b17023SJohn Marino     {
450*e4b17023SJohn Marino       gimple_seq phis = phi_nodes (e->dest);
451*e4b17023SJohn Marino       gimple_stmt_iterator gsi;
452*e4b17023SJohn Marino 
453*e4b17023SJohn Marino       /* If there are no PHI nodes in this destination, then there is
454*e4b17023SJohn Marino 	 no sense in recording any equivalences.  */
455*e4b17023SJohn Marino       if (gimple_seq_empty_p (phis))
456*e4b17023SJohn Marino 	continue;
457*e4b17023SJohn Marino 
458*e4b17023SJohn Marino       /* Record any equivalency associated with E.  */
459*e4b17023SJohn Marino       if (e->aux)
460*e4b17023SJohn Marino 	{
461*e4b17023SJohn Marino 	  struct edge_equivalency *equiv = (struct edge_equivalency *) e->aux;
462*e4b17023SJohn Marino 	  record_equiv (equiv->rhs, equiv->lhs);
463*e4b17023SJohn Marino 	}
464*e4b17023SJohn Marino 
465*e4b17023SJohn Marino       /* Walk over the PHI nodes, unpropagating values.  */
466*e4b17023SJohn Marino       for (gsi = gsi_start (phis) ; !gsi_end_p (gsi); gsi_next (&gsi))
467*e4b17023SJohn Marino 	{
468*e4b17023SJohn Marino 	  gimple phi = gsi_stmt (gsi);
469*e4b17023SJohn Marino 	  tree arg = PHI_ARG_DEF (phi, e->dest_idx);
470*e4b17023SJohn Marino 	  struct equiv_hash_elt equiv_hash_elt;
471*e4b17023SJohn Marino 	  void **slot;
472*e4b17023SJohn Marino 
473*e4b17023SJohn Marino 	  /* If the argument is not an invariant, or refers to the same
474*e4b17023SJohn Marino 	     underlying variable as the PHI result, then there's no
475*e4b17023SJohn Marino 	     point in un-propagating the argument.  */
476*e4b17023SJohn Marino 	  if (!is_gimple_min_invariant (arg)
477*e4b17023SJohn Marino 	      && SSA_NAME_VAR (arg) != SSA_NAME_VAR (PHI_RESULT (phi)))
478*e4b17023SJohn Marino 	    continue;
479*e4b17023SJohn Marino 
480*e4b17023SJohn Marino 	  /* Lookup this argument's value in the hash table.  */
481*e4b17023SJohn Marino 	  equiv_hash_elt.value = arg;
482*e4b17023SJohn Marino 	  equiv_hash_elt.equivalences = NULL;
483*e4b17023SJohn Marino 	  slot = htab_find_slot (equiv, &equiv_hash_elt, NO_INSERT);
484*e4b17023SJohn Marino 
485*e4b17023SJohn Marino 	  if (slot)
486*e4b17023SJohn Marino 	    {
487*e4b17023SJohn Marino 	      struct equiv_hash_elt *elt = (struct equiv_hash_elt *) *slot;
488*e4b17023SJohn Marino 	      int j;
489*e4b17023SJohn Marino 
490*e4b17023SJohn Marino 	      /* Walk every equivalence with the same value.  If we find
491*e4b17023SJohn Marino 		 one with the same underlying variable as the PHI result,
492*e4b17023SJohn Marino 		 then replace the value in the argument with its equivalent
493*e4b17023SJohn Marino 		 SSA_NAME.  Use the most recent equivalence as hopefully
494*e4b17023SJohn Marino 		 that results in shortest lifetimes.  */
495*e4b17023SJohn Marino 	      for (j = VEC_length (tree, elt->equivalences) - 1; j >= 0; j--)
496*e4b17023SJohn Marino 		{
497*e4b17023SJohn Marino 		  tree equiv = VEC_index (tree, elt->equivalences, j);
498*e4b17023SJohn Marino 
499*e4b17023SJohn Marino 		  if (SSA_NAME_VAR (equiv) == SSA_NAME_VAR (PHI_RESULT (phi)))
500*e4b17023SJohn Marino 		    {
501*e4b17023SJohn Marino 		      SET_PHI_ARG_DEF (phi, e->dest_idx, equiv);
502*e4b17023SJohn Marino 		      break;
503*e4b17023SJohn Marino 		    }
504*e4b17023SJohn Marino 		}
505*e4b17023SJohn Marino 	    }
506*e4b17023SJohn Marino 	}
507*e4b17023SJohn Marino 
508*e4b17023SJohn Marino       /* If we had an equivalence associated with this edge, remove it.  */
509*e4b17023SJohn Marino       if (e->aux)
510*e4b17023SJohn Marino 	{
511*e4b17023SJohn Marino 	  struct edge_equivalency *equiv = (struct edge_equivalency *) e->aux;
512*e4b17023SJohn Marino 	  remove_equivalence (equiv->rhs);
513*e4b17023SJohn Marino 	}
514*e4b17023SJohn Marino     }
515*e4b17023SJohn Marino }
516*e4b17023SJohn Marino 
517*e4b17023SJohn Marino /* Ignoring loop backedges, if BB has precisely one incoming edge then
518*e4b17023SJohn Marino    return that edge.  Otherwise return NULL.  */
519*e4b17023SJohn Marino static edge
single_incoming_edge_ignoring_loop_edges(basic_block bb)520*e4b17023SJohn Marino single_incoming_edge_ignoring_loop_edges (basic_block bb)
521*e4b17023SJohn Marino {
522*e4b17023SJohn Marino   edge retval = NULL;
523*e4b17023SJohn Marino   edge e;
524*e4b17023SJohn Marino   edge_iterator ei;
525*e4b17023SJohn Marino 
526*e4b17023SJohn Marino   FOR_EACH_EDGE (e, ei, bb->preds)
527*e4b17023SJohn Marino     {
528*e4b17023SJohn Marino       /* A loop back edge can be identified by the destination of
529*e4b17023SJohn Marino 	 the edge dominating the source of the edge.  */
530*e4b17023SJohn Marino       if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
531*e4b17023SJohn Marino 	continue;
532*e4b17023SJohn Marino 
533*e4b17023SJohn Marino       /* If we have already seen a non-loop edge, then we must have
534*e4b17023SJohn Marino 	 multiple incoming non-loop edges and thus we return NULL.  */
535*e4b17023SJohn Marino       if (retval)
536*e4b17023SJohn Marino 	return NULL;
537*e4b17023SJohn Marino 
538*e4b17023SJohn Marino       /* This is the first non-loop incoming edge we have found.  Record
539*e4b17023SJohn Marino 	 it.  */
540*e4b17023SJohn Marino       retval = e;
541*e4b17023SJohn Marino     }
542*e4b17023SJohn Marino 
543*e4b17023SJohn Marino   return retval;
544*e4b17023SJohn Marino }
545*e4b17023SJohn Marino 
546*e4b17023SJohn Marino static void
uncprop_enter_block(struct dom_walk_data * walk_data ATTRIBUTE_UNUSED,basic_block bb)547*e4b17023SJohn Marino uncprop_enter_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
548*e4b17023SJohn Marino 		     basic_block bb)
549*e4b17023SJohn Marino {
550*e4b17023SJohn Marino   basic_block parent;
551*e4b17023SJohn Marino   edge e;
552*e4b17023SJohn Marino   bool recorded = false;
553*e4b17023SJohn Marino 
554*e4b17023SJohn Marino   /* If this block is dominated by a single incoming edge and that edge
555*e4b17023SJohn Marino      has an equivalency, then record the equivalency and push the
556*e4b17023SJohn Marino      VALUE onto EQUIV_STACK.  Else push a NULL entry on EQUIV_STACK.  */
557*e4b17023SJohn Marino   parent = get_immediate_dominator (CDI_DOMINATORS, bb);
558*e4b17023SJohn Marino   if (parent)
559*e4b17023SJohn Marino     {
560*e4b17023SJohn Marino       e = single_incoming_edge_ignoring_loop_edges (bb);
561*e4b17023SJohn Marino 
562*e4b17023SJohn Marino       if (e && e->src == parent && e->aux)
563*e4b17023SJohn Marino 	{
564*e4b17023SJohn Marino 	  struct edge_equivalency *equiv = (struct edge_equivalency *) e->aux;
565*e4b17023SJohn Marino 
566*e4b17023SJohn Marino 	  record_equiv (equiv->rhs, equiv->lhs);
567*e4b17023SJohn Marino 	  VEC_safe_push (tree, heap, equiv_stack, equiv->rhs);
568*e4b17023SJohn Marino 	  recorded = true;
569*e4b17023SJohn Marino 	}
570*e4b17023SJohn Marino     }
571*e4b17023SJohn Marino 
572*e4b17023SJohn Marino   if (!recorded)
573*e4b17023SJohn Marino     VEC_safe_push (tree, heap, equiv_stack, NULL_TREE);
574*e4b17023SJohn Marino 
575*e4b17023SJohn Marino   uncprop_into_successor_phis (bb);
576*e4b17023SJohn Marino }
577*e4b17023SJohn Marino 
578*e4b17023SJohn Marino static bool
gate_uncprop(void)579*e4b17023SJohn Marino gate_uncprop (void)
580*e4b17023SJohn Marino {
581*e4b17023SJohn Marino   return flag_tree_dom != 0;
582*e4b17023SJohn Marino }
583*e4b17023SJohn Marino 
584*e4b17023SJohn Marino struct gimple_opt_pass pass_uncprop =
585*e4b17023SJohn Marino {
586*e4b17023SJohn Marino  {
587*e4b17023SJohn Marino   GIMPLE_PASS,
588*e4b17023SJohn Marino   "uncprop",				/* name */
589*e4b17023SJohn Marino   gate_uncprop,				/* gate */
590*e4b17023SJohn Marino   tree_ssa_uncprop,			/* execute */
591*e4b17023SJohn Marino   NULL,					/* sub */
592*e4b17023SJohn Marino   NULL,					/* next */
593*e4b17023SJohn Marino   0,					/* static_pass_number */
594*e4b17023SJohn Marino   TV_TREE_SSA_UNCPROP,			/* tv_id */
595*e4b17023SJohn Marino   PROP_cfg | PROP_ssa,			/* properties_required */
596*e4b17023SJohn Marino   0,					/* properties_provided */
597*e4b17023SJohn Marino   0,					/* properties_destroyed */
598*e4b17023SJohn Marino   0,					/* todo_flags_start */
599*e4b17023SJohn Marino   TODO_verify_ssa	                /* todo_flags_finish */
600*e4b17023SJohn Marino  }
601*e4b17023SJohn Marino };
602