2005-04-29 Jim Tison <jtison@us.ibm.com>
[official-gcc.git] / gcc / tree-ssa-uncprop.c
blob9f06e38f3c7f9d0a9d3543ff551d24e16d792fb0
1 /* Routines for discovering and unpropagating edge equivalences.
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "ggc.h"
30 #include "basic-block.h"
31 #include "output.h"
32 #include "errors.h"
33 #include "expr.h"
34 #include "function.h"
35 #include "diagnostic.h"
36 #include "timevar.h"
37 #include "tree-dump.h"
38 #include "tree-flow.h"
39 #include "domwalk.h"
40 #include "real.h"
41 #include "tree-pass.h"
42 #include "tree-ssa-propagate.h"
43 #include "langhooks.h"
45 /* The basic structure describing an equivalency created by traversing
46 an edge. Traversing the edge effectively means that we can assume
47 that we've seen an assignment LHS = RHS. */
48 struct edge_equivalency
50 tree rhs;
51 tree lhs;
54 /* This routine finds and records edge equivalences for every edge
55 in the CFG.
57 When complete, each edge that creates an equivalency will have an
58 EDGE_EQUIVALENCY structure hanging off the edge's AUX field.
59 The caller is responsible for freeing the AUX fields. */
61 static void
62 associate_equivalences_with_edges (void)
64 basic_block bb;
66 /* Walk over each block. If the block ends with a control statement,
67 then it might create a useful equivalence. */
68 FOR_EACH_BB (bb)
70 block_stmt_iterator bsi = bsi_last (bb);
71 tree stmt;
73 /* If the block does not end with a COND_EXPR or SWITCH_EXPR
74 then there is nothing to do. */
75 if (bsi_end_p (bsi))
76 continue;
78 stmt = bsi_stmt (bsi);
80 if (!stmt)
81 continue;
83 /* A COND_EXPR may create an equivalency in a variety of different
84 ways. */
85 if (TREE_CODE (stmt) == COND_EXPR)
87 tree cond = COND_EXPR_COND (stmt);
88 edge true_edge;
89 edge false_edge;
90 struct edge_equivalency *equivalency;
92 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
94 /* If the conditional is a single variable 'X', record 'X = 1'
95 for the true edge and 'X = 0' on the false edge. */
96 if (TREE_CODE (cond) == SSA_NAME)
98 equivalency = xmalloc (sizeof (struct edge_equivalency));
99 equivalency->rhs = constant_boolean_node (1, TREE_TYPE (cond));
100 equivalency->lhs = cond;
101 true_edge->aux = equivalency;
103 equivalency = xmalloc (sizeof (struct edge_equivalency));
104 equivalency->rhs = constant_boolean_node (0, TREE_TYPE (cond));
105 equivalency->lhs = cond;
106 false_edge->aux = equivalency;
108 /* Equality tests may create one or two equivalences. */
109 else if (TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR)
111 tree op0 = TREE_OPERAND (cond, 0);
112 tree op1 = TREE_OPERAND (cond, 1);
114 /* Special case comparing booleans against a constant as we
115 know the value of OP0 on both arms of the branch. i.e., we
116 can record an equivalence for OP0 rather than COND. */
117 if (TREE_CODE (op0) == SSA_NAME
118 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
119 && is_gimple_min_invariant (op1))
121 if (TREE_CODE (cond) == EQ_EXPR)
123 equivalency = xmalloc (sizeof (struct edge_equivalency));
124 equivalency->lhs = op0;
125 equivalency->rhs = (integer_zerop (op1)
126 ? boolean_false_node
127 : boolean_true_node);
128 true_edge->aux = equivalency;
130 equivalency = xmalloc (sizeof (struct edge_equivalency));
131 equivalency->lhs = op0;
132 equivalency->rhs = (integer_zerop (op1)
133 ? boolean_true_node
134 : boolean_false_node);
135 false_edge->aux = equivalency;
137 else
139 equivalency = xmalloc (sizeof (struct edge_equivalency));
140 equivalency->lhs = op0;
141 equivalency->rhs = (integer_zerop (op1)
142 ? boolean_true_node
143 : boolean_false_node);
144 true_edge->aux = equivalency;
146 equivalency = xmalloc (sizeof (struct edge_equivalency));
147 equivalency->lhs = op0;
148 equivalency->rhs = (integer_zerop (op1)
149 ? boolean_false_node
150 : boolean_true_node);
151 false_edge->aux = equivalency;
155 if (TREE_CODE (op0) == SSA_NAME
156 && (is_gimple_min_invariant (op1)
157 || TREE_CODE (op1) == SSA_NAME))
159 /* For IEEE, -0.0 == 0.0, so we don't necessarily know
160 the sign of a variable compared against zero. If
161 we're honoring signed zeros, then we cannot record
162 this value unless we know that the value is nonzero. */
163 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op0)))
164 && (TREE_CODE (op1) != REAL_CST
165 || REAL_VALUES_EQUAL (dconst0, TREE_REAL_CST (op1))))
166 continue;
168 equivalency = xmalloc (sizeof (struct edge_equivalency));
169 equivalency->lhs = op0;
170 equivalency->rhs = op1;
171 if (TREE_CODE (cond) == EQ_EXPR)
172 true_edge->aux = equivalency;
173 else
174 false_edge->aux = equivalency;
179 /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
182 /* For a SWITCH_EXPR, a case label which represents a single
183 value and which is the only case label which reaches the
184 target block creates an equivalence. */
185 if (TREE_CODE (stmt) == SWITCH_EXPR)
187 tree cond = SWITCH_COND (stmt);
189 if (TREE_CODE (cond) == SSA_NAME)
191 tree labels = SWITCH_LABELS (stmt);
192 int i, n_labels = TREE_VEC_LENGTH (labels);
193 tree *info = xcalloc (n_basic_blocks, sizeof (tree));
195 /* Walk over the case label vector. Record blocks
196 which are reached by a single case label which represents
197 a single value. */
198 for (i = 0; i < n_labels; i++)
200 tree label = TREE_VEC_ELT (labels, i);
201 basic_block bb = label_to_block (CASE_LABEL (label));
204 if (CASE_HIGH (label)
205 || !CASE_LOW (label)
206 || info[bb->index])
207 info[bb->index] = error_mark_node;
208 else
209 info[bb->index] = label;
212 /* Now walk over the blocks to determine which ones were
213 marked as being reached by a useful case label. */
214 for (i = 0; i < n_basic_blocks; i++)
216 tree node = info[i];
218 if (node != NULL
219 && node != error_mark_node)
221 tree x = fold_convert (TREE_TYPE (cond), CASE_LOW (node));
222 struct edge_equivalency *equivalency;
224 /* Record an equivalency on the edge from BB to basic
225 block I. */
226 equivalency = xmalloc (sizeof (struct edge_equivalency));
227 equivalency->rhs = x;
228 equivalency->lhs = cond;
229 find_edge (bb, BASIC_BLOCK (i))->aux = equivalency;
232 free (info);
240 /* Translating out of SSA sometimes requires inserting copies and
241 constant initializations on edges to eliminate PHI nodes.
243 In some cases those copies and constant initializations are
244 redundant because the target already has the value on the
245 RHS of the assignment.
247 We previously tried to catch these cases after translating
248 out of SSA form. However, that code often missed cases. Worse
249 yet, the cases it missed were also often missed by the RTL
250 optimizers. Thus the resulting code had redundant instructions.
252 This pass attempts to detect these situations before translating
253 out of SSA form.
255 The key concept that this pass is built upon is that these
256 redundant copies and constant initializations often occur
257 due to constant/copy propagating equivalences resulting from
258 COND_EXPRs and SWITCH_EXPRs.
260 We want to do those propagations as they can sometimes allow
261 the SSA optimizers to do a better job. However, in the cases
262 where such propagations do not result in further optimization,
263 we would like to "undo" the propagation to avoid the redundant
264 copies and constant initializations.
266 This pass works by first associating equivalences with edges in
267 the CFG. For example, the edge leading from a SWITCH_EXPR to
268 its associated CASE_LABEL will have an equivalency between
269 SWITCH_COND and the value in the case label.
271 Once we have found the edge equivalences, we proceed to walk
272 the CFG in dominator order. As we traverse edges we record
273 equivalences associated with those edges we traverse.
275 When we encounter a PHI node, we walk its arguments to see if we
276 have an equivalence for the PHI argument. If so, then we replace
277 the argument.
279 Equivalences are looked up based on their value (think of it as
280 the RHS of an assignment). A value may be an SSA_NAME or an
281 invariant. We may have several SSA_NAMEs with the same value,
282 so with each value we have a list of SSA_NAMEs that have the
283 same value. */
285 /* As we enter each block we record the value for any edge equivalency
286 leading to this block. If no such edge equivalency exists, then we
287 record NULL. These equivalences are live until we leave the dominator
288 subtree rooted at the block where we record the equivalency. */
289 static VEC(tree,heap) *equiv_stack;
291 /* Global hash table implementing a mapping from invariant values
292 to a list of SSA_NAMEs which have the same value. We might be
293 able to reuse tree-vn for this code. */
294 static htab_t equiv;
296 /* Main structure for recording equivalences into our hash table. */
297 struct equiv_hash_elt
299 /* The value/key of this entry. */
300 tree value;
302 /* List of SSA_NAMEs which have the same value/key. */
303 varray_type equivalences;
306 static void uncprop_initialize_block (struct dom_walk_data *, basic_block);
307 static void uncprop_finalize_block (struct dom_walk_data *, basic_block);
308 static void uncprop_into_successor_phis (struct dom_walk_data *, basic_block);
310 /* Hashing and equality routines for the hash table. */
312 static hashval_t
313 equiv_hash (const void *p)
315 tree value = ((struct equiv_hash_elt *)p)->value;
316 return iterative_hash_expr (value, 0);
319 static int
320 equiv_eq (const void *p1, const void *p2)
322 tree value1 = ((struct equiv_hash_elt *)p1)->value;
323 tree value2 = ((struct equiv_hash_elt *)p2)->value;
325 return operand_equal_p (value1, value2, 0);
328 /* Remove the most recently recorded equivalency for VALUE. */
330 static void
331 remove_equivalence (tree value)
333 struct equiv_hash_elt equiv_hash_elt, *equiv_hash_elt_p;
334 void **slot;
336 equiv_hash_elt.value = value;
337 equiv_hash_elt.equivalences = NULL;
339 slot = htab_find_slot (equiv, &equiv_hash_elt, NO_INSERT);
341 equiv_hash_elt_p = (struct equiv_hash_elt *) *slot;
342 VARRAY_POP (equiv_hash_elt_p->equivalences);
345 /* Record EQUIVALENCE = VALUE into our hash table. */
347 static void
348 record_equiv (tree value, tree equivalence)
350 struct equiv_hash_elt *equiv_hash_elt;
351 void **slot;
353 equiv_hash_elt = xmalloc (sizeof (struct equiv_hash_elt));
354 equiv_hash_elt->value = value;
355 equiv_hash_elt->equivalences = NULL;
357 slot = htab_find_slot (equiv, equiv_hash_elt, INSERT);
359 if (*slot == NULL)
360 *slot = (void *) equiv_hash_elt;
361 else
362 free (equiv_hash_elt);
364 equiv_hash_elt = (struct equiv_hash_elt *) *slot;
366 if (!equiv_hash_elt->equivalences)
367 VARRAY_TREE_INIT (equiv_hash_elt->equivalences, 10, "value equivs");
368 VARRAY_PUSH_TREE (equiv_hash_elt->equivalences, equivalence);
371 /* Main driver for un-cprop. */
373 static void
374 tree_ssa_uncprop (void)
376 struct dom_walk_data walk_data;
377 basic_block bb;
379 associate_equivalences_with_edges ();
381 /* Create our global data structures. */
382 equiv = htab_create (1024, equiv_hash, equiv_eq, free);
383 equiv_stack = VEC_alloc (tree, heap, 2);
385 /* We're going to do a dominator walk, so ensure that we have
386 dominance information. */
387 calculate_dominance_info (CDI_DOMINATORS);
389 /* Setup callbacks for the generic dominator tree walker. */
390 walk_data.walk_stmts_backward = false;
391 walk_data.dom_direction = CDI_DOMINATORS;
392 walk_data.initialize_block_local_data = NULL;
393 walk_data.before_dom_children_before_stmts = uncprop_initialize_block;
394 walk_data.before_dom_children_walk_stmts = NULL;
395 walk_data.before_dom_children_after_stmts = uncprop_into_successor_phis;
396 walk_data.after_dom_children_before_stmts = NULL;
397 walk_data.after_dom_children_walk_stmts = NULL;
398 walk_data.after_dom_children_after_stmts = uncprop_finalize_block;
399 walk_data.global_data = NULL;
400 walk_data.block_local_data_size = 0;
401 walk_data.interesting_blocks = NULL;
403 /* Now initialize the dominator walker. */
404 init_walk_dominator_tree (&walk_data);
406 /* Recursively walk the dominator tree undoing unprofitable
407 constant/copy propagations. */
408 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
410 /* Finalize and clean up. */
411 fini_walk_dominator_tree (&walk_data);
413 /* EQUIV_STACK should already be empty at this point, so we just
414 need to empty elements out of the hash table, free EQUIV_STACK,
415 and cleanup the AUX field on the edges. */
416 htab_delete (equiv);
417 VEC_free (tree, heap, equiv_stack);
418 FOR_EACH_BB (bb)
420 edge e;
421 edge_iterator ei;
423 FOR_EACH_EDGE (e, ei, bb->succs)
425 if (e->aux)
427 free (e->aux);
428 e->aux = NULL;
436 /* We have finished processing the dominator children of BB, perform
437 any finalization actions in preparation for leaving this node in
438 the dominator tree. */
440 static void
441 uncprop_finalize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
442 basic_block bb ATTRIBUTE_UNUSED)
444 /* Pop the topmost value off the equiv stack. */
445 tree value = VEC_pop (tree, equiv_stack);
447 /* If that value was non-null, then pop the topmost equivalency off
448 its equivalency stack. */
449 if (value != NULL)
450 remove_equivalence (value);
453 /* Unpropagate values from PHI nodes in successor blocks of BB. */
455 static void
456 uncprop_into_successor_phis (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
457 basic_block bb)
459 edge e;
460 edge_iterator ei;
462 /* For each successor edge, first temporarily record any equivalence
463 on that edge. Then unpropagate values in any PHI nodes at the
464 destination of the edge. Then remove the temporary equivalence. */
465 FOR_EACH_EDGE (e, ei, bb->succs)
467 tree phi = phi_nodes (e->dest);
469 /* If there are no PHI nodes in this destination, then there is
470 no sense in recording any equivalences. */
471 if (!phi)
472 continue;
474 /* Record any equivalency associated with E. */
475 if (e->aux)
477 struct edge_equivalency *equiv = e->aux;
478 record_equiv (equiv->rhs, equiv->lhs);
481 /* Walk over the PHI nodes, unpropagating values. */
482 for ( ; phi; phi = PHI_CHAIN (phi))
484 /* Sigh. We'll have more efficient access to this one day. */
485 tree arg = PHI_ARG_DEF (phi, e->dest_idx);
486 struct equiv_hash_elt equiv_hash_elt;
487 void **slot;
489 /* If the argument is not an invariant, or refers to the same
490 underlying variable as the PHI result, then there's no
491 point in un-propagating the argument. */
492 if (!is_gimple_min_invariant (arg)
493 && SSA_NAME_VAR (arg) != SSA_NAME_VAR (PHI_RESULT (phi)))
494 continue;
496 /* Lookup this argument's value in the hash table. */
497 equiv_hash_elt.value = arg;
498 equiv_hash_elt.equivalences = NULL;
499 slot = htab_find_slot (equiv, &equiv_hash_elt, NO_INSERT);
501 if (slot)
503 struct equiv_hash_elt *elt = *slot;
504 int j;
506 /* Walk every equivalence with the same value. If we find
507 one with the same underlying variable as the PHI result,
508 then replace the value in the argument with its equivalent
509 SSA_NAME. Use the most recent equivalence as hopefully
510 that results in shortest lifetimes. */
511 for (j = VARRAY_ACTIVE_SIZE (elt->equivalences) - 1; j >= 0; j--)
513 tree equiv = VARRAY_TREE (elt->equivalences, j);
515 if (SSA_NAME_VAR (equiv) == SSA_NAME_VAR (PHI_RESULT (phi)))
517 SET_PHI_ARG_DEF (phi, e->dest_idx, equiv);
518 break;
524 /* If we had an equivalence associated with this edge, remove it. */
525 if (e->aux)
527 struct edge_equivalency *equiv = e->aux;
528 remove_equivalence (equiv->rhs);
533 /* Ignoring loop backedges, if BB has precisely one incoming edge then
534 return that edge. Otherwise return NULL. */
535 static edge
536 single_incoming_edge_ignoring_loop_edges (basic_block bb)
538 edge retval = NULL;
539 edge e;
540 edge_iterator ei;
542 FOR_EACH_EDGE (e, ei, bb->preds)
544 /* A loop back edge can be identified by the destination of
545 the edge dominating the source of the edge. */
546 if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
547 continue;
549 /* If we have already seen a non-loop edge, then we must have
550 multiple incoming non-loop edges and thus we return NULL. */
551 if (retval)
552 return NULL;
554 /* This is the first non-loop incoming edge we have found. Record
555 it. */
556 retval = e;
559 return retval;
562 static void
563 uncprop_initialize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
564 basic_block bb)
566 basic_block parent;
567 edge e;
568 bool recorded = false;
570 /* If this block is dominated by a single incoming edge and that edge
571 has an equivalency, then record the equivalency and push the
572 VALUE onto EQUIV_STACK. Else push a NULL entry on EQUIV_STACK. */
573 parent = get_immediate_dominator (CDI_DOMINATORS, bb);
574 if (parent)
576 e = single_incoming_edge_ignoring_loop_edges (bb);
578 if (e && e->src == parent && e->aux)
580 struct edge_equivalency *equiv = e->aux;
582 record_equiv (equiv->rhs, equiv->lhs);
583 VEC_safe_push (tree, heap, equiv_stack, equiv->rhs);
584 recorded = true;
588 if (!recorded)
589 VEC_safe_push (tree, heap, equiv_stack, NULL_TREE);
592 static bool
593 gate_uncprop (void)
595 return flag_tree_dom != 0;
598 struct tree_opt_pass pass_uncprop =
600 "uncprop", /* name */
601 gate_uncprop, /* gate */
602 tree_ssa_uncprop, /* execute */
603 NULL, /* sub */
604 NULL, /* next */
605 0, /* static_pass_number */
606 TV_TREE_SSA_UNCPROP, /* tv_id */
607 PROP_cfg | PROP_ssa, /* properties_required */
608 0, /* properties_provided */
609 0, /* properties_destroyed */
610 0, /* todo_flags_start */
611 TODO_dump_func | TODO_verify_ssa, /* todo_flags_finish */
612 0 /* letter */