Use int64 on time and clock for x32
[official-gcc.git] / gcc / tree-ssa-uncprop.c
blob44194b83ae687944724a2f29662e196b421d6a26
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 "hash-table.h"
31 #include "tree-ssa-alias.h"
32 #include "internal-fn.h"
33 #include "gimple-expr.h"
34 #include "is-a.h"
35 #include "gimple.h"
36 #include "gimple-iterator.h"
37 #include "gimple-ssa.h"
38 #include "tree-cfg.h"
39 #include "tree-phinodes.h"
40 #include "ssa-iterators.h"
41 #include "domwalk.h"
42 #include "tree-pass.h"
43 #include "tree-ssa-propagate.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 gimple_stmt_iterator gsi = gsi_last_bb (bb);
71 gimple stmt;
73 /* If the block does not end with a COND_EXPR or SWITCH_EXPR
74 then there is nothing to do. */
75 if (gsi_end_p (gsi))
76 continue;
78 stmt = gsi_stmt (gsi);
80 if (!stmt)
81 continue;
83 /* A COND_EXPR may create an equivalency in a variety of different
84 ways. */
85 if (gimple_code (stmt) == GIMPLE_COND)
87 edge true_edge;
88 edge false_edge;
89 struct edge_equivalency *equivalency;
90 enum tree_code code = gimple_cond_code (stmt);
92 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
94 /* Equality tests may create one or two equivalences. */
95 if (code == EQ_EXPR || code == NE_EXPR)
97 tree op0 = gimple_cond_lhs (stmt);
98 tree op1 = gimple_cond_rhs (stmt);
100 /* Special case comparing booleans against a constant as we
101 know the value of OP0 on both arms of the branch. i.e., we
102 can record an equivalence for OP0 rather than COND. */
103 if (TREE_CODE (op0) == SSA_NAME
104 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0)
105 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
106 && is_gimple_min_invariant (op1))
108 if (code == EQ_EXPR)
110 equivalency = XNEW (struct edge_equivalency);
111 equivalency->lhs = op0;
112 equivalency->rhs = (integer_zerop (op1)
113 ? boolean_false_node
114 : boolean_true_node);
115 true_edge->aux = equivalency;
117 equivalency = XNEW (struct edge_equivalency);
118 equivalency->lhs = op0;
119 equivalency->rhs = (integer_zerop (op1)
120 ? boolean_true_node
121 : boolean_false_node);
122 false_edge->aux = equivalency;
124 else
126 equivalency = XNEW (struct edge_equivalency);
127 equivalency->lhs = op0;
128 equivalency->rhs = (integer_zerop (op1)
129 ? boolean_true_node
130 : boolean_false_node);
131 true_edge->aux = equivalency;
133 equivalency = XNEW (struct edge_equivalency);
134 equivalency->lhs = op0;
135 equivalency->rhs = (integer_zerop (op1)
136 ? boolean_false_node
137 : boolean_true_node);
138 false_edge->aux = equivalency;
142 else if (TREE_CODE (op0) == SSA_NAME
143 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0)
144 && (is_gimple_min_invariant (op1)
145 || (TREE_CODE (op1) == SSA_NAME
146 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op1))))
148 /* For IEEE, -0.0 == 0.0, so we don't necessarily know
149 the sign of a variable compared against zero. If
150 we're honoring signed zeros, then we cannot record
151 this value unless we know that the value is nonzero. */
152 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op0)))
153 && (TREE_CODE (op1) != REAL_CST
154 || REAL_VALUES_EQUAL (dconst0, TREE_REAL_CST (op1))))
155 continue;
157 equivalency = XNEW (struct edge_equivalency);
158 equivalency->lhs = op0;
159 equivalency->rhs = op1;
160 if (code == EQ_EXPR)
161 true_edge->aux = equivalency;
162 else
163 false_edge->aux = equivalency;
168 /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
171 /* For a SWITCH_EXPR, a case label which represents a single
172 value and which is the only case label which reaches the
173 target block creates an equivalence. */
174 else if (gimple_code (stmt) == GIMPLE_SWITCH)
176 tree cond = gimple_switch_index (stmt);
178 if (TREE_CODE (cond) == SSA_NAME
179 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (cond))
181 int i, n_labels = gimple_switch_num_labels (stmt);
182 tree *info = XCNEWVEC (tree, last_basic_block);
184 /* Walk over the case label vector. Record blocks
185 which are reached by a single case label which represents
186 a single value. */
187 for (i = 0; i < n_labels; i++)
189 tree label = gimple_switch_label (stmt, i);
190 basic_block bb = label_to_block (CASE_LABEL (label));
192 if (CASE_HIGH (label)
193 || !CASE_LOW (label)
194 || info[bb->index])
195 info[bb->index] = error_mark_node;
196 else
197 info[bb->index] = label;
200 /* Now walk over the blocks to determine which ones were
201 marked as being reached by a useful case label. */
202 for (i = 0; i < n_basic_blocks_for_fn (cfun); i++)
204 tree node = info[i];
206 if (node != NULL
207 && node != error_mark_node)
209 tree x = fold_convert (TREE_TYPE (cond), CASE_LOW (node));
210 struct edge_equivalency *equivalency;
212 /* Record an equivalency on the edge from BB to basic
213 block I. */
214 equivalency = XNEW (struct edge_equivalency);
215 equivalency->rhs = x;
216 equivalency->lhs = cond;
217 find_edge (bb, BASIC_BLOCK (i))->aux = equivalency;
220 free (info);
228 /* Translating out of SSA sometimes requires inserting copies and
229 constant initializations on edges to eliminate PHI nodes.
231 In some cases those copies and constant initializations are
232 redundant because the target already has the value on the
233 RHS of the assignment.
235 We previously tried to catch these cases after translating
236 out of SSA form. However, that code often missed cases. Worse
237 yet, the cases it missed were also often missed by the RTL
238 optimizers. Thus the resulting code had redundant instructions.
240 This pass attempts to detect these situations before translating
241 out of SSA form.
243 The key concept that this pass is built upon is that these
244 redundant copies and constant initializations often occur
245 due to constant/copy propagating equivalences resulting from
246 COND_EXPRs and SWITCH_EXPRs.
248 We want to do those propagations as they can sometimes allow
249 the SSA optimizers to do a better job. However, in the cases
250 where such propagations do not result in further optimization,
251 we would like to "undo" the propagation to avoid the redundant
252 copies and constant initializations.
254 This pass works by first associating equivalences with edges in
255 the CFG. For example, the edge leading from a SWITCH_EXPR to
256 its associated CASE_LABEL will have an equivalency between
257 SWITCH_COND and the value in the case label.
259 Once we have found the edge equivalences, we proceed to walk
260 the CFG in dominator order. As we traverse edges we record
261 equivalences associated with those edges we traverse.
263 When we encounter a PHI node, we walk its arguments to see if we
264 have an equivalence for the PHI argument. If so, then we replace
265 the argument.
267 Equivalences are looked up based on their value (think of it as
268 the RHS of an assignment). A value may be an SSA_NAME or an
269 invariant. We may have several SSA_NAMEs with the same value,
270 so with each value we have a list of SSA_NAMEs that have the
271 same value. */
274 /* Main structure for recording equivalences into our hash table. */
275 struct equiv_hash_elt
277 /* The value/key of this entry. */
278 tree value;
280 /* List of SSA_NAMEs which have the same value/key. */
281 vec<tree> equivalences;
284 /* Value to ssa name equivalence hashtable helpers. */
286 struct val_ssa_equiv_hasher
288 typedef equiv_hash_elt value_type;
289 typedef equiv_hash_elt compare_type;
290 static inline hashval_t hash (const value_type *);
291 static inline bool equal (const value_type *, const compare_type *);
292 static inline void remove (value_type *);
295 inline hashval_t
296 val_ssa_equiv_hasher::hash (const value_type *p)
298 tree const value = p->value;
299 return iterative_hash_expr (value, 0);
302 inline bool
303 val_ssa_equiv_hasher::equal (const value_type *p1, const compare_type *p2)
305 tree value1 = p1->value;
306 tree value2 = p2->value;
308 return operand_equal_p (value1, value2, 0);
311 /* Free an instance of equiv_hash_elt. */
313 inline void
314 val_ssa_equiv_hasher::remove (value_type *elt)
316 elt->equivalences.release ();
317 free (elt);
320 /* Global hash table implementing a mapping from invariant values
321 to a list of SSA_NAMEs which have the same value. We might be
322 able to reuse tree-vn for this code. */
323 static hash_table <val_ssa_equiv_hasher> val_ssa_equiv;
325 static void uncprop_into_successor_phis (basic_block);
327 /* Remove the most recently recorded equivalency for VALUE. */
329 static void
330 remove_equivalence (tree value)
332 struct equiv_hash_elt an_equiv_elt, *an_equiv_elt_p;
333 equiv_hash_elt **slot;
335 an_equiv_elt.value = value;
336 an_equiv_elt.equivalences.create (0);
338 slot = val_ssa_equiv.find_slot (&an_equiv_elt, NO_INSERT);
340 an_equiv_elt_p = *slot;
341 an_equiv_elt_p->equivalences.pop ();
344 /* Record EQUIVALENCE = VALUE into our hash table. */
346 static void
347 record_equiv (tree value, tree equivalence)
349 equiv_hash_elt *an_equiv_elt_p;
350 equiv_hash_elt **slot;
352 an_equiv_elt_p = XNEW (struct equiv_hash_elt);
353 an_equiv_elt_p->value = value;
354 an_equiv_elt_p->equivalences.create (0);
356 slot = val_ssa_equiv.find_slot (an_equiv_elt_p, INSERT);
358 if (*slot == NULL)
359 *slot = an_equiv_elt_p;
360 else
361 free (an_equiv_elt_p);
363 an_equiv_elt_p = *slot;
365 an_equiv_elt_p->equivalences.safe_push (equivalence);
368 class uncprop_dom_walker : public dom_walker
370 public:
371 uncprop_dom_walker (cdi_direction direction) : dom_walker (direction) {}
373 virtual void before_dom_children (basic_block);
374 virtual void after_dom_children (basic_block);
376 private:
378 /* As we enter each block we record the value for any edge equivalency
379 leading to this block. If no such edge equivalency exists, then we
380 record NULL. These equivalences are live until we leave the dominator
381 subtree rooted at the block where we record the equivalency. */
382 stack_vec<tree, 2> m_equiv_stack;
385 /* Main driver for un-cprop. */
387 static unsigned int
388 tree_ssa_uncprop (void)
390 basic_block bb;
392 associate_equivalences_with_edges ();
394 /* Create our global data structures. */
395 val_ssa_equiv.create (1024);
397 /* We're going to do a dominator walk, so ensure that we have
398 dominance information. */
399 calculate_dominance_info (CDI_DOMINATORS);
401 /* Recursively walk the dominator tree undoing unprofitable
402 constant/copy propagations. */
403 uncprop_dom_walker (CDI_DOMINATORS).walk (cfun->cfg->x_entry_block_ptr);
405 /* we just need to empty elements out of the hash table, and cleanup the
406 AUX field on the edges. */
407 val_ssa_equiv.dispose ();
408 FOR_EACH_BB (bb)
410 edge e;
411 edge_iterator ei;
413 FOR_EACH_EDGE (e, ei, bb->succs)
415 if (e->aux)
417 free (e->aux);
418 e->aux = NULL;
422 return 0;
426 /* We have finished processing the dominator children of BB, perform
427 any finalization actions in preparation for leaving this node in
428 the dominator tree. */
430 void
431 uncprop_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED)
433 /* Pop the topmost value off the equiv stack. */
434 tree value = m_equiv_stack.pop ();
436 /* If that value was non-null, then pop the topmost equivalency off
437 its equivalency stack. */
438 if (value != NULL)
439 remove_equivalence (value);
442 /* Unpropagate values from PHI nodes in successor blocks of BB. */
444 static void
445 uncprop_into_successor_phis (basic_block bb)
447 edge e;
448 edge_iterator ei;
450 /* For each successor edge, first temporarily record any equivalence
451 on that edge. Then unpropagate values in any PHI nodes at the
452 destination of the edge. Then remove the temporary equivalence. */
453 FOR_EACH_EDGE (e, ei, bb->succs)
455 gimple_seq phis = phi_nodes (e->dest);
456 gimple_stmt_iterator gsi;
458 /* If there are no PHI nodes in this destination, then there is
459 no sense in recording any equivalences. */
460 if (gimple_seq_empty_p (phis))
461 continue;
463 /* Record any equivalency associated with E. */
464 if (e->aux)
466 struct edge_equivalency *equiv = (struct edge_equivalency *) e->aux;
467 record_equiv (equiv->rhs, equiv->lhs);
470 /* Walk over the PHI nodes, unpropagating values. */
471 for (gsi = gsi_start (phis) ; !gsi_end_p (gsi); gsi_next (&gsi))
473 gimple phi = gsi_stmt (gsi);
474 tree arg = PHI_ARG_DEF (phi, e->dest_idx);
475 tree res = PHI_RESULT (phi);
476 equiv_hash_elt an_equiv_elt;
477 equiv_hash_elt **slot;
479 /* If the argument is not an invariant and can be potentially
480 coalesced with the result, then there's no point in
481 un-propagating the argument. */
482 if (!is_gimple_min_invariant (arg)
483 && gimple_can_coalesce_p (arg, res))
484 continue;
486 /* Lookup this argument's value in the hash table. */
487 an_equiv_elt.value = arg;
488 an_equiv_elt.equivalences.create (0);
489 slot = val_ssa_equiv.find_slot (&an_equiv_elt, NO_INSERT);
491 if (slot)
493 struct equiv_hash_elt *elt = *slot;
494 int j;
496 /* Walk every equivalence with the same value. If we find
497 one that can potentially coalesce with the PHI rsult,
498 then replace the value in the argument with its equivalent
499 SSA_NAME. Use the most recent equivalence as hopefully
500 that results in shortest lifetimes. */
501 for (j = elt->equivalences.length () - 1; j >= 0; j--)
503 tree equiv = elt->equivalences[j];
505 if (gimple_can_coalesce_p (equiv, res))
507 SET_PHI_ARG_DEF (phi, e->dest_idx, equiv);
508 break;
514 /* If we had an equivalence associated with this edge, remove it. */
515 if (e->aux)
517 struct edge_equivalency *equiv = (struct edge_equivalency *) e->aux;
518 remove_equivalence (equiv->rhs);
523 /* Ignoring loop backedges, if BB has precisely one incoming edge then
524 return that edge. Otherwise return NULL. */
525 static edge
526 single_incoming_edge_ignoring_loop_edges (basic_block bb)
528 edge retval = NULL;
529 edge e;
530 edge_iterator ei;
532 FOR_EACH_EDGE (e, ei, bb->preds)
534 /* A loop back edge can be identified by the destination of
535 the edge dominating the source of the edge. */
536 if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
537 continue;
539 /* If we have already seen a non-loop edge, then we must have
540 multiple incoming non-loop edges and thus we return NULL. */
541 if (retval)
542 return NULL;
544 /* This is the first non-loop incoming edge we have found. Record
545 it. */
546 retval = e;
549 return retval;
552 void
553 uncprop_dom_walker::before_dom_children (basic_block bb)
555 basic_block parent;
556 edge e;
557 bool recorded = false;
559 /* If this block is dominated by a single incoming edge and that edge
560 has an equivalency, then record the equivalency and push the
561 VALUE onto EQUIV_STACK. Else push a NULL entry on EQUIV_STACK. */
562 parent = get_immediate_dominator (CDI_DOMINATORS, bb);
563 if (parent)
565 e = single_incoming_edge_ignoring_loop_edges (bb);
567 if (e && e->src == parent && e->aux)
569 struct edge_equivalency *equiv = (struct edge_equivalency *) e->aux;
571 record_equiv (equiv->rhs, equiv->lhs);
572 m_equiv_stack.safe_push (equiv->rhs);
573 recorded = true;
577 if (!recorded)
578 m_equiv_stack.safe_push (NULL_TREE);
580 uncprop_into_successor_phis (bb);
583 static bool
584 gate_uncprop (void)
586 return flag_tree_dom != 0;
589 namespace {
591 const pass_data pass_data_uncprop =
593 GIMPLE_PASS, /* type */
594 "uncprop", /* name */
595 OPTGROUP_NONE, /* optinfo_flags */
596 true, /* has_gate */
597 true, /* has_execute */
598 TV_TREE_SSA_UNCPROP, /* tv_id */
599 ( PROP_cfg | PROP_ssa ), /* properties_required */
600 0, /* properties_provided */
601 0, /* properties_destroyed */
602 0, /* todo_flags_start */
603 TODO_verify_ssa, /* todo_flags_finish */
606 class pass_uncprop : public gimple_opt_pass
608 public:
609 pass_uncprop (gcc::context *ctxt)
610 : gimple_opt_pass (pass_data_uncprop, ctxt)
613 /* opt_pass methods: */
614 opt_pass * clone () { return new pass_uncprop (m_ctxt); }
615 bool gate () { return gate_uncprop (); }
616 unsigned int execute () { return tree_ssa_uncprop (); }
618 }; // class pass_uncprop
620 } // anon namespace
622 gimple_opt_pass *
623 make_pass_uncprop (gcc::context *ctxt)
625 return new pass_uncprop (ctxt);