PR fortran/64022
[official-gcc.git] / gcc / tree-ssa-uncprop.c
blob437f69d36c32f9bbfa4e4061054f51ab3bd76cb1
1 /* Routines for discovering and unpropagating edge equivalences.
2 Copyright (C) 2005-2015 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 "backend.h"
24 #include "tree.h"
25 #include "gimple.h"
26 #include "hard-reg-set.h"
27 #include "ssa.h"
28 #include "alias.h"
29 #include "fold-const.h"
30 #include "stor-layout.h"
31 #include "flags.h"
32 #include "tm_p.h"
33 #include "cfganal.h"
34 #include "internal-fn.h"
35 #include "gimple-iterator.h"
36 #include "tree-cfg.h"
37 #include "domwalk.h"
38 #include "tree-pass.h"
39 #include "tree-ssa-propagate.h"
40 #include "tree-hash-traits.h"
42 /* The basic structure describing an equivalency created by traversing
43 an edge. Traversing the edge effectively means that we can assume
44 that we've seen an assignment LHS = RHS. */
45 struct edge_equivalency
47 tree rhs;
48 tree lhs;
51 /* This routine finds and records edge equivalences for every edge
52 in the CFG.
54 When complete, each edge that creates an equivalency will have an
55 EDGE_EQUIVALENCY structure hanging off the edge's AUX field.
56 The caller is responsible for freeing the AUX fields. */
58 static void
59 associate_equivalences_with_edges (void)
61 basic_block bb;
63 /* Walk over each block. If the block ends with a control statement,
64 then it might create a useful equivalence. */
65 FOR_EACH_BB_FN (bb, cfun)
67 gimple_stmt_iterator gsi = gsi_last_bb (bb);
68 gimple stmt;
70 /* If the block does not end with a COND_EXPR or SWITCH_EXPR
71 then there is nothing to do. */
72 if (gsi_end_p (gsi))
73 continue;
75 stmt = gsi_stmt (gsi);
77 if (!stmt)
78 continue;
80 /* A COND_EXPR may create an equivalency in a variety of different
81 ways. */
82 if (gimple_code (stmt) == GIMPLE_COND)
84 edge true_edge;
85 edge false_edge;
86 struct edge_equivalency *equivalency;
87 enum tree_code code = gimple_cond_code (stmt);
89 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
91 /* Equality tests may create one or two equivalences. */
92 if (code == EQ_EXPR || code == NE_EXPR)
94 tree op0 = gimple_cond_lhs (stmt);
95 tree op1 = gimple_cond_rhs (stmt);
97 /* Special case comparing booleans against a constant as we
98 know the value of OP0 on both arms of the branch. i.e., we
99 can record an equivalence for OP0 rather than COND. */
100 if (TREE_CODE (op0) == SSA_NAME
101 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0)
102 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
103 && is_gimple_min_invariant (op1))
105 if (code == EQ_EXPR)
107 equivalency = XNEW (struct edge_equivalency);
108 equivalency->lhs = op0;
109 equivalency->rhs = (integer_zerop (op1)
110 ? boolean_false_node
111 : boolean_true_node);
112 true_edge->aux = equivalency;
114 equivalency = XNEW (struct edge_equivalency);
115 equivalency->lhs = op0;
116 equivalency->rhs = (integer_zerop (op1)
117 ? boolean_true_node
118 : boolean_false_node);
119 false_edge->aux = equivalency;
121 else
123 equivalency = XNEW (struct edge_equivalency);
124 equivalency->lhs = op0;
125 equivalency->rhs = (integer_zerop (op1)
126 ? boolean_true_node
127 : boolean_false_node);
128 true_edge->aux = equivalency;
130 equivalency = XNEW (struct edge_equivalency);
131 equivalency->lhs = op0;
132 equivalency->rhs = (integer_zerop (op1)
133 ? boolean_false_node
134 : boolean_true_node);
135 false_edge->aux = equivalency;
139 else if (TREE_CODE (op0) == SSA_NAME
140 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0)
141 && (is_gimple_min_invariant (op1)
142 || (TREE_CODE (op1) == SSA_NAME
143 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op1))))
145 /* For IEEE, -0.0 == 0.0, so we don't necessarily know
146 the sign of a variable compared against zero. If
147 we're honoring signed zeros, then we cannot record
148 this value unless we know that the value is nonzero. */
149 if (HONOR_SIGNED_ZEROS (op0)
150 && (TREE_CODE (op1) != REAL_CST
151 || REAL_VALUES_EQUAL (dconst0, TREE_REAL_CST (op1))))
152 continue;
154 equivalency = XNEW (struct edge_equivalency);
155 equivalency->lhs = op0;
156 equivalency->rhs = op1;
157 if (code == EQ_EXPR)
158 true_edge->aux = equivalency;
159 else
160 false_edge->aux = equivalency;
165 /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
168 /* For a SWITCH_EXPR, a case label which represents a single
169 value and which is the only case label which reaches the
170 target block creates an equivalence. */
171 else if (gimple_code (stmt) == GIMPLE_SWITCH)
173 gswitch *switch_stmt = as_a <gswitch *> (stmt);
174 tree cond = gimple_switch_index (switch_stmt);
176 if (TREE_CODE (cond) == SSA_NAME
177 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (cond))
179 int i, n_labels = gimple_switch_num_labels (switch_stmt);
180 tree *info = XCNEWVEC (tree, last_basic_block_for_fn (cfun));
182 /* Walk over the case label vector. Record blocks
183 which are reached by a single case label which represents
184 a single value. */
185 for (i = 0; i < n_labels; i++)
187 tree label = gimple_switch_label (switch_stmt, i);
188 basic_block bb = label_to_block (CASE_LABEL (label));
190 if (CASE_HIGH (label)
191 || !CASE_LOW (label)
192 || info[bb->index])
193 info[bb->index] = error_mark_node;
194 else
195 info[bb->index] = label;
198 /* Now walk over the blocks to determine which ones were
199 marked as being reached by a useful case label. */
200 for (i = 0; i < n_basic_blocks_for_fn (cfun); i++)
202 tree node = info[i];
204 if (node != NULL
205 && node != error_mark_node)
207 tree x = fold_convert (TREE_TYPE (cond), CASE_LOW (node));
208 struct edge_equivalency *equivalency;
210 /* Record an equivalency on the edge from BB to basic
211 block I. */
212 equivalency = XNEW (struct edge_equivalency);
213 equivalency->rhs = x;
214 equivalency->lhs = cond;
215 find_edge (bb, BASIC_BLOCK_FOR_FN (cfun, i))->aux =
216 equivalency;
219 free (info);
227 /* Translating out of SSA sometimes requires inserting copies and
228 constant initializations on edges to eliminate PHI nodes.
230 In some cases those copies and constant initializations are
231 redundant because the target already has the value on the
232 RHS of the assignment.
234 We previously tried to catch these cases after translating
235 out of SSA form. However, that code often missed cases. Worse
236 yet, the cases it missed were also often missed by the RTL
237 optimizers. Thus the resulting code had redundant instructions.
239 This pass attempts to detect these situations before translating
240 out of SSA form.
242 The key concept that this pass is built upon is that these
243 redundant copies and constant initializations often occur
244 due to constant/copy propagating equivalences resulting from
245 COND_EXPRs and SWITCH_EXPRs.
247 We want to do those propagations as they can sometimes allow
248 the SSA optimizers to do a better job. However, in the cases
249 where such propagations do not result in further optimization,
250 we would like to "undo" the propagation to avoid the redundant
251 copies and constant initializations.
253 This pass works by first associating equivalences with edges in
254 the CFG. For example, the edge leading from a SWITCH_EXPR to
255 its associated CASE_LABEL will have an equivalency between
256 SWITCH_COND and the value in the case label.
258 Once we have found the edge equivalences, we proceed to walk
259 the CFG in dominator order. As we traverse edges we record
260 equivalences associated with those edges we traverse.
262 When we encounter a PHI node, we walk its arguments to see if we
263 have an equivalence for the PHI argument. If so, then we replace
264 the argument.
266 Equivalences are looked up based on their value (think of it as
267 the RHS of an assignment). A value may be an SSA_NAME or an
268 invariant. We may have several SSA_NAMEs with the same value,
269 so with each value we have a list of SSA_NAMEs that have the
270 same value. */
273 /* Main structure for recording equivalences into our hash table. */
274 struct equiv_hash_elt
276 /* The value/key of this entry. */
277 tree value;
279 /* List of SSA_NAMEs which have the same value/key. */
280 vec<tree> equivalences;
283 /* Value to ssa name equivalence hashtable helpers. */
285 struct val_ssa_equiv_hash_traits : simple_hashmap_traits <tree_operand_hash>
287 template<typename T> static inline void remove (T &);
290 /* Free an instance of equiv_hash_elt. */
292 template<typename T>
293 inline void
294 val_ssa_equiv_hash_traits::remove (T &elt)
296 elt.m_value.release ();
299 /* Global hash table implementing a mapping from invariant values
300 to a list of SSA_NAMEs which have the same value. We might be
301 able to reuse tree-vn for this code. */
302 static hash_map<tree, vec<tree>, val_ssa_equiv_hash_traits> *val_ssa_equiv;
304 static void uncprop_into_successor_phis (basic_block);
306 /* Remove the most recently recorded equivalency for VALUE. */
308 static void
309 remove_equivalence (tree value)
311 val_ssa_equiv->get (value)->pop ();
314 /* Record EQUIVALENCE = VALUE into our hash table. */
316 static void
317 record_equiv (tree value, tree equivalence)
319 val_ssa_equiv->get_or_insert (value).safe_push (equivalence);
322 class uncprop_dom_walker : public dom_walker
324 public:
325 uncprop_dom_walker (cdi_direction direction) : dom_walker (direction) {}
327 virtual void before_dom_children (basic_block);
328 virtual void after_dom_children (basic_block);
330 private:
332 /* As we enter each block we record the value for any edge equivalency
333 leading to this block. If no such edge equivalency exists, then we
334 record NULL. These equivalences are live until we leave the dominator
335 subtree rooted at the block where we record the equivalency. */
336 auto_vec<tree, 2> m_equiv_stack;
339 /* We have finished processing the dominator children of BB, perform
340 any finalization actions in preparation for leaving this node in
341 the dominator tree. */
343 void
344 uncprop_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED)
346 /* Pop the topmost value off the equiv stack. */
347 tree value = m_equiv_stack.pop ();
349 /* If that value was non-null, then pop the topmost equivalency off
350 its equivalency stack. */
351 if (value != NULL)
352 remove_equivalence (value);
355 /* Unpropagate values from PHI nodes in successor blocks of BB. */
357 static void
358 uncprop_into_successor_phis (basic_block bb)
360 edge e;
361 edge_iterator ei;
363 /* For each successor edge, first temporarily record any equivalence
364 on that edge. Then unpropagate values in any PHI nodes at the
365 destination of the edge. Then remove the temporary equivalence. */
366 FOR_EACH_EDGE (e, ei, bb->succs)
368 gimple_seq phis = phi_nodes (e->dest);
369 gimple_stmt_iterator gsi;
371 /* If there are no PHI nodes in this destination, then there is
372 no sense in recording any equivalences. */
373 if (gimple_seq_empty_p (phis))
374 continue;
376 /* Record any equivalency associated with E. */
377 if (e->aux)
379 struct edge_equivalency *equiv = (struct edge_equivalency *) e->aux;
380 record_equiv (equiv->rhs, equiv->lhs);
383 /* Walk over the PHI nodes, unpropagating values. */
384 for (gsi = gsi_start (phis) ; !gsi_end_p (gsi); gsi_next (&gsi))
386 gimple phi = gsi_stmt (gsi);
387 tree arg = PHI_ARG_DEF (phi, e->dest_idx);
388 tree res = PHI_RESULT (phi);
390 /* If the argument is not an invariant and can be potentially
391 coalesced with the result, then there's no point in
392 un-propagating the argument. */
393 if (!is_gimple_min_invariant (arg)
394 && gimple_can_coalesce_p (arg, res))
395 continue;
397 /* Lookup this argument's value in the hash table. */
398 vec<tree> *equivalences = val_ssa_equiv->get (arg);
399 if (equivalences)
401 /* Walk every equivalence with the same value. If we find
402 one that can potentially coalesce with the PHI rsult,
403 then replace the value in the argument with its equivalent
404 SSA_NAME. Use the most recent equivalence as hopefully
405 that results in shortest lifetimes. */
406 for (int j = equivalences->length () - 1; j >= 0; j--)
408 tree equiv = (*equivalences)[j];
410 if (gimple_can_coalesce_p (equiv, res))
412 SET_PHI_ARG_DEF (phi, e->dest_idx, equiv);
413 break;
419 /* If we had an equivalence associated with this edge, remove it. */
420 if (e->aux)
422 struct edge_equivalency *equiv = (struct edge_equivalency *) e->aux;
423 remove_equivalence (equiv->rhs);
428 /* Ignoring loop backedges, if BB has precisely one incoming edge then
429 return that edge. Otherwise return NULL. */
430 static edge
431 single_incoming_edge_ignoring_loop_edges (basic_block bb)
433 edge retval = NULL;
434 edge e;
435 edge_iterator ei;
437 FOR_EACH_EDGE (e, ei, bb->preds)
439 /* A loop back edge can be identified by the destination of
440 the edge dominating the source of the edge. */
441 if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
442 continue;
444 /* If we have already seen a non-loop edge, then we must have
445 multiple incoming non-loop edges and thus we return NULL. */
446 if (retval)
447 return NULL;
449 /* This is the first non-loop incoming edge we have found. Record
450 it. */
451 retval = e;
454 return retval;
457 void
458 uncprop_dom_walker::before_dom_children (basic_block bb)
460 basic_block parent;
461 edge e;
462 bool recorded = false;
464 /* If this block is dominated by a single incoming edge and that edge
465 has an equivalency, then record the equivalency and push the
466 VALUE onto EQUIV_STACK. Else push a NULL entry on EQUIV_STACK. */
467 parent = get_immediate_dominator (CDI_DOMINATORS, bb);
468 if (parent)
470 e = single_incoming_edge_ignoring_loop_edges (bb);
472 if (e && e->src == parent && e->aux)
474 struct edge_equivalency *equiv = (struct edge_equivalency *) e->aux;
476 record_equiv (equiv->rhs, equiv->lhs);
477 m_equiv_stack.safe_push (equiv->rhs);
478 recorded = true;
482 if (!recorded)
483 m_equiv_stack.safe_push (NULL_TREE);
485 uncprop_into_successor_phis (bb);
488 namespace {
490 const pass_data pass_data_uncprop =
492 GIMPLE_PASS, /* type */
493 "uncprop", /* name */
494 OPTGROUP_NONE, /* optinfo_flags */
495 TV_TREE_SSA_UNCPROP, /* tv_id */
496 ( PROP_cfg | PROP_ssa ), /* properties_required */
497 0, /* properties_provided */
498 0, /* properties_destroyed */
499 0, /* todo_flags_start */
500 0, /* todo_flags_finish */
503 class pass_uncprop : public gimple_opt_pass
505 public:
506 pass_uncprop (gcc::context *ctxt)
507 : gimple_opt_pass (pass_data_uncprop, ctxt)
510 /* opt_pass methods: */
511 opt_pass * clone () { return new pass_uncprop (m_ctxt); }
512 virtual bool gate (function *) { return flag_tree_dom != 0; }
513 virtual unsigned int execute (function *);
515 }; // class pass_uncprop
517 unsigned int
518 pass_uncprop::execute (function *fun)
520 basic_block bb;
522 associate_equivalences_with_edges ();
524 /* Create our global data structures. */
525 val_ssa_equiv
526 = new hash_map<tree, vec<tree>, val_ssa_equiv_hash_traits> (1024);
528 /* We're going to do a dominator walk, so ensure that we have
529 dominance information. */
530 calculate_dominance_info (CDI_DOMINATORS);
532 /* Recursively walk the dominator tree undoing unprofitable
533 constant/copy propagations. */
534 uncprop_dom_walker (CDI_DOMINATORS).walk (fun->cfg->x_entry_block_ptr);
536 /* we just need to empty elements out of the hash table, and cleanup the
537 AUX field on the edges. */
538 delete val_ssa_equiv;
539 val_ssa_equiv = NULL;
540 FOR_EACH_BB_FN (bb, fun)
542 edge e;
543 edge_iterator ei;
545 FOR_EACH_EDGE (e, ei, bb->succs)
547 if (e->aux)
549 free (e->aux);
550 e->aux = NULL;
554 return 0;
557 } // anon namespace
559 gimple_opt_pass *
560 make_pass_uncprop (gcc::context *ctxt)
562 return new pass_uncprop (ctxt);