2013-11-22 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / tree-ssa-uncprop.c
blob62ffe421f2978dfba2a846bb62e4cc3dd984e9c2
1 /* Routines for discovering and unpropagating edge equivalences.
2 Copyright (C) 2005-2013 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 3, 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 COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "stor-layout.h"
26 #include "flags.h"
27 #include "tm_p.h"
28 #include "basic-block.h"
29 #include "function.h"
30 #include "gimple.h"
31 #include "gimple-iterator.h"
32 #include "gimple-ssa.h"
33 #include "tree-cfg.h"
34 #include "tree-phinodes.h"
35 #include "ssa-iterators.h"
36 #include "domwalk.h"
37 #include "tree-pass.h"
38 #include "tree-ssa-propagate.h"
40 /* The basic structure describing an equivalency created by traversing
41 an edge. Traversing the edge effectively means that we can assume
42 that we've seen an assignment LHS = RHS. */
43 struct edge_equivalency
45 tree rhs;
46 tree lhs;
49 /* This routine finds and records edge equivalences for every edge
50 in the CFG.
52 When complete, each edge that creates an equivalency will have an
53 EDGE_EQUIVALENCY structure hanging off the edge's AUX field.
54 The caller is responsible for freeing the AUX fields. */
56 static void
57 associate_equivalences_with_edges (void)
59 basic_block bb;
61 /* Walk over each block. If the block ends with a control statement,
62 then it might create a useful equivalence. */
63 FOR_EACH_BB (bb)
65 gimple_stmt_iterator gsi = gsi_last_bb (bb);
66 gimple stmt;
68 /* If the block does not end with a COND_EXPR or SWITCH_EXPR
69 then there is nothing to do. */
70 if (gsi_end_p (gsi))
71 continue;
73 stmt = gsi_stmt (gsi);
75 if (!stmt)
76 continue;
78 /* A COND_EXPR may create an equivalency in a variety of different
79 ways. */
80 if (gimple_code (stmt) == GIMPLE_COND)
82 edge true_edge;
83 edge false_edge;
84 struct edge_equivalency *equivalency;
85 enum tree_code code = gimple_cond_code (stmt);
87 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
89 /* Equality tests may create one or two equivalences. */
90 if (code == EQ_EXPR || code == NE_EXPR)
92 tree op0 = gimple_cond_lhs (stmt);
93 tree op1 = gimple_cond_rhs (stmt);
95 /* Special case comparing booleans against a constant as we
96 know the value of OP0 on both arms of the branch. i.e., we
97 can record an equivalence for OP0 rather than COND. */
98 if (TREE_CODE (op0) == SSA_NAME
99 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0)
100 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
101 && is_gimple_min_invariant (op1))
103 if (code == EQ_EXPR)
105 equivalency = XNEW (struct edge_equivalency);
106 equivalency->lhs = op0;
107 equivalency->rhs = (integer_zerop (op1)
108 ? boolean_false_node
109 : boolean_true_node);
110 true_edge->aux = equivalency;
112 equivalency = XNEW (struct edge_equivalency);
113 equivalency->lhs = op0;
114 equivalency->rhs = (integer_zerop (op1)
115 ? boolean_true_node
116 : boolean_false_node);
117 false_edge->aux = equivalency;
119 else
121 equivalency = XNEW (struct edge_equivalency);
122 equivalency->lhs = op0;
123 equivalency->rhs = (integer_zerop (op1)
124 ? boolean_true_node
125 : boolean_false_node);
126 true_edge->aux = equivalency;
128 equivalency = XNEW (struct edge_equivalency);
129 equivalency->lhs = op0;
130 equivalency->rhs = (integer_zerop (op1)
131 ? boolean_false_node
132 : boolean_true_node);
133 false_edge->aux = equivalency;
137 else if (TREE_CODE (op0) == SSA_NAME
138 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0)
139 && (is_gimple_min_invariant (op1)
140 || (TREE_CODE (op1) == SSA_NAME
141 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op1))))
143 /* For IEEE, -0.0 == 0.0, so we don't necessarily know
144 the sign of a variable compared against zero. If
145 we're honoring signed zeros, then we cannot record
146 this value unless we know that the value is nonzero. */
147 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op0)))
148 && (TREE_CODE (op1) != REAL_CST
149 || REAL_VALUES_EQUAL (dconst0, TREE_REAL_CST (op1))))
150 continue;
152 equivalency = XNEW (struct edge_equivalency);
153 equivalency->lhs = op0;
154 equivalency->rhs = op1;
155 if (code == EQ_EXPR)
156 true_edge->aux = equivalency;
157 else
158 false_edge->aux = equivalency;
163 /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
166 /* For a SWITCH_EXPR, a case label which represents a single
167 value and which is the only case label which reaches the
168 target block creates an equivalence. */
169 else if (gimple_code (stmt) == GIMPLE_SWITCH)
171 tree cond = gimple_switch_index (stmt);
173 if (TREE_CODE (cond) == SSA_NAME
174 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (cond))
176 int i, n_labels = gimple_switch_num_labels (stmt);
177 tree *info = XCNEWVEC (tree, last_basic_block);
179 /* Walk over the case label vector. Record blocks
180 which are reached by a single case label which represents
181 a single value. */
182 for (i = 0; i < n_labels; i++)
184 tree label = gimple_switch_label (stmt, i);
185 basic_block bb = label_to_block (CASE_LABEL (label));
187 if (CASE_HIGH (label)
188 || !CASE_LOW (label)
189 || info[bb->index])
190 info[bb->index] = error_mark_node;
191 else
192 info[bb->index] = label;
195 /* Now walk over the blocks to determine which ones were
196 marked as being reached by a useful case label. */
197 for (i = 0; i < n_basic_blocks_for_fn (cfun); i++)
199 tree node = info[i];
201 if (node != NULL
202 && node != error_mark_node)
204 tree x = fold_convert (TREE_TYPE (cond), CASE_LOW (node));
205 struct edge_equivalency *equivalency;
207 /* Record an equivalency on the edge from BB to basic
208 block I. */
209 equivalency = XNEW (struct edge_equivalency);
210 equivalency->rhs = x;
211 equivalency->lhs = cond;
212 find_edge (bb, BASIC_BLOCK (i))->aux = equivalency;
215 free (info);
223 /* Translating out of SSA sometimes requires inserting copies and
224 constant initializations on edges to eliminate PHI nodes.
226 In some cases those copies and constant initializations are
227 redundant because the target already has the value on the
228 RHS of the assignment.
230 We previously tried to catch these cases after translating
231 out of SSA form. However, that code often missed cases. Worse
232 yet, the cases it missed were also often missed by the RTL
233 optimizers. Thus the resulting code had redundant instructions.
235 This pass attempts to detect these situations before translating
236 out of SSA form.
238 The key concept that this pass is built upon is that these
239 redundant copies and constant initializations often occur
240 due to constant/copy propagating equivalences resulting from
241 COND_EXPRs and SWITCH_EXPRs.
243 We want to do those propagations as they can sometimes allow
244 the SSA optimizers to do a better job. However, in the cases
245 where such propagations do not result in further optimization,
246 we would like to "undo" the propagation to avoid the redundant
247 copies and constant initializations.
249 This pass works by first associating equivalences with edges in
250 the CFG. For example, the edge leading from a SWITCH_EXPR to
251 its associated CASE_LABEL will have an equivalency between
252 SWITCH_COND and the value in the case label.
254 Once we have found the edge equivalences, we proceed to walk
255 the CFG in dominator order. As we traverse edges we record
256 equivalences associated with those edges we traverse.
258 When we encounter a PHI node, we walk its arguments to see if we
259 have an equivalence for the PHI argument. If so, then we replace
260 the argument.
262 Equivalences are looked up based on their value (think of it as
263 the RHS of an assignment). A value may be an SSA_NAME or an
264 invariant. We may have several SSA_NAMEs with the same value,
265 so with each value we have a list of SSA_NAMEs that have the
266 same value. */
269 /* Main structure for recording equivalences into our hash table. */
270 struct equiv_hash_elt
272 /* The value/key of this entry. */
273 tree value;
275 /* List of SSA_NAMEs which have the same value/key. */
276 vec<tree> equivalences;
279 /* Value to ssa name equivalence hashtable helpers. */
281 struct val_ssa_equiv_hasher
283 typedef equiv_hash_elt value_type;
284 typedef equiv_hash_elt compare_type;
285 static inline hashval_t hash (const value_type *);
286 static inline bool equal (const value_type *, const compare_type *);
287 static inline void remove (value_type *);
290 inline hashval_t
291 val_ssa_equiv_hasher::hash (const value_type *p)
293 tree const value = p->value;
294 return iterative_hash_expr (value, 0);
297 inline bool
298 val_ssa_equiv_hasher::equal (const value_type *p1, const compare_type *p2)
300 tree value1 = p1->value;
301 tree value2 = p2->value;
303 return operand_equal_p (value1, value2, 0);
306 /* Free an instance of equiv_hash_elt. */
308 inline void
309 val_ssa_equiv_hasher::remove (value_type *elt)
311 elt->equivalences.release ();
312 free (elt);
315 /* Global hash table implementing a mapping from invariant values
316 to a list of SSA_NAMEs which have the same value. We might be
317 able to reuse tree-vn for this code. */
318 static hash_table <val_ssa_equiv_hasher> val_ssa_equiv;
320 static void uncprop_into_successor_phis (basic_block);
322 /* Remove the most recently recorded equivalency for VALUE. */
324 static void
325 remove_equivalence (tree value)
327 struct equiv_hash_elt an_equiv_elt, *an_equiv_elt_p;
328 equiv_hash_elt **slot;
330 an_equiv_elt.value = value;
331 an_equiv_elt.equivalences.create (0);
333 slot = val_ssa_equiv.find_slot (&an_equiv_elt, NO_INSERT);
335 an_equiv_elt_p = *slot;
336 an_equiv_elt_p->equivalences.pop ();
339 /* Record EQUIVALENCE = VALUE into our hash table. */
341 static void
342 record_equiv (tree value, tree equivalence)
344 equiv_hash_elt *an_equiv_elt_p;
345 equiv_hash_elt **slot;
347 an_equiv_elt_p = XNEW (struct equiv_hash_elt);
348 an_equiv_elt_p->value = value;
349 an_equiv_elt_p->equivalences.create (0);
351 slot = val_ssa_equiv.find_slot (an_equiv_elt_p, INSERT);
353 if (*slot == NULL)
354 *slot = an_equiv_elt_p;
355 else
356 free (an_equiv_elt_p);
358 an_equiv_elt_p = *slot;
360 an_equiv_elt_p->equivalences.safe_push (equivalence);
363 class uncprop_dom_walker : public dom_walker
365 public:
366 uncprop_dom_walker (cdi_direction direction) : dom_walker (direction) {}
368 virtual void before_dom_children (basic_block);
369 virtual void after_dom_children (basic_block);
371 private:
373 /* As we enter each block we record the value for any edge equivalency
374 leading to this block. If no such edge equivalency exists, then we
375 record NULL. These equivalences are live until we leave the dominator
376 subtree rooted at the block where we record the equivalency. */
377 stack_vec<tree, 2> m_equiv_stack;
380 /* Main driver for un-cprop. */
382 static unsigned int
383 tree_ssa_uncprop (void)
385 basic_block bb;
387 associate_equivalences_with_edges ();
389 /* Create our global data structures. */
390 val_ssa_equiv.create (1024);
392 /* We're going to do a dominator walk, so ensure that we have
393 dominance information. */
394 calculate_dominance_info (CDI_DOMINATORS);
396 /* Recursively walk the dominator tree undoing unprofitable
397 constant/copy propagations. */
398 uncprop_dom_walker (CDI_DOMINATORS).walk (cfun->cfg->x_entry_block_ptr);
400 /* we just need to empty elements out of the hash table, and cleanup the
401 AUX field on the edges. */
402 val_ssa_equiv.dispose ();
403 FOR_EACH_BB (bb)
405 edge e;
406 edge_iterator ei;
408 FOR_EACH_EDGE (e, ei, bb->succs)
410 if (e->aux)
412 free (e->aux);
413 e->aux = NULL;
417 return 0;
421 /* We have finished processing the dominator children of BB, perform
422 any finalization actions in preparation for leaving this node in
423 the dominator tree. */
425 void
426 uncprop_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED)
428 /* Pop the topmost value off the equiv stack. */
429 tree value = m_equiv_stack.pop ();
431 /* If that value was non-null, then pop the topmost equivalency off
432 its equivalency stack. */
433 if (value != NULL)
434 remove_equivalence (value);
437 /* Unpropagate values from PHI nodes in successor blocks of BB. */
439 static void
440 uncprop_into_successor_phis (basic_block bb)
442 edge e;
443 edge_iterator ei;
445 /* For each successor edge, first temporarily record any equivalence
446 on that edge. Then unpropagate values in any PHI nodes at the
447 destination of the edge. Then remove the temporary equivalence. */
448 FOR_EACH_EDGE (e, ei, bb->succs)
450 gimple_seq phis = phi_nodes (e->dest);
451 gimple_stmt_iterator gsi;
453 /* If there are no PHI nodes in this destination, then there is
454 no sense in recording any equivalences. */
455 if (gimple_seq_empty_p (phis))
456 continue;
458 /* Record any equivalency associated with E. */
459 if (e->aux)
461 struct edge_equivalency *equiv = (struct edge_equivalency *) e->aux;
462 record_equiv (equiv->rhs, equiv->lhs);
465 /* Walk over the PHI nodes, unpropagating values. */
466 for (gsi = gsi_start (phis) ; !gsi_end_p (gsi); gsi_next (&gsi))
468 gimple phi = gsi_stmt (gsi);
469 tree arg = PHI_ARG_DEF (phi, e->dest_idx);
470 tree res = PHI_RESULT (phi);
471 equiv_hash_elt an_equiv_elt;
472 equiv_hash_elt **slot;
474 /* If the argument is not an invariant and can be potentially
475 coalesced with the result, then there's no point in
476 un-propagating the argument. */
477 if (!is_gimple_min_invariant (arg)
478 && gimple_can_coalesce_p (arg, res))
479 continue;
481 /* Lookup this argument's value in the hash table. */
482 an_equiv_elt.value = arg;
483 an_equiv_elt.equivalences.create (0);
484 slot = val_ssa_equiv.find_slot (&an_equiv_elt, NO_INSERT);
486 if (slot)
488 struct equiv_hash_elt *elt = *slot;
489 int j;
491 /* Walk every equivalence with the same value. If we find
492 one that can potentially coalesce with the PHI rsult,
493 then replace the value in the argument with its equivalent
494 SSA_NAME. Use the most recent equivalence as hopefully
495 that results in shortest lifetimes. */
496 for (j = elt->equivalences.length () - 1; j >= 0; j--)
498 tree equiv = elt->equivalences[j];
500 if (gimple_can_coalesce_p (equiv, res))
502 SET_PHI_ARG_DEF (phi, e->dest_idx, equiv);
503 break;
509 /* If we had an equivalence associated with this edge, remove it. */
510 if (e->aux)
512 struct edge_equivalency *equiv = (struct edge_equivalency *) e->aux;
513 remove_equivalence (equiv->rhs);
518 /* Ignoring loop backedges, if BB has precisely one incoming edge then
519 return that edge. Otherwise return NULL. */
520 static edge
521 single_incoming_edge_ignoring_loop_edges (basic_block bb)
523 edge retval = NULL;
524 edge e;
525 edge_iterator ei;
527 FOR_EACH_EDGE (e, ei, bb->preds)
529 /* A loop back edge can be identified by the destination of
530 the edge dominating the source of the edge. */
531 if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
532 continue;
534 /* If we have already seen a non-loop edge, then we must have
535 multiple incoming non-loop edges and thus we return NULL. */
536 if (retval)
537 return NULL;
539 /* This is the first non-loop incoming edge we have found. Record
540 it. */
541 retval = e;
544 return retval;
547 void
548 uncprop_dom_walker::before_dom_children (basic_block bb)
550 basic_block parent;
551 edge e;
552 bool recorded = false;
554 /* If this block is dominated by a single incoming edge and that edge
555 has an equivalency, then record the equivalency and push the
556 VALUE onto EQUIV_STACK. Else push a NULL entry on EQUIV_STACK. */
557 parent = get_immediate_dominator (CDI_DOMINATORS, bb);
558 if (parent)
560 e = single_incoming_edge_ignoring_loop_edges (bb);
562 if (e && e->src == parent && e->aux)
564 struct edge_equivalency *equiv = (struct edge_equivalency *) e->aux;
566 record_equiv (equiv->rhs, equiv->lhs);
567 m_equiv_stack.safe_push (equiv->rhs);
568 recorded = true;
572 if (!recorded)
573 m_equiv_stack.safe_push (NULL_TREE);
575 uncprop_into_successor_phis (bb);
578 static bool
579 gate_uncprop (void)
581 return flag_tree_dom != 0;
584 namespace {
586 const pass_data pass_data_uncprop =
588 GIMPLE_PASS, /* type */
589 "uncprop", /* name */
590 OPTGROUP_NONE, /* optinfo_flags */
591 true, /* has_gate */
592 true, /* has_execute */
593 TV_TREE_SSA_UNCPROP, /* tv_id */
594 ( PROP_cfg | PROP_ssa ), /* properties_required */
595 0, /* properties_provided */
596 0, /* properties_destroyed */
597 0, /* todo_flags_start */
598 TODO_verify_ssa, /* todo_flags_finish */
601 class pass_uncprop : public gimple_opt_pass
603 public:
604 pass_uncprop (gcc::context *ctxt)
605 : gimple_opt_pass (pass_data_uncprop, ctxt)
608 /* opt_pass methods: */
609 opt_pass * clone () { return new pass_uncprop (m_ctxt); }
610 bool gate () { return gate_uncprop (); }
611 unsigned int execute () { return tree_ssa_uncprop (); }
613 }; // class pass_uncprop
615 } // anon namespace
617 gimple_opt_pass *
618 make_pass_uncprop (gcc::context *ctxt)
620 return new pass_uncprop (ctxt);